blob: c72c759f5cce4a5e511ee8887f83830a45220f78 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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
Florin Malita9206c762017-01-30 12:08:05 -05008#include "SkArenaAlloc.h"
herbb906daf2015-09-29 09:37:59 -07009#include "SkAtomics.h"
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000010#include "SkBitmapProcShader.h"
reed8367b8c2014-08-22 08:30:20 -070011#include "SkColorShader.h"
Florin Malita47e55a52017-06-06 12:26:54 -040012#include "SkColorSpaceXformer.h"
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +000013#include "SkEmptyShader.h"
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000014#include "SkMallocPixelRef.h"
15#include "SkPaint.h"
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000016#include "SkPicture.h"
17#include "SkPictureShader.h"
Florin Malita9206c762017-01-30 12:08:05 -050018#include "SkPM4fPriv.h"
19#include "SkRasterPipeline.h"
mtklein1b249332015-07-07 12:21:21 -070020#include "SkReadBuffer.h"
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +000021#include "SkScalar.h"
Florin Malita4aed1382017-05-25 10:38:07 -040022#include "SkShaderBase.h"
Florin Malita9206c762017-01-30 12:08:05 -050023#include "SkTLazy.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000024#include "SkWriteBuffer.h"
Mike Kleindc80eaa2017-04-21 12:39:08 -040025#include "../jumper/SkJumper.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000026
bungeman06ca8ec2016-06-09 08:01:03 -070027#if SK_SUPPORT_GPU
28#include "GrFragmentProcessor.h"
29#endif
30
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000031//#define SK_TRACK_SHADER_LIFETIME
32
33#ifdef SK_TRACK_SHADER_LIFETIME
34 static int32_t gShaderCounter;
35#endif
36
37static inline void inc_shader_counter() {
38#ifdef SK_TRACK_SHADER_LIFETIME
39 int32_t prev = sk_atomic_inc(&gShaderCounter);
40 SkDebugf("+++ shader counter %d\n", prev + 1);
41#endif
42}
43static inline void dec_shader_counter() {
44#ifdef SK_TRACK_SHADER_LIFETIME
45 int32_t prev = sk_atomic_dec(&gShaderCounter);
46 SkDebugf("--- shader counter %d\n", prev - 1);
47#endif
48}
49
Florin Malita4aed1382017-05-25 10:38:07 -040050SkShaderBase::SkShaderBase(const SkMatrix* localMatrix)
51 : fLocalMatrix(localMatrix ? *localMatrix : SkMatrix::I()) {
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000052 inc_shader_counter();
mtklein435eba72014-12-01 12:06:24 -080053 // Pre-cache so future calls to fLocalMatrix.getType() are threadsafe.
54 (void)fLocalMatrix.getType();
reed@android.com8a1c16f2008-12-17 15:59:43 +000055}
56
Florin Malita4aed1382017-05-25 10:38:07 -040057SkShaderBase::~SkShaderBase() {
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +000058 dec_shader_counter();
reed@android.com8a1c16f2008-12-17 15:59:43 +000059}
60
Florin Malita4aed1382017-05-25 10:38:07 -040061void SkShaderBase::flatten(SkWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 this->INHERITED::flatten(buffer);
commit-bot@chromium.org5970f622014-05-12 20:42:21 +000063 bool hasLocalM = !fLocalMatrix.isIdentity();
bsalomon@google.comf94b3a42012-10-31 18:09:01 +000064 buffer.writeBool(hasLocalM);
65 if (hasLocalM) {
66 buffer.writeMatrix(fLocalMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 }
68}
69
Florin Malita4aed1382017-05-25 10:38:07 -040070bool SkShaderBase::computeTotalInverse(const SkMatrix& ctm,
71 const SkMatrix* outerLocalMatrix,
72 SkMatrix* totalInverse) const {
Florin Malita26368c32017-05-08 13:03:24 -040073 SkMatrix total = SkMatrix::Concat(ctm, fLocalMatrix);
74 if (outerLocalMatrix) {
75 total.preConcat(*outerLocalMatrix);
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +000076 }
Florin Malitabbeb5732017-01-26 16:23:06 -050077
78 return total.invert(totalInverse);
reed@android.com8a1c16f2008-12-17 15:59:43 +000079}
80
Florin Malita4aed1382017-05-25 10:38:07 -040081bool SkShaderBase::asLuminanceColor(SkColor* colorPtr) const {
reed8367b8c2014-08-22 08:30:20 -070082 SkColor storage;
halcanary96fcdcc2015-08-27 07:41:13 -070083 if (nullptr == colorPtr) {
reed8367b8c2014-08-22 08:30:20 -070084 colorPtr = &storage;
85 }
86 if (this->onAsLuminanceColor(colorPtr)) {
87 *colorPtr = SkColorSetA(*colorPtr, 0xFF); // we only return opaque
88 return true;
89 }
90 return false;
91}
92
Florin Malita4aed1382017-05-25 10:38:07 -040093SkShaderBase::Context* SkShaderBase::makeContext(const ContextRec& rec, SkArenaAlloc* alloc) const {
Florin Malita8b9566a2017-07-05 10:59:12 -040094 return this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, nullptr)
95 ? this->onMakeContext(rec, alloc)
96 : nullptr;
commit-bot@chromium.orgf3e50592014-04-30 23:29:02 +000097}
98
Florin Malita47e55a52017-06-06 12:26:54 -040099SkShaderBase::Context* SkShaderBase::makeBurstPipelineContext(const ContextRec& rec,
100 SkArenaAlloc* alloc) const {
101
102 SkASSERT(rec.fPreferredDstType == ContextRec::kPM4f_DstType);
103
Florin Malita5769dd22017-07-12 13:31:25 -0400104 // Always use vanilla stages for perspective.
105 if (rec.fMatrix->hasPerspective() || fLocalMatrix.hasPerspective()) {
106 return nullptr;
107 }
108
Florin Malita8b9566a2017-07-05 10:59:12 -0400109 return this->computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, nullptr)
110 ? this->onMakeBurstPipelineContext(rec, alloc)
111 : nullptr;
Florin Malita47e55a52017-06-06 12:26:54 -0400112}
113
Florin Malita4aed1382017-05-25 10:38:07 -0400114SkShaderBase::Context::Context(const SkShaderBase& shader, const ContextRec& rec)
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000115 : fShader(shader), fCTM(*rec.fMatrix)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000116{
Florin Malita7d022e02017-05-15 15:06:39 -0400117 // We should never use a context for RP-only shaders.
Mike Reed34042072017-08-08 16:29:22 -0400118 SkASSERT(!shader.isRasterPipelineOnly(*rec.fMatrix));
Florin Malita5769dd22017-07-12 13:31:25 -0400119 // ... or for perspective.
120 SkASSERT(!rec.fMatrix->hasPerspective());
121 SkASSERT(!rec.fLocalMatrix || !rec.fLocalMatrix->hasPerspective());
Florin Malita7d022e02017-05-15 15:06:39 -0400122
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000123 // Because the context parameters must be valid at this point, we know that the matrix is
124 // invertible.
Florin Malita26368c32017-05-08 13:03:24 -0400125 SkAssertResult(fShader.computeTotalInverse(*rec.fMatrix, rec.fLocalMatrix, &fTotalInverse));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000126
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000127 fPaintAlpha = rec.fPaint->getAlpha();
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000128}
129
Florin Malita4aed1382017-05-25 10:38:07 -0400130SkShaderBase::Context::~Context() {}
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000131
Florin Malita4aed1382017-05-25 10:38:07 -0400132void SkShaderBase::Context::shadeSpan4f(int x, int y, SkPM4f dst[], int count) {
reeda34be682016-02-15 07:48:35 -0800133 const int N = 128;
134 SkPMColor tmp[N];
135 while (count > 0) {
136 int n = SkTMin(count, N);
137 this->shadeSpan(x, y, tmp, n);
138 for (int i = 0; i < n; ++i) {
139 dst[i] = SkPM4f::FromPMColor(tmp[i]);
140 }
141 dst += n;
142 x += n;
143 count -= n;
144 }
reed6d3cef92016-01-22 01:04:29 -0800145}
146
Florin Malita4aed1382017-05-25 10:38:07 -0400147const SkMatrix& SkShader::getLocalMatrix() const {
148 return as_SB(this)->getLocalMatrix();
149}
150
151#ifdef SK_SUPPORT_LEGACY_SHADER_ISABITMAP
152bool SkShader::isABitmap(SkBitmap* outTexture, SkMatrix* outMatrix, TileMode xy[2]) const {
153 return as_SB(this)->onIsABitmap(outTexture, outMatrix, xy);
154}
155#endif
156
157SkImage* SkShader::isAImage(SkMatrix* localMatrix, TileMode xy[2]) const {
158 return as_SB(this)->onIsAImage(localMatrix, xy);
159}
160
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000161SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const {
162 return kNone_GradientType;
163}
164
bungeman06ca8ec2016-06-09 08:01:03 -0700165#if SK_SUPPORT_GPU
Brian Salomonaff329b2017-08-11 09:40:37 -0400166std::unique_ptr<GrFragmentProcessor> SkShaderBase::asFragmentProcessor(const AsFPArgs&) const {
bsalomonc21b09e2015-08-28 18:46:56 -0700167 return nullptr;
rileya@google.com03c1c352012-07-20 20:02:43 +0000168}
bungeman06ca8ec2016-06-09 08:01:03 -0700169#endif
rileya@google.com03c1c352012-07-20 20:02:43 +0000170
Florin Malitad93e11c2017-05-24 21:15:46 +0000171sk_sp<SkShader> SkShader::makeAsALocalMatrixShader(SkMatrix*) const {
halcanary96fcdcc2015-08-27 07:41:13 -0700172 return nullptr;
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000173}
174
reed8a21c9f2016-03-08 18:50:00 -0800175sk_sp<SkShader> SkShader::MakeEmptyShader() { return sk_make_sp<SkEmptyShader>(); }
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000176
reed8a21c9f2016-03-08 18:50:00 -0800177sk_sp<SkShader> SkShader::MakeColorShader(SkColor color) { return sk_make_sp<SkColorShader>(color); }
reed8367b8c2014-08-22 08:30:20 -0700178
reed8a21c9f2016-03-08 18:50:00 -0800179sk_sp<SkShader> SkShader::MakeBitmapShader(const SkBitmap& src, TileMode tmx, TileMode tmy,
180 const SkMatrix* localMatrix) {
Florin Malita8d3ffad2017-02-03 18:21:17 +0000181 if (localMatrix && !localMatrix->invert(nullptr)) {
182 return nullptr;
183 }
Herb Derbybfdc87a2017-02-14 15:06:23 +0000184 return SkMakeBitmapShader(src, tmx, tmy, localMatrix, kIfMutable_SkCopyPixelsMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185}
186
reed7fb4f8b2016-03-11 04:33:52 -0800187sk_sp<SkShader> SkShader::MakePictureShader(sk_sp<SkPicture> src, TileMode tmx, TileMode tmy,
reed8a21c9f2016-03-08 18:50:00 -0800188 const SkMatrix* localMatrix, const SkRect* tile) {
Florin Malita8d3ffad2017-02-03 18:21:17 +0000189 if (localMatrix && !localMatrix->invert(nullptr)) {
190 return nullptr;
191 }
reed8a21c9f2016-03-08 18:50:00 -0800192 return SkPictureShader::Make(std::move(src), tmx, tmy, localMatrix, tile);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000193}
194
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000195#ifndef SK_IGNORE_TO_STRING
Florin Malita4aed1382017-05-25 10:38:07 -0400196void SkShaderBase::toString(SkString* str) const {
commit-bot@chromium.org5970f622014-05-12 20:42:21 +0000197 if (!fLocalMatrix.isIdentity()) {
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000198 str->append(" ");
commit-bot@chromium.org5970f622014-05-12 20:42:21 +0000199 fLocalMatrix.toString(str);
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000200 }
201}
202#endif
203
Mike Reed1d8c42e2017-08-29 14:58:19 -0400204bool SkShaderBase::appendStages(const StageRec& rec) const {
205 return this->onAppendStages(rec);
Florin Malita9206c762017-01-30 12:08:05 -0500206}
207
Mike Reed1d8c42e2017-08-29 14:58:19 -0400208bool SkShaderBase::onAppendStages(const StageRec& rec) const {
Mike Reed6867eee2017-06-02 13:25:15 -0400209 // SkShader::Context::shadeSpan4f() handles the paint opacity internally,
210 // but SkRasterPipelineBlitter applies it as a separate stage.
211 // We skip the internal shadeSpan4f() step by forcing the paint opaque.
Mike Reed1d8c42e2017-08-29 14:58:19 -0400212 SkTCopyOnFirstWrite<SkPaint> opaquePaint(rec.fPaint);
213 if (rec.fPaint.getAlpha() != SK_AlphaOPAQUE) {
Mike Reed6867eee2017-06-02 13:25:15 -0400214 opaquePaint.writable()->setAlpha(SK_AlphaOPAQUE);
215 }
216
Mike Reed1d8c42e2017-08-29 14:58:19 -0400217 ContextRec cr(*opaquePaint, rec.fCTM, rec.fLocalM, ContextRec::kPM4f_DstType, rec.fDstCS);
Mike Reed6867eee2017-06-02 13:25:15 -0400218
219 struct CallbackCtx : SkJumper_CallbackCtx {
220 sk_sp<SkShader> shader;
221 Context* ctx;
222 };
Mike Reed1d8c42e2017-08-29 14:58:19 -0400223 auto cb = rec.fAlloc->make<CallbackCtx>();
224 cb->shader = rec.fDstCS ? SkColorSpaceXformer::Make(sk_ref_sp(rec.fDstCS))->apply(this)
225 : sk_ref_sp((SkShader*)this);
226 cb->ctx = as_SB(cb->shader)->makeContext(cr, rec.fAlloc);
Mike Reed6867eee2017-06-02 13:25:15 -0400227 cb->fn = [](SkJumper_CallbackCtx* self, int active_pixels) {
228 auto c = (CallbackCtx*)self;
229 int x = (int)c->rgba[0],
230 y = (int)c->rgba[1];
231 c->ctx->shadeSpan4f(x,y, (SkPM4f*)c->rgba, active_pixels);
232 };
233
234 if (cb->ctx) {
Mike Klein85f85362017-10-17 14:22:58 -0400235 rec.fPipeline->append_seed_shader();
Mike Reed1d8c42e2017-08-29 14:58:19 -0400236 rec.fPipeline->append(SkRasterPipeline::callback, cb);
Mike Reed6867eee2017-06-02 13:25:15 -0400237 return true;
238 }
Mike Klein44d32792017-05-10 12:29:38 -0400239 return false;
240}
241
reed0ccc62d2016-05-04 13:09:39 -0700242///////////////////////////////////////////////////////////////////////////////////////////////////
reed830dfd82016-03-16 12:29:01 -0700243
reed60c9b582016-04-03 09:11:13 -0700244sk_sp<SkFlattenable> SkEmptyShader::CreateProc(SkReadBuffer&) {
245 return SkShader::MakeEmptyShader();
reed9fa60da2014-08-21 07:59:51 -0700246}
247
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000248#ifndef SK_IGNORE_TO_STRING
reed@google.com37a20122011-07-05 18:54:12 +0000249#include "SkEmptyShader.h"
250
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000251void SkEmptyShader::toString(SkString* str) const {
252 str->append("SkEmptyShader: (");
253
254 this->INHERITED::toString(str);
255
256 str->append(")");
257}
258#endif