• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
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"
31 
32 class SkBitmap;
33 class SkCanvas;
34 class SkFontStyle;
35 class SkImage;
36 class SkPath;
37 class SkPixmap;
38 class SkRRect;
39 class SkShader;
40 class SkSurface;
41 class SkSurfaceProps;
42 class SkTextBlobBuilder;
43 class SkTypeface;
44 
45 namespace ToolUtils {
46 
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);
51 
52 /**
53  * Map opaque colors from 8888 to 565.
54  */
55 SkColor color_to_565(SkColor color);
56 
57 /* Return a color emoji typeface with planets to scale if available. */
58 sk_sp<SkTypeface> planet_typeface();
59 
60 /** Return a color emoji typeface if available. */
61 sk_sp<SkTypeface> emoji_typeface();
62 
63 /** Sample text for the emoji_typeface font. */
64 const char* emoji_sample_text();
65 
66 /** A simple SkUserTypeface for testing. */
67 sk_sp<SkTypeface> sample_user_typeface();
68 
69 /**
70  * Returns a platform-independent text renderer.
71  */
72 sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style);
73 
create_portable_typeface()74 static inline sk_sp<SkTypeface> create_portable_typeface() {
75     return create_portable_typeface(nullptr, SkFontStyle());
76 }
77 
78 /**
79  *  Turn on portable (--nonativeFonts) or GDI font rendering (--gdi).
80  */
81 void SetDefaultFontMgr();
82 
83 
84 void get_text_path(const SkFont&,
85                    const void* text,
86                    size_t      length,
87                    SkTextEncoding,
88                    SkPath*,
89                    const SkPoint* positions = nullptr);
90 
91 /**
92  *  Returns true iff all of the pixels between the two images are identical.
93  *
94  *  If the configs differ, return false.
95  */
96 bool equal_pixels(const SkPixmap&, const SkPixmap&);
97 bool equal_pixels(const SkBitmap&, const SkBitmap&);
98 bool equal_pixels(const SkImage* a, const SkImage* b);
99 
100 /** Returns a newly created CheckerboardShader. */
101 sk_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. */
105 void draw_checkerboard(SkCanvas* canvas, SkColor color1, SkColor color2, int checkSize);
106 
107 /** Make it easier to create a bitmap-based checkerboard */
108 SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize);
109 
110 sk_sp<SkImage> create_checkerboard_image(int w, int h, SkColor c1, SkColor c2, int checkSize);
111 
112 /** A default checkerboard. */
draw_checkerboard(SkCanvas * canvas)113 inline void draw_checkerboard(SkCanvas* canvas) {
114     ToolUtils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
115 }
116 
117 SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y, int textSize, const char* str);
118 sk_sp<SkImage> create_string_image(int w, int h, SkColor c, int x, int y, int textSize, const char* str);
119 
120 // If the canvas does't make a surface (e.g. recording), make a raster surface
121 sk_sp<SkSurface> makeSurface(SkCanvas*, const SkImageInfo&, const SkSurfaceProps* = nullptr);
122 
123 // A helper for inserting a drawtext call into a SkTextBlobBuilder
124 void 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 
132 void 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.
144 SkPath make_star(const SkRect& bounds, int numPts = 5, int step = 2);
145 
146 void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst);
147 
148 void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst);
149 
150 void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst);
151 
152 SkPath make_big_path();
153 
154 // A helper object to test the topological sorting code (TopoSortBench.cpp & TopoSortTest.cpp)
155 class TopoTestNode : public SkRefCnt {
156 public:
TopoTestNode(int id)157     TopoTestNode(int id) : fID(id) {}
158 
dependsOn(TopoTestNode * src)159     void dependsOn(TopoTestNode* src) { *fDependencies.append() = src; }
targets(uint32_t target)160     void targets(uint32_t target) { *fTargets.append() = target; }
161 
id()162     int  id() const { return fID; }
reset()163     void reset() {
164         fOutputPos = 0;
165         fTempMark = false;
166         fWasOutput = false;
167     }
168 
outputPos()169     uint32_t outputPos() const {
170         SkASSERT(fWasOutput);
171         return fOutputPos;
172     }
173 
174     // check that the topological sort is valid for this node
check()175     bool check() {
176         if (!fWasOutput) {
177             return false;
178         }
179 
180         for (int i = 0; i < fDependencies.count(); ++i) {
181             if (!fDependencies[i]->fWasOutput) {
182                 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
SetTempMark(TopoTestNode * node)194     static void SetTempMark(TopoTestNode* node) { node->fTempMark = true; }
ResetTempMark(TopoTestNode * node)195     static void ResetTempMark(TopoTestNode* node) { node->fTempMark = false; }
IsTempMarked(TopoTestNode * node)196     static bool IsTempMarked(TopoTestNode* node) { return node->fTempMark; }
Output(TopoTestNode * node,uint32_t outputPos)197     static void Output(TopoTestNode* node, uint32_t outputPos) {
198         SkASSERT(!node->fWasOutput);
199         node->fOutputPos = outputPos;
200         node->fWasOutput = true;
201     }
WasOutput(TopoTestNode * node)202     static bool          WasOutput(TopoTestNode* node) { return node->fWasOutput; }
GetIndex(TopoTestNode * node)203     static uint32_t      GetIndex(TopoTestNode* node) { return node->outputPos(); }
NumDependencies(TopoTestNode * node)204     static int           NumDependencies(TopoTestNode* node) { return node->fDependencies.count(); }
Dependency(TopoTestNode * node,int index)205     static TopoTestNode* Dependency(TopoTestNode* node, int index) {
206         return node->fDependencies[index];
207     }
NumTargets(TopoTestNode * node)208     static int           NumTargets(TopoTestNode* node) { return node->fTargets.count(); }
GetTarget(TopoTestNode * node,int i)209     static uint32_t      GetTarget(TopoTestNode* node, int i) { return node->fTargets[i]; }
GetID(TopoTestNode * node)210     static uint32_t      GetID(TopoTestNode* node) { return node->id(); }
211 
212     // Helper functions for TopoSortBench & TopoSortTest
AllocNodes(SkTArray<sk_sp<ToolUtils::TopoTestNode>> * graph,int num)213     static void AllocNodes(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph, int num) {
214         graph->reserve_back(num);
215 
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
Print(const SkTArray<TopoTestNode * > & graph)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
Shuffle(SkTArray<sk_sp<TopoTestNode>> * graph,SkRandom * rand)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 
239     SK_DECLARE_INTERNAL_LLIST_INTERFACE(TopoTestNode);
240 
241 private:
242     int      fID;
243     uint32_t fOutputPos = 0;
244     bool     fTempMark = false;
245     bool     fWasOutput = false;
246 
247     SkTDArray<TopoTestNode*> fDependencies;
248     SkTDArray<uint32_t>      fTargets;
249 };
250 
251 template <typename T>
EncodeImageToFile(const char * path,const T & src,SkEncodedImageFormat f,int q)252 inline 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 
257 bool copy_to(SkBitmap* dst, SkColorType dstCT, const SkBitmap& src);
258 void copy_to_g8(SkBitmap* dst, const SkBitmap& src);
259 
260 class PixelIter {
261 public:
262     PixelIter();
PixelIter(SkSurface * surf)263     PixelIter(SkSurface* surf) {
264         SkPixmap pm;
265         if (!surf->peekPixels(&pm)) {
266             pm.reset();
267         }
268         this->reset(pm);
269     }
270 
reset(const SkPixmap & pm)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 
setDone()294     void setDone() { fPM.reset(); }
295 
296 private:
297     SkPixmap fPM;
298     SkIPoint fLoc;
299 };
300 
301 }  // namespace ToolUtils
302 
303 #endif  // ToolUtils_DEFINED
304