2 * Mesa 3-D graphics library
4 * Copyright (C) 2014 Adrián Arroyo Calle <adrian.arroyocalle@gmail.com>
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
30 #include "eglconfig.h"
31 #include "eglcontext.h"
32 #include "egldisplay.h"
33 #include "egldriver.h"
34 #include "eglcurrent.h"
36 #include "eglsurface.h"
38 #include "egltypedefs.h"
40 #include <InterfaceKit.h>
41 #include <OpenGLKit.h>
45 # define TRACE(x...) printf("egl_haiku: " x)
46 # define CALLED() TRACE("CALLED: %s\n", __PRETTY_FUNCTION__)
51 #define ERROR(x...) printf("egl_haiku: " x)
54 _EGL_DRIVER_STANDARD_TYPECASTS(haiku_egl
)
57 struct haiku_egl_driver
62 struct haiku_egl_config
67 struct haiku_egl_context
72 struct haiku_egl_surface
80 haiku_log(EGLint level
, const char *msg
)
84 fprintf(stderr
,"%s", msg
);
87 fprintf(stderr
,"%s", msg
);
90 fprintf(stderr
,"%s", msg
);
93 fprintf(stderr
,"%s", msg
);
102 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
105 haiku_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
106 _EGLConfig
*conf
, void *native_surface
, const EGLint
*attrib_list
)
113 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
116 haiku_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
117 _EGLConfig
*conf
, void *native_window
, const EGLint
*attrib_list
)
121 struct haiku_egl_surface
* surface
;
122 surface
= (struct haiku_egl_surface
*) calloc(1, sizeof (*surface
));
124 _eglError(EGL_BAD_ALLOC
, "haiku_create_window_surface");
128 if (!_eglInitSurface(&surface
->surf
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
))
129 goto cleanup_surface
;
131 (&surface
->surf
)->SwapInterval
= 1;
133 TRACE("Creating window\n");
134 BWindow
* win
= (BWindow
*)native_window
;
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
);
140 TRACE("Adding GL\n");
141 win
->AddChild(surface
->gl
);
143 TRACE("Showing window\n");
145 return &surface
->surf
;
154 haiku_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
155 _EGLConfig
*conf
, void *native_pixmap
, const EGLint
*attrib_list
)
162 haiku_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
163 _EGLConfig
*conf
, const EGLint
*attrib_list
)
170 haiku_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
177 haiku_add_configs_for_visuals(_EGLDisplay
*dpy
)
181 struct haiku_egl_config
* conf
;
182 conf
= (struct haiku_egl_config
*) calloc(1, sizeof (*conf
));
184 _eglError(EGL_BAD_ALLOC
, "haiku_add_configs_for_visuals");
188 _eglInitConfig(&conf
->base
, dpy
, 1);
189 TRACE("Config inited\n");
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*/);
221 TRACE("Config configuated\n");
222 if (!_eglValidateConfig(&conf
->base
, EGL_FALSE
)) {
223 _eglLog(_EGL_DEBUG
, "Haiku: failed to validate config");
226 TRACE("Validated config\n");
228 _eglLinkConfig(&conf
->base
);
229 if (!_eglGetArraySize(dpy
->Configs
)) {
230 _eglLog(_EGL_WARNING
, "Haiku: failed to create any config");
233 TRACE("Config successfull\n");
240 init_haiku(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
244 _eglSetLogProc(haiku_log
);
246 TRACE("Add configs\n");
247 haiku_add_configs_for_visuals(dpy
);
251 TRACE("Initialization finished\n");
259 haiku_terminate(_EGLDriver
* drv
,_EGLDisplay
* dpy
)
267 haiku_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
268 _EGLContext
*share_list
, const EGLint
*attrib_list
)
272 struct haiku_egl_context
* context
;
273 context
= (struct haiku_egl_context
*) calloc(1, sizeof (*context
));
275 _eglError(EGL_BAD_ALLOC
, "haiku_create_context");
279 if (!_eglInitContext(&context
->ctx
, disp
, conf
, attrib_list
))
280 ERROR("ERROR creating context");
282 TRACE("Context created\n");
283 return &context
->ctx
;
289 haiku_destroy_context(_EGLDriver
* drv
, _EGLDisplay
*disp
, _EGLContext
* ctx
)
298 haiku_make_current(_EGLDriver
* drv
, _EGLDisplay
* dpy
, _EGLSurface
*dsurf
,
299 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
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;
316 haiku_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
318 struct haiku_egl_surface
* surface
=haiku_egl_surface(surf
);
319 surface
->gl
->SwapBuffers();
327 haiku_unload(_EGLDriver
* drv
)
334 * This is the main entrypoint into the driver, called by libEGL.
335 * Create a new _EGLDriver object and init its dispatch table.
339 _eglBuiltInDriverHaiku(const char *args
)
343 struct haiku_egl_driver
* driver
;
344 driver
= (struct haiku_egl_driver
*) calloc(1, sizeof(*driver
));
346 _eglError(EGL_BAD_ALLOC
, "_eglBuiltInDriverHaiku");
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
;
361 driver
->base
.API
.SwapBuffers
= haiku_swap_buffers
;
363 driver
->base
.Name
= "Haiku";
364 driver
->base
.Unload
= haiku_unload
;
366 TRACE("API Calls defined\n");
368 return &driver
->base
;