Chia-I Wu | f2001df | 2011-07-02 17:57:30 +0900 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
José Fonseca | 8771285 | 2014-01-17 16:27:50 +0000 | [diff] [blame] | 3 | * Copyright 2008 VMware, Inc. |
Chia-I Wu | f2001df | 2011-07-02 17:57:30 +0900 | [diff] [blame] | 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 | |
Brian Paul | 6052af1 | 2008-05-27 16:48:23 -0600 | [diff] [blame] | 31 | /** |
| 32 | * Functions for choosing and opening/loading device drivers. |
| 33 | */ |
| 34 | |
| 35 | |
Brian Paul | adbff7e | 2005-04-22 21:09:39 +0000 | [diff] [blame] | 36 | #include <assert.h> |
Brian Paul | 11a261e | 2008-05-28 15:50:58 -0600 | [diff] [blame] | 37 | #include <string.h> |
Brian Paul | adbff7e | 2005-04-22 21:09:39 +0000 | [diff] [blame] | 38 | #include <stdio.h> |
Brian Paul | 0c8908c | 2008-05-28 12:56:36 -0600 | [diff] [blame] | 39 | #include <stdlib.h> |
Emil Velikov | efe87f1 | 2015-03-06 16:54:55 +0000 | [diff] [blame] | 40 | #include "c11/threads.h" |
Chia-I Wu | 1e6c10f | 2010-05-31 11:47:58 +0800 | [diff] [blame] | 41 | |
Brian Paul | e084fe5 | 2008-05-28 15:22:17 -0600 | [diff] [blame] | 42 | #include "egldefines.h" |
Brian Paul | adbff7e | 2005-04-22 21:09:39 +0000 | [diff] [blame] | 43 | #include "egldisplay.h" |
| 44 | #include "egldriver.h" |
Brian Paul | b711eb7 | 2005-11-23 01:38:12 +0000 | [diff] [blame] | 45 | #include "egllog.h" |
Brian Paul | adbff7e | 2005-04-22 21:09:39 +0000 | [diff] [blame] | 46 | |
Emil Velikov | efe87f1 | 2015-03-06 16:54:55 +0000 | [diff] [blame] | 47 | static mtx_t _eglModuleMutex = _MTX_INITIALIZER_NP; |
Adam Jackson | b174a1a | 2017-08-28 11:23:58 -0400 | [diff] [blame] | 48 | static _EGLDriver *_eglDriver; |
Chia-I Wu | f2aa361 | 2010-07-04 15:55:12 +0800 | [diff] [blame] | 49 | |
Adam Jackson | b174a1a | 2017-08-28 11:23:58 -0400 | [diff] [blame] | 50 | static _EGLDriver * |
| 51 | _eglGetDriver(void) |
Chia-I Wu | 0eaa02c | 2009-08-13 13:01:48 +0800 | [diff] [blame] | 52 | { |
Adam Jackson | b174a1a | 2017-08-28 11:23:58 -0400 | [diff] [blame] | 53 | mtx_lock(&_eglModuleMutex); |
Chia-I Wu | 0eaa02c | 2009-08-13 13:01:48 +0800 | [diff] [blame] | 54 | |
Eric Engestrom | 8cb84c8 | 2017-09-25 22:35:24 +0100 | [diff] [blame] | 55 | if (!_eglDriver) { |
| 56 | _eglDriver = calloc(1, sizeof(*_eglDriver)); |
| 57 | if (!_eglDriver) |
| 58 | return NULL; |
| 59 | _eglInitDriver(_eglDriver); |
| 60 | } |
Chia-I Wu | c98ea26 | 2011-01-07 16:30:08 +0800 | [diff] [blame] | 61 | |
Adam Jackson | b174a1a | 2017-08-28 11:23:58 -0400 | [diff] [blame] | 62 | mtx_unlock(&_eglModuleMutex); |
Chia-I Wu | 0eaa02c | 2009-08-13 13:01:48 +0800 | [diff] [blame] | 63 | |
Adam Jackson | b174a1a | 2017-08-28 11:23:58 -0400 | [diff] [blame] | 64 | return _eglDriver; |
Brian Paul | adbff7e | 2005-04-22 21:09:39 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Chia-I Wu | 655e459 | 2011-01-13 00:27:45 +0800 | [diff] [blame] | 67 | static _EGLDriver * |
| 68 | _eglMatchAndInitialize(_EGLDisplay *dpy) |
| 69 | { |
Adam Jackson | b174a1a | 2017-08-28 11:23:58 -0400 | [diff] [blame] | 70 | if (_eglGetDriver()) |
| 71 | if (_eglDriver->API.Initialize(_eglDriver, dpy)) |
| 72 | return _eglDriver; |
Chia-I Wu | 655e459 | 2011-01-13 00:27:45 +0800 | [diff] [blame] | 73 | |
Adam Jackson | b174a1a | 2017-08-28 11:23:58 -0400 | [diff] [blame] | 74 | return NULL; |
Chia-I Wu | 655e459 | 2011-01-13 00:27:45 +0800 | [diff] [blame] | 75 | } |
| 76 | |
Chia-I Wu | 655e459 | 2011-01-13 00:27:45 +0800 | [diff] [blame] | 77 | /** |
Eric Engestrom | d7e769a | 2017-10-18 16:31:23 +0100 | [diff] [blame^] | 78 | * Match a display to a driver. The matching is done by finding the first |
| 79 | * driver that can initialize the display. |
Chia-I Wu | f2aa361 | 2010-07-04 15:55:12 +0800 | [diff] [blame] | 80 | */ |
| 81 | _EGLDriver * |
Eric Engestrom | d7e769a | 2017-10-18 16:31:23 +0100 | [diff] [blame^] | 82 | _eglMatchDriver(_EGLDisplay *dpy) |
Chia-I Wu | f2aa361 | 2010-07-04 15:55:12 +0800 | [diff] [blame] | 83 | { |
Chia-I Wu | 655e459 | 2011-01-13 00:27:45 +0800 | [diff] [blame] | 84 | _EGLDriver *best_drv; |
| 85 | |
| 86 | assert(!dpy->Initialized); |
Chia-I Wu | f2aa361 | 2010-07-04 15:55:12 +0800 | [diff] [blame] | 87 | |
Chia-I Wu | 655e459 | 2011-01-13 00:27:45 +0800 | [diff] [blame] | 88 | /* set options */ |
Chia-I Wu | a22a332 | 2011-01-13 04:40:38 +0800 | [diff] [blame] | 89 | dpy->Options.UseFallback = EGL_FALSE; |
Chia-I Wu | f2aa361 | 2010-07-04 15:55:12 +0800 | [diff] [blame] | 90 | |
Chia-I Wu | 655e459 | 2011-01-13 00:27:45 +0800 | [diff] [blame] | 91 | best_drv = _eglMatchAndInitialize(dpy); |
Chia-I Wu | a22a332 | 2011-01-13 04:40:38 +0800 | [diff] [blame] | 92 | if (!best_drv) { |
| 93 | dpy->Options.UseFallback = EGL_TRUE; |
| 94 | best_drv = _eglMatchAndInitialize(dpy); |
| 95 | } |
Chia-I Wu | f2aa361 | 2010-07-04 15:55:12 +0800 | [diff] [blame] | 96 | |
Chia-I Wu | f2aa361 | 2010-07-04 15:55:12 +0800 | [diff] [blame] | 97 | if (best_drv) { |
Eric Engestrom | d7e769a | 2017-10-18 16:31:23 +0100 | [diff] [blame^] | 98 | _eglLog(_EGL_DEBUG, "the best driver is %s", |
| 99 | best_drv->Name); |
| 100 | dpy->Driver = best_drv; |
| 101 | dpy->Initialized = EGL_TRUE; |
Chia-I Wu | f2aa361 | 2010-07-04 15:55:12 +0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | return best_drv; |
| 105 | } |
| 106 | |
Chia-I Wu | f2aa361 | 2010-07-04 15:55:12 +0800 | [diff] [blame] | 107 | __eglMustCastToProperFunctionPointerType |
| 108 | _eglGetDriverProc(const char *procname) |
| 109 | { |
Adam Jackson | b174a1a | 2017-08-28 11:23:58 -0400 | [diff] [blame] | 110 | if (_eglGetDriver()) |
| 111 | return _eglDriver->API.GetProcAddress(_eglDriver, procname); |
Chia-I Wu | f2aa361 | 2010-07-04 15:55:12 +0800 | [diff] [blame] | 112 | |
Adam Jackson | b174a1a | 2017-08-28 11:23:58 -0400 | [diff] [blame] | 113 | return NULL; |
Chia-I Wu | f2aa361 | 2010-07-04 15:55:12 +0800 | [diff] [blame] | 114 | } |
| 115 | |
Chia-I Wu | f2aa361 | 2010-07-04 15:55:12 +0800 | [diff] [blame] | 116 | /** |
| 117 | * Unload all drivers. |
Brian Paul | e3805ca | 2008-05-30 14:50:33 -0600 | [diff] [blame] | 118 | */ |
| 119 | void |
Chia-I Wu | 0eaa02c | 2009-08-13 13:01:48 +0800 | [diff] [blame] | 120 | _eglUnloadDrivers(void) |
Brian Paul | e3805ca | 2008-05-30 14:50:33 -0600 | [diff] [blame] | 121 | { |
Chia-I Wu | 4afe248 | 2010-02-17 19:03:30 +0800 | [diff] [blame] | 122 | /* this is called at atexit time */ |
Eric Engestrom | 9690759 | 2017-09-26 13:13:39 +0100 | [diff] [blame] | 123 | free(_eglDriver); |
Adam Jackson | b174a1a | 2017-08-28 11:23:58 -0400 | [diff] [blame] | 124 | _eglDriver = NULL; |
Kristian Høgsberg | 681fd73 | 2010-05-13 16:06:29 -0400 | [diff] [blame] | 125 | } |