blob: 06f158f25fc50a85654cad1536469e3a6f82fcac [file] [log] [blame]
John Reck94c40fe2014-10-08 09:28:43 -07001/*
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
Chris Craik27e58b42015-12-07 10:01:38 -080017#include "tests/common/TestContext.h"
John Reck94c40fe2014-10-08 09:28:43 -070018
John Recke10971d2017-02-07 15:31:03 -050019#include <cutils/trace.h>
20
John Reck84e390c2015-01-05 09:42:52 -080021namespace android {
22namespace uirenderer {
23namespace test {
John Reck94c40fe2014-10-08 09:28:43 -070024
Dominik Laskowski69b281d2019-11-22 14:13:12 -080025const DisplayInfo& getDisplayInfo() {
26 static DisplayInfo info = [] {
27 DisplayInfo info;
28#if HWUI_NULL_GPU
29 info.density = 2.f;
John Recke702c9c2015-10-07 10:26:02 -070030#else
Dominik Laskowski69b281d2019-11-22 14:13:12 -080031 const sp<IBinder> token = SurfaceComposerClient::getInternalDisplayToken();
32 LOG_ALWAYS_FATAL_IF(!token, "%s: No internal display", __FUNCTION__);
33
34 const status_t status = SurfaceComposerClient::getDisplayInfo(token, &info);
35 LOG_ALWAYS_FATAL_IF(status, "%s: Failed to get display info", __FUNCTION__);
John Recke702c9c2015-10-07 10:26:02 -070036#endif
Dominik Laskowski69b281d2019-11-22 14:13:12 -080037 return info;
38 }();
39
40 return info;
John Reck94c40fe2014-10-08 09:28:43 -070041}
42
Dominik Laskowski69b281d2019-11-22 14:13:12 -080043const DisplayConfig& getActiveDisplayConfig() {
44 static DisplayConfig config = [] {
45 DisplayConfig config;
46#if HWUI_NULL_GPU
47 config.resolution = ui::Size(1080, 1920);
48 config.xDpi = config.yDpi = 320.f;
49 config.refreshRate = 60.f;
50#else
51 const sp<IBinder> token = SurfaceComposerClient::getInternalDisplayToken();
52 LOG_ALWAYS_FATAL_IF(!token, "%s: No internal display", __FUNCTION__);
53
54 const status_t status = SurfaceComposerClient::getActiveDisplayConfig(token, &config);
55 LOG_ALWAYS_FATAL_IF(status, "%s: Failed to get active display config", __FUNCTION__);
56#endif
57 return config;
58 }();
59
60 return config;
61}
John Reck94c40fe2014-10-08 09:28:43 -070062
John Reck84e390c2015-01-05 09:42:52 -080063TestContext::TestContext() {
64 mLooper = new Looper(true);
65 mSurfaceComposerClient = new SurfaceComposerClient();
Dominik Laskowski69b281d2019-11-22 14:13:12 -080066
67 constexpr int EVENT_ID = 1;
68 mLooper->addFd(mDisplayEventReceiver.getFd(), EVENT_ID, Looper::EVENT_INPUT, nullptr, nullptr);
John Reck94c40fe2014-10-08 09:28:43 -070069}
John Reck84e390c2015-01-05 09:42:52 -080070
71TestContext::~TestContext() {}
72
73sp<Surface> TestContext::surface() {
John Reckf1480762016-07-03 18:28:25 -070074 if (!mSurface.get()) {
75 createSurface();
John Reck84e390c2015-01-05 09:42:52 -080076 }
John Reckf1480762016-07-03 18:28:25 -070077 return mSurface;
78}
John Reck84e390c2015-01-05 09:42:52 -080079
John Reckf1480762016-07-03 18:28:25 -070080void TestContext::createSurface() {
81 if (mRenderOffscreen) {
82 createOffscreenSurface();
83 } else {
84 createWindowSurface();
85 }
86}
87
88void TestContext::createWindowSurface() {
Dominik Laskowski69b281d2019-11-22 14:13:12 -080089 const ui::Size& resolution = getActiveDisplayResolution();
90 mSurfaceControl =
91 mSurfaceComposerClient->createSurface(String8("HwuiTest"), resolution.getWidth(),
92 resolution.getHeight(), PIXEL_FORMAT_RGBX_8888);
John Reckf1480762016-07-03 18:28:25 -070093
Robert Carre13b58e2017-08-31 14:50:44 -070094 SurfaceComposerClient::Transaction t;
John Reck1bcacfd2017-11-03 10:12:19 -070095 t.setLayer(mSurfaceControl, 0x7FFFFFF).show(mSurfaceControl).apply();
John Reckf1480762016-07-03 18:28:25 -070096 mSurface = mSurfaceControl->getSurface();
97}
98
99void TestContext::createOffscreenSurface() {
100 sp<IGraphicBufferProducer> producer;
101 sp<IGraphicBufferConsumer> consumer;
102 BufferQueue::createBufferQueue(&producer, &consumer);
103 producer->setMaxDequeuedBufferCount(3);
104 producer->setAsyncMode(true);
105 mConsumer = new BufferItemConsumer(consumer, GRALLOC_USAGE_HW_COMPOSER, 4);
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800106 const ui::Size& resolution = getActiveDisplayResolution();
107 mConsumer->setDefaultBufferSize(resolution.getWidth(), resolution.getHeight());
John Reckf1480762016-07-03 18:28:25 -0700108 mSurface = new Surface(producer);
John Reck84e390c2015-01-05 09:42:52 -0800109}
110
111void TestContext::waitForVsync() {
John Recke10971d2017-02-07 15:31:03 -0500112 // Hacky fix for not getting sysprop change callbacks
113 // We just poll the sysprop in vsync since it's when the UI thread is
114 // "idle" and shouldn't burn too much time
115 atrace_update_tags();
116
John Reckf1480762016-07-03 18:28:25 -0700117 if (mConsumer.get()) {
118 BufferItem buffer;
119 if (mConsumer->acquireBuffer(&buffer, 0, false) == OK) {
120 // We assume the producer is internally ordered enough such that
121 // it is unneccessary to set a release fence
122 mConsumer->releaseBuffer(buffer);
123 }
124 // We running free, go go go!
125 return;
126 }
John Reckc36df952015-07-29 10:09:36 -0700127#if !HWUI_NULL_GPU
John Reck84e390c2015-01-05 09:42:52 -0800128 // Request vsync
129 mDisplayEventReceiver.requestNextVsync();
130
131 // Wait
132 mLooper->pollOnce(-1);
133
134 // Drain it
135 DisplayEventReceiver::Event buf[100];
John Reck1bcacfd2017-11-03 10:12:19 -0700136 while (mDisplayEventReceiver.getEvents(buf, 100) > 0) {
137 }
John Reckc36df952015-07-29 10:09:36 -0700138#endif
John Reck84e390c2015-01-05 09:42:52 -0800139}
140
John Reck1bcacfd2017-11-03 10:12:19 -0700141} // namespace test
142} // namespace uirenderer
143} // namespace android