diff --git a/contrib/sdk/sources/eglut/Makefile b/contrib/sdk/sources/eglut/Makefile new file mode 100644 index 0000000000..b7f5c6b1ee --- /dev/null +++ b/contrib/sdk/sources/eglut/Makefile @@ -0,0 +1,41 @@ + +LIBRARY= libeglut + +CC=gcc +CFLAGS = -U_Win32 -U_WIN32 -U__MINGW32__ -c -O2 -fomit-frame-pointer + +AR= ar + +INCLUDES= -I. -I../newlib/include -I../Mesa/include -I../Mesa/src/gbm/main -I../render + +LIBPATH:= -L../../lib + + +DEFINES= -D__unix__ -DMESA_EGL_NO_X11_HEADERS + + +SOURCES = eglut.c \ + eglut_screen.c + + +OBJECTS = $(patsubst %.c, %.o, $(SOURCES)) + + +# targets + + +all:$(LIBRARY).a + +$(LIBRARY).a: $(OBJECTS) Makefile + ar cvrs $(LIBRARY).a $(OBJECTS) + mv -f $(LIBRARY).a ../../lib + +%.o : %.c Makefile + $(CC) $(CFLAGS) $(DEFINES) $(INCLUDES) -o $@ $< + +clean: + -rm -f *.o + + + + diff --git a/contrib/sdk/sources/eglut/eglut.c b/contrib/sdk/sources/eglut/eglut.c new file mode 100644 index 0000000000..2f7cacddb6 --- /dev/null +++ b/contrib/sdk/sources/eglut/eglut.c @@ -0,0 +1,335 @@ +/* + * Copyright (C) 2010 LunarG Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Authors: + * Chia-I Wu + */ + +#include +#include +#include +#include +#include + +#include "EGL/egl.h" +#include "EGL/eglext.h" + +#include "render.h" +#include "eglutint.h" +#include + +static struct eglut_state _eglut_state = { + .api_mask = EGLUT_OPENGL_BIT, + .window_width = 300, + .window_height = 300, + .verbose = 0, + .num_windows = 0, +}; + +struct eglut_state *_eglut = &_eglut_state; + +void +_eglutFatal(char *format, ...) +{ + va_list args; + + va_start(args, format); + + fprintf(stderr, "EGLUT: "); + vfprintf(stderr, format, args); + va_end(args); + putc('\n', stderr); + + exit(1); +} + +/* return current time (in milliseconds) */ +int +_eglutNow(void) +{ + struct timeval tv; +#ifdef __VMS + (void) gettimeofday(&tv, NULL ); +#else + struct timezone tz; + (void) gettimeofday(&tv, &tz); +#endif + return tv.tv_sec * 1000 + tv.tv_usec / 1000; +} + +static void +_eglutDestroyWindow(struct eglut_window *win) +{ + eglDestroySurface(_eglut->dpy, win->surface); + _eglutNativeFiniWindow(win); + eglDestroyContext(_eglut->dpy, win->context); +} + +static EGLConfig +_eglutChooseConfig(void) +{ + EGLConfig config; + EGLint config_attribs[32]; + EGLint num_configs, i; + + i = 0; + config_attribs[i++] = EGL_RED_SIZE; + config_attribs[i++] = 1; + config_attribs[i++] = EGL_GREEN_SIZE; + config_attribs[i++] = 1; + config_attribs[i++] = EGL_BLUE_SIZE; + config_attribs[i++] = 1; + config_attribs[i++] = EGL_DEPTH_SIZE; + config_attribs[i++] = 1; + + config_attribs[i++] = EGL_SURFACE_TYPE; + config_attribs[i++] = EGL_WINDOW_BIT; + + config_attribs[i++] = EGL_RENDERABLE_TYPE; + config_attribs[i++] = EGL_OPENGL_BIT; + config_attribs[i] = EGL_NONE; + +// renderable_type = 0x0; +// if (_eglut->api_mask & EGLUT_OPENGL_BIT) +// renderable_type |= EGL_OPENGL_BIT; +// if (_eglut->api_mask & EGLUT_OPENGL_ES1_BIT) +// renderable_type |= EGL_OPENGL_ES_BIT; +// if (_eglut->api_mask & EGLUT_OPENGL_ES2_BIT) +// renderable_type |= EGL_OPENGL_ES2_BIT; +// if (_eglut->api_mask & EGLUT_OPENVG_BIT) +// renderable_type |= EGL_OPENVG_BIT; + + if (!eglChooseConfig(_eglut->dpy, + config_attribs, &config, 1, &num_configs) || !num_configs) + _eglutFatal("failed to choose a config"); + + return config; +} + +static struct eglut_window * +_eglutCreateWindow(const char *title, int x, int y, int w, int h) +{ + struct eglut_window *win; + + win = calloc(1, sizeof(*win)); + if (!win) + _eglutFatal("failed to allocate window"); + + win->config = _eglutChooseConfig(); + + eglBindAPI(EGL_OPENGL_API); + win->context = eglCreateContext(_eglut->dpy, win->config, EGL_NO_CONTEXT, NULL); + if (!win->context) + _eglutFatal("failed to create context"); + + _eglutNativeInitWindow(win, title, x, y, w, h); + switch (_eglut->surface_type) { + case EGL_WINDOW_BIT: + win->surface = eglCreateWindowSurface(_eglut->dpy, + win->config, win->native.u.window, NULL); + break; + default: + break; + } + if (win->surface == EGL_NO_SURFACE) + _eglutFatal("failed to create surface"); + + return win; +} + +void +eglutInitAPIMask(int mask) +{ + _eglut->api_mask = mask; +} + +void +eglutInitWindowSize(int width, int height) +{ + _eglut->window_width = width; + _eglut->window_height = height; +} + +void +eglutInit(int argc, char **argv) +{ + int i; + + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-display") == 0) + _eglut->display_name = argv[++i]; + else if (strcmp(argv[i], "-info") == 0) { + _eglut->verbose = 1; + } + } + + _eglutNativeInitDisplay(); + _eglut->dpy = eglGetDisplay(_eglut->native_dpy); + + if (!eglInitialize(_eglut->dpy, &_eglut->major, &_eglut->minor)) + _eglutFatal("failed to initialize EGL display"); + + _eglut->init_time = _eglutNow(); + + printf("EGL_VERSION = %s\n", eglQueryString(_eglut->dpy, EGL_VERSION)); + if (_eglut->verbose) { + printf("EGL_VENDOR = %s\n", eglQueryString(_eglut->dpy, EGL_VENDOR)); + printf("EGL_EXTENSIONS = %s\n", + eglQueryString(_eglut->dpy, EGL_EXTENSIONS)); + printf("EGL_CLIENT_APIS = %s\n", + eglQueryString(_eglut->dpy, EGL_CLIENT_APIS)); + } +} + +int +eglutGet(int state) +{ + int val; + + switch (state) { + case EGLUT_ELAPSED_TIME: + val = _eglutNow() - _eglut->init_time; + break; + default: + val = -1; + break; + } + + return val; +} + +void +eglutIdleFunc(EGLUTidleCB func) +{ + _eglut->idle_cb = func; +} + +void +eglutPostRedisplay(void) +{ + _eglut->redisplay = 1; +} + +void +eglutMainLoop(void) +{ + struct eglut_window *win = _eglut->current; + + if (!win) + _eglutFatal("no window is created\n"); + + if (win->reshape_cb) + win->reshape_cb(win->native.width, win->native.height); + + _eglutNativeEventLoop(); +} + +void +eglutFini(void) +{ + eglTerminate(_eglut->dpy); + _eglutNativeFiniDisplay(); +} + +void +eglutDestroyWindow(int win) +{ + eglMakeCurrent(_eglut->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); + _eglutDestroyWindow(_eglut->current); +} + +static void +_eglutDefaultKeyboard(unsigned char key) +{ +} + +int +eglutCreateWindow(const char *title) +{ + struct eglut_window *win; + int skinh; + + win = _eglutCreateWindow(title, 20, 20, + _eglut->window_width, _eglut->window_height); + + win->index = _eglut->num_windows++; + win->reshape_cb = NULL; + win->display_cb = NULL; + win->keyboard_cb = _eglutDefaultKeyboard; + win->special_cb = NULL; + + if (!eglMakeCurrent(_eglut->dpy, win->surface, win->surface, win->context)) + _eglutFatal("failed to make window current"); + _eglut->current = win; + + skinh = get_skin_height(); + + _eglut->render = create_render(_eglut->dpy, win->surface, TYPE_3_BORDER_WIDTH, skinh); + return win->index; +} + +int +eglutGetWindowWidth(void) +{ + struct eglut_window *win = _eglut->current; + return win->native.width; +} + +int +eglutGetWindowHeight(void) +{ + struct eglut_window *win = _eglut->current; + return win->native.height; +} + +void +eglutDisplayFunc(EGLUTdisplayCB func) +{ + struct eglut_window *win = _eglut->current; + win->display_cb = func; + +} + +void +eglutReshapeFunc(EGLUTreshapeCB func) +{ + struct eglut_window *win = _eglut->current; + win->reshape_cb = func; +} + +void +eglutKeyboardFunc(EGLUTkeyboardCB func) +{ + struct eglut_window *win = _eglut->current; + win->keyboard_cb = func; +} + +void +eglutSpecialFunc(EGLUTspecialCB func) +{ + struct eglut_window *win = _eglut->current; + win->special_cb = func; +} + +int atexit(void (*func)(void)) +{ + return 0; +}; diff --git a/contrib/sdk/sources/eglut/eglut.h b/contrib/sdk/sources/eglut/eglut.h new file mode 100644 index 0000000000..71b0e0e771 --- /dev/null +++ b/contrib/sdk/sources/eglut/eglut.h @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2010 LunarG Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Authors: + * Chia-I Wu + */ + +#ifndef EGLUT_H +#define EGLUT_H + +/* used by eglutInitAPIMask */ +enum { + EGLUT_OPENGL_BIT = 0x1, + EGLUT_OPENGL_ES1_BIT = 0x2, + EGLUT_OPENGL_ES2_BIT = 0x4, + EGLUT_OPENVG_BIT = 0x8 +}; + +/* used by EGLUTspecialCB */ +enum { + /* function keys */ + EGLUT_KEY_F1, + EGLUT_KEY_F2, + EGLUT_KEY_F3, + EGLUT_KEY_F4, + EGLUT_KEY_F5, + EGLUT_KEY_F6, + EGLUT_KEY_F7, + EGLUT_KEY_F8, + EGLUT_KEY_F9, + EGLUT_KEY_F10, + EGLUT_KEY_F11, + EGLUT_KEY_F12, + + /* directional keys */ + EGLUT_KEY_LEFT = 176, + EGLUT_KEY_UP = 178, + EGLUT_KEY_RIGHT = 179, + EGLUT_KEY_DOWN = 177, +}; + + +/* used by eglutGet */ +enum { + EGLUT_ELAPSED_TIME +}; + +typedef void (*EGLUTidleCB)(void); +typedef void (*EGLUTreshapeCB)(int, int); +typedef void (*EGLUTdisplayCB)(void); +typedef void (*EGLUTkeyboardCB)(unsigned char); +typedef void (*EGLUTspecialCB)(int); + +void eglutInitAPIMask(int mask); +void eglutInitWindowSize(int width, int height); +void eglutInit(int argc, char **argv); +void eglutFini(void); + +int eglutGet(int state); + +void eglutIdleFunc(EGLUTidleCB func); +void eglutPostRedisplay(void); + +void eglutMainLoop(void); + +int eglutCreateWindow(const char *title); +void eglutDestroyWindow(int win); + +int eglutGetWindowWidth(void); +int eglutGetWindowHeight(void); + +void eglutDisplayFunc(EGLUTdisplayCB func); +void eglutReshapeFunc(EGLUTreshapeCB func); +void eglutKeyboardFunc(EGLUTkeyboardCB func); +void eglutSpecialFunc(EGLUTspecialCB func); + +#endif /* EGLUT_H */ diff --git a/contrib/sdk/sources/eglut/eglut_screen.c b/contrib/sdk/sources/eglut/eglut_screen.c new file mode 100644 index 0000000000..ac4ad997c2 --- /dev/null +++ b/contrib/sdk/sources/eglut/eglut_screen.c @@ -0,0 +1,217 @@ +/* + * Copyright (C) 2010 LunarG Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Authors: + * Chia-I Wu + */ + +#include +#include +#include +#include + +#define EGL_EGLEXT_PROTOTYPES +#include "EGL/egl.h" +#include "EGL/eglext.h" +#include "gbm.h" +#include + +#include "eglutint.h" + +#define MAX_MODES 100 + +//static EGLScreenMESA kms_screen; +//static EGLModeMESA kms_mode; +//static EGLint kms_width, kms_height; + +void +_eglutNativeInitDisplay(void) +{ + struct gbm_device *gbm; + int fd; + + fd = get_service("DISPLAY"); + gbm = gbm_create_device(fd); + if( gbm == NULL){ + _eglutFatal("failed to initialize GBM device"); + exit(1); + }; + +// init_pixlib(HW_BIT_BLIT); + + _eglut->native_dpy = gbm; + _eglut->surface_type = EGL_WINDOW_BIT; +} + +void +_eglutNativeFiniDisplay(void) +{ +// done_pixlib(); +} + +#if 0 +static void +init_kms(void) +{ + EGLModeMESA modes[MAX_MODES]; + EGLint num_screens, num_modes; + EGLint width, height, best_mode; + EGLint i; + + if (!eglGetScreensMESA(_eglut->dpy, &kms_screen, 1, &num_screens) || + !num_screens) + _eglutFatal("eglGetScreensMESA failed\n"); + + if (!eglGetModesMESA(_eglut->dpy, kms_screen, + modes, MAX_MODES, &num_modes) || !num_modes) + _eglutFatal("eglGetModesMESA failed!\n"); + + printf("Found %d modes:\n", num_modes); + + best_mode = 0; + width = 0; + height = 0; + for (i = 0; i < num_modes; i++) { + EGLint w, h; + eglGetModeAttribMESA(_eglut->dpy, modes[i], EGL_WIDTH, &w); + eglGetModeAttribMESA(_eglut->dpy, modes[i], EGL_HEIGHT, &h); + printf("%3d: %d x %d\n", i, w, h); + if (w > width && h > height) { + width = w; + height = h; + best_mode = i; + } + } + + printf("Will use screen size: %d x %d\n", width, height); + + kms_mode = modes[best_mode]; + kms_width = width; + kms_height = height; +} +#endif + +void +_eglutNativeInitWindow(struct eglut_window *win, const char *title, + int x, int y, int w, int h) +{ + struct gbm_device *gbm = _eglut->native_dpy; + struct gbm_surface *gs; + + gs = gbm_surface_create(gbm, w, h, GBM_BO_FORMAT_ARGB8888, GBM_BO_USE_RENDERING); + + win->native.u.surface = gs; + + win->native.width = w; + win->native.height = h; + + BeginDraw(); + DrawWindow(x, y, w+TYPE_3_BORDER_WIDTH*2, + h+TYPE_3_BORDER_WIDTH+get_skin_height(), title, 0x000000, 0x74); + + EndDraw(); + +// sna_create_mask(); +} + +void +_eglutNativeFiniWindow(struct eglut_window *win) +{ +// eglShowScreenSurfaceMESA(_eglut->dpy, +// kms_screen, EGL_NO_SURFACE, 0); +// eglDestroySurface(_eglut->dpy, win->native.u.surface); +} + +void +_eglutNativeEventLoop(void) +{ + struct eglut_window *win = _eglut->current; + int start = _eglutNow(); + int frames = 0; + int run = 1; + _eglut->redisplay = 1; + + while (run) + { + int now = _eglutNow(); + oskey_t key; + int ev; + + ev = check_os_event(); +// ev = get_os_event(); + switch(ev) + { + case 1: + BeginDraw(); + DrawWindow(10,10,10,10,NULL,0,0x74); + EndDraw(); + break; + + case 2: + key = get_key(); + if (key.code == 27) + { + run = 0; + continue; + } + if(win->keyboard_cb) + win->keyboard_cb(key.code); + break; + + case 3: + if(get_os_button()==1) + { + run = 0; + continue; + } + break; + }; + + if (now - start >= 5000) + { + double elapsed = (double) (now - start) / 1000.0; + + printf("%d frames in %3.1f seconds = %6.3f FPS\n", + frames, elapsed, frames / elapsed); + fflush(stdout); + + start = now; + frames = 0; + } + + if (_eglut->idle_cb) + _eglut->idle_cb(); + + if (_eglut->redisplay) + { + _eglut->redisplay = 0; + + if (win->display_cb) + win->display_cb(); + + render_swap_and_blit(_eglut->render); + frames++; + } + } + // glFinish(); + eglutDestroyWindow(_eglut->current->index); + eglutFini(); +} diff --git a/contrib/sdk/sources/eglut/eglutint.h b/contrib/sdk/sources/eglut/eglutint.h new file mode 100644 index 0000000000..c3a9e4fedb --- /dev/null +++ b/contrib/sdk/sources/eglut/eglutint.h @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2010 LunarG Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Authors: + * Chia-I Wu + */ + +#ifndef _EGLUTINT_H_ +#define _EGLUTINT_H_ + +#include "EGL/egl.h" +#include "render.h" +#include "eglut.h" + + +struct eglut_window { + EGLConfig config; + EGLContext context; + + /* initialized by native display */ + struct { + union { + EGLNativeWindowType window; + EGLNativePixmapType pixmap; + EGLSurface surface; /* pbuffer or screen surface */ + } u; + int width, height; + } native; + + EGLSurface surface; + + int index; + + EGLUTreshapeCB reshape_cb; + EGLUTdisplayCB display_cb; + EGLUTkeyboardCB keyboard_cb; + EGLUTspecialCB special_cb; +}; + +struct eglut_state { + int api_mask; + int window_width, window_height; + const char *display_name; + int verbose; + int init_time; + + EGLUTidleCB idle_cb; + + int num_windows; + + /* initialized by native display */ + EGLNativeDisplayType native_dpy; + EGLint surface_type; + + EGLDisplay dpy; + EGLint major, minor; + + struct eglut_window *current; + struct render *render; + int redisplay; +}; + +extern struct eglut_state *_eglut; + +void +_eglutFatal(char *format, ...); + +int +_eglutNow(void); + +void +_eglutNativeInitDisplay(void); + +void +_eglutNativeFiniDisplay(void); + +void +_eglutNativeInitWindow(struct eglut_window *win, const char *title, + int x, int y, int w, int h); + +void +_eglutNativeFiniWindow(struct eglut_window *win); + +void +_eglutNativeEventLoop(void); + +#endif /* _EGLUTINT_H_ */