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