blob: 6c1adf8e26273d23d2526acb733b15f3a2fdb90c [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"
robertphillips3e5c2b12015-03-23 05:46:51 -070012#include "SkImageEncoder.h"
tfarina20108912014-06-21 10:54:17 -070013#include "SkImageInfo.h"
robertphillips423f6462015-10-19 12:15:55 -070014#include "SkRandom.h"
Robert Phillips4150eea2018-02-07 17:08:21 -050015#include "SkRefCnt.h"
Hal Canary1fcc4042016-11-30 17:07:59 -050016#include "SkStream.h"
robertphillips423f6462015-10-19 12:15:55 -070017#include "SkTDArray.h"
caryclark5fb6bd42014-06-23 11:25:00 -070018#include "SkTypeface.h"
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000019
Cary Clark992c7b02014-07-31 08:58:44 -040020class SkBitmap;
21class SkCanvas;
Brian Osman964dec32017-01-26 09:32:33 -050022class SkColorFilter;
Mike Reed24846602017-12-04 16:06:03 -050023class SkImage;
Cary Clark992c7b02014-07-31 08:58:44 -040024class SkPaint;
joshualitt98d2e2f2015-10-05 07:23:30 -070025class SkPath;
robertphillips276d3282016-08-04 09:03:19 -070026class SkRRect;
halcanaryb0cce2c2015-01-26 12:49:00 -080027class SkShader;
Mike Reed46596ae2018-01-02 15:40:29 -050028class SkSurface;
29class SkSurfaceProps;
Cary Clark992c7b02014-07-31 08:58:44 -040030class SkTestFont;
joshualitt9e36c1a2015-04-14 12:17:27 -070031class SkTextBlobBuilder;
skia.committer@gmail.comdb0c8752014-03-18 03:02:11 +000032
Cary Clark992c7b02014-07-31 08:58:44 -040033namespace sk_tool_utils {
caryclark5fb6bd42014-06-23 11:25:00 -070034
Mike Reedd4746982018-02-07 16:05:29 -050035 const char* alphatype_name(SkAlphaType);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000036 const char* colortype_name(SkColorType);
caryclark37213552015-07-24 11:08:01 -070037
38 /**
39 * Map opaque colors from 8888 to 565.
40 */
caryclark65cdba62015-06-15 06:51:08 -070041 SkColor color_to_565(SkColor color);
caryclark37213552015-07-24 11:08:01 -070042
43 /**
44 * Return a color emoji typeface if available.
45 */
bungeman13b9c952016-05-12 10:09:30 -070046 sk_sp<SkTypeface> emoji_typeface();
caryclark37213552015-07-24 11:08:01 -070047
48 /**
49 * If the platform supports color emoji, return sample text the emoji can render.
50 */
caryclarkc3dcb672015-07-21 12:27:36 -070051 const char* emoji_sample_text();
caryclark37213552015-07-24 11:08:01 -070052
53 /**
Mike Kleinb251b722017-11-13 11:03:16 -050054 * Returns a string describing the platform font manager, if we're using one, otherwise "".
caryclark37213552015-07-24 11:08:01 -070055 */
Mike Kleinb251b722017-11-13 11:03:16 -050056 const char* platform_font_manager();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000057
58 /**
caryclark1818acb2015-07-24 12:09:25 -070059 * Sets the paint to use a platform-independent text renderer
caryclark5fb6bd42014-06-23 11:25:00 -070060 */
halcanary96fcdcc2015-08-27 07:41:13 -070061 void set_portable_typeface(SkPaint* paint, const char* name = nullptr,
mbocee6a9912016-05-31 11:42:36 -070062 SkFontStyle style = SkFontStyle());
caryclark37213552015-07-24 11:08:01 -070063
64 /**
caryclark37213552015-07-24 11:08:01 -070065 * Returns a platform-independent text renderer.
66 */
mbocee6a9912016-05-31 11:42:36 -070067 sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style);
caryclark37213552015-07-24 11:08:01 -070068
caryclarkf53ce802015-06-15 06:48:30 -070069 /** Call to clean up portable font references. */
70 void release_portable_typefaces();
caryclark5fb6bd42014-06-23 11:25:00 -070071
72 /**
Mike Reed4c790bd2018-02-08 14:10:40 -050073 * Call writePixels() by using the pixels from bitmap, but with an info that claims
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000074 * the pixels are colorType + alphaType
75 */
76 void write_pixels(SkCanvas*, const SkBitmap&, int x, int y, SkColorType, SkAlphaType);
Mike Reed4c790bd2018-02-08 14:10:40 -050077 void write_pixels(SkSurface*, const SkBitmap&, int x, int y, SkColorType, SkAlphaType);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000078
Mike Reed5a625e02017-08-08 15:48:54 -040079 /**
80 * Returns true iff all of the pixels between the two images differ by <= the maxDiff value
81 * per component.
82 *
83 * If the configs differ, return false.
84 *
85 * If the colorType is half-float, then maxDiff is interpreted as 0..255 --> 0..1
86 */
Mike Reed24846602017-12-04 16:06:03 -050087 bool equal_pixels(const SkPixmap&, const SkPixmap&, unsigned maxDiff = 0,
88 bool respectColorSpaces = false);
89 bool equal_pixels(const SkBitmap&, const SkBitmap&, unsigned maxDiff = 0,
90 bool respectColorSpaces = false);
Mike Reed60691a52017-12-05 15:11:24 -050091 bool equal_pixels(const SkImage* a, const SkImage* b, unsigned maxDiff = 0,
Mike Reed24846602017-12-04 16:06:03 -050092 bool respectColorSpaces = false);
Mike Reed5a625e02017-08-08 15:48:54 -040093
Cary Clark992c7b02014-07-31 08:58:44 -040094 // private to sk_tool_utils
mbocee6a9912016-05-31 11:42:36 -070095 sk_sp<SkTypeface> create_font(const char* name, SkFontStyle);
Cary Clark992c7b02014-07-31 08:58:44 -040096
halcanaryb0cce2c2015-01-26 12:49:00 -080097 /** Returns a newly created CheckerboardShader. */
reed8a21c9f2016-03-08 18:50:00 -080098 sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size);
halcanaryb0cce2c2015-01-26 12:49:00 -080099
100 /** Draw a checkerboard pattern in the current canvas, restricted to
halcanaryf77365f2015-01-27 08:38:35 -0800101 the current clip, using SkXfermode::kSrc_Mode. */
halcanaryb0cce2c2015-01-26 12:49:00 -0800102 void draw_checkerboard(SkCanvas* canvas,
103 SkColor color1,
104 SkColor color2,
robertphillips943a4622015-09-03 13:32:33 -0700105 int checkSize);
106
107 /** Make it easier to create a bitmap-based checkerboard */
108 SkBitmap create_checkerboard_bitmap(int w, int h,
109 SkColor c1, SkColor c2,
110 int checkSize);
halcanaryb0cce2c2015-01-26 12:49:00 -0800111
112 /** A default checkerboard. */
113 inline void draw_checkerboard(SkCanvas* canvas) {
114 sk_tool_utils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
115 }
116
robertphillips943a4622015-09-03 13:32:33 -0700117 SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
118 int textSize, const char* str);
119
Mike Reed46596ae2018-01-02 15:40:29 -0500120 // 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
joshualitt9e36c1a2015-04-14 12:17:27 -0700123 // A helper for inserting a drawtext call into a SkTextBlobBuilder
Robert Phillips4c72b262017-08-15 13:28:42 -0400124 void add_to_text_blob_w_len(SkTextBlobBuilder* builder, const char* text, size_t len,
125 const SkPaint& origPaint, SkScalar x, SkScalar y);
126
127 void add_to_text_blob(SkTextBlobBuilder* builder, const char* text,
128 const SkPaint& origPaint, SkScalar x, SkScalar y);
joshualitt9e36c1a2015-04-14 12:17:27 -0700129
Chris Dalton7c02cc72017-11-06 14:10:54 -0700130 // Constructs a star by walking a 'numPts'-sided regular polygon with even/odd fill:
131 //
132 // moveTo(pts[0]);
133 // lineTo(pts[step % numPts]);
134 // ...
135 // lineTo(pts[(step * (N - 1)) % numPts]);
136 //
137 // numPts=5, step=2 will produce a classic five-point star.
138 //
139 // numPts and step must be co-prime.
140 SkPath make_star(const SkRect& bounds, int numPts = 5, int step = 2);
141
joshualitt98d2e2f2015-10-05 07:23:30 -0700142 void make_big_path(SkPath& path);
robertphillips9c4909b2015-10-19 06:39:17 -0700143
144 // Return a blurred version of 'src'. This doesn't use a separable filter
145 // so it is slow!
146 SkBitmap slow_blur(const SkBitmap& src, float sigma);
147
robertphillips276d3282016-08-04 09:03:19 -0700148 SkRect compute_central_occluder(const SkRRect& rr);
robertphillips401c1962016-08-04 12:35:46 -0700149 SkRect compute_widest_occluder(const SkRRect& rr);
150 SkRect compute_tallest_occluder(const SkRRect& rr);
robertphillips276d3282016-08-04 09:03:19 -0700151
robertphillips423f6462015-10-19 12:15:55 -0700152 // A helper object to test the topological sorting code (TopoSortBench.cpp & TopoSortTest.cpp)
Robert Phillips4150eea2018-02-07 17:08:21 -0500153 class TopoTestNode : public SkRefCnt {
robertphillips423f6462015-10-19 12:15:55 -0700154 public:
155 TopoTestNode(int id) : fID(id), fOutputPos(-1), fTempMark(false) { }
156
157 void dependsOn(TopoTestNode* src) {
158 *fDependencies.append() = src;
159 }
160
161 int id() const { return fID; }
162 void reset() { fOutputPos = -1; }
163
164 int outputPos() const { return fOutputPos; }
165
166 // check that the topological sort is valid for this node
167 bool check() {
168 if (-1 == fOutputPos) {
169 return false;
170 }
171
172 for (int i = 0; i < fDependencies.count(); ++i) {
173 if (-1 == fDependencies[i]->outputPos()) {
174 return false;
175 }
176 // This node should've been output after all the nodes on which it depends
177 if (fOutputPos < fDependencies[i]->outputPos()) {
178 return false;
179 }
180 }
181
182 return true;
183 }
184
185 // The following 7 methods are needed by the topological sort
186 static void SetTempMark(TopoTestNode* node) { node->fTempMark = true; }
187 static void ResetTempMark(TopoTestNode* node) { node->fTempMark = false; }
188 static bool IsTempMarked(TopoTestNode* node) { return node->fTempMark; }
halcanary9d524f22016-03-29 09:03:52 -0700189 static void Output(TopoTestNode* node, int outputPos) {
robertphillips423f6462015-10-19 12:15:55 -0700190 SkASSERT(-1 != outputPos);
halcanary9d524f22016-03-29 09:03:52 -0700191 node->fOutputPos = outputPos;
robertphillips423f6462015-10-19 12:15:55 -0700192 }
193 static bool WasOutput(TopoTestNode* node) { return (-1 != node->fOutputPos); }
194 static int NumDependencies(TopoTestNode* node) { return node->fDependencies.count(); }
halcanary9d524f22016-03-29 09:03:52 -0700195 static TopoTestNode* Dependency(TopoTestNode* node, int index) {
robertphillips423f6462015-10-19 12:15:55 -0700196 return node->fDependencies[index];
197 }
198
199 // Helper functions for TopoSortBench & TopoSortTest
Robert Phillips4150eea2018-02-07 17:08:21 -0500200 static void AllocNodes(SkTArray<sk_sp<sk_tool_utils::TopoTestNode>>* graph, int num) {
201 graph->reserve(num);
robertphillips423f6462015-10-19 12:15:55 -0700202
203 for (int i = 0; i < num; ++i) {
Robert Phillips4150eea2018-02-07 17:08:21 -0500204 graph->push_back(sk_sp<TopoTestNode>(new TopoTestNode(i)));
robertphillips423f6462015-10-19 12:15:55 -0700205 }
206 }
207
Robert Phillips4150eea2018-02-07 17:08:21 -0500208#ifdef SK_DEBUG
209 static void Print(const SkTArray<TopoTestNode*>& graph) {
robertphillips423f6462015-10-19 12:15:55 -0700210 for (int i = 0; i < graph.count(); ++i) {
211 SkDebugf("%d, ", graph[i]->id());
212 }
213 SkDebugf("\n");
214 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500215#endif
robertphillips423f6462015-10-19 12:15:55 -0700216
217 // randomize the array
Robert Phillips4150eea2018-02-07 17:08:21 -0500218 static void Shuffle(SkTArray<sk_sp<TopoTestNode>>* graph, SkRandom* rand) {
robertphillips423f6462015-10-19 12:15:55 -0700219 for (int i = graph->count()-1; i > 0; --i) {
220 int swap = rand->nextU() % (i+1);
221
Robert Phillips4150eea2018-02-07 17:08:21 -0500222 (*graph)[i].swap((*graph)[swap]);
robertphillips423f6462015-10-19 12:15:55 -0700223 }
224 }
225
226 private:
227 int fID;
228 int fOutputPos;
229 bool fTempMark;
230
231 SkTDArray<TopoTestNode*> fDependencies;
232 };
233
Hal Canarydb683012016-11-23 08:55:18 -0700234 template <typename T>
235 inline bool EncodeImageToFile(const char* path, const T& src, SkEncodedImageFormat f, int q) {
236 SkFILEWStream file(path);
237 return file.isValid() && SkEncodeImage(&file, src, f, q);
238 }
239
240 template <typename T>
241 inline sk_sp<SkData> EncodeImageToData(const T& src, SkEncodedImageFormat f, int q) {
242 SkDynamicMemoryWStream buf;
243 return SkEncodeImage(&buf, src , f, q) ? buf.detachAsData() : nullptr;
244 }
245
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400246 bool copy_to(SkBitmap* dst, SkColorType dstCT, const SkBitmap& src);
Matt Sarett4897fb82017-01-18 11:49:33 -0500247 void copy_to_g8(SkBitmap* dst, const SkBitmap& src);
tfarina20108912014-06-21 10:54:17 -0700248} // namespace sk_tool_utils
249
250#endif // sk_tool_utils_DEFINED