1 /*
2 * Copyright 2011 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"
9 #include "sk_tool_utils.h"
10
11 #include "Resources.h"
12 #include "SkGradientShader.h"
13 #include "SkStream.h"
14 #include "SkTypeface.h"
15
setTypeface(SkFont * font,const char name[],SkFontStyle style)16 static void setTypeface(SkFont* font, const char name[], SkFontStyle style) {
17 font->setTypeface(sk_tool_utils::create_portable_typeface(name, style));
18 }
19
computeSize(const SkBitmap & bm,const SkMatrix & mat)20 static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) {
21 SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()),
22 SkIntToScalar(bm.height()));
23 mat.mapRect(&bounds);
24 return SkSize::Make(bounds.width(), bounds.height());
25 }
26
draw_cell(SkCanvas * canvas,const SkBitmap & bm,const SkMatrix & mat,SkScalar dx,SkFilterQuality lvl)27 static void draw_cell(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx,
28 SkFilterQuality lvl) {
29 SkPaint paint;
30 paint.setFilterQuality(lvl);
31
32 SkAutoCanvasRestore acr(canvas, true);
33 canvas->translate(dx, 0);
34 canvas->concat(mat);
35 canvas->drawBitmap(bm, 0, 0, &paint);
36 }
37
draw_row(SkCanvas * canvas,const SkBitmap & bm,const SkMatrix & mat,SkScalar dx)38 static void draw_row(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx) {
39 draw_cell(canvas, bm, mat, 0 * dx, kNone_SkFilterQuality);
40 draw_cell(canvas, bm, mat, 1 * dx, kLow_SkFilterQuality);
41 draw_cell(canvas, bm, mat, 2 * dx, kMedium_SkFilterQuality);
42 draw_cell(canvas, bm, mat, 3 * dx, kHigh_SkFilterQuality);
43 }
44
45 class FilterBitmapGM : public skiagm::GM {
onOnceBeforeDraw()46 void onOnceBeforeDraw() override {
47
48 this->makeBitmap();
49
50 SkScalar cx = SkScalarHalf(fBM.width());
51 SkScalar cy = SkScalarHalf(fBM.height());
52 SkScalar scale = this->getScale();
53
54 // these two matrices use a scale factor configured by the subclass
55 fMatrix[0].setScale(scale, scale);
56 fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(scale, scale);
57
58 // up/down scaling mix
59 fMatrix[2].setScale(0.7f, 1.05f);
60 }
61
62 public:
63 SkBitmap fBM;
64 SkMatrix fMatrix[3];
65 SkString fName;
66
FilterBitmapGM()67 FilterBitmapGM()
68 {
69 this->setBGColor(0xFFDDDDDD);
70 }
71
72 protected:
73
onShortName()74 SkString onShortName() override {
75 return fName;
76 }
77
onISize()78 SkISize onISize() override {
79 return SkISize::Make(1024, 768);
80 }
81
82 virtual void makeBitmap() = 0;
83 virtual SkScalar getScale() = 0;
84
onDraw(SkCanvas * canvas)85 void onDraw(SkCanvas* canvas) override {
86
87 canvas->translate(10, 10);
88 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) {
89 SkSize size = computeSize(fBM, fMatrix[i]);
90 size.fWidth += 20;
91 size.fHeight += 20;
92
93 draw_row(canvas, fBM, fMatrix[i], size.fWidth);
94 canvas->translate(0, size.fHeight);
95 }
96 }
97
98 private:
99 typedef skiagm::GM INHERITED;
100 };
101
102 class FilterBitmapTextGM: public FilterBitmapGM {
103 public:
FilterBitmapTextGM(float textSize)104 FilterBitmapTextGM(float textSize)
105 : fTextSize(textSize)
106 {
107 fName.printf("filterbitmap_text_%.2fpt", fTextSize);
108 }
109
110 protected:
111 float fTextSize;
112
getScale()113 SkScalar getScale() override {
114 return 32.f/fTextSize;
115 }
116
makeBitmap()117 void makeBitmap() override {
118 fBM.allocN32Pixels(int(fTextSize * 8), int(fTextSize * 6));
119 SkCanvas canvas(fBM);
120 canvas.drawColor(SK_ColorWHITE);
121
122 SkPaint paint;
123 SkFont font;
124 font.setSize(fTextSize);
125 font.setSubpixel(true);
126
127 setTypeface(&font, "serif", SkFontStyle::Normal());
128 canvas.drawString("Hamburgefons", fTextSize/2, 1.2f*fTextSize, font, paint);
129 setTypeface(&font, "serif", SkFontStyle::Bold());
130 canvas.drawString("Hamburgefons", fTextSize/2, 2.4f*fTextSize, font, paint);
131 setTypeface(&font, "serif", SkFontStyle::Italic());
132 canvas.drawString("Hamburgefons", fTextSize/2, 3.6f*fTextSize, font, paint);
133 setTypeface(&font, "serif", SkFontStyle::BoldItalic());
134 canvas.drawString("Hamburgefons", fTextSize/2, 4.8f*fTextSize, font, paint);
135 }
136 private:
137 typedef FilterBitmapGM INHERITED;
138 };
139
140 class FilterBitmapCheckerboardGM: public FilterBitmapGM {
141 public:
FilterBitmapCheckerboardGM(int size,int num_checks,bool convertToG8=false)142 FilterBitmapCheckerboardGM(int size, int num_checks, bool convertToG8 = false)
143 : fSize(size), fNumChecks(num_checks), fConvertToG8(convertToG8)
144 {
145 fName.printf("filterbitmap_checkerboard_%d_%d%s",
146 fSize, fNumChecks, convertToG8 ? "_g8" : "");
147 }
148
149 protected:
150 int fSize;
151 int fNumChecks;
152
getScale()153 SkScalar getScale() override {
154 return 192.f/fSize;
155 }
156
makeBitmap()157 void makeBitmap() override {
158 fBM.allocN32Pixels(fSize, fSize);
159 for (int y = 0; y < fSize; y ++) {
160 for (int x = 0; x < fSize; x ++) {
161 SkPMColor* s = fBM.getAddr32(x, y);
162 int cx = (x * fNumChecks) / fSize;
163 int cy = (y * fNumChecks) / fSize;
164 if ((cx+cy)%2) {
165 *s = 0xFFFFFFFF;
166 } else {
167 *s = 0xFF000000;
168 }
169 }
170 }
171 if (fConvertToG8) {
172 SkBitmap tmp;
173 sk_tool_utils::copy_to_g8(&tmp, fBM);
174 fBM = tmp;
175 }
176 }
177 private:
178 const bool fConvertToG8;
179 typedef FilterBitmapGM INHERITED;
180 };
181
182 class FilterBitmapImageGM: public FilterBitmapGM {
183 public:
FilterBitmapImageGM(const char filename[],bool convertToG8=false)184 FilterBitmapImageGM(const char filename[], bool convertToG8 = false)
185 : fFilename(filename), fConvertToG8(convertToG8)
186 {
187 fName.printf("filterbitmap_image_%s%s", filename, convertToG8 ? "_g8" : "");
188 }
189
190 protected:
191 SkString fFilename;
192 int fSize;
193
getScale()194 SkScalar getScale() override {
195 return 192.f/fSize;
196 }
197
makeBitmap()198 void makeBitmap() override {
199 SkString resource = SkStringPrintf("images/%s", fFilename.c_str());
200 if (!GetResourceAsBitmap(resource.c_str(), &fBM)) {
201 fBM.allocN32Pixels(1, 1);
202 fBM.eraseARGB(255, 255, 0 , 0); // red == bad
203 }
204 fSize = fBM.height();
205
206 if (fConvertToG8) {
207 SkBitmap tmp;
208 sk_tool_utils::copy_to_g8(&tmp, fBM);
209 fBM = tmp;
210 }
211 }
212 private:
213 const bool fConvertToG8;
214 typedef FilterBitmapGM INHERITED;
215 };
216
217 //////////////////////////////////////////////////////////////////////////////
218
219 DEF_GM( return new FilterBitmapTextGM(3); )
220 DEF_GM( return new FilterBitmapTextGM(7); )
221 DEF_GM( return new FilterBitmapTextGM(10); )
222 DEF_GM( return new FilterBitmapCheckerboardGM(4,4); )
223 DEF_GM( return new FilterBitmapCheckerboardGM(32,32); )
224 DEF_GM( return new FilterBitmapCheckerboardGM(32,32, true); )
225 DEF_GM( return new FilterBitmapCheckerboardGM(32,8); )
226 DEF_GM( return new FilterBitmapCheckerboardGM(32,2); )
227 DEF_GM( return new FilterBitmapCheckerboardGM(192,192); )
228 DEF_GM( return new FilterBitmapImageGM("mandrill_16.png"); )
229 DEF_GM( return new FilterBitmapImageGM("mandrill_32.png"); )
230 DEF_GM( return new FilterBitmapImageGM("mandrill_64.png"); )
231 DEF_GM( return new FilterBitmapImageGM("mandrill_64.png", true); )
232 DEF_GM( return new FilterBitmapImageGM("mandrill_128.png"); )
233 DEF_GM( return new FilterBitmapImageGM("mandrill_256.png"); )
234 DEF_GM( return new FilterBitmapImageGM("mandrill_512.png"); )
235 DEF_GM( return new FilterBitmapImageGM("color_wheel.png"); )
236