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