Hash :
3545ae0c
Author :
Date :
2023-06-09T16:11:47
Add isContextMutexStateConsistent() ASSERT Added `gl::Context::isContextMutexStateConsistent()` method. This method is primarily used to check if "SharedContextMutex" activation worked successfully. It is automatically called in the updated `ScopedContextMutexLock` before unlocking. This is to catch possible errors using ASSERT during normal ANGLE operation in applications and tests. The `ScopedContextMutexLock` is now also used instead of the `std::lock_guard<egl::ContextMutex>` in the `SCOPED_SHARE_CONTEXT_LOCK`. No performance regression observed. Important note: `lockAndActivateSharedContextMutex()` is NOT 100% safe regardless of the `kActivationDelayMicro` value, so `ASSERT` may still fail. However, failure does not necessary mean that there will be an undefined behavior, it means that UB might happen. Bug: angleproject:6957 Bug: chromium:1336126 Change-Id: Iee7357fede0d37fa315fe2cc7d27a4e30a304194 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4610227 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Igor Nazarov <i.nazarov@samsung.com>
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
//
// Copyright 2014 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.
//
// global_state.h : Defines functions for querying the thread-local GL and EGL state.
#ifndef LIBGLESV2_GLOBALSTATE_H_
#define LIBGLESV2_GLOBALSTATE_H_
#include "libANGLE/Context.h"
#include "libANGLE/Debug.h"
#include "libANGLE/Display.h"
#include "libANGLE/GlobalMutex.h"
#include "libANGLE/Thread.h"
#include "libANGLE/features.h"
#include "libANGLE/validationEGL.h"
#if defined(ANGLE_PLATFORM_APPLE) || (ANGLE_PLATFORM_ANDROID)
# include "common/tls.h"
#endif
#include <mutex>
namespace egl
{
class Debug;
class Thread;
#if defined(ANGLE_PLATFORM_APPLE)
extern Thread *GetCurrentThreadTLS();
extern void SetCurrentThreadTLS(Thread *thread);
#else
extern thread_local Thread *gCurrentThread;
#endif
gl::Context *GetGlobalLastContext();
void SetGlobalLastContext(gl::Context *context);
Thread *GetCurrentThread();
Debug *GetDebug();
// Sync the current context from Thread to global state.
class [[nodiscard]] ScopedSyncCurrentContextFromThread
{
public:
ScopedSyncCurrentContextFromThread(egl::Thread *thread);
~ScopedSyncCurrentContextFromThread();
private:
egl::Thread *const mThread;
};
// Tries to lock "ContextMutex" of the Context current to the "thread".
ANGLE_INLINE ScopedContextMutexLock TryLockCurrentContext(Thread *thread)
{
ASSERT(kIsSharedContextMutexEnabled);
gl::Context *context = thread->getContext();
return context != nullptr ? ScopedContextMutexLock(context->getContextMutex(), context)
: ScopedContextMutexLock();
}
// Tries to lock "ContextMutex" or "SharedContextMutex" of the Context with "contextID" if it is
// valid, in order to safely use the Context from the "thread".
// Note: this function may change mutex type to SharedContextMutex.
ANGLE_INLINE ScopedContextMutexLock TryLockContextForThread(Thread *thread,
Display *display,
gl::ContextID contextID)
{
ASSERT(kIsSharedContextMutexEnabled);
gl::Context *context = GetContextIfValid(display, contextID);
return context != nullptr ? (context == thread->getContext()
? ScopedContextMutexLock(context->getContextMutex(), context)
: context->lockAndActivateSharedContextMutex())
: ScopedContextMutexLock();
}
// Tries to lock "SharedContextMutex" of the Context with "contextID" if it is valid.
// Note: this function will change mutex type to SharedContextMutex.
ANGLE_INLINE ScopedContextMutexLock TryLockAndActivateSharedContextMutex(Display *display,
gl::ContextID contextID)
{
ASSERT(kIsSharedContextMutexEnabled);
gl::Context *context = GetContextIfValid(display, contextID);
return context != nullptr ? context->lockAndActivateSharedContextMutex()
: ScopedContextMutexLock();
}
// Locks "SharedContextMutex" of the "context" and then tries to merge it with the
// "SharedContextMutex" of the Image with "imageID" if it is valid.
// Note: this function may change mutex type to SharedContextMutex.
ANGLE_INLINE ScopedContextMutexLock LockAndTryMergeSharedContextMutexes(gl::Context *context,
ImageID imageID)
{
ASSERT(kIsSharedContextMutexEnabled);
ASSERT(context->getDisplay() != nullptr);
const Image *image = context->getDisplay()->getImage(imageID);
if (image != nullptr)
{
ContextMutex *imageMutex = image->getSharedContextMutex();
if (imageMutex != nullptr)
{
ScopedContextMutexLock lock = context->lockAndActivateSharedContextMutex();
context->mergeSharedContextMutexes(imageMutex);
return lock;
}
}
// Do not activate "SharedContextMutex" if Image is not valid or does not have the mutex.
return ScopedContextMutexLock(context->getContextMutex(), context);
}
#if !defined(ANGLE_ENABLE_SHARED_CONTEXT_MUTEX)
# define ANGLE_EGL_SCOPED_CONTEXT_LOCK(EP, THREAD, ...)
#else
# define ANGLE_EGL_SCOPED_CONTEXT_LOCK(EP, THREAD, ...) \
egl::ScopedContextMutexLock shareContextLock = GetContextLock_##EP(THREAD, ##__VA_ARGS__)
#endif
} // namespace egl
#define ANGLE_SCOPED_GLOBAL_LOCK() egl::ScopedGlobalMutexLock globalMutexLock
namespace gl
{
ANGLE_INLINE Context *GetGlobalContext()
{
#if defined(ANGLE_PLATFORM_APPLE)
egl::Thread *currentThread = egl::GetCurrentThreadTLS();
#else
egl::Thread *currentThread = egl::gCurrentThread;
#endif
ASSERT(currentThread);
return currentThread->getContext();
}
ANGLE_INLINE Context *GetValidGlobalContext()
{
#if defined(ANGLE_USE_ANDROID_TLS_SLOT)
// TODO: Replace this branch with a compile time flag (http://anglebug.com/4764)
if (angle::gUseAndroidOpenGLTlsSlot)
{
return static_cast<gl::Context *>(ANGLE_ANDROID_GET_GL_TLS()[angle::kAndroidOpenGLTlsSlot]);
}
#endif
#if defined(ANGLE_PLATFORM_APPLE)
return GetCurrentValidContextTLS();
#else
return gCurrentValidContext;
#endif
}
// Generate a context lost error on the context if it is non-null and lost.
void GenerateContextLostErrorOnContext(Context *context);
void GenerateContextLostErrorOnCurrentGlobalContext();
#if defined(ANGLE_FORCE_CONTEXT_CHECK_EVERY_CALL)
// TODO(b/177574181): This should be handled in a backend-specific way.
// if previous context different from current context, dirty all state
static ANGLE_INLINE void DirtyContextIfNeeded(Context *context)
{
if (context && context != egl::GetGlobalLastContext())
{
context->dirtyAllState();
SetGlobalLastContext(context);
}
}
#endif
#if !defined(ANGLE_ENABLE_SHARE_CONTEXT_LOCK)
# define SCOPED_SHARE_CONTEXT_LOCK(context)
# define SCOPED_EGL_IMAGE_SHARE_CONTEXT_LOCK(context, imageID) ANGLE_SCOPED_GLOBAL_LOCK()
#else
# if defined(ANGLE_FORCE_CONTEXT_CHECK_EVERY_CALL)
# define SCOPED_SHARE_CONTEXT_LOCK(context) \
egl::ScopedGlobalMutexLock shareContextLock; \
DirtyContextIfNeeded(context)
# define SCOPED_EGL_IMAGE_SHARE_CONTEXT_LOCK(context, imageID) \
SCOPED_SHARE_CONTEXT_LOCK(context)
# elif !defined(ANGLE_ENABLE_SHARED_CONTEXT_MUTEX)
# define SCOPED_SHARE_CONTEXT_LOCK(context) \
egl::ScopedOptionalGlobalMutexLock shareContextLock(context->isShared())
# define SCOPED_EGL_IMAGE_SHARE_CONTEXT_LOCK(context, imageID) ANGLE_SCOPED_GLOBAL_LOCK()
# else
# define SCOPED_SHARE_CONTEXT_LOCK(context) \
egl::ScopedContextMutexLock shareContextLock(context->getContextMutex(), context)
# define SCOPED_EGL_IMAGE_SHARE_CONTEXT_LOCK(context, imageID) \
ANGLE_SCOPED_GLOBAL_LOCK(); \
egl::ScopedContextMutexLock shareContextLock = \
egl::LockAndTryMergeSharedContextMutexes(context, imageID)
# endif
#endif
} // namespace gl
#endif // LIBGLESV2_GLOBALSTATE_H_