• 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/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/GrVkImage.h"
16 
17 class GrVkDescriptorSet;
18 class GrVkGpu;
19 class GrVkImageView;
20 struct GrVkImageInfo;
21 
22 class SK_API 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->backendFormat(); }
45 
textureParamsModified()46     void textureParamsModified() override {}
47 
textureImage()48     GrVkImage* textureImage() 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     void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const override;
61 
62 protected:
63     GrVkTexture(GrVkGpu*,
64                 SkISize dimensions,
65                 sk_sp<GrVkImage> texture,
66                 GrMipmapStatus);
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<GrRefCntedCallback> releaseHelper)79     void onSetRelease(sk_sp<GrRefCntedCallback> 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*, SkBudgeted, SkISize, sk_sp<GrVkImage> texture, GrMipmapStatus);
86     GrVkTexture(GrVkGpu*, SkISize, sk_sp<GrVkImage> texture, GrMipmapStatus,
87                 GrWrapCacheable, GrIOType, bool isExternal);
88 
89     sk_sp<GrVkImage> fTexture;
90 
91     struct SamplerHash {
operatorSamplerHash92         uint32_t operator()(GrSamplerState state) const { return state.asIndex(); }
93     };
94     struct DescriptorCacheEntry;
95     SkLRUCache<const GrSamplerState, std::unique_ptr<DescriptorCacheEntry>, SamplerHash>
96             fDescSetCache;
97     static constexpr int kMaxCachedDescSets = 8;
98 };
99 
100 #endif
101