1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <apex/display.h>
#include <gui/SurfaceComposerClient.h>
#include <ui/DisplayMode.h>
#include <ui/DynamicDisplayInfo.h>
#include <ui/GraphicTypes.h>
#include <ui/PixelFormat.h>
#include <ui/StaticDisplayInfo.h>
#include <algorithm>
#include <optional>
#include <type_traits>
#include <vector>
namespace android::display::impl {
/**
* Implementation of ADisplayConfig
*/
struct DisplayConfigImpl {
/**
* The ID of the display configuration.
*/
size_t id;
/**
* The width in pixels of the display configuration.
*/
int32_t width{0};
/**
* The height in pixels of the display configuration.
*/
int32_t height{0};
/**
* The refresh rate of the display configuration, in frames per second.
*/
float fps{0.0};
/**
* The vsync offset at which surfaceflinger runs, in nanoseconds.
*/
int64_t sfOffset{0};
/**
* The vsync offset at which applications run, in nanoseconds.
*/
int64_t appOffset{0};
};
// DisplayConfigImpl allocation is not managed through C++ memory apis, so
// preventing calling the destructor here.
static_assert(std::is_trivially_destructible<DisplayConfigImpl>::value);
/**
* Implementation of ADisplay
*/
struct DisplayImpl {
/**
* A physical display ID, unique to this display.
*/
PhysicalDisplayId id;
/**
* The type of the display, i.e. whether it is an internal or external
* display.
*/
ADisplayType type;
/**
* The preferred WCG dataspace
*/
ADataSpace wcgDataspace;
/**
* The preferred WCG pixel format
*/
AHardwareBuffer_Format wcgPixelFormat;
/**
* Number of supported configs
*/
size_t numConfigs;
/**
* Set of supported configs by this display.
*/
DisplayConfigImpl* configs;
};
// DisplayImpl allocation is not managed through C++ memory apis, so
// preventing calling the destructor here.
static_assert(std::is_trivially_destructible<DisplayImpl>::value);
} // namespace android::display::impl
using namespace android;
using namespace android::display::impl;
#define CHECK_NOT_NULL(name) \
LOG_ALWAYS_FATAL_IF(name == nullptr, "nullptr passed as " #name " argument");
namespace android {
int ADisplay_acquirePhysicalDisplays(ADisplay*** outDisplays) {
const std::vector<PhysicalDisplayId> ids = SurfaceComposerClient::getPhysicalDisplayIds();
const size_t size = ids.size();
if (size == 0) {
return NO_INIT;
}
std::vector<DisplayConfigImpl> modesPerDisplay[size];
ui::DisplayConnectionType displayConnectionTypes[size];
int numModes = 0;
for (int i = 0; i < size; ++i) {
ui::StaticDisplayInfo staticInfo;
if (const status_t status =
SurfaceComposerClient::getStaticDisplayInfo(ids[i].value, &staticInfo);
status != OK) {
return status;
}
displayConnectionTypes[i] = staticInfo.connectionType;
ui::DynamicDisplayInfo dynamicInfo;
if (const status_t status =
SurfaceComposerClient::getDynamicDisplayInfoFromId(ids[i].value, &dynamicInfo);
status != OK) {
return status;
}
const auto& modes = dynamicInfo.supportedDisplayModes;
if (modes.empty()) {
return NO_INIT;
}
numModes += modes.size();
modesPerDisplay[i].reserve(modes.size());
for (int j = 0; j < modes.size(); ++j) {
const ui::DisplayMode& mode = modes[j];
modesPerDisplay[i].emplace_back(
DisplayConfigImpl{static_cast<size_t>(mode.id), mode.resolution.getWidth(),
mode.resolution.getHeight(), mode.peakRefreshRate,
mode.sfVsyncOffset, mode.appVsyncOffset});
}
}
ui::Dataspace defaultDataspace;
ui::PixelFormat defaultPixelFormat;
ui::Dataspace wcgDataspace;
ui::PixelFormat wcgPixelFormat;
const status_t status =
SurfaceComposerClient::getCompositionPreference(&defaultDataspace, &defaultPixelFormat,
&wcgDataspace, &wcgPixelFormat);
if (status != NO_ERROR) {
return status;
}
// Here we allocate all our required memory in one block. The layout is as
// follows:
// ------------------------------------------------------------
// | DisplayImpl pointers | DisplayImpls | DisplayConfigImpls |
// ------------------------------------------------------------
//
// The caller will be given a DisplayImpl** which points to the beginning of
// the block of DisplayImpl pointers.
// Each DisplayImpl* points to a DisplayImpl in the second block.
// Each DisplayImpl contains a DisplayConfigImpl*, which points to a
// contiguous block of DisplayConfigImpls specific to that display.
DisplayImpl** const impls = reinterpret_cast<DisplayImpl**>(
malloc((sizeof(DisplayImpl) + sizeof(DisplayImpl*)) * size +
sizeof(DisplayConfigImpl) * numModes));
DisplayImpl* const displayData = reinterpret_cast<DisplayImpl*>(impls + size);
DisplayConfigImpl* configData = reinterpret_cast<DisplayConfigImpl*>(displayData + size);
for (size_t i = 0; i < size; ++i) {
const PhysicalDisplayId id = ids[i];
const ADisplayType type = (displayConnectionTypes[i] == ui::DisplayConnectionType::Internal)
? ADisplayType::DISPLAY_TYPE_INTERNAL
: ADisplayType::DISPLAY_TYPE_EXTERNAL;
const std::vector<DisplayConfigImpl>& configs = modesPerDisplay[i];
memcpy(configData, configs.data(), sizeof(DisplayConfigImpl) * configs.size());
displayData[i] = DisplayImpl{id,
type,
static_cast<ADataSpace>(wcgDataspace),
static_cast<AHardwareBuffer_Format>(wcgPixelFormat),
configs.size(),
configData};
impls[i] = displayData + i;
// Advance the configData pointer so that future configs are written to
// the correct display.
configData += configs.size();
}
*outDisplays = reinterpret_cast<ADisplay**>(impls);
return size;
}
void ADisplay_release(ADisplay** displays) {
if (displays == nullptr) {
return;
}
free(displays);
}
float ADisplay_getMaxSupportedFps(ADisplay* display) {
CHECK_NOT_NULL(display);
DisplayImpl* impl = reinterpret_cast<DisplayImpl*>(display);
float maxFps = 0.0;
for (int i = 0; i < impl->numConfigs; ++i) {
maxFps = std::max(maxFps, impl->configs[i].fps);
}
return maxFps;
}
ADisplayType ADisplay_getDisplayType(ADisplay* display) {
CHECK_NOT_NULL(display);
return reinterpret_cast<DisplayImpl*>(display)->type;
}
void ADisplay_getPreferredWideColorFormat(ADisplay* display, ADataSpace* outDataspace,
AHardwareBuffer_Format* outPixelFormat) {
CHECK_NOT_NULL(display);
CHECK_NOT_NULL(outDataspace);
CHECK_NOT_NULL(outPixelFormat);
DisplayImpl* impl = reinterpret_cast<DisplayImpl*>(display);
*outDataspace = impl->wcgDataspace;
*outPixelFormat = impl->wcgPixelFormat;
}
int ADisplay_getCurrentConfig(ADisplay* display, ADisplayConfig** outConfig) {
CHECK_NOT_NULL(display);
ui::DynamicDisplayInfo info;
DisplayImpl* impl = reinterpret_cast<DisplayImpl*>(display);
if (const auto status =
SurfaceComposerClient::getDynamicDisplayInfoFromId(impl->id.value, &info);
status != OK) {
return status;
}
for (size_t i = 0; i < impl->numConfigs; i++) {
auto* config = impl->configs + i;
if (config->id == info.activeDisplayModeId) {
*outConfig = reinterpret_cast<ADisplayConfig*>(config);
return OK;
}
}
return NAME_NOT_FOUND;
}
int32_t ADisplayConfig_getWidth(ADisplayConfig* config) {
CHECK_NOT_NULL(config);
return reinterpret_cast<DisplayConfigImpl*>(config)->width;
}
int32_t ADisplayConfig_getHeight(ADisplayConfig* config) {
CHECK_NOT_NULL(config);
return reinterpret_cast<DisplayConfigImpl*>(config)->height;
}
float ADisplayConfig_getFps(ADisplayConfig* config) {
CHECK_NOT_NULL(config);
return reinterpret_cast<DisplayConfigImpl*>(config)->fps;
}
int64_t ADisplayConfig_getCompositorOffsetNanos(ADisplayConfig* config) {
CHECK_NOT_NULL(config);
return reinterpret_cast<DisplayConfigImpl*>(config)->sfOffset;
}
int64_t ADisplayConfig_getAppVsyncOffsetNanos(ADisplayConfig* config) {
CHECK_NOT_NULL(config);
return reinterpret_cast<DisplayConfigImpl*>(config)->appOffset;
}
} // namespace android
|