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 "egldevice.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>
46 # define TRACE(x...) printf("egl_haiku: " x)
47 # define CALLED() TRACE("CALLED: %s\n", __PRETTY_FUNCTION__)
52 #define ERROR(x...) printf("egl_haiku: " x)
55 _EGL_DRIVER_STANDARD_TYPECASTS(haiku_egl
)
58 struct haiku_egl_config
63 struct haiku_egl_context
68 struct haiku_egl_surface
76 * Called via eglCreateWindowSurface(), drv->CreateWindowSurface().
79 haiku_create_window_surface(const _EGLDriver
*drv
, _EGLDisplay
*disp
,
80 _EGLConfig
*conf
, void *native_window
, const EGLint
*attrib_list
)
84 struct haiku_egl_surface
* surface
;
85 surface
= (struct haiku_egl_surface
*) calloc(1, sizeof (*surface
));
87 _eglError(EGL_BAD_ALLOC
, "haiku_create_window_surface");
91 if (!_eglInitSurface(&surface
->surf
, disp
, EGL_WINDOW_BIT
,
92 conf
, attrib_list
, native_window
)) {
97 (&surface
->surf
)->SwapInterval
= 1;
99 TRACE("Creating window\n");
100 BWindow
* win
= (BWindow
*)native_window
;
102 TRACE("Creating GL view\n");
103 surface
->gl
= new BGLView(win
->Bounds(), "OpenGL", B_FOLLOW_ALL_SIDES
, 0,
104 BGL_RGB
| BGL_DOUBLE
| BGL_ALPHA
);
106 TRACE("Adding GL\n");
107 win
->AddChild(surface
->gl
);
109 TRACE("Showing window\n");
111 return &surface
->surf
;
116 haiku_create_pixmap_surface(const _EGLDriver
*drv
, _EGLDisplay
*disp
,
117 _EGLConfig
*conf
, void *native_pixmap
, const EGLint
*attrib_list
)
124 haiku_create_pbuffer_surface(const _EGLDriver
*drv
, _EGLDisplay
*disp
,
125 _EGLConfig
*conf
, const EGLint
*attrib_list
)
132 haiku_destroy_surface(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
134 if (_eglPutSurface(surf
)) {
135 // XXX: detach haiku_egl_surface::gl from the native window and destroy it
143 haiku_add_configs_for_visuals(_EGLDisplay
*disp
)
147 struct haiku_egl_config
* conf
;
148 conf
= (struct haiku_egl_config
*) calloc(1, sizeof (*conf
));
150 return _eglError(EGL_BAD_ALLOC
, "haiku_add_configs_for_visuals");
152 _eglInitConfig(&conf
->base
, disp
, 1);
153 TRACE("Config inited\n");
155 conf
->base
.RedSize
= 8;
156 conf
->base
.BlueSize
= 8;
157 conf
->base
.GreenSize
= 8;
158 conf
->base
.LuminanceSize
= 0;
159 conf
->base
.AlphaSize
= 8;
160 conf
->base
.ColorBufferType
= EGL_RGB_BUFFER
;
161 conf
->base
.BufferSize
= conf
->base
.RedSize
162 + conf
->base
.GreenSize
163 + conf
->base
.BlueSize
164 + conf
->base
.AlphaSize
;
165 conf
->base
.ConfigCaveat
= EGL_NONE
;
166 conf
->base
.ConfigId
= 1;
167 conf
->base
.BindToTextureRGB
= EGL_FALSE
;
168 conf
->base
.BindToTextureRGBA
= EGL_FALSE
;
169 conf
->base
.StencilSize
= 0;
170 conf
->base
.TransparentType
= EGL_NONE
;
171 conf
->base
.NativeRenderable
= EGL_TRUE
; // Let's say yes
172 conf
->base
.NativeVisualID
= 0; // No visual
173 conf
->base
.NativeVisualType
= EGL_NONE
; // No visual
174 conf
->base
.RenderableType
= 0x8;
175 conf
->base
.SampleBuffers
= 0; // TODO: How to get the right value ?
176 conf
->base
.Samples
= conf
->base
.SampleBuffers
== 0 ? 0 : 0;
177 conf
->base
.DepthSize
= 24; // TODO: How to get the right value ?
178 conf
->base
.Level
= 0;
179 conf
->base
.MaxPbufferWidth
= 0; // TODO: How to get the right value ?
180 conf
->base
.MaxPbufferHeight
= 0; // TODO: How to get the right value ?
181 conf
->base
.MaxPbufferPixels
= 0; // TODO: How to get the right value ?
182 conf
->base
.SurfaceType
= EGL_WINDOW_BIT
/*| EGL_PIXMAP_BIT | EGL_PBUFFER_BIT*/;
184 TRACE("Config configuated\n");
185 if (!_eglValidateConfig(&conf
->base
, EGL_FALSE
)) {
186 _eglLog(_EGL_DEBUG
, "Haiku: failed to validate config");
189 TRACE("Validated config\n");
191 _eglLinkConfig(&conf
->base
);
192 if (!_eglGetArraySize(disp
->Configs
)) {
193 _eglLog(_EGL_WARNING
, "Haiku: failed to create any config");
196 TRACE("Config successfull\n");
208 init_haiku(_EGLDisplay
*disp
)
213 dev
= _eglAddDevice(-1, true);
215 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to find EGLDevice");
220 TRACE("Add configs\n");
221 if (!haiku_add_configs_for_visuals(disp
))
224 TRACE("Initialization finished\n");
232 haiku_terminate(_EGLDisplay
*disp
)
240 haiku_create_context(_EGLDisplay
*disp
, _EGLConfig
*conf
,
241 _EGLContext
*share_list
, const EGLint
*attrib_list
)
245 struct haiku_egl_context
* context
;
246 context
= (struct haiku_egl_context
*) calloc(1, sizeof (*context
));
248 _eglError(EGL_BAD_ALLOC
, "haiku_create_context");
252 if (!_eglInitContext(&context
->ctx
, disp
, conf
, attrib_list
))
255 TRACE("Context created\n");
256 return &context
->ctx
;
266 haiku_destroy_context(_EGLDisplay
*disp
, _EGLContext
* ctx
)
268 struct haiku_egl_context
* context
= haiku_egl_context(ctx
);
270 if (_eglPutContext(ctx
)) {
271 // XXX: teardown the context ?
281 haiku_make_current(const _EGLDriver
* drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
282 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
286 struct haiku_egl_context
* cont
= haiku_egl_context(ctx
);
287 struct haiku_egl_surface
* surf
= haiku_egl_surface(dsurf
);
288 _EGLContext
*old_ctx
;
289 _EGLSurface
*old_dsurf
, *old_rsurf
;
291 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
))
294 //cont->ctx.DrawSurface=&surf->surf;
302 haiku_swap_buffers(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
304 struct haiku_egl_surface
* surface
= haiku_egl_surface(surf
);
306 surface
->gl
->SwapBuffers();
313 const _EGLDriver _eglDriver
= {
314 .Initialize
= init_haiku
,
315 .Terminate
= haiku_terminate
,
316 .CreateContext
= haiku_create_context
,
317 .DestroyContext
= haiku_destroy_context
,
318 .MakeCurrent
= haiku_make_current
,
319 .CreateWindowSurface
= haiku_create_window_surface
,
320 .CreatePixmapSurface
= haiku_create_pixmap_surface
,
321 .CreatePbufferSurface
= haiku_create_pbuffer_surface
,
322 .DestroySurface
= haiku_destroy_surface
,
323 .SwapBuffers
= haiku_swap_buffers
,