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

Commit 8fb40faa authored by Android Build Coastguard Worker's avatar Android Build Coastguard Worker
Browse files

Merge cherrypicks of ['googleplex-android-review.googlesource.com/25102441',...

Merge cherrypicks of ['googleplex-android-review.googlesource.com/25102441', 'googleplex-android-review.googlesource.com/25106966'] into sparse-10971902-L45800000963599846.
SPARSE_CHANGE: I5093162cbfa632bafe6870c84177952becc82c50
SPARSE_CHANGE: Ieacc78332716aa6628d67ca3227b6995ec7bb49e

Change-Id: Ib55100ede466eb077c88b2f50ec4db73ff3fab99
parents fb67d41a 2200f3d5
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import android.annotation.RequiresPermission;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.annotation.TestApi;
import android.app.ActivityThread;
import android.app.KeyguardManager;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context;
@@ -757,7 +758,8 @@ public final class DisplayManager {
     */
    public void registerDisplayListener(@NonNull DisplayListener listener,
            @Nullable Handler handler, @EventsMask long eventsMask) {
        mGlobal.registerDisplayListener(listener, handler, eventsMask);
        mGlobal.registerDisplayListener(listener, handler, eventsMask,
                ActivityThread.currentPackageName());
    }

    /**
+83 −6
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.app.ActivityThread;
import android.app.PropertyInvalidatedCache;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context;
@@ -45,8 +46,11 @@ import android.os.Message;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.Trace;
import android.sysprop.DisplayProperties;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
import android.util.Slog;
import android.util.SparseArray;
import android.view.Display;
import android.view.DisplayAdjustments;
@@ -72,7 +76,13 @@ import java.util.concurrent.atomic.AtomicLong;
 */
public final class DisplayManagerGlobal {
    private static final String TAG = "DisplayManager";
    private static final boolean DEBUG = false;

    private static final String EXTRA_LOGGING_PACKAGE_NAME =
            DisplayProperties.debug_vri_package().orElse(null);
    private static String sCurrentPackageName = ActivityThread.currentPackageName();
    private static boolean sExtraDisplayListenerLogging = initExtraLogging();

    private static final boolean DEBUG = false || sExtraDisplayListenerLogging;

    // True if display info and display ids should be cached.
    //
@@ -126,6 +136,8 @@ public final class DisplayManagerGlobal {
    @VisibleForTesting
    public DisplayManagerGlobal(IDisplayManager dm) {
        mDm = dm;
        initExtraLogging();

        try {
            mWideColorSpace =
                    ColorSpace.get(
@@ -317,12 +329,14 @@ public final class DisplayManagerGlobal {
     * If null, listener will use the handler for the current thread, and if still null,
     * the handler for the main thread.
     * If that is still null, a runtime exception will be thrown.
     * @param packageName of the calling package.
     */
    public void registerDisplayListener(@NonNull DisplayListener listener,
            @Nullable Handler handler, @EventsMask long eventsMask) {
            @Nullable Handler handler, @EventsMask long eventsMask, String packageName) {
        Looper looper = getLooperForHandler(handler);
        Handler springBoard = new Handler(looper);
        registerDisplayListener(listener, new HandlerExecutor(springBoard), eventsMask);
        registerDisplayListener(listener, new HandlerExecutor(springBoard), eventsMask,
                packageName);
    }

    /**
@@ -330,9 +344,11 @@ public final class DisplayManagerGlobal {
     *
     * @param listener The listener that will be called when display changes occur.
     * @param executor Executor for the thread that will be receiving the callbacks. Cannot be null.
     * @param eventsMask Mask of events to be listened to.
     * @param packageName of the calling package.
     */
    public void registerDisplayListener(@NonNull DisplayListener listener,
            @NonNull Executor executor, @EventsMask long eventsMask) {
            @NonNull Executor executor, @EventsMask long eventsMask, String packageName) {
        if (listener == null) {
            throw new IllegalArgumentException("listener must not be null");
        }
@@ -341,15 +357,22 @@ public final class DisplayManagerGlobal {
            throw new IllegalArgumentException("The set of events to listen to must not be empty.");
        }

        if (extraLogging()) {
            Slog.i(TAG, "Registering Display Listener: "
                    + Long.toBinaryString(eventsMask) + ", packageName: " + packageName);
        }

        synchronized (mLock) {
            int index = findDisplayListenerLocked(listener);
            if (index < 0) {
                mDisplayListeners.add(new DisplayListenerDelegate(listener, executor, eventsMask));
                mDisplayListeners.add(new DisplayListenerDelegate(listener, executor, eventsMask,
                        packageName));
                registerCallbackIfNeededLocked();
            } else {
                mDisplayListeners.get(index).setEventsMask(eventsMask);
            }
            updateCallbackIfNeededLocked();
            maybeLogAllDisplayListeners();
        }
    }

@@ -358,6 +381,10 @@ public final class DisplayManagerGlobal {
            throw new IllegalArgumentException("listener must not be null");
        }

        if (extraLogging()) {
            Slog.i(TAG, "Unregistering Display Listener: " + listener);
        }

        synchronized (mLock) {
            int index = findDisplayListenerLocked(listener);
            if (index >= 0) {
@@ -367,6 +394,18 @@ public final class DisplayManagerGlobal {
                updateCallbackIfNeededLocked();
            }
        }
        maybeLogAllDisplayListeners();
    }

    private void maybeLogAllDisplayListeners() {
        if (!extraLogging()) {
            return;
        }

        Slog.i(TAG, "Currently Registered Display Listeners:");
        for (int i = 0; i < mDisplayListeners.size(); i++) {
            Slog.i(TAG, i + ": " + mDisplayListeners.get(i));
        }
    }

    private static Looper getLooperForHandler(@Nullable Handler handler) {
@@ -1114,15 +1153,20 @@ public final class DisplayManagerGlobal {
        private final DisplayInfo mDisplayInfo = new DisplayInfo();
        private final Executor mExecutor;
        private AtomicLong mGenerationId = new AtomicLong(1);
        private final String mPackageName;

        DisplayListenerDelegate(DisplayListener listener, @NonNull Executor executor,
                @EventsMask long eventsMask) {
                @EventsMask long eventsMask, String packageName) {
            mExecutor = executor;
            mListener = listener;
            mEventsMask = eventsMask;
            mPackageName = packageName;
        }

        public void sendDisplayEvent(int displayId, @DisplayEvent int event, DisplayInfo info) {
            if (extraLogging()) {
                Slog.i(TAG, "Sending Display Event: " + eventToString(event));
            }
            long generationId = mGenerationId.get();
            Message msg = Message.obtain(null, event, displayId, 0, info);
            mExecutor.execute(() -> {
@@ -1143,6 +1187,14 @@ public final class DisplayManagerGlobal {
        }

        private void handleMessage(Message msg) {
            if (extraLogging()) {
                Slog.i(TAG, "DisplayListenerDelegate(" + eventToString(msg.what)
                        + ", display=" + msg.arg1
                        + ", mEventsMask=" + Long.toBinaryString(mEventsMask)
                        + ", mPackageName=" + mPackageName
                        + ", msg.obj=" + msg.obj
                        + ", listener=" + mListener.getClass() + ")");
            }
            if (DEBUG) {
                Trace.beginSection(
                        "DisplayListenerDelegate(" + eventToString(msg.what)
@@ -1159,6 +1211,10 @@ public final class DisplayManagerGlobal {
                    if ((mEventsMask & DisplayManager.EVENT_FLAG_DISPLAY_CHANGED) != 0) {
                        DisplayInfo newInfo = (DisplayInfo) msg.obj;
                        if (newInfo != null && !newInfo.equals(mDisplayInfo)) {
                            if (extraLogging()) {
                                Slog.i(TAG, "Sending onDisplayChanged: Display Changed. Info: "
                                        + newInfo);
                            }
                            mDisplayInfo.copyFrom(newInfo);
                            mListener.onDisplayChanged(msg.arg1);
                        }
@@ -1184,6 +1240,11 @@ public final class DisplayManagerGlobal {
                Trace.endSection();
            }
        }

        @Override
        public String toString() {
            return "mask: {" + mEventsMask + "}, for " + mListener.getClass();
        }
    }

    /**
@@ -1305,4 +1366,20 @@ public final class DisplayManagerGlobal {
        }
        return "UNKNOWN";
    }


    private static boolean initExtraLogging() {
        if (sCurrentPackageName == null) {
            sCurrentPackageName = ActivityThread.currentPackageName();
            sExtraDisplayListenerLogging = !TextUtils.isEmpty(EXTRA_LOGGING_PACKAGE_NAME)
                    && EXTRA_LOGGING_PACKAGE_NAME.equals(sCurrentPackageName);
        }
        // TODO: b/306170135 - return sExtraDisplayListenerLogging instead
        return true;
    }

    private static boolean extraLogging() {
        // TODO: b/306170135 - return sExtraDisplayListenerLogging & package name check instead
        return true;
    }
}
+3 −1
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.annotation.SuppressLint;
import android.annotation.TestApi;
import android.app.ActivityThread;
import android.app.KeyguardManager;
import android.app.WindowConfiguration;
import android.compat.annotation.UnsupportedAppUsage;
@@ -1366,7 +1367,8 @@ public final class Display {
            // form of the larger DISPLAY_CHANGED event
            mGlobal.registerDisplayListener(toRegister, executor,
                    DisplayManager.EVENT_FLAG_HDR_SDR_RATIO_CHANGED
                            | DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
                            | DisplayManagerGlobal.EVENT_DISPLAY_CHANGED,
                    ActivityThread.currentPackageName());
        }

    }
+53 −10
Original line number Diff line number Diff line
@@ -779,6 +779,10 @@ public final class ViewRootImpl implements ViewParent,
     */
    private boolean mCheckIfCanDraw = false;

    private boolean mWasLastDrawCanceled;
    private boolean mLastTraversalWasVisible = true;
    private boolean mLastDrawScreenOff;

    private boolean mDrewOnceForSync = false;

    int mSyncSeqId = 0;
@@ -1140,7 +1144,8 @@ public final class ViewRootImpl implements ViewParent,
        mDisplay = display;
        mBasePackageName = context.getBasePackageName();
        final String name = DisplayProperties.debug_vri_package().orElse(null);
        mExtraDisplayListenerLogging = !TextUtils.isEmpty(name) && name.equals(mBasePackageName);
        // TODO: b/306170135 - return to using textutils check on package name.
        mExtraDisplayListenerLogging = true;
        mThread = Thread.currentThread();
        mLocation = new WindowLeaked(null);
        mLocation.fillInStackTrace();
@@ -1684,7 +1689,8 @@ public final class ViewRootImpl implements ViewParent,
                        mHandler,
                        DisplayManager.EVENT_FLAG_DISPLAY_ADDED
                        | DisplayManager.EVENT_FLAG_DISPLAY_CHANGED
                        | DisplayManager.EVENT_FLAG_DISPLAY_REMOVED);
                        | DisplayManager.EVENT_FLAG_DISPLAY_REMOVED,
                        mBasePackageName);
    }

    /**
@@ -2005,12 +2011,19 @@ public final class ViewRootImpl implements ViewParent,
    }

    void handleAppVisibility(boolean visible) {
        if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
            Trace.instant(Trace.TRACE_TAG_VIEW, TextUtils.formatSimple(
                    "%s visibilityChanged oldVisibility=%b newVisibility=%b", mTag,
                    mAppVisible, visible));
        }
        if (mAppVisible != visible) {
            final boolean previousVisible = getHostVisibility() == View.VISIBLE;
            mAppVisible = visible;
            final boolean currentVisible = getHostVisibility() == View.VISIBLE;
            // Root view only cares about whether it is visible or not.
            if (previousVisible != currentVisible) {
                Log.d(mTag, "visibilityChanged oldVisibility=" + previousVisible + " newVisibility="
                        + currentVisible);
                mAppVisibilityChanged = true;
                scheduleTraversals();
            }
@@ -2118,6 +2131,10 @@ public final class ViewRootImpl implements ViewParent,
                    Slog.i(mTag, "DisplayState - old: " + oldDisplayState
                            + ", new: " + newDisplayState);
                }
                if (Trace.isTagEnabled(Trace.TRACE_TAG_WINDOW_MANAGER)) {
                    Trace.traceCounter(Trace.TRACE_TAG_WINDOW_MANAGER,
                            "vri#screenState[" + mTag + "] state=", newDisplayState);
                }
                if (oldDisplayState != newDisplayState) {
                    mAttachInfo.mDisplayState = newDisplayState;
                    pokeDrawLockIfNeeded();
@@ -3371,8 +3388,8 @@ public final class ViewRootImpl implements ViewParent,
                || mForceNextWindowRelayout) {
            if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
                Trace.traceBegin(Trace.TRACE_TAG_VIEW,
                        TextUtils.formatSimple("relayoutWindow#"
                                        + "first=%b/resize=%b/vis=%b/params=%b/force=%b",
                        TextUtils.formatSimple("%s-relayoutWindow#"
                                        + "first=%b/resize=%b/vis=%b/params=%b/force=%b", mTag,
                                mFirst, windowShouldResize, viewVisibilityChanged, params != null,
                                mForceNextWindowRelayout));
            }
@@ -3956,11 +3973,7 @@ public final class ViewRootImpl implements ViewParent,
        boolean cancelDueToPreDrawListener = mAttachInfo.mTreeObserver.dispatchOnPreDraw();
        boolean cancelAndRedraw = cancelDueToPreDrawListener
                 || (cancelDraw && mDrewOnceForSync);
        if (cancelAndRedraw) {
            Log.d(mTag, "Cancelling draw."
                    + " cancelDueToPreDrawListener=" + cancelDueToPreDrawListener
                    + " cancelDueToSync=" + (cancelDraw && mDrewOnceForSync));
        }

        if (!cancelAndRedraw) {
            // A sync was already requested before the WMS requested sync. This means we need to
            // sync the buffer, regardless if WMS wants to sync the buffer.
@@ -3984,6 +3997,9 @@ public final class ViewRootImpl implements ViewParent,
        }

        if (!isViewVisible) {
            if (mLastTraversalWasVisible) {
                logAndTrace("Not drawing due to not visible");
            }
            mLastPerformTraversalsSkipDrawReason = "view_not_visible";
            if (mPendingTransitions != null && mPendingTransitions.size() > 0) {
                for (int i = 0; i < mPendingTransitions.size(); ++i) {
@@ -3996,12 +4012,21 @@ public final class ViewRootImpl implements ViewParent,
                mActiveSurfaceSyncGroup.markSyncReady();
            }
        } else if (cancelAndRedraw) {
            logAndTrace("Canceling draw."
                    + " cancelDueToPreDrawListener=" + cancelDueToPreDrawListener
                    + " cancelDueToSync=" + (cancelDraw && mDrewOnceForSync));
            mLastPerformTraversalsSkipDrawReason = cancelDueToPreDrawListener
                ? "predraw_" + mAttachInfo.mTreeObserver.getLastDispatchOnPreDrawCanceledReason()
                : "cancel_" + cancelReason;
            // Try again
            scheduleTraversals();
        } else {
            if (mWasLastDrawCanceled) {
                logAndTrace("Draw frame after cancel");
            }
            if (!mLastTraversalWasVisible) {
                logAndTrace("Start draw after previous draw not visible");
            }
            if (mPendingTransitions != null && mPendingTransitions.size() > 0) {
                for (int i = 0; i < mPendingTransitions.size(); ++i) {
                    mPendingTransitions.get(i).startChangingAnimations();
@@ -4012,6 +4037,8 @@ public final class ViewRootImpl implements ViewParent,
                mActiveSurfaceSyncGroup.markSyncReady();
            }
        }
        mWasLastDrawCanceled = cancelAndRedraw;
        mLastTraversalWasVisible = isViewVisible;

        if (mAttachInfo.mContentCaptureEvents != null) {
            notifyContentCaptureEvents();
@@ -4815,17 +4842,26 @@ public final class ViewRootImpl implements ViewParent,
        mLastPerformDrawSkippedReason = null;
        if (mAttachInfo.mDisplayState == Display.STATE_OFF && !mReportNextDraw) {
            mLastPerformDrawSkippedReason = "screen_off";
            if (!mLastDrawScreenOff) {
                logAndTrace("Not drawing due to screen off");
            }
            mLastDrawScreenOff = true;
            return false;
        } else if (mView == null) {
            mLastPerformDrawSkippedReason = "no_root_view";
            return false;
        }

        if (mLastDrawScreenOff) {
            logAndTrace("Resumed drawing after screen turned on");
            mLastDrawScreenOff = false;
        }

        final boolean fullRedrawNeeded = mFullRedrawNeeded || surfaceSyncGroup != null;
        mFullRedrawNeeded = false;

        mIsDrawing = true;
        Trace.traceBegin(Trace.TRACE_TAG_VIEW, "draw");
        Trace.traceBegin(Trace.TRACE_TAG_VIEW, mTag + "-draw");

        addFrameCommitCallbackIfNeeded();

@@ -11788,4 +11824,11 @@ public final class ViewRootImpl implements ViewParent,
            @NonNull Consumer<Boolean> listener) {
        t.clearTrustedPresentationCallback(getSurfaceControl());
    }

    private void logAndTrace(String msg) {
        if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
            Trace.instant(Trace.TRACE_TAG_VIEW, mTag + "-" + msg);
        }
        Log.d(mTag, msg);
    }
}
+3 −1
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import static com.android.internal.util.FrameworkStatsLog.UIINTERACTION_FRAME_IN

import android.annotation.IntDef;
import android.annotation.Nullable;
import android.app.ActivityThread;
import android.hardware.display.DisplayManager;
import android.hardware.display.DisplayManagerGlobal;
import android.os.Handler;
@@ -147,7 +148,8 @@ public class DisplayResolutionTracker {
                public void registerDisplayListener(DisplayManager.DisplayListener listener) {
                    manager.registerDisplayListener(listener, handler,
                            DisplayManager.EVENT_FLAG_DISPLAY_ADDED
                                    | DisplayManager.EVENT_FLAG_DISPLAY_CHANGED);
                                    | DisplayManager.EVENT_FLAG_DISPLAY_CHANGED,
                            ActivityThread.currentPackageName());
                }

                @Override
Loading