SkMatrixConvolutionImageFilter.cpp revision 118252962f89a80db661a0544f1bd61cbaab6321
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 Context& ctx, 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, ctx, &src, &srcOffset)) { 260 return false; 261 } 262 263 if (src.colorType() != kPMColor_SkColorType) { 264 return false; 265 } 266 267 SkIRect bounds; 268 if (!this->applyCropRect(ctx, proxy, src, &srcOffset, &bounds, &src)) { 269 return false; 270 } 271 272 if (!fConvolveAlpha && !src.isOpaque()) { 273 src = unpremultiplyBitmap(src); 274 } 275 276 SkAutoLockPixels alp(src); 277 if (!src.getPixels()) { 278 return false; 279 } 280 281 result->setConfig(src.config(), bounds.width(), bounds.height()); 282 result->allocPixels(); 283 if (!result->getPixels()) { 284 return false; 285 } 286 287 offset->fX = bounds.fLeft; 288 offset->fY = bounds.fTop; 289 bounds.offset(-srcOffset); 290 SkIRect interior = SkIRect::MakeXYWH(bounds.left() + fKernelOffset.fX, 291 bounds.top() + fKernelOffset.fY, 292 bounds.width() - fKernelSize.fWidth + 1, 293 bounds.height() - fKernelSize.fHeight + 1); 294 SkIRect top = SkIRect::MakeLTRB(bounds.left(), bounds.top(), bounds.right(), interior.top()); 295 SkIRect bottom = SkIRect::MakeLTRB(bounds.left(), interior.bottom(), 296 bounds.right(), bounds.bottom()); 297 SkIRect left = SkIRect::MakeLTRB(bounds.left(), interior.top(), 298 interior.left(), interior.bottom()); 299 SkIRect right = SkIRect::MakeLTRB(interior.right(), interior.top(), 300 bounds.right(), interior.bottom()); 301 filterBorderPixels(src, result, top, bounds); 302 filterBorderPixels(src, result, left, bounds); 303 filterInteriorPixels(src, result, interior, bounds); 304 filterBorderPixels(src, result, right, bounds); 305 filterBorderPixels(src, result, bottom, bounds); 306 return true; 307} 308 309#if SK_SUPPORT_GPU 310 311/////////////////////////////////////////////////////////////////////////////// 312 313class GrGLMatrixConvolutionEffect; 314 315class GrMatrixConvolutionEffect : public GrSingleTextureEffect { 316public: 317 typedef SkMatrixConvolutionImageFilter::TileMode TileMode; 318 static GrEffectRef* Create(GrTexture* texture, 319 const SkIRect& bounds, 320 const SkISize& kernelSize, 321 const SkScalar* kernel, 322 SkScalar gain, 323 SkScalar bias, 324 const SkIPoint& kernelOffset, 325 TileMode tileMode, 326 bool convolveAlpha) { 327 AutoEffectUnref effect(SkNEW_ARGS(GrMatrixConvolutionEffect, (texture, 328 bounds, 329 kernelSize, 330 kernel, 331 gain, 332 bias, 333 kernelOffset, 334 tileMode, 335 convolveAlpha))); 336 return CreateEffectRef(effect); 337 } 338 virtual ~GrMatrixConvolutionEffect(); 339 340 virtual void getConstantColorComponents(GrColor* color, 341 uint32_t* validFlags) const SK_OVERRIDE { 342 // TODO: Try to do better? 343 *validFlags = 0; 344 } 345 346 static const char* Name() { return "MatrixConvolution"; } 347 const SkIRect& bounds() const { return fBounds; } 348 const SkISize& kernelSize() const { return fKernelSize; } 349 const float* kernelOffset() const { return fKernelOffset; } 350 const float* kernel() const { return fKernel; } 351 float gain() const { return fGain; } 352 float bias() const { return fBias; } 353 TileMode tileMode() const { return fTileMode; } 354 bool convolveAlpha() const { return fConvolveAlpha; } 355 356 typedef GrGLMatrixConvolutionEffect GLEffect; 357 358 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; 359 360private: 361 GrMatrixConvolutionEffect(GrTexture*, 362 const SkIRect& bounds, 363 const SkISize& kernelSize, 364 const SkScalar* kernel, 365 SkScalar gain, 366 SkScalar bias, 367 const SkIPoint& kernelOffset, 368 TileMode tileMode, 369 bool convolveAlpha); 370 371 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; 372 373 SkIRect fBounds; 374 SkISize fKernelSize; 375 float *fKernel; 376 float fGain; 377 float fBias; 378 float fKernelOffset[2]; 379 TileMode fTileMode; 380 bool fConvolveAlpha; 381 382 GR_DECLARE_EFFECT_TEST; 383 384 typedef GrSingleTextureEffect INHERITED; 385}; 386 387class GrGLMatrixConvolutionEffect : public GrGLEffect { 388public: 389 GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory, 390 const GrDrawEffect& effect); 391 virtual void emitCode(GrGLShaderBuilder*, 392 const GrDrawEffect&, 393 EffectKey, 394 const char* outputColor, 395 const char* inputColor, 396 const TransformedCoordsArray&, 397 const TextureSamplerArray&) SK_OVERRIDE; 398 399 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); 400 401 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; 402 403private: 404 typedef GrGLUniformManager::UniformHandle UniformHandle; 405 typedef SkMatrixConvolutionImageFilter::TileMode TileMode; 406 SkISize fKernelSize; 407 TileMode fTileMode; 408 bool fConvolveAlpha; 409 410 UniformHandle fBoundsUni; 411 UniformHandle fKernelUni; 412 UniformHandle fImageIncrementUni; 413 UniformHandle fKernelOffsetUni; 414 UniformHandle fGainUni; 415 UniformHandle fBiasUni; 416 417 typedef GrGLEffect INHERITED; 418}; 419 420GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory, 421 const GrDrawEffect& drawEffect) 422 : INHERITED(factory) { 423 const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvolutionEffect>(); 424 fKernelSize = m.kernelSize(); 425 fTileMode = m.tileMode(); 426 fConvolveAlpha = m.convolveAlpha(); 427} 428 429static void appendTextureLookup(GrGLShaderBuilder* builder, 430 const GrGLShaderBuilder::TextureSampler& sampler, 431 const char* coord, 432 const char* bounds, 433 SkMatrixConvolutionImageFilter::TileMode tileMode) { 434 SkString clampedCoord; 435 switch (tileMode) { 436 case SkMatrixConvolutionImageFilter::kClamp_TileMode: 437 clampedCoord.printf("clamp(%s, %s.xy, %s.zw)", coord, bounds, bounds); 438 coord = clampedCoord.c_str(); 439 break; 440 case SkMatrixConvolutionImageFilter::kRepeat_TileMode: 441 clampedCoord.printf("mod(%s - %s.xy, %s.zw - %s.xy) + %s.xy", coord, bounds, bounds, bounds, bounds); 442 coord = clampedCoord.c_str(); 443 break; 444 case SkMatrixConvolutionImageFilter::kClampToBlack_TileMode: 445 builder->fsCodeAppendf("clamp(%s, %s.xy, %s.zw) != %s ? vec4(0, 0, 0, 0) : ", coord, bounds, bounds, coord); 446 break; 447 } 448 builder->fsAppendTextureLookup(sampler, coord); 449} 450 451void GrGLMatrixConvolutionEffect::emitCode(GrGLShaderBuilder* builder, 452 const GrDrawEffect&, 453 EffectKey key, 454 const char* outputColor, 455 const char* inputColor, 456 const TransformedCoordsArray& coords, 457 const TextureSamplerArray& samplers) { 458 sk_ignore_unused_variable(inputColor); 459 SkString coords2D = builder->ensureFSCoords2D(coords, 0); 460 fBoundsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, 461 kVec4f_GrSLType, "Bounds"); 462 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, 463 kVec2f_GrSLType, "ImageIncrement"); 464 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibility, 465 kFloat_GrSLType, 466 "Kernel", 467 fKernelSize.width() * fKernelSize.height()); 468 fKernelOffsetUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, 469 kVec2f_GrSLType, "KernelOffset"); 470 fGainUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, 471 kFloat_GrSLType, "Gain"); 472 fBiasUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, 473 kFloat_GrSLType, "Bias"); 474 475 const char* bounds = builder->getUniformCStr(fBoundsUni); 476 const char* kernelOffset = builder->getUniformCStr(fKernelOffsetUni); 477 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); 478 const char* kernel = builder->getUniformCStr(fKernelUni); 479 const char* gain = builder->getUniformCStr(fGainUni); 480 const char* bias = builder->getUniformCStr(fBiasUni); 481 int kWidth = fKernelSize.width(); 482 int kHeight = fKernelSize.height(); 483 484 builder->fsCodeAppend("\t\tvec4 sum = vec4(0, 0, 0, 0);\n"); 485 builder->fsCodeAppendf("\t\tvec2 coord = %s - %s * %s;\n", coords2D.c_str(), kernelOffset, imgInc); 486 builder->fsCodeAppendf("\t\tfor (int y = 0; y < %d; y++) {\n", kHeight); 487 builder->fsCodeAppendf("\t\t\tfor (int x = 0; x < %d; x++) {\n", kWidth); 488 builder->fsCodeAppendf("\t\t\t\tfloat k = %s[y * %d + x];\n", kernel, kWidth); 489 builder->fsCodeAppendf("\t\t\t\tvec2 coord2 = coord + vec2(x, y) * %s;\n", imgInc); 490 builder->fsCodeAppend("\t\t\t\tvec4 c = "); 491 appendTextureLookup(builder, samplers[0], "coord2", bounds, fTileMode); 492 builder->fsCodeAppend(";\n"); 493 if (!fConvolveAlpha) { 494 builder->fsCodeAppend("\t\t\t\tc.rgb /= c.a;\n"); 495 } 496 builder->fsCodeAppend("\t\t\t\tsum += c * k;\n"); 497 builder->fsCodeAppend("\t\t\t}\n"); 498 builder->fsCodeAppend("\t\t}\n"); 499 if (fConvolveAlpha) { 500 builder->fsCodeAppendf("\t\t%s = sum * %s + %s;\n", outputColor, gain, bias); 501 builder->fsCodeAppendf("\t\t%s.rgb = clamp(%s.rgb, 0.0, %s.a);\n", 502 outputColor, outputColor, outputColor); 503 } else { 504 builder->fsCodeAppend("\t\tvec4 c = "); 505 appendTextureLookup(builder, samplers[0], coords2D.c_str(), bounds, fTileMode); 506 builder->fsCodeAppend(";\n"); 507 builder->fsCodeAppendf("\t\t%s.a = c.a;\n", outputColor); 508 builder->fsCodeAppendf("\t\t%s.rgb = sum.rgb * %s + %s;\n", outputColor, gain, bias); 509 builder->fsCodeAppendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor); 510 } 511} 512 513namespace { 514 515int encodeXY(int x, int y) { 516 SkASSERT(x >= 1 && y >= 1 && x * y <= 32); 517 if (y < x) 518 return 0x40 | encodeXY(y, x); 519 else 520 return (0x40 >> x) | (y - x); 521} 522 523}; 524 525GrGLEffect::EffectKey GrGLMatrixConvolutionEffect::GenKey(const GrDrawEffect& drawEffect, 526 const GrGLCaps&) { 527 const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvolutionEffect>(); 528 EffectKey key = encodeXY(m.kernelSize().width(), m.kernelSize().height()); 529 key |= m.tileMode() << 7; 530 key |= m.convolveAlpha() ? 1 << 9 : 0; 531 return key; 532} 533 534void GrGLMatrixConvolutionEffect::setData(const GrGLUniformManager& uman, 535 const GrDrawEffect& drawEffect) { 536 const GrMatrixConvolutionEffect& conv = drawEffect.castEffect<GrMatrixConvolutionEffect>(); 537 GrTexture& texture = *conv.texture(0); 538 // the code we generated was for a specific kernel size 539 SkASSERT(conv.kernelSize() == fKernelSize); 540 SkASSERT(conv.tileMode() == fTileMode); 541 float imageIncrement[2]; 542 float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; 543 imageIncrement[0] = 1.0f / texture.width(); 544 imageIncrement[1] = ySign / texture.height(); 545 uman.set2fv(fImageIncrementUni, 1, imageIncrement); 546 uman.set2fv(fKernelOffsetUni, 1, conv.kernelOffset()); 547 uman.set1fv(fKernelUni, fKernelSize.width() * fKernelSize.height(), conv.kernel()); 548 uman.set1f(fGainUni, conv.gain()); 549 uman.set1f(fBiasUni, conv.bias()); 550 const SkIRect& bounds = conv.bounds(); 551 float left = (float) bounds.left() / texture.width(); 552 float top = (float) bounds.top() / texture.height(); 553 float right = (float) bounds.right() / texture.width(); 554 float bottom = (float) bounds.bottom() / texture.height(); 555 if (texture.origin() == kBottomLeft_GrSurfaceOrigin) { 556 uman.set4f(fBoundsUni, left, 1.0f - bottom, right, 1.0f - top); 557 } else { 558 uman.set4f(fBoundsUni, left, top, right, bottom); 559 } 560} 561 562GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture, 563 const SkIRect& bounds, 564 const SkISize& kernelSize, 565 const SkScalar* kernel, 566 SkScalar gain, 567 SkScalar bias, 568 const SkIPoint& kernelOffset, 569 TileMode tileMode, 570 bool convolveAlpha) 571 : INHERITED(texture, MakeDivByTextureWHMatrix(texture)), 572 fBounds(bounds), 573 fKernelSize(kernelSize), 574 fGain(SkScalarToFloat(gain)), 575 fBias(SkScalarToFloat(bias) / 255.0f), 576 fTileMode(tileMode), 577 fConvolveAlpha(convolveAlpha) { 578 fKernel = new float[kernelSize.width() * kernelSize.height()]; 579 for (int i = 0; i < kernelSize.width() * kernelSize.height(); i++) { 580 fKernel[i] = SkScalarToFloat(kernel[i]); 581 } 582 fKernelOffset[0] = static_cast<float>(kernelOffset.x()); 583 fKernelOffset[1] = static_cast<float>(kernelOffset.y()); 584 this->setWillNotUseInputColor(); 585} 586 587GrMatrixConvolutionEffect::~GrMatrixConvolutionEffect() { 588 delete[] fKernel; 589} 590 591const GrBackendEffectFactory& GrMatrixConvolutionEffect::getFactory() const { 592 return GrTBackendEffectFactory<GrMatrixConvolutionEffect>::getInstance(); 593} 594 595bool GrMatrixConvolutionEffect::onIsEqual(const GrEffect& sBase) const { 596 const GrMatrixConvolutionEffect& s = CastEffect<GrMatrixConvolutionEffect>(sBase); 597 return this->texture(0) == s.texture(0) && 598 fKernelSize == s.kernelSize() && 599 !memcmp(fKernel, s.kernel(), 600 fKernelSize.width() * fKernelSize.height() * sizeof(float)) && 601 fGain == s.gain() && 602 fBias == s.bias() && 603 fKernelOffset == s.kernelOffset() && 604 fTileMode == s.tileMode() && 605 fConvolveAlpha == s.convolveAlpha(); 606} 607 608GR_DEFINE_EFFECT_TEST(GrMatrixConvolutionEffect); 609 610// A little bit less than the minimum # uniforms required by DX9SM2 (32). 611// Allows for a 5x5 kernel (or 25x1, for that matter). 612#define MAX_KERNEL_SIZE 25 613 614GrEffectRef* GrMatrixConvolutionEffect::TestCreate(SkRandom* random, 615 GrContext* context, 616 const GrDrawTargetCaps&, 617 GrTexture* textures[]) { 618 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : 619 GrEffectUnitTest::kAlphaTextureIdx; 620 int width = random->nextRangeU(1, MAX_KERNEL_SIZE); 621 int height = random->nextRangeU(1, MAX_KERNEL_SIZE / width); 622 SkISize kernelSize = SkISize::Make(width, height); 623 SkAutoTDeleteArray<SkScalar> kernel(new SkScalar[width * height]); 624 for (int i = 0; i < width * height; i++) { 625 kernel.get()[i] = random->nextSScalar1(); 626 } 627 SkScalar gain = random->nextSScalar1(); 628 SkScalar bias = random->nextSScalar1(); 629 SkIPoint kernelOffset = SkIPoint::Make(random->nextRangeU(0, kernelSize.width()), 630 random->nextRangeU(0, kernelSize.height())); 631 SkIRect bounds = SkIRect::MakeXYWH(random->nextRangeU(0, textures[texIdx]->width()), 632 random->nextRangeU(0, textures[texIdx]->height()), 633 random->nextRangeU(0, textures[texIdx]->width()), 634 random->nextRangeU(0, textures[texIdx]->height())); 635 TileMode tileMode = static_cast<TileMode>(random->nextRangeU(0, 2)); 636 bool convolveAlpha = random->nextBool(); 637 return GrMatrixConvolutionEffect::Create(textures[texIdx], 638 bounds, 639 kernelSize, 640 kernel.get(), 641 gain, 642 bias, 643 kernelOffset, 644 tileMode, 645 convolveAlpha); 646} 647 648bool SkMatrixConvolutionImageFilter::asNewEffect(GrEffectRef** effect, 649 GrTexture* texture, 650 const SkMatrix&, 651 const SkIRect& bounds 652 ) const { 653 if (!effect) { 654 return fKernelSize.width() * fKernelSize.height() <= MAX_KERNEL_SIZE; 655 } 656 SkASSERT(fKernelSize.width() * fKernelSize.height() <= MAX_KERNEL_SIZE); 657 *effect = GrMatrixConvolutionEffect::Create(texture, 658 bounds, 659 fKernelSize, 660 fKernel, 661 fGain, 662 fBias, 663 fKernelOffset, 664 fTileMode, 665 fConvolveAlpha); 666 return true; 667} 668 669/////////////////////////////////////////////////////////////////////////////// 670 671#endif 672