1 //
2 // Copyright 2022 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // EGLPowerPreferenceTest.cpp:
7 // Checks the implementation of EGL_ANGLE_power_preference.
8 //
9
10 #include <gtest/gtest.h>
11 #include <tuple>
12
13 #include "common/debug.h"
14 #include "common/string_utils.h"
15 #include "gpu_info_util/SystemInfo.h"
16 #include "test_utils/ANGLETest.h"
17 #include "test_utils/angle_test_platform.h"
18 #include "test_utils/system_info_util.h"
19 #include "util/OSWindow.h"
20
21 using namespace angle;
22
23 class EGLPowerPreferenceTest : public ANGLETest<>
24 {
25 public:
testSetUp()26 void testSetUp() override { (void)GetSystemInfo(&mSystemInfo); }
27
28 protected:
getGpuIdParts(size_t gpuIndex) const29 auto getGpuIdParts(size_t gpuIndex) const
30 {
31 uint64_t deviceId = mSystemInfo.gpus[gpuIndex].systemDeviceId;
32 return std::make_tuple(GetSystemDeviceIdHighPart(deviceId),
33 GetSystemDeviceIdLowPart(deviceId));
34 }
35
getDisplay() const36 EGLDisplay getDisplay() const { return getEGLWindow()->getDisplay(); }
37
38 SystemInfo mSystemInfo;
39 };
40
TEST_P(EGLPowerPreferenceTest,ForceGPUSwitch)41 TEST_P(EGLPowerPreferenceTest, ForceGPUSwitch)
42 {
43 ANGLE_SKIP_TEST_IF(!IsEGLDisplayExtensionEnabled(getDisplay(), "EGL_ANGLE_power_preference"));
44 size_t lowPower = FindLowPowerGPU(mSystemInfo);
45 size_t highPower = FindHighPowerGPU(mSystemInfo);
46 size_t initialGPU = FindActiveOpenGLGPU(mSystemInfo);
47 ASSERT_TRUE(lowPower == initialGPU || highPower == initialGPU);
48
49 EGLint hi = 0;
50 EGLint lo = 0;
51
52 for (int i = 0; i < 5; ++i)
53 {
54 std::tie(hi, lo) = getGpuIdParts(lowPower);
55 eglForceGPUSwitchANGLE(getDisplay(), hi, lo);
56 EXPECT_EQ(lowPower, FindActiveOpenGLGPU(mSystemInfo));
57 std::tie(hi, lo) = getGpuIdParts(highPower);
58 eglForceGPUSwitchANGLE(getDisplay(), hi, lo);
59 EXPECT_EQ(highPower, FindActiveOpenGLGPU(mSystemInfo));
60 }
61 }
62
TEST_P(EGLPowerPreferenceTest,HandleGPUSwitchAfterForceGPUSwitch)63 TEST_P(EGLPowerPreferenceTest, HandleGPUSwitchAfterForceGPUSwitch)
64 {
65 ANGLE_SKIP_TEST_IF(!IsEGLDisplayExtensionEnabled(getDisplay(), "EGL_ANGLE_power_preference"));
66 size_t initialGPU = FindActiveOpenGLGPU(mSystemInfo);
67 size_t changedGPU = FindLowPowerGPU(mSystemInfo);
68 // On all platforms the extension is implemented (e.g. CGL): If we start with integrated, and
69 // force DGPU, we cannot eglHandleGPUSwitchANGLE() from DGPU to integrated.
70 // eglHandleGPUSwitchANGLE() will switch to the "default", which will be DGPU.
71 // If we start with DGPU and switch to integrated, we *can* eglHandleGPUSwitchANGLE() back
72 // to the default, DGPU.
73 ANGLE_SKIP_TEST_IF(initialGPU == changedGPU);
74
75 EGLint hi = 0;
76 EGLint lo = 0;
77 for (int i = 0; i < 5; ++i)
78 {
79 std::tie(hi, lo) = getGpuIdParts(changedGPU);
80 eglForceGPUSwitchANGLE(getDisplay(), hi, lo);
81 ASSERT_EQ(changedGPU, FindActiveOpenGLGPU(mSystemInfo));
82 eglHandleGPUSwitchANGLE(getDisplay());
83 ASSERT_EQ(initialGPU, FindActiveOpenGLGPU(mSystemInfo));
84 }
85 }
86
87 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EGLPowerPreferenceTest);
88 ANGLE_INSTANTIATE_TEST(EGLPowerPreferenceTest, ES2_OPENGL(), ES3_OPENGL());
89