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