michael@0: #include "precompiled.h" michael@0: // michael@0: // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: // main.cpp: DLL entry point and management of thread-local data. michael@0: michael@0: #include "libGLESv2/main.h" michael@0: michael@0: #include "libGLESv2/Context.h" michael@0: michael@0: static DWORD currentTLS = TLS_OUT_OF_INDEXES; michael@0: michael@0: extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) michael@0: { michael@0: switch (reason) michael@0: { michael@0: case DLL_PROCESS_ATTACH: michael@0: { michael@0: currentTLS = TlsAlloc(); michael@0: michael@0: if (currentTLS == TLS_OUT_OF_INDEXES) michael@0: { michael@0: return FALSE; michael@0: } michael@0: } michael@0: // Fall throught to initialize index michael@0: case DLL_THREAD_ATTACH: michael@0: { michael@0: gl::Current *current = (gl::Current*)LocalAlloc(LPTR, sizeof(gl::Current)); michael@0: michael@0: if (current) michael@0: { michael@0: TlsSetValue(currentTLS, current); michael@0: michael@0: current->context = NULL; michael@0: current->display = NULL; michael@0: } michael@0: } michael@0: break; michael@0: case DLL_THREAD_DETACH: michael@0: { michael@0: void *current = TlsGetValue(currentTLS); michael@0: michael@0: if (current) michael@0: { michael@0: LocalFree((HLOCAL)current); michael@0: } michael@0: } michael@0: break; michael@0: case DLL_PROCESS_DETACH: michael@0: { michael@0: void *current = TlsGetValue(currentTLS); michael@0: michael@0: if (current) michael@0: { michael@0: LocalFree((HLOCAL)current); michael@0: } michael@0: michael@0: TlsFree(currentTLS); michael@0: } michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: namespace gl michael@0: { michael@0: void makeCurrent(Context *context, egl::Display *display, egl::Surface *surface) michael@0: { michael@0: Current *current = (Current*)TlsGetValue(currentTLS); michael@0: michael@0: current->context = context; michael@0: current->display = display; michael@0: michael@0: if (context && display && surface) michael@0: { michael@0: context->makeCurrent(surface); michael@0: } michael@0: } michael@0: michael@0: Context *getContext() michael@0: { michael@0: Current *current = (Current*)TlsGetValue(currentTLS); michael@0: michael@0: return current->context; michael@0: } michael@0: michael@0: Context *getNonLostContext() michael@0: { michael@0: Context *context = getContext(); michael@0: michael@0: if (context) michael@0: { michael@0: if (context->isContextLost()) michael@0: { michael@0: gl::error(GL_OUT_OF_MEMORY); michael@0: return NULL; michael@0: } michael@0: else michael@0: { michael@0: return context; michael@0: } michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: egl::Display *getDisplay() michael@0: { michael@0: Current *current = (Current*)TlsGetValue(currentTLS); michael@0: michael@0: return current->display; michael@0: } michael@0: michael@0: // Records an error code michael@0: void error(GLenum errorCode) michael@0: { michael@0: gl::Context *context = glGetCurrentContext(); michael@0: michael@0: if (context) michael@0: { michael@0: switch (errorCode) michael@0: { michael@0: case GL_INVALID_ENUM: michael@0: context->recordInvalidEnum(); michael@0: TRACE("\t! Error generated: invalid enum\n"); michael@0: break; michael@0: case GL_INVALID_VALUE: michael@0: context->recordInvalidValue(); michael@0: TRACE("\t! Error generated: invalid value\n"); michael@0: break; michael@0: case GL_INVALID_OPERATION: michael@0: context->recordInvalidOperation(); michael@0: TRACE("\t! Error generated: invalid operation\n"); michael@0: break; michael@0: case GL_OUT_OF_MEMORY: michael@0: context->recordOutOfMemory(); michael@0: TRACE("\t! Error generated: out of memory\n"); michael@0: break; michael@0: case GL_INVALID_FRAMEBUFFER_OPERATION: michael@0: context->recordInvalidFramebufferOperation(); michael@0: TRACE("\t! Error generated: invalid framebuffer operation\n"); michael@0: break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: } michael@0: