egl: Clean up Haiku visual creation
[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 <stdio.h>
28
29 extern "C" {
30 #include "loader.h"
31 #include "eglconfig.h"
32 #include "eglcontext.h"
33 #include "egldisplay.h"
34 #include "egldriver.h"
35 #include "eglcurrent.h"
36 #include "egllog.h"
37 #include "eglsurface.h"
38 #include "eglimage.h"
39 #include "egltypedefs.h"
40 }
41
42 #include <InterfaceKit.h>
43 #include <OpenGLKit.h>
44
45
46 #define CALLOC_STRUCT(T) (struct T *) calloc(1, sizeof(struct T))
47
48
49 _EGL_DRIVER_STANDARD_TYPECASTS(haiku_egl)
50
51
52 struct haiku_egl_driver
53 {
54 _EGLDriver base;
55
56 void *handle;
57 _EGLProc (*get_proc_address)(const char *procname);
58 void (*glFlush)(void);
59 };
60
61 struct haiku_egl_config
62 {
63 _EGLConfig base;
64 };
65
66 struct haiku_egl_context
67 {
68 _EGLContext ctx;
69 };
70
71 struct haiku_egl_surface
72 {
73 _EGLSurface surf;
74 BGLView* gl;
75 };
76
77
78 /*
79 static void
80 swrastCreateDrawable(struct dri2_egl_display * dri2_dpy,
81 struct dri2_egl_surface * dri2_surf, int depth)
82 {
83
84 }
85
86
87 static void
88 swrastDestroyDrawable(struct dri2_egl_display * dri2_dpy,
89 struct dri2_egl_surface * dri2_surf)
90 {
91
92 }
93
94
95 static void
96 swrastGetDrawableInfo(__DRIdrawable * draw, int *x, int *y,
97 int *w, int *h, void *loaderPrivate)
98 {
99
100 }
101
102
103 static void
104 swrastPutImage(__DRIdrawable * draw, int op, int x, int y,
105 int w, int h, char *data, void *loaderPrivate)
106 {
107
108 }
109
110
111 static void
112 swrastGetImage(__DRIdrawable * read, int x, int y,
113 int w, int h, char *data, void *loaderPrivate)
114 {
115
116 }
117 */
118
119
120 static void
121 haiku_log(EGLint level, const char *msg)
122 {
123 switch (level) {
124 case _EGL_DEBUG:
125 fprintf(stderr,"%s", msg);
126 break;
127 case _EGL_INFO:
128 fprintf(stderr,"%s", msg);
129 break;
130 case _EGL_WARNING:
131 fprintf(stderr,"%s", msg);
132 break;
133 case _EGL_FATAL:
134 fprintf(stderr,"%s", msg);
135 break;
136 default:
137 break;
138 }
139 }
140
141
142 /**
143 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
144 */
145 static _EGLSurface *
146 haiku_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
147 _EGLConfig *conf, void *native_surface, const EGLint *attrib_list)
148 {
149 return NULL;
150 }
151
152
153 /**
154 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
155 */
156 static _EGLSurface *
157 haiku_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
158 _EGLConfig *conf, void *native_window, const EGLint *attrib_list)
159 {
160 struct haiku_egl_surface* surface;
161 surface = (struct haiku_egl_surface*)calloc(1,sizeof (*surface));
162
163 _eglInitSurface(&surface->surf, disp, EGL_WINDOW_BIT, conf, attrib_list);
164 (&surface->surf)->SwapInterval = 1;
165
166 _eglLog(_EGL_DEBUG, "Creating window");
167 BWindow* win = (BWindow*)native_window;
168
169 _eglLog(_EGL_DEBUG, "Creating GL view");
170 surface->gl = new BGLView(win->Bounds(), "OpenGL", B_FOLLOW_ALL_SIDES, 0,
171 BGL_RGB | BGL_DOUBLE | BGL_ALPHA);
172
173 _eglLog(_EGL_DEBUG, "Adding GL");
174 win->AddChild(surface->gl);
175
176 _eglLog(_EGL_DEBUG, "Showing window");
177 win->Show();
178 return &surface->surf;
179 }
180
181
182 static _EGLSurface *
183 haiku_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
184 _EGLConfig *conf, void *native_pixmap, const EGLint *attrib_list)
185 {
186 return NULL;
187 }
188
189
190 static _EGLSurface *
191 haiku_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
192 _EGLConfig *conf, const EGLint *attrib_list)
193 {
194 return NULL;
195 }
196
197
198 static EGLBoolean
199 haiku_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
200 {
201 return EGL_TRUE;
202 }
203
204
205 static EGLBoolean
206 haiku_add_configs_for_visuals(_EGLDisplay *dpy)
207 {
208 printf("Adding configs\n");
209
210 struct haiku_egl_config* conf;
211 conf = CALLOC_STRUCT(haiku_egl_config);
212
213 _eglInitConfig(&conf->base, dpy, 1);
214 _eglLog(_EGL_DEBUG,"Config inited\n");
215 _eglSetConfigKey(&conf->base, EGL_RED_SIZE, 8);
216 _eglSetConfigKey(&conf->base, EGL_BLUE_SIZE, 8);
217 _eglSetConfigKey(&conf->base, EGL_GREEN_SIZE, 8);
218 _eglSetConfigKey(&conf->base, EGL_LUMINANCE_SIZE, 0);
219 _eglSetConfigKey(&conf->base, EGL_ALPHA_SIZE, 8);
220 _eglSetConfigKey(&conf->base, EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER);
221 EGLint r = (_eglGetConfigKey(&conf->base, EGL_RED_SIZE)
222 + _eglGetConfigKey(&conf->base, EGL_GREEN_SIZE)
223 + _eglGetConfigKey(&conf->base, EGL_BLUE_SIZE)
224 + _eglGetConfigKey(&conf->base, EGL_ALPHA_SIZE));
225 _eglSetConfigKey(&conf->base, EGL_BUFFER_SIZE, r);
226 _eglSetConfigKey(&conf->base, EGL_CONFIG_CAVEAT, EGL_NONE);
227 _eglSetConfigKey(&conf->base, EGL_CONFIG_ID, 1);
228 _eglSetConfigKey(&conf->base, EGL_BIND_TO_TEXTURE_RGB, EGL_FALSE);
229 _eglSetConfigKey(&conf->base, EGL_BIND_TO_TEXTURE_RGBA, EGL_FALSE);
230 _eglSetConfigKey(&conf->base, EGL_STENCIL_SIZE, 0);
231 _eglSetConfigKey(&conf->base, EGL_TRANSPARENT_TYPE, EGL_NONE);
232 _eglSetConfigKey(&conf->base, EGL_NATIVE_RENDERABLE, EGL_TRUE); // Let's say yes
233 _eglSetConfigKey(&conf->base, EGL_NATIVE_VISUAL_ID, 0); // No visual
234 _eglSetConfigKey(&conf->base, EGL_NATIVE_VISUAL_TYPE, EGL_NONE); // No visual
235 _eglSetConfigKey(&conf->base, EGL_RENDERABLE_TYPE, 0x8);
236 _eglSetConfigKey(&conf->base, EGL_SAMPLE_BUFFERS, 0); // TODO: How to get the right value ?
237 _eglSetConfigKey(&conf->base, EGL_SAMPLES, _eglGetConfigKey(&conf->base, EGL_SAMPLE_BUFFERS) == 0 ? 0 : 0);
238 _eglSetConfigKey(&conf->base, EGL_DEPTH_SIZE, 24); // TODO: How to get the right value ?
239 _eglSetConfigKey(&conf->base, EGL_LEVEL, 0);
240 _eglSetConfigKey(&conf->base, EGL_MAX_PBUFFER_WIDTH, 0); // TODO: How to get the right value ?
241 _eglSetConfigKey(&conf->base, EGL_MAX_PBUFFER_HEIGHT, 0); // TODO: How to get the right value ?
242 _eglSetConfigKey(&conf->base, EGL_MAX_PBUFFER_PIXELS, 0); // TODO: How to get the right value ?
243 _eglSetConfigKey(&conf->base, EGL_SURFACE_TYPE, EGL_WINDOW_BIT /*| EGL_PIXMAP_BIT | EGL_PBUFFER_BIT*/);
244
245 printf("Config configuated\n");
246 if (!_eglValidateConfig(&conf->base, EGL_FALSE)) {
247 _eglLog(_EGL_DEBUG, "Haiku failed to validate config");
248 return EGL_FALSE;
249 }
250 printf("Validated config\n");
251
252 _eglLinkConfig(&conf->base);
253 if (!_eglGetArraySize(dpy->Configs)) {
254 _eglLog(_EGL_WARNING, "Haiku: failed to create any config");
255 return EGL_FALSE;
256 }
257 printf("Config successful!\n");
258
259 return EGL_TRUE;
260 }
261
262 extern "C"
263 EGLBoolean
264 init_haiku(_EGLDriver *drv, _EGLDisplay *dpy)
265 {
266 _eglLog(_EGL_DEBUG,"\nInitializing Haiku EGL\n");
267 //_EGLDisplay* egl_dpy;
268
269 printf("Initializing Haiku EGL\n");
270 _eglSetLogProc(haiku_log);
271
272 loader_set_logger(_eglLog);
273
274 /*egl_dpy = (_EGLDisplay*) calloc(1, sizeof(_EGLDisplay));
275 if (!egl_dpy)
276 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
277
278 dpy->DriverData=(void*) egl_dpy;
279 if (!dpy->PlatformDisplay) {
280 // OPEN DEVICE
281 //dri2_dpy->bwindow = (void*)haiku_create_window();
282 //dri2_dpy->own_device = true;
283 } else {
284 //dri2_dpy->bwindow = (BWindow*)dpy->PlatformDisplay;
285 }*/
286
287 //dri2_dpy->driver_name = strdup("swrast");
288 //if (!dri2_load_driver_swrast(dpy))
289 // goto cleanup_conn;
290
291 /*dri2_dpy->swrast_loader_extension.base.name = __DRI_SWRAST_LOADER;
292 dri2_dpy->swrast_loader_extension.base.version = __DRI_SWRAST_LOADER_VERSION;
293 dri2_dpy->swrast_loader_extension.getDrawableInfo = swrastGetDrawableInfo;
294 dri2_dpy->swrast_loader_extension.putImage = swrastPutImage;
295 dri2_dpy->swrast_loader_extension.getImage = swrastGetImage;
296
297 dri2_dpy->extensions[0] = &dri2_dpy->swrast_loader_extension.base;
298 dri2_dpy->extensions[1] = NULL;
299 dri2_dpy->extensions[2] = NULL;*/
300
301 /*if (dri2_dpy->bwindow) {
302 if (!dri2_haiku_add_configs_for_visuals(dri2_dpy, dpy))
303 goto cleanup_configs;
304 }*/
305 _eglLog(_EGL_DEBUG,"Add configs");
306 haiku_add_configs_for_visuals(dpy);
307
308 dpy->VersionMajor=1;
309 dpy->VersionMinor=4;
310
311 //dpy->Extensions.KHR_create_context = true;
312
313 //dri2_dpy->vtbl = &dri2_haiku_display_vtbl;
314 _eglLog(_EGL_DEBUG, "Initialization finished");
315
316 return EGL_TRUE;
317 }
318
319
320 extern "C"
321 EGLBoolean
322 haiku_terminate(_EGLDriver* drv,_EGLDisplay* dpy)
323 {
324 return EGL_TRUE;
325 }
326
327
328 extern "C"
329 _EGLContext*
330 haiku_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
331 _EGLContext *share_list, const EGLint *attrib_list)
332 {
333 _eglLog(_EGL_DEBUG,"Creating context");
334 struct haiku_egl_context* context;
335 context=(struct haiku_egl_context*)calloc(1,sizeof (*context));
336 if(!_eglInitContext(&context->ctx, disp, conf, attrib_list))
337 printf("ERROR creating context");
338 _eglLog(_EGL_DEBUG, "Context created");
339 return &context->ctx;
340 }
341
342
343 extern "C"
344 EGLBoolean
345 haiku_destroy_context(_EGLDriver* drv, _EGLDisplay *disp, _EGLContext* ctx)
346 {
347 ctx=NULL;
348 return EGL_TRUE;
349 }
350
351
352 extern "C"
353 EGLBoolean
354 haiku_make_current(_EGLDriver* drv, _EGLDisplay* dpy, _EGLSurface *dsurf,
355 _EGLSurface *rsurf, _EGLContext *ctx)
356 {
357 struct haiku_egl_context* cont=haiku_egl_context(ctx);
358 struct haiku_egl_surface* surf=haiku_egl_surface(dsurf);
359 _EGLContext *old_ctx;
360 _EGLSurface *old_dsurf, *old_rsurf;
361 _eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf);
362 //cont->ctx.DrawSurface=&surf->surf;
363 surf->gl->LockGL();
364 return EGL_TRUE;
365 }
366
367
368 extern "C"
369 EGLBoolean
370 haiku_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
371 {
372 struct haiku_egl_surface* surface=haiku_egl_surface(surf);
373 surface->gl->SwapBuffers();
374 //gl->Render();
375 return EGL_TRUE;
376 }
377
378
379 extern "C"
380 void
381 haiku_unload(_EGLDriver* drv)
382 {
383
384 }
385
386
387 /**
388 * This is the main entrypoint into the driver, called by libEGL.
389 * Create a new _EGLDriver object and init its dispatch table.
390 */
391 extern "C"
392 _EGLDriver*
393 _eglBuiltInDriverHaiku(const char *args)
394 {
395 _eglLog(_EGL_DEBUG,"Driver loaded");
396 struct haiku_egl_driver* driver;
397 driver=(struct haiku_egl_driver*)calloc(1,sizeof(*driver));
398 _eglInitDriverFallbacks(&driver->base);
399 driver->base.API.Initialize = init_haiku;
400 driver->base.API.Terminate = haiku_terminate;
401 driver->base.API.CreateContext = haiku_create_context;
402 driver->base.API.DestroyContext = haiku_destroy_context;
403 driver->base.API.MakeCurrent = haiku_make_current;
404 driver->base.API.CreateWindowSurface = haiku_create_window_surface;
405 driver->base.API.CreatePixmapSurface = haiku_create_pixmap_surface;
406 driver->base.API.CreatePbufferSurface = haiku_create_pbuffer_surface;
407 driver->base.API.DestroySurface = haiku_destroy_surface;
408 /*
409 driver->API.GetProcAddress = dri2_get_proc_address;
410 driver->API.WaitClient = dri2_wait_client;
411 driver->API.WaitNative = dri2_wait_native;
412 driver->API.BindTexImage = dri2_bind_tex_image;
413 driver->API.ReleaseTexImage = dri2_release_tex_image;
414 driver->API.SwapInterval = dri2_swap_interval;
415 */
416
417 driver->base.API.SwapBuffers = haiku_swap_buffers;
418 /*
419 driver->API.SwapBuffersWithDamageEXT = dri2_swap_buffers_with_damage;
420 driver->API.SwapBuffersRegionNOK = dri2_swap_buffers_region;
421 driver->API.PostSubBufferNV = dri2_post_sub_buffer;
422 driver->API.CopyBuffers = dri2_copy_buffers,
423 driver->API.QueryBufferAge = dri2_query_buffer_age;
424 driver->API.CreateImageKHR = dri2_create_image;
425 driver->API.DestroyImageKHR = dri2_destroy_image_khr;
426 driver->API.CreateWaylandBufferFromImageWL = dri2_create_wayland_buffer_from_image;
427 driver->API.GetSyncValuesCHROMIUM = dri2_get_sync_values_chromium;
428 */
429
430 driver->base.Name = "Haiku";
431 driver->base.Unload = haiku_unload;
432
433 _eglLog(_EGL_DEBUG, "API Calls defined");
434
435 return &driver->base;
436 }