Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d5686729 authored by Robert Carr's avatar Robert Carr Committed by Rob Carr
Browse files

Prevent reallocation of Surface object in copyFrom

Previously when we sent the Surface to the client (as opposed to
the SurfaceControl) Surface::readFromParcel would take care of reusing
the object if the underlying IGBP had not changed. Now that we send
the SC and copy it in to the Surface, we need to add similar logic
in Surface::copyFrom.

Bug: 122588130, 122659537
Test: Manual
Change-Id: Id0c8e61a9aff9b3c37b2a783717c15df3b36f175
parent 895ce08d
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -57,7 +57,8 @@ public class Surface implements Parcelable {
            throws OutOfResourcesException;

    private static native long nativeCreateFromSurfaceControl(long surfaceControlNativeObject);
    private static native long nativeGetFromSurfaceControl(long surfaceControlNativeObject);
    private static native long nativeGetFromSurfaceControl(long surfaceObject,
            long surfaceControlNativeObject);

    private static native long nativeLockCanvas(long nativeObject, Canvas canvas, Rect dirty)
            throws OutOfResourcesException;
@@ -519,9 +520,12 @@ public class Surface implements Parcelable {
            throw new NullPointerException(
                    "null SurfaceControl native object. Are you using a released SurfaceControl?");
        }
        long newNativeObject = nativeGetFromSurfaceControl(surfaceControlPtr);
        long newNativeObject = nativeGetFromSurfaceControl(mNativeObject, surfaceControlPtr);

        synchronized (mLock) {
            if (newNativeObject == mNativeObject) {
                return;
            }
            if (mNativeObject != 0) {
                nativeRelease(mNativeObject);
            }
+12 −7
Original line number Diff line number Diff line
@@ -415,18 +415,23 @@ static jlong nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz,
}

static jlong nativeGetFromSurfaceControl(JNIEnv* env, jclass clazz,
        jlong nativeObject,
        jlong surfaceControlNativeObj) {
    /*
     * This is used by the WindowManagerService just after constructing
     * a Surface and is necessary for returning the Surface reference to
     * the caller. At this point, we should only have a SurfaceControl.
     */

    Surface* self(reinterpret_cast<Surface *>(nativeObject));
    sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj));

    // If the underlying IGBP's are the same, we don't need to do anything.
    if (self != nullptr &&
            IInterface::asBinder(self->getIGraphicBufferProducer()) ==
            IInterface::asBinder(ctrl->getIGraphicBufferProducer())) {
        return nativeObject;
    }

    sp<Surface> surface(ctrl->getSurface());
    if (surface != NULL) {
        surface->incStrong(&sRefBaseOwner);
    }

    return reinterpret_cast<jlong>(surface.get());
}

@@ -614,7 +619,7 @@ static const JNINativeMethod gSurfaceMethods[] = {
            (void*)nativeAllocateBuffers },
    {"nativeCreateFromSurfaceControl", "(J)J",
            (void*)nativeCreateFromSurfaceControl },
    {"nativeGetFromSurfaceControl", "(J)J",
    {"nativeGetFromSurfaceControl", "(JJ)J",
            (void*)nativeGetFromSurfaceControl },
    {"nativeReadFromParcel", "(JLandroid/os/Parcel;)J",
            (void*)nativeReadFromParcel },