humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | 5ed1ea8 | 2018-09-25 17:06:12 -0400 | [diff] [blame] | 8 | #include "Resources.h" |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 9 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 10 | #include "sk_tool_utils.h" |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 11 | |
Mike Klein | 5ed1ea8 | 2018-09-25 17:06:12 -0400 | [diff] [blame] | 12 | static const char* kFilterQualityNames[] = { "none", "low", "medium", "high" }; |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 13 | |
Mike Klein | 5ed1ea8 | 2018-09-25 17:06:12 -0400 | [diff] [blame] | 14 | struct DownsampleBitmapGM : public skiagm::GM { |
| 15 | SkBitmap (*fMakeBitmap)(SkImageInfo); |
| 16 | SkString fName; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 17 | SkFilterQuality fFilterQuality; |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 18 | |
Mike Klein | 5ed1ea8 | 2018-09-25 17:06:12 -0400 | [diff] [blame] | 19 | DownsampleBitmapGM(SkBitmap (*fn)(SkImageInfo), const char* kind, SkFilterQuality fq) |
| 20 | : fMakeBitmap(fn) |
| 21 | , fName(SkStringPrintf("downsamplebitmap_%s_%s", kind, kFilterQualityNames[fq])) |
| 22 | , fFilterQuality(fq) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 23 | { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 24 | this->setBGColor(0xFFDDDDDD); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Mike Klein | 5ed1ea8 | 2018-09-25 17:06:12 -0400 | [diff] [blame] | 27 | SkString onShortName() override { return fName; } |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 28 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | SkISize onISize() override { |
Mike Klein | 5ed1ea8 | 2018-09-25 17:06:12 -0400 | [diff] [blame] | 30 | SkBitmap bm = fMakeBitmap(SkImageInfo::MakeN32Premul(1,1)/*whatever*/); |
| 31 | return SkISize::Make(bm.width(), 4 * bm.height()); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 32 | } |
| 33 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | void onDraw(SkCanvas* canvas) override { |
Mike Klein | 5ed1ea8 | 2018-09-25 17:06:12 -0400 | [diff] [blame] | 35 | SkImageInfo info = canvas->imageInfo(); |
| 36 | if (!info.colorType()) { info = info.makeColorType( kN32_SkColorType); } |
| 37 | if (!info.alphaType()) { info = info.makeAlphaType(kPremul_SkAlphaType); } |
| 38 | |
| 39 | SkBitmap bm = fMakeBitmap(info); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 40 | |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 41 | int curY = 0; |
| 42 | int curHeight; |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 43 | float curScale = 1; |
| 44 | do { |
| 45 | |
| 46 | SkMatrix matrix; |
| 47 | matrix.setScale( curScale, curScale ); |
| 48 | |
| 49 | SkPaint paint; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 50 | paint.setFilterQuality(fFilterQuality); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 51 | |
| 52 | canvas->save(); |
Mike Klein | 5ed1ea8 | 2018-09-25 17:06:12 -0400 | [diff] [blame] | 53 | canvas->translate(0, (SkScalar)curY); |
| 54 | canvas->concat(matrix); |
| 55 | canvas->drawBitmap(bm, 0, 0, &paint); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 56 | canvas->restore(); |
| 57 | |
Mike Klein | 5ed1ea8 | 2018-09-25 17:06:12 -0400 | [diff] [blame] | 58 | curHeight = (int) (bm.height() * curScale + 2); |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 59 | curY += curHeight; |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 60 | curScale *= 0.75f; |
Mike Klein | 5ed1ea8 | 2018-09-25 17:06:12 -0400 | [diff] [blame] | 61 | } while (curHeight >= 2 && curY < 4*bm.height()); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 62 | } |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
Mike Klein | 5ed1ea8 | 2018-09-25 17:06:12 -0400 | [diff] [blame] | 65 | static SkBitmap convert_bitmap_format(SkBitmap src, SkImageInfo info) { |
| 66 | SkBitmap dst; |
| 67 | dst.allocPixels(info.makeWH(src.width(), src.height())); |
| 68 | |
| 69 | SkPixmap pm; |
| 70 | SkAssertResult(dst.peekPixels(&pm)); |
| 71 | SkAssertResult(src.readPixels(pm)); |
| 72 | |
| 73 | return dst; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | static SkBitmap make_text(SkImageInfo info) { |
| 78 | const SkScalar textSize = 72; |
| 79 | |
| 80 | SkBitmap bm; |
| 81 | bm.allocPixels(info.makeWH(int(textSize * 8), int(textSize * 6))); |
| 82 | SkCanvas canvas(bm); |
| 83 | canvas.drawColor(SK_ColorWHITE); |
| 84 | |
| 85 | SkPaint paint; |
Mike Reed | c4745d6 | 2019-01-07 09:31:58 -0500 | [diff] [blame] | 86 | SkFont font; |
| 87 | font.setSubpixel(true); |
| 88 | font.setSize(textSize); |
Mike Klein | 5ed1ea8 | 2018-09-25 17:06:12 -0400 | [diff] [blame] | 89 | |
Mike Reed | c4745d6 | 2019-01-07 09:31:58 -0500 | [diff] [blame] | 90 | font.setTypeface(sk_tool_utils::create_portable_typeface("serif", SkFontStyle())); |
| 91 | canvas.drawString("Hamburgefons", textSize/2, 1.2f*textSize, font, paint); |
| 92 | font.setTypeface(sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Bold())); |
| 93 | canvas.drawString("Hamburgefons", textSize/2, 2.4f*textSize, font, paint); |
| 94 | font.setTypeface(sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Italic())); |
| 95 | canvas.drawString("Hamburgefons", textSize/2, 3.6f*textSize, font, paint); |
| 96 | font.setTypeface(sk_tool_utils::create_portable_typeface("serif", SkFontStyle::BoldItalic())); |
| 97 | canvas.drawString("Hamburgefons", textSize/2, 4.8f*textSize, font, paint); |
Mike Klein | 5ed1ea8 | 2018-09-25 17:06:12 -0400 | [diff] [blame] | 98 | |
| 99 | return bm; |
| 100 | } |
| 101 | DEF_GM( return new DownsampleBitmapGM(make_text, "text", kHigh_SkFilterQuality); ) |
| 102 | DEF_GM( return new DownsampleBitmapGM(make_text, "text", kMedium_SkFilterQuality); ) |
| 103 | DEF_GM( return new DownsampleBitmapGM(make_text, "text", kLow_SkFilterQuality); ) |
| 104 | DEF_GM( return new DownsampleBitmapGM(make_text, "text", kNone_SkFilterQuality); ) |
| 105 | |
| 106 | |
| 107 | static SkBitmap make_checkerboard(SkImageInfo info) { |
| 108 | const auto size = 512; |
| 109 | const auto numChecks = 256; |
| 110 | |
| 111 | SkBitmap bm; |
| 112 | bm.allocN32Pixels(size,size); |
| 113 | for (int y = 0; y < size; ++y) { |
| 114 | for (int x = 0; x < size; ++x) { |
| 115 | SkPMColor* s = bm.getAddr32(x, y); |
| 116 | int cx = (x * numChecks) / size; |
| 117 | int cy = (y * numChecks) / size; |
| 118 | if ((cx+cy)%2) { |
| 119 | *s = 0xFFFFFFFF; |
| 120 | } else { |
| 121 | *s = 0xFF000000; |
| 122 | } |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 123 | } |
Mike Klein | 5ed1ea8 | 2018-09-25 17:06:12 -0400 | [diff] [blame] | 124 | } |
| 125 | return convert_bitmap_format(bm, info); |
| 126 | } |
| 127 | DEF_GM( return new DownsampleBitmapGM(make_checkerboard, "checkerboard", kHigh_SkFilterQuality); ) |
| 128 | DEF_GM( return new DownsampleBitmapGM(make_checkerboard, "checkerboard", kMedium_SkFilterQuality); ) |
| 129 | DEF_GM( return new DownsampleBitmapGM(make_checkerboard, "checkerboard", kLow_SkFilterQuality); ) |
| 130 | DEF_GM( return new DownsampleBitmapGM(make_checkerboard, "checkerboard", kNone_SkFilterQuality); ) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 131 | |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 132 | |
Mike Klein | 5ed1ea8 | 2018-09-25 17:06:12 -0400 | [diff] [blame] | 133 | static SkBitmap make_image(SkImageInfo info) { |
| 134 | SkBitmap bm; |
| 135 | if (!GetResourceAsBitmap("images/mandrill_512.png", &bm)) { |
| 136 | bm.allocN32Pixels(1, 1); |
| 137 | bm.eraseARGB(255, 255, 0 , 0); // red == bad |
| 138 | } |
| 139 | return convert_bitmap_format(bm, info); |
| 140 | } |
| 141 | DEF_GM( return new DownsampleBitmapGM(make_image, "image", kHigh_SkFilterQuality); ) |
| 142 | DEF_GM( return new DownsampleBitmapGM(make_image, "image", kMedium_SkFilterQuality); ) |
| 143 | DEF_GM( return new DownsampleBitmapGM(make_image, "image", kLow_SkFilterQuality); ) |
| 144 | DEF_GM( return new DownsampleBitmapGM(make_image, "image", kNone_SkFilterQuality); ) |