2 * Copyright 2006 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkBitmapProcShader.h"
10 #include "SkColorShader.h"
11 #include "SkEmptyShader.h"
12 #include "SkMallocPixelRef.h"
14 #include "SkPicture.h"
15 #include "SkPictureShader.h"
16 #include "SkReadBuffer.h"
19 #include "SkWriteBuffer.h"
21 //#define SK_TRACK_SHADER_LIFETIME
23 #ifdef SK_TRACK_SHADER_LIFETIME
24 static int32_t gShaderCounter;
27 static inline void inc_shader_counter() {
28 #ifdef SK_TRACK_SHADER_LIFETIME
29 int32_t prev = sk_atomic_inc(&gShaderCounter);
30 SkDebugf("+++ shader counter %d\n", prev + 1);
33 static inline void dec_shader_counter() {
34 #ifdef SK_TRACK_SHADER_LIFETIME
35 int32_t prev = sk_atomic_dec(&gShaderCounter);
36 SkDebugf("--- shader counter %d\n", prev - 1);
40 SkShader::SkShader(const SkMatrix* localMatrix) {
43 fLocalMatrix = *localMatrix;
47 // Pre-cache so future calls to fLocalMatrix.getType() are threadsafe.
48 (void)fLocalMatrix.getType();
51 SkShader::~SkShader() {
55 void SkShader::flatten(SkWriteBuffer& buffer) const {
56 this->INHERITED::flatten(buffer);
57 bool hasLocalM = !fLocalMatrix.isIdentity();
58 buffer.writeBool(hasLocalM);
60 buffer.writeMatrix(fLocalMatrix);
64 bool SkShader::computeTotalInverse(const ContextRec& rec, SkMatrix* totalInverse) const {
66 total.setConcat(*rec.fMatrix, fLocalMatrix);
68 const SkMatrix* m = &total;
69 if (rec.fLocalMatrix) {
70 total.setConcat(*m, *rec.fLocalMatrix);
73 return m->invert(totalInverse);
76 bool SkShader::asLuminanceColor(SkColor* colorPtr) const {
78 if (nullptr == colorPtr) {
81 if (this->onAsLuminanceColor(colorPtr)) {
82 *colorPtr = SkColorSetA(*colorPtr, 0xFF); // we only return opaque
88 SkShader::Context* SkShader::createContext(const ContextRec& rec, void* storage) const {
89 if (!this->computeTotalInverse(rec, nullptr)) {
92 return this->onCreateContext(rec, storage);
95 SkShader::Context* SkShader::onCreateContext(const ContextRec& rec, void*) const {
99 size_t SkShader::contextSize(const ContextRec& rec) const {
100 return this->onContextSize(rec);
103 size_t SkShader::onContextSize(const ContextRec&) const {
107 SkShader::Context::Context(const SkShader& shader, const ContextRec& rec)
108 : fShader(shader), fCTM(*rec.fMatrix)
110 // Because the context parameters must be valid at this point, we know that the matrix is
112 SkAssertResult(fShader.computeTotalInverse(rec, &fTotalInverse));
113 fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse);
115 fPaintAlpha = rec.fPaint->getAlpha();
118 SkShader::Context::~Context() {}
120 SkShader::Context::ShadeProc SkShader::Context::asAShadeProc(void** ctx) {
124 void SkShader::Context::shadeSpan4f(int x, int y, SkPM4f dst[], int count) {
128 int n = SkTMin(count, N);
129 this->shadeSpan(x, y, tmp, n);
130 for (int i = 0; i < n; ++i) {
131 dst[i] = SkPM4f::FromPMColor(tmp[i]);
139 #include "SkColorPriv.h"
141 #define kTempColorQuadCount 6 // balance between speed (larger) and saving stack-space
142 #define kTempColorCount (kTempColorQuadCount << 2)
144 #ifdef SK_CPU_BENDIAN
145 #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3))
147 #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3)
150 void SkShader::Context::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
153 SkPMColor colors[kTempColorCount];
155 while ((count -= kTempColorCount) >= 0) {
156 this->shadeSpan(x, y, colors, kTempColorCount);
157 x += kTempColorCount;
159 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
160 int quads = kTempColorQuadCount;
167 *alpha++ = SkToU8(a0);
168 *alpha++ = SkToU8(a1);
169 *alpha++ = SkToU8(a2);
170 *alpha++ = SkToU8(a3);
171 } while (--quads != 0);
174 SkASSERT(count + kTempColorCount >= 0);
175 if (count += kTempColorCount) {
176 this->shadeSpan(x, y, colors, count);
178 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
182 } while (--count != 0);
187 if (n > kTempColorCount)
191 this->shadeSpan(x, y, colors, n);
195 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
204 SkShader::Context::MatrixClass SkShader::Context::ComputeMatrixClass(const SkMatrix& mat) {
205 MatrixClass mc = kLinear_MatrixClass;
207 if (mat.hasPerspective()) {
208 if (mat.isFixedStepInX()) {
209 mc = kFixedStepInX_MatrixClass;
211 mc = kPerspective_MatrixClass;
217 //////////////////////////////////////////////////////////////////////////////
219 SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const {
220 return kNone_GradientType;
223 const GrFragmentProcessor* SkShader::asFragmentProcessor(GrContext*, const SkMatrix&,
224 const SkMatrix*, SkFilterQuality,
225 SkSourceGammaTreatment) const {
229 SkShader* SkShader::refAsALocalMatrixShader(SkMatrix*) const {
233 sk_sp<SkShader> SkShader::MakeEmptyShader() { return sk_make_sp<SkEmptyShader>(); }
235 sk_sp<SkShader> SkShader::MakeColorShader(SkColor color) { return sk_make_sp<SkColorShader>(color); }
237 sk_sp<SkShader> SkShader::MakeBitmapShader(const SkBitmap& src, TileMode tmx, TileMode tmy,
238 const SkMatrix* localMatrix) {
239 return SkMakeBitmapShader(src, tmx, tmy, localMatrix, nullptr);
242 sk_sp<SkShader> SkShader::MakePictureShader(sk_sp<SkPicture> src, TileMode tmx, TileMode tmy,
243 const SkMatrix* localMatrix, const SkRect* tile) {
244 return SkPictureShader::Make(std::move(src), tmx, tmy, localMatrix, tile);
247 #ifndef SK_IGNORE_TO_STRING
248 void SkShader::toString(SkString* str) const {
249 if (!fLocalMatrix.isIdentity()) {
251 fLocalMatrix.toString(str);
256 ///////////////////////////////////////////////////////////////////////////////////////////////////
258 sk_sp<SkFlattenable> SkEmptyShader::CreateProc(SkReadBuffer&) {
259 return SkShader::MakeEmptyShader();
262 #ifndef SK_IGNORE_TO_STRING
263 #include "SkEmptyShader.h"
265 void SkEmptyShader::toString(SkString* str) const {
266 str->append("SkEmptyShader: (");
268 this->INHERITED::toString(str);
274 ///////////////////////////////////////////////////////////////////////////////////////////////////
276 #ifdef SK_SUPPORT_LEGACY_CREATESHADER_PTR
277 SkShader* SkShader::CreateComposeShader(SkShader* dst, SkShader* src, SkXfermode::Mode mode) {
278 return MakeComposeShader(sk_ref_sp(dst), sk_ref_sp(src), mode).release();
280 SkShader* SkShader::CreateComposeShader(SkShader* dst, SkShader* src, SkXfermode* xfer) {
281 return MakeComposeShader(sk_ref_sp(dst), sk_ref_sp(src), xfer).release();
283 SkShader* SkShader::CreatePictureShader(const SkPicture* src, TileMode tmx, TileMode tmy,
284 const SkMatrix* localMatrix, const SkRect* tile) {
285 return MakePictureShader(sk_ref_sp(const_cast<SkPicture*>(src)), tmx, tmy,
286 localMatrix, tile).release();
288 SkShader* SkShader::newWithColorFilter(SkColorFilter* filter) const {
289 return this->makeWithColorFilter(sk_ref_sp(filter)).release();
293 #ifdef SK_SUPPORT_LEGACY_XFERMODE_PTR
294 #include "SkXfermode.h"
295 sk_sp<SkShader> SkShader::MakeComposeShader(sk_sp<SkShader> dst, sk_sp<SkShader> src,
297 return MakeComposeShader(std::move(dst), std::move(src), sk_ref_sp(xfer));