Home | History | Annotate | Download | only in libGLESv2
      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 
      9 #include "libGLESv2/main.h"
     10 
     11 #include "common/debug.h"
     12 #include "libEGL/Surface.h"
     13 
     14 #include "libGLESv2/Framebuffer.h"
     15 
     16 static DWORD currentTLS = TLS_OUT_OF_INDEXES;
     17 
     18 BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
     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
     32       case DLL_THREAD_ATTACH:
     33         {
     34             gl::Current *current = (gl::Current*)LocalAlloc(LPTR, sizeof(gl::Current));
     35 
     36             if (current)
     37             {
     38                 TlsSetValue(currentTLS, current);
     39 
     40                 current->context = NULL;
     41                 current->display = NULL;
     42             }
     43         }
     44         break;
     45       case DLL_THREAD_DETACH:
     46         {
     47             void *current = TlsGetValue(currentTLS);
     48 
     49             if (current)
     50             {
     51                 LocalFree((HLOCAL)current);
     52             }
     53         }
     54         break;
     55       case DLL_PROCESS_DETACH:
     56         {
     57             void *current = TlsGetValue(currentTLS);
     58 
     59             if (current)
     60             {
     61                 LocalFree((HLOCAL)current);
     62             }
     63 
     64             TlsFree(currentTLS);
     65         }
     66         break;
     67       default:
     68         break;
     69     }
     70 
     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     {
     85         context->makeCurrent(display, surface);
     86     }
     87 }
     88 
     89 Context *getContext()
     90 {
     91     Current *current = (Current*)TlsGetValue(currentTLS);
     92 
     93     return current->context;
     94 }
     95 
     96 egl::Display *getDisplay()
     97 {
     98     Current *current = (Current*)TlsGetValue(currentTLS);
     99 
    100     return current->display;
    101 }
    102 
    103 IDirect3DDevice9 *getDevice()
    104 {
    105     egl::Display *display = getDisplay();
    106 
    107     return display->getDevice();
    108 }
    109 }
    110 
    111 // Records an error code
    112 void error(GLenum errorCode)
    113 {
    114     gl::Context *context = glGetCurrentContext();
    115 
    116     if (context)
    117     {
    118         switch (errorCode)
    119         {
    120           case GL_INVALID_ENUM:
    121             context->recordInvalidEnum();
    122             gl::trace("\t! Error generated: invalid enum\n");
    123             break;
    124           case GL_INVALID_VALUE:
    125             context->recordInvalidValue();
    126             gl::trace("\t! Error generated: invalid value\n");
    127             break;
    128           case GL_INVALID_OPERATION:
    129             context->recordInvalidOperation();
    130             gl::trace("\t! Error generated: invalid operation\n");
    131             break;
    132           case GL_OUT_OF_MEMORY:
    133             context->recordOutOfMemory();
    134             gl::trace("\t! Error generated: out of memory\n");
    135             break;
    136           case GL_INVALID_FRAMEBUFFER_OPERATION:
    137             context->recordInvalidFramebufferOperation();
    138             gl::trace("\t! Error generated: invalid framebuffer operation\n");
    139             break;
    140           default: UNREACHABLE();
    141         }
    142     }
    143 }
    144