xref: /third_party/skia/src/gpu/mtl/GrMtlSampler.h (revision cb93a386)
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 "src/gpu/GrManagedResource.h"
15#include <atomic>
16
17class GrSamplerState;
18class GrMtlGpu;
19
20// A wrapper for a MTLSamplerState object with caching support.
21class GrMtlSampler : public GrManagedResource {
22public:
23    static GrMtlSampler* Create(const GrMtlGpu* gpu, GrSamplerState);
24    ~GrMtlSampler() override { fMtlSamplerState = nil; }
25
26    id<MTLSamplerState> mtlSampler() const { return fMtlSamplerState; }
27
28    typedef uint32_t Key;
29
30    // Helpers for hashing GrMtlSampler
31    static Key GenerateKey(GrSamplerState);
32
33    static const Key& GetKey(const GrMtlSampler& sampler) { return sampler.fKey; }
34    static uint32_t Hash(const Key& key) {
35        return SkOpts::hash(reinterpret_cast<const uint32_t*>(&key), sizeof(Key));
36    }
37
38#ifdef SK_TRACE_MANAGED_RESOURCES
39    /** output a human-readable dump of this resource's information
40     */
41    void dumpInfo() const override {
42        SkDebugf("GrMtlSampler: %p (%ld refs)\n", fMtlSamplerState,
43                 CFGetRetainCount((CFTypeRef)fMtlSamplerState));
44    }
45#endif
46
47    void freeGPUData() const override {
48        fMtlSamplerState = nil;
49    }
50
51private:
52    GrMtlSampler(id<MTLSamplerState> mtlSamplerState, Key key)
53        : fMtlSamplerState(mtlSamplerState)
54        , fKey(key) {}
55
56    mutable id<MTLSamplerState> fMtlSamplerState;
57    Key                 fKey;
58};
59
60#endif
61