Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / tools / ToolUtils.h
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 constexpr const char* emoji_sample_text() {
65     return "\xF0\x9F\x98\x80"
66            " "
67            "\xE2\x99\xA2";  // ðŸ˜€ â™¢
68 }
69
70 /** A simple SkUserTypeface for testing. */
71 sk_sp<SkTypeface> sample_user_typeface();
72
73 /**
74  * Returns a platform-independent text renderer.
75  */
76 sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style);
77
78 static inline sk_sp<SkTypeface> create_portable_typeface() {
79     return create_portable_typeface(nullptr, SkFontStyle());
80 }
81
82 void get_text_path(const SkFont&,
83                    const void* text,
84                    size_t      length,
85                    SkTextEncoding,
86                    SkPath*,
87                    const SkPoint* positions = nullptr);
88
89 /**
90  *  Returns true iff all of the pixels between the two images are identical.
91  *
92  *  If the configs differ, return false.
93  */
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);
97
98 /** Returns a newly created CheckerboardShader. */
99 sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size);
100
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);
104
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);
107
108 sk_sp<SkImage> create_checkerboard_image(int w, int h, SkColor c1, SkColor c2, int checkSize);
109
110 /** A default checkerboard. */
111 inline void draw_checkerboard(SkCanvas* canvas) {
112     ToolUtils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
113 }
114
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);
117
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);
120
121 // A helper for inserting a drawtext call into a SkTextBlobBuilder
122 void add_to_text_blob_w_len(SkTextBlobBuilder*,
123                             const char* text,
124                             size_t      len,
125                             SkTextEncoding,
126                             const SkFont&,
127                             SkScalar x,
128                             SkScalar y);
129
130 void add_to_text_blob(SkTextBlobBuilder*, const char* text, const SkFont&, SkScalar x, SkScalar y);
131
132 // Constructs a star by walking a 'numPts'-sided regular polygon with even/odd fill:
133 //
134 //   moveTo(pts[0]);
135 //   lineTo(pts[step % numPts]);
136 //   ...
137 //   lineTo(pts[(step * (N - 1)) % numPts]);
138 //
139 // numPts=5, step=2 will produce a classic five-point star.
140 //
141 // numPts and step must be co-prime.
142 SkPath make_star(const SkRect& bounds, int numPts = 5, int step = 2);
143
144 void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst);
145
146 void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst);
147
148 void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst);
149
150 // A helper object to test the topological sorting code (TopoSortBench.cpp & TopoSortTest.cpp)
151 class TopoTestNode : public SkRefCnt {
152 public:
153     TopoTestNode(int id) : fID(id) {}
154
155     void dependsOn(TopoTestNode* src) { *fDependencies.append() = src; }
156     void targets(uint32_t target) { *fTargets.append() = target; }
157
158     int  id() const { return fID; }
159     void reset() {
160         fOutputPos = 0;
161         fTempMark = false;
162         fWasOutput = false;
163     }
164
165     uint32_t outputPos() const {
166         SkASSERT(fWasOutput);
167         return fOutputPos;
168     }
169
170     // check that the topological sort is valid for this node
171     bool check() {
172         if (!fWasOutput) {
173             return false;
174         }
175
176         for (int i = 0; i < fDependencies.count(); ++i) {
177             if (!fDependencies[i]->fWasOutput) {
178                 return false;
179             }
180             // This node should've been output after all the nodes on which it depends
181             if (fOutputPos < fDependencies[i]->outputPos()) {
182                 return false;
183             }
184         }
185
186         return true;
187     }
188
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;
197     }
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];
203     }
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(); }
207
208     // Helper functions for TopoSortBench & TopoSortTest
209     static void AllocNodes(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph, int num) {
210         graph->reserve_back(num);
211
212         for (int i = 0; i < num; ++i) {
213             graph->push_back(sk_sp<TopoTestNode>(new TopoTestNode(i)));
214         }
215     }
216
217 #ifdef SK_DEBUG
218     static void Print(const SkTArray<TopoTestNode*>& graph) {
219         for (int i = 0; i < graph.count(); ++i) {
220             SkDebugf("%d, ", graph[i]->id());
221         }
222         SkDebugf("\n");
223     }
224 #endif
225
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);
230
231             (*graph)[i].swap((*graph)[swap]);
232         }
233     }
234
235     SK_DECLARE_INTERNAL_LLIST_INTERFACE(TopoTestNode);
236
237 private:
238     int      fID;
239     uint32_t fOutputPos = 0;
240     bool     fTempMark = false;
241     bool     fWasOutput = false;
242
243     SkTDArray<TopoTestNode*> fDependencies;
244     SkTDArray<uint32_t>      fTargets;
245 };
246
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);
251 }
252
253 bool copy_to(SkBitmap* dst, SkColorType dstCT, const SkBitmap& src);
254 void copy_to_g8(SkBitmap* dst, const SkBitmap& src);
255
256 class PixelIter {
257 public:
258     PixelIter();
259     PixelIter(SkSurface* surf) {
260         SkPixmap pm;
261         if (!surf->peekPixels(&pm)) {
262             pm.reset();
263         }
264         this->reset(pm);
265     }
266
267     void reset(const SkPixmap& pm) {
268         fPM  = pm;
269         fLoc = {-1, 0};
270     }
271
272     void* next(SkIPoint* loc = nullptr) {
273         if (!fPM.addr()) {
274             return nullptr;
275         }
276         fLoc.fX += 1;
277         if (fLoc.fX >= fPM.width()) {
278             fLoc.fX = 0;
279             if (++fLoc.fY >= fPM.height()) {
280                 this->setDone();
281                 return nullptr;
282             }
283         }
284         if (loc) {
285             *loc = fLoc;
286         }
287         return fPM.writable_addr(fLoc.fX, fLoc.fY);
288     }
289
290     void setDone() { fPM.reset(); }
291
292 private:
293     SkPixmap fPM;
294     SkIPoint fLoc;
295 };
296
297 using PathSniffCallback = void(const SkMatrix&, const SkPath&, const SkPaint&);
298
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>);
302
303 #if SK_SUPPORT_GPU
304 sk_sp<SkImage> MakeTextureImage(SkCanvas* canvas, sk_sp<SkImage> orig);
305 #endif
306
307 }  // namespace ToolUtils
308
309 #endif  // ToolUtils_DEFINED