Coverage Report

Created: 2024-09-14 07:19

/src/skia/tools/ToolUtils.cpp
Line
Count
Source (jump to first uncovered line)
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
#include "tools/ToolUtils.h"
9
10
#include "include/core/SkAlphaType.h"
11
#include "include/core/SkBitmap.h"
12
#include "include/core/SkBlendMode.h"
13
#include "include/core/SkCanvas.h"
14
#include "include/core/SkColorPriv.h"
15
#include "include/core/SkColorSpace.h"
16
#include "include/core/SkColorType.h"
17
#include "include/core/SkFont.h"
18
#include "include/core/SkFontTypes.h"
19
#include "include/core/SkImage.h"
20
#include "include/core/SkImageInfo.h"
21
#include "include/core/SkMatrix.h"
22
#include "include/core/SkPaint.h"
23
#include "include/core/SkPath.h"
24
#include "include/core/SkPathBuilder.h"
25
#include "include/core/SkPathTypes.h"
26
#include "include/core/SkPicture.h"
27
#include "include/core/SkPixelRef.h"  // IWYU pragma: keep
28
#include "include/core/SkPixmap.h"
29
#include "include/core/SkPoint3.h"
30
#include "include/core/SkRefCnt.h"
31
#include "include/core/SkSamplingOptions.h"
32
#include "include/core/SkStream.h"
33
#include "include/core/SkSurface.h"
34
#include "include/core/SkTextBlob.h"
35
#include "include/core/SkTileMode.h"
36
#include "include/core/SkTypeface.h"
37
#include "include/effects/SkGradientShader.h"
38
#include "include/private/SkColorData.h"
39
#include "include/private/base/SkCPUTypes.h"
40
#include "include/private/base/SkTemplates.h"
41
#include "src/core/SkFontPriv.h"
42
#include "tools/SkMetaData.h"
43
44
#include <cmath>
45
#include <cstring>
46
47
using namespace skia_private;
48
49
namespace ToolUtils {
50
51
0
const char* alphatype_name(SkAlphaType at) {
52
0
    switch (at) {
53
0
        case kUnknown_SkAlphaType:  return "Unknown";
54
0
        case kOpaque_SkAlphaType:   return "Opaque";
55
0
        case kPremul_SkAlphaType:   return "Premul";
56
0
        case kUnpremul_SkAlphaType: return "Unpremul";
57
0
    }
58
0
    SkUNREACHABLE;
59
0
}
60
61
0
const char* colortype_name(SkColorType ct) {
62
0
    switch (ct) {
63
0
        case kUnknown_SkColorType:            return "Unknown";
64
0
        case kAlpha_8_SkColorType:            return "Alpha_8";
65
0
        case kA16_unorm_SkColorType:          return "Alpha_16";
66
0
        case kA16_float_SkColorType:          return "A16_float";
67
0
        case kRGB_565_SkColorType:            return "RGB_565";
68
0
        case kARGB_4444_SkColorType:          return "ARGB_4444";
69
0
        case kRGBA_8888_SkColorType:          return "RGBA_8888";
70
0
        case kSRGBA_8888_SkColorType:         return "SRGBA_8888";
71
0
        case kRGB_888x_SkColorType:           return "RGB_888x";
72
0
        case kBGRA_8888_SkColorType:          return "BGRA_8888";
73
0
        case kRGBA_1010102_SkColorType:       return "RGBA_1010102";
74
0
        case kBGRA_1010102_SkColorType:       return "BGRA_1010102";
75
0
        case kRGB_101010x_SkColorType:        return "RGB_101010x";
76
0
        case kBGR_101010x_SkColorType:        return "BGR_101010x";
77
0
        case kBGR_101010x_XR_SkColorType:     return "BGR_101010x_XR";
78
0
        case kRGBA_10x6_SkColorType:          return "RGBA_10x6";
79
0
        case kGray_8_SkColorType:             return "Gray_8";
80
0
        case kRGBA_F16Norm_SkColorType:       return "RGBA_F16Norm";
81
0
        case kRGB_F16F16F16x_SkColorType:     return "RGB_F16F16F16x";
82
0
        case kRGBA_F16_SkColorType:           return "RGBA_F16";
83
0
        case kRGBA_F32_SkColorType:           return "RGBA_F32";
84
0
        case kR8G8_unorm_SkColorType:         return "R8G8_unorm";
85
0
        case kR16G16_unorm_SkColorType:       return "R16G16_unorm";
86
0
        case kR16G16_float_SkColorType:       return "R16G16_float";
87
0
        case kR16G16B16A16_unorm_SkColorType: return "R16G16B16A16_unorm";
88
0
        case kR8_unorm_SkColorType:           return "R8_unorm";
89
0
        case kBGRA_10101010_XR_SkColorType:   return "BGRA_10101010_XR";
90
0
    }
91
0
    SkUNREACHABLE;
92
0
}
93
94
0
const char* colortype_depth(SkColorType ct) {
95
0
    switch (ct) {
96
0
        case kUnknown_SkColorType:            return "Unknown";
97
0
        case kAlpha_8_SkColorType:            return "A8";
98
0
        case kA16_unorm_SkColorType:          return "A16";
99
0
        case kA16_float_SkColorType:          return "AF16";
100
0
        case kRGB_565_SkColorType:            return "565";
101
0
        case kARGB_4444_SkColorType:          return "4444";
102
0
        case kRGBA_8888_SkColorType:          return "8888";
103
0
        case kSRGBA_8888_SkColorType:         return "8888";
104
0
        case kRGB_888x_SkColorType:           return "888";
105
0
        case kBGRA_8888_SkColorType:          return "8888";
106
0
        case kRGBA_1010102_SkColorType:       return "1010102";
107
0
        case kBGRA_1010102_SkColorType:       return "1010102";
108
0
        case kRGB_101010x_SkColorType:        return "101010";
109
0
        case kBGR_101010x_SkColorType:        return "101010";
110
0
        case kBGR_101010x_XR_SkColorType:     return "101010";
111
0
        case kBGRA_10101010_XR_SkColorType:   return "10101010";
112
0
        case kRGBA_10x6_SkColorType:          return "10101010";
113
0
        case kGray_8_SkColorType:             return "G8";
114
0
        case kRGBA_F16Norm_SkColorType:       return "F16Norm";
115
0
        case kRGB_F16F16F16x_SkColorType:     return "F16F16F16x";
116
0
        case kRGBA_F16_SkColorType:           return "F16";
117
0
        case kRGBA_F32_SkColorType:           return "F32";
118
0
        case kR8G8_unorm_SkColorType:         return "88";
119
0
        case kR16G16_unorm_SkColorType:       return "1616";
120
0
        case kR16G16_float_SkColorType:       return "F16F16";
121
0
        case kR16G16B16A16_unorm_SkColorType: return "16161616";
122
0
        case kR8_unorm_SkColorType:           return "R8";
123
0
    }
124
0
    SkUNREACHABLE;
125
0
}
126
127
0
const char* tilemode_name(SkTileMode mode) {
128
0
    switch (mode) {
129
0
        case SkTileMode::kClamp:  return "clamp";
130
0
        case SkTileMode::kRepeat: return "repeat";
131
0
        case SkTileMode::kMirror: return "mirror";
132
0
        case SkTileMode::kDecal:  return "decal";
133
0
    }
134
0
    SkUNREACHABLE;
135
0
}
136
137
0
SkColor color_to_565(SkColor color) {
138
    // Not a good idea to use this function for greyscale colors...
139
    // it will add an obvious purple or green tint.
140
0
    SkASSERT(SkColorGetR(color) != SkColorGetG(color) || SkColorGetR(color) != SkColorGetB(color) ||
141
0
             SkColorGetG(color) != SkColorGetB(color));
142
143
0
    SkPMColor pmColor = SkPreMultiplyColor(color);
144
0
    U16CPU    color16 = SkPixel32ToPixel16(pmColor);
145
0
    return SkPixel16ToColor(color16);
146
0
}
Unexecuted instantiation: ToolUtils::color_to_565(unsigned int)
Unexecuted instantiation: ToolUtils::color_to_565(unsigned int)
147
148
0
sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
149
0
    SkBitmap bm;
150
0
    bm.allocPixels(SkImageInfo::MakeS32(2 * size, 2 * size, kPremul_SkAlphaType));
151
0
    bm.eraseColor(c1);
152
0
    bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
153
0
    bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
154
0
    return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions());
155
0
}
156
157
0
SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize) {
158
0
    SkBitmap bitmap;
159
0
    bitmap.allocPixels(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
160
0
    SkCanvas canvas(bitmap);
161
162
0
    ToolUtils::draw_checkerboard(&canvas, c1, c2, checkSize);
163
0
    return bitmap;
164
0
}
165
166
0
sk_sp<SkImage> create_checkerboard_image(int w, int h, SkColor c1, SkColor c2, int checkSize) {
167
0
    auto surf = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(w, h));
168
0
    ToolUtils::draw_checkerboard(surf->getCanvas(), c1, c2, checkSize);
169
0
    return surf->makeImageSnapshot();
170
0
}
171
172
0
void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
173
0
    SkPaint paint;
174
0
    paint.setShader(create_checkerboard_shader(c1, c2, size));
175
0
    paint.setBlendMode(SkBlendMode::kSrc);
176
0
    canvas->drawPaint(paint);
177
0
}
178
179
int make_pixmaps(SkColorType ct,
180
                 SkAlphaType at,
181
                 bool withMips,
182
                 const SkColor4f colors[6],
183
                 SkPixmap pixmaps[6],
184
0
                 std::unique_ptr<char[]>* mem) {
185
186
0
    int levelSize = 32;
187
0
    int numMipLevels = withMips ? 6 : 1;
188
0
    size_t size = 0;
189
0
    SkImageInfo ii[6];
190
0
    size_t rowBytes[6];
191
0
    for (int level = 0; level < numMipLevels; ++level) {
192
0
        ii[level] = SkImageInfo::Make(levelSize, levelSize, ct, at);
193
0
        rowBytes[level] = ii[level].minRowBytes();
194
        // Make sure we test row bytes that aren't tight.
195
0
        if (!(level % 2)) {
196
0
            rowBytes[level] += (level + 1)*SkColorTypeBytesPerPixel(ii[level].colorType());
197
0
        }
198
0
        size += rowBytes[level]*ii[level].height();
199
0
        levelSize /= 2;
200
0
    }
201
0
    mem->reset(new char[size]);
202
0
    char* addr = mem->get();
203
0
    for (int level = 0; level < numMipLevels; ++level) {
204
0
        pixmaps[level].reset(ii[level], addr, rowBytes[level]);
205
0
        addr += rowBytes[level]*ii[level].height();
206
0
        pixmaps[level].erase(colors[level]);
207
0
    }
208
0
    return numMipLevels;
209
0
}
210
211
void add_to_text_blob_w_len(SkTextBlobBuilder* builder,
212
                            const char*        text,
213
                            size_t             len,
214
                            SkTextEncoding     encoding,
215
                            const SkFont&      font,
216
                            SkScalar           x,
217
0
                            SkScalar           y) {
218
0
    int  count = font.countText(text, len, encoding);
219
0
    if (count < 1) {
220
0
        return;
221
0
    }
222
0
    auto run   = builder->allocRun(font, count, x, y);
223
0
    font.textToGlyphs(text, len, encoding, run.glyphs, count);
224
0
}
225
226
void add_to_text_blob(SkTextBlobBuilder* builder,
227
                      const char*        text,
228
                      const SkFont&      font,
229
                      SkScalar           x,
230
0
                      SkScalar           y) {
231
0
    add_to_text_blob_w_len(builder, text, strlen(text), SkTextEncoding::kUTF8, font, x, y);
232
0
}
233
234
void get_text_path(const SkFont&  font,
235
                   const void*    text,
236
                   size_t         length,
237
                   SkTextEncoding encoding,
238
                   SkPath*        dst,
239
0
                   const SkPoint  pos[]) {
240
0
    SkAutoToGlyphs        atg(font, text, length, encoding);
241
0
    const int             count = atg.count();
242
0
    AutoTArray<SkPoint> computedPos;
243
0
    if (pos == nullptr) {
244
0
        computedPos.reset(count);
245
0
        font.getPos(atg.glyphs(), count, &computedPos[0]);
246
0
        pos = computedPos.get();
247
0
    }
248
249
0
    struct Rec {
250
0
        SkPath*        fDst;
251
0
        const SkPoint* fPos;
252
0
    } rec = {dst, pos};
253
0
    font.getPaths(atg.glyphs(),
254
0
                  atg.count(),
255
0
                  [](const SkPath* src, const SkMatrix& mx, void* ctx) {
256
0
                      Rec* rec = (Rec*)ctx;
257
0
                      if (src) {
258
0
                          SkMatrix tmp(mx);
259
0
                          tmp.postTranslate(rec->fPos->fX, rec->fPos->fY);
260
0
                          rec->fDst->addPath(*src, tmp);
261
0
                      }
262
0
                      rec->fPos += 1;
263
0
                  },
264
0
                  &rec);
265
0
}
266
267
0
SkPath make_star(const SkRect& bounds, int numPts, int step) {
268
0
    SkASSERT(numPts != step);
269
0
    SkPathBuilder builder;
270
0
    builder.setFillType(SkPathFillType::kEvenOdd);
271
0
    builder.moveTo(0, -1);
272
0
    for (int i = 1; i < numPts; ++i) {
273
0
        int      idx   = i * step % numPts;
274
0
        SkScalar theta = idx * 2 * SK_ScalarPI / numPts + SK_ScalarPI / 2;
275
0
        SkScalar x     = SkScalarCos(theta);
276
0
        SkScalar y     = -SkScalarSin(theta);
277
0
        builder.lineTo(x, y);
278
0
    }
279
0
    SkPath path = builder.detach();
280
0
    path.transform(SkMatrix::RectToRect(path.getBounds(), bounds));
281
0
    return path;
282
0
}
Unexecuted instantiation: ToolUtils::make_star(SkRect const&, int, int)
Unexecuted instantiation: ToolUtils::make_star(SkRect const&, int, int)
283
284
0
static inline void norm_to_rgb(SkBitmap* bm, int x, int y, const SkVector3& norm) {
285
0
    SkASSERT(SkScalarNearlyEqual(norm.length(), 1.0f));
286
0
    unsigned char r      = static_cast<unsigned char>((0.5f * norm.fX + 0.5f) * 255);
287
0
    unsigned char g      = static_cast<unsigned char>((-0.5f * norm.fY + 0.5f) * 255);
288
0
    unsigned char b      = static_cast<unsigned char>((0.5f * norm.fZ + 0.5f) * 255);
289
0
    *bm->getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b);
290
0
}
Unexecuted instantiation: ToolUtils.cpp:ToolUtils::norm_to_rgb(SkBitmap*, int, int, SkPoint3 const&)
Unexecuted instantiation: ToolUtils.cpp:ToolUtils::norm_to_rgb(SkBitmap*, int, int, SkPoint3 const&)
291
292
0
void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst) {
293
0
    const SkPoint center =
294
0
            SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), dst.fTop + (dst.height() / 2.0f));
295
0
    const SkPoint halfSize = SkPoint::Make(dst.width() / 2.0f, dst.height() / 2.0f);
296
297
0
    SkVector3 norm;
298
299
0
    for (int y = dst.fTop; y < dst.fBottom; ++y) {
300
0
        for (int x = dst.fLeft; x < dst.fRight; ++x) {
301
0
            norm.fX = (x + 0.5f - center.fX) / halfSize.fX;
302
0
            norm.fY = (y + 0.5f - center.fY) / halfSize.fY;
303
304
0
            SkScalar tmp = norm.fX * norm.fX + norm.fY * norm.fY;
305
0
            if (tmp >= 1.0f) {
306
0
                norm.set(0.0f, 0.0f, 1.0f);
307
0
            } else {
308
0
                norm.fZ = sqrtf(1.0f - tmp);
309
0
            }
310
311
0
            norm_to_rgb(bm, x, y, norm);
312
0
        }
313
0
    }
