Flutter Engine
The Flutter Engine
Public Member Functions | Static Public Member Functions | List of all members
SkLatticeIter Class Reference

#include <SkLatticeIter.h>

Public Member Functions

 SkLatticeIter (const SkCanvas::Lattice &lattice, const SkRect &dst)
 
 SkLatticeIter (int imageWidth, int imageHeight, const SkIRect &center, const SkRect &dst)
 
bool next (SkIRect *src, SkRect *dst, bool *isFixedColor=nullptr, SkColor *fixedColor=nullptr)
 
bool next (SkRect *src, SkRect *dst, bool *isFixedColor=nullptr, SkColor *fixedColor=nullptr)
 
void mapDstScaleTranslate (const SkMatrix &matrix)
 
int numRectsToDraw () const
 

Static Public Member Functions

static bool Valid (int imageWidth, int imageHeight, const SkCanvas::Lattice &lattice)
 
static bool Valid (int imageWidth, int imageHeight, const SkIRect &center)
 

Detailed Description

Disect a lattice request into an sequence of src-rect / dst-rect pairs

Definition at line 23 of file SkLatticeIter.h.

Constructor & Destructor Documentation

◆ SkLatticeIter() [1/2]

SkLatticeIter::SkLatticeIter ( const SkCanvas::Lattice lattice,
const SkRect dst 
)

Definition at line 120 of file SkLatticeIter.cpp.

120 {
121 const int* xDivs = lattice.fXDivs;
122 const int origXCount = lattice.fXCount;
123 const int* yDivs = lattice.fYDivs;
124 const int origYCount = lattice.fYCount;
125 SkASSERT(lattice.fBounds);
126 const SkIRect src = *lattice.fBounds;
127
128 // In the x-dimension, the first rectangle always starts at x = 0 and is "scalable".
129 // If xDiv[0] is 0, it indicates that the first rectangle is degenerate, so the
130 // first real rectangle "scalable" in the x-direction.
131 //
132 // The same interpretation applies to the y-dimension.
133 //
134 // As we move left to right across the image, alternating patches will be "fixed" or
135 // "scalable" in the x-direction. Similarly, as move top to bottom, alternating
136 // patches will be "fixed" or "scalable" in the y-direction.
137 int xCount = origXCount;
138 int yCount = origYCount;
139 bool xIsScalable = (xCount > 0 && src.fLeft == xDivs[0]);
140 if (xIsScalable) {
141 // Once we've decided that the first patch is "scalable", we don't need the
142 // xDiv. It is always implied that we start at the edge of the bounds.
143 xDivs++;
144 xCount--;
145 }
146 bool yIsScalable = (yCount > 0 && src.fTop == yDivs[0]);
147 if (yIsScalable) {
148 // Once we've decided that the first patch is "scalable", we don't need the
149 // yDiv. It is always implied that we start at the edge of the bounds.
150 yDivs++;
151 yCount--;
152 }
153
154 // Count "scalable" and "fixed" pixels in each dimension.
155 int xCountScalable = count_scalable_pixels(xDivs, xCount, xIsScalable, src.fLeft, src.fRight);
156 int xCountFixed = src.width() - xCountScalable;
157 int yCountScalable = count_scalable_pixels(yDivs, yCount, yIsScalable, src.fTop, src.fBottom);
158 int yCountFixed = src.height() - yCountScalable;
159
160 fSrcX.reset(xCount + 2);
161 fDstX.reset(xCount + 2);
162 set_points(fDstX.begin(), fSrcX.begin(), xDivs, xCount, xCountFixed, xCountScalable,
163 src.fLeft, src.fRight, dst.fLeft, dst.fRight, xIsScalable);
164
165 fSrcY.reset(yCount + 2);
166 fDstY.reset(yCount + 2);
167 set_points(fDstY.begin(), fSrcY.begin(), yDivs, yCount, yCountFixed, yCountScalable,
168 src.fTop, src.fBottom, dst.fTop, dst.fBottom, yIsScalable);
169
170 fCurrX = fCurrY = 0;
171 fNumRectsInLattice = (xCount + 1) * (yCount + 1);
172 fNumRectsToDraw = fNumRectsInLattice;
173
174 if (lattice.fRectTypes) {
175 fRectTypes.push_back_n(fNumRectsInLattice);
176 fColors.push_back_n(fNumRectsInLattice);
177
179 const SkColor* colors = lattice.fColors;
180
181 bool hasPadRow = (yCount != origYCount);
182 bool hasPadCol = (xCount != origXCount);
183 if (hasPadRow) {
184 // The first row of rects are all empty, skip the first row of flags.
185 flags += origXCount + 1;
186 colors += origXCount + 1;
187 }
188
189 int i = 0;
190 for (int y = 0; y < yCount + 1; y++) {
191 for (int x = 0; x < origXCount + 1; x++) {
192 if (0 == x && hasPadCol) {
193 // The first column of rects are all empty. Skip a rect.
194 flags++;
195 colors++;
196 continue;
197 }
198
199 fRectTypes[i] = *flags;
200 fColors[i] = SkCanvas::Lattice::kFixedColor == *flags ? *colors : 0;
201 flags++;
202 colors++;
203 i++;
204 }
205 }
206
207 for (int j = 0; j < fRectTypes.size(); j++) {
208 if (SkCanvas::Lattice::kTransparent == fRectTypes[j]) {
209 fNumRectsToDraw--;
210 }
211 }
212 }
213}
#define SkASSERT(cond)
Definition: SkAssert.h:116
uint32_t SkColor
Definition: SkColor.h:37
static int count_scalable_pixels(const int32_t *divs, int numDivs, bool firstIsScalable, int start, int end)
static void set_points(float *dst, int *src, const int *divs, int divCount, int srcFixed, int srcScalable, int srcStart, int srcEnd, float dstStart, float dstEnd, bool isScalable)
T * push_back_n(int n)
Definition: SkTArray.h:267
void reset(int n)
Definition: SkTArray.h:144
int size() const
Definition: SkTArray.h:421
FlutterSemanticsFlag flags
double y
double x
int xCount
Definition: SkRecords.h:270
int yCount
Definition: SkRecords.h:272
PODArray< int > xDivs
Definition: SkRecords.h:271
PODArray< SkColor > colors
Definition: SkRecords.h:276
PODArray< int > yDivs
Definition: SkRecords.h:273
dst
Definition: cp.py:12
int fYCount
number of y-coordinates
Definition: SkCanvas.h:1617
const SkIRect * fBounds
source bounds to draw from
Definition: SkCanvas.h:1618
@ kFixedColor
draws one of fColors into lattice rectangle
Definition: SkCanvas.h:1610
@ kTransparent
skips lattice rectangle by making it transparent
Definition: SkCanvas.h:1609
const int * fYDivs
y-axis values dividing bitmap
Definition: SkCanvas.h:1614
int fXCount
number of x-coordinates
Definition: SkCanvas.h:1616
const RectType * fRectTypes
array of fill types
Definition: SkCanvas.h:1615
const SkColor * fColors
array of colors
Definition: SkCanvas.h:1619
const int * fXDivs
x-axis values dividing bitmap
Definition: SkCanvas.h:1613
Definition: SkRect.h:32

