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 "SkReadBuffer.h" | 10 #include "SkReadBuffer.h" |
11 #include "SkWriteBuffer.h" | 11 #include "SkWriteBuffer.h" |
12 #include "SkString.h" | 12 #include "SkString.h" |
13 #include "SkUnPreMultiply.h" | 13 #include "SkUnPreMultiply.h" |
14 #if SK_SUPPORT_GPU | 14 #if SK_SUPPORT_GPU |
15 #include "GrContext.h" | 15 #include "SkArithmeticMode_gpu.h" |
16 #include "GrCoordTransform.h" | |
17 #include "GrInvariantOutput.h" | |
18 #include "gl/GrGLProcessor.h" | |
19 #include "gl/builders/GrGLProgramBuilder.h" | |
20 #endif | 16 #endif |
21 | 17 |
22 static const bool gUseUnpremul = false; | 18 static const bool gUseUnpremul = false; |
23 | 19 |
24 class SkArithmeticMode_scalar : public SkXfermode { | 20 class SkArithmeticMode_scalar : public SkXfermode { |
25 public: | 21 public: |
26 static SkArithmeticMode_scalar* Create(SkScalar k1, SkScalar k2, SkScalar k3
, SkScalar k4, | 22 static SkArithmeticMode_scalar* Create(SkScalar k1, SkScalar k2, SkScalar k3
, SkScalar k4, |
27 bool enforcePMColor) { | 23 bool enforcePMColor) { |
28 return SkNEW_ARGS(SkArithmeticMode_scalar, (k1, k2, k3, k4, enforcePMCol
or)); | 24 return SkNEW_ARGS(SkArithmeticMode_scalar, (k1, k2, k3, k4, enforcePMCol
or)); |
29 } | 25 } |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 return SkNEW_ARGS(SkArithmeticMode_linear, (i2, i3, i4)); | 226 return SkNEW_ARGS(SkArithmeticMode_linear, (i2, i3, i4)); |
231 #endif | 227 #endif |
232 } | 228 } |
233 return SkArithmeticMode_scalar::Create(k1, k2, k3, k4, enforcePMColor); | 229 return SkArithmeticMode_scalar::Create(k1, k2, k3, k4, enforcePMColor); |
234 } | 230 } |
235 | 231 |
236 | 232 |
237 ////////////////////////////////////////////////////////////////////////////// | 233 ////////////////////////////////////////////////////////////////////////////// |
238 | 234 |
239 #if SK_SUPPORT_GPU | 235 #if SK_SUPPORT_GPU |
240 | |
241 class GrGLArithmeticEffect : public GrGLFragmentProcessor { | |
242 public: | |
243 GrGLArithmeticEffect(const GrProcessor&); | |
244 virtual ~GrGLArithmeticEffect(); | |
245 | |
246 virtual void emitCode(GrGLFPBuilder*, | |
247 const GrFragmentProcessor&, | |
248 const char* outputColor, | |
249 const char* inputColor, | |
250 const TransformedCoordsArray&, | |
251 const TextureSamplerArray&) SK_OVERRIDE; | |
252 | |
253 void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE; | |
254 | |
255 static void GenKey(const GrProcessor&, const GrGLCaps& caps, GrProcessorKeyB
uilder* b); | |
256 | |
257 private: | |
258 GrGLProgramDataManager::UniformHandle fKUni; | |
259 bool fEnforcePMColor; | |
260 | |
261 typedef GrGLFragmentProcessor INHERITED; | |
262 }; | |
263 | |
264 /////////////////////////////////////////////////////////////////////////////// | |
265 | |
266 class GrArithmeticEffect : public GrFragmentProcessor { | |
267 public: | |
268 static GrFragmentProcessor* Create(float k1, float k2, float k3, float k4, b
ool enforcePMColor, | |
269 GrTexture* background) { | |
270 return SkNEW_ARGS(GrArithmeticEffect, (k1, k2, k3, k4, enforcePMColor, b
ackground)); | |
271 } | |
272 | |
273 virtual ~GrArithmeticEffect(); | |
274 | |
275 const char* name() const SK_OVERRIDE { return "Arithmetic"; } | |
276 | |
277 virtual void getGLProcessorKey(const GrGLCaps& caps, | |
278 GrProcessorKeyBuilder* b) const SK_OVERRIDE { | |
279 GrGLArithmeticEffect::GenKey(*this, caps, b); | |
280 } | |
281 | |
282 GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE { | |
283 return SkNEW_ARGS(GrGLArithmeticEffect, (*this)); | |
284 } | |
285 | |
286 GrTexture* backgroundTexture() const { return fBackgroundAccess.getTexture()
; } | |
287 | |
288 float k1() const { return fK1; } | |
289 float k2() const { return fK2; } | |
290 float k3() const { return fK3; } | |
291 float k4() const { return fK4; } | |
292 bool enforcePMColor() const { return fEnforcePMColor; } | |
293 | |
294 private: | |
295 bool onIsEqual(const GrFragmentProcessor&) const SK_OVERRIDE; | |
296 | |
297 void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE; | |
298 | |
299 GrArithmeticEffect(float k1, float k2, float k3, float k4, bool enforcePMCol
or, | |
300 GrTexture* background); | |
301 float fK1, fK2, fK3, fK4; | |
302 bool fEnforcePMColor; | |
303 GrCoordTransform fBackgroundTransform; | |
304 GrTextureAccess fBackgroundAccess; | |
305 | |
306 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; | |
307 typedef GrFragmentProcessor INHERITED; | |
308 | |
309 }; | |
310 | |
311 /////////////////////////////////////////////////////////////////////////////// | |
312 | |
313 GrArithmeticEffect::GrArithmeticEffect(float k1, float k2, float k3, float k4, | |
314 bool enforcePMColor, GrTexture* backgroun
d) | |
315 : fK1(k1), fK2(k2), fK3(k3), fK4(k4), fEnforcePMColor(enforcePMColor) { | |
316 this->initClassID<GrArithmeticEffect>(); | |
317 if (background) { | |
318 fBackgroundTransform.reset(kLocal_GrCoordSet, background, | |
319 GrTextureParams::kNone_FilterMode); | |
320 this->addCoordTransform(&fBackgroundTransform); | |
321 fBackgroundAccess.reset(background); | |
322 this->addTextureAccess(&fBackgroundAccess); | |
323 } else { | |
324 this->setWillReadDstColor(); | |
325 } | |
326 } | |
327 | |
328 GrArithmeticEffect::~GrArithmeticEffect() { | |
329 } | |
330 | |
331 bool GrArithmeticEffect::onIsEqual(const GrFragmentProcessor& sBase) const { | |
332 const GrArithmeticEffect& s = sBase.cast<GrArithmeticEffect>(); | |
333 return fK1 == s.fK1 && | |
334 fK2 == s.fK2 && | |
335 fK3 == s.fK3 && | |
336 fK4 == s.fK4 && | |
337 fEnforcePMColor == s.fEnforcePMColor; | |
338 } | |
339 | |
340 void GrArithmeticEffect::onComputeInvariantOutput(GrInvariantOutput* inout) cons
t { | |
341 // TODO: optimize this | |
342 inout->setToUnknown(GrInvariantOutput::kWill_ReadInput); | |
343 } | |
344 | |
345 /////////////////////////////////////////////////////////////////////////////// | |
346 | |
347 GrGLArithmeticEffect::GrGLArithmeticEffect(const GrProcessor&) | |
348 : fEnforcePMColor(true) { | |
349 } | |
350 | |
351 GrGLArithmeticEffect::~GrGLArithmeticEffect() { | |
352 } | |
353 | |
354 void GrGLArithmeticEffect::emitCode(GrGLFPBuilder* builder, | |
355 const GrFragmentProcessor& fp, | |
356 const char* outputColor, | |
357 const char* inputColor, | |
358 const TransformedCoordsArray& coords, | |
359 const TextureSamplerArray& samplers) { | |
360 | |
361 GrTexture* backgroundTex = fp.cast<GrArithmeticEffect>().backgroundTexture()
; | |
362 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); | |
363 const char* dstColor; | |
364 if (backgroundTex) { | |
365 fsBuilder->codeAppend("\t\tvec4 bgColor = "); | |
366 fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0]
.getType()); | |
367 fsBuilder->codeAppendf(";\n"); | |
368 dstColor = "bgColor"; | |
369 } else { | |
370 dstColor = fsBuilder->dstColor(); | |
371 } | |
372 | |
373 SkASSERT(dstColor); | |
374 fKUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, | |
375 kVec4f_GrSLType, kDefault_GrSLPrecision, | |
376 "k"); | |
377 const char* kUni = builder->getUniformCStr(fKUni); | |
378 | |
379 // We don't try to optimize for this case at all | |
380 if (NULL == inputColor) { | |
381 fsBuilder->codeAppendf("\t\tconst vec4 src = vec4(1);\n"); | |
382 } else { | |
383 fsBuilder->codeAppendf("\t\tvec4 src = %s;\n", inputColor); | |
384 if (gUseUnpremul) { | |
385 fsBuilder->codeAppendf("\t\tsrc.rgb = clamp(src.rgb / src.a, 0.0, 1.
0);\n"); | |
386 } | |
387 } | |
388 | |
389 fsBuilder->codeAppendf("\t\tvec4 dst = %s;\n", dstColor); | |
390 if (gUseUnpremul) { | |
391 fsBuilder->codeAppendf("\t\tdst.rgb = clamp(dst.rgb / dst.a, 0.0, 1.0);\
n"); | |
392 } | |
393 | |
394 fsBuilder->codeAppendf("\t\t%s = %s.x * src * dst + %s.y * src + %s.z * dst
+ %s.w;\n", outputColor, kUni, kUni, kUni, kUni); | |
395 fsBuilder->codeAppendf("\t\t%s = clamp(%s, 0.0, 1.0);\n", outputColor, outpu
tColor); | |
396 if (gUseUnpremul) { | |
397 fsBuilder->codeAppendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor
); | |
398 } else if (fEnforcePMColor) { | |
399 fsBuilder->codeAppendf("\t\t%s.rgb = min(%s.rgb, %s.a);\n", outputColor,
outputColor, outputColor); | |
400 } | |
401 } | |
402 | |
403 void GrGLArithmeticEffect::setData(const GrGLProgramDataManager& pdman, | |
404 const GrProcessor& processor) { | |
405 const GrArithmeticEffect& arith = processor.cast<GrArithmeticEffect>(); | |
406 pdman.set4f(fKUni, arith.k1(), arith.k2(), arith.k3(), arith.k4()); | |
407 fEnforcePMColor = arith.enforcePMColor(); | |
408 } | |
409 | |
410 void GrGLArithmeticEffect::GenKey(const GrProcessor& processor, | |
411 const GrGLCaps&, GrProcessorKeyBuilder* b) { | |
412 const GrArithmeticEffect& arith = processor.cast<GrArithmeticEffect>(); | |
413 uint32_t key = arith.enforcePMColor() ? 1 : 0; | |
414 if (arith.backgroundTexture()) { | |
415 key |= 2; | |
416 } | |
417 b->add32(key); | |
418 } | |
419 | |
420 GrFragmentProcessor* GrArithmeticEffect::TestCreate(SkRandom* rand, | |
421 GrContext*, | |
422 const GrDrawTargetCaps&, | |
423 GrTexture*[]) { | |
424 float k1 = rand->nextF(); | |
425 float k2 = rand->nextF(); | |
426 float k3 = rand->nextF(); | |
427 float k4 = rand->nextF(); | |
428 bool enforcePMColor = rand->nextBool(); | |
429 | |
430 return SkNEW_ARGS(GrArithmeticEffect, (k1, k2, k3, k4, enforcePMColor, NULL)
); | |
431 } | |
432 | |
433 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrArithmeticEffect); | |
434 | |
435 bool SkArithmeticMode_scalar::asFragmentProcessor(GrFragmentProcessor** fp, | 236 bool SkArithmeticMode_scalar::asFragmentProcessor(GrFragmentProcessor** fp, |
436 GrTexture* background) const { | 237 GrTexture* background) const { |
437 if (fp) { | 238 if (fp) { |
438 *fp = GrArithmeticEffect::Create(SkScalarToFloat(fK[0]), | 239 *fp = GrArithmeticFP::Create(SkScalarToFloat(fK[0]), |
439 SkScalarToFloat(fK[1]), | 240 SkScalarToFloat(fK[1]), |
440 SkScalarToFloat(fK[2]), | 241 SkScalarToFloat(fK[2]), |
441 SkScalarToFloat(fK[3]), | 242 SkScalarToFloat(fK[3]), |
442 fEnforcePMColor, | 243 fEnforcePMColor, |
443 background); | 244 background); |
444 } | 245 } |
445 return true; | 246 return true; |
446 } | 247 } |
447 | 248 |
448 #endif | 249 #endif |
449 | 250 |
450 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkArithmeticMode) | 251 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkArithmeticMode) |
451 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkArithmeticMode_scalar) | 252 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkArithmeticMode_scalar) |
452 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 253 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
OLD | NEW |