egl: move alloc & init out of _eglBuiltInDriver{DRI2,Haiku}
[mesa.git] / src / egl / main / egldriver.c
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright 2010-2011 LunarG, Inc.
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29
30
31 /**
32 * Functions for choosing and opening/loading device drivers.
33 */
34
35
36 #include <assert.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include "c11/threads.h"
41
42 #include "egldefines.h"
43 #include "egldisplay.h"
44 #include "egldriver.h"
45 #include "egllog.h"
46
47 static mtx_t _eglModuleMutex = _MTX_INITIALIZER_NP;
48 static _EGLDriver *_eglDriver;
49
50 static _EGLDriver *
51 _eglGetDriver(void)
52 {
53 mtx_lock(&_eglModuleMutex);
54
55 if (!_eglDriver) {
56 _eglDriver = calloc(1, sizeof(*_eglDriver));
57 if (!_eglDriver)
58 return NULL;
59 _eglInitDriver(_eglDriver);
60 }
61
62 mtx_unlock(&_eglModuleMutex);
63
64 return _eglDriver;
65 }
66
67 static _EGLDriver *
68 _eglMatchAndInitialize(_EGLDisplay *dpy)
69 {
70 if (_eglGetDriver())
71 if (_eglDriver->API.Initialize(_eglDriver, dpy))
72 return _eglDriver;
73
74 return NULL;
75 }
76
77 /**
78 * Match a display to a driver. The display is initialized unless test_only is
79 * true. The matching is done by finding the first driver that can initialize
80 * the display.
81 */
82 _EGLDriver *
83 _eglMatchDriver(_EGLDisplay *dpy, EGLBoolean test_only)
84 {
85 _EGLDriver *best_drv;
86
87 assert(!dpy->Initialized);
88
89 /* set options */
90 dpy->Options.TestOnly = test_only;
91 dpy->Options.UseFallback = EGL_FALSE;
92
93 best_drv = _eglMatchAndInitialize(dpy);
94 if (!best_drv) {
95 dpy->Options.UseFallback = EGL_TRUE;
96 best_drv = _eglMatchAndInitialize(dpy);
97 }
98
99 if (best_drv) {
100 _eglLog(_EGL_DEBUG, "the best driver is %s%s",
101 best_drv->Name, (test_only) ? " (test only) " : "");
102 if (!test_only) {
103 dpy->Driver = best_drv;
104 dpy->Initialized = EGL_TRUE;
105 }
106 }
107
108 return best_drv;
109 }
110
111 __eglMustCastToProperFunctionPointerType
112 _eglGetDriverProc(const char *procname)
113 {
114 if (_eglGetDriver())
115 return _eglDriver->API.GetProcAddress(_eglDriver, procname);
116
117 return NULL;
118 }
119
120 /**
121 * Unload all drivers.
122 */
123 void
124 _eglUnloadDrivers(void)
125 {
126 /* this is called at atexit time */
127 free(_eglDriver);
128 _eglDriver = NULL;
129 }