Harden construction sites of android::Surface.

Since this type is inteded to be managed as a StrongPointer, ensure that
is always constructed as such.

Bug: 393217449
Test: presubmit
Flag: EXEMPT_refactor
Change-Id: Ia74493743653a9e41384cfb4c384ac30bef1e907
diff --git a/cmds/bootanimation/BootAnimation.cpp b/cmds/bootanimation/BootAnimation.cpp
index 844e52c..b0070c5 100644
--- a/cmds/bootanimation/BootAnimation.cpp
+++ b/cmds/bootanimation/BootAnimation.cpp
@@ -207,7 +207,7 @@
         : Thread(false), mLooper(new Looper(false)), mClockEnabled(true), mTimeIsAccurate(false),
         mTimeFormat12Hour(false), mTimeCheckThread(nullptr), mCallbacks(callbacks) {
     ATRACE_CALL();
-    mSession = new SurfaceComposerClient();
+    mSession = sp<SurfaceComposerClient>::make();
 
     std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
     if (powerCtl.empty()) {
diff --git a/core/jni/android_media_ImageWriter.cpp b/core/jni/android_media_ImageWriter.cpp
index 1357dd8..8e58922 100644
--- a/core/jni/android_media_ImageWriter.cpp
+++ b/core/jni/android_media_ImageWriter.cpp
@@ -399,7 +399,7 @@
     }
     sp<JNIImageWriterContext> ctx(new JNIImageWriterContext(env, weakThiz, clazz));
 
-    sp<Surface> producer = new Surface(bufferProducer, /*controlledByApp*/false);
+    sp<Surface> producer = sp<Surface>::make(bufferProducer, /*controlledByApp*/ false);
     ctx->setProducer(producer);
     /**
      * NATIVE_WINDOW_API_CPU isn't a good choice here, as it makes the bufferQueue not connectable
diff --git a/core/jni/android_view_Surface.cpp b/core/jni/android_view_Surface.cpp
index 312c206..783daec 100644
--- a/core/jni/android_view_Surface.cpp
+++ b/core/jni/android_view_Surface.cpp
@@ -139,7 +139,7 @@
         return NULL;
     }
 
-    sp<Surface> surface(new Surface(bufferProducer, true));
+    sp<Surface> surface = sp<Surface>::make(bufferProducer, true);
     return android_view_Surface_createFromSurface(env, surface);
 }
 
@@ -161,7 +161,7 @@
         return 0;
     }
 
-    sp<Surface> surface(new Surface(producer, true));
+    sp<Surface> surface = sp<Surface>::make(producer, true);
     if (surface == NULL) {
         jniThrowException(env, OutOfResourcesException, NULL);
         return 0;
@@ -358,8 +358,8 @@
     sp<Surface> sur;
     if (surfaceShim.graphicBufferProducer != nullptr) {
         // we have a new IGraphicBufferProducer, create a new Surface for it
-        sur = new Surface(surfaceShim.graphicBufferProducer, true,
-                          surfaceShim.surfaceControlHandle);
+        sur = sp<Surface>::make(surfaceShim.graphicBufferProducer, true,
+                                surfaceShim.surfaceControlHandle);
         // and keep a reference before passing to java
         sur->incStrong(&sRefBaseOwner);
     }
diff --git a/core/jni/android_view_SurfaceSession.cpp b/core/jni/android_view_SurfaceSession.cpp
index 6ad109e..4f2ab09 100644
--- a/core/jni/android_view_SurfaceSession.cpp
+++ b/core/jni/android_view_SurfaceSession.cpp
@@ -42,14 +42,14 @@
 
 
 static jlong nativeCreate(JNIEnv* env, jclass clazz) {
-    SurfaceComposerClient* client = new SurfaceComposerClient();
-    client->incStrong((void*)nativeCreate);
-    return reinterpret_cast<jlong>(client);
+    // Will be deleted via decStrong() in nativeDestroy.
+    auto client = sp<SurfaceComposerClient>::make();
+    return reinterpret_cast<jlong>(client.release());
 }
 
 static void nativeDestroy(JNIEnv* env, jclass clazz, jlong ptr) {
     SurfaceComposerClient* client = reinterpret_cast<SurfaceComposerClient*>(ptr);
-    client->decStrong((void*)nativeCreate);
+    client->decStrong((void*)client);
 }
 
 static const JNINativeMethod gMethods[] = {
diff --git a/core/jni/android_view_TextureView.cpp b/core/jni/android_view_TextureView.cpp
index 21fe1f0..f71878c 100644
--- a/core/jni/android_view_TextureView.cpp
+++ b/core/jni/android_view_TextureView.cpp
@@ -85,7 +85,7 @@
         jobject surface) {
 
     sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surface));
-    sp<ANativeWindow> window = new Surface(producer, true);
+    sp<ANativeWindow> window = sp<Surface>::make(producer, true);
 
     window->incStrong((void*)android_view_TextureView_createNativeWindow);
     SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, jlong(window.get()));
diff --git a/core/jni/com_google_android_gles_jni_EGLImpl.cpp b/core/jni/com_google_android_gles_jni_EGLImpl.cpp
index 75330be..1b3b14d 100644
--- a/core/jni/com_google_android_gles_jni_EGLImpl.cpp
+++ b/core/jni/com_google_android_gles_jni_EGLImpl.cpp
@@ -300,7 +300,7 @@
     }
 
     sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(_env, native_window));
-    window = new Surface(producer, true);
+    window = sp<Surface>::make(producer, true);
     if (window == NULL)
         goto not_valid_surface;
 
diff --git a/native/android/surface_control.cpp b/native/android/surface_control.cpp
index 2759724..f6dc41e 100644
--- a/native/android/surface_control.cpp
+++ b/native/android/surface_control.cpp
@@ -89,7 +89,7 @@
     CHECK_NOT_NULL(window);
     CHECK_NOT_NULL(debug_name);
 
-    sp<SurfaceComposerClient> client = new SurfaceComposerClient();
+    sp<SurfaceComposerClient> client = sp<SurfaceComposerClient>::make();
     if (client->initCheck() != NO_ERROR) {
         return nullptr;
     }