blob: 58872433ee6aa05be86cd1fa38912dcbac4ae87f [file] [log] [blame]
Alexander von Gluck IV400b8332014-12-22 10:10:13 -05001/*
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>
Emil Velikovbf0e4d22015-02-28 17:12:40 +000027#include <stdint.h>
Alexander von Gluck IV400b8332014-12-22 10:10:13 -050028#include <stdio.h>
29
Alexander von Gluck IV400b8332014-12-22 10:10:13 -050030#include "eglconfig.h"
31#include "eglcontext.h"
Alexander von Gluck IV1b97a722018-12-27 20:41:47 +000032#include "egldevice.h"
Alexander von Gluck IV400b8332014-12-22 10:10:13 -050033#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"
Alexander von Gluck IV400b8332014-12-22 10:10:13 -050040
41#include <InterfaceKit.h>
42#include <OpenGLKit.h>
43
44
Emil Velikoved9dcdf2015-06-11 12:02:45 +010045#ifdef DEBUG
46# define TRACE(x...) printf("egl_haiku: " x)
47# define CALLED() TRACE("CALLED: %s\n", __PRETTY_FUNCTION__)
48#else
49# define TRACE(x...)
50# define CALLED()
51#endif
52#define ERROR(x...) printf("egl_haiku: " x)
53
Alexander von Gluck IV400b8332014-12-22 10:10:13 -050054
55_EGL_DRIVER_STANDARD_TYPECASTS(haiku_egl)
56
57
Alexander von Gluck IV400b8332014-12-22 10:10:13 -050058struct haiku_egl_config
59{
Alexander von Gluck IVe7ac2122014-12-22 16:02:50 +000060 _EGLConfig base;
Alexander von Gluck IV400b8332014-12-22 10:10:13 -050061};
62
63struct haiku_egl_context
64{
65 _EGLContext ctx;
66};
67
68struct haiku_egl_surface
69{
70 _EGLSurface surf;
71 BGLView* gl;
72};
73
74
Alexander von Gluck IV400b8332014-12-22 10:10:13 -050075/**
Eric Engestromcc034482020-07-20 13:38:24 +020076 * Called via eglCreateWindowSurface(), drv->CreateWindowSurface().
Alexander von Gluck IV400b8332014-12-22 10:10:13 -050077 */
78static _EGLSurface *
Eric Engestrom9c6fa942020-07-31 01:38:41 +020079haiku_create_window_surface(const _EGLDriver *drv, _EGLDisplay *disp,
Alexander von Gluck IV400b8332014-12-22 10:10:13 -050080 _EGLConfig *conf, void *native_window, const EGLint *attrib_list)
81{
Emil Velikoved9dcdf2015-06-11 12:02:45 +010082 CALLED();
83
Alexander von Gluck IV400b8332014-12-22 10:10:13 -050084 struct haiku_egl_surface* surface;
Emil Velikov46f87b22015-06-11 12:17:23 +010085 surface = (struct haiku_egl_surface*) calloc(1, sizeof (*surface));
86 if (!surface) {
87 _eglError(EGL_BAD_ALLOC, "haiku_create_window_surface");
88 return NULL;
89 }
Alexander von Gluck IV400b8332014-12-22 10:10:13 -050090
Alexander von Gluck IVe22e0de2015-06-29 23:29:44 -050091 if (!_eglInitSurface(&surface->surf, disp, EGL_WINDOW_BIT,
Paulo Zanoni04ecda32019-05-01 15:42:26 -070092 conf, attrib_list, native_window)) {
Alexander von Gluck IVe22e0de2015-06-29 23:29:44 -050093 free(surface);
94 return NULL;
95 }
Emil Velikov46f87b22015-06-11 12:17:23 +010096
Alexander von Gluck IV400b8332014-12-22 10:10:13 -050097 (&surface->surf)->SwapInterval = 1;
98
Emil Velikoved9dcdf2015-06-11 12:02:45 +010099 TRACE("Creating window\n");
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500100 BWindow* win = (BWindow*)native_window;
101
Emil Velikoved9dcdf2015-06-11 12:02:45 +0100102 TRACE("Creating GL view\n");
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500103 surface->gl = new BGLView(win->Bounds(), "OpenGL", B_FOLLOW_ALL_SIDES, 0,
104 BGL_RGB | BGL_DOUBLE | BGL_ALPHA);
105
Emil Velikoved9dcdf2015-06-11 12:02:45 +0100106 TRACE("Adding GL\n");
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500107 win->AddChild(surface->gl);
108
Emil Velikoved9dcdf2015-06-11 12:02:45 +0100109 TRACE("Showing window\n");
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500110 win->Show();
111 return &surface->surf;
112}
113
114
115static _EGLSurface *
Eric Engestrom9c6fa942020-07-31 01:38:41 +0200116haiku_create_pixmap_surface(const _EGLDriver *drv, _EGLDisplay *disp,
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500117 _EGLConfig *conf, void *native_pixmap, const EGLint *attrib_list)
118{
Alexander von Gluck IVe7ac2122014-12-22 16:02:50 +0000119 return NULL;
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500120}
121
122
123static _EGLSurface *
Eric Engestrom9c6fa942020-07-31 01:38:41 +0200124haiku_create_pbuffer_surface(const _EGLDriver *drv, _EGLDisplay *disp,
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500125 _EGLConfig *conf, const EGLint *attrib_list)
126{
Alexander von Gluck IVe7ac2122014-12-22 16:02:50 +0000127 return NULL;
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500128}
129
130
131static EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +0200132haiku_destroy_surface(const _EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500133{
Emil Velikove77a32f2015-06-11 12:33:55 +0100134 if (_eglPutSurface(surf)) {
135 // XXX: detach haiku_egl_surface::gl from the native window and destroy it
136 free(surf);
Alexander von Gluck IVe22e0de2015-06-29 23:29:44 -0500137 }
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500138 return EGL_TRUE;
139}
140
141
142static EGLBoolean
Eric Engestrom54fa5ec2019-02-02 11:38:45 +0000143haiku_add_configs_for_visuals(_EGLDisplay *disp)
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500144{
Emil Velikoved9dcdf2015-06-11 12:02:45 +0100145 CALLED();
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500146
Alexander von Gluck IVe7ac2122014-12-22 16:02:50 +0000147 struct haiku_egl_config* conf;
Emil Velikov46f87b22015-06-11 12:17:23 +0100148 conf = (struct haiku_egl_config*) calloc(1, sizeof (*conf));
Emil Velikovc58af5c2017-06-20 15:40:28 +0100149 if (!conf)
150 return _eglError(EGL_BAD_ALLOC, "haiku_add_configs_for_visuals");
Alexander von Gluck IVe7ac2122014-12-22 16:02:50 +0000151
Eric Engestrom54fa5ec2019-02-02 11:38:45 +0000152 _eglInitConfig(&conf->base, disp, 1);
Emil Velikoved9dcdf2015-06-11 12:02:45 +0100153 TRACE("Config inited\n");
154
Eric Engestrom7bee3882020-02-13 15:53:03 +0000155 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*/;
Alexander von Gluck IVe7ac2122014-12-22 16:02:50 +0000183
Emil Velikoved9dcdf2015-06-11 12:02:45 +0100184 TRACE("Config configuated\n");
Alexander von Gluck IVe7ac2122014-12-22 16:02:50 +0000185 if (!_eglValidateConfig(&conf->base, EGL_FALSE)) {
Emil Velikoved9dcdf2015-06-11 12:02:45 +0100186 _eglLog(_EGL_DEBUG, "Haiku: failed to validate config");
Emil Velikovb0f33e92015-06-11 13:07:08 +0100187 goto cleanup;
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500188 }
Emil Velikoved9dcdf2015-06-11 12:02:45 +0100189 TRACE("Validated config\n");
Alexander von Gluck IVe22e0de2015-06-29 23:29:44 -0500190
Alexander von Gluck IVe7ac2122014-12-22 16:02:50 +0000191 _eglLinkConfig(&conf->base);
Eric Engestrom54fa5ec2019-02-02 11:38:45 +0000192 if (!_eglGetArraySize(disp->Configs)) {
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500193 _eglLog(_EGL_WARNING, "Haiku: failed to create any config");
Emil Velikovb0f33e92015-06-11 13:07:08 +0100194 goto cleanup;
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500195 }
Emil Velikoved9dcdf2015-06-11 12:02:45 +0100196 TRACE("Config successfull\n");
Emil Velikovb0f33e92015-06-11 13:07:08 +0100197
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500198 return EGL_TRUE;
Emil Velikovb0f33e92015-06-11 13:07:08 +0100199
200cleanup:
201 free(conf);
202 return EGL_FALSE;
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500203}
204
Alexander von Gluck IVe22e0de2015-06-29 23:29:44 -0500205
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500206extern "C"
207EGLBoolean
Eric Engestromad61d4f2018-04-22 16:48:15 +0200208init_haiku(_EGLDisplay *disp)
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500209{
Emil Velikov00992702018-09-04 11:20:03 +0100210 _EGLDevice *dev;
Emil Velikoved9dcdf2015-06-11 12:02:45 +0100211 CALLED();
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500212
Emil Velikov00992702018-09-04 11:20:03 +0100213 dev = _eglAddDevice(-1, true);
214 if (!dev) {
215 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to find EGLDevice");
216 return EGL_FALSE;
217 }
Eric Engestrom54fa5ec2019-02-02 11:38:45 +0000218 disp->Device = dev;
Emil Velikov00992702018-09-04 11:20:03 +0100219
Emil Velikoved9dcdf2015-06-11 12:02:45 +0100220 TRACE("Add configs\n");
Eric Engestrom54fa5ec2019-02-02 11:38:45 +0000221 if (!haiku_add_configs_for_visuals(disp))
Emil Velikovb0f33e92015-06-11 13:07:08 +0100222 return EGL_FALSE;
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500223
Emil Velikoved9dcdf2015-06-11 12:02:45 +0100224 TRACE("Initialization finished\n");
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500225
226 return EGL_TRUE;
227}
228
229
230extern "C"
231EGLBoolean
Eric Engestrom435ad512018-04-22 16:48:15 +0200232haiku_terminate(_EGLDisplay *disp)
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500233{
234 return EGL_TRUE;
235}
236
237
238extern "C"
239_EGLContext*
Eric Engestrom9c6fa942020-07-31 01:38:41 +0200240haiku_create_context(const _EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500241 _EGLContext *share_list, const EGLint *attrib_list)
242{
Emil Velikoved9dcdf2015-06-11 12:02:45 +0100243 CALLED();
244
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500245 struct haiku_egl_context* context;
Emil Velikov46f87b22015-06-11 12:17:23 +0100246 context = (struct haiku_egl_context*) calloc(1, sizeof (*context));
247 if (!context) {
248 _eglError(EGL_BAD_ALLOC, "haiku_create_context");
249 return NULL;
250 }
Emil Velikoved9dcdf2015-06-11 12:02:45 +0100251
252 if (!_eglInitContext(&context->ctx, disp, conf, attrib_list))
Emil Velikovb0f33e92015-06-11 13:07:08 +0100253 goto cleanup;
Emil Velikoved9dcdf2015-06-11 12:02:45 +0100254
255 TRACE("Context created\n");
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500256 return &context->ctx;
Emil Velikovb0f33e92015-06-11 13:07:08 +0100257
258cleanup:
259 free(context);
260 return NULL;
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500261}
262
263
264extern "C"
265EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +0200266haiku_destroy_context(const _EGLDriver* drv, _EGLDisplay *disp, _EGLContext* ctx)
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500267{
Emil Velikovb0f33e92015-06-11 13:07:08 +0100268 struct haiku_egl_context* context = haiku_egl_context(ctx);
269
270 if (_eglPutContext(ctx)) {
271 // XXX: teardown the context ?
272 free(context);
Alexander von Gluck IVe22e0de2015-06-29 23:29:44 -0500273 ctx = NULL;
Emil Velikovb0f33e92015-06-11 13:07:08 +0100274 }
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500275 return EGL_TRUE;
276}
277
278
279extern "C"
280EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +0200281haiku_make_current(const _EGLDriver* drv, _EGLDisplay *disp, _EGLSurface *dsurf,
Alexander von Gluck IVe22e0de2015-06-29 23:29:44 -0500282 _EGLSurface *rsurf, _EGLContext *ctx)
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500283{
Emil Velikoved9dcdf2015-06-11 12:02:45 +0100284 CALLED();
285
Emil Velikov0e55db32015-06-11 13:08:00 +0100286 struct haiku_egl_context* cont = haiku_egl_context(ctx);
287 struct haiku_egl_surface* surf = haiku_egl_surface(dsurf);
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500288 _EGLContext *old_ctx;
Emil Velikov0e55db32015-06-11 13:08:00 +0100289 _EGLSurface *old_dsurf, *old_rsurf;
Emil Velikovb0f33e92015-06-11 13:07:08 +0100290
291 if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf))
292 return EGL_FALSE;
293
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500294 //cont->ctx.DrawSurface=&surf->surf;
295 surf->gl->LockGL();
296 return EGL_TRUE;
297}
298
299
300extern "C"
301EGLBoolean
Eric Engestrom9c6fa942020-07-31 01:38:41 +0200302haiku_swap_buffers(const _EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500303{
Emil Velikov0e55db32015-06-11 13:08:00 +0100304 struct haiku_egl_surface* surface = haiku_egl_surface(surf);
305
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500306 surface->gl->SwapBuffers();
307 //gl->Render();
308 return EGL_TRUE;
309}
310
311
Alexander von Gluck IV400b8332014-12-22 10:10:13 -0500312extern "C"
Eric Engestrom9c6fa942020-07-31 01:38:41 +0200313const _EGLDriver _eglDriver = {
Eric Engestromd24e3ea2020-07-22 01:06:05 +0200314 .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,
324};