blob: 196543abc554312a33407f5cb15fcc1190d3c38f [file] [log] [blame]
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +00001/*
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 sk_tool_utils_DEFINED
9#define sk_tool_utils_DEFINED
10
halcanaryb0cce2c2015-01-26 12:49:00 -080011#include "SkColor.h"
Ben Wagner97182cc2018-02-15 10:20:04 -050012#include "SkData.h"
13#include "SkEncodedImageFormat.h"
Mike Reed12a6d452018-12-21 22:22:31 -050014#include "SkFont.h"
Ben Wagner97182cc2018-02-15 10:20:04 -050015#include "SkFontStyle.h"
Mike Reed38810f32018-12-21 10:58:25 -050016#include "SkFontTypes.h"
robertphillips3e5c2b12015-03-23 05:46:51 -070017#include "SkImageEncoder.h"
tfarina20108912014-06-21 10:54:17 -070018#include "SkImageInfo.h"
robertphillips423f6462015-10-19 12:15:55 -070019#include "SkRandom.h"
Ben Wagner97182cc2018-02-15 10:20:04 -050020#include "SkRect.h"
Robert Phillips4150eea2018-02-07 17:08:21 -050021#include "SkRefCnt.h"
Ben Wagner97182cc2018-02-15 10:20:04 -050022#include "SkScalar.h"
Hal Canary1fcc4042016-11-30 17:07:59 -050023#include "SkStream.h"
Ben Wagner97182cc2018-02-15 10:20:04 -050024#include "SkTArray.h"
robertphillips423f6462015-10-19 12:15:55 -070025#include "SkTDArray.h"
Mike Reed3185f902018-10-26 16:33:00 -040026#include "SkTypeface.h"
Ben Wagner97182cc2018-02-15 10:20:04 -050027#include "SkTypes.h"
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000028
Cary Clark992c7b02014-07-31 08:58:44 -040029class SkBitmap;
30class SkCanvas;
Ben Wagner97182cc2018-02-15 10:20:04 -050031class SkFontStyle;
Mike Reed24846602017-12-04 16:06:03 -050032class SkImage;
joshualitt98d2e2f2015-10-05 07:23:30 -070033class SkPath;
Ben Wagner97182cc2018-02-15 10:20:04 -050034class SkPixmap;
robertphillips276d3282016-08-04 09:03:19 -070035class SkRRect;
halcanaryb0cce2c2015-01-26 12:49:00 -080036class SkShader;
Mike Reed46596ae2018-01-02 15:40:29 -050037class SkSurface;
38class SkSurfaceProps;
joshualitt9e36c1a2015-04-14 12:17:27 -070039class SkTextBlobBuilder;
Ben Wagner97182cc2018-02-15 10:20:04 -050040class SkTypeface;
skia.committer@gmail.comdb0c8752014-03-18 03:02:11 +000041
Cary Clark992c7b02014-07-31 08:58:44 -040042namespace sk_tool_utils {
caryclark5fb6bd42014-06-23 11:25:00 -070043
Mike Reedd4746982018-02-07 16:05:29 -050044 const char* alphatype_name(SkAlphaType);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000045 const char* colortype_name(SkColorType);
caryclark37213552015-07-24 11:08:01 -070046
47 /**
48 * Map opaque colors from 8888 to 565.
49 */
caryclark65cdba62015-06-15 06:51:08 -070050 SkColor color_to_565(SkColor color);
caryclark37213552015-07-24 11:08:01 -070051
52 /**
53 * Return a color emoji typeface if available.
54 */
bungeman13b9c952016-05-12 10:09:30 -070055 sk_sp<SkTypeface> emoji_typeface();
caryclark37213552015-07-24 11:08:01 -070056
57 /**
58 * If the platform supports color emoji, return sample text the emoji can render.
59 */
caryclarkc3dcb672015-07-21 12:27:36 -070060 const char* emoji_sample_text();
caryclark37213552015-07-24 11:08:01 -070061
62 /**
Mike Kleinb251b722017-11-13 11:03:16 -050063 * Returns a string describing the platform font manager, if we're using one, otherwise "".
caryclark37213552015-07-24 11:08:01 -070064 */
Mike Kleinb251b722017-11-13 11:03:16 -050065 const char* platform_font_manager();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000066
67 /**
caryclark37213552015-07-24 11:08:01 -070068 * Returns a platform-independent text renderer.
69 */
mbocee6a9912016-05-31 11:42:36 -070070 sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style);
caryclark37213552015-07-24 11:08:01 -070071
Mike Reed3185f902018-10-26 16:33:00 -040072 static inline sk_sp<SkTypeface> create_portable_typeface() {
73 return create_portable_typeface(nullptr, SkFontStyle());
74 }
75
Mike Reed38810f32018-12-21 10:58:25 -050076 void get_text_path(const SkFont&, const void* text, size_t length, SkTextEncoding, SkPath*,
77 const SkPoint* positions = nullptr);
78
caryclark5fb6bd42014-06-23 11:25:00 -070079 /**
Mike Reed4c790bd2018-02-08 14:10:40 -050080 * Call writePixels() by using the pixels from bitmap, but with an info that claims
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000081 * the pixels are colorType + alphaType
82 */
83 void write_pixels(SkCanvas*, const SkBitmap&, int x, int y, SkColorType, SkAlphaType);
Mike Reed4c790bd2018-02-08 14:10:40 -050084 void write_pixels(SkSurface*, const SkBitmap&, int x, int y, SkColorType, SkAlphaType);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000085
Mike Reed5a625e02017-08-08 15:48:54 -040086 /**
Brian Osman1b151982018-09-20 12:47:39 -040087 * Returns true iff all of the pixels between the two images are identical.
Mike Reed5a625e02017-08-08 15:48:54 -040088 *
89 * If the configs differ, return false.
Mike Reed5a625e02017-08-08 15:48:54 -040090 */
Brian Osman1b151982018-09-20 12:47:39 -040091 bool equal_pixels(const SkPixmap&, const SkPixmap&);
92 bool equal_pixels(const SkBitmap&, const SkBitmap&);
93 bool equal_pixels(const SkImage* a, const SkImage* b);
Mike Reed5a625e02017-08-08 15:48:54 -040094
halcanaryb0cce2c2015-01-26 12:49:00 -080095 /** Returns a newly created CheckerboardShader. */
reed8a21c9f2016-03-08 18:50:00 -080096 sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size);
halcanaryb0cce2c2015-01-26 12:49:00 -080097
98 /** Draw a checkerboard pattern in the current canvas, restricted to
halcanaryf77365f2015-01-27 08:38:35 -080099 the current clip, using SkXfermode::kSrc_Mode. */
halcanaryb0cce2c2015-01-26 12:49:00 -0800100 void draw_checkerboard(SkCanvas* canvas,
101 SkColor color1,
102 SkColor color2,
robertphillips943a4622015-09-03 13:32:33 -0700103 int checkSize);
104
105 /** Make it easier to create a bitmap-based checkerboard */
106 SkBitmap create_checkerboard_bitmap(int w, int h,
107 SkColor c1, SkColor c2,
108 int checkSize);
halcanaryb0cce2c2015-01-26 12:49:00 -0800109
110 /** A default checkerboard. */
111 inline void draw_checkerboard(SkCanvas* canvas) {
112 sk_tool_utils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
113 }
114
robertphillips943a4622015-09-03 13:32:33 -0700115 SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
116 int textSize, const char* str);
117
Cary Clarka24712e2018-09-05 18:41:40 +0000118 // 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
joshualitt9e36c1a2015-04-14 12:17:27 -0700121 // A helper for inserting a drawtext call into a SkTextBlobBuilder
Mike Reed12a6d452018-12-21 22:22:31 -0500122 void add_to_text_blob_w_len(SkTextBlobBuilder*, const char* text, size_t len, SkTextEncoding,
123 const SkFont&, SkScalar x, SkScalar y);
Robert Phillips4c72b262017-08-15 13:28:42 -0400124
Mike Reed12a6d452018-12-21 22:22:31 -0500125 void add_to_text_blob(SkTextBlobBuilder*, const char* text, const SkFont&,
126 SkScalar x, SkScalar y);
127
Chris Dalton7c02cc72017-11-06 14:10:54 -0700128 // Constructs a star by walking a 'numPts'-sided regular polygon with even/odd fill:
129 //
130 // moveTo(pts[0]);
131 // lineTo(pts[step % numPts]);
132 // ...
133 // lineTo(pts[(step * (N - 1)) % numPts]);
134 //
135 // numPts=5, step=2 will produce a classic five-point star.
136 //
137 // numPts and step must be co-prime.
138 SkPath make_star(const SkRect& bounds, int numPts = 5, int step = 2);
139
Robert Phillipsa8cdbd72018-07-17 12:30:40 -0400140 void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst);
141
142 void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst);
143
144 void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst);
145
joshualitt98d2e2f2015-10-05 07:23:30 -0700146 void make_big_path(SkPath& path);
robertphillips9c4909b2015-10-19 06:39:17 -0700147
robertphillips423f6462015-10-19 12:15:55 -0700148 // A helper object to test the topological sorting code (TopoSortBench.cpp & TopoSortTest.cpp)
Robert Phillips4150eea2018-02-07 17:08:21 -0500149 class TopoTestNode : public SkRefCnt {
robertphillips423f6462015-10-19 12:15:55 -0700150 public:
151 TopoTestNode(int id) : fID(id), fOutputPos(-1), fTempMark(false) { }
152
153 void dependsOn(TopoTestNode* src) {
154 *fDependencies.append() = src;
155 }
156
157 int id() const { return fID; }
158 void reset() { fOutputPos = -1; }
159
160 int outputPos() const { return fOutputPos; }
161
162 // check that the topological sort is valid for this node
163 bool check() {
164 if (-1 == fOutputPos) {
165 return false;
166 }
167
168 for (int i = 0; i < fDependencies.count(); ++i) {
169 if (-1 == fDependencies[i]->outputPos()) {
170 return false;
171 }
172 // This node should've been output after all the nodes on which it depends
173 if (fOutputPos < fDependencies[i]->outputPos()) {
174 return false;
175 }
176 }
177
178 return true;
179 }
180
181 // The following 7 methods are needed by the topological sort
182 static void SetTempMark(TopoTestNode* node) { node->fTempMark = true; }
183 static void ResetTempMark(TopoTestNode* node) { node->fTempMark = false; }
184 static bool IsTempMarked(TopoTestNode* node) { return node->fTempMark; }
halcanary9d524f22016-03-29 09:03:52 -0700185 static void Output(TopoTestNode* node, int outputPos) {
robertphillips423f6462015-10-19 12:15:55 -0700186 SkASSERT(-1 != outputPos);
halcanary9d524f22016-03-29 09:03:52 -0700187 node->fOutputPos = outputPos;
robertphillips423f6462015-10-19 12:15:55 -0700188 }
189 static bool WasOutput(TopoTestNode* node) { return (-1 != node->fOutputPos); }
190 static int NumDependencies(TopoTestNode* node) { return node->fDependencies.count(); }
halcanary9d524f22016-03-29 09:03:52 -0700191 static TopoTestNode* Dependency(TopoTestNode* node, int index) {
robertphillips423f6462015-10-19 12:15:55 -0700192 return node->fDependencies[index];
193 }
194
195 // Helper functions for TopoSortBench & TopoSortTest
Robert Phillips4150eea2018-02-07 17:08:21 -0500196 static void AllocNodes(SkTArray<sk_sp<sk_tool_utils::TopoTestNode>>* graph, int num) {
197 graph->reserve(num);
robertphillips423f6462015-10-19 12:15:55 -0700198
199 for (int i = 0; i < num; ++i) {
Robert Phillips4150eea2018-02-07 17:08:21 -0500200 graph->push_back(sk_sp<TopoTestNode>(new TopoTestNode(i)));
robertphillips423f6462015-10-19 12:15:55 -0700201 }
202 }
203
Robert Phillips4150eea2018-02-07 17:08:21 -0500204#ifdef SK_DEBUG
205 static void Print(const SkTArray<TopoTestNode*>& graph) {
robertphillips423f6462015-10-19 12:15:55 -0700206 for (int i = 0; i < graph.count(); ++i) {
207 SkDebugf("%d, ", graph[i]->id());
208 }
209 SkDebugf("\n");
210 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500211#endif
robertphillips423f6462015-10-19 12:15:55 -0700212
213 // randomize the array
Robert Phillips4150eea2018-02-07 17:08:21 -0500214 static void Shuffle(SkTArray<sk_sp<TopoTestNode>>* graph, SkRandom* rand) {
robertphillips423f6462015-10-19 12:15:55 -0700215 for (int i = graph->count()-1; i > 0; --i) {
216 int swap = rand->nextU() % (i+1);
217
Robert Phillips4150eea2018-02-07 17:08:21 -0500218 (*graph)[i].swap((*graph)[swap]);
robertphillips423f6462015-10-19 12:15:55 -0700219 }
220 }
221
222 private:
223 int fID;
224 int fOutputPos;
225 bool fTempMark;
226
227 SkTDArray<TopoTestNode*> fDependencies;
228 };
229
Hal Canarydb683012016-11-23 08:55:18 -0700230 template <typename T>
231 inline bool EncodeImageToFile(const char* path, const T& src, SkEncodedImageFormat f, int q) {
232 SkFILEWStream file(path);
233 return file.isValid() && SkEncodeImage(&file, src, f, q);
234 }
235
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400236 bool copy_to(SkBitmap* dst, SkColorType dstCT, const SkBitmap& src);
Matt Sarett4897fb82017-01-18 11:49:33 -0500237 void copy_to_g8(SkBitmap* dst, const SkBitmap& src);
tfarina20108912014-06-21 10:54:17 -0700238} // namespace sk_tool_utils
239
240#endif // sk_tool_utils_DEFINED