blob: 5e8880f4972f39873b16c005b79c25cc93894f0b [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"
15#include "include/core/SkPath.h"
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 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);
238 SkPath path;
Mike Reed7d34dc72019-11-26 12:17:17 -0500239 path.setFillType(SkPathFillType::kEvenOdd);
Mike Kleinea3f0142019-03-20 11:12:10 -0500240 path.moveTo(0, -1);
241 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);
246 path.lineTo(x, y);
247 }
248 path.transform(SkMatrix::MakeRectToRect(path.getBounds(), bounds, SkMatrix::kFill_ScaleToFit));
249 return path;
250}
251
252static inline void norm_to_rgb(SkBitmap* bm, int x, int y, const SkVector3& norm) {
253 SkASSERT(SkScalarNearlyEqual(norm.length(), 1.0f));
254 unsigned char r = static_cast<unsigned char>((0.5f * norm.fX + 0.5f) * 255);
255 unsigned char g = static_cast<unsigned char>((-0.5f * norm.fY + 0.5f) * 255);
256 unsigned char b = static_cast<unsigned char>((0.5f * norm.fZ + 0.5f) * 255);
257 *bm->getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b);
258}
259
260void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst) {
261 const SkPoint center =
262 SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), dst.fTop + (dst.height() / 2.0f));
263 const SkPoint halfSize = SkPoint::Make(dst.width() / 2.0f, dst.height() / 2.0f);
264
265 SkVector3 norm;
266
267 for (int y = dst.fTop; y < dst.fBottom; ++y) {
268 for (int x = dst.fLeft; x < dst.fRight; ++x) {
269 norm.fX = (x + 0.5f - center.fX) / halfSize.fX;
270 norm.fY = (y + 0.5f - center.fY) / halfSize.fY;
271
272 SkScalar tmp = norm.fX * norm.fX + norm.fY * norm.fY;
273 if (tmp >= 1.0f) {
274 norm.set(0.0f, 0.0f, 1.0f);
275 } else {
276 norm.fZ = sqrtf(1.0f - tmp);
277 }
278
279 norm_to_rgb(bm, x, y, norm);
280 }
281 }
282}
283
284void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst) {
285 const SkPoint center =
286 SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), dst.fTop + (dst.height() / 2.0f));
287
288 SkIRect inner = dst;
289 inner.inset(dst.width() / 4, dst.height() / 4);
290
291 SkPoint3 norm;
292 const SkPoint3 left = SkPoint3::Make(-SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
293 const SkPoint3 up = SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
294 const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
295 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
296
297 for (int y = dst.fTop; y < dst.fBottom; ++y) {
298 for (int x = dst.fLeft; x < dst.fRight; ++x) {
299 if (inner.contains(x, y)) {
300 norm.set(0.0f, 0.0f, 1.0f);
301 } else {
302 SkScalar locX = x + 0.5f - center.fX;
303 SkScalar locY = y + 0.5f - center.fY;
304
305 if (locX >= 0.0f) {
306 if (locY > 0.0f) {
307 norm = locX >= locY ? right : down; // LR corner
308 } else {
309 norm = locX > -locY ? right : up; // UR corner
310 }
311 } else {
312 if (locY > 0.0f) {
313 norm = -locX > locY ? left : down; // LL corner
314 } else {
315 norm = locX > locY ? up : left; // UL corner
316 }
317 }
318 }
319
320 norm_to_rgb(bm, x, y, norm);
321 }
322 }
323}
324
325void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst) {
326 const SkPoint center =
327 SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), dst.fTop + (dst.height() / 2.0f));
328
329 static const SkScalar k1OverRoot3 = 0.5773502692f;
330
331 SkPoint3 norm;
332 const SkPoint3 leftUp = SkPoint3::Make(-k1OverRoot3, -k1OverRoot3, k1OverRoot3);
333 const SkPoint3 rightUp = SkPoint3::Make(k1OverRoot3, -k1OverRoot3, k1OverRoot3);
334 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
335
336 for (int y = dst.fTop; y < dst.fBottom; ++y) {
337 for (int x = dst.fLeft; x < dst.fRight; ++x) {
338 SkScalar locX = x + 0.5f - center.fX;
339 SkScalar locY = y + 0.5f - center.fY;
340
341 if (locX >= 0.0f) {
342 if (locY > 0.0f) {
343 norm = locX >= locY ? rightUp : down; // LR corner
344 } else {
345 norm = rightUp;
346 }
347 } else {
348 if (locY > 0.0f) {
349 norm = -locX > locY ? leftUp : down; // LL corner
350 } else {
351 norm = leftUp;
352 }
353 }
354
355 norm_to_rgb(bm, x, y, norm);
356 }
357 }
358}
359
360#if !defined(__clang__) && defined(_MSC_VER)
361// MSVC takes ~2 minutes to compile this function with optimization.
362// We don't really care to wait that long for this function.
363#pragma optimize("", off)
364#endif
365void make_big_path(SkPath& path) {
366#include "BigPathBench.inc" // IWYU pragma: keep
367}
368
369bool copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) {
370 SkPixmap srcPM;
371 if (!src.peekPixels(&srcPM)) {
372 return false;
373 }
374
375 SkBitmap tmpDst;
376 SkImageInfo dstInfo = srcPM.info().makeColorType(dstColorType);
377 if (!tmpDst.setInfo(dstInfo)) {
378 return false;
379 }
380
381 if (!tmpDst.tryAllocPixels()) {
382 return false;
383 }
384
385 SkPixmap dstPM;
386 if (!tmpDst.peekPixels(&dstPM)) {
387 return false;
388 }
389
390 if (!srcPM.readPixels(dstPM)) {
391 return false;
392 }
393
394 dst->swap(tmpDst);
395 return true;
396}
397
398void copy_to_g8(SkBitmap* dst, const SkBitmap& src) {
399 SkASSERT(kBGRA_8888_SkColorType == src.colorType() ||
400 kRGBA_8888_SkColorType == src.colorType());
401
402 SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType);
403 dst->allocPixels(grayInfo);
404 uint8_t* dst8 = (uint8_t*)dst->getPixels();
405 const uint32_t* src32 = (const uint32_t*)src.getPixels();
406
407 const int w = src.width();
408 const int h = src.height();
409 const bool isBGRA = (kBGRA_8888_SkColorType == src.colorType());
410 for (int y = 0; y < h; ++y) {
411 if (isBGRA) {
412 // BGRA
413 for (int x = 0; x < w; ++x) {
414 uint32_t s = src32[x];
415 dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF);
416 }
417 } else {
418 // RGBA
419 for (int x = 0; x < w; ++x) {
420 uint32_t s = src32[x];
421 dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF);
422 }
423 }
424 src32 = (const uint32_t*)((const char*)src32 + src.rowBytes());
425 dst8 += dst->rowBytes();
426 }
427}
428
429//////////////////////////////////////////////////////////////////////////////////////////////
430
431bool equal_pixels(const SkPixmap& a, const SkPixmap& b) {
432 if (a.width() != b.width() || a.height() != b.height() || a.colorType() != b.colorType()) {
433 return false;
434 }
435
436 for (int y = 0; y < a.height(); ++y) {
437 const char* aptr = (const char*)a.addr(0, y);
438 const char* bptr = (const char*)b.addr(0, y);
439 if (memcmp(aptr, bptr, a.width() * a.info().bytesPerPixel())) {
440 return false;
441 }
442 aptr += a.rowBytes();
443 bptr += b.rowBytes();
444 }
445 return true;
446}
447
448bool equal_pixels(const SkBitmap& bm0, const SkBitmap& bm1) {
449 SkPixmap pm0, pm1;
450 return bm0.peekPixels(&pm0) && bm1.peekPixels(&pm1) && equal_pixels(pm0, pm1);
451}
452
453bool equal_pixels(const SkImage* a, const SkImage* b) {
454 // ensure that peekPixels will succeed
455 auto imga = a->makeRasterImage();
456 auto imgb = b->makeRasterImage();
457
458 SkPixmap pm0, pm1;
459 return imga->peekPixels(&pm0) && imgb->peekPixels(&pm1) && equal_pixels(pm0, pm1);
460}
461
462sk_sp<SkSurface> makeSurface(SkCanvas* canvas,
463 const SkImageInfo& info,
464 const SkSurfaceProps* props) {
465 auto surf = canvas->makeSurface(info, props);
466 if (!surf) {
467 surf = SkSurface::MakeRaster(info, props);
468 }
469 return surf;
470}
Mike Klein19cc0f62019-03-22 15:30:07 -0500471
472static DEFINE_bool(nativeFonts, true,
473 "If true, use native font manager and rendering. "
474 "If false, fonts will draw as portably as possible.");
475#if defined(SK_BUILD_FOR_WIN)
476 static DEFINE_bool(gdi, false,
477 "Use GDI instead of DirectWrite for font rendering.");
478#endif
479
480void SetDefaultFontMgr() {
481 if (!FLAGS_nativeFonts) {
482 gSkFontMgr_DefaultFactory = &ToolUtils::MakePortableFontMgr;
483 }
484#if defined(SK_BUILD_FOR_WIN)
485 if (FLAGS_gdi) {
486 gSkFontMgr_DefaultFactory = &SkFontMgr_New_GDI;
487 }
488#endif
489}
490
Mike Kleinea3f0142019-03-20 11:12:10 -0500491} // namespace ToolUtils