想要用surfaceview的游戏模式,如果不需要系统UI体系的话,可以直接走NativeActivity模式,直接对接window对应的layer,而不需要走textureview的方案,先转到texture上。也不需要走surfaceview的方案,要重新申请一个layer。而是直接使用原来系统UI使用的Layer,移除系统UI
https://github.com/android/ndk-samples/tree/main/native-activity
classpath 'com.android.tools.build:gradle:3.2.1'
distributionUrl=https://services.gradle.org/distributions/gradle-4.6-all.zip
Copyright (C) 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.
cmake_minimum_required(VERSION 3.4.1)
build native_app_glue as a static lib
set({CMAKE_C_FLAGS}")
add_library(native_app_glue STATIC
${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)
now build app's shared lib
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11 -Wall -Werror")
Export ANativeActivity_onCreate(),
Refer to: https://github.com/android-ndk/ndk/issues/381.
set(CMAKE_SHARED_LINKER_FLAGS
"${CMAKE_SHARED_LINKER_FLAGS} -u ANativeActivity_onCreate")
add_library(native-activity SHARED main.cpp)
target_include_directories(native-activity PRIVATE
${ANDROID_NDK}/sources/android/native_app_glue)
add lib dependencies
target_link_libraries(native-activity
android
native_app_glue
EGL
GLESv2
log)
/*
- 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.
*/
//BEGIN_INCLUDE(all)
include
include
include
include
include
include
include
include
include
include
include
include
include
include
include
include
include
define LOG_TAG "native-activity"
define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,VA_ARGS)
define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,VA_ARGS)
define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "native-activity", VA_ARGS))
/**
- Our saved state data.
*/
struct saved_state {
float angle;
int32_t x;
int32_t y;
};
/**
-
Shared state for our app.
*/
struct engine {
struct android_app *app;ASensorManager *sensorManager;
const ASensor *accelerometerSensor;
ASensorEventQueue *sensorEventQueue;int animating;
EGLDisplay display;
EGLSurface surface;
EGLContext context;
int32_t width;
int32_t height;
struct saved_state state;
};
static void printGLString(const char *name, GLenum s) {
const char *v = (const char *) glGetString(s);
LOGI("GL %s = %s\n", name, v);
}
static void checkGlError(const char *op) {
for (GLint error = glGetError(); error; error
= glGetError()) {
LOGI("after %s() glError (0x%x)\n", op, error);
}
}
auto gVertexShader =
"attribute vec4 vPosition;\n"
"void main() {\n"
" gl_Position = vPosition;\n"
"}\n";
auto gFragmentShader =
"precision mediump float;\n"
"void main() {\n"
" gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
"}\n";
GLuint loadShader(GLenum shaderType, const char *pSource) {
GLuint shader = glCreateShader(shaderType);
if (shader) {
glShaderSource(shader, 1, &pSource, NULL);
glCompileShader(shader);
GLint compiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
GLint infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen) {
char *buf = (char *) malloc(infoLen);
if (buf) {
glGetShaderInfoLog(shader, infoLen, NULL, buf);
LOGE("Could not compile shader %d:\n%s\n",
shaderType, buf);
free(buf);
}
glDeleteShader(shader);
shader = 0;
}
}
}
return shader;
}
GLuint createProgram(const char *pVertexSource, const char *pFragmentSource) {
GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource);
if (!vertexShader) {
return 0;
}
GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource);
if (!pixelShader) {
return 0;
}
GLuint program = glCreateProgram();
if (program) {
glAttachShader(program, vertexShader);
checkGlError("glAttachShader");
glAttachShader(program, pixelShader);
checkGlError("glAttachShader");
glLinkProgram(program);
GLint linkStatus = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
if (linkStatus != GL_TRUE) {
GLint bufLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
if (bufLength) {
char *buf = (char *) malloc(bufLength);
if (buf) {
glGetProgramInfoLog(program, bufLength, NULL, buf);
LOGE("Could not link program:\n%s\n", buf);
free(buf);
}
}
glDeleteProgram(program);
program = 0;
}
}
return program;
}
GLuint gProgram;
GLuint gvPositionHandle;
bool setupGraphics(int w, int h) {
printGLString("Version", GL_VERSION);
printGLString("Vendor", GL_VENDOR);
printGLString("Renderer", GL_RENDERER);
printGLString("Extensions", GL_EXTENSIONS);
LOGI("setupGraphics(%d, %d)", w, h);
gProgram = createProgram(gVertexShader, gFragmentShader);
if (!gProgram) {
LOGE("Could not create program.");
return false;
}
gvPositionHandle = glGetAttribLocation(gProgram, "vPosition");
checkGlError("glGetAttribLocation");
LOGI("glGetAttribLocation(\"vPosition\") = %d\n",
gvPositionHandle);
glViewport(0, 0, w / 2, h / 2);
checkGlError("glViewport");
return true;
}
GLfloat gTriangleVertices[] = {0.0f, 0.5f, -0.5f, -0.5f,
0.5f, -0.5f};
void renderFrame() {
static float grey;
grey += 0.01f;
if (grey > 1.0f) {
grey = 0.0f;
}
// glClearColor(grey, grey, grey, 1.0f);
// checkGlError("glClearColor");
// glClear(GL_COLOR_BUFFER_BIT);
// checkGlError("glClear");
glUseProgram(gProgram);
checkGlError("glUseProgram");
gTriangleVertices[1] = gTriangleVertices[1] + 0.01;
gTriangleVertices[3] = gTriangleVertices[3] + 0.01;
gTriangleVertices[5] = gTriangleVertices[5] + 0.01;
glVertexAttribPointer(gvPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, gTriangleVertices);
checkGlError("glVertexAttribPointer");
glEnableVertexAttribArray(gvPositionHandle);
checkGlError("glEnableVertexAttribArray");
glDrawArrays(GL_TRIANGLES, 0, 3);
checkGlError("glDrawArrays");
}
/**
-
Initialize an EGL context for the current display.
*/
static int engine_init_display(struct engine *engine) {
// initialize OpenGL ES and EGL/*
- Here specify the attributes of the desired configuration.
- Below, we select an EGLConfig with at least 8 bits per color
- component compatible with on-screen windows
*/
const EGLint attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_NONE
};
EGLint w, h, format;
EGLint numConfigs;
EGLConfig config = nullptr;
EGLSurface surface;
EGLContext context;
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
eglInitialize(display, nullptr, nullptr);
/* Here, the application chooses the configuration it desires.
-
find the best match if possible, otherwise use the very first one
*/
eglChooseConfig(display, attribs, nullptr, 0, &numConfigs);
std::unique_ptrsupportedConfigs(new EGLConfig[numConfigs]);
assert(supportedConfigs);
eglChooseConfig(display, attribs, supportedConfigs.get(), numConfigs, &numConfigs);
assert(numConfigs);
auto i = 0;
for (; i < numConfigs; i++) {
auto &cfg = supportedConfigs[i];
EGLint r, g, b, d;
if (eglGetConfigAttrib(display, cfg, EGL_RED_SIZE, &r) &&
eglGetConfigAttrib(display, cfg, EGL_GREEN_SIZE, &g) &&
eglGetConfigAttrib(display, cfg, EGL_BLUE_SIZE, &b) &&
eglGetConfigAttrib(display, cfg, EGL_DEPTH_SIZE, &d) &&
r == 8 && g == 8 && b == 8 && d == 0) {config = supportedConfigs[i]; break;
}
}
if (i == numConfigs) {
config = supportedConfigs[0];
}
if (config == nullptr) {
LOGW("Unable to initialize EGLConfig");
return -1;
}/* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is
- guaranteed to be accepted by ANativeWindow_setBuffersGeometry().
- As soon as we picked a EGLConfig, we can safely reconfigure the
- ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */
eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
surface = eglCreateWindowSurface(display, config, engine->app->window, nullptr);
const int attrib_list[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
context = eglCreateContext(display, config, nullptr, attrib_list);
// EGLAPI EGLBoolean EGLAPIENTRY eglSurfaceAttrib (EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value);
EGLBoolean ok = eglSurfaceAttrib(display, surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_PRESERVED);
LOGI("EGL_SWAP_BEHAVIOR_PRESERVED_BIT : %d %d", ok, eglGetError());
// EGL_BAD_ATTRIBUTE
if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
LOGW("Unable to eglMakeCurrent");
return -1;
}
eglQuerySurface(display, surface, EGL_WIDTH, &w);
eglQuerySurface(display, surface, EGL_HEIGHT, &h);
engine->display = display;
engine->context = context;
engine->surface = surface;
engine->width = w;
engine->height = h;
engine->state.angle = 0;
// Check openGL on the system
auto opengl_info = {GL_VENDOR, GL_RENDERER, GL_VERSION, GL_EXTENSIONS};
for (auto name : opengl_info) {
auto info = glGetString(name);
LOGI("OpenGL Info: %s", info);
}
// Initialize GL state.
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
glEnable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
setupGraphics(w, h);
// glViewport(0, 0, w / 2, h / 2);
return 0;
}
/**
-
Just the current frame in the display.
*/
static void engine_draw_frame(struct engine *engine) {
if (engine->display == nullptr) {
// No display.
return;
}// Just fill the screen with a color.
// glClearColor(((float) engine->state.x) / engine->width, engine->state.angle,
// ((float) engine->state.y) / engine->height, 1);
// glClear(GL_COLOR_BUFFER_BIT);renderFrame();
eglSwapBuffers(engine->display, engine->surface);
}
/**
- Tear down the EGL context currently associated with the display.
*/
static void engine_term_display(struct engine *engine) {
if (engine->display != EGL_NO_DISPLAY) {
eglMakeCurrent(engine->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (engine->context != EGL_NO_CONTEXT) {
eglDestroyContext(engine->display, engine->context);
}
if (engine->surface != EGL_NO_SURFACE) {
eglDestroySurface(engine->display, engine->surface);
}
eglTerminate(engine->display);
}
engine->animating = 0;
engine->display = EGL_NO_DISPLAY;
engine->context = EGL_NO_CONTEXT;
engine->surface = EGL_NO_SURFACE;
}
/**
- Process the next input event.
*/
static int32_t engine_handle_input(struct android_app *app, AInputEvent *event) {
auto *engine = (struct engine *) app->userData;
if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
engine->animating = 1;
engine->state.x = AMotionEvent_getX(event, 0);
engine->state.y = AMotionEvent_getY(event, 0);
return 1;
}
return 0;
}
/**
- Process the next main command.
*/
static void engine_handle_cmd(struct android_app *app, int32_t cmd) {
auto *engine = (struct engine *) app->userData;
switch (cmd) {
case APP_CMD_SAVE_STATE:
// The system has asked us to save our current state. Do so.
engine->app->savedState = malloc(sizeof(struct saved_state));
*((struct saved_state *) engine->app->savedState) = engine->state;
engine->app->savedStateSize = sizeof(struct saved_state);
break;
case APP_CMD_INIT_WINDOW:
// The window is being shown, get it ready.
if (engine->app->window != nullptr) {
engine_init_display(engine);
engine_draw_frame(engine);
}
break;
case APP_CMD_TERM_WINDOW:
// The window is being hidden or closed, clean it up.
engine_term_display(engine);
break;
case APP_CMD_GAINED_FOCUS:
// When our app gains focus, we start monitoring the accelerometer.
if (engine->accelerometerSensor != nullptr) {
ASensorEventQueue_enableSensor(engine->sensorEventQueue,
engine->accelerometerSensor);
// We'd like to get 60 events per second (in us).
ASensorEventQueue_setEventRate(engine->sensorEventQueue,
engine->accelerometerSensor,
(1000L / 60) * 1000);
}
break;
case APP_CMD_LOST_FOCUS:
// When our app loses focus, we stop monitoring the accelerometer.
// This is to avoid consuming battery while not being used.
if (engine->accelerometerSensor != nullptr) {
ASensorEventQueue_disableSensor(engine->sensorEventQueue,
engine->accelerometerSensor);
}
// Also stop animating.
engine->animating = 0;
engine_draw_frame(engine);
break;
default:
break;
}
}
/*
- AcquireASensorManagerInstance(void)
- Workaround ASensorManager_getInstance() deprecation false alarm
- for Android-N and before, when compiling with NDK-r15
*/
include
ASensorManager *AcquireASensorManagerInstance(android_app *app) {
if (!app)
return nullptr;
typedef ASensorManager *(*PF_GETINSTANCEFORPACKAGE)(const char *name);
void *androidHandle = dlopen("libandroid.so", RTLD_NOW);
auto getInstanceForPackageFunc = (PF_GETINSTANCEFORPACKAGE)
dlsym(androidHandle, "ASensorManager_getInstanceForPackage");
if (getInstanceForPackageFunc) {
JNIEnv *env = nullptr;
app->activity->vm->AttachCurrentThread(&env, nullptr);
jclass android_content_Context = env->GetObjectClass(app->activity->clazz);
jmethodID midGetPackageName = env->GetMethodID(android_content_Context,
"getPackageName",
"()Ljava/lang/String;");
auto packageName = (jstring) env->CallObjectMethod(app->activity->clazz,
midGetPackageName);
const char *nativePackageName = env->GetStringUTFChars(packageName, nullptr);
ASensorManager *mgr = getInstanceForPackageFunc(nativePackageName);
env->ReleaseStringUTFChars(packageName, nativePackageName);
app->activity->vm->DetachCurrentThread();
if (mgr) {
dlclose(androidHandle);
return mgr;
}
}
typedef ASensorManager *(*PF_GETINSTANCE)();
auto getInstanceFunc = (PF_GETINSTANCE)
dlsym(androidHandle, "ASensorManager_getInstance");
// by all means at this point, ASensorManager_getInstance should be available
assert(getInstanceFunc);
dlclose(androidHandle);
return getInstanceFunc();
}
/**
This is the main entry point of a native application that is using
android_native_app_glue. It runs in its own thread, with its own
-
event loop for receiving input events and doing other things.
*/
void android_main(struct android_app *state) {
struct engine engine{};memset(&engine, 0, sizeof(engine));
state->userData = &engine;
state->onAppCmd = engine_handle_cmd;
state->onInputEvent = engine_handle_input;
engine.app = state;// Prepare to monitor accelerometer
engine.sensorManager = AcquireASensorManagerInstance(state);
engine.accelerometerSensor = ASensorManager_getDefaultSensor(
engine.sensorManager,
ASENSOR_TYPE_ACCELEROMETER);
engine.sensorEventQueue = ASensorManager_createEventQueue(
engine.sensorManager,
state->looper, LOOPER_ID_USER,
nullptr, nullptr);if (state->savedState != nullptr) {
// We are starting with a previous saved state; restore from it.
engine.state = *(struct saved_state *) state->savedState;
}// loop waiting for stuff to do.
while (true) {
// Read all pending events.
int ident;
int events;
struct android_poll_source *source;// If not animating, we will block forever waiting for events. // If animating, we loop until all events are read, then continue // to draw the next frame of animation. while ((ident = ALooper_pollAll(engine.animating ? 0 : -1, nullptr, &events, (void **) &source)) >= 0) { // Process this event. if (source != nullptr) { source->process(state, source); } // If a sensor has data, process it now. if (ident == LOOPER_ID_USER) { if (engine.accelerometerSensor != nullptr) { ASensorEvent event; while (ASensorEventQueue_getEvents(engine.sensorEventQueue, &event, 1) > 0) { LOGI("accelerometer: x=%f y=%f z=%f", event.acceleration.x, event.acceleration.y, event.acceleration.z); } } } // Check if we are exiting. if (state->destroyRequested != 0) { engine_term_display(&engine); return; } } if (engine.animating) { // Done with events; draw next animation frame. engine.state.angle += .01f; if (engine.state.angle > 1) { engine.state.angle = 0; } // Drawing is throttled to the screen update rate, so there // is no need to do timing here. engine_draw_frame(&engine); }
}
}
//END_INCLUDE(all)