blob: 30709c4f1c341647cd480e0a196ef2292dea3a9c [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()
25 : fRecs(NULL),
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000026 fTopRec(NULL),
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000027 fCount(0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000028}
29
30SkLayerDrawLooper::~SkLayerDrawLooper() {
31 Rec* rec = fRecs;
32 while (rec) {
33 Rec* next = rec->fNext;
34 SkDELETE(rec);
35 rec = next;
36 }
37}
reed@google.com0716c632011-04-12 18:32:06 +000038
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000039SkLayerDrawLooper::Context* SkLayerDrawLooper::createContext(SkCanvas* canvas, void* storage) const {
commit-bot@chromium.org091a5942014-04-18 14:19:31 +000040 canvas->save();
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000041 return SkNEW_PLACEMENT_ARGS(storage, LayerDrawLooperContext, (this));
reed@android.com8a1c16f2008-12-17 15:59:43 +000042}
43
reed@google.com0716c632011-04-12 18:32:06 +000044static SkColor xferColor(SkColor src, SkColor dst, SkXfermode::Mode mode) {
45 switch (mode) {
46 case SkXfermode::kSrc_Mode:
47 return src;
48 case SkXfermode::kDst_Mode:
49 return dst;
50 default: {
51 SkPMColor pmS = SkPreMultiplyColor(src);
52 SkPMColor pmD = SkPreMultiplyColor(dst);
53 SkPMColor result = SkXfermode::GetProc(mode)(pmS, pmD);
54 return SkUnPreMultiply::PMColorToColor(result);
55 }
56 }
57}
58
reed@google.com84260582011-11-21 16:42:10 +000059// Even with kEntirePaint_Bits, we always ensure that the master paint's
60// text-encoding is respected, since that controls how we interpret the
61// text/length parameters of a draw[Pos]Text call.
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000062void SkLayerDrawLooper::LayerDrawLooperContext::ApplyInfo(
63 SkPaint* dst, const SkPaint& src, const LayerInfo& info) {
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000064
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000065 dst->setColor(xferColor(src.getColor(), dst->getColor(), info.fColorMode));
66
67 BitFlags bits = info.fPaintBits;
reed@google.com84260582011-11-21 16:42:10 +000068 SkPaint::TextEncoding encoding = dst->getTextEncoding();
reed@google.com0716c632011-04-12 18:32:06 +000069
mike@reedtribe.org3c1225b2011-04-09 18:54:08 +000070 if (0 == bits) {
71 return;
72 }
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000073 if (kEntirePaint_Bits == bits) {
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000074 // we've already computed these, so save it from the assignment
75 uint32_t f = dst->getFlags();
reed@google.com0716c632011-04-12 18:32:06 +000076 SkColor c = dst->getColor();
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000077 *dst = src;
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000078 dst->setFlags(f);
reed@google.com0716c632011-04-12 18:32:06 +000079 dst->setColor(c);
reed@google.com84260582011-11-21 16:42:10 +000080 dst->setTextEncoding(encoding);
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000081 return;
82 }
83
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000084 if (bits & kStyle_Bit) {
85 dst->setStyle(src.getStyle());
86 dst->setStrokeWidth(src.getStrokeWidth());
87 dst->setStrokeMiter(src.getStrokeMiter());
88 dst->setStrokeCap(src.getStrokeCap());
89 dst->setStrokeJoin(src.getStrokeJoin());
90 }
91
92 if (bits & kTextSkewX_Bit) {
93 dst->setTextSkewX(src.getTextSkewX());
94 }
95
96 if (bits & kPathEffect_Bit) {
97 dst->setPathEffect(src.getPathEffect());
98 }
99 if (bits & kMaskFilter_Bit) {
100 dst->setMaskFilter(src.getMaskFilter());
101 }
102 if (bits & kShader_Bit) {
103 dst->setShader(src.getShader());
104 }
105 if (bits & kColorFilter_Bit) {
106 dst->setColorFilter(src.getColorFilter());
107 }
108 if (bits & kXfermode_Bit) {
109 dst->setXfermode(src.getXfermode());
110 }
111
reed@google.com84260582011-11-21 16:42:10 +0000112 // we don't override these
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000113#if 0
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000114 dst->setTypeface(src.getTypeface());
115 dst->setTextSize(src.getTextSize());
116 dst->setTextScaleX(src.getTextScaleX());
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000117 dst->setRasterizer(src.getRasterizer());
118 dst->setLooper(src.getLooper());
119 dst->setTextEncoding(src.getTextEncoding());
120 dst->setHinting(src.getHinting());
121#endif
122}
123
reed@google.com0716c632011-04-12 18:32:06 +0000124// Should we add this to canvas?
125static void postTranslate(SkCanvas* canvas, SkScalar dx, SkScalar dy) {
126 SkMatrix m = canvas->getTotalMatrix();
127 m.postTranslate(dx, dy);
128 canvas->setMatrix(m);
129}
130
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000131SkLayerDrawLooper::LayerDrawLooperContext::LayerDrawLooperContext(
132 const SkLayerDrawLooper* looper) : fCurrRec(looper->fRecs) {}
133
134bool SkLayerDrawLooper::LayerDrawLooperContext::next(SkCanvas* canvas,
135 SkPaint* paint) {
reed@google.com4e2b3d32011-04-07 14:18:59 +0000136 canvas->restore();
137 if (NULL == fCurrRec) {
138 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +0000141 ApplyInfo(paint, fCurrRec->fPaint, fCurrRec->fInfo);
reed@google.com0716c632011-04-12 18:32:06 +0000142
commit-bot@chromium.org091a5942014-04-18 14:19:31 +0000143 canvas->save();
reed@google.com0716c632011-04-12 18:32:06 +0000144 if (fCurrRec->fInfo.fPostTranslate) {
145 postTranslate(canvas, fCurrRec->fInfo.fOffset.fX,
146 fCurrRec->fInfo.fOffset.fY);
147 } else {
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000148 canvas->translate(fCurrRec->fInfo.fOffset.fX,
149 fCurrRec->fInfo.fOffset.fY);
reed@google.com0716c632011-04-12 18:32:06 +0000150 }
mike@reedtribe.orge5d0def2011-04-08 00:53:48 +0000151 fCurrRec = fCurrRec->fNext;
152
reed@google.com4e2b3d32011-04-07 14:18:59 +0000153 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154}
155
reed@google.comdaaafa62014-04-29 15:20:16 +0000156bool SkLayerDrawLooper::asABlurShadow(BlurShadowRec* bsRec) const {
157 if (fCount != 2) {
158 return false;
159 }
160 const Rec* rec = fRecs;
161
162 // bottom layer needs to be just blur(maskfilter)
163 if ((rec->fInfo.fPaintBits & ~kMaskFilter_Bit)) {
164 return false;
165 }
166 if (SkXfermode::kSrc_Mode != rec->fInfo.fColorMode) {
167 return false;
168 }
169 const SkMaskFilter* mf = rec->fPaint.getMaskFilter();
170 if (NULL == mf) {
171 return false;
172 }
173 SkMaskFilter::BlurRec maskBlur;
174 if (!mf->asABlur(&maskBlur)) {
175 return false;
176 }
177
178 rec = rec->fNext;
179 // top layer needs to be "plain"
180 if (rec->fInfo.fPaintBits) {
181 return false;
182 }
183 if (SkXfermode::kDst_Mode != rec->fInfo.fColorMode) {
184 return false;
185 }
186 if (!rec->fInfo.fOffset.equals(0, 0)) {
187 return false;
188 }
189
190 if (bsRec) {
191 bsRec->fSigma = maskBlur.fSigma;
192 bsRec->fOffset = fRecs->fInfo.fOffset;
193 bsRec->fColor = fRecs->fPaint.getColor();
194 bsRec->fStyle = maskBlur.fStyle;
195 bsRec->fQuality = maskBlur.fQuality;
196 }
197 return true;
198}
199
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200///////////////////////////////////////////////////////////////////////////////
201
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000202void SkLayerDrawLooper::flatten(SkWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203 buffer.writeInt(fCount);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000204
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 Rec* rec = fRecs;
206 for (int i = 0; i < fCount; i++) {
reed@google.com842292f2014-02-28 15:38:22 +0000207 // Legacy "flagsmask" field -- now ignored, remove when we bump version
208 buffer.writeInt(0);
209
reed@google.com0716c632011-04-12 18:32:06 +0000210 buffer.writeInt(rec->fInfo.fPaintBits);
211 buffer.writeInt(rec->fInfo.fColorMode);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000212 buffer.writePoint(rec->fInfo.fOffset);
reed@google.com0716c632011-04-12 18:32:06 +0000213 buffer.writeBool(rec->fInfo.fPostTranslate);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000214 buffer.writePaint(rec->fPaint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215 rec = rec->fNext;
216 }
217}
218
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000219SkFlattenable* SkLayerDrawLooper::CreateProc(SkReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220 int count = buffer.readInt();
221
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000222 Builder builder;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000223 for (int i = 0; i < count; i++) {
reed@google.com0716c632011-04-12 18:32:06 +0000224 LayerInfo info;
reed@google.com842292f2014-02-28 15:38:22 +0000225 // Legacy "flagsmask" field -- now ignored, remove when we bump version
reed@google.com48f31bd2014-02-27 14:27:44 +0000226 (void)buffer.readInt();
reed@google.com842292f2014-02-28 15:38:22 +0000227
reed@google.com0716c632011-04-12 18:32:06 +0000228 info.fPaintBits = buffer.readInt();
229 info.fColorMode = (SkXfermode::Mode)buffer.readInt();
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000230 buffer.readPoint(&info.fOffset);
reed@google.com0716c632011-04-12 18:32:06 +0000231 info.fPostTranslate = buffer.readBool();
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000232 buffer.readPaint(builder.addLayerOnTop(info));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233 }
reed9fa60da2014-08-21 07:59:51 -0700234 return builder.detachLooper();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235}
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000236
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000237#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000238void SkLayerDrawLooper::toString(SkString* str) const {
239 str->appendf("SkLayerDrawLooper (%d): ", fCount);
240
241 Rec* rec = fRecs;
242 for (int i = 0; i < fCount; i++) {
reed@google.com842292f2014-02-28 15:38:22 +0000243 str->appendf("%d: paintBits: (", i);
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000244 if (0 == rec->fInfo.fPaintBits) {
245 str->append("None");
246 } else if (kEntirePaint_Bits == rec->fInfo.fPaintBits) {
247 str->append("EntirePaint");
248 } else {
249 bool needSeparator = false;
250 SkAddFlagToString(str, SkToBool(kStyle_Bit & rec->fInfo.fPaintBits), "Style",
251 &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000252 SkAddFlagToString(str, SkToBool(kTextSkewX_Bit & rec->fInfo.fPaintBits), "TextSkewX",
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000253 &needSeparator);
254 SkAddFlagToString(str, SkToBool(kPathEffect_Bit & rec->fInfo.fPaintBits), "PathEffect",
255 &needSeparator);
256 SkAddFlagToString(str, SkToBool(kMaskFilter_Bit & rec->fInfo.fPaintBits), "MaskFilter",
257 &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000258 SkAddFlagToString(str, SkToBool(kShader_Bit & rec->fInfo.fPaintBits), "Shader",
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000259 &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000260 SkAddFlagToString(str, SkToBool(kColorFilter_Bit & rec->fInfo.fPaintBits), "ColorFilter",
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000261 &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000262 SkAddFlagToString(str, SkToBool(kXfermode_Bit & rec->fInfo.fPaintBits), "Xfermode",
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000263 &needSeparator);
264 }
265 str->append(") ");
266
267 static const char* gModeStrings[SkXfermode::kLastMode+1] = {
268 "kClear", "kSrc", "kDst", "kSrcOver", "kDstOver", "kSrcIn", "kDstIn",
269 "kSrcOut", "kDstOut", "kSrcATop", "kDstATop", "kXor", "kPlus",
270 "kMultiply", "kScreen", "kOverlay", "kDarken", "kLighten", "kColorDodge",
271 "kColorBurn", "kHardLight", "kSoftLight", "kDifference", "kExclusion"
272 };
273
274 str->appendf("mode: %s ", gModeStrings[rec->fInfo.fColorMode]);
275
276 str->append("offset: (");
277 str->appendScalar(rec->fInfo.fOffset.fX);
278 str->append(", ");
279 str->appendScalar(rec->fInfo.fOffset.fY);
280 str->append(") ");
281
282 str->append("postTranslate: ");
283 if (rec->fInfo.fPostTranslate) {
284 str->append("true ");
285 } else {
286 str->append("false ");
287 }
288
robertphillips@google.com791f12e2013-02-14 13:53:53 +0000289 rec->fPaint.toString(str);
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000290 rec = rec->fNext;
291 }
292}
293#endif
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000294
295SkLayerDrawLooper::Builder::Builder()
296 : fRecs(NULL),
297 fTopRec(NULL),
298 fCount(0) {
299}
300
301SkLayerDrawLooper::Builder::~Builder() {
302 Rec* rec = fRecs;
303 while (rec) {
304 Rec* next = rec->fNext;
305 SkDELETE(rec);
306 rec = next;
307 }
308}
309
310SkPaint* SkLayerDrawLooper::Builder::addLayer(const LayerInfo& info) {
311 fCount += 1;
312
313 Rec* rec = SkNEW(Rec);
314 rec->fNext = fRecs;
315 rec->fInfo = info;
316 fRecs = rec;
317 if (NULL == fTopRec) {
318 fTopRec = rec;
319 }
320
321 return &rec->fPaint;
322}
323
324void SkLayerDrawLooper::Builder::addLayer(SkScalar dx, SkScalar dy) {
325 LayerInfo info;
326
327 info.fOffset.set(dx, dy);
328 (void)this->addLayer(info);
329}
330
331SkPaint* SkLayerDrawLooper::Builder::addLayerOnTop(const LayerInfo& info) {
332 fCount += 1;
333
334 Rec* rec = SkNEW(Rec);
335 rec->fNext = NULL;
336 rec->fInfo = info;
337 if (NULL == fRecs) {
338 fRecs = rec;
339 } else {
bsalomon49f085d2014-09-05 13:34:00 -0700340 SkASSERT(fTopRec);
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000341 fTopRec->fNext = rec;
342 }
343 fTopRec = rec;
344
345 return &rec->fPaint;
346}
347
348SkLayerDrawLooper* SkLayerDrawLooper::Builder::detachLooper() {
349 SkLayerDrawLooper* looper = SkNEW(SkLayerDrawLooper);
350 looper->fCount = fCount;
351 looper->fRecs = fRecs;
352
353 fCount = 0;
354 fRecs = NULL;
355 fTopRec = NULL;
356
357 return looper;
358}