From 2c18ea2753707ff9b5e1c6ad18d025ff3cef374c Mon Sep 17 00:00:00 2001 From: Andrew Hsieh Date: Mon, 7 May 2012 13:45:20 +0800 Subject: Update samples The motivation of this update was to remove libnativehelper which was made implicitly available recently. But since we are on it, update samples source to recent one which allows same bitcode to compile/run on ARM/X86/MIPS. Tiny arch-specific *so are still required to call libbcc. Future llvm ABI in ndk will remove this restriction and phase out this. Change-Id: Ic32d7dcfbbe421c07911590b22dfd865cdeb2fb0 --- platforms/android-portable/arch-llvm/usr/include | 2 +- .../android-portable/arch-llvm/usr/include.arm | 1 + .../android-portable/arch-llvm/usr/include.mips | 1 + .../android-portable/arch-llvm/usr/include.x86 | 1 + samples/PhotoEditor/jni/Android-portable.mk | 2 + samples/PhotoEditor/jni/Android.mk | 3 +- samples/PhotoEditor/jni/Application.mk | 4 + samples/PhotoEditor/jni/_jni.cpp | 39 +- .../src/com/android/photoeditor/PhotoEditor.java | 2 + .../android/photoeditor/filters/ImageUtils.java | 1 + samples/bitmap-plasma-llvm/.classpath | 7 - samples/bitmap-plasma-llvm/.project | 33 -- samples/bitmap-plasma-llvm/default.properties | 2 +- samples/bitmap-plasma-llvm/jni/Android-portable.mk | 4 +- samples/bitmap-plasma-llvm/jni/Android.mk | 2 +- samples/bitmap-plasma-llvm/jni/Application.mk | 6 +- samples/bitmap-plasma-llvm/jni/libplasma.c | 224 --------- samples/bitmap-plasma-llvm/jni/plasma.cpp | 514 +++++++++++++++++++++ samples/bitmap-plasma-llvm/jni/plasmaLLVM.cpp | 458 ------------------ samples/bitmap-plasma-llvm/res/raw/gdb.setup | 2 - samples/bitmap-plasma-llvm/res/raw/gdbserver | Bin 125208 -> 0 bytes .../src/com/example/plasma/llvm/Plasma.java | 61 +-- samples/quake/jni/Android-portable.mk | 7 +- samples/quake/jni/Android.mk | 3 +- samples/quake/jni/Application.mk | 5 + samples/quake/jni/gl_draw.cpp | 9 +- samples/quake/jni/gl_vidandroid.cpp | 6 +- samples/quake/jni/masterMain.cpp | 59 ++- samples/quake/jni/net_udp.cpp | 118 ++++- samples/quake/jni/snd_dma.cpp | 9 +- samples/quake/jni/sys_android.cpp | 166 ++++++- .../src/com/android/quake/llvm/QuakeActivity.java | 66 +-- .../quake/src/com/android/quake/llvm/QuakeLib.java | 6 +- 33 files changed, 976 insertions(+), 847 deletions(-) create mode 120000 platforms/android-portable/arch-llvm/usr/include.arm create mode 120000 platforms/android-portable/arch-llvm/usr/include.mips create mode 120000 platforms/android-portable/arch-llvm/usr/include.x86 delete mode 100644 samples/bitmap-plasma-llvm/.classpath delete mode 100644 samples/bitmap-plasma-llvm/.project delete mode 100644 samples/bitmap-plasma-llvm/jni/libplasma.c create mode 100644 samples/bitmap-plasma-llvm/jni/plasma.cpp delete mode 100644 samples/bitmap-plasma-llvm/jni/plasmaLLVM.cpp delete mode 100644 samples/bitmap-plasma-llvm/res/raw/gdb.setup delete mode 100755 samples/bitmap-plasma-llvm/res/raw/gdbserver create mode 100644 samples/quake/jni/Application.mk diff --git a/platforms/android-portable/arch-llvm/usr/include b/platforms/android-portable/arch-llvm/usr/include index 914bdcf..6872ae4 120000 --- a/platforms/android-portable/arch-llvm/usr/include +++ b/platforms/android-portable/arch-llvm/usr/include @@ -1 +1 @@ -../../../../../ndk/platforms/android-9/arch-arm/usr/include \ No newline at end of file +include.arm \ No newline at end of file diff --git a/platforms/android-portable/arch-llvm/usr/include.arm b/platforms/android-portable/arch-llvm/usr/include.arm new file mode 120000 index 0000000..914bdcf --- /dev/null +++ b/platforms/android-portable/arch-llvm/usr/include.arm @@ -0,0 +1 @@ +../../../../../ndk/platforms/android-9/arch-arm/usr/include \ No newline at end of file diff --git a/platforms/android-portable/arch-llvm/usr/include.mips b/platforms/android-portable/arch-llvm/usr/include.mips new file mode 120000 index 0000000..ca09cc3 --- /dev/null +++ b/platforms/android-portable/arch-llvm/usr/include.mips @@ -0,0 +1 @@ +../../../../../ndk/platforms/android-9/arch-mips/usr/include \ No newline at end of file diff --git a/platforms/android-portable/arch-llvm/usr/include.x86 b/platforms/android-portable/arch-llvm/usr/include.x86 new file mode 120000 index 0000000..3fb9a68 --- /dev/null +++ b/platforms/android-portable/arch-llvm/usr/include.x86 @@ -0,0 +1 @@ +../../../../../ndk/platforms/android-9/arch-x86/usr/include \ No newline at end of file diff --git a/samples/PhotoEditor/jni/Android-portable.mk b/samples/PhotoEditor/jni/Android-portable.mk index 861ff01..2a83f8a 100644 --- a/samples/PhotoEditor/jni/Android-portable.mk +++ b/samples/PhotoEditor/jni/Android-portable.mk @@ -40,6 +40,8 @@ ifeq ($(TARGET_ARCH), arm) LOCAL_SDK_VERSION := 9 endif +LOCAL_CFLAGS := -D__GDK__ + LOCAL_C_INCLUDES := $(OUT)/../../../../frameworks/compile/libbcc/include #LOCAL_LDFLAGS := -L$(OUT)/system/lib diff --git a/samples/PhotoEditor/jni/Android.mk b/samples/PhotoEditor/jni/Android.mk index c94794a..794c7b8 100644 --- a/samples/PhotoEditor/jni/Android.mk +++ b/samples/PhotoEditor/jni/Android.mk @@ -41,8 +41,7 @@ ifeq ($(TARGET_ARCH), arm) endif LOCAL_CFLAGS := -Werror \ - -I$(OUT)/../../../../frameworks/compile/libbcc/include \ - -I$(OUT)/../../../../dalvik/libnativehelper/include + -I$(OUT)/../../../../frameworks/compile/libbcc/include LOCAL_LDFLAGS := -L$(OUT)/system/lib diff --git a/samples/PhotoEditor/jni/Application.mk b/samples/PhotoEditor/jni/Application.mk index f05229c..bb9ec4d 100644 --- a/samples/PhotoEditor/jni/Application.mk +++ b/samples/PhotoEditor/jni/Application.mk @@ -1 +1,5 @@ APP_ABI := armeabi armeabi-v7a +#APP_ABI := mips mips-r2 +#APP_ABI := x86 +APP_PLATFORM := android-9 + diff --git a/samples/PhotoEditor/jni/_jni.cpp b/samples/PhotoEditor/jni/_jni.cpp index 87263ff..63fd3d6 100644 --- a/samples/PhotoEditor/jni/_jni.cpp +++ b/samples/PhotoEditor/jni/_jni.cpp @@ -1,10 +1,11 @@ -#if !defined(__clang__) -#include +#include #include + +#if !defined(__GDK__) && !defined(__NOGDK__) #include #include - +#endif // !__GDK__ && !__NOGDK__ #define LOG_TAG "libjni_photoeditor" #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) @@ -30,14 +31,27 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) return JNI_VERSION_1_4; } + +#if !defined(__GDK__) && !defined(__NOGDK__) static void* lookupSymbol(void* pContext, const char* name) { return (void*) dlsym(RTLD_DEFAULT, name); } +#endif // !__GDK__ && !__NOGDK__ + +extern "C" JNIEXPORT jboolean JNICALL Java_com_android_photoeditor_filters_ImageUtils_gdk(JNIEnv *env, jobject obj) +{ +#if !defined(__NOGDK__) + return JNI_TRUE; +#else + return JNI_FALSE; +#endif +} extern "C" JNIEXPORT jboolean JNICALL Java_com_android_photoeditor_filters_ImageUtils_init( JNIEnv *env, jobject obj, jbyteArray scriptRef, jint length) { +#if !defined(__GDK__) && !defined(__NOGDK__) void *new_func_ptr[JNI_max]; int i, all_func_found = 1; @@ -58,10 +72,19 @@ extern "C" JNIEXPORT jboolean JNICALL Java_com_android_photoeditor_filters_Image } #endif bccRegisterSymbolCallback(script_ref, lookupSymbol, NULL); - if (bccPrepareExecutableEx(script_ref, ".", "/data/data/com.android.photoeditor/photoeditorLLVM", 0)) { - LOGE("Error! Cannot bccPrepareExecutableEx"); + + #ifdef OLD_BCC + if (bccPrepareExecutable(script_ref, "/data/data/com.android.photoeditor/photoeditorLLVM.oBCC", 0)) { + LOGE("Error! Cannot bccPrepareExecutable"); + return JNI_FALSE; + } + #else + if (bccPrepareExecutable(script_ref, "/data/data/com.android.photoeditor/", "photoeditorLLVM", 0)) { + LOGE("Error! Cannot bccPrepareExecutable"); return JNI_FALSE; } + #endif // OLD_BCC + for(i=0; i - - - - - - diff --git a/samples/bitmap-plasma-llvm/.project b/samples/bitmap-plasma-llvm/.project deleted file mode 100644 index 1240c70..0000000 --- a/samples/bitmap-plasma-llvm/.project +++ /dev/null @@ -1,33 +0,0 @@ - - - PlasmaLLVM - - - - - - com.android.ide.eclipse.adt.ResourceManagerBuilder - - - - - com.android.ide.eclipse.adt.PreCompilerBuilder - - - - - org.eclipse.jdt.core.javabuilder - - - - - com.android.ide.eclipse.adt.ApkBuilder - - - - - - com.android.ide.eclipse.adt.AndroidNature - org.eclipse.jdt.core.javanature - - diff --git a/samples/bitmap-plasma-llvm/default.properties b/samples/bitmap-plasma-llvm/default.properties index 3ac2523..e2e8061 100644 --- a/samples/bitmap-plasma-llvm/default.properties +++ b/samples/bitmap-plasma-llvm/default.properties @@ -8,4 +8,4 @@ # project structure. # Project target. -target=android-12 +target=android-8 diff --git a/samples/bitmap-plasma-llvm/jni/Android-portable.mk b/samples/bitmap-plasma-llvm/jni/Android-portable.mk index e3e99b5..099ad1e 100644 --- a/samples/bitmap-plasma-llvm/jni/Android-portable.mk +++ b/samples/bitmap-plasma-llvm/jni/Android-portable.mk @@ -3,6 +3,8 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := plasma_portable -LOCAL_SRC_FILES := libplasma.c +LOCAL_SRC_FILES := plasma.cpp + +LOCAL_CFLAGS := -D__GDK__ include $(BUILD_BITCODE) diff --git a/samples/bitmap-plasma-llvm/jni/Android.mk b/samples/bitmap-plasma-llvm/jni/Android.mk index 68cb8f6..08e4058 100644 --- a/samples/bitmap-plasma-llvm/jni/Android.mk +++ b/samples/bitmap-plasma-llvm/jni/Android.mk @@ -6,7 +6,7 @@ LOCAL_MODULE := libplasma LOCAL_MODULE_TAGS := optional -LOCAL_SRC_FILES := plasmaLLVM.cpp +LOCAL_SRC_FILES := plasma.cpp LOCAL_C_INCLUDES := $(OUT)/../../../../frameworks/compile/libbcc/include diff --git a/samples/bitmap-plasma-llvm/jni/Application.mk b/samples/bitmap-plasma-llvm/jni/Application.mk index 59fdfab..5b6cab6 100644 --- a/samples/bitmap-plasma-llvm/jni/Application.mk +++ b/samples/bitmap-plasma-llvm/jni/Application.mk @@ -1,3 +1,5 @@ # The ARMv7 is significanly faster due to the use of the hardware FPU -APP_ABI := armeabi armeabi-v7a -APP_PLATFORM := android-12 +APP_ABI := armeabi armeabi-v7a +#APP_ABI := mips mips-r2 +#APP_ABI := x86 +APP_PLATFORM := android-8 diff --git a/samples/bitmap-plasma-llvm/jni/libplasma.c b/samples/bitmap-plasma-llvm/jni/libplasma.c deleted file mode 100644 index 1922bfe..0000000 --- a/samples/bitmap-plasma-llvm/jni/libplasma.c +++ /dev/null @@ -1,224 +0,0 @@ -#include -#include -#include -#include - -/* Set to 1 to enable debug log traces. */ -#define DEBUG 0 - -/* Set to 1 to optimize memory stores when generating plasma. */ -#define OPTIMIZE_WRITES 1 - -/* We're going to perform computations for every pixel of the target - * bitmap. floating-point operations are very slow on ARMv5, and not - * too bad on ARMv7 with the exception of trigonometric functions. - * - * For better performance on all platforms, we're going to use fixed-point - * arithmetic and all kinds of tricks - */ - -typedef int32_t Fixed; - -#define FIXED_BITS 16 -#define FIXED_ONE (1 << FIXED_BITS) -#define FIXED_AVERAGE(x,y) (((x) + (y)) >> 1) - -#define FIXED_FROM_INT(x) ((x) << FIXED_BITS) -#define FIXED_TO_INT(x) ((x) >> FIXED_BITS) - -#define FIXED_FROM_FLOAT(x) ((Fixed)((x)*FIXED_ONE)) -#define FIXED_TO_FLOAT(x) ((x)/(1.*FIXED_ONE)) - -#define FIXED_MUL(x,y) (((int64_t)(x) * (y)) >> FIXED_BITS) -#define FIXED_DIV(x,y) (((int64_t)(x) * FIXED_ONE) / (y)) - -#define FIXED_DIV2(x) ((x) >> 1) -#define FIXED_AVERAGE(x,y) (((x) + (y)) >> 1) - -#define FIXED_FRAC(x) ((x) & ((1 << FIXED_BITS)-1)) -#define FIXED_TRUNC(x) ((x) & ~((1 << FIXED_BITS)-1)) - -#define FIXED_FROM_INT_FLOAT(x,f) (Fixed)((x)*(FIXED_ONE*(f))) - -typedef int32_t Angle; - -#define ANGLE_BITS 9 - -#if ANGLE_BITS < 8 -# error ANGLE_BITS must be at least 8 -#endif - -#define ANGLE_2PI (1 << ANGLE_BITS) -#define ANGLE_PI (1 << (ANGLE_BITS-1)) -#define ANGLE_PI2 (1 << (ANGLE_BITS-2)) -#define ANGLE_PI4 (1 << (ANGLE_BITS-3)) - -#define ANGLE_FROM_FLOAT(x) (Angle)((x)*ANGLE_PI/M_PI) -#define ANGLE_TO_FLOAT(x) ((x)*M_PI/ANGLE_PI) - -#if ANGLE_BITS <= FIXED_BITS -# define ANGLE_FROM_FIXED(x) (Angle)((x) >> (FIXED_BITS - ANGLE_BITS)) -# define ANGLE_TO_FIXED(x) (Fixed)((x) << (FIXED_BITS - ANGLE_BITS)) -#else -# define ANGLE_FROM_FIXED(x) (Angle)((x) << (ANGLE_BITS - FIXED_BITS)) -# define ANGLE_TO_FIXED(x) (Fixed)((x) >> (ANGLE_BITS - FIXED_BITS)) -#endif - -static Fixed *angle_sin_tab; -//static Fixed angle_sin_tab[ANGLE_2PI+1]; - -static __inline__ Fixed angle_sin( Angle a ) -{ - return angle_sin_tab[(uint32_t)a & (ANGLE_2PI-1)]; -} - -static __inline__ Fixed angle_cos( Angle a ) -{ - return angle_sin(a + ANGLE_PI2); -} - -static __inline__ Fixed fixed_sin( Fixed f ) -{ - return angle_sin(ANGLE_FROM_FIXED(f)); -} - -static __inline__ Fixed fixed_cos( Fixed f ) -{ - return angle_cos(ANGLE_FROM_FIXED(f)); -} - -/* Color palette used for rendering the plasma */ -#define PALETTE_BITS 8 -#define PALETTE_SIZE (1 << PALETTE_BITS) - -#if PALETTE_BITS > FIXED_BITS -# error PALETTE_BITS must be smaller than FIXED_BITS -#endif - -#if 0 -static uint16_t make565(int red, int green, int blue) -{ - return (uint16_t)( ((red << 8) & 0xf800) | - ((green << 2) & 0x03e0) | - ((blue >> 3) & 0x001f) ); -} - -static void init_palette(uint16_t *palette) -{ - int nn, mm = 0; - /* fun with colors */ - for (nn = 0; nn < PALETTE_SIZE/4; nn++) { - int jj = (nn-mm)*4*255/PALETTE_SIZE; - palette[nn] = make565(255, jj, 255-jj); - } - - for ( mm = nn; nn < PALETTE_SIZE/2; nn++ ) { - int jj = (nn-mm)*4*255/PALETTE_SIZE; - palette[nn] = make565(255-jj, 255, jj); - } - - for ( mm = nn; nn < PALETTE_SIZE*3/4; nn++ ) { - int jj = (nn-mm)*4*255/PALETTE_SIZE; - palette[nn] = make565(0, 255-jj, 255); - } - - for ( mm = nn; nn < PALETTE_SIZE; nn++ ) { - int jj = (nn-mm)*4*255/PALETTE_SIZE; - palette[nn] = make565(jj, 0, 255); - } -} -#endif -static __inline__ uint16_t palette_from_fixed(uint16_t* palette, Fixed x ) -{ - if (x < 0) x = -x; - if (x >= FIXED_ONE) x = FIXED_ONE-1; - int idx = FIXED_FRAC(x) >> (FIXED_BITS - PALETTE_BITS); - return palette[idx & (PALETTE_SIZE-1)]; -} - - -extern void root(uint32_t width, uint32_t height, uint32_t stride, double t, uint16_t* palette, void* pixels, void *_angle_sin_tab) -{ - angle_sin_tab = _angle_sin_tab; - Fixed ft = FIXED_FROM_FLOAT(t/1000.); - Fixed yt1 = FIXED_FROM_FLOAT(t/1230.); - Fixed yt2 = yt1; - Fixed xt10 = FIXED_FROM_FLOAT(t/3000.); - Fixed xt20 = xt10; - -#define YT1_INCR FIXED_FROM_FLOAT(1/100.) -#define YT2_INCR FIXED_FROM_FLOAT(1/163.) - - int yy; - for (yy = 0; yy < height; yy++) { - uint16_t* line = (uint16_t*)pixels; - Fixed base = fixed_sin(yt1) + fixed_sin(yt2); - Fixed xt1 = xt10; - Fixed xt2 = xt20; - - yt1 += YT1_INCR; - yt2 += YT2_INCR; - -#define XT1_INCR FIXED_FROM_FLOAT(1/173.) -#define XT2_INCR FIXED_FROM_FLOAT(1/242.) - - uint16_t* line_end = line + width; - - if (line < line_end) { - if (((uint32_t)line & 3) != 0) { - Fixed ii = base + fixed_sin(xt1) + fixed_sin(xt2); - - xt1 += XT1_INCR; - xt2 += XT2_INCR; - - line[0] = palette_from_fixed(palette, ii >> 2); - line++; - } - - while (line + 2 <= line_end) { - Fixed i1 = base + fixed_sin(xt1) + fixed_sin(xt2); - xt1 += XT1_INCR; - xt2 += XT2_INCR; - - Fixed i2 = base + fixed_sin(xt1) + fixed_sin(xt2); - xt1 += XT1_INCR; - xt2 += XT2_INCR; - - uint32_t pixel = ((uint32_t)palette_from_fixed(palette, i1 >> 2) << 16) | - (uint32_t)palette_from_fixed(palette, i2 >> 2); - - ((uint32_t*)line)[0] = pixel; - line += 2; - } - - if (line < line_end) { - Fixed ii = base + fixed_sin(xt1) + fixed_sin(xt2); - line[0] = palette_from_fixed(palette, ii >> 2); - line++; - } - } - - // go to next line - pixels = (char*)pixels + stride; - } -} - -/* simple stats management */ -typedef struct { - double renderTime; - double frameTime; -} FrameStats; - -#define MAX_FRAME_STATS 200 -#define MAX_PERIOD_MS 1500 - -typedef struct { - double firstTime; - double lastTime; - double frameTime; - - int firstFrame; - int numFrames; - FrameStats frames[ MAX_FRAME_STATS ]; -} Stats; - diff --git a/samples/bitmap-plasma-llvm/jni/plasma.cpp b/samples/bitmap-plasma-llvm/jni/plasma.cpp new file mode 100644 index 0000000..d05990f --- /dev/null +++ b/samples/bitmap-plasma-llvm/jni/plasma.cpp @@ -0,0 +1,514 @@ +/* + * Copyright (C) 2010 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 + +#if !defined(__GDK__) && !defined(__NOGDK__) +#include +#include +#endif // !__GDK__ && !__NOGDK__ + +#if !defined(__GDK__) +#include +#include +#include +#endif // !__GDK__ + +#include + +#define LOG_TAG "libplasma" +#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) +#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) + +/* Set to 1 to enable debug log traces. */ +#define DEBUG 0 + +/* Set to 1 to optimize memory stores when generating plasma. */ +#define OPTIMIZE_WRITES 1 + + +/* We're going to perform computations for every pixel of the target + * bitmap. floating-point operations are very slow on ARMv5, and not + * too bad on ARMv7 with the exception of trigonometric functions. + * + * For better performance on all platforms, we're going to use fixed-point + * arithmetic and all kinds of tricks + */ + +typedef int32_t Fixed; + +#define FIXED_BITS 16 +#define FIXED_ONE (1 << FIXED_BITS) +#define FIXED_AVERAGE(x,y) (((x) + (y)) >> 1) + +#define FIXED_FROM_INT(x) ((x) << FIXED_BITS) +#define FIXED_TO_INT(x) ((x) >> FIXED_BITS) + +#define FIXED_FROM_FLOAT(x) ((Fixed)((x)*FIXED_ONE)) +#define FIXED_TO_FLOAT(x) ((x)/(1.*FIXED_ONE)) + +#define FIXED_MUL(x,y) (((int64_t)(x) * (y)) >> FIXED_BITS) +#define FIXED_DIV(x,y) (((int64_t)(x) * FIXED_ONE) / (y)) + +#define FIXED_DIV2(x) ((x) >> 1) +#define FIXED_AVERAGE(x,y) (((x) + (y)) >> 1) + +#define FIXED_FRAC(x) ((x) & ((1 << FIXED_BITS)-1)) +#define FIXED_TRUNC(x) ((x) & ~((1 << FIXED_BITS)-1)) + +#define FIXED_FROM_INT_FLOAT(x,f) (Fixed)((x)*(FIXED_ONE*(f))) + +typedef int32_t Angle; + +#define ANGLE_BITS 9 + +#if ANGLE_BITS < 8 +# error ANGLE_BITS must be at least 8 +#endif + +#define ANGLE_2PI (1 << ANGLE_BITS) +#define ANGLE_PI (1 << (ANGLE_BITS-1)) +#define ANGLE_PI2 (1 << (ANGLE_BITS-2)) +#define ANGLE_PI4 (1 << (ANGLE_BITS-3)) + +#define ANGLE_FROM_FLOAT(x) (Angle)((x)*ANGLE_PI/M_PI) +#define ANGLE_TO_FLOAT(x) ((x)*M_PI/ANGLE_PI) + +#if ANGLE_BITS <= FIXED_BITS +# define ANGLE_FROM_FIXED(x) (Angle)((x) >> (FIXED_BITS - ANGLE_BITS)) +# define ANGLE_TO_FIXED(x) (Fixed)((x) << (FIXED_BITS - ANGLE_BITS)) +#else +# define ANGLE_FROM_FIXED(x) (Angle)((x) << (ANGLE_BITS - FIXED_BITS)) +# define ANGLE_TO_FIXED(x) (Fixed)((x) >> (ANGLE_BITS - FIXED_BITS)) +#endif + +#if defined(__GDK__) +static Fixed *angle_sin_tab; +#else +static Fixed angle_sin_tab[ANGLE_2PI+1]; +#endif // !__GDK__ + +static void init_angles(void) +{ + int nn; + for (nn = 0; nn < ANGLE_2PI+1; nn++) { + double radians = nn*M_PI/ANGLE_PI; + angle_sin_tab[nn] = FIXED_FROM_FLOAT(sin(radians)); + } +} + +static __inline__ Fixed angle_sin( Angle a ) +{ + return angle_sin_tab[(uint32_t)a & (ANGLE_2PI-1)]; +} + +static __inline__ Fixed angle_cos( Angle a ) +{ + return angle_sin(a + ANGLE_PI2); +} + +static __inline__ Fixed fixed_sin( Fixed f ) +{ + return angle_sin(ANGLE_FROM_FIXED(f)); +} + +static __inline__ Fixed fixed_cos( Fixed f ) +{ + return angle_cos(ANGLE_FROM_FIXED(f)); +} + +/* Color palette used for rendering the plasma */ +#define PALETTE_BITS 8 +#define PALETTE_SIZE (1 << PALETTE_BITS) + +#if PALETTE_BITS > FIXED_BITS +# error PALETTE_BITS must be smaller than FIXED_BITS +#endif + +static uint16_t palette[PALETTE_SIZE]; + +static uint16_t make565(int red, int green, int blue) +{ + return (uint16_t)( ((red << 8) & 0xf800) | + ((green << 2) & 0x03e0) | + ((blue >> 3) & 0x001f) ); +} + +static void init_palette(void) +{ + int nn, mm = 0; + /* fun with colors */ + for (nn = 0; nn < PALETTE_SIZE/4; nn++) { + int jj = (nn-mm)*4*255/PALETTE_SIZE; + palette[nn] = make565(255, jj, 255-jj); + } + + for ( mm = nn; nn < PALETTE_SIZE/2; nn++ ) { + int jj = (nn-mm)*4*255/PALETTE_SIZE; + palette[nn] = make565(255-jj, 255, jj); + } + + for ( mm = nn; nn < PALETTE_SIZE*3/4; nn++ ) { + int jj = (nn-mm)*4*255/PALETTE_SIZE; + palette[nn] = make565(0, 255-jj, 255); + } + + for ( mm = nn; nn < PALETTE_SIZE; nn++ ) { + int jj = (nn-mm)*4*255/PALETTE_SIZE; + palette[nn] = make565(jj, 0, 255); + } +} + +static __inline__ uint16_t palette_from_fixed( uint16_t* palette, Fixed x ) +{ + if (x < 0) x = -x; + if (x >= FIXED_ONE) x = FIXED_ONE-1; + int idx = FIXED_FRAC(x) >> (FIXED_BITS - PALETTE_BITS); + return palette[idx & (PALETTE_SIZE-1)]; +} + +/* Angles expressed as fixed point radians */ + +static void init_tables(void) +{ + init_palette(); + init_angles(); +} + + +extern "C" void fill_plasma( + uint32_t width, uint32_t height, uint32_t stride, double t, uint16_t* palette, void* pixels, Fixed *_angle_sin_tab ) +{ + #if defined(__GDK__) + angle_sin_tab = _angle_sin_tab; + #endif // !__GDK__ + Fixed ft = FIXED_FROM_FLOAT(t/1000.); + Fixed yt1 = FIXED_FROM_FLOAT(t/1230.); + Fixed yt2 = yt1; + Fixed xt10 = FIXED_FROM_FLOAT(t/3000.); + Fixed xt20 = xt10; + +#define YT1_INCR FIXED_FROM_FLOAT(1/100.) +#define YT2_INCR FIXED_FROM_FLOAT(1/163.) + + int yy; + for (yy = 0; yy < height; yy++) { + uint16_t* line = (uint16_t*)pixels; + Fixed base = fixed_sin(yt1) + fixed_sin(yt2); + Fixed xt1 = xt10; + Fixed xt2 = xt20; + + yt1 += YT1_INCR; + yt2 += YT2_INCR; + +#define XT1_INCR FIXED_FROM_FLOAT(1/173.) +#define XT2_INCR FIXED_FROM_FLOAT(1/242.) + +#if OPTIMIZE_WRITES + /* optimize memory writes by generating one aligned 32-bit store + * for every pair of pixels. + */ + uint16_t* line_end = line + width; + + if (line < line_end) { + if (((uint32_t)line & 3) != 0) { + Fixed ii = base + fixed_sin(xt1) + fixed_sin(xt2); + + xt1 += XT1_INCR; + xt2 += XT2_INCR; + + line[0] = palette_from_fixed(palette, ii >> 2); + line++; + } + + while (line + 2 <= line_end) { + Fixed i1 = base + fixed_sin(xt1) + fixed_sin(xt2); + xt1 += XT1_INCR; + xt2 += XT2_INCR; + + Fixed i2 = base + fixed_sin(xt1) + fixed_sin(xt2); + xt1 += XT1_INCR; + xt2 += XT2_INCR; + + uint32_t pixel = ((uint32_t)palette_from_fixed(palette, i1 >> 2) << 16) | + (uint32_t)palette_from_fixed(palette, i2 >> 2); + + ((uint32_t*)line)[0] = pixel; + line += 2; + } + + if (line < line_end) { + Fixed ii = base + fixed_sin(xt1) + fixed_sin(xt2); + line[0] = palette_from_fixed(palette, ii >> 2); + line++; + } + } +#else /* !OPTIMIZE_WRITES */ + int xx; + for (xx = 0; xx < width; xx++) { + + Fixed ii = base + fixed_sin(xt1) + fixed_sin(xt2); + + xt1 += XT1_INCR; + xt2 += XT2_INCR; + + line[xx] = palette_from_fixed(palette, ii / 4); + } +#endif /* !OPTIMIZE_WRITES */ + + // go to next line + pixels = (char*)pixels + stride; + } +} + +#if !defined(__GDK__) + +/* Return current time in milliseconds */ +static double now_ms(void) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec*1000. + tv.tv_usec/1000.; +} + +/* simple stats management */ +typedef struct { + double renderTime; + double frameTime; +} FrameStats; + +#define MAX_FRAME_STATS 200 +#define MAX_PERIOD_MS 1500 + +typedef struct { + double firstTime; + double lastTime; + double frameTime; + + int firstFrame; + int numFrames; + FrameStats frames[ MAX_FRAME_STATS ]; +} Stats; + +static void +stats_init( Stats* s ) +{ + s->lastTime = now_ms(); + s->firstTime = 0.; + s->firstFrame = 0; + s->numFrames = 0; +} + +static void +stats_startFrame( Stats* s ) +{ + s->frameTime = now_ms(); +} + +static void +stats_endFrame( Stats* s ) +{ + double now = now_ms(); + double renderTime = now - s->frameTime; + double frameTime = now - s->lastTime; + int nn; + + if (now - s->firstTime >= MAX_PERIOD_MS) { + if (s->numFrames > 0) { + double minRender, maxRender, avgRender; + double minFrame, maxFrame, avgFrame; + int count; + + nn = s->firstFrame; + minRender = maxRender = avgRender = s->frames[nn].renderTime; + minFrame = maxFrame = avgFrame = s->frames[nn].frameTime; + for (count = s->numFrames; count > 0; count-- ) { + nn += 1; + if (nn >= MAX_FRAME_STATS) + nn -= MAX_FRAME_STATS; + double render = s->frames[nn].renderTime; + if (render < minRender) minRender = render; + if (render > maxRender) maxRender = render; + double frame = s->frames[nn].frameTime; + if (frame < minFrame) minFrame = frame; + if (frame > maxFrame) maxFrame = frame; + avgRender += render; + avgFrame += frame; + } + avgRender /= s->numFrames; + avgFrame /= s->numFrames; + + LOGI("frame/s (avg,min,max) = (%.1f,%.1f,%.1f) " + "render time ms (avg,min,max) = (%.1f,%.1f,%.1f)\n", + 1000./avgFrame, 1000./maxFrame, 1000./minFrame, + avgRender, minRender, maxRender); + } + s->numFrames = 0; + s->firstFrame = 0; + s->firstTime = now; + } + + nn = s->firstFrame + s->numFrames; + if (nn >= MAX_FRAME_STATS) + nn -= MAX_FRAME_STATS; + + s->frames[nn].renderTime = renderTime; + s->frames[nn].frameTime = frameTime; + + if (s->numFrames < MAX_FRAME_STATS) { + s->numFrames += 1; + } else { + s->firstFrame += 1; + if (s->firstFrame >= MAX_FRAME_STATS) + s->firstFrame -= MAX_FRAME_STATS; + } + + s->lastTime = now; +} + +typedef void (*pPlasmaType)(uint32_t, uint32_t, uint32_t, double, uint16_t*, void*, Fixed*); + +#if !defined(__GDK__) && !defined(__NOGDK__) +static void* lookupSymbol(void* pContext, const char* name) +{ + return (void*) dlsym(RTLD_DEFAULT, name); +} +#endif // !__GDK__ && !__NOGDK__ + +extern "C" JNIEXPORT jboolean JNICALL Java_com_example_plasma_llvm_PlasmaView_gdk(JNIEnv *env, jobject obj) +{ +#if !defined(__NOGDK__) + return JNI_TRUE; +#else + return JNI_FALSE; +#endif +} + +extern "C" JNIEXPORT jint JNICALL Java_com_example_plasma_llvm_PlasmaView_nativeRenderPlasma + (JNIEnv * env, jobject obj, + jobject bitmap, jlong time_ms, jbyteArray scriptRef, jint length, jboolean use_llvm) +{ + AndroidBitmapInfo info; + void* pixels; + int ret; + static Stats stats; + static int init; + static double time_sum = 0; + static int count = 0; +#if !defined(__NOGDK__) + static bool last_mode = false; + static pPlasmaType native_function = NULL; + static BCCScriptRef script_ref; + + if (last_mode != use_llvm) + count = 0, time_sum = 0; + last_mode = use_llvm; +#endif // !__NOGDK__ + + if (!init) { + init_tables(); + stats_init(&stats); + init = 1; + } + + if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0) { + LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret); + return -1; + } + + if (info.format != ANDROID_BITMAP_FORMAT_RGB_565) { + LOGE("Bitmap format is not RGB_565 !"); + return -1; + } + + if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixels)) < 0) { + LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret); + } + +#if !defined(__NOGDK__) + if (use_llvm) { + double start_jit = now_ms(); + + if (native_function == NULL) { + script_ref = bccCreateScript(); + + jbyte* script_ptr = (jbyte *)env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0); + + LOGI("BCC Script Len: %d", length); + if(bccReadBC(script_ref, "libplasma_portable.bc", (const char*)script_ptr, length, 0)) { + LOGE("Error! Cannot bccReadBc"); + return -1; + } + if (script_ptr) { + env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr, 0); + } + + #if 0 + if (bccLinkFile(script_ref, "/system/lib/libclcore.bc", 0)) { + LOGE("Error! Cannot bccLinkBC"); + return -1; + } + #endif + + bccRegisterSymbolCallback(script_ref, lookupSymbol, NULL); + + #ifdef OLD_BCC + if (bccPrepareExecutable(script_ref, "/data/data/com.example.plasma.llvm/plasmaLLVM.oBCC", 0)) { + LOGE("Error! Cannot bccPrepareExecutable"); + return -1; + } + #else + if (bccPrepareExecutable(script_ref, "/data/data/com.example.plasma.llvm/", "plasmaLLVM", 0)) { + LOGE("Error! Cannot bccPrepareExecutable"); + return -1; + } + #endif // OLD_BCC + + native_function = (pPlasmaType)bccGetFuncAddr(script_ref, "fill_plasma"); + if (native_function == NULL) { + LOGE("Error! Cannot find fill_plasma()"); + return -1; + } + } + + double start_run = now_ms(); + native_function(info.width, info.height, info.stride, time_ms, palette, pixels, angle_sin_tab); + double diff = now_ms()-start_run; + if (((count+1) % 30) == 0) + LOGI("LLVM Time JIT: %.2lf , Run: %.2lf, Avg: %.2lf, count=%d", start_run-start_jit, diff, time_sum / count, count+1); + time_sum += diff + start_run - start_jit; + } + else +#endif // !__NOGDK__ + + { + double start_run = now_ms(); + fill_plasma(info.width, info.height, info.stride, time_ms, palette, pixels, angle_sin_tab); + double diff = now_ms()-start_run; + if (((count+1) % 30) == 0) + LOGI("GCC Time Run: %.2lf, Avg: %.2lf, count=%d", diff, time_sum / count, count+1); + time_sum += diff; + } + count++; + + AndroidBitmap_unlockPixels(env, bitmap); + + return count * 1000.0 / time_sum; +} + +#endif // !__GDK diff --git a/samples/bitmap-plasma-llvm/jni/plasmaLLVM.cpp b/samples/bitmap-plasma-llvm/jni/plasmaLLVM.cpp deleted file mode 100644 index 04dbbbe..0000000 --- a/samples/bitmap-plasma-llvm/jni/plasmaLLVM.cpp +++ /dev/null @@ -1,458 +0,0 @@ -/* - * Copyright (C) 2010 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 "bcc/bcc.h" - -#define LOG_TAG "libplasma" -#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) -#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) - -/* Set to 1 to enable debug log traces. */ -#define DEBUG 0 - -/* Set to 1 to optimize memory stores when generating plasma. */ -#define OPTIMIZE_WRITES 1 - -/* Return current time in milliseconds */ -static double now_ms(void) -{ - struct timeval tv; - gettimeofday(&tv, NULL); - return tv.tv_sec*1000. + tv.tv_usec/1000.; -} - -/* We're going to perform computations for every pixel of the target - * bitmap. floating-point operations are very slow on ARMv5, and not - * too bad on ARMv7 with the exception of trigonometric functions. - * - * For better performance on all platforms, we're going to use fixed-point - * arithmetic and all kinds of tricks - */ - -typedef int32_t Fixed; - -#define FIXED_BITS 16 -#define FIXED_ONE (1 << FIXED_BITS) -#define FIXED_AVERAGE(x,y) (((x) + (y)) >> 1) - -#define FIXED_FROM_INT(x) ((x) << FIXED_BITS) -#define FIXED_TO_INT(x) ((x) >> FIXED_BITS) - -#define FIXED_FROM_FLOAT(x) ((Fixed)((x)*FIXED_ONE)) -#define FIXED_TO_FLOAT(x) ((x)/(1.*FIXED_ONE)) - -#define FIXED_MUL(x,y) (((int64_t)(x) * (y)) >> FIXED_BITS) -#define FIXED_DIV(x,y) (((int64_t)(x) * FIXED_ONE) / (y)) - -#define FIXED_DIV2(x) ((x) >> 1) -#define FIXED_AVERAGE(x,y) (((x) + (y)) >> 1) - -#define FIXED_FRAC(x) ((x) & ((1 << FIXED_BITS)-1)) -#define FIXED_TRUNC(x) ((x) & ~((1 << FIXED_BITS)-1)) - -#define FIXED_FROM_INT_FLOAT(x,f) (Fixed)((x)*(FIXED_ONE*(f))) - -typedef int32_t Angle; - -#define ANGLE_BITS 9 - -#if ANGLE_BITS < 8 -# error ANGLE_BITS must be at least 8 -#endif - -#define ANGLE_2PI (1 << ANGLE_BITS) -#define ANGLE_PI (1 << (ANGLE_BITS-1)) -#define ANGLE_PI2 (1 << (ANGLE_BITS-2)) -#define ANGLE_PI4 (1 << (ANGLE_BITS-3)) - -#define ANGLE_FROM_FLOAT(x) (Angle)((x)*ANGLE_PI/M_PI) -#define ANGLE_TO_FLOAT(x) ((x)*M_PI/ANGLE_PI) - -#if ANGLE_BITS <= FIXED_BITS -# define ANGLE_FROM_FIXED(x) (Angle)((x) >> (FIXED_BITS - ANGLE_BITS)) -# define ANGLE_TO_FIXED(x) (Fixed)((x) << (FIXED_BITS - ANGLE_BITS)) -#else -# define ANGLE_FROM_FIXED(x) (Angle)((x) << (ANGLE_BITS - FIXED_BITS)) -# define ANGLE_TO_FIXED(x) (Fixed)((x) >> (ANGLE_BITS - FIXED_BITS)) -#endif - -static Fixed angle_sin_tab[ANGLE_2PI+1]; - -static void init_angles(void) -{ - int nn; - for (nn = 0; nn < ANGLE_2PI+1; nn++) { - double radians = nn*M_PI/ANGLE_PI; - angle_sin_tab[nn] = FIXED_FROM_FLOAT(sin(radians)); - } -} - -static __inline__ Fixed angle_sin( Angle a ) -{ - return angle_sin_tab[(uint32_t)a & (ANGLE_2PI-1)]; -} - -static __inline__ Fixed angle_cos( Angle a ) -{ - return angle_sin(a + ANGLE_PI2); -} - -static __inline__ Fixed fixed_sin( Fixed f ) -{ - return angle_sin(ANGLE_FROM_FIXED(f)); -} - -static __inline__ Fixed fixed_cos( Fixed f ) -{ - return angle_cos(ANGLE_FROM_FIXED(f)); -} - -/* Color palette used for rendering the plasma */ -#define PALETTE_BITS 8 -#define PALETTE_SIZE (1 << PALETTE_BITS) - -#if PALETTE_BITS > FIXED_BITS -# error PALETTE_BITS must be smaller than FIXED_BITS -#endif - -static uint16_t palette[PALETTE_SIZE]; - -static uint16_t make565(int red, int green, int blue) -{ - return (uint16_t)( ((red << 8) & 0xf800) | - ((green << 2) & 0x03e0) | - ((blue >> 3) & 0x001f) ); -} - -static void init_palette(void) -{ - int nn, mm = 0; - /* fun with colors */ - for (nn = 0; nn < PALETTE_SIZE/4; nn++) { - int jj = (nn-mm)*4*255/PALETTE_SIZE; - palette[nn] = make565(255, jj, 255-jj); - } - - for ( mm = nn; nn < PALETTE_SIZE/2; nn++ ) { - int jj = (nn-mm)*4*255/PALETTE_SIZE; - palette[nn] = make565(255-jj, 255, jj); - } - - for ( mm = nn; nn < PALETTE_SIZE*3/4; nn++ ) { - int jj = (nn-mm)*4*255/PALETTE_SIZE; - palette[nn] = make565(0, 255-jj, 255); - } - - for ( mm = nn; nn < PALETTE_SIZE; nn++ ) { - int jj = (nn-mm)*4*255/PALETTE_SIZE; - palette[nn] = make565(jj, 0, 255); - } -} - -static __inline__ uint16_t palette_from_fixed( Fixed x ) -{ - if (x < 0) x = -x; - if (x >= FIXED_ONE) x = FIXED_ONE-1; - int idx = FIXED_FRAC(x) >> (FIXED_BITS - PALETTE_BITS); - return palette[idx & (PALETTE_SIZE-1)]; -} - -/* Angles expressed as fixed point radians */ - -static void init_tables(void) -{ - init_palette(); - init_angles(); -} - -static void fill_plasma( AndroidBitmapInfo* info, void* pixels, double t ) -{ - Fixed ft = FIXED_FROM_FLOAT(t/1000.); - Fixed yt1 = FIXED_FROM_FLOAT(t/1230.); - Fixed yt2 = yt1; - Fixed xt10 = FIXED_FROM_FLOAT(t/3000.); - Fixed xt20 = xt10; - -#define YT1_INCR FIXED_FROM_FLOAT(1/100.) -#define YT2_INCR FIXED_FROM_FLOAT(1/163.) - - int yy; - for (yy = 0; yy < info->height; yy++) { - uint16_t* line = (uint16_t*)pixels; - Fixed base = fixed_sin(yt1) + fixed_sin(yt2); - Fixed xt1 = xt10; - Fixed xt2 = xt20; - - yt1 += YT1_INCR; - yt2 += YT2_INCR; - -#define XT1_INCR FIXED_FROM_FLOAT(1/173.) -#define XT2_INCR FIXED_FROM_FLOAT(1/242.) - -#if OPTIMIZE_WRITES - /* optimize memory writes by generating one aligned 32-bit store - * for every pair of pixels. - */ - uint16_t* line_end = line + info->width; - - if (line < line_end) { - if (((uint32_t)line & 3) != 0) { - Fixed ii = base + fixed_sin(xt1) + fixed_sin(xt2); - - xt1 += XT1_INCR; - xt2 += XT2_INCR; - - line[0] = palette_from_fixed(ii >> 2); - line++; - } - - while (line + 2 <= line_end) { - Fixed i1 = base + fixed_sin(xt1) + fixed_sin(xt2); - xt1 += XT1_INCR; - xt2 += XT2_INCR; - - Fixed i2 = base + fixed_sin(xt1) + fixed_sin(xt2); - xt1 += XT1_INCR; - xt2 += XT2_INCR; - - uint32_t pixel = ((uint32_t)palette_from_fixed(i1 >> 2) << 16) | - (uint32_t)palette_from_fixed(i2 >> 2); - - ((uint32_t*)line)[0] = pixel; - line += 2; - } - - if (line < line_end) { - Fixed ii = base + fixed_sin(xt1) + fixed_sin(xt2); - line[0] = palette_from_fixed(ii >> 2); - line++; - } - } -#else /* !OPTIMIZE_WRITES */ - int xx; - for (xx = 0; xx < info->width; xx++) { - - Fixed ii = base + fixed_sin(xt1) + fixed_sin(xt2); - - xt1 += XT1_INCR; - xt2 += XT2_INCR; - - line[xx] = palette_from_fixed(ii / 4); - } -#endif /* !OPTIMIZE_WRITES */ - - // go to next line - pixels = (char*)pixels + info->stride; - } -} - -/* simple stats management */ -typedef struct { - double renderTime; - double frameTime; -} FrameStats; - -#define MAX_FRAME_STATS 200 -#define MAX_PERIOD_MS 1500 - -typedef struct { - double firstTime; - double lastTime; - double frameTime; - - int firstFrame; - int numFrames; - FrameStats frames[ MAX_FRAME_STATS ]; -} Stats; - -static void -stats_init( Stats* s ) -{ - s->lastTime = now_ms(); - s->firstTime = 0.; - s->firstFrame = 0; - s->numFrames = 0; -} - -static void -stats_startFrame( Stats* s ) -{ - s->frameTime = now_ms(); -} - -static void -stats_endFrame( Stats* s ) -{ - double now = now_ms(); - double renderTime = now - s->frameTime; - double frameTime = now - s->lastTime; - int nn; - - if (now - s->firstTime >= MAX_PERIOD_MS) { - if (s->numFrames > 0) { - double minRender, maxRender, avgRender; - double minFrame, maxFrame, avgFrame; - int count; - - nn = s->firstFrame; - minRender = maxRender = avgRender = s->frames[nn].renderTime; - minFrame = maxFrame = avgFrame = s->frames[nn].frameTime; - for (count = s->numFrames; count > 0; count-- ) { - nn += 1; - if (nn >= MAX_FRAME_STATS) - nn -= MAX_FRAME_STATS; - double render = s->frames[nn].renderTime; - if (render < minRender) minRender = render; - if (render > maxRender) maxRender = render; - double frame = s->frames[nn].frameTime; - if (frame < minFrame) minFrame = frame; - if (frame > maxFrame) maxFrame = frame; - avgRender += render; - avgFrame += frame; - } - avgRender /= s->numFrames; - avgFrame /= s->numFrames; - - LOGI("frame/s (avg,min,max) = (%.1f,%.1f,%.1f) " - "render time ms (avg,min,max) = (%.1f,%.1f,%.1f)\n", - 1000./avgFrame, 1000./maxFrame, 1000./minFrame, - avgRender, minRender, maxRender); - } - s->numFrames = 0; - s->firstFrame = 0; - s->firstTime = now; - } - - nn = s->firstFrame + s->numFrames; - if (nn >= MAX_FRAME_STATS) - nn -= MAX_FRAME_STATS; - - s->frames[nn].renderTime = renderTime; - s->frames[nn].frameTime = frameTime; - - if (s->numFrames < MAX_FRAME_STATS) { - s->numFrames += 1; - } else { - s->firstFrame += 1; - if (s->firstFrame >= MAX_FRAME_STATS) - s->firstFrame -= MAX_FRAME_STATS; - } - - s->lastTime = now; -} - -typedef void (*pPlasmaType)(uint32_t, uint32_t, uint32_t, double, uint16_t*, void*, void*); - -extern "C" JNIEXPORT jint JNICALL Java_com_example_plasma_llvm_PlasmaView_nativeRenderPlasma - (JNIEnv * env, jobject obj, - jobject bitmap, jlong time_ms, jbyteArray scriptRef, jint length, jboolean use_llvm) -{ - AndroidBitmapInfo info; - void* pixels; - int ret; - static Stats stats; - static int init; - static double time_sum = 0; - static int count = 0; - static bool last_mode = false; - static pPlasmaType native_function = NULL; - static BCCScriptRef script_ref; - - if (last_mode != use_llvm) - count = 0, time_sum = 0; - last_mode = use_llvm; - - if (!init) { - init_tables(); - stats_init(&stats); - init = 1; - } - - if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0) { - LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret); - return -1; - } - - if (info.format != ANDROID_BITMAP_FORMAT_RGB_565) { - LOGE("Bitmap format is not RGB_565 !"); - return -1; - } - - if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixels)) < 0) { - LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret); - } - - - if (use_llvm) { - double start_jit = now_ms(); - - if (native_function == NULL) { - script_ref = bccCreateScript(); - - jbyte* script_ptr = (jbyte *)env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0); - - LOGI("BCC Script Len: %d", length); - if(bccReadBC(script_ref, "libplasma_portable.bc", (const char*)script_ptr, length, 0)) { - LOGE("Error! Cannot bccReadBc"); - return -1; - } - if (script_ptr) { - env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr, 0); - } - - if (bccLinkFile(script_ref, "/system/lib/libclcore.bc", 0)) { - LOGE("Error! Cannot bccLinkBC"); - return -1; - } - - if (bccPrepareExecutable(script_ref, "/data/data/com.example.plasma.llvm/", "plasmaLLVM", 0)) { - LOGE("Error! Cannot bccPrepareExecutable"); - return -1; - } - native_function = (pPlasmaType)bccGetFuncAddr(script_ref, "root"); - if (native_function == NULL) { - LOGE("Error! Cannot find fill_plasma()"); - return -1; - } - } - - double start_run = now_ms(); - native_function(info.width, info.height, info.stride, time_ms, palette, pixels, angle_sin_tab); - double diff = now_ms()-start_run; - LOGI("LLVM Time JIT: %.2lf , Run: %.2lf, Avg: %.2lf", start_run-start_jit, diff, time_sum / count); - time_sum += diff + start_run - start_jit; - } else { - double start_run = now_ms(); - fill_plasma(&info, pixels, time_ms ); - double diff = now_ms()-start_run; - LOGI("GCC Time Run: %.2lf, Avg: %.2lf", diff, time_sum / count); - time_sum += diff; - } - count++; - - AndroidBitmap_unlockPixels(env, bitmap); - - return count * 1000.0 / time_sum; -} diff --git a/samples/bitmap-plasma-llvm/res/raw/gdb.setup b/samples/bitmap-plasma-llvm/res/raw/gdb.setup deleted file mode 100644 index d7abec1..0000000 --- a/samples/bitmap-plasma-llvm/res/raw/gdb.setup +++ /dev/null @@ -1,2 +0,0 @@ -set solib-search-path /home/nowar/cyanogen/gdk/samples/bitmap-plasma-llvm/obj/local/armeabi-v7a -directory /home/nowar/cyanogen/gdk/platforms/android-9/arch-arm/usr/include /home/nowar/cyanogen/gdk/samples/bitmap-plasma-llvm/jni /home/nowar/cyanogen/gdk/sources/cxx-stl/system diff --git a/samples/bitmap-plasma-llvm/res/raw/gdbserver b/samples/bitmap-plasma-llvm/res/raw/gdbserver deleted file mode 100755 index 39634a8..0000000 Binary files a/samples/bitmap-plasma-llvm/res/raw/gdbserver and /dev/null differ diff --git a/samples/bitmap-plasma-llvm/src/com/example/plasma/llvm/Plasma.java b/samples/bitmap-plasma-llvm/src/com/example/plasma/llvm/Plasma.java index 676ca05..a627a1c 100644 --- a/samples/bitmap-plasma-llvm/src/com/example/plasma/llvm/Plasma.java +++ b/samples/bitmap-plasma-llvm/src/com/example/plasma/llvm/Plasma.java @@ -77,49 +77,56 @@ class PlasmaView extends View { private long mStartTime; /* implementend by libplasma.so */ + private static native boolean gdk(); private static native int nativeRenderPlasma(Bitmap bitmap, long time_ms, byte[] script, int scriptLength, boolean useLLVM); private byte[] pgm; private int pgmLength; - private boolean mode = true; + private boolean llvm_mode = false; private Paint paint = null; public void switchMode() { - mode = !mode; + if (gdk()) + llvm_mode = !llvm_mode; } public PlasmaView(Context context) { super(context); + + llvm_mode = gdk(); mStartTime = System.currentTimeMillis(); - - InputStream is = null; - is = getResources().openRawResource(R.raw.libplasma_portable); - try { + if (llvm_mode) + { + InputStream is = null; + is = getResources().openRawResource(R.raw.libplasma_portable); try { - pgm = new byte[1024]; - pgmLength = 0; - while(true) { - int bytesLeft = pgm.length - pgmLength; - if (bytesLeft == 0) { - byte[] buf2 = new byte[pgm.length * 2]; - System.arraycopy(pgm, 0, buf2, 0, pgm.length); - pgm = buf2; - bytesLeft = pgm.length - pgmLength; - } - int bytesRead = is.read(pgm, pgmLength, bytesLeft); - if (bytesRead <= 0) { - break; + try { + pgm = new byte[1024]; + pgmLength = 0; + while(true) { + int bytesLeft = pgm.length - pgmLength; + if (bytesLeft == 0) { + byte[] buf2 = new byte[pgm.length * 2]; + System.arraycopy(pgm, 0, buf2, 0, pgm.length); + pgm = buf2; + bytesLeft = pgm.length - pgmLength; + } + int bytesRead = is.read(pgm, pgmLength, bytesLeft); + if (bytesRead <= 0) { + break; + } + pgmLength += bytesRead; } - pgmLength += bytesRead; + } finally { + is.close(); } - } finally { - is.close(); + } catch(IOException e) { + throw new Resources.NotFoundException(); } - } catch(IOException e) { - throw new Resources.NotFoundException(); - } + } + paint = new Paint(); paint.setTextSize(40); } @@ -128,9 +135,9 @@ class PlasmaView extends View { if (mBitmap == null || mBitmap.getWidth() != getWidth() || mBitmap.getHeight() != getHeight()) mBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.RGB_565); - int frameRate = nativeRenderPlasma(mBitmap, System.currentTimeMillis() - mStartTime, pgm, pgmLength, mode); + int frameRate = nativeRenderPlasma(mBitmap, System.currentTimeMillis() - mStartTime, pgm, pgmLength, llvm_mode); canvas.drawBitmap(mBitmap, 0, 0, null); - canvas.drawText((mode ? "LLVM" : "GCC") + " Frame: " + Integer.toString(frameRate), 100, 100, paint); + canvas.drawText((llvm_mode ? "LLVM/GDK" : "Native") + " Frame: " + Integer.toString(frameRate), 100, 100, paint); // force a redraw, with a different time-based pattern. invalidate(); diff --git a/samples/quake/jni/Android-portable.mk b/samples/quake/jni/Android-portable.mk index a2fd8b1..1cfacb0 100644 --- a/samples/quake/jni/Android-portable.mk +++ b/samples/quake/jni/Android-portable.mk @@ -63,12 +63,11 @@ LOCAL_SRC_FILES:= \ world.cpp \ zone.cpp -LOCAL_CFLAGS := -fno-zero-initialized-in-bss \ - -I$(OUT)/../../../../dalvik/libnativehelper/include \ - -I$(OUT)/../../../../frameworks/base/include \ +LOCAL_CFLAGS := -D__GDK__ \ + -fno-zero-initialized-in-bss \ + -I$(OUT)/../../../../frameworks/native/include \ -I$(OUT)/../../../../system/core/include LOCAL_C_INCLUDES := $(call include-path-for, wilhelm) - include $(BUILD_BITCODE) diff --git a/samples/quake/jni/Android.mk b/samples/quake/jni/Android.mk index 1881080..27dd381 100644 --- a/samples/quake/jni/Android.mk +++ b/samples/quake/jni/Android.mk @@ -34,8 +34,7 @@ LOCAL_MODULE_TAGS := optional LOCAL_CFLAGS := -Werror \ -I$(OUT)/../../../../frameworks/compile/libbcc/include \ - -I$(OUT)/../../../../dalvik/libnativehelper/include \ - -I$(OUT)/../../../../frameworks/base/include \ + -I$(OUT)/../../../../frameworks/native/include \ -I$(OUT)/../../../../system/core/include LOCAL_C_INCLUDES := $(call include-path-for, wilhelm) diff --git a/samples/quake/jni/Application.mk b/samples/quake/jni/Application.mk new file mode 100644 index 0000000..a64dbba --- /dev/null +++ b/samples/quake/jni/Application.mk @@ -0,0 +1,5 @@ +APP_ABI := armeabi armeabi-v7a +#APP_ABI := mips mips-r2 +#APP_ABI := x86 +APP_PLATFORM := android-9 + diff --git a/samples/quake/jni/gl_draw.cpp b/samples/quake/jni/gl_draw.cpp index e822a65..775ac77 100644 --- a/samples/quake/jni/gl_draw.cpp +++ b/samples/quake/jni/gl_draw.cpp @@ -116,6 +116,7 @@ int numgltextures; #include #include #include +int errno_portable(); // Allow named textures to be evicted from memory. @@ -288,25 +289,25 @@ private: mFileId = open(fullpath, O_RDWR | O_CREAT, 0666); if ( mFileId == -1 ) { Sys_Error("Could not open texture store file %s: %d", fullpath, - errno); + errno_portable()); } if (-1 == lseek(mFileId, TEXTURE_STORE_SIZE-1, SEEK_SET)) { Sys_Error("Could not extend the texture store file size. %d", - errno); + errno_portable()); } char end; end = 0; if (-1 == write(mFileId, &end, 1)) { Sys_Error("Could not write last byte of the texture store file. %d", - errno); + errno_portable()); } mBase = (byte*) mmap((caddr_t)0, TEXTURE_STORE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE, mFileId, 0); if (mBase == (byte*) -1) { - Sys_Error("Could not mmap file %s: %d", fullpath, errno); + Sys_Error("Could not mmap file %s: %d", fullpath, errno_portable()); } mLength = 0; mCapacity = TEXTURE_STORE_SIZE; diff --git a/samples/quake/jni/gl_vidandroid.cpp b/samples/quake/jni/gl_vidandroid.cpp index 02588e5..c8d3847 100644 --- a/samples/quake/jni/gl_vidandroid.cpp +++ b/samples/quake/jni/gl_vidandroid.cpp @@ -490,7 +490,7 @@ int AndroidEvent(int type, int value) // Called from Java to process an event. // Return non-zero if the event is handled. -#if !defined(__clang__) +#if !defined(__GDK__) int AndroidEvent2(int type, int value) #else extern "C" int AndroidEvent2_LLVM(int type, int value) @@ -660,7 +660,7 @@ private: GestureDetector gGestureDetector; -#if !defined(__clang__) +#if !defined(__GDK__) int AndroidMotionEvent #else extern "C" int AndroidMotionEvent_LLVM @@ -681,7 +681,7 @@ extern "C" int AndroidMotionEvent_LLVM return true; } -#if !defined(__clang__) +#if !defined(__GDK__) int AndroidTrackballEvent #else extern "C" int AndroidTrackballEvent_LLVM diff --git a/samples/quake/jni/masterMain.cpp b/samples/quake/jni/masterMain.cpp index e513171..67e9697 100644 --- a/samples/quake/jni/masterMain.cpp +++ b/samples/quake/jni/masterMain.cpp @@ -15,22 +15,23 @@ ** limitations under the License. */ -#include +#include #include #include -#include -#if !defined(__clang__) +#if !defined(__GDK__) #include +#include #endif #include +#include "sys.h" #define LOG_TAG "Quake masterMain" #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) -#if !defined(__clang__) +#if !defined(__GDK__) int AndroidInit(); int AndroidEvent2(int type, int value); int AndroidMotionEvent(unsigned long long eventTime, int action, float x, float y, float pressure, float size, int deviceId); @@ -91,14 +92,27 @@ qquit(JNIEnv *env, jobject thiz) { return pAndroidQuit(); } +jboolean +gdk(JNIEnv *env, jobject obj) +{ +#if !defined(__NOGDK__) + return JNI_TRUE; +#else + return JNI_FALSE; +#endif +} + +#if !defined(__GDK__) && !defined(__NOGDK__) static void* lookupSymbol(void* pContext, const char* name) { return (void*) dlsym(RTLD_DEFAULT, name); } +#endif // !__GDK__ && !__NOGDK__ jboolean qcompile_bc(JNIEnv *env, jobject thiz, jbyteArray scriptRef, jint length) { +#if !defined(__GDK__) && !defined(__NOGDK__) if (!use_llvm) return JNI_TRUE; @@ -110,6 +124,8 @@ qcompile_bc(JNIEnv *env, jobject thiz, jbyteArray scriptRef, jint length) pAndroidQuitType new_pAndroidQuit; int all_func_found = 1; + double time = Sys_FloatTime (); + BCCScriptRef script_ref = bccCreateScript(); jbyte* script_ptr = (jbyte *)env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0); LOGI("BCC Script Len: %d", length); @@ -126,11 +142,28 @@ qcompile_bc(JNIEnv *env, jobject thiz, jbyteArray scriptRef, jint length) return JNI_FALSE; } #endif + + double newtime = Sys_FloatTime (); + double readtime = newtime - time; + time = newtime; + bccRegisterSymbolCallback(script_ref, lookupSymbol, NULL); - if (bccPrepareExecutableEx(script_ref, ".", "/data/data/com.android.quake.llvm/quakeLLVM", 0)) { - LOGE("Error! Cannot bccPrepareExecutableEx"); + + #ifdef OLD_BCC + if (bccPrepareExecutable(script_ref, "/data/data/com.android.quake.llvm/quakeLLVM.oBCC", 0)) { + LOGE("Error! Cannot bccPrepareExecutable"); + return JNI_FALSE; + } + #else + if (bccPrepareExecutable(script_ref, "/data/data/com.android.quake.llvm/", "quakeLLVM", 0)) { + LOGE("Error! Cannot bccPrepareExecutable"); return JNI_FALSE; } + #endif // OLD_BCC + + newtime = Sys_FloatTime (); + double compiletime = newtime - time; + time = newtime; new_pAndroidInit = (pAndroidInitType)bccGetFuncAddr(script_ref, "AndroidInit_LLVM"); if (new_pAndroidInit == NULL) { @@ -182,7 +215,6 @@ qcompile_bc(JNIEnv *env, jobject thiz, jbyteArray scriptRef, jint length) //bccDisposeScript(script_ref); - //Uncomment the following if (all_func_found) { LOGI("Use LLVM version"); @@ -193,14 +225,25 @@ qcompile_bc(JNIEnv *env, jobject thiz, jbyteArray scriptRef, jint length) pAndroidStep = new_pAndroidStep; pAndroidQuit = new_pAndroidQuit; } + newtime = Sys_FloatTime (); + double findptrtime = newtime - time; + + LOGI("LLVM JIT time = %lf (%lf + %lf + %lf)\n", readtime+compiletime+findptrtime, + readtime, compiletime, findptrtime); return JNI_TRUE; +#else + + return JNI_FALSE; + +#endif // !__GDK__ && !__NOGDK__ } static const char *classPathName = "com/android/quake/llvm/QuakeLib"; static JNINativeMethod methods[] = { + {"gdk", "()Z", (void*)gdk }, {"compile_bc", "([BI)Z", (void*)qcompile_bc }, {"init", "()Z", (void*)qinit }, {"event", "(II)Z", (void*)qevent }, @@ -293,4 +336,4 @@ bail: } -#endif // __clang__ +#endif // __GDK__ diff --git a/samples/quake/jni/net_udp.cpp b/samples/quake/jni/net_udp.cpp index 68fba41..aaf470f 100644 --- a/samples/quake/jni/net_udp.cpp +++ b/samples/quake/jni/net_udp.cpp @@ -51,6 +51,7 @@ static unsigned long myAddr; #include "net_udp.h" + // **** Start of Android-specific code **** // copied from ifc_utils.c // @@ -61,14 +62,109 @@ static unsigned long myAddr; #include #include +// +// system dependent +// + +#define SOCK_DGRAM_PORTABLE 2 +#define SOCK_STREAM_PORTABLE 1 +#define FIONBIO_PORTABLE 0x5421 +#define FIONREAD_PORTABLE 0x541B +#define ECONNREFUSED_PORTABLE 111 +#define SOL_SOCKET_PORTABLE 1 +#define SO_BROADCAST_PORTABLE 6 + +#define SOCK_DGRAM_MIPS 1 +#define SOCK_STREAM_MIPS 2 +#define FIONBIO_MIPS 0x667e +#define FIONREAD_MIPS 0x467f +#define ECONNREFUSED_MIPS 146 +#define SOL_SOCKET_MIPS 0xffff +#define SO_BROADCAST_MIPS 0x0020 + + +int socket_portable(int domain, int type, int protocol); +int ioctl_portable(int fd, int cmd, void *); // ToDo: ioctl_portable(int fd, int cmd, ...) +int setsockopt_portable(int s, int level, int optname, const void *optval, socklen_t optlen); +int errno_portable(); + + +#if !defined(__GDK__) +int socket_portable(int domain, int type, int protocol) +{ +#if defined(__mips__) + switch(type) { + case SOCK_DGRAM_PORTABLE: + type = SOCK_DGRAM_MIPS; + break; + case SOCK_STREAM_PORTABLE: + type = SOCK_STREAM_MIPS; + break; + } +#endif // __mips__ + return socket(domain, type, protocol); +} + +int ioctl_portable(int fd, int cmd, void *arg) +{ +#if defined(__mips__) + switch(cmd) { + case FIONBIO_PORTABLE: + cmd = FIONBIO_MIPS; + break; + case FIONREAD_PORTABLE: + cmd = FIONREAD_MIPS; + //ToDo: there are a lot more diff! + } +#endif // __mips__ + return ioctl(fd, cmd, arg); +} + +int errno_portable() +{ + int ret = errno; +#if defined(__mips__) + switch(ret) { + case ECONNREFUSED_PORTABLE: + ret = ECONNREFUSED_MIPS; + break; + //ToDo: there are a lot more diff! + } +#endif // __mips__ + return ret; +} + +int setsockopt_portable(int s, int level, int optname, const void *optval, socklen_t optlen) +{ +#if defined(__mips__) + switch(level) { + case SOL_SOCKET_PORTABLE: + level = SOL_SOCKET_MIPS; + break; + //ToDo: there are a lot more diff! + } + + switch(optname) { + case SO_BROADCAST_PORTABLE: + optname = SO_BROADCAST_MIPS; + break; + //ToDo: there are a lot more diff! + } +#endif + return setsockopt(s, level, optname, optval, optlen); +} + +#endif // !__GDK__ + + static int ifc_ctl_sock = -1; int ifc_init(void) { if (ifc_ctl_sock == -1) { - ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM, 0); + ifc_ctl_sock = socket_portable(AF_INET, SOCK_DGRAM_PORTABLE, 0); if (ifc_ctl_sock < 0) { - Con_Printf("socket() failed: %s\n", strerror(errno)); + Con_Printf("socket() failed: %s\n", strerror(errno_portable())); } } return ifc_ctl_sock < 0 ? -1 : 0; @@ -95,7 +191,7 @@ int ifc_get_info(const char *name, in_addr_t *addr, in_addr_t *mask, unsigned *f ifc_init_ifr(name, &ifr); if (addr != NULL) { - if(ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr) < 0) { + if(ioctl_portable(ifc_ctl_sock, SIOCGIFADDR, &ifr) < 0) { *addr = 0; } else { *addr = ((struct sockaddr_in*) (void*) &ifr.ifr_addr)->sin_addr.s_addr; @@ -103,7 +199,7 @@ int ifc_get_info(const char *name, in_addr_t *addr, in_addr_t *mask, unsigned *f } if (mask != NULL) { - if(ioctl(ifc_ctl_sock, SIOCGIFNETMASK, &ifr) < 0) { + if(ioctl_portable(ifc_ctl_sock, SIOCGIFNETMASK, &ifr) < 0) { *mask = 0; } else { *mask = ((struct sockaddr_in*) (void*) &ifr.ifr_addr)->sin_addr.s_addr; @@ -111,7 +207,7 @@ int ifc_get_info(const char *name, in_addr_t *addr, in_addr_t *mask, unsigned *f } if (flags != NULL) { - if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) { + if(ioctl_portable(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) { *flags = 0; } else { *flags = ifr.ifr_flags; @@ -233,10 +329,10 @@ int UDP_OpenSocket (int port) } address; qboolean _true = true; - if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) + if ((newsocket = socket_portable(PF_INET, SOCK_DGRAM_PORTABLE, IPPROTO_UDP)) == -1) return -1; - if (ioctl (newsocket, FIONBIO, (char *)&_true) == -1) + if (ioctl_portable(newsocket, FIONBIO_PORTABLE, (char *)&_true) == -1) goto ErrorReturn; address.in.sin_family = AF_INET; @@ -335,7 +431,7 @@ int UDP_CheckNewConnections (void) if (net_acceptsocket == -1) return -1; - if (ioctl (net_acceptsocket, FIONREAD, &available) == -1) + if (ioctl_portable(net_acceptsocket, FIONREAD_PORTABLE, &available) == -1) Sys_Error ("UDP: ioctlsocket (FIONREAD) failed\n"); if (available) return net_acceptsocket; @@ -350,7 +446,7 @@ int UDP_Read (int socket, byte *buf, int len, struct qsockaddr *addr) int ret; ret = recvfrom (socket, buf, len, 0, (struct sockaddr *)addr, (socklen_t*) &addrlen); - if (ret == -1 && (errno == EWOULDBLOCK || errno == ECONNREFUSED)) + if (ret == -1 && (errno_portable() == EWOULDBLOCK || errno_portable() == ECONNREFUSED_PORTABLE)) return 0; return ret; } @@ -362,7 +458,7 @@ int UDP_MakeSocketBroadcastCapable (int socket) int i = 1; // make this socket broadcast capable - if (setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) < 0) + if (setsockopt_portable(socket, SOL_SOCKET_PORTABLE, SO_BROADCAST_PORTABLE, (char *)&i, sizeof(i)) < 0) return -1; net_broadcastsocket = socket; @@ -397,7 +493,7 @@ int UDP_Write (int socket, byte *buf, int len, struct qsockaddr *addr) int ret; ret = sendto (socket, buf, len, 0, (struct sockaddr *)addr, sizeof(struct qsockaddr)); - if (ret == -1 && errno == EWOULDBLOCK) + if (ret == -1 && errno_portable() == EWOULDBLOCK) return 0; return ret; } diff --git a/samples/quake/jni/snd_dma.cpp b/samples/quake/jni/snd_dma.cpp index e46469f..7e11149 100644 --- a/samples/quake/jni/snd_dma.cpp +++ b/samples/quake/jni/snd_dma.cpp @@ -19,8 +19,14 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ // snd_dma.c -- main control for any streaming sound output device +#include #include "quakedef.h" +#define LOG_TAG "Quake snd_dma" +#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) +#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) + + #ifdef _WIN32 #include "winquake.h" #endif @@ -144,7 +150,6 @@ void S_Startup (void) if (!fakedma) { rc = SNDDMA_Init(); - if (!rc) { #ifndef _WIN32 @@ -199,8 +204,6 @@ void S_Init (void) Con_Printf ("loading all sounds as 8bit\n"); } - - snd_initialized = true; S_Startup (); diff --git a/samples/quake/jni/sys_android.cpp b/samples/quake/jni/sys_android.cpp index 90f0134..5d418ea 100644 --- a/samples/quake/jni/sys_android.cpp +++ b/samples/quake/jni/sys_android.cpp @@ -39,6 +39,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include #include #include +int errno_portable(); #include #include @@ -64,6 +65,142 @@ static const char *cachedir = "/tmp"; cvar_t sys_linerefresh = CVAR2("sys_linerefresh","0");// set for entity display + +// +// system dependent +// + +struct stat_portable { + unsigned long long st_dev; + unsigned char __pad0[4]; + + unsigned long __st_ino; + unsigned int st_mode; + unsigned int st_nlink; + + unsigned long st_uid; + unsigned long st_gid; + + unsigned long long st_rdev; + unsigned char __pad3[4]; + + long long st_size; + unsigned long st_blksize; + unsigned long long st_blocks; + + unsigned long st_atime; + unsigned long st_atime_nsec; + + unsigned long st_mtime; + unsigned long st_mtime_nsec; + + unsigned long st_ctime; + unsigned long st_ctime_nsec; + + unsigned long long st_ino; +}; + +#if defined(__mips__) +struct stat_mips { + unsigned long st_dev; + unsigned long __pad0[3]; + + unsigned long long st_ino; + + unsigned int st_mode; + unsigned int st_nlink; + + unsigned long st_uid; + unsigned long st_gid; + + unsigned long st_rdev; + unsigned long __pad1[3]; + + long long st_size; + + unsigned long st_atime; + unsigned long st_atime_nsec; + + unsigned long st_mtime; + unsigned long st_mtime_nsec; + + unsigned long st_ctime; + unsigned long st_ctime_nsec; + + unsigned long st_blksize; + unsigned long __pad2; + + unsigned long long st_blocks; +}; +#endif //__mips__ + +int stat_portable(const char *path, struct stat_portable *st); +int fstat_portable(int fd, struct stat_portable *st); + + +#if !defined(__GDK__) + +#if defined(__mips__) +static void __copy_mips_stat_to_portable(struct stat_portable *pst_portable, struct stat/*_mips*/ *pst_mips) +{ + pst_portable->st_dev = pst_mips->st_dev; //ToDo: sizeof st_dev is different ! + pst_portable->__st_ino = 0; //ToDo: missing in mips! + pst_portable->st_mode = pst_mips->st_mode; + pst_portable->st_nlink = pst_mips->st_nlink; + pst_portable->st_uid = pst_mips->st_uid; + pst_portable->st_gid = pst_mips->st_gid; + pst_portable->st_rdev = pst_mips->st_rdev; //ToDo: st_rdev is different ! + pst_portable->st_size = pst_mips->st_size; + pst_portable->st_blksize = pst_mips->st_blksize; + pst_portable->st_blocks = pst_mips->st_blocks; + pst_portable->st_atime = pst_mips->st_atime; + pst_portable->st_atime_nsec = pst_mips->st_atime_nsec; + pst_portable->st_mtime = pst_mips->st_mtime; + pst_portable->st_mtime_nsec = pst_mips->st_mtime_nsec; + pst_portable->st_ctime = pst_mips->st_ctime; + pst_portable->st_ctime_nsec = pst_mips->st_ctime_nsec; + pst_portable->st_ino = pst_mips->st_ino; +} +#endif // __mips__ + +int stat_portable(const char *path, struct stat_portable *st) +{ +#if !defined(__mips__) + struct stat st_orig; + //assert(sizeof(st_orig) == sizeof(*st)); //ToDo: and offset of each field ! + + return stat(path, (struct stat*)st); +#else + struct stat/*_mips*/ st_mips; + //assert(sizeof(st_mips) == sizeof(*st)); //ToDo: and offset of each field ! + + int ret = stat(path, &st_mips); + __copy_mips_stat_to_portable((struct stat_portable *)st, &st_mips); + + return ret; +#endif +} + +int fstat_portable(int fd, struct stat_portable *st) +{ +#if !defined(__mips__) + struct stat st_orig; + //assert(sizeof(st_orig) == sizeof(*st)); //ToDo: and offset of each field ! + + return fstat(fd, (struct stat*)st); +#else + struct stat/*_mips*/ st_mips; + //assert(sizeof(st_mips) == sizeof(*st)); //ToDo: and offset of each field ! + + int ret = fstat(fd, &st_mips); + __copy_mips_stat_to_portable((struct stat_portable *)st, &st_mips); + + return ret; +#endif +} + +#endif // !__GDK__ + // ======================================================================= // General routines // ======================================================================= @@ -220,9 +357,9 @@ returns -1 if not present */ int Sys_FileTime (const char *path) { - struct stat buf; + struct stat_portable buf; - if (stat (path,&buf) == -1) + if (stat_portable(path,&buf) == -1) return -1; return buf.st_mtime; @@ -237,15 +374,14 @@ void Sys_mkdir (const char *path) int Sys_FileOpenRead (const char *path, int *handle) { int h; - struct stat fileinfo; - + struct stat_portable fileinfo; - h = open (path, O_RDONLY, 0666); + h = open(path, O_RDONLY, 0666); *handle = h; if (h == -1) return -1; - if (fstat (h,&fileinfo) == -1) + if (fstat_portable(h,&fileinfo) == -1) Sys_Error ("Error fstating %s", path); return fileinfo.st_size; @@ -261,7 +397,7 @@ int Sys_FileOpenWrite (const char *path) , 0666); if (handle == -1) - Sys_Error ("Error opening %s: %s", path,strerror(errno)); + Sys_Error ("Error opening %s: %s", path,strerror(errno_portable())); return handle; } @@ -401,15 +537,19 @@ extern int scr_height; qboolean direxists(const char* path) { - struct stat sb; - if(stat(path, &sb)) + struct stat_portable sb; + + if(stat_portable(path, &sb)) { +LOGI("direxists 1: path=%s\n", path); return 0; // error } if(sb.st_mode & S_IFDIR) { +LOGI("direxists 2: path=%s\n", path); return 1; } +LOGI("direxists 3: path=%s\n", path); return 0; } @@ -488,7 +628,7 @@ void CheckGLCacheVersion(const char* baseDir) fwrite(&vernum, sizeof(vernum), 1, f); fclose(f); } else { - PMPLOG(("Could not write %s %d.\n", cachePath, errno)); + PMPLOG(("Could not write %s %d.\n", cachePath, errno_portable())); } } } @@ -505,7 +645,7 @@ static qboolean gDoubleInitializeGuard; static qboolean gInitialized; void GL_ReInit(); -#if !defined(__clang__) +#if !defined(__GDK__) bool AndroidInit() #else extern "C" bool AndroidInit_LLVM() @@ -665,7 +805,7 @@ int AndroidStepImp(int width, int height) return key_dest == key_game; } -#if !defined(__clang__) +#if !defined(__GDK__) int AndroidStep(int width, int height) #else extern "C" int AndroidStep_LLVM(int width, int height) @@ -685,7 +825,7 @@ extern "C" int AndroidStep_LLVM(int width, int height) extern void Host_Quit(); -#if !defined(__clang__) +#if !defined(__GDK__) void AndroidQuit() { #else extern "C" void AndroidQuit_LLVM() { diff --git a/samples/quake/src/com/android/quake/llvm/QuakeActivity.java b/samples/quake/src/com/android/quake/llvm/QuakeActivity.java index 1a0bc99..a264902 100644 --- a/samples/quake/src/com/android/quake/llvm/QuakeActivity.java +++ b/samples/quake/src/com/android/quake/llvm/QuakeActivity.java @@ -51,40 +51,42 @@ public class QuakeActivity extends Activity { } if (foundQuakeData()) { - // HACK: create faked view in order to read bitcode in resource + byte[] pgm = null; + int pgmLength = 0; + if (QuakeLib.gdk()) + { + // HACK: create faked view in order to read bitcode in resource View view = new View(getApplication()); - byte[] pgm; - int pgmLength; - - // read bitcode in res - InputStream is = view.getResources().openRawResource(R.raw.libquake_portable); - try { - try { - pgm = new byte[1024]; - pgmLength = 0; - - while(true) { - int bytesLeft = pgm.length - pgmLength; - if (bytesLeft == 0) { - byte[] buf2 = new byte[pgm.length * 2]; - System.arraycopy(pgm, 0, buf2, 0, pgm.length); - pgm = buf2; - bytesLeft = pgm.length - pgmLength; - } - int bytesRead = is.read(pgm, pgmLength, bytesLeft); - if (bytesRead <= 0) { - break; - } - pgmLength += bytesRead; - } - } finally { - is.close(); - } - } catch(IOException e) { - throw new Resources.NotFoundException(); - } - // + // read bitcode in res + InputStream is = view.getResources().openRawResource(R.raw.libquake_portable); + try { + try { + pgm = new byte[1024]; + pgmLength = 0; + + while(true) { + int bytesLeft = pgm.length - pgmLength; + if (bytesLeft == 0) { + byte[] buf2 = new byte[pgm.length * 2]; + System.arraycopy(pgm, 0, buf2, 0, pgm.length); + pgm = buf2; + bytesLeft = pgm.length - pgmLength; + } + int bytesRead = is.read(pgm, pgmLength, bytesLeft); + if (bytesRead <= 0) { + break; + } + pgmLength += bytesRead; + } + } finally { + is.close(); + } + } catch(IOException e) { + throw new Resources.NotFoundException(); + } + } + // if (mQuakeLib == null) { mQuakeLib = new QuakeLib(pgm, pgmLength); if(! mQuakeLib.init()) { diff --git a/samples/quake/src/com/android/quake/llvm/QuakeLib.java b/samples/quake/src/com/android/quake/llvm/QuakeLib.java index 54bf7b0..664acf6 100644 --- a/samples/quake/src/com/android/quake/llvm/QuakeLib.java +++ b/samples/quake/src/com/android/quake/llvm/QuakeLib.java @@ -136,11 +136,13 @@ public class QuakeLib { static { System.loadLibrary("quake"); } - + public QuakeLib(byte[] pgm, int pgmLength) { - compile_bc(pgm, pgmLength); + if (gdk()) + compile_bc(pgm, pgmLength); } + public static native boolean gdk(); private native boolean compile_bc(byte[] pgm, int pgmLength); public native boolean init(); -- cgit v1.2.3