Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 GrDynamicAtlas_DEFINED |
| 9 | #define GrDynamicAtlas_DEFINED |
| 10 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 11 | #include "src/core/SkArenaAlloc.h" |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 12 | #include "src/gpu/GrTextureProxy.h" |
| 13 | |
| 14 | class GrOnFlushResourceProvider; |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 15 | class GrResourceProvider; |
| 16 | struct SkIPoint16; |
| 17 | struct SkIRect; |
| 18 | |
| 19 | /** |
| 20 | * This class implements a dynamic size GrRectanizer that grows until it reaches the implementation- |
| 21 | * dependent max texture size. When finalized, it also creates and stores a GrTextureProxy for the |
| 22 | * underlying atlas. |
| 23 | */ |
| 24 | class GrDynamicAtlas { |
| 25 | public: |
| 26 | // As long as GrSurfaceOrigin exists, we just have to decide on one for the atlas texture. |
| 27 | static constexpr GrSurfaceOrigin kTextureOrigin = kTopLeft_GrSurfaceOrigin; |
| 28 | static constexpr int kPadding = 1; // Amount of padding below and to the right of each path. |
| 29 | |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 30 | using LazyAtlasDesc = GrSurfaceProxy::LazySurfaceDesc; |
| 31 | using LazyInstantiateAtlasCallback = GrSurfaceProxy::LazyInstantiateCallback; |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 32 | |
| 33 | enum class InternalMultisample : bool { |
| 34 | kNo = false, |
| 35 | kYes = true |
| 36 | }; |
| 37 | |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 38 | static sk_sp<GrTextureProxy> MakeLazyAtlasProxy(LazyInstantiateAtlasCallback&&, |
| 39 | GrColorType colorType, |
| 40 | InternalMultisample, |
| 41 | const GrCaps&, |
| 42 | GrSurfaceProxy::UseAllocator); |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 43 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 44 | enum class RectanizerAlgorithm { |
| 45 | kSkyline, |
| 46 | kPow2 |
| 47 | }; |
| 48 | |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 49 | GrDynamicAtlas(GrColorType colorType, InternalMultisample, SkISize initialSize, |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 50 | int maxAtlasSize, const GrCaps&, |
| 51 | RectanizerAlgorithm = RectanizerAlgorithm::kSkyline); |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 52 | virtual ~GrDynamicAtlas(); |
| 53 | |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 54 | void reset(SkISize initialSize, const GrCaps& desc); |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 55 | |
Chris Dalton | 83420eb | 2021-06-23 18:47:09 -0600 | [diff] [blame] | 56 | GrColorType colorType() const { return fColorType; } |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 57 | int maxAtlasSize() const { return fMaxAtlasSize; } |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 58 | GrTextureProxy* textureProxy() const { return fTextureProxy.get(); } |
Chris Dalton | 83420eb | 2021-06-23 18:47:09 -0600 | [diff] [blame] | 59 | GrSurfaceProxyView readView(const GrCaps&) const; |
| 60 | GrSurfaceProxyView writeView(const GrCaps&) const; |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 61 | bool isInstantiated() const { return fTextureProxy->isInstantiated(); } |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 62 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 63 | // Attempts to add a rect to the atlas. Returns true if successful, along with the rect's |
| 64 | // top-left location in the atlas. |
| 65 | bool addRect(int width, int height, SkIPoint16* location); |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 66 | const SkISize& drawBounds() { return fDrawBounds; } |
| 67 | |
Chris Dalton | 83420eb | 2021-06-23 18:47:09 -0600 | [diff] [blame] | 68 | // Instantiates our texture proxy for the atlas. After this call, it is no longer valid to call |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 69 | // addRect(), setUserBatchID(), or this method again. |
| 70 | // |
| 71 | // 'backingTexture', if provided, is a renderable texture with which to instantiate our proxy. |
| 72 | // If null then we will create a texture using the resource provider. The purpose of this param |
Chris Dalton | 83420eb | 2021-06-23 18:47:09 -0600 | [diff] [blame] | 73 | // is to provide a guaranteed way to recycle textures from previous atlases. |
| 74 | void instantiate(GrOnFlushResourceProvider*, sk_sp<GrTexture> backingTexture = nullptr); |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 75 | |
| 76 | private: |
| 77 | class Node; |
| 78 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 79 | Node* makeNode(Node* previous, int l, int t, int r, int b); |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 80 | bool internalPlaceRect(int w, int h, SkIPoint16* loc); |
| 81 | |
| 82 | const GrColorType fColorType; |
| 83 | const InternalMultisample fInternalMultisample; |
| 84 | const int fMaxAtlasSize; |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 85 | const RectanizerAlgorithm fRectanizerAlgorithm; |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 86 | int fWidth; |
| 87 | int fHeight; |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 88 | SkISize fDrawBounds; |
| 89 | |
Herb Derby | 6e2c56f | 2020-08-01 16:26:04 -0400 | [diff] [blame] | 90 | SkSTArenaAllocWithReset<512> fNodeAllocator; |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 91 | Node* fTopNode = nullptr; |
| 92 | |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 93 | sk_sp<GrTextureProxy> fTextureProxy; |
| 94 | sk_sp<GrTexture> fBackingTexture; |
| 95 | }; |
| 96 | |
| 97 | #endif |