OLD | NEW |
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 "SkBitmapAlphaThresholdShader.h" | 8 #include "SkBitmapAlphaThresholdShader.h" |
9 | 9 |
10 class BATShader : public SkShader { | 10 class BATShader : public SkShader { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 fBitmap.toString(str); | 55 fBitmap.toString(str); |
56 | 56 |
57 this->INHERITED::toString(str); | 57 this->INHERITED::toString(str); |
58 | 58 |
59 str->append(")"); | 59 str->append(")"); |
60 } | 60 } |
61 #endif | 61 #endif |
62 | 62 |
63 #if SK_SUPPORT_GPU | 63 #if SK_SUPPORT_GPU |
64 #include "GrContext.h" | 64 #include "GrContext.h" |
| 65 #include "GrCoordTransform.h" |
65 #include "GrEffect.h" | 66 #include "GrEffect.h" |
66 #include "gl/GrGLEffect.h" | 67 #include "gl/GrGLEffect.h" |
67 #include "gl/GrGLEffectMatrix.h" | |
68 #include "GrTBackendEffectFactory.h" | 68 #include "GrTBackendEffectFactory.h" |
69 #include "GrTextureAccess.h" | 69 #include "GrTextureAccess.h" |
70 | 70 |
71 #include "SkGr.h" | 71 #include "SkGr.h" |
72 | 72 |
73 /** | 73 /** |
74 * Could create specializations for some simple cases: | 74 * Could create specializations for some simple cases: |
75 * - The region is empty. | 75 * - The region is empty. |
76 * - The region fully contains the bitmap. | 76 * - The region fully contains the bitmap. |
77 * - The regions is 1 rect (or maybe a small number of rects). | 77 * - The regions is 1 rect (or maybe a small number of rects). |
(...skipping 24 matching lines...) Expand all Loading... |
102 return; | 102 return; |
103 } | 103 } |
104 | 104 |
105 static const char* Name() { return "Bitmap Alpha Threshold"; } | 105 static const char* Name() { return "Bitmap Alpha Threshold"; } |
106 | 106 |
107 class GLEffect : public GrGLEffect { | 107 class GLEffect : public GrGLEffect { |
108 public: | 108 public: |
109 GLEffect(const GrBackendEffectFactory& factory, | 109 GLEffect(const GrBackendEffectFactory& factory, |
110 const GrDrawEffect& e) | 110 const GrDrawEffect& e) |
111 : GrGLEffect(factory) | 111 : GrGLEffect(factory) |
112 , fBmpMatrix(GrEffect::kLocal_CoordsType) | |
113 , fMaskMatrix(GrEffect::kLocal_CoordsType) | |
114 , fPrevThreshold(-SK_Scalar1) { | 112 , fPrevThreshold(-SK_Scalar1) { |
115 } | 113 } |
116 | 114 |
117 virtual void emitCode(GrGLShaderBuilder* builder, | 115 virtual void emitCode(GrGLShaderBuilder* builder, |
118 const GrDrawEffect& drawEffect, | 116 const GrDrawEffect& drawEffect, |
119 EffectKey key, | 117 EffectKey key, |
120 const char* outputColor, | 118 const char* outputColor, |
121 const char* inputColor, | 119 const char* inputColor, |
122 const TextureSamplerArray& samplers) SK_OVERRIDE
{ | 120 const TransformedCoordsArray& coords, |
123 SkString bmpCoord; | 121 const TextureSamplerArray& samplers) SK_OVERRIDE { |
124 SkString maskCoord; | |
125 | |
126 GrSLType bmpCoordType = fBmpMatrix.emitCode(builder, key, &bmpCoord,
NULL, "Bmp"); | |
127 EffectKey maskMatrixKey = key >> GrGLEffectMatrix::kKeyBits; | |
128 GrSLType maskCoordType = fMaskMatrix.emitCode(builder, | |
129 maskMatrixKey, | |
130 &maskCoord, | |
131 NULL, | |
132 "Mask"); | |
133 | |
134 // put bitmap color in "color" | 122 // put bitmap color in "color" |
135 builder->fsCodeAppend("\t\tvec4 color = "); | 123 builder->fsCodeAppend("\t\tvec4 color = "); |
136 builder->fsAppendTextureLookup(samplers[0], bmpCoord.c_str(), bmpCoo
rdType); | 124 builder->fsAppendTextureLookup(samplers[0], coords[0].c_str(), coord
s[0].type()); |
137 builder->fsCodeAppend(";\n"); | 125 builder->fsCodeAppend(";\n"); |
138 | 126 |
139 // put alpha from mask texture in "mask" | 127 // put alpha from mask texture in "mask" |
140 builder->fsCodeAppend("\t\tfloat mask = "); | 128 builder->fsCodeAppend("\t\tfloat mask = "); |
141 builder->fsAppendTextureLookup(samplers[1], maskCoord.c_str(), maskC
oordType); | 129 builder->fsAppendTextureLookup(samplers[1], coords[1].c_str(), coord
s[1].type()); |
142 builder->fsCodeAppend(".a;\n"); | 130 builder->fsCodeAppend(".a;\n"); |
143 | 131 |
144 const char* threshold; | 132 const char* threshold; |
145 | 133 |
146 fThresholdUniHandle = builder->addUniform(GrGLShaderBuilder::kFragme
nt_Visibility, | 134 fThresholdUniHandle = builder->addUniform(GrGLShaderBuilder::kFragme
nt_Visibility, |
147 kFloat_GrSLType, | 135 kFloat_GrSLType, |
148 "threshold", | 136 "threshold", |
149 &threshold); | 137 &threshold); |
150 builder->fsCodeAppendf("\t\tfloat thresh = %s;\n", threshold); | 138 builder->fsCodeAppendf("\t\tfloat thresh = %s;\n", threshold); |
151 | 139 |
(...skipping 12 matching lines...) Expand all Loading... |
164 builder->fsCodeAppend("color = "); | 152 builder->fsCodeAppend("color = "); |
165 SkString outStr; | 153 SkString outStr; |
166 outStr.appendf("\t\t%s = ", outputColor); | 154 outStr.appendf("\t\t%s = ", outputColor); |
167 GrGLSLModulatef<4>(&outStr, inputColor, "color"); | 155 GrGLSLModulatef<4>(&outStr, inputColor, "color"); |
168 outStr.append(";\n"); | 156 outStr.append(";\n"); |
169 builder->fsCodeAppend(outStr.c_str()); | 157 builder->fsCodeAppend(outStr.c_str()); |
170 } | 158 } |
171 | 159 |
172 virtual void setData(const GrGLUniformManager& uman, const GrDrawEffect&
e) SK_OVERRIDE { | 160 virtual void setData(const GrGLUniformManager& uman, const GrDrawEffect&
e) SK_OVERRIDE { |
173 const ThresholdEffect& effect = e.castEffect<ThresholdEffect>(); | 161 const ThresholdEffect& effect = e.castEffect<ThresholdEffect>(); |
174 fBmpMatrix.setData(uman, effect.fBmpMatrix, e, effect.fBmpAccess.get
Texture()); | |
175 fMaskMatrix.setData(uman, effect.fMaskMatrix, e, effect.fMaskAccess.
getTexture()); | |
176 if (fPrevThreshold != effect.fThreshold) { | 162 if (fPrevThreshold != effect.fThreshold) { |
177 uman.set1f(fThresholdUniHandle, effect.fThreshold); | 163 uman.set1f(fThresholdUniHandle, effect.fThreshold); |
178 } | 164 } |
179 } | 165 } |
180 | 166 |
181 static inline EffectKey GenKey(const GrDrawEffect& e, const GrGLCaps&) { | |
182 const ThresholdEffect& effect = e.castEffect<ThresholdEffect>(); | |
183 | |
184 EffectKey bmpMKey = GrGLEffectMatrix::GenKey(effect.fBmpMatrix, | |
185 e, | |
186 GrEffect::kLocal_Coords
Type, | |
187 effect.fBmpAccess.getTe
xture()); | |
188 EffectKey maskMKey = GrGLEffectMatrix::GenKey(effect.fMaskMatrix, | |
189 e, | |
190 GrEffect::kLocal_Coord
sType, | |
191 effect.fMaskAccess.get
Texture()); | |
192 return bmpMKey | (maskMKey << GrGLEffectMatrix::kKeyBits); | |
193 } | |
194 | |
195 private: | 167 private: |
196 GrGLEffectMatrix fBmpMatrix; | |
197 GrGLEffectMatrix fMaskMatrix; | |
198 | |
199 GrGLUniformManager::UniformHandle fThresholdUniHandle; | 168 GrGLUniformManager::UniformHandle fThresholdUniHandle; |
200 SkScalar fPrevThreshold; | 169 SkScalar fPrevThreshold; |
201 }; | 170 }; |
202 | 171 |
203 GR_DECLARE_EFFECT_TEST; | 172 GR_DECLARE_EFFECT_TEST; |
204 | 173 |
205 private: | 174 private: |
206 ThresholdEffect(GrTexture* bmpTexture, const SkMatrix& bmpMatrix, | 175 ThresholdEffect(GrTexture* bmpTexture, const SkMatrix& bmpMatrix, |
207 GrTexture* maskTexture, const SkMatrix& maskMatrix, | 176 GrTexture* maskTexture, const SkMatrix& maskMatrix, |
208 SkScalar threshold) | 177 SkScalar threshold) |
209 : fBmpAccess(bmpTexture, GrTextureParams()) | 178 : fBmpTransform(kLocal_GrCoordSet, bmpMatrix, bmpTexture) |
| 179 , fBmpAccess(bmpTexture, GrTextureParams()) |
| 180 , fMaskTransform(kLocal_GrCoordSet, maskMatrix, maskTexture) |
210 , fMaskAccess(maskTexture, GrTextureParams()) | 181 , fMaskAccess(maskTexture, GrTextureParams()) |
211 , fBmpMatrix(bmpMatrix) | |
212 , fMaskMatrix(maskMatrix) | |
213 , fThreshold(threshold) { | 182 , fThreshold(threshold) { |
| 183 this->addCoordTransform(&fBmpTransform); |
214 this->addTextureAccess(&fBmpAccess); | 184 this->addTextureAccess(&fBmpAccess); |
| 185 this->addCoordTransform(&fMaskTransform); |
215 this->addTextureAccess(&fMaskAccess); | 186 this->addTextureAccess(&fMaskAccess); |
216 } | 187 } |
217 | 188 |
218 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { | 189 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
219 const ThresholdEffect& e = CastEffect<ThresholdEffect>(other); | 190 const ThresholdEffect& e = CastEffect<ThresholdEffect>(other); |
220 return e.fBmpAccess.getTexture() == fBmpAccess.getTexture() && | 191 return e.fBmpAccess.getTexture() == fBmpAccess.getTexture() && |
221 e.fMaskAccess.getTexture() == fMaskAccess.getTexture() && | 192 e.fMaskAccess.getTexture() == fMaskAccess.getTexture() && |
222 e.fBmpMatrix == fBmpMatrix && | 193 e.fBmpTransform.getMatrix() == fBmpTransform.getMatrix() && |
223 e.fMaskMatrix == fMaskMatrix && | 194 e.fMaskTransform.getMatrix() == fMaskTransform.getMatrix() && |
224 e.fThreshold == fThreshold; | 195 e.fThreshold == fThreshold; |
225 } | 196 } |
226 | 197 |
227 GrTextureAccess fBmpAccess; | 198 GrCoordTransform fBmpTransform; |
228 GrTextureAccess fMaskAccess; | 199 GrTextureAccess fBmpAccess; |
229 | 200 GrCoordTransform fMaskTransform; |
230 SkMatrix fBmpMatrix; | 201 GrTextureAccess fMaskAccess; |
231 SkMatrix fMaskMatrix; | |
232 | 202 |
233 SkScalar fThreshold; | 203 SkScalar fThreshold; |
234 }; | 204 }; |
235 | 205 |
236 GR_DEFINE_EFFECT_TEST(ThresholdEffect); | 206 GR_DEFINE_EFFECT_TEST(ThresholdEffect); |
237 | 207 |
238 GrEffectRef* ThresholdEffect::TestCreate(SkRandom* rand, | 208 GrEffectRef* ThresholdEffect::TestCreate(SkRandom* rand, |
239 GrContext*, | 209 GrContext*, |
240 const GrDrawTargetCaps&, | 210 const GrDrawTargetCaps&, |
241 GrTexture* textures[]) { | 211 GrTexture* textures[]) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 GrEffectRef* effect = ThresholdEffect::Create(bmpTexture, bmpMatrix, | 272 GrEffectRef* effect = ThresholdEffect::Create(bmpTexture, bmpMatrix, |
303 maskTexture, maskMatrix, | 273 maskTexture, maskMatrix, |
304 fThreshold); | 274 fThreshold); |
305 | 275 |
306 GrUnlockAndUnrefCachedBitmapTexture(bmpTexture); | 276 GrUnlockAndUnrefCachedBitmapTexture(bmpTexture); |
307 | 277 |
308 return effect; | 278 return effect; |
309 } | 279 } |
310 | 280 |
311 #endif | 281 #endif |
OLD | NEW |