robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 GrTextureProxy_DEFINED |
| 9 | #define GrTextureProxy_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/gpu/GrSamplerState.h" |
| 12 | #include "include/private/GrSurfaceProxy.h" |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 13 | |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 14 | class GrCaps; |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 15 | class GrDeferredProxyUploader; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 16 | class GrProxyProvider; |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 17 | class GrResourceProvider; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 18 | class GrTextureOpList; |
Robert Phillips | 420c4cf | 2017-09-28 09:00:45 -0400 | [diff] [blame] | 19 | class GrTextureProxyPriv; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 20 | |
| 21 | // This class delays the acquisition of textures until they are actually required |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 22 | class GrTextureProxy : virtual public GrSurfaceProxy { |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 23 | public: |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 24 | GrTextureProxy* asTextureProxy() override { return this; } |
| 25 | const GrTextureProxy* asTextureProxy() const override { return this; } |
| 26 | |
| 27 | // Actually instantiate the backing texture, if necessary |
Robert Phillips | 10d1721 | 2019-04-24 14:09:10 -0400 | [diff] [blame] | 28 | bool instantiate(GrResourceProvider*) override; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 29 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 30 | GrSamplerState::Filter highestFilterMode() const; |
Robert Phillips | 49081d1 | 2017-05-08 13:41:35 -0400 | [diff] [blame] | 31 | |
Greg Daniel | 3b2ebbb | 2018-02-09 10:49:23 -0500 | [diff] [blame] | 32 | // If we are instantiated and have a target, return the mip state of that target. Otherwise |
| 33 | // returns the proxy's mip state from creation time. This is useful for lazy proxies which may |
| 34 | // claim to not need mips at creation time, but the instantiation happens to give us a mipped |
| 35 | // target. In that case we should use that for our benefit to avoid possible copies/mip |
| 36 | // generation later. |
| 37 | GrMipMapped mipMapped() const; |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 38 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 39 | // Returns the GrMipMapped value of the proxy from creation time regardless of whether it has |
| 40 | // been instantiated or not. |
| 41 | GrMipMapped proxyMipMapped() const { return fMipMapped; } |
| 42 | |
Brian Salomon | 7da2fc7 | 2018-12-10 13:40:07 -0500 | [diff] [blame] | 43 | GrTextureType textureType() const { return this->backendFormat().textureType(); } |
| 44 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 45 | /** If true then the texture does not support MIP maps and only supports clamp wrap mode. */ |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 46 | bool hasRestrictedSampling() const { |
| 47 | return GrTextureTypeHasRestrictedSampling(this->textureType()); |
| 48 | } |
Greg Daniel | 45723ac | 2018-11-30 10:12:43 -0500 | [diff] [blame] | 49 | |
| 50 | // Returns true if the passed in proxies can be used as dynamic state together when flushing |
| 51 | // draws to the gpu. |
| 52 | static bool ProxiesAreCompatibleAsDynamicState(const GrTextureProxy* first, |
| 53 | const GrTextureProxy* second); |
| 54 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 55 | /** |
| 56 | * Return the texture proxy's unique key. It will be invalid if the proxy doesn't have one. |
| 57 | */ |
| 58 | const GrUniqueKey& getUniqueKey() const { |
| 59 | #ifdef SK_DEBUG |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 60 | if (fTarget && fUniqueKey.isValid() && fSyncTargetKey) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 61 | SkASSERT(fTarget->getUniqueKey().isValid()); |
| 62 | // It is possible for a non-keyed proxy to have a uniquely keyed resource assigned to |
| 63 | // it. This just means that a future user of the resource will be filling it with unique |
| 64 | // data. However, if the proxy has a unique key its attached resource should also |
| 65 | // have that key. |
| 66 | SkASSERT(fUniqueKey == fTarget->getUniqueKey()); |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | return fUniqueKey; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Internal-only helper class used for manipulations of the resource by the cache. |
| 75 | */ |
| 76 | class CacheAccess; |
| 77 | inline CacheAccess cacheAccess(); |
| 78 | inline const CacheAccess cacheAccess() const; |
| 79 | |
Robert Phillips | 420c4cf | 2017-09-28 09:00:45 -0400 | [diff] [blame] | 80 | // Provides access to special purpose functions. |
| 81 | GrTextureProxyPriv texPriv(); |
| 82 | const GrTextureProxyPriv texPriv() const; |
| 83 | |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 84 | protected: |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 85 | // DDL TODO: rm the GrSurfaceProxy friending |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 86 | friend class GrSurfaceProxy; // for ctors |
| 87 | friend class GrProxyProvider; // for ctors |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 88 | friend class GrTextureProxyPriv; |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 89 | friend class GrSurfaceProxyPriv; // ability to change key sync state after lazy instantiation. |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 90 | |
Brian Salomon | 3ec1f54 | 2019-06-17 17:54:57 +0000 | [diff] [blame^] | 91 | // Deferred version - when constructed with data the origin is always kTopLeft. |
| 92 | GrTextureProxy(const GrBackendFormat&, const GrSurfaceDesc& srcDesc, GrMipMapped, SkBackingFit, |
| 93 | SkBudgeted, const void* srcData, size_t srcRowBytes, GrInternalSurfaceFlags); |
| 94 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 95 | // Deferred version - no data. |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 96 | GrTextureProxy(const GrBackendFormat&, const GrSurfaceDesc& srcDesc, GrSurfaceOrigin, |
Brian Salomon | 3ec1f54 | 2019-06-17 17:54:57 +0000 | [diff] [blame^] | 97 | GrMipMapped, SkBackingFit, SkBudgeted, GrInternalSurfaceFlags); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 98 | |
| 99 | // Lazy-callback version |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 100 | // There are two main use cases for lazily-instantiated proxies: |
| 101 | // basic knowledge - width, height, config, origin are known |
| 102 | // minimal knowledge - only config is known. |
| 103 | // |
| 104 | // The basic knowledge version is used for DDL where we know the type of proxy we are going to |
| 105 | // use, but we don't have access to the GPU yet to instantiate it. |
| 106 | // |
| 107 | // The minimal knowledge version is used for CCPR where we are generating an atlas but we do not |
| 108 | // know the final size until flush time. |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 109 | GrTextureProxy(LazyInstantiateCallback&&, LazyInstantiationType, const GrBackendFormat&, |
Brian Salomon | 3ec1f54 | 2019-06-17 17:54:57 +0000 | [diff] [blame^] | 110 | const GrSurfaceDesc& desc, GrSurfaceOrigin, GrMipMapped, SkBackingFit, |
| 111 | SkBudgeted, GrInternalSurfaceFlags); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 112 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 113 | // Wrapped version |
Brian Salomon | 3ec1f54 | 2019-06-17 17:54:57 +0000 | [diff] [blame^] | 114 | GrTextureProxy(sk_sp<GrSurface>, GrSurfaceOrigin); |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 115 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 116 | ~GrTextureProxy() override; |
| 117 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 118 | sk_sp<GrSurface> createSurface(GrResourceProvider*) const override; |
| 119 | |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 120 | void setTargetKeySync(bool sync) { fSyncTargetKey = sync; } |
| 121 | |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 122 | private: |
Brian Osman | 086679b | 2018-09-10 16:26:51 -0400 | [diff] [blame] | 123 | // WARNING: Be careful when adding or removing fields here. ASAN is likely to trigger warnings |
| 124 | // when instantiating GrTextureRenderTargetProxy. The std::function in GrSurfaceProxy makes |
| 125 | // each class in the diamond require 16 byte alignment. Clang appears to layout the fields for |
| 126 | // each class to achieve the necessary alignment. However, ASAN checks the alignment of 'this' |
| 127 | // in the constructors, and always looks for the full 16 byte alignment, even if the fields in |
| 128 | // that particular class don't require it. Changing the size of this object can move the start |
| 129 | // address of other types, leading to this problem. |
| 130 | |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 131 | GrMipMapped fMipMapped; |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 132 | bool fSyncTargetKey = true; // Should target's unique key be sync'ed with ours. |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 133 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 134 | GrUniqueKey fUniqueKey; |
| 135 | GrProxyProvider* fProxyProvider; // only set when fUniqueKey is valid |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 136 | |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 137 | // Only used for proxies whose contents are being prepared on a worker thread. This object |
| 138 | // stores the texture data, allowing the proxy to remain uninstantiated until flush. At that |
| 139 | // point, the proxy is instantiated, and this data is used to perform an ASAP upload. |
| 140 | std::unique_ptr<GrDeferredProxyUploader> fDeferredUploader; |
| 141 | |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 142 | size_t onUninstantiatedGpuMemorySize() const override; |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 143 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 144 | // Methods made available via GrTextureProxy::CacheAccess |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 145 | void setUniqueKey(GrProxyProvider*, const GrUniqueKey&); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 146 | void clearUniqueKey(); |
| 147 | |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 148 | SkDEBUGCODE(void onValidateSurface(const GrSurface*) override;) |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 149 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 150 | // For wrapped proxies the GrTexture pointer is stored in GrIORefProxy. |
Robert Phillips | a4c41b3 | 2017-03-15 13:02:45 -0400 | [diff] [blame] | 151 | // For deferred proxies that pointer will be filled in when we need to instantiate |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 152 | // the deferred resource |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 153 | |
| 154 | typedef GrSurfaceProxy INHERITED; |
| 155 | }; |
| 156 | |
| 157 | #endif |