blob: cc521d00a052cff293cc45fa3ce0f5cc3182ea64 [file] [log] [blame]
humper@google.com9c96d4b2013-07-14 01:44:59 +00001/*
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 Klein5ed1ea82018-09-25 17:06:12 -04008#include "Resources.h"
humper@google.com9c96d4b2013-07-14 01:44:59 +00009#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -040010#include "sk_tool_utils.h"
humper@google.com9c96d4b2013-07-14 01:44:59 +000011
Mike Klein5ed1ea82018-09-25 17:06:12 -040012static const char* kFilterQualityNames[] = { "none", "low", "medium", "high" };
humper@google.com9c96d4b2013-07-14 01:44:59 +000013
Mike Klein5ed1ea82018-09-25 17:06:12 -040014struct DownsampleBitmapGM : public skiagm::GM {
15 SkBitmap (*fMakeBitmap)(SkImageInfo);
16 SkString fName;
reed93a12152015-03-16 10:08:34 -070017 SkFilterQuality fFilterQuality;
humper@google.com9c96d4b2013-07-14 01:44:59 +000018
Mike Klein5ed1ea82018-09-25 17:06:12 -040019 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.com9c96d4b2013-07-14 01:44:59 +000023 {
Mike Kleind46dce32018-08-16 10:17:03 -040024 this->setBGColor(0xFFDDDDDD);
humper@google.com9c96d4b2013-07-14 01:44:59 +000025 }
26
Mike Klein5ed1ea82018-09-25 17:06:12 -040027 SkString onShortName() override { return fName; }
humper@google.com9c96d4b2013-07-14 01:44:59 +000028
mtklein36352bf2015-03-25 18:17:31 -070029 SkISize onISize() override {
Mike Klein5ed1ea82018-09-25 17:06:12 -040030 SkBitmap bm = fMakeBitmap(SkImageInfo::MakeN32Premul(1,1)/*whatever*/);
31 return SkISize::Make(bm.width(), 4 * bm.height());
humper@google.com9c96d4b2013-07-14 01:44:59 +000032 }
33
mtklein36352bf2015-03-25 18:17:31 -070034 void onDraw(SkCanvas* canvas) override {
Mike Klein5ed1ea82018-09-25 17:06:12 -040035 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.com9c96d4b2013-07-14 01:44:59 +000040
mtklein6a2f5fe2014-09-03 13:40:52 -070041 int curY = 0;
42 int curHeight;
humper@google.com9c96d4b2013-07-14 01:44:59 +000043 float curScale = 1;
44 do {
45
46 SkMatrix matrix;
47 matrix.setScale( curScale, curScale );
48
49 SkPaint paint;
reed93a12152015-03-16 10:08:34 -070050 paint.setFilterQuality(fFilterQuality);
humper@google.com9c96d4b2013-07-14 01:44:59 +000051
52 canvas->save();
Mike Klein5ed1ea82018-09-25 17:06:12 -040053 canvas->translate(0, (SkScalar)curY);
54 canvas->concat(matrix);
55 canvas->drawBitmap(bm, 0, 0, &paint);
humper@google.com9c96d4b2013-07-14 01:44:59 +000056 canvas->restore();
57
Mike Klein5ed1ea82018-09-25 17:06:12 -040058 curHeight = (int) (bm.height() * curScale + 2);
mtklein6a2f5fe2014-09-03 13:40:52 -070059 curY += curHeight;
humper@google.com9c96d4b2013-07-14 01:44:59 +000060 curScale *= 0.75f;
Mike Klein5ed1ea82018-09-25 17:06:12 -040061 } while (curHeight >= 2 && curY < 4*bm.height());
humper@google.com9c96d4b2013-07-14 01:44:59 +000062 }
humper@google.com9c96d4b2013-07-14 01:44:59 +000063};
64
Mike Klein5ed1ea82018-09-25 17:06:12 -040065static 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
77static 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 Reedc4745d62019-01-07 09:31:58 -050086 SkFont font;
87 font.setSubpixel(true);
88 font.setSize(textSize);
Mike Klein5ed1ea82018-09-25 17:06:12 -040089
Mike Reedc4745d62019-01-07 09:31:58 -050090 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 Klein5ed1ea82018-09-25 17:06:12 -040098
99 return bm;
100}
101DEF_GM( return new DownsampleBitmapGM(make_text, "text", kHigh_SkFilterQuality); )
102DEF_GM( return new DownsampleBitmapGM(make_text, "text", kMedium_SkFilterQuality); )
103DEF_GM( return new DownsampleBitmapGM(make_text, "text", kLow_SkFilterQuality); )
104DEF_GM( return new DownsampleBitmapGM(make_text, "text", kNone_SkFilterQuality); )
105
106
107static 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.com9c96d4b2013-07-14 01:44:59 +0000123 }
Mike Klein5ed1ea82018-09-25 17:06:12 -0400124 }
125 return convert_bitmap_format(bm, info);
126}
127DEF_GM( return new DownsampleBitmapGM(make_checkerboard, "checkerboard", kHigh_SkFilterQuality); )
128DEF_GM( return new DownsampleBitmapGM(make_checkerboard, "checkerboard", kMedium_SkFilterQuality); )
129DEF_GM( return new DownsampleBitmapGM(make_checkerboard, "checkerboard", kLow_SkFilterQuality); )
130DEF_GM( return new DownsampleBitmapGM(make_checkerboard, "checkerboard", kNone_SkFilterQuality); )
humper@google.com9c96d4b2013-07-14 01:44:59 +0000131
humper@google.com9c96d4b2013-07-14 01:44:59 +0000132
Mike Klein5ed1ea82018-09-25 17:06:12 -0400133static 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}
141DEF_GM( return new DownsampleBitmapGM(make_image, "image", kHigh_SkFilterQuality); )
142DEF_GM( return new DownsampleBitmapGM(make_image, "image", kMedium_SkFilterQuality); )
143DEF_GM( return new DownsampleBitmapGM(make_image, "image", kLow_SkFilterQuality); )
144DEF_GM( return new DownsampleBitmapGM(make_image, "image", kNone_SkFilterQuality); )