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/GrSamplerState.h" 14 #include "src/gpu/GrTexture.h" 15 #include "src/gpu/vk/GrVkAttachment.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 SkBudgeted budgeted, 26 SkISize dimensions, 27 VkFormat format, 28 uint32_t mipLevels, 29 GrProtected, 30 GrMipmapStatus); 31 32 static sk_sp<GrVkTexture> MakeWrappedTexture(GrVkGpu*, 33 SkISize dimensions, 34 GrWrapOwnership, 35 GrWrapCacheable, 36 GrIOType, 37 const GrVkImageInfo&, 38 sk_sp<GrBackendSurfaceMutableStateImpl>); 39 40 ~GrVkTexture() override; 41 42 GrBackendTexture getBackendTexture() const override; 43 backendFormat()44 GrBackendFormat backendFormat() const override { return fTexture->getBackendFormat(); } 45 textureParamsModified()46 void textureParamsModified() override {} 47 textureAttachment()48 GrVkAttachment* textureAttachment() const { return fTexture.get(); } 49 const GrVkImageView* textureView(); 50 51 // For each GrVkTexture, there is a cache of GrVkDescriptorSets which only contain a single 52 // texture/sampler descriptor. If there is a cached descriptor set that matches the passed in 53 // GrSamplerState, then a pointer to it is returned. The ref count is not incremented on the 54 // returned pointer, thus the caller must call ref it if they wish to keep ownership of the 55 // GrVkDescriptorSet. 56 const GrVkDescriptorSet* cachedSingleDescSet(GrSamplerState); 57 58 void addDescriptorSetToCache(const GrVkDescriptorSet*, GrSamplerState); 59 60 protected: 61 GrVkTexture(GrVkGpu*, 62 SkISize dimensions, 63 sk_sp<GrVkAttachment> texture, 64 GrMipmapStatus); 65 66 GrVkGpu* getVkGpu() const; 67 68 void onAbandon() override; 69 void onRelease() override; 70 onStealBackendTexture(GrBackendTexture *,SkImage::BackendTextureReleaseProc *)71 bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override { 72 return false; 73 } 74 75 // In Vulkan we call the release proc after we are finished with the underlying 76 // GrVkImage::Resource object (which occurs after the GPU has finished all work on it). onSetRelease(sk_sp<GrRefCntedCallback> releaseHelper)77 void onSetRelease(sk_sp<GrRefCntedCallback> releaseHelper) override { 78 // Forward the release proc onto the fTexture's GrVkImage 79 fTexture->setResourceRelease(std::move(releaseHelper)); 80 } 81 82 private: 83 GrVkTexture(GrVkGpu*, SkBudgeted, SkISize, sk_sp<GrVkAttachment> texture, GrMipmapStatus); 84 GrVkTexture(GrVkGpu*, SkISize, sk_sp<GrVkAttachment> texture, GrMipmapStatus, 85 GrWrapCacheable, GrIOType, bool isExternal); 86 87 sk_sp<GrVkAttachment> fTexture; 88 89 struct SamplerHash { operatorSamplerHash90 uint32_t operator()(GrSamplerState state) const { return state.asIndex(); } 91 }; 92 struct DescriptorCacheEntry; 93 SkLRUCache<const GrSamplerState, std::unique_ptr<DescriptorCacheEntry>, SamplerHash> 94 fDescSetCache; 95 static constexpr int kMaxCachedDescSets = 8; 96 }; 97 98 #endif 99