Add a few more samples:

audio-echo
   bitmap-plasma
   MoreTeapot
   choreographer
   gles3jni
   hello-gl2
   native-audio
   native-codec
   native-media
   native-plasma
   san-angeles
   sensor-graph
This commit is contained in:
guanghuafan
2016-06-06 20:00:06 -07:00
parent 40826299f2
commit 1aaccfa48e
352 changed files with 37312 additions and 43 deletions

View File

@@ -0,0 +1,39 @@
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
defaultConfig {
applicationId 'com.android.accelerometergraph'
minSdkVersion 8
targetSdkVersion 23
cmake {
abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a'
}
}
/* ndk {
platformVersion = 9
moduleName = 'accelerometergraph'
toolchain = 'clang'
stl = 'gnustl_static'
// abiFilters.addAll(['armeabi','armeabi-v7a', 'arm64-v8a', 'x86', 'x86-64'])
cppFlags.addAll(['-std=c++11','-Wall'])
ldLibs.addAll([ 'android', 'log', 'GLESv2'])
}
*/
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
// platformVersion = 9
// toolchain = 'clang'
path 'src/main/cpp'
}
}
}

View File

@@ -0,0 +1,39 @@
<?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.accelerometergraph">
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
<application
android:allowBackup="false"
android:fullBackupContent="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/accelerometergraph_activity">
<activity android:name=".AccelerometerGraphActivity"
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>

View File

@@ -0,0 +1,7 @@
precision mediump float;
uniform vec4 uFragColor;
void main() {
gl_FragColor = uFragColor;
}

View File

@@ -0,0 +1,6 @@
attribute float vPosition;
attribute float vSensorValue;
void main() {
gl_Position = vec4(vPosition, vSensorValue/9.81, 0, 1);
}

View File

@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
add_library(accelerometergraph SHARED
sensorgraph.cpp)
# Include libraries needed for accelerometergraph lib
target_link_libraries(accelerometergraph log android GLESv2)

View File

