1 /* 2 * Copyright 2015 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 GrVkTexture_DEFINED 9 #define GrVkTexture_DEFINED 10 11 #include "include/gpu/vk/GrVkTypes.h" 12 #include "src/core/SkLRUCache.h" 13 #include "src/gpu/ganesh/GrSamplerState.h" 14 #include "src/gpu/ganesh/GrTexture.h" 15 #include "src/gpu/ganesh/vk/GrVkImage.h" 16 17 class GrVkDescriptorSet; 18 class GrVkGpu; 19 class GrVkImageView; 20 struct GrVkImageInfo; 21 22 class GrVkTexture : public GrTexture { 23 public: 24 static sk_sp<GrVkTexture> MakeNewTexture(GrVkGpu*, 25 skgpu::Budgeted budgeted, 26 SkISize dimensions, 27 VkFormat format, 28 uint32_t mipLevels, 29 GrProtected, 30 GrMipmapStatus, 31 std::string_view label); 32 33 static sk_sp<GrVkTexture> MakeWrappedTexture(GrVkGpu*, 34 SkISize dimensions, 35 GrWrapOwnership, 36 GrWrapCacheable, 37 GrIOType, 38 const GrVkImageInfo&, 39 sk_sp<skgpu::MutableTextureStateRef>); 40 41 ~GrVkTexture() override; 42 43 GrBackendTexture getBackendTexture() const override; 44 backendFormat()45 GrBackendFormat backendFormat() const override { return fTexture->backendFormat(); } 46 textureParamsModified()47 void textureParamsModified() override {} 48 textureImage()49 GrVkImage* textureImage() const { return fTexture.get(); } 50 const GrVkImageView* textureView(); 51 52 // For each GrVkTexture, there is a cache of GrVkDescriptorSets which only contain a single 53 // texture/sampler descriptor. If there is a cached descriptor set that matches the passed in 54 // GrSamplerState, then a pointer to it is returned. The ref count is not incremented on the 55 // returned pointer, thus the caller must call ref it if they wish to keep ownership of the 56 // GrVkDescriptorSet. 57 const GrVkDescriptorSet* cachedSingleDescSet(GrSamplerState); 58 59 void addDescriptorSetToCache(const GrVkDescriptorSet*, GrSamplerState); 60 61 protected: 62 GrVkTexture(GrVkGpu*, 63 SkISize dimensions, 64 sk_sp<GrVkImage> texture, 65 GrMipmapStatus, 66 std::string_view label); 67 68 GrVkGpu* getVkGpu() const; 69 70 void onAbandon() override; 71 void onRelease() override; 72 onStealBackendTexture(GrBackendTexture *,SkImage::BackendTextureReleaseProc *)73 bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override { 74 return false; 75 } 76 77 // In Vulkan we call the release proc after we are finished with the underlying 78 // GrVkImage::Resource object (which occurs after the GPU has finished all work on it). onSetRelease(sk_sp<RefCntedReleaseProc> releaseHelper)79 void onSetRelease(sk_sp<RefCntedReleaseProc> releaseHelper) override { 80 // Forward the release proc onto the fTexture's GrVkImage 81 fTexture->setResourceRelease(std::move(releaseHelper)); 82 } 83 84 private: 85 GrVkTexture(GrVkGpu*, 86 skgpu::Budgeted, 87 SkISize, 88 sk_sp<GrVkImage> texture, 89 GrMipmapStatus, 90 std::string_view label); 91 GrVkTexture(GrVkGpu*, SkISize, sk_sp<GrVkImage> texture, GrMipmapStatus, 92 GrWrapCacheable, 93 GrIOType, 94 bool isExternal, 95 std::string_view label); 96 onSetLabel()97 void onSetLabel() override{} 98 99 sk_sp<GrVkImage> fTexture; 100 101 struct SamplerHash { operatorSamplerHash102 uint32_t operator()(GrSamplerState state) const { 103 // In VK the max aniso value is specified in addition to min/mag/mip filters and the 104 // driver is encouraged to consider the other filter settings when doing aniso. 105 return state.asKey(/*anisoIsOrthogonal=*/true); 106 } 107 }; 108 struct DescriptorCacheEntry; 109 SkLRUCache<const GrSamplerState, std::unique_ptr<DescriptorCacheEntry>, SamplerHash> 110 fDescSetCache; 111 static constexpr int kMaxCachedDescSets = 8; 112 }; 113 114 #endif 115