◆ SkLatticeIter() [2/2]

SkLatticeIter::SkLatticeIter ( int  imageWidth,
int  imageHeight,
const SkIRect center,
const SkRect dst 
)

Definition at line 219 of file SkLatticeIter.cpp.

219 {
221
222 fSrcX.reset(4);
223 fSrcY.reset(4);
224 fDstX.reset(4);
225 fDstY.reset(4);
226
227 fSrcX[0] = 0;
228 fSrcX[1] = SkIntToScalar(c.fLeft);
229 fSrcX[2] = SkIntToScalar(c.fRight);
230 fSrcX[3] = SkIntToScalar(w);
231
232 fSrcY[0] = 0;
233 fSrcY[1] = SkIntToScalar(c.fTop);
234 fSrcY[2] = SkIntToScalar(c.fBottom);
235 fSrcY[3] = SkIntToScalar(h);
236
237 fDstX[0] = dst.fLeft;
238 fDstX[1] = dst.fLeft + SkIntToScalar(c.fLeft);
239 fDstX[2] = dst.fRight - SkIntToScalar(w - c.fRight);
240 fDstX[3] = dst.fRight;
241
242 fDstY[0] = dst.fTop;
243 fDstY[1] = dst.fTop + SkIntToScalar(c.fTop);
244 fDstY[2] = dst.fBottom - SkIntToScalar(h - c.fBottom);
245 fDstY[3] = dst.fBottom;
246
247 if (fDstX[1] > fDstX[2]) {
248 fDstX[1] = fDstX[0] + (fDstX[3] - fDstX[0]) * c.fLeft / (w - c.width());
249 fDstX[2] = fDstX[1];
250 }
251
252 if (fDstY[1] > fDstY[2]) {
253 fDstY[1] = fDstY[0] + (fDstY[3] - fDstY[0]) * c.fTop / (h - c.height());
254 fDstY[2] = fDstY[1];
255 }
256
257 fCurrX = fCurrY = 0;
258 fNumRectsInLattice = 9;
259 fNumRectsToDraw = 9;
260}
#define SkIntToScalar(x)
Definition: SkScalar.h:57
constexpr bool contains(std::string_view str, std::string_view needle)
Definition: SkStringView.h:41
SkScalar w
SkScalar h
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56

Member Function Documentation

◆ mapDstScaleTranslate()

void SkLatticeIter::mapDstScaleTranslate ( const SkMatrix matrix)

Apply a matrix to the dst points.

Definition at line 294 of file SkLatticeIter.cpp.

294 {
295 SkASSERT(matrix.isScaleTranslate());
296 SkScalar tx = matrix.getTranslateX();
297 SkScalar sx = matrix.getScaleX();
298 for (int i = 0; i < fDstX.size(); i++) {
299 fDstX[i] = fDstX[i] * sx + tx;
300 }
301
302 SkScalar ty = matrix.getTranslateY();
303 SkScalar sy = matrix.getScaleY();
304 for (int i = 0; i < fDstY.size(); i++) {
305 fDstY[i] = fDstY[i] * sy + ty;
306 }
307}
float SkScalar
Definition: extension.cpp:12
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258