@@ -0,0 +1,266 @@
/*
* Copyright (C) 2015 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.
*/
// OpenGL ES 2.0 code
#include <jni.h>
#include <GLES2/gl2.h>
#include <android/log.h>
#include <android/asset_manager_jni.h>
#include <android/sensor.h>
#include <cstdint>
#include <cassert>
#include <string>
#define LOG_TAG "accelerometergraph"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
const int LOOPER_ID_USER = 3;
const int SENSOR_HISTORY_LENGTH = 100;
const int SENSOR_REFRESH_RATE = 100;
const float SENSOR_FILTER_ALPHA = 0.1f;
class sensorgraph {
std::string vertexShaderSource;
std::string fragmentShaderSource;
ASensorManager *sensorManager;
const ASensor *accelerometer;
ASensorEventQueue *accelerometerEventQueue;
ALooper *looper;
GLuint shaderProgram;
GLuint vPositionHandle;
GLuint vSensorValueHandle;
GLuint uFragColorHandle;
GLfloat xPos[SENSOR_HISTORY_LENGTH];
struct AccelerometerData {
GLfloat x;
GLfloat y;
GLfloat z;
};
AccelerometerData sensorData[SENSOR_HISTORY_LENGTH*2];
AccelerometerData sensorDataFilter;
int sensorDataIndex;
public:
sensorgraph() : sensorDataIndex(0) {}
void init(AAssetManager *assetManager) {
AAsset *vertexShaderAsset = AAssetManager_open(assetManager, "shader.glslv",
AASSET_MODE_BUFFER);
assert(vertexShaderAsset != NULL);
const void *vertexShaderBuf = AAsset_getBuffer(vertexShaderAsset);
assert(vertexShaderBuf != NULL);
off_t vertexShaderLength = AAsset_getLength(vertexShaderAsset);
vertexShaderSource = std::string((const char*)vertexShaderBuf,
(size_t)vertexShaderLength);
AAsset_close(vertexShaderAsset);
AAsset *fragmentShaderAsset = AAssetManager_open(assetManager, "shader.glslf",
AASSET_MODE_BUFFER);
assert(fragmentShaderAsset != NULL);
const void *fragmentShaderBuf = AAsset_getBuffer(fragmentShaderAsset);
assert(fragmentShaderBuf != NULL);
off_t fragmentShaderLength = AAsset_getLength(fragmentShaderAsset);
fragmentShaderSource = std::string((const char*)fragmentShaderBuf,
(size_t)fragmentShaderLength);
AAsset_close(fragmentShaderAsset);
sensorManager = ASensorManager_getInstance();
assert(sensorManager != NULL);
accelerometer = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_ACCELEROMETER);
assert(accelerometer != NULL);
looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
assert(looper != NULL);
accelerometerEventQueue = ASensorManager_createEventQueue(sensorManager, looper,
LOOPER_ID_USER, NULL, NULL);
assert(accelerometerEventQueue != NULL);
ASensorEventQueue_setEventRate(accelerometerEventQueue,
accelerometer,
int32_t(1000000 / SENSOR_REFRESH_RATE));
auto status = ASensorEventQueue_enableSensor(accelerometerEventQueue,
accelerometer);
assert(status >= 0);
(void)status; //to silent unused compiler warning
generateXPos();
}
void surfaceCreated() {
LOGI("GL_VERSION: %s", glGetString(GL_VERSION));
LOGI("GL_VENDOR: %s", glGetString(GL_VENDOR));
LOGI("GL_RENDERER: %s", glGetString(GL_RENDERER));
LOGI("GL_EXTENSIONS: %s", glGetString(GL_EXTENSIONS));
shaderProgram = createProgram(vertexShaderSource, fragmentShaderSource);
assert(shaderProgram != 0);
GLint getPositionLocationResult = glGetAttribLocation(shaderProgram, "vPosition");
assert(getPositionLocationResult != -1);
vPositionHandle = (GLuint)getPositionLocationResult;
GLint getSensorValueLocationResult = glGetAttribLocation(shaderProgram, "vSensorValue");
assert(getSensorValueLocationResult != -1);
vSensorValueHandle = (GLuint)getSensorValueLocationResult;
GLint getFragColorLocationResult = glGetUniformLocation(shaderProgram, "uFragColor");
assert(getFragColorLocationResult != -1);
uFragColorHandle = (GLuint)getFragColorLocationResult;
}
void surfaceChanged(int w, int h) {
glViewport(0, 0, w, h);
}
void generateXPos() {
for (auto i = 0; i < SENSOR_HISTORY_LENGTH; i++) {
float t = static_cast<float>(i) / static_cast<float>(SENSOR_HISTORY_LENGTH - 1);
xPos[i] = -1.f * (1.f - t) + 1.f * t;
}
}
GLuint createProgram(const std::string& pVertexSource, const std::string& pFragmentSource) {
GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource);
GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource);
GLuint program = glCreateProgram();
assert(program != 0);
glAttachShader(program, vertexShader);
glAttachShader(program, pixelShader);
glLinkProgram(program);
GLint programLinked = 0;
glGetProgramiv(program, GL_LINK_STATUS, &programLinked);
assert(programLinked != 0);
glDeleteShader(vertexShader);
glDeleteShader(pixelShader);
return program;
}
GLuint loadShader(GLenum shaderType, const std::string& pSource) {
GLuint shader = glCreateShader(shaderType);
assert(shader != 0);
const char *sourceBuf = pSource.c_str();
glShaderSource(shader, 1, &sourceBuf, NULL);
glCompileShader(shader);
GLint shaderCompiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &shaderCompiled);
assert(shaderCompiled != 0);
return shader;
}
void update() {
ALooper_pollAll(0, NULL, NULL, NULL);
ASensorEvent event;
float a = SENSOR_FILTER_ALPHA;
while (ASensorEventQueue_getEvents(accelerometerEventQueue, &event, 1) > 0) {
sensorDataFilter.x = a * event.acceleration.x + (1.0f - a) * sensorDataFilter.x;
sensorDataFilter.y = a * event.acceleration.y + (1.0f - a) * sensorDataFilter.y;
sensorDataFilter.z = a * event.acceleration.z + (1.0f - a) * sensorDataFilter.z;
}
sensorData[sensorDataIndex] = sensorDataFilter;
sensorData[SENSOR_HISTORY_LENGTH+sensorDataIndex] = sensorDataFilter;
sensorDataIndex = (sensorDataIndex + 1) % SENSOR_HISTORY_LENGTH;
}
void render() {
glClearColor(0.f, 0.f, 0.f, 1.0f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
glEnableVertexAttribArray(vPositionHandle);
glVertexAttribPointer(vPositionHandle, 1, GL_FLOAT, GL_FALSE, 0, xPos);
glEnableVertexAttribArray(vSensorValueHandle);
glVertexAttribPointer(vSensorValueHandle, 1, GL_FLOAT, GL_FALSE, sizeof(AccelerometerData),
&sensorData[sensorDataIndex].x);
glUniform4f(uFragColorHandle, 1.0f, 1.0f, 0.0f, 1.0f);
glDrawArrays(GL_LINE_STRIP, 0, SENSOR_HISTORY_LENGTH);
glVertexAttribPointer(vSensorValueHandle, 1, GL_FLOAT, GL_FALSE, sizeof(AccelerometerData),
&sensorData[sensorDataIndex].y);
glUniform4f(uFragColorHandle, 1.0f, 0.0f, 1.0f, 1.0f);
glDrawArrays(GL_LINE_STRIP, 0, SENSOR_HISTORY_LENGTH);
glVertexAttribPointer(vSensorValueHandle, 1, GL_FLOAT, GL_FALSE, sizeof(AccelerometerData),
&sensorData[sensorDataIndex].z);
glUniform4f(uFragColorHandle, 0.0f, 1.0f, 1.0f, 1.0f);
glDrawArrays(GL_LINE_STRIP, 0, SENSOR_HISTORY_LENGTH);
}
void pause() {
ASensorEventQueue_disableSensor(accelerometerEventQueue, accelerometer);
}
void resume() {
ASensorEventQueue_enableSensor(accelerometerEventQueue, accelerometer);
}
};
sensorgraph gSensorGraph;
extern "C" {
JNIEXPORT void JNICALL
Java_com_android_accelerometergraph_AccelerometerGraphJNI_init(
JNIEnv *env, jclass type, jobject assetManager) {
(void)type;
AAssetManager *nativeAssetManager = AAssetManager_fromJava(env, assetManager);
gSensorGraph.init(nativeAssetManager);
}
JNIEXPORT void JNICALL
Java_com_android_accelerometergraph_AccelerometerGraphJNI_surfaceCreated(JNIEnv *env, jclass type) {
(void)env;
(void)type;
gSensorGraph.surfaceCreated();
}
JNIEXPORT void JNICALL
Java_com_android_accelerometergraph_AccelerometerGraphJNI_surfaceChanged(
JNIEnv *env, jclass type, jint width, jint height) {
(void)env;
(void)type;
gSensorGraph.surfaceChanged(width, height);
}
JNIEXPORT void JNICALL
Java_com_android_accelerometergraph_AccelerometerGraphJNI_drawFrame(
JNIEnv *env, jclass type) {
(void)env;
(void)type;
gSensorGraph.update();
gSensorGraph.render();
}
JNIEXPORT void JNICALL
Java_com_android_accelerometergraph_AccelerometerGraphJNI_pause(
JNIEnv *env, jclass type) {
(void)env;
(void)type;
gSensorGraph.pause();
}
JNIEXPORT void JNICALL
Java_com_android_accelerometergraph_AccelerometerGraphJNI_resume(
JNIEnv *env, jclass type) {
(void)env;
(void)type;
gSensorGraph.resume();
}
}

