Add developer option switch to set ANGLE as the default system driver

This change adds a new developer option switch called
"Enable ANGLE". It defaults to off. User can choose
to toggle it on and off, and the value of the system
property "persist.graphics.egl" is changed accordingly:

switch off: persist.graphics.egl=""
switch on: persist.graphics.egl="angle"

When user toggles the switch, a reboot window is
popped up asking user to reboot now to make the change
takes effect. If user chooses to cancel the reboot,
the switch is toggled back. This enforces that a reboot
is required whenever the "persis.graphics.egl" value
changes.

Upon reboot, we will load either ANGLE or native
GLES driver as the system driver, based on the value of
"persist.graphics.egl".

The switch is disabled if ANGLE is not installed
in /vendor partition. We use the system property
"ro.gfx.angle.supported" as an indicator. We set the
two conditions together in angle.mk file. Any device
mk file that inherits angle.mk file will result in
ANGLE libs installed in /vendor and "ro.gfx.angle.supported"
set to true.

Bug: b/270994705
Test: m; flash and check Pixel 7 boots fine
atest SettingsRoboTests:GraphicsDriverEnableAngleAsSystemDriverControllerTest

Change-Id: I565eff614472bb6ba50742e7dfa49b50dca2809f
This commit is contained in:
Yuxin Hu
2023-04-19 02:40:13 +00:00
parent 0927e2af1d
commit 25b270c0f8
9 changed files with 351 additions and 20 deletions

View File

@@ -0,0 +1,164 @@
/*
* Copyright 2023 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.settings.development.graphicsdriver;
import static com.android.settings.development.graphicsdriver.GraphicsDriverEnableAngleAsSystemDriverController.ANGLE_DRIVER_SUFFIX;
import static com.android.settings.development.graphicsdriver.GraphicsDriverEnableAngleAsSystemDriverController.PROPERTY_PERSISTENT_GRAPHICS_EGL;
import static com.android.settings.development.graphicsdriver.GraphicsDriverEnableAngleAsSystemDriverController.PROPERTY_RO_GFX_ANGLE_SUPPORTED;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.SystemProperties;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import com.android.settings.development.DevelopmentSettingsDashboardFragment;
import com.android.settings.development.RebootConfirmationDialogFragment;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowSystemProperties;
@RunWith(RobolectricTestRunner.class)
public class GraphicsDriverEnableAngleAsSystemDriverControllerTest {
private static final String TAG = "GraphicsDriverEnableAngleAsSystemDriverControllerTest";
@Mock private PreferenceScreen mScreen;
@Mock private SwitchPreference mPreference;
@Mock private DevelopmentSettingsDashboardFragment mFragment;
@Mock private FragmentActivity mActivity;
@Mock private FragmentManager mFragmentManager;
@Mock private FragmentTransaction mTransaction;
private GraphicsDriverEnableAngleAsSystemDriverController mController;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
doReturn(mTransaction).when(mFragmentManager).beginTransaction();
doReturn(mFragmentManager).when(mActivity).getSupportFragmentManager();
doReturn(mActivity).when(mFragment).getActivity();
mController = new GraphicsDriverEnableAngleAsSystemDriverController(mContext, mFragment);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
mController.displayPreference(mScreen);
}
@Test
public void onPreferenceChange_switchOn_shouldEnableAngleAsSystemDriver() {
ShadowSystemProperties.override(PROPERTY_RO_GFX_ANGLE_SUPPORTED, "true");
// since GraphicsEnvironment is mocked in Robolectric test environment,
// we will override the system property persist.graphics.egl as if it is changed by
// mGraphicsEnvironment.toggleAngleAsSystemDriver(true).
// TODO: b/270994705 yuxinhu:
// add test coverage to test mGraphicsEnvironment.toggleAngleAsSystemDriver()
// works properly on Android devices / emulators.
ShadowSystemProperties.override(PROPERTY_PERSISTENT_GRAPHICS_EGL, ANGLE_DRIVER_SUFFIX);
mController.onPreferenceChange(mPreference, true);
final String systemEGLDriver = SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL);
assertThat(systemEGLDriver).isEqualTo(ANGLE_DRIVER_SUFFIX); // empty
verify(mTransaction).add(any(RebootConfirmationDialogFragment.class), any());
}
@Test
public void onPreferenceChange_switchOff_shouldDisableAngleAsSystemDriver() {
ShadowSystemProperties.override(PROPERTY_RO_GFX_ANGLE_SUPPORTED, "true");
// since GraphicsEnvironment is mocked in Robolectric test environment,
// we will override the system property persist.graphics.egl as if it is changed by
// mGraphicsEnvironment.toggleAngleAsSystemDriver(false).
// TODO: b/270994705 yuxinhu:
// add test coverage to test mGraphicsEnvironment.toggleAngleAsSystemDriver()
// works properly on Android devices / emulators.
ShadowSystemProperties.override(PROPERTY_PERSISTENT_GRAPHICS_EGL, "");
mController.onPreferenceChange(mPreference, false);
final String systemEGLDriver = SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL);
assertThat(systemEGLDriver).isEqualTo("");
verify(mTransaction).add(any(RebootConfirmationDialogFragment.class), any());
}
@Test
public void updateState_angleNotSupported_preferenceShouldNotBeChecked() {
ShadowSystemProperties.override(PROPERTY_RO_GFX_ANGLE_SUPPORTED, "");
mController.updateState(mPreference);
verify(mPreference).setChecked(false);
}
@Test
public void updateState_angleNotSupported_preferenceShouldNotBeEnabled() {
ShadowSystemProperties.override(PROPERTY_RO_GFX_ANGLE_SUPPORTED, "");
mController.updateState(mPreference);
verify(mPreference).setEnabled(false);
}
@Test
public void updateState_angleSupported_angleUsed_preferenceShouldBeChecked() {
ShadowSystemProperties.override(PROPERTY_RO_GFX_ANGLE_SUPPORTED, "true");
// TODO: b/270994705 yuxinhu:
// add test coverage to test mGraphicsEnvironment.toggleAngleAsSystemDriver()
// works properly on Android devices / emulators.
ShadowSystemProperties.override(PROPERTY_PERSISTENT_GRAPHICS_EGL, ANGLE_DRIVER_SUFFIX);
mController.updateState(mPreference);
verify(mPreference).setChecked(true); //false
}
@Test
public void updateState_angleSupported_angleNotUsed_preferenceShouldNotBeChecked() {
ShadowSystemProperties.override(PROPERTY_RO_GFX_ANGLE_SUPPORTED, "true");
// TODO: b/270994705 yuxinhu:
// add test coverage to test mGraphicsEnvironment.toggleAngleAsSystemDriver(false)
// works properly on Android devices / emulators.
ShadowSystemProperties.override(PROPERTY_PERSISTENT_GRAPHICS_EGL, "");
mController.updateState(mPreference);
verify(mPreference).setChecked(false);
}
@Test
public void onDeveloperOptionSwitchDisabled_shouldDisableAngleAsSystemDriver() {
mController.onDeveloperOptionsSwitchDisabled();
final String systemEGLDriver = SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL);
assertThat(systemEGLDriver).isEqualTo("");
}
@Test
public void onDeveloperOptionSwitchDisabled_preferenceShouldNotBeChecked() {
mController.onDeveloperOptionsSwitchDisabled();
verify(mPreference).setChecked(false);
}
@Test
public void onDeveloperOptionsSwitchDisabled_preferenceShouldNotBeEnabled() {
mController.onDeveloperOptionsSwitchDisabled();
verify(mPreference).setEnabled(false);
}
}