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 | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 97 | egl::Display *getDisplay() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 98 | { |
| 99 | Current *current = (Current*)TlsGetValue(currentTLS); |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 100 | |
| 101 | return current->display; |
| 102 | } |
| 103 | |
| 104 | IDirect3DDevice9 *getDevice() |
| 105 | { |
| 106 | egl::Display *display = getDisplay(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 107 | |
| 108 | return display->getDevice(); |
| 109 | } |
daniel@transgaming.com | 78d4486 | 2011-11-09 17:46:33 +0000 | [diff] [blame^] | 110 | |
| 111 | bool checkDeviceLost(HRESULT errorCode) |
| 112 | { |
| 113 | egl::Display *display = NULL; |
| 114 | |
| 115 | if (isDeviceLostError(errorCode)) |
| 116 | { |
| 117 | return true; |
| 118 | } |
| 119 | return false; |
| 120 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // Records an error code |
| 124 | void error(GLenum errorCode) |
| 125 | { |
| 126 | gl::Context *context = glGetCurrentContext(); |
| 127 | |
| 128 | if (context) |
| 129 | { |
| 130 | switch (errorCode) |
| 131 | { |
| 132 | case GL_INVALID_ENUM: |
| 133 | context->recordInvalidEnum(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 134 | TRACE("\t! Error generated: invalid enum\n"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 135 | break; |
| 136 | case GL_INVALID_VALUE: |
| 137 | context->recordInvalidValue(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 138 | TRACE("\t! Error generated: invalid value\n"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 139 | break; |
| 140 | case GL_INVALID_OPERATION: |
| 141 | context->recordInvalidOperation(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 142 | TRACE("\t! Error generated: invalid operation\n"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 143 | break; |
| 144 | case GL_OUT_OF_MEMORY: |
| 145 | context->recordOutOfMemory(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 146 | TRACE("\t! Error generated: out of memory\n"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 147 | break; |
| 148 | case GL_INVALID_FRAMEBUFFER_OPERATION: |
| 149 | context->recordInvalidFramebufferOperation(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 150 | TRACE("\t! Error generated: invalid framebuffer operation\n"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 151 | break; |
| 152 | default: UNREACHABLE(); |
| 153 | } |
| 154 | } |
| 155 | } |