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
10#include "Resources.h"
11#include "SkGradientShader.h"
12#include "SkImageDecoder.h"
13#include "SkStream.h"
14#include "SkTypeface.h"
15
16static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style style) {
17    sk_tool_utils::set_portable_typeface(paint, name, style);
18}
19
20static 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
27static 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
38static 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
45class FilterBitmapGM : public skiagm::GM {
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
62public:
63    SkBitmap    fBM;
64    SkMatrix    fMatrix[3];
65    SkString    fName;
66
67    FilterBitmapGM()
68    {
69        this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
70    }
71
72protected:
73
74    SkString onShortName() override {
75        return fName;
76    }
77
78    SkISize onISize() override {
79        return SkISize::Make(1024, 768);
80    }
81
82    virtual void makeBitmap() = 0;
83    virtual SkScalar getScale() = 0;
84
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
98private:
99    typedef skiagm::GM INHERITED;
100};
101
102class FilterBitmapTextGM: public FilterBitmapGM {
103  public:
104      FilterBitmapTextGM(float textSize)
105      : fTextSize(textSize)
106        {
107            fName.printf("filterbitmap_text_%.2fpt", fTextSize);
108        }
109
110  protected:
111      float fTextSize;
112
113      SkScalar getScale() override {
114          return 32.f/fTextSize;
115      }
116
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          paint.setAntiAlias(true);
124          paint.setSubpixelText(true);
125          paint.setTextSize(fTextSize);
126
127          setTypeface(&paint, "serif", SkTypeface::kNormal);
128          canvas.drawText("Hamburgefons", 12, fTextSize/2, 1.2f*fTextSize, paint);
129          setTypeface(&paint, "serif", SkTypeface::kBold);
130          canvas.drawText("Hamburgefons", 12, fTextSize/2, 2.4f*fTextSize, paint);
131          setTypeface(&paint, "serif", SkTypeface::kItalic);
132          canvas.drawText("Hamburgefons", 12, fTextSize/2, 3.6f*fTextSize, paint);
133          setTypeface(&paint, "serif", SkTypeface::kBoldItalic);
134          canvas.drawText("Hamburgefons", 12, fTextSize/2, 4.8f*fTextSize, paint);
135      }
136  private:
137      typedef FilterBitmapGM INHERITED;
138};
139
140class FilterBitmapCheckerboardGM: public FilterBitmapGM {
141public:
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
153      SkScalar getScale() override {
154          return 192.f/fSize;
155      }
156
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              fBM.copyTo(&tmp, kGray_8_SkColorType);
174              fBM = tmp;
175          }
176      }
177private:
178    const bool fConvertToG8;
179    typedef FilterBitmapGM INHERITED;
180};
181
182class FilterBitmapImageGM: public FilterBitmapGM {
183public:
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
190protected:
191      SkString fFilename;
192      int fSize;
193
194      SkScalar getScale() override {
195          return 192.f/fSize;
196      }
197
198      void makeBitmap() override {
199          SkImageDecoder* codec = nullptr;
200          SkString resourcePath = GetResourcePath(fFilename.c_str());
201          SkFILEStream stream(resourcePath.c_str());
202          if (stream.isValid()) {
203              codec = SkImageDecoder::Factory(&stream);
204          }
205          if (codec) {
206              stream.rewind();
207              codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode);
208              delete codec;
209          } else {
210              fBM.allocN32Pixels(1, 1);
211              *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
212          }
213          fSize = fBM.height();
214          if (fConvertToG8) {
215              SkBitmap tmp;
216              fBM.copyTo(&tmp, kGray_8_SkColorType);
217              fBM = tmp;
218          }
219      }
220private:
221    const bool fConvertToG8;
222    typedef FilterBitmapGM INHERITED;
223};
224
225//////////////////////////////////////////////////////////////////////////////
226
227DEF_GM( return new FilterBitmapTextGM(3); )
228DEF_GM( return new FilterBitmapTextGM(7); )
229DEF_GM( return new FilterBitmapTextGM(10); )
230DEF_GM( return new FilterBitmapCheckerboardGM(4,4); )
231DEF_GM( return new FilterBitmapCheckerboardGM(32,32); )
232DEF_GM( return new FilterBitmapCheckerboardGM(32,32, true); )
233DEF_GM( return new FilterBitmapCheckerboardGM(32,8); )
234DEF_GM( return new FilterBitmapCheckerboardGM(32,2); )
235DEF_GM( return new FilterBitmapCheckerboardGM(192,192); )
236DEF_GM( return new FilterBitmapImageGM("mandrill_16.png"); )
237DEF_GM( return new FilterBitmapImageGM("mandrill_32.png"); )
238DEF_GM( return new FilterBitmapImageGM("mandrill_64.png"); )
239DEF_GM( return new FilterBitmapImageGM("mandrill_64.png", true); )
240DEF_GM( return new FilterBitmapImageGM("mandrill_128.png"); )
241DEF_GM( return new FilterBitmapImageGM("mandrill_256.png"); )
242DEF_GM( return new FilterBitmapImageGM("mandrill_512.png"); )
243DEF_GM( return new FilterBitmapImageGM("color_wheel.png"); )
244