blob: 994ab9412b77900acb96e88b02957e392c94a192 [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
17static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style style) {
Cary Clark992c7b02014-07-31 08:58:44 -040018 sk_tool_utils::set_portable_typeface(paint, name, style);
humper@google.com9c96d4b2013-07-14 01:44:59 +000019}
20
21class DownsampleBitmapGM : public skiagm::GM {
humper@google.com9c96d4b2013-07-14 01:44:59 +000022public:
23 SkBitmap fBM;
24 SkString fName;
25 bool fBitmapMade;
humper@google.comaf42a032013-07-25 14:34:13 +000026 SkPaint::FilterLevel fFilterLevel;
humper@google.com9c96d4b2013-07-14 01:44:59 +000027
humper@google.comaf42a032013-07-25 14:34:13 +000028 DownsampleBitmapGM(SkPaint::FilterLevel filterLevel)
29 : fFilterLevel(filterLevel)
humper@google.com9c96d4b2013-07-14 01:44:59 +000030 {
31 this->setBGColor(0xFFDDDDDD);
32 fBitmapMade = false;
33 }
34
humper@google.come2cf8052013-07-25 19:14:37 +000035 const char* filterLevelToString() {
36 static const char *filterLevelNames[] = {
37 "none", "low", "medium", "high"
38 };
39 return filterLevelNames[fFilterLevel];
humper@google.comaf42a032013-07-25 14:34:13 +000040 }
humper@google.com9c96d4b2013-07-14 01:44:59 +000041
42protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000043 virtual uint32_t onGetFlags() const SK_OVERRIDE {
humperd73c1692014-08-28 14:27:42 -070044 return kSkipTiled_Flag;
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000045 }
46
humper@google.com9c96d4b2013-07-14 01:44:59 +000047 virtual SkString onShortName() SK_OVERRIDE {
48 return fName;
49 }
50
51 virtual SkISize onISize() SK_OVERRIDE {
52 make_bitmap_wrapper();
mtklein6a2f5fe2014-09-03 13:40:52 -070053 return SkISize::Make(fBM.width(), 4 * fBM.height());
humper@google.com9c96d4b2013-07-14 01:44:59 +000054 }
55
56 void make_bitmap_wrapper() {
57 if (!fBitmapMade) {
58 fBitmapMade = true;
59 make_bitmap();
60 }
61 }
62
63 virtual void make_bitmap() = 0;
64
65 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
66 make_bitmap_wrapper();
67
mtklein6a2f5fe2014-09-03 13:40:52 -070068 int curY = 0;
69 int curHeight;
humper@google.com9c96d4b2013-07-14 01:44:59 +000070 float curScale = 1;
71 do {
72
73 SkMatrix matrix;
74 matrix.setScale( curScale, curScale );
75
76 SkPaint paint;
humper@google.comaf42a032013-07-25 14:34:13 +000077 paint.setFilterLevel(fFilterLevel);
humper@google.com9c96d4b2013-07-14 01:44:59 +000078
79 canvas->save();
mtklein6a2f5fe2014-09-03 13:40:52 -070080 canvas->translate(0, (SkScalar)curY);
Florin Malitac54d8db2014-12-10 12:02:16 -050081 canvas->concat(matrix);
82 canvas->drawBitmap(fBM, 0, 0, &paint);
humper@google.com9c96d4b2013-07-14 01:44:59 +000083 canvas->restore();
84
mtklein6a2f5fe2014-09-03 13:40:52 -070085 curHeight = (int) (fBM.height() * curScale + 2);
86 curY += curHeight;
humper@google.com9c96d4b2013-07-14 01:44:59 +000087 curScale *= 0.75f;
mtklein6a2f5fe2014-09-03 13:40:52 -070088 } while (curHeight >= 2 && curY < 4*fBM.height());
humper@google.com9c96d4b2013-07-14 01:44:59 +000089 }
90
91private:
92 typedef skiagm::GM INHERITED;
93};
94
95class DownsampleBitmapTextGM: public DownsampleBitmapGM {
96 public:
humper@google.comaf42a032013-07-25 14:34:13 +000097 DownsampleBitmapTextGM(float textSize, SkPaint::FilterLevel filterLevel)
98 : INHERITED(filterLevel), fTextSize(textSize)
humper@google.com9c96d4b2013-07-14 01:44:59 +000099 {
humper@google.come2cf8052013-07-25 19:14:37 +0000100 fName.printf("downsamplebitmap_text_%s_%.2fpt", this->filterLevelToString(), fTextSize);
humper@google.com9c96d4b2013-07-14 01:44:59 +0000101 }
102
103 protected:
104 float fTextSize;
105
106 virtual void make_bitmap() SK_OVERRIDE {
reed@google.comeb9a46c2014-01-25 16:46:20 +0000107 fBM.allocN32Pixels(int(fTextSize * 8), int(fTextSize * 6));
humper@google.com9c96d4b2013-07-14 01:44:59 +0000108 SkCanvas canvas(fBM);
109 canvas.drawColor(SK_ColorWHITE);
110
111 SkPaint paint;
112 paint.setAntiAlias(true);
113 paint.setSubpixelText(true);
114 paint.setTextSize(fTextSize);
115
116 setTypeface(&paint, "Times", SkTypeface::kNormal);
117 canvas.drawText("Hamburgefons", 12, fTextSize/2, 1.2f*fTextSize, paint);
118 setTypeface(&paint, "Times", SkTypeface::kBold);
119 canvas.drawText("Hamburgefons", 12, fTextSize/2, 2.4f*fTextSize, paint);
120 setTypeface(&paint, "Times", SkTypeface::kItalic);
121 canvas.drawText("Hamburgefons", 12, fTextSize/2, 3.6f*fTextSize, paint);
122 setTypeface(&paint, "Times", SkTypeface::kBoldItalic);
123 canvas.drawText("Hamburgefons", 12, fTextSize/2, 4.8f*fTextSize, paint);
124 }
125 private:
126 typedef DownsampleBitmapGM INHERITED;
127};
128
129class DownsampleBitmapCheckerboardGM: public DownsampleBitmapGM {
130 public:
humper@google.comaf42a032013-07-25 14:34:13 +0000131 DownsampleBitmapCheckerboardGM(int size, int numChecks, SkPaint::FilterLevel filterLevel)
132 : INHERITED(filterLevel), fSize(size), fNumChecks(numChecks)
humper@google.com9c96d4b2013-07-14 01:44:59 +0000133 {
humper@google.come2cf8052013-07-25 19:14:37 +0000134 fName.printf("downsamplebitmap_checkerboard_%s_%d_%d", this->filterLevelToString(), fSize, fNumChecks);
humper@google.com9c96d4b2013-07-14 01:44:59 +0000135 }
136
137 protected:
138 int fSize;
139 int fNumChecks;
140
141 virtual void make_bitmap() SK_OVERRIDE {
reed@google.comeb9a46c2014-01-25 16:46:20 +0000142 fBM.allocN32Pixels(fSize, fSize);
humper@google.com9c96d4b2013-07-14 01:44:59 +0000143 for (int y = 0; y < fSize; ++y) {
144 for (int x = 0; x < fSize; ++x) {
145 SkPMColor* s = fBM.getAddr32(x, y);
146 int cx = (x * fNumChecks) / fSize;
147 int cy = (y * fNumChecks) / fSize;
148 if ((cx+cy)%2) {
149 *s = 0xFFFFFFFF;
150 } else {
151 *s = 0xFF000000;
152 }
153 }
154 }
155 }
156 private:
157 typedef DownsampleBitmapGM INHERITED;
158};
159
160class DownsampleBitmapImageGM: public DownsampleBitmapGM {
161 public:
humper@google.comaf42a032013-07-25 14:34:13 +0000162 DownsampleBitmapImageGM(const char filename[], SkPaint::FilterLevel filterLevel)
163 : INHERITED(filterLevel), fFilename(filename)
humper@google.com9c96d4b2013-07-14 01:44:59 +0000164 {
humper@google.come2cf8052013-07-25 19:14:37 +0000165 fName.printf("downsamplebitmap_image_%s_%s", this->filterLevelToString(), filename);
humper@google.com9c96d4b2013-07-14 01:44:59 +0000166 }
167
168 protected:
169 SkString fFilename;
170 int fSize;
171
172 virtual void make_bitmap() SK_OVERRIDE {
tfarinabcbc1782014-06-18 14:32:48 -0700173 SkImageDecoder* codec = NULL;
tfarinac846f4a2014-07-01 12:35:49 -0700174 SkString resourcePath = GetResourcePath(fFilename.c_str());
tfarinabcbc1782014-06-18 14:32:48 -0700175 SkFILEStream stream(resourcePath.c_str());
humper@google.com9c96d4b2013-07-14 01:44:59 +0000176 if (stream.isValid()) {
177 codec = SkImageDecoder::Factory(&stream);
178 }
179 if (codec) {
180 stream.rewind();
reedbfefc7c2014-06-12 17:40:00 -0700181 codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode);
humper@google.com9c96d4b2013-07-14 01:44:59 +0000182 SkDELETE(codec);
183 } else {
reed@google.comeb9a46c2014-01-25 16:46:20 +0000184 fBM.allocN32Pixels(1, 1);
humper@google.com9c96d4b2013-07-14 01:44:59 +0000185 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
186 }
187 fSize = fBM.height();
188 }
189 private:
190 typedef DownsampleBitmapGM INHERITED;
191};
192
193//////////////////////////////////////////////////////////////////////////////
194
humper@google.comaf42a032013-07-25 14:34:13 +0000195DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kHigh_FilterLevel); )
196DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kHigh_FilterLevel); )
197DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kHigh_FilterLevel); )
bsalomonefd7d452014-10-23 14:17:46 -0700198DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc",
199 SkPaint::kHigh_FilterLevel); )
humper@google.comaf42a032013-07-25 14:34:13 +0000200
201DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kMedium_FilterLevel); )
202DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kMedium_FilterLevel); )
203DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kMedium_FilterLevel); )
bsalomonefd7d452014-10-23 14:17:46 -0700204DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc",
205 SkPaint::kMedium_FilterLevel); )
humper@google.comaf42a032013-07-25 14:34:13 +0000206
207DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kLow_FilterLevel); )
208DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kLow_FilterLevel); )
209DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kLow_FilterLevel); )
bsalomonefd7d452014-10-23 14:17:46 -0700210DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc",
211 SkPaint::kLow_FilterLevel); )
humper@google.comaf42a032013-07-25 14:34:13 +0000212
213DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kNone_FilterLevel); )
214DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kNone_FilterLevel); )
215DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kNone_FilterLevel); )
bsalomonefd7d452014-10-23 14:17:46 -0700216DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc",
217 SkPaint::kNone_FilterLevel); )