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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkLatticeIter.h"
#include "SkRect.h"
/**
* Divs must be in increasing order with no duplicates.
*/
static bool valid_divs(const int* divs, int count, int start, int end) {
int prev = start - 1;
for (int i = 0; i < count; i++) {
if (prev >= divs[i] || divs[i] >= end) {
return false;
}
}
return true;
}
bool SkLatticeIter::Valid(int width, int height, const SkCanvas::Lattice& lattice) {
SkIRect totalBounds = SkIRect::MakeWH(width, height);
SkASSERT(lattice.fBounds);
const SkIRect latticeBounds = *lattice.fBounds;
if (!totalBounds.contains(latticeBounds)) {
return false;
}
bool zeroXDivs = lattice.fXCount <= 0 || (1 == lattice.fXCount &&
latticeBounds.fLeft == lattice.fXDivs[0]);
bool zeroYDivs = lattice.fYCount <= 0 || (1 == lattice.fYCount &&
latticeBounds.fTop == lattice.fYDivs[0]);
if (zeroXDivs && zeroYDivs) {
return false;
}
return valid_divs(lattice.fXDivs, lattice.fXCount, latticeBounds.fLeft, latticeBounds.fRight)
&& valid_divs(lattice.fYDivs, lattice.fYCount, latticeBounds.fTop, latticeBounds.fBottom);
}
/**
* Count the number of pixels that are in "scalable" patches.
*/
static int count_scalable_pixels(const int32_t* divs, int numDivs, bool firstIsScalable,
int start, int end) {
if (0 == numDivs) {
return firstIsScalable ? end - start : 0;
}
int i;
int count;
if (firstIsScalable) {
count = divs[0] - start;
i = 1;
} else {
count = 0;
i = 0;
}
for (; i < numDivs; i += 2) {
// Alternatively, we could use |top| and |bottom| as variable names, instead of
// |left| and |right|.
int left = divs[i];
int right = (i + 1 < numDivs) ? divs[i + 1] : end;
count += right - left;
}
return count;
}
/**
* Set points for the src and dst rects on subsequent draw calls.
*/
static void set_points(float* dst, float* src, const int* divs, int divCount, int srcFixed,
int srcScalable, float srcStart, float srcEnd, float dstStart, float dstEnd,
bool isScalable) {
float dstLen = dstEnd - dstStart;
float scale;
if (srcFixed <= dstLen) {
// This is the "normal" case, where we scale the "scalable" patches and leave
// the other patches fixed.
scale = (dstLen - ((float) srcFixed)) / ((float) srcScalable);
} else {
// In this case, we eliminate the "scalable" patches and scale the "fixed" patches.
scale = dstLen / ((float) srcFixed);
}
src[0] = srcStart;
dst[0] = dstStart;
for (int i = 0; i < divCount; i++) {
src[i + 1] = (float) (divs[i]);
float srcDelta = src[i + 1] - src[i];
float dstDelta;
if (srcFixed <= dstLen) {
dstDelta = isScalable ? scale * srcDelta : srcDelta;
} else {
dstDelta = isScalable ? 0.0f : scale * srcDelta;
}
dst[i + 1] = dst[i] + dstDelta;
// Alternate between "scalable" and "fixed" patches.
isScalable = !isScalable;
}
src[divCount + 1] = srcEnd;
dst[divCount + 1] = dstEnd;
}
SkLatticeIter::SkLatticeIter(const SkCanvas::Lattice& lattice, const SkRect& dst) {
const int* xDivs = lattice.fXDivs;
const int origXCount = lattice.fXCount;
const int* yDivs = lattice.fYDivs;
const int origYCount = lattice.fYCount;
SkASSERT(lattice.fBounds);
const SkIRect src = *lattice.fBounds;
// In the x-dimension, the first rectangle always starts at x = 0 and is "scalable".
// If xDiv[0] is 0, it indicates that the first rectangle is degenerate, so the
// first real rectangle "scalable" in the x-direction.
//
// The same interpretation applies to the y-dimension.
//
// As we move left to right across the image, alternating patches will be "fixed" or
// "scalable" in the x-direction. Similarly, as move top to bottom, alternating
// patches will be "fixed" or "scalable" in the y-direction.
int xCount = origXCount;
int yCount = origYCount;
bool xIsScalable = (xCount > 0 && src.fLeft == xDivs[0]);
if (xIsScalable) {
// Once we've decided that the first patch is "scalable", we don't need the
// xDiv. It is always implied that we start at the edge of the bounds.
xDivs++;
xCount--;
}
bool yIsScalable = (yCount > 0 && src.fTop == yDivs[0]);
if (yIsScalable) {
// Once we've decided that the first patch is "scalable", we don't need the
// yDiv. It is always implied that we start at the edge of the bounds.
yDivs++;
yCount--;
}
// Count "scalable" and "fixed" pixels in each dimension.
int xCountScalable = count_scalable_pixels(xDivs, xCount, xIsScalable, src.fLeft, src.fRight);
int xCountFixed = src.width() - xCountScalable;
int yCountScalable = count_scalable_pixels(yDivs, yCount, yIsScalable, src.fTop, src.fBottom);
int yCountFixed = src.height() - yCountScalable;
fSrcX.reset(xCount + 2);
fDstX.reset(xCount + 2);
set_points(fDstX.begin(), fSrcX.begin(), xDivs, xCount, xCountFixed, xCountScalable,
src.fLeft, src.fRight, dst.fLeft, dst.fRight, xIsScalable);
fSrcY.reset(yCount + 2);
fDstY.reset(yCount + 2);
set_points(fDstY.begin(), fSrcY.begin(), yDivs, yCount, yCountFixed, yCountScalable,
src.fTop, src.fBottom, dst.fTop, dst.fBottom, yIsScalable);
fCurrX = fCurrY = 0;
fNumRectsInLattice = (xCount + 1) * (yCount + 1);
fNumRectsToDraw = fNumRectsInLattice;
if (lattice.fFlags) {
fFlags.push_back_n(fNumRectsInLattice);
const SkCanvas::Lattice::Flags* flags = lattice.fFlags;
bool hasPadRow = (yCount != origYCount);
bool hasPadCol = (xCount != origXCount);
if (hasPadRow) {
// The first row of rects are all empty, skip the first row of flags.
flags += origXCount + 1;
}
int i = 0;
for (int y = 0; y < yCount + 1; y++) {
for (int x = 0; x < origXCount + 1; x++) {
if (0 == x && hasPadCol) {
// The first column of rects are all empty. Skip a rect.
flags++;
continue;
}
fFlags[i] = *flags;
flags++;
i++;
}
}
for (int j = 0; j < fFlags.count(); j++) {
if (SkCanvas::Lattice::kTransparent_Flags == fFlags[j]) {
fNumRectsToDraw--;
}
}
}
}
bool SkLatticeIter::Valid(int width, int height, const SkIRect& center) {
return !center.isEmpty() && SkIRect::MakeWH(width, height).contains(center);
}
SkLatticeIter::SkLatticeIter(int w, int h, const SkIRect& c, const SkRect& dst) {
SkASSERT(SkIRect::MakeWH(w, h).contains(c));
fSrcX.reset(4);
fSrcY.reset(4);
fDstX.reset(4);
fDstY.reset(4);
fSrcX[0] = 0;
fSrcX[1] = SkIntToScalar(c.fLeft);
fSrcX[2] = SkIntToScalar(c.fRight);
fSrcX[3] = SkIntToScalar(w);
fSrcY[0] = 0;
fSrcY[1] = SkIntToScalar(c.fTop);
fSrcY[2] = SkIntToScalar(c.fBottom);
fSrcY[3] = SkIntToScalar(h);
fDstX[0] = dst.fLeft;
fDstX[1] = dst.fLeft + SkIntToScalar(c.fLeft);
fDstX[2] = dst.fRight - SkIntToScalar(w - c.fRight);
fDstX[3] = dst.fRight;
fDstY[0] = dst.fTop;
fDstY[1] = dst.fTop + SkIntToScalar(c.fTop);
fDstY[2] = dst.fBottom - SkIntToScalar(h - c.fBottom);
fDstY[3] = dst.fBottom;
if (fDstX[1] > fDstX[2]) {
fDstX[1] = fDstX[0] + (fDstX[3] - fDstX[0]) * c.fLeft / (w - c.width());
fDstX[2] = fDstX[1];
}
if (fDstY[1] > fDstY[2]) {
fDstY[1] = fDstY[0] + (fDstY[3] - fDstY[0]) * c.fTop / (h - c.height());
fDstY[2] = fDstY[1];
}
fCurrX = fCurrY = 0;
fNumRectsInLattice = 9;
fNumRectsToDraw = 9;
}
bool SkLatticeIter::next(SkRect* src, SkRect* dst) {
int currRect = fCurrX + fCurrY * (fSrcX.count() - 1);
if (currRect == fNumRectsInLattice) {
return false;
}
const int x = fCurrX;
const int y = fCurrY;
SkASSERT(x >= 0 && x < fSrcX.count() - 1);
SkASSERT(y >= 0 && y < fSrcY.count() - 1);
if (fSrcX.count() - 1 == ++fCurrX) {
fCurrX = 0;
fCurrY += 1;
}
if (fFlags.count() > 0 && SkToBool(SkCanvas::Lattice::kTransparent_Flags & fFlags[currRect])) {
return this->next(src, dst);
}
src->set(fSrcX[x], fSrcY[y], fSrcX[x + 1], fSrcY[y + 1]);
dst->set(fDstX[x], fDstY[y], fDstX[x + 1], fDstY[y + 1]);
return true;
}
void SkLatticeIter::mapDstScaleTranslate(const SkMatrix& matrix) {
SkASSERT(matrix.isScaleTranslate());
SkScalar tx = matrix.getTranslateX();
SkScalar sx = matrix.getScaleX();
for (int i = 0; i < fDstX.count(); i++) {
fDstX[i] = fDstX[i] * sx + tx;
}
SkScalar ty = matrix.getTranslateY();
SkScalar sy = matrix.getScaleY();
for (int i = 0; i < fDstY.count(); i++) {
fDstY[i] = fDstY[i] * sy + ty;
}
}
|