John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 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 | |
Chris Craik | 27e58b4 | 2015-12-07 10:01:38 -0800 | [diff] [blame] | 17 | #include "tests/common/TestContext.h" |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 18 | |
John Reck | e10971d | 2017-02-07 15:31:03 -0500 | [diff] [blame] | 19 | #include <cutils/trace.h> |
| 20 | |
John Reck | 84e390c | 2015-01-05 09:42:52 -0800 | [diff] [blame] | 21 | namespace android { |
| 22 | namespace uirenderer { |
| 23 | namespace test { |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 24 | |
Dominik Laskowski | 69b281d | 2019-11-22 14:13:12 -0800 | [diff] [blame] | 25 | const DisplayInfo& getDisplayInfo() { |
| 26 | static DisplayInfo info = [] { |
| 27 | DisplayInfo info; |
| 28 | #if HWUI_NULL_GPU |
| 29 | info.density = 2.f; |
John Reck | e702c9c | 2015-10-07 10:26:02 -0700 | [diff] [blame] | 30 | #else |
Dominik Laskowski | 69b281d | 2019-11-22 14:13:12 -0800 | [diff] [blame] | 31 | 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 Reck | e702c9c | 2015-10-07 10:26:02 -0700 | [diff] [blame] | 36 | #endif |
Dominik Laskowski | 69b281d | 2019-11-22 14:13:12 -0800 | [diff] [blame] | 37 | return info; |
| 38 | }(); |
| 39 | |
| 40 | return info; |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Marin Shalamanov | cb783ca | 2021-01-29 21:26:07 +0100 | [diff] [blame] | 43 | const ui::DisplayMode& getActiveDisplayMode() { |
| 44 | static ui::DisplayMode config = [] { |
| 45 | ui::DisplayMode config; |
Dominik Laskowski | 69b281d | 2019-11-22 14:13:12 -0800 | [diff] [blame] | 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 | |
Marin Shalamanov | cb783ca | 2021-01-29 21:26:07 +0100 | [diff] [blame] | 54 | const status_t status = SurfaceComposerClient::getActiveDisplayMode(token, &config); |
Dominik Laskowski | 69b281d | 2019-11-22 14:13:12 -0800 | [diff] [blame] | 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 Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 62 | |
John Reck | 84e390c | 2015-01-05 09:42:52 -0800 | [diff] [blame] | 63 | TestContext::TestContext() { |
| 64 | mLooper = new Looper(true); |
| 65 | mSurfaceComposerClient = new SurfaceComposerClient(); |
Dominik Laskowski | 69b281d | 2019-11-22 14:13:12 -0800 | [diff] [blame] | 66 | |
| 67 | constexpr int EVENT_ID = 1; |
| 68 | mLooper->addFd(mDisplayEventReceiver.getFd(), EVENT_ID, Looper::EVENT_INPUT, nullptr, nullptr); |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 69 | } |
John Reck | 84e390c | 2015-01-05 09:42:52 -0800 | [diff] [blame] | 70 | |
| 71 | TestContext::~TestContext() {} |
| 72 | |
| 73 | sp<Surface> TestContext::surface() { |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 74 | if (!mSurface.get()) { |
| 75 | createSurface(); |
John Reck | 84e390c | 2015-01-05 09:42:52 -0800 | [diff] [blame] | 76 | } |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 77 | return mSurface; |
| 78 | } |
John Reck | 84e390c | 2015-01-05 09:42:52 -0800 | [diff] [blame] | 79 | |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 80 | void TestContext::createSurface() { |
| 81 | if (mRenderOffscreen) { |
| 82 | createOffscreenSurface(); |
| 83 | } else { |
| 84 | createWindowSurface(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void TestContext::createWindowSurface() { |
Dominik Laskowski | 69b281d | 2019-11-22 14:13:12 -0800 | [diff] [blame] | 89 | const ui::Size& resolution = getActiveDisplayResolution(); |
| 90 | mSurfaceControl = |
| 91 | mSurfaceComposerClient->createSurface(String8("HwuiTest"), resolution.getWidth(), |
| 92 | resolution.getHeight(), PIXEL_FORMAT_RGBX_8888); |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 93 | |
Robert Carr | e13b58e | 2017-08-31 14:50:44 -0700 | [diff] [blame] | 94 | SurfaceComposerClient::Transaction t; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 95 | t.setLayer(mSurfaceControl, 0x7FFFFFF).show(mSurfaceControl).apply(); |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 96 | mSurface = mSurfaceControl->getSurface(); |
| 97 | } |
| 98 | |
| 99 | void 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 Laskowski | 69b281d | 2019-11-22 14:13:12 -0800 | [diff] [blame] | 106 | const ui::Size& resolution = getActiveDisplayResolution(); |
| 107 | mConsumer->setDefaultBufferSize(resolution.getWidth(), resolution.getHeight()); |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 108 | mSurface = new Surface(producer); |
John Reck | 84e390c | 2015-01-05 09:42:52 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void TestContext::waitForVsync() { |
John Reck | e10971d | 2017-02-07 15:31:03 -0500 | [diff] [blame] | 112 | // 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 Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 117 | 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 Reck | c36df95 | 2015-07-29 10:09:36 -0700 | [diff] [blame] | 127 | #if !HWUI_NULL_GPU |
John Reck | 84e390c | 2015-01-05 09:42:52 -0800 | [diff] [blame] | 128 | // Request vsync |
| 129 | mDisplayEventReceiver.requestNextVsync(); |
| 130 | |
| 131 | // Wait |
| 132 | mLooper->pollOnce(-1); |
| 133 | |
| 134 | // Drain it |
| 135 | DisplayEventReceiver::Event buf[100]; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 136 | while (mDisplayEventReceiver.getEvents(buf, 100) > 0) { |
| 137 | } |
John Reck | c36df95 | 2015-07-29 10:09:36 -0700 | [diff] [blame] | 138 | #endif |
John Reck | 84e390c | 2015-01-05 09:42:52 -0800 | [diff] [blame] | 139 | } |
| 140 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 141 | } // namespace test |
| 142 | } // namespace uirenderer |
| 143 | } // namespace android |