1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
4 
5   This software is provided 'as-is', without any express or implied
6   warranty.  In no event will the authors be held liable for any damages
7   arising from the use of this software.
8 
9   Permission is granted to anyone to use this software for any purpose,
10   including commercial applications, and to alter it and redistribute it
11   freely, subject to the following restrictions:
12 
13   1. The origin of this software must not be misrepresented; you must not
14      claim that you wrote the original software. If you use this software
15      in a product, an acknowledgment in the product documentation would be
16      appreciated but is not required.
17   2. Altered source versions must be plainly marked as such, and must not be
18      misrepresented as being the original software.
19   3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22 
23 #if SDL_VIDEO_DRIVER_HAIKU && SDL_VIDEO_OPENGL
24 
25 #include "SDL_bopengl.h"
26 
27 #include <unistd.h>
28 #include <KernelKit.h>
29 #include <OpenGLKit.h>
30 #include "SDL_BWin.h"
31 #include "../../main/haiku/SDL_BApp.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 
_ToBeWin(SDL_Window * window)38 static SDL_INLINE SDL_BWin *_ToBeWin(SDL_Window *window) {
39     return ((SDL_BWin*)(window->driverdata));
40 }
41 
_GetBeApp()42 static SDL_INLINE SDL_BApp *_GetBeApp() {
43     return ((SDL_BApp*)be_app);
44 }
45 
46 /* Passing a NULL path means load pointers from the application */
HAIKU_GL_LoadLibrary(_THIS,const char * path)47 int HAIKU_GL_LoadLibrary(_THIS, const char *path)
48 {
49 /* FIXME: Is this working correctly? */
50     image_info info;
51             int32 cookie = 0;
52     while (get_next_image_info(0, &cookie, &info) == B_OK) {
53         void *location = NULL;
54         if( get_image_symbol(info.id, "glBegin", B_SYMBOL_TYPE_ANY,
55                 &location) == B_OK) {
56 
57             _this->gl_config.dll_handle = (void *) (addr_t) info.id;
58             _this->gl_config.driver_loaded = 1;
59             SDL_strlcpy(_this->gl_config.driver_path, "libGL.so",
60                     SDL_arraysize(_this->gl_config.driver_path));
61         }
62     }
63     return 0;
64 }
65 
HAIKU_GL_GetProcAddress(_THIS,const char * proc)66 void *HAIKU_GL_GetProcAddress(_THIS, const char *proc)
67 {
68     if (_this->gl_config.dll_handle != NULL) {
69         void *location = NULL;
70         status_t err;
71         if ((err =
72             get_image_symbol((image_id) (addr_t) _this->gl_config.dll_handle,
73                               proc, B_SYMBOL_TYPE_ANY,
74                               &location)) == B_OK) {
75             return location;
76         } else {
77                 SDL_SetError("Couldn't find OpenGL symbol");
78                 return NULL;
79         }
80     } else {
81         SDL_SetError("OpenGL library not loaded");
82         return NULL;
83     }
84 }
85 
86 
87 
88 
HAIKU_GL_SwapWindow(_THIS,SDL_Window * window)89 int HAIKU_GL_SwapWindow(_THIS, SDL_Window * window) {
90     _ToBeWin(window)->SwapBuffers();
91     return 0;
92 }
93 
HAIKU_GL_MakeCurrent(_THIS,SDL_Window * window,SDL_GLContext context)94 int HAIKU_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context) {
95     SDL_BWin* win = (SDL_BWin*)context;
96     _GetBeApp()->SetCurrentContext(win ? win->GetGLView() : NULL);
97     return 0;
98 }
99 
100 
HAIKU_GL_CreateContext(_THIS,SDL_Window * window)101 SDL_GLContext HAIKU_GL_CreateContext(_THIS, SDL_Window * window) {
102     /* FIXME: Not sure what flags should be included here; may want to have
103        most of them */
104     SDL_BWin *bwin = _ToBeWin(window);
105     Uint32 gl_flags = BGL_RGB;
106     if (_this->gl_config.alpha_size) {
107         gl_flags |= BGL_ALPHA;
108     }
109     if (_this->gl_config.depth_size) {
110         gl_flags |= BGL_DEPTH;
111     }
112     if (_this->gl_config.stencil_size) {
113         gl_flags |= BGL_STENCIL;
114     }
115     if (_this->gl_config.double_buffer) {
116         gl_flags |= BGL_DOUBLE;
117     } else {
118         gl_flags |= BGL_SINGLE;
119     }
120     if (_this->gl_config.accum_red_size ||
121             _this->gl_config.accum_green_size ||
122             _this->gl_config.accum_blue_size ||
123             _this->gl_config.accum_alpha_size) {
124         gl_flags |= BGL_ACCUM;
125     }
126     bwin->CreateGLView(gl_flags);
127     return (SDL_GLContext)(bwin);
128 }
129 
HAIKU_GL_DeleteContext(_THIS,SDL_GLContext context)130 void HAIKU_GL_DeleteContext(_THIS, SDL_GLContext context) {
131     /* Currently, automatically unlocks the view */
132     ((SDL_BWin*)context)->RemoveGLView();
133 }
134 
135 
HAIKU_GL_SetSwapInterval(_THIS,int interval)136 int HAIKU_GL_SetSwapInterval(_THIS, int interval) {
137     /* TODO: Implement this, if necessary? */
138     return SDL_Unsupported();
139 }
140 
HAIKU_GL_GetSwapInterval(_THIS)141 int HAIKU_GL_GetSwapInterval(_THIS) {
142     /* TODO: Implement this, if necessary? */
143     return 0;
144 }
145 
146 
HAIKU_GL_UnloadLibrary(_THIS)147 void HAIKU_GL_UnloadLibrary(_THIS) {
148     /* TODO: Implement this, if necessary? */
149 }
150 
151 
152 /* FIXME: This function is meant to clear the OpenGL context when the video
153    mode changes (see SDL_bmodes.cc), but it doesn't seem to help, and is not
154    currently in use. */
HAIKU_GL_RebootContexts(_THIS)155 void HAIKU_GL_RebootContexts(_THIS) {
156     SDL_Window *window = _this->windows;
157     while(window) {
158         SDL_BWin *bwin = _ToBeWin(window);
159         if(bwin->GetGLView()) {
160             bwin->LockLooper();
161             bwin->RemoveGLView();
162             bwin->CreateGLView(bwin->GetGLType());
163             bwin->UnlockLooper();
164         }
165         window = window->next;
166     }
167 }
168 
169 
170 #ifdef __cplusplus
171 }
172 #endif
173 
174 #endif /* SDL_VIDEO_DRIVER_HAIKU && SDL_VIDEO_OPENGL */
175 
176 /* vi: set ts=4 sw=4 expandtab: */
177