blob: 9081e3c56f0e46fec7b027e22a4a682349b0cca4 [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"
Hal Canary1fcc4042016-11-30 17:07:59 -050015#include "SkStream.h"
robertphillips423f6462015-10-19 12:15:55 -070016#include "SkTDArray.h"
caryclark5fb6bd42014-06-23 11:25:00 -070017#include "SkTypeface.h"
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000018
Cary Clark992c7b02014-07-31 08:58:44 -040019class SkBitmap;
20class SkCanvas;
Brian Osman964dec32017-01-26 09:32:33 -050021class SkColorFilter;
Mike Reed24846602017-12-04 16:06:03 -050022class SkImage;
Cary Clark992c7b02014-07-31 08:58:44 -040023class SkPaint;
joshualitt98d2e2f2015-10-05 07:23:30 -070024class SkPath;
robertphillips276d3282016-08-04 09:03:19 -070025class SkRRect;
halcanaryb0cce2c2015-01-26 12:49:00 -080026class SkShader;
Mike Reed46596ae2018-01-02 15:40:29 -050027class SkSurface;
28class SkSurfaceProps;
Cary Clark992c7b02014-07-31 08:58:44 -040029class SkTestFont;
joshualitt9e36c1a2015-04-14 12:17:27 -070030class SkTextBlobBuilder;
skia.committer@gmail.comdb0c8752014-03-18 03:02:11 +000031
Cary Clark992c7b02014-07-31 08:58:44 -040032namespace sk_tool_utils {
caryclark5fb6bd42014-06-23 11:25:00 -070033
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000034 const char* colortype_name(SkColorType);
caryclark37213552015-07-24 11:08:01 -070035
36 /**
37 * Map opaque colors from 8888 to 565.
38 */
caryclark65cdba62015-06-15 06:51:08 -070039 SkColor color_to_565(SkColor color);
caryclark37213552015-07-24 11:08:01 -070040
41 /**
42 * Return a color emoji typeface if available.
43 */
bungeman13b9c952016-05-12 10:09:30 -070044 sk_sp<SkTypeface> emoji_typeface();
caryclark37213552015-07-24 11:08:01 -070045
46 /**
47 * If the platform supports color emoji, return sample text the emoji can render.
48 */
caryclarkc3dcb672015-07-21 12:27:36 -070049 const char* emoji_sample_text();
caryclark37213552015-07-24 11:08:01 -070050
51 /**
Mike Kleinb251b722017-11-13 11:03:16 -050052 * Returns a string describing the platform font manager, if we're using one, otherwise "".
caryclark37213552015-07-24 11:08:01 -070053 */
Mike Kleinb251b722017-11-13 11:03:16 -050054 const char* platform_font_manager();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000055
56 /**
caryclark1818acb2015-07-24 12:09:25 -070057 * Sets the paint to use a platform-independent text renderer
caryclark5fb6bd42014-06-23 11:25:00 -070058 */
halcanary96fcdcc2015-08-27 07:41:13 -070059 void set_portable_typeface(SkPaint* paint, const char* name = nullptr,
mbocee6a9912016-05-31 11:42:36 -070060 SkFontStyle style = SkFontStyle());
caryclark37213552015-07-24 11:08:01 -070061
62 /**
caryclark37213552015-07-24 11:08:01 -070063 * Returns a platform-independent text renderer.
64 */
mbocee6a9912016-05-31 11:42:36 -070065 sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style);
caryclark37213552015-07-24 11:08:01 -070066
caryclarkf53ce802015-06-15 06:48:30 -070067 /** Call to clean up portable font references. */
68 void release_portable_typefaces();
caryclark5fb6bd42014-06-23 11:25:00 -070069
70 /**
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000071 * Call canvas->writePixels() by using the pixels from bitmap, but with an info that claims
72 * the pixels are colorType + alphaType
73 */
74 void write_pixels(SkCanvas*, const SkBitmap&, int x, int y, SkColorType, SkAlphaType);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000075
Mike Reed5a625e02017-08-08 15:48:54 -040076 /**
77 * Returns true iff all of the pixels between the two images differ by <= the maxDiff value
78 * per component.
79 *
80 * If the configs differ, return false.
81 *
82 * If the colorType is half-float, then maxDiff is interpreted as 0..255 --> 0..1
83 */
Mike Reed24846602017-12-04 16:06:03 -050084 bool equal_pixels(const SkPixmap&, const SkPixmap&, unsigned maxDiff = 0,
85 bool respectColorSpaces = false);
86 bool equal_pixels(const SkBitmap&, const SkBitmap&, unsigned maxDiff = 0,
87 bool respectColorSpaces = false);
Mike Reed60691a52017-12-05 15:11:24 -050088 bool equal_pixels(const SkImage* a, const SkImage* b, unsigned maxDiff = 0,
Mike Reed24846602017-12-04 16:06:03 -050089 bool respectColorSpaces = false);
Mike Reed5a625e02017-08-08 15:48:54 -040090
Cary Clark992c7b02014-07-31 08:58:44 -040091 // private to sk_tool_utils
mbocee6a9912016-05-31 11:42:36 -070092 sk_sp<SkTypeface> create_font(const char* name, SkFontStyle);
Cary Clark992c7b02014-07-31 08:58:44 -040093
halcanaryb0cce2c2015-01-26 12:49:00 -080094 /** Returns a newly created CheckerboardShader. */
reed8a21c9f2016-03-08 18:50:00 -080095 sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size);
halcanaryb0cce2c2015-01-26 12:49:00 -080096
97 /** Draw a checkerboard pattern in the current canvas, restricted to
halcanaryf77365f2015-01-27 08:38:35 -080098 the current clip, using SkXfermode::kSrc_Mode. */
halcanaryb0cce2c2015-01-26 12:49:00 -080099 void draw_checkerboard(SkCanvas* canvas,
100 SkColor color1,
101 SkColor color2,
robertphillips943a4622015-09-03 13:32:33 -0700102 int checkSize);
103
104 /** Make it easier to create a bitmap-based checkerboard */
105 SkBitmap create_checkerboard_bitmap(int w, int h,
106 SkColor c1, SkColor c2,
107 int checkSize);
halcanaryb0cce2c2015-01-26 12:49:00 -0800108
109 /** A default checkerboard. */
110 inline void draw_checkerboard(SkCanvas* canvas) {
111 sk_tool_utils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
112 }
113
robertphillips943a4622015-09-03 13:32:33 -0700114 SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
115 int textSize, const char* str);
116
Mike Reed46596ae2018-01-02 15:40:29 -0500117 // If the canvas does't make a surface (e.g. recording), make a raster surface
118 sk_sp<SkSurface> makeSurface(SkCanvas*, const SkImageInfo&, const SkSurfaceProps* = nullptr);
119
joshualitt9e36c1a2015-04-14 12:17:27 -0700120 // A helper for inserting a drawtext call into a SkTextBlobBuilder
Robert Phillips4c72b262017-08-15 13:28:42 -0400121 void add_to_text_blob_w_len(SkTextBlobBuilder* builder, const char* text, size_t len,
122 const SkPaint& origPaint, SkScalar x, SkScalar y);
123
124 void add_to_text_blob(SkTextBlobBuilder* builder, const char* text,
125 const SkPaint& origPaint, SkScalar x, SkScalar y);
joshualitt9e36c1a2015-04-14 12:17:27 -0700126
Chris Dalton7c02cc72017-11-06 14:10:54 -0700127 // Constructs a star by walking a 'numPts'-sided regular polygon with even/odd fill:
128 //
129 // moveTo(pts[0]);
130 // lineTo(pts[step % numPts]);
131 // ...
132 // lineTo(pts[(step * (N - 1)) % numPts]);
133 //
134 // numPts=5, step=2 will produce a classic five-point star.
135 //
136 // numPts and step must be co-prime.
137 SkPath make_star(const SkRect& bounds, int numPts = 5, int step = 2);
138
joshualitt98d2e2f2015-10-05 07:23:30 -0700139 void make_big_path(SkPath& path);
robertphillips9c4909b2015-10-19 06:39:17 -0700140
141 // Return a blurred version of 'src'. This doesn't use a separable filter
142 // so it is slow!
143 SkBitmap slow_blur(const SkBitmap& src, float sigma);
144
robertphillips276d3282016-08-04 09:03:19 -0700145 SkRect compute_central_occluder(const SkRRect& rr);
robertphillips401c1962016-08-04 12:35:46 -0700146 SkRect compute_widest_occluder(const SkRRect& rr);
147 SkRect compute_tallest_occluder(const SkRRect& rr);
robertphillips276d3282016-08-04 09:03:19 -0700148
robertphillips423f6462015-10-19 12:15:55 -0700149 // A helper object to test the topological sorting code (TopoSortBench.cpp & TopoSortTest.cpp)
150 class TopoTestNode {
151 public:
152 TopoTestNode(int id) : fID(id), fOutputPos(-1), fTempMark(false) { }
153
154 void dependsOn(TopoTestNode* src) {
155 *fDependencies.append() = src;
156 }
157
158 int id() const { return fID; }
159 void reset() { fOutputPos = -1; }
160
161 int outputPos() const { return fOutputPos; }
162
163 // check that the topological sort is valid for this node
164 bool check() {
165 if (-1 == fOutputPos) {
166 return false;
167 }
168
169 for (int i = 0; i < fDependencies.count(); ++i) {
170 if (-1 == fDependencies[i]->outputPos()) {
171 return false;
172 }
173 // This node should've been output after all the nodes on which it depends
174 if (fOutputPos < fDependencies[i]->outputPos()) {
175 return false;
176 }
177 }
178
179 return true;
180 }
181
182 // The following 7 methods are needed by the topological sort
183 static void SetTempMark(TopoTestNode* node) { node->fTempMark = true; }
184 static void ResetTempMark(TopoTestNode* node) { node->fTempMark = false; }
185 static bool IsTempMarked(TopoTestNode* node) { return node->fTempMark; }
halcanary9d524f22016-03-29 09:03:52 -0700186 static void Output(TopoTestNode* node, int outputPos) {
robertphillips423f6462015-10-19 12:15:55 -0700187 SkASSERT(-1 != outputPos);
halcanary9d524f22016-03-29 09:03:52 -0700188 node->fOutputPos = outputPos;
robertphillips423f6462015-10-19 12:15:55 -0700189 }
190 static bool WasOutput(TopoTestNode* node) { return (-1 != node->fOutputPos); }
191 static int NumDependencies(TopoTestNode* node) { return node->fDependencies.count(); }
halcanary9d524f22016-03-29 09:03:52 -0700192 static TopoTestNode* Dependency(TopoTestNode* node, int index) {
robertphillips423f6462015-10-19 12:15:55 -0700193 return node->fDependencies[index];
194 }
195
196 // Helper functions for TopoSortBench & TopoSortTest
197 static void AllocNodes(SkTDArray<TopoTestNode*>* graph, int num) {
198 graph->setReserve(num);
199
200 for (int i = 0; i < num; ++i) {
201 *graph->append() = new TopoTestNode(i);
202 }
203 }
204
205 static void DeallocNodes(SkTDArray<TopoTestNode*>* graph) {
206 for (int i = 0; i < graph->count(); ++i) {
207 delete (*graph)[i];
208 }
209 }
210
211 #ifdef SK_DEBUG
212 static void Print(const SkTDArray<TopoTestNode*>& graph) {
213 for (int i = 0; i < graph.count(); ++i) {
214 SkDebugf("%d, ", graph[i]->id());
215 }
216 SkDebugf("\n");
217 }
218 #endif
219
220 // randomize the array
221 static void Shuffle(SkTDArray<TopoTestNode*>* graph, SkRandom* rand) {
222 for (int i = graph->count()-1; i > 0; --i) {
223 int swap = rand->nextU() % (i+1);
224
225 TopoTestNode* tmp = (*graph)[i];
226 (*graph)[i] = (*graph)[swap];
227 (*graph)[swap] = tmp;
228 }
229 }
230
231 private:
232 int fID;
233 int fOutputPos;
234 bool fTempMark;
235
236 SkTDArray<TopoTestNode*> fDependencies;
237 };
238
Hal Canarydb683012016-11-23 08:55:18 -0700239 template <typename T>
240 inline bool EncodeImageToFile(const char* path, const T& src, SkEncodedImageFormat f, int q) {
241 SkFILEWStream file(path);
242 return file.isValid() && SkEncodeImage(&file, src, f, q);
243 }
244
245 template <typename T>
246 inline sk_sp<SkData> EncodeImageToData(const T& src, SkEncodedImageFormat f, int q) {
247 SkDynamicMemoryWStream buf;
248 return SkEncodeImage(&buf, src , f, q) ? buf.detachAsData() : nullptr;
249 }
250
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400251 bool copy_to(SkBitmap* dst, SkColorType dstCT, const SkBitmap& src);
Matt Sarett4897fb82017-01-18 11:49:33 -0500252 void copy_to_g8(SkBitmap* dst, const SkBitmap& src);
tfarina20108912014-06-21 10:54:17 -0700253} // namespace sk_tool_utils
254
255#endif // sk_tool_utils_DEFINED