blob: f2d8ba8f6629852387dbb882f5fe53ceed1e7740 (
plain)
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
|
#include "SkCanvas.h"
#include "SkLayerDrawLooper.h"
#include "SkPaint.h"
SkLayerDrawLooper::SkLayerDrawLooper() {
fRecs = NULL;
fCount = 0;
}
SkLayerDrawLooper::~SkLayerDrawLooper() {
Rec* rec = fRecs;
while (rec) {
Rec* next = rec->fNext;
SkDELETE(rec);
rec = next;
}
}
SkPaint* SkLayerDrawLooper::addLayer(SkScalar dx, SkScalar dy) {
fCount += 1;
Rec* rec = SkNEW(Rec);
rec->fNext = fRecs;
rec->fOffset.set(dx, dy);
fRecs = rec;
return &rec->fPaint;
}
void SkLayerDrawLooper::init(SkCanvas* canvas, SkPaint* paint) {
fIter.fSavedPaint = *paint;
fIter.fPaint = paint;
fIter.fCanvas = canvas;
fIter.fRec = fRecs;
canvas->save(SkCanvas::kMatrix_SaveFlag);
}
bool SkLayerDrawLooper::next() {
Rec* rec = fIter.fRec;
if (rec) {
*fIter.fPaint = rec->fPaint;
fIter.fCanvas->restore();
fIter.fCanvas->save(SkCanvas::kMatrix_SaveFlag);
fIter.fCanvas->translate(rec->fOffset.fX, rec->fOffset.fY);
fIter.fRec = rec->fNext;
return true;
}
return false;
}
void SkLayerDrawLooper::restore() {
fIter.fCanvas->restore();
*fIter.fPaint = fIter.fSavedPaint;
}
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) {
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.writeScalar(rec->fOffset.fX);
buffer.writeScalar(rec->fOffset.fY);
rec->fPaint.flatten(buffer);
rec = rec->fNext;
}
}
SkLayerDrawLooper::SkLayerDrawLooper(SkFlattenableReadBuffer& buffer)
: INHERITED(buffer) {
fRecs = NULL;
fCount = 0;
int count = buffer.readInt();
for (int i = 0; i < count; i++) {
SkScalar dx = buffer.readScalar();
SkScalar dy = buffer.readScalar();
this->addLayer(dx, dy)->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
}
///////////////////////////////////////////////////////////////////////////////
static SkFlattenable::Registrar gReg("SkLayerDrawLooper",
SkLayerDrawLooper::CreateProc);
|