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