1 /* 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 */ 7 8 #include <string> 9 #include "include/core/SkCanvas.h" 10 #include "include/core/SkFont.h" 11 #include "include/core/SkPath.h" 12 #include "samplecode/Sample.h" 13 #include "src/core/SkGeometry.h" 14 #include "tools/timer/TimeUtils.h" 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 28 SkMSec start = 0; 29 SkMSec curTime; 30 bool first = true; 31 32 // Create a path with one or two cubics, where one has a cusp. cusp(const SkPoint P[4],SkPoint PP[7],bool & split,int speed,SkScalar phase)33 static 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. linearToLoop(int speed,SkScalar phase,SkScalar scale)54 static 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 71 struct 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 100 size_t datCount = SK_ARRAY_COUNT(dat); 101 102 class CuspView : public Sample { 103 public: CuspView()104 CuspView() {} 105 protected: name()106 SkString name() override { return SkString("Cusp"); } 107 onDrawContent(SkCanvas * canvas)108 void onDrawContent(SkCanvas* canvas) override { 109 SkPaint p; 110 p.setAntiAlias(true); 111 p.setStyle(SkPaint::kStroke_Style); 112 p.setStrokeWidth(20); 113 #if 0 // enable to play through the stream above backwards. 114 SkPath path; 115 int i; 116 #if 0 // disable to draw only one problematic cubic 117 i = --datCount; 118 #else 119 i = 14; // index into dat of problematic cubic 120 #endif 121 path.moveTo( SkBits2Float(dat[i].pt[0].fX), SkBits2Float(dat[i].pt[0].fY)); 122 path.cubicTo(SkBits2Float(dat[i].pt[1].fX), SkBits2Float(dat[i].pt[1].fY), 123 SkBits2Float(dat[i].pt[2].fX), SkBits2Float(dat[i].pt[2].fY), 124 SkBits2Float(dat[i].pt[3].fX), SkBits2Float(dat[i].pt[3].fY)); 125 #else 126 SkPath path; 127 SkRect rect; 128 rect.setWH(100, 100); 129 SkMatrix matrix; 130 SkScalar vals[9]; 131 vals[0] = linearToLoop(3000, 0, 1); 132 vals[1] = linearToLoop(4000, .25, 1.25); 133 vals[2] = 200; 134 vals[3] = linearToLoop(5000, .5, 1.5); 135 vals[4] = linearToLoop(7000, .75, 1.75); 136 vals[5] = 300; 137 vals[6] = 0; 138 vals[7] = 0; 139 vals[8] = 1; 140 matrix.set9(vals); 141 SkPoint pts[4], pp[7]; 142 matrix.mapRectToQuad(pts, rect); 143 std::swap(pts[1], pts[2]); 144 bool split; 145 path = cusp(pts, pp, split, 8000, .125); 146 auto debugOutCubic = [](const SkPoint* pts) { 147 return false; // comment out to capture stream of cusp'd cubics in stdout 148 SkDebugf("{{"); 149 for (int i = 0; i < 4; ++i) { 150 SkDebugf("{0x%08x,0x%08x},", SkFloat2Bits(pts[i].fX), SkFloat2Bits(pts[i].fY)); 151 } 152 SkDebugf("}},\n"); 153 }; 154 if (split) { 155 debugOutCubic(&pp[0]); 156 debugOutCubic(&pp[4]); 157 } else { 158 debugOutCubic(&pts[0]); 159 } 160 #endif 161 canvas->drawPath(path, p); 162 // draw time to make it easier to guess when the bad cubic was drawn 163 std::string timeStr = std::to_string((float) (curTime - start) / 1000.f); 164 canvas->drawSimpleText(timeStr.c_str(), timeStr.size(), SkTextEncoding::kUTF8, 20, 20, SkFont(), SkPaint()); 165 SkDebugf(""); 166 } 167 onAnimate(double nanos)168 bool onAnimate(double nanos) override { 169 curTime = TimeUtils::NanosToMSec(nanos); 170 if (!start) { 171 start = curTime; 172 } 173 return true; 174 } 175 176 private: 177 178 typedef Sample INHERITED; 179 }; 180 181 DEF_SAMPLE( return new CuspView(); ) 182