NEON在Android中的使用舉例,neonandroid舉例
1、 開啟Eclipse,File-->New-->AndroidApplication Project-->Application Name:Hello-Neon, Project Name: Hello-Neon,Package Name:com.hello_neon.android, Minimum Required SDK:API 9:Android 2.3(Gingerbread),Next-->去掉Create custom launcher icon的勾選,Next-->Next-->ActivityName:Hello_NeonProjectActivity,Finish.
2、 開啟Hello-Neon工程下的src-->com.hello_neon.android-->Hello_NeonProjectActivity.java,將其內容改為:
package com.hello_neon.android;import android.os.Bundle;import android.app.Activity;import android.widget.TextView;public class Hello_NeonProjectActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* Create a TextView and set its content. * the text is retrieved by calling a native function. */ TextView tv = new TextView(this); tv.setText( stringFromJNI() ); setContentView(tv); } /* A native method that is implemented by the * 'helloneon' native library, which is packaged with this application. */ public native String stringFromJNI(); /* this is used to load the 'helloneon' library on application * startup. The library has already been unpacked into * /data/data/com.example.neon/lib/libhelloneon.so at * installation time by the package manager. */ static { System.loadLibrary("helloneon"); }}
3、 儲存Hello_NeonProjectActivity.java檔案,開啟命令列視窗,將其定位到\bin\classes目錄下,輸入命令:javah –classpath D:\ProgramFiles\Android\android-sdk\platforms\android-9\android.jar;com.hello_neon.android.Hello_NeonProjectActivity ,會在\bin\classes目錄下產生com_hello_neon_android_Hello_NeonProjectActivity.h檔案(說明:*.jar也可以是其它版本);
4、 選中Hello-Neon工程,點擊右鍵-->New-->Folder建立一個jni檔案夾,在此檔案夾下添加Android.mk、Application.mk、helloneon.c、helloneon-intrinsics.c、helloneon-intrinsics.h五個檔案,其中內容分別為:
Android.mk:
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := helloneon#填寫要編譯的源檔案路徑LOCAL_SRC_FILES := helloneon.c helloneon-intrinsics.c#預設包含的標頭檔路徑LOCAL_C_INCLUDES := \$(LOCAL_PATH) \$(LOCAL_PATH)/..#-g 後面的一系列項目添加了才能使用arm_neon-h標頭檔, -mfloat-abi=softfp -mfpu=neon 使用arm_neon.h必須LOCAL_CFLAGS := -g -mfloat-abi=softfp -mfpu=neon -march=armv7-a -mtune=cortex-a8LOCAL_LDLIBS := -lz -llogTARGET_ARCH_ABI := armeabi-v7a LOCAL_ARM_MODE := armifeq ($(TARGET_ARCH_ABI),armeabi-v7a)#採用NEON最佳化技術 LOCAL_ARM_NEON := true #LOCAL_CFLAGS := -DHAVE_NEON=1endifLOCAL_STATIC_LIBRARIES := cpufeatures#產生動態調用庫include $(BUILD_SHARED_LIBRARY)$(call import-module,cpufeatures)
Application.mk:
APP_PROJECT_PATH := $(call my-dir)/..APP_PLATFORM := android-10#choose which library to compile against in your MakefileAPP_STL := stlport_static#APP_ABI這句指定了編譯的目標平台類型,可以針對不同平台進行最佳化,x86 or armeabi-v7a# Build both ARMv5TE and ARMv7-A machine code.APP_ABI := armeabi armeabi-v7aAPP_CPPFLAGS += -fexceptions#for using c++ features,you need to enable these in your MakefileAPP_CPP_FEATURES += exceptions rtti
helloneon.c:
#include <jni.h>#include <time.h>#include <stdio.h>#include <stdlib.h>#include <cpu-features.h>#include "helloneon-intrinsics.h"#define DEBUG 0#define HAVE_NEON#if DEBUG#include <android/log.h># define D(x...) __android_log_print(ANDROID_LOG_INFO,"helloneon",x)#else# define D(...) do {} while (0)#endif/* return current time in milliseconds */static doublenow_ms(void){ struct timespec res; clock_gettime(CLOCK_REALTIME, &res); return 1000.0*res.tv_sec + (double)res.tv_nsec/1e6;}/* this is a FIR filter implemented in C */static voidfir_filter_c(short *output, const short* input, const short* kernel, int width, int kernelSize){ int offset = -kernelSize/2; int nn; for (nn = 0; nn < width; nn++) { int sum = 0; int mm; for (mm = 0; mm < kernelSize; mm++) { sum += kernel[mm]*input[nn+offset+mm]; } output[nn] = (short)((sum + 0x8000) >> 16); }}#define FIR_KERNEL_SIZE 32#define FIR_OUTPUT_SIZE 2560#define FIR_INPUT_SIZE (FIR_OUTPUT_SIZE + FIR_KERNEL_SIZE)#define FIR_ITERATIONS 600static const short fir_kernel[FIR_KERNEL_SIZE] = { 0x10, 0x20, 0x40, 0x70, 0x8c, 0xa2, 0xce, 0xf0, 0xe9, 0xce, 0xa2, 0x8c, 070, 0x40, 0x20, 0x10, 0x10, 0x20, 0x40, 0x70, 0x8c, 0xa2, 0xce, 0xf0, 0xe9, 0xce, 0xa2, 0x8c, 070, 0x40, 0x20, 0x10 };static short fir_output[FIR_OUTPUT_SIZE];static short fir_input_0[FIR_INPUT_SIZE];static const short* fir_input = fir_input_0 + (FIR_KERNEL_SIZE/2);static short fir_output_expected[FIR_OUTPUT_SIZE];/* This is a trivial JNI example where we use a native method * to return a new VM String. See the corresponding Java source * file located at: * * apps/samples/hello-neon/project/src/com/example/neon/HelloNeon.java */JNIEXPORT jstring JNICALL Java_com_hello_1neon_android_Hello_1NeonProjectActivity_stringFromJNI(JNIEnv *env, jobject thiz){ char* str; uint64_t features; char buffer[512]; char tryNeon = 0; double t0, t1, time_c, time_neon; /* setup FIR input - whatever */ { int nn; for (nn = 0; nn < FIR_INPUT_SIZE; nn++) { fir_input_0[nn] = (5*nn) & 255; } fir_filter_c(fir_output_expected, fir_input, fir_kernel, FIR_OUTPUT_SIZE, FIR_KERNEL_SIZE); } /* Benchmark small FIR filter loop - C version */ t0 = now_ms(); { int count = FIR_ITERATIONS; for (; count > 0; count--) { fir_filter_c(fir_output, fir_input, fir_kernel, FIR_OUTPUT_SIZE, FIR_KERNEL_SIZE); } } t1 = now_ms(); time_c = t1 - t0; asprintf(&str, "FIR Filter benchmark:\nC version : %g ms\n", time_c); strlcpy(buffer, str, sizeof buffer); free(str); strlcat(buffer, "Neon version : ", sizeof buffer); if (android_getCpuFamily() != ANDROID_CPU_FAMILY_ARM) { strlcat(buffer, "Not an ARM CPU !\n", sizeof buffer); goto EXIT; } features = android_getCpuFeatures(); if ((features & ANDROID_CPU_ARM_FEATURE_ARMv7) == 0) { strlcat(buffer, "Not an ARMv7 CPU !\n", sizeof buffer); goto EXIT; } /* HAVE_NEON is defined in Android.mk ! */#ifdef HAVE_NEON if ((features & ANDROID_CPU_ARM_FEATURE_NEON) == 0) { strlcat(buffer, "CPU doesn't support NEON !\n", sizeof buffer); goto EXIT; } /* Benchmark small FIR filter loop - Neon version */ t0 = now_ms(); { int count = FIR_ITERATIONS; for (; count > 0; count--) { fir_filter_neon_intrinsics(fir_output, fir_input, fir_kernel, FIR_OUTPUT_SIZE, FIR_KERNEL_SIZE); } } t1 = now_ms(); time_neon = t1 - t0; asprintf(&str, "%g ms (x%g faster)\n", time_neon, time_c / (time_neon < 1e-6 ? 1. : time_neon)); strlcat(buffer, str, sizeof buffer); free(str); /* check the result, just in case */ { int nn, fails = 0; for (nn = 0; nn < FIR_OUTPUT_SIZE; nn++) { if (fir_output[nn] != fir_output_expected[nn]) { if (++fails < 16) D("neon[%d] = %d expected %d", nn, fir_output[nn], fir_output_expected[nn]); } } D("%d fails\n", fails); }#else /* !HAVE_NEON */ strlcat(buffer, "Program not compiled with ARMv7 support !\n", sizeof buffer);#endif /* !HAVE_NEON */EXIT: return (*env)->NewStringUTF(env, buffer);}
helloneon-intrinsics.h:
#ifndef HELLONEON_INTRINSICS_H#define HELLONEON_INTRINSICS_Hvoid fir_filter_neon_intrinsics(short *output, const short* input, const short* kernel, int width, int kernelSize);#endif /* HELLONEON_INTRINSICS_H */
helloneon-intrinsics.c:
#include "helloneon-intrinsics.h"#include <arm_neon.h>/* this source file should only be compiled by Android.mk when targeting * the armeabi-v7a ABI, and should be built in NEON mode */voidfir_filter_neon_intrinsics(short *output, const short* input, const short* kernel, int width, int kernelSize){#if 1 int nn, offset = -kernelSize/2; for (nn = 0; nn < width; nn++) { int mm, sum = 0; int32x4_t sum_vec = vdupq_n_s32(0); for(mm = 0; mm < kernelSize/4; mm++) { int16x4_t kernel_vec = vld1_s16(kernel + mm*4); int16x4_t input_vec = vld1_s16(input + (nn+offset+mm*4)); sum_vec = vmlal_s16(sum_vec, kernel_vec, input_vec); } sum += vgetq_lane_s32(sum_vec, 0); sum += vgetq_lane_s32(sum_vec, 1); sum += vgetq_lane_s32(sum_vec, 2); sum += vgetq_lane_s32(sum_vec, 3); if(kernelSize & 3) { for(mm = kernelSize - (kernelSize & 3); mm < kernelSize; mm++) sum += kernel[mm] * input[nn+offset+mm]; } output[nn] = (short)((sum + 0x8000) >> 16); }#else /* for comparison purposes only */ int nn, offset = -kernelSize/2; for (nn = 0; nn < width; nn++) { int sum = 0; int mm; for (mm = 0; mm < kernelSize; mm++) { sum += kernel[mm]*input[nn+offset+mm]; } output[nn] = (short)((sum + 0x8000) >> 16); }#endif}
5、 利用NDK產生.so檔案:選中工程-->Properties-->Builders-->New-->選中Program-->OK,Name:Hello_Neon_Builder,Location: D:\ProgramFiles\Android\android-sdk\android-ndk-r9\ndk-build.cmd,Working Directory: E:\NEON\Eclipse\Hello-Neon -->Apply,選中Refresh,勾選Refreshresources upon completion, 勾選Specific resources,點擊Specify Resources…,勾選Hello-Neon工程下的libs檔案夾,Finish-->Apply,選中BuildOptions,勾選Allocate Console(necessary for input), After a “Clean”, During manualbuilds, During auto builds, Specify working set of relevant resources,點擊SpecifyResoures…,勾選Hello-Neon工程下的jni檔案夾,Finish-->Apply-->OK-->OK,會在libs檔案夾下產生libhelloneon.so檔案;
6、 選中Hello-Neon,-->Run As-->AndroidApplication,運行結果為:
FIRFilter benchmark:
C version :282.84 ms
Neon version :135985 ms(x2.07994 faster)
以上是.c檔案的操作步驟,若將.c檔案該為.cpp檔案,則需改動兩個檔案:
1、將Android.mk改為:
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := helloneon#填寫要編譯的源檔案路徑LOCAL_SRC_FILES := helloneon.cpp helloneon-intrinsics.cpp#預設包含的標頭檔路徑LOCAL_C_INCLUDES := \$(LOCAL_PATH) \$(LOCAL_PATH)/..#-g 後面的一系列項目添加了才能使用arm_neon-h標頭檔, -mfloat-abi=softfp -mfpu=neon 使用arm_neon.h必須LOCAL_CFLAGS := -g -mfloat-abi=softfp -mfpu=neon -march=armv7-a -mtune=cortex-a8LOCAL_LDLIBS := -lz -llogTARGET_ARCH_ABI := armeabi-v7a LOCAL_ARM_MODE := armifeq ($(TARGET_ARCH_ABI),armeabi-v7a)#採用NEON最佳化技術 LOCAL_ARM_NEON := true #LOCAL_CFLAGS := -DHAVE_NEON=1endifLOCAL_STATIC_LIBRARIES := cpufeatures#產生動態調用庫include $(BUILD_SHARED_LIBRARY)$(call import-module,cpufeatures)
2、helloneon.c改為:
#include <jni.h>#include <time.h>#include <stdio.h>#include <stdlib.h>#include <cpu-features.h>#include "helloneon-intrinsics.h"#define DEBUG 0#define HAVE_NEON#ifdef __cplusplusextern "C" {#endif#if DEBUG#include <android/log.h># define D(x...) __android_log_print(ANDROID_LOG_INFO,"helloneon",x)#else# define D(...) do {} while (0)#endif/* return current time in milliseconds */static doublenow_ms(void){ struct timespec res; clock_gettime(CLOCK_REALTIME, &res); return 1000.0*res.tv_sec + (double)res.tv_nsec/1e6;}/* this is a FIR filter implemented in C */static voidfir_filter_c(short *output, const short* input, const short* kernel, int width, int kernelSize){ int offset = -kernelSize/2; int nn; for (nn = 0; nn < width; nn++) { int sum = 0; int mm; for (mm = 0; mm < kernelSize; mm++) { sum += kernel[mm]*input[nn+offset+mm]; } output[nn] = (short)((sum + 0x8000) >> 16); }}#define FIR_KERNEL_SIZE 32#define FIR_OUTPUT_SIZE 2560#define FIR_INPUT_SIZE (FIR_OUTPUT_SIZE + FIR_KERNEL_SIZE)#define FIR_ITERATIONS 600static const short fir_kernel[FIR_KERNEL_SIZE] = { 0x10, 0x20, 0x40, 0x70, 0x8c, 0xa2, 0xce, 0xf0, 0xe9, 0xce, 0xa2, 0x8c, 070, 0x40, 0x20, 0x10, 0x10, 0x20, 0x40, 0x70, 0x8c, 0xa2, 0xce, 0xf0, 0xe9, 0xce, 0xa2, 0x8c, 070, 0x40, 0x20, 0x10 };static short fir_output[FIR_OUTPUT_SIZE];static short fir_input_0[FIR_INPUT_SIZE];static const short* fir_input = fir_input_0 + (FIR_KERNEL_SIZE/2);static short fir_output_expected[FIR_OUTPUT_SIZE];/* This is a trivial JNI example where we use a native method * to return a new VM String. See the corresponding Java source * file located at: * * apps/samples/hello-neon/project/src/com/example/neon/HelloNeon.java */JNIEXPORT jstring JNICALL Java_com_hello_1neon_android_Hello_1NeonProjectActivity_stringFromJNI(JNIEnv *env, jobject thiz){ char str[512] = {0}; uint64_t features; char buffer[512]; char tryNeon = 0; double t0, t1, time_c, time_neon; /* setup FIR input - whatever */ { int nn; for (nn = 0; nn < FIR_INPUT_SIZE; nn++) { fir_input_0[nn] = (5*nn) & 255; } fir_filter_c(fir_output_expected, fir_input, fir_kernel, FIR_OUTPUT_SIZE, FIR_KERNEL_SIZE); } /* Benchmark small FIR filter loop - C version */ t0 = now_ms(); { int count = FIR_ITERATIONS; for (; count > 0; count--) { fir_filter_c(fir_output, fir_input, fir_kernel, FIR_OUTPUT_SIZE, FIR_KERNEL_SIZE); } } t1 = now_ms(); time_c = t1 - t0; sprintf(str, "FIR Filter benchmark:\nC version : %g ms\n", time_c); strlcpy(buffer, str, sizeof buffer); strlcat(buffer, "Neon version : ", sizeof buffer); if (android_getCpuFamily() != ANDROID_CPU_FAMILY_ARM) { strlcat(buffer, "Not an ARM CPU !\n", sizeof buffer); goto EXIT; } features = android_getCpuFeatures(); if ((features & ANDROID_CPU_ARM_FEATURE_ARMv7) == 0) { strlcat(buffer, "Not an ARMv7 CPU !\n", sizeof buffer); goto EXIT; } /* HAVE_NEON is defined in Android.mk ! */#ifdef HAVE_NEON if ((features & ANDROID_CPU_ARM_FEATURE_NEON) == 0) { strlcat(buffer, "CPU doesn't support NEON !\n", sizeof buffer); goto EXIT; } /* Benchmark small FIR filter loop - Neon version */ t0 = now_ms(); { int count = FIR_ITERATIONS; for (; count > 0; count--) { fir_filter_neon_intrinsics(fir_output, fir_input, fir_kernel, FIR_OUTPUT_SIZE, FIR_KERNEL_SIZE); } } t1 = now_ms(); time_neon = t1 - t0; sprintf(str, "%g ms (x%g faster)\n", time_neon, time_c / (time_neon < 1e-6 ? 1. : time_neon)); strlcat(buffer, str, sizeof buffer); /* check the result, just in case */ { int nn, fails = 0; for (nn = 0; nn < FIR_OUTPUT_SIZE; nn++) { if (fir_output[nn] != fir_output_expected[nn]) { if (++fails < 16) D("neon[%d] = %d expected %d", nn, fir_output[nn], fir_output_expected[nn]); } } D("%d fails\n", fails); }#else /* !HAVE_NEON */ strlcat(buffer, "Program not compiled with ARMv7 support !\n", sizeof buffer);#endif /* !HAVE_NEON */EXIT: return env->NewStringUTF(buffer);}#ifdef __cplusplus}#endif
參考文獻:
1、 http://blog.csdn.net/fengbingchun/article/details/11580983
2、 android-ndk-r9-windows-x86_64中的hello-neon例子代碼
Android中Handler的使用方法
你可以直接建立自己的thread來完成一些工作。
Handler主要是用來跟UI主線程互動用。 比如:
1、你用handler發送一個message,然後在handler的線程中來接收、處理該訊息,以避免直接在UI主線程中處理事務導致影響UI主線程的其他處理工作。
2、你可以將handler對象傳給其他進程,以便在其他進程中通過handler給你發送事件。
3、通過handler的延時發送message,可以延時處理一些事務的處理
你好 , android中的資源引用中舉例的這個語句setTitle(Rstringmain_title)為何錯了
這種錯誤很容易找到問題的,你看看String下有沒有資源main_title這個東西,或者有沒有把資源加到你的軟體中,有的話可以在R.java檔案中找到id