conicpaths.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright 2013 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. #include "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkPaint.h"
  11. #include "include/core/SkPath.h"
  12. #include "include/core/SkPoint.h"
  13. #include "include/core/SkRect.h"
  14. #include "include/core/SkScalar.h"
  15. #include "include/core/SkSize.h"
  16. #include "include/core/SkString.h"
  17. #include "include/core/SkTypes.h"
  18. #include "include/private/SkFloatBits.h"
  19. #include "include/private/SkTArray.h"
  20. class ConicPathsGM : public skiagm::GM {
  21. protected:
  22. SkString onShortName() override {
  23. return SkString("conicpaths");
  24. }
  25. SkISize onISize() override {
  26. return SkISize::Make(920, 960);
  27. }
  28. void onOnceBeforeDraw() override {
  29. {
  30. const SkScalar w = SkScalarSqrt(2)/2;
  31. SkPath* conicCirlce = &fPaths.push_back();
  32. conicCirlce->moveTo(0, 0);
  33. conicCirlce->conicTo(0, 50, 50, 50, w);
  34. conicCirlce->rConicTo(50, 0, 50, -50, w);
  35. conicCirlce->rConicTo(0, -50, -50, -50, w);
  36. conicCirlce->rConicTo(-50, 0, -50, 50, w);
  37. }
  38. {
  39. SkPath* hyperbola = &fPaths.push_back();
  40. hyperbola->moveTo(0, 0);
  41. hyperbola->conicTo(0, 100, 100, 100, 2);
  42. }
  43. {
  44. SkPath* thinHyperbola = &fPaths.push_back();
  45. thinHyperbola->moveTo(0, 0);
  46. thinHyperbola->conicTo(100, 100, 5, 0, 2);
  47. }
  48. {
  49. SkPath* veryThinHyperbola = &fPaths.push_back();
  50. veryThinHyperbola->moveTo(0, 0);
  51. veryThinHyperbola->conicTo(100, 100, 1, 0, 2);
  52. }
  53. {
  54. SkPath* closedHyperbola = &fPaths.push_back();
  55. closedHyperbola->moveTo(0, 0);
  56. closedHyperbola->conicTo(100, 100, 0, 0, 2);
  57. }
  58. {
  59. // using 1 as weight defaults to using quadTo
  60. SkPath* nearParabola = &fPaths.push_back();
  61. nearParabola->moveTo(0, 0);
  62. nearParabola->conicTo(0, 100, 100, 100, 0.999f);
  63. }
  64. {
  65. SkPath* thinEllipse = &fPaths.push_back();
  66. thinEllipse->moveTo(0, 0);
  67. thinEllipse->conicTo(100, 100, 5, 0, SK_ScalarHalf);
  68. }
  69. {
  70. SkPath* veryThinEllipse = &fPaths.push_back();
  71. veryThinEllipse->moveTo(0, 0);
  72. veryThinEllipse->conicTo(100, 100, 1, 0, SK_ScalarHalf);
  73. }
  74. {
  75. SkPath* closedEllipse = &fPaths.push_back();
  76. closedEllipse->moveTo(0, 0);
  77. closedEllipse->conicTo(100, 100, 0, 0, SK_ScalarHalf);
  78. }
  79. {
  80. const SkScalar w = SkScalarSqrt(2)/2;
  81. fGiantCircle.moveTo(2.1e+11f, -1.05e+11f);
  82. fGiantCircle.conicTo(2.1e+11f, 0, 1.05e+11f, 0, w);
  83. fGiantCircle.conicTo(0, 0, 0, -1.05e+11f, w);
  84. fGiantCircle.conicTo(0, -2.1e+11f, 1.05e+11f, -2.1e+11f, w);
  85. fGiantCircle.conicTo(2.1e+11f, -2.1e+11f, 2.1e+11f, -1.05e+11f, w);
  86. }
  87. }
  88. void drawGiantCircle(SkCanvas* canvas) {
  89. SkPaint paint;
  90. canvas->drawPath(fGiantCircle, paint);
  91. }
  92. void onDraw(SkCanvas* canvas) override {
  93. const SkAlpha kAlphaValue[] = { 0xFF, 0x40 };
  94. const SkScalar margin = 15;
  95. canvas->translate(margin, margin);
  96. SkPaint paint;
  97. for (int p = 0; p < fPaths.count(); ++p) {
  98. canvas->save();
  99. for (size_t a = 0; a < SK_ARRAY_COUNT(kAlphaValue); ++a) {
  100. paint.setARGB(kAlphaValue[a], 0, 0, 0);
  101. for (int aa = 0; aa < 2; ++aa) {
  102. paint.setAntiAlias(SkToBool(aa));
  103. for (int fh = 0; fh < 2; ++fh) {
  104. paint.setStyle(fh ? SkPaint::kStroke_Style : SkPaint::kFill_Style);
  105. const SkRect& bounds = fPaths[p].getBounds();
  106. canvas->save();
  107. canvas->translate(-bounds.fLeft, -bounds.fTop);
  108. canvas->drawPath(fPaths[p], paint);
  109. canvas->restore();
  110. canvas->translate(110, 0);
  111. }
  112. }
  113. }
  114. canvas->restore();
  115. canvas->translate(0, 110);
  116. }
  117. canvas->restore();
  118. this->drawGiantCircle(canvas);
  119. }
  120. private:
  121. SkTArray<SkPath> fPaths;
  122. SkPath fGiantCircle;
  123. typedef skiagm::GM INHERITED;
  124. };
  125. DEF_GM(return new ConicPathsGM;)
  126. //////////////////////////////////////////////////////////////////////////////
  127. /* arc should be on top of circle */
  128. DEF_SIMPLE_GM(arccirclegap, canvas, 250, 250) {
  129. canvas->translate(50, 100);
  130. SkPoint c = { 1052.5390625f, 506.8760978034711f };
  131. SkScalar radius = 1096.702150363923f;
  132. SkPaint paint;
  133. paint.setAntiAlias(true);
  134. paint.setStyle(SkPaint::kStroke_Style);
  135. canvas->drawCircle(c, radius, paint);
  136. SkPath path;
  137. path.moveTo(288.88884710654133f, -280.26680862609f);
  138. path.arcTo(0, 0, -39.00216443306411f, 400.6058925796476f, radius);
  139. paint.setColor(0xff007f00);
  140. canvas->drawPath(path, paint);
  141. }
  142. /* circle should be antialiased */
  143. DEF_SIMPLE_GM(largecircle, canvas, 250, 250) {
  144. canvas->translate(50, 100);
  145. SkPoint c = { 1052.5390625f, 506.8760978034711f };
  146. SkScalar radius = 1096.702150363923f;
  147. SkPaint paint;
  148. paint.setAntiAlias(true);
  149. paint.setStyle(SkPaint::kStroke_Style);
  150. canvas->drawCircle(c, radius, paint);
  151. }
  152. /* ovals should not be blurry */
  153. DEF_SIMPLE_GM(largeovals, canvas, 250, 250) {
  154. // Test EllipseOp
  155. SkRect r = SkRect::MakeXYWH(-520, -520, 5000, 4000);
  156. SkPaint paint;
  157. paint.setAntiAlias(true);
  158. paint.setStyle(SkPaint::kStroke_Style);
  159. paint.setStrokeWidth(100);
  160. canvas->drawOval(r, paint);
  161. r.offset(-15, -15);
  162. paint.setColor(SK_ColorDKGRAY);
  163. // we use stroke and fill to avoid falling into the SimpleFill path
  164. paint.setStyle(SkPaint::kStrokeAndFill_Style);
  165. paint.setStrokeWidth(1);
  166. canvas->drawOval(r, paint);
  167. // Test DIEllipseOp
  168. canvas->rotate(1.0f);
  169. r.offset(55, 55);
  170. paint.setColor(SK_ColorGRAY);
  171. paint.setStyle(SkPaint::kStroke_Style);
  172. paint.setStrokeWidth(100);
  173. canvas->drawOval(r, paint);
  174. r.offset(-15, -15);
  175. paint.setColor(SK_ColorLTGRAY);
  176. paint.setStyle(SkPaint::kStrokeAndFill_Style);
  177. paint.setStrokeWidth(1);
  178. canvas->drawOval(r, paint);
  179. }
  180. DEF_SIMPLE_GM(crbug_640176, canvas, 250, 250) {
  181. SkPath path;
  182. path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
  183. path.lineTo(SkBits2Float(0x42cfd89a), SkBits2Float(0xc2700000)); // 103.923f, -60
  184. path.lineTo(SkBits2Float(0x42cfd899), SkBits2Float(0xc2700006)); // 103.923f, -60
  185. path.conicTo(SkBits2Float(0x42f00000), SkBits2Float(0xc2009d9c),
  186. SkBits2Float(0x42f00001), SkBits2Float(0x00000000),
  187. SkBits2Float(0x3f7746ea)); // 120, -32.1539f, 120, 0, 0.965926f
  188. SkPaint paint;
  189. paint.setAntiAlias(true);
  190. canvas->translate(125, 125);
  191. canvas->drawPath(path, paint);
  192. }