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/gpu/vk/GrVkTypes.h"
12 #include "src/core/SkOpts.h"
13 #include "src/gpu/vk/GrVkManagedResource.h"
14 #include "src/gpu/vk/GrVkSamplerYcbcrConversion.h"
15 #include <atomic>
16 
17 class GrSamplerState;
18 class GrVkGpu;
19 
20 class GrVkSampler : public GrVkManagedResource {
21 public:
22     static GrVkSampler* Create(GrVkGpu* gpu, GrSamplerState, const GrVkYcbcrConversionInfo&);
23 
sampler()24     VkSampler sampler() const { return fSampler; }
samplerPtr()25     const VkSampler* samplerPtr() const { return &fSampler; }
26 
27     struct Key {
KeyKey28         Key(uint8_t samplerKey, const GrVkSamplerYcbcrConversion::Key& ycbcrKey) {
29             // We must memset here since the GrVkSamplerYcbcrConversion has a 64 bit value which may
30             // force alignment padding to occur in the middle of the Key struct.
31             memset(this, 0, sizeof(Key));
32             fSamplerKey = samplerKey;
33             fYcbcrKey = ycbcrKey;
34         }
35         uint8_t                         fSamplerKey;
36         GrVkSamplerYcbcrConversion::Key fYcbcrKey;
37 
38         bool operator==(const Key& that) const {
39             return this->fSamplerKey == that.fSamplerKey &&
40                    this->fYcbcrKey == that.fYcbcrKey;
41         }
42     };
43 
44     // Helpers for hashing GrVkSampler
45     static Key GenerateKey(GrSamplerState, const GrVkYcbcrConversionInfo&);
46 
GetKey(const GrVkSampler & sampler)47     static const Key& GetKey(const GrVkSampler& sampler) { return sampler.fKey; }
Hash(const Key & key)48     static uint32_t Hash(const Key& key) {
49         return SkOpts::hash(reinterpret_cast<const uint32_t*>(&key), sizeof(Key));
50     }
51 
uniqueID()52     uint32_t uniqueID() const { return fUniqueID; }
53 
54 #ifdef SK_TRACE_MANAGED_RESOURCES
dumpInfo()55     void dumpInfo() const override {
56         SkDebugf("GrVkSampler: %d (%d refs)\n", fSampler, this->getRefCnt());
57     }
58 #endif
59 
60 private:
GrVkSampler(const GrVkGpu * gpu,VkSampler sampler,GrVkSamplerYcbcrConversion * ycbcrConversion,Key key)61     GrVkSampler(const GrVkGpu* gpu, VkSampler sampler,
62                 GrVkSamplerYcbcrConversion* ycbcrConversion, Key key)
63             : INHERITED(gpu)
64             , fSampler(sampler)
65             , fYcbcrConversion(ycbcrConversion)
66             , fKey(key)
67             , fUniqueID(GenID()) {}
68 
69     void freeGPUData() const override;
70 
GenID()71     static uint32_t GenID() {
72         static std::atomic<uint32_t> nextID{1};
73         uint32_t id;
74         do {
75             id = nextID++;
76         } while (id == SK_InvalidUniqueID);
77         return id;
78     }
79 
80     VkSampler                   fSampler;
81     GrVkSamplerYcbcrConversion* fYcbcrConversion;
82     Key                         fKey;
83     uint32_t                    fUniqueID;
84 
85     typedef GrVkManagedResource INHERITED;
86 };
87 
88 #endif
89