Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [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 ToolUtils_DEFINED |
| 9 | #define ToolUtils_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkData.h" |
| 13 | #include "include/core/SkEncodedImageFormat.h" |
| 14 | #include "include/core/SkFont.h" |
| 15 | #include "include/core/SkFontStyle.h" |
| 16 | #include "include/core/SkFontTypes.h" |
| 17 | #include "include/core/SkImageEncoder.h" |
| 18 | #include "include/core/SkImageInfo.h" |
| 19 | #include "include/core/SkPixmap.h" |
| 20 | #include "include/core/SkRect.h" |
| 21 | #include "include/core/SkRefCnt.h" |
| 22 | #include "include/core/SkScalar.h" |
| 23 | #include "include/core/SkStream.h" |
| 24 | #include "include/core/SkSurface.h" |
| 25 | #include "include/core/SkTypeface.h" |
| 26 | #include "include/core/SkTypes.h" |
| 27 | #include "include/private/SkTArray.h" |
| 28 | #include "include/private/SkTDArray.h" |
| 29 | #include "include/utils/SkRandom.h" |
Adlai Holler | 7803608 | 2021-01-07 11:41:33 -0500 | [diff] [blame] | 30 | #include "src/core/SkTInternalLList.h" |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 31 | |
| 32 | class SkBitmap; |
| 33 | class SkCanvas; |
| 34 | class SkFontStyle; |
| 35 | class SkImage; |
| 36 | class SkPath; |
| 37 | class SkPixmap; |
| 38 | class SkRRect; |
| 39 | class SkShader; |
| 40 | class SkSurface; |
| 41 | class SkSurfaceProps; |
| 42 | class SkTextBlobBuilder; |
| 43 | class SkTypeface; |
| 44 | |
| 45 | namespace ToolUtils { |
| 46 | |
Mike Klein | 9816878 | 2019-04-09 13:45:02 -0500 | [diff] [blame] | 47 | const char* alphatype_name (SkAlphaType); |
| 48 | const char* colortype_name (SkColorType); |
| 49 | const char* colortype_depth(SkColorType); // like colortype_name, but channel order agnostic |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 50 | const char* tilemode_name(SkTileMode); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Map opaque colors from 8888 to 565. |
| 54 | */ |
| 55 | SkColor color_to_565(SkColor color); |
| 56 | |
| 57 | /* Return a color emoji typeface with planets to scale if available. */ |
| 58 | sk_sp<SkTypeface> planet_typeface(); |
| 59 | |
| 60 | /** Return a color emoji typeface if available. */ |
| 61 | sk_sp<SkTypeface> emoji_typeface(); |
| 62 | |
| 63 | /** Sample text for the emoji_typeface font. */ |
| 64 | const char* emoji_sample_text(); |
| 65 | |
Ben Wagner | 81eabce | 2020-08-18 13:17:09 -0400 | [diff] [blame] | 66 | /** A simple SkUserTypeface for testing. */ |
| 67 | sk_sp<SkTypeface> sample_user_typeface(); |
| 68 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 69 | /** |
| 70 | * Returns a platform-independent text renderer. |
| 71 | */ |
| 72 | sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style); |
| 73 | |
| 74 | static inline sk_sp<SkTypeface> create_portable_typeface() { |
| 75 | return create_portable_typeface(nullptr, SkFontStyle()); |
| 76 | } |
| 77 | |
Mike Klein | 19cc0f6 | 2019-03-22 15:30:07 -0500 | [diff] [blame] | 78 | /** |
| 79 | * Turn on portable (--nonativeFonts) or GDI font rendering (--gdi). |
| 80 | */ |
| 81 | void SetDefaultFontMgr(); |
| 82 | |
| 83 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 84 | void get_text_path(const SkFont&, |
| 85 | const void* text, |
| 86 | size_t length, |
| 87 | SkTextEncoding, |
| 88 | SkPath*, |
| 89 | const SkPoint* positions = nullptr); |
| 90 | |
| 91 | /** |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 92 | * Returns true iff all of the pixels between the two images are identical. |
| 93 | * |
| 94 | * If the configs differ, return false. |
| 95 | */ |
| 96 | bool equal_pixels(const SkPixmap&, const SkPixmap&); |
| 97 | bool equal_pixels(const SkBitmap&, const SkBitmap&); |
| 98 | bool equal_pixels(const SkImage* a, const SkImage* b); |
| 99 | |
| 100 | /** Returns a newly created CheckerboardShader. */ |
| 101 | sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size); |
| 102 | |
| 103 | /** Draw a checkerboard pattern in the current canvas, restricted to |
| 104 | the current clip, using SkXfermode::kSrc_Mode. */ |
| 105 | void draw_checkerboard(SkCanvas* canvas, SkColor color1, SkColor color2, int checkSize); |
| 106 | |
| 107 | /** Make it easier to create a bitmap-based checkerboard */ |
| 108 | SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize); |
| 109 | |
Mike Reed | db873dd | 2020-12-04 12:22:10 -0500 | [diff] [blame] | 110 | sk_sp<SkImage> create_checkerboard_image(int w, int h, SkColor c1, SkColor c2, int checkSize); |
| 111 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 112 | /** A default checkerboard. */ |
| 113 | inline void draw_checkerboard(SkCanvas* canvas) { |
| 114 | ToolUtils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8); |
| 115 | } |
| 116 | |
| 117 | SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y, int textSize, const char* str); |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 118 | sk_sp<SkImage> create_string_image(int w, int h, SkColor c, int x, int y, int textSize, const char* str); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 119 | |
| 120 | // If the canvas does't make a surface (e.g. recording), make a raster surface |
| 121 | sk_sp<SkSurface> makeSurface(SkCanvas*, const SkImageInfo&, const SkSurfaceProps* = nullptr); |
| 122 | |
| 123 | // A helper for inserting a drawtext call into a SkTextBlobBuilder |
| 124 | void add_to_text_blob_w_len(SkTextBlobBuilder*, |
| 125 | const char* text, |
| 126 | size_t len, |
| 127 | SkTextEncoding, |
| 128 | const SkFont&, |
| 129 | SkScalar x, |
| 130 | SkScalar y); |
| 131 | |
| 132 | void add_to_text_blob(SkTextBlobBuilder*, const char* text, const SkFont&, SkScalar x, SkScalar y); |
| 133 | |
| 134 | // Constructs a star by walking a 'numPts'-sided regular polygon with even/odd fill: |
| 135 | // |
| 136 | // moveTo(pts[0]); |
| 137 | // lineTo(pts[step % numPts]); |
| 138 | // ... |
| 139 | // lineTo(pts[(step * (N - 1)) % numPts]); |
| 140 | // |
| 141 | // numPts=5, step=2 will produce a classic five-point star. |
| 142 | // |
| 143 | // numPts and step must be co-prime. |
| 144 | SkPath make_star(const SkRect& bounds, int numPts = 5, int step = 2); |
| 145 | |
| 146 | void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst); |
| 147 | |
| 148 | void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst); |
| 149 | |
| 150 | void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst); |
| 151 | |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame] | 152 | SkPath make_big_path(); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 153 | |
| 154 | // A helper object to test the topological sorting code (TopoSortBench.cpp & TopoSortTest.cpp) |
| 155 | class TopoTestNode : public SkRefCnt { |
| 156 | public: |
Robert Phillips | 4dbff75 | 2020-11-18 12:16:31 -0500 | [diff] [blame] | 157 | TopoTestNode(int id) : fID(id) {} |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 158 | |
| 159 | void dependsOn(TopoTestNode* src) { *fDependencies.append() = src; } |
Adlai Holler | 7803608 | 2021-01-07 11:41:33 -0500 | [diff] [blame] | 160 | void targets(uint32_t target) { *fTargets.append() = target; } |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 161 | |
| 162 | int id() const { return fID; } |
Robert Phillips | 4dbff75 | 2020-11-18 12:16:31 -0500 | [diff] [blame] | 163 | void reset() { |
| 164 | fOutputPos = 0; |
| 165 | fTempMark = false; |
| 166 | fWasOutput = false; |
| 167 | } |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 168 | |
Robert Phillips | 4dbff75 | 2020-11-18 12:16:31 -0500 | [diff] [blame] | 169 | uint32_t outputPos() const { |
| 170 | SkASSERT(fWasOutput); |
| 171 | return fOutputPos; |
| 172 | } |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 173 | |
| 174 | // check that the topological sort is valid for this node |
| 175 | bool check() { |
Robert Phillips | 4dbff75 | 2020-11-18 12:16:31 -0500 | [diff] [blame] | 176 | if (!fWasOutput) { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 177 | return false; |
| 178 | } |
| 179 | |
| 180 | for (int i = 0; i < fDependencies.count(); ++i) { |
Robert Phillips | 4dbff75 | 2020-11-18 12:16:31 -0500 | [diff] [blame] | 181 | if (!fDependencies[i]->fWasOutput) { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 182 | return false; |
| 183 | } |
| 184 | // This node should've been output after all the nodes on which it depends |
| 185 | if (fOutputPos < fDependencies[i]->outputPos()) { |
| 186 | return false; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | // The following 7 methods are needed by the topological sort |
| 194 | static void SetTempMark(TopoTestNode* node) { node->fTempMark = true; } |
| 195 | static void ResetTempMark(TopoTestNode* node) { node->fTempMark = false; } |
| 196 | static bool IsTempMarked(TopoTestNode* node) { return node->fTempMark; } |
Robert Phillips | 4dbff75 | 2020-11-18 12:16:31 -0500 | [diff] [blame] | 197 | static void Output(TopoTestNode* node, uint32_t outputPos) { |
| 198 | SkASSERT(!node->fWasOutput); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 199 | node->fOutputPos = outputPos; |
Robert Phillips | 4dbff75 | 2020-11-18 12:16:31 -0500 | [diff] [blame] | 200 | node->fWasOutput = true; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 201 | } |
Robert Phillips | 4dbff75 | 2020-11-18 12:16:31 -0500 | [diff] [blame] | 202 | static bool WasOutput(TopoTestNode* node) { return node->fWasOutput; } |
| 203 | static uint32_t GetIndex(TopoTestNode* node) { return node->outputPos(); } |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 204 | static int NumDependencies(TopoTestNode* node) { return node->fDependencies.count(); } |
| 205 | static TopoTestNode* Dependency(TopoTestNode* node, int index) { |
| 206 | return node->fDependencies[index]; |
| 207 | } |
Adlai Holler | 7803608 | 2021-01-07 11:41:33 -0500 | [diff] [blame] | 208 | static int NumTargets(TopoTestNode* node) { return node->fTargets.count(); } |
| 209 | static uint32_t GetTarget(TopoTestNode* node, int i) { return node->fTargets[i]; } |
| 210 | static uint32_t GetID(TopoTestNode* node) { return node->id(); } |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 211 | |
| 212 | // Helper functions for TopoSortBench & TopoSortTest |
| 213 | static void AllocNodes(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph, int num) { |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 214 | graph->reserve_back(num); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 215 | |
| 216 | for (int i = 0; i < num; ++i) { |
| 217 | graph->push_back(sk_sp<TopoTestNode>(new TopoTestNode(i))); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | #ifdef SK_DEBUG |
| 222 | static void Print(const SkTArray<TopoTestNode*>& graph) { |
| 223 | for (int i = 0; i < graph.count(); ++i) { |
| 224 | SkDebugf("%d, ", graph[i]->id()); |
| 225 | } |
| 226 | SkDebugf("\n"); |
| 227 | } |
| 228 | #endif |
| 229 | |
| 230 | // randomize the array |
| 231 | static void Shuffle(SkTArray<sk_sp<TopoTestNode>>* graph, SkRandom* rand) { |
| 232 | for (int i = graph->count() - 1; i > 0; --i) { |
| 233 | int swap = rand->nextU() % (i + 1); |
| 234 | |
| 235 | (*graph)[i].swap((*graph)[swap]); |
| 236 | } |
| 237 | } |
| 238 | |
Adlai Holler | 7803608 | 2021-01-07 11:41:33 -0500 | [diff] [blame] | 239 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(TopoTestNode); |
| 240 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 241 | private: |
Robert Phillips | 4dbff75 | 2020-11-18 12:16:31 -0500 | [diff] [blame] | 242 | int fID; |
| 243 | uint32_t fOutputPos = 0; |
| 244 | bool fTempMark = false; |
| 245 | bool fWasOutput = false; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 246 | |
| 247 | SkTDArray<TopoTestNode*> fDependencies; |
Adlai Holler | 7803608 | 2021-01-07 11:41:33 -0500 | [diff] [blame] | 248 | SkTDArray<uint32_t> fTargets; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | template <typename T> |
| 252 | inline bool EncodeImageToFile(const char* path, const T& src, SkEncodedImageFormat f, int q) { |
| 253 | SkFILEWStream file(path); |
| 254 | return file.isValid() && SkEncodeImage(&file, src, f, q); |
| 255 | } |
| 256 | |
| 257 | bool copy_to(SkBitmap* dst, SkColorType dstCT, const SkBitmap& src); |
| 258 | void copy_to_g8(SkBitmap* dst, const SkBitmap& src); |
Mike Klein | 0498857 | 2019-03-20 11:43:12 -0500 | [diff] [blame] | 259 | |
| 260 | class PixelIter { |
| 261 | public: |
| 262 | PixelIter(); |
| 263 | PixelIter(SkSurface* surf) { |
| 264 | SkPixmap pm; |
| 265 | if (!surf->peekPixels(&pm)) { |
| 266 | pm.reset(); |
| 267 | } |
| 268 | this->reset(pm); |
| 269 | } |
| 270 | |
| 271 | void reset(const SkPixmap& pm) { |
| 272 | fPM = pm; |
| 273 | fLoc = {-1, 0}; |
| 274 | } |
| 275 | |
| 276 | void* next(SkIPoint* loc = nullptr) { |
| 277 | if (!fPM.addr()) { |
| 278 | return nullptr; |
| 279 | } |
| 280 | fLoc.fX += 1; |
| 281 | if (fLoc.fX >= fPM.width()) { |
| 282 | fLoc.fX = 0; |
| 283 | if (++fLoc.fY >= fPM.height()) { |
| 284 | this->setDone(); |
| 285 | return nullptr; |
| 286 | } |
| 287 | } |
| 288 | if (loc) { |
| 289 | *loc = fLoc; |
| 290 | } |
| 291 | return fPM.writable_addr(fLoc.fX, fLoc.fY); |
| 292 | } |
| 293 | |
| 294 | void setDone() { fPM.reset(); } |
| 295 | |
| 296 | private: |
| 297 | SkPixmap fPM; |
| 298 | SkIPoint fLoc; |
| 299 | }; |
| 300 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 301 | } // namespace ToolUtils |
| 302 | |
| 303 | #endif // ToolUtils_DEFINED |