daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. |
| 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 | |
| 19 | BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) |
| 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 | { |
| 86 | context->makeCurrent(display, surface); |
| 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 | |
| 101 | if (context && !context->isContextLost()) |
| 102 | return context; |
| 103 | |
| 104 | return NULL; |
| 105 | } |
| 106 | |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 107 | egl::Display *getDisplay() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 108 | { |
| 109 | Current *current = (Current*)TlsGetValue(currentTLS); |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 110 | |
| 111 | return current->display; |
| 112 | } |
| 113 | |
| 114 | IDirect3DDevice9 *getDevice() |
| 115 | { |
| 116 | egl::Display *display = getDisplay(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 117 | |
| 118 | return display->getDevice(); |
| 119 | } |
daniel@transgaming.com | 78d4486 | 2011-11-09 17:46:33 +0000 | [diff] [blame] | 120 | |
| 121 | bool checkDeviceLost(HRESULT errorCode) |
| 122 | { |
| 123 | egl::Display *display = NULL; |
| 124 | |
| 125 | if (isDeviceLostError(errorCode)) |
| 126 | { |
daniel@transgaming.com | 09fcc9f | 2011-11-09 17:46:47 +0000 | [diff] [blame] | 127 | display = gl::getDisplay(); |
| 128 | display->notifyDeviceLost(); |
daniel@transgaming.com | 78d4486 | 2011-11-09 17:46:33 +0000 | [diff] [blame] | 129 | return true; |
| 130 | } |
| 131 | return false; |
| 132 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // Records an error code |
| 136 | void error(GLenum errorCode) |
| 137 | { |
| 138 | gl::Context *context = glGetCurrentContext(); |
| 139 | |
| 140 | if (context) |
| 141 | { |
| 142 | switch (errorCode) |
| 143 | { |
| 144 | case GL_INVALID_ENUM: |
| 145 | context->recordInvalidEnum(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 146 | TRACE("\t! Error generated: invalid enum\n"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 147 | break; |
| 148 | case GL_INVALID_VALUE: |
| 149 | context->recordInvalidValue(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 150 | TRACE("\t! Error generated: invalid value\n"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 151 | break; |
| 152 | case GL_INVALID_OPERATION: |
| 153 | context->recordInvalidOperation(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 154 | TRACE("\t! Error generated: invalid operation\n"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 155 | break; |
| 156 | case GL_OUT_OF_MEMORY: |
| 157 | context->recordOutOfMemory(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 158 | TRACE("\t! Error generated: out of memory\n"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 159 | break; |
| 160 | case GL_INVALID_FRAMEBUFFER_OPERATION: |
| 161 | context->recordInvalidFramebufferOperation(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 162 | TRACE("\t! Error generated: invalid framebuffer operation\n"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 163 | break; |
| 164 | default: UNREACHABLE(); |
| 165 | } |
| 166 | } |
| 167 | } |