daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
daniel@transgaming.com | fad16ed | 2012-10-17 18:24:01 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // main.cpp: DLL entry point and management of thread-local data. |
| 8 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 9 | #include "libGLESv2/main.h" |
daniel@transgaming.com | 78d4486 | 2011-11-09 17:46:33 +0000 | [diff] [blame] | 10 | #include "libGLESv2/utilities.h" |
daniel@transgaming.com | a60160b | 2012-11-28 19:41:15 +0000 | [diff] [blame] | 11 | #include "libGLESv2/renderer/renderer9_utils.h" // D3D9_REPLACE |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 12 | |
alokp@chromium.org | ea0e1af | 2010-03-22 19:33:14 +0000 | [diff] [blame] | 13 | #include "common/debug.h" |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 14 | #include "libEGL/Surface.h" |
| 15 | |
| 16 | #include "libGLESv2/Framebuffer.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 17 | |
| 18 | static DWORD currentTLS = TLS_OUT_OF_INDEXES; |
| 19 | |
daniel@transgaming.com | fad16ed | 2012-10-17 18:24:01 +0000 | [diff] [blame] | 20 | extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 21 | { |
| 22 | switch (reason) |
| 23 | { |
| 24 | case DLL_PROCESS_ATTACH: |
| 25 | { |
| 26 | currentTLS = TlsAlloc(); |
| 27 | |
| 28 | if (currentTLS == TLS_OUT_OF_INDEXES) |
| 29 | { |
| 30 | return FALSE; |
| 31 | } |
| 32 | } |
| 33 | // Fall throught to initialize index |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 34 | case DLL_THREAD_ATTACH: |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 35 | { |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 36 | gl::Current *current = (gl::Current*)LocalAlloc(LPTR, sizeof(gl::Current)); |
| 37 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 38 | if (current) |
| 39 | { |
| 40 | TlsSetValue(currentTLS, current); |
| 41 | |
| 42 | current->context = NULL; |
| 43 | current->display = NULL; |
| 44 | } |
| 45 | } |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 46 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 47 | case DLL_THREAD_DETACH: |
| 48 | { |
| 49 | void *current = TlsGetValue(currentTLS); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 50 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 51 | if (current) |
| 52 | { |
| 53 | LocalFree((HLOCAL)current); |
| 54 | } |
| 55 | } |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 56 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 57 | case DLL_PROCESS_DETACH: |
| 58 | { |
| 59 | void *current = TlsGetValue(currentTLS); |
| 60 | |
| 61 | if (current) |
| 62 | { |
| 63 | LocalFree((HLOCAL)current); |
| 64 | } |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 65 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 66 | TlsFree(currentTLS); |
| 67 | } |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 68 | break; |
| 69 | default: |
| 70 | break; |
| 71 | } |
| 72 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 73 | return TRUE; |
| 74 | } |
| 75 | |
| 76 | namespace gl |
| 77 | { |
| 78 | void makeCurrent(Context *context, egl::Display *display, egl::Surface *surface) |
| 79 | { |
| 80 | Current *current = (Current*)TlsGetValue(currentTLS); |
| 81 | |
| 82 | current->context = context; |
| 83 | current->display = display; |
| 84 | |
| 85 | if (context && display && surface) |
| 86 | { |
daniel@transgaming.com | ad62987 | 2012-11-28 19:32:06 +0000 | [diff] [blame] | 87 | context->makeCurrent(surface); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | Context *getContext() |
| 92 | { |
| 93 | Current *current = (Current*)TlsGetValue(currentTLS); |
| 94 | |
| 95 | return current->context; |
| 96 | } |
| 97 | |
daniel@transgaming.com | 9d78850 | 2011-11-09 17:46:55 +0000 | [diff] [blame] | 98 | Context *getNonLostContext() |
| 99 | { |
| 100 | Context *context = getContext(); |
| 101 | |
daniel@transgaming.com | 82b2891 | 2011-12-12 21:01:35 +0000 | [diff] [blame] | 102 | if (context) |
| 103 | { |
| 104 | if (context->isContextLost()) |
| 105 | { |
| 106 | error(GL_OUT_OF_MEMORY); |
| 107 | return NULL; |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | return context; |
| 112 | } |
| 113 | } |
daniel@transgaming.com | 9d78850 | 2011-11-09 17:46:55 +0000 | [diff] [blame] | 114 | return NULL; |
| 115 | } |
| 116 | |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 117 | egl::Display *getDisplay() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 118 | { |
| 119 | Current *current = (Current*)TlsGetValue(currentTLS); |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 120 | |
| 121 | return current->display; |
| 122 | } |
| 123 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // Records an error code |
| 127 | void error(GLenum errorCode) |
| 128 | { |
| 129 | gl::Context *context = glGetCurrentContext(); |
| 130 | |
| 131 | if (context) |
| 132 | { |
| 133 | switch (errorCode) |
| 134 | { |
| 135 | case GL_INVALID_ENUM: |
| 136 | context->recordInvalidEnum(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 137 | TRACE("\t! Error generated: invalid enum\n"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 138 | break; |
| 139 | case GL_INVALID_VALUE: |
| 140 | context->recordInvalidValue(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 141 | TRACE("\t! Error generated: invalid value\n"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 142 | break; |
| 143 | case GL_INVALID_OPERATION: |
| 144 | context->recordInvalidOperation(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 145 | TRACE("\t! Error generated: invalid operation\n"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 146 | break; |
| 147 | case GL_OUT_OF_MEMORY: |
| 148 | context->recordOutOfMemory(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 149 | TRACE("\t! Error generated: out of memory\n"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 150 | break; |
| 151 | case GL_INVALID_FRAMEBUFFER_OPERATION: |
| 152 | context->recordInvalidFramebufferOperation(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 153 | TRACE("\t! Error generated: invalid framebuffer operation\n"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 154 | break; |
| 155 | default: UNREACHABLE(); |
| 156 | } |
| 157 | } |
| 158 | } |