blob: ae31482ae7bdf5ff3747680b902286a9447660a5 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkCanvas.h"
reed@google.com0716c632011-04-12 18:32:06 +00009#include "SkColor.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000010#include "SkReadBuffer.h"
11#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkLayerDrawLooper.h"
robertphillips@google.com4991b8f2013-01-28 20:21:59 +000013#include "SkString.h"
14#include "SkStringUtils.h"
reed@google.com0716c632011-04-12 18:32:06 +000015#include "SkUnPreMultiply.h"
16
17SkLayerDrawLooper::LayerInfo::LayerInfo() {
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000018 fPaintBits = 0; // ignore our paint fields
reed@google.com0716c632011-04-12 18:32:06 +000019 fColorMode = SkXfermode::kDst_Mode; // ignore our color
20 fOffset.set(0, 0);
21 fPostTranslate = false;
22}
reed@android.com8a1c16f2008-12-17 15:59:43 +000023
vandebo@chromium.org3c898182011-06-22 05:01:28 +000024SkLayerDrawLooper::SkLayerDrawLooper()
halcanary96fcdcc2015-08-27 07:41:13 -070025 : fRecs(nullptr),
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000026 fCount(0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000027}
28
29SkLayerDrawLooper::~SkLayerDrawLooper() {
30 Rec* rec = fRecs;
31 while (rec) {
32 Rec* next = rec->fNext;
halcanary385fe4d2015-08-26 13:07:48 -070033 delete rec;
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 rec = next;
35 }
36}
reed@google.com0716c632011-04-12 18:32:06 +000037
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000038SkLayerDrawLooper::Context* SkLayerDrawLooper::createContext(SkCanvas* canvas, void* storage) const {
commit-bot@chromium.org091a5942014-04-18 14:19:31 +000039 canvas->save();
halcanary385fe4d2015-08-26 13:07:48 -070040 return new (storage) LayerDrawLooperContext(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +000041}
42
reed@google.com0716c632011-04-12 18:32:06 +000043static SkColor xferColor(SkColor src, SkColor dst, SkXfermode::Mode mode) {
44 switch (mode) {
45 case SkXfermode::kSrc_Mode:
46 return src;
47 case SkXfermode::kDst_Mode:
48 return dst;
49 default: {
50 SkPMColor pmS = SkPreMultiplyColor(src);
51 SkPMColor pmD = SkPreMultiplyColor(dst);
52 SkPMColor result = SkXfermode::GetProc(mode)(pmS, pmD);
53 return SkUnPreMultiply::PMColorToColor(result);
54 }
55 }
56}
57
reed@google.com84260582011-11-21 16:42:10 +000058// Even with kEntirePaint_Bits, we always ensure that the master paint's
59// text-encoding is respected, since that controls how we interpret the
60// text/length parameters of a draw[Pos]Text call.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000061void SkLayerDrawLooper::LayerDrawLooperContext::ApplyInfo(
62 SkPaint* dst, const SkPaint& src, const LayerInfo& info) {
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000063
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000064 dst->setColor(xferColor(src.getColor(), dst->getColor(), info.fColorMode));
65
66 BitFlags bits = info.fPaintBits;
reed@google.com84260582011-11-21 16:42:10 +000067 SkPaint::TextEncoding encoding = dst->getTextEncoding();
reed@google.com0716c632011-04-12 18:32:06 +000068
mike@reedtribe.org3c1225b2011-04-09 18:54:08 +000069 if (0 == bits) {
70 return;
71 }
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000072 if (kEntirePaint_Bits == bits) {
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000073 // we've already computed these, so save it from the assignment
74 uint32_t f = dst->getFlags();
reed@google.com0716c632011-04-12 18:32:06 +000075 SkColor c = dst->getColor();
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000076 *dst = src;
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000077 dst->setFlags(f);
reed@google.com0716c632011-04-12 18:32:06 +000078 dst->setColor(c);
reed@google.com84260582011-11-21 16:42:10 +000079 dst->setTextEncoding(encoding);
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000080 return;
81 }
82
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000083 if (bits & kStyle_Bit) {
84 dst->setStyle(src.getStyle());
85 dst->setStrokeWidth(src.getStrokeWidth());
86 dst->setStrokeMiter(src.getStrokeMiter());
87 dst->setStrokeCap(src.getStrokeCap());
88 dst->setStrokeJoin(src.getStrokeJoin());
89 }
90
91 if (bits & kTextSkewX_Bit) {
92 dst->setTextSkewX(src.getTextSkewX());
93 }
94
95 if (bits & kPathEffect_Bit) {
reeda4393342016-03-18 11:22:57 -070096 dst->setPathEffect(sk_ref_sp(src.getPathEffect()));
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000097 }
98 if (bits & kMaskFilter_Bit) {
99 dst->setMaskFilter(src.getMaskFilter());
100 }
101 if (bits & kShader_Bit) {
102 dst->setShader(src.getShader());
103 }
104 if (bits & kColorFilter_Bit) {
reed1eb81db2016-03-22 07:35:17 -0700105 dst->setColorFilter(src.getColorFilter());
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000106 }
107 if (bits & kXfermode_Bit) {
108 dst->setXfermode(src.getXfermode());
109 }
110
reed@google.com84260582011-11-21 16:42:10 +0000111 // we don't override these
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000112#if 0
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000113 dst->setTypeface(src.getTypeface());
114 dst->setTextSize(src.getTextSize());
115 dst->setTextScaleX(src.getTextScaleX());
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000116 dst->setRasterizer(src.getRasterizer());
117 dst->setLooper(src.getLooper());
118 dst->setTextEncoding(src.getTextEncoding());
119 dst->setHinting(src.getHinting());
120#endif
121}
122
reed@google.com0716c632011-04-12 18:32:06 +0000123// Should we add this to canvas?
124static void postTranslate(SkCanvas* canvas, SkScalar dx, SkScalar dy) {
125 SkMatrix m = canvas->getTotalMatrix();
126 m.postTranslate(dx, dy);
127 canvas->setMatrix(m);
128}
129
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000130SkLayerDrawLooper::LayerDrawLooperContext::LayerDrawLooperContext(
131 const SkLayerDrawLooper* looper) : fCurrRec(looper->fRecs) {}
132
133bool SkLayerDrawLooper::LayerDrawLooperContext::next(SkCanvas* canvas,
134 SkPaint* paint) {
reed@google.com4e2b3d32011-04-07 14:18:59 +0000135 canvas->restore();
halcanary96fcdcc2015-08-27 07:41:13 -0700136 if (nullptr == fCurrRec) {
reed@google.com4e2b3d32011-04-07 14:18:59 +0000137 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +0000140 ApplyInfo(paint, fCurrRec->fPaint, fCurrRec->fInfo);
reed@google.com0716c632011-04-12 18:32:06 +0000141
commit-bot@chromium.org091a5942014-04-18 14:19:31 +0000142 canvas->save();
reed@google.com0716c632011-04-12 18:32:06 +0000143 if (fCurrRec->fInfo.fPostTranslate) {
144 postTranslate(canvas, fCurrRec->fInfo.fOffset.fX,
145 fCurrRec->fInfo.fOffset.fY);
146 } else {
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000147 canvas->translate(fCurrRec->fInfo.fOffset.fX,
148 fCurrRec->fInfo.fOffset.fY);
reed@google.com0716c632011-04-12 18:32:06 +0000149 }
mike@reedtribe.orge5d0def2011-04-08 00:53:48 +0000150 fCurrRec = fCurrRec->fNext;
151
reed@google.com4e2b3d32011-04-07 14:18:59 +0000152 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153}
154
reed@google.comdaaafa62014-04-29 15:20:16 +0000155bool SkLayerDrawLooper::asABlurShadow(BlurShadowRec* bsRec) const {
156 if (fCount != 2) {
157 return false;
158 }
159 const Rec* rec = fRecs;
160
161 // bottom layer needs to be just blur(maskfilter)
162 if ((rec->fInfo.fPaintBits & ~kMaskFilter_Bit)) {
163 return false;
164 }
165 if (SkXfermode::kSrc_Mode != rec->fInfo.fColorMode) {
166 return false;
167 }
168 const SkMaskFilter* mf = rec->fPaint.getMaskFilter();
halcanary96fcdcc2015-08-27 07:41:13 -0700169 if (nullptr == mf) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000170 return false;
171 }
172 SkMaskFilter::BlurRec maskBlur;
173 if (!mf->asABlur(&maskBlur)) {
174 return false;
175 }
176
177 rec = rec->fNext;
178 // top layer needs to be "plain"
179 if (rec->fInfo.fPaintBits) {
180 return false;
181 }
182 if (SkXfermode::kDst_Mode != rec->fInfo.fColorMode) {
183 return false;
184 }
185 if (!rec->fInfo.fOffset.equals(0, 0)) {
186 return false;
187 }
188
189 if (bsRec) {
190 bsRec->fSigma = maskBlur.fSigma;
191 bsRec->fOffset = fRecs->fInfo.fOffset;
192 bsRec->fColor = fRecs->fPaint.getColor();
193 bsRec->fStyle = maskBlur.fStyle;
194 bsRec->fQuality = maskBlur.fQuality;
195 }
196 return true;
197}
198
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199///////////////////////////////////////////////////////////////////////////////
200
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000201void SkLayerDrawLooper::flatten(SkWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202 buffer.writeInt(fCount);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000203
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204 Rec* rec = fRecs;
205 for (int i = 0; i < fCount; i++) {
reed@google.com842292f2014-02-28 15:38:22 +0000206 // Legacy "flagsmask" field -- now ignored, remove when we bump version
207 buffer.writeInt(0);
208
reed@google.com0716c632011-04-12 18:32:06 +0000209 buffer.writeInt(rec->fInfo.fPaintBits);
210 buffer.writeInt(rec->fInfo.fColorMode);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000211 buffer.writePoint(rec->fInfo.fOffset);
reed@google.com0716c632011-04-12 18:32:06 +0000212 buffer.writeBool(rec->fInfo.fPostTranslate);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000213 buffer.writePaint(rec->fPaint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214 rec = rec->fNext;
215 }
216}
217
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000218SkFlattenable* SkLayerDrawLooper::CreateProc(SkReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219 int count = buffer.readInt();
220
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000221 Builder builder;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000222 for (int i = 0; i < count; i++) {
reed@google.com0716c632011-04-12 18:32:06 +0000223 LayerInfo info;
reed@google.com842292f2014-02-28 15:38:22 +0000224 // Legacy "flagsmask" field -- now ignored, remove when we bump version
reed@google.com48f31bd2014-02-27 14:27:44 +0000225 (void)buffer.readInt();
reed@google.com842292f2014-02-28 15:38:22 +0000226
reed@google.com0716c632011-04-12 18:32:06 +0000227 info.fPaintBits = buffer.readInt();
228 info.fColorMode = (SkXfermode::Mode)buffer.readInt();
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000229 buffer.readPoint(&info.fOffset);
reed@google.com0716c632011-04-12 18:32:06 +0000230 info.fPostTranslate = buffer.readBool();
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000231 buffer.readPaint(builder.addLayerOnTop(info));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000232 }
reed7b380d02016-03-21 13:25:16 -0700233 return builder.detach().release();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234}
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000235
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000236#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000237void SkLayerDrawLooper::toString(SkString* str) const {
238 str->appendf("SkLayerDrawLooper (%d): ", fCount);
239
240 Rec* rec = fRecs;
241 for (int i = 0; i < fCount; i++) {
reed@google.com842292f2014-02-28 15:38:22 +0000242 str->appendf("%d: paintBits: (", i);
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000243 if (0 == rec->fInfo.fPaintBits) {
244 str->append("None");
245 } else if (kEntirePaint_Bits == rec->fInfo.fPaintBits) {
246 str->append("EntirePaint");
247 } else {
248 bool needSeparator = false;
249 SkAddFlagToString(str, SkToBool(kStyle_Bit & rec->fInfo.fPaintBits), "Style",
250 &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000251 SkAddFlagToString(str, SkToBool(kTextSkewX_Bit & rec->fInfo.fPaintBits), "TextSkewX",
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000252 &needSeparator);
253 SkAddFlagToString(str, SkToBool(kPathEffect_Bit & rec->fInfo.fPaintBits), "PathEffect",
254 &needSeparator);
255 SkAddFlagToString(str, SkToBool(kMaskFilter_Bit & rec->fInfo.fPaintBits), "MaskFilter",
256 &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000257 SkAddFlagToString(str, SkToBool(kShader_Bit & rec->fInfo.fPaintBits), "Shader",
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000258 &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000259 SkAddFlagToString(str, SkToBool(kColorFilter_Bit & rec->fInfo.fPaintBits), "ColorFilter",
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000260 &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000261 SkAddFlagToString(str, SkToBool(kXfermode_Bit & rec->fInfo.fPaintBits), "Xfermode",
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000262 &needSeparator);
263 }
264 str->append(") ");
265
266 static const char* gModeStrings[SkXfermode::kLastMode+1] = {
267 "kClear", "kSrc", "kDst", "kSrcOver", "kDstOver", "kSrcIn", "kDstIn",
268 "kSrcOut", "kDstOut", "kSrcATop", "kDstATop", "kXor", "kPlus",
269 "kMultiply", "kScreen", "kOverlay", "kDarken", "kLighten", "kColorDodge",
270 "kColorBurn", "kHardLight", "kSoftLight", "kDifference", "kExclusion"
271 };
272
273 str->appendf("mode: %s ", gModeStrings[rec->fInfo.fColorMode]);
274
275 str->append("offset: (");
276 str->appendScalar(rec->fInfo.fOffset.fX);
277 str->append(", ");
278 str->appendScalar(rec->fInfo.fOffset.fY);
279 str->append(") ");
280
281 str->append("postTranslate: ");
282 if (rec->fInfo.fPostTranslate) {
283 str->append("true ");
284 } else {
285 str->append("false ");
286 }
287
robertphillips@google.com791f12e2013-02-14 13:53:53 +0000288 rec->fPaint.toString(str);
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000289 rec = rec->fNext;
290 }
291}
292#endif
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000293
294SkLayerDrawLooper::Builder::Builder()
halcanary96fcdcc2015-08-27 07:41:13 -0700295 : fRecs(nullptr),
296 fTopRec(nullptr),
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000297 fCount(0) {
298}
299
300SkLayerDrawLooper::Builder::~Builder() {
301 Rec* rec = fRecs;
302 while (rec) {
303 Rec* next = rec->fNext;
halcanary385fe4d2015-08-26 13:07:48 -0700304 delete rec;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000305 rec = next;
306 }
307}
308
309SkPaint* SkLayerDrawLooper::Builder::addLayer(const LayerInfo& info) {
310 fCount += 1;
311
halcanary385fe4d2015-08-26 13:07:48 -0700312 Rec* rec = new Rec;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000313 rec->fNext = fRecs;
314 rec->fInfo = info;
315 fRecs = rec;
halcanary96fcdcc2015-08-27 07:41:13 -0700316 if (nullptr == fTopRec) {
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000317 fTopRec = rec;
318 }
319
320 return &rec->fPaint;
321}
322
323void SkLayerDrawLooper::Builder::addLayer(SkScalar dx, SkScalar dy) {
324 LayerInfo info;
325
326 info.fOffset.set(dx, dy);
327 (void)this->addLayer(info);
328}
329
330SkPaint* SkLayerDrawLooper::Builder::addLayerOnTop(const LayerInfo& info) {
331 fCount += 1;
332
halcanary385fe4d2015-08-26 13:07:48 -0700333 Rec* rec = new Rec;
halcanary96fcdcc2015-08-27 07:41:13 -0700334 rec->fNext = nullptr;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000335 rec->fInfo = info;
halcanary96fcdcc2015-08-27 07:41:13 -0700336 if (nullptr == fRecs) {
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000337 fRecs = rec;
338 } else {
bsalomon49f085d2014-09-05 13:34:00 -0700339 SkASSERT(fTopRec);
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000340 fTopRec->fNext = rec;
341 }
342 fTopRec = rec;
343
344 return &rec->fPaint;
345}
346
reed7b380d02016-03-21 13:25:16 -0700347sk_sp<SkDrawLooper> SkLayerDrawLooper::Builder::detach() {
halcanary385fe4d2015-08-26 13:07:48 -0700348 SkLayerDrawLooper* looper = new SkLayerDrawLooper;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000349 looper->fCount = fCount;
350 looper->fRecs = fRecs;
351
352 fCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700353 fRecs = nullptr;
354 fTopRec = nullptr;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000355
reed7b380d02016-03-21 13:25:16 -0700356 return sk_sp<SkDrawLooper>(looper);
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000357}