blob: 0a5ecc6f78c0bf6bf46eda26e7cae6c6f73a5dd4 [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"
Mike Kleinea3f0142019-03-20 11:12:10 -050030
31class SkBitmap;
32class SkCanvas;
33class SkFontStyle;
34class SkImage;
35class SkPath;
36class SkPixmap;
37class SkRRect;
38class SkShader;
39class SkSurface;
40class SkSurfaceProps;
41class SkTextBlobBuilder;
42class SkTypeface;
43
44namespace ToolUtils {
45
Mike Klein98168782019-04-09 13:45:02 -050046const char* alphatype_name (SkAlphaType);
47const char* colortype_name (SkColorType);
48const char* colortype_depth(SkColorType); // like colortype_name, but channel order agnostic
Michael Ludwig23003182019-08-05 11:25:23 -040049const char* tilemode_name(SkTileMode);
Mike Kleinea3f0142019-03-20 11:12:10 -050050
51/**
52 * Map opaque colors from 8888 to 565.
53 */
54SkColor color_to_565(SkColor color);
55
56/* Return a color emoji typeface with planets to scale if available. */
57sk_sp<SkTypeface> planet_typeface();
58
59/** Return a color emoji typeface if available. */
60sk_sp<SkTypeface> emoji_typeface();
61
62/** Sample text for the emoji_typeface font. */
63const char* emoji_sample_text();
64
Ben Wagner81eabce2020-08-18 13:17:09 -040065/** A simple SkUserTypeface for testing. */
66sk_sp<SkTypeface> sample_user_typeface();
67
Mike Kleinea3f0142019-03-20 11:12:10 -050068/**
69 * Returns a platform-independent text renderer.
70 */
71sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style);
72
73static inline sk_sp<SkTypeface> create_portable_typeface() {
74 return create_portable_typeface(nullptr, SkFontStyle());
75}
76
Mike Klein19cc0f62019-03-22 15:30:07 -050077/**
78 * Turn on portable (--nonativeFonts) or GDI font rendering (--gdi).
79 */
80void SetDefaultFontMgr();
81
82
Mike Kleinea3f0142019-03-20 11:12:10 -050083void get_text_path(const SkFont&,
84 const void* text,
85 size_t length,
86 SkTextEncoding,
87 SkPath*,
88 const SkPoint* positions = nullptr);
89
90/**
Mike Kleinea3f0142019-03-20 11:12:10 -050091 * Returns true iff all of the pixels between the two images are identical.
92 *
93 * If the configs differ, return false.
94 */
95bool equal_pixels(const SkPixmap&, const SkPixmap&);
96bool equal_pixels(const SkBitmap&, const SkBitmap&);
97bool equal_pixels(const SkImage* a, const SkImage* b);
98
99/** Returns a newly created CheckerboardShader. */
100sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size);
101
102/** Draw a checkerboard pattern in the current canvas, restricted to
103 the current clip, using SkXfermode::kSrc_Mode. */
104void draw_checkerboard(SkCanvas* canvas, SkColor color1, SkColor color2, int checkSize);
105
106/** Make it easier to create a bitmap-based checkerboard */
107SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize);
108
109/** A default checkerboard. */
110inline void draw_checkerboard(SkCanvas* canvas) {
111 ToolUtils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
112}
113
114SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y, int textSize, const char* str);
115
116// If the canvas does't make a surface (e.g. recording), make a raster surface
117sk_sp<SkSurface> makeSurface(SkCanvas*, const SkImageInfo&, const SkSurfaceProps* = nullptr);
118
119// A helper for inserting a drawtext call into a SkTextBlobBuilder
120void add_to_text_blob_w_len(SkTextBlobBuilder*,
121 const char* text,
122 size_t len,
123 SkTextEncoding,
124 const SkFont&,
125 SkScalar x,
126 SkScalar y);
127
128void add_to_text_blob(SkTextBlobBuilder*, const char* text, const SkFont&, SkScalar x, SkScalar y);
129
130// Constructs a star by walking a 'numPts'-sided regular polygon with even/odd fill:
131//
132// moveTo(pts[0]);
133// lineTo(pts[step % numPts]);
134// ...
135// lineTo(pts[(step * (N - 1)) % numPts]);
136//
137// numPts=5, step=2 will produce a classic five-point star.
138//
139// numPts and step must be co-prime.
140SkPath make_star(const SkRect& bounds, int numPts = 5, int step = 2);
141
142void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst);
143
144void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst);
145
146void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst);
147
Mike Reed92f6eb12020-08-25 11:48:41 -0400148SkPath make_big_path();
Mike Kleinea3f0142019-03-20 11:12:10 -0500149
150// A helper object to test the topological sorting code (TopoSortBench.cpp & TopoSortTest.cpp)
151class TopoTestNode : public SkRefCnt {
152public:
153 TopoTestNode(int id) : fID(id), fOutputPos(-1), fTempMark(false) {}
154
155 void dependsOn(TopoTestNode* src) { *fDependencies.append() = src; }
156
157 int id() const { return fID; }
158 void reset() { fOutputPos = -1; }
159
160 int outputPos() const { return fOutputPos; }
161
162 // check that the topological sort is valid for this node
163 bool check() {
164 if (-1 == fOutputPos) {
165 return false;
166 }
167
168 for (int i = 0; i < fDependencies.count(); ++i) {
169 if (-1 == fDependencies[i]->outputPos()) {
170 return false;
171 }
172 // This node should've been output after all the nodes on which it depends
173 if (fOutputPos < fDependencies[i]->outputPos()) {
174 return false;
175 }
176 }
177
178 return true;
179 }
180
181 // The following 7 methods are needed by the topological sort
182 static void SetTempMark(TopoTestNode* node) { node->fTempMark = true; }
183 static void ResetTempMark(TopoTestNode* node) { node->fTempMark = false; }
184 static bool IsTempMarked(TopoTestNode* node) { return node->fTempMark; }
185 static void Output(TopoTestNode* node, int outputPos) {
186 SkASSERT(-1 != outputPos);
187 node->fOutputPos = outputPos;
188 }
189 static bool WasOutput(TopoTestNode* node) { return (-1 != node->fOutputPos); }
190 static int NumDependencies(TopoTestNode* node) { return node->fDependencies.count(); }
191 static TopoTestNode* Dependency(TopoTestNode* node, int index) {
192 return node->fDependencies[index];
193 }
194
195 // Helper functions for TopoSortBench & TopoSortTest
196 static void AllocNodes(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph, int num) {
197 graph->reserve(num);
198
199 for (int i = 0; i < num; ++i) {
200 graph->push_back(sk_sp<TopoTestNode>(new TopoTestNode(i)));
201 }
202 }
203
204#ifdef SK_DEBUG
205 static void Print(const SkTArray<TopoTestNode*>& graph) {
206 for (int i = 0; i < graph.count(); ++i) {
207 SkDebugf("%d, ", graph[i]->id());
208 }
209 SkDebugf("\n");
210 }
211#endif
212
213 // randomize the array
214 static void Shuffle(SkTArray<sk_sp<TopoTestNode>>* graph, SkRandom* rand) {
215 for (int i = graph->count() - 1; i > 0; --i) {
216 int swap = rand->nextU() % (i + 1);
217
218 (*graph)[i].swap((*graph)[swap]);
219 }
220 }
221
222private:
223 int fID;
224 int fOutputPos;
225 bool fTempMark;
226
227 SkTDArray<TopoTestNode*> fDependencies;
228};
229
230template <typename T>
231inline bool EncodeImageToFile(const char* path, const T& src, SkEncodedImageFormat f, int q) {
232 SkFILEWStream file(path);
233 return file.isValid() && SkEncodeImage(&file, src, f, q);
234}
235
236bool copy_to(SkBitmap* dst, SkColorType dstCT, const SkBitmap& src);
237void copy_to_g8(SkBitmap* dst, const SkBitmap& src);
Mike Klein04988572019-03-20 11:43:12 -0500238
239class PixelIter {
240public:
241 PixelIter();
242 PixelIter(SkSurface* surf) {
243 SkPixmap pm;
244 if (!surf->peekPixels(&pm)) {
245 pm.reset();
246 }
247 this->reset(pm);
248 }
249
250 void reset(const SkPixmap& pm) {
251 fPM = pm;
252 fLoc = {-1, 0};
253 }
254
255 void* next(SkIPoint* loc = nullptr) {
256 if (!fPM.addr()) {
257 return nullptr;
258 }
259 fLoc.fX += 1;
260 if (fLoc.fX >= fPM.width()) {
261 fLoc.fX = 0;
262 if (++fLoc.fY >= fPM.height()) {
263 this->setDone();
264 return nullptr;
265 }
266 }
267 if (loc) {
268 *loc = fLoc;
269 }
270 return fPM.writable_addr(fLoc.fX, fLoc.fY);
271 }
272
273 void setDone() { fPM.reset(); }
274
275private:
276 SkPixmap fPM;
277 SkIPoint fLoc;
278};
279
Mike Kleinea3f0142019-03-20 11:12:10 -0500280} // namespace ToolUtils
281
282#endif // ToolUtils_DEFINED