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:
Dianne Hackborn
2010-06-30 18:36:34 -07:00
parent 24ff92954b
commit 88510488e9
4 changed files with 103 additions and 47 deletions

View File

@@ -24,15 +24,12 @@
#include <jni.h>
#include <android/input.h>
#include <android/native_window.h>
#ifdef __cplusplus
extern "C" {
#endif
// Temporary until native surface API is defined.
struct ASurfaceHolder;
typedef struct ASurfaceHolder ASurfaceHolder;
struct ANativeActivityCallbacks;
/**
@@ -129,30 +126,28 @@ typedef struct ANativeActivityCallbacks {
void (*onWindowFocusChanged)(ANativeActivity* activity, int hasFocus);
/**
* The drawing surface for this native activity has been created. You
* can use the given surface object to start drawing. NOTE: surface
* drawing API is not yet defined.
* The drawing window for this native activity has been created. You
* can use the given native window object to start drawing.
*/
void (*onSurfaceCreated)(ANativeActivity* activity, ASurfaceHolder* surface);
void (*onNativeWindowCreated)(ANativeActivity* activity, ANativeWindow* window);
/**
* The drawing surface for this native activity has changed. The surface
* given here is guaranteed to be the same as the one last given to
* onSurfaceCreated. This is simply to inform you about interesting
* changed to that surface.
* The drawing window for this native activity has changed. During this time,
* old ANativeWindow object is still valid but no longer active and drawing
* should switch to the new ANativeWindow given here. After returning from
* this function, you must not touch the old window.
*/
void (*onSurfaceChanged)(ANativeActivity* activity, ASurfaceHolder* surface,
int format, int width, int height);
void (*onNativeWindowChanged)(ANativeActivity* activity, ANativeWindow* window);
/**
* The drawing surface for this native activity is going to be destroyed.
* You MUST ensure that you do not touch the surface object after returning
* from this function: in the common case of drawing to the surface from
* The drawing window for this native activity is going to be destroyed.
* You MUST ensure that you do not touch the window object after returning
* from this function: in the common case of drawing to the window from
* another thread, that means the implementation of this callback must
* properly synchronize with the other thread to stop its drawing before
* 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.

View File

@@ -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

View File

@@ -35,7 +35,6 @@ struct engine {
int msgread;
int msgwrite;
int msgpipe[2];
ANativeActivity* activity;
pthread_t thread;
@@ -43,11 +42,14 @@ struct engine {
int running;
int destroyed;
AInputQueue* inputQueue;
ANativeWindow* window;
AInputQueue* pendingInputQueue;
ANativeWindow* pendingWindow;
};
enum {
ENGINE_CMD_GAIN_INPUT,
ENGINE_CMD_LOSE_INPUT,
ENGINE_CMD_INPUT_CHANGED,
ENGINE_CMD_WINDOW_CHANGED,
ENGINE_CMD_DESTROY,
};
@@ -90,13 +92,17 @@ static void* engine_entry(void* param) {
int8_t cmd;
if (read(engine->msgread, &cmd, sizeof(cmd)) == sizeof(cmd)) {
switch (cmd) {
case ENGINE_CMD_GAIN_INPUT:
LOGI("Engine: ENGINE_CMD_GAIN_INPUT\n");
break;
case ENGINE_CMD_LOSE_INPUT:
LOGI("Engine: ENGINE_CMD_LOSE_INPUT\n");
case ENGINE_CMD_INPUT_CHANGED:
LOGI("Engine: ENGINE_CMD_INPUT_CHANGED\n");
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_mutex_unlock(&engine->mutex);
break;
@@ -113,7 +119,7 @@ static void* engine_entry(void* param) {
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;
if (AInputQueue_getEvent(engine->inputQueue, &event) >= 0) {
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) {
pthread_mutex_lock(&engine->mutex);
engine->inputQueue = inputQueue;
engine_write_cmd(engine, ENGINE_CMD_GAIN_INPUT);
engine->pendingInputQueue = inputQueue;
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);
}
static void engine_clear_input(struct engine* engine) {
static void engine_set_window(struct engine* engine, ANativeWindow* window) {
pthread_mutex_lock(&engine->mutex);
engine_write_cmd(engine, ENGINE_CMD_LOSE_INPUT);
while (engine->inputQueue != NULL) {
engine->pendingWindow = window;
engine_write_cmd(engine, ENGINE_CMD_WINDOW_CHANGED);
while (engine->window != engine->pendingWindow) {
pthread_cond_wait(&engine->cond, &engine->mutex);
}
pthread_mutex_unlock(&engine->mutex);
@@ -185,8 +195,8 @@ static void engine_destroy(struct engine* engine) {
}
pthread_mutex_unlock(&engine->mutex);
close(engine->msgpipe[0]);
close(engine->msgpipe[1]);
close(engine->msgread);
close(engine->msgwrite);
pthread_cond_destroy(&engine->cond);
pthread_mutex_destroy(&engine->mutex);
}
@@ -233,21 +243,22 @@ static void onWindowFocusChanged(ANativeActivity* activity, int 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,
int format, int width, int height)
static void onNativeWindowChanged(ANativeActivity* activity, ANativeWindow* window)
{
LOGI("SurfaceChanged: %p -- %p fmt=%d w=%d h=%d\n", activity, surface,
format, width, height);
LOGI("NativeWindowChanged: %p -- %p\n", activity, window);
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)
@@ -259,7 +270,7 @@ static void onInputQueueCreated(ANativeActivity* activity, AInputQueue* queue)
static void onInputQueueDestroyed(ANativeActivity* activity, AInputQueue* 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,
@@ -274,9 +285,9 @@ void ANativeActivity_onCreate(ANativeActivity* activity,
activity->callbacks->onStop = onStop;
activity->callbacks->onLowMemory = onLowMemory;
activity->callbacks->onWindowFocusChanged = onWindowFocusChanged;
activity->callbacks->onSurfaceCreated = onSurfaceCreated;
activity->callbacks->onSurfaceChanged = onSurfaceChanged;
activity->callbacks->onSurfaceDestroyed = onSurfaceDestroyed;
activity->callbacks->onNativeWindowCreated = onNativeWindowCreated;
activity->callbacks->onNativeWindowChanged = onNativeWindowChanged;
activity->callbacks->onNativeWindowDestroyed = onNativeWindowDestroyed;
activity->callbacks->onInputQueueCreated = onInputQueueCreated;
activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed;