Commit 7b8f3c9b by Geoff Lang Committed by Commit Bot

Encapsulate the thread local storage into an egl::Thread class.

Instead of having separate GetGlobal* functions, interact with the global objects through a single Thread object. This reduces the number of TLS lookups in many EGL functions and allows the Thread object to be passed down to other objects if needed. BUG=angleproject:1618 Change-Id: I1f9a89e8899d637633f4e91fda0e38ac308dd020 Reviewed-on: https://chromium-review.googlesource.com/409637Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org> Commit-Queue: Geoff Lang <geofflang@chromium.org>
parent d7490967
//
// Copyright(c) 2016 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.
//
// Thread.cpp : Defines the Thread class which represents a global EGL thread.
#include "libANGLE/Thread.h"
#include "libANGLE/Context.h"
#include "libANGLE/Error.h"
namespace egl
{
Thread::Thread()
: mError(EGL_SUCCESS),
mAPI(EGL_OPENGL_ES_API),
mDisplay(static_cast<egl::Display *>(EGL_NO_DISPLAY)),
mDrawSurface(static_cast<egl::Surface *>(EGL_NO_SURFACE)),
mReadSurface(static_cast<egl::Surface *>(EGL_NO_SURFACE)),
mContext(static_cast<gl::Context *>(EGL_NO_CONTEXT))
{
}
void Thread::setError(const Error &error)
{
mError = error.getCode();
}
EGLint Thread::getError() const
{
return mError;
}
void Thread::setAPI(EGLenum api)
{
mAPI = api;
}
EGLenum Thread::getAPI() const
{
return mAPI;
}
void Thread::setCurrent(Display *display,
Surface *drawSurface,
Surface *readSurface,
gl::Context *context)
{
mDisplay = display;
mDrawSurface = drawSurface;
mReadSurface = readSurface;
mContext = context;
}
Display *Thread::getDisplay() const
{
return mDisplay;
}
Surface *Thread::getDrawSurface() const
{
return mDrawSurface;
}
Surface *Thread::getReadSurface() const
{
return mReadSurface;
}
gl::Context *Thread::getContext() const
{
return mContext;
}
gl::Context *Thread::getValidContext() const
{
if (mContext && mContext->isContextLost())
{
mContext->handleError(gl::Error(GL_OUT_OF_MEMORY, "Context has been lost."));
return nullptr;
}
return mContext;
}
} // namespace egl
//
// Copyright(c) 2016 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.
//
// Thread.h : Defines the Thread class which represents a global EGL thread.
#ifndef LIBANGLE_THREAD_H_
#define LIBANGLE_THREAD_H_
#include <EGL/egl.h>
namespace gl
{
class Context;
} // namespace gl
namespace egl
{
class Error;
class Display;
class Surface;
class Thread
{
public:
Thread();
void setError(const Error &error);
EGLint getError() const;
void setAPI(EGLenum api);
EGLenum getAPI() const;
void setCurrent(Display *display,
Surface *drawSurface,
Surface *readSurface,
gl::Context *context);
Display *getDisplay() const;
Surface *getDrawSurface() const;
Surface *getReadSurface() const;
gl::Context *getContext() const;
gl::Context *getValidContext() const;
private:
EGLint mError;
EGLenum mAPI;
egl::Display *mDisplay;
egl::Surface *mDrawSurface;
egl::Surface *mReadSurface;
gl::Context *mContext;
};
} // namespace egl
#endif // LIBANGLE_THREAD_H_
......@@ -138,6 +138,8 @@
'libANGLE/Surface.h',
'libANGLE/Texture.cpp',
'libANGLE/Texture.h',
'libANGLE/Thread.cpp',
'libANGLE/Thread.h',
'libANGLE/TransformFeedback.cpp',
'libANGLE/TransformFeedback.h',
'libANGLE/Uniform.cpp',
......
......@@ -18,6 +18,7 @@
#include "libANGLE/Query.h"
#include "libANGLE/queryconversions.h"
#include "libANGLE/queryutils.h"
#include "libANGLE/Thread.h"
#include "libANGLE/VertexArray.h"
#include "libANGLE/validationES.h"
......@@ -990,10 +991,11 @@ ANGLE_EXPORT void GL_APIENTRY EGLImageTargetTexture2DOES(GLenum target, GLeglIma
{
EVENT("(GLenum target = 0x%X, GLeglImageOES image = 0x%0.8p)", target, image);
Context *context = GetValidGlobalContext();
egl::Thread *thread = egl::GetCurrentThread();
Context *context = thread->getValidContext();
if (context)
{
egl::Display *display = egl::GetGlobalDisplay();
egl::Display *display = thread->getDisplay();
egl::Image *imageObject = reinterpret_cast<egl::Image *>(image);
if (!ValidateEGLImageTargetTexture2DOES(context, display, target, imageObject))
{
......@@ -1015,10 +1017,11 @@ ANGLE_EXPORT void GL_APIENTRY EGLImageTargetRenderbufferStorageOES(GLenum target
{
EVENT("(GLenum target = 0x%X, GLeglImageOES image = 0x%0.8p)", target, image);
Context *context = GetValidGlobalContext();
egl::Thread *thread = egl::GetCurrentThread();
Context *context = thread->getValidContext();
if (context)
{
egl::Display *display = egl::GetGlobalDisplay();
egl::Display *display = thread->getDisplay();
egl::Image *imageObject = reinterpret_cast<egl::Image *>(image);
if (!ValidateEGLImageTargetRenderbufferStorageOES(context, display, target, imageObject))
{
......
......@@ -8,229 +8,141 @@
#include "libGLESv2/global_state.h"
#include "libANGLE/Context.h"
#include "libANGLE/Error.h"
#include "common/debug.h"
#include "common/platform.h"
#include "common/tls.h"
namespace
{
static TLSIndex currentTLS = TLS_INVALID_INDEX;
#include "libANGLE/Thread.h"
struct Current
{
EGLint error;
EGLenum API;
egl::Display *display;
egl::Surface *drawSurface;
egl::Surface *readSurface;
gl::Context *context;
};
Current *AllocateCurrent()
{
ASSERT(currentTLS != TLS_INVALID_INDEX);
if (currentTLS == TLS_INVALID_INDEX)
{
return NULL;
}
Current *current = new Current();
current->error = EGL_SUCCESS;
current->API = EGL_OPENGL_ES_API;
current->display = reinterpret_cast<egl::Display*>(EGL_NO_DISPLAY);
current->drawSurface = reinterpret_cast<egl::Surface*>(EGL_NO_SURFACE);
current->readSurface = reinterpret_cast<egl::Surface*>(EGL_NO_SURFACE);
current->context = reinterpret_cast<gl::Context*>(EGL_NO_CONTEXT);
if (!SetTLSValue(currentTLS, current))
{
ERR("Could not set thread local storage.");
return NULL;
}
return current;
}
Current *GetCurrentData()
namespace gl
{
// Create a TLS index if one has not been created for this DLL
if (currentTLS == TLS_INVALID_INDEX)
{
currentTLS = CreateTLSIndex();
}
Current *current = reinterpret_cast<Current*>(GetTLSValue(currentTLS));
// ANGLE issue 488: when the dll is loaded after thread initialization,
// thread local storage (current) might not exist yet.
return (current ? current : AllocateCurrent());
}
#ifdef ANGLE_PLATFORM_WINDOWS
void DeallocateCurrent()
Context *GetGlobalContext()
{
Current *current = reinterpret_cast<Current*>(GetTLSValue(currentTLS));
SafeDelete(current);
SetTLSValue(currentTLS, NULL);
egl::Thread *thread = egl::GetCurrentThread();
return thread->getContext();
}
extern "C" BOOL WINAPI DllMain(HINSTANCE, DWORD reason, LPVOID)
Context *GetValidGlobalContext()
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
currentTLS = CreateTLSIndex();
if (currentTLS == TLS_INVALID_INDEX)
{
return FALSE;
}
AllocateCurrent();
break;
case DLL_THREAD_ATTACH:
AllocateCurrent();
break;
case DLL_THREAD_DETACH:
DeallocateCurrent();
break;
case DLL_PROCESS_DETACH:
DeallocateCurrent();
if (currentTLS != TLS_INVALID_INDEX)
{
DestroyTLSIndex(currentTLS);
currentTLS = TLS_INVALID_INDEX;
}
break;
}
return TRUE;
egl::Thread *thread = egl::GetCurrentThread();
return thread->getValidContext();
}
#endif
}
} // namespace gl
namespace gl
namespace egl
{
Context *GetGlobalContext()
namespace
{
Current *current = GetCurrentData();
return current->context;
}
static TLSIndex threadTLS = TLS_INVALID_INDEX;
Context *GetValidGlobalContext()
Thread *AllocateCurrentThread()
{
gl::Context *context = GetGlobalContext();
if (context)
ASSERT(threadTLS != TLS_INVALID_INDEX);
if (threadTLS == TLS_INVALID_INDEX)
{
if (context->isContextLost())
{
context->handleError(gl::Error(GL_OUT_OF_MEMORY, "Context has been lost."));
return nullptr;
}
else
{
return context;
}
return nullptr;
}
return nullptr;
}
Thread *thread = new Thread();
if (!SetTLSValue(threadTLS, thread))
{
ERR("Could not set thread local storage.");
return nullptr;
}
return thread;
}
namespace egl
{
} // anonymous namespace
void SetGlobalError(const Error &error)
Thread *GetCurrentThread()
{
Current *current = GetCurrentData();
current->error = error.getCode();
}
// Create a TLS index if one has not been created for this DLL
if (threadTLS == TLS_INVALID_INDEX)
{
threadTLS = CreateTLSIndex();
}
EGLint GetGlobalError()
{
Current *current = GetCurrentData();
Thread *current = static_cast<Thread *>(GetTLSValue(threadTLS));
return current->error;
// ANGLE issue 488: when the dll is loaded after thread initialization,
// thread local storage (current) might not exist yet.
return (current ? current : AllocateCurrentThread());
}
EGLenum GetGlobalAPI()
{
Current *current = GetCurrentData();
return current->API;
}
} // namespace egl
void SetGlobalAPI(EGLenum API)
#ifdef ANGLE_PLATFORM_WINDOWS
namespace egl
{
Current *current = GetCurrentData();
current->API = API;
}
void SetGlobalDisplay(Display *dpy)
namespace
{
Current *current = GetCurrentData();
current->display = dpy;
}
Display *GetGlobalDisplay()
bool DeallocateCurrentThread()
{
Current *current = GetCurrentData();
return current->display;
Thread *thread = static_cast<Thread *>(GetTLSValue(threadTLS));
SafeDelete(thread);
return SetTLSValue(threadTLS, nullptr);
}
void SetGlobalDrawSurface(Surface *surface)
bool InitializeProcess()
{
Current *current = GetCurrentData();
threadTLS = CreateTLSIndex();
if (threadTLS == TLS_INVALID_INDEX)
{
return false;
}
current->drawSurface = surface;
return AllocateCurrentThread() != nullptr;
}
Surface *GetGlobalDrawSurface()
bool TerminateProcess()
{
Current *current = GetCurrentData();
if (!DeallocateCurrentThread())
{
return false;
}
return current->drawSurface;
}
if (threadTLS != TLS_INVALID_INDEX)
{
TLSIndex tlsCopy = threadTLS;
threadTLS = TLS_INVALID_INDEX;
void SetGlobalReadSurface(Surface *surface)
{
Current *current = GetCurrentData();
if (!DestroyTLSIndex(tlsCopy))
{
return false;
}
}
current->readSurface = surface;
return true;
}
Surface *GetGlobalReadSurface()
{
Current *current = GetCurrentData();
} // anonymous namespace
return current->readSurface;
}
} // namespace egl
void SetGlobalContext(gl::Context *context)
extern "C" BOOL WINAPI DllMain(HINSTANCE, DWORD reason, LPVOID)
{
Current *current = GetCurrentData();
switch (reason)
{
case DLL_PROCESS_ATTACH:
return static_cast<BOOL>(egl::InitializeProcess());
current->context = context;
}
case DLL_THREAD_ATTACH:
return static_cast<BOOL>(egl::AllocateCurrentThread() != nullptr);
gl::Context *GetGlobalContext()
{
Current *current = GetCurrentData();
case DLL_THREAD_DETACH:
return static_cast<BOOL>(egl::DeallocateCurrentThread());
return current->context;
}
case DLL_PROCESS_DETACH:
return static_cast<BOOL>(egl::TerminateProcess());
}
return TRUE;
}
#endif // ANGLE_PLATFORM_WINDOWS
......@@ -9,8 +9,6 @@
#ifndef LIBGLESV2_GLOBALSTATE_H_
#define LIBGLESV2_GLOBALSTATE_H_
#include <EGL/egl.h>
namespace gl
{
class Context;
......@@ -18,32 +16,14 @@ class Context;
Context *GetGlobalContext();
Context *GetValidGlobalContext();
}
} // namespace gl
namespace egl
{
class Error;
class Display;
class Surface;
void SetGlobalError(const Error &error);
EGLint GetGlobalError();
void SetGlobalAPI(EGLenum API);
EGLenum GetGlobalAPI();
void SetGlobalDisplay(Display *dpy);
Display *GetGlobalDisplay();
void SetGlobalDrawSurface(Surface *surface);
Surface *GetGlobalDrawSurface();
void SetGlobalReadSurface(Surface *surface);
Surface *GetGlobalReadSurface();
class Thread;
void SetGlobalContext(gl::Context *context);
gl::Context *GetGlobalContext();
Thread *GetCurrentThread();
}
} // namespace egl
#endif // LIBGLESV2_GLOBALSTATE_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment