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; |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 23 | class SkShader; |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 24 | class SkTestFont; |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 25 | class SkTextBlobBuilder; |
skia.committer@gmail.com | db0c875 | 2014-03-18 03:02:11 +0000 | [diff] [blame] | 26 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 27 | namespace sk_tool_utils { |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 28 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 29 | const char* colortype_name(SkColorType); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Map opaque colors from 8888 to 565. |
| 33 | */ |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 34 | SkColor color_to_565(SkColor color); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Return a color emoji typeface if available. |
| 38 | */ |
caryclark | 3b0f98e | 2015-07-23 11:52:01 -0700 | [diff] [blame] | 39 | void emoji_typeface(SkAutoTUnref<SkTypeface>* ); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * If the platform supports color emoji, return sample text the emoji can render. |
| 43 | */ |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 44 | const char* emoji_sample_text(); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * If the platform supports color emoji, return the type (i.e. "CBDT", "SBIX", ""). |
| 48 | */ |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 49 | const char* platform_os_emoji(); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Return the platform name with the version number ("Mac10.9", "Win8", etc.) if available. |
| 53 | */ |
caryclark | 6531c36 | 2015-07-20 13:38:56 -0700 | [diff] [blame] | 54 | const char* platform_os_name(); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 55 | |
| 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 | */ |
caryclark | 6531c36 | 2015-07-20 13:38:56 -0700 | [diff] [blame] | 64 | const char* platform_extra_config(const char* config); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 65 | |
| 66 | /** |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 67 | * Map serif, san-serif, and monospace to the platform-specific font name. |
| 68 | */ |
| 69 | const char* platform_font_name(const char* name); |
| 70 | |
| 71 | /** |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 72 | * Sets the paint to use a platform-independent text renderer |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 73 | */ |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 74 | void set_portable_typeface(SkPaint* paint, const char* name = nullptr, |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 75 | SkTypeface::Style style = SkTypeface::kNormal); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 78 | * Returns a platform-independent text renderer. |
| 79 | */ |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 80 | SkTypeface* create_portable_typeface(const char* name, SkTypeface::Style style); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 81 | |
caryclark | f53ce80 | 2015-06-15 06:48:30 -0700 | [diff] [blame] | 82 | /** Call to clean up portable font references. */ |
| 83 | void release_portable_typefaces(); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 84 | |
| 85 | /** |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 86 | * 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.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 90 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 91 | // private to sk_tool_utils |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 92 | SkTypeface* create_font(const char* name, SkTypeface::Style); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 93 | |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 94 | /** Returns a newly created CheckerboardShader. */ |
| 95 | SkShader* create_checkerboard_shader(SkColor c1, SkColor c2, int size); |
| 96 | |
| 97 | /** Draw a checkerboard pattern in the current canvas, restricted to |
halcanary | f77365f | 2015-01-27 08:38:35 -0800 | [diff] [blame] | 98 | the current clip, using SkXfermode::kSrc_Mode. */ |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 99 | void draw_checkerboard(SkCanvas* canvas, |
| 100 | SkColor color1, |
| 101 | SkColor color2, |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 102 | 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); |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 108 | |
| 109 | /** A default checkerboard. */ |
| 110 | inline void draw_checkerboard(SkCanvas* canvas) { |
| 111 | sk_tool_utils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8); |
| 112 | } |
| 113 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 114 | SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y, |
| 115 | int textSize, const char* str); |
| 116 | |
halcanary | a2e0567 | 2015-12-09 03:56:02 -0800 | [diff] [blame^] | 117 | // Encodes to PNG, unless there is already encoded data, in which case that gets |
| 118 | // used. |
| 119 | class PngPixelSerializer : public SkPixelSerializer { |
| 120 | public: |
| 121 | bool onUseEncodedData(const void*, size_t) override { return true; } |
| 122 | SkData* onEncodePixels(const SkImageInfo& info, const void* pixels, |
| 123 | size_t rowBytes) override { |
| 124 | return SkImageEncoder::EncodeData(info, pixels, rowBytes, |
| 125 | SkImageEncoder::kPNG_Type, 100); |
| 126 | } |
| 127 | }; |
| 128 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 129 | // A helper for inserting a drawtext call into a SkTextBlobBuilder |
| 130 | void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkPaint& origPaint, |
| 131 | SkScalar x, SkScalar y); |
| 132 | |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame] | 133 | void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst); |
| 134 | |
| 135 | void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst); |
| 136 | |
| 137 | void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst); |
| 138 | |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 139 | void make_big_path(SkPath& path); |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 140 | |
| 141 | // Return a blurred version of 'src'. This doesn't use a separable filter |
| 142 | // so it is slow! |
| 143 | SkBitmap slow_blur(const SkBitmap& src, float sigma); |
| 144 | |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 145 | // A helper object to test the topological sorting code (TopoSortBench.cpp & TopoSortTest.cpp) |
| 146 | class TopoTestNode { |
| 147 | public: |
| 148 | TopoTestNode(int id) : fID(id), fOutputPos(-1), fTempMark(false) { } |
| 149 | |
| 150 | void dependsOn(TopoTestNode* src) { |
| 151 | *fDependencies.append() = src; |
| 152 | } |
| 153 | |
| 154 | int id() const { return fID; } |
| 155 | void reset() { fOutputPos = -1; } |
| 156 | |
| 157 | int outputPos() const { return fOutputPos; } |
| 158 | |
| 159 | // check that the topological sort is valid for this node |
| 160 | bool check() { |
| 161 | if (-1 == fOutputPos) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | for (int i = 0; i < fDependencies.count(); ++i) { |
| 166 | if (-1 == fDependencies[i]->outputPos()) { |
| 167 | return false; |
| 168 | } |
| 169 | // This node should've been output after all the nodes on which it depends |
| 170 | if (fOutputPos < fDependencies[i]->outputPos()) { |
| 171 | return false; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | // The following 7 methods are needed by the topological sort |
| 179 | static void SetTempMark(TopoTestNode* node) { node->fTempMark = true; } |
| 180 | static void ResetTempMark(TopoTestNode* node) { node->fTempMark = false; } |
| 181 | static bool IsTempMarked(TopoTestNode* node) { return node->fTempMark; } |
| 182 | static void Output(TopoTestNode* node, int outputPos) { |
| 183 | SkASSERT(-1 != outputPos); |
| 184 | node->fOutputPos = outputPos; |
| 185 | } |
| 186 | static bool WasOutput(TopoTestNode* node) { return (-1 != node->fOutputPos); } |
| 187 | static int NumDependencies(TopoTestNode* node) { return node->fDependencies.count(); } |
| 188 | static TopoTestNode* Dependency(TopoTestNode* node, int index) { |
| 189 | return node->fDependencies[index]; |
| 190 | } |
| 191 | |
| 192 | // Helper functions for TopoSortBench & TopoSortTest |
| 193 | static void AllocNodes(SkTDArray<TopoTestNode*>* graph, int num) { |
| 194 | graph->setReserve(num); |
| 195 | |
| 196 | for (int i = 0; i < num; ++i) { |
| 197 | *graph->append() = new TopoTestNode(i); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | static void DeallocNodes(SkTDArray<TopoTestNode*>* graph) { |
| 202 | for (int i = 0; i < graph->count(); ++i) { |
| 203 | delete (*graph)[i]; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | #ifdef SK_DEBUG |
| 208 | static void Print(const SkTDArray<TopoTestNode*>& graph) { |
| 209 | for (int i = 0; i < graph.count(); ++i) { |
| 210 | SkDebugf("%d, ", graph[i]->id()); |
| 211 | } |
| 212 | SkDebugf("\n"); |
| 213 | } |
| 214 | #endif |
| 215 | |
| 216 | // randomize the array |
| 217 | static void Shuffle(SkTDArray<TopoTestNode*>* graph, SkRandom* rand) { |
| 218 | for (int i = graph->count()-1; i > 0; --i) { |
| 219 | int swap = rand->nextU() % (i+1); |
| 220 | |
| 221 | TopoTestNode* tmp = (*graph)[i]; |
| 222 | (*graph)[i] = (*graph)[swap]; |
| 223 | (*graph)[swap] = tmp; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | private: |
| 228 | int fID; |
| 229 | int fOutputPos; |
| 230 | bool fTempMark; |
| 231 | |
| 232 | SkTDArray<TopoTestNode*> fDependencies; |
| 233 | }; |
| 234 | |
tfarina | 2010891 | 2014-06-21 10:54:17 -0700 | [diff] [blame] | 235 | } // namespace sk_tool_utils |
| 236 | |
| 237 | #endif // sk_tool_utils_DEFINED |