blob: c6abbcf7c2316c604ba59b06b30f03ff07416b55 [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"
16#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;
21class SkPaint;
joshualitt98d2e2f2015-10-05 07:23:30 -070022class SkPath;
robertphillips276d3282016-08-04 09:03:19 -070023class SkRRect;
halcanaryb0cce2c2015-01-26 12:49:00 -080024class SkShader;
Cary Clark992c7b02014-07-31 08:58:44 -040025class SkTestFont;
joshualitt9e36c1a2015-04-14 12:17:27 -070026class SkTextBlobBuilder;
skia.committer@gmail.comdb0c8752014-03-18 03:02:11 +000027
Cary Clark992c7b02014-07-31 08:58:44 -040028namespace sk_tool_utils {
caryclark5fb6bd42014-06-23 11:25:00 -070029
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000030 const char* colortype_name(SkColorType);
caryclark37213552015-07-24 11:08:01 -070031
32 /**
33 * Map opaque colors from 8888 to 565.
34 */
caryclark65cdba62015-06-15 06:51:08 -070035 SkColor color_to_565(SkColor color);
caryclark37213552015-07-24 11:08:01 -070036
37 /**
38 * Return a color emoji typeface if available.
39 */
bungeman13b9c952016-05-12 10:09:30 -070040 sk_sp<SkTypeface> emoji_typeface();
caryclark37213552015-07-24 11:08:01 -070041
42 /**
43 * If the platform supports color emoji, return sample text the emoji can render.
44 */
caryclarkc3dcb672015-07-21 12:27:36 -070045 const char* emoji_sample_text();
caryclark37213552015-07-24 11:08:01 -070046
47 /**
48 * If the platform supports color emoji, return the type (i.e. "CBDT", "SBIX", "").
49 */
caryclarkc3dcb672015-07-21 12:27:36 -070050 const char* platform_os_emoji();
caryclark37213552015-07-24 11:08:01 -070051
52 /**
53 * Return the platform name with the version number ("Mac10.9", "Win8", etc.) if available.
54 */
caryclark6531c362015-07-20 13:38:56 -070055 const char* platform_os_name();
caryclark37213552015-07-24 11:08:01 -070056
57 /**
58 * Return the platform name without the version number ("Mac", "Win", etc.) if available.
59 */
60 SkString major_platform_os_name();
61
62 /**
63 * Return the platform extra config (e.g. "GDI") if available.
64 */
caryclark6531c362015-07-20 13:38:56 -070065 const char* platform_extra_config(const char* config);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000066
67 /**
caryclark37213552015-07-24 11:08:01 -070068 * Map serif, san-serif, and monospace to the platform-specific font name.
69 */
70 const char* platform_font_name(const char* name);
71
72 /**
caryclark1818acb2015-07-24 12:09:25 -070073 * Sets the paint to use a platform-independent text renderer
caryclark5fb6bd42014-06-23 11:25:00 -070074 */
halcanary96fcdcc2015-08-27 07:41:13 -070075 void set_portable_typeface(SkPaint* paint, const char* name = nullptr,
mbocee6a9912016-05-31 11:42:36 -070076 SkFontStyle style = SkFontStyle());
caryclark37213552015-07-24 11:08:01 -070077
78 /**
caryclark37213552015-07-24 11:08:01 -070079 * Returns a platform-independent text renderer.
80 */
mbocee6a9912016-05-31 11:42:36 -070081 sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style);
caryclark37213552015-07-24 11:08:01 -070082
caryclarkf53ce802015-06-15 06:48:30 -070083 /** Call to clean up portable font references. */
84 void release_portable_typefaces();
caryclark5fb6bd42014-06-23 11:25:00 -070085
86 /**
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000087 * Call canvas->writePixels() by using the pixels from bitmap, but with an info that claims
88 * the pixels are colorType + alphaType
89 */
90 void write_pixels(SkCanvas*, const SkBitmap&, int x, int y, SkColorType, SkAlphaType);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000091
Cary Clark992c7b02014-07-31 08:58:44 -040092 // private to sk_tool_utils
mbocee6a9912016-05-31 11:42:36 -070093 sk_sp<SkTypeface> create_font(const char* name, SkFontStyle);
Cary Clark992c7b02014-07-31 08:58:44 -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
joshualitt9e36c1a2015-04-14 12:17:27 -0700118 // A helper for inserting a drawtext call into a SkTextBlobBuilder
119 void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkPaint& origPaint,
120 SkScalar x, SkScalar y);
121
robertphillipsea4529d2015-08-17 15:04:47 -0700122 void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst);
123
124 void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst);
125
126 void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst);
127
joshualitt98d2e2f2015-10-05 07:23:30 -0700128 void make_big_path(SkPath& path);
robertphillips9c4909b2015-10-19 06:39:17 -0700129
130 // Return a blurred version of 'src'. This doesn't use a separable filter
131 // so it is slow!
132 SkBitmap slow_blur(const SkBitmap& src, float sigma);
133
robertphillips276d3282016-08-04 09:03:19 -0700134 SkRect compute_central_occluder(const SkRRect& rr);
robertphillips401c1962016-08-04 12:35:46 -0700135 SkRect compute_widest_occluder(const SkRRect& rr);
136 SkRect compute_tallest_occluder(const SkRRect& rr);
robertphillips276d3282016-08-04 09:03:19 -0700137
robertphillips423f6462015-10-19 12:15:55 -0700138 // A helper object to test the topological sorting code (TopoSortBench.cpp & TopoSortTest.cpp)
139 class TopoTestNode {
140 public:
141 TopoTestNode(int id) : fID(id), fOutputPos(-1), fTempMark(false) { }
142
143 void dependsOn(TopoTestNode* src) {
144 *fDependencies.append() = src;
145 }
146
147 int id() const { return fID; }
148 void reset() { fOutputPos = -1; }
149
150 int outputPos() const { return fOutputPos; }
151
152 // check that the topological sort is valid for this node
153 bool check() {
154 if (-1 == fOutputPos) {
155 return false;
156 }
157
158 for (int i = 0; i < fDependencies.count(); ++i) {
159 if (-1 == fDependencies[i]->outputPos()) {
160 return false;
161 }
162 // This node should've been output after all the nodes on which it depends
163 if (fOutputPos < fDependencies[i]->outputPos()) {
164 return false;
165 }
166 }
167
168 return true;
169 }
170
171 // The following 7 methods are needed by the topological sort
172 static void SetTempMark(TopoTestNode* node) { node->fTempMark = true; }
173 static void ResetTempMark(TopoTestNode* node) { node->fTempMark = false; }
174 static bool IsTempMarked(TopoTestNode* node) { return node->fTempMark; }
halcanary9d524f22016-03-29 09:03:52 -0700175 static void Output(TopoTestNode* node, int outputPos) {
robertphillips423f6462015-10-19 12:15:55 -0700176 SkASSERT(-1 != outputPos);
halcanary9d524f22016-03-29 09:03:52 -0700177 node->fOutputPos = outputPos;
robertphillips423f6462015-10-19 12:15:55 -0700178 }
179 static bool WasOutput(TopoTestNode* node) { return (-1 != node->fOutputPos); }
180 static int NumDependencies(TopoTestNode* node) { return node->fDependencies.count(); }
halcanary9d524f22016-03-29 09:03:52 -0700181 static TopoTestNode* Dependency(TopoTestNode* node, int index) {
robertphillips423f6462015-10-19 12:15:55 -0700182 return node->fDependencies[index];
183 }
184
185 // Helper functions for TopoSortBench & TopoSortTest
186 static void AllocNodes(SkTDArray<TopoTestNode*>* graph, int num) {
187 graph->setReserve(num);
188
189 for (int i = 0; i < num; ++i) {
190 *graph->append() = new TopoTestNode(i);
191 }
192 }
193
194 static void DeallocNodes(SkTDArray<TopoTestNode*>* graph) {
195 for (int i = 0; i < graph->count(); ++i) {
196 delete (*graph)[i];
197 }
198 }
199
200 #ifdef SK_DEBUG
201 static void Print(const SkTDArray<TopoTestNode*>& graph) {
202 for (int i = 0; i < graph.count(); ++i) {
203 SkDebugf("%d, ", graph[i]->id());
204 }
205 SkDebugf("\n");
206 }
207 #endif
208
209 // randomize the array
210 static void Shuffle(SkTDArray<TopoTestNode*>* graph, SkRandom* rand) {
211 for (int i = graph->count()-1; i > 0; --i) {
212 int swap = rand->nextU() % (i+1);
213
214 TopoTestNode* tmp = (*graph)[i];
215 (*graph)[i] = (*graph)[swap];
216 (*graph)[swap] = tmp;
217 }
218 }
219
220 private:
221 int fID;
222 int fOutputPos;
223 bool fTempMark;
224
225 SkTDArray<TopoTestNode*> fDependencies;
226 };
227
tfarina20108912014-06-21 10:54:17 -0700228} // namespace sk_tool_utils
229
230#endif // sk_tool_utils_DEFINED