From d28e8283d39e7eb6c714511a0430d4cc836298cb Mon Sep 17 00:00:00 2001 From: Jiwen 'Steve' Cai Date: Fri, 31 Mar 2017 11:19:50 -0700 Subject: [PATCH] ANativeWindow_toSurface implementation This enables the conversion from an ANativeWindow (created by NDK API such as: AImageReader_create) to a Java Surface, so that developers can hookup a Java Producer to a native buffer consumer. This CL also introduces android_view_Surface_createFromSurface helper function in libandroid_runtime to convert a C++ sp to a Java Surface object. Bug: 36862948 Test: android.media.cts.NativeImageReaderTest Change-Id: Ia99adb654da505ac117a8e58153ac800df23a650 --- core/jni/android_view_Surface.cpp | 22 +++++++++---------- .../android_runtime/android_view_Surface.h | 4 ++++ native/android/libandroid.map.txt | 1 + native/android/native_window_jni.cpp | 9 ++++++++ 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/core/jni/android_view_Surface.cpp b/core/jni/android_view_Surface.cpp index 713287e4cd7a2..5839fd50d79ac 100644 --- a/core/jni/android_view_Surface.cpp +++ b/core/jni/android_view_Surface.cpp @@ -101,17 +101,7 @@ sp android_view_Surface_getSurface(JNIEnv* env, jobject surfaceObj) { return sur; } -jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env, - const sp& bufferProducer) { - if (bufferProducer == NULL) { - return NULL; - } - - sp surface(new Surface(bufferProducer, true)); - if (surface == NULL) { - return NULL; - } - +jobject android_view_Surface_createFromSurface(JNIEnv* env, const sp& surface) { jobject surfaceObj = env->NewObject(gSurfaceClassInfo.clazz, gSurfaceClassInfo.ctor, (jlong)surface.get()); if (surfaceObj == NULL) { @@ -126,6 +116,16 @@ jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env, return surfaceObj; } +jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env, + const sp& bufferProducer) { + if (bufferProducer == NULL) { + return NULL; + } + + sp surface(new Surface(bufferProducer, true)); + return android_view_Surface_createFromSurface(env, surface); +} + int android_view_Surface_mapPublicFormatToHalFormat(PublicFormat f) { switch(f) { diff --git a/core/jni/include/android_runtime/android_view_Surface.h b/core/jni/include/android_runtime/android_view_Surface.h index 3f1bdff81aeff..2641ab8f0337a 100644 --- a/core/jni/include/android_runtime/android_view_Surface.h +++ b/core/jni/include/android_runtime/android_view_Surface.h @@ -69,6 +69,10 @@ extern bool android_view_Surface_isInstanceOf(JNIEnv* env, jobject obj); /* Gets the underlying Surface from a Surface Java object. */ extern sp android_view_Surface_getSurface(JNIEnv* env, jobject surfaceObj); +/* Creates a Surface from an android::Surface. */ +extern jobject android_view_Surface_createFromSurface(JNIEnv* env, + const sp& surface); + /* Creates a Surface from an IGraphicBufferProducer. */ extern jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env, const sp& bufferProducer); diff --git a/native/android/libandroid.map.txt b/native/android/libandroid.map.txt index c7bed9bc16855..c82a1f6a646ab 100644 --- a/native/android/libandroid.map.txt +++ b/native/android/libandroid.map.txt @@ -153,6 +153,7 @@ LIBANDROID { ANativeWindow_acquire; ANativeWindow_fromSurface; ANativeWindow_fromSurfaceTexture; # introduced-arm=13 introduced-mips=13 introduced-x86=13 + ANativeWindow_toSurface; # introduced=26 ANativeWindow_getFormat; ANativeWindow_getHeight; ANativeWindow_getWidth; diff --git a/native/android/native_window_jni.cpp b/native/android/native_window_jni.cpp index dc3040533974f..859c550db94df 100644 --- a/native/android/native_window_jni.cpp +++ b/native/android/native_window_jni.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -33,3 +34,11 @@ ANativeWindow* ANativeWindow_fromSurface(JNIEnv* env, jobject surface) { } return win.get(); } + +jobject ANativeWindow_toSurface(JNIEnv* env, ANativeWindow* window) { + if (window == NULL) { + return NULL; + } + sp surface = static_cast(window); + return android_view_Surface_createFromSurface(env, surface); +}