Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(122)

Side by Side Diff: src/effects/SkAlphaThresholdFilter.cpp

Issue 582963002: Solo gp (Closed) Base URL: https://skia.googlesource.com/skia.git@no_peb
Patch Set: rebase Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkXfermode_proccoeff.h ('k') | src/effects/SkArithmeticMode.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 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 #include "SkAlphaThresholdFilter.h" 8 #include "SkAlphaThresholdFilter.h"
9 #include "SkBitmap.h" 9 #include "SkBitmap.h"
10 #include "SkReadBuffer.h" 10 #include "SkReadBuffer.h"
11 #include "SkWriteBuffer.h" 11 #include "SkWriteBuffer.h"
12 #include "SkRegion.h" 12 #include "SkRegion.h"
13 13
14 class SK_API SkAlphaThresholdFilterImpl : public SkImageFilter { 14 class SK_API SkAlphaThresholdFilterImpl : public SkImageFilter {
15 public: 15 public:
16 SkAlphaThresholdFilterImpl(const SkRegion& region, SkScalar innerThreshold, 16 SkAlphaThresholdFilterImpl(const SkRegion& region, SkScalar innerThreshold,
17 SkScalar outerThreshold, SkImageFilter* input); 17 SkScalar outerThreshold, SkImageFilter* input);
18 18
19 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkAlphaThresholdFilterIm pl) 19 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkAlphaThresholdFilterIm pl)
20 20
21 protected: 21 protected:
22 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING 22 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
23 explicit SkAlphaThresholdFilterImpl(SkReadBuffer& buffer); 23 explicit SkAlphaThresholdFilterImpl(SkReadBuffer& buffer);
24 #endif 24 #endif
25 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; 25 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
26 26
27 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, 27 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&,
28 SkBitmap* result, SkIPoint* offset) const SK_OVER RIDE; 28 SkBitmap* result, SkIPoint* offset) const SK_OVER RIDE;
29 #if SK_SUPPORT_GPU 29 #if SK_SUPPORT_GPU
30 virtual bool asNewEffect(GrEffect** effect, GrTexture* texture, 30 virtual bool asFragmentProcessor(GrFragmentProcessor**, GrTexture*, const Sk Matrix&,
31 const SkMatrix& matrix, const SkIRect& bounds) cons t SK_OVERRIDE; 31 const SkIRect& bounds) const SK_OVERRIDE;
32 #endif 32 #endif
33 33
34 private: 34 private:
35 SkRegion fRegion; 35 SkRegion fRegion;
36 SkScalar fInnerThreshold; 36 SkScalar fInnerThreshold;
37 SkScalar fOuterThreshold; 37 SkScalar fOuterThreshold;
38 typedef SkImageFilter INHERITED; 38 typedef SkImageFilter INHERITED;
39 }; 39 };
40 40
41 SkImageFilter* SkAlphaThresholdFilter::Create(const SkRegion& region, 41 SkImageFilter* SkAlphaThresholdFilter::Create(const SkRegion& region,
42 SkScalar innerThreshold, 42 SkScalar innerThreshold,
43 SkScalar outerThreshold, 43 SkScalar outerThreshold,
44 SkImageFilter* input) { 44 SkImageFilter* input) {
45 return SkNEW_ARGS(SkAlphaThresholdFilterImpl, (region, innerThreshold, outer Threshold, input)); 45 return SkNEW_ARGS(SkAlphaThresholdFilterImpl, (region, innerThreshold, outer Threshold, input));
46 } 46 }
47 47
48 #if SK_SUPPORT_GPU 48 #if SK_SUPPORT_GPU
49 #include "GrContext.h" 49 #include "GrContext.h"
50 #include "GrCoordTransform.h" 50 #include "GrCoordTransform.h"
51 #include "GrEffect.h" 51 #include "GrProcessor.h"
52 #include "gl/GrGLEffect.h" 52 #include "gl/GrGLProcessor.h"
53 #include "gl/builders/GrGLProgramBuilder.h" 53 #include "gl/builders/GrGLProgramBuilder.h"
54 #include "GrTBackendEffectFactory.h" 54 #include "GrTBackendProcessorFactory.h"
55 #include "GrTextureAccess.h" 55 #include "GrTextureAccess.h"
56 56
57 #include "SkGr.h" 57 #include "SkGr.h"
58 58
59 class GrGLAlphaThresholdEffect; 59 class GrGLAlphaThresholdEffect;
60 60
61 class AlphaThresholdEffect : public GrEffect { 61 class AlphaThresholdEffect : public GrFragmentProcessor {
62 62
63 public: 63 public:
64 static GrEffect* Create(GrTexture* texture, 64 static GrFragmentProcessor* Create(GrTexture* texture,
65 GrTexture* maskTexture, 65 GrTexture* maskTexture,
66 float innerThreshold, 66 float innerThreshold,
67 float outerThreshold) { 67 float outerThreshold) {
68 return SkNEW_ARGS(AlphaThresholdEffect, (texture, 68 return SkNEW_ARGS(AlphaThresholdEffect, (texture,
69 maskTexture, 69 maskTexture,
70 innerThreshold, 70 innerThreshold,
71 outerThreshold)); 71 outerThreshold));
72 } 72 }
73 73
74 virtual ~AlphaThresholdEffect() {}; 74 virtual ~AlphaThresholdEffect() {};
75 75
76 static const char* Name() { return "Alpha Threshold"; } 76 static const char* Name() { return "Alpha Threshold"; }
77 77
78 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; 78 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERR IDE;
79 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags ) const SK_OVERRIDE; 79 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags ) const SK_OVERRIDE;
80 80
81 float innerThreshold() const { return fInnerThreshold; } 81 float innerThreshold() const { return fInnerThreshold; }
82 float outerThreshold() const { return fOuterThreshold; } 82 float outerThreshold() const { return fOuterThreshold; }
83 83
84 typedef GrGLAlphaThresholdEffect GLEffect; 84 typedef GrGLAlphaThresholdEffect GLProcessor;
85 85
86 private: 86 private:
87 AlphaThresholdEffect(GrTexture* texture, 87 AlphaThresholdEffect(GrTexture* texture,
88 GrTexture* maskTexture, 88 GrTexture* maskTexture,
89 float innerThreshold, 89 float innerThreshold,
90 float outerThreshold) 90 float outerThreshold)
91 : fInnerThreshold(innerThreshold) 91 : fInnerThreshold(innerThreshold)
92 , fOuterThreshold(outerThreshold) 92 , fOuterThreshold(outerThreshold)
93 , fImageCoordTransform(kLocal_GrCoordSet, 93 , fImageCoordTransform(kLocal_GrCoordSet,
94 GrCoordTransform::MakeDivByTextureWHMatrix(textur e), texture) 94 GrCoordTransform::MakeDivByTextureWHMatrix(textur e), texture)
95 , fImageTextureAccess(texture) 95 , fImageTextureAccess(texture)
96 , fMaskCoordTransform(kLocal_GrCoordSet, 96 , fMaskCoordTransform(kLocal_GrCoordSet,
97 GrCoordTransform::MakeDivByTextureWHMatrix(maskTex ture), maskTexture) 97 GrCoordTransform::MakeDivByTextureWHMatrix(maskTex ture), maskTexture)
98 , fMaskTextureAccess(maskTexture) { 98 , fMaskTextureAccess(maskTexture) {
99 this->addCoordTransform(&fImageCoordTransform); 99 this->addCoordTransform(&fImageCoordTransform);
100 this->addTextureAccess(&fImageTextureAccess); 100 this->addTextureAccess(&fImageTextureAccess);
101 this->addCoordTransform(&fMaskCoordTransform); 101 this->addCoordTransform(&fMaskCoordTransform);
102 this->addTextureAccess(&fMaskTextureAccess); 102 this->addTextureAccess(&fMaskTextureAccess);
103 } 103 }
104 104
105 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; 105 virtual bool onIsEqual(const GrProcessor&) const SK_OVERRIDE;
106 106
107 GR_DECLARE_EFFECT_TEST; 107 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
108 108
109 float fInnerThreshold; 109 float fInnerThreshold;
110 float fOuterThreshold; 110 float fOuterThreshold;
111 GrCoordTransform fImageCoordTransform; 111 GrCoordTransform fImageCoordTransform;
112 GrTextureAccess fImageTextureAccess; 112 GrTextureAccess fImageTextureAccess;
113 GrCoordTransform fMaskCoordTransform; 113 GrCoordTransform fMaskCoordTransform;
114 GrTextureAccess fMaskTextureAccess; 114 GrTextureAccess fMaskTextureAccess;
115 115
116 typedef GrEffect INHERITED; 116 typedef GrFragmentProcessor INHERITED;
117 }; 117 };
118 118
119 class GrGLAlphaThresholdEffect : public GrGLEffect { 119 class GrGLAlphaThresholdEffect : public GrGLFragmentProcessor {
120 public: 120 public:
121 GrGLAlphaThresholdEffect(const GrBackendEffectFactory&, const GrEffect&); 121 GrGLAlphaThresholdEffect(const GrBackendProcessorFactory&, const GrProcessor &);
122 122
123 virtual void emitCode(GrGLProgramBuilder*, 123 virtual void emitCode(GrGLProgramBuilder*,
124 const GrEffect&, 124 const GrFragmentProcessor&,
125 const GrEffectKey&, 125 const GrProcessorKey&,
126 const char* outputColor, 126 const char* outputColor,
127 const char* inputColor, 127 const char* inputColor,
128 const TransformedCoordsArray&, 128 const TransformedCoordsArray&,
129 const TextureSamplerArray&) SK_OVERRIDE; 129 const TextureSamplerArray&) SK_OVERRIDE;
130 130
131 virtual void setData(const GrGLProgramDataManager&, const GrEffect&) SK_OVER RIDE; 131 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_O VERRIDE;
132 132
133 private: 133 private:
134 134
135 GrGLProgramDataManager::UniformHandle fInnerThresholdVar; 135 GrGLProgramDataManager::UniformHandle fInnerThresholdVar;
136 GrGLProgramDataManager::UniformHandle fOuterThresholdVar; 136 GrGLProgramDataManager::UniformHandle fOuterThresholdVar;
137 137
138 typedef GrGLEffect INHERITED; 138 typedef GrGLFragmentProcessor INHERITED;
139 }; 139 };
140 140
141 GrGLAlphaThresholdEffect::GrGLAlphaThresholdEffect(const GrBackendEffectFactory& factory, 141 GrGLAlphaThresholdEffect::GrGLAlphaThresholdEffect(const GrBackendProcessorFacto ry& factory,
142 const GrEffect&) 142 const GrProcessor&)
143 : INHERITED(factory) { 143 : INHERITED(factory) {
144 } 144 }
145 145
146 void GrGLAlphaThresholdEffect::emitCode(GrGLProgramBuilder* builder, 146 void GrGLAlphaThresholdEffect::emitCode(GrGLProgramBuilder* builder,
147 const GrEffect&, 147 const GrFragmentProcessor&,
148 const GrEffectKey& key, 148 const GrProcessorKey& key,
149 const char* outputColor, 149 const char* outputColor,
150 const char* inputColor, 150 const char* inputColor,
151 const TransformedCoordsArray& coords, 151 const TransformedCoordsArray& coords,
152 const TextureSamplerArray& samplers) { 152 const TextureSamplerArray& samplers) {
153 fInnerThresholdVar = builder->addUniform( 153 fInnerThresholdVar = builder->addUniform(
154 GrGLProgramBuilder::kFragment_Visibility, 154 GrGLProgramBuilder::kFragment_Visibility,
155 kFloat_GrSLType, "inner_threshold"); 155 kFloat_GrSLType, "inner_threshold");
156 fOuterThresholdVar = builder->addUniform( 156 fOuterThresholdVar = builder->addUniform(
157 GrGLProgramBuilder::kFragment_Visibility, 157 GrGLProgramBuilder::kFragment_Visibility,
158 kFloat_GrSLType, "outer_threshold"); 158 kFloat_GrSLType, "outer_threshold");
(...skipping 28 matching lines...) Expand all
187 "\t\t\tfloat scale = inner_thresh / max(0.001, color.a );\n" 187 "\t\t\tfloat scale = inner_thresh / max(0.001, color.a );\n"
188 "\t\t\tcolor.rgb *= scale;\n" 188 "\t\t\tcolor.rgb *= scale;\n"
189 "\t\t\tcolor.a = inner_thresh;\n" 189 "\t\t\tcolor.a = inner_thresh;\n"
190 "\t\t}\n"); 190 "\t\t}\n");
191 191
192 fsBuilder->codeAppendf("%s = %s;\n", outputColor, 192 fsBuilder->codeAppendf("%s = %s;\n", outputColor,
193 (GrGLSLExpr4(inputColor) * GrGLSLExpr4("color")).c_st r()); 193 (GrGLSLExpr4(inputColor) * GrGLSLExpr4("color")).c_st r());
194 } 194 }
195 195
196 void GrGLAlphaThresholdEffect::setData(const GrGLProgramDataManager& pdman, 196 void GrGLAlphaThresholdEffect::setData(const GrGLProgramDataManager& pdman,
197 const GrEffect& effect) { 197 const GrProcessor& proc) {
198 const AlphaThresholdEffect& alpha_threshold = effect.cast<AlphaThresholdEffe ct>(); 198 const AlphaThresholdEffect& alpha_threshold = proc.cast<AlphaThresholdEffect >();
199 pdman.set1f(fInnerThresholdVar, alpha_threshold.innerThreshold()); 199 pdman.set1f(fInnerThresholdVar, alpha_threshold.innerThreshold());
200 pdman.set1f(fOuterThresholdVar, alpha_threshold.outerThreshold()); 200 pdman.set1f(fOuterThresholdVar, alpha_threshold.outerThreshold());
201 } 201 }
202 202
203 ///////////////////////////////////////////////////////////////////// 203 /////////////////////////////////////////////////////////////////////
204 204
205 GR_DEFINE_EFFECT_TEST(AlphaThresholdEffect); 205 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(AlphaThresholdEffect);
206 206
207 GrEffect* AlphaThresholdEffect::TestCreate(SkRandom* random, 207 GrFragmentProcessor* AlphaThresholdEffect::TestCreate(SkRandom* random,
208 GrContext* context, 208 GrContext* context,
209 const GrDrawTargetCaps&, 209 const GrDrawTargetCaps&,
210 GrTexture** textures) { 210 GrTexture** textures) {
211 GrTexture* bmpTex = textures[GrEffectUnitTest::kSkiaPMTextureIdx]; 211 GrTexture* bmpTex = textures[GrProcessorUnitTest::kSkiaPMTextureIdx];
212 GrTexture* maskTex = textures[GrEffectUnitTest::kAlphaTextureIdx]; 212 GrTexture* maskTex = textures[GrProcessorUnitTest::kAlphaTextureIdx];
213 float inner_thresh = random->nextUScalar1(); 213 float inner_thresh = random->nextUScalar1();
214 float outer_thresh = random->nextUScalar1(); 214 float outer_thresh = random->nextUScalar1();
215 return AlphaThresholdEffect::Create(bmpTex, maskTex, inner_thresh, outer_thr esh); 215 return AlphaThresholdEffect::Create(bmpTex, maskTex, inner_thresh, outer_thr esh);
216 } 216 }
217 217
218 /////////////////////////////////////////////////////////////////////////////// 218 ///////////////////////////////////////////////////////////////////////////////
219 219
220 const GrBackendEffectFactory& AlphaThresholdEffect::getFactory() const { 220 const GrBackendFragmentProcessorFactory& AlphaThresholdEffect::getFactory() cons t {
221 return GrTBackendEffectFactory<AlphaThresholdEffect>::getInstance(); 221 return GrTBackendFragmentProcessorFactory<AlphaThresholdEffect>::getInstance ();
222 } 222 }
223 223
224 bool AlphaThresholdEffect::onIsEqual(const GrEffect& sBase) const { 224 bool AlphaThresholdEffect::onIsEqual(const GrProcessor& sBase) const {
225 const AlphaThresholdEffect& s = sBase.cast<AlphaThresholdEffect>(); 225 const AlphaThresholdEffect& s = sBase.cast<AlphaThresholdEffect>();
226 return (this->texture(0) == s.texture(0) && 226 return (this->texture(0) == s.texture(0) &&
227 this->fInnerThreshold == s.fInnerThreshold && 227 this->fInnerThreshold == s.fInnerThreshold &&
228 this->fOuterThreshold == s.fOuterThreshold); 228 this->fOuterThreshold == s.fOuterThreshold);
229 } 229 }
230 230
231 void AlphaThresholdEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { 231 void AlphaThresholdEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
232 if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color ) && 232 if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color ) &&
233 GrPixelConfigIsOpaque(this->texture(0)->config())) { 233 GrPixelConfigIsOpaque(this->texture(0)->config())) {
234 *validFlags = kA_GrColorComponentFlag; 234 *validFlags = kA_GrColorComponentFlag;
(...skipping 26 matching lines...) Expand all
261 SkScalar innerThreshold, 261 SkScalar innerThreshold,
262 SkScalar outerThreshold, 262 SkScalar outerThreshold,
263 SkImageFilter* input) 263 SkImageFilter* input)
264 : INHERITED(1, &input) 264 : INHERITED(1, &input)
265 , fRegion(region) 265 , fRegion(region)
266 , fInnerThreshold(innerThreshold) 266 , fInnerThreshold(innerThreshold)
267 , fOuterThreshold(outerThreshold) { 267 , fOuterThreshold(outerThreshold) {
268 } 268 }
269 269
270 #if SK_SUPPORT_GPU 270 #if SK_SUPPORT_GPU
271 bool SkAlphaThresholdFilterImpl::asNewEffect(GrEffect** effect, GrTexture* textu re, 271 bool SkAlphaThresholdFilterImpl::asFragmentProcessor(GrFragmentProcessor** fp,
272 const SkMatrix& in_matrix, const Sk IRect&) const { 272 GrTexture* texture,
273 if (effect) { 273 const SkMatrix& in_matrix,
274 const SkIRect&) const {
275 if (fp) {
274 GrContext* context = texture->getContext(); 276 GrContext* context = texture->getContext();
275 GrTextureDesc maskDesc; 277 GrTextureDesc maskDesc;
276 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) { 278 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
277 maskDesc.fConfig = kAlpha_8_GrPixelConfig; 279 maskDesc.fConfig = kAlpha_8_GrPixelConfig;
278 } else { 280 } else {
279 maskDesc.fConfig = kRGBA_8888_GrPixelConfig; 281 maskDesc.fConfig = kRGBA_8888_GrPixelConfig;
280 } 282 }
281 maskDesc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureF lagBit; 283 maskDesc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureF lagBit;
282 // Add one pixel of border to ensure that clamp mode will be all zeros 284 // Add one pixel of border to ensure that clamp mode will be all zeros
283 // the outside. 285 // the outside.
(...skipping 16 matching lines...) Expand all
300 context->setMatrix(in_matrix); 302 context->setMatrix(in_matrix);
301 303
302 while (!iter.done()) { 304 while (!iter.done()) {
303 SkRect rect = SkRect::Make(iter.rect()); 305 SkRect rect = SkRect::Make(iter.rect());
304 context->drawRect(grPaint, rect); 306 context->drawRect(grPaint, rect);
305 iter.next(); 307 iter.next();
306 } 308 }
307 context->setMatrix(old_matrix); 309 context->setMatrix(old_matrix);
308 } 310 }
309 311
310 *effect = AlphaThresholdEffect::Create(texture, 312 *fp = AlphaThresholdEffect::Create(texture,
311 maskTexture, 313 maskTexture,
312 fInnerThreshold, 314 fInnerThreshold,
313 fOuterThreshold); 315 fOuterThreshold);
314 } 316 }
315 return true; 317 return true;
316 } 318 }
317 #endif 319 #endif
318 320
319 void SkAlphaThresholdFilterImpl::flatten(SkWriteBuffer& buffer) const { 321 void SkAlphaThresholdFilterImpl::flatten(SkWriteBuffer& buffer) const {
320 this->INHERITED::flatten(buffer); 322 this->INHERITED::flatten(buffer);
321 buffer.writeScalar(fInnerThreshold); 323 buffer.writeScalar(fInnerThreshold);
322 buffer.writeScalar(fOuterThreshold); 324 buffer.writeScalar(fOuterThreshold);
323 buffer.writeRegion(fRegion); 325 buffer.writeRegion(fRegion);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 (U8CPU)(SkColorGetG(source) * scale), 379 (U8CPU)(SkColorGetG(source) * scale),
378 (U8CPU)(SkColorGetB(source) * scale)); 380 (U8CPU)(SkColorGetB(source) * scale));
379 } 381 }
380 } 382 }
381 dptr[y * dst->width() + x] = output_color; 383 dptr[y * dst->width() + x] = output_color;
382 } 384 }
383 } 385 }
384 386
385 return true; 387 return true;
386 } 388 }
OLDNEW
« no previous file with comments | « src/core/SkXfermode_proccoeff.h ('k') | src/effects/SkArithmeticMode.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698