xref: /aosp_15_r20/frameworks/base/libs/hwui/tests/common/TestContext.cpp (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "tests/common/TestContext.h"
18 
19 #include <com_android_graphics_libgui_flags.h>
20 #include <cutils/trace.h>
21 
22 namespace android {
23 namespace uirenderer {
24 namespace test {
25 
getDisplayInfo()26 const ui::StaticDisplayInfo& getDisplayInfo() {
27     static ui::StaticDisplayInfo info = [] {
28         ui::StaticDisplayInfo info;
29 #if HWUI_NULL_GPU
30         info.density = 2.f;
31 #else
32         const std::vector<PhysicalDisplayId> ids = SurfaceComposerClient::getPhysicalDisplayIds();
33         LOG_ALWAYS_FATAL_IF(ids.empty(), "%s: No displays", __FUNCTION__);
34 
35         const status_t status =
36                 SurfaceComposerClient::getStaticDisplayInfo(ids.front().value, &info);
37         LOG_ALWAYS_FATAL_IF(status, "%s: Failed to get display info", __FUNCTION__);
38 #endif
39         return info;
40     }();
41 
42     return info;
43 }
44 
getActiveDisplayMode()45 const ui::DisplayMode& getActiveDisplayMode() {
46     static ui::DisplayMode config = [] {
47         ui::DisplayMode config;
48 #if HWUI_NULL_GPU
49         config.resolution = ui::Size(1080, 1920);
50         config.xDpi = config.yDpi = 320.f;
51         config.refreshRate = 60.f;
52 #else
53         const std::vector<PhysicalDisplayId> ids = SurfaceComposerClient::getPhysicalDisplayIds();
54         LOG_ALWAYS_FATAL_IF(ids.empty(), "%s: No displays", __FUNCTION__);
55 
56         const sp<IBinder> token = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
57         LOG_ALWAYS_FATAL_IF(!token, "%s: No internal display", __FUNCTION__);
58 
59         const status_t status = SurfaceComposerClient::getActiveDisplayMode(token, &config);
60         LOG_ALWAYS_FATAL_IF(status, "%s: Failed to get active display config", __FUNCTION__);
61 #endif
62         return config;
63     }();
64 
65     return config;
66 }
67 
TestContext()68 TestContext::TestContext() {
69     mLooper = new Looper(true);
70     mSurfaceComposerClient = new SurfaceComposerClient();
71 
72     constexpr int EVENT_ID = 1;
73     mLooper->addFd(mDisplayEventReceiver.getFd(), EVENT_ID, Looper::EVENT_INPUT, nullptr, nullptr);
74 }
75 
~TestContext()76 TestContext::~TestContext() {}
77 
surface()78 sp<Surface> TestContext::surface() {
79     if (!mSurface.get()) {
80         createSurface();
81     }
82     return mSurface;
83 }
84 
createSurface()85 void TestContext::createSurface() {
86     if (mRenderOffscreen) {
87         createOffscreenSurface();
88     } else {
89         createWindowSurface();
90     }
91 }
92 
createWindowSurface()93 void TestContext::createWindowSurface() {
94     const ui::Size& resolution = getActiveDisplayResolution();
95     mSurfaceControl =
96             mSurfaceComposerClient->createSurface(String8("HwuiTest"), resolution.getWidth(),
97                                                   resolution.getHeight(), PIXEL_FORMAT_RGBX_8888);
98 
99     SurfaceComposerClient::Transaction t;
100     t.setLayer(mSurfaceControl, 0x7FFFFFF).show(mSurfaceControl).apply();
101     mSurface = mSurfaceControl->getSurface();
102 }
103 
createOffscreenSurface()104 void TestContext::createOffscreenSurface() {
105 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
106     mConsumer = new BufferItemConsumer(GRALLOC_USAGE_HW_COMPOSER, 4);
107     const ui::Size& resolution = getActiveDisplayResolution();
108     mConsumer->setDefaultBufferSize(resolution.getWidth(), resolution.getHeight());
109     mSurface = mConsumer->getSurface();
110     mSurface->setMaxDequeuedBufferCount(3);
111     mSurface->setAsyncMode(true);
112 #else
113     sp<IGraphicBufferProducer> producer;
114     sp<IGraphicBufferConsumer> consumer;
115     BufferQueue::createBufferQueue(&producer, &consumer);
116     producer->setMaxDequeuedBufferCount(3);
117     producer->setAsyncMode(true);
118     mConsumer = new BufferItemConsumer(consumer, GRALLOC_USAGE_HW_COMPOSER, 4);
119     const ui::Size& resolution = getActiveDisplayResolution();
120     mConsumer->setDefaultBufferSize(resolution.getWidth(), resolution.getHeight());
121     mSurface = new Surface(producer);
122 #endif  // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
123 }
124 
waitForVsync()125 void TestContext::waitForVsync() {
126     // Hacky fix for not getting sysprop change callbacks
127     // We just poll the sysprop in vsync since it's when the UI thread is
128     // "idle" and shouldn't burn too much time
129     atrace_update_tags();
130 
131     if (mConsumer.get()) {
132         BufferItem buffer;
133         if (mConsumer->acquireBuffer(&buffer, 0, false) == OK) {
134             // We assume the producer is internally ordered enough such that
135             // it is unneccessary to set a release fence
136             mConsumer->releaseBuffer(buffer);
137         }
138         // We running free, go go go!
139         return;
140     }
141 #if !HWUI_NULL_GPU
142     // Request vsync
143     mDisplayEventReceiver.requestNextVsync();
144 
145     // Wait
146     mLooper->pollOnce(-1);
147 
148     // Drain it
149     DisplayEventReceiver::Event buf[100];
150     while (mDisplayEventReceiver.getEvents(buf, 100) > 0) {
151     }
152 #endif
153 }
154 
155 }  // namespace test
156 }  // namespace uirenderer
157 }  // namespace android