egl/haiku: we don't use src/loader, drop all the references to it
[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 static void
80 haiku_log(EGLint level, const char *msg)
81 {
82 switch (level) {
83 case _EGL_DEBUG:
84 fprintf(stderr,"%s", msg);
85 break;
86 case _EGL_INFO:
87 fprintf(stderr,"%s", msg);
88 break;
89 case _EGL_WARNING:
90 fprintf(stderr,"%s", msg);
91 break;
92 case _EGL_FATAL:
93 fprintf(stderr,"%s", msg);
94 break;
95 default:
96 break;
97 }
98 }
99
100
101 /**
102 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
103 */
104 static _EGLSurface *
105 haiku_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
106 _EGLConfig *conf, void *native_surface, const EGLint *attrib_list)
107 {
108 return NULL;
109 }
110
111
112 /**
113 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
114 */
115 static _EGLSurface *
116 haiku_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
117 _EGLConfig *conf, void *native_window, const EGLint *attrib_list)
118 {
119 CALLED();
120
121 struct haiku_egl_surface* surface;
122 surface = (struct haiku_egl_surface*) calloc(1, sizeof (*surface));
123 if (!surface) {
124 _eglError(EGL_BAD_ALLOC, "haiku_create_window_surface");
125 return NULL;
126 }
127
128 if (!_eglInitSurface(&surface->surf, disp, EGL_WINDOW_BIT, conf, attrib_list))
129 goto cleanup_surface;
130
131 (&surface->surf)->SwapInterval = 1;
132
133 TRACE("Creating window\n");
134 BWindow* win = (BWindow*)native_window;
135
136 TRACE("Creating GL view\n");
137 surface->gl = new BGLView(win->Bounds(), "OpenGL", B_FOLLOW_ALL_SIDES, 0,
138 BGL_RGB | BGL_DOUBLE | BGL_ALPHA);
139
140 TRACE("Adding GL\n");
141 win->AddChild(surface->gl);
142
143 TRACE("Showing window\n");
144 win->Show();
145 return &surface->surf;
146
147 cleanup_surface:
148 free(surface);
149 return NULL;
150 }
151
152
153 static _EGLSurface *
154 haiku_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
155 _EGLConfig *conf, void *native_pixmap, const EGLint *attrib_list)
156 {
157 return NULL;
158 }
159
160
161 static _EGLSurface *
162 haiku_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
163 _EGLConfig *conf, const EGLint *attrib_list)
164 {
165 return NULL;
166 }
167
168
169 static EGLBoolean
170 haiku_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
171 {
172 return EGL_TRUE;
173 }
174
175
176 static EGLBoolean
177 haiku_add_configs_for_visuals(_EGLDisplay *dpy)
178 {
179 CALLED();
180
181 struct haiku_egl_config* conf;
182 conf = (struct haiku_egl_config*) calloc(1, sizeof (*conf));
183 if (!conf) {
184 _eglError(EGL_BAD_ALLOC, "haiku_add_configs_for_visuals");
185 return NULL;
186 }
187
188 _eglInitConfig(&conf->base, dpy, 1);
189 TRACE("Config inited\n");
190
191 _eglSetConfigKey(&conf->base, EGL_RED_SIZE, 8);
192 _eglSetConfigKey(&conf->base, EGL_BLUE_SIZE, 8);
193 _eglSetConfigKey(&conf->base, EGL_GREEN_SIZE, 8);
194 _eglSetConfigKey(&conf->base, EGL_LUMINANCE_SIZE, 0);
195 _eglSetConfigKey(&conf->base, EGL_ALPHA_SIZE, 8);
196 _eglSetConfigKey(&conf->base, EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER);
197 EGLint r = (_eglGetConfigKey(&conf->base, EGL_RED_SIZE)
198 + _eglGetConfigKey(&conf->base, EGL_GREEN_SIZE)
199 + _eglGetConfigKey(&conf->base, EGL_BLUE_SIZE)
200 + _eglGetConfigKey(&conf->base, EGL_ALPHA_SIZE));
201 _eglSetConfigKey(&conf->base, EGL_BUFFER_SIZE, r);
202 _eglSetConfigKey(&conf->base, EGL_CONFIG_CAVEAT, EGL_NONE);
203 _eglSetConfigKey(&conf->base, EGL_CONFIG_ID, 1);
204 _eglSetConfigKey(&conf->base, EGL_BIND_TO_TEXTURE_RGB, EGL_FALSE);
205 _eglSetConfigKey(&conf->base, EGL_BIND_TO_TEXTURE_RGBA, EGL_FALSE);
206 _eglSetConfigKey(&conf->base, EGL_STENCIL_SIZE, 0);
207 _eglSetConfigKey(&conf->base, EGL_TRANSPARENT_TYPE, EGL_NONE);
208 _eglSetConfigKey(&conf->base, EGL_NATIVE_RENDERABLE, EGL_TRUE); // Let's say yes
209 _eglSetConfigKey(&conf->base, EGL_NATIVE_VISUAL_ID, 0); // No visual
210 _eglSetConfigKey(&conf->base, EGL_NATIVE_VISUAL_TYPE, EGL_NONE); // No visual
211 _eglSetConfigKey(&conf->base, EGL_RENDERABLE_TYPE, 0x8);
212 _eglSetConfigKey(&conf->base, EGL_SAMPLE_BUFFERS, 0); // TODO: How to get the right value ?
213 _eglSetConfigKey(&conf->base, EGL_SAMPLES, _eglGetConfigKey(&conf->base, EGL_SAMPLE_BUFFERS) == 0 ? 0 : 0);
214 _eglSetConfigKey(&conf->base, EGL_DEPTH_SIZE, 24); // TODO: How to get the right value ?
215 _eglSetConfigKey(&conf->base, EGL_LEVEL, 0);
216 _eglSetConfigKey(&conf->base, EGL_MAX_PBUFFER_WIDTH, 0); // TODO: How to get the right value ?
217 _eglSetConfigKey(&conf->base, EGL_MAX_PBUFFER_HEIGHT, 0); // TODO: How to get the right value ?
218 _eglSetConfigKey(&conf->base, EGL_MAX_PBUFFER_PIXELS, 0); // TODO: How to get the right value ?
219 _eglSetConfigKey(&conf->base, EGL_SURFACE_TYPE, EGL_WINDOW_BIT /*| EGL_PIXMAP_BIT | EGL_PBUFFER_BIT*/);
220
221 TRACE("Config configuated\n");
222 if (!_eglValidateConfig(&conf->base, EGL_FALSE)) {
223 _eglLog(_EGL_DEBUG, "Haiku: failed to validate config");
224 return EGL_FALSE;
225 }
226 TRACE("Validated config\n");
227
228 _eglLinkConfig(&conf->base);
229 if (!_eglGetArraySize(dpy->Configs)) {
230 _eglLog(_EGL_WARNING, "Haiku: failed to create any config");
231 return EGL_FALSE;
232 }
233 TRACE("Config successfull\n");
234
235 return EGL_TRUE;
236 }
237
238 extern "C"
239 EGLBoolean
240 init_haiku(_EGLDriver *drv, _EGLDisplay *dpy)
241 {
242 CALLED();
243
244 _eglSetLogProc(haiku_log);
245
246 TRACE("Add configs\n");
247 haiku_add_configs_for_visuals(dpy);
248
249 dpy->Version = 14;
250
251 TRACE("Initialization finished\n");
252
253 return EGL_TRUE;
254 }
255
256
257 extern "C"
258 EGLBoolean
259 haiku_terminate(_EGLDriver* drv,_EGLDisplay* dpy)
260 {
261 return EGL_TRUE;
262 }
263
264
265 extern "C"
266 _EGLContext*
267 haiku_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
268 _EGLContext *share_list, const EGLint *attrib_list)
269 {
270 CALLED();
271
272 struct haiku_egl_context* context;
273 context = (struct haiku_egl_context*) calloc(1, sizeof (*context));
274 if (!context) {
275 _eglError(EGL_BAD_ALLOC, "haiku_create_context");
276 return NULL;
277 }
278
279 if (!_eglInitContext(&context->ctx, disp, conf, attrib_list))
280 ERROR("ERROR creating context");
281
282 TRACE("Context created\n");
283 return &context->ctx;
284 }
285
286
287 extern "C"
288 EGLBoolean
289 haiku_destroy_context(_EGLDriver* drv, _EGLDisplay *disp, _EGLContext* ctx)
290 {
291 ctx=NULL;
292 return EGL_TRUE;
293 }
294
295
296 extern "C"
297 EGLBoolean
298 haiku_make_current(_EGLDriver* drv, _EGLDisplay* dpy, _EGLSurface *dsurf,
299 _EGLSurface *rsurf, _EGLContext *ctx)
300 {
301 CALLED();
302
303 struct haiku_egl_context* cont=haiku_egl_context(ctx);
304 struct haiku_egl_surface* surf=haiku_egl_surface(dsurf);
305 _EGLContext *old_ctx;
306 _EGLSurface *old_dsurf, *old_rsurf;
307 _eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf);
308 //cont->ctx.DrawSurface=&surf->surf;
309 surf->gl->LockGL();
310 return EGL_TRUE;
311 }
312
313
314 extern "C"
315 EGLBoolean
316 haiku_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
317 {
318 struct haiku_egl_surface* surface=haiku_egl_surface(surf);
319 surface->gl->SwapBuffers();
320 //gl->Render();
321 return EGL_TRUE;
322 }
323
324
325 extern "C"
326 void
327 haiku_unload(_EGLDriver* drv)
328 {
329
330 }
331
332
333 /**
334 * This is the main entrypoint into the driver, called by libEGL.
335 * Create a new _EGLDriver object and init its dispatch table.
336 */
337 extern "C"
338 _EGLDriver*
339 _eglBuiltInDriverHaiku(const char *args)
340 {
341 CALLED();
342
343 struct haiku_egl_driver* driver;
344 driver = (struct haiku_egl_driver*) calloc(1, sizeof(*driver));
345 if (!driver) {
346 _eglError(EGL_BAD_ALLOC, "_eglBuiltInDriverHaiku");
347 return NULL;
348 }
349
350 _eglInitDriverFallbacks(&driver->base);
351 driver->base.API.Initialize = init_haiku;
352 driver->base.API.Terminate = haiku_terminate;
353 driver->base.API.CreateContext = haiku_create_context;
354 driver->base.API.DestroyContext = haiku_destroy_context;
355 driver->base.API.MakeCurrent = haiku_make_current;
356 driver->base.API.CreateWindowSurface = haiku_create_window_surface;
357 driver->base.API.CreatePixmapSurface = haiku_create_pixmap_surface;
358 driver->base.API.CreatePbufferSurface = haiku_create_pbuffer_surface;
359 driver->base.API.DestroySurface = haiku_destroy_surface;
360
361 driver->base.API.SwapBuffers = haiku_swap_buffers;
362
363 driver->base.Name = "Haiku";
364 driver->base.Unload = haiku_unload;
365
366 TRACE("API Calls defined\n");
367
368 return &driver->base;
369 }