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 "GrVkResource.h" 12 #include "GrVkSamplerYcbcrConversion.h" 13 #include "SkOpts.h" 14 #include "vk/GrVkTypes.h" 15 #include <atomic> 16 17 class GrSamplerState; 18 class GrVkGpu; 19 20 class GrVkSampler : public GrVkResource { 21 public: 22 static GrVkSampler* Create(GrVkGpu* gpu, const 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(uint16_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 uint16_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(const 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_VK_RESOURCES dumpInfo()55 void dumpInfo() const override { 56 SkDebugf("GrVkSampler: %d (%d refs)\n", fSampler, this->getRefCnt()); 57 } 58 #endif 59 60 private: GrVkSampler(VkSampler sampler,GrVkSamplerYcbcrConversion * ycbcrConversion,Key key)61 GrVkSampler(VkSampler sampler, GrVkSamplerYcbcrConversion* ycbcrConversion, Key key) 62 : INHERITED() 63 , fSampler(sampler) 64 , fYcbcrConversion(ycbcrConversion) 65 , fKey(key) 66 , fUniqueID(GenID()) {} 67 68 void freeGPUData(GrVkGpu* gpu) const override; 69 void abandonGPUData() 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 GrVkResource INHERITED; 86 }; 87 88 #endif 89