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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
| 9 | #include "include/core/SkBlendMode.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColorPriv.h" |
| 12 | #include "include/core/SkImage.h" |
| 13 | #include "include/core/SkMatrix.h" |
| 14 | #include "include/core/SkPaint.h" |
Mike Reed | e9d783c | 2020-08-17 14:14:13 -0400 | [diff] [blame] | 15 | #include "include/core/SkPathBuilder.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/core/SkPixelRef.h" |
| 17 | #include "include/core/SkPixmap.h" |
| 18 | #include "include/core/SkPoint3.h" |
| 19 | #include "include/core/SkRRect.h" |
| 20 | #include "include/core/SkShader.h" |
| 21 | #include "include/core/SkSurface.h" |
| 22 | #include "include/core/SkTextBlob.h" |
| 23 | #include "include/ports/SkTypeface_win.h" |
| 24 | #include "include/private/SkColorData.h" |
| 25 | #include "include/private/SkFloatingPoint.h" |
| 26 | #include "src/core/SkFontMgrPriv.h" |
| 27 | #include "src/core/SkFontPriv.h" |
| 28 | #include "tools/ToolUtils.h" |
| 29 | #include "tools/flags/CommandLineFlags.h" |
| 30 | #include "tools/fonts/TestFontMgr.h" |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 31 | |
| 32 | #include <cmath> |
| 33 | #include <cstring> |
| 34 | #include <memory> |
| 35 | |
| 36 | namespace ToolUtils { |
| 37 | |
| 38 | const char* alphatype_name(SkAlphaType at) { |
| 39 | switch (at) { |
Mike Klein | 9816878 | 2019-04-09 13:45:02 -0500 | [diff] [blame] | 40 | case kUnknown_SkAlphaType: return "Unknown"; |
| 41 | case kOpaque_SkAlphaType: return "Opaque"; |
| 42 | case kPremul_SkAlphaType: return "Premul"; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 43 | case kUnpremul_SkAlphaType: return "Unpremul"; |
| 44 | } |
| 45 | SkASSERT(false); |
| 46 | return "unexpected alphatype"; |
| 47 | } |
| 48 | |
| 49 | const char* colortype_name(SkColorType ct) { |
| 50 | switch (ct) { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 51 | case kUnknown_SkColorType: return "Unknown"; |
| 52 | case kAlpha_8_SkColorType: return "Alpha_8"; |
| 53 | case kA16_unorm_SkColorType: return "Alpha_16"; |
| 54 | case kA16_float_SkColorType: return "A16_float"; |
| 55 | case kRGB_565_SkColorType: return "RGB_565"; |
| 56 | case kARGB_4444_SkColorType: return "ARGB_4444"; |
| 57 | case kRGBA_8888_SkColorType: return "RGBA_8888"; |
Brian Osman | 9f1e06a | 2021-08-10 14:39:18 -0400 | [diff] [blame] | 58 | case kSRGBA_8888_SkColorType: return "SRGBA_8888"; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 59 | case kRGB_888x_SkColorType: return "RGB_888x"; |
| 60 | case kBGRA_8888_SkColorType: return "BGRA_8888"; |
| 61 | case kRGBA_1010102_SkColorType: return "RGBA_1010102"; |
Robert Phillips | a50ce3c | 2020-04-28 22:30:33 +0000 | [diff] [blame] | 62 | case kBGRA_1010102_SkColorType: return "BGRA_1010102"; |
Robert Phillips | 9a30ee0 | 2020-04-29 08:58:39 -0400 | [diff] [blame] | 63 | case kRGB_101010x_SkColorType: return "RGB_101010x"; |
Mike Klein | f7eb054 | 2020-02-11 12:19:08 -0600 | [diff] [blame] | 64 | case kBGR_101010x_SkColorType: return "BGR_101010x"; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 65 | case kGray_8_SkColorType: return "Gray_8"; |
| 66 | case kRGBA_F16Norm_SkColorType: return "RGBA_F16Norm"; |
| 67 | case kRGBA_F16_SkColorType: return "RGBA_F16"; |
| 68 | case kRGBA_F32_SkColorType: return "RGBA_F32"; |
| 69 | case kR8G8_unorm_SkColorType: return "R8G8_unorm"; |
| 70 | case kR16G16_unorm_SkColorType: return "R16G16_unorm"; |
| 71 | case kR16G16_float_SkColorType: return "R16G16_float"; |
| 72 | case kR16G16B16A16_unorm_SkColorType: return "R16G16B16A16_unorm"; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 73 | } |
| 74 | SkASSERT(false); |
| 75 | return "unexpected colortype"; |
| 76 | } |
| 77 | |
Mike Klein | 9816878 | 2019-04-09 13:45:02 -0500 | [diff] [blame] | 78 | const char* colortype_depth(SkColorType ct) { |
| 79 | switch (ct) { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 80 | case kUnknown_SkColorType: return "Unknown"; |
| 81 | case kAlpha_8_SkColorType: return "A8"; |
| 82 | case kA16_unorm_SkColorType: return "A16"; |
| 83 | case kA16_float_SkColorType: return "AF16"; |
| 84 | case kRGB_565_SkColorType: return "565"; |
| 85 | case kARGB_4444_SkColorType: return "4444"; |
| 86 | case kRGBA_8888_SkColorType: return "8888"; |
Brian Osman | 9f1e06a | 2021-08-10 14:39:18 -0400 | [diff] [blame] | 87 | case kSRGBA_8888_SkColorType: return "8888"; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 88 | case kRGB_888x_SkColorType: return "888"; |
| 89 | case kBGRA_8888_SkColorType: return "8888"; |
| 90 | case kRGBA_1010102_SkColorType: return "1010102"; |
Robert Phillips | a50ce3c | 2020-04-28 22:30:33 +0000 | [diff] [blame] | 91 | case kBGRA_1010102_SkColorType: return "1010102"; |
Robert Phillips | 9a30ee0 | 2020-04-29 08:58:39 -0400 | [diff] [blame] | 92 | case kRGB_101010x_SkColorType: return "101010"; |
Mike Klein | f7eb054 | 2020-02-11 12:19:08 -0600 | [diff] [blame] | 93 | case kBGR_101010x_SkColorType: return "101010"; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 94 | case kGray_8_SkColorType: return "G8"; |
| 95 | case kRGBA_F16Norm_SkColorType: return "F16Norm"; // TODO: "F16"? |
| 96 | case kRGBA_F16_SkColorType: return "F16"; |
| 97 | case kRGBA_F32_SkColorType: return "F32"; |
| 98 | case kR8G8_unorm_SkColorType: return "88"; |
| 99 | case kR16G16_unorm_SkColorType: return "1616"; |
| 100 | case kR16G16_float_SkColorType: return "F16F16"; |
| 101 | case kR16G16B16A16_unorm_SkColorType: return "16161616"; |
Mike Klein | 9816878 | 2019-04-09 13:45:02 -0500 | [diff] [blame] | 102 | } |
| 103 | SkASSERT(false); |
| 104 | return "unexpected colortype"; |
| 105 | } |
| 106 | |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 107 | const char* tilemode_name(SkTileMode mode) { |
| 108 | switch (mode) { |
| 109 | case SkTileMode::kClamp: return "clamp"; |
| 110 | case SkTileMode::kRepeat: return "repeat"; |
| 111 | case SkTileMode::kMirror: return "mirror"; |
| 112 | case SkTileMode::kDecal: return "decal"; |
| 113 | } |
| 114 | SkASSERT(false); |
| 115 | return "unexpected tilemode"; |
| 116 | } |
Mike Klein | 9816878 | 2019-04-09 13:45:02 -0500 | [diff] [blame] | 117 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 118 | SkColor color_to_565(SkColor color) { |
| 119 | // Not a good idea to use this function for greyscale colors... |
| 120 | // it will add an obvious purple or green tint. |
| 121 | SkASSERT(SkColorGetR(color) != SkColorGetG(color) || SkColorGetR(color) != SkColorGetB(color) || |
| 122 | SkColorGetG(color) != SkColorGetB(color)); |
| 123 | |
| 124 | SkPMColor pmColor = SkPreMultiplyColor(color); |
| 125 | U16CPU color16 = SkPixel32ToPixel16(pmColor); |
| 126 | return SkPixel16ToColor(color16); |
| 127 | } |
| 128 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 129 | sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) { |
| 130 | SkBitmap bm; |
| 131 | bm.allocPixels(SkImageInfo::MakeS32(2 * size, 2 * size, kPremul_SkAlphaType)); |
| 132 | bm.eraseColor(c1); |
| 133 | bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2); |
| 134 | bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2); |
Mike Reed | 82abece | 2020-12-12 09:51:11 -0500 | [diff] [blame] | 135 | return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions()); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize) { |
| 139 | SkBitmap bitmap; |
| 140 | bitmap.allocPixels(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType)); |
| 141 | SkCanvas canvas(bitmap); |
| 142 | |
| 143 | ToolUtils::draw_checkerboard(&canvas, c1, c2, checkSize); |
| 144 | return bitmap; |
| 145 | } |
| 146 | |
Mike Reed | db873dd | 2020-12-04 12:22:10 -0500 | [diff] [blame] | 147 | sk_sp<SkImage> create_checkerboard_image(int w, int h, SkColor c1, SkColor c2, int checkSize) { |
| 148 | auto surf = SkSurface::MakeRasterN32Premul(w, h); |
| 149 | ToolUtils::draw_checkerboard(surf->getCanvas(), c1, c2, checkSize); |
| 150 | return surf->makeImageSnapshot(); |
| 151 | } |
| 152 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 153 | void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) { |
| 154 | SkPaint paint; |
| 155 | paint.setShader(create_checkerboard_shader(c1, c2, size)); |
| 156 | paint.setBlendMode(SkBlendMode::kSrc); |
| 157 | canvas->drawPaint(paint); |
| 158 | } |
| 159 | |
| 160 | SkBitmap |
| 161 | create_string_bitmap(int w, int h, SkColor c, int x, int y, int textSize, const char* str) { |
| 162 | SkBitmap bitmap; |
| 163 | bitmap.allocN32Pixels(w, h); |
| 164 | SkCanvas canvas(bitmap); |
| 165 | |
| 166 | SkPaint paint; |
| 167 | paint.setColor(c); |
| 168 | |
| 169 | SkFont font(ToolUtils::create_portable_typeface(), textSize); |
| 170 | |
| 171 | canvas.clear(0x00000000); |
| 172 | canvas.drawSimpleText(str, |
| 173 | strlen(str), |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 174 | SkTextEncoding::kUTF8, |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 175 | SkIntToScalar(x), |
| 176 | SkIntToScalar(y), |
| 177 | font, |
| 178 | paint); |
| 179 | |
| 180 | // Tag data as sRGB (without doing any color space conversion). Color-space aware configs |
| 181 | // will process this correctly but legacy configs will render as if this returned N32. |
| 182 | SkBitmap result; |
| 183 | result.setInfo(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType)); |
| 184 | result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0); |
| 185 | return result; |
| 186 | } |
| 187 | |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 188 | sk_sp<SkImage> create_string_image(int w, int h, SkColor c, int x, int y, int textSize, |
| 189 | const char* str) { |
| 190 | return create_string_bitmap(w, h, c, x, y, textSize, str).asImage(); |
| 191 | } |
| 192 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 193 | void add_to_text_blob_w_len(SkTextBlobBuilder* builder, |
| 194 | const char* text, |
| 195 | size_t len, |
| 196 | SkTextEncoding encoding, |
| 197 | const SkFont& font, |
| 198 | SkScalar x, |
| 199 | SkScalar y) { |
| 200 | int count = font.countText(text, len, encoding); |
Ben Wagner | 5da8e55 | 2019-12-06 16:24:00 -0500 | [diff] [blame] | 201 | if (count < 1) { |
| 202 | return; |
| 203 | } |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 204 | auto run = builder->allocRun(font, count, x, y); |
| 205 | font.textToGlyphs(text, len, encoding, run.glyphs, count); |
| 206 | } |
| 207 | |
| 208 | void add_to_text_blob(SkTextBlobBuilder* builder, |
| 209 | const char* text, |
| 210 | const SkFont& font, |
| 211 | SkScalar x, |
| 212 | SkScalar y) { |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 213 | add_to_text_blob_w_len(builder, text, strlen(text), SkTextEncoding::kUTF8, font, x, y); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void get_text_path(const SkFont& font, |
| 217 | const void* text, |
| 218 | size_t length, |
| 219 | SkTextEncoding encoding, |
| 220 | SkPath* dst, |
| 221 | const SkPoint pos[]) { |
| 222 | SkAutoToGlyphs atg(font, text, length, encoding); |
| 223 | const int count = atg.count(); |
| 224 | SkAutoTArray<SkPoint> computedPos; |
| 225 | if (pos == nullptr) { |
| 226 | computedPos.reset(count); |
| 227 | font.getPos(atg.glyphs(), count, &computedPos[0]); |
| 228 | pos = computedPos.get(); |
| 229 | } |
| 230 | |
| 231 | struct Rec { |
| 232 | SkPath* fDst; |
| 233 | const SkPoint* fPos; |
| 234 | } rec = {dst, pos}; |
| 235 | font.getPaths(atg.glyphs(), |
| 236 | atg.count(), |
| 237 | [](const SkPath* src, const SkMatrix& mx, void* ctx) { |
| 238 | Rec* rec = (Rec*)ctx; |
| 239 | if (src) { |
| 240 | SkMatrix tmp(mx); |
| 241 | tmp.postTranslate(rec->fPos->fX, rec->fPos->fY); |
| 242 | rec->fDst->addPath(*src, tmp); |
| 243 | } |
| 244 | rec->fPos += 1; |
| 245 | }, |
| 246 | &rec); |
| 247 | } |
| 248 | |
| 249 | SkPath make_star(const SkRect& bounds, int numPts, int step) { |
| 250 | SkASSERT(numPts != step); |
Mike Reed | e9d783c | 2020-08-17 14:14:13 -0400 | [diff] [blame] | 251 | SkPathBuilder builder; |
| 252 | builder.setFillType(SkPathFillType::kEvenOdd); |
| 253 | builder.moveTo(0, -1); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 254 | for (int i = 1; i < numPts; ++i) { |
| 255 | int idx = i * step % numPts; |
| 256 | SkScalar theta = idx * 2 * SK_ScalarPI / numPts + SK_ScalarPI / 2; |
| 257 | SkScalar x = SkScalarCos(theta); |
| 258 | SkScalar y = -SkScalarSin(theta); |
Mike Reed | e9d783c | 2020-08-17 14:14:13 -0400 | [diff] [blame] | 259 | builder.lineTo(x, y); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 260 | } |
Mike Reed | e9d783c | 2020-08-17 14:14:13 -0400 | [diff] [blame] | 261 | SkPath path = builder.detach(); |
Mike Reed | 2ac6ce8 | 2021-01-15 12:26:22 -0500 | [diff] [blame] | 262 | path.transform(SkMatrix::RectToRect(path.getBounds(), bounds)); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 263 | return path; |
| 264 | } |
| 265 | |
| 266 | static inline void norm_to_rgb(SkBitmap* bm, int x, int y, const SkVector3& norm) { |
| 267 | SkASSERT(SkScalarNearlyEqual(norm.length(), 1.0f)); |
| 268 | unsigned char r = static_cast<unsigned char>((0.5f * norm.fX + 0.5f) * 255); |
| 269 | unsigned char g = static_cast<unsigned char>((-0.5f * norm.fY + 0.5f) * 255); |
| 270 | unsigned char b = static_cast<unsigned char>((0.5f * norm.fZ + 0.5f) * 255); |
| 271 | *bm->getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b); |
| 272 | } |
| 273 | |
| 274 | void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst) { |
| 275 | const SkPoint center = |
| 276 | SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), dst.fTop + (dst.height() / 2.0f)); |
| 277 | const SkPoint halfSize = SkPoint::Make(dst.width() / 2.0f, dst.height() / 2.0f); |
| 278 | |
| 279 | SkVector3 norm; |
| 280 | |
| 281 | for (int y = dst.fTop; y < dst.fBottom; ++y) { |
| 282 | for (int x = dst.fLeft; x < dst.fRight; ++x) { |
| 283 | norm.fX = (x + 0.5f - center.fX) / halfSize.fX; |
| 284 | norm.fY = (y + 0.5f - center.fY) / halfSize.fY; |
| 285 | |
| 286 | SkScalar tmp = norm.fX * norm.fX + norm.fY * norm.fY; |
| 287 | if (tmp >= 1.0f) { |
| 288 | norm.set(0.0f, 0.0f, 1.0f); |
| 289 | } else { |
| 290 | norm.fZ = sqrtf(1.0f - tmp); |
| 291 | } |
| 292 | |
| 293 | norm_to_rgb(bm, x, y, norm); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst) { |
| 299 | const SkPoint center = |
| 300 | SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), dst.fTop + (dst.height() / 2.0f)); |
| 301 | |
| 302 | SkIRect inner = dst; |
| 303 | inner.inset(dst.width() / 4, dst.height() / 4); |
| 304 | |
| 305 | SkPoint3 norm; |
| 306 | const SkPoint3 left = SkPoint3::Make(-SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2); |
| 307 | const SkPoint3 up = SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarRoot2Over2); |
| 308 | const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2); |
| 309 | const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2); |
| 310 | |
| 311 | for (int y = dst.fTop; y < dst.fBottom; ++y) { |
| 312 | for (int x = dst.fLeft; x < dst.fRight; ++x) { |
| 313 | if (inner.contains(x, y)) { |
| 314 | norm.set(0.0f, 0.0f, 1.0f); |
| 315 | } else { |
| 316 | SkScalar locX = x + 0.5f - center.fX; |
| 317 | SkScalar locY = y + 0.5f - center.fY; |
| 318 | |
| 319 | if (locX >= 0.0f) { |
| 320 | if (locY > 0.0f) { |
| 321 | norm = locX >= locY ? right : down; // LR corner |
| 322 | } else { |
| 323 | norm = locX > -locY ? right : up; // UR corner |
| 324 | } |
| 325 | } else { |
| 326 | if (locY > 0.0f) { |
| 327 | norm = -locX > locY ? left : down; // LL corner |
| 328 | } else { |
| 329 | norm = locX > locY ? up : left; // UL corner |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | norm_to_rgb(bm, x, y, norm); |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst) { |
| 340 | const SkPoint center = |
| 341 | SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), dst.fTop + (dst.height() / 2.0f)); |
| 342 | |
| 343 | static const SkScalar k1OverRoot3 = 0.5773502692f; |
| 344 | |
| 345 | SkPoint3 norm; |
| 346 | const SkPoint3 leftUp = SkPoint3::Make(-k1OverRoot3, -k1OverRoot3, k1OverRoot3); |
| 347 | const SkPoint3 rightUp = SkPoint3::Make(k1OverRoot3, -k1OverRoot3, k1OverRoot3); |
| 348 | const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2); |
| 349 | |
| 350 | for (int y = dst.fTop; y < dst.fBottom; ++y) { |
| 351 | for (int x = dst.fLeft; x < dst.fRight; ++x) { |
| 352 | SkScalar locX = x + 0.5f - center.fX; |
| 353 | SkScalar locY = y + 0.5f - center.fY; |
| 354 | |
| 355 | if (locX >= 0.0f) { |
| 356 | if (locY > 0.0f) { |
| 357 | norm = locX >= locY ? rightUp : down; // LR corner |
| 358 | } else { |
| 359 | norm = rightUp; |
| 360 | } |
| 361 | } else { |
| 362 | if (locY > 0.0f) { |
| 363 | norm = -locX > locY ? leftUp : down; // LL corner |
| 364 | } else { |
| 365 | norm = leftUp; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | norm_to_rgb(bm, x, y, norm); |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | #if !defined(__clang__) && defined(_MSC_VER) |
| 375 | // MSVC takes ~2 minutes to compile this function with optimization. |
| 376 | // We don't really care to wait that long for this function. |
| 377 | #pragma optimize("", off) |
| 378 | #endif |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame] | 379 | SkPath make_big_path() { |
| 380 | SkPathBuilder path; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 381 | #include "BigPathBench.inc" // IWYU pragma: keep |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame] | 382 | return path.detach(); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | bool copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) { |
| 386 | SkPixmap srcPM; |
| 387 | if (!src.peekPixels(&srcPM)) { |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | SkBitmap tmpDst; |
| 392 | SkImageInfo dstInfo = srcPM.info().makeColorType(dstColorType); |
| 393 | if (!tmpDst.setInfo(dstInfo)) { |
| 394 | return false; |
| 395 | } |
| 396 | |
| 397 | if (!tmpDst.tryAllocPixels()) { |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | SkPixmap dstPM; |
| 402 | if (!tmpDst.peekPixels(&dstPM)) { |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | if (!srcPM.readPixels(dstPM)) { |
| 407 | return false; |
| 408 | } |
| 409 | |
| 410 | dst->swap(tmpDst); |
| 411 | return true; |
| 412 | } |
| 413 | |
| 414 | void copy_to_g8(SkBitmap* dst, const SkBitmap& src) { |
| 415 | SkASSERT(kBGRA_8888_SkColorType == src.colorType() || |
| 416 | kRGBA_8888_SkColorType == src.colorType()); |
| 417 | |
| 418 | SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType); |
| 419 | dst->allocPixels(grayInfo); |
| 420 | uint8_t* dst8 = (uint8_t*)dst->getPixels(); |
| 421 | const uint32_t* src32 = (const uint32_t*)src.getPixels(); |
| 422 | |
| 423 | const int w = src.width(); |
| 424 | const int h = src.height(); |
| 425 | const bool isBGRA = (kBGRA_8888_SkColorType == src.colorType()); |
| 426 | for (int y = 0; y < h; ++y) { |
| 427 | if (isBGRA) { |
| 428 | // BGRA |
| 429 | for (int x = 0; x < w; ++x) { |
| 430 | uint32_t s = src32[x]; |
| 431 | dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF); |
| 432 | } |
| 433 | } else { |
| 434 | // RGBA |
| 435 | for (int x = 0; x < w; ++x) { |
| 436 | uint32_t s = src32[x]; |
| 437 | dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF); |
| 438 | } |
| 439 | } |
| 440 | src32 = (const uint32_t*)((const char*)src32 + src.rowBytes()); |
| 441 | dst8 += dst->rowBytes(); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | ////////////////////////////////////////////////////////////////////////////////////////////// |
| 446 | |
| 447 | bool equal_pixels(const SkPixmap& a, const SkPixmap& b) { |
| 448 | if (a.width() != b.width() || a.height() != b.height() || a.colorType() != b.colorType()) { |
| 449 | return false; |
| 450 | } |
| 451 | |
| 452 | for (int y = 0; y < a.height(); ++y) { |
| 453 | const char* aptr = (const char*)a.addr(0, y); |
| 454 | const char* bptr = (const char*)b.addr(0, y); |
John Stiles | c1c3c6d | 2020-08-15 23:22:53 -0400 | [diff] [blame] | 455 | if (0 != memcmp(aptr, bptr, a.width() * a.info().bytesPerPixel())) { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 456 | return false; |
| 457 | } |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 458 | } |
| 459 | return true; |
| 460 | } |
| 461 | |
| 462 | bool equal_pixels(const SkBitmap& bm0, const SkBitmap& bm1) { |
| 463 | SkPixmap pm0, pm1; |
| 464 | return bm0.peekPixels(&pm0) && bm1.peekPixels(&pm1) && equal_pixels(pm0, pm1); |
| 465 | } |
| 466 | |
| 467 | bool equal_pixels(const SkImage* a, const SkImage* b) { |
| 468 | // ensure that peekPixels will succeed |
| 469 | auto imga = a->makeRasterImage(); |
| 470 | auto imgb = b->makeRasterImage(); |
| 471 | |
| 472 | SkPixmap pm0, pm1; |
| 473 | return imga->peekPixels(&pm0) && imgb->peekPixels(&pm1) && equal_pixels(pm0, pm1); |
| 474 | } |
| 475 | |
| 476 | sk_sp<SkSurface> makeSurface(SkCanvas* canvas, |
| 477 | const SkImageInfo& info, |
| 478 | const SkSurfaceProps* props) { |
| 479 | auto surf = canvas->makeSurface(info, props); |
| 480 | if (!surf) { |
| 481 | surf = SkSurface::MakeRaster(info, props); |
| 482 | } |
| 483 | return surf; |
| 484 | } |
Mike Klein | 19cc0f6 | 2019-03-22 15:30:07 -0500 | [diff] [blame] | 485 | |
| 486 | static DEFINE_bool(nativeFonts, true, |
| 487 | "If true, use native font manager and rendering. " |
| 488 | "If false, fonts will draw as portably as possible."); |
| 489 | #if defined(SK_BUILD_FOR_WIN) |
| 490 | static DEFINE_bool(gdi, false, |
| 491 | "Use GDI instead of DirectWrite for font rendering."); |
| 492 | #endif |
| 493 | |
| 494 | void SetDefaultFontMgr() { |
| 495 | if (!FLAGS_nativeFonts) { |
| 496 | gSkFontMgr_DefaultFactory = &ToolUtils::MakePortableFontMgr; |
| 497 | } |
| 498 | #if defined(SK_BUILD_FOR_WIN) |
| 499 | if (FLAGS_gdi) { |
| 500 | gSkFontMgr_DefaultFactory = &SkFontMgr_New_GDI; |
| 501 | } |
| 502 | #endif |
| 503 | } |
| 504 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 505 | } // namespace ToolUtils |