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

Commit e1b7ffcf authored by Fiona Campbell's avatar Fiona Campbell
Browse files

Add DisplayManager Logging

- Add logging to registering & unregistering listeners in display
  manager.
- Add logging to follow path of listener notifications

Bug: 284687313
Test: adb shell setprop persist.debug.vri_package <package-name>

Change-Id: I5093162cbfa632bafe6870c84177952becc82c50
parent c41764f9
Loading
Loading
Loading
Loading
+3 −1
Original line number Original line Diff line number Diff line
@@ -31,6 +31,7 @@ import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.annotation.SystemService;
import android.annotation.TestApi;
import android.annotation.TestApi;
import android.app.ActivityThread;
import android.app.KeyguardManager;
import android.app.KeyguardManager;
import android.compat.annotation.UnsupportedAppUsage;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.content.Context;
@@ -775,7 +776,8 @@ public final class DisplayManager {
     */
     */
    public void registerDisplayListener(@NonNull DisplayListener listener,
    public void registerDisplayListener(@NonNull DisplayListener listener,
            @Nullable Handler handler, @EventsMask long eventsMask) {
            @Nullable Handler handler, @EventsMask long eventsMask) {
        mGlobal.registerDisplayListener(listener, handler, eventsMask);
        mGlobal.registerDisplayListener(listener, handler, eventsMask,
                ActivityThread.currentPackageName());
    }
    }


    /**
    /**
+83 −7
Original line number Original line Diff line number Diff line
@@ -25,6 +25,7 @@ import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.annotation.RequiresPermission;
import android.app.ActivityThread;
import android.app.PropertyInvalidatedCache;
import android.app.PropertyInvalidatedCache;
import android.compat.annotation.UnsupportedAppUsage;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.content.Context;
@@ -45,8 +46,11 @@ import android.os.Message;
import android.os.RemoteException;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.ServiceManager;
import android.os.Trace;
import android.os.Trace;
import android.sysprop.DisplayProperties;
import android.text.TextUtils;
import android.util.Log;
import android.util.Log;
import android.util.Pair;
import android.util.Pair;
import android.util.Slog;
import android.util.SparseArray;
import android.util.SparseArray;
import android.view.Display;
import android.view.Display;
import android.view.DisplayAdjustments;
import android.view.DisplayAdjustments;
@@ -72,7 +76,13 @@ import java.util.concurrent.atomic.AtomicLong;
 */
 */
public final class DisplayManagerGlobal {
public final class DisplayManagerGlobal {
    private static final String TAG = "DisplayManager";
    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.
    // True if display info and display ids should be cached.
    //
    //
@@ -130,6 +140,8 @@ public final class DisplayManagerGlobal {
    @VisibleForTesting
    @VisibleForTesting
    public DisplayManagerGlobal(IDisplayManager dm) {
    public DisplayManagerGlobal(IDisplayManager dm) {
        mDm = dm;
        mDm = dm;
        initExtraLogging();

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


        registerCallbackIfNeededLocked();
        registerCallbackIfNeededLocked();


        if (DEBUG) {
        if (DEBUG || extraLogging()) {
            Log.d(TAG, "getDisplayInfo: displayId=" + displayId + ", info=" + info);
            Log.d(TAG, "getDisplayInfo: displayId=" + displayId + ", info=" + info);
        }
        }
        return 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,
     * If null, listener will use the handler for the current thread, and if still null,
     * the handler for the main thread.
     * the handler for the main thread.
     * If that is still null, a runtime exception will be thrown.
     * If that is still null, a runtime exception will be thrown.
     * @param packageName of the calling package.
     */
     */
    public void registerDisplayListener(@NonNull DisplayListener listener,
    public void registerDisplayListener(@NonNull DisplayListener listener,
            @Nullable Handler handler, @EventsMask long eventsMask) {
            @Nullable Handler handler, @EventsMask long eventsMask, String packageName) {
        Looper looper = getLooperForHandler(handler);
        Looper looper = getLooperForHandler(handler);
        Handler springBoard = new Handler(looper);
        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 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 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,
    public void registerDisplayListener(@NonNull DisplayListener listener,
            @NonNull Executor executor, @EventsMask long eventsMask) {
            @NonNull Executor executor, @EventsMask long eventsMask, String packageName) {
        if (listener == null) {
        if (listener == null) {
            throw new IllegalArgumentException("listener must not be 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.");
            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) {
        synchronized (mLock) {
            int index = findDisplayListenerLocked(listener);
            int index = findDisplayListenerLocked(listener);
            if (index < 0) {
            if (index < 0) {
                mDisplayListeners.add(new DisplayListenerDelegate(listener, executor, eventsMask));
                mDisplayListeners.add(new DisplayListenerDelegate(listener, executor, eventsMask,
                        packageName));
                registerCallbackIfNeededLocked();
                registerCallbackIfNeededLocked();
            } else {
            } else {
                mDisplayListeners.get(index).setEventsMask(eventsMask);
                mDisplayListeners.get(index).setEventsMask(eventsMask);
            }
            }
            updateCallbackIfNeededLocked();
            updateCallbackIfNeededLocked();
            maybeLogAllDisplayListeners();
        }
        }
    }
    }


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


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

        synchronized (mLock) {
        synchronized (mLock) {
            int index = findDisplayListenerLocked(listener);
            int index = findDisplayListenerLocked(listener);
            if (index >= 0) {
            if (index >= 0) {
@@ -371,6 +398,18 @@ public final class DisplayManagerGlobal {
                updateCallbackIfNeededLocked();
                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) {
    private static Looper getLooperForHandler(@Nullable Handler handler) {
@@ -1148,15 +1187,20 @@ public final class DisplayManagerGlobal {
        private final DisplayInfo mDisplayInfo = new DisplayInfo();
        private final DisplayInfo mDisplayInfo = new DisplayInfo();
        private final Executor mExecutor;
        private final Executor mExecutor;
        private AtomicLong mGenerationId = new AtomicLong(1);
        private AtomicLong mGenerationId = new AtomicLong(1);
        private final String mPackageName;


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


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


        private void handleMessage(Message msg) {
        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) {
            if (DEBUG) {
                Trace.beginSection(
                Trace.beginSection(
                        "DisplayListenerDelegate(" + eventToString(msg.what)
                        "DisplayListenerDelegate(" + eventToString(msg.what)
@@ -1193,6 +1245,10 @@ public final class DisplayManagerGlobal {
                    if ((mEventsMask & DisplayManager.EVENT_FLAG_DISPLAY_CHANGED) != 0) {
                    if ((mEventsMask & DisplayManager.EVENT_FLAG_DISPLAY_CHANGED) != 0) {
                        DisplayInfo newInfo = (DisplayInfo) msg.obj;
                        DisplayInfo newInfo = (DisplayInfo) msg.obj;
                        if (newInfo != null && !newInfo.equals(mDisplayInfo)) {
                        if (newInfo != null && !newInfo.equals(mDisplayInfo)) {
                            if (extraLogging()) {
                                Slog.i(TAG, "Sending onDisplayChanged: Display Changed. Info: "
                                        + newInfo);
                            }
                            mDisplayInfo.copyFrom(newInfo);
                            mDisplayInfo.copyFrom(newInfo);
                            mListener.onDisplayChanged(msg.arg1);
                            mListener.onDisplayChanged(msg.arg1);
                        }
                        }
@@ -1228,6 +1284,11 @@ public final class DisplayManagerGlobal {
                Trace.endSection();
                Trace.endSection();
            }
            }
        }
        }

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


    /**
    /**
@@ -1353,4 +1414,19 @@ public final class DisplayManagerGlobal {
        }
        }
        return "UNKNOWN";
        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 Original line Diff line number Diff line
@@ -26,6 +26,7 @@ import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.annotation.RequiresPermission;
import android.annotation.SuppressLint;
import android.annotation.SuppressLint;
import android.annotation.TestApi;
import android.annotation.TestApi;
import android.app.ActivityThread;
import android.app.KeyguardManager;
import android.app.KeyguardManager;
import android.app.WindowConfiguration;
import android.app.WindowConfiguration;
import android.compat.annotation.UnsupportedAppUsage;
import android.compat.annotation.UnsupportedAppUsage;
@@ -1366,7 +1367,8 @@ public final class Display {
            // form of the larger DISPLAY_CHANGED event
            // form of the larger DISPLAY_CHANGED event
            mGlobal.registerDisplayListener(toRegister, executor,
            mGlobal.registerDisplayListener(toRegister, executor,
                    DisplayManager.EVENT_FLAG_HDR_SDR_RATIO_CHANGED
                    DisplayManager.EVENT_FLAG_HDR_SDR_RATIO_CHANGED
                            | DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
                            | DisplayManagerGlobal.EVENT_DISPLAY_CHANGED,
                    ActivityThread.currentPackageName());
        }
        }


    }
    }
+2 −1
Original line number Original line Diff line number Diff line
@@ -1549,7 +1549,8 @@ public final class ViewRootImpl implements ViewParent,
                        mHandler,
                        mHandler,
                        DisplayManager.EVENT_FLAG_DISPLAY_ADDED
                        DisplayManager.EVENT_FLAG_DISPLAY_ADDED
                        | DisplayManager.EVENT_FLAG_DISPLAY_CHANGED
                        | DisplayManager.EVENT_FLAG_DISPLAY_CHANGED
                        | DisplayManager.EVENT_FLAG_DISPLAY_REMOVED);
                        | DisplayManager.EVENT_FLAG_DISPLAY_REMOVED,
                        mBasePackageName);
    }
    }


    /**
    /**
+3 −1
Original line number Original line 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.IntDef;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.app.ActivityThread;
import android.hardware.display.DisplayManager;
import android.hardware.display.DisplayManager;
import android.hardware.display.DisplayManagerGlobal;
import android.hardware.display.DisplayManagerGlobal;
import android.os.Handler;
import android.os.Handler;
@@ -147,7 +148,8 @@ public class DisplayResolutionTracker {
                public void registerDisplayListener(DisplayManager.DisplayListener listener) {
                public void registerDisplayListener(DisplayManager.DisplayListener listener) {
                    manager.registerDisplayListener(listener, handler,
                    manager.registerDisplayListener(listener, handler,
                            DisplayManager.EVENT_FLAG_DISPLAY_ADDED
                            DisplayManager.EVENT_FLAG_DISPLAY_ADDED
                                    | DisplayManager.EVENT_FLAG_DISPLAY_CHANGED);
                                    | DisplayManager.EVENT_FLAG_DISPLAY_CHANGED,
                            ActivityThread.currentPackageName());
                }
                }


                @Override
                @Override
Loading