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 <SkFont.h>
9 #include "gm.h"
10 #include "sk_tool_utils.h"
11 #include "SkArithmeticImageFilter.h"
12 #include "SkCanvas.h"
13 #include "SkColorPriv.h"
14 #include "SkGradientShader.h"
15 #include "SkImage.h"
16 #include "SkImageSource.h"
17 #include "SkShader.h"
18 #include "SkSurface.h"
19 
20 #define WW  100
21 #define HH  32
22 
make_src()23 static sk_sp<SkImage> make_src() {
24     sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(WW, HH));
25     SkCanvas* canvas = surface->getCanvas();
26 
27     SkPaint paint;
28     SkPoint pts[] = { {0, 0}, {SkIntToScalar(WW), SkIntToScalar(HH)} };
29     SkColor colors[] = {
30         SK_ColorTRANSPARENT, SK_ColorGREEN, SK_ColorCYAN,
31         SK_ColorRED, SK_ColorMAGENTA, SK_ColorWHITE,
32     };
33     paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
34                                                  SkShader::kClamp_TileMode));
35     canvas->drawPaint(paint);
36     return surface->makeImageSnapshot();
37 }
38 
make_dst()39 static sk_sp<SkImage> make_dst() {
40     sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(WW, HH));
41     SkCanvas* canvas = surface->getCanvas();
42 
43     SkPaint paint;
44     SkPoint pts[] = { {0, SkIntToScalar(HH)}, {SkIntToScalar(WW), 0} };
45     SkColor colors[] = {
46         SK_ColorBLUE, SK_ColorYELLOW, SK_ColorBLACK, SK_ColorGREEN,
47         SK_ColorGRAY,
48     };
49     paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
50                                                  SkShader::kClamp_TileMode));
51     canvas->drawPaint(paint);
52     return surface->makeImageSnapshot();
53 }
54 
show_k_text(SkCanvas * canvas,SkScalar x,SkScalar y,const SkScalar k[])55 static void show_k_text(SkCanvas* canvas, SkScalar x, SkScalar y, const SkScalar k[]) {
56     SkFont font(sk_tool_utils::create_portable_typeface(), 24);
57     font.setEdging(SkFont::Edging::kAntiAlias);
58     SkPaint paint;
59     paint.setAntiAlias(true);
60     for (int i = 0; i < 4; ++i) {
61         SkString str;
62         str.appendScalar(k[i]);
63         SkScalar width = font.measureText(str.c_str(), str.size(), kUTF8_SkTextEncoding);
64         canvas->drawString(str, x, y + font.getSize(), font, paint);
65         x += width + SkIntToScalar(10);
66     }
67 }
68 
69 class ArithmodeGM : public skiagm::GM {
70 public:
ArithmodeGM()71     ArithmodeGM () {}
72 
73 protected:
74 
onShortName()75     virtual SkString onShortName() {
76         return SkString("arithmode");
77     }
78 
onISize()79     virtual SkISize onISize() { return SkISize::Make(640, 572); }
80 
onDraw(SkCanvas * canvas)81     virtual void onDraw(SkCanvas* canvas) {
82         sk_sp<SkImage> src = make_src();
83         sk_sp<SkImage> dst = make_dst();
84         sk_sp<SkImageFilter> srcFilter = SkImageSource::Make(src);
85         sk_sp<SkImageFilter> dstFilter = SkImageSource::Make(dst);
86 
87         constexpr SkScalar one = SK_Scalar1;
88         constexpr 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         const SkRect rect = SkRect::MakeWH(WW, HH);
105         SkScalar gap = SkIntToScalar(WW + 20);
106         while (k < stop) {
107             {
108                 SkAutoCanvasRestore acr(canvas, true);
109                 canvas->drawImage(src, 0, 0);
110                 canvas->translate(gap, 0);
111                 canvas->drawImage(dst, 0, 0);
112                 canvas->translate(gap, 0);
113                 SkPaint paint;
114                 paint.setImageFilter(SkArithmeticImageFilter::Make(k[0], k[1], k[2], k[3], true,
115                                                                    dstFilter, srcFilter, nullptr));
116                 canvas->saveLayer(&rect, &paint);
117                 canvas->restore();
118 
119                 canvas->translate(gap, 0);
120                 show_k_text(canvas, 0, 0, k);
121             }
122 
123             k += 4;
124             canvas->translate(0, HH + 12);
125         }
126 
127         // Draw two special cases to test enforcePMColor. In these cases, we
128         // draw the dst bitmap twice, the first time it is halved and inverted,
129         // leading to invalid premultiplied colors. If we enforcePMColor, these
130         // invalid values should be clamped, and will not contribute to the
131         // second draw.
132         for (int i = 0; i < 2; i++) {
133             const bool enforcePMColor = (i == 0);
134 
135             {
136                 SkAutoCanvasRestore acr(canvas, true);
137                 canvas->translate(gap, 0);
138                 canvas->drawImage(dst, 0, 0);
139                 canvas->translate(gap, 0);
140 
141                 sk_sp<SkImageFilter> bg =
142                         SkArithmeticImageFilter::Make(0, 0, -one / 2, 1, enforcePMColor, dstFilter,
143                                                       nullptr, nullptr);
144                 SkPaint p;
145                 p.setImageFilter(SkArithmeticImageFilter::Make(0, one / 2, -one, 1, true,
146                                                                std::move(bg), dstFilter, nullptr));
147                 canvas->saveLayer(&rect, &p);
148                 canvas->restore();
149                 canvas->translate(gap, 0);
150 
151                 // Label
152                 SkFont font(sk_tool_utils::create_portable_typeface(), 24);
153                 SkString str(enforcePMColor ? "enforcePM" : "no enforcePM");
154                 canvas->drawString(str, 0, font.getSize(), font, SkPaint());
155             }
156             canvas->translate(0, HH + 12);
157         }
158     }
159 
160 private:
161     typedef GM INHERITED;
162 };
163 
164 ///////////////////////////////////////////////////////////////////////////////
165 
166 DEF_GM( return new ArithmodeGM; )
167