blob: 76b85d60022773228ad39abf9a540041c4f8c0b6 [file] [log] [blame]
Alec Mouri671d0f52019-09-05 13:59:19 -07001/*
2 * Copyright 2019 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 <apex/display.h>
18#include <gui/SurfaceComposerClient.h>
Marin Shalamanova7fe3042021-01-29 21:02:08 +010019#include <ui/DisplayMode.h>
Marin Shalamanov228f46b2021-01-28 21:11:45 +010020#include <ui/DynamicDisplayInfo.h>
Alec Mouri671d0f52019-09-05 13:59:19 -070021#include <ui/GraphicTypes.h>
Alec Mouri46170512019-11-20 11:04:55 -080022#include <ui/PixelFormat.h>
Marin Shalamanov228f46b2021-01-28 21:11:45 +010023#include <ui/StaticDisplayInfo.h>
Alec Mouri671d0f52019-09-05 13:59:19 -070024
25#include <algorithm>
26#include <optional>
27#include <type_traits>
28#include <vector>
29
30namespace android::display::impl {
31
32/**
33 * Implementation of ADisplayConfig
34 */
35struct DisplayConfigImpl {
36 /**
Marin Shalamanov228f46b2021-01-28 21:11:45 +010037 * The ID of the display configuration.
38 */
39 size_t id;
40
41 /**
Alec Mouri671d0f52019-09-05 13:59:19 -070042 * The width in pixels of the display configuration.
43 */
44 int32_t width{0};
45
46 /**
47 * The height in pixels of the display configuration.
48 */
49
50 int32_t height{0};
51
52 /**
Alec Mouri671d0f52019-09-05 13:59:19 -070053 * The refresh rate of the display configuration, in frames per second.
54 */
55 float fps{0.0};
56
57 /**
58 * The vsync offset at which surfaceflinger runs, in nanoseconds.
59 */
60 int64_t sfOffset{0};
61
62 /**
63 * The vsync offset at which applications run, in nanoseconds.
64 */
65 int64_t appOffset{0};
66};
67
68// DisplayConfigImpl allocation is not managed through C++ memory apis, so
69// preventing calling the destructor here.
70static_assert(std::is_trivially_destructible<DisplayConfigImpl>::value);
71
72/**
73 * Implementation of ADisplay
74 */
75struct DisplayImpl {
76 /**
77 * A physical display ID, unique to this display.
78 */
79 PhysicalDisplayId id;
80
81 /**
82 * The type of the display, i.e. whether it is an internal or external
83 * display.
84 */
85 ADisplayType type;
86
87 /**
Alec Mouri46170512019-11-20 11:04:55 -080088 * The preferred WCG dataspace
89 */
90 ADataSpace wcgDataspace;
91
92 /**
93 * The preferred WCG pixel format
94 */
95 AHardwareBuffer_Format wcgPixelFormat;
96
97 /**
Alec Mouri671d0f52019-09-05 13:59:19 -070098 * Number of supported configs
99 */
100 size_t numConfigs;
101
102 /**
103 * Set of supported configs by this display.
104 */
105 DisplayConfigImpl* configs;
106};
107
108// DisplayImpl allocation is not managed through C++ memory apis, so
109// preventing calling the destructor here.
110static_assert(std::is_trivially_destructible<DisplayImpl>::value);
111
112} // namespace android::display::impl
113
114using namespace android;
115using namespace android::display::impl;
116
117#define CHECK_NOT_NULL(name) \
118 LOG_ALWAYS_FATAL_IF(name == nullptr, "nullptr passed as " #name " argument");
119
120namespace {
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800121
Alec Mouri671d0f52019-09-05 13:59:19 -0700122sp<IBinder> getToken(ADisplay* display) {
123 DisplayImpl* impl = reinterpret_cast<DisplayImpl*>(display);
124 return SurfaceComposerClient::getPhysicalDisplayToken(impl->id);
125}
126
Alec Mouri671d0f52019-09-05 13:59:19 -0700127} // namespace
128
Alec Mourid9ff3272019-11-19 16:23:59 -0800129namespace android {
130
Alec Mouri671d0f52019-09-05 13:59:19 -0700131int ADisplay_acquirePhysicalDisplays(ADisplay*** outDisplays) {
132 const std::vector<PhysicalDisplayId> ids = SurfaceComposerClient::getPhysicalDisplayIds();
133 const size_t size = ids.size();
134 if (size == 0) {
135 return NO_INIT;
136 }
137
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100138 std::vector<DisplayConfigImpl> modesPerDisplay[size];
139 int numModes = 0;
Alec Mouri671d0f52019-09-05 13:59:19 -0700140 for (int i = 0; i < size; ++i) {
141 const sp<IBinder> token = SurfaceComposerClient::getPhysicalDisplayToken(ids[i]);
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800142
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100143 ui::StaticDisplayInfo staticInfo;
144 if (const status_t status = SurfaceComposerClient::getStaticDisplayInfo(token, &staticInfo);
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800145 status != OK) {
146 return status;
147 }
148
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100149 ui::DynamicDisplayInfo dynamicInfo;
150 if (const status_t status =
151 SurfaceComposerClient::getDynamicDisplayInfo(token, &dynamicInfo);
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -0800152 status != OK) {
Alec Mouri671d0f52019-09-05 13:59:19 -0700153 return status;
154 }
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100155 const auto& modes = dynamicInfo.supportedDisplayModes;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100156 if (modes.empty()) {
Alec Mouri671d0f52019-09-05 13:59:19 -0700157 return NO_INIT;
158 }
159
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100160 numModes += modes.size();
161 modesPerDisplay[i].reserve(modes.size());
162 for (int j = 0; j < modes.size(); ++j) {
163 const ui::DisplayMode& mode = modes[j];
164 modesPerDisplay[i].emplace_back(
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100165 DisplayConfigImpl{static_cast<size_t>(mode.id), mode.resolution.getWidth(),
Brian Lindahl9b416b02022-01-12 10:57:39 +0100166 mode.resolution.getHeight(), mode.refreshRate,
167 mode.sfVsyncOffset, mode.appVsyncOffset});
Alec Mouri671d0f52019-09-05 13:59:19 -0700168 }
169 }
170
171 const std::optional<PhysicalDisplayId> internalId =
172 SurfaceComposerClient::getInternalDisplayId();
Alec Mouri46170512019-11-20 11:04:55 -0800173 ui::Dataspace defaultDataspace;
174 ui::PixelFormat defaultPixelFormat;
175 ui::Dataspace wcgDataspace;
176 ui::PixelFormat wcgPixelFormat;
177
178 const status_t status =
179 SurfaceComposerClient::getCompositionPreference(&defaultDataspace, &defaultPixelFormat,
180 &wcgDataspace, &wcgPixelFormat);
181 if (status != NO_ERROR) {
182 return status;
183 }
Alec Mouri671d0f52019-09-05 13:59:19 -0700184
185 // Here we allocate all our required memory in one block. The layout is as
186 // follows:
187 // ------------------------------------------------------------
188 // | DisplayImpl pointers | DisplayImpls | DisplayConfigImpls |
189 // ------------------------------------------------------------
190 //
191 // The caller will be given a DisplayImpl** which points to the beginning of
192 // the block of DisplayImpl pointers.
193 // Each DisplayImpl* points to a DisplayImpl in the second block.
194 // Each DisplayImpl contains a DisplayConfigImpl*, which points to a
195 // contiguous block of DisplayConfigImpls specific to that display.
196 DisplayImpl** const impls = reinterpret_cast<DisplayImpl**>(
197 malloc((sizeof(DisplayImpl) + sizeof(DisplayImpl*)) * size +
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100198 sizeof(DisplayConfigImpl) * numModes));
Alec Mouri671d0f52019-09-05 13:59:19 -0700199 DisplayImpl* const displayData = reinterpret_cast<DisplayImpl*>(impls + size);
200 DisplayConfigImpl* configData = reinterpret_cast<DisplayConfigImpl*>(displayData + size);
201
202 for (size_t i = 0; i < size; ++i) {
203 const PhysicalDisplayId id = ids[i];
204 const ADisplayType type = (internalId == id) ? ADisplayType::DISPLAY_TYPE_INTERNAL
205 : ADisplayType::DISPLAY_TYPE_EXTERNAL;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100206 const std::vector<DisplayConfigImpl>& configs = modesPerDisplay[i];
Alec Mouri671d0f52019-09-05 13:59:19 -0700207 memcpy(configData, configs.data(), sizeof(DisplayConfigImpl) * configs.size());
208
Alec Mouri46170512019-11-20 11:04:55 -0800209 displayData[i] = DisplayImpl{id,
210 type,
211 static_cast<ADataSpace>(wcgDataspace),
212 static_cast<AHardwareBuffer_Format>(wcgPixelFormat),
213 configs.size(),
214 configData};
Alec Mouri671d0f52019-09-05 13:59:19 -0700215 impls[i] = displayData + i;
216 // Advance the configData pointer so that future configs are written to
217 // the correct display.
218 configData += configs.size();
219 }
220
221 *outDisplays = reinterpret_cast<ADisplay**>(impls);
222 return size;
223}
224
225void ADisplay_release(ADisplay** displays) {
226 if (displays == nullptr) {
227 return;
228 }
229 free(displays);
230}
231
232float ADisplay_getMaxSupportedFps(ADisplay* display) {
233 CHECK_NOT_NULL(display);
234 DisplayImpl* impl = reinterpret_cast<DisplayImpl*>(display);
235 float maxFps = 0.0;
236 for (int i = 0; i < impl->numConfigs; ++i) {
237 maxFps = std::max(maxFps, impl->configs[i].fps);
238 }
239 return maxFps;
240}
241
242ADisplayType ADisplay_getDisplayType(ADisplay* display) {
243 CHECK_NOT_NULL(display);
244
245 return reinterpret_cast<DisplayImpl*>(display)->type;
246}
247
Alec Mouri46170512019-11-20 11:04:55 -0800248void ADisplay_getPreferredWideColorFormat(ADisplay* display, ADataSpace* outDataspace,
249 AHardwareBuffer_Format* outPixelFormat) {
250 CHECK_NOT_NULL(display);
251 CHECK_NOT_NULL(outDataspace);
252 CHECK_NOT_NULL(outPixelFormat);
253
254 DisplayImpl* impl = reinterpret_cast<DisplayImpl*>(display);
255 *outDataspace = impl->wcgDataspace;
256 *outPixelFormat = impl->wcgPixelFormat;
257}
258
Alec Mouri671d0f52019-09-05 13:59:19 -0700259int ADisplay_getCurrentConfig(ADisplay* display, ADisplayConfig** outConfig) {
260 CHECK_NOT_NULL(display);
261
262 sp<IBinder> token = getToken(display);
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100263 ui::DynamicDisplayInfo info;
264 if (const auto status = SurfaceComposerClient::getDynamicDisplayInfo(token, &info);
265 status != OK) {
266 return status;
Alec Mouri671d0f52019-09-05 13:59:19 -0700267 }
268
269 DisplayImpl* impl = reinterpret_cast<DisplayImpl*>(display);
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100270 for (size_t i = 0; i < impl->numConfigs; i++) {
271 auto* config = impl->configs + i;
272 if (config->id == info.activeDisplayModeId) {
273 *outConfig = reinterpret_cast<ADisplayConfig*>(config);
274 return OK;
275 }
276 }
Alec Mouri671d0f52019-09-05 13:59:19 -0700277
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100278 return NAME_NOT_FOUND;
Alec Mouri671d0f52019-09-05 13:59:19 -0700279}
280
Alec Mouri671d0f52019-09-05 13:59:19 -0700281int32_t ADisplayConfig_getWidth(ADisplayConfig* config) {
282 CHECK_NOT_NULL(config);
283
284 return reinterpret_cast<DisplayConfigImpl*>(config)->width;
285}
286
287int32_t ADisplayConfig_getHeight(ADisplayConfig* config) {
288 CHECK_NOT_NULL(config);
289
290 return reinterpret_cast<DisplayConfigImpl*>(config)->height;
291}
292
293float ADisplayConfig_getFps(ADisplayConfig* config) {
294 CHECK_NOT_NULL(config);
295
296 return reinterpret_cast<DisplayConfigImpl*>(config)->fps;
297}
298
299int64_t ADisplayConfig_getCompositorOffsetNanos(ADisplayConfig* config) {
300 CHECK_NOT_NULL(config);
301
302 return reinterpret_cast<DisplayConfigImpl*>(config)->sfOffset;
303}
304
305int64_t ADisplayConfig_getAppVsyncOffsetNanos(ADisplayConfig* config) {
306 CHECK_NOT_NULL(config);
307
308 return reinterpret_cast<DisplayConfigImpl*>(config)->appOffset;
309}
Alec Mourid9ff3272019-11-19 16:23:59 -0800310
311} // namespace android