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

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

Issue 16064002: Provide a GPU implementation of SkArithmeticMode (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Don't attempt to write to inputColor. Created 7 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « include/effects/SkArithmeticMode.h ('k') | src/ports/SkGlobalInitialization_default.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 "SkArithmeticMode.h" 8 #include "SkArithmeticMode.h"
9 #include "SkColorPriv.h" 9 #include "SkColorPriv.h"
10 #include "SkFlattenableBuffers.h"
10 #include "SkString.h" 11 #include "SkString.h"
11 #include "SkUnPreMultiply.h" 12 #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
12 19
13 class SkArithmeticMode_scalar : public SkXfermode { 20 class SkArithmeticMode_scalar : public SkXfermode {
14 public: 21 public:
15 SkArithmeticMode_scalar(SkScalar k1, SkScalar k2, SkScalar k3, SkScalar k4) { 22 SkArithmeticMode_scalar(SkScalar k1, SkScalar k2, SkScalar k3, SkScalar k4) {
16 fK[0] = k1; 23 fK[0] = k1;
17 fK[1] = k2; 24 fK[1] = k2;
18 fK[2] = k3; 25 fK[2] = k3;
19 fK[3] = k4; 26 fK[3] = k4;
20 } 27 }
21 28
22 virtual void xfer32(SkPMColor dst[], const SkPMColor src[], int count, 29 virtual void xfer32(SkPMColor dst[], const SkPMColor src[], int count,
23 const SkAlpha aa[]) const SK_OVERRIDE; 30 const SkAlpha aa[]) const SK_OVERRIDE;
24 31
25 SK_DEVELOPER_TO_STRING() 32 SK_DEVELOPER_TO_STRING()
26 SK_DECLARE_UNFLATTENABLE_OBJECT() 33 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkArithmeticMode_scalar)
34
35 #if SK_SUPPORT_GPU
36 virtual bool asNewEffectOrCoeff(GrContext*, GrEffectRef** effect, Coeff*, Co eff*) const SK_OVERRIDE;
37 #endif
27 38
28 private: 39 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 }
29 SkScalar fK[4]; 54 SkScalar fK[4];
30 55
31 typedef SkXfermode INHERITED; 56 typedef SkXfermode INHERITED;
32 }; 57 };
33 58
34 static int pinToByte(int value) { 59 static int pinToByte(int value) {
35 if (value < 0) { 60 if (value < 0) {
36 value = 0; 61 value = 0;
37 } else if (value > 255) { 62 } else if (value > 255) {
38 value = 255; 63 value = 255;
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 return SkNEW_ARGS(SkArithmeticMode_dst, (i3, i4)); 209 return SkNEW_ARGS(SkArithmeticMode_dst, (i3, i4));
185 } 210 }
186 if (0 == i3) { 211 if (0 == i3) {
187 return SkNEW_ARGS(SkArithmeticMode_src, (i2, i4)); 212 return SkNEW_ARGS(SkArithmeticMode_src, (i2, i4));
188 } 213 }
189 return SkNEW_ARGS(SkArithmeticMode_linear, (i2, i3, i4)); 214 return SkNEW_ARGS(SkArithmeticMode_linear, (i2, i3, i4));
190 #endif 215 #endif
191 } 216 }
192 return SkNEW_ARGS(SkArithmeticMode_scalar, (k1, k2, k3, k4)); 217 return SkNEW_ARGS(SkArithmeticMode_scalar, (k1, k2, k3, k4));
193 } 218 }
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 fKUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
328 kVec4f_GrSLType, "k");
329 const char* kUni = builder->getUniformCStr(fKUni);
330
331 const char* srcColor = "srcColor";
332
333 // We don't try to optimize for this case at all
334 if (NULL == inputColor) {
335 builder->fsCodeAppendf("\t\tconst vec4 srcColor = %s;\n", GrGLSLOnesVecf (4));
336 } else {
337 builder->fsCodeAppendf("\t\tvec4 srcColor = %s;\n", inputColor);
338 builder->fsCodeAppendf("\t\tsrcColor.rgb = clamp(srcColor.rgb / srcColor .a, 0.0, 1.0);\n");
339 }
340
341 builder->fsCodeAppendf("\t\t%s.rgb = clamp(%s.rgb / %s.a, 0.0, 1.0);\n", dst Color, dstColor, dstColor);
342
343 builder->fsCodeAppendf("\t\t%s = %s.x * %s * %s + %s.y * %s + %s.z * %s + %s .w;\n", outputColor, kUni, srcColor, dstColor, kUni, srcColor, kUni, dstColor, k Uni);
344 builder->fsCodeAppendf("\t\t%s = clamp(%s, 0.0, 1.0);\n", outputColor, outpu tColor);
345 builder->fsCodeAppendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor);
346 }
347
348 void GrGLArithmeticEffect::setData(const GrGLUniformManager& uman, const GrDrawE ffect& drawEffect) {
349 const GrArithmeticEffect& arith = drawEffect.castEffect<GrArithmeticEffect>( );
350 uman.set4f(fKUni, arith.k1(), arith.k2(), arith.k3(), arith.k4());
351 }
352
353 GrGLEffect::EffectKey GrGLArithmeticEffect::GenKey(const GrDrawEffect& drawEffec t, const GrGLCaps&) {
354 return 0;
355 }
356
357 GrEffectRef* GrArithmeticEffect::TestCreate(SkMWCRandom* rand,
358 GrContext*,
359 const GrDrawTargetCaps&,
360 GrTexture*[]) {
361 float k1 = rand->nextF();
362 float k2 = rand->nextF();
363 float k3 = rand->nextF();
364 float k4 = rand->nextF();
365
366 static AutoEffectUnref gEffect(SkNEW_ARGS(GrArithmeticEffect, (k1, k2, k3, k 4)));
367 return CreateEffectRef(gEffect);
368 }
369
370 GR_DEFINE_EFFECT_TEST(GrArithmeticEffect);
371
372 bool SkArithmeticMode_scalar::asNewEffectOrCoeff(GrContext*,
373 GrEffectRef** effect,
374 Coeff*,
375 Coeff*) const {
376 if (effect) {
377 *effect = GrArithmeticEffect::Create(SkScalarToFloat(fK[0]),
378 SkScalarToFloat(fK[1]),
379 SkScalarToFloat(fK[2]),
380 SkScalarToFloat(fK[3]));
381 }
382 return true;
383 }
384
385 #endif
386
387 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkArithmeticMode)
388 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkArithmeticMode_scalar)
389 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
OLDNEW
« no previous file with comments | « include/effects/SkArithmeticMode.h ('k') | src/ports/SkGlobalInitialization_default.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698