OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
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 #ifndef GrConvolutionEffect_DEFINED | 8 #ifndef GrConvolutionEffect_DEFINED |
9 #define GrConvolutionEffect_DEFINED | 9 #define GrConvolutionEffect_DEFINED |
10 | 10 |
11 #include "Gr1DKernelEffect.h" | 11 #include "Gr1DKernelEffect.h" |
12 | 12 |
13 class GrGLConvolutionEffect; | 13 class GrGLConvolutionEffect; |
14 | 14 |
15 /** | 15 /** |
16 * A convolution effect. The kernel is specified as an array of 2 * half-width | 16 * A convolution effect. The kernel is specified as an array of 2 * half-width |
17 * + 1 weights. Each texel is multiplied by it's weight and summed to determine | 17 * + 1 weights. Each texel is multiplied by it's weight and summed to determine |
18 * the output color. The output color is modulated by the input color. | 18 * the output color. The output color is modulated by the input color. |
19 */ | 19 */ |
20 class GrConvolutionEffect : public Gr1DKernelEffect { | 20 class GrConvolutionEffect : public Gr1DKernelEffect { |
21 | 21 |
22 public: | 22 public: |
23 | 23 |
24 /// Convolve with an arbitrary user-specified kernel | 24 /// Convolve with an arbitrary user-specified kernel |
25 static GrEffectRef* Create(GrTexture* tex, Direction dir, int halfWidth, con
st float* kernel) { | 25 static GrEffectRef* Create(GrTexture* tex, |
| 26 Direction dir, |
| 27 int halfWidth, |
| 28 const float* kernel, |
| 29 bool useCropRect, |
| 30 float cropRect[4]) { |
26 AutoEffectUnref effect(SkNEW_ARGS(GrConvolutionEffect, (tex, | 31 AutoEffectUnref effect(SkNEW_ARGS(GrConvolutionEffect, (tex, |
27 dir, | 32 dir, |
28 halfWidth, | 33 halfWidth, |
29 kernel))); | 34 kernel, |
| 35 useCropRect, |
| 36 cropRect))); |
30 return CreateEffectRef(effect); | 37 return CreateEffectRef(effect); |
31 } | 38 } |
32 | 39 |
33 /// Convolve with a Gaussian kernel | 40 /// Convolve with a Gaussian kernel |
34 static GrEffectRef* CreateGaussian(GrTexture* tex, | 41 static GrEffectRef* CreateGaussian(GrTexture* tex, |
35 Direction dir, | 42 Direction dir, |
36 int halfWidth, | 43 int halfWidth, |
37 float gaussianSigma) { | 44 float gaussianSigma, |
| 45 bool useCropRect, |
| 46 float cropRect[4]) { |
38 AutoEffectUnref effect(SkNEW_ARGS(GrConvolutionEffect, (tex, | 47 AutoEffectUnref effect(SkNEW_ARGS(GrConvolutionEffect, (tex, |
39 dir, | 48 dir, |
40 halfWidth, | 49 halfWidth, |
41 gaussianSigma)))
; | 50 gaussianSigma, |
| 51 useCropRect, |
| 52 cropRect))); |
42 return CreateEffectRef(effect); | 53 return CreateEffectRef(effect); |
43 } | 54 } |
44 | 55 |
45 virtual ~GrConvolutionEffect(); | 56 virtual ~GrConvolutionEffect(); |
46 | 57 |
47 const float* kernel() const { return fKernel; } | 58 const float* kernel() const { return fKernel; } |
48 | 59 |
| 60 const float* cropRect() const { return fCropRect; } |
| 61 bool useCropRect() const { return fUseCropRect; } |
| 62 |
49 static const char* Name() { return "Convolution"; } | 63 static const char* Name() { return "Convolution"; } |
50 | 64 |
51 typedef GrGLConvolutionEffect GLEffect; | 65 typedef GrGLConvolutionEffect GLEffect; |
52 | 66 |
53 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; | 67 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
54 | 68 |
55 virtual void getConstantColorComponents(GrColor*, uint32_t* validFlags) cons
t { | 69 virtual void getConstantColorComponents(GrColor*, uint32_t* validFlags) cons
t { |
56 // If the texture was opaque we could know that the output color if we k
new the sum of the | 70 // If the texture was opaque we could know that the output color if we k
new the sum of the |
57 // kernel values. | 71 // kernel values. |
58 *validFlags = 0; | 72 *validFlags = 0; |
59 } | 73 } |
60 | 74 |
61 enum { | 75 enum { |
62 // This was decided based on the min allowed value for the max texture | 76 // This was decided based on the min allowed value for the max texture |
63 // samples per fragment program run in DX9SM2 (32). A sigma param of 4.0 | 77 // samples per fragment program run in DX9SM2 (32). A sigma param of 4.0 |
64 // on a blur filter gives a kernel width of 25 while a sigma of 5.0 | 78 // on a blur filter gives a kernel width of 25 while a sigma of 5.0 |
65 // would exceed a 32 wide kernel. | 79 // would exceed a 32 wide kernel. |
66 kMaxKernelRadius = 12, | 80 kMaxKernelRadius = 12, |
67 // With a C++11 we could have a constexpr version of WidthFromRadius() | 81 // With a C++11 we could have a constexpr version of WidthFromRadius() |
68 // and not have to duplicate this calculation. | 82 // and not have to duplicate this calculation. |
69 kMaxKernelWidth = 2 * kMaxKernelRadius + 1, | 83 kMaxKernelWidth = 2 * kMaxKernelRadius + 1, |
70 }; | 84 }; |
71 | 85 |
72 protected: | 86 protected: |
73 | 87 |
74 float fKernel[kMaxKernelWidth]; | 88 float fKernel[kMaxKernelWidth]; |
| 89 bool fUseCropRect; |
| 90 float fCropRect[4]; |
75 | 91 |
76 private: | 92 private: |
77 GrConvolutionEffect(GrTexture*, Direction, | 93 GrConvolutionEffect(GrTexture*, Direction, |
78 int halfWidth, const float* kernel); | 94 int halfWidth, |
| 95 const float* kernel, |
| 96 bool useCropRect, |
| 97 float cropRect[4]); |
79 | 98 |
80 /// Convolve with a Gaussian kernel | 99 /// Convolve with a Gaussian kernel |
81 GrConvolutionEffect(GrTexture*, Direction, | 100 GrConvolutionEffect(GrTexture*, Direction, |
82 int halfWidth, | 101 int halfWidth, |
83 float gaussianSigma); | 102 float gaussianSigma, |
| 103 bool useCropRect, |
| 104 float cropRect[4]); |
84 | 105 |
85 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; | 106 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
86 | 107 |
87 GR_DECLARE_EFFECT_TEST; | 108 GR_DECLARE_EFFECT_TEST; |
88 | 109 |
89 typedef Gr1DKernelEffect INHERITED; | 110 typedef Gr1DKernelEffect INHERITED; |
90 }; | 111 }; |
91 | 112 |
92 #endif | 113 #endif |
OLD | NEW |