Alec Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 1 | /* |
| 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 Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 19 | #include <ui/DisplayMode.h> |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 20 | #include <ui/DynamicDisplayInfo.h> |
Alec Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 21 | #include <ui/GraphicTypes.h> |
Alec Mouri | 4617051 | 2019-11-20 11:04:55 -0800 | [diff] [blame] | 22 | #include <ui/PixelFormat.h> |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 23 | #include <ui/StaticDisplayInfo.h> |
Alec Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 24 | |
| 25 | #include <algorithm> |
| 26 | #include <optional> |
| 27 | #include <type_traits> |
| 28 | #include <vector> |
| 29 | |
| 30 | namespace android::display::impl { |
| 31 | |
| 32 | /** |
| 33 | * Implementation of ADisplayConfig |
| 34 | */ |
| 35 | struct DisplayConfigImpl { |
| 36 | /** |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 37 | * The ID of the display configuration. |
| 38 | */ |
| 39 | size_t id; |
| 40 | |
| 41 | /** |
Alec Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 42 | * 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 Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 53 | * 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. |
| 70 | static_assert(std::is_trivially_destructible<DisplayConfigImpl>::value); |
| 71 | |
| 72 | /** |
| 73 | * Implementation of ADisplay |
| 74 | */ |
| 75 | struct 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 Mouri | 4617051 | 2019-11-20 11:04:55 -0800 | [diff] [blame] | 88 | * The preferred WCG dataspace |
| 89 | */ |
| 90 | ADataSpace wcgDataspace; |
| 91 | |
| 92 | /** |
| 93 | * The preferred WCG pixel format |
| 94 | */ |
| 95 | AHardwareBuffer_Format wcgPixelFormat; |
| 96 | |
| 97 | /** |
Alec Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 98 | * 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. |
| 110 | static_assert(std::is_trivially_destructible<DisplayImpl>::value); |
| 111 | |
| 112 | } // namespace android::display::impl |
| 113 | |
| 114 | using namespace android; |
| 115 | using namespace android::display::impl; |
| 116 | |
| 117 | #define CHECK_NOT_NULL(name) \ |
| 118 | LOG_ALWAYS_FATAL_IF(name == nullptr, "nullptr passed as " #name " argument"); |
| 119 | |
| 120 | namespace { |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 121 | |
Alec Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 122 | sp<IBinder> getToken(ADisplay* display) { |
| 123 | DisplayImpl* impl = reinterpret_cast<DisplayImpl*>(display); |
| 124 | return SurfaceComposerClient::getPhysicalDisplayToken(impl->id); |
| 125 | } |
| 126 | |
Alec Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 127 | } // namespace |
| 128 | |
Alec Mouri | d9ff327 | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 129 | namespace android { |
| 130 | |
Alec Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 131 | int 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 Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 138 | std::vector<DisplayConfigImpl> modesPerDisplay[size]; |
Huihong Luo | 31b5ac2 | 2022-08-15 20:38:10 -0700 | [diff] [blame^] | 139 | ui::DisplayConnectionType displayConnectionTypes[size]; |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 140 | int numModes = 0; |
Alec Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 141 | for (int i = 0; i < size; ++i) { |
| 142 | const sp<IBinder> token = SurfaceComposerClient::getPhysicalDisplayToken(ids[i]); |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 143 | |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 144 | ui::StaticDisplayInfo staticInfo; |
| 145 | if (const status_t status = SurfaceComposerClient::getStaticDisplayInfo(token, &staticInfo); |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 146 | status != OK) { |
| 147 | return status; |
| 148 | } |
Huihong Luo | 31b5ac2 | 2022-08-15 20:38:10 -0700 | [diff] [blame^] | 149 | displayConnectionTypes[i] = staticInfo.connectionType; |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 150 | |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 151 | ui::DynamicDisplayInfo dynamicInfo; |
| 152 | if (const status_t status = |
| 153 | SurfaceComposerClient::getDynamicDisplayInfo(token, &dynamicInfo); |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 154 | status != OK) { |
Alec Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 155 | return status; |
| 156 | } |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 157 | const auto& modes = dynamicInfo.supportedDisplayModes; |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 158 | if (modes.empty()) { |
Alec Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 159 | return NO_INIT; |
| 160 | } |
| 161 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 162 | numModes += modes.size(); |
| 163 | modesPerDisplay[i].reserve(modes.size()); |
| 164 | for (int j = 0; j < modes.size(); ++j) { |
| 165 | const ui::DisplayMode& mode = modes[j]; |
| 166 | modesPerDisplay[i].emplace_back( |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 167 | DisplayConfigImpl{static_cast<size_t>(mode.id), mode.resolution.getWidth(), |
Brian Lindahl | 9b416b0 | 2022-01-12 10:57:39 +0100 | [diff] [blame] | 168 | mode.resolution.getHeight(), mode.refreshRate, |
| 169 | mode.sfVsyncOffset, mode.appVsyncOffset}); |
Alec Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
Alec Mouri | 4617051 | 2019-11-20 11:04:55 -0800 | [diff] [blame] | 173 | 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 Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 184 | |
| 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 Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 198 | sizeof(DisplayConfigImpl) * numModes)); |
Alec Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 199 | 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]; |
Huihong Luo | 31b5ac2 | 2022-08-15 20:38:10 -0700 | [diff] [blame^] | 204 | const ADisplayType type = (displayConnectionTypes[i] == ui::DisplayConnectionType::Internal) |
| 205 | ? ADisplayType::DISPLAY_TYPE_INTERNAL |
| 206 | : ADisplayType::DISPLAY_TYPE_EXTERNAL; |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 207 | const std::vector<DisplayConfigImpl>& configs = modesPerDisplay[i]; |
Alec Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 208 | memcpy(configData, configs.data(), sizeof(DisplayConfigImpl) * configs.size()); |
| 209 | |
Alec Mouri | 4617051 | 2019-11-20 11:04:55 -0800 | [diff] [blame] | 210 | displayData[i] = DisplayImpl{id, |
| 211 | type, |
| 212 | static_cast<ADataSpace>(wcgDataspace), |
| 213 | static_cast<AHardwareBuffer_Format>(wcgPixelFormat), |
| 214 | configs.size(), |
| 215 | configData}; |
Alec Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 216 | impls[i] = displayData + i; |
| 217 | // Advance the configData pointer so that future configs are written to |
| 218 | // the correct display. |
| 219 | configData += configs.size(); |
| 220 | } |
| 221 | |
| 222 | *outDisplays = reinterpret_cast<ADisplay**>(impls); |
| 223 | return size; |
| 224 | } |
| 225 | |
| 226 | void ADisplay_release(ADisplay** displays) { |
| 227 | if (displays == nullptr) { |
| 228 | return; |
| 229 | } |
| 230 | free(displays); |
| 231 | } |
| 232 | |
| 233 | float ADisplay_getMaxSupportedFps(ADisplay* display) { |
| 234 | CHECK_NOT_NULL(display); |
| 235 | DisplayImpl* impl = reinterpret_cast<DisplayImpl*>(display); |
| 236 | float maxFps = 0.0; |
| 237 | for (int i = 0; i < impl->numConfigs; ++i) { |
| 238 | maxFps = std::max(maxFps, impl->configs[i].fps); |
| 239 | } |
| 240 | return maxFps; |
| 241 | } |
| 242 | |
| 243 | ADisplayType ADisplay_getDisplayType(ADisplay* display) { |
| 244 | CHECK_NOT_NULL(display); |
| 245 | |
| 246 | return reinterpret_cast<DisplayImpl*>(display)->type; |
| 247 | } |
| 248 | |
Alec Mouri | 4617051 | 2019-11-20 11:04:55 -0800 | [diff] [blame] | 249 | void ADisplay_getPreferredWideColorFormat(ADisplay* display, ADataSpace* outDataspace, |
| 250 | AHardwareBuffer_Format* outPixelFormat) { |
| 251 | CHECK_NOT_NULL(display); |
| 252 | CHECK_NOT_NULL(outDataspace); |
| 253 | CHECK_NOT_NULL(outPixelFormat); |
| 254 | |
| 255 | DisplayImpl* impl = reinterpret_cast<DisplayImpl*>(display); |
| 256 | *outDataspace = impl->wcgDataspace; |
| 257 | *outPixelFormat = impl->wcgPixelFormat; |
| 258 | } |
| 259 | |
Alec Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 260 | int ADisplay_getCurrentConfig(ADisplay* display, ADisplayConfig** outConfig) { |
| 261 | CHECK_NOT_NULL(display); |
| 262 | |
| 263 | sp<IBinder> token = getToken(display); |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 264 | ui::DynamicDisplayInfo info; |
| 265 | if (const auto status = SurfaceComposerClient::getDynamicDisplayInfo(token, &info); |
| 266 | status != OK) { |
| 267 | return status; |
Alec Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | DisplayImpl* impl = reinterpret_cast<DisplayImpl*>(display); |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 271 | for (size_t i = 0; i < impl->numConfigs; i++) { |
| 272 | auto* config = impl->configs + i; |
| 273 | if (config->id == info.activeDisplayModeId) { |
| 274 | *outConfig = reinterpret_cast<ADisplayConfig*>(config); |
| 275 | return OK; |
| 276 | } |
| 277 | } |
Alec Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 278 | |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 279 | return NAME_NOT_FOUND; |
Alec Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Alec Mouri | 671d0f5 | 2019-09-05 13:59:19 -0700 | [diff] [blame] | 282 | int32_t ADisplayConfig_getWidth(ADisplayConfig* config) { |
| 283 | CHECK_NOT_NULL(config); |
| 284 | |
| 285 | return reinterpret_cast<DisplayConfigImpl*>(config)->width; |
| 286 | } |
| 287 | |
| 288 | int32_t ADisplayConfig_getHeight(ADisplayConfig* config) { |
| 289 | CHECK_NOT_NULL(config); |
| 290 | |
| 291 | return reinterpret_cast<DisplayConfigImpl*>(config)->height; |
| 292 | } |
| 293 | |
| 294 | float ADisplayConfig_getFps(ADisplayConfig* config) { |
| 295 | CHECK_NOT_NULL(config); |
| 296 | |
| 297 | return reinterpret_cast<DisplayConfigImpl*>(config)->fps; |
| 298 | } |
| 299 | |
| 300 | int64_t ADisplayConfig_getCompositorOffsetNanos(ADisplayConfig* config) { |
| 301 | CHECK_NOT_NULL(config); |
| 302 | |
| 303 | return reinterpret_cast<DisplayConfigImpl*>(config)->sfOffset; |
| 304 | } |
| 305 | |
| 306 | int64_t ADisplayConfig_getAppVsyncOffsetNanos(ADisplayConfig* config) { |
| 307 | CHECK_NOT_NULL(config); |
| 308 | |
| 309 | return reinterpret_cast<DisplayConfigImpl*>(config)->appOffset; |
| 310 | } |
Alec Mouri | d9ff327 | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 311 | |
| 312 | } // namespace android |