blob: aaabcbd7ce13ad5af2a0a487b58ff055877e2ecc [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{
Chris Forbes108f3e12018-08-30 19:41:59 -070078Context *getContextLocked()
Nicolas Capens0bac2852016-05-07 06:09:58 -040079{
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
Chris Forbes108f3e12018-08-30 19:41:59 -070091ContextPtr getContext()
92{
93 return ContextPtr{getContextLocked()};
94}
95
Nicolas Capens0bac2852016-05-07 06:09:58 -040096Device *getDevice()
97{
Chris Forbes108f3e12018-08-30 19:41:59 -070098 Context *context = getContextLocked();
Nicolas Capens0bac2852016-05-07 06:09:58 -040099
Nicolas Capens81aa97b2017-06-27 17:08:08 -0400100 return context ? context->getDevice() : nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400101}
102
103// Records an error code
Chris Forbes108f3e12018-08-30 19:41:59 -0700104// Assumed to already hold the context lock for the current context
Nicolas Capens0bac2852016-05-07 06:09:58 -0400105void error(GLenum errorCode)
106{
Chris Forbes108f3e12018-08-30 19:41:59 -0700107 es2::Context *context = es2::getContextLocked();
Nicolas Capens0bac2852016-05-07 06:09:58 -0400108
109 if(context)
110 {
111 switch(errorCode)
112 {
113 case GL_INVALID_ENUM:
114 context->recordInvalidEnum();
115 TRACE("\t! Error generated: invalid enum\n");
116 break;
117 case GL_INVALID_VALUE:
118 context->recordInvalidValue();
119 TRACE("\t! Error generated: invalid value\n");
120 break;
121 case GL_INVALID_OPERATION:
122 context->recordInvalidOperation();
123 TRACE("\t! Error generated: invalid operation\n");
124 break;
125 case GL_OUT_OF_MEMORY:
126 context->recordOutOfMemory();
127 TRACE("\t! Error generated: out of memory\n");
128 break;
129 case GL_INVALID_FRAMEBUFFER_OPERATION:
130 context->recordInvalidFramebufferOperation();
131 TRACE("\t! Error generated: invalid framebuffer operation\n");
132 break;
133 default: UNREACHABLE(errorCode);
134 }
135 }
136}
137}
138
139namespace egl
140{
141GLint getClientVersion()
142{
143 Context *context = libEGL->clientGetCurrentContext();
144
145 return context ? context->getClientVersion() : 0;
146}
147}