From 02949f14151f10f906f1fab58e788fadb98baba8 Mon Sep 17 00:00:00 2001 From: Bryce Lee Date: Fri, 16 Jun 2017 07:20:34 -0700 Subject: [PATCH] Create native Surface object on updateWindow for legacy apps. There are some apps that use the Surface object itself to indicate changes. As a result, recycling the existing Surface object for updates can lead to such apps ignoring events such as size changes. This changelist restores the original behavior for legacy apps, where the underlying native Surface object is recreated during updates. Fixes: 62108743 Test: go/wm-smoke Test: Open affected application, observe expansion to fullscreen when nav bar disappears. Rotate to other orientation and observe expansion to fullscreen. Change-Id: I874602b6b8686c6ecb05cf7b1a04ec4b700ad3f9 --- core/java/android/view/Surface.java | 39 ++++++++++++++++++++++++- core/java/android/view/SurfaceView.java | 10 +++++++ core/jni/android_view_Surface.cpp | 12 ++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/core/java/android/view/Surface.java b/core/java/android/view/Surface.java index 8bb3fa988a45c..4f9dbd5ad7a09 100644 --- a/core/java/android/view/Surface.java +++ b/core/java/android/view/Surface.java @@ -52,7 +52,9 @@ public class Surface implements Parcelable { private static native long nativeCreateFromSurfaceTexture(SurfaceTexture surfaceTexture) throws OutOfResourcesException; + private static native long nativeCreateFromSurfaceControl(long surfaceControlNativeObject); + private static native long nativeGetFromSurfaceControl(long surfaceControlNativeObject); private static native long nativeLockCanvas(long nativeObject, Canvas canvas, Rect dirty) throws OutOfResourcesException; @@ -410,6 +412,9 @@ public class Surface implements Parcelable { * back from a client, converting it from the representation being managed * by the window manager to the representation the client uses to draw * in to it. + * + * @param other {@link SurfaceControl} to copy from. + * * @hide */ public void copyFrom(SurfaceControl other) { @@ -420,7 +425,39 @@ public class Surface implements Parcelable { long surfaceControlPtr = other.mNativeObject; if (surfaceControlPtr == 0) { throw new NullPointerException( - "SurfaceControl native object is null. Are you using a released SurfaceControl?"); + "null SurfaceControl native object. Are you using a released SurfaceControl?"); + } + long newNativeObject = nativeGetFromSurfaceControl(surfaceControlPtr); + + synchronized (mLock) { + if (mNativeObject != 0) { + nativeRelease(mNativeObject); + } + setNativeObjectLocked(newNativeObject); + } + } + + /** + * Gets a reference a surface created from this one. This surface now holds a reference + * to the same data as the original surface, and is -not- the owner. + * This is for use by the window manager when returning a window surface + * back from a client, converting it from the representation being managed + * by the window manager to the representation the client uses to draw + * in to it. + * + * @param other {@link SurfaceControl} to create surface from. + * + * @hide + */ + public void createFrom(SurfaceControl other) { + if (other == null) { + throw new IllegalArgumentException("other must not be null"); + } + + long surfaceControlPtr = other.mNativeObject; + if (surfaceControlPtr == 0) { + throw new NullPointerException( + "null SurfaceControl native object. Are you using a released SurfaceControl?"); } long newNativeObject = nativeCreateFromSurfaceControl(surfaceControlPtr); diff --git a/core/java/android/view/SurfaceView.java b/core/java/android/view/SurfaceView.java index 679a9cd92bc27..34ceeb7bcc0d1 100644 --- a/core/java/android/view/SurfaceView.java +++ b/core/java/android/view/SurfaceView.java @@ -641,6 +641,16 @@ public class SurfaceView extends View implements ViewRootImpl.WindowStoppedCallb mSurface.copyFrom(mSurfaceControl); } + if (getContext().getApplicationInfo().targetSdkVersion + < Build.VERSION_CODES.O) { + // Some legacy applications use the underlying native {@link Surface} object + // as a key to whether anything has changed. In these cases, updates to the + // existing {@link Surface} will be ignored when the size changes. + // Therefore, we must explicitly recreate the {@link Surface} in these + // cases. + mSurface.createFrom(mSurfaceControl); + } + if (visible && mSurface.isValid()) { if (!mSurfaceCreated && (surfaceChanged || visibleChanged)) { mSurfaceCreated = true; diff --git a/core/jni/android_view_Surface.cpp b/core/jni/android_view_Surface.cpp index 5839fd50d79ac..7744e0cd6b5d6 100644 --- a/core/jni/android_view_Surface.cpp +++ b/core/jni/android_view_Surface.cpp @@ -394,6 +394,16 @@ static void nativeAllocateBuffers(JNIEnv* /* env */ , jclass /* clazz */, static jlong nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz, jlong surfaceControlNativeObj) { + sp ctrl(reinterpret_cast(surfaceControlNativeObj)); + sp surface(ctrl->createSurface()); + if (surface != NULL) { + surface->incStrong(&sRefBaseOwner); + } + return reinterpret_cast(surface.get()); +} + +static jlong nativeGetFromSurfaceControl(JNIEnv* env, jclass clazz, + jlong surfaceControlNativeObj) { /* * This is used by the WindowManagerService just after constructing * a Surface and is necessary for returning the Surface reference to @@ -590,6 +600,8 @@ static const JNINativeMethod gSurfaceMethods[] = { (void*)nativeAllocateBuffers }, {"nativeCreateFromSurfaceControl", "(J)J", (void*)nativeCreateFromSurfaceControl }, + {"nativeGetFromSurfaceControl", "(J)J", + (void*)nativeGetFromSurfaceControl }, {"nativeReadFromParcel", "(JLandroid/os/Parcel;)J", (void*)nativeReadFromParcel }, {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V",