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 <atomic>
16 
17 class GrMtlGpu;
18 class GrStencilSettings;
19 
20 // A wrapper for a MTLDepthStencilState object with caching support.
21 class GrMtlDepthStencil : public SkRefCnt {
22 public:
23     static GrMtlDepthStencil* Create(const GrMtlGpu*, const GrStencilSettings&, GrSurfaceOrigin);
24 
~GrMtlDepthStencil()25     ~GrMtlDepthStencil() override { fMtlDepthStencilState = nil; }
26 
mtlDepthStencil()27     id<MTLDepthStencilState> mtlDepthStencil() const { return fMtlDepthStencilState; }
28 
29     struct Key {
30         struct Face {
31             uint32_t fReadMask;
32             uint32_t fWriteMask;
33             uint32_t fOps;
34         };
35         Face fFront;
36         Face fBack;
37 
38         bool operator==(const Key& that) const {
39             return this->fFront.fReadMask == that.fFront.fReadMask &&
40                    this->fFront.fWriteMask == that.fFront.fWriteMask &&
41                    this->fFront.fOps == that.fFront.fOps &&
42                    this->fBack.fReadMask == that.fBack.fReadMask &&
43                    this->fBack.fWriteMask == that.fBack.fWriteMask &&
44                    this->fBack.fOps == that.fBack.fOps;
45         }
46     };
47 
48     // Helpers for hashing GrMtlSampler
49     static Key GenerateKey(const GrStencilSettings&, GrSurfaceOrigin);
50 
GetKey(const GrMtlDepthStencil & depthStencil)51     static const Key& GetKey(const GrMtlDepthStencil& depthStencil) { return depthStencil.fKey; }
Hash(const Key & key)52     static uint32_t Hash(const Key& key) {
53         return SkOpts::hash(reinterpret_cast<const uint32_t*>(&key), sizeof(Key));
54     }
55 
56 private:
GrMtlDepthStencil(id<MTLDepthStencilState> mtlDepthStencilState,Key key)57     GrMtlDepthStencil(id<MTLDepthStencilState> mtlDepthStencilState, Key key)
58         : fMtlDepthStencilState(mtlDepthStencilState)
59         , fKey(key) {}
60 
61     id<MTLDepthStencilState> fMtlDepthStencilState;
62     Key                      fKey;
63 };
64 
65 #endif
66