Add fixes & test for isConfigTexturable and isConfigRenderable
[platform/upstream/libSkiaSharp.git] / gm / filterindiabox.cpp
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 "SkBitmapProcState.h"
13 #include "SkBitmapScaler.h"
14 #include "SkGradientShader.h"
15 #include "SkImageEncoder.h"
16 #include "SkStream.h"
17 #include "SkTypeface.h"
18
19 static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) {
20     SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()),
21                                    SkIntToScalar(bm.height()));
22     mat.mapRect(&bounds);
23     return SkSize::Make(bounds.width(), bounds.height());
24 }
25
26 static void draw_cell(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx,
27                       SkFilterQuality lvl) {
28     SkPaint paint;
29     paint.setFilterQuality(lvl);
30
31     SkAutoCanvasRestore acr(canvas, true);
32
33     canvas->translate(dx, 0);
34     canvas->concat(mat);
35     canvas->drawBitmap(bm, 0, 0, &paint);
36 }
37
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 FilterIndiaBoxGM : public skiagm::GM {
46     void onOnceBeforeDraw() override {
47         this->makeBitmap();
48
49         SkScalar cx = SkScalarHalf(fBM.width());
50         SkScalar cy = SkScalarHalf(fBM.height());
51
52         float vertScale = 30.0f/55.0f;
53         float horizScale = 150.0f/200.0f;
54
55         fMatrix[0].setScale(horizScale, vertScale);
56         fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(horizScale, vertScale);
57     }
58
59 public:
60     SkBitmap    fBM;
61     SkMatrix    fMatrix[2];
62     SkString    fName;
63
64     FilterIndiaBoxGM() {
65         this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
66     }
67
68     FilterIndiaBoxGM(const char filename[]) : fFilename(filename) {
69         fName.printf("filterindiabox");
70     }
71
72 protected:
73     SkString onShortName() override {
74         return fName;
75     }
76
77     SkISize onISize() override {
78         return SkISize::Make(1024, 768);
79     }
80
81     void onDraw(SkCanvas* canvas) override {
82         canvas->translate(10, 10);
83         for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) {
84             SkSize size = computeSize(fBM, fMatrix[i]);
85             size.fWidth += 20;
86             size.fHeight += 20;
87
88             draw_row(canvas, fBM, fMatrix[i], size.fWidth);
89             canvas->translate(0, size.fHeight);
90         }
91     }
92
93   protected:
94       SkString fFilename;
95       int fSize;
96
97       SkScalar getScale() {
98           return 192.f/fSize;
99       }
100
101       void makeBitmap() {
102         if (!GetResourceAsBitmap(fFilename.c_str(), &fBM)) {
103             fBM.allocN32Pixels(1, 1);
104             fBM.eraseARGB(255, 255, 0 , 0); // red == bad
105         }
106         fSize = fBM.height();
107       }
108   private:
109     typedef skiagm::GM INHERITED;
110 };
111
112 //////////////////////////////////////////////////////////////////////////////
113
114
115 DEF_GM( return new FilterIndiaBoxGM("box.gif"); )