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

Commit 1418f411 authored by Xiang Wang's avatar Xiang Wang
Browse files

Add GameModeConfiguration

Bug: b/243448953
Test: atest GameModeConfigurationTest GameModeInfoTest
Change-Id: Ie1db1889c82ca01ede6de9c68076f67a423ae484
parent 8954f782
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -813,11 +813,29 @@ package android.app {
    method @RequiresPermission(android.Manifest.permission.MANAGE_GAME_MODE) public void setGameMode(@NonNull String, int);
  }
  public final class GameModeConfiguration implements android.os.Parcelable {
    method public int describeContents();
    method public int getFpsOverride();
    method public float getScalingFactor();
    method @NonNull public android.app.GameModeConfiguration.Builder toBuilder();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.app.GameModeConfiguration> CREATOR;
    field public static final int FPS_OVERRIDE_NONE = 0; // 0x0
  }
  public static final class GameModeConfiguration.Builder {
    ctor public GameModeConfiguration.Builder();
    method @NonNull public android.app.GameModeConfiguration build();
    method @NonNull public android.app.GameModeConfiguration.Builder setFpsOverride(int);
    method @NonNull public android.app.GameModeConfiguration.Builder setScalingFactor(float);
  }
  public final class GameModeInfo implements android.os.Parcelable {
    ctor @Deprecated public GameModeInfo(int, @NonNull int[]);
    method public int describeContents();
    method public int getActiveGameMode();
    method @NonNull public int[] getAvailableGameModes();
    method @Nullable public android.app.GameModeConfiguration getGameModeConfiguration(int);
    method @NonNull public int[] getOptedInGameModes();
    method public boolean isDownscalingAllowed();
    method public boolean isFpsOverrideAllowed();
@@ -832,6 +850,7 @@ package android.app {
    method @NonNull public android.app.GameModeInfo.Builder setAvailableGameModes(@NonNull int[]);
    method @NonNull public android.app.GameModeInfo.Builder setDownscalingAllowed(boolean);
    method @NonNull public android.app.GameModeInfo.Builder setFpsOverrideAllowed(boolean);
    method @NonNull public android.app.GameModeInfo.Builder setGameModeConfiguration(int, @NonNull android.app.GameModeConfiguration);
    method @NonNull public android.app.GameModeInfo.Builder setOptedInGameModes(@NonNull int[]);
  }
+20 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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 android.app;

/** @hide*/
parcelable GameModeConfiguration;
 No newline at end of file
+192 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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 android.app;

import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.Display;

import androidx.annotation.FloatRange;
import androidx.annotation.IntRange;

import com.android.internal.annotations.Immutable;
import com.android.internal.util.Preconditions;

/**
 * GameModeConfiguration is the game's platform configuration for a game mode.
 * <p>
 * Only the game modes that are enabled by OEMs will have an active configuration, whereas game
 * modes opted in by the game will not.
 *
 * @hide
 */
@Immutable
@SystemApi
public final class GameModeConfiguration implements Parcelable {
    // Default value indicating that no FPS override will be applied as game intervention, or
    // default to the current display mode's frame rate.
    public static final int FPS_OVERRIDE_NONE = 0;

    public static final @NonNull Creator<GameModeConfiguration> CREATOR = new Creator<>() {
        @Override
        public GameModeConfiguration createFromParcel(Parcel in) {
            return new GameModeConfiguration(in);
        }

        @Override
        public GameModeConfiguration[] newArray(int size) {
            return new GameModeConfiguration[size];
        }
    };

    /**
     * Builder for {@link GameModeConfiguration}.
     *
     * @hide
     */
    @SystemApi
    public static final class Builder {
        /** Constructs a new Builder for a game mode’s configuration */
        public Builder() {
        }

        /**
         * Sets the scaling factor used for game resolution downscaling.
         * <br>
         *
         * @param scalingFactor the desired scaling factor ranged from 0.1 to 1.0 inclusively
         * @throws IllegalArgumentException if the scaling factor is not in range of [0.1, 1.0]
         */
        @NonNull
        public GameModeConfiguration.Builder setScalingFactor(
                @FloatRange(from = 0.1, to = 1.0) float scalingFactor) {
            Preconditions.checkArgument(scalingFactor >= 0.1 && scalingFactor <= 1.0,
                    "Scaling factor should fall between 0.1 and 1.0 (inclusive)");
            mScalingFactor = scalingFactor;
            return this;
        }

        /**
         * Sets the FPS override used for game frame rate throttling.
         * <br>
         * The list of valid throttled frame rates can be queried by
         * <ol>
         * <li>Obtain display modes by calling {@link Display#getSupportedModes}
         * <li>For each mode, get valid FPS by getting the divisor of the
         * {@link Display.Mode#getRefreshRate()} that is >= 30,
         * e.g. when Display.Mode#getRefreshRate() is 120 Hz, the valid FPS
         * of this mode is 120, 60, 40, 30
         * <li>Aggregate the valid FPS of each mode to get the full list
         * </ol>
         * <br>
         *
         * @param fpsOverride the desired non-negative FPS override value, default to
         *                    {@link #FPS_OVERRIDE_NONE}.
         * @throws IllegalArgumentException if the provided value is negative
         */
        @NonNull
        public GameModeConfiguration.Builder setFpsOverride(@IntRange(from = 0) int fpsOverride) {
            Preconditions.checkArgument(fpsOverride >= 0,
                    "FPS override should be non-negative");
            mFpsOverride = fpsOverride;
            return this;
        }

        /**
         * Builds a GameModeConfiguration.
         */
        @NonNull
        public GameModeConfiguration build() {
            return new GameModeConfiguration(mScalingFactor, mFpsOverride);
        }

        ;
        private float mScalingFactor;
        private int mFpsOverride;
    }

    GameModeConfiguration(float scalingFactor, int fpsOverride) {
        this.mScalingFactor = scalingFactor;
        this.mFpsOverride = fpsOverride;
    }

    GameModeConfiguration(Parcel in) {
        this.mScalingFactor = in.readFloat();
        this.mFpsOverride = in.readInt();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeFloat(mScalingFactor);
        dest.writeInt(mFpsOverride);
    }

    /**
     * Gets the scaling factor used for game resolution downscaling.
     */
    public float getScalingFactor() {
        return mScalingFactor;
    }

    /**
     * Gets the FPS override used for frame rate throttling.
     */
    public int getFpsOverride() {
        return mFpsOverride;
    }

    /**
     * Converts and returns the game mode config as a new builder.
     */
    @NonNull
    public GameModeConfiguration.Builder toBuilder() {
        return new GameModeConfiguration.Builder()
                .setFpsOverride(mFpsOverride)
                .setScalingFactor(mScalingFactor);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof GameModeConfiguration)) {
            return false;
        }
        GameModeConfiguration config = (GameModeConfiguration) obj;
        return config.mFpsOverride == this.mFpsOverride
                && config.mScalingFactor == this.mScalingFactor;
    }

    @Override
    public int hashCode() {
        int result = 7;
        result = 31 * result + mFpsOverride;
        result = 31 * result + Float.floatToIntBits(mScalingFactor);
        return result;
    }

    private final float mScalingFactor;
    private final int mFpsOverride;
}
+37 −4
Original line number Diff line number Diff line
@@ -20,10 +20,14 @@ import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.ArrayMap;

