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.
31 #include "eglconfig.h"
32 #include "eglcontext.h"
33 #include "egldisplay.h"
34 #include "egldriver.h"
35 #include "eglcurrent.h"
37 #include "eglsurface.h"
39 #include "egltypedefs.h"
41 #include <InterfaceKit.h>
42 #include <OpenGLKit.h>
45 #define CALLOC_STRUCT(T) (struct T *) calloc(1, sizeof(struct T))
48 _EGL_DRIVER_STANDARD_TYPECASTS(haiku_egl
)
51 struct haiku_egl_driver
56 _EGLProc (*get_proc_address
)(const char *procname
);
57 void (*glFlush
)(void);
60 struct haiku_egl_config
65 struct haiku_egl_context
70 struct haiku_egl_surface
78 haiku_log(EGLint level
, const char *msg
)
82 fprintf(stderr
,"%s", msg
);
85 fprintf(stderr
,"%s", msg
);
88 fprintf(stderr
,"%s", msg
);
91 fprintf(stderr
,"%s", msg
);
100 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
103 haiku_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
104 _EGLConfig
*conf
, void *native_surface
, const EGLint
*attrib_list
)
111 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
114 haiku_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
115 _EGLConfig
*conf
, void *native_window
, const EGLint
*attrib_list
)
117 struct haiku_egl_surface
* surface
;
118 surface
= (struct haiku_egl_surface
*)calloc(1,sizeof (*surface
));
120 _eglInitSurface(&surface
->surf
, disp
, EGL_WINDOW_BIT
, conf
, attrib_list
);
121 (&surface
->surf
)->SwapInterval
= 1;
123 _eglLog(_EGL_DEBUG
, "Creating window");
124 BWindow
* win
= (BWindow
*)native_window
;
126 _eglLog(_EGL_DEBUG
, "Creating GL view");
127 surface
->gl
= new BGLView(win
->Bounds(), "OpenGL", B_FOLLOW_ALL_SIDES
, 0,
128 BGL_RGB
| BGL_DOUBLE
| BGL_ALPHA
);
130 _eglLog(_EGL_DEBUG
, "Adding GL");
131 win
->AddChild(surface
->gl
);
133 _eglLog(_EGL_DEBUG
, "Showing window");
135 return &surface
->surf
;
140 haiku_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
141 _EGLConfig
*conf
, void *native_pixmap
, const EGLint
*attrib_list
)
148 haiku_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
149 _EGLConfig
*conf
, const EGLint
*attrib_list
)
156 haiku_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
163 haiku_add_configs_for_visuals(_EGLDisplay
*dpy
)
165 printf("Adding configs\n");
167 struct haiku_egl_config
* conf
;
168 conf
= CALLOC_STRUCT(haiku_egl_config
);
170 _eglInitConfig(&conf
->base
, dpy
, 1);
171 _eglLog(_EGL_DEBUG
,"Config inited\n");
172 _eglSetConfigKey(&conf
->base
, EGL_RED_SIZE
, 8);
173 _eglSetConfigKey(&conf
->base
, EGL_BLUE_SIZE
, 8);
174 _eglSetConfigKey(&conf
->base
, EGL_GREEN_SIZE
, 8);
175 _eglSetConfigKey(&conf
->base
, EGL_LUMINANCE_SIZE
, 0);
176 _eglSetConfigKey(&conf
->base
, EGL_ALPHA_SIZE
, 8);
177 _eglSetConfigKey(&conf
->base
, EGL_COLOR_BUFFER_TYPE
, EGL_RGB_BUFFER
);
178 EGLint r
= (_eglGetConfigKey(&conf
->base
, EGL_RED_SIZE
)
179 + _eglGetConfigKey(&conf
->base
, EGL_GREEN_SIZE
)
180 + _eglGetConfigKey(&conf
->base
, EGL_BLUE_SIZE
)
181 + _eglGetConfigKey(&conf
->base
, EGL_ALPHA_SIZE
));
182 _eglSetConfigKey(&conf
->base
, EGL_BUFFER_SIZE
, r
);
183 _eglSetConfigKey(&conf
->base
, EGL_CONFIG_CAVEAT
, EGL_NONE
);
184 _eglSetConfigKey(&conf
->base
, EGL_CONFIG_ID
, 1);
185 _eglSetConfigKey(&conf
->base
, EGL_BIND_TO_TEXTURE_RGB
, EGL_FALSE
);
186 _eglSetConfigKey(&conf
->base
, EGL_BIND_TO_TEXTURE_RGBA
, EGL_FALSE
);
187 _eglSetConfigKey(&conf
->base
, EGL_STENCIL_SIZE
, 0);
188 _eglSetConfigKey(&conf
->base
, EGL_TRANSPARENT_TYPE
, EGL_NONE
);
189 _eglSetConfigKey(&conf
->base
, EGL_NATIVE_RENDERABLE
, EGL_TRUE
); // Let's say yes
190 _eglSetConfigKey(&conf
->base
, EGL_NATIVE_VISUAL_ID
, 0); // No visual
191 _eglSetConfigKey(&conf
->base
, EGL_NATIVE_VISUAL_TYPE
, EGL_NONE
); // No visual
192 _eglSetConfigKey(&conf
->base
, EGL_RENDERABLE_TYPE
, 0x8);
193 _eglSetConfigKey(&conf
->base
, EGL_SAMPLE_BUFFERS
, 0); // TODO: How to get the right value ?
194 _eglSetConfigKey(&conf
->base
, EGL_SAMPLES
, _eglGetConfigKey(&conf
->base
, EGL_SAMPLE_BUFFERS
) == 0 ? 0 : 0);
195 _eglSetConfigKey(&conf
->base
, EGL_DEPTH_SIZE
, 24); // TODO: How to get the right value ?
196 _eglSetConfigKey(&conf
->base
, EGL_LEVEL
, 0);
197 _eglSetConfigKey(&conf
->base
, EGL_MAX_PBUFFER_WIDTH
, 0); // TODO: How to get the right value ?
198 _eglSetConfigKey(&conf
->base
, EGL_MAX_PBUFFER_HEIGHT
, 0); // TODO: How to get the right value ?
199 _eglSetConfigKey(&conf
->base
, EGL_MAX_PBUFFER_PIXELS
, 0); // TODO: How to get the right value ?
200 _eglSetConfigKey(&conf
->base
, EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
/*| EGL_PIXMAP_BIT | EGL_PBUFFER_BIT*/);
202 printf("Config configuated\n");
203 if (!_eglValidateConfig(&conf
->base
, EGL_FALSE
)) {
204 _eglLog(_EGL_DEBUG
, "Haiku failed to validate config");
207 printf("Validated config\n");
209 _eglLinkConfig(&conf
->base
);
210 if (!_eglGetArraySize(dpy
->Configs
)) {
211 _eglLog(_EGL_WARNING
, "Haiku: failed to create any config");
214 printf("Config successful!\n");
221 init_haiku(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
223 _eglLog(_EGL_DEBUG
,"\nInitializing Haiku EGL\n");
225 printf("Initializing Haiku EGL\n");
226 _eglSetLogProc(haiku_log
);
228 loader_set_logger(_eglLog
);
230 _eglLog(_EGL_DEBUG
,"Add configs");
231 haiku_add_configs_for_visuals(dpy
);
235 //dpy->Extensions.KHR_create_context = true;
237 //dri2_dpy->vtbl = &dri2_haiku_display_vtbl;
238 _eglLog(_EGL_DEBUG
, "Initialization finished");
246 haiku_terminate(_EGLDriver
* drv
,_EGLDisplay
* dpy
)
254 haiku_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
255 _EGLContext
*share_list
, const EGLint
*attrib_list
)
257 _eglLog(_EGL_DEBUG
,"Creating context");
258 struct haiku_egl_context
* context
;
259 context
=(struct haiku_egl_context
*)calloc(1,sizeof (*context
));
260 if(!_eglInitContext(&context
->ctx
, disp
, conf
, attrib_list
))
261 printf("ERROR creating context");
262 _eglLog(_EGL_DEBUG
, "Context created");
263 return &context
->ctx
;
269 haiku_destroy_context(_EGLDriver
* drv
, _EGLDisplay
*disp
, _EGLContext
* ctx
)
278 haiku_make_current(_EGLDriver
* drv
, _EGLDisplay
* dpy
, _EGLSurface
*dsurf
,
279 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
281 struct haiku_egl_context
* cont
=haiku_egl_context(ctx
);
282 struct haiku_egl_surface
* surf
=haiku_egl_surface(dsurf
);
283 _EGLContext
*old_ctx
;
284 _EGLSurface
*old_dsurf
, *old_rsurf
;
285 _eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
);
286 //cont->ctx.DrawSurface=&surf->surf;
294 haiku_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
296 struct haiku_egl_surface
* surface
=haiku_egl_surface(surf
);
297 surface
->gl
->SwapBuffers();
305 haiku_unload(_EGLDriver
* drv
)
312 * This is the main entrypoint into the driver, called by libEGL.
313 * Create a new _EGLDriver object and init its dispatch table.
317 _eglBuiltInDriverHaiku(const char *args
)
319 _eglLog(_EGL_DEBUG
,"Driver loaded");
320 struct haiku_egl_driver
* driver
;
321 driver
=(struct haiku_egl_driver
*)calloc(1,sizeof(*driver
));
322 _eglInitDriverFallbacks(&driver
->base
);
323 driver
->base
.API
.Initialize
= init_haiku
;
324 driver
->base
.API
.Terminate
= haiku_terminate
;
325 driver
->base
.API
.CreateContext
= haiku_create_context
;
326 driver
->base
.API
.DestroyContext
= haiku_destroy_context
;
327 driver
->base
.API
.MakeCurrent
= haiku_make_current
;
328 driver
->base
.API
.CreateWindowSurface
= haiku_create_window_surface
;
329 driver
->base
.API
.CreatePixmapSurface
= haiku_create_pixmap_surface
;
330 driver
->base
.API
.CreatePbufferSurface
= haiku_create_pbuffer_surface
;
331 driver
->base
.API
.DestroySurface
= haiku_destroy_surface
;
333 driver
->base
.API
.SwapBuffers
= haiku_swap_buffers
;
335 driver
->base
.Name
= "Haiku";
336 driver
->base
.Unload
= haiku_unload
;
338 _eglLog(_EGL_DEBUG
, "API Calls defined");
340 return &driver
->base
;