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 enum class Renderable : bool; 24 class ResourceProvider; 25 class Texture; 26 27 class TextureProxy : public SkRefCnt { 28 public: 29 TextureProxy(SkISize dimensions, const TextureInfo& info, skgpu::Budgeted budgeted); 30 TextureProxy(sk_sp<Texture>); 31 32 TextureProxy() = delete; 33 34 ~TextureProxy() override; 35 numSamples()36 int numSamples() const { return fInfo.numSamples(); } mipmapped()37 Mipmapped mipmapped() const { return fInfo.mipmapped(); } 38 39 SkISize dimensions() const; textureInfo()40 const TextureInfo& textureInfo() const { return fInfo; } 41 42 bool isLazy() const; 43 bool isFullyLazy() const; 44 bool isVolatile() const; 45 46 bool instantiate(ResourceProvider*); 47 /* 48 * We currently only instantiate lazy proxies at insertion-time. Snap-time 'instantiate' 49 * calls should be wrapped in 'InstantiateIfNotLazy'. 50 * 51 * Unlike Ganesh, in Graphite we do not update the proxy's dimensions with the instantiating 52 * texture's dimensions. This means that when a fully-lazy proxy is instantiated and 53 * deinstantiated, it goes back to being fully-lazy and without dimensions, and can be 54 * re-instantiated with a new texture with different dimensions than the first. 55 */ 56 bool lazyInstantiate(ResourceProvider*); 57 /* 58 * For Lazy proxies this will return true. Otherwise, it will return the result of 59 * calling instantiate on the texture proxy. 60 */ 61 static bool InstantiateIfNotLazy(ResourceProvider*, TextureProxy*); isInstantiated()62 bool isInstantiated() const { return SkToBool(fTexture); } 63 void deinstantiate(); 64 sk_sp<Texture> refTexture() const; 65 const Texture* texture() const; 66 67 static sk_sp<TextureProxy> Make(const Caps*, 68 SkISize dimensions, 69 SkColorType, 70 Mipmapped, 71 Protected, 72 Renderable, 73 skgpu::Budgeted); 74 75 using LazyInstantiateCallback = std::function<sk_sp<Texture> (ResourceProvider*)>; 76 77 static sk_sp<TextureProxy> MakeLazy(SkISize dimensions, 78 const TextureInfo&, 79 skgpu::Budgeted, 80 Volatile, 81 LazyInstantiateCallback&&); 82 static sk_sp<TextureProxy> MakeFullyLazy(const TextureInfo&, 83 skgpu::Budgeted, 84 Volatile, 85 LazyInstantiateCallback&&); 86 87 private: 88 TextureProxy(SkISize dimensions, 89 const TextureInfo&, 90 skgpu::Budgeted, 91 Volatile, 92 LazyInstantiateCallback&&); 93 94 #ifdef SK_DEBUG 95 void validateTexture(const Texture*); 96 #endif 97 98 // In the following, 'fVolatile' and 'fLazyInstantiateCallback' can be accessed from 99 // multiple threads so need to remain immutable. 100 SkISize fDimensions; 101 const TextureInfo fInfo; 102 103 skgpu::Budgeted fBudgeted; 104 const Volatile fVolatile; 105 106 sk_sp<Texture> fTexture; 107 108 const LazyInstantiateCallback fLazyInstantiateCallback; 109 }; 110 111 // Volatile texture proxy that deinstantiates itself on destruction. 112 class AutoDeinstantiateTextureProxy { 113 public: AutoDeinstantiateTextureProxy(TextureProxy * textureProxy)114 AutoDeinstantiateTextureProxy(TextureProxy* textureProxy) : fTextureProxy(textureProxy) {} 115 ~AutoDeinstantiateTextureProxy()116 ~AutoDeinstantiateTextureProxy() { 117 if (fTextureProxy) { 118 fTextureProxy->deinstantiate(); 119 } 120 } 121 122 private: 123 TextureProxy* const fTextureProxy; 124 }; 125 126 } // namepsace skgpu::graphite 127 128 #endif // skgpu_graphite_TextureProxy_DEFINED 129