blob: ba752d0cff32949a0f5002a6fbb7d4597a5f4fb0 [file] [log] [blame]
jvanverthfa38a302014-10-06 05:59:05 -07001/*
2 * Copyright 2014 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
Jim Van Verth83010462017-03-16 08:45:39 -04008#ifndef GrSmallPathRenderer_DEFINED
9#define GrSmallPathRenderer_DEFINED
jvanverthfa38a302014-10-06 05:59:05 -070010
Brian Salomon903da792016-12-16 14:24:46 -050011#include "GrDrawOpAtlas.h"
jvanverthfa38a302014-10-06 05:59:05 -070012#include "GrPathRenderer.h"
13#include "GrRect.h"
bsalomonee432412016-06-27 07:18:18 -070014#include "GrShape.h"
jvanverthfa38a302014-10-06 05:59:05 -070015
mtklein4e976072016-08-08 09:06:27 -070016#include "SkOpts.h"
bsalomon71cb0c22014-11-14 12:10:14 -080017#include "SkTDynamicHash.h"
jvanverthfa38a302014-10-06 05:59:05 -070018
19class GrContext;
jvanverthfa38a302014-10-06 05:59:05 -070020
Jim Van Verth83010462017-03-16 08:45:39 -040021class GrSmallPathRenderer : public GrPathRenderer {
jvanverthfa38a302014-10-06 05:59:05 -070022public:
Jim Van Verth83010462017-03-16 08:45:39 -040023 GrSmallPathRenderer();
24 virtual ~GrSmallPathRenderer();
jvanverthfa38a302014-10-06 05:59:05 -070025
26private:
bsalomon8acedde2016-06-24 10:42:16 -070027 StencilSupport onGetStencilSupport(const GrShape&) const override {
robertphillipse7d4b2f2015-08-13 07:57:10 -070028 return GrPathRenderer::kNoSupport_StencilSupport;
29 }
bsalomon0aff2fa2015-07-31 06:48:27 -070030
31 bool onCanDrawPath(const CanDrawPathArgs&) const override;
32
33 bool onDrawPath(const DrawPathArgs&) override;
34
bsalomonee432412016-06-27 07:18:18 -070035 struct ShapeData {
jvanverth512e4372015-11-23 11:50:02 -080036 class Key {
37 public:
bsalomonee432412016-06-27 07:18:18 -070038 Key() {}
39 Key(const Key& that) { *this = that; }
40 Key(const GrShape& shape, uint32_t dim) { this->set(shape, dim); }
Jim Van Verth33632d82017-02-28 10:24:39 -050041 Key(const GrShape& shape, const SkMatrix& ctm) { this->set(shape, ctm); }
halcanary9d524f22016-03-29 09:03:52 -070042
bsalomonee432412016-06-27 07:18:18 -070043 Key& operator=(const Key& that) {
44 fKey.reset(that.fKey.count());
45 memcpy(fKey.get(), that.fKey.get(), fKey.count() * sizeof(uint32_t));
46 return *this;
jvanverth512e4372015-11-23 11:50:02 -080047 }
halcanary9d524f22016-03-29 09:03:52 -070048
bsalomonee432412016-06-27 07:18:18 -070049 void set(const GrShape& shape, uint32_t dim) {
50 // Shapes' keys are for their pre-style geometry, but by now we shouldn't have any
51 // relevant styling information.
52 SkASSERT(shape.style().isSimpleFill());
53 SkASSERT(shape.hasUnstyledKey());
54 int shapeKeySize = shape.unstyledKeySize();
55 fKey.reset(1 + shapeKeySize);
56 fKey[0] = dim;
57 shape.writeUnstyledKey(&fKey[1]);
58 }
59
Jim Van Verth33632d82017-02-28 10:24:39 -050060 void set(const GrShape& shape, const SkMatrix& ctm) {
61 GrUniqueKey maskKey;
62 struct KeyData {
63 SkScalar fFractionalTranslateX;
64 SkScalar fFractionalTranslateY;
65 };
66
67 // Shapes' keys are for their pre-style geometry, but by now we shouldn't have any
68 // relevant styling information.
69 SkASSERT(shape.style().isSimpleFill());
70 SkASSERT(shape.hasUnstyledKey());
71 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
72 SkScalar sx = ctm.get(SkMatrix::kMScaleX);
73 SkScalar sy = ctm.get(SkMatrix::kMScaleY);
74 SkScalar kx = ctm.get(SkMatrix::kMSkewX);
75 SkScalar ky = ctm.get(SkMatrix::kMSkewY);
76 SkScalar tx = ctm.get(SkMatrix::kMTransX);
77 SkScalar ty = ctm.get(SkMatrix::kMTransY);
78 // Allow 8 bits each in x and y of subpixel positioning.
79 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
80 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
81 int shapeKeySize = shape.unstyledKeySize();
82 fKey.reset(5 + shapeKeySize);
83 fKey[0] = SkFloat2Bits(sx);
84 fKey[1] = SkFloat2Bits(sy);
85 fKey[2] = SkFloat2Bits(kx);
86 fKey[3] = SkFloat2Bits(ky);
87 fKey[4] = fracX | (fracY >> 8);
88 shape.writeUnstyledKey(&fKey[5]);
89 }
90
bsalomonee432412016-06-27 07:18:18 -070091 bool operator==(const Key& that) const {
92 return fKey.count() == that.fKey.count() &&
93 0 == memcmp(fKey.get(), that.fKey.get(), sizeof(uint32_t) * fKey.count());
94 }
95
96 int count32() const { return fKey.count(); }
97 const uint32_t* data() const { return fKey.get(); }
98
jvanverth512e4372015-11-23 11:50:02 -080099 private:
Jim Van Verth33632d82017-02-28 10:24:39 -0500100 // The key is composed of the GrShape's key, and either the dimensions of the DF
101 // generated for the path (32x32 max, 64x64 max, 128x128 max) if an SDF image or
102 // the matrix for the path with only fractional translation.
bsalomonee432412016-06-27 07:18:18 -0700103 SkAutoSTArray<24, uint32_t> fKey;
jvanverthb61283f2014-10-30 05:57:21 -0700104 };
Brian Salomon2ee084e2016-12-16 18:59:19 -0500105 Key fKey;
106 GrDrawOpAtlas::AtlasID fID;
Jim Van Verth77047542017-01-11 14:17:00 -0500107 SkRect fBounds;
108 SkScalar fScale;
109 SkVector fTranslate;
bsalomonee432412016-06-27 07:18:18 -0700110 SK_DECLARE_INTERNAL_LLIST_INTERFACE(ShapeData);
halcanary9d524f22016-03-29 09:03:52 -0700111
bsalomonee432412016-06-27 07:18:18 -0700112 static inline const Key& GetKey(const ShapeData& data) {
jvanverthb61283f2014-10-30 05:57:21 -0700113 return data.fKey;
jvanverthfa38a302014-10-06 05:59:05 -0700114 }
halcanary9d524f22016-03-29 09:03:52 -0700115
jvanverthb61283f2014-10-30 05:57:21 -0700116 static inline uint32_t Hash(Key key) {
mtklein4e976072016-08-08 09:06:27 -0700117 return SkOpts::hash(key.data(), sizeof(uint32_t) * key.count32());
jvanverthfa38a302014-10-06 05:59:05 -0700118 }
119 };
joshualitt5bf99f12015-03-13 11:47:42 -0700120
Brian Salomon2ee084e2016-12-16 18:59:19 -0500121 static void HandleEviction(GrDrawOpAtlas::AtlasID, void*);
joshualitt5bf99f12015-03-13 11:47:42 -0700122
bsalomonee432412016-06-27 07:18:18 -0700123 typedef SkTDynamicHash<ShapeData, ShapeData::Key> ShapeCache;
124 typedef SkTInternalLList<ShapeData> ShapeDataList;
halcanary9d524f22016-03-29 09:03:52 -0700125
Brian Salomon2ee084e2016-12-16 18:59:19 -0500126 std::unique_ptr<GrDrawOpAtlas> fAtlas;
Ben Wagner594f9ed2016-11-08 14:13:39 -0500127 ShapeCache fShapeCache;
128 ShapeDataList fShapeList;
halcanary9d524f22016-03-29 09:03:52 -0700129
jvanverthfa38a302014-10-06 05:59:05 -0700130 typedef GrPathRenderer INHERITED;
joshualitt5bf99f12015-03-13 11:47:42 -0700131
Jim Van Verth83010462017-03-16 08:45:39 -0400132 friend class SmallPathOp;
joshualitt21279c72015-05-11 07:21:37 -0700133 friend struct PathTestStruct;
jvanverthfa38a302014-10-06 05:59:05 -0700134};
135
136#endif