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