Debian Codegraph @

tools/ToolUtils.h

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