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

Commit 80d40316 authored by Fiona Campbell's avatar Fiona Campbell Committed by Android (Google) Code Review
Browse files

Merge "Add DisplayManager Logging" into main

parents 5a97f509 e1b7ffcf
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -31,6 +31,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;
@@ -775,7 +776,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 −7
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.
    //
@@ -130,6 +140,8 @@ public final class DisplayManagerGlobal {
    @VisibleForTesting
    public DisplayManagerGlobal(IDisplayManager dm) {
        mDm = dm;
        initExtraLogging();

        try {
            mWideColorSpace =
                    ColorSpace.get(
@@ -208,7 +220,7 @@ public final class DisplayManagerGlobal {

        registerCallbackIfNeededLocked();

        if (DEBUG) {
        if (DEBUG || extraLogging()) {
            Log.d(TAG, "getDisplayInfo: displayId=" + displayId + ", info=" + info);
        }
        return info;
@@ -321,12 +333,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);
    }

    /**
@@ -334,9 +348,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");
        }
@@ -345,15 +361,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();
        }
    }

@@ -362,6 +385,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) {
@@ -371,6 +398,18 @@ public final class DisplayManagerGlobal {
                updateCallbackIfNeededLocked();
            }
        }
        maybeLogAllDisplayListeners();
    }

    private void maybeLogAllDisplayListeners() {
        if (!sExtraDisplayListenerLogging) {
            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) {
@@ -1148,15 +1187,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(() -> {
@@ -1177,6 +1221,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)
@@ -1193,6 +1245,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);
                        }
@@ -1228,6 +1284,11 @@ public final class DisplayManagerGlobal {
                Trace.endSection();
            }
        }

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

    /**
@@ -1353,4 +1414,19 @@ 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);
        }
        return sExtraDisplayListenerLogging;
    }

    private static boolean extraLogging() {
        return sExtraDisplayListenerLogging && EXTRA_LOGGING_PACKAGE_NAME.equals(
                sCurrentPackageName);
    }
}
+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());
        }

    }
+2 −1
Original line number Diff line number Diff line
@@ -1549,7 +1549,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);
    }

    /**
+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