1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2015 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#ifndef GrVkTexture_DEFINED
9cb93a386Sopenharmony_ci#define GrVkTexture_DEFINED
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include "include/gpu/vk/GrVkTypes.h"
12cb93a386Sopenharmony_ci#include "src/core/SkLRUCache.h"
13cb93a386Sopenharmony_ci#include "src/gpu/GrSamplerState.h"
14cb93a386Sopenharmony_ci#include "src/gpu/GrTexture.h"
15cb93a386Sopenharmony_ci#include "src/gpu/vk/GrVkImage.h"
16cb93a386Sopenharmony_ci
17cb93a386Sopenharmony_ciclass GrVkDescriptorSet;
18cb93a386Sopenharmony_ciclass GrVkGpu;
19cb93a386Sopenharmony_ciclass GrVkImageView;
20cb93a386Sopenharmony_cistruct GrVkImageInfo;
21cb93a386Sopenharmony_ci
22cb93a386Sopenharmony_ciclass SK_API GrVkTexture : public GrTexture {
23cb93a386Sopenharmony_cipublic:
24cb93a386Sopenharmony_ci    static sk_sp<GrVkTexture> MakeNewTexture(GrVkGpu*,
25cb93a386Sopenharmony_ci                                             SkBudgeted budgeted,
26cb93a386Sopenharmony_ci                                             SkISize dimensions,
27cb93a386Sopenharmony_ci                                             VkFormat format,
28cb93a386Sopenharmony_ci                                             uint32_t mipLevels,
29cb93a386Sopenharmony_ci                                             GrProtected,
30cb93a386Sopenharmony_ci                                             GrMipmapStatus);
31cb93a386Sopenharmony_ci
32cb93a386Sopenharmony_ci    static sk_sp<GrVkTexture> MakeWrappedTexture(GrVkGpu*,
33cb93a386Sopenharmony_ci                                                 SkISize dimensions,
34cb93a386Sopenharmony_ci                                                 GrWrapOwnership,
35cb93a386Sopenharmony_ci                                                 GrWrapCacheable,
36cb93a386Sopenharmony_ci                                                 GrIOType,
37cb93a386Sopenharmony_ci                                                 const GrVkImageInfo&,
38cb93a386Sopenharmony_ci                                                 sk_sp<GrBackendSurfaceMutableStateImpl>);
39cb93a386Sopenharmony_ci
40cb93a386Sopenharmony_ci    ~GrVkTexture() override;
41cb93a386Sopenharmony_ci
42cb93a386Sopenharmony_ci    GrBackendTexture getBackendTexture() const override;
43cb93a386Sopenharmony_ci
44cb93a386Sopenharmony_ci    GrBackendFormat backendFormat() const override { return fTexture->backendFormat(); }
45cb93a386Sopenharmony_ci
46cb93a386Sopenharmony_ci    void textureParamsModified() override {}
47cb93a386Sopenharmony_ci
48cb93a386Sopenharmony_ci    GrVkImage* textureImage() const { return fTexture.get(); }
49cb93a386Sopenharmony_ci    const GrVkImageView* textureView();
50cb93a386Sopenharmony_ci
51cb93a386Sopenharmony_ci    // For each GrVkTexture, there is a cache of GrVkDescriptorSets which only contain a single
52cb93a386Sopenharmony_ci    // texture/sampler descriptor. If there is a cached descriptor set that matches the passed in
53cb93a386Sopenharmony_ci    // GrSamplerState, then a pointer to it is returned. The ref count is not incremented on the
54cb93a386Sopenharmony_ci    // returned pointer, thus the caller must call ref it if they wish to keep ownership of the
55cb93a386Sopenharmony_ci    // GrVkDescriptorSet.
56cb93a386Sopenharmony_ci    const GrVkDescriptorSet* cachedSingleDescSet(GrSamplerState);
57cb93a386Sopenharmony_ci
58cb93a386Sopenharmony_ci    void addDescriptorSetToCache(const GrVkDescriptorSet*, GrSamplerState);
59cb93a386Sopenharmony_ci
60cb93a386Sopenharmony_ci    void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const override;
61cb93a386Sopenharmony_ci
62cb93a386Sopenharmony_ciprotected:
63cb93a386Sopenharmony_ci    GrVkTexture(GrVkGpu*,
64cb93a386Sopenharmony_ci                SkISize dimensions,
65cb93a386Sopenharmony_ci                sk_sp<GrVkImage> texture,
66cb93a386Sopenharmony_ci                GrMipmapStatus);
67cb93a386Sopenharmony_ci
68cb93a386Sopenharmony_ci    GrVkGpu* getVkGpu() const;
69cb93a386Sopenharmony_ci
70cb93a386Sopenharmony_ci    void onAbandon() override;
71cb93a386Sopenharmony_ci    void onRelease() override;
72cb93a386Sopenharmony_ci
73cb93a386Sopenharmony_ci    bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override {
74cb93a386Sopenharmony_ci        return false;
75cb93a386Sopenharmony_ci    }
76cb93a386Sopenharmony_ci
77cb93a386Sopenharmony_ci    // In Vulkan we call the release proc after we are finished with the underlying
78cb93a386Sopenharmony_ci    // GrVkImage::Resource object (which occurs after the GPU has finished all work on it).
79cb93a386Sopenharmony_ci    void onSetRelease(sk_sp<GrRefCntedCallback> releaseHelper) override {
80cb93a386Sopenharmony_ci        // Forward the release proc onto the fTexture's GrVkImage
81cb93a386Sopenharmony_ci        fTexture->setResourceRelease(std::move(releaseHelper));
82cb93a386Sopenharmony_ci    }
83cb93a386Sopenharmony_ci
84cb93a386Sopenharmony_ciprivate:
85cb93a386Sopenharmony_ci    GrVkTexture(GrVkGpu*, SkBudgeted, SkISize, sk_sp<GrVkImage> texture, GrMipmapStatus);
86cb93a386Sopenharmony_ci    GrVkTexture(GrVkGpu*, SkISize, sk_sp<GrVkImage> texture, GrMipmapStatus,
87cb93a386Sopenharmony_ci                GrWrapCacheable, GrIOType, bool isExternal);
88cb93a386Sopenharmony_ci
89cb93a386Sopenharmony_ci    sk_sp<GrVkImage> fTexture;
90cb93a386Sopenharmony_ci
91cb93a386Sopenharmony_ci    struct SamplerHash {
92cb93a386Sopenharmony_ci        uint32_t operator()(GrSamplerState state) const { return state.asIndex(); }
93cb93a386Sopenharmony_ci    };
94cb93a386Sopenharmony_ci    struct DescriptorCacheEntry;
95cb93a386Sopenharmony_ci    SkLRUCache<const GrSamplerState, std::unique_ptr<DescriptorCacheEntry>, SamplerHash>
96cb93a386Sopenharmony_ci            fDescSetCache;
97cb93a386Sopenharmony_ci    static constexpr int kMaxCachedDescSets = 8;
98cb93a386Sopenharmony_ci};
99cb93a386Sopenharmony_ci
100cb93a386Sopenharmony_ci#endif
101