From 381cbc71923288ed189504b5b784c551f88a1010 Mon Sep 17 00:00:00 2001 From: Chia-I Wu Date: Mon, 9 Nov 2009 11:13:38 +0800 Subject: progs/es1: Port egltri to OpenGL ES 1.1. This demo requires EGL_MESA_screen_surface to run. Signed-off-by: Chia-I Wu --- progs/es1/.gitignore | 1 + progs/es1/screen/Makefile | 28 +++++ progs/es1/screen/tri.c | 129 ++++++++++++++++++++++ progs/es1/screen/winsys.c | 272 ++++++++++++++++++++++++++++++++++++++++++++++ progs/es1/screen/winsys.h | 36 ++++++ 5 files changed, 466 insertions(+) create mode 100644 progs/es1/screen/Makefile create mode 100644 progs/es1/screen/tri.c create mode 100644 progs/es1/screen/winsys.c create mode 100644 progs/es1/screen/winsys.h diff --git a/progs/es1/.gitignore b/progs/es1/.gitignore index 2e7946f4ddd..ac79ee48f2e 100644 --- a/progs/es1/.gitignore +++ b/progs/es1/.gitignore @@ -1,3 +1,4 @@ +screen/tri xegl/drawtex xegl/es1_info xegl/msaa diff --git a/progs/es1/screen/Makefile b/progs/es1/screen/Makefile new file mode 100644 index 00000000000..27be054d891 --- /dev/null +++ b/progs/es1/screen/Makefile @@ -0,0 +1,28 @@ +# progs/es1/screen/Makefile + +TOP = ../../.. +include $(TOP)/configs/current + +ES1_CFLAGS = -I$(TOP)/include +ES1_LIBS = -L$(TOP)/$(LIB_DIR) -lEGL -lGLESv1_CM + +ES1_LIB_DEPS = \ + $(TOP)/$(LIB_DIR)/libEGL.so \ + $(TOP)/$(LIB_DIR)/libGLESv1_CM.so + +WINSYS_OBJS = winsys.o + +PROGRAMS = \ + tri + +.c.o: + $(CC) -c $(ES1_CFLAGS) $(CFLAGS) $< -o $@ + +default: $(PROGRAMS) + +tri: tri.o $(WINSYS_OBJS) $(ES1_LIB_DEPS) + $(CC) $(CFLAGS) -o $@ $@.o $(WINSYS_OBJS) $(ES1_LIBS) + +clean: + -rm -f *.o *~ + -rm -f $(PROGRAMS) diff --git a/progs/es1/screen/tri.c b/progs/es1/screen/tri.c new file mode 100644 index 00000000000..bab9499944b --- /dev/null +++ b/progs/es1/screen/tri.c @@ -0,0 +1,129 @@ +/* + * Copyright (C) 2009 Chia-I Wu + * + * Based on egltri by + * Copyright (C) 1999-2001 Brian Paul All Rights Reserved. + * Copyright (C) 2008 Brian Paul All Rights Reserved. + * Copyright (C) 2008 Jakob Bornecrantz All Rights Reserved. + * + * 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 + * BRIAN PAUL 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. + */ + +#include +#include +#include +#include +#include "winsys.h" + +static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0; + +static void tri_init() +{ + glClearColor(0.4, 0.4, 0.4, 0.0); +} + +static void tri_reshape(int width, int height) +{ + GLfloat ar = (GLfloat) width / (GLfloat) height; + + glViewport(0, 0, (GLint) width, (GLint) height); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustumf(-ar, ar, -1, 1, 5.0, 60.0); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0, 0.0, -10.0); +} + +static void tri_draw(void *data) +{ + static const GLfloat verts[3][2] = { + { -1, -1 }, + { 1, -1 }, + { 0, 1 } + }; + static const GLfloat colors[3][4] = { + { 1, 0, 0, 1 }, + { 0, 1, 0, 1 }, + { 0, 0, 1, 1 } + }; + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glPushMatrix(); + glRotatef(view_rotx, 1, 0, 0); + glRotatef(view_roty, 0, 1, 0); + glRotatef(view_rotz, 0, 0, 1); + + { + glVertexPointer(2, GL_FLOAT, 0, verts); + glColorPointer(4, GL_FLOAT, 0, colors); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_COLOR_ARRAY); + + glDrawArrays(GL_TRIANGLES, 0, 3); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); + } + + glPopMatrix(); +} + +static void tri_run(void) +{ + winsysRun(3.0, tri_draw, NULL); +} + +int main(int argc, char *argv[]) +{ + EGLint width, height; + GLboolean printInfo = GL_FALSE; + int i; + + /* parse cmd line args */ + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-info") == 0) { + printInfo = GL_TRUE; + } + else { + printf("Warning: unknown parameter: %s\n", argv[i]); + } + } + + if (!winsysInitScreen()) + exit(1); + winsysQueryScreenSize(&width, &height); + + if (printInfo) { + printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); + printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); + printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); + printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS)); + } + + tri_init(); + tri_reshape(width, height); + tri_run(); + + winsysFiniScreen(); + + return 0; +} diff --git a/progs/es1/screen/winsys.c b/progs/es1/screen/winsys.c new file mode 100644 index 00000000000..84d00471eba --- /dev/null +++ b/progs/es1/screen/winsys.c @@ -0,0 +1,272 @@ +/* + * Copyright (C) 2009 Chia-I Wu + * + * Based on eglgears by + * Copyright (C) 1999-2001 Brian Paul All Rights Reserved. + * + * 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 + * BRIAN PAUL 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. + */ + +#include +#include +#include +#include +#include + +#define EGL_EGLEXT_PROTOTYPES + +#include +#include + +#include "winsys.h" + +#define MAX_MODES 100 + +static struct { + EGLBoolean verbose; + + EGLDisplay dpy; + EGLConfig conf; + + EGLScreenMESA screen; + EGLModeMESA mode; + EGLint width, height; + + EGLContext ctx; + EGLSurface surf; +} screen; + + +static EGLBoolean +init_screen(void) +{ + EGLModeMESA modes[MAX_MODES]; + EGLint num_screens, num_modes; + EGLint width, height, best_mode; + EGLint i; + + if (!eglGetScreensMESA(screen.dpy, &screen.screen, 1, &num_screens) || + !num_screens) { + printf("eglGetScreensMESA failed\n"); + return EGL_FALSE; + } + + if (!eglGetModesMESA(screen.dpy, screen.screen, modes, MAX_MODES, + &num_modes) || + !num_modes) { + printf("eglGetModesMESA failed!\n"); + return EGL_FALSE; + } + + 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(screen.dpy, modes[i], EGL_WIDTH, &w); + eglGetModeAttribMESA(screen.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; + } + } + + screen.mode = modes[best_mode]; + screen.width = width; + screen.height = height; + + return EGL_TRUE; +} + + +static EGLBoolean +init_display(void) +{ + EGLint maj, min; + const char *exts; + const EGLint attribs[] = { + EGL_SURFACE_TYPE, 0x0, /* should be EGL_SCREEN_BIT_MESA */ + EGL_RENDERABLE_TYPE, 0x0, /* should be EGL_OPENGL_ES_BIT */ + EGL_NONE + }; + EGLint num_configs; + + screen.dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); + if (!screen.dpy) { + printf("eglGetDisplay failed\n"); + return EGL_FALSE; + } + + if (!eglInitialize(screen.dpy, &maj, &min)) { + printf("eglInitialize failed\n"); + return EGL_FALSE; + } + + printf("EGL_VERSION = %s\n", eglQueryString(screen.dpy, EGL_VERSION)); + printf("EGL_VENDOR = %s\n", eglQueryString(screen.dpy, EGL_VENDOR)); + + exts = eglQueryString(screen.dpy, EGL_EXTENSIONS); + assert(exts); + + if (!strstr(exts, "EGL_MESA_screen_surface")) { + printf("EGL_MESA_screen_surface is not supported\n"); + return EGL_FALSE; + } + + if (!eglChooseConfig(screen.dpy, attribs, &screen.conf, 1, + &num_configs) || + !num_configs) { + printf("eglChooseConfig failed\n"); + return EGL_FALSE; + } + + return EGL_TRUE; +} + + +EGLBoolean +winsysInitScreen(void) +{ + EGLint surf_attribs[20]; + EGLint i; + EGLBoolean ok; + + if (!init_display()) + goto fail; + if (!init_screen()) + goto fail; + + /* create context */ + screen.ctx = eglCreateContext(screen.dpy, screen.conf, + EGL_NO_CONTEXT, NULL); + if (screen.ctx == EGL_NO_CONTEXT) { + printf("eglCreateContext failed\n"); + goto fail; + } + + i = 0; + surf_attribs[i++] = EGL_WIDTH; + surf_attribs[i++] = screen.width; + surf_attribs[i++] = EGL_HEIGHT; + surf_attribs[i++] = screen.height; + surf_attribs[i++] = EGL_NONE; + + /* create surface */ + printf("Using screen size: %d x %d\n", screen.width, screen.height); + screen.surf = eglCreateScreenSurfaceMESA(screen.dpy, screen.conf, + surf_attribs); + if (screen.surf == EGL_NO_SURFACE) { + printf("eglCreateScreenSurfaceMESA failed\n"); + goto fail; + } + + ok = eglMakeCurrent(screen.dpy, screen.surf, screen.surf, screen.ctx); + if (!ok) { + printf("eglMakeCurrent failed\n"); + goto fail; + } + + ok = eglShowScreenSurfaceMESA(screen.dpy, screen.screen, + screen.surf, screen.mode); + if (!ok) { + printf("eglShowScreenSurfaceMESA failed\n"); + goto fail; + } + + return EGL_TRUE; + +fail: + winsysFiniScreen(); + return EGL_FALSE; +} + + +EGLBoolean +winsysQueryScreenSize(EGLint *width, EGLint *height) +{ + if (!screen.dpy) + return EGL_FALSE; + + if (width) + *width = screen.width; + if (height) + *height = screen.height; + + return EGL_TRUE; +} + + +void +winsysFiniScreen(void) +{ + if (screen.dpy) { + eglMakeCurrent(screen.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, + EGL_NO_CONTEXT); + if (screen.surf != EGL_NO_SURFACE) + eglDestroySurface(screen.dpy, screen.surf); + if (screen.ctx != EGL_NO_CONTEXT) + eglDestroyContext(screen.dpy, screen.ctx); + eglTerminate(screen.dpy); + + memset(&screen, 0, sizeof(screen)); + } +} + + +void +winsysSwapBuffers(void) +{ + eglSwapBuffers(screen.dpy, screen.surf); +} + + +/* return current time (in seconds) */ +double +winsysNow(void) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + return (double) tv.tv_sec + tv.tv_usec / 1000000.0; +} + + +void +winsysRun(double seconds, void (*draw_frame)(void *data), void *data) +{ + double begin, end, last_frame, duration; + EGLint num_frames = 0; + + begin = winsysNow(); + end = begin + seconds; + + last_frame = begin; + while (last_frame < end) { + draw_frame(data); + winsysSwapBuffers(); + last_frame = winsysNow(); + num_frames++; + } + + duration = last_frame - begin; + printf("%d frames in %3.1f seconds = %6.3f FPS\n", + num_frames, duration, (double) num_frames / duration); +} diff --git a/progs/es1/screen/winsys.h b/progs/es1/screen/winsys.h new file mode 100644 index 00000000000..679c7e0bd6d --- /dev/null +++ b/progs/es1/screen/winsys.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2009 Chia-I Wu + * + * 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 + * BRIAN PAUL 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. + */ + +#ifndef _WINSYS_H_ +#define _WINSYS_H_ + +#include + +EGLBoolean winsysInitScreen(void); +EGLBoolean winsysQueryScreenSize(EGLint *width, EGLint *height); +void winsysFiniScreen(void); + +void winsysSwapBuffers(void); +double winsysNow(void); + +void winsysRun(double seconds, void (*draw_frame)(void *data), void *data); + +#endif /* _WINSYS_H_ */ -- cgit v1.2.3