Merge remote branch 'origin/master' into radeon-rewrite
[mesa.git] / src / egl / main / egldisplay.c
1
2 /**
3 * Functions related to EGLDisplay.
4 */
5
6 #include <assert.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include "eglcontext.h"
10 #include "egldisplay.h"
11 #include "egldriver.h"
12 #include "eglglobals.h"
13 #include "eglhash.h"
14 #include "eglstring.h"
15
16
17 /**
18 * Allocate a new _EGLDisplay object for the given nativeDisplay handle.
19 * We'll also try to determine the device driver name at this time.
20 *
21 * Note that nativeDisplay may be an X Display ptr, or a string.
22 */
23 _EGLDisplay *
24 _eglNewDisplay(NativeDisplayType nativeDisplay)
25 {
26 _EGLDisplay *dpy = (_EGLDisplay *) calloc(1, sizeof(_EGLDisplay));
27 if (dpy) {
28 EGLuint key = _eglHashGenKey(_eglGlobal.Displays);
29
30 dpy->Handle = (EGLDisplay) key;
31 _eglHashInsert(_eglGlobal.Displays, key, dpy);
32
33 dpy->NativeDisplay = nativeDisplay;
34 #if defined(_EGL_PLATFORM_X)
35 dpy->Xdpy = (Display *) nativeDisplay;
36 #endif
37
38 dpy->DriverName = _eglChooseDriver(dpy);
39 if (!dpy->DriverName) {
40 free(dpy);
41 return NULL;
42 }
43 }
44 return dpy;
45 }
46
47
48 /**
49 * Return the public handle for an internal _EGLDisplay.
50 * This is the inverse of _eglLookupDisplay().
51 */
52 EGLDisplay
53 _eglGetDisplayHandle(_EGLDisplay *display)
54 {
55 if (display)
56 return display->Handle;
57 else
58 return EGL_NO_DISPLAY;
59 }
60
61
62 /**
63 * Return the _EGLDisplay object that corresponds to the given public/
64 * opaque display handle.
65 * This is the inverse of _eglGetDisplayHandle().
66 */
67 _EGLDisplay *
68 _eglLookupDisplay(EGLDisplay dpy)
69 {
70 EGLuint key = (EGLuint) dpy;
71 if (!_eglGlobal.Displays)
72 return NULL;
73 return (_EGLDisplay *) _eglHashLookup(_eglGlobal.Displays, key);
74 }
75
76
77 void
78 _eglSaveDisplay(_EGLDisplay *dpy)
79 {
80 EGLuint key = _eglHashGenKey(_eglGlobal.Displays);
81 assert(dpy);
82 assert(!dpy->Handle);
83 dpy->Handle = (EGLDisplay) key;
84 assert(dpy->Handle);
85 _eglHashInsert(_eglGlobal.Displays, key, dpy);
86 }
87
88
89 _EGLDisplay *
90 _eglGetCurrentDisplay(void)
91 {
92 _EGLContext *ctx = _eglGetCurrentContext();
93 if (ctx)
94 return ctx->Display;
95 else
96 return NULL;
97 }
98
99
100 /**
101 * Free all the data hanging of an _EGLDisplay object, but not
102 * the object itself.
103 */
104 void
105 _eglCleanupDisplay(_EGLDisplay *disp)
106 {
107 EGLint i;
108
109 for (i = 0; i < disp->NumConfigs; i++) {
110 free(disp->Configs[i]);
111 }
112 free(disp->Configs);
113 disp->Configs = NULL;
114
115 /* XXX incomplete */
116
117 free((void *) disp->DriverName);
118 disp->DriverName = NULL;
119
120 /* driver deletes the _EGLDisplay object */
121 }