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.
8 #include "SkArenaAlloc.h"
10 #include "SkBitmapProcShader.h"
11 #include "SkColorShader.h"
12 #include "SkEmptyShader.h"
13 #include "SkMallocPixelRef.h"
15 #include "SkPicture.h"
16 #include "SkPictureShader.h"
17 #include "SkPM4fPriv.h"
18 #include "SkRasterPipeline.h"
19 #include "SkReadBuffer.h"
23 #include "SkWriteBuffer.h"
24 #include "../jumper/SkJumper.h"
27 #include "GrFragmentProcessor.h"
30 //#define SK_TRACK_SHADER_LIFETIME
32 #ifdef SK_TRACK_SHADER_LIFETIME
33 static int32_t gShaderCounter;
36 static inline void inc_shader_counter() {
37 #ifdef SK_TRACK_SHADER_LIFETIME
38 int32_t prev = sk_atomic_inc(&gShaderCounter);
39 SkDebugf("+++ shader counter %d\n", prev + 1);
42 static inline void dec_shader_counter() {
43 #ifdef SK_TRACK_SHADER_LIFETIME
44 int32_t prev = sk_atomic_dec(&gShaderCounter);
45 SkDebugf("--- shader counter %d\n", prev - 1);
49 SkShader::SkShader(const SkMatrix* localMatrix) {
52 fLocalMatrix = *localMatrix;
56 // Pre-cache so future calls to fLocalMatrix.getType() are threadsafe.
57 (void)fLocalMatrix.getType();
60 SkShader::~SkShader() {
64 void SkShader::flatten(SkWriteBuffer& buffer) const {
65 this->INHERITED::flatten(buffer);
66 bool hasLocalM = !fLocalMatrix.isIdentity();
67 buffer.writeBool(hasLocalM);
69 buffer.writeMatrix(fLocalMatrix);
73 bool SkShader::computeTotalInverse(const SkMatrix& ctm,
74 const SkMatrix* outerLocalMatrix,
75 SkMatrix* totalInverse) const {
76 SkMatrix total = SkMatrix::Concat(ctm, fLocalMatrix);
77 if (outerLocalMatrix) {
78 total.preConcat(*outerLocalMatrix);
81 return total.invert(totalInverse);
84 bool SkShader::asLuminanceColor(SkColor* colorPtr) const {
86 if (nullptr == colorPtr) {
89 if (this->onAsLuminanceColor(colorPtr)) {
90 *colorPtr = SkColorSetA(*colorPtr, 0xFF); // we only return opaque
96 SkShader::Context* SkShader::makeContext(const ContextRec& rec, SkArenaAlloc* alloc) const {
97 if (!this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, nullptr)) {
100 return this->onMakeContext(rec, alloc);
103 SkShader::Context::Context(const SkShader& shader, const ContextRec& rec)
104 : fShader(shader), fCTM(*rec.fMatrix)
106 // We should never use a context for RP-only shaders.
107 SkASSERT(!shader.isRasterPipelineOnly());
109 // Because the context parameters must be valid at this point, we know that the matrix is
111 SkAssertResult(fShader.computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &fTotalInverse));
112 fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse);
114 fPaintAlpha = rec.fPaint->getAlpha();
117 SkShader::Context::~Context() {}
119 SkShader::Context::ShadeProc SkShader::Context::asAShadeProc(void** ctx) {
123 void SkShader::Context::shadeSpan4f(int x, int y, SkPM4f dst[], int count) {
127 int n = SkTMin(count, N);
128 this->shadeSpan(x, y, tmp, n);
129 for (int i = 0; i < n; ++i) {
130 dst[i] = SkPM4f::FromPMColor(tmp[i]);
138 #include "SkColorPriv.h"
140 #define kTempColorQuadCount 6 // balance between speed (larger) and saving stack-space
141 #define kTempColorCount (kTempColorQuadCount << 2)
143 #ifdef SK_CPU_BENDIAN
144 #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3))
146 #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3)
149 void SkShader::Context::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
152 SkPMColor colors[kTempColorCount];
154 while ((count -= kTempColorCount) >= 0) {
155 this->shadeSpan(x, y, colors, kTempColorCount);
156 x += kTempColorCount;
158 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
159 int quads = kTempColorQuadCount;
166 *alpha++ = SkToU8(a0);
167 *alpha++ = SkToU8(a1);
168 *alpha++ = SkToU8(a2);
169 *alpha++ = SkToU8(a3);
170 } while (--quads != 0);
173 SkASSERT(count + kTempColorCount >= 0);
174 if (count += kTempColorCount) {
175 this->shadeSpan(x, y, colors, count);
177 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
181 } while (--count != 0);
186 if (n > kTempColorCount)
190 this->shadeSpan(x, y, colors, n);
194 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
203 SkShader::Context::MatrixClass SkShader::Context::ComputeMatrixClass(const SkMatrix& mat) {
204 MatrixClass mc = kLinear_MatrixClass;
206 if (mat.hasPerspective()) {
207 if (mat.isFixedStepInX()) {
208 mc = kFixedStepInX_MatrixClass;
210 mc = kPerspective_MatrixClass;
216 //////////////////////////////////////////////////////////////////////////////
218 SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const {
219 return kNone_GradientType;
223 sk_sp<GrFragmentProcessor> SkShader::asFragmentProcessor(const AsFPArgs&) const {
228 sk_sp<SkShader> SkShader::makeAsALocalMatrixShader(SkMatrix*) const {
232 sk_sp<SkShader> SkShader::MakeEmptyShader() { return sk_make_sp<SkEmptyShader>(); }
234 sk_sp<SkShader> SkShader::MakeColorShader(SkColor color) { return sk_make_sp<SkColorShader>(color); }
236 sk_sp<SkShader> SkShader::MakeBitmapShader(const SkBitmap& src, TileMode tmx, TileMode tmy,
237 const SkMatrix* localMatrix) {
238 if (localMatrix && !localMatrix->invert(nullptr)) {
241 return SkMakeBitmapShader(src, tmx, tmy, localMatrix, kIfMutable_SkCopyPixelsMode);
244 sk_sp<SkShader> SkShader::MakePictureShader(sk_sp<SkPicture> src, TileMode tmx, TileMode tmy,
245 const SkMatrix* localMatrix, const SkRect* tile) {
246 if (localMatrix && !localMatrix->invert(nullptr)) {
249 return SkPictureShader::Make(std::move(src), tmx, tmy, localMatrix, tile);
252 #ifndef SK_IGNORE_TO_STRING
253 void SkShader::toString(SkString* str) const {
254 if (!fLocalMatrix.isIdentity()) {
256 fLocalMatrix.toString(str);
261 bool SkShader::appendStages(SkRasterPipeline* p,
265 const SkPaint& paint,
266 const SkMatrix* localM) const {
267 SkRasterPipeline_<256> subclass;
268 if (this->onAppendStages(&subclass, dstCS, alloc, ctm, paint, localM)) {
273 // SkShader::Context::shadeSpan4f() handles the paint opacity internally,
274 // but SkRasterPipelineBlitter applies it as a separate stage.
275 // We skip the internal shadeSpan4f() step by forcing the paint opaque.
276 SkTCopyOnFirstWrite<SkPaint> opaquePaint(paint);
277 if (paint.getAlpha() != SK_AlphaOPAQUE) {
278 opaquePaint.writable()->setAlpha(SK_AlphaOPAQUE);
281 ContextRec rec(*opaquePaint, ctm, localM, ContextRec::kPM4f_DstType, dstCS);
283 struct CallbackCtx : SkJumper_CallbackCtx {
284 sk_sp<SkShader> shader;
287 auto cb = alloc->make<CallbackCtx>();
288 cb->shader = dstCS ? SkColorSpaceXformer::Make(sk_ref_sp(dstCS))->apply(this)
289 : sk_ref_sp(const_cast<SkShader*>(this));
290 cb->ctx = cb->shader->makeContext(rec, alloc);
291 cb->fn = [](SkJumper_CallbackCtx* self, int active_pixels) {
292 auto c = (CallbackCtx*)self;
293 int x = (int)c->rgba[0],
295 c->ctx->shadeSpan4f(x,y, (SkPM4f*)c->rgba, active_pixels);
299 p->append(SkRasterPipeline::callback, cb);
305 bool SkShader::onAppendStages(SkRasterPipeline* p,
309 const SkPaint& paint,
310 const SkMatrix* localM) const {
314 ///////////////////////////////////////////////////////////////////////////////////////////////////
316 sk_sp<SkFlattenable> SkEmptyShader::CreateProc(SkReadBuffer&) {
317 return SkShader::MakeEmptyShader();
320 #ifndef SK_IGNORE_TO_STRING
321 #include "SkEmptyShader.h"
323 void SkEmptyShader::toString(SkString* str) const {
324 str->append("SkEmptyShader: (");
326 this->INHERITED::toString(str);