xref: /aosp_15_r20/external/angle/src/libANGLE/renderer/metal/mtl_context_device.mm (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1//
2// Copyright 2021 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6// mtl_context_device.mm:
7//      Implementation of Metal framework's MTLDevice wrapper per context.
8//
9
10#include "libANGLE/renderer/metal/mtl_context_device.h"
11#if ANGLE_USE_METAL_OWNERSHIP_IDENTITY
12#    include "libANGLE/renderer/metal/mtl_resource_spi.h"
13#endif
14
15namespace rx
16{
17namespace mtl
18{
19
20ContextDevice::ContextDevice(GLint ownershipIdentity)
21{
22#if ANGLE_USE_METAL_OWNERSHIP_IDENTITY
23    mOwnershipIdentity = static_cast<task_id_token_t>(ownershipIdentity);
24    if (mOwnershipIdentity != TASK_ID_TOKEN_NULL)
25    {
26        kern_return_t kr =
27            mach_port_mod_refs(mach_task_self(), mOwnershipIdentity, MACH_PORT_RIGHT_SEND, 1);
28        if (ANGLE_UNLIKELY(kr != KERN_SUCCESS))
29        {
30            ERR() << "mach_port_mod_refs failed with: %s (%x)" << mach_error_string(kr) << kr;
31            ASSERT(false);
32        }
33    }
34#endif
35}
36
37ContextDevice::~ContextDevice()
38{
39#if ANGLE_USE_METAL_OWNERSHIP_IDENTITY
40    if (mOwnershipIdentity != TASK_ID_TOKEN_NULL)
41    {
42        kern_return_t kr =
43            mach_port_mod_refs(mach_task_self(), mOwnershipIdentity, MACH_PORT_RIGHT_SEND, -1);
44        if (ANGLE_UNLIKELY(kr != KERN_SUCCESS))
45        {
46            ERR() << "mach_port_mod_refs failed with: %s (%x)" << mach_error_string(kr) << kr;
47            ASSERT(false);
48        }
49    }
50#endif
51}
52
53AutoObjCPtr<id<MTLSamplerState>> ContextDevice::newSamplerStateWithDescriptor(
54    MTLSamplerDescriptor *descriptor) const
55{
56    return adoptObjCObj([get() newSamplerStateWithDescriptor:descriptor]);
57}
58
59AutoObjCPtr<id<MTLTexture>> ContextDevice::newTextureWithDescriptor(
60    MTLTextureDescriptor *descriptor) const
61{
62
63    auto resource = adoptObjCObj([get() newTextureWithDescriptor:descriptor]);
64    setOwnerWithIdentity(resource);
65    return resource;
66}
67
68AutoObjCPtr<id<MTLTexture>> ContextDevice::newTextureWithDescriptor(
69    MTLTextureDescriptor *descriptor,
70    IOSurfaceRef iosurface,
71    NSUInteger plane) const
72{
73    return adoptObjCObj([get() newTextureWithDescriptor:descriptor
74                                              iosurface:iosurface
75                                                  plane:plane]);
76}
77
78AutoObjCPtr<id<MTLBuffer>> ContextDevice::newBufferWithLength(NSUInteger length,
79                                                              MTLResourceOptions options) const
80{
81    auto resource = adoptObjCObj([get() newBufferWithLength:length options:options]);
82    setOwnerWithIdentity(resource);
83    return resource;
84}
85
86AutoObjCPtr<id<MTLBuffer>> ContextDevice::newBufferWithBytes(const void *pointer,
87                                                             NSUInteger length,
88                                                             MTLResourceOptions options) const
89{
90    auto resource = adoptObjCObj([get() newBufferWithBytes:pointer length:length options:options]);
91    setOwnerWithIdentity(resource);
92    return resource;
93}
94
95AutoObjCPtr<id<MTLComputePipelineState>> ContextDevice::newComputePipelineStateWithFunction(
96    id<MTLFunction> computeFunction,
97    __autoreleasing NSError **error) const
98{
99    return adoptObjCObj([get() newComputePipelineStateWithFunction:computeFunction error:error]);
100}
101
102AutoObjCPtr<id<MTLRenderPipelineState>> ContextDevice::newRenderPipelineStateWithDescriptor(
103    MTLRenderPipelineDescriptor *descriptor,
104    __autoreleasing NSError **error) const
105{
106    return adoptObjCObj([get() newRenderPipelineStateWithDescriptor:descriptor error:error]);
107}
108
109AutoObjCPtr<id<MTLDepthStencilState>> ContextDevice::newDepthStencilStateWithDescriptor(
110    MTLDepthStencilDescriptor *descriptor) const
111{
112    return adoptObjCObj([get() newDepthStencilStateWithDescriptor:descriptor]);
113}
114
115AutoObjCPtr<id<MTLSharedEvent>> ContextDevice::newSharedEvent() const
116{
117    return adoptObjCObj([get() newSharedEvent]);
118}
119
120AutoObjCPtr<id<MTLEvent>> ContextDevice::newEvent() const
121{
122    return adoptObjCObj([get() newEvent]);
123}
124
125void ContextDevice::setOwnerWithIdentity(id<MTLResource> resource) const
126{
127#if ANGLE_USE_METAL_OWNERSHIP_IDENTITY
128    mtl::setOwnerWithIdentity(resource, mOwnershipIdentity);
129#endif
130}
131
132bool ContextDevice::hasUnifiedMemory() const
133{
134    return [get() hasUnifiedMemory];
135}
136
137}  // namespace mtl
138}  // namespace rx
139