From 88510488e99d02e3b778ae530bb6e1e6aee3a226 Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Wed, 30 Jun 2010 18:36:34 -0700 Subject: [PATCH] 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 --- .../usr/include/android/native_activity.h | 31 ++++---- .../usr/include/android/native_window.h | 50 +++++++++++++ .../android-9/arch-arm/usr/lib/libandroid.so | Bin 9424 -> 9492 bytes ndk/samples/native-activity/jni/main.c | 69 ++++++++++-------- 4 files changed, 103 insertions(+), 47 deletions(-) create mode 100644 ndk/platforms/android-9/arch-arm/usr/include/android/native_window.h diff --git a/ndk/platforms/android-9/arch-arm/usr/include/android/native_activity.h b/ndk/platforms/android-9/arch-arm/usr/include/android/native_activity.h index c5c8f9def..d23e40fbc 100644 --- a/ndk/platforms/android-9/arch-arm/usr/include/android/native_activity.h +++ b/ndk/platforms/android-9/arch-arm/usr/include/android/native_activity.h @@ -24,15 +24,12 @@ #include #include +#include #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. diff --git a/ndk/platforms/android-9/arch-arm/usr/include/android/native_window.h b/ndk/platforms/android-9/arch-arm/usr/include/android/native_window.h new file mode 100644 index 000000000..b3f47b2d3 --- /dev/null +++ b/ndk/platforms/android-9/arch-arm/usr/include/android/native_window.h @@ -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 diff --git a/ndk/platforms/android-9/arch-arm/usr/lib/libandroid.so b/ndk/platforms/android-9/arch-arm/usr/lib/libandroid.so index 42dbfc6559c3bd4b32c92d7af29279e6247bf237..5ef3a22cb3c91ab30d43ec4c16f503039bec16a7 100644 GIT binary patch literal 9492 zcmeHNe{56N6}~S{VkZp{D75qk?K6cEXmMlcn$VVxki;RSP?Kb8s3w_*{fHlNY|HOC zA*>tesMcX7?r>I&cbu1RFRTDdP zo7_ATjequEjyRn2opbKF=iYbkwV!YHH9rzk6om=$uo@=Ny)Ztw)bW(v%T!hl-vTxt zk}>iL9)?a_%2~7rbm*ytj1iX+s%C}!Ds;|GXs`oJdC6Y_u@FAN6Trmer#`lgS}}+s za@xa+RO!3XpWsf|h(&#k&|APh@O|J_U?M+w8TbM4J>WVp(O1E1z%*VW72E`V5L^Sk z6O2CDZD5)12cSM50#|ZJGnXVn;9I~A1VHNxf~|#I4PMFda!3q^T?f7$d>5FA9?b?) zI4$Cm#nkAFab1WxLb*oi;c;TV307QV+xa~1W-ga-IhV`pA*;APk4st) zL^pzOaI~raZkkdZ?QZ;E%WVr>zOMjdc{xODv7YNe$UC?$i;V!c$_B2}vp$02=$S6S zsuc51Zt9b27=oXkJ^5*^ydmMoflo>}34B_@PXhm1!p{T0BjKaKXC?d=@Hq*81pFsp zTHn0&fFe%JB~1tb{YbFG%sv0w0s` zE5N6LX+2ZE-vhq`Oly+xXTTpw_)ctqPl55z@oxvd3_$yz;z&_UIkt$`Og5am+*&DJmvTlIP8Z4 zs>W0Lg8V8W!j-HH57MVP5coQ_5STs-h^pBodnqt|9tj)(Zko(@$$r3vH@fgv7ydf% z4yk>Y3!5(d4Hq5&F0A%KeXOB1na*0xy?WA$bm>+{e_Cgeh^A{hjYu}xXCz~hXgVE9 zXKLM{O?s~p)mvk3`&P}$W^~rDS?`}r+Yq&kRC2m4l+e1&=_WL3OvPMn+oM{t*t$ix z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U zd<>o|@o{hzOh=Oz{0ZC*#u64k0I!m`4hMfT*l+i*0Ph0R|Ayw*ZA0LM6i9$CNfSQz;=SY6I{spz%^7EnxBn{BjET1 z8)M=N1pZxcOr&%8>m0tE!#BXOiG;?)uQ~jC4nL4tMMmRl`Xf(GKHu-F;@!}XMeF#U zA|K!CJDT25{I)V>Htr~FFbS7dE7>`?1jsgXvm|J&381ped+#bg^xa+a;QT$uk4ye? z$&X2Xr{sqvzg6-ZCBH%PHTd~#Z{c0W_TBqii2LF6$raN*g z$(BkEd_xr`YvVa(_V3-t~c#q%S@5n*I5V$;!-XGo7UMBIZS zGb}uZHEYwOrXJP1+}PA@Z<<81B6HOAB)#X9*2X(CKu65_JKllzabGd@9XCA@e>&e< z&GnVY^osnXf6S{iqHn8N8ZEVoaM607h|DH)b+!Y8Qnc7sYlD^M(oxZ<+)S%88FAUwtrV`LvQ|mZH?Rz|6sG7ymVPOrnCFMg>xP3!)bR zTA>9=l45);7^r*)5gJ94oYebxA{40nK|Q@lk{@c9YYyoL{Z2SNEX*Apb2C;J)eJe*hN><7@x` diff --git a/ndk/samples/native-activity/jni/main.c b/ndk/samples/native-activity/jni/main.c index 4d62400d5..bf353ea64 100644 --- a/ndk/samples/native-activity/jni/main.c +++ b/ndk/samples/native-activity/jni/main.c @@ -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;