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 "sk_tool_utils.h"
9 #include "sk_tool_utils_flags.h"
10
11 #include "Resources.h"
12 #include "SkBitmap.h"
13 #include "SkCanvas.h"
14 #include "SkCommonFlags.h"
15 #include "SkFontMgr.h"
16 #include "SkFontStyle.h"
17 #include "SkPixelRef.h"
18 #include "SkPoint3.h"
19 #include "SkShader.h"
20 #include "SkTestScalerContext.h"
21 #include "SkTextBlob.h"
22
23 DEFINE_bool(portableFonts, false, "Use portable fonts");
24
25 #if SK_SUPPORT_GPU
26 #include "effects/GrSRGBEffect.h"
27 #include "SkColorFilter.h"
28
29 // Color filter that just wraps GrSRGBEffect
30 class SkSRGBColorFilter : public SkColorFilter {
31 public:
Make(GrSRGBEffect::Mode mode)32 static sk_sp<SkColorFilter> Make(GrSRGBEffect::Mode mode) {
33 return sk_sp<SkColorFilter>(new SkSRGBColorFilter(mode));
34 }
35
asFragmentProcessor(GrContext *,SkColorSpace *) const36 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, SkColorSpace*) const override {
37 return GrSRGBEffect::Make(fMode);
38 }
39
filterSpan(const SkPMColor src[],int count,SkPMColor dst[]) const40 void filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const override {
41 SK_ABORT("SkSRGBColorFilter is only implemented for GPU");
42 }
getFactory() const43 Factory getFactory() const override { return nullptr; }
44
45 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const46 void toString(SkString* str) const override {}
47 #endif
48
49 private:
SkSRGBColorFilter(GrSRGBEffect::Mode mode)50 SkSRGBColorFilter(GrSRGBEffect::Mode mode) : fMode(mode) {}
51
52 GrSRGBEffect::Mode fMode;
53 typedef SkColorFilter INHERITED;
54 };
55 #endif
56
57 namespace sk_tool_utils {
58
59 /* these are the default fonts chosen by Chrome for serif, sans-serif, and monospace */
60 static const char* gStandardFontNames[][3] = {
61 { "Times", "Helvetica", "Courier" }, // Mac
62 { "Times New Roman", "Helvetica", "Courier" }, // iOS
63 { "Times New Roman", "Arial", "Courier New" }, // Win
64 { "Times New Roman", "Arial", "Monospace" }, // Ubuntu
65 { "serif", "sans-serif", "monospace" }, // Android
66 { "Tinos", "Arimo", "Cousine" } // ChromeOS
67 };
68
platform_font_name(const char * name)69 const char* platform_font_name(const char* name) {
70 SkString platform = major_platform_os_name();
71 int index;
72 if (!strcmp(name, "serif")) {
73 index = 0;
74 } else if (!strcmp(name, "san-serif")) {
75 index = 1;
76 } else if (!strcmp(name, "monospace")) {
77 index = 2;
78 } else {
79 return name;
80 }
81 if (platform.equals("Mac")) {
82 return gStandardFontNames[0][index];
83 }
84 if (platform.equals("iOS")) {
85 return gStandardFontNames[1][index];
86 }
87 if (platform.equals("Win")) {
88 return gStandardFontNames[2][index];
89 }
90 if (platform.equals("Ubuntu")) {
91 return gStandardFontNames[3][index];
92 }
93 if (platform.equals("Android")) {
94 return gStandardFontNames[4][index];
95 }
96 if (platform.equals("ChromeOS")) {
97 return gStandardFontNames[5][index];
98 }
99 return name;
100 }
101
platform_os_emoji()102 const char* platform_os_emoji() {
103 const char* osName = platform_os_name();
104 if (!strcmp(osName, "Android") || !strcmp(osName, "Ubuntu")) {
105 return "CBDT";
106 }
107 if (!strncmp(osName, "Mac", 3) || !strncmp(osName, "iOS", 3)) {
108 return "SBIX";
109 }
110 if (!strncmp(osName, "Win", 3)) {
111 return "COLR";
112 }
113 return "";
114 }
115
emoji_typeface()116 sk_sp<SkTypeface> emoji_typeface() {
117 if (!strcmp(sk_tool_utils::platform_os_emoji(), "CBDT")) {
118 return MakeResourceAsTypeface("/fonts/Funkster.ttf");
119 }
120 if (!strcmp(sk_tool_utils::platform_os_emoji(), "SBIX")) {
121 return SkTypeface::MakeFromName("Apple Color Emoji", SkFontStyle());
122 }
123 if (!strcmp(sk_tool_utils::platform_os_emoji(), "COLR")) {
124 sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
125 const char *colorEmojiFontName = "Segoe UI Emoji";
126 sk_sp<SkTypeface> typeface(fm->matchFamilyStyle(colorEmojiFontName, SkFontStyle()));
127 if (typeface) {
128 return typeface;
129 }
130 sk_sp<SkTypeface> fallback(fm->matchFamilyStyleCharacter(
131 colorEmojiFontName, SkFontStyle(), nullptr /* bcp47 */, 0 /* bcp47Count */,
132 0x1f4b0 /* character: */));
133 if (fallback) {
134 return fallback;
135 }
136 // If we don't have Segoe UI Emoji and can't find a fallback, try Segoe UI Symbol.
137 // Windows 7 does not have Segoe UI Emoji; Segoe UI Symbol has the (non - color) emoji.
138 return SkTypeface::MakeFromName("Segoe UI Symbol", SkFontStyle());
139 }
140 return nullptr;
141 }
142
emoji_sample_text()143 const char* emoji_sample_text() {
144 if (!strcmp(sk_tool_utils::platform_os_emoji(), "CBDT")) {
145 return "Hamburgefons";
146 }
147 if (!strcmp(sk_tool_utils::platform_os_emoji(), "SBIX") ||
148 !strcmp(sk_tool_utils::platform_os_emoji(), "COLR"))
149 {
150 return "\xF0\x9F\x92\xB0" "\xF0\x9F\x8F\xA1" "\xF0\x9F\x8E\x85" //
151 "\xF0\x9F\x8D\xAA" "\xF0\x9F\x8D\x95" "\xF0\x9F\x9A\x80" //
152 "\xF0\x9F\x9A\xBB" "\xF0\x9F\x92\xA9" "\xF0\x9F\x93\xB7" //
153 "\xF0\x9F\x93\xA6" //
154 "\xF0\x9F\x87\xBA" "\xF0\x9F\x87\xB8" "\xF0\x9F\x87\xA6"; //
155 }
156 return "";
157 }
158
platform_os_name()159 const char* platform_os_name() {
160 for (int index = 0; index < FLAGS_key.count(); index += 2) {
161 if (!strcmp("os", FLAGS_key[index])) {
162 return FLAGS_key[index + 1];
163 }
164 }
165 // when running SampleApp or dm without a --key pair, omit the platform name
166 return "";
167 }
168
169 // omit version number in returned value
major_platform_os_name()170 SkString major_platform_os_name() {
171 SkString name;
172 for (int index = 0; index < FLAGS_key.count(); index += 2) {
173 if (!strcmp("os", FLAGS_key[index])) {
174 const char* platform = FLAGS_key[index + 1];
175 const char* end = platform;
176 while (*end && (*end < '0' || *end > '9')) {
177 ++end;
178 }
179 name.append(platform, end - platform);
180 break;
181 }
182 }
183 return name;
184 }
185
platform_extra_config(const char * config)186 const char* platform_extra_config(const char* config) {
187 for (int index = 0; index < FLAGS_key.count(); index += 2) {
188 if (!strcmp("extra_config", FLAGS_key[index]) && !strcmp(config, FLAGS_key[index + 1])) {
189 return config;
190 }
191 }
192 return "";
193 }
194
colortype_name(SkColorType ct)195 const char* colortype_name(SkColorType ct) {
196 switch (ct) {
197 case kUnknown_SkColorType: return "Unknown";
198 case kAlpha_8_SkColorType: return "Alpha_8";
199 case kIndex_8_SkColorType: return "Index_8";
200 case kARGB_4444_SkColorType: return "ARGB_4444";
201 case kRGB_565_SkColorType: return "RGB_565";
202 case kRGBA_8888_SkColorType: return "RGBA_8888";
203 case kBGRA_8888_SkColorType: return "BGRA_8888";
204 case kRGBA_F16_SkColorType: return "RGBA_F16";
205 default:
206 SkASSERT(false);
207 return "unexpected colortype";
208 }
209 }
210
color_to_565(SkColor color)211 SkColor color_to_565(SkColor color) {
212 SkPMColor pmColor = SkPreMultiplyColor(color);
213 U16CPU color16 = SkPixel32ToPixel16(pmColor);
214 return SkPixel16ToColor(color16);
215 }
216
create_portable_typeface(const char * name,SkFontStyle style)217 sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style) {
218 return create_font(name, style);
219 }
220
set_portable_typeface(SkPaint * paint,const char * name,SkFontStyle style)221 void set_portable_typeface(SkPaint* paint, const char* name, SkFontStyle style) {
222 paint->setTypeface(create_font(name, style));
223 }
224
write_pixels(SkCanvas * canvas,const SkBitmap & bitmap,int x,int y,SkColorType colorType,SkAlphaType alphaType)225 void write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y,
226 SkColorType colorType, SkAlphaType alphaType) {
227 SkBitmap tmp(bitmap);
228 tmp.lockPixels();
229
230 const SkImageInfo info = SkImageInfo::Make(tmp.width(), tmp.height(), colorType, alphaType);
231
232 canvas->writePixels(info, tmp.getPixels(), tmp.rowBytes(), x, y);
233 }
234
create_checkerboard_shader(SkColor c1,SkColor c2,int size)235 sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
236 SkBitmap bm;
237 bm.allocPixels(SkImageInfo::MakeS32(2 * size, 2 * size, kPremul_SkAlphaType));
238 bm.eraseColor(c1);
239 bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
240 bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
241 return SkShader::MakeBitmapShader(
242 bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
243 }
244
create_checkerboard_bitmap(int w,int h,SkColor c1,SkColor c2,int checkSize)245 SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize) {
246 SkBitmap bitmap;
247 bitmap.allocPixels(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
248 SkCanvas canvas(bitmap);
249
250 sk_tool_utils::draw_checkerboard(&canvas, c1, c2, checkSize);
251 return bitmap;
252 }
253
draw_checkerboard(SkCanvas * canvas,SkColor c1,SkColor c2,int size)254 void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
255 SkPaint paint;
256 paint.setShader(create_checkerboard_shader(c1, c2, size));
257 paint.setBlendMode(SkBlendMode::kSrc);
258 canvas->drawPaint(paint);
259 }
260
create_string_bitmap(int w,int h,SkColor c,int x,int y,int textSize,const char * str)261 SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
262 int textSize, const char* str) {
263 SkBitmap bitmap;
264 bitmap.allocN32Pixels(w, h);
265 SkCanvas canvas(bitmap);
266
267 SkPaint paint;
268 paint.setAntiAlias(true);
269 sk_tool_utils::set_portable_typeface(&paint);
270 paint.setColor(c);
271 paint.setTextSize(SkIntToScalar(textSize));
272
273 canvas.clear(0x00000000);
274 canvas.drawText(str, strlen(str), SkIntToScalar(x), SkIntToScalar(y), paint);
275
276 // Tag data as sRGB (without doing any color space conversion). Color-space aware configs
277 // will process this correctly but legacy configs will render as if this returned N32.
278 SkBitmap result;
279 result.setInfo(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType));
280 result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
281 return result;
282 }
283
add_to_text_blob(SkTextBlobBuilder * builder,const char * text,const SkPaint & origPaint,SkScalar x,SkScalar y)284 void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkPaint& origPaint,
285 SkScalar x, SkScalar y) {
286 SkPaint paint(origPaint);
287 SkTDArray<uint16_t> glyphs;
288
289 size_t len = strlen(text);
290 glyphs.append(paint.textToGlyphs(text, len, nullptr));
291 paint.textToGlyphs(text, len, glyphs.begin());
292
293 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
294 const SkTextBlobBuilder::RunBuffer& run = builder->allocRun(paint, glyphs.count(), x, y,
295 nullptr);
296 memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t));
297 }
298
norm_to_rgb(SkBitmap * bm,int x,int y,const SkVector3 & norm)299 static inline void norm_to_rgb(SkBitmap* bm, int x, int y, const SkVector3& norm) {
300 SkASSERT(SkScalarNearlyEqual(norm.length(), 1.0f));
301 unsigned char r = static_cast<unsigned char>((0.5f * norm.fX + 0.5f) * 255);
302 unsigned char g = static_cast<unsigned char>((-0.5f * norm.fY + 0.5f) * 255);
303 unsigned char b = static_cast<unsigned char>((0.5f * norm.fZ + 0.5f) * 255);
304 *bm->getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b);
305 }
306
create_hemi_normal_map(SkBitmap * bm,const SkIRect & dst)307 void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst) {
308 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
309 dst.fTop + (dst.height() / 2.0f));
310 const SkPoint halfSize = SkPoint::Make(dst.width() / 2.0f, dst.height() / 2.0f);
311
312 SkVector3 norm;
313
314 for (int y = dst.fTop; y < dst.fBottom; ++y) {
315 for (int x = dst.fLeft; x < dst.fRight; ++x) {
316 norm.fX = (x + 0.5f - center.fX) / halfSize.fX;
317 norm.fY = (y + 0.5f - center.fY) / halfSize.fY;
318
319 SkScalar tmp = norm.fX * norm.fX + norm.fY * norm.fY;
320 if (tmp >= 1.0f) {
321 norm.set(0.0f, 0.0f, 1.0f);
322 } else {
323 norm.fZ = sqrtf(1.0f - tmp);
324 }
325
326 norm_to_rgb(bm, x, y, norm);
327 }
328 }
329 }
330
create_frustum_normal_map(SkBitmap * bm,const SkIRect & dst)331 void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst) {
332 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
333 dst.fTop + (dst.height() / 2.0f));
334
335 SkIRect inner = dst;
336 inner.inset(dst.width()/4, dst.height()/4);
337
338 SkPoint3 norm;
339 const SkPoint3 left = SkPoint3::Make(-SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
340 const SkPoint3 up = SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
341 const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
342 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
343
344 for (int y = dst.fTop; y < dst.fBottom; ++y) {
345 for (int x = dst.fLeft; x < dst.fRight; ++x) {
346 if (inner.contains(x, y)) {
347 norm.set(0.0f, 0.0f, 1.0f);
348 } else {
349 SkScalar locX = x + 0.5f - center.fX;
350 SkScalar locY = y + 0.5f - center.fY;
351
352 if (locX >= 0.0f) {
353 if (locY > 0.0f) {
354 norm = locX >= locY ? right : down; // LR corner
355 } else {
356 norm = locX > -locY ? right : up; // UR corner
357 }
358 } else {
359 if (locY > 0.0f) {
360 norm = -locX > locY ? left : down; // LL corner
361 } else {
362 norm = locX > locY ? up : left; // UL corner
363 }
364 }
365 }
366
367 norm_to_rgb(bm, x, y, norm);
368 }
369 }
370 }
371
create_tetra_normal_map(SkBitmap * bm,const SkIRect & dst)372 void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst) {
373 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
374 dst.fTop + (dst.height() / 2.0f));
375
376 static const SkScalar k1OverRoot3 = 0.5773502692f;
377
378 SkPoint3 norm;
379 const SkPoint3 leftUp = SkPoint3::Make(-k1OverRoot3, -k1OverRoot3, k1OverRoot3);
380 const SkPoint3 rightUp = SkPoint3::Make(k1OverRoot3, -k1OverRoot3, k1OverRoot3);
381 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
382
383 for (int y = dst.fTop; y < dst.fBottom; ++y) {
384 for (int x = dst.fLeft; x < dst.fRight; ++x) {
385 SkScalar locX = x + 0.5f - center.fX;
386 SkScalar locY = y + 0.5f - center.fY;
387
388 if (locX >= 0.0f) {
389 if (locY > 0.0f) {
390 norm = locX >= locY ? rightUp : down; // LR corner
391 } else {
392 norm = rightUp;
393 }
394 } else {
395 if (locY > 0.0f) {
396 norm = -locX > locY ? leftUp : down; // LL corner
397 } else {
398 norm = leftUp;
399 }
400 }
401
402 norm_to_rgb(bm, x, y, norm);
403 }
404 }
405 }
406
407 #if defined(_MSC_VER)
408 // MSVC takes ~2 minutes to compile this function with optimization.
409 // We don't really care to wait that long for this function.
410 #pragma optimize("", off)
411 #endif
make_big_path(SkPath & path)412 void make_big_path(SkPath& path) {
413 #include "BigPathBench.inc"
414 }
415
gaussian2d_value(int x,int y,float sigma)416 static float gaussian2d_value(int x, int y, float sigma) {
417 // don't bother with the scale term since we're just going to normalize the
418 // kernel anyways
419 float temp = expf(-(x*x + y*y)/(2*sigma*sigma));
420 return temp;
421 }
422
create_2d_kernel(float sigma,int * filterSize)423 static float* create_2d_kernel(float sigma, int* filterSize) {
424 // We will actually take 2*halfFilterSize+1 samples (i.e., our filter kernel
425 // sizes are always odd)
426 int halfFilterSize = SkScalarCeilToInt(6*sigma)/2;
427 int wh = *filterSize = 2*halfFilterSize + 1;
428
429 float* temp = new float[wh*wh];
430
431 float filterTot = 0.0f;
432 for (int yOff = 0; yOff < wh; ++yOff) {
433 for (int xOff = 0; xOff < wh; ++xOff) {
434 temp[yOff*wh+xOff] = gaussian2d_value(xOff-halfFilterSize, yOff-halfFilterSize, sigma);
435
436 filterTot += temp[yOff*wh+xOff];
437 }
438 }
439
440 // normalize the kernel
441 for (int yOff = 0; yOff < wh; ++yOff) {
442 for (int xOff = 0; xOff < wh; ++xOff) {
443 temp[yOff*wh+xOff] /= filterTot;
444 }
445 }
446
447 return temp;
448 }
449
blur_pixel(const SkBitmap & bm,int x,int y,float * kernel,int wh)450 static SkPMColor blur_pixel(const SkBitmap& bm, int x, int y, float* kernel, int wh) {
451 SkASSERT(wh & 0x1);
452
453 int halfFilterSize = (wh-1)/2;
454
455 float r = 0.0f, g = 0.0f, b = 0.0f;
456 for (int yOff = 0; yOff < wh; ++yOff) {
457 int ySamp = y + yOff - halfFilterSize;
458
459 if (ySamp < 0) {
460 ySamp = 0;
461 } else if (ySamp > bm.height()-1) {
462 ySamp = bm.height()-1;
463 }
464
465 for (int xOff = 0; xOff < wh; ++xOff) {
466 int xSamp = x + xOff - halfFilterSize;
467
468 if (xSamp < 0) {
469 xSamp = 0;
470 } else if (xSamp > bm.width()-1) {
471 xSamp = bm.width()-1;
472 }
473
474 float filter = kernel[yOff*wh + xOff];
475
476 SkPMColor c = *bm.getAddr32(xSamp, ySamp);
477
478 r += SkGetPackedR32(c) * filter;
479 g += SkGetPackedG32(c) * filter;
480 b += SkGetPackedB32(c) * filter;
481 }
482 }
483
484 U8CPU r8, g8, b8;
485
486 r8 = (U8CPU) (r+0.5f);
487 g8 = (U8CPU) (g+0.5f);
488 b8 = (U8CPU) (b+0.5f);
489
490 return SkPackARGB32(255, r8, g8, b8);
491 }
492
slow_blur(const SkBitmap & src,float sigma)493 SkBitmap slow_blur(const SkBitmap& src, float sigma) {
494 SkBitmap dst;
495
496 dst.allocN32Pixels(src.width(), src.height(), true);
497
498 int wh;
499 std::unique_ptr<float[]> kernel(create_2d_kernel(sigma, &wh));
500
501 for (int y = 0; y < src.height(); ++y) {
502 for (int x = 0; x < src.width(); ++x) {
503 *dst.getAddr32(x, y) = blur_pixel(src, x, y, kernel.get(), wh);
504 }
505 }
506
507 return dst;
508 }
509
510 // compute the intersection point between the diagonal and the ellipse in the
511 // lower right corner
intersection(SkScalar w,SkScalar h)512 static SkPoint intersection(SkScalar w, SkScalar h) {
513 SkASSERT(w > 0.0f || h > 0.0f);
514
515 return SkPoint::Make(w / SK_ScalarSqrt2, h / SK_ScalarSqrt2);
516 }
517
518 // Use the intersection of the corners' diagonals with their ellipses to shrink
519 // the bounding rect
compute_central_occluder(const SkRRect & rr)520 SkRect compute_central_occluder(const SkRRect& rr) {
521 const SkRect r = rr.getBounds();
522
523 SkScalar newL = r.fLeft, newT = r.fTop, newR = r.fRight, newB = r.fBottom;
524
525 SkVector radii = rr.radii(SkRRect::kUpperLeft_Corner);
526 if (!radii.isZero()) {
527 SkPoint p = intersection(radii.fX, radii.fY);
528
529 newL = SkTMax(newL, r.fLeft + radii.fX - p.fX);
530 newT = SkTMax(newT, r.fTop + radii.fY - p.fY);
531 }
532
533 radii = rr.radii(SkRRect::kUpperRight_Corner);
534 if (!radii.isZero()) {
535 SkPoint p = intersection(radii.fX, radii.fY);
536
537 newR = SkTMin(newR, r.fRight + p.fX - radii.fX);
538 newT = SkTMax(newT, r.fTop + radii.fY - p.fY);
539 }
540
541 radii = rr.radii(SkRRect::kLowerRight_Corner);
542 if (!radii.isZero()) {
543 SkPoint p = intersection(radii.fX, radii.fY);
544
545 newR = SkTMin(newR, r.fRight + p.fX - radii.fX);
546 newB = SkTMin(newB, r.fBottom - radii.fY + p.fY);
547 }
548
549 radii = rr.radii(SkRRect::kLowerLeft_Corner);
550 if (!radii.isZero()) {
551 SkPoint p = intersection(radii.fX, radii.fY);
552
553 newL = SkTMax(newL, r.fLeft + radii.fX - p.fX);
554 newB = SkTMin(newB, r.fBottom - radii.fY + p.fY);
555 }
556
557 return SkRect::MakeLTRB(newL, newT, newR, newB);
558 }
559
560 // The widest inset rect
compute_widest_occluder(const SkRRect & rr)561 SkRect compute_widest_occluder(const SkRRect& rr) {
562 const SkRect& r = rr.getBounds();
563
564 const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner);
565 const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner);
566 const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner);
567 const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner);
568
569 SkScalar maxT = SkTMax(ul.fY, ur.fY);
570 SkScalar maxB = SkTMax(ll.fY, lr.fY);
571
572 return SkRect::MakeLTRB(r.fLeft, r.fTop + maxT, r.fRight, r.fBottom - maxB);
573
574 }
575
576 // The tallest inset rect
compute_tallest_occluder(const SkRRect & rr)577 SkRect compute_tallest_occluder(const SkRRect& rr) {
578 const SkRect& r = rr.getBounds();
579
580 const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner);
581 const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner);
582 const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner);
583 const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner);
584
585 SkScalar maxL = SkTMax(ul.fX, ll.fX);
586 SkScalar maxR = SkTMax(ur.fX, lr.fX);
587
588 return SkRect::MakeLTRB(r.fLeft + maxL, r.fTop, r.fRight - maxR, r.fBottom);
589 }
590
copy_to_g8(SkBitmap * dst,const SkBitmap & src)591 void copy_to_g8(SkBitmap* dst, const SkBitmap& src) {
592 SkASSERT(kBGRA_8888_SkColorType == src.colorType() ||
593 kRGBA_8888_SkColorType == src.colorType());
594
595 SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType);
596 dst->allocPixels(grayInfo);
597 uint8_t* dst8 = (uint8_t*)dst->getPixels();
598 const uint32_t* src32 = (const uint32_t*)src.getPixels();
599
600 const int w = src.width();
601 const int h = src.height();
602 const bool isBGRA = (kBGRA_8888_SkColorType == src.colorType());
603 for (int y = 0; y < h; ++y) {
604 if (isBGRA) {
605 // BGRA
606 for (int x = 0; x < w; ++x) {
607 uint32_t s = src32[x];
608 dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF);
609 }
610 } else {
611 // RGBA
612 for (int x = 0; x < w; ++x) {
613 uint32_t s = src32[x];
614 dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF);
615 }
616 }
617 src32 = (const uint32_t*)((const char*)src32 + src.rowBytes());
618 dst8 += dst->rowBytes();
619 }
620 }
621
622 #if SK_SUPPORT_GPU
MakeLinearToSRGBColorFilter()623 sk_sp<SkColorFilter> MakeLinearToSRGBColorFilter() {
624 return SkSRGBColorFilter::Make(GrSRGBEffect::Mode::kLinearToSRGB);
625 }
626
MakeSRGBToLinearColorFilter()627 sk_sp<SkColorFilter> MakeSRGBToLinearColorFilter() {
628 return SkSRGBColorFilter::Make(GrSRGBEffect::Mode::kSRGBToLinear);
629 }
630 #endif
631
632 } // namespace sk_tool_utils
633