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

Commit 356dcab5 authored by Santos Cordon's avatar Santos Cordon Committed by Automerger Merge Worker
Browse files

Merge "Add display layout classes." into sc-dev am: 02981fe4

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/13483715

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: I5251c1f84f27262fbe68fa2fd57ec2e271f64153
parents 97f46f69 02981fe4
Loading
Loading
Loading
Loading
+121 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.display;

import android.content.Context;
import android.hardware.devicestate.DeviceStateManager;
import android.text.TextUtils;
import android.util.IndentingPrintWriter;
import android.util.Slog;
import android.util.SparseArray;
import android.view.DisplayAddress;

import com.android.server.display.layout.Layout;

import java.util.Arrays;

/**
 * Mapping from device states into {@link Layout}s. This allows us to map device
 * states into specific layouts for the connected displays; particularly useful for
 * foldable and multi-display devices where the default display and which displays are ON can
 * change depending on the state of the device.
 */
class DeviceStateToLayoutMap {
    private static final String TAG = "DeviceStateToLayoutMap";

    public static final int STATE_DEFAULT = DeviceStateManager.INVALID_DEVICE_STATE;

    // TODO - b/168208162 - Remove these when we check in static definitions for layouts
    public static final int STATE_FOLDED = 100;
    public static final int STATE_UNFOLDED = 101;

    private final SparseArray<Layout> mLayoutMap = new SparseArray<>();

    DeviceStateToLayoutMap(Context context) {
        mLayoutMap.append(STATE_DEFAULT, new Layout());
        loadFoldedDisplayConfig(context);
    }

    public void dumpLocked(IndentingPrintWriter ipw) {
        ipw.println("DeviceStateToLayoutMap:");
        ipw.increaseIndent();

        ipw.println("Registered Layouts:");
        for (int i = 0; i < mLayoutMap.size(); i++) {
            ipw.println("state(" + mLayoutMap.keyAt(i) + "): " + mLayoutMap.valueAt(i));
        }
    }

    Layout get(int state) {
        Layout layout = mLayoutMap.get(state);
        if (layout == null) {
            layout = mLayoutMap.get(STATE_DEFAULT);
        }
        return layout;
    }

    private Layout create(int state) {
        if (mLayoutMap.contains(state)) {
            Slog.e(TAG, "Attempted to create a second layout for state " + state);
            return null;
        }

        final Layout layout = new Layout();
        mLayoutMap.append(state, layout);
        return layout;
    }

    private void loadFoldedDisplayConfig(Context context) {
        final String[] strDisplayIds = context.getResources().getStringArray(
                com.android.internal.R.array.config_internalFoldedPhysicalDisplayIds);

        if (strDisplayIds.length != 2 || TextUtils.isEmpty(strDisplayIds[0])
                || TextUtils.isEmpty(strDisplayIds[1])) {
            Slog.w(TAG, "Folded display configuration invalid: [" + Arrays.toString(strDisplayIds)
                    + "]");
            return;
        }

        final long[] displayIds;
        try {
            displayIds = new long[] {
                Long.parseLong(strDisplayIds[0]),
                Long.parseLong(strDisplayIds[1])
            };
        } catch (NumberFormatException nfe) {
            Slog.w(TAG, "Folded display config non numerical: " + Arrays.toString(strDisplayIds));
            return;
        }

        final int[] foldedDeviceStates = context.getResources().getIntArray(
                com.android.internal.R.array.config_foldedDeviceStates);
        // Only add folded states if folded state config is not empty
        if (foldedDeviceStates.length == 0) {
            return;
        }

        // Create the folded state layout
        final Layout foldedLayout = create(STATE_FOLDED);
        foldedLayout.createDisplayLocked(
                DisplayAddress.fromPhysicalDisplayId(displayIds[0]), true /*isDefault*/);

        // Create the unfolded state layout
        final Layout unfoldedLayout = create(STATE_UNFOLDED);
        unfoldedLayout.createDisplayLocked(
                DisplayAddress.fromPhysicalDisplayId(displayIds[1]), true /*isDefault*/);
    }
}
+6 −6
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server.display;
import android.annotation.NonNull;
import android.util.Slog;
import android.view.Display;
import android.view.DisplayAddress;

