Fix SurfaceControl.setDisplaySurface() such that it accepts a null Surface

also fix a typo that made us call the wrong Surface ctor

Bug: 8225509
Change-Id: I23f92179b6003d4c3e0febb35166c1caeafa27f5
diff --git a/core/java/android/view/SurfaceControl.java b/core/java/android/view/SurfaceControl.java
index 7ef7e2b..ded2f47 100644
--- a/core/java/android/view/SurfaceControl.java
+++ b/core/java/android/view/SurfaceControl.java
@@ -509,13 +509,8 @@
         if (displayToken == null) {
             throw new IllegalArgumentException("displayToken must not be null");
         }
-        if (surface == null) {
-            throw new IllegalArgumentException("surface must not be null");
-        }
-        if (surface.mNativeObject == 0) 
-            throw new NullPointerException("Surface native object is null. Are you using a released surface?");
-            
-        nativeSetDisplaySurface(displayToken, surface.mNativeObject);
+        int nativeSurface = surface != null ? surface.mNativeObject : 0;
+        nativeSetDisplaySurface(displayToken, nativeSurface);
     }
 
     public static IBinder createDisplay(String name, boolean secure) {
diff --git a/core/jni/android_view_Surface.cpp b/core/jni/android_view_Surface.cpp
index b4a19f1..02e76e5 100644
--- a/core/jni/android_view_Surface.cpp
+++ b/core/jni/android_view_Surface.cpp
@@ -388,7 +388,7 @@
             env->GetFieldID(gSurfaceClassInfo.clazz, "mGenerationId", "I");
     gSurfaceClassInfo.mCanvas =
             env->GetFieldID(gSurfaceClassInfo.clazz, "mCanvas", "Landroid/graphics/Canvas;");
-    gSurfaceClassInfo.ctor = env->GetMethodID(gSurfaceClassInfo.clazz, "<init>", "()V");
+    gSurfaceClassInfo.ctor = env->GetMethodID(gSurfaceClassInfo.clazz, "<init>", "(I)V");
 
     clazz = env->FindClass("android/graphics/Canvas");
     gCanvasClassInfo.mFinalizer = env->GetFieldID(clazz, "mFinalizer", "Landroid/graphics/Canvas$CanvasFinalizer;");
diff --git a/core/jni/android_view_SurfaceControl.cpp b/core/jni/android_view_SurfaceControl.cpp
index a218488..5da5b4f 100644
--- a/core/jni/android_view_SurfaceControl.cpp
+++ b/core/jni/android_view_SurfaceControl.cpp
@@ -320,8 +320,11 @@
         jobject tokenObj, jint nativeSurfaceObject) {
     sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
     if (token == NULL) return;
+    sp<IGraphicBufferProducer> bufferProducer;
     sp<Surface> sur(reinterpret_cast<Surface *>(nativeSurfaceObject));
-    sp<IGraphicBufferProducer> bufferProducer(sur->getIGraphicBufferProducer());
+    if (sur != NULL) {
+        bufferProducer = sur->getIGraphicBufferProducer();
+    }
     SurfaceComposerClient::setDisplaySurface(token, bufferProducer);
 }