pathmaskcache.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  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. */
  7. #include "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkMatrix.h"
  10. #include "include/core/SkPaint.h"
  11. #include "include/core/SkPath.h"
  12. #include "include/core/SkRect.h"
  13. #include "include/core/SkScalar.h"
  14. #include "include/core/SkSize.h"
  15. #include "include/core/SkString.h"
  16. #include "include/gpu/GrContextOptions.h"
  17. #include "include/private/GrTypesPriv.h"
  18. #include "include/private/SkTArray.h"
  19. /** This tests the GPU backend's caching of path coverage masks */
  20. class PathMaskCache : public skiagm::GM {
  21. public:
  22. PathMaskCache() {}
  23. protected:
  24. SkString onShortName() override { return SkString("path_mask_cache"); }
  25. SkISize onISize() override {
  26. return SkISize::Make(650, 950);
  27. }
  28. void onDraw(SkCanvas* canvas) override {
  29. static constexpr SkScalar kPad = 5.f;
  30. SkPaint paint;
  31. paint.setAntiAlias(true);
  32. auto drawPathSet = [canvas] (const SkPath& path, const SkMatrix& m) {
  33. SkPaint paint;
  34. paint.setAntiAlias(true);
  35. SkRect bounds = path.getBounds();
  36. m.mapRect(&bounds);
  37. bounds.roundOut();
  38. canvas->save();
  39. canvas->translate(-bounds.fLeft, -bounds.fTop);
  40. canvas->save();
  41. canvas->concat(m);
  42. canvas->drawPath(path, paint);
  43. canvas->restore();
  44. // translate by integer
  45. canvas->translate(bounds.width() + kPad, 0.f);
  46. canvas->save();
  47. canvas->concat(m);
  48. canvas->drawPath(path, paint);
  49. canvas->restore();
  50. // translate by non-integer
  51. canvas->translate(bounds.width() + kPad + 0.15f, 0.f);
  52. canvas->save();
  53. canvas->concat(m);
  54. canvas->drawPath(path, paint);
  55. canvas->restore();
  56. // translate again so total translate fraction is almost identical to previous.
  57. canvas->translate(bounds.width() + kPad + 0.002f, 0.f);
  58. canvas->save();
  59. canvas->concat(m);
  60. canvas->drawPath(path, paint);
  61. canvas->restore();
  62. canvas->restore();
  63. return bounds.fBottom + kPad;
  64. };
  65. SkTArray<SkPath> paths;
  66. paths.push_back();
  67. paths.back().moveTo(0.f, 0.f);
  68. paths.back().lineTo(98.f, 100.f);
  69. paths.back().lineTo(100.f, 100.f);
  70. paths.back().conicTo(150.f, 50.f, 100.f, 0.f, 0.6f);
  71. paths.back().conicTo(148.f, 50.f, 100.f, 100.f, 0.6f);
  72. paths.back().conicTo(50.f, 30.f, 0.f, 100.f, 0.9f);
  73. paths.push_back();
  74. paths.back().addCircle(30.f, 30.f, 30.f);
  75. paths.back().addRect(SkRect::MakeXYWH(45.f, 45.f, 50.f, 60.f));
  76. paths.back().setFillType(SkPath::kEvenOdd_FillType);
  77. canvas->translate(kPad, kPad);
  78. for (const SkPath& path : paths) {
  79. SkScalar ty = drawPathSet(path, SkMatrix::I());
  80. canvas->translate(0, ty);
  81. // Non-uniform scale.
  82. SkMatrix s;
  83. s.setScale(0.5f, 2.f);
  84. ty = drawPathSet(path, s);
  85. canvas->translate(0.f, ty);
  86. // Rotation
  87. SkMatrix r;
  88. r.setRotate(60.f, path.getBounds().centerX(), path.getBounds().centerY());
  89. ty = drawPathSet(path, r);
  90. canvas->translate(0.f, ty);
  91. }
  92. }
  93. void modifyGrContextOptions(GrContextOptions* options) override {
  94. options->fGpuPathRenderers = GpuPathRenderers::kNone;
  95. options->fAllowPathMaskCaching = true;
  96. }
  97. private:
  98. typedef GM INHERITED;
  99. };
  100. DEF_GM( return new PathMaskCache(); )