import com.android.internal.annotations.GuardedBy;
import com.android.server.display.DisplayManagerService.SyncRoot;
@@ -112,12 +113,11 @@ class DisplayDeviceRepository implements DisplayAdapter.Listener {
        }
    }

    public DisplayDevice getByIdLocked(@NonNull String uniqueId) {
        final int count = mDisplayDevices.size();
        for (int i = 0; i < count; i++) {
            final DisplayDevice d = mDisplayDevices.get(i);
            if (uniqueId.equals(d.getUniqueId())) {
                return d;
    public DisplayDevice getByAddressLocked(@NonNull DisplayAddress address) {
        for (int i = mDisplayDevices.size() - 1; i >= 0; i--) {
            final DisplayDevice device = mDisplayDevices.get(i);
            if (address.equals(device.getDisplayDeviceInfoLocked().address)) {
                return device;
            }
        }
        return null;
+1 −6
Original line number Diff line number Diff line
@@ -1188,6 +1188,7 @@ public final class DisplayManagerService extends SystemService {
            final LogicalDisplay display = mLogicalDisplayMapper.getLocked(device);
            final int state;
            final int displayId = display.getDisplayIdLocked();

            if (display.isEnabled()) {
                state = mDisplayStates.get(displayId);
            } else {
@@ -1564,12 +1565,6 @@ public final class DisplayManagerService extends SystemService {
        }
    }

    void setFoldOverride(Boolean isFolded) {
        synchronized (mSyncRoot) {
            mLogicalDisplayMapper.setFoldOverrideLocked(isFolded);
        }
    }

    private void clearViewportsLocked() {
        mViewports.clear();
    }
+0 −26
Original line number Diff line number Diff line
@@ -60,8 +60,6 @@ class DisplayManagerShellCommand extends ShellCommand {
                return setDisplayModeDirectorLoggingEnabled(false);
            case "dwb-set-cct":
                return setAmbientColorTemperatureOverride();
            case "set-fold":
                return setFoldOverride();
            default:
                return handleDefaultCommands(cmd);
        }
@@ -92,8 +90,6 @@ class DisplayManagerShellCommand extends ShellCommand {
        pw.println("    Disable display mode director logging.");
        pw.println("  dwb-set-cct CCT");
        pw.println("    Sets the ambient color temperature override to CCT (use -1 to disable).");
        pw.println("  set-fold [fold|unfold|reset]");
        pw.println("    Simulates the 'fold' state of a device. 'reset' for default behavior.");
        pw.println();
        Intent.printIntentArgsHelp(pw , "");
    }
@@ -165,26 +161,4 @@ class DisplayManagerShellCommand extends ShellCommand {
        mService.setAmbientColorTemperatureOverride(cct);
        return 0;
    }

    private int setFoldOverride() {
        String state = getNextArg();
        if (state == null) {
            getErrPrintWriter().println("Error: no parameter specified for set-fold");
            return 1;
        }
        final Boolean isFolded;
        if ("fold".equals(state)) {
            isFolded = true;
        } else if ("unfold".equals(state)) {
            isFolded = false;
        } else if ("reset".equals(state)) {
            isFolded = null;
        } else {
            getErrPrintWriter().println("Error: Invalid fold state request: " + state);
            return 1;
        }

        mService.setFoldOverride(isFolded);
        return 0;
    }
}
+9 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import android.view.SurfaceControl;
import com.android.server.wm.utils.InsetUtils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Objects;

@@ -718,6 +719,7 @@ final class LogicalDisplay {
    public void dumpLocked(PrintWriter pw) {
        pw.println("mDisplayId=" + mDisplayId);
        pw.println("mLayerStack=" + mLayerStack);
        pw.println("mIsEnabled=" + mIsEnabled);
        pw.println("mHasContent=" + mHasContent);
        pw.println("mDesiredDisplayModeSpecs={" + mDesiredDisplayModeSpecs + "}");
        pw.println("mRequestedColorMode=" + mRequestedColorMode);
@@ -731,4 +733,11 @@ final class LogicalDisplay {
        pw.println("mFrameRateOverrides=" + Arrays.toString(mFrameRateOverrides));
        pw.println("mPendingFrameRateOverrideUids=" + mPendingFrameRateOverrideUids);
    }

    @Override
    public String toString() {
        StringWriter sw = new StringWriter();
        dumpLocked(new PrintWriter(sw));
        return sw.toString();
    }
}
Loading