blob: ce34bfa86b237bdc52426bf7857949feccdf7eeb [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:
32
33
34 SkString onShortName() {
35 SkString str;
36 str.printf("complexclip4_%s",
37 fDoAAClip ? "aa" : "bw");
38 return str;
39 }
40
41 SkISize onISize() { return SkISize::Make(970, 780); }
42
Michael Ludwigc19b9c52020-06-25 16:19:03 -040043 // Android Framework will still support the legacy kReplace SkClipOp on older devices, so
44 // this represents how to do so while also respecting the device restriction using the newer
45 // androidFramework_replaceClip() API.
46 void emulateDeviceRestriction(SkCanvas* canvas, const SkIRect& deviceRestriction) {
47 // or any other device-space rect intersection
48 SkCanvasPriv::ReplaceClip(canvas, deviceRestriction);
49 // save for later replace clip ops
50 fDeviceRestriction = deviceRestriction;
51 }
52
53 void emulateClipRectReplace(SkCanvas* canvas,
54 const SkRect& clipRect,
55 bool aa) {
56 SkCanvasPriv::ReplaceClip(canvas, fDeviceRestriction);
57 canvas->clipRect(clipRect, SkClipOp::kIntersect, aa);
58 }
59
60 void emulateClipRRectReplace(SkCanvas* canvas,
61 const SkRRect& clipRRect,
62 bool aa) {
63 SkCanvasPriv::ReplaceClip(canvas, fDeviceRestriction);
64 canvas->clipRRect(clipRRect, SkClipOp::kIntersect, aa);
65 }
66
67 void emulateClipPathReplace(SkCanvas* canvas,
68 const SkPath& path,
69 bool aa) {
70 SkCanvasPriv::ReplaceClip(canvas, fDeviceRestriction);
71 canvas->clipPath(path, SkClipOp::kIntersect, aa);
72 }
73
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050074 virtual void onDraw(SkCanvas* canvas) {
75 SkPaint p;
76 p.setAntiAlias(fDoAAClip);
77 p.setColor(SK_ColorYELLOW);
78
79 canvas->save();
80 // draw a yellow rect through a rect clip
81 canvas->save();
Michael Ludwigc19b9c52020-06-25 16:19:03 -040082 emulateDeviceRestriction(canvas, SkIRect::MakeLTRB(100, 100, 300, 300));
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050083 canvas->drawColor(SK_ColorGREEN);
Michael Ludwigc19b9c52020-06-25 16:19:03 -040084 emulateClipRectReplace(canvas, SkRect::MakeLTRB(100, 200, 400, 500), fDoAAClip);
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050085 canvas->drawRect(SkRect::MakeLTRB(100, 200, 400, 500), p);
86 canvas->restore();
87
88 // draw a yellow rect through a diamond clip
89 canvas->save();
Michael Ludwigc19b9c52020-06-25 16:19:03 -040090 emulateDeviceRestriction(canvas, SkIRect::MakeLTRB(500, 100, 800, 300));
Stan Iliev5f1bb0a2016-12-12 17:39:55 -050091 canvas->drawColor(SK_ColorGREEN);
92
93 SkPath pathClip;
94 pathClip.moveTo(SkIntToScalar(650), SkIntToScalar(200));
95 pathClip.lineTo(SkIntToScalar(900), SkIntToScalar(300));
96 pathClip.lineTo(SkIntToScalar(650), SkIntToScalar(400));
97 pathClip.lineTo(SkIntToScalar(650), SkIntToScalar(300));
98 pathClip.close();
Michael Ludwigc19b9c52020-06-25 16:19:03 -040099 emulateClipPathReplace(canvas, pathClip, fDoAAClip);
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500100 canvas->drawRect(SkRect::MakeLTRB(500, 200, 900, 500), p);
101 canvas->restore();
102
103 // draw a yellow rect through a round rect clip
104 canvas->save();
Michael Ludwigc19b9c52020-06-25 16:19:03 -0400105 emulateDeviceRestriction(canvas, SkIRect::MakeLTRB(500, 500, 800, 700));
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500106 canvas->drawColor(SK_ColorGREEN);
107
Michael Ludwigc19b9c52020-06-25 16:19:03 -0400108 emulateClipRRectReplace(
109 canvas, SkRRect::MakeOval(SkRect::MakeLTRB(500, 600, 900, 750)), fDoAAClip);
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500110 canvas->drawRect(SkRect::MakeLTRB(500, 600, 900, 750), p);
111 canvas->restore();
112
Michael Ludwigc19b9c52020-06-25 16:19:03 -0400113 // fill the clip with yellow color showing that androidFramework_replaceClip is
114 // in device space
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500115 canvas->save();
116 canvas->clipRect(SkRect::MakeLTRB(100, 400, 300, 750),
117 kIntersect_SkClipOp, fDoAAClip);
118 canvas->drawColor(SK_ColorGREEN);
Michael Ludwigc19b9c52020-06-25 16:19:03 -0400119 // should not affect the device-space clip
120 canvas->rotate(20.f);
121 canvas->translate(50.f, 50.f);
122 emulateDeviceRestriction(canvas, SkIRect::MakeLTRB(150, 450, 250, 700));
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500123 canvas->drawColor(SK_ColorYELLOW);
124 canvas->restore();
125
126 canvas->restore();
127 }
128private:
Michael Ludwigc19b9c52020-06-25 16:19:03 -0400129 SkIRect fDeviceRestriction;
130 bool fDoAAClip;
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500131
132 typedef GM INHERITED;
133};
134
135//////////////////////////////////////////////////////////////////////////////
136
137DEF_GM(return new ComplexClip4GM(false);)
138DEF_GM(return new ComplexClip4GM(true);)
139}