egl/haiku: kill off haiku_log()
[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 <stdint.h>
28 #include <stdio.h>
29
30 #include "eglconfig.h"
31 #include "eglcontext.h"
32 #include "egldisplay.h"
33 #include "egldriver.h"
34 #include "eglcurrent.h"
35 #include "egllog.h"
36 #include "eglsurface.h"
37 #include "eglimage.h"
38 #include "egltypedefs.h"
39
40 #include <InterfaceKit.h>
41 #include <OpenGLKit.h>
42
43
44 #ifdef DEBUG
45 # define TRACE(x...) printf("egl_haiku: " x)
46 # define CALLED() TRACE("CALLED: %s\n", __PRETTY_FUNCTION__)
47 #else
48 # define TRACE(x...)
49 # define CALLED()
50 #endif
51 #define ERROR(x...) printf("egl_haiku: " x)
52
53
54 _EGL_DRIVER_STANDARD_TYPECASTS(haiku_egl)
55
56
57 struct haiku_egl_driver
58 {
59 _EGLDriver base;
60 };
61
62 struct haiku_egl_config
63 {
64 _EGLConfig base;
65 };
66
67 struct haiku_egl_context
68 {
69 _EGLContext ctx;
70 };
71
72 struct haiku_egl_surface
73 {
74 _EGLSurface surf;
75 BGLView* gl;
76 };
77
78
79 /**
80 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
81 */
82 static _EGLSurface *
83 haiku_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
84 _EGLConfig *conf, void *native_surface, const EGLint *attrib_list)
85 {
86 return NULL;
87 }
88
89
90 /**
91 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
92 */
93 static _EGLSurface *
94 haiku_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
95 _EGLConfig *conf, void *native_window, const EGLint *attrib_list)
96 {
97 CALLED();
98
99 struct haiku_egl_surface* surface;
100 surface = (struct haiku_egl_surface*) calloc(1, sizeof (*surface));
101 if (!surface) {
102 _eglError(EGL_BAD_ALLOC, "haiku_create_window_surface");
103 return NULL;
104 }
105
106 if (!_eglInitSurface(&surface->surf, disp, EGL_WINDOW_BIT, conf, attrib_list))
107 goto cleanup_surface;
108
109 (&surface->surf)->SwapInterval = 1;
110
111 TRACE("Creating window\n");
112 BWindow* win = (BWindow*)native_window;
113
114 TRACE("Creating GL view\n");
115 surface->gl = new BGLView(win->Bounds(), "OpenGL", B_FOLLOW_ALL_SIDES, 0,
116 BGL_RGB | BGL_DOUBLE | BGL_ALPHA);
117
118 TRACE("Adding GL\n");
119 win->AddChild(surface->gl);
120
121 TRACE("Showing window\n");
122 win->Show();
123 return &surface->surf;
124
125 cleanup_surface:
126 free(surface);
127 return NULL;
128 }
129
130
131 static _EGLSurface *
132 haiku_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
133 _EGLConfig *conf, void *native_pixmap, const EGLint *attrib_list)
134 {
135 return NULL;
136 }
137
138
139 static _EGLSurface *
140 haiku_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
141 _EGLConfig *conf, const EGLint *attrib_list)
142 {
143 return NULL;
144 }
145
146
147 static EGLBoolean
148 haiku_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
149 {
150 return EGL_TRUE;
151 }
152
153
154 static EGLBoolean
155 haiku_add_configs_for_visuals(_EGLDisplay *dpy)
156 {
157 CALLED();
158
159 struct haiku_egl_config* conf;
160 conf = (struct haiku_egl_config*) calloc(1, sizeof (*conf));
161 if (!conf) {
162 _eglError(EGL_BAD_ALLOC, "haiku_add_configs_for_visuals");
163 return NULL;
164 }
165
166 _eglInitConfig(&conf->base, dpy, 1);
167 TRACE("Config inited\n");
168
169 _eglSetConfigKey(&conf->base, EGL_RED_SIZE, 8);
170 _eglSetConfigKey(&conf->base, EGL_BLUE_SIZE, 8);
171 _eglSetConfigKey(&conf->base, EGL_GREEN_SIZE, 8);
172 _eglSetConfigKey(&conf->base, EGL_LUMINANCE_SIZE, 0);
173 _eglSetConfigKey(&conf->base, EGL_ALPHA_SIZE, 8);
174 _eglSetConfigKey(&conf->base, EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER);
175 EGLint r = (_eglGetConfigKey(&conf->base, EGL_RED_SIZE)
176 + _eglGetConfigKey(&conf->base, EGL_GREEN_SIZE)
177 + _eglGetConfigKey(&conf->base, EGL_BLUE_SIZE)
178 + _eglGetConfigKey(&conf->base, EGL_ALPHA_SIZE));
179 _eglSetConfigKey(&conf->base, EGL_BUFFER_SIZE, r);
180 _eglSetConfigKey(&conf->base, EGL_CONFIG_CAVEAT, EGL_NONE);
181 _eglSetConfigKey(&conf->base, EGL_CONFIG_ID, 1);
182 _eglSetConfigKey(&conf->base, EGL_BIND_TO_TEXTURE_RGB, EGL_FALSE);
183 _eglSetConfigKey(&conf->base, EGL_BIND_TO_TEXTURE_RGBA, EGL_FALSE);
184 _eglSetConfigKey(&conf->base, EGL_STENCIL_SIZE, 0);
185 _eglSetConfigKey(&conf->base, EGL_TRANSPARENT_TYPE, EGL_NONE);
186 _eglSetConfigKey(&conf->base, EGL_NATIVE_RENDERABLE, EGL_TRUE); // Let's say yes
187 _eglSetConfigKey(&conf->base, EGL_NATIVE_VISUAL_ID, 0); // No visual
188 _eglSetConfigKey(&conf->base, EGL_NATIVE_VISUAL_TYPE, EGL_NONE); // No visual
189 _eglSetConfigKey(&conf->base, EGL_RENDERABLE_TYPE, 0x8);
190 _eglSetConfigKey(&conf->base, EGL_SAMPLE_BUFFERS, 0); // TODO: How to get the right value ?
191 _eglSetConfigKey(&conf->base, EGL_SAMPLES, _eglGetConfigKey(&conf->base, EGL_SAMPLE_BUFFERS) == 0 ? 0 : 0);
192 _eglSetConfigKey(&conf->base, EGL_DEPTH_SIZE, 24); // TODO: How to get the right value ?
193 _eglSetConfigKey(&conf->base, EGL_LEVEL, 0);
194 _eglSetConfigKey(&conf->base, EGL_MAX_PBUFFER_WIDTH, 0); // TODO: How to get the right value ?
195 _eglSetConfigKey(&conf->base, EGL_MAX_PBUFFER_HEIGHT, 0); // TODO: How to get the right value ?
196 _eglSetConfigKey(&conf->base, EGL_MAX_PBUFFER_PIXELS, 0); // TODO: How to get the right value ?
197 _eglSetConfigKey(&conf->base, EGL_SURFACE_TYPE, EGL_WINDOW_BIT /*| EGL_PIXMAP_BIT | EGL_PBUFFER_BIT*/);
198
199 TRACE("Config configuated\n");
200 if (!_eglValidateConfig(&conf->base, EGL_FALSE)) {
201 _eglLog(_EGL_DEBUG, "Haiku: failed to validate config");
202 return EGL_FALSE;
203 }
204 TRACE("Validated config\n");
205
206 _eglLinkConfig(&conf->base);
207 if (!_eglGetArraySize(dpy->Configs)) {
208 _eglLog(_EGL_WARNING, "Haiku: failed to create any config");
209 return EGL_FALSE;
210 }
211 TRACE("Config successfull\n");
212
213 return EGL_TRUE;
214 }
215
216 extern "C"
217 EGLBoolean
218 init_haiku(_EGLDriver *drv, _EGLDisplay *dpy)
219 {
220 CALLED();
221
222 TRACE("Add configs\n");
223 haiku_add_configs_for_visuals(dpy);
224
225 dpy->Version = 14;
226
227 TRACE("Initialization finished\n");
228
229 return EGL_TRUE;
230 }
231
232
233 extern "C"
234 EGLBoolean
235 haiku_terminate(_EGLDriver* drv,_EGLDisplay* dpy)
236 {
237 return EGL_TRUE;
238 }
239
240
241 extern "C"
242 _EGLContext*
243 haiku_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
244 _EGLContext *share_list, const EGLint *attrib_list)
245 {
246 CALLED();
247
248 struct haiku_egl_context* context;
249 context = (struct haiku_egl_context*) calloc(1, sizeof (*context));
250 if (!context) {
251 _eglError(EGL_BAD_ALLOC, "haiku_create_context");
252 return NULL;
253 }
254
255 if (!_eglInitContext(&context->ctx, disp, conf, attrib_list))
256 ERROR("ERROR creating context");
257
258 TRACE("Context created\n");
259 return &context->ctx;
260 }
261
262
263 extern "C"
264 EGLBoolean
265 haiku_destroy_context(_EGLDriver* drv, _EGLDisplay *disp, _EGLContext* ctx)
266 {
267 ctx=NULL;
268 return EGL_TRUE;
269 }
270
271
272 extern "C"
273 EGLBoolean
274 haiku_make_current(_EGLDriver* drv, _EGLDisplay* dpy, _EGLSurface *dsurf,
275 _EGLSurface *rsurf, _EGLContext *ctx)
276 {
277 CALLED();
278
279 struct haiku_egl_context* cont=haiku_egl_context(ctx);
280 struct haiku_egl_surface* surf=haiku_egl_surface(dsurf);
281 _EGLContext *old_ctx;
282 _EGLSurface *old_dsurf, *old_rsurf;
283 _eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf);
284 //cont->ctx.DrawSurface=&surf->surf;
285 surf->gl->LockGL();
286 return EGL_TRUE;
287 }
288
289
290 extern "C"
291 EGLBoolean
292 haiku_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
293 {
294 struct haiku_egl_surface* surface=haiku_egl_surface(surf);
295 surface->gl->SwapBuffers();
296 //gl->Render();
297 return EGL_TRUE;
298 }
299
300
301 extern "C"
302 void
303 haiku_unload(_EGLDriver* drv)
304 {
305
306 }
307
308
309 /**
310 * This is the main entrypoint into the driver, called by libEGL.
311 * Create a new _EGLDriver object and init its dispatch table.
312 */
313 extern "C"
314 _EGLDriver*
315 _eglBuiltInDriverHaiku(const char *args)
316 {
317 CALLED();
318
319 struct haiku_egl_driver* driver;
320 driver = (struct haiku_egl_driver*) calloc(1, sizeof(*driver));
321 if (!driver) {
322 _eglError(EGL_BAD_ALLOC, "_eglBuiltInDriverHaiku");
323 return NULL;
324 }
325
326 _eglInitDriverFallbacks(&driver->base);
327 driver->base.API.Initialize = init_haiku;
328 driver->base.API.Terminate = haiku_terminate;
329 driver->base.API.CreateContext = haiku_create_context;
330 driver->base.API.DestroyContext = haiku_destroy_context;
331 driver->base.API.MakeCurrent = haiku_make_current;
332 driver->base.API.CreateWindowSurface = haiku_create_window_surface;
333 driver->base.API.CreatePixmapSurface = haiku_create_pixmap_surface;
334 driver->base.API.CreatePbufferSurface = haiku_create_pbuffer_surface;
335 driver->base.API.DestroySurface = haiku_destroy_surface;
336
337 driver->base.API.SwapBuffers = haiku_swap_buffers;
338
339 driver->base.Name = "Haiku";
340 driver->base.Unload = haiku_unload;
341
342 TRACE("API Calls defined\n");
343
344 return &driver->base;
345 }