View File

@@ -0,0 +1,81 @@
/*
* 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.accelerometergraph;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class AccelerometerGraphActivity extends Activity {
GLSurfaceView mView;
@Override protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
mView = new GLSurfaceView(getApplication());
mView.setEGLContextClientVersion(2);
mView.setRenderer(new GLSurfaceView.Renderer() {
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
AccelerometerGraphJNI.surfaceCreated();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
AccelerometerGraphJNI.surfaceChanged(width, height);
}
@Override
public void onDrawFrame(GL10 gl) {
AccelerometerGraphJNI.drawFrame();
}
});
mView.queueEvent(new Runnable() {
@Override
public void run() {
AccelerometerGraphJNI.init(getAssets());
}
});
setContentView(mView);
}
@Override protected void onPause() {
super.onPause();
mView.onPause();
mView.queueEvent(new Runnable() {
@Override
public void run() {
AccelerometerGraphJNI.pause();
}
});
}
@Override protected void onResume() {
super.onResume();
mView.onResume();
mView.queueEvent(new Runnable() {
@Override
public void run() {
AccelerometerGraphJNI.resume();
}
});
}
}

View File

@@ -0,0 +1,35 @@
/*
* 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.accelerometergraph;
// Wrapper for native library
import android.content.res.AssetManager;
public class AccelerometerGraphJNI {
static {
System.loadLibrary("accelerometergraph");
}
public static native void init(AssetManager assetManager);
public static native void surfaceCreated();
public static native void surfaceChanged(int width, int height);
public static native void drawFrame();
public static native void pause();
public static native void resume();
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

View File

@@ -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="accelerometergraph_activity">Accelerometer Graph</string>
</resources>