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