blob: 24be22acc9f2e7ff22f2de3fa11eba85165afa6f [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkClipOpPriv.h"
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050020
21namespace skiagm {
22
Michael Ludwigc19b9c52020-06-25 16:19:03 -040023// This test exercise SkCanvas::androidFramework_replaceClip behavior
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050024class ComplexClip4GM : public GM {
25public:
26 ComplexClip4GM(bool aaclip)
27 : fDoAAClip(aaclip) {
28 this->setBGColor(0xFFDEDFDE);
29 }
30
31protected:
Brian Salomond0072812020-07-21 17:03:56 -040032 SkString onShortName() override {
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050033 SkString str;
34 str.printf("complexclip4_%s",
35 fDoAAClip ? "aa" : "bw");
36 return str;
37 }
38
Brian Salomond0072812020-07-21 17:03:56 -040039 SkISize onISize() override { return SkISize::Make(970, 780); }
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050040
Michael Ludwigc19b9c52020-06-25 16:19:03 -040041 // Android Framework will still support the legacy kReplace SkClipOp on older devices, so
42 // this represents how to do so while also respecting the device restriction using the newer
43 // androidFramework_replaceClip() API.
44 void emulateDeviceRestriction(SkCanvas* canvas, const SkIRect& deviceRestriction) {
45 // or any other device-space rect intersection
46 SkCanvasPriv::ReplaceClip(canvas, deviceRestriction);
47 // save for later replace clip ops
48 fDeviceRestriction = deviceRestriction;
49 }
50
51 void emulateClipRectReplace(SkCanvas* canvas,
52 const SkRect& clipRect,
53 bool aa) {
54 SkCanvasPriv::ReplaceClip(canvas, fDeviceRestriction);
55 canvas->clipRect(clipRect, SkClipOp::kIntersect, aa);
56 }
57
58 void emulateClipRRectReplace(SkCanvas* canvas,
59 const SkRRect& clipRRect,
60 bool aa) {
61 SkCanvasPriv::ReplaceClip(canvas, fDeviceRestriction);
62 canvas->clipRRect(clipRRect, SkClipOp::kIntersect, aa);
63 }
64
65 void emulateClipPathReplace(SkCanvas* canvas,
66 const SkPath& path,
67 bool aa) {
68 SkCanvasPriv::ReplaceClip(canvas, fDeviceRestriction);
69 canvas->clipPath(path, SkClipOp::kIntersect, aa);
70 }
71
Brian Salomond0072812020-07-21 17:03:56 -040072 void onDraw(SkCanvas* canvas) override {
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050073 SkPaint p;
74 p.setAntiAlias(fDoAAClip);
75 p.setColor(SK_ColorYELLOW);
76
77 canvas->save();
78 // draw a yellow rect through a rect clip
79 canvas->save();
Michael Ludwigc19b9c52020-06-25 16:19:03 -040080 emulateDeviceRestriction(canvas, SkIRect::MakeLTRB(100, 100, 300, 300));
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050081 canvas->drawColor(SK_ColorGREEN);
Michael Ludwigc19b9c52020-06-25 16:19:03 -040082 emulateClipRectReplace(canvas, SkRect::MakeLTRB(100, 200, 400, 500), fDoAAClip);
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050083 canvas->drawRect(SkRect::MakeLTRB(100, 200, 400, 500), p);
84 canvas->restore();
85
86 // draw a yellow rect through a diamond clip
87 canvas->save();
Michael Ludwigc19b9c52020-06-25 16:19:03 -040088 emulateDeviceRestriction(canvas, SkIRect::MakeLTRB(500, 100, 800, 300));
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050089 canvas->drawColor(SK_ColorGREEN);
90
91 SkPath pathClip;
92 pathClip.moveTo(SkIntToScalar(650), SkIntToScalar(200));
93 pathClip.lineTo(SkIntToScalar(900), SkIntToScalar(300));
94 pathClip.lineTo(SkIntToScalar(650), SkIntToScalar(400));
95 pathClip.lineTo(SkIntToScalar(650), SkIntToScalar(300));
96 pathClip.close();
Michael Ludwigc19b9c52020-06-25 16:19:03 -040097 emulateClipPathReplace(canvas, pathClip, fDoAAClip);
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050098 canvas->drawRect(SkRect::MakeLTRB(500, 200, 900, 500), p);
99 canvas->restore();
100
101 // draw a yellow rect through a round rect clip
102 canvas->save();
Michael Ludwigc19b9c52020-06-25 16:19:03 -0400103 emulateDeviceRestriction(canvas, SkIRect::MakeLTRB(500, 500, 800, 700));
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500104 canvas->drawColor(SK_ColorGREEN);
105
Michael Ludwigc19b9c52020-06-25 16:19:03 -0400106 emulateClipRRectReplace(
107 canvas, SkRRect::MakeOval(SkRect::MakeLTRB(500, 600, 900, 750)), fDoAAClip);
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500108 canvas->drawRect(SkRect::MakeLTRB(500, 600, 900, 750), p);
109 canvas->restore();
110
Michael Ludwigc19b9c52020-06-25 16:19:03 -0400111 // fill the clip with yellow color showing that androidFramework_replaceClip is
112 // in device space
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500113 canvas->save();
114 canvas->clipRect(SkRect::MakeLTRB(100, 400, 300, 750),
115 kIntersect_SkClipOp, fDoAAClip);
116 canvas->drawColor(SK_ColorGREEN);
Michael Ludwigc19b9c52020-06-25 16:19:03 -0400117 // should not affect the device-space clip
118 canvas->rotate(20.f);
119 canvas->translate(50.f, 50.f);
120 emulateDeviceRestriction(canvas, SkIRect::MakeLTRB(150, 450, 250, 700));
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500121 canvas->drawColor(SK_ColorYELLOW);
122 canvas->restore();
123
124 canvas->restore();
125 }
126private:
Michael Ludwigc19b9c52020-06-25 16:19:03 -0400127 SkIRect fDeviceRestriction;
128 bool fDoAAClip;
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500129
130 typedef GM INHERITED;
131};
132
133//////////////////////////////////////////////////////////////////////////////
134
135DEF_GM(return new ComplexClip4GM(false);)
136DEF_GM(return new ComplexClip4GM(true);)
John Stilesa6841be2020-08-06 14:11:56 -0400137} // namespace skiagm