Home | History | Annotate | Download | only in libGLESv2
      1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //    http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 // main.cpp: DLLMain and management of thread-local data.
     16 
     17 #include "main.h"
     18 
     19 #if !defined(_MSC_VER)
     20 #define CONSTRUCTOR __attribute__((constructor))
     21 #define DESTRUCTOR __attribute__((destructor))
     22 #else
     23 #define CONSTRUCTOR
     24 #define DESTRUCTOR
     25 #endif
     26 
     27 static void glAttachThread()
     28 {
     29 	TRACE("()");
     30 }
     31 
     32 static void glDetachThread()
     33 {
     34 	TRACE("()");
     35 }
     36 
     37 CONSTRUCTOR static void glAttachProcess()
     38 {
     39 	TRACE("()");
     40 
     41 	glAttachThread();
     42 }
     43 
     44 DESTRUCTOR static void glDetachProcess()
     45 {
     46 	TRACE("()");
     47 
     48 	glDetachThread();
     49 }
     50 
     51 #if defined(_WIN32)
     52 extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
     53 {
     54 	switch(reason)
     55 	{
     56 	case DLL_PROCESS_ATTACH:
     57 		glAttachProcess();
     58 		break;
     59 	case DLL_THREAD_ATTACH:
     60 		glAttachThread();
     61 		break;
     62 	case DLL_THREAD_DETACH:
     63 		glDetachThread();
     64 		break;
     65 	case DLL_PROCESS_DETACH:
     66 		glDetachProcess();
     67 		break;
     68 	default:
     69 		break;
     70 	}
     71 
     72 	return TRUE;
     73 }
     74 #endif
     75 
     76 namespace es2
     77 {
     78 es2::Context *getContext()
     79 {
     80 	egl::Context *context = libEGL->clientGetCurrentContext();
     81 
     82 	if(context && (context->getClientVersion() == 2 ||
     83 	               context->getClientVersion() == 3))
     84 	{
     85 		return static_cast<es2::Context*>(context);
     86 	}
     87 
     88 	return nullptr;
     89 }
     90 
     91 Device *getDevice()
     92 {
     93 	Context *context = getContext();
     94 
     95 	return context ? context->getDevice() : nullptr;
     96 }
     97 
     98 // Records an error code
     99 void error(GLenum errorCode)
    100 {
    101 	es2::Context *context = es2::getContext();
    102 
    103 	if(context)
    104 	{
    105 		switch(errorCode)
    106 		{
    107 		case GL_INVALID_ENUM:
    108 			context->recordInvalidEnum();
    109 			TRACE("\t! Error generated: invalid enum\n");
    110 			break;
    111 		case GL_INVALID_VALUE:
    112 			context->recordInvalidValue();
    113 			TRACE("\t! Error generated: invalid value\n");
    114 			break;
    115 		case GL_INVALID_OPERATION:
    116 			context->recordInvalidOperation();
    117 			TRACE("\t! Error generated: invalid operation\n");
    118 			break;
    119 		case GL_OUT_OF_MEMORY:
    120 			context->recordOutOfMemory();
    121 			TRACE("\t! Error generated: out of memory\n");
    122 			break;
    123 		case GL_INVALID_FRAMEBUFFER_OPERATION:
    124 			context->recordInvalidFramebufferOperation();
    125 			TRACE("\t! Error generated: invalid framebuffer operation\n");
    126 			break;
    127 		default: UNREACHABLE(errorCode);
    128 		}
    129 	}
    130 }
    131 }
    132 
    133 namespace egl
    134 {
    135 GLint getClientVersion()
    136 {
    137 	Context *context = libEGL->clientGetCurrentContext();
    138 
    139 	return context ? context->getClientVersion() : 0;
    140 }
    141 }
    142