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