OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 The Android Open Source Project | 2 * Copyright 2012 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkMatrixConvolutionImageFilter.h" | 8 #include "SkMatrixConvolutionImageFilter.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 break; | 31 break; |
32 } | 32 } |
33 return false; | 33 return false; |
34 } | 34 } |
35 | 35 |
36 SkMatrixConvolutionImageFilter::SkMatrixConvolutionImageFilter( | 36 SkMatrixConvolutionImageFilter::SkMatrixConvolutionImageFilter( |
37 const SkISize& kernelSize, | 37 const SkISize& kernelSize, |
38 const SkScalar* kernel, | 38 const SkScalar* kernel, |
39 SkScalar gain, | 39 SkScalar gain, |
40 SkScalar bias, | 40 SkScalar bias, |
41 const SkIPoint& target, | 41 const SkIPoint& kernelOffset, |
42 TileMode tileMode, | 42 TileMode tileMode, |
43 bool convolveAlpha, | 43 bool convolveAlpha, |
44 SkImageFilter* input, | 44 SkImageFilter* input, |
45 const CropRect* cropRect) | 45 const CropRect* cropRect) |
46 : INHERITED(input, cropRect), | 46 : INHERITED(input, cropRect), |
47 fKernelSize(kernelSize), | 47 fKernelSize(kernelSize), |
48 fGain(gain), | 48 fGain(gain), |
49 fBias(bias), | 49 fBias(bias), |
50 fTarget(target), | 50 fKernelOffset(kernelOffset), |
51 fTileMode(tileMode), | 51 fTileMode(tileMode), |
52 fConvolveAlpha(convolveAlpha) { | 52 fConvolveAlpha(convolveAlpha) { |
53 uint32_t size = fKernelSize.fWidth * fKernelSize.fHeight; | 53 uint32_t size = fKernelSize.fWidth * fKernelSize.fHeight; |
54 fKernel = SkNEW_ARRAY(SkScalar, size); | 54 fKernel = SkNEW_ARRAY(SkScalar, size); |
55 memcpy(fKernel, kernel, size * sizeof(SkScalar)); | 55 memcpy(fKernel, kernel, size * sizeof(SkScalar)); |
56 SkASSERT(kernelSize.fWidth >= 1 && kernelSize.fHeight >= 1); | 56 SkASSERT(kernelSize.fWidth >= 1 && kernelSize.fHeight >= 1); |
57 SkASSERT(target.fX >= 0 && target.fX < kernelSize.fWidth); | 57 SkASSERT(kernelOffset.fX >= 0 && kernelOffset.fX < kernelSize.fWidth); |
58 SkASSERT(target.fY >= 0 && target.fY < kernelSize.fHeight); | 58 SkASSERT(kernelOffset.fY >= 0 && kernelOffset.fY < kernelSize.fHeight); |
59 } | 59 } |
60 | 60 |
61 SkMatrixConvolutionImageFilter::SkMatrixConvolutionImageFilter(SkReadBuffer& buf
fer) | 61 SkMatrixConvolutionImageFilter::SkMatrixConvolutionImageFilter(SkReadBuffer& buf
fer) |
62 : INHERITED(1, buffer) { | 62 : INHERITED(1, buffer) { |
63 // We need to be able to read at most SK_MaxS32 bytes, so divide that | 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. | 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); | 65 static const int32_t kMaxSize = SK_MaxS32 / sizeof(SkScalar); |
66 fKernelSize.fWidth = buffer.readInt(); | 66 fKernelSize.fWidth = buffer.readInt(); |
67 fKernelSize.fHeight = buffer.readInt(); | 67 fKernelSize.fHeight = buffer.readInt(); |
68 if ((fKernelSize.fWidth >= 1) && (fKernelSize.fHeight >= 1) && | 68 if ((fKernelSize.fWidth >= 1) && (fKernelSize.fHeight >= 1) && |
69 // Make sure size won't be larger than a signed int, | 69 // Make sure size won't be larger than a signed int, |
70 // which would still be extremely large for a kernel, | 70 // which would still be extremely large for a kernel, |
71 // but we don't impose a hard limit for kernel size | 71 // but we don't impose a hard limit for kernel size |
72 (kMaxSize / fKernelSize.fWidth >= fKernelSize.fHeight)) { | 72 (kMaxSize / fKernelSize.fWidth >= fKernelSize.fHeight)) { |
73 size_t size = fKernelSize.fWidth * fKernelSize.fHeight; | 73 size_t size = fKernelSize.fWidth * fKernelSize.fHeight; |
74 fKernel = SkNEW_ARRAY(SkScalar, size); | 74 fKernel = SkNEW_ARRAY(SkScalar, size); |
75 SkDEBUGCODE(bool success =) buffer.readScalarArray(fKernel, size); | 75 SkDEBUGCODE(bool success =) buffer.readScalarArray(fKernel, size); |
76 SkASSERT(success); | 76 SkASSERT(success); |
77 } else { | 77 } else { |
78 fKernel = 0; | 78 fKernel = 0; |
79 } | 79 } |
80 fGain = buffer.readScalar(); | 80 fGain = buffer.readScalar(); |
81 fBias = buffer.readScalar(); | 81 fBias = buffer.readScalar(); |
82 fTarget.fX = buffer.readInt(); | 82 fKernelOffset.fX = buffer.readInt(); |
83 fTarget.fY = buffer.readInt(); | 83 fKernelOffset.fY = buffer.readInt(); |
84 fTileMode = (TileMode) buffer.readInt(); | 84 fTileMode = (TileMode) buffer.readInt(); |
85 fConvolveAlpha = buffer.readBool(); | 85 fConvolveAlpha = buffer.readBool(); |
86 buffer.validate((fKernel != 0) && | 86 buffer.validate((fKernel != 0) && |
87 SkScalarIsFinite(fGain) && | 87 SkScalarIsFinite(fGain) && |
88 SkScalarIsFinite(fBias) && | 88 SkScalarIsFinite(fBias) && |
89 tile_mode_is_valid(fTileMode)); | 89 tile_mode_is_valid(fTileMode)); |
90 } | 90 } |
91 | 91 |
92 void SkMatrixConvolutionImageFilter::flatten(SkWriteBuffer& buffer) const { | 92 void SkMatrixConvolutionImageFilter::flatten(SkWriteBuffer& buffer) const { |
93 this->INHERITED::flatten(buffer); | 93 this->INHERITED::flatten(buffer); |
94 buffer.writeInt(fKernelSize.fWidth); | 94 buffer.writeInt(fKernelSize.fWidth); |
95 buffer.writeInt(fKernelSize.fHeight); | 95 buffer.writeInt(fKernelSize.fHeight); |
96 buffer.writeScalarArray(fKernel, fKernelSize.fWidth * fKernelSize.fHeight); | 96 buffer.writeScalarArray(fKernel, fKernelSize.fWidth * fKernelSize.fHeight); |
97 buffer.writeScalar(fGain); | 97 buffer.writeScalar(fGain); |
98 buffer.writeScalar(fBias); | 98 buffer.writeScalar(fBias); |
99 buffer.writeInt(fTarget.fX); | 99 buffer.writeInt(fKernelOffset.fX); |
100 buffer.writeInt(fTarget.fY); | 100 buffer.writeInt(fKernelOffset.fY); |
101 buffer.writeInt((int) fTileMode); | 101 buffer.writeInt((int) fTileMode); |
102 buffer.writeBool(fConvolveAlpha); | 102 buffer.writeBool(fConvolveAlpha); |
103 } | 103 } |
104 | 104 |
105 SkMatrixConvolutionImageFilter::~SkMatrixConvolutionImageFilter() { | 105 SkMatrixConvolutionImageFilter::~SkMatrixConvolutionImageFilter() { |
106 delete[] fKernel; | 106 delete[] fKernel; |
107 } | 107 } |
108 | 108 |
109 class UncheckedPixelFetcher { | 109 class UncheckedPixelFetcher { |
110 public: | 110 public: |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 SkBitmap* result, | 153 SkBitmap* result, |
154 const SkIRect& rect, | 154 const SkIRect& rect, |
155 const SkIRect& bounds) const { | 155 const SkIRect& bounds) const { |
156 for (int y = rect.fTop; y < rect.fBottom; ++y) { | 156 for (int y = rect.fTop; y < rect.fBottom; ++y) { |
157 SkPMColor* dptr = result->getAddr32(rect.fLeft - bounds.fLeft, y - bound
s.fTop); | 157 SkPMColor* dptr = result->getAddr32(rect.fLeft - bounds.fLeft, y - bound
s.fTop); |
158 for (int x = rect.fLeft; x < rect.fRight; ++x) { | 158 for (int x = rect.fLeft; x < rect.fRight; ++x) { |
159 SkScalar sumA = 0, sumR = 0, sumG = 0, sumB = 0; | 159 SkScalar sumA = 0, sumR = 0, sumG = 0, sumB = 0; |
160 for (int cy = 0; cy < fKernelSize.fHeight; cy++) { | 160 for (int cy = 0; cy < fKernelSize.fHeight; cy++) { |
161 for (int cx = 0; cx < fKernelSize.fWidth; cx++) { | 161 for (int cx = 0; cx < fKernelSize.fWidth; cx++) { |
162 SkPMColor s = PixelFetcher::fetch(src, | 162 SkPMColor s = PixelFetcher::fetch(src, |
163 x + cx - fTarget.fX, | 163 x + cx - fKernelOffset.fX, |
164 y + cy - fTarget.fY, | 164 y + cy - fKernelOffset.fY, |
165 bounds); | 165 bounds); |
166 SkScalar k = fKernel[cy * fKernelSize.fWidth + cx]; | 166 SkScalar k = fKernel[cy * fKernelSize.fWidth + cx]; |
167 if (convolveAlpha) { | 167 if (convolveAlpha) { |
168 sumA += SkScalarMul(SkIntToScalar(SkGetPackedA32(s)), k)
; | 168 sumA += SkScalarMul(SkIntToScalar(SkGetPackedA32(s)), k)
; |
169 } | 169 } |
170 sumR += SkScalarMul(SkIntToScalar(SkGetPackedR32(s)), k); | 170 sumR += SkScalarMul(SkIntToScalar(SkGetPackedR32(s)), k); |
171 sumG += SkScalarMul(SkIntToScalar(SkGetPackedG32(s)), k); | 171 sumG += SkScalarMul(SkIntToScalar(SkGetPackedG32(s)), k); |
172 sumB += SkScalarMul(SkIntToScalar(SkGetPackedB32(s)), k); | 172 sumB += SkScalarMul(SkIntToScalar(SkGetPackedB32(s)), k); |
173 } | 173 } |
174 } | 174 } |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 | 282 |
283 result->setConfig(src.config(), bounds.width(), bounds.height()); | 283 result->setConfig(src.config(), bounds.width(), bounds.height()); |
284 result->allocPixels(); | 284 result->allocPixels(); |
285 if (!result->getPixels()) { | 285 if (!result->getPixels()) { |
286 return false; | 286 return false; |
287 } | 287 } |
288 | 288 |
289 offset->fX = bounds.fLeft; | 289 offset->fX = bounds.fLeft; |
290 offset->fY = bounds.fTop; | 290 offset->fY = bounds.fTop; |
291 bounds.offset(-srcOffset); | 291 bounds.offset(-srcOffset); |
292 SkIRect interior = SkIRect::MakeXYWH(bounds.left() + fTarget.fX, | 292 SkIRect interior = SkIRect::MakeXYWH(bounds.left() + fKernelOffset.fX, |
293 bounds.top() + fTarget.fY, | 293 bounds.top() + fKernelOffset.fY, |
294 bounds.width() - fKernelSize.fWidth + 1
, | 294 bounds.width() - fKernelSize.fWidth + 1
, |
295 bounds.height() - fKernelSize.fHeight +
1); | 295 bounds.height() - fKernelSize.fHeight +
1); |
296 SkIRect top = SkIRect::MakeLTRB(bounds.left(), bounds.top(), bounds.right(),
interior.top()); | 296 SkIRect top = SkIRect::MakeLTRB(bounds.left(), bounds.top(), bounds.right(),
interior.top()); |
297 SkIRect bottom = SkIRect::MakeLTRB(bounds.left(), interior.bottom(), | 297 SkIRect bottom = SkIRect::MakeLTRB(bounds.left(), interior.bottom(), |
298 bounds.right(), bounds.bottom()); | 298 bounds.right(), bounds.bottom()); |
299 SkIRect left = SkIRect::MakeLTRB(bounds.left(), interior.top(), | 299 SkIRect left = SkIRect::MakeLTRB(bounds.left(), interior.top(), |
300 interior.left(), interior.bottom()); | 300 interior.left(), interior.bottom()); |
301 SkIRect right = SkIRect::MakeLTRB(interior.right(), interior.top(), | 301 SkIRect right = SkIRect::MakeLTRB(interior.right(), interior.top(), |
302 bounds.right(), interior.bottom()); | 302 bounds.right(), interior.bottom()); |
303 filterBorderPixels(src, result, top, bounds); | 303 filterBorderPixels(src, result, top, bounds); |
(...skipping 12 matching lines...) Expand all Loading... |
316 | 316 |
317 class GrMatrixConvolutionEffect : public GrSingleTextureEffect { | 317 class GrMatrixConvolutionEffect : public GrSingleTextureEffect { |
318 public: | 318 public: |
319 typedef SkMatrixConvolutionImageFilter::TileMode TileMode; | 319 typedef SkMatrixConvolutionImageFilter::TileMode TileMode; |
320 static GrEffectRef* Create(GrTexture* texture, | 320 static GrEffectRef* Create(GrTexture* texture, |
321 const SkIRect& bounds, | 321 const SkIRect& bounds, |
322 const SkISize& kernelSize, | 322 const SkISize& kernelSize, |
323 const SkScalar* kernel, | 323 const SkScalar* kernel, |
324 SkScalar gain, | 324 SkScalar gain, |
325 SkScalar bias, | 325 SkScalar bias, |
326 const SkIPoint& target, | 326 const SkIPoint& kernelOffset, |
327 TileMode tileMode, | 327 TileMode tileMode, |
328 bool convolveAlpha) { | 328 bool convolveAlpha) { |
329 AutoEffectUnref effect(SkNEW_ARGS(GrMatrixConvolutionEffect, (texture, | 329 AutoEffectUnref effect(SkNEW_ARGS(GrMatrixConvolutionEffect, (texture, |
330 bounds, | 330 bounds, |
331 kernelSize
, | 331 kernelSize
, |
332 kernel, | 332 kernel, |
333 gain, | 333 gain, |
334 bias, | 334 bias, |
335 target, | 335 kernelOffs
et, |
336 tileMode, | 336 tileMode, |
337 convolveAl
pha))); | 337 convolveAl
pha))); |
338 return CreateEffectRef(effect); | 338 return CreateEffectRef(effect); |
339 } | 339 } |
340 virtual ~GrMatrixConvolutionEffect(); | 340 virtual ~GrMatrixConvolutionEffect(); |
341 | 341 |
342 virtual void getConstantColorComponents(GrColor* color, | 342 virtual void getConstantColorComponents(GrColor* color, |
343 uint32_t* validFlags) const SK_OVERR
IDE { | 343 uint32_t* validFlags) const SK_OVERR
IDE { |
344 // TODO: Try to do better? | 344 // TODO: Try to do better? |
345 *validFlags = 0; | 345 *validFlags = 0; |
346 } | 346 } |
347 | 347 |
348 static const char* Name() { return "MatrixConvolution"; } | 348 static const char* Name() { return "MatrixConvolution"; } |
349 const SkIRect& bounds() const { return fBounds; } | 349 const SkIRect& bounds() const { return fBounds; } |
350 const SkISize& kernelSize() const { return fKernelSize; } | 350 const SkISize& kernelSize() const { return fKernelSize; } |
351 const float* target() const { return fTarget; } | 351 const float* kernelOffset() const { return fKernelOffset; } |
352 const float* kernel() const { return fKernel; } | 352 const float* kernel() const { return fKernel; } |
353 float gain() const { return fGain; } | 353 float gain() const { return fGain; } |
354 float bias() const { return fBias; } | 354 float bias() const { return fBias; } |
355 TileMode tileMode() const { return fTileMode; } | 355 TileMode tileMode() const { return fTileMode; } |
356 bool convolveAlpha() const { return fConvolveAlpha; } | 356 bool convolveAlpha() const { return fConvolveAlpha; } |
357 | 357 |
358 typedef GrGLMatrixConvolutionEffect GLEffect; | 358 typedef GrGLMatrixConvolutionEffect GLEffect; |
359 | 359 |
360 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; | 360 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
361 | 361 |
362 private: | 362 private: |
363 GrMatrixConvolutionEffect(GrTexture*, | 363 GrMatrixConvolutionEffect(GrTexture*, |
364 const SkIRect& bounds, | 364 const SkIRect& bounds, |
365 const SkISize& kernelSize, | 365 const SkISize& kernelSize, |
366 const SkScalar* kernel, | 366 const SkScalar* kernel, |
367 SkScalar gain, | 367 SkScalar gain, |
368 SkScalar bias, | 368 SkScalar bias, |
369 const SkIPoint& target, | 369 const SkIPoint& kernelOffset, |
370 TileMode tileMode, | 370 TileMode tileMode, |
371 bool convolveAlpha); | 371 bool convolveAlpha); |
372 | 372 |
373 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; | 373 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
374 | 374 |
375 SkIRect fBounds; | 375 SkIRect fBounds; |
376 SkISize fKernelSize; | 376 SkISize fKernelSize; |
377 float *fKernel; | 377 float *fKernel; |
378 float fGain; | 378 float fGain; |
379 float fBias; | 379 float fBias; |
380 float fTarget[2]; | 380 float fKernelOffset[2]; |
381 TileMode fTileMode; | 381 TileMode fTileMode; |
382 bool fConvolveAlpha; | 382 bool fConvolveAlpha; |
383 | 383 |
384 GR_DECLARE_EFFECT_TEST; | 384 GR_DECLARE_EFFECT_TEST; |
385 | 385 |
386 typedef GrSingleTextureEffect INHERITED; | 386 typedef GrSingleTextureEffect INHERITED; |
387 }; | 387 }; |
388 | 388 |
389 class GrGLMatrixConvolutionEffect : public GrGLEffect { | 389 class GrGLMatrixConvolutionEffect : public GrGLEffect { |
390 public: | 390 public: |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 "Kernel", | 468 "Kernel", |
469 fKernelSize.width() * fKernelSize.h
eight()); | 469 fKernelSize.width() * fKernelSize.h
eight()); |
470 fTargetUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, | 470 fTargetUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
471 kVec2f_GrSLType, "Target"); | 471 kVec2f_GrSLType, "Target"); |
472 fGainUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, | 472 fGainUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
473 kFloat_GrSLType, "Gain"); | 473 kFloat_GrSLType, "Gain"); |
474 fBiasUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, | 474 fBiasUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
475 kFloat_GrSLType, "Bias"); | 475 kFloat_GrSLType, "Bias"); |
476 | 476 |
477 const char* bounds = builder->getUniformCStr(fBoundsUni); | 477 const char* bounds = builder->getUniformCStr(fBoundsUni); |
478 const char* target = builder->getUniformCStr(fTargetUni); | 478 const char* kernelOffset = builder->getUniformCStr(fTargetUni); |
479 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); | 479 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
480 const char* kernel = builder->getUniformCStr(fKernelUni); | 480 const char* kernel = builder->getUniformCStr(fKernelUni); |
481 const char* gain = builder->getUniformCStr(fGainUni); | 481 const char* gain = builder->getUniformCStr(fGainUni); |
482 const char* bias = builder->getUniformCStr(fBiasUni); | 482 const char* bias = builder->getUniformCStr(fBiasUni); |
483 int kWidth = fKernelSize.width(); | 483 int kWidth = fKernelSize.width(); |
484 int kHeight = fKernelSize.height(); | 484 int kHeight = fKernelSize.height(); |
485 | 485 |
486 builder->fsCodeAppend("\t\tvec4 sum = vec4(0, 0, 0, 0);\n"); | 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(),
target, imgInc); | 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); | 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); | 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
); | 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", i
mgInc); | 491 builder->fsCodeAppendf("\t\t\t\tvec2 coord2 = coord + vec2(x, y) * %s;\n", i
mgInc); |
492 builder->fsCodeAppend("\t\t\t\tvec4 c = "); | 492 builder->fsCodeAppend("\t\t\t\tvec4 c = "); |
493 appendTextureLookup(builder, samplers[0], "coord2", bounds, fTileMode); | 493 appendTextureLookup(builder, samplers[0], "coord2", bounds, fTileMode); |
494 builder->fsCodeAppend(";\n"); | 494 builder->fsCodeAppend(";\n"); |
495 if (!fConvolveAlpha) { | 495 if (!fConvolveAlpha) { |
496 builder->fsCodeAppend("\t\t\t\tc.rgb /= c.a;\n"); | 496 builder->fsCodeAppend("\t\t\t\tc.rgb /= c.a;\n"); |
497 } | 497 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 const GrMatrixConvolutionEffect& conv = drawEffect.castEffect<GrMatrixConvol
utionEffect>(); | 538 const GrMatrixConvolutionEffect& conv = drawEffect.castEffect<GrMatrixConvol
utionEffect>(); |
539 GrTexture& texture = *conv.texture(0); | 539 GrTexture& texture = *conv.texture(0); |
540 // the code we generated was for a specific kernel size | 540 // the code we generated was for a specific kernel size |
541 SkASSERT(conv.kernelSize() == fKernelSize); | 541 SkASSERT(conv.kernelSize() == fKernelSize); |
542 SkASSERT(conv.tileMode() == fTileMode); | 542 SkASSERT(conv.tileMode() == fTileMode); |
543 float imageIncrement[2]; | 543 float imageIncrement[2]; |
544 float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; | 544 float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; |
545 imageIncrement[0] = 1.0f / texture.width(); | 545 imageIncrement[0] = 1.0f / texture.width(); |
546 imageIncrement[1] = ySign / texture.height(); | 546 imageIncrement[1] = ySign / texture.height(); |
547 uman.set2fv(fImageIncrementUni, 1, imageIncrement); | 547 uman.set2fv(fImageIncrementUni, 1, imageIncrement); |
548 uman.set2fv(fTargetUni, 1, conv.target()); | 548 uman.set2fv(fTargetUni, 1, conv.kernelOffset()); |
549 uman.set1fv(fKernelUni, fKernelSize.width() * fKernelSize.height(), conv.ker
nel()); | 549 uman.set1fv(fKernelUni, fKernelSize.width() * fKernelSize.height(), conv.ker
nel()); |
550 uman.set1f(fGainUni, conv.gain()); | 550 uman.set1f(fGainUni, conv.gain()); |
551 uman.set1f(fBiasUni, conv.bias()); | 551 uman.set1f(fBiasUni, conv.bias()); |
552 const SkIRect& bounds = conv.bounds(); | 552 const SkIRect& bounds = conv.bounds(); |
553 float left = (float) bounds.left() / texture.width(); | 553 float left = (float) bounds.left() / texture.width(); |
554 float top = (float) bounds.top() / texture.height(); | 554 float top = (float) bounds.top() / texture.height(); |
555 float right = (float) bounds.right() / texture.width(); | 555 float right = (float) bounds.right() / texture.width(); |
556 float bottom = (float) bounds.bottom() / texture.height(); | 556 float bottom = (float) bounds.bottom() / texture.height(); |
557 if (texture.origin() == kBottomLeft_GrSurfaceOrigin) { | 557 if (texture.origin() == kBottomLeft_GrSurfaceOrigin) { |
558 uman.set4f(fBoundsUni, left, 1.0f - bottom, right, 1.0f - top); | 558 uman.set4f(fBoundsUni, left, 1.0f - bottom, right, 1.0f - top); |
559 } else { | 559 } else { |
560 uman.set4f(fBoundsUni, left, top, right, bottom); | 560 uman.set4f(fBoundsUni, left, top, right, bottom); |
561 } | 561 } |
562 } | 562 } |
563 | 563 |
564 GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture, | 564 GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture, |
565 const SkIRect& bounds, | 565 const SkIRect& bounds, |
566 const SkISize& kernelSize, | 566 const SkISize& kernelSize, |
567 const SkScalar* kernel, | 567 const SkScalar* kernel, |
568 SkScalar gain, | 568 SkScalar gain, |
569 SkScalar bias, | 569 SkScalar bias, |
570 const SkIPoint& target, | 570 const SkIPoint& kernelOffse
t, |
571 TileMode tileMode, | 571 TileMode tileMode, |
572 bool convolveAlpha) | 572 bool convolveAlpha) |
573 : INHERITED(texture, MakeDivByTextureWHMatrix(texture)), | 573 : INHERITED(texture, MakeDivByTextureWHMatrix(texture)), |
574 fBounds(bounds), | 574 fBounds(bounds), |
575 fKernelSize(kernelSize), | 575 fKernelSize(kernelSize), |
576 fGain(SkScalarToFloat(gain)), | 576 fGain(SkScalarToFloat(gain)), |
577 fBias(SkScalarToFloat(bias) / 255.0f), | 577 fBias(SkScalarToFloat(bias) / 255.0f), |
578 fTileMode(tileMode), | 578 fTileMode(tileMode), |
579 fConvolveAlpha(convolveAlpha) { | 579 fConvolveAlpha(convolveAlpha) { |
580 fKernel = new float[kernelSize.width() * kernelSize.height()]; | 580 fKernel = new float[kernelSize.width() * kernelSize.height()]; |
581 for (int i = 0; i < kernelSize.width() * kernelSize.height(); i++) { | 581 for (int i = 0; i < kernelSize.width() * kernelSize.height(); i++) { |
582 fKernel[i] = SkScalarToFloat(kernel[i]); | 582 fKernel[i] = SkScalarToFloat(kernel[i]); |
583 } | 583 } |
584 fTarget[0] = static_cast<float>(target.x()); | 584 fKernelOffset[0] = static_cast<float>(kernelOffset.x()); |
585 fTarget[1] = static_cast<float>(target.y()); | 585 fKernelOffset[1] = static_cast<float>(kernelOffset.y()); |
586 this->setWillNotUseInputColor(); | 586 this->setWillNotUseInputColor(); |
587 } | 587 } |
588 | 588 |
589 GrMatrixConvolutionEffect::~GrMatrixConvolutionEffect() { | 589 GrMatrixConvolutionEffect::~GrMatrixConvolutionEffect() { |
590 delete[] fKernel; | 590 delete[] fKernel; |
591 } | 591 } |
592 | 592 |
593 const GrBackendEffectFactory& GrMatrixConvolutionEffect::getFactory() const { | 593 const GrBackendEffectFactory& GrMatrixConvolutionEffect::getFactory() const { |
594 return GrTBackendEffectFactory<GrMatrixConvolutionEffect>::getInstance(); | 594 return GrTBackendEffectFactory<GrMatrixConvolutionEffect>::getInstance(); |
595 } | 595 } |
596 | 596 |
597 bool GrMatrixConvolutionEffect::onIsEqual(const GrEffect& sBase) const { | 597 bool GrMatrixConvolutionEffect::onIsEqual(const GrEffect& sBase) const { |
598 const GrMatrixConvolutionEffect& s = CastEffect<GrMatrixConvolutionEffect>(s
Base); | 598 const GrMatrixConvolutionEffect& s = CastEffect<GrMatrixConvolutionEffect>(s
Base); |
599 return this->texture(0) == s.texture(0) && | 599 return this->texture(0) == s.texture(0) && |
600 fKernelSize == s.kernelSize() && | 600 fKernelSize == s.kernelSize() && |
601 !memcmp(fKernel, s.kernel(), | 601 !memcmp(fKernel, s.kernel(), |
602 fKernelSize.width() * fKernelSize.height() * sizeof(float)) &
& | 602 fKernelSize.width() * fKernelSize.height() * sizeof(float)) &
& |
603 fGain == s.gain() && | 603 fGain == s.gain() && |
604 fBias == s.bias() && | 604 fBias == s.bias() && |
605 fTarget == s.target() && | 605 fKernelOffset == s.kernelOffset() && |
606 fTileMode == s.tileMode() && | 606 fTileMode == s.tileMode() && |
607 fConvolveAlpha == s.convolveAlpha(); | 607 fConvolveAlpha == s.convolveAlpha(); |
608 } | 608 } |
609 | 609 |
610 GR_DEFINE_EFFECT_TEST(GrMatrixConvolutionEffect); | 610 GR_DEFINE_EFFECT_TEST(GrMatrixConvolutionEffect); |
611 | 611 |
612 // A little bit less than the minimum # uniforms required by DX9SM2 (32). | 612 // A little bit less than the minimum # uniforms required by DX9SM2 (32). |
613 // Allows for a 5x5 kernel (or 25x1, for that matter). | 613 // Allows for a 5x5 kernel (or 25x1, for that matter). |
614 #define MAX_KERNEL_SIZE 25 | 614 #define MAX_KERNEL_SIZE 25 |
615 | 615 |
616 GrEffectRef* GrMatrixConvolutionEffect::TestCreate(SkRandom* random, | 616 GrEffectRef* GrMatrixConvolutionEffect::TestCreate(SkRandom* random, |
617 GrContext* context, | 617 GrContext* context, |
618 const GrDrawTargetCaps&, | 618 const GrDrawTargetCaps&, |
619 GrTexture* textures[]) { | 619 GrTexture* textures[]) { |
620 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : | 620 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
621 GrEffectUnitTest::kAlphaTextureIdx; | 621 GrEffectUnitTest::kAlphaTextureIdx; |
622 int width = random->nextRangeU(1, MAX_KERNEL_SIZE); | 622 int width = random->nextRangeU(1, MAX_KERNEL_SIZE); |
623 int height = random->nextRangeU(1, MAX_KERNEL_SIZE / width); | 623 int height = random->nextRangeU(1, MAX_KERNEL_SIZE / width); |
624 SkISize kernelSize = SkISize::Make(width, height); | 624 SkISize kernelSize = SkISize::Make(width, height); |
625 SkAutoTDeleteArray<SkScalar> kernel(new SkScalar[width * height]); | 625 SkAutoTDeleteArray<SkScalar> kernel(new SkScalar[width * height]); |
626 for (int i = 0; i < width * height; i++) { | 626 for (int i = 0; i < width * height; i++) { |
627 kernel.get()[i] = random->nextSScalar1(); | 627 kernel.get()[i] = random->nextSScalar1(); |
628 } | 628 } |
629 SkScalar gain = random->nextSScalar1(); | 629 SkScalar gain = random->nextSScalar1(); |
630 SkScalar bias = random->nextSScalar1(); | 630 SkScalar bias = random->nextSScalar1(); |
631 SkIPoint target = SkIPoint::Make(random->nextRangeU(0, kernelSize.width()), | 631 SkIPoint kernelOffset = SkIPoint::Make(random->nextRangeU(0, kernelSize.widt
h()), |
632 random->nextRangeU(0, kernelSize.height()))
; | 632 random->nextRangeU(0, kernelSize.heig
ht())); |
633 SkIRect bounds = SkIRect::MakeXYWH(random->nextRangeU(0, textures[texIdx]->w
idth()), | 633 SkIRect bounds = SkIRect::MakeXYWH(random->nextRangeU(0, textures[texIdx]->w
idth()), |
634 random->nextRangeU(0, textures[texIdx]->h
eight()), | 634 random->nextRangeU(0, textures[texIdx]->h
eight()), |
635 random->nextRangeU(0, textures[texIdx]->w
idth()), | 635 random->nextRangeU(0, textures[texIdx]->w
idth()), |
636 random->nextRangeU(0, textures[texIdx]->h
eight())); | 636 random->nextRangeU(0, textures[texIdx]->h
eight())); |
637 TileMode tileMode = static_cast<TileMode>(random->nextRangeU(0, 2)); | 637 TileMode tileMode = static_cast<TileMode>(random->nextRangeU(0, 2)); |
638 bool convolveAlpha = random->nextBool(); | 638 bool convolveAlpha = random->nextBool(); |
639 return GrMatrixConvolutionEffect::Create(textures[texIdx], | 639 return GrMatrixConvolutionEffect::Create(textures[texIdx], |
640 bounds, | 640 bounds, |
641 kernelSize, | 641 kernelSize, |
642 kernel.get(), | 642 kernel.get(), |
643 gain, | 643 gain, |
644 bias, | 644 bias, |
645 target, | 645 kernelOffset, |
646 tileMode, | 646 tileMode, |
647 convolveAlpha); | 647 convolveAlpha); |
648 } | 648 } |
649 | 649 |
650 bool SkMatrixConvolutionImageFilter::asNewEffect(GrEffectRef** effect, | 650 bool SkMatrixConvolutionImageFilter::asNewEffect(GrEffectRef** effect, |
651 GrTexture* texture, | 651 GrTexture* texture, |
652 const SkMatrix&, | 652 const SkMatrix&, |
653 const SkIRect& bounds | 653 const SkIRect& bounds |
654 ) const { | 654 ) const { |
655 if (!effect) { | 655 if (!effect) { |
656 return fKernelSize.width() * fKernelSize.height() <= MAX_KERNEL_SIZE; | 656 return fKernelSize.width() * fKernelSize.height() <= MAX_KERNEL_SIZE; |
657 } | 657 } |
658 SkASSERT(fKernelSize.width() * fKernelSize.height() <= MAX_KERNEL_SIZE); | 658 SkASSERT(fKernelSize.width() * fKernelSize.height() <= MAX_KERNEL_SIZE); |
659 *effect = GrMatrixConvolutionEffect::Create(texture, | 659 *effect = GrMatrixConvolutionEffect::Create(texture, |
660 bounds, | 660 bounds, |
661 fKernelSize, | 661 fKernelSize, |
662 fKernel, | 662 fKernel, |
663 fGain, | 663 fGain, |
664 fBias, | 664 fBias, |
665 fTarget, | 665 fKernelOffset, |
666 fTileMode, | 666 fTileMode, |
667 fConvolveAlpha); | 667 fConvolveAlpha); |
668 return true; | 668 return true; |
669 } | 669 } |
670 | 670 |
671 /////////////////////////////////////////////////////////////////////////////// | 671 /////////////////////////////////////////////////////////////////////////////// |
672 | 672 |
673 #endif | 673 #endif |
OLD | NEW |