Update native_activity sample code to use ANativeWindow.
We now get the native window to the engine, all ready for something to be done with it. Change-Id: Ie4537e505cdf8a2fffc28de82b7e3cda448fde41
This commit is contained in:
@@ -24,15 +24,12 @@
|
|||||||
#include <jni.h>
|
#include <jni.h>
|
||||||
|
|
||||||
#include <android/input.h>
|
#include <android/input.h>
|
||||||
|
#include <android/native_window.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Temporary until native surface API is defined.
|
|
||||||
struct ASurfaceHolder;
|
|
||||||
typedef struct ASurfaceHolder ASurfaceHolder;
|
|
||||||
|
|
||||||
struct ANativeActivityCallbacks;
|
struct ANativeActivityCallbacks;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -129,30 +126,28 @@ typedef struct ANativeActivityCallbacks {
|
|||||||
void (*onWindowFocusChanged)(ANativeActivity* activity, int hasFocus);
|
void (*onWindowFocusChanged)(ANativeActivity* activity, int hasFocus);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The drawing surface for this native activity has been created. You
|
* The drawing window for this native activity has been created. You
|
||||||
* can use the given surface object to start drawing. NOTE: surface
|
* can use the given native window object to start drawing.
|
||||||
* drawing API is not yet defined.
|
|
||||||
*/
|
*/
|
||||||
void (*onSurfaceCreated)(ANativeActivity* activity, ASurfaceHolder* surface);
|
void (*onNativeWindowCreated)(ANativeActivity* activity, ANativeWindow* window);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The drawing surface for this native activity has changed. The surface
|
* The drawing window for this native activity has changed. During this time,
|
||||||
* given here is guaranteed to be the same as the one last given to
|
* old ANativeWindow object is still valid but no longer active and drawing
|
||||||
* onSurfaceCreated. This is simply to inform you about interesting
|
* should switch to the new ANativeWindow given here. After returning from
|
||||||
* changed to that surface.
|
* this function, you must not touch the old window.
|
||||||
*/
|
*/
|
||||||
void (*onSurfaceChanged)(ANativeActivity* activity, ASurfaceHolder* surface,
|
void (*onNativeWindowChanged)(ANativeActivity* activity, ANativeWindow* window);
|
||||||
int format, int width, int height);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The drawing surface for this native activity is going to be destroyed.
|
* The drawing window for this native activity is going to be destroyed.
|
||||||
* You MUST ensure that you do not touch the surface object after returning
|
* You MUST ensure that you do not touch the window object after returning
|
||||||
* from this function: in the common case of drawing to the surface from
|
* from this function: in the common case of drawing to the window from
|
||||||
* another thread, that means the implementation of this callback must
|
* another thread, that means the implementation of this callback must
|
||||||
* properly synchronize with the other thread to stop its drawing before
|
* properly synchronize with the other thread to stop its drawing before
|
||||||
* returning from here.
|
* returning from here.
|
||||||
*/
|
*/
|
||||||
void (*onSurfaceDestroyed)(ANativeActivity* activity, ASurfaceHolder* surface);
|
void (*onNativeWindowDestroyed)(ANativeActivity* activity, ANativeWindow* window);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The input queue for this native activity's window has been created.
|
* The input queue for this native activity's window has been created.
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef ANDROID_NATIVE_WINDOW_H
|
||||||
|
#define ANDROID_NATIVE_WINDOW_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct ANativeWindow;
|
||||||
|
typedef struct ANativeWindow ANativeWindow;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Return the current width in pixels of the window surface. Returns a
|
||||||
|
* negative value on error.
|
||||||
|
*/
|
||||||
|
int32_t ANativeWindow_getWidth(ANativeWindow* window);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Return the current height in pixels of the window surface. Returns a
|
||||||
|
* negative value on error.
|
||||||
|
*/
|
||||||
|
int32_t ANativeWindow_getHeight(ANativeWindow* window);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Return the current pixel format of the window surface. Returns a
|
||||||
|
* negative value on error.
|
||||||
|
*/
|
||||||
|
int32_t ANativeWindow_getFormat(ANativeWindow* window);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // ANDROID_NATIVE_WINDOW_H
|
||||||
Binary file not shown.
@@ -35,7 +35,6 @@ struct engine {
|
|||||||
|
|
||||||
int msgread;
|
int msgread;
|
||||||
int msgwrite;
|
int msgwrite;
|
||||||
int msgpipe[2];
|
|
||||||
|
|
||||||
ANativeActivity* activity;
|
ANativeActivity* activity;
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
@@ -43,11 +42,14 @@ struct engine {
|
|||||||
int running;
|
int running;
|
||||||
int destroyed;
|
int destroyed;
|
||||||
AInputQueue* inputQueue;
|
AInputQueue* inputQueue;
|
||||||
|
ANativeWindow* window;
|
||||||
|
AInputQueue* pendingInputQueue;
|
||||||
|
ANativeWindow* pendingWindow;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
ENGINE_CMD_GAIN_INPUT,
|
ENGINE_CMD_INPUT_CHANGED,
|
||||||
ENGINE_CMD_LOSE_INPUT,
|
ENGINE_CMD_WINDOW_CHANGED,
|
||||||
ENGINE_CMD_DESTROY,
|
ENGINE_CMD_DESTROY,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -90,13 +92,17 @@ static void* engine_entry(void* param) {
|
|||||||
int8_t cmd;
|
int8_t cmd;
|
||||||
if (read(engine->msgread, &cmd, sizeof(cmd)) == sizeof(cmd)) {
|
if (read(engine->msgread, &cmd, sizeof(cmd)) == sizeof(cmd)) {
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case ENGINE_CMD_GAIN_INPUT:
|
case ENGINE_CMD_INPUT_CHANGED:
|
||||||
LOGI("Engine: ENGINE_CMD_GAIN_INPUT\n");
|
LOGI("Engine: ENGINE_CMD_INPUT_CHANGED\n");
|
||||||
break;
|
|
||||||
case ENGINE_CMD_LOSE_INPUT:
|
|
||||||
LOGI("Engine: ENGINE_CMD_LOSE_INPUT\n");
|
|
||||||
pthread_mutex_lock(&engine->mutex);
|
pthread_mutex_lock(&engine->mutex);
|
||||||
engine->inputQueue = NULL;
|
engine->inputQueue = engine->pendingInputQueue;
|
||||||
|
pthread_cond_broadcast(&engine->cond);
|
||||||
|
pthread_mutex_unlock(&engine->mutex);
|
||||||
|
break;
|
||||||
|
case ENGINE_CMD_WINDOW_CHANGED:
|
||||||
|
LOGI("Engine: ENGINE_CMD_WINDOW_CHANGED\n");
|
||||||
|
pthread_mutex_lock(&engine->mutex);
|
||||||
|
engine->window = engine->pendingWindow;
|
||||||
pthread_cond_broadcast(&engine->cond);
|
pthread_cond_broadcast(&engine->cond);
|
||||||
pthread_mutex_unlock(&engine->mutex);
|
pthread_mutex_unlock(&engine->mutex);
|
||||||
break;
|
break;
|
||||||
@@ -113,7 +119,7 @@ static void* engine_entry(void* param) {
|
|||||||
LOGI("Failure reading engine cmd: %s\n", strerror(errno));
|
LOGI("Failure reading engine cmd: %s\n", strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (pfd[1].revents == POLLIN) {
|
} else if (engine->inputQueue != NULL && pfd[1].revents == POLLIN) {
|
||||||
AInputEvent* event = NULL;
|
AInputEvent* event = NULL;
|
||||||
if (AInputQueue_getEvent(engine->inputQueue, &event) >= 0) {
|
if (AInputQueue_getEvent(engine->inputQueue, &event) >= 0) {
|
||||||
LOGI("New input event: type=%d\n", AInputEvent_getType(event));
|
LOGI("New input event: type=%d\n", AInputEvent_getType(event));
|
||||||
@@ -163,15 +169,19 @@ static void engine_write_cmd(struct engine* engine, int8_t cmd) {
|
|||||||
|
|
||||||
static void engine_set_input(struct engine* engine, AInputQueue* inputQueue) {
|
static void engine_set_input(struct engine* engine, AInputQueue* inputQueue) {
|
||||||
pthread_mutex_lock(&engine->mutex);
|
pthread_mutex_lock(&engine->mutex);
|
||||||
engine->inputQueue = inputQueue;
|
engine->pendingInputQueue = inputQueue;
|
||||||
engine_write_cmd(engine, ENGINE_CMD_GAIN_INPUT);
|
engine_write_cmd(engine, ENGINE_CMD_INPUT_CHANGED);
|
||||||
|
while (engine->inputQueue != engine->pendingInputQueue) {
|
||||||
|
pthread_cond_wait(&engine->cond, &engine->mutex);
|
||||||
|
}
|
||||||
pthread_mutex_unlock(&engine->mutex);
|
pthread_mutex_unlock(&engine->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void engine_clear_input(struct engine* engine) {
|
static void engine_set_window(struct engine* engine, ANativeWindow* window) {
|
||||||
pthread_mutex_lock(&engine->mutex);
|
pthread_mutex_lock(&engine->mutex);
|
||||||
engine_write_cmd(engine, ENGINE_CMD_LOSE_INPUT);
|
engine->pendingWindow = window;
|
||||||
while (engine->inputQueue != NULL) {
|
engine_write_cmd(engine, ENGINE_CMD_WINDOW_CHANGED);
|
||||||
|
while (engine->window != engine->pendingWindow) {
|
||||||
pthread_cond_wait(&engine->cond, &engine->mutex);
|
pthread_cond_wait(&engine->cond, &engine->mutex);
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&engine->mutex);
|
pthread_mutex_unlock(&engine->mutex);
|
||||||
@@ -185,8 +195,8 @@ static void engine_destroy(struct engine* engine) {
|
|||||||
}
|
}
|
||||||
pthread_mutex_unlock(&engine->mutex);
|
pthread_mutex_unlock(&engine->mutex);
|
||||||
|
|
||||||
close(engine->msgpipe[0]);
|
close(engine->msgread);
|
||||||
close(engine->msgpipe[1]);
|
close(engine->msgwrite);
|
||||||
pthread_cond_destroy(&engine->cond);
|
pthread_cond_destroy(&engine->cond);
|
||||||
pthread_mutex_destroy(&engine->mutex);
|
pthread_mutex_destroy(&engine->mutex);
|
||||||
}
|
}
|
||||||
@@ -233,21 +243,22 @@ static void onWindowFocusChanged(ANativeActivity* activity, int focused)
|
|||||||
LOGI("WindowFocusChanged: %p -- %d\n", activity, focused);
|
LOGI("WindowFocusChanged: %p -- %d\n", activity, focused);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void onSurfaceCreated(ANativeActivity* activity, ASurfaceHolder* surface)
|
static void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow* window)
|
||||||
{
|
{
|
||||||
LOGI("SurfaceCreated: %p -- %p\n", activity, surface);
|
LOGI("NativeWindowCreated: %p -- %p\n", activity, window);
|
||||||
|
engine_set_window((struct engine*)activity->instance, window);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void onSurfaceChanged(ANativeActivity* activity, ASurfaceHolder* surface,
|
static void onNativeWindowChanged(ANativeActivity* activity, ANativeWindow* window)
|
||||||
int format, int width, int height)
|
|
||||||
{
|
{
|
||||||
LOGI("SurfaceChanged: %p -- %p fmt=%d w=%d h=%d\n", activity, surface,
|
LOGI("NativeWindowChanged: %p -- %p\n", activity, window);
|
||||||
format, width, height);
|
engine_set_window((struct engine*)activity->instance, window);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void onSurfaceDestroyed(ANativeActivity* activity, ASurfaceHolder* surface)
|
static void onNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow* window)
|
||||||
{
|
{
|
||||||
LOGI("SurfaceDestroyed: %p -- %p\n", activity, surface);
|
LOGI("NativeWindowDestroyed: %p -- %p\n", activity, window);
|
||||||
|
engine_set_window((struct engine*)activity->instance, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void onInputQueueCreated(ANativeActivity* activity, AInputQueue* queue)
|
static void onInputQueueCreated(ANativeActivity* activity, AInputQueue* queue)
|
||||||
@@ -259,7 +270,7 @@ static void onInputQueueCreated(ANativeActivity* activity, AInputQueue* queue)
|
|||||||
static void onInputQueueDestroyed(ANativeActivity* activity, AInputQueue* queue)
|
static void onInputQueueDestroyed(ANativeActivity* activity, AInputQueue* queue)
|
||||||
{
|
{
|
||||||
LOGI("InputQueueDestroyed: %p -- %p\n", activity, queue);
|
LOGI("InputQueueDestroyed: %p -- %p\n", activity, queue);
|
||||||
engine_clear_input((struct engine*)activity->instance);
|
engine_set_input((struct engine*)activity->instance, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ANativeActivity_onCreate(ANativeActivity* activity,
|
void ANativeActivity_onCreate(ANativeActivity* activity,
|
||||||
@@ -274,9 +285,9 @@ void ANativeActivity_onCreate(ANativeActivity* activity,
|
|||||||
activity->callbacks->onStop = onStop;
|
activity->callbacks->onStop = onStop;
|
||||||
activity->callbacks->onLowMemory = onLowMemory;
|
activity->callbacks->onLowMemory = onLowMemory;
|
||||||
activity->callbacks->onWindowFocusChanged = onWindowFocusChanged;
|
activity->callbacks->onWindowFocusChanged = onWindowFocusChanged;
|
||||||
activity->callbacks->onSurfaceCreated = onSurfaceCreated;
|
activity->callbacks->onNativeWindowCreated = onNativeWindowCreated;
|
||||||
activity->callbacks->onSurfaceChanged = onSurfaceChanged;
|
activity->callbacks->onNativeWindowChanged = onNativeWindowChanged;
|
||||||
activity->callbacks->onSurfaceDestroyed = onSurfaceDestroyed;
|
activity->callbacks->onNativeWindowDestroyed = onNativeWindowDestroyed;
|
||||||
activity->callbacks->onInputQueueCreated = onInputQueueCreated;
|
activity->callbacks->onInputQueueCreated = onInputQueueCreated;
|
||||||
activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed;
|
activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user