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