xref: /aosp_15_r20/external/skia/src/gpu/ganesh/vk/GrVkSampler.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2016 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 GrVkSampler_DEFINED
9 #define GrVkSampler_DEFINED
10 
11 #include "include/core/SkTypes.h"
12 #include "include/private/base/SkDebug.h"
13 #include "include/private/base/SkMacros.h"
14 #include "include/private/gpu/vk/SkiaVulkan.h"
15 #include "src/core/SkChecksum.h"
16 #include "src/gpu/ganesh/GrManagedResource.h"
17 #include "src/gpu/ganesh/vk/GrVkManagedResource.h"
18 #include "src/gpu/ganesh/vk/GrVkSamplerYcbcrConversion.h"
19 
20 #include <atomic>
21 #include <cinttypes>
22 #include <cstdint>
23 
24 class GrSamplerState;
25 class GrVkGpu;
26 namespace skgpu {
27 struct VulkanYcbcrConversionInfo;
28 }
29 
30 class GrVkSampler : public GrVkManagedResource {
31 public:
32     static GrVkSampler* Create(GrVkGpu* gpu,
33                                GrSamplerState,
34                                const skgpu::VulkanYcbcrConversionInfo&);
35 
sampler()36     VkSampler sampler() const { return fSampler; }
samplerPtr()37     const VkSampler* samplerPtr() const { return &fSampler; }
38 
39     SK_BEGIN_REQUIRE_DENSE
40     struct Key {
KeyKey41         Key(uint32_t samplerKey, const GrVkSamplerYcbcrConversion::Key& ycbcrKey) {
42             fSamplerKey = samplerKey;
43             fYcbcrKey = ycbcrKey;
44         }
45         GrVkSamplerYcbcrConversion::Key fYcbcrKey;
46         uint32_t                        fSamplerKey;
47         uint32_t                        fPadding = 0;
48 
49         bool operator==(const Key& that) const {
50             return this->fSamplerKey == that.fSamplerKey &&
51                    this->fYcbcrKey == that.fYcbcrKey;
52         }
53     };
54     SK_END_REQUIRE_DENSE
55 
56     // Helpers for hashing GrVkSampler
57     static Key GenerateKey(GrSamplerState, const skgpu::VulkanYcbcrConversionInfo&);
58 
GetKey(const GrVkSampler & sampler)59     static const Key& GetKey(const GrVkSampler& sampler) { return sampler.fKey; }
Hash(const Key & key)60     static uint32_t Hash(const Key& key) {
61         return SkChecksum::Hash32(&key, sizeof(Key));
62     }
63 
uniqueID()64     uint32_t uniqueID() const { return fUniqueID; }
65 
66 #ifdef SK_TRACE_MANAGED_RESOURCES
dumpInfo()67     void dumpInfo() const override {
68         SkDebugf("GrVkSampler: %" PRIdPTR " (%d refs)\n", (intptr_t)fSampler, this->getRefCnt());
69     }
70 #endif
71 
72 private:
GrVkSampler(const GrVkGpu * gpu,VkSampler sampler,GrVkSamplerYcbcrConversion * ycbcrConversion,Key key)73     GrVkSampler(const GrVkGpu* gpu, VkSampler sampler,
74                 GrVkSamplerYcbcrConversion* ycbcrConversion, Key key)
75             : INHERITED(gpu)
76             , fSampler(sampler)
77             , fYcbcrConversion(ycbcrConversion)
78             , fKey(key)
79             , fUniqueID(GenID()) {}
80 
81     void freeGPUData() const override;
82 
GenID()83     static uint32_t GenID() {
84         static std::atomic<uint32_t> nextID{1};
85         uint32_t id;
86         do {
87             id = nextID++;
88         } while (id == SK_InvalidUniqueID);
89         return id;
90     }
91 
92     VkSampler                   fSampler;
93     GrVkSamplerYcbcrConversion* fYcbcrConversion;
94     Key                         fKey;
95     uint32_t                    fUniqueID;
96 
97     using INHERITED = GrVkManagedResource;
98 };
99 
100 #endif
101