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 SK_API 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 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const override; 78 79 #ifdef SKIA_DFX_FOR_RECORD_VKIMAGE 80 void updateNodeId(uint64_t nodeId) override; 81 #endif 82 83 protected: 84 GrVkTexture(GrVkGpu*, 85 SkISize dimensions, 86 sk_sp<GrVkImage> texture, 87 GrMipmapStatus, 88 std::string_view label); 89 90 GrVkGpu* getVkGpu() const; 91 92 void onAbandon() override; 93 void onRelease() override; 94 onStealBackendTexture(GrBackendTexture *,SkImages::BackendTextureReleaseProc *)95 bool onStealBackendTexture(GrBackendTexture*, SkImages::BackendTextureReleaseProc*) override { 96 return false; 97 } 98 99 // In Vulkan we call the release proc after we are finished with the underlying 100 // GrVkImage::Resource object (which occurs after the GPU has finished all work on it). onSetRelease(sk_sp<RefCntedReleaseProc> releaseHelper)101 void onSetRelease(sk_sp<RefCntedReleaseProc> releaseHelper) override { 102 // Forward the release proc onto the fTexture's GrVkImage 103 fTexture->setResourceRelease(std::move(releaseHelper)); 104 } 105 106 private: 107 GrVkTexture(GrVkGpu*, 108 skgpu::Budgeted, 109 SkISize, 110 sk_sp<GrVkImage> texture, 111 GrMipmapStatus, 112 std::string_view label); 113 GrVkTexture(GrVkGpu*, SkISize, sk_sp<GrVkImage> texture, GrMipmapStatus, 114 GrWrapCacheable, 115 GrIOType, 116 bool isExternal, 117 std::string_view label); 118 onSetLabel()119 void onSetLabel() override{} 120 121 sk_sp<GrVkImage> fTexture; 122 123 struct SamplerHash { operatorSamplerHash124 uint32_t operator()(GrSamplerState state) const { 125 // In VK the max aniso value is specified in addition to min/mag/mip filters and the 126 // driver is encouraged to consider the other filter settings when doing aniso. 127 return state.asKey(/*anisoIsOrthogonal=*/true); 128 } 129 }; 130 struct DescriptorCacheEntry; 131 SkLRUCache<const GrSamplerState, std::unique_ptr<DescriptorCacheEntry>, SamplerHash> 132 fDescSetCache; 133 static constexpr int kMaxCachedDescSets = 8; 134 }; 135 136 #endif 137