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