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> |
| 19 | #include <ui/DisplayInfo.h> |
| 20 | #include <ui/GraphicTypes.h> |
| 21 | |
| 22 | #include <algorithm> |
| 23 | #include <optional> |
| 24 | #include <type_traits> |
| 25 | #include <vector> |
| 26 | |
| 27 | namespace android::display::impl { |
| 28 | |
| 29 | /** |
| 30 | * Implementation of ADisplayConfig |
| 31 | */ |
| 32 | struct DisplayConfigImpl { |
| 33 | /** |
| 34 | * The width in pixels of the display configuration. |
| 35 | */ |
| 36 | int32_t width{0}; |
| 37 | |
| 38 | /** |
| 39 | * The height in pixels of the display configuration. |
| 40 | */ |
| 41 | |
| 42 | int32_t height{0}; |
| 43 | |
| 44 | /** |
| 45 | * The display density. |
| 46 | */ |
| 47 | float density{0}; |
| 48 | |
| 49 | /** |
| 50 | * The refresh rate of the display configuration, in frames per second. |
| 51 | */ |
| 52 | float fps{0.0}; |
| 53 | |
| 54 | /** |
| 55 | * The vsync offset at which surfaceflinger runs, in nanoseconds. |
| 56 | */ |
| 57 | int64_t sfOffset{0}; |
| 58 | |
| 59 | /** |
| 60 | * The vsync offset at which applications run, in nanoseconds. |
| 61 | */ |
| 62 | int64_t appOffset{0}; |
| 63 | }; |
| 64 | |
| 65 | // DisplayConfigImpl allocation is not managed through C++ memory apis, so |
| 66 | // preventing calling the destructor here. |
| 67 | static_assert(std::is_trivially_destructible<DisplayConfigImpl>::value); |
| 68 | |
| 69 | /** |
| 70 | * Implementation of ADisplay |
| 71 | */ |
| 72 | struct DisplayImpl { |
| 73 | /** |
| 74 | * A physical display ID, unique to this display. |
| 75 | */ |
| 76 | PhysicalDisplayId id; |
| 77 | |
| 78 | /** |
| 79 | * The type of the display, i.e. whether it is an internal or external |
| 80 | * display. |
| 81 | */ |
| 82 | ADisplayType type; |
| 83 | |
| 84 | /** |
| 85 | * Number of supported configs |
| 86 | */ |
| 87 | size_t numConfigs; |
| 88 | |
| 89 | /** |
| 90 | * Set of supported configs by this display. |
| 91 | */ |
| 92 | DisplayConfigImpl* configs; |
| 93 | }; |
| 94 | |
| 95 | // DisplayImpl allocation is not managed through C++ memory apis, so |
| 96 | // preventing calling the destructor here. |
| 97 | static_assert(std::is_trivially_destructible<DisplayImpl>::value); |
| 98 | |
| 99 | } // namespace android::display::impl |
| 100 | |
| 101 | using namespace android; |
| 102 | using namespace android::display::impl; |
| 103 | |
| 104 | #define CHECK_NOT_NULL(name) \ |
| 105 | LOG_ALWAYS_FATAL_IF(name == nullptr, "nullptr passed as " #name " argument"); |
| 106 | |
| 107 | namespace { |
| 108 | sp<IBinder> getToken(ADisplay* display) { |
| 109 | DisplayImpl* impl = reinterpret_cast<DisplayImpl*>(display); |
| 110 | return SurfaceComposerClient::getPhysicalDisplayToken(impl->id); |
| 111 | } |
| 112 | |
| 113 | int64_t computeSfOffset(const DisplayInfo& info) { |
| 114 | // This should probably be part of the config instead of extrapolated from |
| 115 | // the presentation deadline and fudged here, but the way the math works out |
| 116 | // here we do get the right offset. |
| 117 | return static_cast<int64_t>((1000000000 / info.fps) - info.presentationDeadline + 1000000); |
| 118 | } |
| 119 | } // namespace |
| 120 | |
| 121 | int ADisplay_acquirePhysicalDisplays(ADisplay*** outDisplays) { |
| 122 | const std::vector<PhysicalDisplayId> ids = SurfaceComposerClient::getPhysicalDisplayIds(); |
| 123 | const size_t size = ids.size(); |
| 124 | if (size == 0) { |
| 125 | return NO_INIT; |
| 126 | } |
| 127 | |
| 128 | std::vector<DisplayConfigImpl> configsPerDisplay[size]; |
| 129 | int numConfigs = 0; |
| 130 | for (int i = 0; i < size; ++i) { |
| 131 | const sp<IBinder> token = SurfaceComposerClient::getPhysicalDisplayToken(ids[i]); |
| 132 | Vector<DisplayInfo> configs; |
| 133 | const status_t status = SurfaceComposerClient::getDisplayConfigs(token, &configs); |
| 134 | if (status != OK) { |
| 135 | return status; |
| 136 | } |
| 137 | if (configs.empty()) { |
| 138 | return NO_INIT; |
| 139 | } |
| 140 | |
| 141 | numConfigs += configs.size(); |
| 142 | configsPerDisplay[i].reserve(configs.size()); |
| 143 | for (int j = 0; j < configs.size(); ++j) { |
| 144 | const DisplayInfo config = configs[j]; |
| 145 | configsPerDisplay[i].emplace_back( |
| 146 | DisplayConfigImpl{static_cast<int32_t>(config.w), |
| 147 | static_cast<int32_t>(config.h), config.density, config.fps, |
| 148 | computeSfOffset(config), config.appVsyncOffset}); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | const std::optional<PhysicalDisplayId> internalId = |
| 153 | SurfaceComposerClient::getInternalDisplayId(); |
| 154 | |
| 155 | // Here we allocate all our required memory in one block. The layout is as |
| 156 | // follows: |
| 157 | // ------------------------------------------------------------ |
| 158 | // | DisplayImpl pointers | DisplayImpls | DisplayConfigImpls | |
| 159 | // ------------------------------------------------------------ |
| 160 | // |
| 161 | // The caller will be given a DisplayImpl** which points to the beginning of |
| 162 | // the block of DisplayImpl pointers. |
| 163 | // Each DisplayImpl* points to a DisplayImpl in the second block. |
| 164 | // Each DisplayImpl contains a DisplayConfigImpl*, which points to a |
| 165 | // contiguous block of DisplayConfigImpls specific to that display. |
| 166 | DisplayImpl** const impls = reinterpret_cast<DisplayImpl**>( |
| 167 | malloc((sizeof(DisplayImpl) + sizeof(DisplayImpl*)) * size + |
| 168 | sizeof(DisplayConfigImpl) * numConfigs)); |
| 169 | DisplayImpl* const displayData = reinterpret_cast<DisplayImpl*>(impls + size); |
| 170 | DisplayConfigImpl* configData = reinterpret_cast<DisplayConfigImpl*>(displayData + size); |
| 171 | |
| 172 | for (size_t i = 0; i < size; ++i) { |
| 173 | const PhysicalDisplayId id = ids[i]; |
| 174 | const ADisplayType type = (internalId == id) ? ADisplayType::DISPLAY_TYPE_INTERNAL |
| 175 | : ADisplayType::DISPLAY_TYPE_EXTERNAL; |
| 176 | const std::vector<DisplayConfigImpl>& configs = configsPerDisplay[i]; |
| 177 | memcpy(configData, configs.data(), sizeof(DisplayConfigImpl) * configs.size()); |
| 178 | |
| 179 | displayData[i] = DisplayImpl{id, type, configs.size(), configData}; |
| 180 | impls[i] = displayData + i; |
| 181 | // Advance the configData pointer so that future configs are written to |
| 182 | // the correct display. |
| 183 | configData += configs.size(); |
| 184 | } |
| 185 | |
| 186 | *outDisplays = reinterpret_cast<ADisplay**>(impls); |
| 187 | return size; |
| 188 | } |
| 189 | |
| 190 | void ADisplay_release(ADisplay** displays) { |
| 191 | if (displays == nullptr) { |
| 192 | return; |
| 193 | } |
| 194 | free(displays); |
| 195 | } |
| 196 | |
| 197 | float ADisplay_getMaxSupportedFps(ADisplay* display) { |
| 198 | CHECK_NOT_NULL(display); |
| 199 | DisplayImpl* impl = reinterpret_cast<DisplayImpl*>(display); |
| 200 | float maxFps = 0.0; |
| 201 | for (int i = 0; i < impl->numConfigs; ++i) { |
| 202 | maxFps = std::max(maxFps, impl->configs[i].fps); |
| 203 | } |
| 204 | return maxFps; |
| 205 | } |
| 206 | |
| 207 | ADisplayType ADisplay_getDisplayType(ADisplay* display) { |
| 208 | CHECK_NOT_NULL(display); |
| 209 | |
| 210 | return reinterpret_cast<DisplayImpl*>(display)->type; |
| 211 | } |
| 212 | |
| 213 | int ADisplay_getCurrentConfig(ADisplay* display, ADisplayConfig** outConfig) { |
| 214 | CHECK_NOT_NULL(display); |
| 215 | |
| 216 | sp<IBinder> token = getToken(display); |
| 217 | const int index = SurfaceComposerClient::getActiveConfig(token); |
| 218 | if (index < 0) { |
| 219 | return index; |
| 220 | } |
| 221 | |
| 222 | DisplayImpl* impl = reinterpret_cast<DisplayImpl*>(display); |
| 223 | |
| 224 | *outConfig = reinterpret_cast<ADisplayConfig*>(impl->configs + index); |
| 225 | return OK; |
| 226 | } |
| 227 | |
| 228 | float ADisplayConfig_getDensity(ADisplayConfig* config) { |
| 229 | CHECK_NOT_NULL(config); |
| 230 | |
| 231 | return reinterpret_cast<DisplayConfigImpl*>(config)->density; |
| 232 | } |
| 233 | |
| 234 | int32_t ADisplayConfig_getWidth(ADisplayConfig* config) { |
| 235 | CHECK_NOT_NULL(config); |
| 236 | |
| 237 | return reinterpret_cast<DisplayConfigImpl*>(config)->width; |
| 238 | } |
| 239 | |
| 240 | int32_t ADisplayConfig_getHeight(ADisplayConfig* config) { |
| 241 | CHECK_NOT_NULL(config); |
| 242 | |
| 243 | return reinterpret_cast<DisplayConfigImpl*>(config)->height; |
| 244 | } |
| 245 | |
| 246 | float ADisplayConfig_getFps(ADisplayConfig* config) { |
| 247 | CHECK_NOT_NULL(config); |
| 248 | |
| 249 | return reinterpret_cast<DisplayConfigImpl*>(config)->fps; |
| 250 | } |
| 251 | |
| 252 | int64_t ADisplayConfig_getCompositorOffsetNanos(ADisplayConfig* config) { |
| 253 | CHECK_NOT_NULL(config); |
| 254 | |
| 255 | return reinterpret_cast<DisplayConfigImpl*>(config)->sfOffset; |
| 256 | } |
| 257 | |
| 258 | int64_t ADisplayConfig_getAppVsyncOffsetNanos(ADisplayConfig* config) { |
| 259 | CHECK_NOT_NULL(config); |
| 260 | |
| 261 | return reinterpret_cast<DisplayConfigImpl*>(config)->appOffset; |
| 262 | } |