314
0
}
315
316
0
void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst) {
317
0
    const SkPoint center =
318
0
            SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), dst.fTop + (dst.height() / 2.0f));
319
320
0
    SkIRect inner = dst;
321
0
    inner.inset(dst.width() / 4, dst.height() / 4);
322
323
0
    SkPoint3       norm;
324
0
    const SkPoint3 left  = SkPoint3::Make(-SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
325
0
    const SkPoint3 up    = SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
326
0
    const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
327
0
    const SkPoint3 down  = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
328
329
0
    for (int y = dst.fTop; y < dst.fBottom; ++y) {
330
0
        for (int x = dst.fLeft; x < dst.fRight; ++x) {
331
0
            if (inner.contains(x, y)) {
332
0
                norm.set(0.0f, 0.0f, 1.0f);
333
0
            } else {
334
0
                SkScalar locX = x + 0.5f - center.fX;
335
0
                SkScalar locY = y + 0.5f - center.fY;
336
337
0
                if (locX >= 0.0f) {
338
0
                    if (locY > 0.0f) {
339
0
                        norm = locX >= locY ? right : down;  // LR corner
340
0
                    } else {
341
0
                        norm = locX > -locY ? right : up;  // UR corner
342
0
                    }
343
0
                } else {
344
0
                    if (locY > 0.0f) {
345
0
                        norm = -locX > locY ? left : down;  // LL corner
346
0
                    } else {
347
0
                        norm = locX > locY ? up : left;  // UL corner
348
0
                    }
349
0
                }
350
0
            }
351
352
0
            norm_to_rgb(bm, x, y, norm);
353
0
        }
354
0
    }
355
0
}
356
357
0
void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst) {
358
0
    const SkPoint center =
359
0
            SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), dst.fTop + (dst.height() / 2.0f));
360
361
0
    static const SkScalar k1OverRoot3 = 0.5773502692f;
362
363
0
    SkPoint3       norm;
364
0
    const SkPoint3 leftUp  = SkPoint3::Make(-k1OverRoot3, -k1OverRoot3, k1OverRoot3);
365
0
    const SkPoint3 rightUp = SkPoint3::Make(k1OverRoot3, -k1OverRoot3, k1OverRoot3);
366
0
    const SkPoint3 down    = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
367
368
0
    for (int y = dst.fTop; y < dst.fBottom; ++y) {
369
0
        for (int x = dst.fLeft; x < dst.fRight; ++x) {
370
0
            SkScalar locX = x + 0.5f - center.fX;
371
0
            SkScalar locY = y + 0.5f - center.fY;
372
373
0
            if (locX >= 0.0f) {
374
0
                if (locY > 0.0f) {
375
0
                    norm = locX >= locY ? rightUp : down;  // LR corner
376
0
                } else {
377
0
                    norm = rightUp;
378
0
                }
379
0
            } else {
380
0
                if (locY > 0.0f) {
381
0
                    norm = -locX > locY ? leftUp : down;  // LL corner
382
0
                } else {
383
0
                    norm = leftUp;
384
0
                }
385
0
            }
386
387
0
            norm_to_rgb(bm, x, y, norm);
388
0
        }
389
0
    }
390
0
}
391
392
0
bool copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) {
393
0
    SkPixmap srcPM;
394
0
    if (!src.peekPixels(&srcPM)) {
395
0
        return false;
396
0
    }
397
398
0
    SkBitmap    tmpDst;
399
0
    SkImageInfo dstInfo = srcPM.info().makeColorType(dstColorType);
400
0
    if (!tmpDst.setInfo(dstInfo)) {
401
0
        return false;
402
0
    }
403
404
0
    if (!tmpDst.tryAllocPixels()) {
405
0
        return false;
406
0
    }
407
408
0
    SkPixmap dstPM;
409
0
    if (!tmpDst.peekPixels(&dstPM)) {
410
0
        return false;
411
0
    }
412
413
0
    if (!srcPM.readPixels(dstPM)) {
414
0
        return false;
415
0
    }
416
417
0
    dst->swap(tmpDst);
418
0
    return true;
419
0
}
420
421
0
void copy_to_g8(SkBitmap* dst, const SkBitmap& src) {
422
0
    SkASSERT(kBGRA_8888_SkColorType == src.colorType() ||
423
0
             kRGBA_8888_SkColorType == src.colorType());
424
425
0
    SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType);
426
0
    dst->allocPixels(grayInfo);
427
0
    uint8_t*        dst8  = (uint8_t*)dst->getPixels();
428
0
    const uint32_t* src32 = (const uint32_t*)src.getPixels();
429
430
0
    const int  w      = src.width();
431
0
    const int  h      = src.height();
432
0
    const bool isBGRA = (kBGRA_8888_SkColorType == src.colorType());
433
0
    for (int y = 0; y < h; ++y) {
434
0
        if (isBGRA) {
435
            // BGRA
436
0
            for (int x = 0; x < w; ++x) {
437
0
                uint32_t s = src32[x];
438
0
                dst8[x]    = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF);
439
0
            }
440
0
        } else {
441
            // RGBA
442
0
            for (int x = 0; x < w; ++x) {
443
0
                uint32_t s = src32[x];
444
0
                dst8[x]    = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF);
445
0
            }
446
0
        }
447
0
        src32 = (const uint32_t*)((const char*)src32 + src.rowBytes());
448
0
        dst8 += dst->rowBytes();
449
0
    }
450
0
}
Unexecuted instantiation: ToolUtils::copy_to_g8(SkBitmap*, SkBitmap const&)
Unexecuted instantiation: ToolUtils::copy_to_g8(SkBitmap*, SkBitmap const&)
451
452
//////////////////////////////////////////////////////////////////////////////////////////////
453
454
0
bool equal_pixels(const SkPixmap& a, const SkPixmap& b) {
455
0
    if (a.width() != b.width() || a.height() != b.height()) {
456
0
        SkDebugf("[ToolUtils::equal_pixels] Dimensions do not match (%d x %d) != (%d x %d)\n",
457
0
                 a.width(), a.height(), b.width(), b.height());
458
0
        return false;
459
0
    }
460
461
0
    if (a.colorType() != b.colorType()) {
462
0
        SkDebugf("[ToolUtils::equal_pixels] colorType does not match %d != %d\n",
463
0
                 (int) a.colorType(), (int) b.colorType());
464
0
        return false;
465
0
    }
466
467
0
    for (int y = 0; y < a.height(); ++y) {
468
0
        const char* aptr = (const char*)a.addr(0, y);
469
0
        const char* bptr = (const char*)b.addr(0, y);
470
0
        if (0 != memcmp(aptr, bptr, a.width() * a.info().bytesPerPixel())) {
471
0
            SkDebugf("[ToolUtils::equal_pixels] row %d does not match byte for byte\n", y);
472
0
            return false;
473
0
        }
474
0
    }
475
0
    return true;
476
0
}
477
478
0
bool equal_pixels(const SkBitmap& bm0, const SkBitmap& bm1) {
479
0
    SkPixmap pm0, pm1;
480
0
    if (!bm0.peekPixels(&pm0)) {
481
0
        SkDebugf("Could not read pixels from A\n");
482
0
        return false;
483
0
    }
484
0
    if (!bm1.peekPixels(&pm1)) {
485
0
        SkDebugf("Could not read pixels from B\n");
486
0
        return false;
487
0
    }
488
0
    return equal_pixels(pm0, pm1);
489
0
}
490
491
0
bool equal_pixels(const SkImage* a, const SkImage* b) {
492
0
    SkASSERT_RELEASE(a);
493
0
    SkASSERT_RELEASE(b);
494
    // ensure that peekPixels will succeed
495
0
    auto imga = a->makeRasterImage();
496
0
    auto imgb = b->makeRasterImage();
497
498
0
    SkPixmap pm0, pm1;
499
0
    if (!imga->peekPixels(&pm0)) {
500
0
        SkDebugf("Could not read pixels from A\n");
501
0
        return false;
502
0
    }
503
0
    if (!imgb->peekPixels(&pm1)) {
504
0
        SkDebugf("Could not read pixels from B\n");
505
0
        return false;
506
0
    }
507
0
    return equal_pixels(pm0, pm1);
508
0
}
509
510
sk_sp<SkSurface> makeSurface(SkCanvas*             canvas,
511
                             const SkImageInfo&    info,
512
0
                             const SkSurfaceProps* props) {
513
0
    auto surf = canvas->makeSurface(info, props);
514
0
    if (!surf) {
515
0
        surf = SkSurfaces::Raster(info, props);
516
0
    }
517
0
    return surf;
518
0
}
519
520
VariationSliders::VariationSliders(SkTypeface* typeface,
521
0
                                   SkFontArguments::VariationPosition variationPosition) {
522
0
    if (!typeface) {
523
0
        return;
524
0
    }
525
526
0
    int numAxes = typeface->getVariationDesignParameters(nullptr, 0);
527
0
    if (numAxes < 0) {
528
0
        return;
529
0
    }
530
531
0
    std::unique_ptr<SkFontParameters::Variation::Axis[]> copiedAxes =
532
0
            std::make_unique<SkFontParameters::Variation::Axis[]>(numAxes);
533
534
0
    numAxes = typeface->getVariationDesignParameters(copiedAxes.get(), numAxes);
535
0
    if (numAxes < 0) {
536
0
        return;
537
0
    }
538
539
0
    auto argVariationPositionOrDefault = [&variationPosition](SkFourByteTag tag,
540
0
                                                              SkScalar defaultValue) -> SkScalar {
541
0
        for (int i = 0; i < variationPosition.coordinateCount; ++i) {
542
0
            if (variationPosition.coordinates[i].axis == tag) {
543
0
                return variationPosition.coordinates[i].value;
544
0
            }
545
0
        }
546
0
        return defaultValue;
547
0
    };
548
549
0
    fAxisSliders.resize(numAxes);
550
0
    fCoords = std::make_unique<SkFontArguments::VariationPosition::Coordinate[]>(numAxes);
551
0
    for (int i = 0; i < numAxes; ++i) {
552
0
        fAxisSliders[i].axis = copiedAxes[i];
553
0
        fAxisSliders[i].current =
554
0
                argVariationPositionOrDefault(copiedAxes[i].tag, copiedAxes[i].def);
555
0
        fAxisSliders[i].name = tagToString(fAxisSliders[i].axis.tag);
556
0
        fCoords[i] = { fAxisSliders[i].axis.tag, fAxisSliders[i].current };
557
0
    }
558
0
}
559
560
/* static */
561
0
SkString VariationSliders::tagToString(SkFourByteTag tag) {
562
0
    char tagAsString[5];
563
0
    tagAsString[4] = 0;
564
0
    tagAsString[0] = (char)(uint8_t)(tag >> 24);
565
0
    tagAsString[1] = (char)(uint8_t)(tag >> 16);
566
0
    tagAsString[2] = (char)(uint8_t)(tag >> 8);
567
0
    tagAsString[3] = (char)(uint8_t)(tag >> 0);
568
0
    return SkString(tagAsString);
569
0
}
570
571
0
bool VariationSliders::writeControls(SkMetaData* controls) {
572
0
    for (size_t i = 0; i < fAxisSliders.size(); ++i) {
573
0
        SkScalar axisVars[kAxisVarsSize];
574
575
0
        axisVars[0] = fAxisSliders[i].current;
576
0
        axisVars[1] = fAxisSliders[i].axis.min;
577
0
        axisVars[2] = fAxisSliders[i].axis.max;
578
0
        controls->setScalars(fAxisSliders[i].name.c_str(), kAxisVarsSize, axisVars);
579
0
    }
580
0
    return true;
581
0
}
582
583
0
void VariationSliders::readControls(const SkMetaData& controls, bool* changed) {
584
0
    for (size_t i = 0; i < fAxisSliders.size(); ++i) {
585
0
        SkScalar axisVars[kAxisVarsSize] = {0};
586
0
        int resultAxisVarsSize = 0;
587
0
        SkASSERT_RELEASE(controls.findScalars(
588
0
                tagToString(fAxisSliders[i].axis.tag).c_str(), &resultAxisVarsSize, axisVars));
589
0
        SkASSERT_RELEASE(resultAxisVarsSize == kAxisVarsSize);
590
0
        if (changed) {
591
0
            *changed |= fAxisSliders[i].current != axisVars[0];
592
0
        }
593
0
        fAxisSliders[i].current = axisVars[0];
594
0
        fCoords[i] = { fAxisSliders[i].axis.tag, fAxisSliders[i].current };
595
0
    }
596
0
}
597
598
0
SkSpan<const SkFontArguments::VariationPosition::Coordinate> VariationSliders::getCoordinates() {
599
0
    return SkSpan<const SkFontArguments::VariationPosition::Coordinate>{fCoords.get(),
600
0
                                                                        fAxisSliders.size()};
601
0
}
602
603
////////////////////////////////////////////////////////////////////////////////////////////////////
604
HilbertGenerator::HilbertGenerator(float desiredSize, float desiredLineWidth, int desiredDepth)
605
        : fDesiredSize(desiredSize)
