jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 8 | #ifndef GrSmallPathRenderer_DEFINED |
| 9 | #define GrSmallPathRenderer_DEFINED |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 10 | |
Brian Salomon | 903da79 | 2016-12-16 14:24:46 -0500 | [diff] [blame] | 11 | #include "GrDrawOpAtlas.h" |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 12 | #include "GrPathRenderer.h" |
| 13 | #include "GrRect.h" |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 14 | #include "GrShape.h" |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 15 | |
mtklein | 4e97607 | 2016-08-08 09:06:27 -0700 | [diff] [blame] | 16 | #include "SkOpts.h" |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 17 | #include "SkTDynamicHash.h" |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 18 | |
| 19 | class GrContext; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 20 | |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 21 | class GrSmallPathRenderer : public GrPathRenderer { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 22 | public: |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 23 | GrSmallPathRenderer(); |
| 24 | virtual ~GrSmallPathRenderer(); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 25 | |
| 26 | private: |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 27 | StencilSupport onGetStencilSupport(const GrShape&) const override { |
robertphillips | e7d4b2f | 2015-08-13 07:57:10 -0700 | [diff] [blame] | 28 | return GrPathRenderer::kNoSupport_StencilSupport; |
| 29 | } |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 30 | |
| 31 | bool onCanDrawPath(const CanDrawPathArgs&) const override; |
| 32 | |
| 33 | bool onDrawPath(const DrawPathArgs&) override; |
| 34 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 35 | struct ShapeData { |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 36 | class Key { |
| 37 | public: |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 38 | Key() {} |
| 39 | Key(const Key& that) { *this = that; } |
| 40 | Key(const GrShape& shape, uint32_t dim) { this->set(shape, dim); } |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 41 | Key(const GrShape& shape, const SkMatrix& ctm) { this->set(shape, ctm); } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 42 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 43 | 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; |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 47 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 48 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 49 | 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 Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 60 | 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 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 91 | 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 | |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 99 | private: |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 100 | // 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. |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 103 | SkAutoSTArray<24, uint32_t> fKey; |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 104 | }; |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 105 | Key fKey; |
| 106 | GrDrawOpAtlas::AtlasID fID; |
Jim Van Verth | 7704754 | 2017-01-11 14:17:00 -0500 | [diff] [blame] | 107 | SkRect fBounds; |
| 108 | SkScalar fScale; |
| 109 | SkVector fTranslate; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 110 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(ShapeData); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 111 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 112 | static inline const Key& GetKey(const ShapeData& data) { |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 113 | return data.fKey; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 114 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 115 | |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 116 | static inline uint32_t Hash(Key key) { |
mtklein | 4e97607 | 2016-08-08 09:06:27 -0700 | [diff] [blame] | 117 | return SkOpts::hash(key.data(), sizeof(uint32_t) * key.count32()); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 118 | } |
| 119 | }; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 120 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 121 | static void HandleEviction(GrDrawOpAtlas::AtlasID, void*); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 122 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 123 | typedef SkTDynamicHash<ShapeData, ShapeData::Key> ShapeCache; |
| 124 | typedef SkTInternalLList<ShapeData> ShapeDataList; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 125 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 126 | std::unique_ptr<GrDrawOpAtlas> fAtlas; |
Ben Wagner | 594f9ed | 2016-11-08 14:13:39 -0500 | [diff] [blame] | 127 | ShapeCache fShapeCache; |
| 128 | ShapeDataList fShapeList; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 129 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 130 | typedef GrPathRenderer INHERITED; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 131 | |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 132 | friend class SmallPathOp; |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 133 | friend struct PathTestStruct; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | #endif |