From 9db3d07b9620b4269ab33f78604a36327e536ce1 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Queru Date: Thu, 12 Nov 2009 18:45:53 -0800 Subject: eclair snapshot --- opengl/tests/angeles/Android.mk | 2 +- opengl/tests/angeles/app-linux.c | 223 ------------- opengl/tests/angeles/app-linux.cpp | 213 ++++++++++++ opengl/tests/fillrate/Android.mk | 17 + opengl/tests/fillrate/fillrate.cpp | 161 ++++++++++ opengl/tests/filter/Android.mk | 4 +- opengl/tests/filter/filter.c | 130 -------- opengl/tests/filter/filter.cpp | 190 +++++++++++ opengl/tests/finish/Android.mk | 4 +- opengl/tests/finish/finish.c | 224 ------------- opengl/tests/finish/finish.cpp | 226 +++++++++++++ opengl/tests/gl2_basic/Android.mk | 19 ++ opengl/tests/gl2_basic/gl2_basic.cpp | 357 +++++++++++++++++++++ opengl/tests/gl2_jni/Android.mk | 51 +++ opengl/tests/gl2_jni/AndroidManifest.xml | 35 ++ opengl/tests/gl2_jni/jni/gl_code.cpp | 165 ++++++++++ opengl/tests/gl2_jni/res/values/strings.xml | 29 ++ .../src/com/android/gl2jni/GL2JNIActivity.java | 46 +++ .../gl2_jni/src/com/android/gl2jni/GL2JNILib.java | 33 ++ .../gl2_jni/src/com/android/gl2jni/GL2JNIView.java | 139 ++++++++ opengl/tests/gl_basic/Android.mk | 17 + opengl/tests/gl_basic/gl_basic.cpp | 357 +++++++++++++++++++++ opengl/tests/gl_jni/Android.mk | 53 +++ opengl/tests/gl_jni/AndroidManifest.xml | 38 +++ opengl/tests/gl_jni/jni/gl_code.cpp | 183 +++++++++++ opengl/tests/gl_jni/res/values/strings.xml | 29 ++ .../src/com/android/gljni/GLJNIActivity.java | 45 +++ .../gl_jni/src/com/android/gljni/GLJNILib.java | 34 ++ .../gl_jni/src/com/android/gljni/GLJNIView.java | 90 ++++++ opengl/tests/gralloc/Android.mk | 16 + opengl/tests/gralloc/gralloc.cpp | 110 +++++++ opengl/tests/linetex/Android.mk | 17 + opengl/tests/linetex/linetex.cpp | 116 +++++++ opengl/tests/swapinterval/Android.mk | 17 + opengl/tests/swapinterval/swapinterval.cpp | 121 +++++++ opengl/tests/textures/Android.mk | 4 +- opengl/tests/textures/textures.c | 109 ------- opengl/tests/textures/textures.cpp | 118 +++++++ opengl/tests/tritex/Android.mk | 2 +- opengl/tests/tritex/tritex.c | 273 ---------------- opengl/tests/tritex/tritex.cpp | 277 ++++++++++++++++ 41 files changed, 3330 insertions(+), 964 deletions(-) delete mode 100644 opengl/tests/angeles/app-linux.c create mode 100644 opengl/tests/angeles/app-linux.cpp create mode 100644 opengl/tests/fillrate/Android.mk create mode 100644 opengl/tests/fillrate/fillrate.cpp delete mode 100644 opengl/tests/filter/filter.c create mode 100644 opengl/tests/filter/filter.cpp delete mode 100644 opengl/tests/finish/finish.c create mode 100644 opengl/tests/finish/finish.cpp create mode 100644 opengl/tests/gl2_basic/Android.mk create mode 100644 opengl/tests/gl2_basic/gl2_basic.cpp create mode 100644 opengl/tests/gl2_jni/Android.mk create mode 100644 opengl/tests/gl2_jni/AndroidManifest.xml create mode 100644 opengl/tests/gl2_jni/jni/gl_code.cpp create mode 100644 opengl/tests/gl2_jni/res/values/strings.xml create mode 100644 opengl/tests/gl2_jni/src/com/android/gl2jni/GL2JNIActivity.java create mode 100644 opengl/tests/gl2_jni/src/com/android/gl2jni/GL2JNILib.java create mode 100644 opengl/tests/gl2_jni/src/com/android/gl2jni/GL2JNIView.java create mode 100644 opengl/tests/gl_basic/Android.mk create mode 100644 opengl/tests/gl_basic/gl_basic.cpp create mode 100644 opengl/tests/gl_jni/Android.mk create mode 100644 opengl/tests/gl_jni/AndroidManifest.xml create mode 100644 opengl/tests/gl_jni/jni/gl_code.cpp create mode 100644 opengl/tests/gl_jni/res/values/strings.xml create mode 100644 opengl/tests/gl_jni/src/com/android/gljni/GLJNIActivity.java create mode 100644 opengl/tests/gl_jni/src/com/android/gljni/GLJNILib.java create mode 100644 opengl/tests/gl_jni/src/com/android/gljni/GLJNIView.java create mode 100644 opengl/tests/gralloc/Android.mk create mode 100644 opengl/tests/gralloc/gralloc.cpp create mode 100644 opengl/tests/linetex/Android.mk create mode 100644 opengl/tests/linetex/linetex.cpp create mode 100644 opengl/tests/swapinterval/Android.mk create mode 100644 opengl/tests/swapinterval/swapinterval.cpp delete mode 100644 opengl/tests/textures/textures.c create mode 100644 opengl/tests/textures/textures.cpp delete mode 100644 opengl/tests/tritex/tritex.c create mode 100644 opengl/tests/tritex/tritex.cpp (limited to 'opengl/tests') diff --git a/opengl/tests/angeles/Android.mk b/opengl/tests/angeles/Android.mk index e193483f5d69..d0c3221cfba4 100644 --- a/opengl/tests/angeles/Android.mk +++ b/opengl/tests/angeles/Android.mk @@ -2,7 +2,7 @@ LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) -LOCAL_SRC_FILES:= app-linux.c demo.c.arm +LOCAL_SRC_FILES:= app-linux.cpp demo.c.arm LOCAL_SHARED_LIBRARIES := libEGL libGLESv1_CM libui LOCAL_MODULE:= angeles LOCAL_MODULE_TAGS := optional diff --git a/opengl/tests/angeles/app-linux.c b/opengl/tests/angeles/app-linux.c deleted file mode 100644 index 7d0d320a3241..000000000000 --- a/opengl/tests/angeles/app-linux.c +++ /dev/null @@ -1,223 +0,0 @@ -/* San Angeles Observation OpenGL ES version example - * Copyright 2004-2005 Jetro Lauha - * All rights reserved. - * Web: http://iki.fi/jetro/ - * - * This source is free software; you can redistribute it and/or - * modify it under the terms of EITHER: - * (1) The GNU Lesser General Public License as published by the Free - * Software Foundation; either version 2.1 of the License, or (at - * your option) any later version. The text of the GNU Lesser - * General Public License is included with this source in the - * file LICENSE-LGPL.txt. - * (2) The BSD-style license that is included with this source in - * the file LICENSE-BSD.txt. - * - * This source is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files - * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details. - * - * $Id: app-linux.c,v 1.4 2005/02/08 18:42:48 tonic Exp $ - * $Revision: 1.4 $ - * - * Parts of this source file is based on test/example code from - * GLESonGL implementation by David Blythe. Here is copy of the - * license notice from that source: - * - * Copyright (C) 2003 David Blythe 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 - * DAVID BLYTHE 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 - -#include "app.h" - - -int gAppAlive = 1; - -static const char sAppName[] = - "San Angeles Observation OpenGL ES version example (Linux)"; - -static int sWindowWidth = WINDOW_DEFAULT_WIDTH; -static int sWindowHeight = WINDOW_DEFAULT_HEIGHT; -static EGLDisplay sEglDisplay = EGL_NO_DISPLAY; -static EGLContext sEglContext = EGL_NO_CONTEXT; -static EGLSurface sEglSurface = EGL_NO_SURFACE; - -const char *egl_strerror(unsigned err) -{ - switch(err){ - case EGL_SUCCESS: return "SUCCESS"; - case EGL_NOT_INITIALIZED: return "NOT INITIALIZED"; - case EGL_BAD_ACCESS: return "BAD ACCESS"; - case EGL_BAD_ALLOC: return "BAD ALLOC"; - case EGL_BAD_ATTRIBUTE: return "BAD_ATTRIBUTE"; - case EGL_BAD_CONFIG: return "BAD CONFIG"; - case EGL_BAD_CONTEXT: return "BAD CONTEXT"; - case EGL_BAD_CURRENT_SURFACE: return "BAD CURRENT SURFACE"; - case EGL_BAD_DISPLAY: return "BAD DISPLAY"; - case EGL_BAD_MATCH: return "BAD MATCH"; - case EGL_BAD_NATIVE_PIXMAP: return "BAD NATIVE PIXMAP"; - case EGL_BAD_NATIVE_WINDOW: return "BAD NATIVE WINDOW"; - case EGL_BAD_PARAMETER: return "BAD PARAMETER"; - case EGL_BAD_SURFACE: return "BAD_SURFACE"; -// case EGL_CONTEXT_LOST: return "CONTEXT LOST"; - default: return "UNKNOWN"; - } -} - -void egl_error(const char *name) -{ - unsigned err = eglGetError(); - if(err != EGL_SUCCESS) { - fprintf(stderr,"%s(): egl error 0x%x (%s)\n", - name, err, egl_strerror(err)); - } -} - -static void checkGLErrors() -{ - GLenum error = glGetError(); - if (error != GL_NO_ERROR) - fprintf(stderr, "GL Error: 0x%04x\n", (int)error); -} - - -static void checkEGLErrors() -{ - EGLint error = eglGetError(); - // GLESonGL seems to be returning 0 when there is no errors? - if (error && error != EGL_SUCCESS) - fprintf(stderr, "EGL Error: 0x%04x\n", (int)error); -} - -static int initGraphics() -{ - EGLint s_configAttribs[] = { - EGL_RED_SIZE, 5, - EGL_GREEN_SIZE, 6, - EGL_BLUE_SIZE, 5, - #if 1 - EGL_DEPTH_SIZE, 16, - EGL_STENCIL_SIZE, 0, - #else - EGL_ALPHA_SIZE, EGL_DONT_CARE, - EGL_DEPTH_SIZE, EGL_DONT_CARE, - EGL_STENCIL_SIZE, EGL_DONT_CARE, - EGL_SURFACE_TYPE, EGL_DONT_CARE, - #endif - EGL_NONE - }; - - EGLint numConfigs = -1; - EGLint majorVersion; - EGLint minorVersion; - EGLConfig config; - EGLContext context; - EGLSurface surface; - - EGLDisplay dpy; - - dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); - egl_error("eglGetDisplay"); - fprintf(stderr,"dpy = 0x%08x\n", (unsigned) dpy); - - eglInitialize(dpy, &majorVersion, &minorVersion); - egl_error("eglInitialize"); - - eglGetConfigs(dpy, NULL, 0, &numConfigs); - egl_error("eglGetConfigs"); - fprintf(stderr,"num configs %d\n", numConfigs); - - eglChooseConfig(dpy, s_configAttribs, &config, 1, &numConfigs); - egl_error("eglChooseConfig"); - - surface = eglCreateWindowSurface(dpy, config, - android_createDisplaySurface(), NULL); - egl_error("eglMapWindowSurface"); - - fprintf(stderr,"surface = %p\n", surface); - - context = eglCreateContext(dpy, config, NULL, NULL); - egl_error("eglCreateContext"); - fprintf(stderr,"context = %p\n", context); - - eglMakeCurrent(dpy, surface, surface, context); - egl_error("eglMakeCurrent"); - - eglQuerySurface(dpy, surface, EGL_WIDTH, &sWindowWidth); - eglQuerySurface(dpy, surface, EGL_HEIGHT, &sWindowHeight); - - sEglDisplay = dpy; - sEglSurface = surface; - sEglContext = context; - - return EGL_TRUE; -} - - -static void deinitGraphics() -{ - eglMakeCurrent(sEglDisplay, NULL, NULL, NULL); - eglDestroyContext(sEglDisplay, sEglContext); - eglDestroySurface(sEglDisplay, sEglSurface); - eglTerminate(sEglDisplay); -} - - -int main(int argc, char *argv[]) -{ - // not referenced: - argc = argc; - argv = argv; - - if (!initGraphics()) - { - fprintf(stderr, "Graphics initialization failed.\n"); - return EXIT_FAILURE; - } - - appInit(); - - while (gAppAlive) - { - struct timeval timeNow; - - if (gAppAlive) - { - gettimeofday(&timeNow, NULL); - appRender(timeNow.tv_sec * 1000 + timeNow.tv_usec / 1000, - sWindowWidth, sWindowHeight); - checkGLErrors(); - eglSwapBuffers(sEglDisplay, sEglSurface); - checkEGLErrors(); - } - } - - appDeinit(); - deinitGraphics(); - - return EXIT_SUCCESS; -} diff --git a/opengl/tests/angeles/app-linux.cpp b/opengl/tests/angeles/app-linux.cpp new file mode 100644 index 000000000000..06fa0c2bd711 --- /dev/null +++ b/opengl/tests/angeles/app-linux.cpp @@ -0,0 +1,213 @@ +/* San Angeles Observation OpenGL ES version example + * Copyright 2004-2005 Jetro Lauha + * All rights reserved. + * Web: http://iki.fi/jetro/ + * + * This source is free software; you can redistribute it and/or + * modify it under the terms of EITHER: + * (1) The GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at + * your option) any later version. The text of the GNU Lesser + * General Public License is included with this source in the + * file LICENSE-LGPL.txt. + * (2) The BSD-style license that is included with this source in + * the file LICENSE-BSD.txt. + * + * This source is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files + * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details. + * + * $Id: app-linux.c,v 1.4 2005/02/08 18:42:48 tonic Exp $ + * $Revision: 1.4 $ + * + * Parts of this source file is based on test/example code from + * GLESonGL implementation by David Blythe. Here is copy of the + * license notice from that source: + * + * Copyright (C) 2003 David Blythe 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 + * DAVID BLYTHE 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 + +#include +#include + +using namespace android; + +#include "app.h" + + +int gAppAlive = 1; + +static const char sAppName[] = + "San Angeles Observation OpenGL ES version example (Linux)"; + +static int sWindowWidth = WINDOW_DEFAULT_WIDTH; +static int sWindowHeight = WINDOW_DEFAULT_HEIGHT; +static EGLDisplay sEglDisplay = EGL_NO_DISPLAY; +static EGLContext sEglContext = EGL_NO_CONTEXT; +static EGLSurface sEglSurface = EGL_NO_SURFACE; + +const char *egl_strerror(unsigned err) +{ + switch(err){ + case EGL_SUCCESS: return "SUCCESS"; + case EGL_NOT_INITIALIZED: return "NOT INITIALIZED"; + case EGL_BAD_ACCESS: return "BAD ACCESS"; + case EGL_BAD_ALLOC: return "BAD ALLOC"; + case EGL_BAD_ATTRIBUTE: return "BAD_ATTRIBUTE"; + case EGL_BAD_CONFIG: return "BAD CONFIG"; + case EGL_BAD_CONTEXT: return "BAD CONTEXT"; + case EGL_BAD_CURRENT_SURFACE: return "BAD CURRENT SURFACE"; + case EGL_BAD_DISPLAY: return "BAD DISPLAY"; + case EGL_BAD_MATCH: return "BAD MATCH"; + case EGL_BAD_NATIVE_PIXMAP: return "BAD NATIVE PIXMAP"; + case EGL_BAD_NATIVE_WINDOW: return "BAD NATIVE WINDOW"; + case EGL_BAD_PARAMETER: return "BAD PARAMETER"; + case EGL_BAD_SURFACE: return "BAD_SURFACE"; +// case EGL_CONTEXT_LOST: return "CONTEXT LOST"; + default: return "UNKNOWN"; + } +} + +void egl_error(const char *name) +{ + unsigned err = eglGetError(); + if(err != EGL_SUCCESS) { + fprintf(stderr,"%s(): egl error 0x%x (%s)\n", + name, err, egl_strerror(err)); + } +} + +static void checkGLErrors() +{ + GLenum error = glGetError(); + if (error != GL_NO_ERROR) + fprintf(stderr, "GL Error: 0x%04x\n", (int)error); +} + + +static void checkEGLErrors() +{ + EGLint error = eglGetError(); + // GLESonGL seems to be returning 0 when there is no errors? + if (error && error != EGL_SUCCESS) + fprintf(stderr, "EGL Error: 0x%04x\n", (int)error); +} + +static int initGraphics() +{ + EGLint configAttribs[] = { + EGL_DEPTH_SIZE, 16, + EGL_NONE + }; + + EGLint majorVersion; + EGLint minorVersion; + EGLContext context; + EGLConfig config; + EGLSurface surface; + EGLint w, h; + EGLDisplay dpy; + + EGLNativeWindowType window = android_createDisplaySurface(); + + dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); + eglInitialize(dpy, &majorVersion, &minorVersion); + + status_t err = EGLUtils::selectConfigForNativeWindow( + dpy, configAttribs, window, &config); + if (err) { + fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n"); + return 0; + } + + surface = eglCreateWindowSurface(dpy, config, window, NULL); + egl_error("eglCreateWindowSurface"); + + fprintf(stderr,"surface = %p\n", surface); + + context = eglCreateContext(dpy, config, NULL, NULL); + egl_error("eglCreateContext"); + fprintf(stderr,"context = %p\n", context); + + eglMakeCurrent(dpy, surface, surface, context); + egl_error("eglMakeCurrent"); + + eglQuerySurface(dpy, surface, EGL_WIDTH, &sWindowWidth); + eglQuerySurface(dpy, surface, EGL_HEIGHT, &sWindowHeight); + + sEglDisplay = dpy; + sEglSurface = surface; + sEglContext = context; + + return EGL_TRUE; +} + + +static void deinitGraphics() +{ + eglMakeCurrent(sEglDisplay, NULL, NULL, NULL); + eglDestroyContext(sEglDisplay, sEglContext); + eglDestroySurface(sEglDisplay, sEglSurface); + eglTerminate(sEglDisplay); +} + + +int main(int argc, char *argv[]) +{ + // not referenced: + argc = argc; + argv = argv; + + if (!initGraphics()) + { + fprintf(stderr, "Graphics initialization failed.\n"); + return EXIT_FAILURE; + } + + appInit(); + + while (gAppAlive) + { + struct timeval timeNow; + + if (gAppAlive) + { + gettimeofday(&timeNow, NULL); + appRender(timeNow.tv_sec * 1000 + timeNow.tv_usec / 1000, + sWindowWidth, sWindowHeight); + checkGLErrors(); + eglSwapBuffers(sEglDisplay, sEglSurface); + checkEGLErrors(); + } + } + + appDeinit(); + deinitGraphics(); + + return EXIT_SUCCESS; +} diff --git a/opengl/tests/fillrate/Android.mk b/opengl/tests/fillrate/Android.mk new file mode 100644 index 000000000000..a7d30c25f0f0 --- /dev/null +++ b/opengl/tests/fillrate/Android.mk @@ -0,0 +1,17 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_SRC_FILES:= \ + fillrate.cpp + +LOCAL_SHARED_LIBRARIES := \ + libcutils \ + libEGL \ + libGLESv1_CM \ + libui + +LOCAL_MODULE:= test-opengl-fillrate + +LOCAL_MODULE_TAGS := optional + +include $(BUILD_EXECUTABLE) diff --git a/opengl/tests/fillrate/fillrate.cpp b/opengl/tests/fillrate/fillrate.cpp new file mode 100644 index 000000000000..911d35469b1e --- /dev/null +++ b/opengl/tests/fillrate/fillrate.cpp @@ -0,0 +1,161 @@ +/* +** +** Copyright 2006, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +#define LOG_TAG "fillrate" + +#include +#include + +#include +#include +#include + +#include +#include +#include + +using namespace android; + +int main(int argc, char** argv) +{ + EGLint configAttribs[] = { + EGL_DEPTH_SIZE, 0, + EGL_NONE + }; + + EGLint majorVersion; + EGLint minorVersion; + EGLContext context; + EGLConfig config; + EGLSurface surface; + EGLint w, h; + EGLDisplay dpy; + + EGLNativeWindowType window = android_createDisplaySurface(); + + dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); + eglInitialize(dpy, &majorVersion, &minorVersion); + + status_t err = EGLUtils::selectConfigForNativeWindow( + dpy, configAttribs, window, &config); + if (err) { + fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n"); + return 0; + } + + surface = eglCreateWindowSurface(dpy, config, window, NULL); + context = eglCreateContext(dpy, config, NULL, NULL); + eglMakeCurrent(dpy, surface, surface, context); + eglQuerySurface(dpy, surface, EGL_WIDTH, &w); + eglQuerySurface(dpy, surface, EGL_HEIGHT, &h); + + printf("w=%d, h=%d\n", w, h); + + glBindTexture(GL_TEXTURE_2D, 0); + glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glDisable(GL_DITHER); + glEnable(GL_BLEND); + glEnable(GL_TEXTURE_2D); + glColor4f(1,1,1,1); + + uint32_t* t32 = (uint32_t*)malloc(512*512*4); + for (int y=0 ; y<512 ; y++) { + for (int x=0 ; x<512 ; x++) { + int u = x-256; + int v = y-256; + if (u*u+v*v < 256*256) { + t32[x+y*512] = 0x10FFFFFF; + } else { + t32[x+y*512] = 0x20FF0000; + } + } + } + + const GLfloat vertices[4][2] = { + { 0, 0 }, + { 0, h }, + { w, h }, + { w, 0 } + }; + + const GLfloat texCoords[4][2] = { + { 0, 0 }, + { 0, 1 }, + { 1, 1 }, + { 1, 0 } + }; + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, t32); + + glViewport(0, 0, w, h); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrthof(0, w, 0, h, 0, 1); + + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glVertexPointer(2, GL_FLOAT, 0, vertices); + glTexCoordPointer(2, GL_FLOAT, 0, texCoords); + + eglSwapInterval(dpy, 1); + + glClearColor(1,0,0,0); + glClear(GL_COLOR_BUFFER_BIT); + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + eglSwapBuffers(dpy, surface); + + + nsecs_t times[32]; + + for (int c=1 ; c<32 ; c++) { + glClear(GL_COLOR_BUFFER_BIT); + for (int i=0 ; i=1 ; c--) { + int j=0; + for (int c=1 ; c<32 ; c++) { + glClear(GL_COLOR_BUFFER_BIT); + nsecs_t now = systemTime(); + for (int i=0 ; i -#include - -#include -#include -#include - -int main(int argc, char** argv) -{ - if (argc!=2 && argc!=3) { - printf("usage: %s <0-6> [pbuffer]\n", argv[0]); - return 0; - } - - const int test = atoi(argv[1]); - int usePbuffer = argc==3 && !strcmp(argv[2], "pbuffer"); - EGLint s_configAttribs[] = { - EGL_SURFACE_TYPE, EGL_PBUFFER_BIT|EGL_WINDOW_BIT, - EGL_RED_SIZE, 5, - EGL_GREEN_SIZE, 6, - EGL_BLUE_SIZE, 5, - EGL_NONE - }; - - EGLint numConfigs = -1; - EGLint majorVersion; - EGLint minorVersion; - EGLConfig config; - EGLContext context; - EGLSurface surface; - EGLint w, h; - - EGLDisplay dpy; - - dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); - eglInitialize(dpy, &majorVersion, &minorVersion); - eglChooseConfig(dpy, s_configAttribs, &config, 1, &numConfigs); - if (!usePbuffer) { - surface = eglCreateWindowSurface(dpy, config, - android_createDisplaySurface(), NULL); - } else { - printf("using pbuffer\n"); - EGLint attribs[] = { EGL_WIDTH, 320, EGL_HEIGHT, 480, EGL_NONE }; - surface = eglCreatePbufferSurface(dpy, config, attribs); - if (surface == EGL_NO_SURFACE) { - printf("eglCreatePbufferSurface error %x\n", eglGetError()); - } - } - context = eglCreateContext(dpy, config, NULL, NULL); - eglMakeCurrent(dpy, surface, surface, context); - eglQuerySurface(dpy, surface, EGL_WIDTH, &w); - eglQuerySurface(dpy, surface, EGL_HEIGHT, &h); - GLint dim = w +#include + +#include +#include +#include + +#include +#include + +using namespace android; + +#define USE_DRAW_TEXTURE 1 + +int main(int argc, char** argv) +{ + if (argc!=2 && argc!=3) { + printf("usage: %s <0-6> [pbuffer]\n", argv[0]); + return 0; + } + + const int test = atoi(argv[1]); + int usePbuffer = argc==3 && !strcmp(argv[2], "pbuffer"); + EGLint s_configAttribs[] = { + EGL_SURFACE_TYPE, EGL_PBUFFER_BIT|EGL_WINDOW_BIT, + EGL_RED_SIZE, 5, + EGL_GREEN_SIZE, 6, + EGL_BLUE_SIZE, 5, + EGL_NONE + }; + + EGLint numConfigs = -1; + EGLint majorVersion; + EGLint minorVersion; + EGLConfig config; + EGLContext context; + EGLSurface surface; + EGLint w, h; + + EGLDisplay dpy; + + EGLNativeWindowType window = 0; + if (!usePbuffer) { + window = android_createDisplaySurface(); + } + + dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); + eglInitialize(dpy, &majorVersion, &minorVersion); + if (!usePbuffer) { + EGLUtils::selectConfigForNativeWindow( + dpy, s_configAttribs, window, &config); + surface = eglCreateWindowSurface(dpy, config, window, NULL); + } else { + printf("using pbuffer\n"); + eglChooseConfig(dpy, s_configAttribs, &config, 1, &numConfigs); + EGLint attribs[] = { EGL_WIDTH, 320, EGL_HEIGHT, 480, EGL_NONE }; + surface = eglCreatePbufferSurface(dpy, config, attribs); + if (surface == EGL_NO_SURFACE) { + printf("eglCreatePbufferSurface error %x\n", eglGetError()); + } + } + context = eglCreateContext(dpy, config, NULL, NULL); + eglMakeCurrent(dpy, surface, surface, context); + eglQuerySurface(dpy, surface, EGL_WIDTH, &w); + eglQuerySurface(dpy, surface, EGL_HEIGHT, &h); + GLint dim = w -#include -#include -#include -#include - -#include -#include -#include - - -long long systemTime() -{ - struct timespec t; - t.tv_sec = t.tv_nsec = 0; - clock_gettime(CLOCK_MONOTONIC, &t); - return (long long)(t.tv_sec)*1000000000LL + t.tv_nsec; -} - -int main(int argc, char** argv) -{ - EGLint s_configAttribs[] = { - EGL_RED_SIZE, 5, - EGL_GREEN_SIZE, 6, - EGL_BLUE_SIZE, 5, - EGL_NONE - }; - - EGLint numConfigs = -1; - EGLint majorVersion; - EGLint minorVersion; - EGLConfig config; - EGLContext context; - EGLSurface surface; - EGLint w, h; - - EGLDisplay dpy; - - dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); - eglInitialize(dpy, &majorVersion, &minorVersion); - eglChooseConfig(dpy, s_configAttribs, &config, 1, &numConfigs); - surface = eglCreateWindowSurface(dpy, config, - android_createDisplaySurface(), NULL); - context = eglCreateContext(dpy, config, NULL, NULL); - eglMakeCurrent(dpy, surface, surface, context); - eglQuerySurface(dpy, surface, EGL_WIDTH, &w); - eglQuerySurface(dpy, surface, EGL_HEIGHT, &h); - GLint dim = w +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include +#include + +using namespace android; + +int main(int argc, char** argv) +{ + EGLint configAttribs[] = { + EGL_DEPTH_SIZE, 0, + EGL_NONE + }; + + EGLint majorVersion; + EGLint minorVersion; + EGLContext context; + EGLConfig config; + EGLSurface surface; + EGLint w, h; + EGLDisplay dpy; + + EGLNativeWindowType window = android_createDisplaySurface(); + + dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); + eglInitialize(dpy, &majorVersion, &minorVersion); + + status_t err = EGLUtils::selectConfigForNativeWindow( + dpy, configAttribs, window, &config); + if (err) { + fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n"); + return 0; + } + + surface = eglCreateWindowSurface(dpy, config, window, NULL); + context = eglCreateContext(dpy, config, NULL, NULL); + eglMakeCurrent(dpy, surface, surface, context); + eglQuerySurface(dpy, surface, EGL_WIDTH, &w); + eglQuerySurface(dpy, surface, EGL_HEIGHT, &h); + GLint dim = w +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include +#include + +using namespace android; + +static void printGLString(const char *name, GLenum s) { + // fprintf(stderr, "printGLString %s, %d\n", name, s); + const char *v = (const char *) glGetString(s); + // int error = glGetError(); + // fprintf(stderr, "glGetError() = %d, result of glGetString = %x\n", error, + // (unsigned int) v); + // if ((v < (const char*) 0) || (v > (const char*) 0x10000)) + // fprintf(stderr, "GL %s = %s\n", name, v); + // else + // fprintf(stderr, "GL %s = (null) 0x%08x\n", name, (unsigned int) v); + fprintf(stderr, "GL %s = %s\n", name, v); +} + +static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) { + if (returnVal != EGL_TRUE) { + fprintf(stderr, "%s() returned %d\n", op, returnVal); + } + + for (EGLint error = eglGetError(); error != EGL_SUCCESS; error + = eglGetError()) { + fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error), + error); + } +} + +static void checkGlError(const char* op) { + for (GLint error = glGetError(); error; error + = glGetError()) { + fprintf(stderr, "after %s() glError (0x%x)\n", op, error); + } +} + +static const char gVertexShader[] = "attribute vec4 vPosition;\n" + "void main() {\n" + " gl_Position = vPosition;\n" + "}\n"; + +static const char gFragmentShader[] = "precision mediump float;\n" + "void main() {\n" + " gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n" + "}\n"; + +GLuint loadShader(GLenum shaderType, const char* pSource) { + GLuint shader = glCreateShader(shaderType); + if (shader) { + glShaderSource(shader, 1, &pSource, NULL); + glCompileShader(shader); + GLint compiled = 0; + glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); + if (!compiled) { + GLint infoLen = 0; + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen); + if (infoLen) { + char* buf = (char*) malloc(infoLen); + if (buf) { + glGetShaderInfoLog(shader, infoLen, NULL, buf); + fprintf(stderr, "Could not compile shader %d:\n%s\n", + shaderType, buf); + free(buf); + } + glDeleteShader(shader); + shader = 0; + } + } + } + return shader; +} + +GLuint createProgram(const char* pVertexSource, const char* pFragmentSource) { + GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource); + if (!vertexShader) { + return 0; + } + + GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource); + if (!pixelShader) { + return 0; + } + + GLuint program = glCreateProgram(); + if (program) { + glAttachShader(program, vertexShader); + checkGlError("glAttachShader"); + glAttachShader(program, pixelShader); + checkGlError("glAttachShader"); + glLinkProgram(program); + GLint linkStatus = GL_FALSE; + glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); + if (linkStatus != GL_TRUE) { + GLint bufLength = 0; + glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength); + if (bufLength) { + char* buf = (char*) malloc(bufLength); + if (buf) { + glGetProgramInfoLog(program, bufLength, NULL, buf); + fprintf(stderr, "Could not link program:\n%s\n", buf); + free(buf); + } + } + glDeleteProgram(program); + program = 0; + } + } + return program; +} + +GLuint gProgram; +GLuint gvPositionHandle; + +bool setupGraphics(int w, int h) { + gProgram = createProgram(gVertexShader, gFragmentShader); + if (!gProgram) { + return false; + } + gvPositionHandle = glGetAttribLocation(gProgram, "vPosition"); + checkGlError("glGetAttribLocation"); + fprintf(stderr, "glGetAttribLocation(\"vPosition\") = %d\n", + gvPositionHandle); + + glViewport(0, 0, w, h); + checkGlError("glViewport"); + return true; +} + +const GLfloat gTriangleVertices[] = { 0.0f, 0.5f, -0.5f, -0.5f, + 0.5f, -0.5f }; + +void renderFrame() { + glClearColor(0.0f, 0.0f, 1.0f, 1.0f); + checkGlError("glClearColor"); + glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); + checkGlError("glClear"); + + glUseProgram(gProgram); + checkGlError("glUseProgram"); + + glVertexAttribPointer(gvPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, gTriangleVertices); + checkGlError("glVertexAttribPointer"); + glEnableVertexAttribArray(gvPositionHandle); + checkGlError("glEnableVertexAttribArray"); + glDrawArrays(GL_TRIANGLES, 0, 3); + checkGlError("glDrawArrays"); +} + +void printEGLConfiguration(EGLDisplay dpy, EGLConfig config) { + +#define X(VAL) {VAL, #VAL} + struct {EGLint attribute; const char* name;} names[] = { + X(EGL_BUFFER_SIZE), + X(EGL_ALPHA_SIZE), + X(EGL_BLUE_SIZE), + X(EGL_GREEN_SIZE), + X(EGL_RED_SIZE), + X(EGL_DEPTH_SIZE), + X(EGL_STENCIL_SIZE), + X(EGL_CONFIG_CAVEAT), + X(EGL_CONFIG_ID), + X(EGL_LEVEL), + X(EGL_MAX_PBUFFER_HEIGHT), + X(EGL_MAX_PBUFFER_PIXELS), + X(EGL_MAX_PBUFFER_WIDTH), + X(EGL_NATIVE_RENDERABLE), + X(EGL_NATIVE_VISUAL_ID), + X(EGL_NATIVE_VISUAL_TYPE), + X(EGL_PRESERVED_RESOURCES), + X(EGL_SAMPLES), + X(EGL_SAMPLE_BUFFERS), + X(EGL_SURFACE_TYPE), + X(EGL_TRANSPARENT_TYPE), + X(EGL_TRANSPARENT_RED_VALUE), + X(EGL_TRANSPARENT_GREEN_VALUE), + X(EGL_TRANSPARENT_BLUE_VALUE), + X(EGL_BIND_TO_TEXTURE_RGB), + X(EGL_BIND_TO_TEXTURE_RGBA), + X(EGL_MIN_SWAP_INTERVAL), + X(EGL_MAX_SWAP_INTERVAL), + X(EGL_LUMINANCE_SIZE), + X(EGL_ALPHA_MASK_SIZE), + X(EGL_COLOR_BUFFER_TYPE), + X(EGL_RENDERABLE_TYPE), + X(EGL_CONFORMANT), + }; +#undef X + + for (size_t j = 0; j < sizeof(names) / sizeof(names[0]); j++) { + EGLint value = -1; + EGLint returnVal = eglGetConfigAttrib(dpy, config, names[j].attribute, &value); + EGLint error = eglGetError(); + if (returnVal && error == EGL_SUCCESS) { + printf(" %s: ", names[j].name); + printf("%d (0x%x)", value, value); + } + } + printf("\n"); +} + +int printEGLConfigurations(EGLDisplay dpy) { + EGLint numConfig = 0; + EGLint returnVal = eglGetConfigs(dpy, NULL, 0, &numConfig); + checkEglError("eglGetConfigs", returnVal); + if (!returnVal) { + return false; + } + + printf("Number of EGL configuration: %d\n", numConfig); + + EGLConfig* configs = (EGLConfig*) malloc(sizeof(EGLConfig) * numConfig); + if (! configs) { + printf("Could not allocate configs.\n"); + return false; + } + + returnVal = eglGetConfigs(dpy, configs, numConfig, &numConfig); + checkEglError("eglGetConfigs", returnVal); + if (!returnVal) { + free(configs); + return false; + } + + for(int i = 0; i < numConfig; i++) { + printf("Configuration %d\n", i); + printEGLConfiguration(dpy, configs[i]); + } + + free(configs); + return true; +} + +int main(int argc, char** argv) { + EGLBoolean returnValue; + EGLConfig myConfig = {0}; + + EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; + EGLint s_configAttribs[] = { + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, + EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, + EGL_NONE }; + EGLint majorVersion; + EGLint minorVersion; + EGLContext context; + EGLSurface surface; + EGLint w, h; + + EGLDisplay dpy; + + checkEglError(""); + dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); + checkEglError("eglGetDisplay"); + if (dpy == EGL_NO_DISPLAY) { + printf("eglGetDisplay returned EGL_NO_DISPLAY.\n"); + return 0; + } + + returnValue = eglInitialize(dpy, &majorVersion, &minorVersion); + checkEglError("eglInitialize", returnValue); + fprintf(stderr, "EGL version %d.%d\n", majorVersion, minorVersion); + if (returnValue != EGL_TRUE) { + printf("eglInitialize failed\n"); + return 0; + } + + if (!printEGLConfigurations(dpy)) { + printf("printEGLConfigurations failed\n"); + return 0; + } + + checkEglError("printEGLConfigurations"); + + EGLNativeWindowType window = android_createDisplaySurface(); + returnValue = EGLUtils::selectConfigForNativeWindow(dpy, s_configAttribs, window, &myConfig); + if (returnValue) { + printf("EGLUtils::selectConfigForNativeWindow() returned %d", returnValue); + return 0; + } + + checkEglError("EGLUtils::selectConfigForNativeWindow"); + + printf("Chose this configuration:\n"); + printEGLConfiguration(dpy, myConfig); + + surface = eglCreateWindowSurface(dpy, myConfig, window, NULL); + checkEglError("eglCreateWindowSurface"); + if (surface == EGL_NO_SURFACE) { + printf("gelCreateWindowSurface failed.\n"); + return 0; + } + + context = eglCreateContext(dpy, myConfig, EGL_NO_CONTEXT, context_attribs); + checkEglError("eglCreateContext"); + if (context == EGL_NO_CONTEXT) { + printf("eglCreateContext failed\n"); + return 0; + } + returnValue = eglMakeCurrent(dpy, surface, surface, context); + checkEglError("eglMakeCurrent", returnValue); + if (returnValue != EGL_TRUE) { + return 0; + } + eglQuerySurface(dpy, surface, EGL_WIDTH, &w); + checkEglError("eglQuerySurface"); + eglQuerySurface(dpy, surface, EGL_HEIGHT, &h); + checkEglError("eglQuerySurface"); + GLint dim = w < h ? w : h; + + fprintf(stderr, "Window dimensions: %d x %d\n", w, h); + + printGLString("Version", GL_VERSION); + printGLString("Vendor", GL_VENDOR); + printGLString("Renderer", GL_RENDERER); + printGLString("Extensions", GL_EXTENSIONS); + + if(!setupGraphics(w, h)) { + fprintf(stderr, "Could not set up graphics.\n"); + return 0; + } + + for (;;) { + renderFrame(); + eglSwapBuffers(dpy, surface); + checkEglError("eglSwapBuffers"); + } + + return 0; +} diff --git a/opengl/tests/gl2_jni/Android.mk b/opengl/tests/gl2_jni/Android.mk new file mode 100644 index 000000000000..81247dfcfbfe --- /dev/null +++ b/opengl/tests/gl2_jni/Android.mk @@ -0,0 +1,51 @@ +######################################################################### +# OpenGL ES JNI sample +# This makefile builds both an activity and a shared library. +######################################################################### +ifneq ($(TARGET_SIMULATOR),true) # not 64 bit clean + +TOP_LOCAL_PATH:= $(call my-dir) + +# Build activity + +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE_TAGS := user + +LOCAL_SRC_FILES := $(call all-subdir-java-files) + +LOCAL_PACKAGE_NAME := GL2JNI + +LOCAL_JNI_SHARED_LIBRARIES := libgl2jni + +include $(BUILD_PACKAGE) + +######################################################################### +# Build JNI Shared Library +######################################################################### + +LOCAL_PATH:= $(LOCAL_PATH)/jni + +include $(CLEAR_VARS) + +# Optional tag would mean it doesn't get installed by default +LOCAL_MODULE_TAGS := optional + +LOCAL_CFLAGS := -Werror + +LOCAL_SRC_FILES:= \ + gl_code.cpp + +LOCAL_SHARED_LIBRARIES := \ + libutils \ + libEGL \ + libGLESv2 + +LOCAL_MODULE := libgl2jni + +LOCAL_PRELINK_MODULE := false + +include $(BUILD_SHARED_LIBRARY) + +endif # TARGET_SIMULATOR diff --git a/opengl/tests/gl2_jni/AndroidManifest.xml b/opengl/tests/gl2_jni/AndroidManifest.xml new file mode 100644 index 000000000000..a72a6a54a35c --- /dev/null +++ b/opengl/tests/gl2_jni/AndroidManifest.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + diff --git a/opengl/tests/gl2_jni/jni/gl_code.cpp b/opengl/tests/gl2_jni/jni/gl_code.cpp new file mode 100644 index 000000000000..c2fabe6c302f --- /dev/null +++ b/opengl/tests/gl2_jni/jni/gl_code.cpp @@ -0,0 +1,165 @@ +// OpenGL ES 2.0 code + +#include +#define LOG_TAG "GL2JNI gl_code.cpp" +#include + +#include +#include +#include + +#include +#include +#include + +static void printGLString(const char *name, GLenum s) { + const char *v = (const char *) glGetString(s); + LOGI("GL %s = %s\n", name, v); +} + +static void checkGlError(const char* op) { + for (GLint error = glGetError(); error; error + = glGetError()) { + LOGI("after %s() glError (0x%x)\n", op, error); + } +} + +static const char gVertexShader[] = "attribute vec4 vPosition;\n" + "void main() {\n" + " gl_Position = vPosition;\n" + "}\n"; + +static const char gFragmentShader[] = "precision mediump float;\n" + "void main() {\n" + " gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n" + "}\n"; + +GLuint loadShader(GLenum shaderType, const char* pSource) { + GLuint shader = glCreateShader(shaderType); + if (shader) { + glShaderSource(shader, 1, &pSource, NULL); + glCompileShader(shader); + GLint compiled = 0; + glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); + if (!compiled) { + GLint infoLen = 0; + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen); + if (infoLen) { + char* buf = (char*) malloc(infoLen); + if (buf) { + glGetShaderInfoLog(shader, infoLen, NULL, buf); + LOGE("Could not compile shader %d:\n%s\n", + shaderType, buf); + free(buf); + } + glDeleteShader(shader); + shader = 0; + } + } + } + return shader; +} + +GLuint createProgram(const char* pVertexSource, const char* pFragmentSource) { + GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource); + if (!vertexShader) { + return 0; + } + + GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource); + if (!pixelShader) { + return 0; + } + + GLuint program = glCreateProgram(); + if (program) { + glAttachShader(program, vertexShader); + checkGlError("glAttachShader"); + glAttachShader(program, pixelShader); + checkGlError("glAttachShader"); + glLinkProgram(program); + GLint linkStatus = GL_FALSE; + glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); + if (linkStatus != GL_TRUE) { + GLint bufLength = 0; + glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength); + if (bufLength) { + char* buf = (char*) malloc(bufLength); + if (buf) { + glGetProgramInfoLog(program, bufLength, NULL, buf); + LOGE("Could not link program:\n%s\n", buf); + free(buf); + } + } + glDeleteProgram(program); + program = 0; + } + } + return program; +} + +GLuint gProgram; +GLuint gvPositionHandle; + +bool setupGraphics(int w, int h) { + printGLString("Version", GL_VERSION); + printGLString("Vendor", GL_VENDOR); + printGLString("Renderer", GL_RENDERER); + printGLString("Extensions", GL_EXTENSIONS); + + LOGI("setupGraphics(%d, %d)", w, h); + gProgram = createProgram(gVertexShader, gFragmentShader); + if (!gProgram) { + LOGE("Could not create program."); + return false; + } + gvPositionHandle = glGetAttribLocation(gProgram, "vPosition"); + checkGlError("glGetAttribLocation"); + LOGI("glGetAttribLocation(\"vPosition\") = %d\n", + gvPositionHandle); + + glViewport(0, 0, w, h); + checkGlError("glViewport"); + return true; +} + +const GLfloat gTriangleVertices[] = { 0.0f, 0.5f, -0.5f, -0.5f, + 0.5f, -0.5f }; + +void renderFrame() { + static float grey; + grey += 0.01f; + if (grey > 1.0f) { + grey = 0.0f; + } + glClearColor(grey, grey, grey, 1.0f); + checkGlError("glClearColor"); + glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); + checkGlError("glClear"); + + glUseProgram(gProgram); + checkGlError("glUseProgram"); + + glVertexAttribPointer(gvPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, gTriangleVertices); + checkGlError("glVertexAttribPointer"); + glEnableVertexAttribArray(gvPositionHandle); + checkGlError("glEnableVertexAttribArray"); + glDrawArrays(GL_TRIANGLES, 0, 3); + checkGlError("glDrawArrays"); +} + +extern "C" { + JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_init(JNIEnv * env, jobject obj, jint width, jint height); + JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_step(JNIEnv * env, jobject obj); +}; + +JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_init(JNIEnv * env, jobject obj, jint width, jint height) +{ + setupGraphics(width, height); +} + +JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_step(JNIEnv * env, jobject obj) +{ + renderFrame(); +} + diff --git a/opengl/tests/gl2_jni/res/values/strings.xml b/opengl/tests/gl2_jni/res/values/strings.xml new file mode 100644 index 000000000000..e3f7331888ba --- /dev/null +++ b/opengl/tests/gl2_jni/res/values/strings.xml @@ -0,0 +1,29 @@ + + + + + + + + GL2JNI + + + diff --git a/opengl/tests/gl2_jni/src/com/android/gl2jni/GL2JNIActivity.java b/opengl/tests/gl2_jni/src/com/android/gl2jni/GL2JNIActivity.java new file mode 100644 index 000000000000..c366a2cbc3c2 --- /dev/null +++ b/opengl/tests/gl2_jni/src/com/android/gl2jni/GL2JNIActivity.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gl2jni; + +import android.app.Activity; +import android.os.Bundle; +import android.util.Log; +import android.view.WindowManager; + +import java.io.File; + + +public class GL2JNIActivity extends Activity { + + GL2JNIView mView; + + @Override protected void onCreate(Bundle icicle) { + super.onCreate(icicle); + mView = new GL2JNIView(getApplication()); + setContentView(mView); + } + + @Override protected void onPause() { + super.onPause(); + mView.onPause(); + } + + @Override protected void onResume() { + super.onResume(); + mView.onResume(); + } +} diff --git a/opengl/tests/gl2_jni/src/com/android/gl2jni/GL2JNILib.java b/opengl/tests/gl2_jni/src/com/android/gl2jni/GL2JNILib.java new file mode 100644 index 000000000000..040a98480432 --- /dev/null +++ b/opengl/tests/gl2_jni/src/com/android/gl2jni/GL2JNILib.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gl2jni; + +// Wrapper for native library + +public class GL2JNILib { + + static { + System.loadLibrary("gl2jni"); + } + + /** + * @param width the current view width + * @param height the current view height + */ + public static native void init(int width, int height); + public static native void step(); +} diff --git a/opengl/tests/gl2_jni/src/com/android/gl2jni/GL2JNIView.java b/opengl/tests/gl2_jni/src/com/android/gl2jni/GL2JNIView.java new file mode 100644 index 000000000000..2dae090950cf --- /dev/null +++ b/opengl/tests/gl2_jni/src/com/android/gl2jni/GL2JNIView.java @@ -0,0 +1,139 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gl2jni; +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +import android.content.Context; +import android.opengl.GLSurfaceView; +import android.util.AttributeSet; +import android.util.Log; +import android.view.KeyEvent; +import android.view.MotionEvent; + +import javax.microedition.khronos.egl.EGL10; +import javax.microedition.khronos.egl.EGLConfig; +import javax.microedition.khronos.egl.EGLContext; +import javax.microedition.khronos.egl.EGLDisplay; +import javax.microedition.khronos.opengles.GL10; + +/** + * An implementation of SurfaceView that uses the dedicated surface for + * displaying an OpenGL animation. This allows the animation to run in a + * separate thread, without requiring that it be driven by the update mechanism + * of the view hierarchy. + * + * The application-specific rendering code is delegated to a GLView.Renderer + * instance. + */ +class GL2JNIView extends GLSurfaceView { + private static String TAG = "GL2JNIView"; + GL2JNIView(Context context) { + super(context); + init(); + } + + public GL2JNIView(Context context, AttributeSet attrs) { + super(context, attrs); + init(); + } + + private void init() { + setEGLContextFactory(new ContextFactory()); + setEGLConfigChooser(new ConfigChooser()); + setRenderer(new Renderer()); + } + + private static class ContextFactory implements GLSurfaceView.EGLContextFactory { + private static int EGL_CONTEXT_CLIENT_VERSION = 0x3098; + public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig) { + Log.w(TAG, "creating OpenGL ES 2.0 context"); + checkEglError("Before eglCreateContext", egl); + int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE }; + EGLContext context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list); + checkEglError("After eglCreateContext", egl); + return context; + } + + public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context) { + egl.eglDestroyContext(display, context); + } + } + + private static void checkEglError(String prompt, EGL10 egl) { + int error; + while ((error = egl.eglGetError()) != EGL10.EGL_SUCCESS) { + Log.e(TAG, String.format("%s: EGL error: 0x%x", prompt, error)); + } + } + + private static class ConfigChooser implements GLSurfaceView.EGLConfigChooser { + private static int EGL_OPENGL_ES2_BIT = 4; + private static int[] s_configAttribs2 = + { + EGL10.EGL_RED_SIZE, 4, + EGL10.EGL_GREEN_SIZE, 4, + EGL10.EGL_BLUE_SIZE, 4, + EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, + EGL10.EGL_NONE + }; + public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) { + + int[] num_config = new int[1]; + egl.eglChooseConfig(display, s_configAttribs2, null, 0, num_config); + + int numConfigs = num_config[0]; + + Log.w(TAG, String.format("Found %d configurations", numConfigs)); + if (numConfigs <= 0) { + throw new IllegalArgumentException("No configs match configSpec"); + } + EGLConfig[] configs = new EGLConfig[numConfigs]; + egl.eglChooseConfig(display, s_configAttribs2, configs, numConfigs, num_config); + return configs[0]; + } + } + + private static class Renderer implements GLSurfaceView.Renderer { + public void onDrawFrame(GL10 gl) { + GL2JNILib.step(); + } + + public void onSurfaceChanged(GL10 gl, int width, int height) { + GL2JNILib.init(width, height); + } + + public void onSurfaceCreated(GL10 gl, EGLConfig config) { + // Do nothing. + } + } +} + diff --git a/opengl/tests/gl_basic/Android.mk b/opengl/tests/gl_basic/Android.mk new file mode 100644 index 000000000000..6b6341fc3581 --- /dev/null +++ b/opengl/tests/gl_basic/Android.mk @@ -0,0 +1,17 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_SRC_FILES:= \ + gl_basic.cpp + +LOCAL_SHARED_LIBRARIES := \ + libcutils \ + libEGL \ + libGLESv1_CM \ + libui + +LOCAL_MODULE:= test-opengl-gl_basic + +LOCAL_MODULE_TAGS := optional + +include $(BUILD_EXECUTABLE) diff --git a/opengl/tests/gl_basic/gl_basic.cpp b/opengl/tests/gl_basic/gl_basic.cpp new file mode 100644 index 000000000000..7dc2378edcd6 --- /dev/null +++ b/opengl/tests/gl_basic/gl_basic.cpp @@ -0,0 +1,357 @@ +// Simple OpenGL ES 1.x application showing how to initialize and draw something. + +#include +#include +#include + +#include +#include + +#include +#include +#include + +using namespace android; + +EGLDisplay eglDisplay; +EGLSurface eglSurface; +EGLContext eglContext; +GLuint texture; + +#define FIXED_ONE 0x10000 +#define ITERATIONS 50 + +int init_gl_surface(void); +void free_gl_surface(void); +void init_scene(void); +void render(); +void create_texture(void); +int readTimer(void); + +static void gluLookAt(float eyeX, float eyeY, float eyeZ, + float centerX, float centerY, float centerZ, float upX, float upY, + float upZ) +{ + // See the OpenGL GLUT documentation for gluLookAt for a description + // of the algorithm. We implement it in a straightforward way: + + float fx = centerX - eyeX; + float fy = centerY - eyeY; + float fz = centerZ - eyeZ; + + // Normalize f + float rlf = 1.0f / sqrtf(fx*fx + fy*fy + fz*fz); + fx *= rlf; + fy *= rlf; + fz *= rlf; + + // Normalize up + float rlup = 1.0f / sqrtf(upX*upX + upY*upY + upZ*upZ); + upX *= rlup; + upY *= rlup; + upZ *= rlup; + + // compute s = f x up (x means "cross product") + + float sx = fy * upZ - fz * upY; + float sy = fz * upX - fx * upZ; + float sz = fx * upY - fy * upX; + + // compute u = s x f + float ux = sy * fz - sz * fy; + float uy = sz * fx - sx * fz; + float uz = sx * fy - sy * fx; + + float m[16] ; + m[0] = sx; + m[1] = ux; + m[2] = -fx; + m[3] = 0.0f; + + m[4] = sy; + m[5] = uy; + m[6] = -fy; + m[7] = 0.0f; + + m[8] = sz; + m[9] = uz; + m[10] = -fz; + m[11] = 0.0f; + + m[12] = 0.0f; + m[13] = 0.0f; + m[14] = 0.0f; + m[15] = 1.0f; + + glMultMatrixf(m); + glTranslatef(-eyeX, -eyeY, -eyeZ); +} + + +void printEGLConfiguration(EGLDisplay dpy, EGLConfig config) { + +#define X(VAL) {VAL, #VAL} + struct {EGLint attribute; const char* name;} names[] = { + X(EGL_BUFFER_SIZE), + X(EGL_ALPHA_SIZE), + X(EGL_BLUE_SIZE), + X(EGL_GREEN_SIZE), + X(EGL_RED_SIZE), + X(EGL_DEPTH_SIZE), + X(EGL_STENCIL_SIZE), + X(EGL_CONFIG_CAVEAT), + X(EGL_CONFIG_ID), + X(EGL_LEVEL), + X(EGL_MAX_PBUFFER_HEIGHT), + X(EGL_MAX_PBUFFER_PIXELS), + X(EGL_MAX_PBUFFER_WIDTH), + X(EGL_NATIVE_RENDERABLE), + X(EGL_NATIVE_VISUAL_ID), + X(EGL_NATIVE_VISUAL_TYPE), + X(EGL_PRESERVED_RESOURCES), + X(EGL_SAMPLES), + X(EGL_SAMPLE_BUFFERS), + X(EGL_SURFACE_TYPE), + X(EGL_TRANSPARENT_TYPE), + X(EGL_TRANSPARENT_RED_VALUE), + X(EGL_TRANSPARENT_GREEN_VALUE), + X(EGL_TRANSPARENT_BLUE_VALUE), + X(EGL_BIND_TO_TEXTURE_RGB), + X(EGL_BIND_TO_TEXTURE_RGBA), + X(EGL_MIN_SWAP_INTERVAL), + X(EGL_MAX_SWAP_INTERVAL), + X(EGL_LUMINANCE_SIZE), + X(EGL_ALPHA_MASK_SIZE), + X(EGL_COLOR_BUFFER_TYPE), + X(EGL_RENDERABLE_TYPE), + X(EGL_CONFORMANT), + }; +#undef X + + for (size_t j = 0; j < sizeof(names) / sizeof(names[0]); j++) { + EGLint value = -1; + EGLint returnVal = eglGetConfigAttrib(dpy, config, names[j].attribute, &value); + EGLint error = eglGetError(); + if (returnVal && error == EGL_SUCCESS) { + printf(" %s: ", names[j].name); + printf("%d (0x%x)", value, value); + } + } + printf("\n"); +} + +static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) { + if (returnVal != EGL_TRUE) { + fprintf(stderr, "%s() returned %d\n", op, returnVal); + } + + for (EGLint error = eglGetError(); error != EGL_SUCCESS; error + = eglGetError()) { + fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error), + error); + } +} + +int printEGLConfigurations(EGLDisplay dpy) { + EGLint numConfig = 0; + EGLint returnVal = eglGetConfigs(dpy, NULL, 0, &numConfig); + checkEglError("eglGetConfigs", returnVal); + if (!returnVal) { + return false; + } + + printf("Number of EGL configurations: %d\n", numConfig); + + EGLConfig* configs = (EGLConfig*) malloc(sizeof(EGLConfig) * numConfig); + if (! configs) { + printf("Could not allocate configs.\n"); + return false; + } + + returnVal = eglGetConfigs(dpy, configs, numConfig, &numConfig); + checkEglError("eglGetConfigs", returnVal); + if (!returnVal) { + free(configs); + return false; + } + + for(int i = 0; i < numConfig; i++) { + printf("Configuration %d\n", i); + printEGLConfiguration(dpy, configs[i]); + } + + free(configs); + return true; +} + +int main(int argc, char **argv) +{ + int q; + int start, end; + + printf("Initializing EGL...\n"); + + if(!init_gl_surface()) + { + printf("GL initialisation failed - exiting\n"); + return 0; + } + + init_scene(); + + create_texture(); + + printf("Running...\n"); + + while(true) { + render(); + } + + free_gl_surface(); + + return 0; +} + +int init_gl_surface(void) +{ + EGLint numConfigs = 1; + EGLConfig myConfig = {0}; + EGLint attrib[] = + { + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, + EGL_NONE + }; + + if ( (eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY ) + { + printf("eglGetDisplay failed\n"); + return 0; + } + + if ( eglInitialize(eglDisplay, NULL, NULL) != EGL_TRUE ) + { + printf("eglInitialize failed\n"); + return 0; + } + + if (! printEGLConfigurations(eglDisplay)) { + printf("printEGLConfigurations failed.\n"); + return 0; + } + EGLNativeWindowType window = android_createDisplaySurface(); + EGLUtils::selectConfigForNativeWindow(eglDisplay, attrib, window, &myConfig); + + if ( (eglSurface = eglCreateWindowSurface(eglDisplay, myConfig, + window, 0)) == EGL_NO_SURFACE ) + { + printf("eglCreateWindowSurface failed\n"); + return 0; + } + + if ( (eglContext = eglCreateContext(eglDisplay, myConfig, 0, 0)) == EGL_NO_CONTEXT ) + { + printf("eglCreateContext failed\n"); + return 0; + } + + if ( eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext) != EGL_TRUE ) + { + printf("eglMakeCurrent failed\n"); + return 0; + } + + return 1; +} + +void free_gl_surface(void) +{ + if (eglDisplay != EGL_NO_DISPLAY) + { + eglMakeCurrent( EGL_NO_DISPLAY, EGL_NO_SURFACE, + EGL_NO_SURFACE, EGL_NO_CONTEXT ); + eglDestroyContext( eglDisplay, eglContext ); + eglDestroySurface( eglDisplay, eglSurface ); + eglTerminate( eglDisplay ); + eglDisplay = EGL_NO_DISPLAY; + } +} + +void init_scene(void) +{ + glDisable(GL_DITHER); + glEnable(GL_CULL_FACE); + + float ratio = 320.0f / 480.0f; + glViewport(0, 0, 320, 480); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustumf(-ratio, ratio, -1, 1, 1, 10); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + gluLookAt( + 0, 0, 3, // eye + 0, 0, 0, // center + 0, 1, 0); // up + + glEnable(GL_TEXTURE_2D); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); +} + +void create_texture(void) +{ + const unsigned int on = 0xff0000ff; + const unsigned int off = 0xffffffff; + const unsigned int pixels[] = + { + on, off, on, off, on, off, on, off, + off, on, off, on, off, on, off, on, + on, off, on, off, on, off, on, off, + off, on, off, on, off, on, off, on, + on, off, on, off, on, off, on, off, + off, on, off, on, off, on, off, on, + on, off, on, off, on, off, on, off, + off, on, off, on, off, on, off, on, + }; + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); +} + +void render() +{ + int i, j; + int quads = 1; + + const GLfloat vertices[] = { + -1, -1, 0, + 1, -1, 0, + 1, 1, 0, + -1, 1, 0 + }; + + const GLfixed texCoords[] = { + 0, 0, + FIXED_ONE, 0, + FIXED_ONE, FIXED_ONE, + 0, FIXED_ONE + }; + + const GLushort indices[] = { 0, 1, 2, 0, 2, 3 }; + + glVertexPointer(3, GL_FLOAT, 0, vertices); + glTexCoordPointer(2, GL_FIXED, 0, texCoords); + + glClearColor(1.0, 1.0, 1.0, 1.0); + + int nelem = sizeof(indices)/sizeof(indices[0]); + glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); + glDrawElements(GL_TRIANGLES, nelem, GL_UNSIGNED_SHORT, indices); + eglSwapBuffers(eglDisplay, eglSurface); +} + diff --git a/opengl/tests/gl_jni/Android.mk b/opengl/tests/gl_jni/Android.mk new file mode 100644 index 000000000000..4029fa126106 --- /dev/null +++ b/opengl/tests/gl_jni/Android.mk @@ -0,0 +1,53 @@ +######################################################################### +# OpenGL ES JNI sample +# This makefile builds both an activity and a shared library. +######################################################################### +ifneq ($(TARGET_SIMULATOR),true) # not 64 bit clean + +TOP_LOCAL_PATH:= $(call my-dir) + +# Build activity + +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE_TAGS := user + +LOCAL_SRC_FILES := $(call all-subdir-java-files) + +LOCAL_PACKAGE_NAME := GLJNI + +LOCAL_JNI_SHARED_LIBRARIES := libgljni + +include $(BUILD_PACKAGE) + +######################################################################### +# Build JNI Shared Library +######################################################################### + +LOCAL_PATH:= $(LOCAL_PATH)/jni + +include $(CLEAR_VARS) + +# Optional tag would mean it doesn't get installed by default +LOCAL_MODULE_TAGS := optional + +LOCAL_CFLAGS := -Werror + +LOCAL_SRC_FILES:= \ + gl_code.cpp + +LOCAL_SHARED_LIBRARIES := \ + libutils \ + libEGL \ + libGLESv1_CM + +LOCAL_MODULE := libgljni + +LOCAL_ARM_MODE := arm + +LOCAL_PRELINK_MODULE := false + +include $(BUILD_SHARED_LIBRARY) + +endif # TARGET_SIMULATOR diff --git a/opengl/tests/gl_jni/AndroidManifest.xml b/opengl/tests/gl_jni/AndroidManifest.xml new file mode 100644 index 000000000000..64bd6bf96c78 --- /dev/null +++ b/opengl/tests/gl_jni/AndroidManifest.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + diff --git a/opengl/tests/gl_jni/jni/gl_code.cpp b/opengl/tests/gl_jni/jni/gl_code.cpp new file mode 100644 index 000000000000..33b25ab6c775 --- /dev/null +++ b/opengl/tests/gl_jni/jni/gl_code.cpp @@ -0,0 +1,183 @@ +// OpenGL ES 1.0 code + +#include +#define LOG_TAG "GLJNI gl_code.cpp" +#include + +#include + +#include + +#include +#include + +GLuint texture; +GLfloat background; + +#define FIXED_ONE 0x10000 + +static void printGLString(const char *name, GLenum s) { + const char *v = (const char *) glGetString(s); + LOGI("GL %s = %s\n", name, v); +} + +static void gluLookAt(float eyeX, float eyeY, float eyeZ, + float centerX, float centerY, float centerZ, float upX, float upY, + float upZ) +{ + // See the OpenGL GLUT documentation for gluLookAt for a description + // of the algorithm. We implement it in a straightforward way: + + float fx = centerX - eyeX; + float fy = centerY - eyeY; + float fz = centerZ - eyeZ; + + // Normalize f + float rlf = 1.0f / sqrtf(fx*fx + fy*fy + fz*fz); + fx *= rlf; + fy *= rlf; + fz *= rlf; + + // Normalize up + float rlup = 1.0f / sqrtf(upX*upX + upY*upY + upZ*upZ); + upX *= rlup; + upY *= rlup; + upZ *= rlup; + + // compute s = f x up (x means "cross product") + + float sx = fy * upZ - fz * upY; + float sy = fz * upX - fx * upZ; + float sz = fx * upY - fy * upX; + + // compute u = s x f + float ux = sy * fz - sz * fy; + float uy = sz * fx - sx * fz; + float uz = sx * fy - sy * fx; + + float m[16] ; + m[0] = sx; + m[1] = ux; + m[2] = -fx; + m[3] = 0.0f; + + m[4] = sy; + m[5] = uy; + m[6] = -fy; + m[7] = 0.0f; + + m[8] = sz; + m[9] = uz; + m[10] = -fz; + m[11] = 0.0f; + + m[12] = 0.0f; + m[13] = 0.0f; + m[14] = 0.0f; + m[15] = 1.0f; + + glMultMatrixf(m); + glTranslatef(-eyeX, -eyeY, -eyeZ); +} + +void init_scene(int width, int height) +{ + printGLString("Version", GL_VERSION); + printGLString("Vendor", GL_VENDOR); + printGLString("Renderer", GL_RENDERER); + printGLString("Extensions", GL_EXTENSIONS); + + glDisable(GL_DITHER); + glEnable(GL_CULL_FACE); + + float ratio = width / height; + glViewport(0, 0, width, height); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustumf(-ratio, ratio, -1, 1, 1, 10); + + glMatrixMode(GL_MODELVIEW); + + glLoadIdentity(); + gluLookAt( + 0, 0, 3, // eye + 0, 0, 0, // center + 0, 1, 0); // up + + glEnable(GL_TEXTURE_2D); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); +} + +void create_texture() +{ + const unsigned int on = 0xff0000ff; + const unsigned int off = 0xffffffff; + const unsigned int pixels[] = + { + on, off, on, off, on, off, on, off, + off, on, off, on, off, on, off, on, + on, off, on, off, on, off, on, off, + off, on, off, on, off, on, off, on, + on, off, on, off, on, off, on, off, + off, on, off, on, off, on, off, on, + on, off, on, off, on, off, on, off, + off, on, off, on, off, on, off, on, + }; + + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); +} + +extern "C" { + JNIEXPORT void JNICALL Java_com_android_gljni_GLJNILib_init(JNIEnv * env, jobject obj, jint width, jint height); + JNIEXPORT void JNICALL Java_com_android_gljni_GLJNILib_step(JNIEnv * env, jobject obj); + JNIEXPORT void JNICALL Java_com_android_gljni_GLJNILib_changeBackground(JNIEnv * env, jobject obj); +}; + +JNIEXPORT void JNICALL Java_com_android_gljni_GLJNILib_init(JNIEnv * env, jobject obj, jint width, jint height) +{ + init_scene(width, height); + create_texture(); +} + +JNIEXPORT void JNICALL Java_com_android_gljni_GLJNILib_step(JNIEnv * env, jobject obj) +{ + const GLfloat vertices[] = { + -1, -1, 0, + 1, -1, 0, + 1, 1, 0, + -1, 1, 0 + }; + + const GLfixed texCoords[] = { + 0, 0, + FIXED_ONE, 0, + FIXED_ONE, FIXED_ONE, + 0, FIXED_ONE + }; + + const GLushort quadIndices[] = { 0, 1, 2, 0, 2, 3 }; + glVertexPointer(3, GL_FLOAT, 0, vertices); + glTexCoordPointer(2, GL_FIXED, 0, texCoords); + + int nelem = sizeof(quadIndices)/sizeof(quadIndices[0]); + static float grey; + grey += 0.01f; + if (grey > 1.0f) { + grey = 0.0f; + } + glClearColor(background, grey, grey, 1.0f); + glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); + glDrawElements(GL_TRIANGLES, nelem, GL_UNSIGNED_SHORT, quadIndices); +} + +JNIEXPORT void JNICALL Java_com_android_gljni_GLJNILib_changeBackground(JNIEnv * env, jobject obj) +{ + background = 1.0f - background; +} \ No newline at end of file diff --git a/opengl/tests/gl_jni/res/values/strings.xml b/opengl/tests/gl_jni/res/values/strings.xml new file mode 100644 index 000000000000..880f5c99945b --- /dev/null +++ b/opengl/tests/gl_jni/res/values/strings.xml @@ -0,0 +1,29 @@ + + + + + + + + GLJNI + + + diff --git a/opengl/tests/gl_jni/src/com/android/gljni/GLJNIActivity.java b/opengl/tests/gl_jni/src/com/android/gljni/GLJNIActivity.java new file mode 100644 index 000000000000..c6f53134f940 --- /dev/null +++ b/opengl/tests/gl_jni/src/com/android/gljni/GLJNIActivity.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gljni; + +import android.app.Activity; +import android.os.Bundle; + +public class GLJNIActivity extends Activity { + + GLJNIView mView; + + @Override + protected void onCreate(Bundle icicle) { + super.onCreate(icicle); + mView = new GLJNIView(getApplication()); + mView.setFocusableInTouchMode(true); + setContentView(mView); + } + + @Override + protected void onPause() { + super.onPause(); + mView.onPause(); + } + + @Override + protected void onResume() { + super.onResume(); + mView.onResume(); + } +} diff --git a/opengl/tests/gl_jni/src/com/android/gljni/GLJNILib.java b/opengl/tests/gl_jni/src/com/android/gljni/GLJNILib.java new file mode 100644 index 000000000000..f56d2af95b38 --- /dev/null +++ b/opengl/tests/gl_jni/src/com/android/gljni/GLJNILib.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gljni; + +// Wrapper for native library + +public class GLJNILib { + + static { + System.loadLibrary("gljni"); + } + + /** + * @param width the current view width + * @param height the current view height + */ + public static native void init(int width, int height); + public static native void step(); + public static native void changeBackground(); +} diff --git a/opengl/tests/gl_jni/src/com/android/gljni/GLJNIView.java b/opengl/tests/gl_jni/src/com/android/gljni/GLJNIView.java new file mode 100644 index 000000000000..9a2c8c45480d --- /dev/null +++ b/opengl/tests/gl_jni/src/com/android/gljni/GLJNIView.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gljni; +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +import android.content.Context; +import android.opengl.GLSurfaceView; +import android.util.AttributeSet; +import android.util.Log; +import android.view.KeyEvent; +import android.view.MotionEvent; + +import javax.microedition.khronos.egl.EGL10; +import javax.microedition.khronos.egl.EGLConfig; +import javax.microedition.khronos.opengles.GL10; +/** + * An implementation of SurfaceView that uses the dedicated surface for + * displaying an OpenGL animation. This allows the animation to run in a + * separate thread, without requiring that it be driven by the update mechanism + * of the view hierarchy. + * + * The application-specific rendering code is delegated to a GLView.Renderer + * instance. + */ +class GLJNIView extends GLSurfaceView { + GLJNIView(Context context) { + super(context); + init(); + } + + public GLJNIView(Context context, AttributeSet attrs) { + super(context, attrs); + init(); + } + + private void init() { + setRenderer(new Renderer()); + } + + private class Renderer implements GLSurfaceView.Renderer { + private static final String TAG = "Renderer"; + public void onDrawFrame(GL10 gl) { + GLJNILib.step(); + } + + public void onSurfaceChanged(GL10 gl, int width, int height) { + GLJNILib.init(width, height); + } + + public void onSurfaceCreated(GL10 gl, EGLConfig config) { + // Do nothing. + } + } + + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + GLJNILib.changeBackground(); + return true; + } +} + diff --git a/opengl/tests/gralloc/Android.mk b/opengl/tests/gralloc/Android.mk new file mode 100644 index 000000000000..d43c39a46201 --- /dev/null +++ b/opengl/tests/gralloc/Android.mk @@ -0,0 +1,16 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_SRC_FILES:= \ + gralloc.cpp + +LOCAL_SHARED_LIBRARIES := \ + libcutils \ + libutils \ + libui + +LOCAL_MODULE:= test-opengl-gralloc + +LOCAL_MODULE_TAGS := optional + +include $(BUILD_EXECUTABLE) diff --git a/opengl/tests/gralloc/gralloc.cpp b/opengl/tests/gralloc/gralloc.cpp new file mode 100644 index 000000000000..8987040c80fc --- /dev/null +++ b/opengl/tests/gralloc/gralloc.cpp @@ -0,0 +1,110 @@ +/* + ** + ** Copyright 2009, The Android Open Source Project + ** + ** Licensed under the Apache License, Version 2.0 (the "License"); + ** you may not use this file except in compliance with the License. + ** You may obtain a copy of the License at + ** + ** http://www.apache.org/licenses/LICENSE-2.0 + ** + ** Unless required by applicable law or agreed to in writing, software + ** distributed under the License is distributed on an "AS IS" BASIS, + ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ** See the License for the specific language governing permissions and + ** limitations under the License. + */ + +#define LOG_TAG "StopWatch" + +#include +#include +#include +#include + +#include +#include + +using namespace android; + +void* lamecpy(void* d, void const* s, size_t size) { + char* dst = (char*)d; + char const* src = (char const*)s; + while (size) { + *dst++ = *src++; + size--; + } + return d; +} + +int main(int argc, char** argv) +{ + size_t size = 128*256*4; + void* temp = malloc(size); + void* temp2 = malloc(size); + memset(temp, 0, size); + memset(temp2, 0, size); + + + sp buffer = new GraphicBuffer(128, 256, HAL_PIXEL_FORMAT_RGBA_8888, + GRALLOC_USAGE_SW_READ_OFTEN | + GRALLOC_USAGE_SW_WRITE_OFTEN); + + status_t err = buffer->initCheck(); + if (err != NO_ERROR) { + printf("%s\n", strerror(-err)); + return 0; + } + + void* vaddr; + buffer->lock( + GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN, + &vaddr); + + { + StopWatch watch("memset"); + for (int i=0 ; i<10 ; i++) + memset(vaddr, 0, size); + } + + { + StopWatch watch("memcpy baseline"); + for (int i=0 ; i<10 ; i++) + memcpy(temp, temp2, size); + } + + { + StopWatch watch("memcpy from gralloc"); + for (int i=0 ; i<10 ; i++) + memcpy(temp, vaddr, size); + } + + { + StopWatch watch("memcpy into gralloc"); + for (int i=0 ; i<10 ; i++) + memcpy(vaddr, temp, size); + } + + + { + StopWatch watch("lamecpy baseline"); + for (int i=0 ; i<10 ; i++) + lamecpy(temp, temp2, size); + } + + { + StopWatch watch("lamecpy from gralloc"); + for (int i=0 ; i<10 ; i++) + lamecpy(temp, vaddr, size); + } + + { + StopWatch watch("lamecpy into gralloc"); + for (int i=0 ; i<10 ; i++) + lamecpy(vaddr, temp, size); + } + + buffer->unlock(); + + return 0; +} diff --git a/opengl/tests/linetex/Android.mk b/opengl/tests/linetex/Android.mk new file mode 100644 index 000000000000..6ff248dcce34 --- /dev/null +++ b/opengl/tests/linetex/Android.mk @@ -0,0 +1,17 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_SRC_FILES:= \ + linetex.cpp + +LOCAL_SHARED_LIBRARIES := \ + libcutils \ + libEGL \ + libGLESv1_CM \ + libui + +LOCAL_MODULE:= test-opengl-linetex + +LOCAL_MODULE_TAGS := optional + +include $(BUILD_EXECUTABLE) diff --git a/opengl/tests/linetex/linetex.cpp b/opengl/tests/linetex/linetex.cpp new file mode 100644 index 000000000000..68429409403b --- /dev/null +++ b/opengl/tests/linetex/linetex.cpp @@ -0,0 +1,116 @@ +/* +** +** Copyright 2006, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +#define LOG_TAG "fillrate" + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +using namespace android; + +int main(int argc, char** argv) +{ + EGLint configAttribs[] = { + EGL_DEPTH_SIZE, 0, + EGL_NONE + }; + + EGLint majorVersion; + EGLint minorVersion; + EGLContext context; + EGLConfig config; + EGLSurface surface; + EGLint w, h; + EGLDisplay dpy; + + EGLNativeWindowType window = android_createDisplaySurface(); + + dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); + eglInitialize(dpy, &majorVersion, &minorVersion); + + status_t err = EGLUtils::selectConfigForNativeWindow( + dpy, configAttribs, window, &config); + if (err) { + fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n"); + return 0; + } + + surface = eglCreateWindowSurface(dpy, config, window, NULL); + context = eglCreateContext(dpy, config, NULL, NULL); + eglMakeCurrent(dpy, surface, surface, context); + eglQuerySurface(dpy, surface, EGL_WIDTH, &w); + eglQuerySurface(dpy, surface, EGL_HEIGHT, &h); + + printf("w=%d, h=%d\n", w, h); + + glBindTexture(GL_TEXTURE_2D, 0); + glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); + glDisable(GL_DITHER); + glDisable(GL_BLEND); + glEnable(GL_TEXTURE_2D); + glColor4f(1,1,1,1); + + + // default pack-alignment is 4 + const uint16_t t16[64] = { 0xFFFF, 0, 0xF800, 0, 0x07E0, 0, 0x001F, 0 }; + + const GLfloat vertices[4][2] = { + { w/2, 0 }, + { w/2, h } + }; + + const GLfloat texCoords[4][2] = { + { 0, 0 }, + { 1, 1 } + }; + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 4, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, t16); + + glViewport(0, 0, w, h); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrthof(0, w, 0, h, 0, 1); + + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glVertexPointer(2, GL_FLOAT, 0, vertices); + glTexCoordPointer(2, GL_FLOAT, 0, texCoords); + + glClearColor(0,0,0,0); + glClear(GL_COLOR_BUFFER_BIT); + glDrawArrays(GL_LINES, 0, 2); + eglSwapBuffers(dpy, surface); + + usleep(5*1000000); + + eglTerminate(dpy); + + return 0; +} diff --git a/opengl/tests/swapinterval/Android.mk b/opengl/tests/swapinterval/Android.mk new file mode 100644 index 000000000000..619447ca6422 --- /dev/null +++ b/opengl/tests/swapinterval/Android.mk @@ -0,0 +1,17 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_SRC_FILES:= \ + swapinterval.cpp + +LOCAL_SHARED_LIBRARIES := \ + libcutils \ + libEGL \ + libGLESv1_CM \ + libui + +LOCAL_MODULE:= test-opengl-swapinterval + +LOCAL_MODULE_TAGS := optional + +include $(BUILD_EXECUTABLE) diff --git a/opengl/tests/swapinterval/swapinterval.cpp b/opengl/tests/swapinterval/swapinterval.cpp new file mode 100644 index 000000000000..df53b6269925 --- /dev/null +++ b/opengl/tests/swapinterval/swapinterval.cpp @@ -0,0 +1,121 @@ +/* + ** + ** Copyright 2006, The Android Open Source Project + ** + ** Licensed under the Apache License, Version 2.0 (the "License"); + ** you may not use this file except in compliance with the License. + ** You may obtain a copy of the License at + ** + ** http://www.apache.org/licenses/LICENSE-2.0 + ** + ** Unless required by applicable law or agreed to in writing, software + ** distributed under the License is distributed on an "AS IS" BASIS, + ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ** See the License for the specific language governing permissions and + ** limitations under the License. + */ + +#include +#include + +#include +#include +#include + +#include +#include +#include + +using namespace android; + +int main(int argc, char** argv) +{ + EGLint configAttribs[] = { + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, + EGL_NONE + }; + + EGLint majorVersion; + EGLint minorVersion; + EGLContext context; + EGLConfig config; + EGLint numConfigs=0; + EGLSurface surface; + EGLint w, h; + EGLDisplay dpy; + + + EGLNativeWindowType window = android_createDisplaySurface(); + + dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); + eglInitialize(dpy, 0 ,0) ;//&majorVersion, &minorVersion); + eglGetConfigs(dpy, NULL, 0, &numConfigs); + printf("# configs = %d\n", numConfigs); + + status_t err = EGLUtils::selectConfigForNativeWindow( + dpy, configAttribs, window, &config); + if (err) { + fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n"); + return 0; + } + + EGLint r,g,b,a; + eglGetConfigAttrib(dpy, config, EGL_RED_SIZE, &r); + eglGetConfigAttrib(dpy, config, EGL_GREEN_SIZE, &g); + eglGetConfigAttrib(dpy, config, EGL_BLUE_SIZE, &b); + eglGetConfigAttrib(dpy, config, EGL_ALPHA_SIZE, &a); + + surface = eglCreateWindowSurface(dpy, config, window, NULL); + if (surface == EGL_NO_SURFACE) { + EGLint err = eglGetError(); + fprintf(stderr, "%s, config=%p, format = %d-%d-%d-%d\n", + EGLUtils::strerror(err), config, r,g,b,a); + return 0; + } else { + printf("config=%p, format = %d-%d-%d-%d\n", config, r,g,b,a); + } + + context = eglCreateContext(dpy, config, NULL, NULL); + eglMakeCurrent(dpy, surface, surface, context); + eglQuerySurface(dpy, surface, EGL_WIDTH, &w); + eglQuerySurface(dpy, surface, EGL_HEIGHT, &h); + + printf("w=%d, h=%d\n", w, h); + + glDisable(GL_DITHER); + glEnable(GL_BLEND); + + glViewport(0, 0, w, h); + glOrthof(0, w, 0, h, 0, 1); + + eglSwapInterval(dpy, 1); + + glClearColor(1,0,0,0); + glClear(GL_COLOR_BUFFER_BIT); + eglSwapBuffers(dpy, surface); + + + int time = 10; + printf("screen should flash red/green quickly for %d s...\n", time); + + int c = 0; + nsecs_t start = systemTime(); + nsecs_t t; + do { + glClearColor(1,0,0,0); + glClear(GL_COLOR_BUFFER_BIT); + eglSwapBuffers(dpy, surface); + glClearColor(0,1,0,0); + glClear(GL_COLOR_BUFFER_BIT); + eglSwapBuffers(dpy, surface); + t = systemTime() - start; + c += 2; + } while (int(ns2s(t))<=time); + + double p = (double(t) / c) / 1000000000.0; + printf("refresh-rate is %f fps (%f ms)\n", 1.0f/p, p*1000.0); + + eglTerminate(dpy); + + return 0; +} diff --git a/opengl/tests/textures/Android.mk b/opengl/tests/textures/Android.mk index 8d5f56de2bba..b2fa18557744 100644 --- a/opengl/tests/textures/Android.mk +++ b/opengl/tests/textures/Android.mk @@ -2,7 +2,7 @@ LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES:= \ - textures.c + textures.cpp LOCAL_SHARED_LIBRARIES := \ libcutils \ @@ -14,4 +14,6 @@ LOCAL_MODULE:= test-opengl-textures LOCAL_MODULE_TAGS := optional +LOCAL_CFLAGS := -DGL_GLEXT_PROTOTYPES + include $(BUILD_EXECUTABLE) diff --git a/opengl/tests/textures/textures.c b/opengl/tests/textures/textures.c deleted file mode 100644 index 214291b3b156..000000000000 --- a/opengl/tests/textures/textures.c +++ /dev/null @@ -1,109 +0,0 @@ -/* -** -** Copyright 2006, The Android Open Source Project -** -** Licensed under the Apache License, Version 2.0 (the "License"); -** you may not use this file except in compliance with the License. -** You may obtain a copy of the License at -** -** http://www.apache.org/licenses/LICENSE-2.0 -** -** Unless required by applicable law or agreed to in writing, software -** distributed under the License is distributed on an "AS IS" BASIS, -** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -** See the License for the specific language governing permissions and -** limitations under the License. -*/ - -#include -#include - -#include -#include -#include - -int main(int argc, char** argv) -{ - EGLint s_configAttribs[] = { - EGL_RED_SIZE, 5, - EGL_GREEN_SIZE, 6, - EGL_BLUE_SIZE, 5, - EGL_NONE - }; - - EGLint numConfigs = -1; - EGLint majorVersion; - EGLint minorVersion; - EGLConfig config; - EGLContext context; - EGLSurface surface; - EGLint w, h; - - EGLDisplay dpy; - - dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); - eglInitialize(dpy, &majorVersion, &minorVersion); - eglChooseConfig(dpy, s_configAttribs, &config, 1, &numConfigs); - surface = eglCreateWindowSurface(dpy, config, - android_createDisplaySurface(), NULL); - context = eglCreateContext(dpy, config, NULL, NULL); - eglMakeCurrent(dpy, surface, surface, context); - eglQuerySurface(dpy, surface, EGL_WIDTH, &w); - eglQuerySurface(dpy, surface, EGL_HEIGHT, &h); - GLint dim = w +#include + +#include +#include +#include + +#include +#include + +using namespace android; + +int main(int argc, char** argv) +{ + EGLint configAttribs[] = { + EGL_DEPTH_SIZE, 0, + EGL_NONE + }; + + EGLint majorVersion; + EGLint minorVersion; + EGLContext context; + EGLConfig config; + EGLSurface surface; + EGLint w, h; + EGLDisplay dpy; + + EGLNativeWindowType window = android_createDisplaySurface(); + + dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); + eglInitialize(dpy, &majorVersion, &minorVersion); + + status_t err = EGLUtils::selectConfigForNativeWindow( + dpy, configAttribs, window, &config); + if (err) { + fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n"); + return 0; + } + + surface = eglCreateWindowSurface(dpy, config, window, NULL); + context = eglCreateContext(dpy, config, NULL, NULL); + eglMakeCurrent(dpy, surface, surface, context); + eglQuerySurface(dpy, surface, EGL_WIDTH, &w); + eglQuerySurface(dpy, surface, EGL_HEIGHT, &h); + GLint dim = w -#include - -#include -#include -#include - -EGLDisplay eglDisplay; -EGLSurface eglSurface; -EGLContext eglContext; -GLuint texture; - -#define FIXED_ONE 0x10000 -#define ITERATIONS 50 - -int init_gl_surface(void); -void free_gl_surface(void); -void init_scene(void); -void render(int quads); -void create_texture(void); -int readTimer(void); - -static void gluLookAt(float eyeX, float eyeY, float eyeZ, - float centerX, float centerY, float centerZ, float upX, float upY, - float upZ) -{ - // See the OpenGL GLUT documentation for gluLookAt for a description - // of the algorithm. We implement it in a straightforward way: - - float fx = centerX - eyeX; - float fy = centerY - eyeY; - float fz = centerZ - eyeZ; - - // Normalize f - float rlf = 1.0f / sqrtf(fx*fx + fy*fy + fz*fz); - fx *= rlf; - fy *= rlf; - fz *= rlf; - - // Normalize up - float rlup = 1.0f / sqrtf(upX*upX + upY*upY + upZ*upZ); - upX *= rlup; - upY *= rlup; - upZ *= rlup; - - // compute s = f x up (x means "cross product") - - float sx = fy * upZ - fz * upY; - float sy = fz * upX - fx * upZ; - float sz = fx * upY - fy * upX; - - // compute u = s x f - float ux = sy * fz - sz * fy; - float uy = sz * fx - sx * fz; - float uz = sx * fy - sy * fx; - - float m[16] ; - m[0] = sx; - m[1] = ux; - m[2] = -fx; - m[3] = 0.0f; - - m[4] = sy; - m[5] = uy; - m[6] = -fy; - m[7] = 0.0f; - - m[8] = sz; - m[9] = uz; - m[10] = -fz; - m[11] = 0.0f; - - m[12] = 0.0f; - m[13] = 0.0f; - m[14] = 0.0f; - m[15] = 1.0f; - - glMultMatrixf(m); - glTranslatef(-eyeX, -eyeY, -eyeZ); -} - -int main(int argc, char **argv) -{ - int q; - int start, end; - - printf("Initializing EGL...\n"); - - if(!init_gl_surface()) - { - printf("GL initialisation failed - exiting\n"); - return 0; - } - - init_scene(); - - create_texture(); - - printf("Start test...\n"); - - render(argc==2 ? atoi(argv[1]) : ITERATIONS); - - free_gl_surface(); - - return 0; -} - -int init_gl_surface(void) -{ - EGLint numConfigs = 1; - EGLConfig myConfig = {0}; - EGLint attrib[] = - { - EGL_DEPTH_SIZE, 16, - EGL_NONE - }; - - if ( (eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY ) - { - printf("eglGetDisplay failed\n"); - return 0; - } - - if ( eglInitialize(eglDisplay, NULL, NULL) != EGL_TRUE ) - { - printf("eglInitialize failed\n"); - return 0; - } - - if ( eglChooseConfig(eglDisplay, attrib, &myConfig, 1, &numConfigs) != EGL_TRUE ) - { - printf("eglChooseConfig failed\n"); - return 0; - } - - if ( (eglSurface = eglCreateWindowSurface(eglDisplay, myConfig, - android_createDisplaySurface(), 0)) == EGL_NO_SURFACE ) - { - printf("eglCreateWindowSurface failed\n"); - return 0; - } - - if ( (eglContext = eglCreateContext(eglDisplay, myConfig, 0, 0)) == EGL_NO_CONTEXT ) - { - printf("eglCreateContext failed\n"); - return 0; - } - - if ( eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext) != EGL_TRUE ) - { - printf("eglMakeCurrent failed\n"); - return 0; - } - - return 1; -} - -void free_gl_surface(void) -{ - if (eglDisplay != EGL_NO_DISPLAY) - { - eglMakeCurrent( EGL_NO_DISPLAY, EGL_NO_SURFACE, - EGL_NO_SURFACE, EGL_NO_CONTEXT ); - eglDestroyContext( eglDisplay, eglContext ); - eglDestroySurface( eglDisplay, eglSurface ); - eglTerminate( eglDisplay ); - eglDisplay = EGL_NO_DISPLAY; - } -} - -void init_scene(void) -{ - glDisable(GL_DITHER); - glEnable(GL_CULL_FACE); - - float ratio = 320.0f / 480.0f; - glViewport(0, 0, 320, 480); - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glFrustumf(-ratio, ratio, -1, 1, 1, 10); - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - gluLookAt( - 0, 0, 3, // eye - 0, 0, 0, // center - 0, 1, 0); // up - - glEnable(GL_TEXTURE_2D); - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); -} - -void create_texture(void) -{ - const unsigned int on = 0xff0000ff; - const unsigned int off = 0xffffffff; - const unsigned int pixels[] = - { - on, off, on, off, on, off, on, off, - off, on, off, on, off, on, off, on, - on, off, on, off, on, off, on, off, - off, on, off, on, off, on, off, on, - on, off, on, off, on, off, on, off, - off, on, off, on, off, on, off, on, - on, off, on, off, on, off, on, off, - off, on, off, on, off, on, off, on, - }; - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); - glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); -} - -void render(int quads) -{ - int i, j; - - const GLfloat vertices[] = { - -1, -1, 0, - 1, -1, 0, - 1, 1, 0, - -1, 1, 0 - }; - - const GLfixed texCoords[] = { - 0, 0, - FIXED_ONE, 0, - FIXED_ONE, FIXED_ONE, - 0, FIXED_ONE - }; - - const GLushort template[] = { 0, 1, 2, 0, 2, 3 }; - - - GLushort* indices = (GLushort*)malloc(quads*sizeof(template)); - for (i=0 ; i +#include +#include + +#include +#include + +#include +#include +#include + +using namespace android; + +EGLDisplay eglDisplay; +EGLSurface eglSurface; +EGLContext eglContext; +GLuint texture; + +#define FIXED_ONE 0x10000 +#define ITERATIONS 50 + +int init_gl_surface(void); +void free_gl_surface(void); +void init_scene(void); +void render(int quads); +void create_texture(void); +int readTimer(void); + +static void gluLookAt(float eyeX, float eyeY, float eyeZ, + float centerX, float centerY, float centerZ, float upX, float upY, + float upZ) +{ + // See the OpenGL GLUT documentation for gluLookAt for a description + // of the algorithm. We implement it in a straightforward way: + + float fx = centerX - eyeX; + float fy = centerY - eyeY; + float fz = centerZ - eyeZ; + + // Normalize f + float rlf = 1.0f / sqrtf(fx*fx + fy*fy + fz*fz); + fx *= rlf; + fy *= rlf; + fz *= rlf; + + // Normalize up + float rlup = 1.0f / sqrtf(upX*upX + upY*upY + upZ*upZ); + upX *= rlup; + upY *= rlup; + upZ *= rlup; + + // compute s = f x up (x means "cross product") + + float sx = fy * upZ - fz * upY; + float sy = fz * upX - fx * upZ; + float sz = fx * upY - fy * upX; + + // compute u = s x f + float ux = sy * fz - sz * fy; + float uy = sz * fx - sx * fz; + float uz = sx * fy - sy * fx; + + float m[16] ; + m[0] = sx; + m[1] = ux; + m[2] = -fx; + m[3] = 0.0f; + + m[4] = sy; + m[5] = uy; + m[6] = -fy; + m[7] = 0.0f; + + m[8] = sz; + m[9] = uz; + m[10] = -fz; + m[11] = 0.0f; + + m[12] = 0.0f; + m[13] = 0.0f; + m[14] = 0.0f; + m[15] = 1.0f; + + glMultMatrixf(m); + glTranslatef(-eyeX, -eyeY, -eyeZ); +} + +int main(int argc, char **argv) +{ + int q; + int start, end; + + printf("Initializing EGL...\n"); + + if(!init_gl_surface()) + { + printf("GL initialisation failed - exiting\n"); + return 0; + } + + init_scene(); + + create_texture(); + + printf("Start test...\n"); + + render(argc==2 ? atoi(argv[1]) : ITERATIONS); + + free_gl_surface(); + + return 0; +} + +int init_gl_surface(void) +{ + EGLint numConfigs = 1; + EGLConfig myConfig = {0}; + EGLint attrib[] = + { + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, + EGL_DEPTH_SIZE, 16, + EGL_NONE + }; + + if ( (eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY ) + { + printf("eglGetDisplay failed\n"); + return 0; + } + + if ( eglInitialize(eglDisplay, NULL, NULL) != EGL_TRUE ) + { + printf("eglInitialize failed\n"); + return 0; + } + + EGLNativeWindowType window = android_createDisplaySurface(); + EGLUtils::selectConfigForNativeWindow(eglDisplay, attrib, window, &myConfig); + + if ( (eglSurface = eglCreateWindowSurface(eglDisplay, myConfig, + window, 0)) == EGL_NO_SURFACE ) + { + printf("eglCreateWindowSurface failed\n"); + return 0; + } + + if ( (eglContext = eglCreateContext(eglDisplay, myConfig, 0, 0)) == EGL_NO_CONTEXT ) + { + printf("eglCreateContext failed\n"); + return 0; + } + + if ( eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext) != EGL_TRUE ) + { + printf("eglMakeCurrent failed\n"); + return 0; + } + + return 1; +} + +void free_gl_surface(void) +{ + if (eglDisplay != EGL_NO_DISPLAY) + { + eglMakeCurrent( EGL_NO_DISPLAY, EGL_NO_SURFACE, + EGL_NO_SURFACE, EGL_NO_CONTEXT ); + eglDestroyContext( eglDisplay, eglContext ); + eglDestroySurface( eglDisplay, eglSurface ); + eglTerminate( eglDisplay ); + eglDisplay = EGL_NO_DISPLAY; + } +} + +void init_scene(void) +{ + glDisable(GL_DITHER); + glEnable(GL_CULL_FACE); + + float ratio = 320.0f / 480.0f; + glViewport(0, 0, 320, 480); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustumf(-ratio, ratio, -1, 1, 1, 10); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + gluLookAt( + 0, 0, 3, // eye + 0, 0, 0, // center + 0, 1, 0); // up + + glEnable(GL_TEXTURE_2D); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); +} + +void create_texture(void) +{ + const unsigned int on = 0xff0000ff; + const unsigned int off = 0xffffffff; + const unsigned int pixels[] = + { + on, off, on, off, on, off, on, off, + off, on, off, on, off, on, off, on, + on, off, on, off, on, off, on, off, + off, on, off, on, off, on, off, on, + on, off, on, off, on, off, on, off, + off, on, off, on, off, on, off, on, + on, off, on, off, on, off, on, off, + off, on, off, on, off, on, off, on, + }; + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); +} + +void render(int quads) +{ + int i, j; + + const GLfloat vertices[] = { + -1, -1, 0, + 1, -1, 0, + 1, 1, 0, + -1, 1, 0 + }; + + const GLfixed texCoords[] = { + 0, 0, + FIXED_ONE, 0, + FIXED_ONE, FIXED_ONE, + 0, FIXED_ONE + }; + + const GLushort quadIndices[] = { 0, 1, 2, 0, 2, 3 }; + + + GLushort* indices = (GLushort*)malloc(quads*sizeof(quadIndices)); + for (i=0 ; i