Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | #ifndef GrCCAtlas_DEFINED |
| 9 | #define GrCCAtlas_DEFINED |
| 10 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 11 | #include "GrAllocator.h" |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame^] | 12 | #include "GrNonAtomicRef.h" |
| 13 | #include "GrResourceKey.h" |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 14 | #include "GrTypes.h" |
| 15 | #include "GrTypesPriv.h" |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 16 | #include "SkRefCnt.h" |
| 17 | #include "SkSize.h" |
| 18 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 19 | class GrOnFlushResourceProvider; |
| 20 | class GrRenderTargetContext; |
| 21 | class GrTextureProxy; |
| 22 | struct SkIPoint16; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame^] | 23 | struct SkIRect; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * This class implements a dynamic size GrRectanizer that grows until it reaches the implementation- |
| 27 | * dependent max texture size. When finalized, it also creates and stores a GrTextureProxy for the |
| 28 | * underlying atlas. |
| 29 | */ |
| 30 | class GrCCAtlas { |
| 31 | public: |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 32 | // As long as GrSurfaceOrigin exists, we just have to decide on one for the atlas texture. |
| 33 | static constexpr GrSurfaceOrigin kTextureOrigin = kTopLeft_GrSurfaceOrigin; |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 34 | static constexpr int kPadding = 1; // Amount of padding below and to the right of each path. |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 35 | |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 36 | // This struct encapsulates the minimum and desired requirements for an atlas, as well as an |
| 37 | // approximate number of pixels to help select a good initial size. |
| 38 | struct Specs { |
| 39 | int fMaxPreferredTextureSize = 0; |
| 40 | int fMinTextureSize = 0; |
| 41 | int fMinWidth = 0; // If there are 100 20x10 paths, this should be 20. |
| 42 | int fMinHeight = 0; // If there are 100 20x10 paths, this should be 10. |
| 43 | int fApproxNumPixels = 0; |
| 44 | |
| 45 | // Add space for a rect in the desired atlas specs. |
| 46 | void accountForSpace(int width, int height); |
| 47 | }; |
| 48 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 49 | GrCCAtlas(GrPixelConfig, const Specs&, const GrCaps&); |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 50 | ~GrCCAtlas(); |
| 51 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 52 | GrTextureProxy* textureProxy() const { return fTextureProxy.get(); } |
| 53 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 54 | // Attempts to add a rect to the atlas. If successful, returns the integer offset from |
| 55 | // device-space pixels where the path will be drawn, to atlas pixels where its mask resides. |
| 56 | bool addRect(const SkIRect& devIBounds, SkIVector* atlasOffset); |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 57 | const SkISize& drawBounds() { return fDrawBounds; } |
| 58 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 59 | // This is an optional space for the caller to jot down which user-defined batch to use when |
| 60 | // they render the content of this atlas. |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 61 | void setUserBatchID(int id); |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 62 | int getUserBatchID() const { return fUserBatchID; } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 63 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame^] | 64 | // Manages a unique resource cache key that gets assigned to the atlas texture. The unique key |
| 65 | // does not get assigned to the texture proxy until it is instantiated. |
| 66 | const GrUniqueKey& getOrAssignUniqueKey(GrOnFlushResourceProvider*); |
| 67 | const GrUniqueKey& uniqueKey() const { return fUniqueKey; } |
| 68 | |
| 69 | // An object for simple bookkeeping on the atlas texture once it has a unique key. In practice, |
| 70 | // we use it to track the percentage of the original atlas pixels that could still ever |
| 71 | // potentially be reused (i.e., those which still represent an extant path). When the percentage |
| 72 | // of useful pixels drops below 50%, the entire texture is purged from the resource cache. |
| 73 | struct CachedAtlasInfo : public GrNonAtomicRef<CachedAtlasInfo> { |
| 74 | int fNumPathPixels = 0; |
| 75 | int fNumInvalidatedPathPixels = 0; |
| 76 | bool fIsPurgedFromResourceCache = false; |
| 77 | }; |
| 78 | sk_sp<CachedAtlasInfo> refOrMakeCachedAtlasInfo(); |
| 79 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 80 | // Instantiates our texture proxy for the atlas and returns a pre-cleared GrRenderTargetContext |
| 81 | // that the caller may use to render the content. After this call, it is no longer valid to call |
| 82 | // addRect(), setUserBatchID(), or this method again. |
| 83 | sk_sp<GrRenderTargetContext> makeRenderTargetContext(GrOnFlushResourceProvider*); |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 84 | |
| 85 | private: |
| 86 | class Node; |
| 87 | |
| 88 | bool internalPlaceRect(int w, int h, SkIPoint16* loc); |
| 89 | |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 90 | const int fMaxTextureSize; |
Chris Dalton | 2612bae | 2018-02-22 13:41:37 -0700 | [diff] [blame] | 91 | int fWidth, fHeight; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 92 | std::unique_ptr<Node> fTopNode; |
Chris Dalton | 2612bae | 2018-02-22 13:41:37 -0700 | [diff] [blame] | 93 | SkISize fDrawBounds = {0, 0}; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 94 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 95 | int fUserBatchID; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame^] | 96 | |
| 97 | // Not every atlas will have a unique key -- a mainline CCPR one won't if we don't stash any |
| 98 | // paths, and only the first atlas in the stack is eligible to be stashed. |
| 99 | GrUniqueKey fUniqueKey; |
| 100 | |
| 101 | sk_sp<CachedAtlasInfo> fCachedAtlasInfo; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 102 | sk_sp<GrTextureProxy> fTextureProxy; |
| 103 | }; |
| 104 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 105 | /** |
| 106 | * This class implements an unbounded stack of atlases. When the current atlas reaches the |
| 107 | * implementation-dependent max texture size, a new one is pushed to the back and we continue on. |
| 108 | */ |
| 109 | class GrCCAtlasStack { |
| 110 | public: |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 111 | GrCCAtlasStack(GrPixelConfig pixelConfig, const GrCCAtlas::Specs& specs, const GrCaps* caps) |
| 112 | : fPixelConfig(pixelConfig), fSpecs(specs), fCaps(caps) {} |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 113 | |
| 114 | bool empty() const { return fAtlases.empty(); } |
| 115 | const GrCCAtlas& front() const { SkASSERT(!this->empty()); return fAtlases.front(); } |
| 116 | GrCCAtlas& front() { SkASSERT(!this->empty()); return fAtlases.front(); } |
| 117 | GrCCAtlas& current() { SkASSERT(!this->empty()); return fAtlases.back(); } |
| 118 | |
| 119 | class Iter { |
| 120 | public: |
| 121 | Iter(GrCCAtlasStack& stack) : fImpl(&stack.fAtlases) {} |
| 122 | bool next() { return fImpl.next(); } |
| 123 | GrCCAtlas* operator->() const { return fImpl.get(); } |
| 124 | private: |
| 125 | typename GrTAllocator<GrCCAtlas>::Iter fImpl; |
| 126 | }; |
| 127 | |
| 128 | // Adds a rect to the current atlas and returns the offset from device space to atlas space. |
| 129 | // Call current() to get the atlas it was added to. |
| 130 | // |
| 131 | // If the return value is non-null, it means the given rect did not fit in the then-current |
| 132 | // atlas, so it was retired and a new one was added to the stack. The return value is the |
| 133 | // newly-retired atlas. The caller should call setUserBatchID() on the retired atlas before |
| 134 | // moving on. |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 135 | GrCCAtlas* addRect(const SkIRect& devIBounds, SkIVector* devToAtlasOffset); |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 136 | |
| 137 | private: |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 138 | const GrPixelConfig fPixelConfig; |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 139 | const GrCCAtlas::Specs fSpecs; |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 140 | const GrCaps* const fCaps; |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 141 | GrSTAllocator<4, GrCCAtlas> fAtlases; |
| 142 | }; |
| 143 | |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 144 | inline void GrCCAtlas::Specs::accountForSpace(int width, int height) { |
| 145 | fMinWidth = SkTMax(width, fMinWidth); |
| 146 | fMinHeight = SkTMax(height, fMinHeight); |
| 147 | fApproxNumPixels += (width + kPadding) * (height + kPadding); |
| 148 | } |
| 149 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 150 | #endif |