blob: 003f74ed63d577fd2d0b98e2902c3456954f075a [file] [log] [blame]
Chia-I Wuf2001df2011-07-02 17:57:30 +09001/**************************************************************************
2 *
José Fonseca87712852014-01-17 16:27:50 +00003 * Copyright 2008 VMware, Inc.
Chia-I Wuf2001df2011-07-02 17:57:30 +09004 * 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
Brian Paul6052af12008-05-27 16:48:23 -060031/**
32 * Functions for choosing and opening/loading device drivers.
33 */
34
35
Brian Pauladbff7e2005-04-22 21:09:39 +000036#include <assert.h>
Brian Paul11a261e2008-05-28 15:50:58 -060037#include <string.h>
Brian Pauladbff7e2005-04-22 21:09:39 +000038#include <stdio.h>
Brian Paul0c8908c2008-05-28 12:56:36 -060039#include <stdlib.h>
Emil Velikovefe87f12015-03-06 16:54:55 +000040#include "c11/threads.h"
Chia-I Wu1e6c10f2010-05-31 11:47:58 +080041
Brian Paule084fe52008-05-28 15:22:17 -060042#include "egldefines.h"
Brian Pauladbff7e2005-04-22 21:09:39 +000043#include "egldisplay.h"
44#include "egldriver.h"
Brian Paulb711eb72005-11-23 01:38:12 +000045#include "egllog.h"
Brian Pauladbff7e2005-04-22 21:09:39 +000046
Emil Velikovefe87f12015-03-06 16:54:55 +000047static mtx_t _eglModuleMutex = _MTX_INITIALIZER_NP;
Adam Jacksonb174a1a2017-08-28 11:23:58 -040048static _EGLDriver *_eglDriver;
Chia-I Wuf2aa3612010-07-04 15:55:12 +080049
Adam Jacksonb174a1a2017-08-28 11:23:58 -040050static _EGLDriver *
51_eglGetDriver(void)
Chia-I Wu0eaa02c2009-08-13 13:01:48 +080052{
Adam Jacksonb174a1a2017-08-28 11:23:58 -040053 mtx_lock(&_eglModuleMutex);
Chia-I Wu0eaa02c2009-08-13 13:01:48 +080054
Eric Engestrom8cb84c82017-09-25 22:35:24 +010055 if (!_eglDriver) {
56 _eglDriver = calloc(1, sizeof(*_eglDriver));
57 if (!_eglDriver)
58 return NULL;
59 _eglInitDriver(_eglDriver);
60 }
Chia-I Wuc98ea262011-01-07 16:30:08 +080061
Adam Jacksonb174a1a2017-08-28 11:23:58 -040062 mtx_unlock(&_eglModuleMutex);
Chia-I Wu0eaa02c2009-08-13 13:01:48 +080063
Adam Jacksonb174a1a2017-08-28 11:23:58 -040064 return _eglDriver;
Brian Pauladbff7e2005-04-22 21:09:39 +000065}
66
Chia-I Wu655e4592011-01-13 00:27:45 +080067static _EGLDriver *
68_eglMatchAndInitialize(_EGLDisplay *dpy)
69{
Adam Jacksonb174a1a2017-08-28 11:23:58 -040070 if (_eglGetDriver())
71 if (_eglDriver->API.Initialize(_eglDriver, dpy))
72 return _eglDriver;
Chia-I Wu655e4592011-01-13 00:27:45 +080073
Adam Jacksonb174a1a2017-08-28 11:23:58 -040074 return NULL;
Chia-I Wu655e4592011-01-13 00:27:45 +080075}
76
Chia-I Wu655e4592011-01-13 00:27:45 +080077/**
Eric Engestromd7e769a2017-10-18 16:31:23 +010078 * Match a display to a driver. The matching is done by finding the first
79 * driver that can initialize the display.
Chia-I Wuf2aa3612010-07-04 15:55:12 +080080 */
81_EGLDriver *
Eric Engestromd7e769a2017-10-18 16:31:23 +010082_eglMatchDriver(_EGLDisplay *dpy)
Chia-I Wuf2aa3612010-07-04 15:55:12 +080083{
Chia-I Wu655e4592011-01-13 00:27:45 +080084 _EGLDriver *best_drv;
85
86 assert(!dpy->Initialized);
Chia-I Wuf2aa3612010-07-04 15:55:12 +080087
Chia-I Wu655e4592011-01-13 00:27:45 +080088 /* set options */
Chia-I Wua22a3322011-01-13 04:40:38 +080089 dpy->Options.UseFallback = EGL_FALSE;
Chia-I Wuf2aa3612010-07-04 15:55:12 +080090
Chia-I Wu655e4592011-01-13 00:27:45 +080091 best_drv = _eglMatchAndInitialize(dpy);
Chia-I Wua22a3322011-01-13 04:40:38 +080092 if (!best_drv) {
93 dpy->Options.UseFallback = EGL_TRUE;
94 best_drv = _eglMatchAndInitialize(dpy);
95 }
Chia-I Wuf2aa3612010-07-04 15:55:12 +080096
Chia-I Wuf2aa3612010-07-04 15:55:12 +080097 if (best_drv) {
Eric Engestromd7e769a2017-10-18 16:31:23 +010098 _eglLog(_EGL_DEBUG, "the best driver is %s",
99 best_drv->Name);
100 dpy->Driver = best_drv;
101 dpy->Initialized = EGL_TRUE;
Chia-I Wuf2aa3612010-07-04 15:55:12 +0800102 }
103
104 return best_drv;
105}
106
Chia-I Wuf2aa3612010-07-04 15:55:12 +0800107__eglMustCastToProperFunctionPointerType
108_eglGetDriverProc(const char *procname)
109{
Adam Jacksonb174a1a2017-08-28 11:23:58 -0400110 if (_eglGetDriver())
111 return _eglDriver->API.GetProcAddress(_eglDriver, procname);
Chia-I Wuf2aa3612010-07-04 15:55:12 +0800112
Adam Jacksonb174a1a2017-08-28 11:23:58 -0400113 return NULL;
Chia-I Wuf2aa3612010-07-04 15:55:12 +0800114}
115
Chia-I Wuf2aa3612010-07-04 15:55:12 +0800116/**
117 * Unload all drivers.
Brian Paule3805ca2008-05-30 14:50:33 -0600118 */
119void
Chia-I Wu0eaa02c2009-08-13 13:01:48 +0800120_eglUnloadDrivers(void)
Brian Paule3805ca2008-05-30 14:50:33 -0600121{
Chia-I Wu4afe2482010-02-17 19:03:30 +0800122 /* this is called at atexit time */
Eric Engestrom96907592017-09-26 13:13:39 +0100123 free(_eglDriver);
Adam Jacksonb174a1a2017-08-28 11:23:58 -0400124 _eglDriver = NULL;
Kristian Høgsberg681fd732010-05-13 16:06:29 -0400125}