xref: /aosp_15_r20/external/skia/src/gpu/graphite/mtl/MtlBackendSemaphore.mm (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1/*
2 * Copyright 2024 Google LLC
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#include "include/core/SkString.h"
8#include "include/gpu/MutableTextureState.h"
9#include "include/gpu/graphite/mtl/MtlGraphiteTypes.h"
10#include "src/gpu/graphite/BackendSemaphorePriv.h"
11#include "src/gpu/graphite/mtl/MtlGraphiteTypesPriv.h"
12#include "src/gpu/mtl/MtlUtilsPriv.h"
13
14#include <cstdint>
15
16#import <Metal/Metal.h>
17
18namespace skgpu::graphite {
19
20class MtlBackendSemaphoreData final : public BackendSemaphoreData {
21public:
22    MtlBackendSemaphoreData(CFTypeRef mtlEvent, uint64_t value)
23            : fMtlEvent(mtlEvent), fMtlValue(value) {}
24
25#if defined(SK_DEBUG)
26    skgpu::BackendApi type() const override { return skgpu::BackendApi::kMetal; }
27#endif
28
29    CFTypeRef event() const { return fMtlEvent; }
30    uint64_t value() const { return fMtlValue; }
31
32private:
33    CFTypeRef fMtlEvent;  // Expected to be an id<MTLEvent>
34    uint64_t fMtlValue;
35
36    void copyTo(AnyBackendSemaphoreData& dstData) const override {
37        // Don't assert that dstData is a metal type because it could be
38        // uninitialized and that assert would fail.
39        dstData.emplace<MtlBackendSemaphoreData>(fMtlEvent, fMtlValue);
40    }
41};
42
43static const MtlBackendSemaphoreData* get_and_cast_data(const BackendSemaphore& sem) {
44    auto data = BackendSemaphorePriv::GetData(sem);
45    SkASSERT(!data || data->type() == skgpu::BackendApi::kMetal);
46    return static_cast<const MtlBackendSemaphoreData*>(data);
47}
48
49namespace BackendSemaphores {
50BackendSemaphore MakeMetal(CFTypeRef mtlEvent, uint64_t value) {
51    return BackendSemaphorePriv::Make(skgpu::BackendApi::kMetal,
52                                      MtlBackendSemaphoreData(mtlEvent, value));
53}
54
55CFTypeRef GetMtlEvent(const BackendSemaphore& sem) {
56    if (!sem.isValid() || sem.backend() != skgpu::BackendApi::kMetal) {
57        return nullptr;
58    }
59    const MtlBackendSemaphoreData* mtlData = get_and_cast_data(sem);
60    SkASSERT(mtlData);
61    return mtlData->event();
62}
63
64uint64_t GetMtlValue(const BackendSemaphore& sem) {
65    if (!sem.isValid() || sem.backend() != skgpu::BackendApi::kMetal) {
66        return 0;
67    }
68    const MtlBackendSemaphoreData* mtlData = get_and_cast_data(sem);
69    SkASSERT(mtlData);
70    return mtlData->value();
71}
72
73}  // namespace BackendSemaphores
74
75}  // namespace skgpu::graphite
76