Add fixes & test for isConfigTexturable and isConfigRenderable
[platform/upstream/libSkiaSharp.git] / gm / bitmapfilters.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 static void make_bm(SkBitmap* bm) {
12     const SkColor colors[4] = {
13         SK_ColorRED, SK_ColorGREEN,
14         SK_ColorBLUE, SK_ColorWHITE
15     };
16     SkPMColor colorsPM[4];
17     for (size_t i = 0; i < SK_ARRAY_COUNT(colors); ++i) {
18         colorsPM[i] = SkPreMultiplyColor(colors[i]);
19     }
20     bm->allocPixels(SkImageInfo::Make(2, 2, kIndex_8_SkColorType,
21                                       kOpaque_SkAlphaType),
22                     SkColorTable::Make(colorsPM, 4));
23
24     *bm->getAddr8(0, 0) = 0;
25     *bm->getAddr8(1, 0) = 1;
26     *bm->getAddr8(0, 1) = 2;
27     *bm->getAddr8(1, 1) = 3;
28 }
29
30 static SkScalar draw_bm(SkCanvas* canvas, const SkBitmap& bm,
31                         SkScalar x, SkScalar y, SkPaint* paint) {
32     canvas->drawBitmap(bm, x, y, paint);
33     return SkIntToScalar(bm.width()) * 5/4;
34 }
35
36 static SkScalar draw_set(SkCanvas* c, const SkBitmap& bm, SkScalar x,
37                          SkPaint* p) {
38     x += draw_bm(c, bm, x, 0, p);
39     p->setFilterQuality(kLow_SkFilterQuality);
40     x += draw_bm(c, bm, x, 0, p);
41     p->setDither(true);
42     return x + draw_bm(c, bm, x, 0, p);
43 }
44
45 static SkScalar draw_row(SkCanvas* canvas, const SkBitmap& bm) {
46     SkAutoCanvasRestore acr(canvas, true);
47
48     SkPaint paint;
49     SkScalar x = 0;
50     const int scale = 32;
51
52     paint.setAntiAlias(true);
53     sk_tool_utils::set_portable_typeface(&paint);
54     const char* name = sk_tool_utils::colortype_name(bm.colorType());
55     canvas->drawString(name, x, SkIntToScalar(bm.height())*scale*5/8,
56                      paint);
57     canvas->translate(SkIntToScalar(48), 0);
58
59     canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale));
60
61     x += draw_set(canvas, bm, 0, &paint);
62     paint.reset();
63     paint.setAlpha(0x80);
64     draw_set(canvas, bm, x, &paint);
65     return x * scale / 3;
66 }
67
68 class FilterGM : public skiagm::GM {
69     void onOnceBeforeDraw() override {
70         make_bm(&fBM8);
71         sk_tool_utils::copy_to(&fBM4444, kARGB_4444_SkColorType, fBM8);
72         sk_tool_utils::copy_to(&fBM16, kRGB_565_SkColorType, fBM8);
73         sk_tool_utils::copy_to(&fBM32, kN32_SkColorType, fBM8);
74     }
75
76 public:
77     SkBitmap    fBM8, fBM4444, fBM16, fBM32;
78
79     FilterGM() {
80         this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
81     }
82
83 protected:
84     SkString onShortName() override {
85         return SkString("bitmapfilters");
86     }
87
88     SkISize onISize() override {
89         return SkISize::Make(540, 330);
90     }
91
92     void onDraw(SkCanvas* canvas) override {
93         SkScalar x = SkIntToScalar(10);
94         SkScalar y = SkIntToScalar(10);
95
96         canvas->translate(x, y);
97         y = draw_row(canvas, fBM8);
98         canvas->translate(0, y);
99         y = draw_row(canvas, fBM4444);
100         canvas->translate(0, y);
101         y = draw_row(canvas, fBM16);
102         canvas->translate(0, y);
103         draw_row(canvas, fBM32);
104     }
105
106 private:
107     typedef skiagm::GM INHERITED;
108 };
109 DEF_GM( return new FilterGM; )
110
111 //////////////////////////////////////////////////////////////////////////////
112
113 class TestExtractAlphaGM : public skiagm::GM {
114     void onOnceBeforeDraw() override {
115         // Make a bitmap with per-pixels alpha (stroked circle)
116         fBitmap.allocN32Pixels(100, 100);
117         SkCanvas canvas(fBitmap);
118         canvas.clear(0);
119
120         SkPaint paint;
121         paint.setAntiAlias(true);
122         paint.setColor(SK_ColorBLUE);
123         paint.setStyle(SkPaint::kStroke_Style);
124         paint.setStrokeWidth(20);
125
126         canvas.drawCircle(50, 50, 39, paint);
127         canvas.flush();
128
129         fBitmap.extractAlpha(&fAlpha);
130     }
131
132 public:
133     SkBitmap fBitmap, fAlpha;
134
135 protected:
136     SkString onShortName() override {
137         return SkString("extractalpha");
138     }
139
140     SkISize onISize() override {
141         return SkISize::Make(540, 330);
142     }
143
144     void onDraw(SkCanvas* canvas) override {
145         SkPaint paint;
146         paint.setAntiAlias(true);
147         paint.setFilterQuality(kLow_SkFilterQuality);
148         paint.setColor(SK_ColorRED);
149
150         canvas->drawBitmap(fBitmap, 10, 10, &paint);    // should stay blue (ignore paint's color)
151         canvas->drawBitmap(fAlpha, 120, 10, &paint);    // should draw red
152     }
153
154 private:
155     typedef skiagm::GM INHERITED;
156 };
157 DEF_GM( return new TestExtractAlphaGM; )