9c42194c6119019718aea01979ab388d1ad7b065
[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
15
16 static char *
17 my_strdup(const char *s)
18 {
19 if (s) {
20 int l = strlen(s);
21 char *s2 = malloc(l + 1);
22 if (s2)
23 strcpy(s2, s);
24 return s2;
25 }
26 return NULL;
27 }
28
29
30 /**
31 * Allocate a new _EGLDisplay object for the given nativeDisplay handle.
32 * We'll also try to determine the device driver name at this time.
33 */
34 _EGLDisplay *
35 _eglNewDisplay(NativeDisplayType nativeDisplay)
36 {
37 _EGLDisplay *dpy = (_EGLDisplay *) calloc(1, sizeof(_EGLDisplay));
38 if (dpy) {
39 EGLuint key = _eglHashGenKey(_eglGlobal.Displays);
40
41 dpy->Handle = (EGLDisplay) key;
42 _eglHashInsert(_eglGlobal.Displays, key, dpy);
43
44 dpy->NativeDisplay = nativeDisplay;
45 #if defined(_EGL_PLATFORM_X)
46 dpy->Xdpy = (Display *) nativeDisplay;
47 #endif
48
49 dpy->DriverName = my_strdup(_eglChooseDriver(dpy));
50 if (!dpy->DriverName) {
51 free(dpy);
52 return NULL;
53 }
54 }
55 return dpy;
56 }
57
58
59 /**
60 * Return the public handle for an internal _EGLDisplay.
61 * This is the inverse of _eglLookupDisplay().
62 */
63 EGLDisplay
64 _eglGetDisplayHandle(_EGLDisplay *display)
65 {
66 if (display)
67 return display->Handle;
68 else
69 return EGL_NO_DISPLAY;
70 }
71
72
73 /**
74 * Return the _EGLDisplay object that corresponds to the given public/
75 * opaque display handle.
76 * This is the inverse of _eglGetDisplayHandle().
77 */
78 _EGLDisplay *
79 _eglLookupDisplay(EGLDisplay dpy)
80 {
81 EGLuint key = (EGLuint) dpy;
82 _EGLDisplay *d = (_EGLDisplay *) _eglHashLookup(_eglGlobal.Displays, key);
83 return d;
84 }
85
86
87 void
88 _eglSaveDisplay(_EGLDisplay *dpy)
89 {
90 EGLuint key = _eglHashGenKey(_eglGlobal.Displays);
91 assert(dpy);
92 assert(!dpy->Handle);
93 dpy->Handle = (EGLDisplay) key;
94 assert(dpy->Handle);
95 _eglHashInsert(_eglGlobal.Displays, key, dpy);
96 }
97
98
99 _EGLDisplay *
100 _eglGetCurrentDisplay(void)
101 {
102 _EGLContext *ctx = _eglGetCurrentContext();
103 if (ctx)
104 return ctx->Display;
105 else
106 return NULL;
107 }
108
109
110 void
111 _eglCleanupDisplay(_EGLDisplay *disp)
112 {
113 /* XXX incomplete */
114 free(disp->Configs);
115 free((void *) disp->DriverName);
116 /* driver deletes _EGLDisplay */
117 }