◆ next() [1/2]

bool SkLatticeIter::next ( SkIRect src,
SkRect dst,
bool *  isFixedColor = nullptr,
SkColor fixedColor = nullptr 
)

While it returns true, use src/dst to draw the image/bitmap. Optional parameters isFixedColor and fixedColor specify if the rectangle is filled with a fixed color. If (*isFixedColor) is true, then (*fixedColor) contains the rectangle color.

Definition at line 262 of file SkLatticeIter.cpp.

262 {
263 int currRect = fCurrX + fCurrY * (fSrcX.size() - 1);
264 if (currRect == fNumRectsInLattice) {
265 return false;
266 }
267
268 const int x = fCurrX;
269 const int y = fCurrY;
270 SkASSERT(x >= 0 && x < fSrcX.size() - 1);
271 SkASSERT(y >= 0 && y < fSrcY.size() - 1);
272
273 if (fSrcX.size() - 1 == ++fCurrX) {
274 fCurrX = 0;
275 fCurrY += 1;
276 }
277
278 if (!fRectTypes.empty() && SkToBool(SkCanvas::Lattice::kTransparent == fRectTypes[currRect])) {
279 return this->next(src, dst, isFixedColor, fixedColor);
280 }
281
282 src->setLTRB(fSrcX[x], fSrcY[y], fSrcX[x + 1], fSrcY[y + 1]);
283 dst->setLTRB(fDstX[x], fDstY[y], fDstX[x + 1], fDstY[y + 1]);
284 if (isFixedColor && fixedColor) {
285 *isFixedColor = !fRectTypes.empty() &&
286 SkToBool(SkCanvas::Lattice::kFixedColor == fRectTypes[currRect]);
287 if (*isFixedColor) {
288 *fixedColor = fColors[currRect];
289 }
290 }
291 return true;
292}
static constexpr bool SkToBool(const T &x)
Definition: SkTo.h:35
bool next(SkIRect *src, SkRect *dst, bool *isFixedColor=nullptr, SkColor *fixedColor=nullptr)
bool empty() const
Definition: SkTArray.h:199

◆ next() [2/2]

bool SkLatticeIter::next ( SkRect src,
SkRect dst,
bool *  isFixedColor = nullptr,
SkColor fixedColor = nullptr 
)
inline

Version of above that converts the integer src rect to a scalar rect.

Definition at line 43 of file SkLatticeIter.h.

44 {
45 SkIRect isrcR;
46 if (this->next(&isrcR, dst, isFixedColor, fixedColor)) {
47 *src = SkRect::Make(isrcR);
48 return true;
49 }
50 return false;
51 }
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669

◆ numRectsToDraw()

int SkLatticeIter::numRectsToDraw ( ) const
inline

Returns the number of rects that will actually be drawn.

Definition at line 61 of file SkLatticeIter.h.

61 {
62 return fNumRectsToDraw;
63 }

◆ Valid() [1/2]

bool SkLatticeIter::Valid ( int  imageWidth,
int  imageHeight,
const SkCanvas::Lattice lattice 
)
static

Definition at line 32 of file SkLatticeIter.cpp.

32 {
33 SkIRect totalBounds = SkIRect::MakeWH(width, height);
34 SkASSERT(lattice.fBounds);
35 const SkIRect latticeBounds = *lattice.fBounds;
36 if (!totalBounds.contains(latticeBounds)) {
37 return false;
38 }
39
40 bool zeroXDivs = lattice.fXCount <= 0 || (1 == lattice.fXCount &&
41 latticeBounds.fLeft == lattice.fXDivs[0]);
42 bool zeroYDivs = lattice.fYCount <= 0 || (1 == lattice.fYCount &&
43 latticeBounds.fTop == lattice.fYDivs[0]);
44 if (zeroXDivs && zeroYDivs) {
45 return false;
46 }
47
48 return valid_divs(lattice.fXDivs, lattice.fXCount, latticeBounds.fLeft, latticeBounds.fRight)
49 && valid_divs(lattice.fYDivs, lattice.fYCount, latticeBounds.fTop, latticeBounds.fBottom);
50}
static bool valid_divs(const int *divs, int count, int start, int end)
int32_t height
int32_t width
int32_t fBottom
larger y-axis bounds
Definition: SkRect.h:36
int32_t fTop
smaller y-axis bounds
Definition: SkRect.h:34
int32_t fLeft
smaller x-axis bounds
Definition: SkRect.h:33
bool contains(int32_t x, int32_t y) const
Definition: SkRect.h:463
int32_t fRight
larger x-axis bounds
Definition: SkRect.h:35

◆ Valid() [2/2]

bool SkLatticeIter::Valid ( int  imageWidth,
int  imageHeight,
const SkIRect center 
)
static

Definition at line 215 of file SkLatticeIter.cpp.

215 {
216 return !center.isEmpty() && SkIRect::MakeWH(width, height).contains(center);
217}
static SkScalar center(float pos0, float pos1)

The documentation for this class was generated from the following files: