• 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/GrTexture.h"
12 #include "include/gpu/vk/GrVkTypes.h"
13 #include "src/core/SkLRUCache.h"
14 #include "src/gpu/GrSamplerState.h"
15 #include "src/gpu/vk/GrVkImage.h"
16 
17 class GrVkDescriptorSet;
18 class GrVkGpu;
19 class GrVkImageView;
20 struct GrVkImageInfo;
21 
22 class GrVkTexture : public GrTexture, public virtual GrVkImage {
23 public:
24     static sk_sp<GrVkTexture> MakeNewTexture(GrVkGpu*,
25                                              SkBudgeted budgeted,
26                                              SkISize dimensions,
27                                              const GrVkImage::ImageDesc&,
28                                              GrMipMapsStatus);
29 
30     static sk_sp<GrVkTexture> MakeWrappedTexture(GrVkGpu*,
31                                                  SkISize dimensions,
32                                                  GrWrapOwnership,
33                                                  GrWrapCacheable,
34                                                  GrIOType,
35                                                  const GrVkImageInfo&,
36                                                  sk_sp<GrVkImageLayout>);
37 
38     ~GrVkTexture() override;
39 
40     GrBackendTexture getBackendTexture() const override;
41 
backendFormat()42     GrBackendFormat backendFormat() const override { return this->getBackendFormat(); }
43 
textureParamsModified()44     void textureParamsModified() override {}
45 
46     const GrVkImageView* textureView();
47 
48     void addIdleProc(sk_sp<GrRefCntedCallback>, IdleState) override;
49     void callIdleProcsOnBehalfOfResource();
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                 const GrVkImageInfo&,
64                 sk_sp<GrVkImageLayout>,
65                 const GrVkImageView*,
66                 GrMipMapsStatus,
67                 GrBackendObjectOwnership);
68 
69     GrVkGpu* getVkGpu() const;
70 
71     void onAbandon() override;
72     void onRelease() override;
73 
onStealBackendTexture(GrBackendTexture *,SkImage::BackendTextureReleaseProc *)74     bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override {
75         return false;
76     }
77 
78     void willRemoveLastRef() override;
79 
80 private:
81     GrVkTexture(GrVkGpu*, SkBudgeted, SkISize, const GrVkImageInfo&, sk_sp<GrVkImageLayout> layout,
82                 const GrVkImageView* imageView, GrMipMapsStatus);
83     GrVkTexture(GrVkGpu*, SkISize, const GrVkImageInfo&, sk_sp<GrVkImageLayout>,
84                 const GrVkImageView*, GrMipMapsStatus, GrBackendObjectOwnership, GrWrapCacheable,
85                 GrIOType, bool isExternal);
86 
87     // In Vulkan we call the release proc after we are finished with the underlying
88     // GrVkImage::Resource object (which occurs after the GPU has finished all work on it).
onSetRelease(sk_sp<GrRefCntedCallback> releaseHelper)89     void onSetRelease(sk_sp<GrRefCntedCallback> releaseHelper) override {
90         // Forward the release proc on to GrVkImage
91         this->setResourceRelease(std::move(releaseHelper));
92     }
93 
94     void removeFinishIdleProcs();
95 
96     const GrVkImageView* fTextureView;
97 
98     struct SamplerHash {
operatorSamplerHash99         uint32_t operator()(GrSamplerState state) const {
100             return GrSamplerState::GenerateKey(state);
101         }
102     };
103     struct DescriptorCacheEntry;
104     SkLRUCache<const GrSamplerState, std::unique_ptr<DescriptorCacheEntry>, SamplerHash>
105             fDescSetCache;
106     static constexpr int kMaxCachedDescSets = 8;
107 
108     typedef GrTexture INHERITED;
109 };
110 
111 #endif
112