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 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 11 | #include "GrSamplerState.h" |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 12 | #include "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 | eee4d6e | 2017-06-05 09:26:07 -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 |
| 60 | if (fTarget && fUniqueKey.isValid()) { |
| 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 |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 86 | friend class GrSurfaceProxy; // for ctors |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 87 | friend class GrProxyProvider; // for ctors |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 88 | friend class GrTextureProxyPriv; |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 89 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 90 | // Deferred version - when constructed with data the origin is always kTopLeft. |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 91 | GrTextureProxy(const GrBackendFormat&, const GrSurfaceDesc& srcDesc, GrMipMapped, SkBackingFit, |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 92 | SkBudgeted, const void* srcData, size_t srcRowBytes, GrInternalSurfaceFlags); |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 93 | |
| 94 | // Deferred version - no data. |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 95 | GrTextureProxy(const GrBackendFormat&, const GrSurfaceDesc& srcDesc, GrSurfaceOrigin, |
| 96 | GrMipMapped, SkBackingFit, SkBudgeted, GrInternalSurfaceFlags); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 97 | |
| 98 | // Lazy-callback version |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 99 | // There are two main use cases for lazily-instantiated proxies: |
| 100 | // basic knowledge - width, height, config, origin are known |
| 101 | // minimal knowledge - only config is known. |
| 102 | // |
| 103 | // The basic knowledge version is used for DDL where we know the type of proxy we are going to |
| 104 | // use, but we don't have access to the GPU yet to instantiate it. |
| 105 | // |
| 106 | // The minimal knowledge version is used for CCPR where we are generating an atlas but we do not |
| 107 | // know the final size until flush time. |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 108 | GrTextureProxy(LazyInstantiateCallback&&, LazyInstantiationType, const GrBackendFormat&, |
| 109 | const GrSurfaceDesc& desc, GrSurfaceOrigin, GrMipMapped, SkBackingFit, |
| 110 | SkBudgeted, GrInternalSurfaceFlags); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 111 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 112 | // Wrapped version |
Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 113 | GrTextureProxy(sk_sp<GrSurface>, GrSurfaceOrigin); |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 114 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 115 | ~GrTextureProxy() override; |
| 116 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 117 | sk_sp<GrSurface> createSurface(GrResourceProvider*) const override; |
| 118 | |
Robert Phillips | 84a8120 | 2016-11-04 11:59:10 -0400 | [diff] [blame] | 119 | private: |
Brian Osman | 086679b | 2018-09-10 16:26:51 -0400 | [diff] [blame] | 120 | // WARNING: Be careful when adding or removing fields here. ASAN is likely to trigger warnings |
| 121 | // when instantiating GrTextureRenderTargetProxy. The std::function in GrSurfaceProxy makes |
| 122 | // each class in the diamond require 16 byte alignment. Clang appears to layout the fields for |
| 123 | // each class to achieve the necessary alignment. However, ASAN checks the alignment of 'this' |
| 124 | // in the constructors, and always looks for the full 16 byte alignment, even if the fields in |
| 125 | // that particular class don't require it. Changing the size of this object can move the start |
| 126 | // address of other types, leading to this problem. |
| 127 | |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 128 | GrMipMapped fMipMapped; |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 129 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 130 | GrUniqueKey fUniqueKey; |
| 131 | GrProxyProvider* fProxyProvider; // only set when fUniqueKey is valid |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 132 | |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 133 | // Only used for proxies whose contents are being prepared on a worker thread. This object |
| 134 | // stores the texture data, allowing the proxy to remain uninstantiated until flush. At that |
| 135 | // point, the proxy is instantiated, and this data is used to perform an ASAP upload. |
| 136 | std::unique_ptr<GrDeferredProxyUploader> fDeferredUploader; |
| 137 | |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 138 | size_t onUninstantiatedGpuMemorySize() const override; |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 139 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 140 | // Methods made available via GrTextureProxy::CacheAccess |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 141 | void setUniqueKey(GrProxyProvider*, const GrUniqueKey&); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 142 | void clearUniqueKey(); |
| 143 | |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 144 | SkDEBUGCODE(void onValidateSurface(const GrSurface*) override;) |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 145 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 146 | // For wrapped proxies the GrTexture pointer is stored in GrIORefProxy. |
Robert Phillips | a4c41b3 | 2017-03-15 13:02:45 -0400 | [diff] [blame] | 147 | // 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] | 148 | // the deferred resource |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 149 | |
| 150 | typedef GrSurfaceProxy INHERITED; |
| 151 | }; |
| 152 | |
| 153 | #endif |