Flutter Engine
The Flutter Engine
ToolUtils.h
Go to the documentation of this file.
1/*
2 * Copyright 2014 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#ifndef ToolUtils_DEFINED
9#define ToolUtils_DEFINED
10
17#include "include/core/SkRect.h"
20#include "include/core/SkSpan.h"
23#include "include/core/SkTypeface.h" // IWYU pragma: keep
28#include "src/base/SkRandom.h"
30
31#include <cstddef>
32#include <cstdint>
33#include <memory>
34#include <vector>
35#include <functional>
36
37class SkBitmap;
38class SkCanvas;
39class SkFont;
40class SkImage;
41class SkMatrix;
42class SkMetaData;
43class SkPaint;
44class SkPath;
45class SkShader;
46class SkSurfaceProps;
48enum SkAlphaType : int;
49enum SkColorType : int;
50enum class SkTextEncoding;
51enum class SkTileMode;
52struct SkImageInfo;
53
54namespace ToolUtils {
55
56const char* alphatype_name (SkAlphaType);
57const char* colortype_name (SkColorType);
58const char* colortype_depth(SkColorType); // like colortype_name, but channel order agnostic
59const char* tilemode_name(SkTileMode);
60
61/**
62 * Map opaque colors from 8888 to 565.
63 */
65
67 const void* text,
68 size_t length,
70 SkPath*,
71 const SkPoint* positions = nullptr);
72
73/**
74 * Returns true iff all of the pixels between the two images are identical.
75 *
76 * If the configs differ, return false.
77 */
78bool equal_pixels(const SkPixmap&, const SkPixmap&);
79bool equal_pixels(const SkBitmap&, const SkBitmap&);
80bool equal_pixels(const SkImage* a, const SkImage* b);
81
82/** Returns a newly created CheckerboardShader. */
84
85/** Draw a checkerboard pattern in the current canvas, restricted to
86 the current clip, using SkBlendMode::kSrc. */
87void draw_checkerboard(SkCanvas* canvas, SkColor color1, SkColor color2, int checkSize);
88
89/** Make it easier to create a bitmap-based checkerboard */
90SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize);
91
92sk_sp<SkImage> create_checkerboard_image(int w, int h, SkColor c1, SkColor c2, int checkSize);
93
94/** A default checkerboard. */
95inline void draw_checkerboard(SkCanvas* canvas) {
96 ToolUtils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
97}
98
100public:
101 HilbertGenerator(float desiredSize, float desiredLineWidth, int desiredDepth);
102
103 // Draw a Hilbert curve into the canvas w/ a gradient along its length
104 void draw(SkCanvas* canvas);
105
106private:
107 void turn90(bool turnLeft);
108 void line(SkCanvas* canvas);
109 void recursiveDraw(SkCanvas* canvas, int curDepth, bool turnLeft);
110 SkColor4f getColor(float curLen);
111
112 const float fDesiredSize;
113 const int fDesiredDepth;
114 const float fSegmentLength; // length of a line segment
115 const float fDesiredLineWidth;
116
117 SkRect fActualBounds;
118
119 // The "turtle" state
120 SkPoint fCurPos;
121 int fCurDir;
122
123 const float fExpectedLen;
124 float fCurLen;
125};
126
127/** Create pixmaps to initialize a 32x32 image w/ or w/o mipmaps.
128 * Returns the number of levels (either 1 or 6). The mipmap levels will be colored as
129 * specified in 'colors'
130 */
133 bool withMips,
134 const SkColor4f colors[6],
135 SkPixmap pixmaps[6],
136 std::unique_ptr<char[]>* mem);
137
138// If the canvas doesn't make a surface (e.g. recording), make a raster surface
140
141// A helper for inserting a drawtext call into a SkTextBlobBuilder
143 const char* text,
144 size_t len,
146 const SkFont&,
147 SkScalar x,
148 SkScalar y);
149
150void add_to_text_blob(SkTextBlobBuilder*, const char* text, const SkFont&, SkScalar x, SkScalar y);
151
152// Constructs a star by walking a 'numPts'-sided regular polygon with even/odd fill:
153//
154// moveTo(pts[0]);
155// lineTo(pts[step % numPts]);
156// ...
157// lineTo(pts[(step * (N - 1)) % numPts]);
158//
159// numPts=5, step=2 will produce a classic five-point star.
160//
161// numPts and step must be co-prime.
162SkPath make_star(const SkRect& bounds, int numPts = 5, int step = 2);
163
165
167
169
170// A helper object to test the topological sorting code (TopoSortBench.cpp & TopoSortTest.cpp)
171class TopoTestNode : public SkRefCnt {
172public:
173 TopoTestNode(int id) : fID(id) {}
174
175 void dependsOn(TopoTestNode* src) { *fDependencies.append() = src; }
176 void targets(uint32_t target) { *fTargets.append() = target; }
177
178 int id() const { return fID; }
179 void reset() {
180 fOutputPos = 0;
181 fTempMark = false;
182 fWasOutput = false;
183 }
184
185 uint32_t outputPos() const {
186 SkASSERT(fWasOutput);
187 return fOutputPos;
188 }
189
190 // check that the topological sort is valid for this node
191 bool check() {
192 if (!fWasOutput) {
193 return false;
194 }
195
196 for (int i = 0; i < fDependencies.size(); ++i) {
197 if (!fDependencies[i]->fWasOutput) {
198 return false;
199 }
200 // This node should've been output after all the nodes on which it depends
201 if (fOutputPos < fDependencies[i]->outputPos()) {
202 return false;
203 }
204 }
205
206 return true;
207 }
208
209 // The following 7 methods are needed by the topological sort
210 static void SetTempMark(TopoTestNode* node) { node->fTempMark = true; }
211 static void ResetTempMark(TopoTestNode* node) { node->fTempMark = false; }
212 static bool IsTempMarked(TopoTestNode* node) { return node->fTempMark; }
213 static void Output(TopoTestNode* node, uint32_t outputPos) {
214 SkASSERT(!node->fWasOutput);
215 node->fOutputPos = outputPos;
216 node->fWasOutput = true;
217 }
218 static bool WasOutput(TopoTestNode* node) { return node->fWasOutput; }
219 static uint32_t GetIndex(TopoTestNode* node) { return node->outputPos(); }
220 static int NumDependencies(TopoTestNode* node) { return node->fDependencies.size(); }
221 static TopoTestNode* Dependency(TopoTestNode* node, int index) {
222 return node->fDependencies[index];
223 }
224 static int NumTargets(TopoTestNode* node) { return node->fTargets.size(); }
225 static uint32_t GetTarget(TopoTestNode* node, int i) { return node->fTargets[i]; }
226 static uint32_t GetID(TopoTestNode* node) { return node->id(); }
227
228 // Helper functions for TopoSortBench & TopoSortTest
230 graph->reserve_exact(graph->size() + num);
231
232 for (int i = 0; i < num; ++i) {
233 graph->push_back(sk_sp<TopoTestNode>(new TopoTestNode(i)));
234 }
235 }
236
237#ifdef SK_DEBUG
238 static void Print(const skia_private::TArray<TopoTestNode*>& graph) {
239 for (int i = 0; i < graph.size(); ++i) {
240 SkDebugf("%d, ", graph[i]->id());
241 }
242 SkDebugf("\n");
243 }
244#endif
245
246 // randomize the array
247 static void Shuffle(SkSpan<sk_sp<TopoTestNode>> graph, SkRandom* rand) {
248 for (size_t i = graph.size() - 1; i > 0; --i) {
249 int swap = rand->nextU() % (i + 1);
250
251 graph[i].swap(graph[swap]);
252 }
253 }
254
256
257private:
258 int fID;
259 uint32_t fOutputPos = 0;
260 bool fTempMark = false;
261 bool fWasOutput = false;
262
263 SkTDArray<TopoTestNode*> fDependencies;
264 SkTDArray<uint32_t> fTargets;
265};
266
267bool copy_to(SkBitmap* dst, SkColorType dstCT, const SkBitmap& src);
268void copy_to_g8(SkBitmap* dst, const SkBitmap& src);
269
271public:
274 SkPixmap pm;
275 if (!surf->peekPixels(&pm)) {
276 pm.reset();
277 }
278 this->reset(pm);
279 }
280
281 void reset(const SkPixmap& pm) {
282 fPM = pm;
283 fLoc = {-1, 0};
284 }
285
286 void* next(SkIPoint* loc = nullptr) {
287 if (!fPM.addr()) {
288 return nullptr;
289 }
290 fLoc.fX += 1;
291 if (fLoc.fX >= fPM.width()) {
292 fLoc.fX = 0;
293 if (++fLoc.fY >= fPM.height()) {
294 this->setDone();
295 return nullptr;
296 }
297 }
298 if (loc) {
299 *loc = fLoc;
300 }
301 return fPM.writable_addr(fLoc.fX, fLoc.fY);
302 }
303
304 void setDone() { fPM.reset(); }
305
306private:
307 SkPixmap fPM;
308 SkIPoint fLoc;
309};
310
311using PathSniffCallback = void(const SkMatrix&, const SkPath&, const SkPaint&);
312
313// Calls the provided PathSniffCallback for each path in the given file.
314// Supported file formats .skp. (See SvgPathExtractor for .svg)
315void ExtractPathsFromSKP(const char filepath[], std::function<PathSniffCallback>);
316
317// Initialised with a font, this class can be called to setup GM UI with sliders for font
318// variations, and returns a set of variation coordinates that matches what the sliders in the UI
319// are set to. Useful for testing variable font properties, see colrv1.cpp.
321public:
323
325 SkFontArguments::VariationPosition variationPosition = {nullptr, 0});
326
327 bool writeControls(SkMetaData* controls);
328
329 /* Scans controls for information about the variation axes that the user may have configured.
330 * Optionally pass in a boolean to receive information on whether the axes were updated. */
331 void readControls(const SkMetaData& controls, bool* changed = nullptr);
332
334
336
337private:
338 struct AxisSlider {
339 SkScalar current;
342 };
343
344 std::vector<AxisSlider> fAxisSliders;
345 std::unique_ptr<SkFontArguments::VariationPosition::Coordinate[]> fCoords;
346 static constexpr size_t kAxisVarsSize = 3;
347};
348
349} // namespace ToolUtils
350
351#endif // ToolUtils_DEFINED
static int step(int x, SkScalar min, SkScalar max)
Definition: BlurTest.cpp:215
SkAlphaType
Definition: SkAlphaType.h:26
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkColorType
Definition: SkColorType.h:19
uint32_t SkColor
Definition: SkColor.h:37
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
SkTextEncoding
Definition: SkFontTypes.h:11
void swap(sk_sp< T > &a, sk_sp< T > &b)
Definition: SkRefCnt.h:341
SkTileMode
Definition: SkTileMode.h:13
uint32_t SkFourByteTag
Definition: SkTypes.h:166
Definition: SkFont.h:35
Definition: SkPath.h:59
int width() const
Definition: SkPixmap.h:160
void * writable_addr() const
Definition: SkPixmap.h:483
const void * addr() const
Definition: SkPixmap.h:153
int height() const
Definition: SkPixmap.h:166
void reset()
Definition: SkPixmap.cpp:32
uint32_t nextU()
Definition: SkRandom.h:42
bool peekPixels(SkPixmap *pixmap)
Definition: SkSurface.cpp:121
int size() const
Definition: SkTDArray.h:138
T * append()
Definition: SkTDArray.h:191
void draw(SkCanvas *canvas)
Definition: ToolUtils.cpp:618
HilbertGenerator(float desiredSize, float desiredLineWidth, int desiredDepth)
Definition: ToolUtils.cpp:606
void reset(const SkPixmap &pm)
Definition: ToolUtils.h:281
PixelIter(SkSurface *surf)
Definition: ToolUtils.h:273
void * next(SkIPoint *loc=nullptr)
Definition: ToolUtils.h:286
static void ResetTempMark(TopoTestNode *node)
Definition: ToolUtils.h:211
SK_DECLARE_INTERNAL_LLIST_INTERFACE(TopoTestNode)
static uint32_t GetIndex(TopoTestNode *node)
Definition: ToolUtils.h:219
void targets(uint32_t target)
Definition: ToolUtils.h:176
static uint32_t GetID(TopoTestNode *node)
Definition: ToolUtils.h:226
static void Output(TopoTestNode *node, uint32_t outputPos)
Definition: ToolUtils.h:213
static bool WasOutput(TopoTestNode *node)
Definition: ToolUtils.h:218
static void AllocNodes(skia_private::TArray< sk_sp< ToolUtils::TopoTestNode > > *graph, int num)
Definition: ToolUtils.h:229
static int NumTargets(TopoTestNode *node)
Definition: ToolUtils.h:224
void dependsOn(TopoTestNode *src)
Definition: ToolUtils.h:175
static int NumDependencies(TopoTestNode *node)
Definition: ToolUtils.h:220
static bool IsTempMarked(TopoTestNode *node)
Definition: ToolUtils.h:212
static TopoTestNode * Dependency(TopoTestNode *node, int index)
Definition: ToolUtils.h:221
uint32_t outputPos() const
Definition: ToolUtils.h:185
static void Shuffle(SkSpan< sk_sp< TopoTestNode > > graph, SkRandom *rand)
Definition: ToolUtils.h:247
static void SetTempMark(TopoTestNode *node)
Definition: ToolUtils.h:210
static uint32_t GetTarget(TopoTestNode *node, int i)
Definition: ToolUtils.h:225
SkSpan< const SkFontArguments::VariationPosition::Coordinate > getCoordinates()
Definition: ToolUtils.cpp:600
void readControls(const SkMetaData &controls, bool *changed=nullptr)
Definition: ToolUtils.cpp:585
static SkString tagToString(SkFourByteTag tag)
Definition: ToolUtils.cpp:563
bool writeControls(SkMetaData *controls)
Definition: ToolUtils.cpp:573
int size() const
Definition: SkTArray.h:421
void swap(TArray &that)
Definition: SkTArray.h:358
DlColor color
float SkScalar
Definition: extension.cpp:12
static bool b
struct MyStruct a[10]
uint32_t * target
size_t length
std::u16string text
double y
double x
Optional< SkRect > bounds
Definition: SkRecords.h:189
PODArray< SkColor > colors
Definition: SkRecords.h:276
void draw_checkerboard(SkCanvas *canvas, SkColor c1, SkColor c2, int size)
Definition: ToolUtils.cpp:174
sk_sp< SkSurface > makeSurface(SkCanvas *canvas, const SkImageInfo &info, const SkSurfaceProps *props)
Definition: ToolUtils.cpp:512
sk_sp< SkShader > create_checkerboard_shader(SkColor c1, SkColor c2, int size)
Definition: ToolUtils.cpp:150
void copy_to_g8(SkBitmap *dst, const SkBitmap &src)
Definition: ToolUtils.cpp:423
bool copy_to(SkBitmap *dst, SkColorType dstColorType, const SkBitmap &src)
Definition: ToolUtils.cpp:394
int make_pixmaps(SkColorType ct, SkAlphaType at, bool withMips, const SkColor4f colors[6], SkPixmap pixmaps[6], std::unique_ptr< char[]> *mem)
Definition: ToolUtils.cpp:181
const char * colortype_depth(SkColorType ct)
Definition: ToolUtils.cpp:97
void add_to_text_blob_w_len(SkTextBlobBuilder *builder, const char *text, size_t len, SkTextEncoding encoding, const SkFont &font, SkScalar x, SkScalar y)
Definition: ToolUtils.cpp:213
void add_to_text_blob(SkTextBlobBuilder *builder, const char *text, const SkFont &font, SkScalar x, SkScalar y)
Definition: ToolUtils.cpp:228
void create_frustum_normal_map(SkBitmap *bm, const SkIRect &dst)
Definition: ToolUtils.cpp:318
void get_text_path(const SkFont &font, const void *text, size_t length, SkTextEncoding encoding, SkPath *dst, const SkPoint pos[])
Definition: ToolUtils.cpp:236
const char * colortype_name(SkColorType ct)
Definition: ToolUtils.cpp:65
void ExtractPathsFromSKP(const char filepath[], std::function< PathSniffCallback > callback)
Definition: ToolUtils.cpp:757
void(const SkMatrix &, const SkPath &, const SkPaint &) PathSniffCallback
Definition: ToolUtils.h:311
SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize)
Definition: ToolUtils.cpp:159
const char * alphatype_name(SkAlphaType at)
Definition: ToolUtils.cpp:55
bool equal_pixels(const SkPixmap &a, const SkPixmap &b)
Definition: ToolUtils.cpp:456
const char * tilemode_name(SkTileMode mode)
Definition: ToolUtils.cpp:129
SkPath make_star(const SkRect &bounds, int numPts, int step)
Definition: ToolUtils.cpp:269
sk_sp< SkImage > create_checkerboard_image(int w, int h, SkColor c1, SkColor c2, int checkSize)
Definition: ToolUtils.cpp:168
void create_tetra_normal_map(SkBitmap *bm, const SkIRect &dst)
Definition: ToolUtils.cpp:359
void create_hemi_normal_map(SkBitmap *bm, const SkIRect &dst)
Definition: ToolUtils.cpp:294
SkColor color_to_565(SkColor color)
Definition: ToolUtils.cpp:139
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
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
dst
Definition: cp.py:12
SkScalar w
SkScalar h
int32_t fX
x-axis value
Definition: SkPoint_impl.h:29
int32_t fY
y-axis value
Definition: SkPoint_impl.h:30
Definition: SkRect.h:32