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" |
| 12 | #include "GrTypes.h" |
| 13 | #include "GrTypesPriv.h" |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 14 | #include "SkRefCnt.h" |
| 15 | #include "SkSize.h" |
| 16 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 17 | class GrOnFlushResourceProvider; |
| 18 | class GrRenderTargetContext; |
| 19 | class GrTextureProxy; |
| 20 | struct SkIPoint16; |
| 21 | |
| 22 | /** |
| 23 | * This class implements a dynamic size GrRectanizer that grows until it reaches the implementation- |
| 24 | * dependent max texture size. When finalized, it also creates and stores a GrTextureProxy for the |
| 25 | * underlying atlas. |
| 26 | */ |
| 27 | class GrCCAtlas { |
| 28 | public: |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 29 | // As long as GrSurfaceOrigin exists, we just have to decide on one for the atlas texture. |
| 30 | static constexpr GrSurfaceOrigin kTextureOrigin = kTopLeft_GrSurfaceOrigin; |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 31 | 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] | 32 | |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 33 | // This struct encapsulates the minimum and desired requirements for an atlas, as well as an |
| 34 | // approximate number of pixels to help select a good initial size. |
| 35 | struct Specs { |
| 36 | int fMaxPreferredTextureSize = 0; |
| 37 | int fMinTextureSize = 0; |
| 38 | int fMinWidth = 0; // If there are 100 20x10 paths, this should be 20. |
| 39 | int fMinHeight = 0; // If there are 100 20x10 paths, this should be 10. |
| 40 | int fApproxNumPixels = 0; |
| 41 | |
| 42 | // Add space for a rect in the desired atlas specs. |
| 43 | void accountForSpace(int width, int height); |
| 44 | }; |
| 45 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame^] | 46 | GrCCAtlas(GrPixelConfig, const Specs&, const GrCaps&); |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 47 | ~GrCCAtlas(); |
| 48 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame^] | 49 | GrTextureProxy* textureProxy() const { return fTextureProxy.get(); } |
| 50 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 51 | // Attempts to add a rect to the atlas. If successful, returns the integer offset from |
| 52 | // device-space pixels where the path will be drawn, to atlas pixels where its mask resides. |
| 53 | bool addRect(const SkIRect& devIBounds, SkIVector* atlasOffset); |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 54 | const SkISize& drawBounds() { return fDrawBounds; } |
| 55 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 56 | // This is an optional space for the caller to jot down which user-defined batch to use when |
| 57 | // they render the content of this atlas. |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame^] | 58 | void setUserBatchID(int id); |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 59 | int getUserBatchID() const { return fUserBatchID; } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 60 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame^] | 61 | // Instantiates our texture proxy for the atlas and returns a pre-cleared GrRenderTargetContext |
| 62 | // that the caller may use to render the content. After this call, it is no longer valid to call |
| 63 | // addRect(), setUserBatchID(), or this method again. |
| 64 | sk_sp<GrRenderTargetContext> makeRenderTargetContext(GrOnFlushResourceProvider*); |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 65 | |
| 66 | private: |
| 67 | class Node; |
| 68 | |
| 69 | bool internalPlaceRect(int w, int h, SkIPoint16* loc); |
| 70 | |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 71 | const int fMaxTextureSize; |
Chris Dalton | 2612bae | 2018-02-22 13:41:37 -0700 | [diff] [blame] | 72 | int fWidth, fHeight; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 73 | std::unique_ptr<Node> fTopNode; |
Chris Dalton | 2612bae | 2018-02-22 13:41:37 -0700 | [diff] [blame] | 74 | SkISize fDrawBounds = {0, 0}; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 75 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 76 | int fUserBatchID; |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 77 | sk_sp<GrTextureProxy> fTextureProxy; |
| 78 | }; |
| 79 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 80 | /** |
| 81 | * This class implements an unbounded stack of atlases. When the current atlas reaches the |
| 82 | * implementation-dependent max texture size, a new one is pushed to the back and we continue on. |
| 83 | */ |
| 84 | class GrCCAtlasStack { |
| 85 | public: |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame^] | 86 | GrCCAtlasStack(GrPixelConfig pixelConfig, const GrCCAtlas::Specs& specs, const GrCaps* caps) |
| 87 | : fPixelConfig(pixelConfig), fSpecs(specs), fCaps(caps) {} |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 88 | |
| 89 | bool empty() const { return fAtlases.empty(); } |
| 90 | const GrCCAtlas& front() const { SkASSERT(!this->empty()); return fAtlases.front(); } |
| 91 | GrCCAtlas& front() { SkASSERT(!this->empty()); return fAtlases.front(); } |
| 92 | GrCCAtlas& current() { SkASSERT(!this->empty()); return fAtlases.back(); } |
| 93 | |
| 94 | class Iter { |
| 95 | public: |
| 96 | Iter(GrCCAtlasStack& stack) : fImpl(&stack.fAtlases) {} |
| 97 | bool next() { return fImpl.next(); } |
| 98 | GrCCAtlas* operator->() const { return fImpl.get(); } |
| 99 | private: |
| 100 | typename GrTAllocator<GrCCAtlas>::Iter fImpl; |
| 101 | }; |
| 102 | |
| 103 | // Adds a rect to the current atlas and returns the offset from device space to atlas space. |
| 104 | // Call current() to get the atlas it was added to. |
| 105 | // |
| 106 | // If the return value is non-null, it means the given rect did not fit in the then-current |
| 107 | // atlas, so it was retired and a new one was added to the stack. The return value is the |
| 108 | // newly-retired atlas. The caller should call setUserBatchID() on the retired atlas before |
| 109 | // moving on. |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame^] | 110 | GrCCAtlas* addRect(const SkIRect& devIBounds, SkIVector* devToAtlasOffset); |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 111 | |
| 112 | private: |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame^] | 113 | const GrPixelConfig fPixelConfig; |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 114 | const GrCCAtlas::Specs fSpecs; |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame^] | 115 | const GrCaps* const fCaps; |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 116 | GrSTAllocator<4, GrCCAtlas> fAtlases; |
| 117 | }; |
| 118 | |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 119 | inline void GrCCAtlas::Specs::accountForSpace(int width, int height) { |
| 120 | fMinWidth = SkTMax(width, fMinWidth); |
| 121 | fMinHeight = SkTMax(height, fMinHeight); |
| 122 | fApproxNumPixels += (width + kPadding) * (height + kPadding); |
| 123 | } |
| 124 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 125 | #endif |