1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkCanvas.h"
#include "SkColor.h"
#include "SkLayerDrawLooper.h"
#include "SkPaint.h"
#include "SkUnPreMultiply.h"
SkLayerDrawLooper::LayerInfo::LayerInfo() {
fFlagsMask = 0; // ignore our paint flags
fPaintBits = 0; // ignore our paint fields
fColorMode = SkXfermode::kDst_Mode; // ignore our color
fOffset.set(0, 0);
fPostTranslate = false;
}
SkLayerDrawLooper::SkLayerDrawLooper()
: fRecs(NULL),
fCount(0),
fCurrRec(NULL) {
}
SkLayerDrawLooper::~SkLayerDrawLooper() {
Rec* rec = fRecs;
while (rec) {
Rec* next = rec->fNext;
SkDELETE(rec);
rec = next;
}
}
SkPaint* SkLayerDrawLooper::addLayer(const LayerInfo& info) {
fCount += 1;
Rec* rec = SkNEW(Rec);
rec->fNext = fRecs;
rec->fInfo = info;
fRecs = rec;
return &rec->fPaint;
}
void SkLayerDrawLooper::addLayer(SkScalar dx, SkScalar dy) {
LayerInfo info;
info.fOffset.set(dx, dy);
(void)this->addLayer(info);
}
void SkLayerDrawLooper::init(SkCanvas* canvas) {
fCurrRec = fRecs;
canvas->save(SkCanvas::kMatrix_SaveFlag);
}
static SkColor xferColor(SkColor src, SkColor dst, SkXfermode::Mode mode) {
switch (mode) {
case SkXfermode::kSrc_Mode:
return src;
case SkXfermode::kDst_Mode:
return dst;
default: {
SkPMColor pmS = SkPreMultiplyColor(src);
SkPMColor pmD = SkPreMultiplyColor(dst);
SkPMColor result = SkXfermode::GetProc(mode)(pmS, pmD);
return SkUnPreMultiply::PMColorToColor(result);
}
}
}
// Even with kEntirePaint_Bits, we always ensure that the master paint's
// text-encoding is respected, since that controls how we interpret the
// text/length parameters of a draw[Pos]Text call.
void SkLayerDrawLooper::ApplyInfo(SkPaint* dst, const SkPaint& src,
const LayerInfo& info) {
uint32_t mask = info.fFlagsMask;
dst->setFlags((dst->getFlags() & ~mask) | (src.getFlags() & mask));
dst->setColor(xferColor(src.getColor(), dst->getColor(), info.fColorMode));
BitFlags bits = info.fPaintBits;
SkPaint::TextEncoding encoding = dst->getTextEncoding();
if (0 == bits) {
return;
}
if (kEntirePaint_Bits == bits) {
// we've already computed these, so save it from the assignment
uint32_t f = dst->getFlags();
SkColor c = dst->getColor();
*dst = src;
dst->setFlags(f);
dst->setColor(c);
dst->setTextEncoding(encoding);
return;
}
if (bits & kStyle_Bit) {
dst->setStyle(src.getStyle());
dst->setStrokeWidth(src.getStrokeWidth());
dst->setStrokeMiter(src.getStrokeMiter());
dst->setStrokeCap(src.getStrokeCap());
dst->setStrokeJoin(src.getStrokeJoin());
}
if (bits & kTextSkewX_Bit) {
dst->setTextSkewX(src.getTextSkewX());
}
if (bits & kPathEffect_Bit) {
dst->setPathEffect(src.getPathEffect());
}
if (bits & kMaskFilter_Bit) {
dst->setMaskFilter(src.getMaskFilter());
}
if (bits & kShader_Bit) {
dst->setShader(src.getShader());
}
if (bits & kColorFilter_Bit) {
dst->setColorFilter(src.getColorFilter());
}
if (bits & kXfermode_Bit) {
dst->setXfermode(src.getXfermode());
}
// we don't override these
#if 0
dst->setTypeface(src.getTypeface());
dst->setTextSize(src.getTextSize());
dst->setTextScaleX(src.getTextScaleX());
dst->setRasterizer(src.getRasterizer());
dst->setLooper(src.getLooper());
dst->setTextEncoding(src.getTextEncoding());
dst->setHinting(src.getHinting());
#endif
}
// Should we add this to canvas?
static void postTranslate(SkCanvas* canvas, SkScalar dx, SkScalar dy) {
SkMatrix m = canvas->getTotalMatrix();
m.postTranslate(dx, dy);
canvas->setMatrix(m);
}
bool SkLayerDrawLooper::next(SkCanvas* canvas, SkPaint* paint) {
canvas->restore();
if (NULL == fCurrRec) {
return false;
}
ApplyInfo(paint, fCurrRec->fPaint, fCurrRec->fInfo);
canvas->save(SkCanvas::kMatrix_SaveFlag);
if (fCurrRec->fInfo.fPostTranslate) {
postTranslate(canvas, fCurrRec->fInfo.fOffset.fX,
fCurrRec->fInfo.fOffset.fY);
} else {
canvas->translate(fCurrRec->fInfo.fOffset.fX, fCurrRec->fInfo.fOffset.fY);
}
fCurrRec = fCurrRec->fNext;
return true;
}
SkLayerDrawLooper::Rec* SkLayerDrawLooper::Rec::Reverse(Rec* head) {
Rec* rec = head;
Rec* prev = NULL;
while (rec) {
Rec* next = rec->fNext;
rec->fNext = prev;
prev = rec;
rec = next;
}
return prev;
}
///////////////////////////////////////////////////////////////////////////////
void SkLayerDrawLooper::flatten(SkFlattenableWriteBuffer& buffer) const {
this->INHERITED::flatten(buffer);
#ifdef SK_DEBUG
{
Rec* rec = fRecs;
int count = 0;
while (rec) {
rec = rec->fNext;
count += 1;
}
SkASSERT(count == fCount);
}
#endif
buffer.writeInt(fCount);
Rec* rec = fRecs;
for (int i = 0; i < fCount; i++) {
buffer.writeInt(rec->fInfo.fPaintBits);
buffer.writeInt(rec->fInfo.fColorMode);
buffer.writeScalar(rec->fInfo.fOffset.fX);
buffer.writeScalar(rec->fInfo.fOffset.fY);
buffer.writeBool(rec->fInfo.fPostTranslate);
rec->fPaint.flatten(buffer);
rec = rec->fNext;
}
}
SkLayerDrawLooper::SkLayerDrawLooper(SkFlattenableReadBuffer& buffer)
: INHERITED(buffer),
fRecs(NULL),
fCount(0),
fCurrRec(NULL) {
int count = buffer.readInt();
for (int i = 0; i < count; i++) {
LayerInfo info;
info.fPaintBits = buffer.readInt();
info.fColorMode = (SkXfermode::Mode)buffer.readInt();
info.fOffset.fX = buffer.readScalar();
info.fOffset.fY = buffer.readScalar();
info.fPostTranslate = buffer.readBool();
this->addLayer(info)->unflatten(buffer);
}
SkASSERT(count == fCount);
// we're in reverse order, so fix it now
fRecs = Rec::Reverse(fRecs);
#ifdef SK_DEBUG
{
Rec* rec = fRecs;
int n = 0;
while (rec) {
rec = rec->fNext;
n += 1;
}
SkASSERT(count == n);
}
#endif
}
///////////////////////////////////////////////////////////////////////////////
SK_DEFINE_FLATTENABLE_REGISTRAR(SkLayerDrawLooper)
|