xref: /aosp_15_r20/external/skia/src/gpu/ganesh/mtl/GrMtlBackendSemaphore.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/gpu/ganesh/mtl/GrMtlBackendSemaphore.h"
8
9#include "include/gpu/ganesh/GrTypes.h"
10#include "include/private/base/SkAssert.h"
11#include "src/gpu/ganesh/GrBackendSemaphorePriv.h"
12
13#if !__has_feature(objc_arc)
14#error This file must be compiled with Arc. Use -fobjc-arc flag
15#endif
16
17class GrMtlBackendSemaphoreData final : public GrBackendSemaphoreData {
18public:
19    GrMtlBackendSemaphoreData(GrMTLHandle event, uint64_t value) : fEvent(event), fValue(value) {}
20
21    GrMTLHandle event() const { return fEvent; }
22    uint64_t value() const { return fValue; }
23
24private:
25    void copyTo(AnySemaphoreData& data) const override {
26        data.emplace<GrMtlBackendSemaphoreData>(fEvent, fValue);
27    }
28
29#if defined(SK_DEBUG)
30    GrBackendApi type() const override { return GrBackendApi::kMetal; }
31#endif
32
33    GrMTLHandle fEvent;
34    uint64_t fValue;
35};
36
37static const GrMtlBackendSemaphoreData* get_and_cast_data(const GrBackendSemaphore& sem) {
38    auto data = GrBackendSemaphorePriv::GetBackendData(sem);
39    SkASSERT(!data || data->type() == GrBackendApi::kMetal);
40    return static_cast<const GrMtlBackendSemaphoreData*>(data);
41}
42
43namespace GrBackendSemaphores {
44GrBackendSemaphore MakeMtl(GrMTLHandle event, uint64_t value) {
45    GrMtlBackendSemaphoreData data(event, value);
46    return GrBackendSemaphorePriv::MakeGrBackendSemaphore(GrBackendApi::kMetal, data);
47}
48
49GrMTLHandle GetMtlHandle(const GrBackendSemaphore& sem) {
50    SkASSERT(sem.backend() == GrBackendApi::kMetal);
51    const GrMtlBackendSemaphoreData* data = get_and_cast_data(sem);
52    SkASSERT(data);
53    return data->event();
54}
55
56uint64_t GetMtlValue(const GrBackendSemaphore& sem) {
57    SkASSERT(sem.backend() == GrBackendApi::kMetal);
58    const GrMtlBackendSemaphoreData* data = get_and_cast_data(sem);
59    SkASSERT(data);
60    return data->value();
61}
62}  // namespace GrBackendSemaphores
63