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