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