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" |
Ben Wagner | 97182cc | 2018-02-15 10:20:04 -0500 | [diff] [blame] | 12 | #include "SkData.h" |
| 13 | #include "SkEncodedImageFormat.h" |
| 14 | #include "SkFontStyle.h" |
robertphillips | 3e5c2b1 | 2015-03-23 05:46:51 -0700 | [diff] [blame] | 15 | #include "SkImageEncoder.h" |
tfarina | 2010891 | 2014-06-21 10:54:17 -0700 | [diff] [blame] | 16 | #include "SkImageInfo.h" |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 17 | #include "SkRandom.h" |
Ben Wagner | 97182cc | 2018-02-15 10:20:04 -0500 | [diff] [blame] | 18 | #include "SkRect.h" |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 19 | #include "SkRefCnt.h" |
Ben Wagner | 97182cc | 2018-02-15 10:20:04 -0500 | [diff] [blame] | 20 | #include "SkScalar.h" |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 21 | #include "SkStream.h" |
Ben Wagner | 97182cc | 2018-02-15 10:20:04 -0500 | [diff] [blame] | 22 | #include "SkTArray.h" |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 23 | #include "SkTDArray.h" |
Ben Wagner | 97182cc | 2018-02-15 10:20:04 -0500 | [diff] [blame] | 24 | #include "SkTypes.h" |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 25 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 26 | class SkBitmap; |
| 27 | class SkCanvas; |
Ben Wagner | 97182cc | 2018-02-15 10:20:04 -0500 | [diff] [blame] | 28 | class SkFontStyle; |
Mike Reed | 2484660 | 2017-12-04 16:06:03 -0500 | [diff] [blame] | 29 | class SkImage; |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 30 | class SkPaint; |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 31 | class SkPath; |
Ben Wagner | 97182cc | 2018-02-15 10:20:04 -0500 | [diff] [blame] | 32 | class SkPixmap; |
robertphillips | 276d328 | 2016-08-04 09:03:19 -0700 | [diff] [blame] | 33 | class SkRRect; |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 34 | class SkShader; |
Mike Reed | 46596ae | 2018-01-02 15:40:29 -0500 | [diff] [blame] | 35 | class SkSurface; |
| 36 | class SkSurfaceProps; |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 37 | class SkTextBlobBuilder; |
Ben Wagner | 97182cc | 2018-02-15 10:20:04 -0500 | [diff] [blame] | 38 | class SkTypeface; |
skia.committer@gmail.com | db0c875 | 2014-03-18 03:02:11 +0000 | [diff] [blame] | 39 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 40 | namespace sk_tool_utils { |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 41 | |
Mike Reed | d474698 | 2018-02-07 16:05:29 -0500 | [diff] [blame] | 42 | const char* alphatype_name(SkAlphaType); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 43 | const char* colortype_name(SkColorType); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Map opaque colors from 8888 to 565. |
| 47 | */ |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 48 | SkColor color_to_565(SkColor color); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Return a color emoji typeface if available. |
| 52 | */ |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 53 | sk_sp<SkTypeface> emoji_typeface(); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * If the platform supports color emoji, return sample text the emoji can render. |
| 57 | */ |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 58 | const char* emoji_sample_text(); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
Mike Klein | b251b72 | 2017-11-13 11:03:16 -0500 | [diff] [blame] | 61 | * Returns a string describing the platform font manager, if we're using one, otherwise "". |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 62 | */ |
Mike Klein | b251b72 | 2017-11-13 11:03:16 -0500 | [diff] [blame] | 63 | const char* platform_font_manager(); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 64 | |
| 65 | /** |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 66 | * Sets the paint to use a platform-independent text renderer |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 67 | */ |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 68 | void set_portable_typeface(SkPaint* paint, const char* name = nullptr, |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 69 | SkFontStyle style = SkFontStyle()); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 72 | * Returns a platform-independent text renderer. |
| 73 | */ |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 74 | sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 75 | |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 76 | /** |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame] | 77 | * Call writePixels() by using the pixels from bitmap, but with an info that claims |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 78 | * the pixels are colorType + alphaType |
| 79 | */ |
| 80 | void write_pixels(SkCanvas*, const SkBitmap&, int x, int y, SkColorType, SkAlphaType); |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame] | 81 | void write_pixels(SkSurface*, const SkBitmap&, int x, int y, SkColorType, SkAlphaType); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 82 | |
Mike Reed | 5a625e0 | 2017-08-08 15:48:54 -0400 | [diff] [blame] | 83 | /** |
| 84 | * Returns true iff all of the pixels between the two images differ by <= the maxDiff value |
| 85 | * per component. |
| 86 | * |
| 87 | * If the configs differ, return false. |
| 88 | * |
| 89 | * If the colorType is half-float, then maxDiff is interpreted as 0..255 --> 0..1 |
| 90 | */ |
Mike Reed | 2484660 | 2017-12-04 16:06:03 -0500 | [diff] [blame] | 91 | bool equal_pixels(const SkPixmap&, const SkPixmap&, unsigned maxDiff = 0, |
| 92 | bool respectColorSpaces = false); |
| 93 | bool equal_pixels(const SkBitmap&, const SkBitmap&, unsigned maxDiff = 0, |
| 94 | bool respectColorSpaces = false); |
Mike Reed | 60691a5 | 2017-12-05 15:11:24 -0500 | [diff] [blame] | 95 | bool equal_pixels(const SkImage* a, const SkImage* b, unsigned maxDiff = 0, |
Mike Reed | 2484660 | 2017-12-04 16:06:03 -0500 | [diff] [blame] | 96 | bool respectColorSpaces = false); |
Mike Reed | 5a625e0 | 2017-08-08 15:48:54 -0400 | [diff] [blame] | 97 | |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 98 | /** Returns a newly created CheckerboardShader. */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 99 | sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size); |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 100 | |
| 101 | /** Draw a checkerboard pattern in the current canvas, restricted to |
halcanary | f77365f | 2015-01-27 08:38:35 -0800 | [diff] [blame] | 102 | the current clip, using SkXfermode::kSrc_Mode. */ |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 103 | void draw_checkerboard(SkCanvas* canvas, |
| 104 | SkColor color1, |
| 105 | SkColor color2, |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 106 | int checkSize); |
| 107 | |
| 108 | /** Make it easier to create a bitmap-based checkerboard */ |
| 109 | SkBitmap create_checkerboard_bitmap(int w, int h, |
| 110 | SkColor c1, SkColor c2, |
| 111 | int checkSize); |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 112 | |
| 113 | /** A default checkerboard. */ |
| 114 | inline void draw_checkerboard(SkCanvas* canvas) { |
| 115 | sk_tool_utils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8); |
| 116 | } |
| 117 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 118 | SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y, |
| 119 | int textSize, const char* str); |
| 120 | |
Mike Reed | 46596ae | 2018-01-02 15:40:29 -0500 | [diff] [blame] | 121 | // If the canvas does't make a surface (e.g. recording), make a raster surface |
| 122 | sk_sp<SkSurface> makeSurface(SkCanvas*, const SkImageInfo&, const SkSurfaceProps* = nullptr); |
| 123 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 124 | // A helper for inserting a drawtext call into a SkTextBlobBuilder |
Robert Phillips | 4c72b26 | 2017-08-15 13:28:42 -0400 | [diff] [blame] | 125 | void add_to_text_blob_w_len(SkTextBlobBuilder* builder, const char* text, size_t len, |
| 126 | const SkPaint& origPaint, SkScalar x, SkScalar y); |
| 127 | |
| 128 | void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, |
| 129 | const SkPaint& origPaint, SkScalar x, SkScalar y); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 130 | |
Chris Dalton | 7c02cc7 | 2017-11-06 14:10:54 -0700 | [diff] [blame] | 131 | // Constructs a star by walking a 'numPts'-sided regular polygon with even/odd fill: |
| 132 | // |
| 133 | // moveTo(pts[0]); |
| 134 | // lineTo(pts[step % numPts]); |
| 135 | // ... |
| 136 | // lineTo(pts[(step * (N - 1)) % numPts]); |
| 137 | // |
| 138 | // numPts=5, step=2 will produce a classic five-point star. |
| 139 | // |
| 140 | // numPts and step must be co-prime. |
| 141 | SkPath make_star(const SkRect& bounds, int numPts = 5, int step = 2); |
| 142 | |
Robert Phillips | a8cdbd7 | 2018-07-17 12:30:40 -0400 | [diff] [blame] | 143 | void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst); |
| 144 | |
| 145 | void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst); |
| 146 | |
| 147 | void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst); |
| 148 | |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 149 | void make_big_path(SkPath& path); |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 150 | |
| 151 | // Return a blurred version of 'src'. This doesn't use a separable filter |
| 152 | // so it is slow! |
| 153 | SkBitmap slow_blur(const SkBitmap& src, float sigma); |
| 154 | |
robertphillips | 276d328 | 2016-08-04 09:03:19 -0700 | [diff] [blame] | 155 | SkRect compute_central_occluder(const SkRRect& rr); |
robertphillips | 401c196 | 2016-08-04 12:35:46 -0700 | [diff] [blame] | 156 | SkRect compute_widest_occluder(const SkRRect& rr); |
| 157 | SkRect compute_tallest_occluder(const SkRRect& rr); |
robertphillips | 276d328 | 2016-08-04 09:03:19 -0700 | [diff] [blame] | 158 | |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 159 | // A helper object to test the topological sorting code (TopoSortBench.cpp & TopoSortTest.cpp) |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 160 | class TopoTestNode : public SkRefCnt { |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 161 | public: |
| 162 | TopoTestNode(int id) : fID(id), fOutputPos(-1), fTempMark(false) { } |
| 163 | |
| 164 | void dependsOn(TopoTestNode* src) { |
| 165 | *fDependencies.append() = src; |
| 166 | } |
| 167 | |
| 168 | int id() const { return fID; } |
| 169 | void reset() { fOutputPos = -1; } |
| 170 | |
| 171 | int outputPos() const { return fOutputPos; } |
| 172 | |
| 173 | // check that the topological sort is valid for this node |
| 174 | bool check() { |
| 175 | if (-1 == fOutputPos) { |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | for (int i = 0; i < fDependencies.count(); ++i) { |
| 180 | if (-1 == fDependencies[i]->outputPos()) { |
| 181 | return false; |
| 182 | } |
| 183 | // This node should've been output after all the nodes on which it depends |
| 184 | if (fOutputPos < fDependencies[i]->outputPos()) { |
| 185 | return false; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | // The following 7 methods are needed by the topological sort |
| 193 | static void SetTempMark(TopoTestNode* node) { node->fTempMark = true; } |
| 194 | static void ResetTempMark(TopoTestNode* node) { node->fTempMark = false; } |
| 195 | static bool IsTempMarked(TopoTestNode* node) { return node->fTempMark; } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 196 | static void Output(TopoTestNode* node, int outputPos) { |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 197 | SkASSERT(-1 != outputPos); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 198 | node->fOutputPos = outputPos; |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 199 | } |
| 200 | static bool WasOutput(TopoTestNode* node) { return (-1 != node->fOutputPos); } |
| 201 | static int NumDependencies(TopoTestNode* node) { return node->fDependencies.count(); } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 202 | static TopoTestNode* Dependency(TopoTestNode* node, int index) { |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 203 | return node->fDependencies[index]; |
| 204 | } |
| 205 | |
| 206 | // Helper functions for TopoSortBench & TopoSortTest |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 207 | static void AllocNodes(SkTArray<sk_sp<sk_tool_utils::TopoTestNode>>* graph, int num) { |
| 208 | graph->reserve(num); |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 209 | |
| 210 | for (int i = 0; i < num; ++i) { |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 211 | graph->push_back(sk_sp<TopoTestNode>(new TopoTestNode(i))); |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 215 | #ifdef SK_DEBUG |
| 216 | static void Print(const SkTArray<TopoTestNode*>& graph) { |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 217 | for (int i = 0; i < graph.count(); ++i) { |
| 218 | SkDebugf("%d, ", graph[i]->id()); |
| 219 | } |
| 220 | SkDebugf("\n"); |
| 221 | } |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 222 | #endif |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 223 | |
| 224 | // randomize the array |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 225 | static void Shuffle(SkTArray<sk_sp<TopoTestNode>>* graph, SkRandom* rand) { |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 226 | for (int i = graph->count()-1; i > 0; --i) { |
| 227 | int swap = rand->nextU() % (i+1); |
| 228 | |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 229 | (*graph)[i].swap((*graph)[swap]); |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | private: |
| 234 | int fID; |
| 235 | int fOutputPos; |
| 236 | bool fTempMark; |
| 237 | |
| 238 | SkTDArray<TopoTestNode*> fDependencies; |
| 239 | }; |
| 240 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 241 | template <typename T> |
| 242 | inline bool EncodeImageToFile(const char* path, const T& src, SkEncodedImageFormat f, int q) { |
| 243 | SkFILEWStream file(path); |
| 244 | return file.isValid() && SkEncodeImage(&file, src, f, q); |
| 245 | } |
| 246 | |
| 247 | template <typename T> |
| 248 | inline sk_sp<SkData> EncodeImageToData(const T& src, SkEncodedImageFormat f, int q) { |
| 249 | SkDynamicMemoryWStream buf; |
| 250 | return SkEncodeImage(&buf, src , f, q) ? buf.detachAsData() : nullptr; |
| 251 | } |
| 252 | |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame] | 253 | bool copy_to(SkBitmap* dst, SkColorType dstCT, const SkBitmap& src); |
Matt Sarett | 4897fb8 | 2017-01-18 11:49:33 -0500 | [diff] [blame] | 254 | void copy_to_g8(SkBitmap* dst, const SkBitmap& src); |
tfarina | 2010891 | 2014-06-21 10:54:17 -0700 | [diff] [blame] | 255 | } // namespace sk_tool_utils |
| 256 | |
| 257 | #endif // sk_tool_utils_DEFINED |