blob: e655e8d2ec7b46decbbda79b77c4ea6a4623fbd2 [file] [log] [blame]
Mike Kleinea3f0142019-03-20 11:12:10 -05001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkColor.h"
12#include "include/core/SkData.h"
13#include "include/core/SkEncodedImageFormat.h"
14#include "include/core/SkFont.h"
15#include "include/core/SkFontStyle.h"
16#include "include/core/SkFontTypes.h"
17#include "include/core/SkImageEncoder.h"
18#include "include/core/SkImageInfo.h"
19#include "include/core/SkPixmap.h"
20#include "include/core/SkRect.h"
21#include "include/core/SkRefCnt.h"
22#include "include/core/SkScalar.h"
23#include "include/core/SkStream.h"
24#include "include/core/SkSurface.h"
25#include "include/core/SkTypeface.h"
26#include "include/core/SkTypes.h"
27#include "include/private/SkTArray.h"
28#include "include/private/SkTDArray.h"
29#include "include/utils/SkRandom.h"
Adlai Holler78036082021-01-07 11:41:33 -050030#include "src/core/SkTInternalLList.h"
Mike Kleinea3f0142019-03-20 11:12:10 -050031
32class SkBitmap;
33class SkCanvas;
34class SkFontStyle;
35class SkImage;
36class SkPath;
37class SkPixmap;
38class SkRRect;
39class SkShader;
40class SkSurface;
41class SkSurfaceProps;
42class SkTextBlobBuilder;
43class SkTypeface;
44
45namespace ToolUtils {
46
Mike Klein98168782019-04-09 13:45:02 -050047const char* alphatype_name (SkAlphaType);
48const char* colortype_name (SkColorType);
49const char* colortype_depth(SkColorType); // like colortype_name, but channel order agnostic
Michael Ludwig23003182019-08-05 11:25:23 -040050const char* tilemode_name(SkTileMode);
Mike Kleinea3f0142019-03-20 11:12:10 -050051
52/**
53 * Map opaque colors from 8888 to 565.
54 */
55SkColor color_to_565(SkColor color);
56
57/* Return a color emoji typeface with planets to scale if available. */
58sk_sp<SkTypeface> planet_typeface();
59
60/** Return a color emoji typeface if available. */
61sk_sp<SkTypeface> emoji_typeface();
62
63/** Sample text for the emoji_typeface font. */
64const char* emoji_sample_text();
65
Ben Wagner81eabce2020-08-18 13:17:09 -040066/** A simple SkUserTypeface for testing. */
67sk_sp<SkTypeface> sample_user_typeface();
68
Mike Kleinea3f0142019-03-20 11:12:10 -050069/**
70 * Returns a platform-independent text renderer.
71 */
72sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style);
73
74static inline sk_sp<SkTypeface> create_portable_typeface() {
75 return create_portable_typeface(nullptr, SkFontStyle());
76}
77
Mike Klein19cc0f62019-03-22 15:30:07 -050078/**
79 * Turn on portable (--nonativeFonts) or GDI font rendering (--gdi).
80 */
81void SetDefaultFontMgr();
82
83
Mike Kleinea3f0142019-03-20 11:12:10 -050084void get_text_path(const SkFont&,
85 const void* text,
86 size_t length,
87 SkTextEncoding,
88 SkPath*,
89 const SkPoint* positions = nullptr);
90
91/**
Mike Kleinea3f0142019-03-20 11:12:10 -050092 * Returns true iff all of the pixels between the two images are identical.
93 *
94 * If the configs differ, return false.
95 */
96bool equal_pixels(const SkPixmap&, const SkPixmap&);
97bool equal_pixels(const SkBitmap&, const SkBitmap&);
98bool equal_pixels(const SkImage* a, const SkImage* b);
99
100/** Returns a newly created CheckerboardShader. */
101sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size);
102
103/** Draw a checkerboard pattern in the current canvas, restricted to
104 the current clip, using SkXfermode::kSrc_Mode. */
105void draw_checkerboard(SkCanvas* canvas, SkColor color1, SkColor color2, int checkSize);
106
107/** Make it easier to create a bitmap-based checkerboard */
108SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize);
109
Mike Reeddb873dd2020-12-04 12:22:10 -0500110sk_sp<SkImage> create_checkerboard_image(int w, int h, SkColor c1, SkColor c2, int checkSize);
111
Mike Kleinea3f0142019-03-20 11:12:10 -0500112/** A default checkerboard. */
113inline void draw_checkerboard(SkCanvas* canvas) {
114 ToolUtils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
115}
116
117SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y, int textSize, const char* str);
Mike Reedac9f0c92020-12-23 10:11:33 -0500118sk_sp<SkImage> create_string_image(int w, int h, SkColor c, int x, int y, int textSize, const char* str);
Mike Kleinea3f0142019-03-20 11:12:10 -0500119
120// If the canvas does't make a surface (e.g. recording), make a raster surface
121sk_sp<SkSurface> makeSurface(SkCanvas*, const SkImageInfo&, const SkSurfaceProps* = nullptr);
122
123// A helper for inserting a drawtext call into a SkTextBlobBuilder
124void add_to_text_blob_w_len(SkTextBlobBuilder*,
125 const char* text,
126 size_t len,
127 SkTextEncoding,
128 const SkFont&,
129 SkScalar x,
130 SkScalar y);
131
132void add_to_text_blob(SkTextBlobBuilder*, const char* text, const SkFont&, SkScalar x, SkScalar y);
133
134// Constructs a star by walking a 'numPts'-sided regular polygon with even/odd fill:
135//
136// moveTo(pts[0]);
137// lineTo(pts[step % numPts]);
138// ...
139// lineTo(pts[(step * (N - 1)) % numPts]);
140//
141// numPts=5, step=2 will produce a classic five-point star.
142//
143// numPts and step must be co-prime.
144SkPath make_star(const SkRect& bounds, int numPts = 5, int step = 2);
145
146void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst);
147
148void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst);
149
150void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst);
151
Mike Reed92f6eb12020-08-25 11:48:41 -0400152SkPath make_big_path();
Mike Kleinea3f0142019-03-20 11:12:10 -0500153
154// A helper object to test the topological sorting code (TopoSortBench.cpp & TopoSortTest.cpp)
155class TopoTestNode : public SkRefCnt {
156public:
Robert Phillips4dbff752020-11-18 12:16:31 -0500157 TopoTestNode(int id) : fID(id) {}
Mike Kleinea3f0142019-03-20 11:12:10 -0500158
159 void dependsOn(TopoTestNode* src) { *fDependencies.append() = src; }
Adlai Holler78036082021-01-07 11:41:33 -0500160 void targets(uint32_t target) { *fTargets.append() = target; }
Mike Kleinea3f0142019-03-20 11:12:10 -0500161
162 int id() const { return fID; }
Robert Phillips4dbff752020-11-18 12:16:31 -0500163 void reset() {
164 fOutputPos = 0;
165 fTempMark = false;
166 fWasOutput = false;
167 }
Mike Kleinea3f0142019-03-20 11:12:10 -0500168
Robert Phillips4dbff752020-11-18 12:16:31 -0500169 uint32_t outputPos() const {
170 SkASSERT(fWasOutput);
171 return fOutputPos;
172 }
Mike Kleinea3f0142019-03-20 11:12:10 -0500173
174 // check that the topological sort is valid for this node
175 bool check() {
Robert Phillips4dbff752020-11-18 12:16:31 -0500176 if (!fWasOutput) {
Mike Kleinea3f0142019-03-20 11:12:10 -0500177 return false;
178 }
179
180 for (int i = 0; i < fDependencies.count(); ++i) {
Robert Phillips4dbff752020-11-18 12:16:31 -0500181 if (!fDependencies[i]->fWasOutput) {
Mike Kleinea3f0142019-03-20 11:12:10 -0500182 return false;
183 }
184 // This node should've been output after all the nodes on which it depends
185 if (fOutputPos < fDependencies[i]->outputPos()) {
186 return false;
187 }
188 }
189
190 return true;
191 }
192
193 // The following 7 methods are needed by the topological sort
194 static void SetTempMark(TopoTestNode* node) { node->fTempMark = true; }
195 static void ResetTempMark(TopoTestNode* node) { node->fTempMark = false; }
196 static bool IsTempMarked(TopoTestNode* node) { return node->fTempMark; }
Robert Phillips4dbff752020-11-18 12:16:31 -0500197 static void Output(TopoTestNode* node, uint32_t outputPos) {
198 SkASSERT(!node->fWasOutput);
Mike Kleinea3f0142019-03-20 11:12:10 -0500199 node->fOutputPos = outputPos;
Robert Phillips4dbff752020-11-18 12:16:31 -0500200 node->fWasOutput = true;
Mike Kleinea3f0142019-03-20 11:12:10 -0500201 }
Robert Phillips4dbff752020-11-18 12:16:31 -0500202 static bool WasOutput(TopoTestNode* node) { return node->fWasOutput; }
203 static uint32_t GetIndex(TopoTestNode* node) { return node->outputPos(); }
Mike Kleinea3f0142019-03-20 11:12:10 -0500204 static int NumDependencies(TopoTestNode* node) { return node->fDependencies.count(); }
205 static TopoTestNode* Dependency(TopoTestNode* node, int index) {
206 return node->fDependencies[index];
207 }
Adlai Holler78036082021-01-07 11:41:33 -0500208 static int NumTargets(TopoTestNode* node) { return node->fTargets.count(); }
209 static uint32_t GetTarget(TopoTestNode* node, int i) { return node->fTargets[i]; }
210 static uint32_t GetID(TopoTestNode* node) { return node->id(); }
Mike Kleinea3f0142019-03-20 11:12:10 -0500211
212 // Helper functions for TopoSortBench & TopoSortTest
213 static void AllocNodes(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph, int num) {
John Stilesf4bda742020-10-14 16:57:41 -0400214 graph->reserve_back(num);
Mike Kleinea3f0142019-03-20 11:12:10 -0500215
216 for (int i = 0; i < num; ++i) {
217 graph->push_back(sk_sp<TopoTestNode>(new TopoTestNode(i)));
218 }
219 }
220
221#ifdef SK_DEBUG
222 static void Print(const SkTArray<TopoTestNode*>& graph) {
223 for (int i = 0; i < graph.count(); ++i) {
224 SkDebugf("%d, ", graph[i]->id());
225 }
226 SkDebugf("\n");
227 }
228#endif
229
230 // randomize the array
231 static void Shuffle(SkTArray<sk_sp<TopoTestNode>>* graph, SkRandom* rand) {
232 for (int i = graph->count() - 1; i > 0; --i) {
233 int swap = rand->nextU() % (i + 1);
234
235 (*graph)[i].swap((*graph)[swap]);
236 }
237 }
238
Adlai Holler78036082021-01-07 11:41:33 -0500239 SK_DECLARE_INTERNAL_LLIST_INTERFACE(TopoTestNode);
240
Mike Kleinea3f0142019-03-20 11:12:10 -0500241private:
Robert Phillips4dbff752020-11-18 12:16:31 -0500242 int fID;
243 uint32_t fOutputPos = 0;
244 bool fTempMark = false;
245 bool fWasOutput = false;
Mike Kleinea3f0142019-03-20 11:12:10 -0500246
247 SkTDArray<TopoTestNode*> fDependencies;
Adlai Holler78036082021-01-07 11:41:33 -0500248 SkTDArray<uint32_t> fTargets;
Mike Kleinea3f0142019-03-20 11:12:10 -0500249};
250
251template <typename T>
252inline bool EncodeImageToFile(const char* path, const T& src, SkEncodedImageFormat f, int q) {
253 SkFILEWStream file(path);
254 return file.isValid() && SkEncodeImage(&file, src, f, q);
255}
256
257bool copy_to(SkBitmap* dst, SkColorType dstCT, const SkBitmap& src);
258void copy_to_g8(SkBitmap* dst, const SkBitmap& src);
Mike Klein04988572019-03-20 11:43:12 -0500259
260class PixelIter {
261public:
262 PixelIter();
263 PixelIter(SkSurface* surf) {
264 SkPixmap pm;
265 if (!surf->peekPixels(&pm)) {
266 pm.reset();
267 }
268 this->reset(pm);
269 }
270
271 void reset(const SkPixmap& pm) {
272 fPM = pm;
273 fLoc = {-1, 0};
274 }
275
276 void* next(SkIPoint* loc = nullptr) {
277 if (!fPM.addr()) {
278 return nullptr;
279 }
280 fLoc.fX += 1;
281 if (fLoc.fX >= fPM.width()) {
282 fLoc.fX = 0;
283 if (++fLoc.fY >= fPM.height()) {
284 this->setDone();
285 return nullptr;
286 }
287 }
288 if (loc) {
289 *loc = fLoc;
290 }
291 return fPM.writable_addr(fLoc.fX, fLoc.fY);
292 }
293
294 void setDone() { fPM.reset(); }
295
296private:
297 SkPixmap fPM;
298 SkIPoint fLoc;
299};
300
Mike Kleinea3f0142019-03-20 11:12:10 -0500301} // namespace ToolUtils
302
303#endif // ToolUtils_DEFINED