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

Commit 6038bead authored by Vishnu Nair's avatar Vishnu Nair Committed by Android (Google) Code Review
Browse files

Merge "Cache native properties in SurfaceControl.java" into main

parents 6a7970f4 29e3a5f9
Loading
Loading
Loading
Loading
+40 −21
Original line number Diff line number Diff line
@@ -111,6 +111,8 @@ import java.util.function.Consumer;
 */
public final class SurfaceControl implements Parcelable {
    private static final String TAG = "SurfaceControl";
    private static final NativeProperties RELEASED_NATIVE_PROPERTIES =
            new NativeProperties(0, "<released>", -1);

    private static native long nativeCreate(SurfaceSession session, String name,
            int w, int h, int format, int flags, long parentObject, Parcel metadata)
@@ -335,6 +337,7 @@ public final class SurfaceControl implements Parcelable {
            long nativeObject, long pictureProfileId);
    private static native void nativeSetContentPriority(long transactionObj, long nativeObject,
            int priority);
    private static native String nativeGetName(long nativeObject);

    /**
     * Transforms that can be applied to buffers as they are displayed to a window.
@@ -678,16 +681,32 @@ public final class SurfaceControl implements Parcelable {
        }
    }

    /**
     * Cache SurfaceControl properties for easy rerieval. Cache is updated everytime
     * native object changes.
     */
    private static class NativeProperties {
        public final long nativeHandle;
        public final String name;
        public final int layerId;

        NativeProperties(long nativeHandle, String name, int layerId) {
            this.nativeHandle = nativeHandle;
            this.name = name;
            this.layerId = layerId;
        }
    }


    private final CloseGuard mCloseGuard = CloseGuard.get();
    private String mName;
    private NativeProperties mNativeProperties = RELEASED_NATIVE_PROPERTIES;
    private String mCallsite;

     /**
     * Note: do not rename, this field is used by native code.
     * @hide
     */
    public long mNativeObject;
    private long mNativeHandle;
    public long mNativeObject; // used by native

