3 * Copyright 2006 The Android Open Source Project
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "SkComposeShader.h"
11 #include "SkColorFilter.h"
12 #include "SkColorPriv.h"
13 #include "SkColorShader.h"
14 #include "SkReadBuffer.h"
15 #include "SkWriteBuffer.h"
16 #include "SkXfermode.h"
19 ///////////////////////////////////////////////////////////////////////////////
21 SkComposeShader::SkComposeShader(SkShader* sA, SkShader* sB, SkXfermode* mode) {
22 fShaderA = sA; sA->ref();
23 fShaderB = sB; sB->ref();
29 SkComposeShader::SkComposeShader(SkReadBuffer& buffer) :
31 fShaderA = buffer.readShader();
32 if (NULL == fShaderA) {
33 fShaderA = SkNEW_ARGS(SkColorShader, ((SkColor)0));
35 fShaderB = buffer.readShader();
36 if (NULL == fShaderB) {
37 fShaderB = SkNEW_ARGS(SkColorShader, ((SkColor)0));
39 fMode = buffer.readXfermode();
42 SkComposeShader::~SkComposeShader() {
48 size_t SkComposeShader::contextSize() const {
49 return sizeof(ComposeShaderContext) + fShaderA->contextSize() + fShaderB->contextSize();
52 class SkAutoAlphaRestore {
54 SkAutoAlphaRestore(SkPaint* paint, uint8_t newAlpha) {
55 fAlpha = paint->getAlpha();
57 paint->setAlpha(newAlpha);
60 ~SkAutoAlphaRestore() {
61 fPaint->setAlpha(fAlpha);
67 #define SkAutoAlphaRestore(...) SK_REQUIRE_LOCAL_VAR(SkAutoAlphaRestore)
69 void SkComposeShader::flatten(SkWriteBuffer& buffer) const {
70 this->INHERITED::flatten(buffer);
71 buffer.writeFlattenable(fShaderA);
72 buffer.writeFlattenable(fShaderB);
73 buffer.writeFlattenable(fMode);
76 template <typename T> void safe_call_destructor(T* obj) {
82 SkShader::Context* SkComposeShader::onCreateContext(const ContextRec& rec, void* storage) const {
83 char* aStorage = (char*) storage + sizeof(ComposeShaderContext);
84 char* bStorage = aStorage + fShaderA->contextSize();
86 // we preconcat our localMatrix (if any) with the device matrix
87 // before calling our sub-shaders
89 tmpM.setConcat(*rec.fMatrix, this->getLocalMatrix());
91 // Our sub-shaders need to see opaque, so by combining them we don't double-alphatize the
92 // result. ComposeShader itself will respect the alpha, and post-apply it after calling the
94 SkPaint opaquePaint(*rec.fPaint);
95 opaquePaint.setAlpha(0xFF);
97 ContextRec newRec(rec);
98 newRec.fMatrix = &tmpM;
99 newRec.fPaint = &opaquePaint;
101 SkShader::Context* contextA = fShaderA->createContext(newRec, aStorage);
102 SkShader::Context* contextB = fShaderB->createContext(newRec, bStorage);
103 if (!contextA || !contextB) {
104 safe_call_destructor(contextA);
105 safe_call_destructor(contextB);
109 return SkNEW_PLACEMENT_ARGS(storage, ComposeShaderContext, (*this, rec, contextA, contextB));
112 SkComposeShader::ComposeShaderContext::ComposeShaderContext(
113 const SkComposeShader& shader, const ContextRec& rec,
114 SkShader::Context* contextA, SkShader::Context* contextB)
115 : INHERITED(shader, rec)
116 , fShaderContextA(contextA)
117 , fShaderContextB(contextB) {}
119 SkComposeShader::ComposeShaderContext::~ComposeShaderContext() {
120 fShaderContextA->~Context();
121 fShaderContextB->~Context();
124 // larger is better (fewer times we have to loop), but we shouldn't
125 // take up too much stack-space (each element is 4 bytes)
126 #define TMP_COLOR_COUNT 64
128 void SkComposeShader::ComposeShaderContext::shadeSpan(int x, int y, SkPMColor result[], int count) {
129 SkShader::Context* shaderContextA = fShaderContextA;
130 SkShader::Context* shaderContextB = fShaderContextB;
131 SkXfermode* mode = static_cast<const SkComposeShader&>(fShader).fMode;
132 unsigned scale = SkAlpha255To256(this->getPaintAlpha());
134 #ifdef SK_BUILD_FOR_ANDROID
135 scale = 256; // ugh -- maintain old bug/behavior for now
138 SkPMColor tmp[TMP_COLOR_COUNT];
140 if (NULL == mode) { // implied SRC_OVER
141 // TODO: when we have a good test-case, should use SkBlitRow::Proc32
145 if (n > TMP_COLOR_COUNT) {
149 shaderContextA->shadeSpan(x, y, result, n);
150 shaderContextB->shadeSpan(x, y, tmp, n);
153 for (int i = 0; i < n; i++) {
154 result[i] = SkPMSrcOver(tmp[i], result[i]);
157 for (int i = 0; i < n; i++) {
158 result[i] = SkAlphaMulQ(SkPMSrcOver(tmp[i], result[i]),
167 } else { // use mode for the composition
170 if (n > TMP_COLOR_COUNT) {
174 shaderContextA->shadeSpan(x, y, result, n);
175 shaderContextB->shadeSpan(x, y, tmp, n);
176 mode->xfer32(result, tmp, n, NULL);
179 for (int i = 0; i < n; i++) {
180 result[i] = SkAlphaMulQ(result[i], scale);
191 #ifndef SK_IGNORE_TO_STRING
192 void SkComposeShader::toString(SkString* str) const {
193 str->append("SkComposeShader: (");
195 str->append("ShaderA: ");
196 fShaderA->toString(str);
197 str->append(" ShaderB: ");
198 fShaderB->toString(str);
199 str->append(" Xfermode: ");
200 fMode->toString(str);
202 this->INHERITED::toString(str);