blob: 0924a75c98624998704eb3691fb4ff99af5468a0 [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// 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
Corentin Wallezb0397112017-10-26 19:27:22 -040015// main.cpp: DLLMain and management of thread-local data.
Nicolas Capens0bac2852016-05-07 06:09:58 -040016
17#include "main.h"
18
Nicolas Capens0bac2852016-05-07 06:09:58 -040019#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
27static void glAttachThread()
28{
29 TRACE("()");
30}
31
32static void glDetachThread()
33{
34 TRACE("()");
35}
36
37CONSTRUCTOR static void glAttachProcess()
38{
39 TRACE("()");
40
41 glAttachThread();
42}
43
44DESTRUCTOR static void glDetachProcess()
45{
46 TRACE("()");
47
48 glDetachThread();
49}
50
51#if defined(_WIN32)
52extern "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
76namespace es2
77{
78es2::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
Nicolas Capens81aa97b2017-06-27 17:08:08 -040088 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -040089}
90
91Device *getDevice()
92{
93 Context *context = getContext();
94
Nicolas Capens81aa97b2017-06-27 17:08:08 -040095 return context ? context->getDevice() : nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -040096}
97
98// Records an error code
99void 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
133namespace egl
134{
135GLint getClientVersion()
136{
137 Context *context = libEGL->clientGetCurrentContext();
138
139 return context ? context->getClientVersion() : 0;
140}
141}