    private final Object mChoreographerLock = new Object();
    @GuardedBy("mChoreographerLock")
@@ -1015,6 +1034,14 @@ public final class SurfaceControl implements Parcelable {
    @Retention(RetentionPolicy.SOURCE)
    public @interface CachingHint {}

    private NativeProperties getNativeProperties(long nativeObject) {
        if (nativeObject == 0) {
            return RELEASED_NATIVE_PROPERTIES;
        }
        return new NativeProperties(nativeGetHandle(nativeObject), nativeGetName(nativeObject),
                nativeGetLayerId(nativeObject));
    }

    private void assignNativeObject(long nativeObject, String callsite) {
        if (mNativeObject != 0) {
            release();
@@ -1024,7 +1051,7 @@ public final class SurfaceControl implements Parcelable {
                    sRegistry.registerNativeAllocation(this, nativeObject);
        }
        mNativeObject = nativeObject;
        mNativeHandle = mNativeObject != 0 ? nativeGetHandle(nativeObject) : 0;
        mNativeProperties = getNativeProperties(mNativeObject);
        if (sDebugUsageAfterRelease && mNativeObject == 0) {
            mReleaseStack = new Throwable("Assigned invalid nativeObject");
        } else {
@@ -1042,7 +1069,6 @@ public final class SurfaceControl implements Parcelable {
     * @hide
     */
    public void copyFrom(@NonNull SurfaceControl other, String callsite) {
        mName = other.mName;
        mWidth = other.mWidth;
        mHeight = other.mHeight;
        mLocalOwnerView = other.mLocalOwnerView;
@@ -1503,7 +1529,6 @@ public final class SurfaceControl implements Parcelable {
            throw new IllegalArgumentException("name must not be null");
        }

        mName = name;
        mWidth = w;
        mHeight = h;
        mLocalOwnerView = localOwnerView;
@@ -1563,7 +1588,6 @@ public final class SurfaceControl implements Parcelable {
            throw new IllegalArgumentException("source must not be null");
        }

        mName = in.readString8();
        mWidth = in.readInt();
        mHeight = in.readInt();

@@ -1584,7 +1608,6 @@ public final class SurfaceControl implements Parcelable {
        if (sDebugUsageAfterRelease) {
            checkNotReleased();
        }
        dest.writeString8(mName);
        dest.writeInt(mWidth);
        dest.writeInt(mHeight);
        if (mNativeObject == 0) {
@@ -1639,7 +1662,7 @@ public final class SurfaceControl implements Parcelable {
     * @hide
     */
    @NonNull String getName() {
        return mName;
        return mNativeProperties.name;
    }

    /**
@@ -1651,7 +1674,7 @@ public final class SurfaceControl implements Parcelable {
     */
    @TestApi
    public boolean isSameSurface(@NonNull SurfaceControl other) {
        return other.mNativeHandle == mNativeHandle;
        return other.mNativeProperties.nativeHandle == mNativeProperties.nativeHandle;
    }

    /**
@@ -1693,7 +1716,8 @@ public final class SurfaceControl implements Parcelable {
        checkNotReleased();
        synchronized (mChoreographerLock) {
            if (mChoreographer == null) {
                mChoreographer = Choreographer.getInstanceForSurfaceControl(mNativeHandle, looper);
                mChoreographer = Choreographer.getInstanceForSurfaceControl(
                        mNativeProperties.nativeHandle, looper);
            } else if (!mChoreographer.isTheLooperSame(looper)) {
                throw new IllegalStateException(
                        "Choreographer already exists with a different looper");
@@ -1728,7 +1752,7 @@ public final class SurfaceControl implements Parcelable {
    public void dumpDebug(ProtoOutputStream proto, long fieldId) {
        final long token = proto.start(fieldId);
        proto.write(HASH_CODE, System.identityHashCode(this));
        proto.write(NAME, mName);
        proto.write(NAME, mNativeProperties.name);
        proto.write(LAYER_ID, getLayerId());
        proto.end(token);
    }
@@ -1777,7 +1801,7 @@ public final class SurfaceControl implements Parcelable {
            }
            mFreeNativeResources.run();
            mNativeObject = 0;
            mNativeHandle = 0;
            mNativeProperties = RELEASED_NATIVE_PROPERTIES;
            if (sDebugUsageAfterRelease) {
                mReleaseStack = new Throwable("Released");
            }
@@ -1912,8 +1936,8 @@ public final class SurfaceControl implements Parcelable {

    @Override
    public String toString() {
        return "Surface(name=" + mName + ")/@0x" +
                Integer.toHexString(System.identityHashCode(this));
        return "Surface(name=" + mNativeProperties.name + ")/@0x"
                + Integer.toHexString(System.identityHashCode(this));
    }

    /**
@@ -2831,7 +2855,6 @@ public final class SurfaceControl implements Parcelable {
        long stopAtObj = stopAt != null ? stopAt.mNativeObject : 0;
        long nativeObj = nativeMirrorSurface(mirrorOf.mNativeObject, stopAtObj);
        SurfaceControl sc = new SurfaceControl();
        sc.mName = mirrorOf.mName + " (mirror)";
        sc.assignNativeObject(nativeObj, "mirrorSurface");
        return sc;
    }
@@ -5488,11 +5511,7 @@ public final class SurfaceControl implements Parcelable {
     * @hide
     */
    public int getLayerId() {
        if (mNativeObject != 0) {
            return nativeGetLayerId(mNativeObject);
        }

        return -1;
        return mNativeProperties.layerId;
    }

    // Called by native
+17 −9
Original line number Diff line number Diff line
@@ -272,9 +272,9 @@ static struct {
static struct {
    jclass clazz;
    jfieldID mNativeObject;
    jfieldID mName;
    jmethodID ctor;
    jmethodID invokeReleaseCallback;
    jmethodID assignNativeObject;
} gSurfaceControlClassInfo;

static struct {
@@ -2527,6 +2527,14 @@ static void nativeNotifyShutdown() {
    SurfaceComposerClient::notifyShutdown();
}

static jstring nativeGetName(JNIEnv* env, jclass clazz, jlong nativeObject) {
    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl*>(nativeObject);
    if (ctrl == nullptr) {
        return env->NewStringUTF("<null>");
    }
    return env->NewStringUTF(ctrl->getName().c_str());
}

// ----------------------------------------------------------------------------

SurfaceControl* android_view_SurfaceControl_getNativeSurfaceControl(JNIEnv* env,
@@ -2544,12 +2552,9 @@ jobject android_view_SurfaceControl_getJavaSurfaceControl(JNIEnv* env,
                                                          const SurfaceControl& surfaceControl) {
    jobject surfaceControlObj =
            env->NewObject(gSurfaceControlClassInfo.clazz, gSurfaceControlClassInfo.ctor);
    env->SetLongField(surfaceControlObj, gSurfaceControlClassInfo.mNativeObject,
                      reinterpret_cast<jlong>(&surfaceControl));
    env->SetObjectField(surfaceControlObj, gSurfaceControlClassInfo.mName,
                        ScopedLocalRef<jobject>(env,
                                                env->NewStringUTF(surfaceControl.getName().c_str()))
                                .get());
    jstring callsite = env->NewStringUTF("android_view_SurfaceControl_getJavaSurfaceControl");
    env->CallVoidMethod(surfaceControlObj, gSurfaceControlClassInfo.assignNativeObject,
                        reinterpret_cast<jlong>(&surfaceControl), callsite);
    surfaceControl.incStrong((void*)nativeCreate);
    return surfaceControlObj;
}
@@ -2576,6 +2581,8 @@ static const JNINativeMethod sSurfaceControlMethods[] = {
            (void*)nativeCreate },
    {"nativeReadFromParcel", "(Landroid/os/Parcel;)J",
            (void*)nativeReadFromParcel },
    {"nativeGetName", "(J)Ljava/lang/String;",
            (void*)nativeGetName },
    {"nativeCopyFromSurfaceControl", "(J)J" ,
            (void*)nativeCopyFromSurfaceControl },
    {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V",
@@ -3084,12 +3091,13 @@ int register_android_view_SurfaceControl(JNIEnv* env)
    gSurfaceControlClassInfo.clazz = MakeGlobalRefOrDie(env, surfaceControlClazz);
    gSurfaceControlClassInfo.mNativeObject =
            GetFieldIDOrDie(env, gSurfaceControlClassInfo.clazz, "mNativeObject", "J");
    gSurfaceControlClassInfo.mName =
            GetFieldIDOrDie(env, gSurfaceControlClassInfo.clazz, "mName", "Ljava/lang/String;");
    gSurfaceControlClassInfo.ctor = GetMethodIDOrDie(env, surfaceControlClazz, "<init>", "()V");
    gSurfaceControlClassInfo.invokeReleaseCallback =
            GetStaticMethodIDOrDie(env, surfaceControlClazz, "invokeReleaseCallback",
                                   "(Ljava/util/function/Consumer;J)V");
    gSurfaceControlClassInfo.assignNativeObject =
            GetMethodIDOrDie(env, surfaceControlClazz, "assignNativeObject",
                             "(JLjava/lang/String;)V");

    jclass surfaceTransactionClazz = FindClassOrDie(env, "android/view/SurfaceControl$Transaction");
    gTransactionClassInfo.clazz = MakeGlobalRefOrDie(env, surfaceTransactionClazz);