blob: b097757ccd62faf325c1bce779e7bd71332ae13e [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;
22class SkPaint;
joshualitt98d2e2f2015-10-05 07:23:30 -070023class SkPath;
robertphillips276d3282016-08-04 09:03:19 -070024class SkRRect;
halcanaryb0cce2c2015-01-26 12:49:00 -080025class SkShader;
Cary Clark992c7b02014-07-31 08:58:44 -040026class SkTestFont;
joshualitt9e36c1a2015-04-14 12:17:27 -070027class SkTextBlobBuilder;
skia.committer@gmail.comdb0c8752014-03-18 03:02:11 +000028
Cary Clark992c7b02014-07-31 08:58:44 -040029namespace sk_tool_utils {
caryclark5fb6bd42014-06-23 11:25:00 -070030
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000031 const char* colortype_name(SkColorType);
caryclark37213552015-07-24 11:08:01 -070032
33 /**
34 * Map opaque colors from 8888 to 565.
35 */
caryclark65cdba62015-06-15 06:51:08 -070036 SkColor color_to_565(SkColor color);
caryclark37213552015-07-24 11:08:01 -070037
38 /**
39 * Return a color emoji typeface if available.
40 */
bungeman13b9c952016-05-12 10:09:30 -070041 sk_sp<SkTypeface> emoji_typeface();
caryclark37213552015-07-24 11:08:01 -070042
43 /**
44 * If the platform supports color emoji, return sample text the emoji can render.
45 */
caryclarkc3dcb672015-07-21 12:27:36 -070046 const char* emoji_sample_text();
caryclark37213552015-07-24 11:08:01 -070047
48 /**
49 * If the platform supports color emoji, return the type (i.e. "CBDT", "SBIX", "").
50 */
caryclarkc3dcb672015-07-21 12:27:36 -070051 const char* platform_os_emoji();
caryclark37213552015-07-24 11:08:01 -070052
53 /**
54 * Return the platform name with the version number ("Mac10.9", "Win8", etc.) if available.
55 */
caryclark6531c362015-07-20 13:38:56 -070056 const char* platform_os_name();
caryclark37213552015-07-24 11:08:01 -070057
58 /**
59 * Return the platform name without the version number ("Mac", "Win", etc.) if available.
60 */
61 SkString major_platform_os_name();
62
63 /**
64 * Return the platform extra config (e.g. "GDI") if available.
65 */
caryclark6531c362015-07-20 13:38:56 -070066 const char* platform_extra_config(const char* config);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000067
68 /**
caryclark37213552015-07-24 11:08:01 -070069 * Map serif, san-serif, and monospace to the platform-specific font name.
70 */
71 const char* platform_font_name(const char* name);
72
73 /**
caryclark1818acb2015-07-24 12:09:25 -070074 * Sets the paint to use a platform-independent text renderer
caryclark5fb6bd42014-06-23 11:25:00 -070075 */
halcanary96fcdcc2015-08-27 07:41:13 -070076 void set_portable_typeface(SkPaint* paint, const char* name = nullptr,
mbocee6a9912016-05-31 11:42:36 -070077 SkFontStyle style = SkFontStyle());
caryclark37213552015-07-24 11:08:01 -070078
79 /**
caryclark37213552015-07-24 11:08:01 -070080 * Returns a platform-independent text renderer.
81 */
mbocee6a9912016-05-31 11:42:36 -070082 sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style);
caryclark37213552015-07-24 11:08:01 -070083
caryclarkf53ce802015-06-15 06:48:30 -070084 /** Call to clean up portable font references. */
85 void release_portable_typefaces();
caryclark5fb6bd42014-06-23 11:25:00 -070086
87 /**
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000088 * Call canvas->writePixels() by using the pixels from bitmap, but with an info that claims
89 * the pixels are colorType + alphaType
90 */
91 void write_pixels(SkCanvas*, const SkBitmap&, int x, int y, SkColorType, SkAlphaType);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000092
Cary Clark992c7b02014-07-31 08:58:44 -040093 // private to sk_tool_utils
mbocee6a9912016-05-31 11:42:36 -070094 sk_sp<SkTypeface> create_font(const char* name, SkFontStyle);
Cary Clark992c7b02014-07-31 08:58:44 -040095
halcanaryb0cce2c2015-01-26 12:49:00 -080096 /** Returns a newly created CheckerboardShader. */
reed8a21c9f2016-03-08 18:50:00 -080097 sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size);
halcanaryb0cce2c2015-01-26 12:49:00 -080098
99 /** Draw a checkerboard pattern in the current canvas, restricted to
halcanaryf77365f2015-01-27 08:38:35 -0800100 the current clip, using SkXfermode::kSrc_Mode. */
halcanaryb0cce2c2015-01-26 12:49:00 -0800101 void draw_checkerboard(SkCanvas* canvas,
102 SkColor color1,
103 SkColor color2,
robertphillips943a4622015-09-03 13:32:33 -0700104 int checkSize);
105
106 /** Make it easier to create a bitmap-based checkerboard */
107 SkBitmap create_checkerboard_bitmap(int w, int h,
108 SkColor c1, SkColor c2,
109 int checkSize);
halcanaryb0cce2c2015-01-26 12:49:00 -0800110
111 /** A default checkerboard. */
112 inline void draw_checkerboard(SkCanvas* canvas) {
113 sk_tool_utils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
114 }
115
robertphillips943a4622015-09-03 13:32:33 -0700116 SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
117 int textSize, const char* str);
118
joshualitt9e36c1a2015-04-14 12:17:27 -0700119 // A helper for inserting a drawtext call into a SkTextBlobBuilder
120 void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkPaint& origPaint,
121 SkScalar x, SkScalar y);
122
robertphillipsea4529d2015-08-17 15:04:47 -0700123 void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst);
124
125 void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst);
126
127 void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst);
128
joshualitt98d2e2f2015-10-05 07:23:30 -0700129 void make_big_path(SkPath& path);
robertphillips9c4909b2015-10-19 06:39:17 -0700130
131 // Return a blurred version of 'src'. This doesn't use a separable filter
132 // so it is slow!
133 SkBitmap slow_blur(const SkBitmap& src, float sigma);
134
robertphillips276d3282016-08-04 09:03:19 -0700135 SkRect compute_central_occluder(const SkRRect& rr);
robertphillips401c1962016-08-04 12:35:46 -0700136 SkRect compute_widest_occluder(const SkRRect& rr);
137 SkRect compute_tallest_occluder(const SkRRect& rr);
robertphillips276d3282016-08-04 09:03:19 -0700138
robertphillips423f6462015-10-19 12:15:55 -0700139 // A helper object to test the topological sorting code (TopoSortBench.cpp & TopoSortTest.cpp)
140 class TopoTestNode {
141 public:
142 TopoTestNode(int id) : fID(id), fOutputPos(-1), fTempMark(false) { }
143
144 void dependsOn(TopoTestNode* src) {
145 *fDependencies.append() = src;
146 }
147
148 int id() const { return fID; }
149 void reset() { fOutputPos = -1; }
150
151 int outputPos() const { return fOutputPos; }
152
153 // check that the topological sort is valid for this node
154 bool check() {
155 if (-1 == fOutputPos) {
156 return false;
157 }
158
159 for (int i = 0; i < fDependencies.count(); ++i) {
160 if (-1 == fDependencies[i]->outputPos()) {
161 return false;
162 }
163 // This node should've been output after all the nodes on which it depends
164 if (fOutputPos < fDependencies[i]->outputPos()) {
165 return false;
166 }
167 }
168
169 return true;
170 }
171
172 // The following 7 methods are needed by the topological sort
173 static void SetTempMark(TopoTestNode* node) { node->fTempMark = true; }
174 static void ResetTempMark(TopoTestNode* node) { node->fTempMark = false; }
175 static bool IsTempMarked(TopoTestNode* node) { return node->fTempMark; }
halcanary9d524f22016-03-29 09:03:52 -0700176 static void Output(TopoTestNode* node, int outputPos) {
robertphillips423f6462015-10-19 12:15:55 -0700177 SkASSERT(-1 != outputPos);
halcanary9d524f22016-03-29 09:03:52 -0700178 node->fOutputPos = outputPos;
robertphillips423f6462015-10-19 12:15:55 -0700179 }
180 static bool WasOutput(TopoTestNode* node) { return (-1 != node->fOutputPos); }
181 static int NumDependencies(TopoTestNode* node) { return node->fDependencies.count(); }
halcanary9d524f22016-03-29 09:03:52 -0700182 static TopoTestNode* Dependency(TopoTestNode* node, int index) {
robertphillips423f6462015-10-19 12:15:55 -0700183 return node->fDependencies[index];
184 }
185
186 // Helper functions for TopoSortBench & TopoSortTest
187 static void AllocNodes(SkTDArray<TopoTestNode*>* graph, int num) {
188 graph->setReserve(num);
189
190 for (int i = 0; i < num; ++i) {
191 *graph->append() = new TopoTestNode(i);
192 }
193 }
194
195 static void DeallocNodes(SkTDArray<TopoTestNode*>* graph) {
196 for (int i = 0; i < graph->count(); ++i) {
197 delete (*graph)[i];
198 }
199 }
200
201 #ifdef SK_DEBUG
202 static void Print(const SkTDArray<TopoTestNode*>& graph) {
203 for (int i = 0; i < graph.count(); ++i) {
204 SkDebugf("%d, ", graph[i]->id());
205 }
206 SkDebugf("\n");
207 }
208 #endif
209
210 // randomize the array
211 static void Shuffle(SkTDArray<TopoTestNode*>* graph, SkRandom* rand) {
212 for (int i = graph->count()-1; i > 0; --i) {
213 int swap = rand->nextU() % (i+1);
214
215 TopoTestNode* tmp = (*graph)[i];
216 (*graph)[i] = (*graph)[swap];
217 (*graph)[swap] = tmp;
218 }
219 }
220
221 private:
222 int fID;
223 int fOutputPos;
224 bool fTempMark;
225
226 SkTDArray<TopoTestNode*> fDependencies;
227 };
228
Hal Canarydb683012016-11-23 08:55:18 -0700229 template <typename T>
230 inline bool EncodeImageToFile(const char* path, const T& src, SkEncodedImageFormat f, int q) {
231 SkFILEWStream file(path);
232 return file.isValid() && SkEncodeImage(&file, src, f, q);
233 }
234
235 template <typename T>
236 inline sk_sp<SkData> EncodeImageToData(const T& src, SkEncodedImageFormat f, int q) {
237 SkDynamicMemoryWStream buf;
238 return SkEncodeImage(&buf, src , f, q) ? buf.detachAsData() : nullptr;
239 }
240
241 /**
242 * Uses SkEncodeImage to serialize images that are not already
243 * encoded as SkEncodedImageFormat::kPNG images.
244 */
245 inline sk_sp<SkPixelSerializer> MakePixelSerializer() {
246 struct EncodeImagePixelSerializer final : SkPixelSerializer {
247 bool onUseEncodedData(const void*, size_t) override { return true; }
248 SkData* onEncode(const SkPixmap& pmap) override {
249 return EncodeImageToData(pmap, SkEncodedImageFormat::kPNG, 100).release();
250 }
251 };
252 return sk_make_sp<EncodeImagePixelSerializer>();
253 }
tfarina20108912014-06-21 10:54:17 -0700254} // namespace sk_tool_utils
255
256#endif // sk_tool_utils_DEFINED