OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 #include "GrMatrixConvolutionEffect.h" | 7 #include "GrMatrixConvolutionEffect.h" |
8 #include "gl/GrGLFragmentProcessor.h" | 8 #include "gl/GrGLFragmentProcessor.h" |
9 #include "gl/GrGLTexture.h" | 9 #include "gl/GrGLTexture.h" |
10 #include "gl/builders/GrGLProgramBuilder.h" | 10 #include "gl/builders/GrGLProgramBuilder.h" |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 imageIncrement[0] = 1.0f / texture.width(); | 122 imageIncrement[0] = 1.0f / texture.width(); |
123 imageIncrement[1] = ySign / texture.height(); | 123 imageIncrement[1] = ySign / texture.height(); |
124 pdman.set2fv(fImageIncrementUni, 1, imageIncrement); | 124 pdman.set2fv(fImageIncrementUni, 1, imageIncrement); |
125 pdman.set2fv(fKernelOffsetUni, 1, conv.kernelOffset()); | 125 pdman.set2fv(fKernelOffsetUni, 1, conv.kernelOffset()); |
126 pdman.set1fv(fKernelUni, fKernelSize.width() * fKernelSize.height(), conv.ke
rnel()); | 126 pdman.set1fv(fKernelUni, fKernelSize.width() * fKernelSize.height(), conv.ke
rnel()); |
127 pdman.set1f(fGainUni, conv.gain()); | 127 pdman.set1f(fGainUni, conv.gain()); |
128 pdman.set1f(fBiasUni, conv.bias()); | 128 pdman.set1f(fBiasUni, conv.bias()); |
129 fDomain.setData(pdman, conv.domain(), texture.origin()); | 129 fDomain.setData(pdman, conv.domain(), texture.origin()); |
130 } | 130 } |
131 | 131 |
132 GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrProcessorDataManager* pro
cDataManager, | 132 GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture, |
133 GrTexture* texture, | |
134 const SkIRect& bounds, | 133 const SkIRect& bounds, |
135 const SkISize& kernelSize, | 134 const SkISize& kernelSize, |
136 const SkScalar* kernel, | 135 const SkScalar* kernel, |
137 SkScalar gain, | 136 SkScalar gain, |
138 SkScalar bias, | 137 SkScalar bias, |
139 const SkIPoint& kernelOffse
t, | 138 const SkIPoint& kernelOffse
t, |
140 GrTextureDomain::Mode tileM
ode, | 139 GrTextureDomain::Mode tileM
ode, |
141 bool convolveAlpha) | 140 bool convolveAlpha) |
142 : INHERITED(procDataManager, texture, GrCoordTransform::MakeDivByTextureWHMatr
ix(texture)), | 141 : INHERITED(texture, GrCoordTransform::MakeDivByTextureWHMatrix(texture)), |
143 fKernelSize(kernelSize), | 142 fKernelSize(kernelSize), |
144 fGain(SkScalarToFloat(gain)), | 143 fGain(SkScalarToFloat(gain)), |
145 fBias(SkScalarToFloat(bias) / 255.0f), | 144 fBias(SkScalarToFloat(bias) / 255.0f), |
146 fConvolveAlpha(convolveAlpha), | 145 fConvolveAlpha(convolveAlpha), |
147 fDomain(GrTextureDomain::MakeTexelDomainForMode(texture, bounds, tileMode),
tileMode) { | 146 fDomain(GrTextureDomain::MakeTexelDomainForMode(texture, bounds, tileMode),
tileMode) { |
148 this->initClassID<GrMatrixConvolutionEffect>(); | 147 this->initClassID<GrMatrixConvolutionEffect>(); |
149 for (int i = 0; i < kernelSize.width() * kernelSize.height(); i++) { | 148 for (int i = 0; i < kernelSize.width() * kernelSize.height(); i++) { |
150 fKernel[i] = SkScalarToFloat(kernel[i]); | 149 fKernel[i] = SkScalarToFloat(kernel[i]); |
151 } | 150 } |
152 fKernelOffset[0] = static_cast<float>(kernelOffset.x()); | 151 fKernelOffset[0] = static_cast<float>(kernelOffset.x()); |
(...skipping 19 matching lines...) Expand all Loading... |
172 fKernelSize.width() * fKernelSize.height() * sizeof(float)) &
& | 171 fKernelSize.width() * fKernelSize.height() * sizeof(float)) &
& |
173 fGain == s.gain() && | 172 fGain == s.gain() && |
174 fBias == s.bias() && | 173 fBias == s.bias() && |
175 fKernelOffset == s.kernelOffset() && | 174 fKernelOffset == s.kernelOffset() && |
176 fConvolveAlpha == s.convolveAlpha() && | 175 fConvolveAlpha == s.convolveAlpha() && |
177 fDomain == s.domain(); | 176 fDomain == s.domain(); |
178 } | 177 } |
179 | 178 |
180 // Static function to create a 2D convolution | 179 // Static function to create a 2D convolution |
181 GrFragmentProcessor* | 180 GrFragmentProcessor* |
182 GrMatrixConvolutionEffect::CreateGaussian(GrProcessorDataManager* procDataManage
r, | 181 GrMatrixConvolutionEffect::CreateGaussian(GrTexture* texture, |
183 GrTexture* texture, | |
184 const SkIRect& bounds, | 182 const SkIRect& bounds, |
185 const SkISize& kernelSize, | 183 const SkISize& kernelSize, |
186 SkScalar gain, | 184 SkScalar gain, |
187 SkScalar bias, | 185 SkScalar bias, |
188 const SkIPoint& kernelOffset, | 186 const SkIPoint& kernelOffset, |
189 GrTextureDomain::Mode tileMode, | 187 GrTextureDomain::Mode tileMode, |
190 bool convolveAlpha, | 188 bool convolveAlpha, |
191 SkScalar sigmaX, | 189 SkScalar sigmaX, |
192 SkScalar sigmaY) { | 190 SkScalar sigmaY) { |
193 float kernel[MAX_KERNEL_SIZE]; | 191 float kernel[MAX_KERNEL_SIZE]; |
(...skipping 15 matching lines...) Expand all Loading... |
209 // is dropped here, since we renormalize the kernel below. | 207 // is dropped here, since we renormalize the kernel below. |
210 kernel[y * width + x] = xyTerm; | 208 kernel[y * width + x] = xyTerm; |
211 sum += xyTerm; | 209 sum += xyTerm; |
212 } | 210 } |
213 } | 211 } |
214 // Normalize the kernel | 212 // Normalize the kernel |
215 float scale = 1.0f / sum; | 213 float scale = 1.0f / sum; |
216 for (int i = 0; i < width * height; ++i) { | 214 for (int i = 0; i < width * height; ++i) { |
217 kernel[i] *= scale; | 215 kernel[i] *= scale; |
218 } | 216 } |
219 return new GrMatrixConvolutionEffect(procDataManager, texture, bounds, kerne
lSize, kernel, gain, | 217 return new GrMatrixConvolutionEffect(texture, bounds, kernelSize, kernel, ga
in, bias, |
220 bias, kernelOffset, tileMode, convolveA
lpha); | 218 kernelOffset, tileMode, convolveAlpha); |
221 } | 219 } |
222 | 220 |
223 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMatrixConvolutionEffect); | 221 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMatrixConvolutionEffect); |
224 | 222 |
225 const GrFragmentProcessor* GrMatrixConvolutionEffect::TestCreate(GrProcessorTest
Data* d) { | 223 const GrFragmentProcessor* GrMatrixConvolutionEffect::TestCreate(GrProcessorTest
Data* d) { |
226 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
: | 224 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
: |
227 GrProcessorUnitTest::kAlphaTextureIdx; | 225 GrProcessorUnitTest::kAlphaTextureIdx; |
228 int width = d->fRandom->nextRangeU(1, MAX_KERNEL_SIZE); | 226 int width = d->fRandom->nextRangeU(1, MAX_KERNEL_SIZE); |
229 int height = d->fRandom->nextRangeU(1, MAX_KERNEL_SIZE / width); | 227 int height = d->fRandom->nextRangeU(1, MAX_KERNEL_SIZE / width); |
230 SkISize kernelSize = SkISize::Make(width, height); | 228 SkISize kernelSize = SkISize::Make(width, height); |
231 SkAutoTDeleteArray<SkScalar> kernel(new SkScalar[width * height]); | 229 SkAutoTDeleteArray<SkScalar> kernel(new SkScalar[width * height]); |
232 for (int i = 0; i < width * height; i++) { | 230 for (int i = 0; i < width * height; i++) { |
233 kernel.get()[i] = d->fRandom->nextSScalar1(); | 231 kernel.get()[i] = d->fRandom->nextSScalar1(); |
234 } | 232 } |
235 SkScalar gain = d->fRandom->nextSScalar1(); | 233 SkScalar gain = d->fRandom->nextSScalar1(); |
236 SkScalar bias = d->fRandom->nextSScalar1(); | 234 SkScalar bias = d->fRandom->nextSScalar1(); |
237 SkIPoint kernelOffset = SkIPoint::Make(d->fRandom->nextRangeU(0, kernelSize.
width()), | 235 SkIPoint kernelOffset = SkIPoint::Make(d->fRandom->nextRangeU(0, kernelSize.
width()), |
238 d->fRandom->nextRangeU(0, kernelSize.
height())); | 236 d->fRandom->nextRangeU(0, kernelSize.
height())); |
239 SkIRect bounds = SkIRect::MakeXYWH(d->fRandom->nextRangeU(0, d->fTextures[te
xIdx]->width()), | 237 SkIRect bounds = SkIRect::MakeXYWH(d->fRandom->nextRangeU(0, d->fTextures[te
xIdx]->width()), |
240 d->fRandom->nextRangeU(0, d->fTextures[te
xIdx]->height()), | 238 d->fRandom->nextRangeU(0, d->fTextures[te
xIdx]->height()), |
241 d->fRandom->nextRangeU(0, d->fTextures[te
xIdx]->width()), | 239 d->fRandom->nextRangeU(0, d->fTextures[te
xIdx]->width()), |
242 d->fRandom->nextRangeU(0, d->fTextures[te
xIdx]->height())); | 240 d->fRandom->nextRangeU(0, d->fTextures[te
xIdx]->height())); |
243 GrTextureDomain::Mode tileMode = | 241 GrTextureDomain::Mode tileMode = |
244 static_cast<GrTextureDomain::Mode>(d->fRandom->nextRangeU(0, 2)); | 242 static_cast<GrTextureDomain::Mode>(d->fRandom->nextRangeU(0, 2)); |
245 bool convolveAlpha = d->fRandom->nextBool(); | 243 bool convolveAlpha = d->fRandom->nextBool(); |
246 return GrMatrixConvolutionEffect::Create(d->fProcDataManager, | 244 return GrMatrixConvolutionEffect::Create(d->fTextures[texIdx], |
247 d->fTextures[texIdx], | |
248 bounds, | 245 bounds, |
249 kernelSize, | 246 kernelSize, |
250 kernel.get(), | 247 kernel.get(), |
251 gain, | 248 gain, |
252 bias, | 249 bias, |
253 kernelOffset, | 250 kernelOffset, |
254 tileMode, | 251 tileMode, |
255 convolveAlpha); | 252 convolveAlpha); |
256 } | 253 } |
OLD | NEW |