606
        , fDesiredDepth(desiredDepth)
607
        , fSegmentLength(fDesiredSize / ((0x1 << fDesiredDepth) - 1.0f))
608
        , fDesiredLineWidth(desiredLineWidth)
609
        , fActualBounds(SkRect::MakeEmpty())
610
        , fCurPos(SkPoint::Make(0.0f, 0.0f))
611
        , fCurDir(0)
612
        , fExpectedLen(fSegmentLength * ((0x1 << (2*fDesiredDepth)) - 1.0f))
613
0
        , fCurLen(0.0f) {
614
0
}
615
616
0
void HilbertGenerator::draw(SkCanvas* canvas) {
617
0
    this->recursiveDraw(canvas, /* curDepth= */ 0, /* turnLeft= */ true);
618
619
0
    SkScalarNearlyEqual(fExpectedLen, fCurLen, 0.01f);
620
0
    SkScalarNearlyEqual(fDesiredSize, fActualBounds.width(), 0.01f);
621
0
    SkScalarNearlyEqual(fDesiredSize, fActualBounds.height(), 0.01f);
622
0
}
623
624
0
void HilbertGenerator::turn90(bool turnLeft) {
625
0
    fCurDir += turnLeft ? 90 : -90;
626
0
    if (fCurDir >= 360) {
627
0
        fCurDir = 0;
628
0
    } else if (fCurDir < 0) {
629
0
        fCurDir = 270;
630
0
    }
631
632
0
    SkASSERT(fCurDir == 0 || fCurDir == 90 || fCurDir == 180 || fCurDir == 270);
633
0
}
Unexecuted instantiation: ToolUtils::HilbertGenerator::turn90(bool)
Unexecuted instantiation: ToolUtils::HilbertGenerator::turn90(bool)
634
635
0
void HilbertGenerator::line(SkCanvas* canvas) {
636
637
0
    SkPoint before = fCurPos;
638
639
0
    SkRect r;
640
0
    switch (fCurDir) {
641
0
        case 0:
642
0
            r.fLeft = fCurPos.fX;
643
0
            r.fTop = fCurPos.fY - fDesiredLineWidth / 2.0f;
644
0
            r.fRight = fCurPos.fX + fSegmentLength;
645
0
            r.fBottom = fCurPos.fY + fDesiredLineWidth / 2.0f;
646
0
            fCurPos.fX += fSegmentLength;
647
0
            break;
648
0
        case 90:
649
0
            r.fLeft = fCurPos.fX - fDesiredLineWidth / 2.0f;
650
0
            r.fTop = fCurPos.fY - fSegmentLength;
651
0
            r.fRight = fCurPos.fX + fDesiredLineWidth / 2.0f;
652
0
            r.fBottom = fCurPos.fY;
653
0
            fCurPos.fY -= fSegmentLength;
654
0
            break;
655
0
        case 180:
656
0
            r.fLeft = fCurPos.fX - fSegmentLength;
657
0
            r.fTop = fCurPos.fY - fDesiredLineWidth / 2.0f;
658
0
            r.fRight = fCurPos.fX;
659
0
            r.fBottom = fCurPos.fY + fDesiredLineWidth / 2.0f;
660
0
            fCurPos.fX -= fSegmentLength;
661
0
            break;
662
0
        case 270:
663
0
            r.fLeft = fCurPos.fX - fDesiredLineWidth / 2.0f;
664
0
            r.fTop = fCurPos.fY;
665
0
            r.fRight = fCurPos.fX + fDesiredLineWidth / 2.0f;
666
0
            r.fBottom = fCurPos.fY + fSegmentLength;
667
0
            fCurPos.fY += fSegmentLength;
668
0
            break;
669
0
        default:
670
0
            return;
671
0
    }
672
673
0
    SkPoint pts[2] = { before, fCurPos };
674
675
0
    SkColor4f colors[2] = {
676
0
            this->getColor(fCurLen),
677
0
            this->getColor(fCurLen + fSegmentLength),
678
0
    };
679
680
0
    fCurLen += fSegmentLength;
681
0
    if (fActualBounds.isEmpty()) {
682
0
        fActualBounds = r;
683
0
    } else {
684
0
        fActualBounds.join(r);
685
0
    }
686
687
0
    SkPaint paint;
688
0
    paint.setShader(SkGradientShader::MakeLinear(pts, colors, /* colorSpace= */ nullptr,
689
0
                                                 /* pos= */ nullptr, 2, SkTileMode::kClamp));
690
0
    canvas->drawRect(r, paint);
691
0
}
692
693
0
void HilbertGenerator::recursiveDraw(SkCanvas* canvas, int curDepth, bool turnLeft) {
694
0
    if (curDepth >= fDesiredDepth) {
695
0
        return;
696
0
    }
697
698
0
    this->turn90(turnLeft);
699
0
    this->recursiveDraw(canvas, curDepth + 1, !turnLeft);
700
0
    this->line(canvas);
701
0
    this->turn90(!turnLeft);
702
0
    this->recursiveDraw(canvas, curDepth + 1, turnLeft);
703
0
    this->line(canvas);
704
0
    this->recursiveDraw(canvas, curDepth + 1, turnLeft);
705
0
    this->turn90(!turnLeft);
706
0
    this->line(canvas);
707
0
    this->recursiveDraw(canvas, curDepth + 1, !turnLeft);
708
0
    this->turn90(turnLeft);
709
0
}
710
711
0
SkColor4f HilbertGenerator::getColor(float curLen) {
712
0
    static const SkColor4f kColors[] = {
713
0
            SkColors::kBlack,
714
0
            SkColors::kBlue,
715
0
            SkColors::kCyan,
716
0
            SkColors::kGreen,
717
0
            SkColors::kYellow,
718
0
            SkColors::kRed,
719
0
            SkColors::kWhite,
720
0
    };
721
722
0
    static const float kStops[] = {
723
0
            0.0f,
724
0
            1.0f/6.0f,
725
0
            2.0f/6.0f,
726
0
            0.5f,
727
0
            4.0f/6.0f,
728
0
            5.0f/6.0f,
729
0
            1.0f,
730
0
    };
731
0
    static_assert(std::size(kColors) == std::size(kStops));
732
733
0
    float t = curLen / fExpectedLen;
734
0
    if (t <= 0.0f) {
735
0
        return kColors[0];
736
0
    } else if (t >= 1.0f) {
737
0
        return kColors[std::size(kColors)-1];
738
0
    }
739
740
0
    for (unsigned int i = 0; i < std::size(kColors)-1; ++i) {
741
0
        if (kStops[i] <= t && t <= kStops[i+1]) {
742
0
            t = (t - kStops[i]) / (kStops[i+1] - kStops[i]);
743
0
            SkASSERT(0.0f <= t && t <= 1.0f);
744
0
            return { kColors[i].fR * (1 - t) + kColors[i+1].fR * t,
745
0
                     kColors[i].fG * (1 - t) + kColors[i+1].fG * t,
746
0
                     kColors[i].fB * (1 - t) + kColors[i+1].fB * t,
747
0
                     kColors[i].fA * (1 - t) + kColors[i+1].fA * t };
748
749
0
        }
750
0
    }
751
752
0
    return SkColors::kBlack;
753
0
}
Unexecuted instantiation: ToolUtils::HilbertGenerator::getColor(float)
Unexecuted instantiation: ToolUtils::HilbertGenerator::getColor(float)
754
755
0
void ExtractPathsFromSKP(const char filepath[], std::function<PathSniffCallback> callback) {
756
0
    SkFILEStream stream(filepath);
757
0
    if (!stream.isValid()) {
758
0
        SkDebugf("ExtractPaths: invalid input file at \"%s\"\n", filepath);
759
0
        return;
760
0
    }
761
762
0
    class PathSniffer : public SkCanvas {
763
0
    public:
764
0
        PathSniffer(std::function<PathSniffCallback> callback)
765
0
                : SkCanvas(4096, 4096, nullptr)
766
0
                , fPathSniffCallback(callback) {}
767
0
    private:
768
0
        void onDrawPath(const SkPath& path, const SkPaint& paint) override {
769
0
            fPathSniffCallback(this->getTotalMatrix(), path, paint);
770
0
        }
771
0
        std::function<PathSniffCallback> fPathSniffCallback;
772
0
    };
773
774
0
    sk_sp<SkPicture> skp = SkPicture::MakeFromStream(&stream);
775
0
    if (!skp) {
776
0
        SkDebugf("ExtractPaths: couldn't load skp at \"%s\"\n", filepath);
777
0
        return;
778
0
    }
779
0
    PathSniffer pathSniffer(callback);
780
0
    skp->playback(&pathSniffer);
781
0
}
782
783
}  // namespace ToolUtils