1 /* 2 * Copyright 2013 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #include "SkArithmeticMode.h" 9 #include "SkColorPriv.h" 10 #include "SkFlattenableBuffers.h" 11 #include "SkString.h" 12 #include "SkUnPreMultiply.h" 13 #if SK_SUPPORT_GPU 14 #include "GrContext.h" 15 #include "GrCoordTransform.h" 16 #include "gl/GrGLEffect.h" 17 #include "GrTBackendEffectFactory.h" 18 #include "SkImageFilterUtils.h" 19 #endif 20 21 static const bool gUseUnpremul = false; 22 23 class SkArithmeticMode_scalar : public SkXfermode { 24 public: 25 SkArithmeticMode_scalar(SkScalar k1, SkScalar k2, SkScalar k3, SkScalar k4) { 26 fK[0] = k1; 27 fK[1] = k2; 28 fK[2] = k3; 29 fK[3] = k4; 30 } 31 32 virtual void xfer32(SkPMColor dst[], const SkPMColor src[], int count, 33 const SkAlpha aa[]) const SK_OVERRIDE; 34 35 SK_DEVELOPER_TO_STRING() 36 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkArithmeticMode_scalar) 37 38 #if SK_SUPPORT_GPU 39 virtual bool asNewEffect(GrEffectRef** effect, GrTexture* background) const SK_OVERRIDE; 40 #endif 41 42 private: 43 SkArithmeticMode_scalar(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { 44 fK[0] = buffer.readScalar(); 45 fK[1] = buffer.readScalar(); 46 fK[2] = buffer.readScalar(); 47 fK[3] = buffer.readScalar(); 48 } 49 50 virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE { 51 INHERITED::flatten(buffer); 52 buffer.writeScalar(fK[0]); 53 buffer.writeScalar(fK[1]); 54 buffer.writeScalar(fK[2]); 55 buffer.writeScalar(fK[3]); 56 } 57 SkScalar fK[4]; 58 59 typedef SkXfermode INHERITED; 60 }; 61 62 static int pinToByte(int value) { 63 if (value < 0) { 64 value = 0; 65 } else if (value > 255) { 66 value = 255; 67 } 68 return value; 69 } 70 71 static int arith(SkScalar k1, SkScalar k2, SkScalar k3, SkScalar k4, 72 int src, int dst) { 73 SkScalar result = SkScalarMul(k1, src * dst) + 74 SkScalarMul(k2, src) + 75 SkScalarMul(k3, dst) + 76 k4; 77 int res = SkScalarRoundToInt(result); 78 return pinToByte(res); 79 } 80 81 static int blend(int src, int dst, int scale) { 82 return dst + ((src - dst) * scale >> 8); 83 } 84 85 static bool needsUnpremul(int alpha) { 86 return 0 != alpha && 0xFF != alpha; 87 } 88 89 void SkArithmeticMode_scalar::xfer32(SkPMColor dst[], const SkPMColor src[], 90 int count, const SkAlpha aaCoverage[]) const { 91 SkScalar k1 = fK[0] / 255; 92 SkScalar k2 = fK[1]; 93 SkScalar k3 = fK[2]; 94 SkScalar k4 = fK[3] * 255; 95 96 for (int i = 0; i < count; ++i) { 97 if ((NULL == aaCoverage) || aaCoverage[i]) { 98 SkPMColor sc = src[i]; 99 SkPMColor dc = dst[i]; 100 101 int a, r, g, b; 102 103 if (gUseUnpremul) { 104 int sa = SkGetPackedA32(sc); 105 int da = SkGetPackedA32(dc); 106 107 int srcNeedsUnpremul = needsUnpremul(sa); 108 int dstNeedsUnpremul = needsUnpremul(da); 109 110 if (!srcNeedsUnpremul && !dstNeedsUnpremul) { 111 a = arith(k1, k2, k3, k4, sa, da); 112 r = arith(k1, k2, k3, k4, SkGetPackedR32(sc), SkGetPackedR32(dc)); 113 g = arith(k1, k2, k3, k4, SkGetPackedG32(sc), SkGetPackedG32(dc)); 114 b = arith(k1, k2, k3, k4, SkGetPackedB32(sc), SkGetPackedB32(dc)); 115 } else { 116 int sr = SkGetPackedR32(sc); 117 int sg = SkGetPackedG32(sc); 118 int sb = SkGetPackedB32(sc); 119 if (srcNeedsUnpremul) { 120 SkUnPreMultiply::Scale scale = SkUnPreMultiply::GetScale(sa); 121 sr = SkUnPreMultiply::ApplyScale(scale, sr); 122 sg = SkUnPreMultiply::ApplyScale(scale, sg); 123 sb = SkUnPreMultiply::ApplyScale(scale, sb); 124 } 125 126 int dr = SkGetPackedR32(dc); 127 int dg = SkGetPackedG32(dc); 128 int db = SkGetPackedB32(dc); 129 if (dstNeedsUnpremul) { 130 SkUnPreMultiply::Scale scale = SkUnPreMultiply::GetScale(da); 131 dr = SkUnPreMultiply::ApplyScale(scale, dr); 132 dg = SkUnPreMultiply::ApplyScale(scale, dg); 133 db = SkUnPreMultiply::ApplyScale(scale, db); 134 } 135 136 a = arith(k1, k2, k3, k4, sa, da); 137 r = arith(k1, k2, k3, k4, sr, dr); 138 g = arith(k1, k2, k3, k4, sg, dg); 139 b = arith(k1, k2, k3, k4, sb, db); 140 } 141 } else { 142 a = arith(k1, k2, k3, k4, SkGetPackedA32(sc), SkGetPackedA32(dc)); 143 r = arith(k1, k2, k3, k4, SkGetPackedR32(sc), SkGetPackedR32(dc)); 144 r = SkMin32(r, a); 145 g = arith(k1, k2, k3, k4, SkGetPackedG32(sc), SkGetPackedG32(dc)); 146 g = SkMin32(g, a); 147 b = arith(k1, k2, k3, k4, SkGetPackedB32(sc), SkGetPackedB32(dc)); 148 b = SkMin32(b, a); 149 } 150 151 // apply antialias coverage if necessary 152 if (aaCoverage && 0xFF != aaCoverage[i]) { 153 int scale = aaCoverage[i] + (aaCoverage[i] >> 7); 154 a = blend(a, SkGetPackedA32(sc), scale); 155 r = blend(r, SkGetPackedR32(sc), scale); 156 g = blend(g, SkGetPackedG32(sc), scale); 157 b = blend(b, SkGetPackedB32(sc), scale); 158 } 159 160 // turn the result back into premul 161 if (gUseUnpremul && (0xFF != a)) { 162 int scale = a + (a >> 7); 163 r = SkAlphaMul(r, scale); 164 g = SkAlphaMul(g, scale); 165 b = SkAlphaMul(b, scale); 166 } 167 dst[i] = SkPackARGB32(a, r, g, b); 168 } 169 } 170 } 171 172 #ifdef SK_DEVELOPER 173 void SkArithmeticMode_scalar::toString(SkString* str) const { 174 str->append("SkArithmeticMode_scalar: "); 175 for (int i = 0; i < 4; ++i) { 176 str->appendScalar(fK[i]); 177 if (i < 3) { 178 str->append(" "); 179 } 180 } 181 } 182 #endif 183 184 /////////////////////////////////////////////////////////////////////////////// 185 186 static bool fitsInBits(SkScalar x, int bits) { 187 #ifdef SK_SCALAR_IS_FIXED 188 x = SkAbs32(x); 189 x += 1 << 7; 190 x >>= 8; 191 return x < (1 << (bits - 1)); 192 #else 193 return SkScalarAbs(x) < (1 << (bits - 1)); 194 #endif 195 } 196 197 #if 0 // UNUSED 198 static int32_t toDot8(SkScalar x) { 199 #ifdef SK_SCALAR_IS_FIXED 200 x += 1 << 7; 201 x >>= 8; 202 return x; 203 #else 204 return (int32_t)(x * 256); 205 #endif 206 } 207 #endif 208 209 SkXfermode* SkArithmeticMode::Create(SkScalar k1, SkScalar k2, 210 SkScalar k3, SkScalar k4) { 211 if (fitsInBits(k1, 8) && fitsInBits(k2, 16) && 212 fitsInBits(k2, 16) && fitsInBits(k2, 24)) { 213 214 #if 0 // UNUSED 215 int32_t i1 = toDot8(k1); 216 int32_t i2 = toDot8(k2); 217 int32_t i3 = toDot8(k3); 218 int32_t i4 = toDot8(k4); 219 if (i1) { 220 return SkNEW_ARGS(SkArithmeticMode_quad, (i1, i2, i3, i4)); 221 } 222 if (0 == i2) { 223 return SkNEW_ARGS(SkArithmeticMode_dst, (i3, i4)); 224 } 225 if (0 == i3) { 226 return SkNEW_ARGS(SkArithmeticMode_src, (i2, i4)); 227 } 228 return SkNEW_ARGS(SkArithmeticMode_linear, (i2, i3, i4)); 229 #endif 230 } 231 return SkNEW_ARGS(SkArithmeticMode_scalar, (k1, k2, k3, k4)); 232 } 233 234 235 ////////////////////////////////////////////////////////////////////////////// 236 237 #if SK_SUPPORT_GPU 238 239 class GrGLArithmeticEffect : public GrGLEffect { 240 public: 241 GrGLArithmeticEffect(const GrBackendEffectFactory&, const GrDrawEffect&); 242 virtual ~GrGLArithmeticEffect(); 243 244 virtual void emitCode(GrGLShaderBuilder*, 245 const GrDrawEffect&, 246 EffectKey, 247 const char* outputColor, 248 const char* inputColor, 249 const TransformedCoordsArray&, 250 const TextureSamplerArray&) SK_OVERRIDE; 251 252 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; 253 254 private: 255 GrGLUniformManager::UniformHandle fKUni; 256 257 typedef GrGLEffect INHERITED; 258 }; 259 260 /////////////////////////////////////////////////////////////////////////////// 261 262 class GrArithmeticEffect : public GrEffect { 263 public: 264 static GrEffectRef* Create(float k1, float k2, float k3, float k4, GrTexture* background) { 265 AutoEffectUnref effect(SkNEW_ARGS(GrArithmeticEffect, (k1, k2, k3, k4, background))); 266 return CreateEffectRef(effect); 267 } 268 269 virtual ~GrArithmeticEffect(); 270 271 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; 272 273 typedef GrGLArithmeticEffect GLEffect; 274 static const char* Name() { return "Arithmetic"; } 275 GrTexture* backgroundTexture() const { return fBackgroundAccess.getTexture(); } 276 277 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; 278 279 float k1() const { return fK1; } 280 float k2() const { return fK2; } 281 float k3() const { return fK3; } 282 float k4() const { return fK4; } 283 284 private: 285 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; 286 287 GrArithmeticEffect(float k1, float k2, float k3, float k4, GrTexture* background); 288 float fK1, fK2, fK3, fK4; 289 GrCoordTransform fBackgroundTransform; 290 GrTextureAccess fBackgroundAccess; 291 292 GR_DECLARE_EFFECT_TEST; 293 typedef GrEffect INHERITED; 294 295 }; 296 297 /////////////////////////////////////////////////////////////////////////////// 298 299 GrArithmeticEffect::GrArithmeticEffect(float k1, float k2, float k3, float k4, 300 GrTexture* background) 301 : fK1(k1), fK2(k2), fK3(k3), fK4(k4) { 302 if (background) { 303 fBackgroundTransform.reset(kLocal_GrCoordSet, background); 304 this->addCoordTransform(&fBackgroundTransform); 305 fBackgroundAccess.reset(background); 306 this->addTextureAccess(&fBackgroundAccess); 307 } else { 308 this->setWillReadDstColor(); 309 } 310 } 311 312 GrArithmeticEffect::~GrArithmeticEffect() { 313 } 314 315 bool GrArithmeticEffect::onIsEqual(const GrEffect& sBase) const { 316 const GrArithmeticEffect& s = CastEffect<GrArithmeticEffect>(sBase); 317 return fK1 == s.fK1 && 318 fK2 == s.fK2 && 319 fK3 == s.fK3 && 320 fK4 == s.fK4 && 321 backgroundTexture() == s.backgroundTexture(); 322 } 323 324 const GrBackendEffectFactory& GrArithmeticEffect::getFactory() const { 325 return GrTBackendEffectFactory<GrArithmeticEffect>::getInstance(); 326 } 327 328 void GrArithmeticEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { 329 // TODO: optimize this 330 *validFlags = 0; 331 } 332 333 /////////////////////////////////////////////////////////////////////////////// 334 335 GrGLArithmeticEffect::GrGLArithmeticEffect(const GrBackendEffectFactory& factory, 336 const GrDrawEffect& drawEffect) 337 : INHERITED(factory) { 338 } 339 340 GrGLArithmeticEffect::~GrGLArithmeticEffect() { 341 } 342 343 void GrGLArithmeticEffect::emitCode(GrGLShaderBuilder* builder, 344 const GrDrawEffect& drawEffect, 345 EffectKey key, 346 const char* outputColor, 347 const char* inputColor, 348 const TransformedCoordsArray& coords, 349 const TextureSamplerArray& samplers) { 350 351 GrTexture* backgroundTex = drawEffect.castEffect<GrArithmeticEffect>().backgroundTexture(); 352 const char* dstColor; 353 if (backgroundTex) { 354 builder->fsCodeAppend("\t\tvec4 bgColor = "); 355 builder->fsAppendTextureLookup(samplers[0], coords[0].c_str(), coords[0].type()); 356 builder->fsCodeAppendf(";\n"); 357 dstColor = "bgColor"; 358 } else { 359 dstColor = builder->dstColor(); 360 } 361 362 SkASSERT(NULL != dstColor); 363 fKUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, 364 kVec4f_GrSLType, "k"); 365 const char* kUni = builder->getUniformCStr(fKUni); 366 367 // We don't try to optimize for this case at all 368 if (NULL == inputColor) { 369 builder->fsCodeAppendf("\t\tconst vec4 src = vec4(1);\n"); 370 } else { 371 builder->fsCodeAppendf("\t\tvec4 src = %s;\n", inputColor); 372 if (gUseUnpremul) { 373 builder->fsCodeAppendf("\t\tsrc.rgb = clamp(src.rgb / src.a, 0.0, 1.0);\n"); 374 } 375 } 376 377 builder->fsCodeAppendf("\t\tvec4 dst = %s;\n", dstColor); 378 if (gUseUnpremul) { 379 builder->fsCodeAppendf("\t\tdst.rgb = clamp(dst.rgb / dst.a, 0.0, 1.0);\n"); 380 } 381 382 builder->fsCodeAppendf("\t\t%s = %s.x * src * dst + %s.y * src + %s.z * dst + %s.w;\n", outputColor, kUni, kUni, kUni, kUni); 383 builder->fsCodeAppendf("\t\t%s = clamp(%s, 0.0, 1.0);\n", outputColor, outputColor); 384 if (gUseUnpremul) { 385 builder->fsCodeAppendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor); 386 } else { 387 builder->fsCodeAppendf("\t\t%s.rgb = min(%s.rgb, %s.a);\n", outputColor, outputColor, outputColor); 388 } 389 } 390 391 void GrGLArithmeticEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) { 392 const GrArithmeticEffect& arith = drawEffect.castEffect<GrArithmeticEffect>(); 393 uman.set4f(fKUni, arith.k1(), arith.k2(), arith.k3(), arith.k4()); 394 } 395 396 GrEffectRef* GrArithmeticEffect::TestCreate(SkRandom* rand, 397 GrContext*, 398 const GrDrawTargetCaps&, 399 GrTexture*[]) { 400 float k1 = rand->nextF(); 401 float k2 = rand->nextF(); 402 float k3 = rand->nextF(); 403 float k4 = rand->nextF(); 404 405 AutoEffectUnref gEffect(SkNEW_ARGS(GrArithmeticEffect, (k1, k2, k3, k4, NULL))); 406 return CreateEffectRef(gEffect); 407 } 408 409 GR_DEFINE_EFFECT_TEST(GrArithmeticEffect); 410 411 bool SkArithmeticMode_scalar::asNewEffect(GrEffectRef** effect, GrTexture* background) const { 412 if (effect) { 413 *effect = GrArithmeticEffect::Create(SkScalarToFloat(fK[0]), 414 SkScalarToFloat(fK[1]), 415 SkScalarToFloat(fK[2]), 416 SkScalarToFloat(fK[3]), 417 background); 418 } 419 return true; 420 } 421 422 #endif 423 424 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkArithmeticMode) 425 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkArithmeticMode_scalar) 426 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END 427