import androidx.annotation.Nullable;

import com.android.internal.annotations.VisibleForTesting;

import java.util.Arrays;
import java.util.Map;

/**
 * GameModeInfo returned from {@link GameManager#getGameModeInfo(String)}.
@@ -120,13 +124,24 @@ public final class GameModeInfo implements Parcelable {
            return this;
        }

        /**
         * Sets the GameModeConfiguration for a game mode.
         */
        @NonNull
        public GameModeInfo.Builder setGameModeConfiguration(
                @GameManager.GameMode int gameMode,
                @NonNull GameModeConfiguration gameModeConfiguration) {
            mConfigMap.put(gameMode, gameModeConfiguration);
            return this;
        }

        /**
         * Builds a GameModeInfo.
         */
        @NonNull
        public GameModeInfo build() {
            return new GameModeInfo(mActiveGameMode, mAvailableGameModes, mOptedInGameModes,
                    mIsDownscalingAllowed, mIsFpsOverrideAllowed);
                    mIsDownscalingAllowed, mIsFpsOverrideAllowed, mConfigMap);
        }

        private @GameManager.GameMode int[] mAvailableGameModes = new int[]{};
@@ -134,6 +149,7 @@ public final class GameModeInfo implements Parcelable {
        private @GameManager.GameMode int mActiveGameMode;
        private boolean mIsDownscalingAllowed;
        private boolean mIsFpsOverrideAllowed;
        private Map<Integer, GameModeConfiguration> mConfigMap = new ArrayMap<>();
    }

    /**
@@ -143,18 +159,19 @@ public final class GameModeInfo implements Parcelable {
     */
    public GameModeInfo(@GameManager.GameMode int activeGameMode,
            @NonNull @GameManager.GameMode int[] availableGameModes) {
        this(activeGameMode, availableGameModes, new int[]{}, true, true);
        this(activeGameMode, availableGameModes, new int[]{}, true, true, new ArrayMap<>());
    }

    GameModeInfo(@GameManager.GameMode int activeGameMode,
    private GameModeInfo(@GameManager.GameMode int activeGameMode,
            @NonNull @GameManager.GameMode int[] availableGameModes,
            @NonNull @GameManager.GameMode int[] optedInGameModes, boolean isDownscalingAllowed,
            boolean isFpsOverrideAllowed) {
            boolean isFpsOverrideAllowed, @NonNull Map<Integer, GameModeConfiguration> configMap) {
        mActiveGameMode = activeGameMode;
        mAvailableGameModes = Arrays.copyOf(availableGameModes, availableGameModes.length);
        mOptedInGameModes = Arrays.copyOf(optedInGameModes, optedInGameModes.length);
        mIsDownscalingAllowed = isDownscalingAllowed;
        mIsFpsOverrideAllowed = isFpsOverrideAllowed;
        mConfigMap = configMap;
    }

    /** @hide */
@@ -165,6 +182,9 @@ public final class GameModeInfo implements Parcelable {
        mOptedInGameModes = in.createIntArray();
        mIsDownscalingAllowed = in.readBoolean();
        mIsFpsOverrideAllowed = in.readBoolean();
        mConfigMap = new ArrayMap<>();
        in.readMap(mConfigMap,
                getClass().getClassLoader(), Integer.class, GameModeConfiguration.class);
    }

    /**
@@ -199,6 +219,17 @@ public final class GameModeInfo implements Parcelable {
        return Arrays.copyOf(mOptedInGameModes, mOptedInGameModes.length);
    }

    /**
     * Gets the current game mode configuration of a game mode.
     * <p>
     * The game mode can be null if it's opted in by the game itself, or not configured in device
     * config nor set by the user as custom game mode configuration.
     */
    public @Nullable GameModeConfiguration getGameModeConfiguration(
            @GameManager.GameMode int gameMode) {
        return mConfigMap.get(gameMode);
    }

    /**
     * Returns if downscaling is allowed (not opted out) by the game in their Game Mode config.
     * <p>
@@ -223,6 +254,7 @@ public final class GameModeInfo implements Parcelable {
    private final @GameManager.GameMode int mActiveGameMode;
    private final boolean mIsDownscalingAllowed;
    private final boolean mIsFpsOverrideAllowed;
    private final Map<Integer, GameModeConfiguration> mConfigMap;

    @Override
    public int describeContents() {
@@ -236,5 +268,6 @@ public final class GameModeInfo implements Parcelable {
        dest.writeIntArray(mOptedInGameModes);
        dest.writeBoolean(mIsDownscalingAllowed);
        dest.writeBoolean(mIsFpsOverrideAllowed);
        dest.writeMap(mConfigMap);
    }
}
+94 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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 android.app;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import android.platform.test.annotations.Presubmit;

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

@SmallTest
@Presubmit
@RunWith(AndroidJUnit4.class)
public class GameModeConfigurationTest {
    @Test
    public void testEqualsAndHashCode() {
        GameModeConfiguration config = new GameModeConfiguration.Builder()
                .setScalingFactor(0.5f).setFpsOverride(10).build();
        assertTrue(config.equals(config));

        GameModeConfiguration config1 = new GameModeConfiguration.Builder()
                .setScalingFactor(0.5f).setFpsOverride(10).build();
        assertTrue(config.equals(config1));
        assertEquals(config.hashCode(), config1.hashCode());

        GameModeConfiguration config2 = new GameModeConfiguration.Builder()
                .setScalingFactor(0.5f).build();
        assertFalse(config.equals(config2));
        assertNotEquals(config.hashCode(), config2.hashCode());

        GameModeConfiguration config3 = new GameModeConfiguration.Builder()
                .setFpsOverride(10).build();
        assertFalse(config.equals(config3));
        assertNotEquals(config.hashCode(), config3.hashCode());
        assertFalse(config2.equals(config3));
        assertNotEquals(config2.hashCode(), config3.hashCode());

        GameModeConfiguration config4 = new GameModeConfiguration.Builder()
                .setScalingFactor(0.2f).setFpsOverride(10).build();
        assertFalse(config.equals(config4));
        assertNotEquals(config.hashCode(), config4.hashCode());

        GameModeConfiguration config5 = new GameModeConfiguration.Builder()
                .setScalingFactor(0.5f).setFpsOverride(30).build();
        assertFalse(config.equals(config5));
        assertNotEquals(config.hashCode(), config5.hashCode());

        GameModeConfiguration config6 = new GameModeConfiguration.Builder()
                .build();
        assertFalse(config.equals(config6));
        assertNotEquals(config.hashCode(), config6.hashCode());
        assertFalse(config2.equals(config6));
        assertNotEquals(config2.hashCode(), config6.hashCode());
        assertFalse(config3.equals(config6));
        assertNotEquals(config3.hashCode(), config6.hashCode());
    }

    @Test
    public void testToBuilder() {
        GameModeConfiguration config = new GameModeConfiguration
                .Builder().setFpsOverride(40).setScalingFactor(0.5f).build();
        GameModeConfiguration newConfig = config.toBuilder().build();
        assertEquals(config, newConfig);
    }

    @Test
    public void testGetters() {
        GameModeConfiguration config = new GameModeConfiguration.Builder()
                .setScalingFactor(0.5f).setFpsOverride(10).build();
        assertEquals(0.5f, config.getScalingFactor(), 0.01f);
        assertEquals(10, config.getFpsOverride());
    }
}
Loading