blob: b2158a6c8663d3f86f3d16f183018019cfa8924e [file] [log] [blame]
robertphillips7defaa62015-01-27 06:17:22 -08001/*
2 * Copyright 2015 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 "SampleCode.h"
9#include "SkCanvas.h"
10#include "SkInterpolator.h"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.h"
12#include "SkRRect.h"
robertphillips7defaa62015-01-27 06:17:22 -080013#include "SkTime.h"
14
15// This slide tests out the match up between BW clipping and rendering. It can
16// draw a large rect through some clip geometry and draw the same geometry
17// normally. Which one is drawn first can be toggled. The pair of objects is translated
18// fractionally (via an animator) to expose snapping bugs. The key bindings are:
19// 1-9: the different geometries
20// t: toggle which is drawn first the clip or the normal geometry
robertphillipsc89f6fb2015-02-09 07:47:17 -080021// f: flip-flops which corner the bottom AA clip rect occupies in the complex clip cases
robertphillips7defaa62015-01-27 06:17:22 -080022
23// The possible geometric combinations to test
24enum Geometry {
25 kRect_Geometry,
26 kRRect_Geometry,
27 kCircle_Geometry,
28 kConvexPath_Geometry,
29 kConcavePath_Geometry,
30 kRectAndRect_Geometry,
31 kRectAndRRect_Geometry,
32 kRectAndConvex_Geometry,
33 kRectAndConcave_Geometry
34};
35
36// The basic rect used is [kMin,kMin]..[kMax,kMax]
37static const float kMin = 100.5f;
38static const float kMid = 200.0f;
39static const float kMax = 299.5f;
40
robertphillipsc89f6fb2015-02-09 07:47:17 -080041// The translation applied to the base AA rect in the combination cases
42// (i.e., kRectAndRect through kRectAndConcave)
43static const float kXlate = 100.0f;
44
robertphillips7defaa62015-01-27 06:17:22 -080045SkRect create_rect(const SkPoint& offset) {
46 SkRect r = SkRect::MakeLTRB(kMin, kMin, kMax, kMax);
47 r.offset(offset);
48 return r;
49}
50
51SkRRect create_rrect(const SkPoint& offset) {
52 SkRRect rrect;
53 rrect.setRectXY(create_rect(offset), 10, 10);
54 return rrect;
55}
56
57SkRRect create_circle(const SkPoint& offset) {
58 SkRRect circle;
59 circle.setOval(create_rect(offset));
60 return circle;
61}
62
63SkPath create_convex_path(const SkPoint& offset) {
64 SkPath convexPath;
65 convexPath.moveTo(kMin, kMin);
66 convexPath.lineTo(kMax, kMax);
67 convexPath.lineTo(kMin, kMax);
68 convexPath.close();
69 convexPath.offset(offset.fX, offset.fY);
70 return convexPath;
71}
72
73SkPath create_concave_path(const SkPoint& offset) {
74 SkPath concavePath;
75 concavePath.moveTo(kMin, kMin);
76 concavePath.lineTo(kMid, 105.0f);
77 concavePath.lineTo(kMax, kMin);
78 concavePath.lineTo(295.0f, kMid);
79 concavePath.lineTo(kMax, kMax);
80 concavePath.lineTo(kMid, 295.0f);
81 concavePath.lineTo(kMin, kMax);
82 concavePath.lineTo(105.0f, kMid);
83 concavePath.close();
84
85 concavePath.offset(offset.fX, offset.fY);
86 return concavePath;
87}
88
robertphillips7defaa62015-01-27 06:17:22 -080089static void draw_normal_geom(SkCanvas* canvas, const SkPoint& offset, int geom, bool useAA) {
90 SkPaint p;
91 p.setAntiAlias(useAA);
92 p.setColor(SK_ColorBLACK);
93
94 switch (geom) {
95 case kRect_Geometry: // fall thru
96 case kRectAndRect_Geometry:
97 canvas->drawRect(create_rect(offset), p);
98 break;
99 case kRRect_Geometry: // fall thru
100 case kRectAndRRect_Geometry:
101 canvas->drawRRect(create_rrect(offset), p);
102 break;
103 case kCircle_Geometry:
104 canvas->drawRRect(create_circle(offset), p);
105 break;
106 case kConvexPath_Geometry: // fall thru
107 case kRectAndConvex_Geometry:
108 canvas->drawPath(create_convex_path(offset), p);
109 break;
110 case kConcavePath_Geometry: // fall thru
111 case kRectAndConcave_Geometry:
112 canvas->drawPath(create_concave_path(offset), p);
113 break;
halcanary9d524f22016-03-29 09:03:52 -0700114 }
robertphillips7defaa62015-01-27 06:17:22 -0800115}
116
117class ClipDrawMatchView : public SampleView {
118public:
robertphillipsc89f6fb2015-02-09 07:47:17 -0800119 ClipDrawMatchView() : fTrans(2, 5), fGeom(kRect_Geometry), fClipFirst(true), fSign(1) {
robertphillips7defaa62015-01-27 06:17:22 -0800120 SkScalar values[2];
121
122 fTrans.setRepeatCount(999);
123 values[0] = values[1] = 0;
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700124 fTrans.setKeyFrame(0, GetMSecs() + 1000, values);
robertphillips7defaa62015-01-27 06:17:22 -0800125 values[1] = 1;
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700126 fTrans.setKeyFrame(1, GetMSecs() + 2000, values);
robertphillips7defaa62015-01-27 06:17:22 -0800127 values[0] = values[1] = 1;
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700128 fTrans.setKeyFrame(2, GetMSecs() + 3000, values);
robertphillips7defaa62015-01-27 06:17:22 -0800129 values[1] = 0;
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700130 fTrans.setKeyFrame(3, GetMSecs() + 4000, values);
robertphillips7defaa62015-01-27 06:17:22 -0800131 values[0] = 0;
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700132 fTrans.setKeyFrame(4, GetMSecs() + 5000, values);
robertphillips7defaa62015-01-27 06:17:22 -0800133 }
134
135protected:
mtklein36352bf2015-03-25 18:17:31 -0700136 bool onQuery(SkEvent* evt) override {
robertphillips7defaa62015-01-27 06:17:22 -0800137 if (SampleCode::TitleQ(*evt)) {
138 SampleCode::TitleR(evt, "ClipDrawMatch");
139 return true;
140 }
141 SkUnichar uni;
142 if (SampleCode::CharQ(*evt, &uni)) {
143 switch (uni) {
halcanary96fcdcc2015-08-27 07:41:13 -0700144 case '1': fGeom = kRect_Geometry; this->inval(nullptr); return true;
145 case '2': fGeom = kRRect_Geometry; this->inval(nullptr); return true;
146 case '3': fGeom = kCircle_Geometry; this->inval(nullptr); return true;
147 case '4': fGeom = kConvexPath_Geometry; this->inval(nullptr); return true;
148 case '5': fGeom = kConcavePath_Geometry; this->inval(nullptr); return true;
149 case '6': fGeom = kRectAndRect_Geometry; this->inval(nullptr); return true;
150 case '7': fGeom = kRectAndRRect_Geometry; this->inval(nullptr); return true;
151 case '8': fGeom = kRectAndConvex_Geometry; this->inval(nullptr); return true;
152 case '9': fGeom = kRectAndConcave_Geometry; this->inval(nullptr); return true;
153 case 'f': fSign = -fSign; this->inval(nullptr); return true;
154 case 't': fClipFirst = !fClipFirst; this->inval(nullptr); return true;
robertphillips7defaa62015-01-27 06:17:22 -0800155 default: break;
156 }
157 }
158 return this->INHERITED::onQuery(evt);
159 }
160
robertphillipsc89f6fb2015-02-09 07:47:17 -0800161 void drawClippedGeom(SkCanvas* canvas, const SkPoint& offset, bool useAA) {
162
163 int count = canvas->save();
164
165 switch (fGeom) {
166 case kRect_Geometry:
reed66998382016-09-21 11:15:07 -0700167 canvas->clipRect(create_rect(offset), useAA);
robertphillipsc89f6fb2015-02-09 07:47:17 -0800168 break;
169 case kRRect_Geometry:
reed66998382016-09-21 11:15:07 -0700170 canvas->clipRRect(create_rrect(offset), useAA);
robertphillipsc89f6fb2015-02-09 07:47:17 -0800171 break;
172 case kCircle_Geometry:
reed66998382016-09-21 11:15:07 -0700173 canvas->clipRRect(create_circle(offset), useAA);
robertphillipsc89f6fb2015-02-09 07:47:17 -0800174 break;
175 case kConvexPath_Geometry:
reed66998382016-09-21 11:15:07 -0700176 canvas->clipPath(create_convex_path(offset), useAA);
robertphillipsc89f6fb2015-02-09 07:47:17 -0800177 break;
178 case kConcavePath_Geometry:
reed66998382016-09-21 11:15:07 -0700179 canvas->clipPath(create_concave_path(offset), useAA);
robertphillipsc89f6fb2015-02-09 07:47:17 -0800180 break;
181 case kRectAndRect_Geometry: {
182 SkRect r = create_rect(offset);
183 r.offset(fSign * kXlate, fSign * kXlate);
reed66998382016-09-21 11:15:07 -0700184 canvas->clipRect(r, true); // AA here forces shader clips
185 canvas->clipRect(create_rect(offset), useAA);
robertphillipsc89f6fb2015-02-09 07:47:17 -0800186 } break;
187 case kRectAndRRect_Geometry: {
188 SkRect r = create_rect(offset);
189 r.offset(fSign * kXlate, fSign * kXlate);
reed66998382016-09-21 11:15:07 -0700190 canvas->clipRect(r, true); // AA here forces shader clips
191 canvas->clipRRect(create_rrect(offset), useAA);
robertphillipsc89f6fb2015-02-09 07:47:17 -0800192 } break;
193 case kRectAndConvex_Geometry: {
194 SkRect r = create_rect(offset);
195 r.offset(fSign * kXlate, fSign * kXlate);
reed66998382016-09-21 11:15:07 -0700196 canvas->clipRect(r, true); // AA here forces shader clips
197 canvas->clipPath(create_convex_path(offset), useAA);
robertphillipsc89f6fb2015-02-09 07:47:17 -0800198 } break;
199 case kRectAndConcave_Geometry: {
200 SkRect r = create_rect(offset);
201 r.offset(fSign * kXlate, fSign * kXlate);
reed66998382016-09-21 11:15:07 -0700202 canvas->clipRect(r, true); // AA here forces shader clips
203 canvas->clipPath(create_concave_path(offset), useAA);
robertphillipsc89f6fb2015-02-09 07:47:17 -0800204 } break;
halcanary9d524f22016-03-29 09:03:52 -0700205 }
robertphillipsc89f6fb2015-02-09 07:47:17 -0800206
Mike Reed3661bc92017-02-22 13:21:42 -0500207 SkISize size = canvas->getBaseLayerSize();
robertphillipsc89f6fb2015-02-09 07:47:17 -0800208 SkRect bigR = SkRect::MakeWH(SkIntToScalar(size.width()), SkIntToScalar(size.height()));
209
210 SkPaint p;
211 p.setColor(SK_ColorRED);
212
213 canvas->drawRect(bigR, p);
214 canvas->restoreToCount(count);
215 }
216
robertphillips7defaa62015-01-27 06:17:22 -0800217 // Draw a big red rect through some clip geometry and also draw that same
218 // geometry in black. The order in which they are drawn can be swapped.
219 // This tests whether the clip and normally drawn geometry match up.
220 void drawGeometry(SkCanvas* canvas, const SkPoint& offset, bool useAA) {
221 if (fClipFirst) {
robertphillipsc89f6fb2015-02-09 07:47:17 -0800222 this->drawClippedGeom(canvas, offset, useAA);
robertphillips7defaa62015-01-27 06:17:22 -0800223 }
224
225 draw_normal_geom(canvas, offset, fGeom, useAA);
226
227 if (!fClipFirst) {
robertphillipsc89f6fb2015-02-09 07:47:17 -0800228 this->drawClippedGeom(canvas, offset, useAA);
robertphillips7defaa62015-01-27 06:17:22 -0800229 }
230 }
231
mtklein36352bf2015-03-25 18:17:31 -0700232 void onDrawContent(SkCanvas* canvas) override {
robertphillips7defaa62015-01-27 06:17:22 -0800233 SkScalar trans[2];
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700234 fTrans.timeToValues(GetMSecs(), trans);
robertphillips7defaa62015-01-27 06:17:22 -0800235
236 SkPoint offset;
237 offset.set(trans[0], trans[1]);
238
239 int saveCount = canvas->save();
240 this->drawGeometry(canvas, offset, false);
241 canvas->restoreToCount(saveCount);
242
halcanary96fcdcc2015-08-27 07:41:13 -0700243 this->inval(nullptr);
robertphillips7defaa62015-01-27 06:17:22 -0800244 }
245
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700246 SkMSec GetMSecs() const {
247 return static_cast<SkMSec>(SkTime::GetMSecs() - fStart);
248 }
249
robertphillips7defaa62015-01-27 06:17:22 -0800250private:
251 SkInterpolator fTrans;
252 Geometry fGeom;
253 bool fClipFirst;
robertphillipsc89f6fb2015-02-09 07:47:17 -0800254 int fSign;
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700255 const double fStart = SkTime::GetMSecs();
robertphillips7defaa62015-01-27 06:17:22 -0800256
257 typedef SampleView INHERITED;
258};
259
260//////////////////////////////////////////////////////////////////////////////
261
262static SkView* MyFactory() { return new ClipDrawMatchView; }
263static SkViewRegister reg(MyFactory);