commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 11 | #include "SkColor.h" |
robertphillips | 3e5c2b1 | 2015-03-23 05:46:51 -0700 | [diff] [blame] | 12 | #include "SkImageEncoder.h" |
tfarina | 2010891 | 2014-06-21 10:54:17 -0700 | [diff] [blame] | 13 | #include "SkImageInfo.h" |
robertphillips | 3e5c2b1 | 2015-03-23 05:46:51 -0700 | [diff] [blame] | 14 | #include "SkPixelSerializer.h" |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 15 | #include "SkRandom.h" |
| 16 | #include "SkTDArray.h" |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 17 | #include "SkTypeface.h" |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 18 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 19 | class SkBitmap; |
| 20 | class SkCanvas; |
| 21 | class SkPaint; |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 22 | class SkPath; |
robertphillips | 276d328 | 2016-08-04 09:03:19 -0700 | [diff] [blame] | 23 | class SkRRect; |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 24 | class SkShader; |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 25 | class SkTestFont; |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 26 | class SkTextBlobBuilder; |
skia.committer@gmail.com | db0c875 | 2014-03-18 03:02:11 +0000 | [diff] [blame] | 27 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 28 | namespace sk_tool_utils { |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 29 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 30 | const char* colortype_name(SkColorType); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Map opaque colors from 8888 to 565. |
| 34 | */ |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 35 | SkColor color_to_565(SkColor color); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Return a color emoji typeface if available. |
| 39 | */ |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 40 | sk_sp<SkTypeface> emoji_typeface(); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * If the platform supports color emoji, return sample text the emoji can render. |
| 44 | */ |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 45 | const char* emoji_sample_text(); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * If the platform supports color emoji, return the type (i.e. "CBDT", "SBIX", ""). |
| 49 | */ |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 50 | const char* platform_os_emoji(); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Return the platform name with the version number ("Mac10.9", "Win8", etc.) if available. |
| 54 | */ |
caryclark | 6531c36 | 2015-07-20 13:38:56 -0700 | [diff] [blame] | 55 | const char* platform_os_name(); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 56 | |
| 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 | */ |
caryclark | 6531c36 | 2015-07-20 13:38:56 -0700 | [diff] [blame] | 65 | const char* platform_extra_config(const char* config); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 66 | |
| 67 | /** |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 68 | * Map serif, san-serif, and monospace to the platform-specific font name. |
| 69 | */ |
| 70 | const char* platform_font_name(const char* name); |
| 71 | |
| 72 | /** |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 73 | * Sets the paint to use a platform-independent text renderer |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 74 | */ |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 75 | void set_portable_typeface(SkPaint* paint, const char* name = nullptr, |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 76 | SkFontStyle style = SkFontStyle()); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 77 | |
| 78 | /** |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 79 | * Returns a platform-independent text renderer. |
| 80 | */ |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 81 | sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 82 | |
caryclark | f53ce80 | 2015-06-15 06:48:30 -0700 | [diff] [blame] | 83 | /** Call to clean up portable font references. */ |
| 84 | void release_portable_typefaces(); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 85 | |
| 86 | /** |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 87 | * 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.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 91 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 92 | // private to sk_tool_utils |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 93 | sk_sp<SkTypeface> create_font(const char* name, SkFontStyle); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 94 | |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 95 | /** Returns a newly created CheckerboardShader. */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 96 | sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size); |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 97 | |
| 98 | /** Draw a checkerboard pattern in the current canvas, restricted to |
halcanary | f77365f | 2015-01-27 08:38:35 -0800 | [diff] [blame] | 99 | the current clip, using SkXfermode::kSrc_Mode. */ |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 100 | void draw_checkerboard(SkCanvas* canvas, |
| 101 | SkColor color1, |
| 102 | SkColor color2, |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 103 | 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); |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 109 | |
| 110 | /** A default checkerboard. */ |
| 111 | inline void draw_checkerboard(SkCanvas* canvas) { |
| 112 | sk_tool_utils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8); |
| 113 | } |
| 114 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 115 | SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y, |
| 116 | int textSize, const char* str); |
| 117 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 118 | // 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 | |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame] | 122 | 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 | |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 128 | void make_big_path(SkPath& path); |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 129 | |
| 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 | |
robertphillips | 276d328 | 2016-08-04 09:03:19 -0700 | [diff] [blame] | 134 | SkRect compute_central_occluder(const SkRRect& rr); |
robertphillips | 401c196 | 2016-08-04 12:35:46 -0700 | [diff] [blame] | 135 | SkRect compute_widest_occluder(const SkRRect& rr); |
| 136 | SkRect compute_tallest_occluder(const SkRRect& rr); |
robertphillips | 276d328 | 2016-08-04 09:03:19 -0700 | [diff] [blame] | 137 | |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 138 | // 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; } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 175 | static void Output(TopoTestNode* node, int outputPos) { |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 176 | SkASSERT(-1 != outputPos); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 177 | node->fOutputPos = outputPos; |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 178 | } |
| 179 | static bool WasOutput(TopoTestNode* node) { return (-1 != node->fOutputPos); } |
| 180 | static int NumDependencies(TopoTestNode* node) { return node->fDependencies.count(); } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 181 | static TopoTestNode* Dependency(TopoTestNode* node, int index) { |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 182 | 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 | |
tfarina | 2010891 | 2014-06-21 10:54:17 -0700 | [diff] [blame] | 228 | } // namespace sk_tool_utils |
| 229 | |
| 230 | #endif // sk_tool_utils_DEFINED |