blob: e72d137c7ba858762830d63e125180d5d90e3bdf [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00007#include "SkCanvas.h"
reed@google.com0716c632011-04-12 18:32:06 +00008#include "SkColor.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +00009#include "SkReadBuffer.h"
10#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkLayerDrawLooper.h"
robertphillips@google.com4991b8f2013-01-28 20:21:59 +000012#include "SkString.h"
13#include "SkStringUtils.h"
reed@google.com0716c632011-04-12 18:32:06 +000014#include "SkUnPreMultiply.h"
15
16SkLayerDrawLooper::LayerInfo::LayerInfo() {
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000017 fPaintBits = 0; // ignore our paint fields
Mike Reedfaba3712016-11-03 14:45:31 -040018 fColorMode = SkBlendMode::kDst; // ignore our color
reed@google.com0716c632011-04-12 18:32:06 +000019 fOffset.set(0, 0);
20 fPostTranslate = false;
21}
reed@android.com8a1c16f2008-12-17 15:59:43 +000022
vandebo@chromium.org3c898182011-06-22 05:01:28 +000023SkLayerDrawLooper::SkLayerDrawLooper()
halcanary96fcdcc2015-08-27 07:41:13 -070024 : fRecs(nullptr),
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000025 fCount(0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000026}
27
28SkLayerDrawLooper::~SkLayerDrawLooper() {
29 Rec* rec = fRecs;
30 while (rec) {
31 Rec* next = rec->fNext;
halcanary385fe4d2015-08-26 13:07:48 -070032 delete rec;
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 rec = next;
34 }
35}
reed@google.com0716c632011-04-12 18:32:06 +000036
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000037SkLayerDrawLooper::Context* SkLayerDrawLooper::createContext(SkCanvas* canvas, void* storage) const {
commit-bot@chromium.org091a5942014-04-18 14:19:31 +000038 canvas->save();
halcanary385fe4d2015-08-26 13:07:48 -070039 return new (storage) LayerDrawLooperContext(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +000040}
41
Mike Reed7d954ad2016-10-28 15:42:34 -040042static SkColor xferColor(SkColor src, SkColor dst, SkBlendMode mode) {
reed@google.com0716c632011-04-12 18:32:06 +000043 switch (mode) {
Mike Reed7d954ad2016-10-28 15:42:34 -040044 case SkBlendMode::kSrc:
reed@google.com0716c632011-04-12 18:32:06 +000045 return src;
Mike Reed7d954ad2016-10-28 15:42:34 -040046 case SkBlendMode::kDst:
reed@google.com0716c632011-04-12 18:32:06 +000047 return dst;
48 default: {
49 SkPMColor pmS = SkPreMultiplyColor(src);
50 SkPMColor pmD = SkPreMultiplyColor(dst);
Mike Reed6a015542016-11-09 10:38:09 -050051 SkPMColor result = SkXfermode::GetProc(mode)(pmS, pmD);
reed@google.com0716c632011-04-12 18:32:06 +000052 return SkUnPreMultiply::PMColorToColor(result);
53 }
54 }
55}
56
reed@google.com84260582011-11-21 16:42:10 +000057// Even with kEntirePaint_Bits, we always ensure that the master paint's
58// text-encoding is respected, since that controls how we interpret the
59// text/length parameters of a draw[Pos]Text call.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000060void SkLayerDrawLooper::LayerDrawLooperContext::ApplyInfo(
61 SkPaint* dst, const SkPaint& src, const LayerInfo& info) {
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000062
Mike Reed7d954ad2016-10-28 15:42:34 -040063 dst->setColor(xferColor(src.getColor(), dst->getColor(), (SkBlendMode)info.fColorMode));
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000064
65 BitFlags bits = info.fPaintBits;
reed@google.com84260582011-11-21 16:42:10 +000066 SkPaint::TextEncoding encoding = dst->getTextEncoding();
reed@google.com0716c632011-04-12 18:32:06 +000067
mike@reedtribe.org3c1225b2011-04-09 18:54:08 +000068 if (0 == bits) {
69 return;
70 }
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000071 if (kEntirePaint_Bits == bits) {
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000072 // we've already computed these, so save it from the assignment
73 uint32_t f = dst->getFlags();
reed@google.com0716c632011-04-12 18:32:06 +000074 SkColor c = dst->getColor();
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000075 *dst = src;
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000076 dst->setFlags(f);
reed@google.com0716c632011-04-12 18:32:06 +000077 dst->setColor(c);
reed@google.com84260582011-11-21 16:42:10 +000078 dst->setTextEncoding(encoding);
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000079 return;
80 }
81
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000082 if (bits & kStyle_Bit) {
83 dst->setStyle(src.getStyle());
84 dst->setStrokeWidth(src.getStrokeWidth());
85 dst->setStrokeMiter(src.getStrokeMiter());
86 dst->setStrokeCap(src.getStrokeCap());
87 dst->setStrokeJoin(src.getStrokeJoin());
88 }
89
90 if (bits & kTextSkewX_Bit) {
91 dst->setTextSkewX(src.getTextSkewX());
92 }
93
94 if (bits & kPathEffect_Bit) {
reeda4393342016-03-18 11:22:57 -070095 dst->setPathEffect(sk_ref_sp(src.getPathEffect()));
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000096 }
97 if (bits & kMaskFilter_Bit) {
reedefdfd512016-04-04 10:02:58 -070098 dst->setMaskFilter(sk_ref_sp(src.getMaskFilter()));
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000099 }
100 if (bits & kShader_Bit) {
reedfe630452016-03-25 09:08:00 -0700101 dst->setShader(sk_ref_sp(src.getShader()));
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000102 }
103 if (bits & kColorFilter_Bit) {
reedd053ce92016-03-22 10:17:23 -0700104 dst->setColorFilter(sk_ref_sp(src.getColorFilter()));
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000105 }
106 if (bits & kXfermode_Bit) {
reed374772b2016-10-05 17:33:02 -0700107 dst->setBlendMode(src.getBlendMode());
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000108 }
109
reed@google.com84260582011-11-21 16:42:10 +0000110 // we don't override these
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000111#if 0
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000112 dst->setTypeface(src.getTypeface());
113 dst->setTextSize(src.getTextSize());
114 dst->setTextScaleX(src.getTextScaleX());
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000115 dst->setRasterizer(src.getRasterizer());
116 dst->setLooper(src.getLooper());
117 dst->setTextEncoding(src.getTextEncoding());
118 dst->setHinting(src.getHinting());
119#endif
120}
121
reed@google.com0716c632011-04-12 18:32:06 +0000122// Should we add this to canvas?
123static void postTranslate(SkCanvas* canvas, SkScalar dx, SkScalar dy) {
124 SkMatrix m = canvas->getTotalMatrix();
125 m.postTranslate(dx, dy);
126 canvas->setMatrix(m);
127}
128
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000129SkLayerDrawLooper::LayerDrawLooperContext::LayerDrawLooperContext(
130 const SkLayerDrawLooper* looper) : fCurrRec(looper->fRecs) {}
131
132bool SkLayerDrawLooper::LayerDrawLooperContext::next(SkCanvas* canvas,
133 SkPaint* paint) {
reed@google.com4e2b3d32011-04-07 14:18:59 +0000134 canvas->restore();
halcanary96fcdcc2015-08-27 07:41:13 -0700135 if (nullptr == fCurrRec) {
reed@google.com4e2b3d32011-04-07 14:18:59 +0000136 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +0000139 ApplyInfo(paint, fCurrRec->fPaint, fCurrRec->fInfo);
reed@google.com0716c632011-04-12 18:32:06 +0000140
commit-bot@chromium.org091a5942014-04-18 14:19:31 +0000141 canvas->save();
reed@google.com0716c632011-04-12 18:32:06 +0000142 if (fCurrRec->fInfo.fPostTranslate) {
143 postTranslate(canvas, fCurrRec->fInfo.fOffset.fX,
144 fCurrRec->fInfo.fOffset.fY);
145 } else {
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000146 canvas->translate(fCurrRec->fInfo.fOffset.fX,
147 fCurrRec->fInfo.fOffset.fY);
reed@google.com0716c632011-04-12 18:32:06 +0000148 }
mike@reedtribe.orge5d0def2011-04-08 00:53:48 +0000149 fCurrRec = fCurrRec->fNext;
150
reed@google.com4e2b3d32011-04-07 14:18:59 +0000151 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152}
153
reed@google.comdaaafa62014-04-29 15:20:16 +0000154bool SkLayerDrawLooper::asABlurShadow(BlurShadowRec* bsRec) const {
155 if (fCount != 2) {
156 return false;
157 }
158 const Rec* rec = fRecs;
159
160 // bottom layer needs to be just blur(maskfilter)
161 if ((rec->fInfo.fPaintBits & ~kMaskFilter_Bit)) {
162 return false;
163 }
Mike Reed7d954ad2016-10-28 15:42:34 -0400164 if (SkBlendMode::kSrc != (SkBlendMode)rec->fInfo.fColorMode) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000165 return false;
166 }
167 const SkMaskFilter* mf = rec->fPaint.getMaskFilter();
halcanary96fcdcc2015-08-27 07:41:13 -0700168 if (nullptr == mf) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000169 return false;
170 }
171 SkMaskFilter::BlurRec maskBlur;
172 if (!mf->asABlur(&maskBlur)) {
173 return false;
174 }
175
176 rec = rec->fNext;
177 // top layer needs to be "plain"
178 if (rec->fInfo.fPaintBits) {
179 return false;
180 }
Mike Reed7d954ad2016-10-28 15:42:34 -0400181 if (SkBlendMode::kDst != (SkBlendMode)rec->fInfo.fColorMode) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000182 return false;
183 }
184 if (!rec->fInfo.fOffset.equals(0, 0)) {
185 return false;
186 }
187
188 if (bsRec) {
189 bsRec->fSigma = maskBlur.fSigma;
190 bsRec->fOffset = fRecs->fInfo.fOffset;
191 bsRec->fColor = fRecs->fPaint.getColor();
192 bsRec->fStyle = maskBlur.fStyle;
193 bsRec->fQuality = maskBlur.fQuality;
194 }
195 return true;
196}
197
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198///////////////////////////////////////////////////////////////////////////////
199
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000200void SkLayerDrawLooper::flatten(SkWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201 buffer.writeInt(fCount);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000202
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203 Rec* rec = fRecs;
204 for (int i = 0; i < fCount; i++) {
reed@google.com842292f2014-02-28 15:38:22 +0000205 // Legacy "flagsmask" field -- now ignored, remove when we bump version
206 buffer.writeInt(0);
207
reed@google.com0716c632011-04-12 18:32:06 +0000208 buffer.writeInt(rec->fInfo.fPaintBits);
Mike Reed7d954ad2016-10-28 15:42:34 -0400209 buffer.writeInt((int)rec->fInfo.fColorMode);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000210 buffer.writePoint(rec->fInfo.fOffset);
reed@google.com0716c632011-04-12 18:32:06 +0000211 buffer.writeBool(rec->fInfo.fPostTranslate);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000212 buffer.writePaint(rec->fPaint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000213 rec = rec->fNext;
214 }
215}
216
reed60c9b582016-04-03 09:11:13 -0700217sk_sp<SkFlattenable> SkLayerDrawLooper::CreateProc(SkReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000218 int count = buffer.readInt();
219
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000220 Builder builder;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221 for (int i = 0; i < count; i++) {
reed@google.com0716c632011-04-12 18:32:06 +0000222 LayerInfo info;
reed@google.com842292f2014-02-28 15:38:22 +0000223 // Legacy "flagsmask" field -- now ignored, remove when we bump version
reed@google.com48f31bd2014-02-27 14:27:44 +0000224 (void)buffer.readInt();
reed@google.com842292f2014-02-28 15:38:22 +0000225
reed@google.com0716c632011-04-12 18:32:06 +0000226 info.fPaintBits = buffer.readInt();
Mike Reedfaba3712016-11-03 14:45:31 -0400227 info.fColorMode = (SkBlendMode)buffer.readInt();
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000228 buffer.readPoint(&info.fOffset);
reed@google.com0716c632011-04-12 18:32:06 +0000229 info.fPostTranslate = buffer.readBool();
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000230 buffer.readPaint(builder.addLayerOnTop(info));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231 }
reed60c9b582016-04-03 09:11:13 -0700232 return builder.detach();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233}
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000234
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000235#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000236void SkLayerDrawLooper::toString(SkString* str) const {
237 str->appendf("SkLayerDrawLooper (%d): ", fCount);
238
239 Rec* rec = fRecs;
240 for (int i = 0; i < fCount; i++) {
reed@google.com842292f2014-02-28 15:38:22 +0000241 str->appendf("%d: paintBits: (", i);
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000242 if (0 == rec->fInfo.fPaintBits) {
243 str->append("None");
244 } else if (kEntirePaint_Bits == rec->fInfo.fPaintBits) {
245 str->append("EntirePaint");
246 } else {
247 bool needSeparator = false;
248 SkAddFlagToString(str, SkToBool(kStyle_Bit & rec->fInfo.fPaintBits), "Style",
249 &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000250 SkAddFlagToString(str, SkToBool(kTextSkewX_Bit & rec->fInfo.fPaintBits), "TextSkewX",
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000251 &needSeparator);
252 SkAddFlagToString(str, SkToBool(kPathEffect_Bit & rec->fInfo.fPaintBits), "PathEffect",
253 &needSeparator);
254 SkAddFlagToString(str, SkToBool(kMaskFilter_Bit & rec->fInfo.fPaintBits), "MaskFilter",
255 &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000256 SkAddFlagToString(str, SkToBool(kShader_Bit & rec->fInfo.fPaintBits), "Shader",
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000257 &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000258 SkAddFlagToString(str, SkToBool(kColorFilter_Bit & rec->fInfo.fPaintBits), "ColorFilter",
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000259 &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000260 SkAddFlagToString(str, SkToBool(kXfermode_Bit & rec->fInfo.fPaintBits), "Xfermode",
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000261 &needSeparator);
262 }
263 str->append(") ");
264
Mike Reed7d954ad2016-10-28 15:42:34 -0400265 static const char* gModeStrings[(int)SkBlendMode::kLastMode+1] = {
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000266 "kClear", "kSrc", "kDst", "kSrcOver", "kDstOver", "kSrcIn", "kDstIn",
267 "kSrcOut", "kDstOut", "kSrcATop", "kDstATop", "kXor", "kPlus",
268 "kMultiply", "kScreen", "kOverlay", "kDarken", "kLighten", "kColorDodge",
269 "kColorBurn", "kHardLight", "kSoftLight", "kDifference", "kExclusion"
270 };
271
Mike Reed7d954ad2016-10-28 15:42:34 -0400272 str->appendf("mode: %s ", gModeStrings[(int)rec->fInfo.fColorMode]);
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000273
274 str->append("offset: (");
275 str->appendScalar(rec->fInfo.fOffset.fX);
276 str->append(", ");
277 str->appendScalar(rec->fInfo.fOffset.fY);
278 str->append(") ");
279
280 str->append("postTranslate: ");
281 if (rec->fInfo.fPostTranslate) {
282 str->append("true ");
283 } else {
284 str->append("false ");
285 }
286
robertphillips@google.com791f12e2013-02-14 13:53:53 +0000287 rec->fPaint.toString(str);
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000288 rec = rec->fNext;
289 }
290}
291#endif
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000292
293SkLayerDrawLooper::Builder::Builder()
halcanary96fcdcc2015-08-27 07:41:13 -0700294 : fRecs(nullptr),
295 fTopRec(nullptr),
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000296 fCount(0) {
297}
298
299SkLayerDrawLooper::Builder::~Builder() {
300 Rec* rec = fRecs;
301 while (rec) {
302 Rec* next = rec->fNext;
halcanary385fe4d2015-08-26 13:07:48 -0700303 delete rec;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000304 rec = next;
305 }
306}
307
308SkPaint* SkLayerDrawLooper::Builder::addLayer(const LayerInfo& info) {
309 fCount += 1;
310
halcanary385fe4d2015-08-26 13:07:48 -0700311 Rec* rec = new Rec;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000312 rec->fNext = fRecs;
313 rec->fInfo = info;
314 fRecs = rec;
halcanary96fcdcc2015-08-27 07:41:13 -0700315 if (nullptr == fTopRec) {
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000316 fTopRec = rec;
317 }
318
319 return &rec->fPaint;
320}
321
322void SkLayerDrawLooper::Builder::addLayer(SkScalar dx, SkScalar dy) {
323 LayerInfo info;
324
325 info.fOffset.set(dx, dy);
326 (void)this->addLayer(info);
327}
328
329SkPaint* SkLayerDrawLooper::Builder::addLayerOnTop(const LayerInfo& info) {
330 fCount += 1;
331
halcanary385fe4d2015-08-26 13:07:48 -0700332 Rec* rec = new Rec;
halcanary96fcdcc2015-08-27 07:41:13 -0700333 rec->fNext = nullptr;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000334 rec->fInfo = info;
halcanary96fcdcc2015-08-27 07:41:13 -0700335 if (nullptr == fRecs) {
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000336 fRecs = rec;
337 } else {
bsalomon49f085d2014-09-05 13:34:00 -0700338 SkASSERT(fTopRec);
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000339 fTopRec->fNext = rec;
340 }
341 fTopRec = rec;
342
343 return &rec->fPaint;
344}
345
reed7b380d02016-03-21 13:25:16 -0700346sk_sp<SkDrawLooper> SkLayerDrawLooper::Builder::detach() {
halcanary385fe4d2015-08-26 13:07:48 -0700347 SkLayerDrawLooper* looper = new SkLayerDrawLooper;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000348 looper->fCount = fCount;
349 looper->fRecs = fRecs;
350
351 fCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700352 fRecs = nullptr;
353 fTopRec = nullptr;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000354
reed7b380d02016-03-21 13:25:16 -0700355 return sk_sp<SkDrawLooper>(looper);
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000356}