Debian Codegraph @

gm/pathmaskcache.cpp

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