blob: 32ba90d58ed55161e70b1bc313e6a3a2e6d43e20 [file] [log] [blame]
Cary Clarkdb160012018-08-31 15:07:51 -04001/*
2 * Copyright 2018 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 */
Mike Reed89126e42019-01-03 12:59:14 -05007
Cary Clarkdb160012018-08-31 15:07:51 -04008#include "Sample.h"
9#include "SkAnimTimer.h"
10#include "SkCanvas.h"
Mike Reed89126e42019-01-03 12:59:14 -050011#include "SkFont.h"
Cary Clarkdb160012018-08-31 15:07:51 -040012#include "SkGeometry.h"
13#include "SkPath.h"
14#include <string>
15
16// This draws an animation where every cubic has a cusp, to test drawing a circle
17// at the cusp point. Create a unit square. A cubic with its control points
18// at the four corners crossing over itself has a cusp.
19
20// Project the unit square through a random affine matrix.
21// Chop the cubic in two. One half of the cubic will have a cusp
22// (unless it was chopped exactly at the cusp point).
23
24// Running this looks mostly OK, but will occasionally draw something odd.
25// The odd parts don't appear related to the cusp code, but are old stroking
26// bugs that have not been fixed, yet.
27
28SkMSec start = 0;
29SkMSec curTime;
30bool first = true;
31
32// Create a path with one or two cubics, where one has a cusp.
33static SkPath cusp(const SkPoint P[4], SkPoint PP[7], bool& split, int speed, SkScalar phase) {
34 SkPath path;
35 path.moveTo(P[0]);
36 SkScalar t = (curTime % speed) / SkIntToFloat(speed);
37 t += phase;
38 if (t > 1) {
39 t -= 1;
40 }
41 if (0 <= t || t >= 1) {
42 path.cubicTo(P[1], P[2], P[3]);
43 split = false;
44 } else {
45 SkChopCubicAt(P, PP, t);
46 path.cubicTo(PP[1], PP[2], PP[3]);
47 path.cubicTo(PP[4], PP[5], PP[6]);
48 split = true;
49 }
50 return path;
51}
52
53// Scale the animation counter to a value that oscillates from -scale to +scale.
54static SkScalar linearToLoop(int speed, SkScalar phase, SkScalar scale) {
55 SkScalar loop;
56 SkScalar linear = (curTime % speed) / SkIntToFloat(speed); // 0 to 1
57 linear += phase;
58 if (linear > 1) {
59 linear -= 1;
60 }
61 if (linear < .25) {
62 loop = linear * 4; // 0 to .25 ==> 0 to 1
63 } else if (linear < .75) { // .25 to .75 ==> 1 to -1
64 loop = (.5 - linear) * 4;
65 } else { // .75 to 1 ==> -1 to 0
66 loop = (linear - 1) * 4;
67 }
68 return loop * scale;
69}
70
71struct data {
72 SkIPoint pt[4];
73} dat[] = {
74// When the animation looks funny, pause, and paste the last part of the stream in stdout here.
75// Enable the 1st #if to play the recorded stream backwards.
76// Enable the 2nd #if and replace the second 'i = ##' with the value of datCount that shows the bug.
77{{{0x43480000,0x43960000},{0x4318b999,0x4321570b},{0x432f999a,0x435a0a3d},{0x43311fff,0x43734cce},}},
78{{{0x43480000,0x43960000},{0x431d1ddf,0x4321ae13},{0x4331ddde,0x435c147c},{0x43334001,0x43719997},}},
79{{{0x43480000,0x43960000},{0x43218224,0x43220520},{0x43342223,0x435e1eba},{0x43356001,0x436fe666},}},
80{{{0x43480000,0x43960000},{0x4325a445,0x43225708},{0x43364444,0x43600a3c},{0x43376001,0x436e4ccc},}},
81{{{0x43480000,0x43960000},{0x432a0889,0x4322ae16},{0x43388889,0x4362147b},{0x43398000,0x436c999b},}},
82{{{0x43480000,0x43960000},{0x432e6ccd,0x43230523},{0x433acccd,0x43641eba},{0x433ba000,0x436ae66a},}},
83{{{0x43480000,0x43960000},{0x43328eef,0x4323570c},{0x433ceeee,0x43660a3c},{0x433da000,0x43694cd0},}},
84{{{0x43480000,0x43960000},{0x4336f333,0x4323ae13},{0x433f3333,0x4368147a},{0x433fc000,0x43679998},}},
85{{{0x43480000,0x43960000},{0x433b5777,0x43240520},{0x43417777,0x436a1eb9},{0x4341e000,0x4365e668},}},
86{{{0x43480000,0x43960000},{0x433f799a,0x4324570c},{0x4343999a,0x436c0a3e},{0x4343e000,0x43644cce},}},
87{{{0x43480000,0x43960000},{0x4343ddde,0x4324ae13},{0x4345dddf,0x436e147c},{0x43460000,0x43629996},}},
88{{{0x43480000,0x43960000},{0x43484222,0x4325051e},{0x43482222,0x43701eb9},{0x43481fff,0x4360e666},}},
89{{{0x43480000,0x43960000},{0x434c6446,0x43255709},{0x434a4444,0x43720a3e},{0x434a2002,0x435f4ccc},}},
90{{{0x43480000,0x43960000},{0x4350c888,0x4325ae16},{0x434c8889,0x4374147c},{0x434c3fff,0x435d999a},}},
91{{{0x43480000,0x43960000},{0x43552cce,0x43260521},{0x434ecccd,0x43761eb8},{0x434e6001,0x435be669},}},
92{{{0x43480000,0x43960000},{0x43594eee,0x4326570c},{0x4350eeef,0x43780a3d},{0x43505fff,0x435a4ccf},}},
93{{{0x43480000,0x43960000},{0x435db334,0x4326ae19},{0x43533333,0x437a147c},{0x43528001,0x4358999e},}},
94{{{0x43480000,0x43960000},{0x4361d555,0x43270002},{0x43555555,0x437bfffe},{0x43547fff,0x43570004},}},
95{{{0x43480000,0x43960000},{0x4366399a,0x4327570c},{0x4357999a,0x437e0a3f},{0x4356a001,0x43554ccd},}},
96{{{0x43480000,0x43960000},{0x436a9ddc,0x4327ae12},{0x4359ddde,0x43800a3e},{0x4358bffe,0x43539996},}},
97{{{0x43480000,0x43960000},{0x436f0222,0x4328051c},{0x435c2222,0x43810f5c},{0x435ae000,0x4351e664},}},
98};
99
100size_t datCount = SK_ARRAY_COUNT(dat);
101
102class CuspView : public Sample {
103public:
104 CuspView() {}
105protected:
106 bool onQuery(Sample::Event* evt) override {
107 if (Sample::TitleQ(*evt)) {
108 Sample::TitleR(evt, "Cusp");
109 return true;
110 }
111 return this->INHERITED::onQuery(evt);
112 }
113
114 void onDrawContent(SkCanvas* canvas) override {
115 SkPaint p;
116 p.setAntiAlias(true);
117 p.setStyle(SkPaint::kStroke_Style);
118 p.setStrokeWidth(20);
119 #if 0 // enable to play through the stream above backwards.
120 SkPath path;
121 int i;
122 #if 0 // disable to draw only one problematic cubic
123 i = --datCount;
124 #else
125 i = 14; // index into dat of problematic cubic
126 #endif
127 path.moveTo( SkBits2Float(dat[i].pt[0].fX), SkBits2Float(dat[i].pt[0].fY));
128 path.cubicTo(SkBits2Float(dat[i].pt[1].fX), SkBits2Float(dat[i].pt[1].fY),
129 SkBits2Float(dat[i].pt[2].fX), SkBits2Float(dat[i].pt[2].fY),
130 SkBits2Float(dat[i].pt[3].fX), SkBits2Float(dat[i].pt[3].fY));
131 #else
132 SkPath path;
133 SkRect rect;
134 rect.setWH(100, 100);
135 SkMatrix matrix;
136 SkScalar vals[9];
137 vals[0] = linearToLoop(3000, 0, 1);
138 vals[1] = linearToLoop(4000, .25, 1.25);
139 vals[2] = 200;
140 vals[3] = linearToLoop(5000, .5, 1.5);
141 vals[4] = linearToLoop(7000, .75, 1.75);
142 vals[5] = 300;
143 vals[6] = 0;
144 vals[7] = 0;
145 vals[8] = 1;
146 matrix.set9(vals);
147 SkPoint pts[4], pp[7];
148 matrix.mapRectToQuad(pts, rect);
149 std::swap(pts[1], pts[2]);
150 bool split;
151 path = cusp(pts, pp, split, 8000, .125);
152 auto debugOutCubic = [](const SkPoint* pts) {
153 return false; // comment out to capture stream of cusp'd cubics in stdout
154 SkDebugf("{{");
155 for (int i = 0; i < 4; ++i) {
156 SkDebugf("{0x%08x,0x%08x},", SkFloat2Bits(pts[i].fX), SkFloat2Bits(pts[i].fY));
157 }
158 SkDebugf("}},\n");
159 };
160 if (split) {
161 debugOutCubic(&pp[0]);
162 debugOutCubic(&pp[4]);
163 } else {
164 debugOutCubic(&pts[0]);
165 }
166 #endif
167 canvas->drawPath(path, p);
168 // draw time to make it easier to guess when the bad cubic was drawn
169 std::string timeStr = std::to_string((float) (curTime - start) / 1000.f);
Mike Reed89126e42019-01-03 12:59:14 -0500170 canvas->drawSimpleText(timeStr.c_str(), timeStr.size(), kUTF8_SkTextEncoding, 20, 20, SkFont(), SkPaint());
Cary Clarkdb160012018-08-31 15:07:51 -0400171 SkDebugf("");
172 }
173
174 bool onAnimate(const SkAnimTimer& timer) override {
175 curTime = timer.msec();
176 if (!start) {
177 start = curTime;
178 }
179 return true;
180 }
181
182private:
183
184 typedef Sample INHERITED;
185};
186
187DEF_SAMPLE( return new CuspView(); )