Adapt SurfaceControl to libgui API for display info

Bug: 144601064
Test: dumpsys display
Change-Id: I477e0e3126cc15eb5f120610b4e6b6b1568df389
diff --git a/libs/hwui/tests/common/TestContext.cpp b/libs/hwui/tests/common/TestContext.cpp
index e075d80..06f158f 100644
--- a/libs/hwui/tests/common/TestContext.cpp
+++ b/libs/hwui/tests/common/TestContext.cpp
@@ -22,43 +22,50 @@
 namespace uirenderer {
 namespace test {
 
-static const int IDENT_DISPLAYEVENT = 1;
-
-static android::DisplayInfo DUMMY_DISPLAY{
-        1080,           // w
-        1920,           // h
-        320.0,          // xdpi
-        320.0,          // ydpi
-        60.0,           // fps
-        2.0,            // density
-        ui::ROTATION_0, // orientation
-        false,          // secure?
-        0,              // appVsyncOffset
-        0,              // presentationDeadline
-};
-
-DisplayInfo getInternalDisplay() {
-#if !HWUI_NULL_GPU
-    DisplayInfo display;
-    const sp<IBinder> token = SurfaceComposerClient::getInternalDisplayToken();
-    LOG_ALWAYS_FATAL_IF(token == nullptr,
-                        "Failed to get display info because internal display is disconnected\n");
-    status_t status = SurfaceComposerClient::getDisplayInfo(token, &display);
-    LOG_ALWAYS_FATAL_IF(status, "Failed to get display info\n");
-    return display;
+const DisplayInfo& getDisplayInfo() {
+    static DisplayInfo info = [] {
+        DisplayInfo info;
+#if HWUI_NULL_GPU
+        info.density = 2.f;
 #else
-    return DUMMY_DISPLAY;
+        const sp<IBinder> token = SurfaceComposerClient::getInternalDisplayToken();
+        LOG_ALWAYS_FATAL_IF(!token, "%s: No internal display", __FUNCTION__);
+
+        const status_t status = SurfaceComposerClient::getDisplayInfo(token, &info);
+        LOG_ALWAYS_FATAL_IF(status, "%s: Failed to get display info", __FUNCTION__);
 #endif
+        return info;
+    }();
+
+    return info;
 }
 
-// Initialize to a dummy default
-android::DisplayInfo gDisplay = DUMMY_DISPLAY;
+const DisplayConfig& getActiveDisplayConfig() {
+    static DisplayConfig config = [] {
+        DisplayConfig config;
+#if HWUI_NULL_GPU
+        config.resolution = ui::Size(1080, 1920);
+        config.xDpi = config.yDpi = 320.f;
+        config.refreshRate = 60.f;
+#else
+        const sp<IBinder> token = SurfaceComposerClient::getInternalDisplayToken();
+        LOG_ALWAYS_FATAL_IF(!token, "%s: No internal display", __FUNCTION__);
+
+        const status_t status = SurfaceComposerClient::getActiveDisplayConfig(token, &config);
+        LOG_ALWAYS_FATAL_IF(status, "%s: Failed to get active display config", __FUNCTION__);
+#endif
+        return config;
+    }();
+
+    return config;
+}
 
 TestContext::TestContext() {
     mLooper = new Looper(true);
     mSurfaceComposerClient = new SurfaceComposerClient();
-    mLooper->addFd(mDisplayEventReceiver.getFd(), IDENT_DISPLAYEVENT, Looper::EVENT_INPUT, nullptr,
-                   nullptr);
+
+    constexpr int EVENT_ID = 1;
+    mLooper->addFd(mDisplayEventReceiver.getFd(), EVENT_ID, Looper::EVENT_INPUT, nullptr, nullptr);
 }
 
 TestContext::~TestContext() {}
@@ -79,8 +86,10 @@
 }
 
 void TestContext::createWindowSurface() {
-    mSurfaceControl = mSurfaceComposerClient->createSurface(String8("HwuiTest"), gDisplay.w,
-                                                            gDisplay.h, PIXEL_FORMAT_RGBX_8888);
+    const ui::Size& resolution = getActiveDisplayResolution();
+    mSurfaceControl =
+            mSurfaceComposerClient->createSurface(String8("HwuiTest"), resolution.getWidth(),
+                                                  resolution.getHeight(), PIXEL_FORMAT_RGBX_8888);
 
     SurfaceComposerClient::Transaction t;
     t.setLayer(mSurfaceControl, 0x7FFFFFF).show(mSurfaceControl).apply();
@@ -94,7 +103,8 @@
     producer->setMaxDequeuedBufferCount(3);
     producer->setAsyncMode(true);
     mConsumer = new BufferItemConsumer(consumer, GRALLOC_USAGE_HW_COMPOSER, 4);
-    mConsumer->setDefaultBufferSize(gDisplay.w, gDisplay.h);
+    const ui::Size& resolution = getActiveDisplayResolution();
+    mConsumer->setDefaultBufferSize(resolution.getWidth(), resolution.getHeight());
     mSurface = new Surface(producer);
 }
 
diff --git a/libs/hwui/tests/common/TestContext.h b/libs/hwui/tests/common/TestContext.h
index 116d4de..a012ecb 100644
--- a/libs/hwui/tests/common/TestContext.h
+++ b/libs/hwui/tests/common/TestContext.h
@@ -23,20 +23,25 @@
 #include <gui/Surface.h>
 #include <gui/SurfaceComposerClient.h>
 #include <gui/SurfaceControl.h>
+#include <ui/DisplayConfig.h>
 #include <ui/DisplayInfo.h>
 #include <utils/Looper.h>
 
 #include <atomic>
 #include <thread>
 
+#define dp(x) ((x) * android::uirenderer::test::getDisplayInfo().density)
+
 namespace android {
 namespace uirenderer {
 namespace test {
 
-extern DisplayInfo gDisplay;
-#define dp(x) ((x)*android::uirenderer::test::gDisplay.density)
+const DisplayInfo& getDisplayInfo();
+const DisplayConfig& getActiveDisplayConfig();
 
-DisplayInfo getInternalDisplay();
+inline const ui::Size& getActiveDisplayResolution() {
+    return getActiveDisplayConfig().resolution;
+}
 
 class TestContext {
 public:
diff --git a/libs/hwui/tests/macrobench/TestSceneRunner.cpp b/libs/hwui/tests/macrobench/TestSceneRunner.cpp
index 22d5abb..3b6baa7 100644
--- a/libs/hwui/tests/macrobench/TestSceneRunner.cpp
+++ b/libs/hwui/tests/macrobench/TestSceneRunner.cpp
@@ -109,16 +109,14 @@
 
 void run(const TestScene::Info& info, const TestScene::Options& opts,
          benchmark::BenchmarkReporter* reporter) {
-    // Switch to the real display
-    gDisplay = getInternalDisplay();
-
     Properties::forceDrawFrame = true;
     TestContext testContext;
     testContext.setRenderOffscreen(opts.renderOffscreen);
 
     // create the native surface
-    const int width = gDisplay.w;
-    const int height = gDisplay.h;
+    const ui::Size& resolution = getActiveDisplayResolution();
+    const int width = resolution.getWidth();
+    const int height = resolution.getHeight();
     sp<Surface> surface = testContext.surface();
 
     std::unique_ptr<TestScene> scene(info.createScene(opts));