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