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