egl/haiku: remove commented out code
[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 #define CALLOC_STRUCT(T) (struct T *) calloc(1, sizeof(struct T))
46
47
48 _EGL_DRIVER_STANDARD_TYPECASTS(haiku_egl)
49
50
51 struct haiku_egl_driver
52 {
53 _EGLDriver base;
54
55 void *handle;
56 _EGLProc (*get_proc_address)(const char *procname);
57 void (*glFlush)(void);
58 };
59
60 struct haiku_egl_config
61 {
62 _EGLConfig base;
63 };
64
65 struct haiku_egl_context
66 {
67 _EGLContext ctx;
68 };
69
70 struct haiku_egl_surface
71 {
72 _EGLSurface surf;
73 BGLView* gl;
74 };
75
76
77 static void
78 haiku_log(EGLint level, const char *msg)
79 {
80 switch (level) {
81 case _EGL_DEBUG:
82 fprintf(stderr,"%s", msg);
83 break;
84 case _EGL_INFO:
85 fprintf(stderr,"%s", msg);
86 break;
87 case _EGL_WARNING:
88 fprintf(stderr,"%s", msg);
89 break;
90 case _EGL_FATAL:
91 fprintf(stderr,"%s", msg);
92 break;
93 default:
94 break;
95 }
96 }
97
98
99 /**
100 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
101 */
102 static _EGLSurface *
103 haiku_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
104 _EGLConfig *conf, void *native_surface, const EGLint *attrib_list)
105 {
106 return NULL;
107 }
108
109
110 /**
111 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
112 */
113 static _EGLSurface *
114 haiku_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
115 _EGLConfig *conf, void *native_window, const EGLint *attrib_list)
116 {
117 struct haiku_egl_surface* surface;
118 surface = (struct haiku_egl_surface*)calloc(1,sizeof (*surface));
119
120 _eglInitSurface(&surface->surf, disp, EGL_WINDOW_BIT, conf, attrib_list);
121 (&surface->surf)->SwapInterval = 1;
122
123 _eglLog(_EGL_DEBUG, "Creating window");
124 BWindow* win = (BWindow*)native_window;
125
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);
129
130 _eglLog(_EGL_DEBUG, "Adding GL");
131 win->AddChild(surface->gl);
132
133 _eglLog(_EGL_DEBUG, "Showing window");
134 win->Show();
135 return &surface->surf;
136 }
137
138
139 static _EGLSurface *
140 haiku_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
141 _EGLConfig *conf, void *native_pixmap, const EGLint *attrib_list)
142 {
143 return NULL;
144 }
145
146
147 static _EGLSurface *
148 haiku_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
149 _EGLConfig *conf, const EGLint *attrib_list)
150 {
151 return NULL;
152 }
153
154
155 static EGLBoolean
156 haiku_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
157 {
158 return EGL_TRUE;
159 }
160
161
162 static EGLBoolean
163 haiku_add_configs_for_visuals(_EGLDisplay *dpy)
164 {
165 printf("Adding configs\n");
166
167 struct haiku_egl_config* conf;
168 conf = CALLOC_STRUCT(haiku_egl_config);
169
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*/);
201
202 printf("Config configuated\n");
203 if (!_eglValidateConfig(&conf->base, EGL_FALSE)) {
204 _eglLog(_EGL_DEBUG, "Haiku failed to validate config");
205 return EGL_FALSE;
206 }
207 printf("Validated config\n");
208
209 _eglLinkConfig(&conf->base);
210 if (!_eglGetArraySize(dpy->Configs)) {
211 _eglLog(_EGL_WARNING, "Haiku: failed to create any config");
212 return EGL_FALSE;
213 }
214 printf("Config successful!\n");
215
216 return EGL_TRUE;
217 }
218
219 extern "C"
220 EGLBoolean
221 init_haiku(_EGLDriver *drv, _EGLDisplay *dpy)
222 {
223 _eglLog(_EGL_DEBUG,"\nInitializing Haiku EGL\n");
224
225 printf("Initializing Haiku EGL\n");
226 _eglSetLogProc(haiku_log);
227
228 loader_set_logger(_eglLog);
229
230 _eglLog(_EGL_DEBUG,"Add configs");
231 haiku_add_configs_for_visuals(dpy);
232
233 dpy->Version = 14;
234
235 //dpy->Extensions.KHR_create_context = true;
236
237 //dri2_dpy->vtbl = &dri2_haiku_display_vtbl;
238 _eglLog(_EGL_DEBUG, "Initialization finished");
239
240 return EGL_TRUE;
241 }
242
243
244 extern "C"
245 EGLBoolean
246 haiku_terminate(_EGLDriver* drv,_EGLDisplay* dpy)
247 {
248 return EGL_TRUE;
249 }
250
251
252 extern "C"
253 _EGLContext*
254 haiku_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
255 _EGLContext *share_list, const EGLint *attrib_list)
256 {
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;
264 }
265
266
267 extern "C"
268 EGLBoolean
269 haiku_destroy_context(_EGLDriver* drv, _EGLDisplay *disp, _EGLContext* ctx)
270 {
271 ctx=NULL;
272 return EGL_TRUE;
273 }
274
275
276 extern "C"
277 EGLBoolean
278 haiku_make_current(_EGLDriver* drv, _EGLDisplay* dpy, _EGLSurface *dsurf,
279 _EGLSurface *rsurf, _EGLContext *ctx)
280 {
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;
287 surf->gl->LockGL();
288 return EGL_TRUE;
289 }
290
291
292 extern "C"
293 EGLBoolean
294 haiku_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
295 {
296 struct haiku_egl_surface* surface=haiku_egl_surface(surf);
297 surface->gl->SwapBuffers();
298 //gl->Render();
299 return EGL_TRUE;
300 }
301
302
303 extern "C"
304 void
305 haiku_unload(_EGLDriver* drv)
306 {
307
308 }
309
310
311 /**
312 * This is the main entrypoint into the driver, called by libEGL.
313 * Create a new _EGLDriver object and init its dispatch table.
314 */
315 extern "C"
316 _EGLDriver*
317 _eglBuiltInDriverHaiku(const char *args)
318 {
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;
332
333 driver->base.API.SwapBuffers = haiku_swap_buffers;
334
335 driver->base.Name = "Haiku";
336 driver->base.Unload = haiku_unload;
337
338 _eglLog(_EGL_DEBUG, "API Calls defined");
339
340 return &driver->base;
341 }