1 /*
2  * Copyright 2015 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "SkLatticeIter.h"
9 #include "SkRect.h"
10 
11 /**
12  *  Divs must be in increasing order with no duplicates.
13  */
valid_divs(const int * divs,int count,int start,int end)14 static bool valid_divs(const int* divs, int count, int start, int end) {
15     int prev = start - 1;
16     for (int i = 0; i < count; i++) {
17         if (prev >= divs[i] || divs[i] >= end) {
18             return false;
19         }
20     }
21 
22     return true;
23 }
24 
Valid(int width,int height,const SkCanvas::Lattice & lattice)25 bool SkLatticeIter::Valid(int width, int height, const SkCanvas::Lattice& lattice) {
26     SkIRect totalBounds = SkIRect::MakeWH(width, height);
27     SkASSERT(lattice.fBounds);
28     const SkIRect latticeBounds = *lattice.fBounds;
29     if (!totalBounds.contains(latticeBounds)) {
30         return false;
31     }
32 
33     bool zeroXDivs = lattice.fXCount <= 0 || (1 == lattice.fXCount &&
34                                               latticeBounds.fLeft == lattice.fXDivs[0]);
35     bool zeroYDivs = lattice.fYCount <= 0 || (1 == lattice.fYCount &&
36                                               latticeBounds.fTop == lattice.fYDivs[0]);
37     if (zeroXDivs && zeroYDivs) {
38         return false;
39     }
40 
41     return valid_divs(lattice.fXDivs, lattice.fXCount, latticeBounds.fLeft, latticeBounds.fRight)
42         && valid_divs(lattice.fYDivs, lattice.fYCount, latticeBounds.fTop, latticeBounds.fBottom);
43 }
44 
45 /**
46  *  Count the number of pixels that are in "scalable" patches.
47  */
count_scalable_pixels(const int32_t * divs,int numDivs,bool firstIsScalable,int start,int end)48 static int count_scalable_pixels(const int32_t* divs, int numDivs, bool firstIsScalable,
49                                  int start, int end) {
50     if (0 == numDivs) {
51         return firstIsScalable ? end - start : 0;
52     }
53 
54     int i;
55     int count;
56     if (firstIsScalable) {
57         count = divs[0] - start;
58         i = 1;
59     } else {
60         count = 0;
61         i = 0;
62     }
63 
64     for (; i < numDivs; i += 2) {
65         // Alternatively, we could use |top| and |bottom| as variable names, instead of
66         // |left| and |right|.
67         int left = divs[i];
68         int right = (i + 1 < numDivs) ? divs[i + 1] : end;
69         count += right - left;
70     }
71 
72     return count;
73 }
74 
75 /**
76  *  Set points for the src and dst rects on subsequent draw calls.
77  */
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)78 static void set_points(float* dst, float* src, const int* divs, int divCount, int srcFixed,
79                        int srcScalable, float srcStart, float srcEnd, float dstStart, float dstEnd,
80                        bool isScalable) {
81 
82     float dstLen = dstEnd - dstStart;
83     float scale;
84     if (srcFixed <= dstLen) {
85         // This is the "normal" case, where we scale the "scalable" patches and leave
86         // the other patches fixed.
87         scale = (dstLen - ((float) srcFixed)) / ((float) srcScalable);
88     } else {
89         // In this case, we eliminate the "scalable" patches and scale the "fixed" patches.
90         scale = dstLen / ((float) srcFixed);
91     }
92 
93     src[0] = srcStart;
94     dst[0] = dstStart;
95     for (int i = 0; i < divCount; i++) {
96         src[i + 1] = (float) (divs[i]);
97         float srcDelta = src[i + 1] - src[i];
98         float dstDelta;
99         if (srcFixed <= dstLen) {
100             dstDelta = isScalable ? scale * srcDelta : srcDelta;
101         } else {
102             dstDelta = isScalable ? 0.0f : scale * srcDelta;
103         }
104         dst[i + 1] = dst[i] + dstDelta;
105 
106         // Alternate between "scalable" and "fixed" patches.
107         isScalable = !isScalable;
108     }
109 
110     src[divCount + 1] = srcEnd;
111     dst[divCount + 1] = dstEnd;
112 }
113 
SkLatticeIter(const SkCanvas::Lattice & lattice,const SkRect & dst)114 SkLatticeIter::SkLatticeIter(const SkCanvas::Lattice& lattice, const SkRect& dst) {
115     const int* xDivs = lattice.fXDivs;
116     const int origXCount = lattice.fXCount;
117     const int* yDivs = lattice.fYDivs;
118     const int origYCount = lattice.fYCount;
119     SkASSERT(lattice.fBounds);
120     const SkIRect src = *lattice.fBounds;
121 
122     // In the x-dimension, the first rectangle always starts at x = 0 and is "scalable".
123     // If xDiv[0] is 0, it indicates that the first rectangle is degenerate, so the
124     // first real rectangle "scalable" in the x-direction.
125     //
126     // The same interpretation applies to the y-dimension.
127     //
128     // As we move left to right across the image, alternating patches will be "fixed" or
129     // "scalable" in the x-direction.  Similarly, as move top to bottom, alternating
130     // patches will be "fixed" or "scalable" in the y-direction.
131     int xCount = origXCount;
132     int yCount = origYCount;
133     bool xIsScalable = (xCount > 0 && src.fLeft == xDivs[0]);
134     if (xIsScalable) {
135         // Once we've decided that the first patch is "scalable", we don't need the
136         // xDiv.  It is always implied that we start at the edge of the bounds.
137         xDivs++;
138         xCount--;
139     }
140     bool yIsScalable = (yCount > 0 && src.fTop == yDivs[0]);
141     if (yIsScalable) {
142         // Once we've decided that the first patch is "scalable", we don't need the
143         // yDiv.  It is always implied that we start at the edge of the bounds.
144         yDivs++;
145         yCount--;
146     }
147 
148     // Count "scalable" and "fixed" pixels in each dimension.
149     int xCountScalable = count_scalable_pixels(xDivs, xCount, xIsScalable, src.fLeft, src.fRight);
150     int xCountFixed = src.width() - xCountScalable;
151     int yCountScalable = count_scalable_pixels(yDivs, yCount, yIsScalable, src.fTop, src.fBottom);
152     int yCountFixed = src.height() - yCountScalable;
153 
154     fSrcX.reset(xCount + 2);
155     fDstX.reset(xCount + 2);
156     set_points(fDstX.begin(), fSrcX.begin(), xDivs, xCount, xCountFixed, xCountScalable,
157                src.fLeft, src.fRight, dst.fLeft, dst.fRight, xIsScalable);
158 
159     fSrcY.reset(yCount + 2);
160     fDstY.reset(yCount + 2);
161     set_points(fDstY.begin(), fSrcY.begin(), yDivs, yCount, yCountFixed, yCountScalable,
162                src.fTop, src.fBottom, dst.fTop, dst.fBottom, yIsScalable);
163 
164     fCurrX = fCurrY = 0;
165     fNumRectsInLattice = (xCount + 1) * (yCount + 1);
166     fNumRectsToDraw = fNumRectsInLattice;
167 
168     if (lattice.fFlags) {
169         fFlags.push_back_n(fNumRectsInLattice);
170 
171         const SkCanvas::Lattice::Flags* flags = lattice.fFlags;
172 
173         bool hasPadRow = (yCount != origYCount);
174         bool hasPadCol = (xCount != origXCount);
175         if (hasPadRow) {
176             // The first row of rects are all empty, skip the first row of flags.
177             flags += origXCount + 1;
178         }
179 
180         int i = 0;
181         for (int y = 0; y < yCount + 1; y++) {
182             for (int x = 0; x < origXCount + 1; x++) {
183                 if (0 == x && hasPadCol) {
184                     // The first column of rects are all empty.  Skip a rect.
185                     flags++;
186                     continue;
187                 }
188 
189                 fFlags[i] = *flags;
190                 flags++;
191                 i++;
192             }
193         }
194 
195         for (int j = 0; j < fFlags.count(); j++) {
196             if (SkCanvas::Lattice::kTransparent_Flags == fFlags[j]) {
197                 fNumRectsToDraw--;
198             }
199         }
200     }
201 }
202 
Valid(int width,int height,const SkIRect & center)203 bool SkLatticeIter::Valid(int width, int height, const SkIRect& center) {
204     return !center.isEmpty() && SkIRect::MakeWH(width, height).contains(center);
205 }
206 
SkLatticeIter(int w,int h,const SkIRect & c,const SkRect & dst)207 SkLatticeIter::SkLatticeIter(int w, int h, const SkIRect& c, const SkRect& dst) {
208     SkASSERT(SkIRect::MakeWH(w, h).contains(c));
209 
210     fSrcX.reset(4);
211     fSrcY.reset(4);
212     fDstX.reset(4);
213     fDstY.reset(4);
214 
215     fSrcX[0] = 0;
216     fSrcX[1] = SkIntToScalar(c.fLeft);
217     fSrcX[2] = SkIntToScalar(c.fRight);
218     fSrcX[3] = SkIntToScalar(w);
219 
220     fSrcY[0] = 0;
221     fSrcY[1] = SkIntToScalar(c.fTop);
222     fSrcY[2] = SkIntToScalar(c.fBottom);
223     fSrcY[3] = SkIntToScalar(h);
224 
225     fDstX[0] = dst.fLeft;
226     fDstX[1] = dst.fLeft + SkIntToScalar(c.fLeft);
227     fDstX[2] = dst.fRight - SkIntToScalar(w - c.fRight);
228     fDstX[3] = dst.fRight;
229 
230     fDstY[0] = dst.fTop;
231     fDstY[1] = dst.fTop + SkIntToScalar(c.fTop);
232     fDstY[2] = dst.fBottom - SkIntToScalar(h - c.fBottom);
233     fDstY[3] = dst.fBottom;
234 
235     if (fDstX[1] > fDstX[2]) {
236         fDstX[1] = fDstX[0] + (fDstX[3] - fDstX[0]) * c.fLeft / (w - c.width());
237         fDstX[2] = fDstX[1];
238     }
239 
240     if (fDstY[1] > fDstY[2]) {
241         fDstY[1] = fDstY[0] + (fDstY[3] - fDstY[0]) * c.fTop / (h - c.height());
242         fDstY[2] = fDstY[1];
243     }
244 
245     fCurrX = fCurrY = 0;
246     fNumRectsInLattice = 9;
247     fNumRectsToDraw = 9;
248 }
249 
next(SkRect * src,SkRect * dst)250 bool SkLatticeIter::next(SkRect* src, SkRect* dst) {
251     int currRect = fCurrX + fCurrY * (fSrcX.count() - 1);
252     if (currRect == fNumRectsInLattice) {
253         return false;
254     }
255 
256     const int x = fCurrX;
257     const int y = fCurrY;
258     SkASSERT(x >= 0 && x < fSrcX.count() - 1);
259     SkASSERT(y >= 0 && y < fSrcY.count() - 1);
260 
261     if (fSrcX.count() - 1 == ++fCurrX) {
262         fCurrX = 0;
263         fCurrY += 1;
264     }
265 
266     if (fFlags.count() > 0 && SkToBool(SkCanvas::Lattice::kTransparent_Flags & fFlags[currRect])) {
267         return this->next(src, dst);
268     }
269 
270     src->set(fSrcX[x], fSrcY[y], fSrcX[x + 1], fSrcY[y + 1]);
271     dst->set(fDstX[x], fDstY[y], fDstX[x + 1], fDstY[y + 1]);
272     return true;
273 }
274 
mapDstScaleTranslate(const SkMatrix & matrix)275 void SkLatticeIter::mapDstScaleTranslate(const SkMatrix& matrix) {
276     SkASSERT(matrix.isScaleTranslate());
277     SkScalar tx = matrix.getTranslateX();
278     SkScalar sx = matrix.getScaleX();
279     for (int i = 0; i < fDstX.count(); i++) {
280         fDstX[i] = fDstX[i] * sx + tx;
281     }
282 
283     SkScalar ty = matrix.getTranslateY();
284     SkScalar sy = matrix.getScaleY();
285     for (int i = 0; i < fDstY.count(); i++) {
286         fDstY[i] = fDstY[i] * sy + ty;
287     }
288 }
289