1/* 2 * Copyright 2012 The Android Open Source Project 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 "SkMatrixConvolutionImageFilter.h" 9#include "SkBitmap.h" 10#include "SkColorPriv.h" 11#include "SkReadBuffer.h" 12#include "SkWriteBuffer.h" 13#include "SkRect.h" 14#include "SkUnPreMultiply.h" 15 16#if SK_SUPPORT_GPU 17#include "gl/GrGLEffect.h" 18#include "effects/GrSingleTextureEffect.h" 19#include "GrTBackendEffectFactory.h" 20#include "GrTexture.h" 21#include "SkMatrix.h" 22#endif 23 24static bool tile_mode_is_valid(SkMatrixConvolutionImageFilter::TileMode tileMode) { 25 switch (tileMode) { 26 case SkMatrixConvolutionImageFilter::kClamp_TileMode: 27 case SkMatrixConvolutionImageFilter::kRepeat_TileMode: 28 case SkMatrixConvolutionImageFilter::kClampToBlack_TileMode: 29 return true; 30 default: 31 break; 32 } 33 return false; 34} 35 36SkMatrixConvolutionImageFilter::SkMatrixConvolutionImageFilter( 37 const SkISize& kernelSize, 38 const SkScalar* kernel, 39 SkScalar gain, 40 SkScalar bias, 41 const SkIPoint& kernelOffset, 42 TileMode tileMode, 43 bool convolveAlpha, 44 SkImageFilter* input, 45 const CropRect* cropRect) 46 : INHERITED(input, cropRect), 47 fKernelSize(kernelSize), 48 fGain(gain), 49 fBias(bias), 50 fKernelOffset(kernelOffset), 51 fTileMode(tileMode), 52 fConvolveAlpha(convolveAlpha) { 53 uint32_t size = fKernelSize.fWidth * fKernelSize.fHeight; 54 fKernel = SkNEW_ARRAY(SkScalar, size); 55 memcpy(fKernel, kernel, size * sizeof(SkScalar)); 56 SkASSERT(kernelSize.fWidth >= 1 && kernelSize.fHeight >= 1); 57 SkASSERT(kernelOffset.fX >= 0 && kernelOffset.fX < kernelSize.fWidth); 58 SkASSERT(kernelOffset.fY >= 0 && kernelOffset.fY < kernelSize.fHeight); 59} 60 61SkMatrixConvolutionImageFilter::SkMatrixConvolutionImageFilter(SkReadBuffer& buffer) 62 : INHERITED(1, buffer) { 63 // We need to be able to read at most SK_MaxS32 bytes, so divide that 64 // by the size of a scalar to know how many scalars we can read. 65 static const int32_t kMaxSize = SK_MaxS32 / sizeof(SkScalar); 66 fKernelSize.fWidth = buffer.readInt(); 67 fKernelSize.fHeight = buffer.readInt(); 68 if ((fKernelSize.fWidth >= 1) && (fKernelSize.fHeight >= 1) && 69 // Make sure size won't be larger than a signed int, 70 // which would still be extremely large for a kernel, 71 // but we don't impose a hard limit for kernel size 72 (kMaxSize / fKernelSize.fWidth >= fKernelSize.fHeight)) { 73 size_t size = fKernelSize.fWidth * fKernelSize.fHeight; 74 fKernel = SkNEW_ARRAY(SkScalar, size); 75 SkDEBUGCODE(bool success =) buffer.readScalarArray(fKernel, size); 76 SkASSERT(success); 77 } else { 78 fKernel = 0; 79 } 80 fGain = buffer.readScalar(); 81 fBias = buffer.readScalar(); 82 fKernelOffset.fX = buffer.readInt(); 83 fKernelOffset.fY = buffer.readInt(); 84 fTileMode = (TileMode) buffer.readInt(); 85 fConvolveAlpha = buffer.readBool(); 86 buffer.validate((fKernel != 0) && 87 SkScalarIsFinite(fGain) && 88 SkScalarIsFinite(fBias) && 89 tile_mode_is_valid(fTileMode) && 90 (fKernelOffset.fX >= 0) && (fKernelOffset.fX < fKernelSize.fWidth) && 91 (fKernelOffset.fY >= 0) && (fKernelOffset.fY < fKernelSize.fHeight)); 92} 93 94void SkMatrixConvolutionImageFilter::flatten(SkWriteBuffer& buffer) const { 95 this->INHERITED::flatten(buffer); 96 buffer.writeInt(fKernelSize.fWidth); 97 buffer.writeInt(fKernelSize.fHeight); 98 buffer.writeScalarArray(fKernel, fKernelSize.fWidth * fKernelSize.fHeight); 99 buffer.writeScalar(fGain); 100 buffer.writeScalar(fBias); 101 buffer.writeInt(fKernelOffset.fX); 102 buffer.writeInt(fKernelOffset.fY); 103 buffer.writeInt((int) fTileMode); 104 buffer.writeBool(fConvolveAlpha); 105} 106 107SkMatrixConvolutionImageFilter::~SkMatrixConvolutionImageFilter() { 108 delete[] fKernel; 109} 110 111class UncheckedPixelFetcher { 112public: 113 static inline SkPMColor fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) { 114 return *src.getAddr32(x, y); 115 } 116}; 117 118class ClampPixelFetcher { 119public: 120 static inline SkPMColor fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) { 121 x = SkPin32(x, bounds.fLeft, bounds.fRight - 1); 122 y = SkPin32(y, bounds.fTop, bounds.fBottom - 1); 123 return *src.getAddr32(x, y); 124 } 125}; 126 127class RepeatPixelFetcher { 128public: 129 static inline SkPMColor fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) { 130 x = (x - bounds.left()) % bounds.width() + bounds.left(); 131 y = (y - bounds.top()) % bounds.height() + bounds.top(); 132 if (x < bounds.left()) { 133 x += bounds.width(); 134 } 135 if (y < bounds.top()) { 136 y += bounds.height(); 137 } 138 return *src.getAddr32(x, y); 139 } 140}; 141 142class ClampToBlackPixelFetcher { 143public: 144 static inline SkPMColor fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) { 145 if (x < bounds.fLeft || x >= bounds.fRight || y < bounds.fTop || y >= bounds.fBottom) { 146 return 0; 147 } else { 148 return *src.getAddr32(x, y); 149 } 150 } 151}; 152 153template<class PixelFetcher, bool convolveAlpha> 154void SkMatrixConvolutionImageFilter::filterPixels(const SkBitmap& src, 155 SkBitmap* result, 156 const SkIRect& r, 157 const SkIRect& bounds) const { 158 SkIRect rect(r); 159 if (!rect.intersect(bounds)) { 160 return; 161 } 162 for (int y = rect.fTop; y < rect.fBottom; ++y) { 163 SkPMColor* dptr = result->getAddr32(rect.fLeft - bounds.fLeft, y - bounds.fTop); 164 for (int x = rect.fLeft; x < rect.fRight; ++x) { 165 SkScalar sumA = 0, sumR = 0, sumG = 0, sumB = 0; 166 for (int cy = 0; cy < fKernelSize.fHeight; cy++) { 167 for (int cx = 0; cx < fKernelSize.fWidth; cx++) { 168 SkPMColor s = PixelFetcher::fetch(src, 169 x + cx - fKernelOffset.fX, 170 y + cy - fKernelOffset.fY, 171 bounds); 172 SkScalar k = fKernel[cy * fKernelSize.fWidth + cx]; 173 if (convolveAlpha) { 174 sumA += SkScalarMul(SkIntToScalar(SkGetPackedA32(s)), k); 175 } 176 sumR += SkScalarMul(SkIntToScalar(SkGetPackedR32(s)), k); 177 sumG += SkScalarMul(SkIntToScalar(SkGetPackedG32(s)), k); 178 sumB += SkScalarMul(SkIntToScalar(SkGetPackedB32(s)), k); 179 } 180 } 181 int a = convolveAlpha 182 ? SkClampMax(SkScalarFloorToInt(SkScalarMul(sumA, fGain) + fBias), 255) 183 : 255; 184 int r = SkClampMax(SkScalarFloorToInt(SkScalarMul(sumR, fGain) + fBias), a); 185 int g = SkClampMax(SkScalarFloorToInt(SkScalarMul(sumG, fGain) + fBias), a); 186 int b = SkClampMax(SkScalarFloorToInt(SkScalarMul(sumB, fGain) + fBias), a); 187 if (!convolveAlpha) { 188 a = SkGetPackedA32(PixelFetcher::fetch(src, x, y, bounds)); 189 *dptr++ = SkPreMultiplyARGB(a, r, g, b); 190 } else { 191 *dptr++ = SkPackARGB32(a, r, g, b); 192 } 193 } 194 } 195} 196 197template<class PixelFetcher> 198void SkMatrixConvolutionImageFilter::filterPixels(const SkBitmap& src, 199 SkBitmap* result, 200 const SkIRect& rect, 201 const SkIRect& bounds) const { 202 if (fConvolveAlpha) { 203 filterPixels<PixelFetcher, true>(src, result, rect, bounds); 204 } else { 205 filterPixels<PixelFetcher, false>(src, result, rect, bounds); 206 } 207} 208 209void SkMatrixConvolutionImageFilter::filterInteriorPixels(const SkBitmap& src, 210 SkBitmap* result, 211 const SkIRect& rect, 212 const SkIRect& bounds) const { 213 filterPixels<UncheckedPixelFetcher>(src, result, rect, bounds); 214} 215 216void SkMatrixConvolutionImageFilter::filterBorderPixels(const SkBitmap& src, 217 SkBitmap* result, 218 const SkIRect& rect, 219 const SkIRect& bounds) const { 220 switch (fTileMode) { 221 case kClamp_TileMode: 222 filterPixels<ClampPixelFetcher>(src, result, rect, bounds); 223 break; 224 case kRepeat_TileMode: 225 filterPixels<RepeatPixelFetcher>(src, result, rect, bounds); 226 break; 227 case kClampToBlack_TileMode: 228 filterPixels<ClampToBlackPixelFetcher>(src, result, rect, bounds); 229 break; 230 } 231} 232 233// FIXME: This should be refactored to SkImageFilterUtils for 234// use by other filters. For now, we assume the input is always 235// premultiplied and unpremultiply it 236static SkBitmap unpremultiplyBitmap(const SkBitmap& src) 237{ 238 SkAutoLockPixels alp(src); 239 if (!src.getPixels()) { 240 return SkBitmap(); 241 } 242 SkBitmap result; 243 if (!result.allocPixels(src.info())) { 244 return SkBitmap(); 245 } 246 for (int y = 0; y < src.height(); ++y) { 247 const uint32_t* srcRow = src.getAddr32(0, y); 248 uint32_t* dstRow = result.getAddr32(0, y); 249 for (int x = 0; x < src.width(); ++x) { 250 dstRow[x] = SkUnPreMultiply::PMColorToColor(srcRow[x]); 251 } 252 } 253 return result; 254} 255 256bool SkMatrixConvolutionImageFilter::onFilterImage(Proxy* proxy, 257 const SkBitmap& source, 258 const Context& ctx, 259 SkBitmap* result, 260 SkIPoint* offset) const { 261 SkBitmap src = source; 262 SkIPoint srcOffset = SkIPoint::Make(0, 0); 263 if (getInput(0) && !getInput(0)->filterImage(proxy, source, ctx, &src, &srcOffset)) { 264 return false; 265 } 266 267 if (src.colorType() != kN32_SkColorType) { 268 return false; 269 } 270 271 SkIRect bounds; 272 if (!this->applyCropRect(ctx, proxy, src, &srcOffset, &bounds, &src)) { 273 return false; 274 } 275 276 if (!fConvolveAlpha && !src.isOpaque()) { 277 src = unpremultiplyBitmap(src); 278 } 279 280 SkAutoLockPixels alp(src); 281 if (!src.getPixels()) { 282 return false; 283 } 284 285 if (!result->allocPixels(src.info().makeWH(bounds.width(), bounds.height()))) { 286 return false; 287 } 288 289 offset->fX = bounds.fLeft; 290 offset->fY = bounds.fTop; 291 bounds.offset(-srcOffset); 292 SkIRect interior = SkIRect::MakeXYWH(bounds.left() + fKernelOffset.fX, 293 bounds.top() + fKernelOffset.fY, 294 bounds.width() - fKernelSize.fWidth + 1, 295 bounds.height() - fKernelSize.fHeight + 1); 296 SkIRect top = SkIRect::MakeLTRB(bounds.left(), bounds.top(), bounds.right(), interior.top()); 297 SkIRect bottom = SkIRect::MakeLTRB(bounds.left(), interior.bottom(), 298 bounds.right(), bounds.bottom()); 299 SkIRect left = SkIRect::MakeLTRB(bounds.left(), interior.top(), 300 interior.left(), interior.bottom()); 301 SkIRect right = SkIRect::MakeLTRB(interior.right(), interior.top(), 302 bounds.right(), interior.bottom()); 303 filterBorderPixels(src, result, top, bounds); 304 filterBorderPixels(src, result, left, bounds); 305 filterInteriorPixels(src, result, interior, bounds); 306 filterBorderPixels(src, result, right, bounds); 307 filterBorderPixels(src, result, bottom, bounds); 308 return true; 309} 310 311bool SkMatrixConvolutionImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& ctm, 312 SkIRect* dst) const { 313 SkIRect bounds = src; 314 bounds.fRight += fKernelSize.width() - 1; 315 bounds.fBottom += fKernelSize.height() - 1; 316 bounds.offset(-fKernelOffset); 317 if (getInput(0) && !getInput(0)->filterBounds(bounds, ctm, &bounds)) { 318 return false; 319 } 320 *dst = bounds; 321 return true; 322} 323 324#if SK_SUPPORT_GPU 325 326/////////////////////////////////////////////////////////////////////////////// 327 328class GrGLMatrixConvolutionEffect; 329 330class GrMatrixConvolutionEffect : public GrSingleTextureEffect { 331public: 332 typedef SkMatrixConvolutionImageFilter::TileMode TileMode; 333 static GrEffectRef* Create(GrTexture* texture, 334 const SkIRect& bounds, 335 const SkISize& kernelSize, 336 const SkScalar* kernel, 337 SkScalar gain, 338 SkScalar bias, 339 const SkIPoint& kernelOffset, 340 TileMode tileMode, 341 bool convolveAlpha) { 342 AutoEffectUnref effect(SkNEW_ARGS(GrMatrixConvolutionEffect, (texture, 343 bounds, 344 kernelSize, 345 kernel, 346 gain, 347 bias, 348 kernelOffset, 349 tileMode, 350 convolveAlpha))); 351 return CreateEffectRef(effect); 352 } 353 virtual ~GrMatrixConvolutionEffect(); 354 355 virtual void getConstantColorComponents(GrColor* color, 356 uint32_t* validFlags) const SK_OVERRIDE { 357 // TODO: Try to do better? 358 *validFlags = 0; 359 } 360 361 static const char* Name() { return "MatrixConvolution"; } 362 const SkIRect& bounds() const { return fBounds; } 363 const SkISize& kernelSize() const { return fKernelSize; } 364 const float* kernelOffset() const { return fKernelOffset; } 365 const float* kernel() const { return fKernel; } 366 float gain() const { return fGain; } 367 float bias() const { return fBias; } 368 TileMode tileMode() const { return fTileMode; } 369 bool convolveAlpha() const { return fConvolveAlpha; } 370 371 typedef GrGLMatrixConvolutionEffect GLEffect; 372 373 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; 374 375private: 376 GrMatrixConvolutionEffect(GrTexture*, 377 const SkIRect& bounds, 378 const SkISize& kernelSize, 379 const SkScalar* kernel, 380 SkScalar gain, 381 SkScalar bias, 382 const SkIPoint& kernelOffset, 383 TileMode tileMode, 384 bool convolveAlpha); 385 386 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; 387 388 SkIRect fBounds; 389 SkISize fKernelSize; 390 float *fKernel; 391 float fGain; 392 float fBias; 393 float fKernelOffset[2]; 394 TileMode fTileMode; 395 bool fConvolveAlpha; 396 397 GR_DECLARE_EFFECT_TEST; 398 399 typedef GrSingleTextureEffect INHERITED; 400}; 401 402class GrGLMatrixConvolutionEffect : public GrGLEffect { 403public: 404 GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory, 405 const GrDrawEffect& effect); 406 virtual void emitCode(GrGLShaderBuilder*, 407 const GrDrawEffect&, 408 EffectKey, 409 const char* outputColor, 410 const char* inputColor, 411 const TransformedCoordsArray&, 412 const TextureSamplerArray&) SK_OVERRIDE; 413 414 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); 415 416 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; 417 418private: 419 typedef GrGLUniformManager::UniformHandle UniformHandle; 420 typedef SkMatrixConvolutionImageFilter::TileMode TileMode; 421 SkISize fKernelSize; 422 TileMode fTileMode; 423 bool fConvolveAlpha; 424 425 UniformHandle fBoundsUni; 426 UniformHandle fKernelUni; 427 UniformHandle fImageIncrementUni; 428 UniformHandle fKernelOffsetUni; 429 UniformHandle fGainUni; 430 UniformHandle fBiasUni; 431 432 typedef GrGLEffect INHERITED; 433}; 434 435GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory, 436 const GrDrawEffect& drawEffect) 437 : INHERITED(factory) { 438 const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvolutionEffect>(); 439 fKernelSize = m.kernelSize(); 440 fTileMode = m.tileMode(); 441 fConvolveAlpha = m.convolveAlpha(); 442} 443 444static void appendTextureLookup(GrGLShaderBuilder* builder, 445 const GrGLShaderBuilder::TextureSampler& sampler, 446 const char* coord, 447 const char* bounds, 448 SkMatrixConvolutionImageFilter::TileMode tileMode) { 449 SkString clampedCoord; 450 switch (tileMode) { 451 case SkMatrixConvolutionImageFilter::kClamp_TileMode: 452 clampedCoord.printf("clamp(%s, %s.xy, %s.zw)", coord, bounds, bounds); 453 coord = clampedCoord.c_str(); 454 break; 455 case SkMatrixConvolutionImageFilter::kRepeat_TileMode: 456 clampedCoord.printf("mod(%s - %s.xy, %s.zw - %s.xy) + %s.xy", coord, bounds, bounds, bounds, bounds); 457 coord = clampedCoord.c_str(); 458 break; 459 case SkMatrixConvolutionImageFilter::kClampToBlack_TileMode: 460 builder->fsCodeAppendf("clamp(%s, %s.xy, %s.zw) != %s ? vec4(0, 0, 0, 0) : ", coord, bounds, bounds, coord); 461 break; 462 } 463 builder->fsAppendTextureLookup(sampler, coord); 464} 465 466void GrGLMatrixConvolutionEffect::emitCode(GrGLShaderBuilder* builder, 467 const GrDrawEffect&, 468 EffectKey key, 469 const char* outputColor, 470 const char* inputColor, 471 const TransformedCoordsArray& coords, 472 const TextureSamplerArray& samplers) { 473 sk_ignore_unused_variable(inputColor); 474 SkString coords2D = builder->ensureFSCoords2D(coords, 0); 475 fBoundsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, 476 kVec4f_GrSLType, "Bounds"); 477 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, 478 kVec2f_GrSLType, "ImageIncrement"); 479 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibility, 480 kFloat_GrSLType, 481 "Kernel", 482 fKernelSize.width() * fKernelSize.height()); 483 fKernelOffsetUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, 484 kVec2f_GrSLType, "KernelOffset"); 485 fGainUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, 486 kFloat_GrSLType, "Gain"); 487 fBiasUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, 488 kFloat_GrSLType, "Bias"); 489 490 const char* bounds = builder->getUniformCStr(fBoundsUni); 491 const char* kernelOffset = builder->getUniformCStr(fKernelOffsetUni); 492 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); 493 const char* kernel = builder->getUniformCStr(fKernelUni); 494 const char* gain = builder->getUniformCStr(fGainUni); 495 const char* bias = builder->getUniformCStr(fBiasUni); 496 int kWidth = fKernelSize.width(); 497 int kHeight = fKernelSize.height(); 498 499 builder->fsCodeAppend("\t\tvec4 sum = vec4(0, 0, 0, 0);\n"); 500 builder->fsCodeAppendf("\t\tvec2 coord = %s - %s * %s;\n", coords2D.c_str(), kernelOffset, imgInc); 501 builder->fsCodeAppendf("\t\tfor (int y = 0; y < %d; y++) {\n", kHeight); 502 builder->fsCodeAppendf("\t\t\tfor (int x = 0; x < %d; x++) {\n", kWidth); 503 builder->fsCodeAppendf("\t\t\t\tfloat k = %s[y * %d + x];\n", kernel, kWidth); 504 builder->fsCodeAppendf("\t\t\t\tvec2 coord2 = coord + vec2(x, y) * %s;\n", imgInc); 505 builder->fsCodeAppend("\t\t\t\tvec4 c = "); 506 appendTextureLookup(builder, samplers[0], "coord2", bounds, fTileMode); 507 builder->fsCodeAppend(";\n"); 508 if (!fConvolveAlpha) { 509 builder->fsCodeAppend("\t\t\t\tc.rgb /= c.a;\n"); 510 } 511 builder->fsCodeAppend("\t\t\t\tsum += c * k;\n"); 512 builder->fsCodeAppend("\t\t\t}\n"); 513 builder->fsCodeAppend("\t\t}\n"); 514 if (fConvolveAlpha) { 515 builder->fsCodeAppendf("\t\t%s = sum * %s + %s;\n", outputColor, gain, bias); 516 builder->fsCodeAppendf("\t\t%s.rgb = clamp(%s.rgb, 0.0, %s.a);\n", 517 outputColor, outputColor, outputColor); 518 } else { 519 builder->fsCodeAppend("\t\tvec4 c = "); 520 appendTextureLookup(builder, samplers[0], coords2D.c_str(), bounds, fTileMode); 521 builder->fsCodeAppend(";\n"); 522 builder->fsCodeAppendf("\t\t%s.a = c.a;\n", outputColor); 523 builder->fsCodeAppendf("\t\t%s.rgb = sum.rgb * %s + %s;\n", outputColor, gain, bias); 524 builder->fsCodeAppendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor); 525 } 526} 527 528namespace { 529 530int encodeXY(int x, int y) { 531 SkASSERT(x >= 1 && y >= 1 && x * y <= 32); 532 if (y < x) 533 return 0x40 | encodeXY(y, x); 534 else 535 return (0x40 >> x) | (y - x); 536} 537 538}; 539 540GrGLEffect::EffectKey GrGLMatrixConvolutionEffect::GenKey(const GrDrawEffect& drawEffect, 541 const GrGLCaps&) { 542 const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvolutionEffect>(); 543 EffectKey key = encodeXY(m.kernelSize().width(), m.kernelSize().height()); 544 key |= m.tileMode() << 7; 545 key |= m.convolveAlpha() ? 1 << 9 : 0; 546 return key; 547} 548 549void GrGLMatrixConvolutionEffect::setData(const GrGLUniformManager& uman, 550 const GrDrawEffect& drawEffect) { 551 const GrMatrixConvolutionEffect& conv = drawEffect.castEffect<GrMatrixConvolutionEffect>(); 552 GrTexture& texture = *conv.texture(0); 553 // the code we generated was for a specific kernel size 554 SkASSERT(conv.kernelSize() == fKernelSize); 555 SkASSERT(conv.tileMode() == fTileMode); 556 float imageIncrement[2]; 557 float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; 558 imageIncrement[0] = 1.0f / texture.width(); 559 imageIncrement[1] = ySign / texture.height(); 560 uman.set2fv(fImageIncrementUni, 1, imageIncrement); 561 uman.set2fv(fKernelOffsetUni, 1, conv.kernelOffset()); 562 uman.set1fv(fKernelUni, fKernelSize.width() * fKernelSize.height(), conv.kernel()); 563 uman.set1f(fGainUni, conv.gain()); 564 uman.set1f(fBiasUni, conv.bias()); 565 const SkIRect& bounds = conv.bounds(); 566 float left = (float) bounds.left() / texture.width(); 567 float top = (float) bounds.top() / texture.height(); 568 float right = (float) bounds.right() / texture.width(); 569 float bottom = (float) bounds.bottom() / texture.height(); 570 if (texture.origin() == kBottomLeft_GrSurfaceOrigin) { 571 uman.set4f(fBoundsUni, left, 1.0f - bottom, right, 1.0f - top); 572 } else { 573 uman.set4f(fBoundsUni, left, top, right, bottom); 574 } 575} 576 577GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture, 578 const SkIRect& bounds, 579 const SkISize& kernelSize, 580 const SkScalar* kernel, 581 SkScalar gain, 582 SkScalar bias, 583 const SkIPoint& kernelOffset, 584 TileMode tileMode, 585 bool convolveAlpha) 586 : INHERITED(texture, MakeDivByTextureWHMatrix(texture)), 587 fBounds(bounds), 588 fKernelSize(kernelSize), 589 fGain(SkScalarToFloat(gain)), 590 fBias(SkScalarToFloat(bias) / 255.0f), 591 fTileMode(tileMode), 592 fConvolveAlpha(convolveAlpha) { 593 fKernel = new float[kernelSize.width() * kernelSize.height()]; 594 for (int i = 0; i < kernelSize.width() * kernelSize.height(); i++) { 595 fKernel[i] = SkScalarToFloat(kernel[i]); 596 } 597 fKernelOffset[0] = static_cast<float>(kernelOffset.x()); 598 fKernelOffset[1] = static_cast<float>(kernelOffset.y()); 599 this->setWillNotUseInputColor(); 600} 601 602GrMatrixConvolutionEffect::~GrMatrixConvolutionEffect() { 603 delete[] fKernel; 604} 605 606const GrBackendEffectFactory& GrMatrixConvolutionEffect::getFactory() const { 607 return GrTBackendEffectFactory<GrMatrixConvolutionEffect>::getInstance(); 608} 609 610bool GrMatrixConvolutionEffect::onIsEqual(const GrEffect& sBase) const { 611 const GrMatrixConvolutionEffect& s = CastEffect<GrMatrixConvolutionEffect>(sBase); 612 return this->texture(0) == s.texture(0) && 613 fKernelSize == s.kernelSize() && 614 !memcmp(fKernel, s.kernel(), 615 fKernelSize.width() * fKernelSize.height() * sizeof(float)) && 616 fGain == s.gain() && 617 fBias == s.bias() && 618 fKernelOffset == s.kernelOffset() && 619 fTileMode == s.tileMode() && 620 fConvolveAlpha == s.convolveAlpha(); 621} 622 623GR_DEFINE_EFFECT_TEST(GrMatrixConvolutionEffect); 624 625// A little bit less than the minimum # uniforms required by DX9SM2 (32). 626// Allows for a 5x5 kernel (or 25x1, for that matter). 627#define MAX_KERNEL_SIZE 25 628 629GrEffectRef* GrMatrixConvolutionEffect::TestCreate(SkRandom* random, 630 GrContext* context, 631 const GrDrawTargetCaps&, 632 GrTexture* textures[]) { 633 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : 634 GrEffectUnitTest::kAlphaTextureIdx; 635 int width = random->nextRangeU(1, MAX_KERNEL_SIZE); 636 int height = random->nextRangeU(1, MAX_KERNEL_SIZE / width); 637 SkISize kernelSize = SkISize::Make(width, height); 638 SkAutoTDeleteArray<SkScalar> kernel(new SkScalar[width * height]); 639 for (int i = 0; i < width * height; i++) { 640 kernel.get()[i] = random->nextSScalar1(); 641 } 642 SkScalar gain = random->nextSScalar1(); 643 SkScalar bias = random->nextSScalar1(); 644 SkIPoint kernelOffset = SkIPoint::Make(random->nextRangeU(0, kernelSize.width()), 645 random->nextRangeU(0, kernelSize.height())); 646 SkIRect bounds = SkIRect::MakeXYWH(random->nextRangeU(0, textures[texIdx]->width()), 647 random->nextRangeU(0, textures[texIdx]->height()), 648 random->nextRangeU(0, textures[texIdx]->width()), 649 random->nextRangeU(0, textures[texIdx]->height())); 650 TileMode tileMode = static_cast<TileMode>(random->nextRangeU(0, 2)); 651 bool convolveAlpha = random->nextBool(); 652 return GrMatrixConvolutionEffect::Create(textures[texIdx], 653 bounds, 654 kernelSize, 655 kernel.get(), 656 gain, 657 bias, 658 kernelOffset, 659 tileMode, 660 convolveAlpha); 661} 662 663bool SkMatrixConvolutionImageFilter::asNewEffect(GrEffectRef** effect, 664 GrTexture* texture, 665 const SkMatrix&, 666 const SkIRect& bounds 667 ) const { 668 if (!effect) { 669 return fKernelSize.width() * fKernelSize.height() <= MAX_KERNEL_SIZE; 670 } 671 SkASSERT(fKernelSize.width() * fKernelSize.height() <= MAX_KERNEL_SIZE); 672 *effect = GrMatrixConvolutionEffect::Create(texture, 673 bounds, 674 fKernelSize, 675 fKernel, 676 fGain, 677 fBias, 678 fKernelOffset, 679 fTileMode, 680 fConvolveAlpha); 681 return true; 682} 683 684/////////////////////////////////////////////////////////////////////////////// 685 686#endif 687