Hash :
d2a58f00
Author :
Date :
2022-09-08T14:19:21
EGL: Implement eglCopyMetalSharedEventANGLE Add eglCopyMetalSharedEventANGLE function to the ANGLE_metal_shared_event_sync extension. This brings the extension on par with the EGL_ANDROID_native_fence_sync extension. eglCopyMetalSharedEventANGLE allows for copying the Metal event object from EGLSync objects implemented by the ANGLE Metal renderer. This function follows Objective-C convention for "copy" methods and increases the retain count of the Metal event object. The EGL API user is thus responsible for ensuring to release the returned object to avoid memory leaks. Test: angle_end2end_tests --gtest_filter=EGLSyncTestMetalSharedEvent.* Bug: angleproject:7561 Change-Id: I8c35b559014b85cb8c6a0e76ac2ab7891eed5da0 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3881423 Reviewed-by: Kenneth Russell <kbr@chromium.org> Commit-Queue: Kenneth Russell <kbr@chromium.org> Reviewed-by: Quyen Le <lehoangquyen@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
//
// Copyright 2022 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// EGLSyncTestMetalSharedEvent:
// Tests pertaining to EGL_ANGLE_sync_mtl_shared_event extension.
//
#include <gtest/gtest.h>
#include "test_utils/ANGLETest.h"
#include "util/EGLWindow.h"
#include <Metal/Metal.h>
using namespace angle;
static inline EGLAttrib Uint64HighPart(uint64_t value)
{
return value >> 32;
}
static inline EGLAttrib Uint64LowPart(uint64_t value)
{
return value & 0xFFFFFFFF;
}
class EGLSyncTestMetalSharedEvent : public ANGLETest<>
{
protected:
id<MTLSharedEvent> createMetalSharedEvent() const
{
id<MTLDevice> device = getMetalDevice();
id<MTLSharedEvent> sharedEvent = [device newSharedEvent];
sharedEvent.label = @"TestSharedEvent";
return sharedEvent;
}
id<MTLDevice> getMetalDevice() const
{
EGLAttrib angleDevice = 0;
EXPECT_EGL_TRUE(
eglQueryDisplayAttribEXT(getEGLWindow()->getDisplay(), EGL_DEVICE_EXT, &angleDevice));
EGLAttrib device = 0;
EXPECT_EGL_TRUE(eglQueryDeviceAttribEXT(reinterpret_cast<EGLDeviceEXT>(angleDevice),
EGL_METAL_DEVICE_ANGLE, &device));
return (__bridge id<MTLDevice>)reinterpret_cast<void *>(device);
}
bool hasEGLDisplayExtension(const char *extname) const
{
return IsEGLDisplayExtensionEnabled(getEGLWindow()->getDisplay(), extname);
}
bool hasSyncMetalSharedEventExtension() const
{
return hasEGLDisplayExtension("EGL_ANGLE_metal_shared_event_sync");
}
EGLAttrib sharedEventAsAttrib(id<MTLSharedEvent> sharedEvent) const
{
return reinterpret_cast<EGLAttrib>(sharedEvent);
}
id<MTLSharedEvent> sharedEventFromVoidPtr(void *ptr) const
{
return (__bridge id<MTLSharedEvent>)ptr;
}
};
// Test existing fence is created unsignaled
TEST_P(EGLSyncTestMetalSharedEvent, BasicEGLSync)
{
ANGLE_SKIP_TEST_IF(!hasSyncMetalSharedEventExtension());
EGLDisplay display = getEGLWindow()->getDisplay();
EGLSyncKHR sync = eglCreateSyncKHR(display, EGL_SYNC_FENCE_KHR, nullptr);
EXPECT_NE(sync, EGL_NO_SYNC_KHR);
constexpr EGLint kSentinelAttribValue = 123456789;
EGLint signaledValue = kSentinelAttribValue;
EXPECT_EGL_TRUE(eglGetSyncAttribKHR(display, sync, EGL_SYNC_STATUS_KHR, &signaledValue));
EXPECT_EQ(signaledValue, EGL_UNSIGNALED_KHR);
glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_EGL_TRUE(eglWaitSyncKHR(display, sync, 0));
glFinish();
// Don't wait forever to make sure the test terminates
constexpr GLuint64 kTimeout = 1'000'000'000; // 1 second
EXPECT_EQ(EGL_CONDITION_SATISFIED_KHR,
eglClientWaitSyncKHR(display, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, kTimeout));
signaledValue = kSentinelAttribValue;
EXPECT_EGL_TRUE(eglGetSyncAttribKHR(display, sync, EGL_SYNC_STATUS_KHR, &signaledValue));
EXPECT_EQ(signaledValue, EGL_SIGNALED_KHR);
EXPECT_EGL_TRUE(eglDestroySyncKHR(display, sync));
}
// Test usage of eglGetSyncAttrib
TEST_P(EGLSyncTestMetalSharedEvent, GetSyncAttrib)
{
ANGLE_SKIP_TEST_IF(!hasSyncMetalSharedEventExtension());
id<MTLSharedEvent> sharedEvent = createMetalSharedEvent();
EXPECT_EQ([sharedEvent retainCount], 1ul);
uint64_t initialSignalValue = sharedEvent.signaledValue;
EXPECT_EQ(initialSignalValue, 0u);
EGLDisplay display = getEGLWindow()->getDisplay();
EGLAttrib syncAttribs[] = {EGL_SYNC_METAL_SHARED_EVENT_OBJECT_ANGLE,
sharedEventAsAttrib(sharedEvent), EGL_NONE};
EXPECT_EQ([sharedEvent retainCount], 1ul);
EGLSync sync = eglCreateSync(display, EGL_SYNC_METAL_SHARED_EVENT_ANGLE, syncAttribs);
EXPECT_NE(sync, EGL_NO_SYNC);
// sharedEvent, sync, mtlCommandBuffer
EXPECT_EQ([sharedEvent retainCount], 3ul);
// Fence sync attributes are:
//
// EGL_SYNC_TYPE: EGL_SYNC_METAL_SHARED_EVENT_ANGLE
// EGL_SYNC_STATUS: EGL_UNSIGNALED or EGL_SIGNALED
// EGL_SYNC_CONDITION: EGL_SYNC_PRIOR_COMMANDS_COMPLETE
constexpr EGLAttrib kSentinelAttribValue = 123456789;
EGLAttrib attribValue = kSentinelAttribValue;
EXPECT_EGL_TRUE(eglGetSyncAttrib(display, sync, EGL_SYNC_TYPE, &attribValue));
EXPECT_EQ(attribValue, EGL_SYNC_METAL_SHARED_EVENT_ANGLE);
attribValue = kSentinelAttribValue;
EXPECT_EGL_TRUE(eglGetSyncAttrib(display, sync, EGL_SYNC_CONDITION, &attribValue));
EXPECT_EQ(attribValue, EGL_SYNC_PRIOR_COMMANDS_COMPLETE);
attribValue = kSentinelAttribValue;
EXPECT_EGL_TRUE(eglGetSyncAttrib(display, sync, EGL_SYNC_STATUS, &attribValue));
EXPECT_EQ(attribValue, EGL_UNSIGNALED);
EXPECT_EQ([sharedEvent retainCount], 3ul);
glFinish();
attribValue = kSentinelAttribValue;
EXPECT_EGL_TRUE(eglGetSyncAttrib(display, sync, EGL_SYNC_STATUS, &attribValue));
EXPECT_EQ(attribValue, EGL_SIGNALED);
EXPECT_EQ(sharedEvent.signaledValue, initialSignalValue + 1);
EXPECT_EQ([sharedEvent retainCount], 2ul);
EXPECT_EGL_TRUE(eglDestroySync(display, sync));
EXPECT_EQ([sharedEvent retainCount], 1ul);
sharedEvent = nil;
}
// Verify CreateSync and ClientWait for EGL_ANGLE_metal_shared_event_sync
TEST_P(EGLSyncTestMetalSharedEvent, AngleMetalSharedEventSync_ClientWait)
{
ANGLE_SKIP_TEST_IF(!hasSyncMetalSharedEventExtension());
id<MTLSharedEvent> sharedEvent = createMetalSharedEvent();
EGLDisplay display = getEGLWindow()->getDisplay();
EGLAttrib syncAttribs[] = {EGL_SYNC_METAL_SHARED_EVENT_OBJECT_ANGLE,
sharedEventAsAttrib(sharedEvent), EGL_NONE};
// We can ClientWait on this
EGLSync syncWithSharedEvent =
eglCreateSync(display, EGL_SYNC_METAL_SHARED_EVENT_ANGLE, syncAttribs);
EXPECT_NE(syncWithSharedEvent, EGL_NO_SYNC);
// Create work to do
glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
// Wait for the draw to complete
glClear(GL_COLOR_BUFFER_BIT);
// Don't wait forever to make sure the test terminates
constexpr GLuint64 kTimeout = 1000000000; // 1 second
EGLAttrib value = 0;
EXPECT_EQ(EGL_CONDITION_SATISFIED, eglClientWaitSync(display, syncWithSharedEvent,
EGL_SYNC_FLUSH_COMMANDS_BIT, kTimeout));
EXPECT_EGL_TRUE(eglGetSyncAttrib(display, syncWithSharedEvent, EGL_SYNC_STATUS, &value));
EXPECT_EQ(value, EGL_SIGNALED);
// Clean up created objects.
EXPECT_EGL_TRUE(eglDestroySync(display, syncWithSharedEvent));
sharedEvent = nil;
}
// Verify CreateSync and ClientWait for EGL_ANGLE_metal_shared_event_sync
TEST_P(EGLSyncTestMetalSharedEvent, AngleMetalSharedEventSync_ClientWait_WithSignalValue)
{
ANGLE_SKIP_TEST_IF(!hasSyncMetalSharedEventExtension());
id<MTLSharedEvent> sharedEvent = createMetalSharedEvent();
constexpr uint64_t kSignalValue = 0xDEADBEEFCAFE;
EGLDisplay display = getEGLWindow()->getDisplay();
EGLAttrib syncAttribs[] = {EGL_SYNC_METAL_SHARED_EVENT_OBJECT_ANGLE,
sharedEventAsAttrib(sharedEvent),
EGL_SYNC_METAL_SHARED_EVENT_SIGNAL_VALUE_HI_ANGLE,
Uint64HighPart(kSignalValue),
EGL_SYNC_METAL_SHARED_EVENT_SIGNAL_VALUE_LO_ANGLE,
Uint64LowPart(kSignalValue),
EGL_NONE};
// We can ClientWait on this
EGLSync syncWithSharedEvent =
eglCreateSync(display, EGL_SYNC_METAL_SHARED_EVENT_ANGLE, syncAttribs);
EXPECT_NE(syncWithSharedEvent, EGL_NO_SYNC);
// Create work to do
glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
// Wait for the draw to complete
glClear(GL_COLOR_BUFFER_BIT);
// Don't wait forever to make sure the test terminates
constexpr GLuint64 kTimeout = 1000000000; // 1 second
EGLAttrib value = 0;
EXPECT_EQ(EGL_CONDITION_SATISFIED, eglClientWaitSync(display, syncWithSharedEvent,
EGL_SYNC_FLUSH_COMMANDS_BIT, kTimeout));
EXPECT_EGL_TRUE(eglGetSyncAttrib(display, syncWithSharedEvent, EGL_SYNC_STATUS, &value));
EXPECT_EQ(value, EGL_SIGNALED);
EXPECT_EQ(sharedEvent.signaledValue, kSignalValue);
// Clean up created objects.
EXPECT_EGL_TRUE(eglDestroySync(display, syncWithSharedEvent));
sharedEvent = nil;
}
// Verify eglCopyMetalSharedEventANGLE for EGL_ANGLE_metal_shared_event_sync
TEST_P(EGLSyncTestMetalSharedEvent, AngleMetalSharedEventSync_CopyMetalSharedEventANGLE)
{
ANGLE_SKIP_TEST_IF(!hasSyncMetalSharedEventExtension());
EGLDisplay display = getEGLWindow()->getDisplay();
// We can ClientWait on this
EGLSync syncWithGeneratedEvent =
eglCreateSyncKHR(display, EGL_SYNC_METAL_SHARED_EVENT_ANGLE, nullptr);
EXPECT_NE(syncWithGeneratedEvent, EGL_NO_SYNC_KHR);
id<MTLSharedEvent> sharedEvent =
sharedEventFromVoidPtr(eglCopyMetalSharedEventANGLE(display, syncWithGeneratedEvent));
EXPECT_EGL_SUCCESS();
EXPECT_EQ([sharedEvent retainCount], 3ul);
glFinish();
EXPECT_EGL_TRUE(eglDestroySync(display, syncWithGeneratedEvent));
// Clean up created objects.
EXPECT_EQ([sharedEvent retainCount], 1ul);
[sharedEvent release];
}
// Verify WaitSync with EGL_ANGLE_metal_shared_event_sync
// Simulate passing shared events across processes by passing across Contexts.
TEST_P(EGLSyncTestMetalSharedEvent, AngleMetalSharedEventSync_WaitSync)
{
ANGLE_SKIP_TEST_IF(!hasSyncMetalSharedEventExtension());
id<MTLSharedEvent> sharedEvent = createMetalSharedEvent();
EGLAttrib value = 0;
EGLDisplay display = getEGLWindow()->getDisplay();
EGLSurface surface = getEGLWindow()->getSurface();
/*- First Context ------------------------*/
EGLAttrib syncAttribs[] = {EGL_SYNC_METAL_SHARED_EVENT_OBJECT_ANGLE,
sharedEventAsAttrib(sharedEvent), EGL_NONE};
// We can ClientWait on this
EGLSync syncWithSharedEvent1 =
eglCreateSync(display, EGL_SYNC_METAL_SHARED_EVENT_ANGLE, syncAttribs);
EXPECT_NE(syncWithSharedEvent1, EGL_NO_SYNC);
if (syncWithSharedEvent1 == EGL_NO_SYNC)
{
// Unable to continue with test.
return;
}
// Create work to do
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
/*- Second Context ------------------------*/
EXPECT_EGL_TRUE(eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EGLContext context2 = getEGLWindow()->createContext(EGL_NO_CONTEXT, nullptr);
EXPECT_EGL_TRUE(eglMakeCurrent(display, surface, surface, context2));
EGLSync syncWithSharedEvent2 =
eglCreateSync(display, EGL_SYNC_METAL_SHARED_EVENT_ANGLE, syncAttribs);
EXPECT_NE(syncWithSharedEvent2, EGL_NO_SYNC);
if (syncWithSharedEvent2 == EGL_NO_SYNC)
{
// Unable to continue with test.
return;
}
// Second draw waits for first to complete. May already be signaled - ignore error.
if (eglWaitSync(display, syncWithSharedEvent2, 0) == EGL_TRUE)
{
// Create work to do
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
// Wait for second draw to complete
EXPECT_EQ(EGL_CONDITION_SATISFIED, eglClientWaitSync(display, syncWithSharedEvent2,
EGL_SYNC_FLUSH_COMMANDS_BIT, 1000000000));
EXPECT_EGL_TRUE(eglGetSyncAttrib(display, syncWithSharedEvent2, EGL_SYNC_STATUS, &value));
EXPECT_EQ(value, EGL_SIGNALED);
// Reset to default context and surface.
EXPECT_EGL_TRUE(eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EXPECT_EGL_TRUE(eglMakeCurrent(display, surface, surface, getEGLWindow()->getContext()));
// Clean up created objects.
EXPECT_EGL_TRUE(eglDestroySync(display, syncWithSharedEvent2));
EXPECT_EGL_TRUE(eglDestroyContext(display, context2));
// Wait for first draw to complete
EXPECT_EQ(EGL_CONDITION_SATISFIED, eglClientWaitSync(display, syncWithSharedEvent1,
EGL_SYNC_FLUSH_COMMANDS_BIT, 1000000000));
EXPECT_EGL_TRUE(eglGetSyncAttrib(display, syncWithSharedEvent1, EGL_SYNC_STATUS, &value));
EXPECT_EQ(value, EGL_SIGNALED);
// Clean up created objects.
EXPECT_EGL_TRUE(eglDestroySync(display, syncWithSharedEvent1));
sharedEvent = nil;
}
ANGLE_INSTANTIATE_TEST(EGLSyncTestMetalSharedEvent, ES2_METAL(), ES3_METAL());
// This test suite is not instantiated on non-Metal backends and OSes.
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EGLSyncTestMetalSharedEvent);