2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef ToolUtils_DEFINED
9 #define ToolUtils_DEFINED
11 #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"
30 #include "src/core/SkTInternalLList.h"
42 class SkTextBlobBuilder;
47 const char* alphatype_name (SkAlphaType);
48 const char* colortype_name (SkColorType);
49 const char* colortype_depth(SkColorType); // like colortype_name, but channel order agnostic
50 const char* tilemode_name(SkTileMode);
53 * Map opaque colors from 8888 to 565.
55 SkColor color_to_565(SkColor color);
57 /* Return a color emoji typeface with planets to scale if available. */
58 sk_sp<SkTypeface> planet_typeface();
60 /** Return a color emoji typeface if available. */
61 sk_sp<SkTypeface> emoji_typeface();
63 /** Sample text for the emoji_typeface font. */
64 constexpr const char* emoji_sample_text() {
65 return "\xF0\x9F\x98\x80"
67 "\xE2\x99\xA2"; // 😀 ♢
70 /** A simple SkUserTypeface for testing. */
71 sk_sp<SkTypeface> sample_user_typeface();
74 * Returns a platform-independent text renderer.
76 sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style);
78 static inline sk_sp<SkTypeface> create_portable_typeface() {
79 return create_portable_typeface(nullptr, SkFontStyle());
82 void get_text_path(const SkFont&,
87 const SkPoint* positions = nullptr);
90 * Returns true iff all of the pixels between the two images are identical.
92 * If the configs differ, return false.
94 bool equal_pixels(const SkPixmap&, const SkPixmap&);
95 bool equal_pixels(const SkBitmap&, const SkBitmap&);
96 bool equal_pixels(const SkImage* a, const SkImage* b);
98 /** Returns a newly created CheckerboardShader. */
99 sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size);
101 /** Draw a checkerboard pattern in the current canvas, restricted to
102 the current clip, using SkXfermode::kSrc_Mode. */
103 void draw_checkerboard(SkCanvas* canvas, SkColor color1, SkColor color2, int checkSize);
105 /** Make it easier to create a bitmap-based checkerboard */
106 SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize);
108 sk_sp<SkImage> create_checkerboard_image(int w, int h, SkColor c1, SkColor c2, int checkSize);
110 /** A default checkerboard. */
111 inline void draw_checkerboard(SkCanvas* canvas) {
112 ToolUtils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
115 SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y, int textSize, const char* str);
116 sk_sp<SkImage> create_string_image(int w, int h, SkColor c, int x, int y, int textSize, const char* str);
118 // If the canvas does't make a surface (e.g. recording), make a raster surface
119 sk_sp<SkSurface> makeSurface(SkCanvas*, const SkImageInfo&, const SkSurfaceProps* = nullptr);
121 // A helper for inserting a drawtext call into a SkTextBlobBuilder
122 void add_to_text_blob_w_len(SkTextBlobBuilder*,
130 void add_to_text_blob(SkTextBlobBuilder*, const char* text, const SkFont&, SkScalar x, SkScalar y);
132 // Constructs a star by walking a 'numPts'-sided regular polygon with even/odd fill:
135 // lineTo(pts[step % numPts]);
137 // lineTo(pts[(step * (N - 1)) % numPts]);
139 // numPts=5, step=2 will produce a classic five-point star.
141 // numPts and step must be co-prime.
142 SkPath make_star(const SkRect& bounds, int numPts = 5, int step = 2);
144 void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst);
146 void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst);
148 void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst);
150 // A helper object to test the topological sorting code (TopoSortBench.cpp & TopoSortTest.cpp)
151 class TopoTestNode : public SkRefCnt {
153 TopoTestNode(int id) : fID(id) {}
155 void dependsOn(TopoTestNode* src) { *fDependencies.append() = src; }
156 void targets(uint32_t target) { *fTargets.append() = target; }
158 int id() const { return fID; }
165 uint32_t outputPos() const {
166 SkASSERT(fWasOutput);
170 // check that the topological sort is valid for this node
176 for (int i = 0; i < fDependencies.count(); ++i) {
177 if (!fDependencies[i]->fWasOutput) {
180 // This node should've been output after all the nodes on which it depends
181 if (fOutputPos < fDependencies[i]->outputPos()) {
189 // The following 7 methods are needed by the topological sort
190 static void SetTempMark(TopoTestNode* node) { node->fTempMark = true; }
191 static void ResetTempMark(TopoTestNode* node) { node->fTempMark = false; }
192 static bool IsTempMarked(TopoTestNode* node) { return node->fTempMark; }
193 static void Output(TopoTestNode* node, uint32_t outputPos) {
194 SkASSERT(!node->fWasOutput);
195 node->fOutputPos = outputPos;
196 node->fWasOutput = true;
198 static bool WasOutput(TopoTestNode* node) { return node->fWasOutput; }
199 static uint32_t GetIndex(TopoTestNode* node) { return node->outputPos(); }
200 static int NumDependencies(TopoTestNode* node) { return node->fDependencies.count(); }
201 static TopoTestNode* Dependency(TopoTestNode* node, int index) {
202 return node->fDependencies[index];
204 static int NumTargets(TopoTestNode* node) { return node->fTargets.count(); }
205 static uint32_t GetTarget(TopoTestNode* node, int i) { return node->fTargets[i]; }
206 static uint32_t GetID(TopoTestNode* node) { return node->id(); }
208 // Helper functions for TopoSortBench & TopoSortTest
209 static void AllocNodes(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph, int num) {
210 graph->reserve_back(num);
212 for (int i = 0; i < num; ++i) {
213 graph->push_back(sk_sp<TopoTestNode>(new TopoTestNode(i)));
218 static void Print(const SkTArray<TopoTestNode*>& graph) {
219 for (int i = 0; i < graph.count(); ++i) {
220 SkDebugf("%d, ", graph[i]->id());
226 // randomize the array
227 static void Shuffle(SkTArray<sk_sp<TopoTestNode>>* graph, SkRandom* rand) {
228 for (int i = graph->count() - 1; i > 0; --i) {
229 int swap = rand->nextU() % (i + 1);
231 (*graph)[i].swap((*graph)[swap]);
235 SK_DECLARE_INTERNAL_LLIST_INTERFACE(TopoTestNode);
239 uint32_t fOutputPos = 0;
240 bool fTempMark = false;
241 bool fWasOutput = false;
243 SkTDArray<TopoTestNode*> fDependencies;
244 SkTDArray<uint32_t> fTargets;
247 template <typename T>
248 inline bool EncodeImageToFile(const char* path, const T& src, SkEncodedImageFormat f, int q) {
249 SkFILEWStream file(path);
250 return file.isValid() && SkEncodeImage(&file, src, f, q);
253 bool copy_to(SkBitmap* dst, SkColorType dstCT, const SkBitmap& src);
254 void copy_to_g8(SkBitmap* dst, const SkBitmap& src);
259 PixelIter(SkSurface* surf) {
261 if (!surf->peekPixels(&pm)) {
267 void reset(const SkPixmap& pm) {
272 void* next(SkIPoint* loc = nullptr) {
277 if (fLoc.fX >= fPM.width()) {
279 if (++fLoc.fY >= fPM.height()) {
287 return fPM.writable_addr(fLoc.fX, fLoc.fY);
290 void setDone() { fPM.reset(); }
297 using PathSniffCallback = void(const SkMatrix&, const SkPath&, const SkPaint&);
299 // Calls the provided PathSniffCallback for each path in the given file.
300 // Supported file formats are .svg and .skp.
301 void sniff_paths(const char filepath[], std::function<PathSniffCallback>);
304 sk_sp<SkImage> MakeTextureImage(SkCanvas* canvas, sk_sp<SkImage> orig);
307 } // namespace ToolUtils
309 #endif // ToolUtils_DEFINED