• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2006 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "SkArenaAlloc.h"
9 #include "SkBitmapProcShader.h"
10 #include "SkColorShader.h"
11 #include "SkColorSpaceXformer.h"
12 #include "SkEmptyShader.h"
13 #include "SkMallocPixelRef.h"
14 #include "SkPaint.h"
15 #include "SkPicture.h"
16 #include "SkPictureShader.h"
17 #include "SkRasterPipeline.h"
18 #include "SkReadBuffer.h"
19 #include "SkScalar.h"
20 #include "SkShaderBase.h"
21 #include "SkTLazy.h"
22 #include "SkWriteBuffer.h"
23 
24 #if SK_SUPPORT_GPU
25 #include "GrFragmentProcessor.h"
26 #endif
27 
SkShaderBase(const SkMatrix * localMatrix)28 SkShaderBase::SkShaderBase(const SkMatrix* localMatrix)
29     : fLocalMatrix(localMatrix ? *localMatrix : SkMatrix::I()) {
30     // Pre-cache so future calls to fLocalMatrix.getType() are threadsafe.
31     (void)fLocalMatrix.getType();
32 }
33 
~SkShaderBase()34 SkShaderBase::~SkShaderBase() {}
35 
flatten(SkWriteBuffer & buffer) const36 void SkShaderBase::flatten(SkWriteBuffer& buffer) const {
37     this->INHERITED::flatten(buffer);
38     bool hasLocalM = !fLocalMatrix.isIdentity();
39     buffer.writeBool(hasLocalM);
40     if (hasLocalM) {
41         buffer.writeMatrix(fLocalMatrix);
42     }
43 }
44 
45 SkTCopyOnFirstWrite<SkMatrix>
totalLocalMatrix(const SkMatrix * preLocalMatrix,const SkMatrix * postLocalMatrix) const46 SkShaderBase::totalLocalMatrix(const SkMatrix* preLocalMatrix,
47                                const SkMatrix* postLocalMatrix) const {
48     SkTCopyOnFirstWrite<SkMatrix> m(fLocalMatrix);
49 
50     if (preLocalMatrix) {
51         m.writable()->preConcat(*preLocalMatrix);
52     }
53 
54     if (postLocalMatrix) {
55         m.writable()->postConcat(*postLocalMatrix);
56     }
57 
58     return m;
59 }
60 
computeTotalInverse(const SkMatrix & ctm,const SkMatrix * outerLocalMatrix,SkMatrix * totalInverse) const61 bool SkShaderBase::computeTotalInverse(const SkMatrix& ctm,
62                                        const SkMatrix* outerLocalMatrix,
63                                        SkMatrix* totalInverse) const {
64     return SkMatrix::Concat(ctm, *this->totalLocalMatrix(outerLocalMatrix)).invert(totalInverse);
65 }
66 
asLuminanceColor(SkColor * colorPtr) const67 bool SkShaderBase::asLuminanceColor(SkColor* colorPtr) const {
68     SkColor storage;
69     if (nullptr == colorPtr) {
70         colorPtr = &storage;
71     }
72     if (this->onAsLuminanceColor(colorPtr)) {
73         *colorPtr = SkColorSetA(*colorPtr, 0xFF);   // we only return opaque
74         return true;
75     }
76     return false;
77 }
78 
makeContext(const ContextRec & rec,SkArenaAlloc * alloc) const79 SkShaderBase::Context* SkShaderBase::makeContext(const ContextRec& rec, SkArenaAlloc* alloc) const {
80 #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
81     // We always fall back to raster pipeline when perspective is present.
82     if (rec.fMatrix->hasPerspective() ||
83         fLocalMatrix.hasPerspective() ||
84         (rec.fLocalMatrix && rec.fLocalMatrix->hasPerspective()) ||
85         !this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, nullptr)) {
86         return nullptr;
87     }
88 
89     return this->onMakeContext(rec, alloc);
90 #else
91     return nullptr;
92 #endif
93 }
94 
Context(const SkShaderBase & shader,const ContextRec & rec)95 SkShaderBase::Context::Context(const SkShaderBase& shader, const ContextRec& rec)
96     : fShader(shader), fCTM(*rec.fMatrix)
97 {
98     // We should never use a context with perspective.
99     SkASSERT(!rec.fMatrix->hasPerspective());
100     SkASSERT(!rec.fLocalMatrix || !rec.fLocalMatrix->hasPerspective());
101     SkASSERT(!shader.getLocalMatrix().hasPerspective());
102 
103     // Because the context parameters must be valid at this point, we know that the matrix is
104     // invertible.
105     SkAssertResult(fShader.computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &fTotalInverse));
106 
107     fPaintAlpha = rec.fPaint->getAlpha();
108 }
109 
~Context()110 SkShaderBase::Context::~Context() {}
111 
getLocalMatrix() const112 const SkMatrix& SkShader::getLocalMatrix() const {
113     return as_SB(this)->getLocalMatrix();
114 }
115 
isAImage(SkMatrix * localMatrix,TileMode xy[2]) const116 SkImage* SkShader::isAImage(SkMatrix* localMatrix, TileMode xy[2]) const {
117     return as_SB(this)->onIsAImage(localMatrix, xy);
118 }
119 
asAGradient(GradientInfo * info) const120 SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const {
121     return kNone_GradientType;
122 }
123 
124 #if SK_SUPPORT_GPU
asFragmentProcessor(const GrFPArgs &) const125 std::unique_ptr<GrFragmentProcessor> SkShaderBase::asFragmentProcessor(const GrFPArgs&) const {
126     return nullptr;
127 }
128 #endif
129 
makeAsALocalMatrixShader(SkMatrix *) const130 sk_sp<SkShader> SkShader::makeAsALocalMatrixShader(SkMatrix*) const {
131     return nullptr;
132 }
133 
MakeEmptyShader()134 sk_sp<SkShader> SkShader::MakeEmptyShader() { return sk_make_sp<SkEmptyShader>(); }
135 
MakeColorShader(SkColor color)136 sk_sp<SkShader> SkShader::MakeColorShader(SkColor color) { return sk_make_sp<SkColorShader>(color); }
137 
MakeBitmapShader(const SkBitmap & src,TileMode tmx,TileMode tmy,const SkMatrix * localMatrix)138 sk_sp<SkShader> SkShader::MakeBitmapShader(const SkBitmap& src, TileMode tmx, TileMode tmy,
139                                            const SkMatrix* localMatrix) {
140     if (localMatrix && !localMatrix->invert(nullptr)) {
141         return nullptr;
142     }
143     return SkMakeBitmapShader(src, tmx, tmy, localMatrix, kIfMutable_SkCopyPixelsMode);
144 }
145 
MakePictureShader(sk_sp<SkPicture> src,TileMode tmx,TileMode tmy,const SkMatrix * localMatrix,const SkRect * tile)146 sk_sp<SkShader> SkShader::MakePictureShader(sk_sp<SkPicture> src, TileMode tmx, TileMode tmy,
147                                             const SkMatrix* localMatrix, const SkRect* tile) {
148     if (localMatrix && !localMatrix->invert(nullptr)) {
149         return nullptr;
150     }
151     return SkPictureShader::Make(std::move(src), tmx, tmy, localMatrix, tile);
152 }
153 
appendStages(const StageRec & rec) const154 bool SkShaderBase::appendStages(const StageRec& rec) const {
155     return this->onAppendStages(rec);
156 }
157 
onAppendStages(const StageRec & rec) const158 bool SkShaderBase::onAppendStages(const StageRec& rec) const {
159     // SkShader::Context::shadeSpan() handles the paint opacity internally,
160     // but SkRasterPipelineBlitter applies it as a separate stage.
161     // We skip the internal shadeSpan() step by forcing the paint opaque.
162     SkTCopyOnFirstWrite<SkPaint> opaquePaint(rec.fPaint);
163     if (rec.fPaint.getAlpha() != SK_AlphaOPAQUE) {
164         opaquePaint.writable()->setAlpha(SK_AlphaOPAQUE);
165     }
166 
167     ContextRec cr(*opaquePaint, rec.fCTM, rec.fLocalM, rec.fDstColorType, rec.fDstCS);
168 
169     struct CallbackCtx : SkRasterPipeline_CallbackCtx {
170         sk_sp<SkShader> shader;
171         Context*        ctx;
172     };
173     auto cb = rec.fAlloc->make<CallbackCtx>();
174     cb->shader = rec.fDstCS ? SkColorSpaceXformer::Make(sk_ref_sp(rec.fDstCS))->apply(this)
175                             : sk_ref_sp((SkShader*)this);
176     cb->ctx = as_SB(cb->shader)->makeContext(cr, rec.fAlloc);
177     cb->fn  = [](SkRasterPipeline_CallbackCtx* self, int active_pixels) {
178         auto c = (CallbackCtx*)self;
179         int x = (int)c->rgba[0],
180             y = (int)c->rgba[1];
181         SkPMColor tmp[SkRasterPipeline_kMaxStride];
182         c->ctx->shadeSpan(x,y, tmp, active_pixels);
183 
184         for (int i = 0; i < active_pixels; i++) {
185             auto rgba_4f = SkPMColor4f::FromPMColor(tmp[i]);
186             memcpy(c->rgba + 4*i, rgba_4f.vec(), 4*sizeof(float));
187         }
188     };
189 
190     if (cb->ctx) {
191         rec.fPipeline->append(SkRasterPipeline::seed_shader);
192         rec.fPipeline->append(SkRasterPipeline::callback, cb);
193         return true;
194     }
195     return false;
196 }
197 
198 ///////////////////////////////////////////////////////////////////////////////////////////////////
199 
CreateProc(SkReadBuffer &)200 sk_sp<SkFlattenable> SkEmptyShader::CreateProc(SkReadBuffer&) {
201     return SkShader::MakeEmptyShader();
202 }
203