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