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