1 /* 2 * Copyright 2019 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 GrMtlDepthStencil_DEFINED 9 #define GrMtlDepthStencil_DEFINED 10 11 #import <Metal/Metal.h> 12 13 #include "include/gpu/GrTypes.h" 14 #include "src/core/SkOpts.h" 15 #include "src/gpu/GrManagedResource.h" 16 #include <atomic> 17 18 class GrMtlGpu; 19 class GrStencilSettings; 20 21 // A wrapper for a MTLDepthStencilState object with caching support. 22 class GrMtlDepthStencil : public GrManagedResource { 23 public: 24 static GrMtlDepthStencil* Create(const GrMtlGpu*, const GrStencilSettings&, GrSurfaceOrigin); 25 ~GrMtlDepthStencil()26 ~GrMtlDepthStencil() override { fMtlDepthStencilState = nil; } 27 mtlDepthStencil()28 id<MTLDepthStencilState> mtlDepthStencil() const { return fMtlDepthStencilState; } 29 30 struct Key { 31 struct Face { 32 uint32_t fReadMask; 33 uint32_t fWriteMask; 34 uint32_t fOps; 35 }; 36 Face fFront; 37 Face fBack; 38 39 bool operator==(const Key& that) const { 40 return this->fFront.fReadMask == that.fFront.fReadMask && 41 this->fFront.fWriteMask == that.fFront.fWriteMask && 42 this->fFront.fOps == that.fFront.fOps && 43 this->fBack.fReadMask == that.fBack.fReadMask && 44 this->fBack.fWriteMask == that.fBack.fWriteMask && 45 this->fBack.fOps == that.fBack.fOps; 46 } 47 }; 48 49 // Helpers for hashing GrMtlDepthStencil 50 static Key GenerateKey(const GrStencilSettings&, GrSurfaceOrigin); 51 GetKey(const GrMtlDepthStencil & depthStencil)52 static const Key& GetKey(const GrMtlDepthStencil& depthStencil) { return depthStencil.fKey; } Hash(const Key & key)53 static uint32_t Hash(const Key& key) { 54 return SkOpts::hash(reinterpret_cast<const uint32_t*>(&key), sizeof(Key)); 55 } 56 57 #ifdef SK_TRACE_MANAGED_RESOURCES 58 /** output a human-readable dump of this resource's information 59 */ dumpInfo()60 void dumpInfo() const override { 61 SkDebugf("GrMtlDepthStencil: %p (%ld refs)\n", fMtlDepthStencilState, 62 CFGetRetainCount((CFTypeRef)fMtlDepthStencilState)); 63 } 64 #endif 65 freeGPUData()66 void freeGPUData() const override { 67 fMtlDepthStencilState = nil; 68 } 69 70 private: GrMtlDepthStencil(id<MTLDepthStencilState> mtlDepthStencilState,Key key)71 GrMtlDepthStencil(id<MTLDepthStencilState> mtlDepthStencilState, Key key) 72 : fMtlDepthStencilState(mtlDepthStencilState) 73 , fKey(key) {} 74 75 mutable id<MTLDepthStencilState> fMtlDepthStencilState; 76 Key fKey; 77 }; 78 79 #endif 80