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
14static 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 */
47DEF_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
69    canvas->restore();  // red-layer
70}
71
72/** Draw a 2px border around the target, then red behind the target;
73    set the clip to match the target, then draw >> the target in blue.
74*/
75
76static void draw(SkCanvas* canvas, SkRect& target, int x, int y) {
77    SkPaint borderPaint;
78    borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
79    borderPaint.setAntiAlias(true);
80    SkPaint backgroundPaint;
81    backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
82    backgroundPaint.setAntiAlias(true);
83    SkPaint foregroundPaint;
84    foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
85    foregroundPaint.setAntiAlias(true);
86
87    canvas->save();
88    canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
89    target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
90    canvas->drawRect(target, borderPaint);
91    target.inset(SkIntToScalar(2), SkIntToScalar(2));
92    canvas->drawRect(target, backgroundPaint);
93    canvas->clipRect(target, true);
94    target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
95    canvas->drawRect(target, foregroundPaint);
96    canvas->restore();
97}
98
99static void draw_square(SkCanvas* canvas, int x, int y) {
100    SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
101    draw(canvas, target, x, y);
102}
103
104static void draw_column(SkCanvas* canvas, int x, int y) {
105    SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
106    draw(canvas, target, x, y);
107}
108
109static void draw_bar(SkCanvas* canvas, int x, int y) {
110    SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
111    draw(canvas, target, x, y);
112}
113
114static void draw_rect_tests(SkCanvas* canvas) {
115    draw_square(canvas, 10, 10);
116    draw_column(canvas, 30, 10);
117    draw_bar(canvas, 10, 30);
118}
119
120/**
121   Test a set of clipping problems discovered while writing blitAntiRect,
122   and test all the code paths through the clipping blitters.
123   Each region should show as a blue center surrounded by a 2px green
124   border, with no red.
125*/
126
127class AAClipGM : public skiagm::GM {
128public:
129    AAClipGM() {
130
131    }
132
133protected:
134    SkString onShortName() override {
135        return SkString("aaclip");
136    }
137
138    SkISize onISize() override {
139        return SkISize::Make(240, 120);
140    }
141
142    void onDraw(SkCanvas* canvas) override {
143        // Initial pixel-boundary-aligned draw
144        draw_rect_tests(canvas);
145
146        // Repeat 4x with .2, .4, .6, .8 px offsets
147        canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
148        canvas->translate(SkIntToScalar(50), 0);
149        draw_rect_tests(canvas);
150
151        canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
152        canvas->translate(SkIntToScalar(50), 0);
153        draw_rect_tests(canvas);
154
155        canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
156        canvas->translate(SkIntToScalar(50), 0);
157        draw_rect_tests(canvas);
158
159        canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
160        canvas->translate(SkIntToScalar(50), 0);
161        draw_rect_tests(canvas);
162    }
163
164private:
165    typedef skiagm::GM INHERITED;
166};
167
168DEF_GM(return new AAClipGM;)
169
170/////////////////////////////////////////////////////////////////////////
171
172#ifdef SK_BUILD_FOR_MAC
173
174static std::unique_ptr<SkCanvas> make_canvas(const SkBitmap& bm) {
175    const SkImageInfo& info = bm.info();
176    if (info.bytesPerPixel() == 4) {
177        return SkCanvas::MakeRasterDirectN32(info.width(), info.height(),
178                                             (SkPMColor*)bm.getPixels(),
179                                             bm.rowBytes());
180    } else {
181        return skstd::make_unique<SkCanvas>(bm);
182    }
183}
184
185#include "SkCGUtils.h"
186static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
187    SkBitmap bm;
188    bm.allocPixels(info);
189
190    if (info.isOpaque()) {
191        bm.eraseColor(SK_ColorGREEN);
192    } else {
193        bm.eraseColor(0);
194    }
195
196    SkPaint paint;
197    paint.setAntiAlias(true);
198    paint.setColor(SK_ColorBLUE);
199    make_canvas(bm)->drawCircle(50, 50, 49, paint);
200    canvas->drawBitmap(bm, 10, 10);
201
202    CGImageRef image = SkCreateCGImageRefWithColorspace(bm, nullptr);
203
204    SkBitmap bm2;
205    SkCreateBitmapFromCGImage(&bm2, image);
206    canvas->drawBitmap(bm2, 10, 120);
207    canvas->drawImage(SkMakeImageFromCGImage(image), 10, 120 + bm2.height() + 10);
208
209    CGImageRelease(image);
210}
211
212class CGImageGM : public skiagm::GM {
213public:
214    CGImageGM() {}
215
216protected:
217    SkString onShortName() override {
218        return SkString("cgimage");
219    }
220
221    SkISize onISize() override {
222        return SkISize::Make(800, 250);
223    }
224
225    void onDraw(SkCanvas* canvas) override {
226        const struct {
227            SkColorType fCT;
228            SkAlphaType fAT;
229        } rec[] = {
230            { kRGB_565_SkColorType, kOpaque_SkAlphaType },
231
232            { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
233            { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
234            { kRGBA_8888_SkColorType, kOpaque_SkAlphaType },
235
236            { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
237            { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
238            { kBGRA_8888_SkColorType, kOpaque_SkAlphaType },
239        };
240
241        for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
242            SkImageInfo info = SkImageInfo::Make(100, 100, rec[i].fCT, rec[i].fAT);
243            test_image(canvas, info);
244            canvas->translate(info.width() + 10, 0);
245        }
246    }
247
248private:
249    typedef skiagm::GM INHERITED;
250};
251//DEF_GM( return new CGImageGM; )
252#endif
253
254///////////////////////////////////////////////////////////////////////////////////////////////////
255
256// https://bug.skia.org/3716
257class ClipCubicGM : public skiagm::GM {
258    const SkScalar W = 100;
259    const SkScalar H = 240;
260
261    SkPath fVPath, fHPath;
262public:
263    ClipCubicGM() {
264        fVPath.moveTo(W, 0);
265        fVPath.cubicTo(W, H-10, 0, 10, 0, H);
266
267        SkMatrix pivot;
268        pivot.setRotate(90, W/2, H/2);
269        fVPath.transform(pivot, &fHPath);
270    }
271
272protected:
273    SkString onShortName() override {
274        return SkString("clipcubic");
275    }
276
277    SkISize onISize() override {
278        return SkISize::Make(400, 410);
279    }
280
281    void doDraw(SkCanvas* canvas, const SkPath& path) {
282        SkPaint paint;
283        paint.setAntiAlias(true);
284
285        paint.setColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
286        canvas->drawPath(path, paint);
287
288        paint.setColor(SK_ColorRED);
289        paint.setStyle(SkPaint::kStroke_Style);
290        canvas->drawPath(path, paint);
291    }
292
293    void drawAndClip(SkCanvas* canvas, const SkPath& path, SkScalar dx, SkScalar dy) {
294        SkAutoCanvasRestore acr(canvas, true);
295
296        SkRect r = SkRect::MakeXYWH(0, H/4, W, H/2);
297        SkPaint paint;
298        paint.setColor(sk_tool_utils::color_to_565(0xFF8888FF));
299
300        canvas->drawRect(r, paint);
301        this->doDraw(canvas, path);
302
303        canvas->translate(dx, dy);
304
305        canvas->drawRect(r, paint);
306        canvas->clipRect(r);
307        this->doDraw(canvas, path);
308    }
309
310    void onDraw(SkCanvas* canvas) override {
311        canvas->translate(80, 10);
312        this->drawAndClip(canvas, fVPath, 200, 0);
313        canvas->translate(0, 200);
314        this->drawAndClip(canvas, fHPath, 200, 0);
315    }
316
317private:
318    typedef skiagm::GM INHERITED;
319};
320DEF_GM(return new ClipCubicGM;)
321