diff options
author | Jean-Baptiste Queru <jbq@google.com> | 2009-11-15 12:06:20 -0800 |
---|---|---|
committer | Jean-Baptiste Queru <jbq@google.com> | 2009-11-15 12:06:23 -0800 |
commit | daeaa536fab8f9a71227578aab212fa5d4daaf00 (patch) | |
tree | 1de0bdf145436d2ce71bb665124fc24871ace417 /opengl/tests | |
parent | 4511f47c50b39b02eb04eaac8e6a660c29ea8444 (diff) | |
parent | cc8c35cee5de7fdf2d79a1a3716120b64301cdfe (diff) | |
download | frameworks_native-daeaa536fab8f9a71227578aab212fa5d4daaf00.zip frameworks_native-daeaa536fab8f9a71227578aab212fa5d4daaf00.tar.gz frameworks_native-daeaa536fab8f9a71227578aab212fa5d4daaf00.tar.bz2 |
merge from eclair
Diffstat (limited to 'opengl/tests')
36 files changed, 2448 insertions, 82 deletions
diff --git a/opengl/tests/angeles/Android.mk b/opengl/tests/angeles/Android.mk index e193483..d0c3221 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.cpp index 7d0d320..06fa0c2 100644 --- a/opengl/tests/angeles/app-linux.c +++ b/opengl/tests/angeles/app-linux.cpp @@ -52,6 +52,11 @@ #include <EGL/egl.h> #include <GLES/gl.h> +#include <ui/FramebufferNativeWindow.h> +#include <ui/EGLUtils.h> + +using namespace android; + #include "app.h" @@ -115,48 +120,33 @@ static void checkEGLErrors() 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 + EGLint configAttribs[] = { + EGL_DEPTH_SIZE, 16, EGL_NONE }; - EGLint numConfigs = -1; EGLint majorVersion; EGLint minorVersion; - EGLConfig config; EGLContext context; + EGLConfig config; EGLSurface surface; - + EGLint w, h; EGLDisplay dpy; - dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); - egl_error("eglGetDisplay"); - fprintf(stderr,"dpy = 0x%08x\n", (unsigned) dpy); + EGLNativeWindowType window = android_createDisplaySurface(); + dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); 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"); + + 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); diff --git a/opengl/tests/fillrate/Android.mk b/opengl/tests/fillrate/Android.mk new file mode 100644 index 0000000..a7d30c2 --- /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 0000000..911d354 --- /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 <stdlib.h> +#include <stdio.h> + +#include <EGL/egl.h> +#include <GLES/gl.h> +#include <GLES/glext.h> + +#include <utils/StopWatch.h> +#include <ui/FramebufferNativeWindow.h> +#include <ui/EGLUtils.h> + +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<c ; i++) { + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + } + eglSwapBuffers(dpy, surface); + } + + + // for (int c=31 ; c>=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<c ; i++) { + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + } + eglSwapBuffers(dpy, surface); + nsecs_t t = systemTime() - now; + times[j++] = t; + } + + for (int c=1, j=0 ; c<32 ; c++, j++) { + nsecs_t t = times[j]; + printf("%lld\t%d\t%f\n", t, c, (double(t)/c)/1000000.0); + } + + + + eglTerminate(dpy); + + return 0; +} diff --git a/opengl/tests/filter/Android.mk b/opengl/tests/filter/Android.mk index 31b7d9a..a254127 100644 --- a/opengl/tests/filter/Android.mk +++ b/opengl/tests/filter/Android.mk @@ -2,7 +2,7 @@ LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES:= \ - filter.c + filter.cpp LOCAL_SHARED_LIBRARIES := \ libcutils \ @@ -14,4 +14,6 @@ LOCAL_MODULE:= test-opengl-filter LOCAL_MODULE_TAGS := optional +LOCAL_CFLAGS := -DGL_GLEXT_PROTOTYPES + include $(BUILD_EXECUTABLE) diff --git a/opengl/tests/filter/filter.c b/opengl/tests/filter/filter.cpp index de97119..2351909 100644 --- a/opengl/tests/filter/filter.c +++ b/opengl/tests/filter/filter.cpp @@ -5,6 +5,13 @@ #include <GLES/gl.h> #include <GLES/glext.h> +#include <ui/FramebufferNativeWindow.h> +#include <ui/EGLUtils.h> + +using namespace android; + +#define USE_DRAW_TEXTURE 1 + int main(int argc, char** argv) { if (argc!=2 && argc!=3) { @@ -32,14 +39,20 @@ int main(int argc, char** argv) EGLDisplay dpy; + EGLNativeWindowType window = 0; + if (!usePbuffer) { + window = android_createDisplaySurface(); + } + 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); + 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) { @@ -52,6 +65,12 @@ int main(int argc, char** argv) eglQuerySurface(dpy, surface, EGL_HEIGHT, &h); GLint dim = w<h ? w : h; + glViewport(0, 0, w, h); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrthof(0, w, 0, h, 0, 1); + + glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT); GLint crop[4] = { 0, 4, 4, -4 }; @@ -117,14 +136,55 @@ int main(int argc, char** argv) break; } - glDrawTexiOES(0, 0, 0, dim, dim); + //glDrawTexiOES(0, 0, 0, dim, dim); + + const GLfloat vertices[4][2] = { + { 0, 0 }, + { 0, dim }, + { dim, dim }, + { dim, 0 } + }; + const GLfloat texCoords[4][2] = { + { 0, 0 }, + { 0, 1 }, + { 1, 1 }, + { 1, 0 } + }; + if (!usePbuffer) { eglSwapBuffers(dpy, surface); - } else { - glFinish(); } + glMatrixMode(GL_MODELVIEW); + glScissor(0,dim,dim,h-dim); + glDisable(GL_SCISSOR_TEST); + + for (int y=0 ; y<dim ; y++) { + //glDisable(GL_SCISSOR_TEST); + glClear(GL_COLOR_BUFFER_BIT); + + //glEnable(GL_SCISSOR_TEST); + +#if USE_DRAW_TEXTURE && GL_OES_draw_texture + glDrawTexiOES(0, y, 1, dim, dim); +#else + glLoadIdentity(); + glTranslatef(0, y, 0); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glVertexPointer(2, GL_FLOAT, 0, vertices); + glTexCoordPointer(2, GL_FLOAT, 0, texCoords); + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); +#endif + + if (!usePbuffer) { + eglSwapBuffers(dpy, surface); + } else { + glFinish(); + } + } + eglTerminate(dpy); return 0; } diff --git a/opengl/tests/finish/Android.mk b/opengl/tests/finish/Android.mk index 8b46cd7..5620814 100644 --- a/opengl/tests/finish/Android.mk +++ b/opengl/tests/finish/Android.mk @@ -2,7 +2,7 @@ LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES:= \ - finish.c + finish.cpp LOCAL_SHARED_LIBRARIES := \ libcutils \ @@ -14,4 +14,6 @@ LOCAL_MODULE:= test-opengl-finish LOCAL_MODULE_TAGS := optional +LOCAL_CFLAGS := -DGL_GLEXT_PROTOTYPES + include $(BUILD_EXECUTABLE) diff --git a/opengl/tests/finish/finish.c b/opengl/tests/finish/finish.cpp index 45fc758..91f5c45 100644 --- a/opengl/tests/finish/finish.c +++ b/opengl/tests/finish/finish.cpp @@ -24,39 +24,41 @@ #include <GLES/gl.h> #include <GLES/glext.h> +#include <utils/Timers.h> -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; -} +#include <ui/FramebufferNativeWindow.h> +#include <ui/EGLUtils.h> + +using namespace android; int main(int argc, char** argv) { - EGLint s_configAttribs[] = { - EGL_RED_SIZE, 5, - EGL_GREEN_SIZE, 6, - EGL_BLUE_SIZE, 5, + EGLint configAttribs[] = { + EGL_DEPTH_SIZE, 0, EGL_NONE }; - EGLint numConfigs = -1; EGLint majorVersion; EGLint minorVersion; - EGLConfig config; EGLContext context; + EGLConfig config; EGLSurface surface; EGLint w, h; - EGLDisplay dpy; + EGLNativeWindowType window = android_createDisplaySurface(); + dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); eglInitialize(dpy, &majorVersion, &minorVersion); - eglChooseConfig(dpy, s_configAttribs, &config, 1, &numConfigs); - surface = eglCreateWindowSurface(dpy, config, - android_createDisplaySurface(), NULL); + + 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); @@ -75,13 +77,13 @@ int main(int argc, char** argv) long long now, t; int i; - char* texels = malloc(512*512*2); + char* texels = (char*)malloc(512*512*2); memset(texels,0xFF,512*512*2); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 512, 512, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, texels); - char* dst = malloc(320*480*2); + char* dst = (char*)malloc(320*480*2); memset(dst, 0, 320*480*2); printf("307200 bytes memcpy\n"); for (i=0 ; i<4 ; i++) { diff --git a/opengl/tests/gl2_basic/Android.mk b/opengl/tests/gl2_basic/Android.mk new file mode 100644 index 0000000..a642eaf --- /dev/null +++ b/opengl/tests/gl2_basic/Android.mk @@ -0,0 +1,19 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_SRC_FILES:= \ + gl2_basic.cpp + +LOCAL_SHARED_LIBRARIES := \ + libcutils \ + libEGL \ + libGLESv2 \ + libui + +LOCAL_MODULE:= test-opengl-gl2_basic + +LOCAL_MODULE_TAGS := optional + +LOCAL_CFLAGS := -DGL_GLEXT_PROTOTYPES + +include $(BUILD_EXECUTABLE) diff --git a/opengl/tests/gl2_basic/gl2_basic.cpp b/opengl/tests/gl2_basic/gl2_basic.cpp new file mode 100644 index 0000000..2361db5 --- /dev/null +++ b/opengl/tests/gl2_basic/gl2_basic.cpp @@ -0,0 +1,357 @@ +/* + * 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. + */ + +#include <stdlib.h> +#include <stdio.h> +#include <time.h> +#include <sched.h> +#include <sys/resource.h> + +#include <EGL/egl.h> +#include <GLES2/gl2.h> +#include <GLES2/gl2ext.h> + +#include <utils/Timers.h> + +#include <ui/FramebufferNativeWindow.h> +#include <ui/EGLUtils.h> + +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("<init>"); + 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 0000000..81247df --- /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 0000000..a72a6a5 --- /dev/null +++ b/opengl/tests/gl2_jni/AndroidManifest.xml @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +/* +** +** 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. +*/ +--> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.gl2jni"> + <application + android:label="@string/gl2jni_activity"> + <activity android:name="GL2JNIActivity" + android:theme="@android:style/Theme.NoTitleBar.Fullscreen" + android:launchMode="singleTask" + android:configChanges="orientation|keyboardHidden"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + </activity> + </application> +</manifest> 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 0000000..c2fabe6 --- /dev/null +++ b/opengl/tests/gl2_jni/jni/gl_code.cpp @@ -0,0 +1,165 @@ +// OpenGL ES 2.0 code + +#include <nativehelper/jni.h> +#define LOG_TAG "GL2JNI gl_code.cpp" +#include <utils/Log.h> + +#include <EGL/egl.h> +#include <GLES2/gl2.h> +#include <GLES2/gl2ext.h> + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> + +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 0000000..e3f7331 --- /dev/null +++ b/opengl/tests/gl2_jni/res/values/strings.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +/* +** +** 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. +*/ +--> + +<!-- This file contains resource definitions for displayed strings, allowing + them to be changed based on the locale and options. --> + +<resources> + <!-- Simple strings. --> + <string name="gl2jni_activity">GL2JNI</string> + +</resources> + 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 0000000..c366a2c --- /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 0000000..040a984 --- /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 0000000..2dae090 --- /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 0000000..6b6341f --- /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 0000000..7dc2378 --- /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 <EGL/egl.h>
+#include <GLES/gl.h> +#include <GLES/glext.h> + +#include <ui/FramebufferNativeWindow.h> +#include <ui/EGLUtils.h> + +#include <stdio.h>
+#include <stdlib.h> +#include <math.h> + +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 0000000..4029fa1 --- /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 0000000..64bd6bf --- /dev/null +++ b/opengl/tests/gl_jni/AndroidManifest.xml @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +/* +** +** 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. +*/ +--> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.gljni"> + <uses-permission android:name="android.permission.INTERNET" /> + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> + <application + android:label="@string/gljni_activity"> + <activity android:name="GLJNIActivity" + android:theme="@android:style/Theme.NoTitleBar.Fullscreen" + android:launchMode="singleTask" + android:screenOrientation="landscape" + android:configChanges="orientation|keyboardHidden"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + </activity> + </application> +</manifest> 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 0000000..33b25ab --- /dev/null +++ b/opengl/tests/gl_jni/jni/gl_code.cpp @@ -0,0 +1,183 @@ +// OpenGL ES 1.0 code + +#include <nativehelper/jni.h> +#define LOG_TAG "GLJNI gl_code.cpp" +#include <utils/Log.h> + +#include <GLES/gl.h> + +#include <stdio.h> + +#include <stdlib.h> +#include <math.h> + +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 0000000..880f5c9 --- /dev/null +++ b/opengl/tests/gl_jni/res/values/strings.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +/* +** +** 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. +*/ +--> + +<!-- This file contains resource definitions for displayed strings, allowing + them to be changed based on the locale and options. --> + +<resources> + <!-- Simple strings. --> + <string name="gljni_activity">GLJNI</string> + +</resources> + 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 0000000..c6f5313 --- /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 0000000..f56d2af --- /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 0000000..9a2c8c4 --- /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 0000000..d43c39a --- /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 0000000..8987040 --- /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 <stdlib.h> +#include <stdio.h> +#include <utils/StopWatch.h> +#include <utils/Log.h> + +#include <ui/GraphicBuffer.h> +#include <ui/GraphicBufferMapper.h> + +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<GraphicBuffer> 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 0000000..6ff248d --- /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 0000000..6842940 --- /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 <unistd.h> +#include <stdlib.h> +#include <stdio.h> + +#include <EGL/egl.h> +#include <GLES/gl.h> +#include <GLES/glext.h> + +#include <utils/StopWatch.h> +#include <ui/FramebufferNativeWindow.h> +#include <ui/EGLUtils.h> + +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 0000000..619447c --- /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 0000000..df53b62 --- /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 <stdlib.h> +#include <stdio.h> + +#include <EGL/egl.h> +#include <GLES/gl.h> +#include <GLES/glext.h> + +#include <utils/StopWatch.h> +#include <ui/FramebufferNativeWindow.h> +#include <ui/EGLUtils.h> + +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 8d5f56d..b2fa185 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.cpp index 214291b..cbe8ffd 100644 --- a/opengl/tests/textures/textures.c +++ b/opengl/tests/textures/textures.cpp @@ -22,30 +22,39 @@ #include <GLES/gl.h> #include <GLES/glext.h> +#include <ui/FramebufferNativeWindow.h> +#include <ui/EGLUtils.h> + +using namespace android; + int main(int argc, char** argv) { - EGLint s_configAttribs[] = { - EGL_RED_SIZE, 5, - EGL_GREEN_SIZE, 6, - EGL_BLUE_SIZE, 5, + EGLint configAttribs[] = { + EGL_DEPTH_SIZE, 0, EGL_NONE }; - EGLint numConfigs = -1; EGLint majorVersion; EGLint minorVersion; - EGLConfig config; EGLContext context; + EGLConfig config; EGLSurface surface; EGLint w, h; - EGLDisplay dpy; + EGLNativeWindowType window = android_createDisplaySurface(); + dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); eglInitialize(dpy, &majorVersion, &minorVersion); - eglChooseConfig(dpy, s_configAttribs, &config, 1, &numConfigs); - surface = eglCreateWindowSurface(dpy, config, - android_createDisplaySurface(), NULL); + + 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); diff --git a/opengl/tests/tritex/Android.mk b/opengl/tests/tritex/Android.mk index 76fd8dd..6db3f49 100644 --- a/opengl/tests/tritex/Android.mk +++ b/opengl/tests/tritex/Android.mk @@ -2,7 +2,7 @@ LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES:= \ - tritex.c + tritex.cpp LOCAL_SHARED_LIBRARIES := \ libcutils \ diff --git a/opengl/tests/tritex/tritex.c b/opengl/tests/tritex/tritex.cpp index 60a7feb..3365ab4 100644 --- a/opengl/tests/tritex/tritex.c +++ b/opengl/tests/tritex/tritex.cpp @@ -6,10 +6,16 @@ #include <EGL/egl.h>
#include <GLES/gl.h> +#include <GLES/glext.h> + +#include <ui/FramebufferNativeWindow.h> +#include <ui/EGLUtils.h> #include <stdio.h>
#include <stdlib.h> #include <math.h> + +using namespace android; EGLDisplay eglDisplay;
EGLSurface eglSurface;
@@ -117,6 +123,7 @@ int init_gl_surface(void) EGLConfig myConfig = {0};
EGLint attrib[] =
{
+ EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_DEPTH_SIZE, 16,
EGL_NONE
};
@@ -132,15 +139,12 @@ int init_gl_surface(void) printf("eglInitialize failed\n");
return 0;
}
-
- if ( eglChooseConfig(eglDisplay, attrib, &myConfig, 1, &numConfigs) != EGL_TRUE )
- {
- printf("eglChooseConfig failed\n");
- return 0;
- }
+ + EGLNativeWindowType window = android_createDisplaySurface();
+ EGLUtils::selectConfigForNativeWindow(eglDisplay, attrib, window, &myConfig);
if ( (eglSurface = eglCreateWindowSurface(eglDisplay, myConfig, - android_createDisplaySurface(), 0)) == EGL_NO_SURFACE )
+ window, 0)) == EGL_NO_SURFACE )
{
printf("eglCreateWindowSurface failed\n");
return 0;
@@ -239,12 +243,12 @@ void render(int quads) 0, FIXED_ONE
};
- const GLushort template[] = { 0, 1, 2, 0, 2, 3 }; + const GLushort quadIndices[] = { 0, 1, 2, 0, 2, 3 }; - GLushort* indices = (GLushort*)malloc(quads*sizeof(template)); + GLushort* indices = (GLushort*)malloc(quads*sizeof(quadIndices)); for (i=0 ; i<quads ; i++) - memcpy(indices+(sizeof(template)/sizeof(indices[0]))*i, template, sizeof(template)); + memcpy(indices+(sizeof(quadIndices)/sizeof(indices[0]))*i, quadIndices, sizeof(quadIndices)); glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FIXED, 0, texCoords); @@ -262,7 +266,7 @@ void render(int quads) for (j=0 ; j<10 ; j++) { printf("loop %d / 10 (%d quads / loop)\n", j, quads); - int nelem = sizeof(template)/sizeof(template[0]); + int nelem = sizeof(quadIndices)/sizeof(quadIndices[0]); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glDrawElements(GL_TRIANGLES, nelem*quads, GL_UNSIGNED_SHORT, indices); eglSwapBuffers(eglDisplay, eglSurface); |