Add new SkSourceGammaTreatment enum, used in situations like mipmap construction...
[platform/upstream/libSkiaSharp.git] / src / core / SkShader.cpp
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 "SkAtomics.h"
9 #include "SkBitmapProcShader.h"
10 #include "SkColorShader.h"
11 #include "SkEmptyShader.h"
12 #include "SkMallocPixelRef.h"
13 #include "SkPaint.h"
14 #include "SkPicture.h"
15 #include "SkPictureShader.h"
16 #include "SkReadBuffer.h"
17 #include "SkScalar.h"
18 #include "SkShader.h"
19 #include "SkWriteBuffer.h"
20
21 //#define SK_TRACK_SHADER_LIFETIME
22
23 #ifdef SK_TRACK_SHADER_LIFETIME
24     static int32_t gShaderCounter;
25 #endif
26
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);
31 #endif
32 }
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);
37 #endif
38 }
39
40 SkShader::SkShader(const SkMatrix* localMatrix) {
41     inc_shader_counter();
42     if (localMatrix) {
43         fLocalMatrix = *localMatrix;
44     } else {
45         fLocalMatrix.reset();
46     }
47     // Pre-cache so future calls to fLocalMatrix.getType() are threadsafe.
48     (void)fLocalMatrix.getType();
49 }
50
51 SkShader::~SkShader() {
52     dec_shader_counter();
53 }
54
55 void SkShader::flatten(SkWriteBuffer& buffer) const {
56     this->INHERITED::flatten(buffer);
57     bool hasLocalM = !fLocalMatrix.isIdentity();
58     buffer.writeBool(hasLocalM);
59     if (hasLocalM) {
60         buffer.writeMatrix(fLocalMatrix);
61     }
62 }
63
64 bool SkShader::computeTotalInverse(const ContextRec& rec, SkMatrix* totalInverse) const {
65     SkMatrix total;
66     total.setConcat(*rec.fMatrix, fLocalMatrix);
67
68     const SkMatrix* m = &total;
69     if (rec.fLocalMatrix) {
70         total.setConcat(*m, *rec.fLocalMatrix);
71         m = &total;
72     }
73     return m->invert(totalInverse);
74 }
75
76 bool SkShader::asLuminanceColor(SkColor* colorPtr) const {
77     SkColor storage;
78     if (nullptr == colorPtr) {
79         colorPtr = &storage;
80     }
81     if (this->onAsLuminanceColor(colorPtr)) {
82         *colorPtr = SkColorSetA(*colorPtr, 0xFF);   // we only return opaque
83         return true;
84     }
85     return false;
86 }
87
88 SkShader::Context* SkShader::createContext(const ContextRec& rec, void* storage) const {
89     if (!this->computeTotalInverse(rec, nullptr)) {
90         return nullptr;
91     }
92     return this->onCreateContext(rec, storage);
93 }
94
95 SkShader::Context* SkShader::onCreateContext(const ContextRec& rec, void*) const {
96     return nullptr;
97 }
98
99 size_t SkShader::contextSize(const ContextRec& rec) const {
100     return this->onContextSize(rec);
101 }
102
103 size_t SkShader::onContextSize(const ContextRec&) const {
104     return 0;
105 }
106
107 SkShader::Context::Context(const SkShader& shader, const ContextRec& rec)
108     : fShader(shader), fCTM(*rec.fMatrix)
109 {
110     // Because the context parameters must be valid at this point, we know that the matrix is
111     // invertible.
112     SkAssertResult(fShader.computeTotalInverse(rec, &fTotalInverse));
113     fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse);
114
115     fPaintAlpha = rec.fPaint->getAlpha();
116 }
117
118 SkShader::Context::~Context() {}
119
120 SkShader::Context::ShadeProc SkShader::Context::asAShadeProc(void** ctx) {
121     return nullptr;
122 }
123
124 void SkShader::Context::shadeSpan4f(int x, int y, SkPM4f dst[], int count) {
125     const int N = 128;
126     SkPMColor tmp[N];
127     while (count > 0) {
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]);
132         }
133         dst += n;
134         x += n;
135         count -= n;
136     }
137 }
138
139 #include "SkColorPriv.h"
140
141 #define kTempColorQuadCount 6   // balance between speed (larger) and saving stack-space
142 #define kTempColorCount     (kTempColorQuadCount << 2)
143
144 #ifdef SK_CPU_BENDIAN
145     #define SkU32BitShiftToByteOffset(shift)    (3 - ((shift) >> 3))
146 #else
147     #define SkU32BitShiftToByteOffset(shift)    ((shift) >> 3)
148 #endif
149
150 void SkShader::Context::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
151     SkASSERT(count > 0);
152
153     SkPMColor   colors[kTempColorCount];
154
155     while ((count -= kTempColorCount) >= 0) {
156         this->shadeSpan(x, y, colors, kTempColorCount);
157         x += kTempColorCount;
158
159         const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
160         int quads = kTempColorQuadCount;
161         do {
162             U8CPU a0 = srcA[0];
163             U8CPU a1 = srcA[4];
164             U8CPU a2 = srcA[8];
165             U8CPU a3 = srcA[12];
166             srcA += 4*4;
167             *alpha++ = SkToU8(a0);
168             *alpha++ = SkToU8(a1);
169             *alpha++ = SkToU8(a2);
170             *alpha++ = SkToU8(a3);
171         } while (--quads != 0);
172     }
173     SkASSERT(count < 0);
174     SkASSERT(count + kTempColorCount >= 0);
175     if (count += kTempColorCount) {
176         this->shadeSpan(x, y, colors, count);
177
178         const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
179         do {
180             *alpha++ = *srcA;
181             srcA += 4;
182         } while (--count != 0);
183     }
184 #if 0
185     do {
186         int n = count;
187         if (n > kTempColorCount)
188             n = kTempColorCount;
189         SkASSERT(n > 0);
190
191         this->shadeSpan(x, y, colors, n);
192         x += n;
193         count -= n;
194
195         const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
196         do {
197             *alpha++ = *srcA;
198             srcA += 4;
199         } while (--n != 0);
200     } while (count > 0);
201 #endif
202 }
203
204 SkShader::Context::MatrixClass SkShader::Context::ComputeMatrixClass(const SkMatrix& mat) {
205     MatrixClass mc = kLinear_MatrixClass;
206
207     if (mat.hasPerspective()) {
208         if (mat.isFixedStepInX()) {
209             mc = kFixedStepInX_MatrixClass;
210         } else {
211             mc = kPerspective_MatrixClass;
212         }
213     }
214     return mc;
215 }
216
217 //////////////////////////////////////////////////////////////////////////////
218
219 SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const {
220     return kNone_GradientType;
221 }
222
223 const GrFragmentProcessor* SkShader::asFragmentProcessor(GrContext*, const SkMatrix&,
224                                                          const SkMatrix*, SkFilterQuality,
225                                                          SkSourceGammaTreatment)  const {
226     return nullptr;
227 }
228
229 SkShader* SkShader::refAsALocalMatrixShader(SkMatrix*) const {
230     return nullptr;
231 }
232
233 sk_sp<SkShader> SkShader::MakeEmptyShader() { return sk_make_sp<SkEmptyShader>(); }
234
235 sk_sp<SkShader> SkShader::MakeColorShader(SkColor color) { return sk_make_sp<SkColorShader>(color); }
236
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);
240 }
241
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);
245 }
246
247 #ifndef SK_IGNORE_TO_STRING
248 void SkShader::toString(SkString* str) const {
249     if (!fLocalMatrix.isIdentity()) {
250         str->append(" ");
251         fLocalMatrix.toString(str);
252     }
253 }
254 #endif
255
256 ///////////////////////////////////////////////////////////////////////////////////////////////////
257
258 sk_sp<SkFlattenable> SkEmptyShader::CreateProc(SkReadBuffer&) {
259     return SkShader::MakeEmptyShader();
260 }
261
262 #ifndef SK_IGNORE_TO_STRING
263 #include "SkEmptyShader.h"
264
265 void SkEmptyShader::toString(SkString* str) const {
266     str->append("SkEmptyShader: (");
267
268     this->INHERITED::toString(str);
269
270     str->append(")");
271 }
272 #endif
273
274 ///////////////////////////////////////////////////////////////////////////////////////////////////
275
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();
279 }
280 SkShader* SkShader::CreateComposeShader(SkShader* dst, SkShader* src, SkXfermode* xfer) {
281     return MakeComposeShader(sk_ref_sp(dst), sk_ref_sp(src), xfer).release();
282 }
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();
287 }
288 SkShader* SkShader::newWithColorFilter(SkColorFilter* filter) const {
289     return this->makeWithColorFilter(sk_ref_sp(filter)).release();
290 }
291 #endif
292
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,
296                                             SkXfermode* xfer) {
297     return MakeComposeShader(std::move(dst), std::move(src), sk_ref_sp(xfer));
298 }
299 #endif