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

Commit 0856c9f7 authored by Wei Sheng Shih's avatar Wei Sheng Shih Committed by Android (Google) Code Review
Browse files

Merge "Close TaskSnapshot's hardware buffer once no more strong reference to it." into main

parents cb37b042 0a5aed0c
Loading
Loading
Loading
Loading
+41 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.window;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.ComponentName;
@@ -32,6 +33,9 @@ import android.os.SystemClock;
import android.view.Surface;
import android.view.WindowInsetsController;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Represents a task snapshot.
 * @hide
@@ -68,6 +72,21 @@ public class TaskSnapshot implements Parcelable {
    private final boolean mHasImeSurface;
    // Must be one of the named color spaces, otherwise, always use SRGB color space.
    private final ColorSpace mColorSpace;
    private int mInternalReferences;

    /** This snapshot object is being broadcast. */
    public static final int REFERENCE_BROADCAST = 1;
    /** This snapshot object is in the cache. */
    public static final int REFERENCE_CACHE = 1 << 1;
    /** This snapshot object is being persistent. */
    public static final int REFERENCE_PERSIST = 1 << 2;
    @IntDef(flag = true, prefix = { "REFERENCE_" }, value = {
            REFERENCE_BROADCAST,
            REFERENCE_CACHE,
            REFERENCE_PERSIST
    })
    @Retention(RetentionPolicy.SOURCE)
    @interface ReferenceFlags {}

    public TaskSnapshot(long id, long captureTime,
            @NonNull ComponentName topActivityComponent, HardwareBuffer snapshot,
@@ -296,7 +315,28 @@ public class TaskSnapshot implements Parcelable {
                + " mWindowingMode=" + mWindowingMode
                + " mAppearance=" + mAppearance
                + " mIsTranslucent=" + mIsTranslucent
                + " mHasImeSurface=" + mHasImeSurface;
                + " mHasImeSurface=" + mHasImeSurface
                + " mInternalReferences=" + mInternalReferences;
    }

    /**
     * Adds a reference when the object is held somewhere.
     * Only used in core.
     */
    public synchronized void addReference(@ReferenceFlags int usage) {
        mInternalReferences |= usage;
    }

    /**
     * Removes a reference when the object is not held from somewhere. The snapshot will be closed
     * once the reference becomes zero.
     * Only used in core.
     */
    public synchronized void removeReference(@ReferenceFlags int usage) {
        mInternalReferences &= ~usage;
        if (mInternalReferences == 0 && mSnapshot != null && !mSnapshot.isClosed()) {
            mSnapshot.close();
        }
    }

    public static final @NonNull Creator<TaskSnapshot> CREATOR = new Creator<TaskSnapshot>() {
+2 −0
Original line number Diff line number Diff line
@@ -30,10 +30,12 @@ class ActivitySnapshotCache extends SnapshotCache<ActivityRecord> {
    @Override
    void putSnapshot(ActivityRecord ar, TaskSnapshot snapshot) {
        final int hasCode = System.identityHashCode(ar);
        snapshot.addReference(TaskSnapshot.REFERENCE_CACHE);
        synchronized (mLock) {
            final CacheEntry entry = mRunningCache.get(hasCode);
            if (entry != null) {
                mAppIdMap.remove(entry.topApp);
                entry.snapshot.removeReference(TaskSnapshot.REFERENCE_CACHE);
            }
            mAppIdMap.put(ar, hasCode);
            mRunningCache.put(hasCode, new CacheEntry(snapshot, ar));
+1 −0
Original line number Diff line number Diff line
@@ -92,6 +92,7 @@ abstract class SnapshotCache<TYPE extends WindowContainer> {
            if (entry != null) {
                mAppIdMap.remove(entry.topApp);
                mRunningCache.remove(id);
                entry.snapshot.removeReference(TaskSnapshot.REFERENCE_CACHE);
            }
        }
    }
+2 −0
Original line number Diff line number Diff line
@@ -253,6 +253,7 @@ class SnapshotPersistQueue {
                PersistInfoProvider provider) {
            super(provider, userId);
            mId = id;
            snapshot.addReference(TaskSnapshot.REFERENCE_PERSIST);
            mSnapshot = snapshot;
        }

@@ -289,6 +290,7 @@ class SnapshotPersistQueue {
            if (failed) {
                deleteSnapshot(mId, mUserId, mPersistInfoProvider);
            }
            mSnapshot.removeReference(TaskSnapshot.REFERENCE_PERSIST);
            Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
        }

+2 −0
Original line number Diff line number Diff line
@@ -247,6 +247,7 @@ class TaskChangeNotificationController {
                    break;
                case NOTIFY_TASK_SNAPSHOT_CHANGED_LISTENERS_MSG:
                    forAllRemoteListeners(mNotifyTaskSnapshotChanged, msg);
                    ((TaskSnapshot) msg.obj).removeReference(TaskSnapshot.REFERENCE_BROADCAST);
                    break;
                case NOTIFY_BACK_PRESSED_ON_TASK_ROOT:
                    forAllRemoteListeners(mNotifyBackPressedOnTaskRoot, msg);
@@ -485,6 +486,7 @@ class TaskChangeNotificationController {
     * Notify listeners that the snapshot of a task has changed.
     */
    void notifyTaskSnapshotChanged(int taskId, TaskSnapshot snapshot) {
        snapshot.addReference(TaskSnapshot.REFERENCE_BROADCAST);
        final Message msg = mHandler.obtainMessage(NOTIFY_TASK_SNAPSHOT_CHANGED_LISTENERS_MSG,
                taskId, 0, snapshot);
        forAllLocalListeners(mNotifyTaskSnapshotChanged, msg);
Loading