blob: 49263fbbb5d746c7ccca6b204998c3643e4d4b18 [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 fFlagsMask = 0; // ignore our paint flags
19 fPaintBits = 0; // ignore our paint fields
reed@google.com0716c632011-04-12 18:32:06 +000020 fColorMode = SkXfermode::kDst_Mode; // ignore our color
21 fOffset.set(0, 0);
22 fPostTranslate = false;
23}
reed@android.com8a1c16f2008-12-17 15:59:43 +000024
vandebo@chromium.org3c898182011-06-22 05:01:28 +000025SkLayerDrawLooper::SkLayerDrawLooper()
26 : fRecs(NULL),
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000027 fTopRec(NULL),
vandebo@chromium.org3c898182011-06-22 05:01:28 +000028 fCount(0),
29 fCurrRec(NULL) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000030}
31
32SkLayerDrawLooper::~SkLayerDrawLooper() {
33 Rec* rec = fRecs;
34 while (rec) {
35 Rec* next = rec->fNext;
36 SkDELETE(rec);
37 rec = next;
38 }
39}
reed@google.com0716c632011-04-12 18:32:06 +000040
41SkPaint* SkLayerDrawLooper::addLayer(const LayerInfo& info) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 fCount += 1;
43
44 Rec* rec = SkNEW(Rec);
45 rec->fNext = fRecs;
reed@google.com0716c632011-04-12 18:32:06 +000046 rec->fInfo = info;
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 fRecs = rec;
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000048 if (NULL == fTopRec) {
49 fTopRec = rec;
50 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000051
52 return &rec->fPaint;
53}
54
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000055void SkLayerDrawLooper::addLayer(SkScalar dx, SkScalar dy) {
reed@google.com0716c632011-04-12 18:32:06 +000056 LayerInfo info;
57
58 info.fOffset.set(dx, dy);
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000059 (void)this->addLayer(info);
reed@google.com0716c632011-04-12 18:32:06 +000060}
61
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000062SkPaint* SkLayerDrawLooper::addLayerOnTop(const LayerInfo& info) {
63 fCount += 1;
64
65 Rec* rec = SkNEW(Rec);
66 rec->fNext = NULL;
67 rec->fInfo = info;
68 if (NULL == fRecs) {
69 fRecs = rec;
70 } else {
71 SkASSERT(NULL != fTopRec);
72 fTopRec->fNext = rec;
73 }
74 fTopRec = rec;
75
76 return &rec->fPaint;
77}
78
reed@google.com4e2b3d32011-04-07 14:18:59 +000079void SkLayerDrawLooper::init(SkCanvas* canvas) {
80 fCurrRec = fRecs;
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 canvas->save(SkCanvas::kMatrix_SaveFlag);
82}
83
reed@google.com0716c632011-04-12 18:32:06 +000084static SkColor xferColor(SkColor src, SkColor dst, SkXfermode::Mode mode) {
85 switch (mode) {
86 case SkXfermode::kSrc_Mode:
87 return src;
88 case SkXfermode::kDst_Mode:
89 return dst;
90 default: {
91 SkPMColor pmS = SkPreMultiplyColor(src);
92 SkPMColor pmD = SkPreMultiplyColor(dst);
93 SkPMColor result = SkXfermode::GetProc(mode)(pmS, pmD);
94 return SkUnPreMultiply::PMColorToColor(result);
95 }
96 }
97}
98
reed@google.com84260582011-11-21 16:42:10 +000099// Even with kEntirePaint_Bits, we always ensure that the master paint's
100// text-encoding is respected, since that controls how we interpret the
101// text/length parameters of a draw[Pos]Text call.
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +0000102void SkLayerDrawLooper::ApplyInfo(SkPaint* dst, const SkPaint& src,
103 const LayerInfo& info) {
104
105 uint32_t mask = info.fFlagsMask;
106 dst->setFlags((dst->getFlags() & ~mask) | (src.getFlags() & mask));
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +0000107 dst->setColor(xferColor(src.getColor(), dst->getColor(), info.fColorMode));
108
109 BitFlags bits = info.fPaintBits;
reed@google.com84260582011-11-21 16:42:10 +0000110 SkPaint::TextEncoding encoding = dst->getTextEncoding();
reed@google.com0716c632011-04-12 18:32:06 +0000111
mike@reedtribe.org3c1225b2011-04-09 18:54:08 +0000112 if (0 == bits) {
113 return;
114 }
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000115 if (kEntirePaint_Bits == bits) {
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +0000116 // we've already computed these, so save it from the assignment
117 uint32_t f = dst->getFlags();
reed@google.com0716c632011-04-12 18:32:06 +0000118 SkColor c = dst->getColor();
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000119 *dst = src;
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +0000120 dst->setFlags(f);
reed@google.com0716c632011-04-12 18:32:06 +0000121 dst->setColor(c);
reed@google.com84260582011-11-21 16:42:10 +0000122 dst->setTextEncoding(encoding);
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000123 return;
124 }
125
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000126 if (bits & kStyle_Bit) {
127 dst->setStyle(src.getStyle());
128 dst->setStrokeWidth(src.getStrokeWidth());
129 dst->setStrokeMiter(src.getStrokeMiter());
130 dst->setStrokeCap(src.getStrokeCap());
131 dst->setStrokeJoin(src.getStrokeJoin());
132 }
133
134 if (bits & kTextSkewX_Bit) {
135 dst->setTextSkewX(src.getTextSkewX());
136 }
137
138 if (bits & kPathEffect_Bit) {
139 dst->setPathEffect(src.getPathEffect());
140 }
141 if (bits & kMaskFilter_Bit) {
142 dst->setMaskFilter(src.getMaskFilter());
143 }
144 if (bits & kShader_Bit) {
145 dst->setShader(src.getShader());
146 }
147 if (bits & kColorFilter_Bit) {
148 dst->setColorFilter(src.getColorFilter());
149 }
150 if (bits & kXfermode_Bit) {
151 dst->setXfermode(src.getXfermode());
152 }
153
reed@google.com84260582011-11-21 16:42:10 +0000154 // we don't override these
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000155#if 0
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000156 dst->setTypeface(src.getTypeface());
157 dst->setTextSize(src.getTextSize());
158 dst->setTextScaleX(src.getTextScaleX());
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000159 dst->setRasterizer(src.getRasterizer());
160 dst->setLooper(src.getLooper());
161 dst->setTextEncoding(src.getTextEncoding());
162 dst->setHinting(src.getHinting());
163#endif
164}
165
reed@google.com0716c632011-04-12 18:32:06 +0000166// Should we add this to canvas?
167static void postTranslate(SkCanvas* canvas, SkScalar dx, SkScalar dy) {
168 SkMatrix m = canvas->getTotalMatrix();
169 m.postTranslate(dx, dy);
170 canvas->setMatrix(m);
171}
172
reed@google.com4e2b3d32011-04-07 14:18:59 +0000173bool SkLayerDrawLooper::next(SkCanvas* canvas, SkPaint* paint) {
174 canvas->restore();
175 if (NULL == fCurrRec) {
176 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +0000179 ApplyInfo(paint, fCurrRec->fPaint, fCurrRec->fInfo);
reed@google.com0716c632011-04-12 18:32:06 +0000180
reed@google.com4e2b3d32011-04-07 14:18:59 +0000181 canvas->save(SkCanvas::kMatrix_SaveFlag);
reed@google.com0716c632011-04-12 18:32:06 +0000182 if (fCurrRec->fInfo.fPostTranslate) {
183 postTranslate(canvas, fCurrRec->fInfo.fOffset.fX,
184 fCurrRec->fInfo.fOffset.fY);
185 } else {
186 canvas->translate(fCurrRec->fInfo.fOffset.fX, fCurrRec->fInfo.fOffset.fY);
187 }
mike@reedtribe.orge5d0def2011-04-08 00:53:48 +0000188 fCurrRec = fCurrRec->fNext;
189
reed@google.com4e2b3d32011-04-07 14:18:59 +0000190 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191}
192
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193///////////////////////////////////////////////////////////////////////////////
194
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000195void SkLayerDrawLooper::flatten(SkWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 this->INHERITED::flatten(buffer);
197
198#ifdef SK_DEBUG
199 {
200 Rec* rec = fRecs;
201 int count = 0;
202 while (rec) {
203 rec = rec->fNext;
204 count += 1;
205 }
206 SkASSERT(count == fCount);
207 }
208#endif
209
210 buffer.writeInt(fCount);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000211
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212 Rec* rec = fRecs;
213 for (int i = 0; i < fCount; i++) {
reed@google.com076f4c92012-07-31 14:32:38 +0000214 buffer.writeInt(rec->fInfo.fFlagsMask);
reed@google.com0716c632011-04-12 18:32:06 +0000215 buffer.writeInt(rec->fInfo.fPaintBits);
216 buffer.writeInt(rec->fInfo.fColorMode);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000217 buffer.writePoint(rec->fInfo.fOffset);
reed@google.com0716c632011-04-12 18:32:06 +0000218 buffer.writeBool(rec->fInfo.fPostTranslate);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000219 buffer.writePaint(rec->fPaint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220 rec = rec->fNext;
221 }
222}
223
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000224SkFlattenable* SkLayerDrawLooper::CreateProc(SkReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225 int count = buffer.readInt();
226
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000227 Builder builder;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000228 for (int i = 0; i < count; i++) {
reed@google.com0716c632011-04-12 18:32:06 +0000229 LayerInfo info;
reed@google.com076f4c92012-07-31 14:32:38 +0000230 info.fFlagsMask = buffer.readInt();
reed@google.com0716c632011-04-12 18:32:06 +0000231 info.fPaintBits = buffer.readInt();
232 info.fColorMode = (SkXfermode::Mode)buffer.readInt();
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000233 buffer.readPoint(&info.fOffset);
reed@google.com0716c632011-04-12 18:32:06 +0000234 info.fPostTranslate = buffer.readBool();
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000235 buffer.readPaint(builder.addLayerOnTop(info));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000236 }
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000237 SkLayerDrawLooper* looper = builder.detachLooper();
238 SkASSERT(count == looper->fCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240#ifdef SK_DEBUG
241 {
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000242 Rec* rec = looper->fRecs;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243 int n = 0;
244 while (rec) {
245 rec = rec->fNext;
246 n += 1;
247 }
248 SkASSERT(count == n);
249 }
250#endif
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000251
252 return looper;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000253}
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000254
255#ifdef SK_DEVELOPER
256void SkLayerDrawLooper::toString(SkString* str) const {
257 str->appendf("SkLayerDrawLooper (%d): ", fCount);
258
259 Rec* rec = fRecs;
260 for (int i = 0; i < fCount; i++) {
261 str->appendf("%d: ", i);
262
263 str->append("flagsMask: (");
264 if (0 == rec->fInfo.fFlagsMask) {
265 str->append("None");
266 } else {
267 bool needSeparator = false;
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000268 SkAddFlagToString(str, SkToBool(SkPaint::kAntiAlias_Flag & rec->fInfo.fFlagsMask),
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000269 "AntiAlias", &needSeparator);
reed@google.com9cfc83c2013-07-22 17:18:18 +0000270// SkAddFlagToString(str, SkToBool(SkPaint::kFilterBitmap_Flag & rec->fInfo.fFlagsMask), "FilterBitmap", &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000271 SkAddFlagToString(str, SkToBool(SkPaint::kDither_Flag & rec->fInfo.fFlagsMask),
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000272 "Dither", &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000273 SkAddFlagToString(str, SkToBool(SkPaint::kUnderlineText_Flag & rec->fInfo.fFlagsMask),
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000274 "UnderlineText", &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000275 SkAddFlagToString(str, SkToBool(SkPaint::kStrikeThruText_Flag & rec->fInfo.fFlagsMask),
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000276 "StrikeThruText", &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000277 SkAddFlagToString(str, SkToBool(SkPaint::kFakeBoldText_Flag & rec->fInfo.fFlagsMask),
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000278 "FakeBoldText", &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000279 SkAddFlagToString(str, SkToBool(SkPaint::kLinearText_Flag & rec->fInfo.fFlagsMask),
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000280 "LinearText", &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000281 SkAddFlagToString(str, SkToBool(SkPaint::kSubpixelText_Flag & rec->fInfo.fFlagsMask),
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000282 "SubpixelText", &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000283 SkAddFlagToString(str, SkToBool(SkPaint::kDevKernText_Flag & rec->fInfo.fFlagsMask),
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000284 "DevKernText", &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000285 SkAddFlagToString(str, SkToBool(SkPaint::kLCDRenderText_Flag & rec->fInfo.fFlagsMask),
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000286 "LCDRenderText", &needSeparator);
287 SkAddFlagToString(str, SkToBool(SkPaint::kEmbeddedBitmapText_Flag & rec->fInfo.fFlagsMask),
288 "EmbeddedBitmapText", &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000289 SkAddFlagToString(str, SkToBool(SkPaint::kAutoHinting_Flag & rec->fInfo.fFlagsMask),
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000290 "Autohinted", &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000291 SkAddFlagToString(str, SkToBool(SkPaint::kVerticalText_Flag & rec->fInfo.fFlagsMask),
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000292 "VerticalText", &needSeparator);
293 SkAddFlagToString(str, SkToBool(SkPaint::kGenA8FromLCD_Flag & rec->fInfo.fFlagsMask),
294 "GenA8FromLCD", &needSeparator);
295 }
296 str->append(") ");
297
298 str->append("paintBits: (");
299 if (0 == rec->fInfo.fPaintBits) {
300 str->append("None");
301 } else if (kEntirePaint_Bits == rec->fInfo.fPaintBits) {
302 str->append("EntirePaint");
303 } else {
304 bool needSeparator = false;
305 SkAddFlagToString(str, SkToBool(kStyle_Bit & rec->fInfo.fPaintBits), "Style",
306 &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000307 SkAddFlagToString(str, SkToBool(kTextSkewX_Bit & rec->fInfo.fPaintBits), "TextSkewX",
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000308 &needSeparator);
309 SkAddFlagToString(str, SkToBool(kPathEffect_Bit & rec->fInfo.fPaintBits), "PathEffect",
310 &needSeparator);
311 SkAddFlagToString(str, SkToBool(kMaskFilter_Bit & rec->fInfo.fPaintBits), "MaskFilter",
312 &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000313 SkAddFlagToString(str, SkToBool(kShader_Bit & rec->fInfo.fPaintBits), "Shader",
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000314 &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000315 SkAddFlagToString(str, SkToBool(kColorFilter_Bit & rec->fInfo.fPaintBits), "ColorFilter",
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000316 &needSeparator);
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000317 SkAddFlagToString(str, SkToBool(kXfermode_Bit & rec->fInfo.fPaintBits), "Xfermode",
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000318 &needSeparator);
319 }
320 str->append(") ");
321
322 static const char* gModeStrings[SkXfermode::kLastMode+1] = {
323 "kClear", "kSrc", "kDst", "kSrcOver", "kDstOver", "kSrcIn", "kDstIn",
324 "kSrcOut", "kDstOut", "kSrcATop", "kDstATop", "kXor", "kPlus",
325 "kMultiply", "kScreen", "kOverlay", "kDarken", "kLighten", "kColorDodge",
326 "kColorBurn", "kHardLight", "kSoftLight", "kDifference", "kExclusion"
327 };
328
329 str->appendf("mode: %s ", gModeStrings[rec->fInfo.fColorMode]);
330
331 str->append("offset: (");
332 str->appendScalar(rec->fInfo.fOffset.fX);
333 str->append(", ");
334 str->appendScalar(rec->fInfo.fOffset.fY);
335 str->append(") ");
336
337 str->append("postTranslate: ");
338 if (rec->fInfo.fPostTranslate) {
339 str->append("true ");
340 } else {
341 str->append("false ");
342 }
343
robertphillips@google.com791f12e2013-02-14 13:53:53 +0000344 rec->fPaint.toString(str);
robertphillips@google.com4991b8f2013-01-28 20:21:59 +0000345 rec = rec->fNext;
346 }
347}
348#endif
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000349
350SkLayerDrawLooper::Builder::Builder()
351 : fRecs(NULL),
352 fTopRec(NULL),
353 fCount(0) {
354}
355
356SkLayerDrawLooper::Builder::~Builder() {
357 Rec* rec = fRecs;
358 while (rec) {
359 Rec* next = rec->fNext;
360 SkDELETE(rec);
361 rec = next;
362 }
363}
364
365SkPaint* SkLayerDrawLooper::Builder::addLayer(const LayerInfo& info) {
366 fCount += 1;
367
368 Rec* rec = SkNEW(Rec);
369 rec->fNext = fRecs;
370 rec->fInfo = info;
371 fRecs = rec;
372 if (NULL == fTopRec) {
373 fTopRec = rec;
374 }
375
376 return &rec->fPaint;
377}
378
379void SkLayerDrawLooper::Builder::addLayer(SkScalar dx, SkScalar dy) {
380 LayerInfo info;
381
382 info.fOffset.set(dx, dy);
383 (void)this->addLayer(info);
384}
385
386SkPaint* SkLayerDrawLooper::Builder::addLayerOnTop(const LayerInfo& info) {
387 fCount += 1;
388
389 Rec* rec = SkNEW(Rec);
390 rec->fNext = NULL;
391 rec->fInfo = info;
392 if (NULL == fRecs) {
393 fRecs = rec;
394 } else {
395 SkASSERT(NULL != fTopRec);
396 fTopRec->fNext = rec;
397 }
398 fTopRec = rec;
399
400 return &rec->fPaint;
401}
402
403SkLayerDrawLooper* SkLayerDrawLooper::Builder::detachLooper() {
404 SkLayerDrawLooper* looper = SkNEW(SkLayerDrawLooper);
405 looper->fCount = fCount;
406 looper->fRecs = fRecs;
407
408 fCount = 0;
409 fRecs = NULL;
410 fTopRec = NULL;
411
412 return looper;
413}