egl: fold _eglError() + return EGL_FALSE
[mesa.git] / src / egl / drivers / haiku / egl_haiku.cpp
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2014 Adrián Arroyo Calle <adrian.arroyocalle@gmail.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <errno.h>
26 #include <dlfcn.h>
27 #include <stdint.h>
28 #include <stdio.h>
29
30 #include "eglconfig.h"
31 #include "eglcontext.h"
32 #include "egldisplay.h"
33 #include "egldriver.h"
34 #include "eglcurrent.h"
35 #include "egllog.h"
36 #include "eglsurface.h"
37 #include "eglimage.h"
38 #include "egltypedefs.h"
39
40 #include <InterfaceKit.h>
41 #include <OpenGLKit.h>
42
43
44 #ifdef DEBUG
45 # define TRACE(x...) printf("egl_haiku: " x)
46 # define CALLED() TRACE("CALLED: %s\n", __PRETTY_FUNCTION__)
47 #else
48 # define TRACE(x...)
49 # define CALLED()
50 #endif
51 #define ERROR(x...) printf("egl_haiku: " x)
52
53
54 _EGL_DRIVER_STANDARD_TYPECASTS(haiku_egl)
55
56
57 struct haiku_egl_driver
58 {
59 _EGLDriver base;
60 };
61
62 struct haiku_egl_config
63 {
64 _EGLConfig base;
65 };
66
67 struct haiku_egl_context
68 {
69 _EGLContext ctx;
70 };
71
72 struct haiku_egl_surface
73 {
74 _EGLSurface surf;
75 BGLView* gl;
76 };
77
78
79 /**
80 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
81 */
82 static _EGLSurface *
83 haiku_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
84 _EGLConfig *conf, void *native_window, const EGLint *attrib_list)
85 {
86 CALLED();
87
88 struct haiku_egl_surface* surface;
89 surface = (struct haiku_egl_surface*) calloc(1, sizeof (*surface));
90 if (!surface) {
91 _eglError(EGL_BAD_ALLOC, "haiku_create_window_surface");
92 return NULL;
93 }
94
95 if (!_eglInitSurface(&surface->surf, disp, EGL_WINDOW_BIT,
96 conf, attrib_list)) {
97 free(surface);
98 return NULL;
99 }
100
101 (&surface->surf)->SwapInterval = 1;
102
103 TRACE("Creating window\n");
104 BWindow* win = (BWindow*)native_window;
105
106 TRACE("Creating GL view\n");
107 surface->gl = new BGLView(win->Bounds(), "OpenGL", B_FOLLOW_ALL_SIDES, 0,
108 BGL_RGB | BGL_DOUBLE | BGL_ALPHA);
109
110 TRACE("Adding GL\n");
111 win->AddChild(surface->gl);
112
113 TRACE("Showing window\n");
114 win->Show();
115 return &surface->surf;
116 }
117
118
119 static _EGLSurface *
120 haiku_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
121 _EGLConfig *conf, void *native_pixmap, const EGLint *attrib_list)
122 {
123 return NULL;
124 }
125
126
127 static _EGLSurface *
128 haiku_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
129 _EGLConfig *conf, const EGLint *attrib_list)
130 {
131 return NULL;
132 }
133
134
135 static EGLBoolean
136 haiku_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
137 {
138 if (_eglPutSurface(surf)) {
139 // XXX: detach haiku_egl_surface::gl from the native window and destroy it
140 free(surf);
141 }
142 return EGL_TRUE;
143 }
144
145
146 static EGLBoolean
147 haiku_add_configs_for_visuals(_EGLDisplay *dpy)
148 {
149 CALLED();
150
151 struct haiku_egl_config* conf;
152 conf = (struct haiku_egl_config*) calloc(1, sizeof (*conf));
153 if (!conf)
154 return _eglError(EGL_BAD_ALLOC, "haiku_add_configs_for_visuals");
155
156 _eglInitConfig(&conf->base, dpy, 1);
157 TRACE("Config inited\n");
158
159 _eglSetConfigKey(&conf->base, EGL_RED_SIZE, 8);
160 _eglSetConfigKey(&conf->base, EGL_BLUE_SIZE, 8);
161 _eglSetConfigKey(&conf->base, EGL_GREEN_SIZE, 8);
162 _eglSetConfigKey(&conf->base, EGL_LUMINANCE_SIZE, 0);
163 _eglSetConfigKey(&conf->base, EGL_ALPHA_SIZE, 8);
164 _eglSetConfigKey(&conf->base, EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER);
165 EGLint r = (_eglGetConfigKey(&conf->base, EGL_RED_SIZE)
166 + _eglGetConfigKey(&conf->base, EGL_GREEN_SIZE)
167 + _eglGetConfigKey(&conf->base, EGL_BLUE_SIZE)
168 + _eglGetConfigKey(&conf->base, EGL_ALPHA_SIZE));
169 _eglSetConfigKey(&conf->base, EGL_BUFFER_SIZE, r);
170 _eglSetConfigKey(&conf->base, EGL_CONFIG_CAVEAT, EGL_NONE);
171 _eglSetConfigKey(&conf->base, EGL_CONFIG_ID, 1);
172 _eglSetConfigKey(&conf->base, EGL_BIND_TO_TEXTURE_RGB, EGL_FALSE);
173 _eglSetConfigKey(&conf->base, EGL_BIND_TO_TEXTURE_RGBA, EGL_FALSE);
174 _eglSetConfigKey(&conf->base, EGL_STENCIL_SIZE, 0);
175 _eglSetConfigKey(&conf->base, EGL_TRANSPARENT_TYPE, EGL_NONE);
176 _eglSetConfigKey(&conf->base, EGL_NATIVE_RENDERABLE, EGL_TRUE); // Let's say yes
177 _eglSetConfigKey(&conf->base, EGL_NATIVE_VISUAL_ID, 0); // No visual
178 _eglSetConfigKey(&conf->base, EGL_NATIVE_VISUAL_TYPE, EGL_NONE); // No visual
179 _eglSetConfigKey(&conf->base, EGL_RENDERABLE_TYPE, 0x8);
180 _eglSetConfigKey(&conf->base, EGL_SAMPLE_BUFFERS, 0); // TODO: How to get the right value ?
181 _eglSetConfigKey(&conf->base, EGL_SAMPLES, _eglGetConfigKey(&conf->base, EGL_SAMPLE_BUFFERS) == 0 ? 0 : 0);
182 _eglSetConfigKey(&conf->base, EGL_DEPTH_SIZE, 24); // TODO: How to get the right value ?
183 _eglSetConfigKey(&conf->base, EGL_LEVEL, 0);
184 _eglSetConfigKey(&conf->base, EGL_MAX_PBUFFER_WIDTH, 0); // TODO: How to get the right value ?
185 _eglSetConfigKey(&conf->base, EGL_MAX_PBUFFER_HEIGHT, 0); // TODO: How to get the right value ?
186 _eglSetConfigKey(&conf->base, EGL_MAX_PBUFFER_PIXELS, 0); // TODO: How to get the right value ?
187 _eglSetConfigKey(&conf->base, EGL_SURFACE_TYPE, EGL_WINDOW_BIT /*| EGL_PIXMAP_BIT | EGL_PBUFFER_BIT*/);
188
189 TRACE("Config configuated\n");
190 if (!_eglValidateConfig(&conf->base, EGL_FALSE)) {
191 _eglLog(_EGL_DEBUG, "Haiku: failed to validate config");
192 goto cleanup;
193 }
194 TRACE("Validated config\n");
195
196 _eglLinkConfig(&conf->base);
197 if (!_eglGetArraySize(dpy->Configs)) {
198 _eglLog(_EGL_WARNING, "Haiku: failed to create any config");
199 goto cleanup;
200 }
201 TRACE("Config successfull\n");
202
203 return EGL_TRUE;
204
205 cleanup:
206 free(conf);
207 return EGL_FALSE;
208 }
209
210
211 extern "C"
212 EGLBoolean
213 init_haiku(_EGLDriver *drv, _EGLDisplay *dpy)
214 {
215 CALLED();
216
217 TRACE("Add configs\n");
218 if (!haiku_add_configs_for_visuals(dpy))
219 return EGL_FALSE;
220
221 dpy->Version = 14;
222
223 TRACE("Initialization finished\n");
224
225 return EGL_TRUE;
226 }
227
228
229 extern "C"
230 EGLBoolean
231 haiku_terminate(_EGLDriver* drv,_EGLDisplay* dpy)
232 {
233 return EGL_TRUE;
234 }
235
236
237 extern "C"
238 _EGLContext*
239 haiku_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
240 _EGLContext *share_list, const EGLint *attrib_list)
241 {
242 CALLED();
243
244 struct haiku_egl_context* context;
245 context = (struct haiku_egl_context*) calloc(1, sizeof (*context));
246 if (!context) {
247 _eglError(EGL_BAD_ALLOC, "haiku_create_context");
248 return NULL;
249 }
250
251 if (!_eglInitContext(&context->ctx, disp, conf, attrib_list))
252 goto cleanup;
253
254 TRACE("Context created\n");
255 return &context->ctx;
256
257 cleanup:
258 free(context);
259 return NULL;
260 }
261
262
263 extern "C"
264 EGLBoolean
265 haiku_destroy_context(_EGLDriver* drv, _EGLDisplay *disp, _EGLContext* ctx)
266 {
267 struct haiku_egl_context* context = haiku_egl_context(ctx);
268
269 if (_eglPutContext(ctx)) {
270 // XXX: teardown the context ?
271 free(context);
272 ctx = NULL;
273 }
274 return EGL_TRUE;
275 }
276
277
278 extern "C"
279 EGLBoolean
280 haiku_make_current(_EGLDriver* drv, _EGLDisplay* dpy, _EGLSurface *dsurf,
281 _EGLSurface *rsurf, _EGLContext *ctx)
282 {
283 CALLED();
284
285 struct haiku_egl_context* cont = haiku_egl_context(ctx);
286 struct haiku_egl_surface* surf = haiku_egl_surface(dsurf);
287 _EGLContext *old_ctx;
288 _EGLSurface *old_dsurf, *old_rsurf;
289
290 if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf))
291 return EGL_FALSE;
292
293 //cont->ctx.DrawSurface=&surf->surf;
294 surf->gl->LockGL();
295 return EGL_TRUE;
296 }
297
298
299 extern "C"
300 EGLBoolean
301 haiku_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
302 {
303 struct haiku_egl_surface* surface = haiku_egl_surface(surf);
304
305 surface->gl->SwapBuffers();
306 //gl->Render();
307 return EGL_TRUE;
308 }
309
310
311 extern "C"
312 void
313 haiku_unload(_EGLDriver* drv)
314 {
315
316 }
317
318
319 /**
320 * This is the main entrypoint into the driver, called by libEGL.
321 * Create a new _EGLDriver object and init its dispatch table.
322 */
323 extern "C"
324 _EGLDriver*
325 _eglBuiltInDriverHaiku(const char *args)
326 {
327 CALLED();
328
329 struct haiku_egl_driver* driver;
330 driver = (struct haiku_egl_driver*) calloc(1, sizeof(*driver));
331 if (!driver) {
332 _eglError(EGL_BAD_ALLOC, "_eglBuiltInDriverHaiku");
333 return NULL;
334 }
335
336 _eglInitDriverFallbacks(&driver->base);
337 driver->base.API.Initialize = init_haiku;
338 driver->base.API.Terminate = haiku_terminate;
339 driver->base.API.CreateContext = haiku_create_context;
340 driver->base.API.DestroyContext = haiku_destroy_context;
341 driver->base.API.MakeCurrent = haiku_make_current;
342 driver->base.API.CreateWindowSurface = haiku_create_window_surface;
343 driver->base.API.CreatePixmapSurface = haiku_create_pixmap_surface;
344 driver->base.API.CreatePbufferSurface = haiku_create_pbuffer_surface;
345 driver->base.API.DestroySurface = haiku_destroy_surface;
346
347 driver->base.API.SwapBuffers = haiku_swap_buffers;
348
349 driver->base.Name = "Haiku";
350 driver->base.Unload = haiku_unload;
351
352 TRACE("API Calls defined\n");
353
354 return &driver->base;
355 }