1 /* 2 * Copyright 2018 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 GrMtlSampler_DEFINED 9 #define GrMtlSampler_DEFINED 10 11 #import <Metal/Metal.h> 12 13 #include "src/core/SkOpts.h" 14 #include <atomic> 15 16 class GrSamplerState; 17 class GrMtlGpu; 18 19 // A wrapper for a MTLSamplerState object with caching support. 20 class GrMtlSampler : public SkRefCnt { 21 public: 22 static GrMtlSampler* Create(const GrMtlGpu* gpu, const GrSamplerState&, uint32_t maxMipLevel); ~GrMtlSampler()23 ~GrMtlSampler() { fMtlSamplerState = nil; } 24 mtlSampler()25 id<MTLSamplerState> mtlSampler() const { return fMtlSamplerState; } 26 27 typedef uint32_t Key; 28 29 // Helpers for hashing GrMtlSampler 30 static Key GenerateKey(const GrSamplerState&, uint32_t maxMipLevel); 31 GetKey(const GrMtlSampler & sampler)32 static const Key& GetKey(const GrMtlSampler& sampler) { return sampler.fKey; } Hash(const Key & key)33 static uint32_t Hash(const Key& key) { 34 return SkOpts::hash(reinterpret_cast<const uint32_t*>(&key), sizeof(Key)); 35 } 36 37 private: GrMtlSampler(id<MTLSamplerState> mtlSamplerState,Key key)38 GrMtlSampler(id<MTLSamplerState> mtlSamplerState, Key key) 39 : fMtlSamplerState(mtlSamplerState) 40 , fKey(key) {} 41 42 id<MTLSamplerState> fMtlSamplerState; 43 Key fKey; 44 }; 45 46 #endif 47