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 "SkArithmeticMode.h" | 8 #include "SkArithmeticMode.h" |
9 #include "SkColorPriv.h" | 9 #include "SkColorPriv.h" |
10 #include "SkFlattenableBuffers.h" | |
11 #include "SkString.h" | 10 #include "SkString.h" |
12 #include "SkUnPreMultiply.h" | 11 #include "SkUnPreMultiply.h" |
13 #if SK_SUPPORT_GPU | |
14 #include "GrContext.h" | |
15 #include "gl/GrGLEffect.h" | |
16 #include "GrTBackendEffectFactory.h" | |
17 #include "SkImageFilterUtils.h" | |
18 #endif | |
19 | 12 |
20 class SkArithmeticMode_scalar : public SkXfermode { | 13 class SkArithmeticMode_scalar : public SkXfermode { |
21 public: | 14 public: |
22 SkArithmeticMode_scalar(SkScalar k1, SkScalar k2, SkScalar k3, SkScalar k4)
{ | 15 SkArithmeticMode_scalar(SkScalar k1, SkScalar k2, SkScalar k3, SkScalar k4)
{ |
23 fK[0] = k1; | 16 fK[0] = k1; |
24 fK[1] = k2; | 17 fK[1] = k2; |
25 fK[2] = k3; | 18 fK[2] = k3; |
26 fK[3] = k4; | 19 fK[3] = k4; |
27 } | 20 } |
28 | 21 |
29 virtual void xfer32(SkPMColor dst[], const SkPMColor src[], int count, | 22 virtual void xfer32(SkPMColor dst[], const SkPMColor src[], int count, |
30 const SkAlpha aa[]) const SK_OVERRIDE; | 23 const SkAlpha aa[]) const SK_OVERRIDE; |
31 | 24 |
32 SK_DEVELOPER_TO_STRING() | 25 SK_DEVELOPER_TO_STRING() |
33 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkArithmeticMode_scalar) | 26 SK_DECLARE_UNFLATTENABLE_OBJECT() |
34 | |
35 #if SK_SUPPORT_GPU | |
36 virtual bool asNewEffectOrCoeff(GrContext*, GrEffectRef** effect, Coeff*, Co
eff*) const SK_OVERRIDE; | |
37 #endif | |
38 | 27 |
39 private: | 28 private: |
40 SkArithmeticMode_scalar(SkFlattenableReadBuffer& buffer) : INHERITED(buffer)
{ | |
41 fK[0] = buffer.readScalar(); | |
42 fK[1] = buffer.readScalar(); | |
43 fK[2] = buffer.readScalar(); | |
44 fK[3] = buffer.readScalar(); | |
45 } | |
46 | |
47 virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE { | |
48 INHERITED::flatten(buffer); | |
49 buffer.writeScalar(fK[0]); | |
50 buffer.writeScalar(fK[1]); | |
51 buffer.writeScalar(fK[2]); | |
52 buffer.writeScalar(fK[3]); | |
53 } | |
54 SkScalar fK[4]; | 29 SkScalar fK[4]; |
55 | 30 |
56 typedef SkXfermode INHERITED; | 31 typedef SkXfermode INHERITED; |
57 }; | 32 }; |
58 | 33 |
59 static int pinToByte(int value) { | 34 static int pinToByte(int value) { |
60 if (value < 0) { | 35 if (value < 0) { |
61 value = 0; | 36 value = 0; |
62 } else if (value > 255) { | 37 } else if (value > 255) { |
63 value = 255; | 38 value = 255; |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 return SkNEW_ARGS(SkArithmeticMode_dst, (i3, i4)); | 184 return SkNEW_ARGS(SkArithmeticMode_dst, (i3, i4)); |
210 } | 185 } |
211 if (0 == i3) { | 186 if (0 == i3) { |
212 return SkNEW_ARGS(SkArithmeticMode_src, (i2, i4)); | 187 return SkNEW_ARGS(SkArithmeticMode_src, (i2, i4)); |
213 } | 188 } |
214 return SkNEW_ARGS(SkArithmeticMode_linear, (i2, i3, i4)); | 189 return SkNEW_ARGS(SkArithmeticMode_linear, (i2, i3, i4)); |
215 #endif | 190 #endif |
216 } | 191 } |
217 return SkNEW_ARGS(SkArithmeticMode_scalar, (k1, k2, k3, k4)); | 192 return SkNEW_ARGS(SkArithmeticMode_scalar, (k1, k2, k3, k4)); |
218 } | 193 } |
219 | |
220 | |
221 ////////////////////////////////////////////////////////////////////////////// | |
222 | |
223 #if SK_SUPPORT_GPU | |
224 | |
225 class GrGLArithmeticEffect : public GrGLEffect { | |
226 public: | |
227 GrGLArithmeticEffect(const GrBackendEffectFactory&, const GrDrawEffect&); | |
228 virtual ~GrGLArithmeticEffect(); | |
229 | |
230 virtual void emitCode(GrGLShaderBuilder*, | |
231 const GrDrawEffect&, | |
232 EffectKey, | |
233 const char* outputColor, | |
234 const char* inputColor, | |
235 const TextureSamplerArray&) SK_OVERRIDE; | |
236 | |
237 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); | |
238 | |
239 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; | |
240 | |
241 private: | |
242 static const GrEffect::CoordsType kCoordsType = GrEffect::kLocal_CoordsType; | |
243 | |
244 GrGLUniformManager::UniformHandle fKUni; | |
245 | |
246 typedef GrGLEffect INHERITED; | |
247 }; | |
248 | |
249 /////////////////////////////////////////////////////////////////////////////// | |
250 | |
251 class GrArithmeticEffect : public GrEffect { | |
252 public: | |
253 static GrEffectRef* Create(float k1, float k2, float k3, float k4) { | |
254 AutoEffectUnref effect(SkNEW_ARGS(GrArithmeticEffect, (k1, k2, k3, k4)))
; | |
255 return CreateEffectRef(effect); | |
256 } | |
257 | |
258 virtual ~GrArithmeticEffect(); | |
259 | |
260 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; | |
261 | |
262 typedef GrGLArithmeticEffect GLEffect; | |
263 static const char* Name() { return "Arithmetic"; } | |
264 | |
265 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags
) const SK_OVERRIDE; | |
266 | |
267 float k1() const { return fK1; } | |
268 float k2() const { return fK2; } | |
269 float k3() const { return fK3; } | |
270 float k4() const { return fK4; } | |
271 | |
272 private: | |
273 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; | |
274 | |
275 GrArithmeticEffect(float k1, float k2, float k3, float k4); | |
276 float fK1, fK2, fK3, fK4; | |
277 | |
278 GR_DECLARE_EFFECT_TEST; | |
279 typedef GrEffect INHERITED; | |
280 | |
281 }; | |
282 | |
283 /////////////////////////////////////////////////////////////////////////////// | |
284 | |
285 GrArithmeticEffect::GrArithmeticEffect(float k1, float k2, float k3, float k4) | |
286 : fK1(k1), fK2(k2), fK3(k3), fK4(k4) { | |
287 this->setWillReadDstColor(); | |
288 } | |
289 | |
290 GrArithmeticEffect::~GrArithmeticEffect() { | |
291 } | |
292 | |
293 bool GrArithmeticEffect::onIsEqual(const GrEffect& sBase) const { | |
294 const GrArithmeticEffect& s = CastEffect<GrArithmeticEffect>(sBase); | |
295 return fK1 == s.fK1 && | |
296 fK2 == s.fK2 && | |
297 fK3 == s.fK3 && | |
298 fK4 == s.fK4; | |
299 } | |
300 | |
301 const GrBackendEffectFactory& GrArithmeticEffect::getFactory() const { | |
302 return GrTBackendEffectFactory<GrArithmeticEffect>::getInstance(); | |
303 } | |
304 | |
305 void GrArithmeticEffect::getConstantColorComponents(GrColor* color, uint32_t* va
lidFlags) const { | |
306 // TODO: optimize this | |
307 *validFlags = 0; | |
308 } | |
309 | |
310 /////////////////////////////////////////////////////////////////////////////// | |
311 | |
312 GrGLArithmeticEffect::GrGLArithmeticEffect(const GrBackendEffectFactory& factory
, | |
313 const GrDrawEffect& drawEffect) : INH
ERITED(factory) { | |
314 } | |
315 | |
316 GrGLArithmeticEffect::~GrGLArithmeticEffect() { | |
317 } | |
318 | |
319 void GrGLArithmeticEffect::emitCode(GrGLShaderBuilder* builder, | |
320 const GrDrawEffect&, | |
321 EffectKey key, | |
322 const char* outputColor, | |
323 const char* inputColor, | |
324 const TextureSamplerArray& samplers) { | |
325 const char* dstColor = builder->dstColor(); | |
326 GrAssert(NULL != dstColor); | |
327 | |
328 // We don't try to optimize for this case at all | |
329 if (NULL == inputColor) { | |
330 builder->fsCodeAppendf("\t\tconst vec4 ones = %s;\n", GrGLSLOnesVecf(4))
; | |
331 inputColor = "ones"; | |
332 } | |
333 fKUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, | |
334 kVec4f_GrSLType, "k"); | |
335 const char* kUni = builder->getUniformCStr(fKUni); | |
336 | |
337 builder->fsCodeAppendf("\t\t%s.rgb = clamp(%s.rgb / %s.a, 0.0, 1.0);\n", inp
utColor, inputColor, inputColor); | |
338 builder->fsCodeAppendf("\t\t%s.rgb = clamp(%s.rgb / %s.a, 0.0, 1.0);\n", dst
Color, dstColor, dstColor); | |
339 | |
340 builder->fsCodeAppendf("\t\t%s = %s.x * %s * %s + %s.y * %s + %s.z * %s + %s
.w;\n", outputColor, kUni, inputColor, dstColor, kUni, inputColor, kUni, dstColo
r, kUni); | |
341 builder->fsCodeAppendf("\t\t%s = clamp(%s, 0.0, 1.0);\n", outputColor, outpu
tColor); | |
342 builder->fsCodeAppendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor); | |
343 } | |
344 | |
345 void GrGLArithmeticEffect::setData(const GrGLUniformManager& uman, const GrDrawE
ffect& drawEffect) { | |
346 const GrArithmeticEffect& arith = drawEffect.castEffect<GrArithmeticEffect>(
); | |
347 uman.set4f(fKUni, arith.k1(), arith.k2(), arith.k3(), arith.k4()); | |
348 } | |
349 | |
350 GrGLEffect::EffectKey GrGLArithmeticEffect::GenKey(const GrDrawEffect& drawEffec
t, const GrGLCaps&) { | |
351 return 0; | |
352 } | |
353 | |
354 GrEffectRef* GrArithmeticEffect::TestCreate(SkMWCRandom* rand, | |
355 GrContext*, | |
356 const GrDrawTargetCaps&, | |
357 GrTexture*[]) { | |
358 float k1 = rand->nextF(); | |
359 float k2 = rand->nextF(); | |
360 float k3 = rand->nextF(); | |
361 float k4 = rand->nextF(); | |
362 | |
363 static AutoEffectUnref gEffect(SkNEW_ARGS(GrArithmeticEffect, (k1, k2, k3, k
4))); | |
364 return CreateEffectRef(gEffect); | |
365 } | |
366 | |
367 GR_DEFINE_EFFECT_TEST(GrArithmeticEffect); | |
368 | |
369 bool SkArithmeticMode_scalar::asNewEffectOrCoeff(GrContext*, | |
370 GrEffectRef** effect, | |
371 Coeff*, | |
372 Coeff*) const { | |
373 if (effect) { | |
374 *effect = GrArithmeticEffect::Create(SkScalarToFloat(fK[0]), | |
375 SkScalarToFloat(fK[1]), | |
376 SkScalarToFloat(fK[2]), | |
377 SkScalarToFloat(fK[3])); | |
378 } | |
379 return true; | |
380 } | |
381 | |
382 #endif | |
383 | |
384 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkArithmeticMode) | |
385 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkArithmeticMode_scalar) | |
386 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | |
OLD | NEW |