blob: 162fac3ac3ad7603c9f72f7309507045a3a4cb7f [file] [log] [blame]
Stan Iliev5f1bb0a2016-12-12 17:39:55 -05001/*
2 * Copyright 2016 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 */
Ben Wagner7fde8e12019-05-01 17:28:53 -04007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkRRect.h"
14#include "include/core/SkRect.h"
15#include "include/core/SkScalar.h"
16#include "include/core/SkSize.h"
17#include "include/core/SkString.h"
Michael Ludwigc19b9c52020-06-25 16:19:03 -040018#include "src/core/SkCanvasPriv.h"
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050019
20namespace skiagm {
21
Michael Ludwigc19b9c52020-06-25 16:19:03 -040022// This test exercise SkCanvas::androidFramework_replaceClip behavior
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050023class ComplexClip4GM : public GM {
24public:
25 ComplexClip4GM(bool aaclip)
26 : fDoAAClip(aaclip) {
27 this->setBGColor(0xFFDEDFDE);
28 }
29
30protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000031 SkString getName() const override {
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050032 SkString str;
33 str.printf("complexclip4_%s",
34 fDoAAClip ? "aa" : "bw");
35 return str;
36 }
37
Leandro Lovisolo8f023882023-08-15 21:13:52 +000038 SkISize getISize() override { return SkISize::Make(970, 780); }
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050039
Michael Ludwigc19b9c52020-06-25 16:19:03 -040040 // Android Framework will still support the legacy kReplace SkClipOp on older devices, so
41 // this represents how to do so while also respecting the device restriction using the newer
Michael Ludwigcfd204a2021-07-23 11:48:34 -040042 // androidFramework_resetClip() API.
Michael Ludwigc19b9c52020-06-25 16:19:03 -040043 void emulateDeviceRestriction(SkCanvas* canvas, const SkIRect& deviceRestriction) {
Michael Ludwigcfd204a2021-07-23 11:48:34 -040044 // TODO(michaelludwig): It may make more sense for device clip restriction to move on to
45 // the SkSurface (which would let this GM draw correctly in viewer).
46 canvas->androidFramework_setDeviceClipRestriction(deviceRestriction);
Michael Ludwigc19b9c52020-06-25 16:19:03 -040047 }
48
49 void emulateClipRectReplace(SkCanvas* canvas,
50 const SkRect& clipRect,
51 bool aa) {
Michael Ludwigcfd204a2021-07-23 11:48:34 -040052 SkCanvasPriv::ResetClip(canvas);
Michael Ludwigc19b9c52020-06-25 16:19:03 -040053 canvas->clipRect(clipRect, SkClipOp::kIntersect, aa);
54 }
55
56 void emulateClipRRectReplace(SkCanvas* canvas,
57 const SkRRect& clipRRect,
58 bool aa) {
Michael Ludwigcfd204a2021-07-23 11:48:34 -040059 SkCanvasPriv::ResetClip(canvas);
Michael Ludwigc19b9c52020-06-25 16:19:03 -040060 canvas->clipRRect(clipRRect, SkClipOp::kIntersect, aa);
61 }
62
63 void emulateClipPathReplace(SkCanvas* canvas,
64 const SkPath& path,
65 bool aa) {
Michael Ludwigcfd204a2021-07-23 11:48:34 -040066 SkCanvasPriv::ResetClip(canvas);
Michael Ludwigc19b9c52020-06-25 16:19:03 -040067 canvas->clipPath(path, SkClipOp::kIntersect, aa);
68 }
69
Brian Salomond0072812020-07-21 17:03:56 -040070 void onDraw(SkCanvas* canvas) override {
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050071 SkPaint p;
72 p.setAntiAlias(fDoAAClip);
73 p.setColor(SK_ColorYELLOW);
74
75 canvas->save();
76 // draw a yellow rect through a rect clip
77 canvas->save();
Michael Ludwigc19b9c52020-06-25 16:19:03 -040078 emulateDeviceRestriction(canvas, SkIRect::MakeLTRB(100, 100, 300, 300));
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050079 canvas->drawColor(SK_ColorGREEN);
Michael Ludwigc19b9c52020-06-25 16:19:03 -040080 emulateClipRectReplace(canvas, SkRect::MakeLTRB(100, 200, 400, 500), fDoAAClip);
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050081 canvas->drawRect(SkRect::MakeLTRB(100, 200, 400, 500), p);
82 canvas->restore();
83
84 // draw a yellow rect through a diamond clip
85 canvas->save();
Michael Ludwigc19b9c52020-06-25 16:19:03 -040086 emulateDeviceRestriction(canvas, SkIRect::MakeLTRB(500, 100, 800, 300));
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050087 canvas->drawColor(SK_ColorGREEN);
88
Mike Reede9d783c2020-08-17 14:14:13 -040089 SkPath pathClip = SkPath::Polygon({
90 {650, 200},
91 {900, 300},
92 {650, 400},
93 {650, 300},
94 }, true);
Michael Ludwigc19b9c52020-06-25 16:19:03 -040095 emulateClipPathReplace(canvas, pathClip, fDoAAClip);
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050096 canvas->drawRect(SkRect::MakeLTRB(500, 200, 900, 500), p);
97 canvas->restore();
98
99 // draw a yellow rect through a round rect clip
100 canvas->save();
Michael Ludwigc19b9c52020-06-25 16:19:03 -0400101 emulateDeviceRestriction(canvas, SkIRect::MakeLTRB(500, 500, 800, 700));
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500102 canvas->drawColor(SK_ColorGREEN);
103
Michael Ludwigc19b9c52020-06-25 16:19:03 -0400104 emulateClipRRectReplace(
105 canvas, SkRRect::MakeOval(SkRect::MakeLTRB(500, 600, 900, 750)), fDoAAClip);
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500106 canvas->drawRect(SkRect::MakeLTRB(500, 600, 900, 750), p);
107 canvas->restore();
108
Michael Ludwigc19b9c52020-06-25 16:19:03 -0400109 // fill the clip with yellow color showing that androidFramework_replaceClip is
110 // in device space
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500111 canvas->save();
112 canvas->clipRect(SkRect::MakeLTRB(100, 400, 300, 750),
Michael Ludwig2f6e2f82021-08-03 13:08:50 -0400113 SkClipOp::kIntersect, fDoAAClip);
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500114 canvas->drawColor(SK_ColorGREEN);
Michael Ludwigc19b9c52020-06-25 16:19:03 -0400115 // should not affect the device-space clip
116 canvas->rotate(20.f);
117 canvas->translate(50.f, 50.f);
118 emulateDeviceRestriction(canvas, SkIRect::MakeLTRB(150, 450, 250, 700));
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500119 canvas->drawColor(SK_ColorYELLOW);
120 canvas->restore();
121
122 canvas->restore();
123 }
124private:
Michael Ludwigc19b9c52020-06-25 16:19:03 -0400125 bool fDoAAClip;
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500126
John Stiles7571f9e2020-09-02 22:42:33 -0400127 using INHERITED = GM;
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500128};
129
130//////////////////////////////////////////////////////////////////////////////
131
132DEF_GM(return new ComplexClip4GM(false);)
133DEF_GM(return new ComplexClip4GM(true);)
John Stilesa6841be2020-08-06 14:11:56 -0400134} // namespace skiagm