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