Flutter Engine
The Flutter Engine
lattice.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2016 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 "gm/gm.h"
16#include "include/core/SkRect.h"
19#include "include/core/SkSize.h"
24#include "tools/ToolUtils.h"
25
26static sk_sp<SkSurface> make_surface(SkCanvas* root, int N, int padLeft, int padTop,
27 int padRight, int padBottom) {
28 SkImageInfo info = SkImageInfo::MakeN32Premul(N + padLeft + padRight, N + padTop + padBottom);
30}
31
32static sk_sp<SkImage> make_image(SkCanvas* root, int* xDivs, int* yDivs, int padLeft, int padTop,
33 int padRight, int padBottom) {
34 const int kCap = 28;
35 const int kMid = 8;
36 const int kSize = 2*kCap + 3*kMid;
37
38 auto surface(make_surface(root, kSize, padLeft, padTop, padRight, padBottom));
39 SkCanvas* canvas = surface->getCanvas();
40 canvas->translate((float) padLeft, (float) padTop);
41
44 const SkScalar radius = SkIntToScalar(kCap) - strokeWidth/2;
45
46 xDivs[0] = kCap + padLeft;
47 yDivs[0] = kCap + padTop;
48 xDivs[1] = kCap + kMid + padLeft;
49 yDivs[1] = kCap + kMid + padTop;
50 xDivs[2] = kCap + 2 * kMid + padLeft;
51 yDivs[2] = kCap + 2 * kMid + padTop;
52 xDivs[3] = kCap + 3 * kMid + padLeft;
53 yDivs[3] = kCap + 3 * kMid + padTop;
54
56 paint.setAntiAlias(true);
57
58 paint.setColor(0xFFFFFF00);
59 canvas->drawRoundRect(r, radius, radius, paint);
60
62 paint.setColor(0x8800FF00);
63 canvas->drawRect(r, paint);
64 r.setXYWH(SkIntToScalar(kCap + kMid), 0, SkIntToScalar(kMid), SkIntToScalar(kSize));
65 paint.setColor(0x880000FF);
66 canvas->drawRect(r, paint);
67 r.setXYWH(SkIntToScalar(kCap + 2*kMid), 0, SkIntToScalar(kMid), SkIntToScalar(kSize));
68 paint.setColor(0x88FF00FF);
69 canvas->drawRect(r, paint);
70
72 paint.setColor(0x8800FF00);
73 canvas->drawRect(r, paint);
74 r.setXYWH(0, SkIntToScalar(kCap + kMid), SkIntToScalar(kSize), SkIntToScalar(kMid));
75 paint.setColor(0x880000FF);
76 canvas->drawRect(r, paint);
77 r.setXYWH(0, SkIntToScalar(kCap + 2*kMid), SkIntToScalar(kSize), SkIntToScalar(kMid));
78 paint.setColor(0x88FF00FF);
79 canvas->drawRect(r, paint);
80
81 return surface->makeImageSnapshot();
82}
83
84static void image_to_bitmap(GrDirectContext* dContext, const SkImage* image, SkBitmap* bm) {
86 bm->allocPixels(info);
87 image->readPixels(dContext, info, bm->getPixels(), bm->rowBytes(), 0, 0);
88}
89
90/**
91 * This is similar to NinePatchStretchGM, but it also tests "ninepatch" images with more
92 * than nine patches.
93 */
94class LatticeGM : public skiagm::GM {
95public:
97
98protected:
99 SkString getName() const override { return SkString("lattice"); }
100
101 SkISize getISize() override { return SkISize::Make(800, 800); }
102
103 void onDrawHelper(GrDirectContext* dContext, SkCanvas* canvas, int padLeft, int padTop,
104 int padRight, int padBottom) {
105 canvas->save();
106
107 int xDivs[5];
108 int yDivs[5];
109 xDivs[0] = padLeft;
110 yDivs[0] = padTop;
111
113 sk_sp<SkImage> image = make_image(canvas, xDivs + 1, yDivs + 1, padLeft, padTop,
114 padRight, padBottom);
115 image_to_bitmap(dContext, image.get(), &bitmap);
116
117 const SkSize size[] = {
118 { 50, 50, }, // shrink in both axes
119 { 50, 200, }, // shrink in X
120 { 200, 50, }, // shrink in Y
121 { 200, 200, },
122 };
123
124 canvas->drawImage(image, 10, 10);
125
126 SkScalar x = SkIntToScalar(100);
127 SkScalar y = SkIntToScalar(100);
128
129 SkCanvas::Lattice lattice;
130 lattice.fXCount = 4;
131 lattice.fXDivs = xDivs + 1;
132 lattice.fYCount = 4;
133 lattice.fYDivs = yDivs + 1;
134 lattice.fRectTypes = nullptr;
135 lattice.fColors = nullptr;
136
137 SkIRect bounds = SkIRect::MakeLTRB(padLeft, padTop,
138 image->width() - padRight, image->height() - padBottom);
139 lattice.fBounds = (bounds == SkIRect::MakeWH(image->width(), image->height())) ?
140 nullptr : &bounds;
141
142 for (int iy = 0; iy < 2; ++iy) {
143 for (int ix = 0; ix < 2; ++ix) {
144 int i = ix * 2 + iy;
145 SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60,
146 size[i].width(), size[i].height());
147 canvas->drawImageLattice(image.get(), lattice, r);
148 }
149 }
150
151 // Provide hints about 3 solid color rects. These colors match
152 // what was already in the bitmap.
153 int fixedColorX[3] = {2, 4, 1};
154 int fixedColorY[3] = {1, 1, 2};
158 for (int rectNum = 0; rectNum < 3; rectNum++) {
159 int srcX = xDivs[fixedColorX[rectNum]-1];
160 int srcY = yDivs[fixedColorY[rectNum]-1];
161 image->readPixels(dContext, info, &fixedColor[rectNum], 4, srcX, srcY);
162 }
163
164 // Include the degenerate first div. While normally the first patch is "scalable",
165 // this will mean that the first non-degenerate patch is "fixed".
166 lattice.fXCount = 5;
167 lattice.fXDivs = xDivs;
168 lattice.fYCount = 5;
169 lattice.fYDivs = yDivs;
170
171 // Let's skip a few rects.
178 for (int rectNum = 0; rectNum < 3; rectNum++) {
179 flags[fixedColorY[rectNum]*6 + fixedColorX[rectNum]]
181 }
182 lattice.fRectTypes = flags;
183
184 SkColor colors[36];
185 sk_bzero(colors, 36 * sizeof(SkColor));
186 for (int rectNum = 0; rectNum < 3; rectNum++) {
187 colors[fixedColorY[rectNum]*6 + fixedColorX[rectNum]]
188 = fixedColor[rectNum];
189 }
190
191 lattice.fColors = colors;
192
193 canvas->translate(400, 0);
194 for (int iy = 0; iy < 2; ++iy) {
195 for (int ix = 0; ix < 2; ++ix) {
196 int i = ix * 2 + iy;
197 SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60,
198 size[i].width(), size[i].height());
199 canvas->drawImageLattice(image.get(), lattice, r);
200 }
201 }
202
203 canvas->restore();
204 }
205
206 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
207 auto rContext = canvas->recordingContext();
208 auto dContext = GrAsDirectContext(rContext);
209 if (rContext && !dContext) {
210 *errorMsg = "not supported in ddl";
211 return DrawResult::kSkip;
212 }
213 this->onDrawHelper(dContext, canvas, 0, 0, 0, 0);
214 canvas->translate(0.0f, 400.0f);
215 this->onDrawHelper(dContext, canvas, 3, 7, 4, 11);
216 return DrawResult::kOk;
217 }
218
219private:
220 using INHERITED = skiagm::GM;
221};
222DEF_GM( return new LatticeGM; )
223
224
225// LatticeGM2 exercises code paths that draw fixed color and 1x1 rectangles.
226class LatticeGM2 : public skiagm::GM {
227public:
229 SkString getName() const override { return SkString("lattice2"); }
230
231 SkISize getISize() override { return SkISize::Make(800, 800); }
232
233 sk_sp<SkImage> makeImage(SkCanvas* root, int padLeft, int padTop, int padRight, int padBottom) {
234 const int kSize = 80;
235 auto surface(make_surface(root, kSize, padLeft, padTop, padRight, padBottom));
236 SkCanvas* canvas = surface->getCanvas();
238 paint.setAntiAlias(false);
239 SkRect r;
240
241 //first line
242 r.setXYWH(0, 0, 4, 1); //4x1 green rect
243 paint.setColor(0xFF00FF00);
244 canvas->drawRect(r, paint);
245
246 r.setXYWH(4, 0, 1, 1); //1x1 blue pixel -> draws as rectangle
247 paint.setColor(0xFF0000FF);
248 canvas->drawRect(r, paint);
249
250 r.setXYWH(5, 0, kSize-5, 1); //the rest of the line is red
251 paint.setColor(0xFFFF0000);
252 canvas->drawRect(r, paint);
253
254
255 //second line -> draws as fixed color rectangles
256 r.setXYWH(0, 1, 4, 1); //4x1 red rect
257 paint.setColor(0xFFFF0000);
258 canvas->drawRect(r, paint);
259
260 r.setXYWH(4, 1, 1, 1); //1x1 blue pixel with alpha
261 paint.setColor(0x880000FF);
262 canvas->drawRect(r, paint);
263
264 r.setXYWH(5, 1, kSize-5, 1); //the rest of the line is green
265 paint.setColor(0xFF00FF00);
266 canvas->drawRect(r, paint);
267
268
269 //third line - does not draw, because it is transparent
270 r.setXYWH(0, 2, 4, kSize-2); //4x78 green rect
271 paint.setColor(0xFF00FF00);
272 canvas->drawRect(r, paint);
273
274 r.setXYWH(4, 2, 1, kSize-2); //1x78 red pixel with alpha
275 paint.setColor(0x88FF0000);
276 canvas->drawRect(r, paint);
277
278 r.setXYWH(5, 2, kSize-5, kSize-2); //the rest of the image is blue
279 paint.setColor(0xFF0000FF);
280 canvas->drawRect(r, paint);
281
282 return surface->makeImageSnapshot();
283 }
284
285 void onDrawHelper(SkCanvas* canvas, int padLeft, int padTop, int padRight, int padBottom,
286 SkPaint& paint) {
287 int xDivs[2] = {4, 5};
288 int yDivs[2] = {1, 2};
289
290 canvas->save();
291
292 sk_sp<SkImage> image = makeImage(canvas, padLeft, padTop, padRight, padBottom);
293
294 canvas->drawImage(image, 10, 10);
295
296 SkCanvas::Lattice lattice;
297 lattice.fXCount = 2;
298 lattice.fXDivs = xDivs;
299 lattice.fYCount = 2;
300 lattice.fYDivs = yDivs;
301 lattice.fBounds = nullptr;
302
308
312 lattice.fRectTypes = flags;
313
315 0xFFFF0000, 0x880000FF, 0xFF00FF00,
317 lattice.fColors = colors;
318 paint.setColor(0xFFFFFFFF);
319 canvas->drawImageLattice(image.get(), lattice,
320 SkRect::MakeXYWH(100, 100, 200, 200),
322
323 //draw the same content with alpha
324 canvas->translate(400, 0);
325 paint.setColor(0x80000FFF);
326 canvas->drawImageLattice(image.get(), lattice,
327 SkRect::MakeXYWH(100, 100, 200, 200),
329
330 canvas->restore();
331 }
332
333 void onDraw(SkCanvas* canvas) override {
334
335 //draw a rectangle in the background with transparent pixels
337 paint.setColor(0x7F123456);
338 paint.setBlendMode(SkBlendMode::kSrc);
339 canvas->drawRect( SkRect::MakeXYWH(300, 0, 300, 800), paint);
340
341 //draw image lattice with kSrcOver blending
342 paint.setBlendMode(SkBlendMode::kSrcOver);
343 this->onDrawHelper(canvas, 0, 0, 0, 0, paint);
344
345 //draw image lattice with kSrcATop blending
346 canvas->translate(0.0f, 400.0f);
347 paint.setBlendMode(SkBlendMode::kSrcATop);
348 this->onDrawHelper(canvas, 0, 0, 0, 0, paint);
349 }
350
351private:
352 using INHERITED = skiagm::GM;
353};
354DEF_GM( return new LatticeGM2; )
355
356// Code paths that incorporate the paint color when drawing the lattice (using an alpha image)
357DEF_SIMPLE_GM_BG(lattice_alpha, canvas, 120, 120, SK_ColorWHITE) {
358 auto surface = ToolUtils::makeSurface(canvas, SkImageInfo::MakeA8(100, 100));
359 surface->getCanvas()->clear(0);
360 surface->getCanvas()->drawCircle(50, 50, 50, SkPaint());
361 auto image = surface->makeImageSnapshot();
362
363 int divs[] = { 20, 40, 60, 80 };
364
365 SkCanvas::Lattice lattice;
366 lattice.fXCount = 4;
367 lattice.fXDivs = divs;
368 lattice.fYCount = 4;
369 lattice.fYDivs = divs;
370 lattice.fRectTypes = nullptr;
371 lattice.fColors = nullptr;
372 lattice.fBounds = nullptr;
373
375 paint.setColor(SK_ColorMAGENTA);
376 canvas->drawImageLattice(image.get(), lattice, SkRect::MakeWH(120, 120),
378}
static const int strokeWidth
Definition: BlurTest.cpp:60
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
sk_bzero(glyphs, sizeof(glyphs))
static GrDirectContext * GrAsDirectContext(GrContext_Base *base)
kUnpremul_SkAlphaType
@ kSrcOver
r = s + (1-sa)*d
@ kSrcATop
r = s*da + d*(1-sa)
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition: SkColorType.h:26
constexpr SkColor SK_ColorMAGENTA
Definition: SkColor.h:147
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
#define SkIntToScalar(x)
Definition: SkScalar.h:57
#define N
Definition: beziers.cpp:19
void onDrawHelper(SkCanvas *canvas, int padLeft, int padTop, int padRight, int padBottom, SkPaint &paint)
Definition: lattice.cpp:285
SkISize getISize() override
Definition: lattice.cpp:231
void onDraw(SkCanvas *canvas) override
Definition: lattice.cpp:333
SkString getName() const override
Definition: lattice.cpp:229
sk_sp< SkImage > makeImage(SkCanvas *root, int padLeft, int padTop, int padRight, int padBottom)
Definition: lattice.cpp:233
SkISize getISize() override
Definition: lattice.cpp:101
void onDrawHelper(GrDirectContext *dContext, SkCanvas *canvas, int padLeft, int padTop, int padRight, int padBottom)
Definition: lattice.cpp:103
DrawResult onDraw(SkCanvas *canvas, SkString *errorMsg) override
Definition: lattice.cpp:206
SkString getName() const override
Definition: lattice.cpp:99
LatticeGM()
Definition: lattice.cpp:96
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:258
size_t rowBytes() const
Definition: SkBitmap.h:238
void * getPixels() const
Definition: SkBitmap.h:283
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
virtual GrRecordingContext * recordingContext() const
Definition: SkCanvas.cpp:1637
void drawRoundRect(const SkRect &rect, SkScalar rx, SkScalar ry, const SkPaint &paint)
Definition: SkCanvas.cpp:2717
void drawImageLattice(const SkImage *image, const Lattice &lattice, const SkRect &dst, SkFilterMode filter, const SkPaint *paint=nullptr)
Definition: SkCanvas.cpp:1786
int save()
Definition: SkCanvas.cpp:447
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
bool readPixels(GrDirectContext *context, const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY, CachingHint cachingHint=kAllow_CachingHint) const
Definition: SkImage.cpp:42
int width() const
Definition: SkImage.h:285
int height() const
Definition: SkImage.h:291
T * get() const
Definition: SkRefCnt.h:303
Definition: gm.h:110
SkScalar width()
Definition: gm.h:159
SkScalar height()
Definition: gm.h:162
const Paint & paint
Definition: color_source.cc:38
VkSurfaceKHR surface
Definition: main.cc:49
float SkScalar
Definition: extension.cpp:12
FlutterSemanticsFlag flags
#define DEF_GM(CODE)
Definition: gm.h:40
constexpr int kSize
static void image_to_bitmap(GrDirectContext *dContext, const SkImage *image, SkBitmap *bm)
Definition: lattice.cpp:84
static sk_sp< SkSurface > make_surface(SkCanvas *root, int N, int padLeft, int padTop, int padRight, int padBottom)
Definition: lattice.cpp:26
DEF_SIMPLE_GM_BG(lattice_alpha, canvas, 120, 120, SK_ColorWHITE)
Definition: lattice.cpp:357
static sk_sp< SkImage > make_image(SkCanvas *root, int *xDivs, int *yDivs, int padLeft, int padTop, int padRight, int padBottom)
Definition: lattice.cpp:32
double y
double x
Optional< SkRect > bounds
Definition: SkRecords.h:189
sk_sp< const SkImage > image
Definition: SkRecords.h:269
PODArray< int > xDivs
Definition: SkRecords.h:271
PODArray< SkColor > colors
Definition: SkRecords.h:276
PODArray< int > yDivs
Definition: SkRecords.h:273
sk_sp< SkSurface > makeSurface(SkCanvas *canvas, const SkImageInfo &info, const SkSurfaceProps *props)
Definition: ToolUtils.cpp:512
Definition: bitmap.py:1
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
string root
Definition: scale_cpu.py:20
DrawResult
Definition: gm.h:104
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
static constexpr SkIRect MakeLTRB(int32_t l, int32_t t, int32_t r, int32_t b)
Definition: SkRect.h:91
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static SkImageInfo MakeN32Premul(int width, int height)
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static SkImageInfo MakeA8(int width, int height)
void setXYWH(float x, float y, float width, float height)
Definition: SkRect.h:931
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609
Definition: SkSize.h:52