1 /* 2 * Copyright 2021 Google LLC 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 skgpu_graphite_TextureProxy_DEFINED 9 #define skgpu_graphite_TextureProxy_DEFINED 10 11 #include "include/core/SkRefCnt.h" 12 #include "include/core/SkSize.h" 13 #include "include/gpu/graphite/TextureInfo.h" 14 #include "include/private/base/SkTo.h" 15 16 #include <functional> 17 18 enum SkColorType : int; 19 20 namespace skgpu::graphite { 21 22 class Caps; 23 class Recorder; 24 class ResourceProvider; 25 class ScratchResourceManager; 26 class Texture; 27 28 class TextureProxy : public SkRefCnt { 29 public: 30 TextureProxy() = delete; 31 32 ~TextureProxy() override; 33 numSamples()34 int numSamples() const { return fInfo.numSamples(); } mipmapped()35 Mipmapped mipmapped() const { return fInfo.mipmapped(); } 36 37 SkISize dimensions() const; textureInfo()38 const TextureInfo& textureInfo() const { return fInfo; } 39 label()40 const char* label() const { return fLabel.c_str(); } 41 42 bool isLazy() const; 43 bool isFullyLazy() const; 44 bool isVolatile() const; 45 isProtected()46 Protected isProtected() const { return fInfo.isProtected(); } 47 48 size_t uninstantiatedGpuMemorySize() const; 49 50 bool instantiate(ResourceProvider*); 51 /* 52 * We currently only instantiate lazy proxies at insertion-time. Snap-time 'instantiate' 53 * calls should be wrapped in 'InstantiateIfNotLazy'. 54 * 55 * Unlike Ganesh, in Graphite we do not update the proxy's dimensions with the instantiating 56 * texture's dimensions. This means that when a fully-lazy proxy is instantiated and 57 * deinstantiated, it goes back to being fully-lazy and without dimensions, and can be 58 * re-instantiated with a new texture with different dimensions than the first. 59 */ 60 bool lazyInstantiate(ResourceProvider*); 61 /* 62 * For Lazy proxies this will return true. Otherwise, it will return the result of 63 * calling instantiate on the texture proxy. 64 * 65 * DEPRECATED: Eventually all un-instantiated non-lazy proxies should use the 66 * ScratchResourceManager function instead of the ResourceProvider directly. 67 */ 68 static bool InstantiateIfNotLazy(ResourceProvider*, TextureProxy*); 69 70 /* 71 * Instantiate any scratch proxy (not already instantiated and not lazy) by using a texture 72 * from the ScratchResourceManager. When possible, this will be a texture that has been returned 73 * for reuse by a prior task. Lazy proxies and already instantiated proxies will return true. 74 * 75 * False is returned if instantiation fails. 76 */ 77 static bool InstantiateIfNotLazy(ScratchResourceManager*, TextureProxy*); 78 isInstantiated()79 bool isInstantiated() const { return SkToBool(fTexture); } 80 void deinstantiate(); 81 sk_sp<Texture> refTexture() const; 82 const Texture* texture() const; texture()83 Texture* texture() { return fTexture.get(); } 84 85 // Make() will immediately instantiate non-budgeted proxies. 86 static sk_sp<TextureProxy> Make(const Caps*, 87 ResourceProvider*, 88 SkISize dimensions, 89 const TextureInfo&, 90 std::string_view label, 91 skgpu::Budgeted); 92 93 using LazyInstantiateCallback = std::function<sk_sp<Texture> (ResourceProvider*)>; 94 95 static sk_sp<TextureProxy> MakeLazy(const Caps*, 96 SkISize dimensions, 97 const TextureInfo&, 98 skgpu::Budgeted, 99 Volatile, 100 LazyInstantiateCallback&&); 101 static sk_sp<TextureProxy> MakeFullyLazy(const TextureInfo&, 102 skgpu::Budgeted, 103 Volatile, 104 LazyInstantiateCallback&&); 105 106 static sk_sp<TextureProxy> Wrap(sk_sp<Texture>); 107 108 private: 109 TextureProxy(SkISize dimensions, 110 const TextureInfo& info, 111 std::string_view label, 112 skgpu::Budgeted budgeted); 113 TextureProxy(SkISize dimensions, 114 const TextureInfo&, 115 skgpu::Budgeted, 116 Volatile, 117 LazyInstantiateCallback&&); 118 TextureProxy(sk_sp<Texture>); 119 120 #ifdef SK_DEBUG 121 void validateTexture(const Texture*); 122 #endif 123 124 // In the following, 'fVolatile' and 'fLazyInstantiateCallback' can be accessed from 125 // multiple threads so need to remain immutable. 126 SkISize fDimensions; 127 const TextureInfo fInfo; 128 129 // String used to describe the current use of this TextureProxy. It will be set on its 130 // Texture object when the proxy gets instantiated. 131 std::string fLabel; 132 133 skgpu::Budgeted fBudgeted; 134 const Volatile fVolatile; 135 136 sk_sp<Texture> fTexture; 137 138 const LazyInstantiateCallback fLazyInstantiateCallback; 139 }; 140 141 // Volatile texture proxy that deinstantiates itself on destruction. 142 class AutoDeinstantiateTextureProxy { 143 public: AutoDeinstantiateTextureProxy(TextureProxy * textureProxy)144 AutoDeinstantiateTextureProxy(TextureProxy* textureProxy) : fTextureProxy(textureProxy) {} 145 ~AutoDeinstantiateTextureProxy()146 ~AutoDeinstantiateTextureProxy() { 147 if (fTextureProxy) { 148 fTextureProxy->deinstantiate(); 149 } 150 } 151 152 private: 153 TextureProxy* const fTextureProxy; 154 }; 155 156 } // namespace skgpu::graphite 157 158 #endif // skgpu_graphite_TextureProxy_DEFINED 159