blob: 1d80731fd4a67f86e267a7d7f21abcd30b9d1a81 [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;
halcanaryb0cce2c2015-01-26 12:49:00 -080023class SkShader;
Cary Clark992c7b02014-07-31 08:58:44 -040024class SkTestFont;
joshualitt9e36c1a2015-04-14 12:17:27 -070025class SkTextBlobBuilder;
skia.committer@gmail.comdb0c8752014-03-18 03:02:11 +000026
Cary Clark992c7b02014-07-31 08:58:44 -040027namespace sk_tool_utils {
caryclark5fb6bd42014-06-23 11:25:00 -070028
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000029 const char* colortype_name(SkColorType);
caryclark37213552015-07-24 11:08:01 -070030
31 /**
32 * Map opaque colors from 8888 to 565.
33 */
caryclark65cdba62015-06-15 06:51:08 -070034 SkColor color_to_565(SkColor color);
caryclark37213552015-07-24 11:08:01 -070035
36 /**
37 * Return a color emoji typeface if available.
38 */
caryclark3b0f98e2015-07-23 11:52:01 -070039 void emoji_typeface(SkAutoTUnref<SkTypeface>* );
caryclark37213552015-07-24 11:08:01 -070040
41 /**
42 * If the platform supports color emoji, return sample text the emoji can render.
43 */
caryclarkc3dcb672015-07-21 12:27:36 -070044 const char* emoji_sample_text();
caryclark37213552015-07-24 11:08:01 -070045
46 /**
47 * If the platform supports color emoji, return the type (i.e. "CBDT", "SBIX", "").
48 */
caryclarkc3dcb672015-07-21 12:27:36 -070049 const char* platform_os_emoji();
caryclark37213552015-07-24 11:08:01 -070050
51 /**
52 * Return the platform name with the version number ("Mac10.9", "Win8", etc.) if available.
53 */
caryclark6531c362015-07-20 13:38:56 -070054 const char* platform_os_name();
caryclark37213552015-07-24 11:08:01 -070055
56 /**
57 * Return the platform name without the version number ("Mac", "Win", etc.) if available.
58 */
59 SkString major_platform_os_name();
60
61 /**
62 * Return the platform extra config (e.g. "GDI") if available.
63 */
caryclark6531c362015-07-20 13:38:56 -070064 const char* platform_extra_config(const char* config);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000065
66 /**
caryclark37213552015-07-24 11:08:01 -070067 * Map serif, san-serif, and monospace to the platform-specific font name.
68 */
69 const char* platform_font_name(const char* name);
70
71 /**
caryclark1818acb2015-07-24 12:09:25 -070072 * Sets the paint to use a platform-independent text renderer
caryclark5fb6bd42014-06-23 11:25:00 -070073 */
halcanary96fcdcc2015-08-27 07:41:13 -070074 void set_portable_typeface(SkPaint* paint, const char* name = nullptr,
Cary Clark992c7b02014-07-31 08:58:44 -040075 SkTypeface::Style style = SkTypeface::kNormal);
caryclark37213552015-07-24 11:08:01 -070076
77 /**
caryclark37213552015-07-24 11:08:01 -070078 * Returns a platform-independent text renderer.
79 */
caryclark1818acb2015-07-24 12:09:25 -070080 SkTypeface* create_portable_typeface(const char* name, SkTypeface::Style style);
caryclark37213552015-07-24 11:08:01 -070081
caryclarkf53ce802015-06-15 06:48:30 -070082 /** Call to clean up portable font references. */
83 void release_portable_typefaces();
caryclark5fb6bd42014-06-23 11:25:00 -070084
85 /**
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000086 * Call canvas->writePixels() by using the pixels from bitmap, but with an info that claims
87 * the pixels are colorType + alphaType
88 */
89 void write_pixels(SkCanvas*, const SkBitmap&, int x, int y, SkColorType, SkAlphaType);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000090
Cary Clark992c7b02014-07-31 08:58:44 -040091 // private to sk_tool_utils
robertphillips943a4622015-09-03 13:32:33 -070092 SkTypeface* create_font(const char* name, SkTypeface::Style);
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
joshualitt9e36c1a2015-04-14 12:17:27 -0700117 // A helper for inserting a drawtext call into a SkTextBlobBuilder
118 void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkPaint& origPaint,
119 SkScalar x, SkScalar y);
120
robertphillipsea4529d2015-08-17 15:04:47 -0700121 void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst);
122
123 void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst);
124
125 void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst);
126
joshualitt98d2e2f2015-10-05 07:23:30 -0700127 void make_big_path(SkPath& path);
robertphillips9c4909b2015-10-19 06:39:17 -0700128
129 // Return a blurred version of 'src'. This doesn't use a separable filter
130 // so it is slow!
131 SkBitmap slow_blur(const SkBitmap& src, float sigma);
132
robertphillips423f6462015-10-19 12:15:55 -0700133 // A helper object to test the topological sorting code (TopoSortBench.cpp & TopoSortTest.cpp)
134 class TopoTestNode {
135 public:
136 TopoTestNode(int id) : fID(id), fOutputPos(-1), fTempMark(false) { }
137
138 void dependsOn(TopoTestNode* src) {
139 *fDependencies.append() = src;
140 }
141
142 int id() const { return fID; }
143 void reset() { fOutputPos = -1; }
144
145 int outputPos() const { return fOutputPos; }
146
147 // check that the topological sort is valid for this node
148 bool check() {
149 if (-1 == fOutputPos) {
150 return false;
151 }
152
153 for (int i = 0; i < fDependencies.count(); ++i) {
154 if (-1 == fDependencies[i]->outputPos()) {
155 return false;
156 }
157 // This node should've been output after all the nodes on which it depends
158 if (fOutputPos < fDependencies[i]->outputPos()) {
159 return false;
160 }
161 }
162
163 return true;
164 }
165
166 // The following 7 methods are needed by the topological sort
167 static void SetTempMark(TopoTestNode* node) { node->fTempMark = true; }
168 static void ResetTempMark(TopoTestNode* node) { node->fTempMark = false; }
169 static bool IsTempMarked(TopoTestNode* node) { return node->fTempMark; }
halcanary9d524f22016-03-29 09:03:52 -0700170 static void Output(TopoTestNode* node, int outputPos) {
robertphillips423f6462015-10-19 12:15:55 -0700171 SkASSERT(-1 != outputPos);
halcanary9d524f22016-03-29 09:03:52 -0700172 node->fOutputPos = outputPos;
robertphillips423f6462015-10-19 12:15:55 -0700173 }
174 static bool WasOutput(TopoTestNode* node) { return (-1 != node->fOutputPos); }
175 static int NumDependencies(TopoTestNode* node) { return node->fDependencies.count(); }
halcanary9d524f22016-03-29 09:03:52 -0700176 static TopoTestNode* Dependency(TopoTestNode* node, int index) {
robertphillips423f6462015-10-19 12:15:55 -0700177 return node->fDependencies[index];
178 }
179
180 // Helper functions for TopoSortBench & TopoSortTest
181 static void AllocNodes(SkTDArray<TopoTestNode*>* graph, int num) {
182 graph->setReserve(num);
183
184 for (int i = 0; i < num; ++i) {
185 *graph->append() = new TopoTestNode(i);
186 }
187 }
188
189 static void DeallocNodes(SkTDArray<TopoTestNode*>* graph) {
190 for (int i = 0; i < graph->count(); ++i) {
191 delete (*graph)[i];
192 }
193 }
194
195 #ifdef SK_DEBUG
196 static void Print(const SkTDArray<TopoTestNode*>& graph) {
197 for (int i = 0; i < graph.count(); ++i) {
198 SkDebugf("%d, ", graph[i]->id());
199 }
200 SkDebugf("\n");
201 }
202 #endif
203
204 // randomize the array
205 static void Shuffle(SkTDArray<TopoTestNode*>* graph, SkRandom* rand) {
206 for (int i = graph->count()-1; i > 0; --i) {
207 int swap = rand->nextU() % (i+1);
208
209 TopoTestNode* tmp = (*graph)[i];
210 (*graph)[i] = (*graph)[swap];
211 (*graph)[swap] = tmp;
212 }
213 }
214
215 private:
216 int fID;
217 int fOutputPos;
218 bool fTempMark;
219
220 SkTDArray<TopoTestNode*> fDependencies;
221 };
222
tfarina20108912014-06-21 10:54:17 -0700223} // namespace sk_tool_utils
224
225#endif // sk_tool_utils_DEFINED