blob: b1bde770ea8c4f514de004b427fa99f178e8e68e [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"
robertphillips3e5c2b12015-03-23 05:46:51 -070014#include "SkPixelSerializer.h"
robertphillips423f6462015-10-19 12:15:55 -070015#include "SkRandom.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;
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;
Cary Clark992c7b02014-07-31 08:58:44 -040027class SkTestFont;
joshualitt9e36c1a2015-04-14 12:17:27 -070028class SkTextBlobBuilder;
skia.committer@gmail.comdb0c8752014-03-18 03:02:11 +000029
Cary Clark992c7b02014-07-31 08:58:44 -040030namespace sk_tool_utils {
caryclark5fb6bd42014-06-23 11:25:00 -070031
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000032 const char* colortype_name(SkColorType);
caryclark37213552015-07-24 11:08:01 -070033
34 /**
35 * Map opaque colors from 8888 to 565.
36 */
caryclark65cdba62015-06-15 06:51:08 -070037 SkColor color_to_565(SkColor color);
caryclark37213552015-07-24 11:08:01 -070038
39 /**
40 * Return a color emoji typeface if available.
41 */
bungeman13b9c952016-05-12 10:09:30 -070042 sk_sp<SkTypeface> emoji_typeface();
caryclark37213552015-07-24 11:08:01 -070043
44 /**
45 * If the platform supports color emoji, return sample text the emoji can render.
46 */
caryclarkc3dcb672015-07-21 12:27:36 -070047 const char* emoji_sample_text();
caryclark37213552015-07-24 11:08:01 -070048
49 /**
50 * If the platform supports color emoji, return the type (i.e. "CBDT", "SBIX", "").
51 */
caryclarkc3dcb672015-07-21 12:27:36 -070052 const char* platform_os_emoji();
caryclark37213552015-07-24 11:08:01 -070053
54 /**
55 * Return the platform name with the version number ("Mac10.9", "Win8", etc.) if available.
56 */
caryclark6531c362015-07-20 13:38:56 -070057 const char* platform_os_name();
caryclark37213552015-07-24 11:08:01 -070058
59 /**
60 * Return the platform name without the version number ("Mac", "Win", etc.) if available.
61 */
62 SkString major_platform_os_name();
63
64 /**
65 * Return the platform extra config (e.g. "GDI") if available.
66 */
caryclark6531c362015-07-20 13:38:56 -070067 const char* platform_extra_config(const char* config);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000068
69 /**
caryclark37213552015-07-24 11:08:01 -070070 * Map serif, san-serif, and monospace to the platform-specific font name.
71 */
72 const char* platform_font_name(const char* name);
73
74 /**
caryclark1818acb2015-07-24 12:09:25 -070075 * Sets the paint to use a platform-independent text renderer
caryclark5fb6bd42014-06-23 11:25:00 -070076 */
halcanary96fcdcc2015-08-27 07:41:13 -070077 void set_portable_typeface(SkPaint* paint, const char* name = nullptr,
mbocee6a9912016-05-31 11:42:36 -070078 SkFontStyle style = SkFontStyle());
caryclark37213552015-07-24 11:08:01 -070079
80 /**
caryclark37213552015-07-24 11:08:01 -070081 * Returns a platform-independent text renderer.
82 */
mbocee6a9912016-05-31 11:42:36 -070083 sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style);
caryclark37213552015-07-24 11:08:01 -070084
caryclarkf53ce802015-06-15 06:48:30 -070085 /** Call to clean up portable font references. */
86 void release_portable_typefaces();
caryclark5fb6bd42014-06-23 11:25:00 -070087
88 /**
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000089 * Call canvas->writePixels() by using the pixels from bitmap, but with an info that claims
90 * the pixels are colorType + alphaType
91 */
92 void write_pixels(SkCanvas*, const SkBitmap&, int x, int y, SkColorType, SkAlphaType);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000093
Mike Reed5a625e02017-08-08 15:48:54 -040094 /**
95 * Returns true iff all of the pixels between the two images differ by <= the maxDiff value
96 * per component.
97 *
98 * If the configs differ, return false.
99 *
100 * If the colorType is half-float, then maxDiff is interpreted as 0..255 --> 0..1
101 */
102 bool equal_pixels(const SkPixmap&, const SkPixmap&, unsigned maxDiff = 0);
103 bool equal_pixels(const SkBitmap&, const SkBitmap&, unsigned maxDiff = 0);
104
Cary Clark992c7b02014-07-31 08:58:44 -0400105 // private to sk_tool_utils
mbocee6a9912016-05-31 11:42:36 -0700106 sk_sp<SkTypeface> create_font(const char* name, SkFontStyle);
Cary Clark992c7b02014-07-31 08:58:44 -0400107
halcanaryb0cce2c2015-01-26 12:49:00 -0800108 /** Returns a newly created CheckerboardShader. */
reed8a21c9f2016-03-08 18:50:00 -0800109 sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size);
halcanaryb0cce2c2015-01-26 12:49:00 -0800110
111 /** Draw a checkerboard pattern in the current canvas, restricted to
halcanaryf77365f2015-01-27 08:38:35 -0800112 the current clip, using SkXfermode::kSrc_Mode. */
halcanaryb0cce2c2015-01-26 12:49:00 -0800113 void draw_checkerboard(SkCanvas* canvas,
114 SkColor color1,
115 SkColor color2,
robertphillips943a4622015-09-03 13:32:33 -0700116 int checkSize);
117
118 /** Make it easier to create a bitmap-based checkerboard */
119 SkBitmap create_checkerboard_bitmap(int w, int h,
120 SkColor c1, SkColor c2,
121 int checkSize);
halcanaryb0cce2c2015-01-26 12:49:00 -0800122
123 /** A default checkerboard. */
124 inline void draw_checkerboard(SkCanvas* canvas) {
125 sk_tool_utils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
126 }
127
robertphillips943a4622015-09-03 13:32:33 -0700128 SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
129 int textSize, const char* str);
130
joshualitt9e36c1a2015-04-14 12:17:27 -0700131 // A helper for inserting a drawtext call into a SkTextBlobBuilder
Robert Phillips4c72b262017-08-15 13:28:42 -0400132 void add_to_text_blob_w_len(SkTextBlobBuilder* builder, const char* text, size_t len,
133 const SkPaint& origPaint, SkScalar x, SkScalar y);
134
135 void add_to_text_blob(SkTextBlobBuilder* builder, const char* text,
136 const SkPaint& origPaint, SkScalar x, SkScalar y);
joshualitt9e36c1a2015-04-14 12:17:27 -0700137
joshualitt98d2e2f2015-10-05 07:23:30 -0700138 void make_big_path(SkPath& path);
robertphillips9c4909b2015-10-19 06:39:17 -0700139
140 // Return a blurred version of 'src'. This doesn't use a separable filter
141 // so it is slow!
142 SkBitmap slow_blur(const SkBitmap& src, float sigma);
143
robertphillips276d3282016-08-04 09:03:19 -0700144 SkRect compute_central_occluder(const SkRRect& rr);
robertphillips401c1962016-08-04 12:35:46 -0700145 SkRect compute_widest_occluder(const SkRRect& rr);
146 SkRect compute_tallest_occluder(const SkRRect& rr);
robertphillips276d3282016-08-04 09:03:19 -0700147
robertphillips423f6462015-10-19 12:15:55 -0700148 // A helper object to test the topological sorting code (TopoSortBench.cpp & TopoSortTest.cpp)
149 class TopoTestNode {
150 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
196 static void AllocNodes(SkTDArray<TopoTestNode*>* graph, int num) {
197 graph->setReserve(num);
198
199 for (int i = 0; i < num; ++i) {
200 *graph->append() = new TopoTestNode(i);
201 }
202 }
203
204 static void DeallocNodes(SkTDArray<TopoTestNode*>* graph) {
205 for (int i = 0; i < graph->count(); ++i) {
206 delete (*graph)[i];
207 }
208 }
209
210 #ifdef SK_DEBUG
211 static void Print(const SkTDArray<TopoTestNode*>& graph) {
212 for (int i = 0; i < graph.count(); ++i) {
213 SkDebugf("%d, ", graph[i]->id());
214 }
215 SkDebugf("\n");
216 }
217 #endif
218
219 // randomize the array
220 static void Shuffle(SkTDArray<TopoTestNode*>* graph, SkRandom* rand) {
221 for (int i = graph->count()-1; i > 0; --i) {
222 int swap = rand->nextU() % (i+1);
223
224 TopoTestNode* tmp = (*graph)[i];
225 (*graph)[i] = (*graph)[swap];
226 (*graph)[swap] = tmp;
227 }
228 }
229
230 private:
231 int fID;
232 int fOutputPos;
233 bool fTempMark;
234
235 SkTDArray<TopoTestNode*> fDependencies;
236 };
237
Hal Canarydb683012016-11-23 08:55:18 -0700238 template <typename T>
239 inline bool EncodeImageToFile(const char* path, const T& src, SkEncodedImageFormat f, int q) {
240 SkFILEWStream file(path);
241 return file.isValid() && SkEncodeImage(&file, src, f, q);
242 }
243
244 template <typename T>
245 inline sk_sp<SkData> EncodeImageToData(const T& src, SkEncodedImageFormat f, int q) {
246 SkDynamicMemoryWStream buf;
247 return SkEncodeImage(&buf, src , f, q) ? buf.detachAsData() : nullptr;
248 }
249
250 /**
251 * Uses SkEncodeImage to serialize images that are not already
252 * encoded as SkEncodedImageFormat::kPNG images.
253 */
254 inline sk_sp<SkPixelSerializer> MakePixelSerializer() {
255 struct EncodeImagePixelSerializer final : SkPixelSerializer {
256 bool onUseEncodedData(const void*, size_t) override { return true; }
257 SkData* onEncode(const SkPixmap& pmap) override {
258 return EncodeImageToData(pmap, SkEncodedImageFormat::kPNG, 100).release();
259 }
260 };
261 return sk_make_sp<EncodeImagePixelSerializer>();
262 }
Matt Sarett4897fb82017-01-18 11:49:33 -0500263
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400264 bool copy_to(SkBitmap* dst, SkColorType dstCT, const SkBitmap& src);
Matt Sarett4897fb82017-01-18 11:49:33 -0500265 void copy_to_g8(SkBitmap* dst, const SkBitmap& src);
266
tfarina20108912014-06-21 10:54:17 -0700267} // namespace sk_tool_utils
268
269#endif // sk_tool_utils_DEFINED