blob: bb4736826f0684e81f2b8c1064e3ed89337343e8 [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
8#include "gm.h"
humper@google.com9c96d4b2013-07-14 01:44:59 +00009
tfarinabcbc1782014-06-18 14:32:48 -070010#include "Resources.h"
11#include "SkGradientShader.h"
humper@google.com9c96d4b2013-07-14 01:44:59 +000012#include "SkTypeface.h"
13#include "SkImageDecoder.h"
14#include "SkStream.h"
humper@google.comaf42a032013-07-25 14:34:13 +000015#include "SkPaint.h"
humper@google.com9c96d4b2013-07-14 01:44:59 +000016
reedc695e952015-01-16 05:01:16 -080017static void make_checker(SkBitmap* bm, int size, int numChecks) {
18 bm->allocN32Pixels(size, size);
19 for (int y = 0; y < size; ++y) {
20 for (int x = 0; x < size; ++x) {
21 SkPMColor* s = bm->getAddr32(x, y);
22 int cx = (x * numChecks) / size;
23 int cy = (y * numChecks) / size;
24 if ((cx+cy)%2) {
25 *s = 0xFFFFFFFF;
26 } else {
27 *s = 0xFF000000;
28 }
29 }
30 }
31}
32
humper@google.com9c96d4b2013-07-14 01:44:59 +000033static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style style) {
caryclark65cdba62015-06-15 06:51:08 -070034 sk_tool_utils::set_portable_typeface_always(paint, name, style);
humper@google.com9c96d4b2013-07-14 01:44:59 +000035}
36
37class DownsampleBitmapGM : public skiagm::GM {
humper@google.com9c96d4b2013-07-14 01:44:59 +000038public:
39 SkBitmap fBM;
40 SkString fName;
41 bool fBitmapMade;
reed93a12152015-03-16 10:08:34 -070042 SkFilterQuality fFilterQuality;
humper@google.com9c96d4b2013-07-14 01:44:59 +000043
reed93a12152015-03-16 10:08:34 -070044 DownsampleBitmapGM(SkFilterQuality filterQuality)
45 : fFilterQuality(filterQuality)
humper@google.com9c96d4b2013-07-14 01:44:59 +000046 {
caryclark65cdba62015-06-15 06:51:08 -070047 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
humper@google.com9c96d4b2013-07-14 01:44:59 +000048 fBitmapMade = false;
49 }
50
reed93a12152015-03-16 10:08:34 -070051 const char* filterQualityToString() {
52 static const char *filterQualityNames[] = {
humper@google.come2cf8052013-07-25 19:14:37 +000053 "none", "low", "medium", "high"
54 };
reed93a12152015-03-16 10:08:34 -070055 return filterQualityNames[fFilterQuality];
humper@google.comaf42a032013-07-25 14:34:13 +000056 }
humper@google.com9c96d4b2013-07-14 01:44:59 +000057
58protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000059
mtklein36352bf2015-03-25 18:17:31 -070060 SkString onShortName() override {
humper@google.com9c96d4b2013-07-14 01:44:59 +000061 return fName;
62 }
63
mtklein36352bf2015-03-25 18:17:31 -070064 SkISize onISize() override {
humper@google.com9c96d4b2013-07-14 01:44:59 +000065 make_bitmap_wrapper();
mtklein6a2f5fe2014-09-03 13:40:52 -070066 return SkISize::Make(fBM.width(), 4 * fBM.height());
humper@google.com9c96d4b2013-07-14 01:44:59 +000067 }
68
69 void make_bitmap_wrapper() {
70 if (!fBitmapMade) {
71 fBitmapMade = true;
72 make_bitmap();
73 }
74 }
75
76 virtual void make_bitmap() = 0;
77
mtklein36352bf2015-03-25 18:17:31 -070078 void onDraw(SkCanvas* canvas) override {
humper@google.com9c96d4b2013-07-14 01:44:59 +000079 make_bitmap_wrapper();
80
mtklein6a2f5fe2014-09-03 13:40:52 -070081 int curY = 0;
82 int curHeight;
humper@google.com9c96d4b2013-07-14 01:44:59 +000083 float curScale = 1;
84 do {
85
86 SkMatrix matrix;
87 matrix.setScale( curScale, curScale );
88
89 SkPaint paint;
reed93a12152015-03-16 10:08:34 -070090 paint.setFilterQuality(fFilterQuality);
humper@google.com9c96d4b2013-07-14 01:44:59 +000091
92 canvas->save();
mtklein6a2f5fe2014-09-03 13:40:52 -070093 canvas->translate(0, (SkScalar)curY);
Florin Malitac54d8db2014-12-10 12:02:16 -050094 canvas->concat(matrix);
95 canvas->drawBitmap(fBM, 0, 0, &paint);
humper@google.com9c96d4b2013-07-14 01:44:59 +000096 canvas->restore();
97
mtklein6a2f5fe2014-09-03 13:40:52 -070098 curHeight = (int) (fBM.height() * curScale + 2);
99 curY += curHeight;
humper@google.com9c96d4b2013-07-14 01:44:59 +0000100 curScale *= 0.75f;
mtklein6a2f5fe2014-09-03 13:40:52 -0700101 } while (curHeight >= 2 && curY < 4*fBM.height());
humper@google.com9c96d4b2013-07-14 01:44:59 +0000102 }
103
104private:
105 typedef skiagm::GM INHERITED;
106};
107
108class DownsampleBitmapTextGM: public DownsampleBitmapGM {
109 public:
reed93a12152015-03-16 10:08:34 -0700110 DownsampleBitmapTextGM(float textSize, SkFilterQuality filterQuality)
111 : INHERITED(filterQuality), fTextSize(textSize)
humper@google.com9c96d4b2013-07-14 01:44:59 +0000112 {
reed93a12152015-03-16 10:08:34 -0700113 fName.printf("downsamplebitmap_text_%s_%.2fpt", this->filterQualityToString(), fTextSize);
humper@google.com9c96d4b2013-07-14 01:44:59 +0000114 }
115
116 protected:
117 float fTextSize;
118
mtklein36352bf2015-03-25 18:17:31 -0700119 void make_bitmap() override {
reed@google.comeb9a46c2014-01-25 16:46:20 +0000120 fBM.allocN32Pixels(int(fTextSize * 8), int(fTextSize * 6));
humper@google.com9c96d4b2013-07-14 01:44:59 +0000121 SkCanvas canvas(fBM);
122 canvas.drawColor(SK_ColorWHITE);
123
124 SkPaint paint;
125 paint.setAntiAlias(true);
126 paint.setSubpixelText(true);
127 paint.setTextSize(fTextSize);
128
caryclark65cdba62015-06-15 06:51:08 -0700129 setTypeface(&paint, "serif", SkTypeface::kNormal);
humper@google.com9c96d4b2013-07-14 01:44:59 +0000130 canvas.drawText("Hamburgefons", 12, fTextSize/2, 1.2f*fTextSize, paint);
caryclark65cdba62015-06-15 06:51:08 -0700131 setTypeface(&paint, "serif", SkTypeface::kBold);
humper@google.com9c96d4b2013-07-14 01:44:59 +0000132 canvas.drawText("Hamburgefons", 12, fTextSize/2, 2.4f*fTextSize, paint);
caryclark65cdba62015-06-15 06:51:08 -0700133 setTypeface(&paint, "serif", SkTypeface::kItalic);
humper@google.com9c96d4b2013-07-14 01:44:59 +0000134 canvas.drawText("Hamburgefons", 12, fTextSize/2, 3.6f*fTextSize, paint);
caryclark65cdba62015-06-15 06:51:08 -0700135 setTypeface(&paint, "serif", SkTypeface::kBoldItalic);
humper@google.com9c96d4b2013-07-14 01:44:59 +0000136 canvas.drawText("Hamburgefons", 12, fTextSize/2, 4.8f*fTextSize, paint);
137 }
138 private:
139 typedef DownsampleBitmapGM INHERITED;
140};
141
142class DownsampleBitmapCheckerboardGM: public DownsampleBitmapGM {
143 public:
reed93a12152015-03-16 10:08:34 -0700144 DownsampleBitmapCheckerboardGM(int size, int numChecks, SkFilterQuality filterQuality)
145 : INHERITED(filterQuality), fSize(size), fNumChecks(numChecks)
humper@google.com9c96d4b2013-07-14 01:44:59 +0000146 {
reed93a12152015-03-16 10:08:34 -0700147 fName.printf("downsamplebitmap_checkerboard_%s_%d_%d", this->filterQualityToString(), fSize, fNumChecks);
humper@google.com9c96d4b2013-07-14 01:44:59 +0000148 }
149
150 protected:
151 int fSize;
152 int fNumChecks;
153
mtklein36352bf2015-03-25 18:17:31 -0700154 void make_bitmap() override {
reedc695e952015-01-16 05:01:16 -0800155 make_checker(&fBM, fSize, fNumChecks);
humper@google.com9c96d4b2013-07-14 01:44:59 +0000156 }
157 private:
158 typedef DownsampleBitmapGM INHERITED;
159};
160
161class DownsampleBitmapImageGM: public DownsampleBitmapGM {
162 public:
reed93a12152015-03-16 10:08:34 -0700163 DownsampleBitmapImageGM(const char filename[], SkFilterQuality filterQuality)
164 : INHERITED(filterQuality), fFilename(filename)
humper@google.com9c96d4b2013-07-14 01:44:59 +0000165 {
reed93a12152015-03-16 10:08:34 -0700166 fName.printf("downsamplebitmap_image_%s_%s", this->filterQualityToString(), filename);
humper@google.com9c96d4b2013-07-14 01:44:59 +0000167 }
168
169 protected:
170 SkString fFilename;
171 int fSize;
172
mtklein36352bf2015-03-25 18:17:31 -0700173 void make_bitmap() override {
tfarinabcbc1782014-06-18 14:32:48 -0700174 SkImageDecoder* codec = NULL;
tfarinac846f4a2014-07-01 12:35:49 -0700175 SkString resourcePath = GetResourcePath(fFilename.c_str());
tfarinabcbc1782014-06-18 14:32:48 -0700176 SkFILEStream stream(resourcePath.c_str());
humper@google.com9c96d4b2013-07-14 01:44:59 +0000177 if (stream.isValid()) {
178 codec = SkImageDecoder::Factory(&stream);
179 }
180 if (codec) {
181 stream.rewind();
reedbfefc7c2014-06-12 17:40:00 -0700182 codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode);
humper@google.com9c96d4b2013-07-14 01:44:59 +0000183 SkDELETE(codec);
184 } else {
reed@google.comeb9a46c2014-01-25 16:46:20 +0000185 fBM.allocN32Pixels(1, 1);
humper@google.com9c96d4b2013-07-14 01:44:59 +0000186 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
187 }
188 fSize = fBM.height();
189 }
190 private:
191 typedef DownsampleBitmapGM INHERITED;
192};
193
reedc695e952015-01-16 05:01:16 -0800194#include "SkMipMap.h"
halcanarya1cb3782015-02-24 09:25:09 -0800195
196static void release_mipmap(void*, void* context) {
197 ((SkMipMap*)context)->unref();
198}
199
reedc695e952015-01-16 05:01:16 -0800200class ShowMipLevels : public skiagm::GM {
201public:
202 SkBitmap fBM;
203
204 ShowMipLevels() {
caryclark65cdba62015-06-15 06:51:08 -0700205 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
reedc695e952015-01-16 05:01:16 -0800206 make_checker(&fBM, 512, 256);
207 }
208
209protected:
reedc695e952015-01-16 05:01:16 -0800210
mtklein36352bf2015-03-25 18:17:31 -0700211 SkString onShortName() override {
reedc695e952015-01-16 05:01:16 -0800212 return SkString("showmiplevels");
213 }
214
mtklein36352bf2015-03-25 18:17:31 -0700215 SkISize onISize() override {
reedc695e952015-01-16 05:01:16 -0800216 return SkISize::Make(fBM.width() + 8, 2 * fBM.height() + 80);
217 }
218
mtklein36352bf2015-03-25 18:17:31 -0700219 void onDraw(SkCanvas* canvas) override {
reedc695e952015-01-16 05:01:16 -0800220 SkScalar x = 4;
221 SkScalar y = 4;
222 canvas->drawBitmap(fBM, x, y, NULL);
223 y += fBM.height() + 4;
224
225 SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(fBM, NULL));
226
227 SkMipMap::Level level;
228 SkScalar scale = 0.5f;
229 while (mm->extractLevel(scale, &level)) {
230 SkImageInfo info = SkImageInfo::MakeN32Premul(level.fWidth, level.fHeight);
231 SkBitmap bm;
halcanarya1cb3782015-02-24 09:25:09 -0800232 bm.installPixels(info, level.fPixels, level.fRowBytes, NULL,
233 &release_mipmap, (void*)(SkRef(mm.get())));
234 bm.setImmutable();
reedc695e952015-01-16 05:01:16 -0800235 canvas->drawBitmap(bm, x, y, NULL);
236 y += bm.height() + 4;
237 scale /= 2;
238 if (info.width() == 1 || info.height() == 1) {
239 break;
240 }
241 }
242 }
mtklein1c402922015-01-23 11:07:07 -0800243
reedc695e952015-01-16 05:01:16 -0800244private:
245 typedef skiagm::GM INHERITED;
246};
247DEF_GM( return new ShowMipLevels; )
248
humper@google.com9c96d4b2013-07-14 01:44:59 +0000249//////////////////////////////////////////////////////////////////////////////
250
reed93a12152015-03-16 10:08:34 -0700251DEF_GM( return new DownsampleBitmapTextGM(72, kHigh_SkFilterQuality); )
252DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, kHigh_SkFilterQuality); )
253DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", kHigh_SkFilterQuality); )
bsalomonefd7d452014-10-23 14:17:46 -0700254DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc",
reed93a12152015-03-16 10:08:34 -0700255 kHigh_SkFilterQuality); )
humper@google.comaf42a032013-07-25 14:34:13 +0000256
reed93a12152015-03-16 10:08:34 -0700257DEF_GM( return new DownsampleBitmapTextGM(72, kMedium_SkFilterQuality); )
258DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, kMedium_SkFilterQuality); )
259DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", kMedium_SkFilterQuality); )
bsalomonefd7d452014-10-23 14:17:46 -0700260DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc",
reed93a12152015-03-16 10:08:34 -0700261 kMedium_SkFilterQuality); )
humper@google.comaf42a032013-07-25 14:34:13 +0000262
reed93a12152015-03-16 10:08:34 -0700263DEF_GM( return new DownsampleBitmapTextGM(72, kLow_SkFilterQuality); )
264DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, kLow_SkFilterQuality); )
265DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", kLow_SkFilterQuality); )
bsalomonefd7d452014-10-23 14:17:46 -0700266DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc",
reed93a12152015-03-16 10:08:34 -0700267 kLow_SkFilterQuality); )
humper@google.comaf42a032013-07-25 14:34:13 +0000268
reed93a12152015-03-16 10:08:34 -0700269DEF_GM( return new DownsampleBitmapTextGM(72, kNone_SkFilterQuality); )
270DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, kNone_SkFilterQuality); )
271DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", kNone_SkFilterQuality); )
bsalomonefd7d452014-10-23 14:17:46 -0700272DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc",
reed93a12152015-03-16 10:08:34 -0700273 kNone_SkFilterQuality); )