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