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 "SkCanvas.h"
10#include "SkColorPriv.h"
11#include "SkShader.h"
12
13#include "SkArithmeticMode.h"
14#include "SkGradientShader.h"
15#define WW  100
16#define HH  32
17
18static SkBitmap make_bm() {
19    SkBitmap bm;
20    bm.allocN32Pixels(WW, HH);
21    bm.eraseColor(SK_ColorTRANSPARENT);
22    return bm;
23}
24
25static SkBitmap make_src() {
26    SkBitmap bm = make_bm();
27    SkCanvas canvas(bm);
28    SkPaint paint;
29    SkPoint pts[] = { {0, 0}, {SkIntToScalar(WW), SkIntToScalar(HH)} };
30    SkColor colors[] = {
31        SK_ColorTRANSPARENT, SK_ColorGREEN, SK_ColorCYAN,
32        SK_ColorRED, SK_ColorMAGENTA, SK_ColorWHITE,
33    };
34    SkShader* s = SkGradientShader::CreateLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
35                                                 SkShader::kClamp_TileMode);
36    paint.setShader(s)->unref();
37    canvas.drawPaint(paint);
38    return bm;
39}
40
41static SkBitmap make_dst() {
42    SkBitmap bm = make_bm();
43    SkCanvas canvas(bm);
44    SkPaint paint;
45    SkPoint pts[] = { {0, SkIntToScalar(HH)}, {SkIntToScalar(WW), 0} };
46    SkColor colors[] = {
47        SK_ColorBLUE, SK_ColorYELLOW, SK_ColorBLACK, SK_ColorGREEN,
48        sk_tool_utils::color_to_565(SK_ColorGRAY)
49    };
50    SkShader* s = SkGradientShader::CreateLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
51                                                 SkShader::kClamp_TileMode);
52    paint.setShader(s)->unref();
53    canvas.drawPaint(paint);
54    return bm;
55}
56
57static void show_k_text(SkCanvas* canvas, SkScalar x, SkScalar y, const SkScalar k[]) {
58    SkPaint paint;
59    paint.setTextSize(SkIntToScalar(24));
60    paint.setAntiAlias(true);
61    sk_tool_utils::set_portable_typeface(&paint);
62    for (int i = 0; i < 4; ++i) {
63        SkString str;
64        str.appendScalar(k[i]);
65        SkScalar width = paint.measureText(str.c_str(), str.size());
66        canvas->drawText(str.c_str(), str.size(), x, y + paint.getTextSize(), paint);
67        x += width + SkIntToScalar(10);
68    }
69}
70
71class ArithmodeGM : public skiagm::GM {
72public:
73    ArithmodeGM () {}
74
75protected:
76
77    virtual SkString onShortName() {
78        return SkString("arithmode");
79    }
80
81    virtual SkISize onISize() { return SkISize::Make(640, 572); }
82
83    virtual void onDraw(SkCanvas* canvas) {
84        SkBitmap src = make_src();
85        SkBitmap dst = make_dst();
86
87        const SkScalar one = SK_Scalar1;
88        static const SkScalar K[] = {
89            0, 0, 0, 0,
90            0, 0, 0, one,
91            0, one, 0, 0,
92            0, 0, one, 0,
93            0, one, one, 0,
94            0, one, -one, 0,
95            0, one/2, one/2, 0,
96            0, one/2, one/2, one/4,
97            0, one/2, one/2, -one/4,
98            one/4, one/2, one/2, 0,
99            -one/4, one/2, one/2, 0,
100        };
101
102        const SkScalar* k = K;
103        const SkScalar* stop = k + SK_ARRAY_COUNT(K);
104        SkScalar y = 0;
105        SkScalar gap = SkIntToScalar(src.width() + 20);
106        while (k < stop) {
107            SkScalar x = 0;
108            canvas->drawBitmap(src, x, y, nullptr);
109            x += gap;
110            canvas->drawBitmap(dst, x, y, nullptr);
111            x += gap;
112            SkRect rect = SkRect::MakeXYWH(x, y, SkIntToScalar(WW), SkIntToScalar(HH));
113            canvas->saveLayer(&rect, nullptr);
114            canvas->drawBitmap(dst, x, y, nullptr);
115            SkXfermode* xfer = SkArithmeticMode::Create(k[0], k[1], k[2], k[3]);
116            SkPaint paint;
117            paint.setXfermode(xfer)->unref();
118            canvas->drawBitmap(src, x, y, &paint);
119            canvas->restore();
120            x += gap;
121            show_k_text(canvas, x, y, k);
122            k += 4;
123            y += SkIntToScalar(src.height() + 12);
124        }
125
126        // Draw two special cases to test enforcePMColor. In these cases, we
127        // draw the dst bitmap twice, the first time it is halved and inverted,
128        // leading to invalid premultiplied colors. If we enforcePMColor, these
129        // invalid values should be clamped, and will not contribute to the
130        // second draw.
131        for (int i = 0; i < 2; i++) {
132            const bool enforcePMColor = (i == 0);
133            SkScalar x = gap;
134            canvas->drawBitmap(dst, x, y, nullptr);
135            x += gap;
136            SkRect rect = SkRect::MakeXYWH(x, y, SkIntToScalar(WW), SkIntToScalar(HH));
137            canvas->saveLayer(&rect, nullptr);
138            SkXfermode* xfer1 = SkArithmeticMode::Create(0, -one / 2, 0, 1, enforcePMColor);
139            SkPaint paint1;
140            paint1.setXfermode(xfer1)->unref();
141            canvas->drawBitmap(dst, x, y, &paint1);
142            SkXfermode* xfer2 = SkArithmeticMode::Create(0, one / 2, -one, 1);
143            SkPaint paint2;
144            paint2.setXfermode(xfer2)->unref();
145            canvas->drawBitmap(dst, x, y, &paint2);
146            canvas->restore();
147            x += gap;
148
149            // Label
150            SkPaint paint;
151            paint.setTextSize(SkIntToScalar(24));
152            paint.setAntiAlias(true);
153            sk_tool_utils::set_portable_typeface(&paint);
154            SkString str(enforcePMColor ? "enforcePM" : "no enforcePM");
155            canvas->drawText(str.c_str(), str.size(), x, y + paint.getTextSize(), paint);
156
157            y += SkIntToScalar(src.height() + 12);
158        }
159    }
160
161private:
162    typedef GM INHERITED;
163};
164
165///////////////////////////////////////////////////////////////////////////////
166
167DEF_GM( return new ArithmodeGM; )
168