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