add helper makeShader for the common Clamp case
[platform/upstream/libSkiaSharp.git] / gm / aaclip.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 #include "SkCanvas.h"
11 #include "SkPath.h"
12 #include "SkMakeUnique.h"
13
14 static void do_draw(SkCanvas* canvas, const SkRect& r) {
15     SkPaint paint;
16     paint.setBlendMode(SkBlendMode::kSrc);
17     paint.setColor(0x800000FF);
18     canvas->drawRect(r, paint);
19 }
20
21 /**
22  *  Exercise kDontClipToLayer_Legacy_SaveLayerFlag flag, which does not limit the clip to the
23  *  layer's bounds. Thus when a draw occurs, it can (depending on "where" it is) draw into the layer
24  *  and/or draw onto the surrounding portions of the canvas, or both.
25  *
26  *  This GM has a 100x100 rectangle (r), which its going to draw. However first it creates a layer
27  *  with this flag covering 1/2 of the rectangle (upper half). Then it draws the rect in SRC mode.
28  *
29  *  The portion of the draw that intersects the layer should see the SRC draw, apply it to the layer
30  *  and then during restore, it will SRC_OVER that layer onto the canvas (SRC_OVER since the layer
31  *  has no paint, so it gets the default xfermode during restore).
32  *
33  *  Note: when we talk about drawing directly into the "canvas", in fact we are drawing into an
34  *        "outer" layer we created (filled with red). This is a testing detail, so that our final
35  *        output image is itself opaque, otherwise we make it harder to view the GM as a PNG.
36  *
37  *  The portion of the draw below the layer draws directly into the canvas. Since it is in SRC mode,
38  *  it will write 0x80 to the canvas' alpha, overwriting the "red", which then gets blended with
39  *  the GM's white background.
40  *
41  *  The portion in the layer, will end up SRC_OVERing the 0x80 layer pixels onto the canvas' red
42  *  pixels, making magenta.
43  *
44  *  Thus the expected result is the upper half to be magenta 0xFF7F0080, and the lower half to be
45  *  light blue 0xFF7F7FFF.
46  */
47 DEF_SIMPLE_GM(dont_clip_to_layer, canvas, 120, 120) {
48     const SkRect r { 10, 10, 110, 110 };
49
50     // Wrap the entire test inside a red layer, so we don't punch the actual gm's alpha with
51     // kSrc_Mode, which makes it hard to view (we like our GMs to have opaque pixels).
52     canvas->saveLayer(&r, nullptr);
53     canvas->drawColor(SK_ColorRED);
54
55     SkRect r0 = { 20, 20, 100, 55 };
56     SkRect r1 = { 20, 65, 100, 100 };
57
58     SkCanvas::SaveLayerRec rec;
59     rec.fPaint = nullptr;
60     rec.fBounds = &r0;
61     rec.fBackdrop = nullptr;
62     rec.fSaveLayerFlags = 1 << 31;//SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
63     canvas->saveLayer(rec);
64     rec.fBounds = &r1;
65     canvas->saveLayer(rec);
66     do_draw(canvas, r);
67     canvas->restore();
68     canvas->restore();
69
70     canvas->restore();  // red-layer
71 }
72
73 /** Draw a 2px border around the target, then red behind the target;
74     set the clip to match the target, then draw >> the target in blue.
75 */
76
77 static void draw(SkCanvas* canvas, SkRect& target, int x, int y) {
78     SkPaint borderPaint;
79     borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
80     borderPaint.setAntiAlias(true);
81     SkPaint backgroundPaint;
82     backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
83     backgroundPaint.setAntiAlias(true);
84     SkPaint foregroundPaint;
85     foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
86     foregroundPaint.setAntiAlias(true);
87
88     canvas->save();
89     canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
90     target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
91     canvas->drawRect(target, borderPaint);
92     target.inset(SkIntToScalar(2), SkIntToScalar(2));
93     canvas->drawRect(target, backgroundPaint);
94     canvas->clipRect(target, true);
95     target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
96     canvas->drawRect(target, foregroundPaint);
97     canvas->restore();
98 }
99
100 static void draw_square(SkCanvas* canvas, int x, int y) {
101     SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
102     draw(canvas, target, x, y);
103 }
104
105 static void draw_column(SkCanvas* canvas, int x, int y) {
106     SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
107     draw(canvas, target, x, y);
108 }
109
110 static void draw_bar(SkCanvas* canvas, int x, int y) {
111     SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
112     draw(canvas, target, x, y);
113 }
114
115 static void draw_rect_tests(SkCanvas* canvas) {
116     draw_square(canvas, 10, 10);
117     draw_column(canvas, 30, 10);
118     draw_bar(canvas, 10, 30);
119 }
120
121 /**
122    Test a set of clipping problems discovered while writing blitAntiRect,
123    and test all the code paths through the clipping blitters.
124    Each region should show as a blue center surrounded by a 2px green
125    border, with no red.
126 */
127
128 class AAClipGM : public skiagm::GM {
129 public:
130     AAClipGM() {
131
132     }
133
134 protected:
135     SkString onShortName() override {
136         return SkString("aaclip");
137     }
138
139     SkISize onISize() override {
140         return SkISize::Make(240, 120);
141     }
142
143     void onDraw(SkCanvas* canvas) override {
144         // Initial pixel-boundary-aligned draw
145         draw_rect_tests(canvas);
146
147         // Repeat 4x with .2, .4, .6, .8 px offsets
148         canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
149         canvas->translate(SkIntToScalar(50), 0);
150         draw_rect_tests(canvas);
151
152         canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
153         canvas->translate(SkIntToScalar(50), 0);
154         draw_rect_tests(canvas);
155
156         canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
157         canvas->translate(SkIntToScalar(50), 0);
158         draw_rect_tests(canvas);
159
160         canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
161         canvas->translate(SkIntToScalar(50), 0);
162         draw_rect_tests(canvas);
163     }
164
165 private:
166     typedef skiagm::GM INHERITED;
167 };
168
169 DEF_GM(return new AAClipGM;)
170
171 /////////////////////////////////////////////////////////////////////////
172
173 #ifdef SK_BUILD_FOR_MAC
174
175 static std::unique_ptr<SkCanvas> make_canvas(const SkBitmap& bm) {
176     const SkImageInfo& info = bm.info();
177     if (info.bytesPerPixel() == 4) {
178         return SkCanvas::MakeRasterDirectN32(info.width(), info.height(),
179                                              (SkPMColor*)bm.getPixels(),
180                                              bm.rowBytes());
181     } else {
182         return skstd::make_unique<SkCanvas>(bm);
183     }
184 }
185
186 #include "SkCGUtils.h"
187 static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
188     SkBitmap bm;
189     bm.allocPixels(info);
190
191     if (info.isOpaque()) {
192         bm.eraseColor(SK_ColorGREEN);
193     } else {
194         bm.eraseColor(0);
195     }
196
197     SkPaint paint;
198     paint.setAntiAlias(true);
199     paint.setColor(SK_ColorBLUE);
200     make_canvas(bm)->drawCircle(50, 50, 49, paint);
201     canvas->drawBitmap(bm, 10, 10);
202
203     CGImageRef image = SkCreateCGImageRefWithColorspace(bm, nullptr);
204
205     SkBitmap bm2;
206     SkCreateBitmapFromCGImage(&bm2, image);
207     canvas->drawBitmap(bm2, 10, 120);
208     canvas->drawImage(SkMakeImageFromCGImage(image), 10, 120 + bm2.height() + 10);
209
210     CGImageRelease(image);
211 }
212
213 class CGImageGM : public skiagm::GM {
214 public:
215     CGImageGM() {}
216
217 protected:
218     SkString onShortName() override {
219         return SkString("cgimage");
220     }
221
222     SkISize onISize() override {
223         return SkISize::Make(800, 250);
224     }
225
226     void onDraw(SkCanvas* canvas) override {
227         const struct {
228             SkColorType fCT;
229             SkAlphaType fAT;
230         } rec[] = {
231             { kRGB_565_SkColorType, kOpaque_SkAlphaType },
232
233             { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
234             { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
235             { kRGBA_8888_SkColorType, kOpaque_SkAlphaType },
236
237             { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
238             { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
239             { kBGRA_8888_SkColorType, kOpaque_SkAlphaType },
240         };
241
242         for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
243             SkImageInfo info = SkImageInfo::Make(100, 100, rec[i].fCT, rec[i].fAT);
244             test_image(canvas, info);
245             canvas->translate(info.width() + 10, 0);
246         }
247     }
248
249 private:
250     typedef skiagm::GM INHERITED;
251 };
252 //DEF_GM( return new CGImageGM; )
253 #endif
254
255 ///////////////////////////////////////////////////////////////////////////////////////////////////
256
257 // https://bug.skia.org/3716
258 class ClipCubicGM : public skiagm::GM {
259     const SkScalar W = 100;
260     const SkScalar H = 240;
261
262     SkPath fVPath, fHPath;
263 public:
264     ClipCubicGM() {
265         fVPath.moveTo(W, 0);
266         fVPath.cubicTo(W, H-10, 0, 10, 0, H);
267
268         SkMatrix pivot;
269         pivot.setRotate(90, W/2, H/2);
270         fVPath.transform(pivot, &fHPath);
271     }
272
273 protected:
274     SkString onShortName() override {
275         return SkString("clipcubic");
276     }
277
278     SkISize onISize() override {
279         return SkISize::Make(400, 410);
280     }
281
282     void doDraw(SkCanvas* canvas, const SkPath& path) {
283         SkPaint paint;
284         paint.setAntiAlias(true);
285
286         paint.setColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
287         canvas->drawPath(path, paint);
288
289         paint.setColor(SK_ColorRED);
290         paint.setStyle(SkPaint::kStroke_Style);
291         canvas->drawPath(path, paint);
292     }
293
294     void drawAndClip(SkCanvas* canvas, const SkPath& path, SkScalar dx, SkScalar dy) {
295         SkAutoCanvasRestore acr(canvas, true);
296
297         SkRect r = SkRect::MakeXYWH(0, H/4, W, H/2);
298         SkPaint paint;
299         paint.setColor(sk_tool_utils::color_to_565(0xFF8888FF));
300
301         canvas->drawRect(r, paint);
302         this->doDraw(canvas, path);
303
304         canvas->translate(dx, dy);
305
306         canvas->drawRect(r, paint);
307         canvas->clipRect(r);
308         this->doDraw(canvas, path);
309     }
310
311     void onDraw(SkCanvas* canvas) override {
312         canvas->translate(80, 10);
313         this->drawAndClip(canvas, fVPath, 200, 0);
314         canvas->translate(0, 200);
315         this->drawAndClip(canvas, fHPath, 200, 0);
316     }
317
318 private:
319     typedef skiagm::GM INHERITED;
320 };
321 DEF_GM(return new ClipCubicGM;)