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" |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 16 | #include "SkStream.h" |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 17 | #include "SkTDArray.h" |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 18 | #include "SkTypeface.h" |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 19 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 20 | class SkBitmap; |
| 21 | class SkCanvas; |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 22 | class SkColorFilter; |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 23 | class SkPaint; |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 24 | class SkPath; |
robertphillips | 276d328 | 2016-08-04 09:03:19 -0700 | [diff] [blame] | 25 | class SkRRect; |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 26 | class SkShader; |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 27 | class SkTestFont; |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 28 | class SkTextBlobBuilder; |
skia.committer@gmail.com | db0c875 | 2014-03-18 03:02:11 +0000 | [diff] [blame] | 29 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 30 | namespace sk_tool_utils { |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 31 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 32 | const char* colortype_name(SkColorType); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Map opaque colors from 8888 to 565. |
| 36 | */ |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 37 | SkColor color_to_565(SkColor color); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Return a color emoji typeface if available. |
| 41 | */ |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 42 | sk_sp<SkTypeface> emoji_typeface(); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * If the platform supports color emoji, return sample text the emoji can render. |
| 46 | */ |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 47 | const char* emoji_sample_text(); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * If the platform supports color emoji, return the type (i.e. "CBDT", "SBIX", ""). |
| 51 | */ |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 52 | const char* platform_os_emoji(); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Return the platform name with the version number ("Mac10.9", "Win8", etc.) if available. |
| 56 | */ |
caryclark | 6531c36 | 2015-07-20 13:38:56 -0700 | [diff] [blame] | 57 | const char* platform_os_name(); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Return the platform name without the version number ("Mac", "Win", etc.) if available. |
| 61 | */ |
| 62 | SkString major_platform_os_name(); |
| 63 | |
| 64 | /** |
| 65 | * Return the platform extra config (e.g. "GDI") if available. |
| 66 | */ |
caryclark | 6531c36 | 2015-07-20 13:38:56 -0700 | [diff] [blame] | 67 | const char* platform_extra_config(const char* config); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 68 | |
| 69 | /** |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 70 | * Map serif, san-serif, and monospace to the platform-specific font name. |
| 71 | */ |
| 72 | const char* platform_font_name(const char* name); |
| 73 | |
| 74 | /** |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 75 | * Sets the paint to use a platform-independent text renderer |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 76 | */ |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 77 | void set_portable_typeface(SkPaint* paint, const char* name = nullptr, |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 78 | SkFontStyle style = SkFontStyle()); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 79 | |
| 80 | /** |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 81 | * Returns a platform-independent text renderer. |
| 82 | */ |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 83 | sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 84 | |
caryclark | f53ce80 | 2015-06-15 06:48:30 -0700 | [diff] [blame] | 85 | /** Call to clean up portable font references. */ |
| 86 | void release_portable_typefaces(); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 87 | |
| 88 | /** |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 89 | * Call canvas->writePixels() by using the pixels from bitmap, but with an info that claims |
| 90 | * the pixels are colorType + alphaType |
| 91 | */ |
| 92 | 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] | 93 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 94 | // private to sk_tool_utils |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 95 | sk_sp<SkTypeface> create_font(const char* name, SkFontStyle); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 96 | |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 97 | /** Returns a newly created CheckerboardShader. */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 98 | sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size); |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 99 | |
| 100 | /** Draw a checkerboard pattern in the current canvas, restricted to |
halcanary | f77365f | 2015-01-27 08:38:35 -0800 | [diff] [blame] | 101 | the current clip, using SkXfermode::kSrc_Mode. */ |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 102 | void draw_checkerboard(SkCanvas* canvas, |
| 103 | SkColor color1, |
| 104 | SkColor color2, |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 105 | int checkSize); |
| 106 | |
| 107 | /** Make it easier to create a bitmap-based checkerboard */ |
| 108 | SkBitmap create_checkerboard_bitmap(int w, int h, |
| 109 | SkColor c1, SkColor c2, |
| 110 | int checkSize); |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 111 | |
| 112 | /** A default checkerboard. */ |
| 113 | inline void draw_checkerboard(SkCanvas* canvas) { |
| 114 | sk_tool_utils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8); |
| 115 | } |
| 116 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 117 | SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y, |
| 118 | int textSize, const char* str); |
| 119 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 120 | // A helper for inserting a drawtext call into a SkTextBlobBuilder |
| 121 | void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkPaint& origPaint, |
| 122 | SkScalar x, SkScalar y); |
| 123 | |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame] | 124 | void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst); |
| 125 | |
| 126 | void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst); |
| 127 | |
| 128 | void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst); |
| 129 | |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 130 | void make_big_path(SkPath& path); |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 131 | |
| 132 | // Return a blurred version of 'src'. This doesn't use a separable filter |
| 133 | // so it is slow! |
| 134 | SkBitmap slow_blur(const SkBitmap& src, float sigma); |
| 135 | |
robertphillips | 276d328 | 2016-08-04 09:03:19 -0700 | [diff] [blame] | 136 | SkRect compute_central_occluder(const SkRRect& rr); |
robertphillips | 401c196 | 2016-08-04 12:35:46 -0700 | [diff] [blame] | 137 | SkRect compute_widest_occluder(const SkRRect& rr); |
| 138 | SkRect compute_tallest_occluder(const SkRRect& rr); |
robertphillips | 276d328 | 2016-08-04 09:03:19 -0700 | [diff] [blame] | 139 | |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 140 | // A helper object to test the topological sorting code (TopoSortBench.cpp & TopoSortTest.cpp) |
| 141 | class TopoTestNode { |
| 142 | public: |
| 143 | TopoTestNode(int id) : fID(id), fOutputPos(-1), fTempMark(false) { } |
| 144 | |
| 145 | void dependsOn(TopoTestNode* src) { |
| 146 | *fDependencies.append() = src; |
| 147 | } |
| 148 | |
| 149 | int id() const { return fID; } |
| 150 | void reset() { fOutputPos = -1; } |
| 151 | |
| 152 | int outputPos() const { return fOutputPos; } |
| 153 | |
| 154 | // check that the topological sort is valid for this node |
| 155 | bool check() { |
| 156 | if (-1 == fOutputPos) { |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | for (int i = 0; i < fDependencies.count(); ++i) { |
| 161 | if (-1 == fDependencies[i]->outputPos()) { |
| 162 | return false; |
| 163 | } |
| 164 | // This node should've been output after all the nodes on which it depends |
| 165 | if (fOutputPos < fDependencies[i]->outputPos()) { |
| 166 | return false; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | // The following 7 methods are needed by the topological sort |
| 174 | static void SetTempMark(TopoTestNode* node) { node->fTempMark = true; } |
| 175 | static void ResetTempMark(TopoTestNode* node) { node->fTempMark = false; } |
| 176 | static bool IsTempMarked(TopoTestNode* node) { return node->fTempMark; } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 177 | static void Output(TopoTestNode* node, int outputPos) { |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 178 | SkASSERT(-1 != outputPos); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 179 | node->fOutputPos = outputPos; |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 180 | } |
| 181 | static bool WasOutput(TopoTestNode* node) { return (-1 != node->fOutputPos); } |
| 182 | static int NumDependencies(TopoTestNode* node) { return node->fDependencies.count(); } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 183 | static TopoTestNode* Dependency(TopoTestNode* node, int index) { |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 184 | return node->fDependencies[index]; |
| 185 | } |
| 186 | |
| 187 | // Helper functions for TopoSortBench & TopoSortTest |
| 188 | static void AllocNodes(SkTDArray<TopoTestNode*>* graph, int num) { |
| 189 | graph->setReserve(num); |
| 190 | |
| 191 | for (int i = 0; i < num; ++i) { |
| 192 | *graph->append() = new TopoTestNode(i); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | static void DeallocNodes(SkTDArray<TopoTestNode*>* graph) { |
| 197 | for (int i = 0; i < graph->count(); ++i) { |
| 198 | delete (*graph)[i]; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | #ifdef SK_DEBUG |
| 203 | static void Print(const SkTDArray<TopoTestNode*>& graph) { |
| 204 | for (int i = 0; i < graph.count(); ++i) { |
| 205 | SkDebugf("%d, ", graph[i]->id()); |
| 206 | } |
| 207 | SkDebugf("\n"); |
| 208 | } |
| 209 | #endif |
| 210 | |
| 211 | // randomize the array |
| 212 | static void Shuffle(SkTDArray<TopoTestNode*>* graph, SkRandom* rand) { |
| 213 | for (int i = graph->count()-1; i > 0; --i) { |
| 214 | int swap = rand->nextU() % (i+1); |
| 215 | |
| 216 | TopoTestNode* tmp = (*graph)[i]; |
| 217 | (*graph)[i] = (*graph)[swap]; |
| 218 | (*graph)[swap] = tmp; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | private: |
| 223 | int fID; |
| 224 | int fOutputPos; |
| 225 | bool fTempMark; |
| 226 | |
| 227 | SkTDArray<TopoTestNode*> fDependencies; |
| 228 | }; |
| 229 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 230 | template <typename T> |
| 231 | inline bool EncodeImageToFile(const char* path, const T& src, SkEncodedImageFormat f, int q) { |
| 232 | SkFILEWStream file(path); |
| 233 | return file.isValid() && SkEncodeImage(&file, src, f, q); |
| 234 | } |
| 235 | |
| 236 | template <typename T> |
| 237 | inline sk_sp<SkData> EncodeImageToData(const T& src, SkEncodedImageFormat f, int q) { |
| 238 | SkDynamicMemoryWStream buf; |
| 239 | return SkEncodeImage(&buf, src , f, q) ? buf.detachAsData() : nullptr; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Uses SkEncodeImage to serialize images that are not already |
| 244 | * encoded as SkEncodedImageFormat::kPNG images. |
| 245 | */ |
| 246 | inline sk_sp<SkPixelSerializer> MakePixelSerializer() { |
| 247 | struct EncodeImagePixelSerializer final : SkPixelSerializer { |
| 248 | bool onUseEncodedData(const void*, size_t) override { return true; } |
| 249 | SkData* onEncode(const SkPixmap& pmap) override { |
| 250 | return EncodeImageToData(pmap, SkEncodedImageFormat::kPNG, 100).release(); |
| 251 | } |
| 252 | }; |
| 253 | return sk_make_sp<EncodeImagePixelSerializer>(); |
| 254 | } |
Matt Sarett | 4897fb8 | 2017-01-18 11:49:33 -0500 | [diff] [blame] | 255 | |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame] | 256 | bool copy_to(SkBitmap* dst, SkColorType dstCT, const SkBitmap& src); |
Matt Sarett | 4897fb8 | 2017-01-18 11:49:33 -0500 | [diff] [blame] | 257 | void copy_to_g8(SkBitmap* dst, const SkBitmap& src); |
| 258 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 259 | #if SK_SUPPORT_GPU |
| 260 | sk_sp<SkColorFilter> MakeLinearToSRGBColorFilter(); |
| 261 | sk_sp<SkColorFilter> MakeSRGBToLinearColorFilter(); |
| 262 | #endif |
| 263 | |
tfarina | 2010891 | 2014-06-21 10:54:17 -0700 | [diff] [blame] | 264 | } // namespace sk_tool_utils |
| 265 | |
| 266 | #endif // sk_tool_utils_DEFINED |