blob: 46dc54e90fcd22993a32d1c0bbdfce74490741f1 [file] [log] [blame]
Mathias Agopiana350ff92010-08-10 17:14:02 -07001/*
2 * Copyright (C) 2010 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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Dan Stoza9e56aa02015-11-02 13:00:03 -080021// #define LOG_NDEBUG 0
22
23#undef LOG_TAG
24#define LOG_TAG "HWComposer"
Mathias Agopian2965b262012-04-08 15:13:32 -070025#define ATRACE_TAG ATRACE_TAG_GRAPHICS
26
Lloyd Pique66d68602019-02-13 14:23:31 -080027#include "HWComposer.h"
28
Marin Shalamanovf8c63722020-10-06 13:11:21 +020029#include <android-base/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080030#include <compositionengine/Output.h>
31#include <compositionengine/OutputLayer.h>
32#include <compositionengine/impl/OutputLayerCompositionState.h>
Dominik Laskowski4e2b71f2020-11-10 15:05:32 -080033#include <ftl/future.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080034#include <log/log.h>
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070035#include <ui/DebugUtils.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070036#include <ui/GraphicBuffer.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080037#include <utils/Errors.h>
38#include <utils/Trace.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070039
Lloyd Pique66d68602019-02-13 14:23:31 -080040#include "../Layer.h" // needed only for debugging
Mathias Agopian33ceeb32013-04-01 16:54:58 -070041#include "../SurfaceFlinger.h"
Marin Shalamanovf8c63722020-10-06 13:11:21 +020042#include "../SurfaceFlingerProperties.h"
Lloyd Pique66d68602019-02-13 14:23:31 -080043#include "ComposerHal.h"
44#include "HWC2.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070045
Dominik Laskowskic1f18f62018-06-13 15:17:55 -070046#define LOG_HWC_DISPLAY_ERROR(hwcDisplayId, msg) \
47 ALOGE("%s failed for HWC display %" PRIu64 ": %s", __FUNCTION__, hwcDisplayId, msg)
48
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070049#define LOG_DISPLAY_ERROR(displayId, msg) \
Dominik Laskowski34157762018-10-31 13:07:19 -070050 ALOGE("%s failed for display %s: %s", __FUNCTION__, to_string(displayId).c_str(), msg)
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070051
Dominik Laskowski34157762018-10-31 13:07:19 -070052#define LOG_HWC_ERROR(what, error, displayId) \
53 ALOGE("%s: %s failed for display %s: %s (%d)", __FUNCTION__, what, \
54 to_string(displayId).c_str(), to_string(error).c_str(), static_cast<int32_t>(error))
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070055
56#define RETURN_IF_INVALID_DISPLAY(displayId, ...) \
57 do { \
Dominik Laskowski075d3172018-05-24 15:50:06 -070058 if (mDisplayData.count(displayId) == 0) { \
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070059 LOG_DISPLAY_ERROR(displayId, "Invalid display"); \
60 return __VA_ARGS__; \
61 } \
62 } while (false)
63
64#define RETURN_IF_HWC_ERROR_FOR(what, error, displayId, ...) \
65 do { \
Peiyong Line9d809e2020-04-14 13:10:48 -070066 if (error != hal::Error::NONE) { \
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070067 LOG_HWC_ERROR(what, error, displayId); \
68 return __VA_ARGS__; \
69 } \
70 } while (false)
71
72#define RETURN_IF_HWC_ERROR(error, displayId, ...) \
73 RETURN_IF_HWC_ERROR_FOR(__FUNCTION__, error, displayId, __VA_ARGS__)
74
Peiyong Line9d809e2020-04-14 13:10:48 -070075namespace hal = android::hardware::graphics::composer::hal;
76
Marin Shalamanov6e840172020-12-14 22:13:28 +010077namespace android {
Peiyong Linbdd08cc2019-12-17 21:35:14 -080078namespace {
79
80using android::hardware::Return;
81using android::hardware::Void;
Peiyong Line9d809e2020-04-14 13:10:48 -070082using android::HWC2::ComposerCallback;
Peiyong Linbdd08cc2019-12-17 21:35:14 -080083
Peiyong Line9d809e2020-04-14 13:10:48 -070084class ComposerCallbackBridge : public hal::IComposerCallback {
Peiyong Linbdd08cc2019-12-17 21:35:14 -080085public:
Peiyong Line9d809e2020-04-14 13:10:48 -070086 ComposerCallbackBridge(ComposerCallback* callback, int32_t sequenceId,
Peiyong Linbdd08cc2019-12-17 21:35:14 -080087 bool vsyncSwitchingSupported)
88 : mCallback(callback),
89 mSequenceId(sequenceId),
90 mVsyncSwitchingSupported(vsyncSwitchingSupported) {}
91
Marin Shalamanov6e840172020-12-14 22:13:28 +010092 Return<void> onHotplug(hal::HWDisplayId display, hal::Connection conn) override {
Peiyong Line9d809e2020-04-14 13:10:48 -070093 mCallback->onHotplugReceived(mSequenceId, display, conn);
Marin Shalamanov6e840172020-12-14 22:13:28 +010094 return Void();
Peiyong Linbdd08cc2019-12-17 21:35:14 -080095 }
96
Marin Shalamanov6e840172020-12-14 22:13:28 +010097 Return<void> onRefresh(hal::HWDisplayId display) override {
Peiyong Linbdd08cc2019-12-17 21:35:14 -080098 mCallback->onRefreshReceived(mSequenceId, display);
Marin Shalamanov6e840172020-12-14 22:13:28 +010099 return Void();
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800100 }
101
Marin Shalamanov6e840172020-12-14 22:13:28 +0100102 Return<void> onVsync(hal::HWDisplayId display, int64_t timestamp) override {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800103 if (!mVsyncSwitchingSupported) {
104 mCallback->onVsyncReceived(mSequenceId, display, timestamp, std::nullopt);
105 } else {
106 ALOGW("Unexpected onVsync callback on composer >= 2.4, ignoring.");
107 }
Marin Shalamanov6e840172020-12-14 22:13:28 +0100108 return Void();
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800109 }
110
Marin Shalamanov6e840172020-12-14 22:13:28 +0100111 Return<void> onVsync_2_4(hal::HWDisplayId display, int64_t timestamp,
112 hal::VsyncPeriodNanos vsyncPeriodNanos) override {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800113 if (mVsyncSwitchingSupported) {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800114 mCallback->onVsyncReceived(mSequenceId, display, timestamp,
115 std::make_optional(vsyncPeriodNanos));
116 } else {
117 ALOGW("Unexpected onVsync_2_4 callback on composer <= 2.3, ignoring.");
118 }
Marin Shalamanov6e840172020-12-14 22:13:28 +0100119 return Void();
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800120 }
121
Marin Shalamanov6e840172020-12-14 22:13:28 +0100122 Return<void> onVsyncPeriodTimingChanged(
Peiyong Line9d809e2020-04-14 13:10:48 -0700123 hal::HWDisplayId display,
124 const hal::VsyncPeriodChangeTimeline& updatedTimeline) override {
125 mCallback->onVsyncPeriodTimingChangedReceived(mSequenceId, display, updatedTimeline);
Marin Shalamanov6e840172020-12-14 22:13:28 +0100126 return Void();
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800127 }
128
Marin Shalamanov6e840172020-12-14 22:13:28 +0100129 Return<void> onSeamlessPossible(hal::HWDisplayId display) override {
Ady Abrahamb0433bc2020-01-08 17:31:06 -0800130 mCallback->onSeamlessPossible(mSequenceId, display);
Marin Shalamanov6e840172020-12-14 22:13:28 +0100131 return Void();
Ady Abrahamb0433bc2020-01-08 17:31:06 -0800132 }
133
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800134private:
Peiyong Line9d809e2020-04-14 13:10:48 -0700135 ComposerCallback* mCallback;
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800136 const int32_t mSequenceId;
137 const bool mVsyncSwitchingSupported;
138};
139
140} // namespace
141
Lloyd Pique441d5042018-10-18 16:49:51 -0700142HWComposer::~HWComposer() = default;
143
144namespace impl {
145
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200146HWComposer::HWComposer(std::unique_ptr<Hwc2::Composer> composer)
147 : mComposer(std::move(composer)),
148 mUpdateDeviceProductInfoOnHotplugReconnect(
149 android::sysprop::update_device_product_info_on_hotplug_reconnect(false)) {}
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800150
151HWComposer::HWComposer(const std::string& composerServiceName)
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200152 : HWComposer(std::make_unique<Hwc2::impl::Composer>(composerServiceName)) {}
Mathias Agopianbef42c52013-08-21 17:45:46 -0700153
Dominik Laskowskib04f98a2018-11-07 21:07:16 -0800154HWComposer::~HWComposer() {
155 mDisplayData.clear();
156}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800157
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800158void HWComposer::setConfiguration(HWC2::ComposerCallback* callback, int32_t sequenceId) {
159 loadCapabilities();
160 loadLayerMetadataSupport();
161
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800162 if (mRegisteredCallback) {
163 ALOGW("Callback already registered. Ignored extra registration attempt.");
164 return;
165 }
166 mRegisteredCallback = true;
167 sp<ComposerCallbackBridge> callbackBridge(
168 new ComposerCallbackBridge(callback, sequenceId,
169 mComposer->isVsyncPeriodSwitchSupported()));
170 mComposer->registerCallback(callbackBridge);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800171}
172
Peiyong Line9d809e2020-04-14 13:10:48 -0700173bool HWComposer::getDisplayIdentificationData(hal::HWDisplayId hwcDisplayId, uint8_t* outPort,
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700174 DisplayIdentificationData* outData) const {
Peiyong Line9d809e2020-04-14 13:10:48 -0700175 const auto error = static_cast<hal::Error>(
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800176 mComposer->getDisplayIdentificationData(hwcDisplayId, outPort, outData));
Peiyong Line9d809e2020-04-14 13:10:48 -0700177 if (error != hal::Error::NONE) {
178 if (error != hal::Error::UNSUPPORTED) {
Chia-I Wud0aff9d2018-06-21 13:39:09 +0800179 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, to_string(error).c_str());
180 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700181 return false;
182 }
183 return true;
184}
185
Peiyong Line9d809e2020-04-14 13:10:48 -0700186bool HWComposer::hasCapability(hal::Capability capability) const {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800187 return mCapabilities.count(capability) > 0;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700188}
189
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200190bool HWComposer::hasDisplayCapability(HalDisplayId displayId,
Peiyong Line9d809e2020-04-14 13:10:48 -0700191 hal::DisplayCapability capability) const {
Dominik Laskowski1162e472020-04-02 19:02:47 -0700192 RETURN_IF_INVALID_DISPLAY(displayId, false);
193 return mDisplayData.at(displayId).hwcDisplay->getCapabilities().count(capability) > 0;
Peiyong Lined531a32018-10-26 18:27:56 -0700194}
195
Peiyong Line9d809e2020-04-14 13:10:48 -0700196std::optional<DisplayIdentificationInfo> HWComposer::onHotplug(hal::HWDisplayId hwcDisplayId,
197 hal::Connection connection) {
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100198 switch (connection) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700199 case hal::Connection::CONNECTED:
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100200 return onHotplugConnect(hwcDisplayId);
Peiyong Line9d809e2020-04-14 13:10:48 -0700201 case hal::Connection::DISCONNECTED:
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100202 return onHotplugDisconnect(hwcDisplayId);
Peiyong Line9d809e2020-04-14 13:10:48 -0700203 case hal::Connection::INVALID:
Dominik Laskowski075d3172018-05-24 15:50:06 -0700204 return {};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700205 }
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700206}
207
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200208bool HWComposer::updatesDeviceProductInfoOnHotplugReconnect() const {
209 return mUpdateDeviceProductInfoOnHotplugReconnect;
210}
211
Peiyong Line9d809e2020-04-14 13:10:48 -0700212bool HWComposer::onVsync(hal::HWDisplayId hwcDisplayId, int64_t timestamp) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700213 const auto displayId = toPhysicalDisplayId(hwcDisplayId);
214 if (!displayId) {
215 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, "Invalid HWC display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700216 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700217 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700218
Dominik Laskowski075d3172018-05-24 15:50:06 -0700219 RETURN_IF_INVALID_DISPLAY(*displayId, false);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700220
Dominik Laskowski1af47932018-11-12 10:20:46 -0800221 auto& displayData = mDisplayData[*displayId];
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200222 LOG_FATAL_IF(displayData.isVirtual, "%s: Invalid operation on virtual display with ID %s",
223 __FUNCTION__, to_string(*displayId).c_str());
Jesse Hall1bd20e02012-08-29 10:47:52 -0700224
Dan Stoza9e56aa02015-11-02 13:00:03 -0800225 {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800226 std::lock_guard lock(displayData.lastHwVsyncLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700227
Dan Stoza9e56aa02015-11-02 13:00:03 -0800228 // There have been reports of HWCs that signal several vsync events
229 // with the same timestamp when turning the display off and on. This
230 // is a bug in the HWC implementation, but filter the extra events
231 // out here so they don't cause havoc downstream.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800232 if (timestamp == displayData.lastHwVsync) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700233 ALOGW("Ignoring duplicate VSYNC event from HWC for display %s (t=%" PRId64 ")",
234 to_string(*displayId).c_str(), timestamp);
Steven Thomas94e35b92017-07-26 18:48:28 -0700235 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800236 }
237
Dominik Laskowski1af47932018-11-12 10:20:46 -0800238 displayData.lastHwVsync = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700239 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800240
Dominik Laskowski34157762018-10-31 13:07:19 -0700241 const auto tag = "HW_VSYNC_" + to_string(*displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800242 ATRACE_INT(tag.c_str(), displayData.vsyncTraceToggle);
243 displayData.vsyncTraceToggle = !displayData.vsyncTraceToggle;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800244
Steven Thomas94e35b92017-07-26 18:48:28 -0700245 return true;
Jesse Hall1c569c42013-04-05 13:44:52 -0700246}
247
Dominik Laskowski075d3172018-05-24 15:50:06 -0700248std::optional<DisplayId> HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
249 ui::PixelFormat* format) {
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800250 if (SurfaceFlinger::maxVirtualDisplaySize != 0 &&
251 (width > SurfaceFlinger::maxVirtualDisplaySize ||
252 height > SurfaceFlinger::maxVirtualDisplaySize)) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700253 ALOGE("%s: Display size %ux%u exceeds maximum dimension of %" PRIu64, __FUNCTION__, width,
254 height, SurfaceFlinger::maxVirtualDisplaySize);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700255 return {};
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800256 }
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200257
258 const auto displayId = mVirtualIdGenerator.nextId();
259 if (!displayId) {
260 ALOGE("%s: No remaining virtual displays", __FUNCTION__);
261 return {};
262 }
263
Peiyong Line9d809e2020-04-14 13:10:48 -0700264 hal::HWDisplayId hwcDisplayId = 0;
265 const auto error = static_cast<hal::Error>(
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800266 mComposer->createVirtualDisplay(width, height, format, &hwcDisplayId));
Peiyong Line9d809e2020-04-14 13:10:48 -0700267 if (error != hal::Error::NONE) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700268 ALOGE("%s: Failed to create HWC virtual display", __FUNCTION__);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200269 mVirtualIdGenerator.markUnused(*displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700270 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700271 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800272
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800273 auto display = std::make_unique<HWC2::impl::Display>(*mComposer.get(), mCapabilities,
Peiyong Line9d809e2020-04-14 13:10:48 -0700274 hwcDisplayId, hal::DisplayType::VIRTUAL);
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800275 display->setConnected(true);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200276 auto& displayData = mDisplayData[*displayId];
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800277 displayData.hwcDisplay = std::move(display);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700278 displayData.isVirtual = true;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700279 return displayId;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700280}
281
Marin Shalamanova524a092020-07-27 21:39:55 +0200282void HWComposer::allocatePhysicalDisplay(hal::HWDisplayId hwcDisplayId,
283 PhysicalDisplayId displayId) {
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100284 mPhysicalDisplayIdMap[hwcDisplayId] = displayId;
285
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100286 if (!mInternalHwcDisplayId) {
287 mInternalHwcDisplayId = hwcDisplayId;
288 } else if (mInternalHwcDisplayId != hwcDisplayId && !mExternalHwcDisplayId) {
289 mExternalHwcDisplayId = hwcDisplayId;
290 }
291
292 auto& displayData = mDisplayData[displayId];
293 auto newDisplay =
294 std::make_unique<HWC2::impl::Display>(*mComposer.get(), mCapabilities, hwcDisplayId,
Peiyong Line9d809e2020-04-14 13:10:48 -0700295 hal::DisplayType::PHYSICAL);
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100296 newDisplay->setConnected(true);
297 displayData.hwcDisplay = std::move(newDisplay);
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100298 loadModes(displayData, hwcDisplayId);
299}
300
301int32_t HWComposer::getAttribute(hal::HWDisplayId hwcDisplayId, hal::HWConfigId configId,
302 hal::Attribute attribute) {
303 int32_t value = 0;
304 auto error = static_cast<hal::Error>(
305 mComposer->getDisplayAttribute(hwcDisplayId, configId, attribute, &value));
306
307 RETURN_IF_HWC_ERROR_FOR("getDisplayAttribute", error, *toPhysicalDisplayId(hwcDisplayId), -1);
308 return value;
309}
310
311void HWComposer::loadModes(DisplayData& displayData, hal::HWDisplayId hwcDisplayId) {
312 ALOGV("[HWC display %" PRIu64 "] %s", hwcDisplayId, __FUNCTION__);
313
314 std::vector<hal::HWConfigId> configIds;
315 auto error = static_cast<hal::Error>(mComposer->getDisplayConfigs(hwcDisplayId, &configIds));
316 RETURN_IF_HWC_ERROR_FOR("getDisplayConfigs", error, *toPhysicalDisplayId(hwcDisplayId));
317
318 displayData.modes.clear();
319 for (auto configId : configIds) {
320 auto mode = DisplayMode::Builder(configId)
Marin Shalamanov23c44202020-12-22 19:09:20 +0100321 .setId(DisplayModeId(displayData.modes.size()))
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100322 .setWidth(getAttribute(hwcDisplayId, configId, hal::Attribute::WIDTH))
323 .setHeight(getAttribute(hwcDisplayId, configId, hal::Attribute::HEIGHT))
324 .setVsyncPeriod(getAttribute(hwcDisplayId, configId,
325 hal::Attribute::VSYNC_PERIOD))
326 .setDpiX(getAttribute(hwcDisplayId, configId, hal::Attribute::DPI_X))
327 .setDpiY(getAttribute(hwcDisplayId, configId, hal::Attribute::DPI_Y))
328 .setConfigGroup(getAttribute(hwcDisplayId, configId,
329 hal::Attribute::CONFIG_GROUP))
330 .build();
331 displayData.modes.push_back(std::move(mode));
332 }
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100333}
334
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200335HWC2::Layer* HWComposer::createLayer(HalDisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700336 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
337
Steven Thomas94e35b92017-07-26 18:48:28 -0700338 HWC2::Layer* layer;
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800339 auto error = mDisplayData[displayId].hwcDisplay->createLayer(&layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700340 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800341 return layer;
342}
343
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200344void HWComposer::destroyLayer(HalDisplayId displayId, HWC2::Layer* layer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700345 RETURN_IF_INVALID_DISPLAY(displayId);
346
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800347 auto error = mDisplayData[displayId].hwcDisplay->destroyLayer(layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700348 RETURN_IF_HWC_ERROR(error, displayId);
Steven Thomas94e35b92017-07-26 18:48:28 -0700349}
350
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200351nsecs_t HWComposer::getRefreshTimestamp(PhysicalDisplayId displayId) const {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700352 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800353 const auto& displayData = mDisplayData.at(displayId);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700354 // this returns the last refresh timestamp.
355 // if the last one is not available, we estimate it based on
356 // the refresh period and whatever closest timestamp we have.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800357 std::lock_guard lock(displayData.lastHwVsyncLock);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700358 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800359 auto vsyncPeriodNanos = getDisplayVsyncPeriod(displayId);
360 return now - ((now - displayData.lastHwVsync) % vsyncPeriodNanos);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700361}
362
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200363bool HWComposer::isConnected(PhysicalDisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700364 RETURN_IF_INVALID_DISPLAY(displayId, false);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700365 return mDisplayData.at(displayId).hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700366}
367
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100368DisplayModes HWComposer::getModes(PhysicalDisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700369 RETURN_IF_INVALID_DISPLAY(displayId, {});
370
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100371 // We cache the modes when the DisplayData is created on hotplug. If the modes need to
Marin Shalamanov6e840172020-12-14 22:13:28 +0100372 // change HWC will send a hotplug event which will recreate displayData.
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100373 return mDisplayData.at(displayId).modes;
Mathias Agopianda27af92012-09-13 18:17:13 -0700374}
375
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100376DisplayModePtr HWComposer::getActiveMode(PhysicalDisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700377 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
378
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100379 const auto hwcId = *fromPhysicalDisplayId(displayId);
380 ALOGV("[%" PRIu64 "] getActiveMode", hwcId);
381 hal::HWConfigId configId;
382 auto error = static_cast<hal::Error>(mComposer->getActiveConfig(hwcId, &configId));
383
384 const auto& modes = mDisplayData.at(displayId).modes;
Peiyong Line9d809e2020-04-14 13:10:48 -0700385 if (error == hal::Error::BAD_CONFIG) {
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100386 LOG_DISPLAY_ERROR(displayId, "No active mode");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800387 return nullptr;
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700388 }
389
390 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
391
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100392 const auto it = std::find_if(modes.begin(), modes.end(),
393 [configId](auto mode) { return mode->getHwcId() == configId; });
394 if (it == modes.end()) {
395 LOG_DISPLAY_ERROR(displayId, "Unknown mode");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800396 return nullptr;
397 }
398
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100399 return *it;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700400}
401
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800402// Composer 2.4
403
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200404DisplayConnectionType HWComposer::getDisplayConnectionType(PhysicalDisplayId displayId) const {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800405 RETURN_IF_INVALID_DISPLAY(displayId, DisplayConnectionType::Internal);
406 const auto& hwcDisplay = mDisplayData.at(displayId).hwcDisplay;
407
408 DisplayConnectionType type;
409 const auto error = hwcDisplay->getConnectionType(&type);
410
411 const auto FALLBACK_TYPE = hwcDisplay->getId() == mInternalHwcDisplayId
412 ? DisplayConnectionType::Internal
413 : DisplayConnectionType::External;
414
415 RETURN_IF_HWC_ERROR(error, displayId, FALLBACK_TYPE);
416 return type;
417}
418
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200419bool HWComposer::isVsyncPeriodSwitchSupported(PhysicalDisplayId displayId) const {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800420 RETURN_IF_INVALID_DISPLAY(displayId, false);
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800421 return mDisplayData.at(displayId).hwcDisplay->isVsyncPeriodSwitchSupported();
422}
423
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200424nsecs_t HWComposer::getDisplayVsyncPeriod(PhysicalDisplayId displayId) const {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800425 RETURN_IF_INVALID_DISPLAY(displayId, 0);
426
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100427 if (isVsyncPeriodSwitchSupported(displayId)) {
428 const auto hwcId = *fromPhysicalDisplayId(displayId);
429 Hwc2::VsyncPeriodNanos vsyncPeriodNanos = 0;
430 auto error =
431 static_cast<hal::Error>(mComposer->getDisplayVsyncPeriod(hwcId, &vsyncPeriodNanos));
432 RETURN_IF_HWC_ERROR(error, displayId, 0);
433 return static_cast<nsecs_t>(vsyncPeriodNanos);
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700434 }
435
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100436 // Get the default vsync period
437 auto mode = getActiveMode(displayId);
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700438
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100439 if (!mode) {
440 // HWC has updated the display modes and hasn't notified us yet.
441 RETURN_IF_HWC_ERROR(hal::Error::BAD_CONFIG, displayId, 0);
Lloyd Pique3c085a02018-05-09 19:38:32 -0700442 }
443
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100444 return mode->getVsyncPeriod();
Lloyd Pique3c085a02018-05-09 19:38:32 -0700445}
446
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200447std::vector<ui::ColorMode> HWComposer::getColorModes(PhysicalDisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700448 RETURN_IF_INVALID_DISPLAY(displayId, {});
449
Peiyong Linfd997e02018-03-28 15:29:00 -0700450 std::vector<ui::ColorMode> modes;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700451 auto error = mDisplayData.at(displayId).hwcDisplay->getColorModes(&modes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700452 RETURN_IF_HWC_ERROR(error, displayId, {});
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600453 return modes;
454}
455
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200456status_t HWComposer::setActiveColorMode(PhysicalDisplayId displayId, ui::ColorMode mode,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700457 ui::RenderIntent renderIntent) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700458 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Michael Wright28f24d02016-07-12 13:30:53 -0700459
460 auto& displayData = mDisplayData[displayId];
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700461 auto error = displayData.hwcDisplay->setColorMode(mode, renderIntent);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700462 RETURN_IF_HWC_ERROR_FOR(("setColorMode(" + decodeColorMode(mode) + ", " +
463 decodeRenderIntent(renderIntent) + ")")
464 .c_str(),
465 error, displayId, UNKNOWN_ERROR);
Michael Wright28f24d02016-07-12 13:30:53 -0700466
467 return NO_ERROR;
468}
469
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200470void HWComposer::setVsyncEnabled(PhysicalDisplayId displayId, hal::Vsync enabled) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700471 RETURN_IF_INVALID_DISPLAY(displayId);
472 auto& displayData = mDisplayData[displayId];
473
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200474 LOG_FATAL_IF(displayData.isVirtual, "%s: Invalid operation on virtual display with ID %s",
475 __FUNCTION__, to_string(displayId).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800476
Dan Stoza9e56aa02015-11-02 13:00:03 -0800477 // NOTE: we use our own internal lock here because we have to call
478 // into the HWC with the lock held, and we want to make sure
479 // that even if HWC blocks (which it shouldn't), it won't
480 // affect other threads.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800481 std::lock_guard lock(displayData.vsyncEnabledLock);
482 if (enabled == displayData.vsyncEnabled) {
483 return;
Colin Cross10fbdb62012-07-12 17:56:34 -0700484 }
Dominik Laskowski1af47932018-11-12 10:20:46 -0800485
486 ATRACE_CALL();
487 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
488 RETURN_IF_HWC_ERROR(error, displayId);
489
490 displayData.vsyncEnabled = enabled;
491
492 const auto tag = "HW_VSYNC_ON_" + to_string(displayId);
Peiyong Line9d809e2020-04-14 13:10:48 -0700493 ATRACE_INT(tag.c_str(), enabled == hal::Vsync::ENABLE ? 1 : 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700494}
495
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200496status_t HWComposer::setClientTarget(HalDisplayId displayId, uint32_t slot,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700497 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
498 ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700499 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Jesse Hall851cfe82013-03-20 13:44:00 -0700500
Dominik Laskowski34157762018-10-31 13:07:19 -0700501 ALOGV("%s for display %s", __FUNCTION__, to_string(displayId).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800502 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400503 auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700504 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Jesse Hall851cfe82013-03-20 13:44:00 -0700505 return NO_ERROR;
506}
507
Lloyd Pique66d68602019-02-13 14:23:31 -0800508status_t HWComposer::getDeviceCompositionChanges(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200509 HalDisplayId displayId, bool frameUsesClientComposition,
Lloyd Pique66d68602019-02-13 14:23:31 -0800510 std::optional<android::HWComposer::DeviceRequestedChanges>* outChanges) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800511 ATRACE_CALL();
512
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700513 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800514
515 auto& displayData = mDisplayData[displayId];
516 auto& hwcDisplay = displayData.hwcDisplay;
517 if (!hwcDisplay->isConnected()) {
518 return NO_ERROR;
519 }
520
521 uint32_t numTypes = 0;
522 uint32_t numRequests = 0;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700523
Peiyong Line9d809e2020-04-14 13:10:48 -0700524 hal::Error error = hal::Error::NONE;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700525
Chia-I Wu41b98d42017-12-11 11:04:36 -0800526 // First try to skip validate altogether when there is no client
527 // composition. When there is client composition, since we haven't
528 // rendered to the client target yet, we should not attempt to skip
529 // validate.
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700530 displayData.validateWasSkipped = false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800531 if (!frameUsesClientComposition) {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800532 sp<Fence> outPresentFence;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700533 uint32_t state = UINT32_MAX;
534 error = hwcDisplay->presentOrValidate(&numTypes, &numRequests, &outPresentFence , &state);
Peiyong Line9d809e2020-04-14 13:10:48 -0700535 if (!hasChangesError(error)) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700536 RETURN_IF_HWC_ERROR_FOR("presentOrValidate", error, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700537 }
538 if (state == 1) { //Present Succeeded.
Steven Thomas94e35b92017-07-26 18:48:28 -0700539 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700540 error = hwcDisplay->getReleaseFences(&releaseFences);
541 displayData.releaseFences = std::move(releaseFences);
542 displayData.lastPresentFence = outPresentFence;
543 displayData.validateWasSkipped = true;
544 displayData.presentError = error;
545 return NO_ERROR;
546 }
547 // Present failed but Validate ran.
548 } else {
549 error = hwcDisplay->validate(&numTypes, &numRequests);
550 }
551 ALOGV("SkipValidate failed, Falling back to SLOW validate/present");
Peiyong Line9d809e2020-04-14 13:10:48 -0700552 if (!hasChangesError(error)) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700553 RETURN_IF_HWC_ERROR_FOR("validate", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800554 }
555
Lloyd Pique66d68602019-02-13 14:23:31 -0800556 android::HWComposer::DeviceRequestedChanges::ChangedTypes changedTypes;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800557 changedTypes.reserve(numTypes);
558 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700559 RETURN_IF_HWC_ERROR_FOR("getChangedCompositionTypes", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800560
Peiyong Line9d809e2020-04-14 13:10:48 -0700561 auto displayRequests = static_cast<hal::DisplayRequest>(0);
Lloyd Pique66d68602019-02-13 14:23:31 -0800562 android::HWComposer::DeviceRequestedChanges::LayerRequests layerRequests;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800563 layerRequests.reserve(numRequests);
Lloyd Pique66d68602019-02-13 14:23:31 -0800564 error = hwcDisplay->getRequests(&displayRequests, &layerRequests);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700565 RETURN_IF_HWC_ERROR_FOR("getRequests", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800566
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700567 DeviceRequestedChanges::ClientTargetProperty clientTargetProperty;
568 error = hwcDisplay->getClientTargetProperty(&clientTargetProperty);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800569
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700570 outChanges->emplace(DeviceRequestedChanges{std::move(changedTypes), std::move(displayRequests),
571 std::move(layerRequests),
572 std::move(clientTargetProperty)});
Dan Stoza9e56aa02015-11-02 13:00:03 -0800573 error = hwcDisplay->acceptChanges();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700574 RETURN_IF_HWC_ERROR_FOR("acceptChanges", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800575
576 return NO_ERROR;
577}
578
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200579sp<Fence> HWComposer::getPresentFence(HalDisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700580 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700581 return mDisplayData.at(displayId).lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700582}
583
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200584sp<Fence> HWComposer::getLayerReleaseFence(HalDisplayId displayId, HWC2::Layer* layer) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700585 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Tim Murray2d3f8b82019-12-04 16:24:17 -0800586 const auto& displayFences = mDisplayData.at(displayId).releaseFences;
587 auto fence = displayFences.find(layer);
588 if (fence == displayFences.end()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800589 ALOGV("getLayerReleaseFence: Release fence not found");
590 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700591 }
Tim Murray2d3f8b82019-12-04 16:24:17 -0800592 return fence->second;
Riley Andrews03414a12014-07-01 14:22:59 -0700593}
594
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200595status_t HWComposer::presentAndGetReleaseFences(HalDisplayId displayId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800596 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700597
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700598 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700599
Dan Stoza9e56aa02015-11-02 13:00:03 -0800600 auto& displayData = mDisplayData[displayId];
601 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700602
603 if (displayData.validateWasSkipped) {
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700604 // explicitly flush all pending commands
Peiyong Line9d809e2020-04-14 13:10:48 -0700605 auto error = static_cast<hal::Error>(mComposer->executeCommands());
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800606 RETURN_IF_HWC_ERROR_FOR("executeCommands", error, displayId, UNKNOWN_ERROR);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700607 RETURN_IF_HWC_ERROR_FOR("present", displayData.presentError, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700608 return NO_ERROR;
609 }
610
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800611 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700612 RETURN_IF_HWC_ERROR_FOR("present", error, displayId, UNKNOWN_ERROR);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700613
Steven Thomas94e35b92017-07-26 18:48:28 -0700614 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800615 error = hwcDisplay->getReleaseFences(&releaseFences);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700616 RETURN_IF_HWC_ERROR_FOR("getReleaseFences", error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800617
618 displayData.releaseFences = std::move(releaseFences);
619
620 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700621}
622
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200623status_t HWComposer::setPowerMode(PhysicalDisplayId displayId, hal::PowerMode mode) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700624 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
625
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700626 const auto& displayData = mDisplayData[displayId];
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200627 LOG_FATAL_IF(displayData.isVirtual, "%s: Invalid operation on virtual display with ID %s",
628 __FUNCTION__, to_string(displayId).c_str());
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700629
Peiyong Line9d809e2020-04-14 13:10:48 -0700630 if (mode == hal::PowerMode::OFF) {
631 setVsyncEnabled(displayId, hal::Vsync::DISABLE);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800632 }
633
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700634 auto& hwcDisplay = displayData.hwcDisplay;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800635 switch (mode) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700636 case hal::PowerMode::OFF:
637 case hal::PowerMode::ON:
Dan Stoza9e56aa02015-11-02 13:00:03 -0800638 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
639 {
640 auto error = hwcDisplay->setPowerMode(mode);
Peiyong Line9d809e2020-04-14 13:10:48 -0700641 if (error != hal::Error::NONE) {
642 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(), error,
643 displayId);
Peiyong Lin306e4992018-05-07 16:18:22 -0700644 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700645 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800646 break;
Peiyong Line9d809e2020-04-14 13:10:48 -0700647 case hal::PowerMode::DOZE:
648 case hal::PowerMode::DOZE_SUSPEND:
Dan Stoza9e56aa02015-11-02 13:00:03 -0800649 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
650 {
651 bool supportsDoze = false;
652 auto error = hwcDisplay->supportsDoze(&supportsDoze);
Peiyong Line9d809e2020-04-14 13:10:48 -0700653 if (error != hal::Error::NONE) {
Peiyong Lin306e4992018-05-07 16:18:22 -0700654 LOG_HWC_ERROR("supportsDoze", error, displayId);
655 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700656
Dan Stoza9e56aa02015-11-02 13:00:03 -0800657 if (!supportsDoze) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700658 mode = hal::PowerMode::ON;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800659 }
660
661 error = hwcDisplay->setPowerMode(mode);
Peiyong Line9d809e2020-04-14 13:10:48 -0700662 if (error != hal::Error::NONE) {
663 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(), error,
664 displayId);
Peiyong Lin306e4992018-05-07 16:18:22 -0700665 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800666 }
667 break;
668 default:
669 ALOGV("setPowerMode: Not calling HWC");
670 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700671 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800672
673 return NO_ERROR;
674}
675
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100676status_t HWComposer::setActiveModeWithConstraints(
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100677 PhysicalDisplayId displayId, hal::HWConfigId hwcModeId,
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200678 const hal::VsyncPeriodChangeConstraints& constraints,
Peiyong Line9d809e2020-04-14 13:10:48 -0700679 hal::VsyncPeriodChangeTimeline* outTimeline) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700680 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800681
Marin Shalamanov12c9e5a2021-01-07 00:25:35 +0100682 auto error = mDisplayData[displayId].hwcDisplay->setActiveConfigWithConstraints(hwcModeId,
683 constraints,
684 outTimeline);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700685 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800686 return NO_ERROR;
687}
688
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200689status_t HWComposer::setColorTransform(HalDisplayId displayId, const mat4& transform) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700690 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700691
692 auto& displayData = mDisplayData[displayId];
693 bool isIdentity = transform == mat4();
Peiyong Line9d809e2020-04-14 13:10:48 -0700694 auto error = displayData.hwcDisplay
695 ->setColorTransform(transform,
696 isIdentity ? hal::ColorTransform::IDENTITY
697 : hal::ColorTransform::ARBITRARY_MATRIX);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700698 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700699 return NO_ERROR;
700}
701
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200702void HWComposer::disconnectDisplay(HalDisplayId displayId) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700703 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800704 auto& displayData = mDisplayData[displayId];
705
Dan Stoza9e56aa02015-11-02 13:00:03 -0800706 // If this was a virtual display, add its slot back for reuse by future
707 // virtual displays
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700708 if (displayData.isVirtual) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200709 mVirtualIdGenerator.markUnused(*HalVirtualDisplayId::tryCast(displayId));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800710 }
711
Dominik Laskowski7e045462018-05-30 13:02:02 -0700712 const auto hwcDisplayId = displayData.hwcDisplay->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700713
714 // TODO(b/74619554): Select internal/external display from remaining displays.
715 if (hwcDisplayId == mInternalHwcDisplayId) {
716 mInternalHwcDisplayId.reset();
717 } else if (hwcDisplayId == mExternalHwcDisplayId) {
718 mExternalHwcDisplayId.reset();
719 }
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800720 mPhysicalDisplayIdMap.erase(hwcDisplayId);
721 mDisplayData.erase(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800722}
723
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200724status_t HWComposer::setOutputBuffer(HalVirtualDisplayId displayId, const sp<Fence>& acquireFence,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700725 const sp<GraphicBuffer>& buffer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700726 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700727 const auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800728
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200729 LOG_FATAL_IF(!displayData.isVirtual, "%s: Invalid operation on physical display with ID %s",
730 __FUNCTION__, to_string(displayId).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800731
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700732 auto error = displayData.hwcDisplay->setOutputBuffer(buffer, acquireFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700733 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800734 return NO_ERROR;
735}
736
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200737void HWComposer::clearReleaseFences(HalDisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700738 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800739 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700740}
741
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200742status_t HWComposer::getHdrCapabilities(HalDisplayId displayId, HdrCapabilities* outCapabilities) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700743 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stozac4f471e2016-03-24 09:31:08 -0700744
745 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Peiyong Lin62665892018-04-16 11:07:44 -0700746 auto error = hwcDisplay->getHdrCapabilities(outCapabilities);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700747 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Peiyong Lin62665892018-04-16 11:07:44 -0700748 return NO_ERROR;
Dan Stozac4f471e2016-03-24 09:31:08 -0700749}
750
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200751int32_t HWComposer::getSupportedPerFrameMetadata(HalDisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700752 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700753 return mDisplayData.at(displayId).hwcDisplay->getSupportedPerFrameMetadata();
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700754}
755
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200756std::vector<ui::RenderIntent> HWComposer::getRenderIntents(HalDisplayId displayId,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700757 ui::ColorMode colorMode) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700758 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700759
760 std::vector<ui::RenderIntent> renderIntents;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700761 auto error = mDisplayData.at(displayId).hwcDisplay->getRenderIntents(colorMode, &renderIntents);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700762 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700763 return renderIntents;
764}
765
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200766mat4 HWComposer::getDataspaceSaturationMatrix(HalDisplayId displayId, ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700767 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700768
769 mat4 matrix;
770 auto error = mDisplayData[displayId].hwcDisplay->getDataspaceSaturationMatrix(dataspace,
771 &matrix);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700772 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700773 return matrix;
774}
775
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200776status_t HWComposer::getDisplayedContentSamplingAttributes(HalDisplayId displayId,
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700777 ui::PixelFormat* outFormat,
778 ui::Dataspace* outDataspace,
779 uint8_t* outComponentMask) {
780 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
781 const auto error =
782 mDisplayData[displayId]
783 .hwcDisplay->getDisplayedContentSamplingAttributes(outFormat, outDataspace,
784 outComponentMask);
Peiyong Line9d809e2020-04-14 13:10:48 -0700785 if (error == hal::Error::UNSUPPORTED) RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700786 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
787 return NO_ERROR;
788}
789
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200790status_t HWComposer::setDisplayContentSamplingEnabled(HalDisplayId displayId, bool enabled,
Kevin DuBois74e53772018-11-19 10:52:38 -0800791 uint8_t componentMask, uint64_t maxFrames) {
792 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
793 const auto error =
794 mDisplayData[displayId].hwcDisplay->setDisplayContentSamplingEnabled(enabled,
795 componentMask,
796 maxFrames);
797
Peiyong Line9d809e2020-04-14 13:10:48 -0700798 if (error == hal::Error::UNSUPPORTED) RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
799 if (error == hal::Error::BAD_PARAMETER) RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Kevin DuBois74e53772018-11-19 10:52:38 -0800800 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
801 return NO_ERROR;
802}
803
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200804status_t HWComposer::getDisplayedContentSample(HalDisplayId displayId, uint64_t maxFrames,
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700805 uint64_t timestamp, DisplayedFrameStats* outStats) {
806 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
807 const auto error =
808 mDisplayData[displayId].hwcDisplay->getDisplayedContentSample(maxFrames, timestamp,
809 outStats);
810 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
811 return NO_ERROR;
812}
813
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200814std::future<status_t> HWComposer::setDisplayBrightness(PhysicalDisplayId displayId,
815 float brightness) {
Dominik Laskowski4e2b71f2020-11-10 15:05:32 -0800816 RETURN_IF_INVALID_DISPLAY(displayId, ftl::yield<status_t>(BAD_INDEX));
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700817 auto& display = mDisplayData[displayId].hwcDisplay;
818
Dominik Laskowski4e2b71f2020-11-10 15:05:32 -0800819 return ftl::chain(display->setDisplayBrightness(brightness))
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700820 .then([displayId](hal::Error error) -> status_t {
821 if (error == hal::Error::UNSUPPORTED) {
822 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
823 }
824 if (error == hal::Error::BAD_PARAMETER) {
825 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
826 }
827 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
828 return NO_ERROR;
829 });
Dan Gittik57e63c52019-01-18 16:37:54 +0000830}
831
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200832status_t HWComposer::setAutoLowLatencyMode(PhysicalDisplayId displayId, bool on) {
Galia Peycheva5492cb52019-10-30 14:13:16 +0100833 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
834 const auto error = mDisplayData[displayId].hwcDisplay->setAutoLowLatencyMode(on);
Peiyong Line9d809e2020-04-14 13:10:48 -0700835 if (error == hal::Error::UNSUPPORTED) {
Galia Peycheva5492cb52019-10-30 14:13:16 +0100836 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
837 }
Peiyong Line9d809e2020-04-14 13:10:48 -0700838 if (error == hal::Error::BAD_PARAMETER) {
Galia Peycheva5492cb52019-10-30 14:13:16 +0100839 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
840 }
841 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
842 return NO_ERROR;
843}
844
845status_t HWComposer::getSupportedContentTypes(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200846 PhysicalDisplayId displayId, std::vector<hal::ContentType>* outSupportedContentTypes) {
Galia Peycheva5492cb52019-10-30 14:13:16 +0100847 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
848 const auto error =
849 mDisplayData[displayId].hwcDisplay->getSupportedContentTypes(outSupportedContentTypes);
850
851 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
852
853 return NO_ERROR;
854}
855
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200856status_t HWComposer::setContentType(PhysicalDisplayId displayId, hal::ContentType contentType) {
Galia Peycheva5492cb52019-10-30 14:13:16 +0100857 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
858 const auto error = mDisplayData[displayId].hwcDisplay->setContentType(contentType);
Peiyong Line9d809e2020-04-14 13:10:48 -0700859 if (error == hal::Error::UNSUPPORTED) {
Galia Peycheva5492cb52019-10-30 14:13:16 +0100860 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
861 }
Peiyong Line9d809e2020-04-14 13:10:48 -0700862 if (error == hal::Error::BAD_PARAMETER) {
Galia Peycheva5492cb52019-10-30 14:13:16 +0100863 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
864 }
865 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
866
867 return NO_ERROR;
868}
869
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800870const std::unordered_map<std::string, bool>& HWComposer::getSupportedLayerGenericMetadata() const {
871 return mSupportedLayerGenericMetadata;
872}
873
Yiwei Zhang5434a782018-12-05 18:06:32 -0800874void HWComposer::dump(std::string& result) const {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800875 result.append(mComposer->dumpDebugInfo());
Mathias Agopian83727852010-09-23 18:13:21 -0700876}
877
Marin Shalamanova524a092020-07-27 21:39:55 +0200878std::optional<PhysicalDisplayId> HWComposer::toPhysicalDisplayId(
879 hal::HWDisplayId hwcDisplayId) const {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700880 if (const auto it = mPhysicalDisplayIdMap.find(hwcDisplayId);
881 it != mPhysicalDisplayIdMap.end()) {
882 return it->second;
883 }
884 return {};
885}
886
Marin Shalamanova524a092020-07-27 21:39:55 +0200887std::optional<hal::HWDisplayId> HWComposer::fromPhysicalDisplayId(
888 PhysicalDisplayId displayId) const {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700889 if (const auto it = mDisplayData.find(displayId);
890 it != mDisplayData.end() && !it->second.isVirtual) {
891 return it->second.hwcDisplay->getId();
892 }
893 return {};
894}
895
Peiyong Line9d809e2020-04-14 13:10:48 -0700896bool HWComposer::shouldIgnoreHotplugConnect(hal::HWDisplayId hwcDisplayId,
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100897 bool hasDisplayIdentificationData) const {
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100898 if (mHasMultiDisplaySupport && !hasDisplayIdentificationData) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700899 ALOGE("Ignoring connection of display %" PRIu64 " without identification data",
900 hwcDisplayId);
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100901 return true;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700902 }
903
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100904 if (!mHasMultiDisplaySupport && mInternalHwcDisplayId && mExternalHwcDisplayId) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700905 ALOGE("Ignoring connection of tertiary display %" PRIu64, hwcDisplayId);
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100906 return true;
907 }
908
909 return false;
910}
911
Peiyong Line9d809e2020-04-14 13:10:48 -0700912std::optional<DisplayIdentificationInfo> HWComposer::onHotplugConnect(
913 hal::HWDisplayId hwcDisplayId) {
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100914 std::optional<DisplayIdentificationInfo> info;
915 if (const auto displayId = toPhysicalDisplayId(hwcDisplayId)) {
916 info = DisplayIdentificationInfo{.id = *displayId,
917 .name = std::string(),
918 .deviceProductInfo = std::nullopt};
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200919 if (mUpdateDeviceProductInfoOnHotplugReconnect) {
920 uint8_t port;
921 DisplayIdentificationData data;
922 getDisplayIdentificationData(hwcDisplayId, &port, &data);
923 if (auto newInfo = parseDisplayIdentificationData(port, data)) {
924 info->deviceProductInfo = std::move(newInfo->deviceProductInfo);
925 } else {
926 ALOGE("Failed to parse identification data for display %" PRIu64, hwcDisplayId);
927 }
928 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700929 } else {
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100930 uint8_t port;
931 DisplayIdentificationData data;
932 const bool hasDisplayIdentificationData =
933 getDisplayIdentificationData(hwcDisplayId, &port, &data);
934 if (mPhysicalDisplayIdMap.empty()) {
935 mHasMultiDisplaySupport = hasDisplayIdentificationData;
936 ALOGI("Switching to %s multi-display mode",
937 mHasMultiDisplaySupport ? "generalized" : "legacy");
938 }
939
940 if (shouldIgnoreHotplugConnect(hwcDisplayId, hasDisplayIdentificationData)) {
941 return {};
942 }
943
944 info = [this, hwcDisplayId, &port, &data, hasDisplayIdentificationData] {
945 const bool isPrimary = !mInternalHwcDisplayId;
946 if (mHasMultiDisplaySupport) {
947 if (const auto info = parseDisplayIdentificationData(port, data)) {
948 return *info;
949 }
950 ALOGE("Failed to parse identification data for display %" PRIu64, hwcDisplayId);
951 } else {
952 ALOGW_IF(hasDisplayIdentificationData,
953 "Ignoring identification data for display %" PRIu64, hwcDisplayId);
Peiyong Lin65248e02020-04-18 21:15:07 -0700954 port = isPrimary ? LEGACY_DISPLAY_TYPE_PRIMARY : LEGACY_DISPLAY_TYPE_EXTERNAL;
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100955 }
956
Marin Shalamanova524a092020-07-27 21:39:55 +0200957 return DisplayIdentificationInfo{.id = PhysicalDisplayId::fromPort(port),
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100958 .name = isPrimary ? "Internal display"
959 : "External display",
960 .deviceProductInfo = std::nullopt};
961 }();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700962 }
963
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100964 if (!isConnected(info->id)) {
965 allocatePhysicalDisplay(hwcDisplayId, info->id);
966 }
967 return info;
968}
969
970std::optional<DisplayIdentificationInfo> HWComposer::onHotplugDisconnect(
Peiyong Line9d809e2020-04-14 13:10:48 -0700971 hal::HWDisplayId hwcDisplayId) {
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100972 const auto displayId = toPhysicalDisplayId(hwcDisplayId);
973 if (!displayId) {
974 ALOGE("Ignoring disconnection of invalid HWC display %" PRIu64, hwcDisplayId);
975 return {};
Dominik Laskowski075d3172018-05-24 15:50:06 -0700976 }
977
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100978 // The display will later be destroyed by a call to
979 // destroyDisplay(). For now we just mark it disconnected.
980 if (isConnected(*displayId)) {
981 mDisplayData[*displayId].hwcDisplay->setConnected(false);
982 } else {
983 ALOGW("Attempted to disconnect unknown display %" PRIu64, hwcDisplayId);
984 }
985 // The cleanup of Disconnect is handled through HWComposer::disconnectDisplay
986 // via SurfaceFlinger's onHotplugReceived callback handling
987 return DisplayIdentificationInfo{.id = *displayId,
988 .name = std::string(),
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200989 .deviceProductInfo = std::nullopt};
Steven Thomas6e8f7062017-11-22 14:15:29 -0800990}
991
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800992void HWComposer::loadCapabilities() {
Peiyong Line9d809e2020-04-14 13:10:48 -0700993 static_assert(sizeof(hal::Capability) == sizeof(int32_t), "Capability size has changed");
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800994 auto capabilities = mComposer->getCapabilities();
995 for (auto capability : capabilities) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700996 mCapabilities.emplace(static_cast<hal::Capability>(capability));
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800997 }
998}
999
Lloyd Pique4603f3c2020-02-11 12:06:56 -08001000void HWComposer::loadLayerMetadataSupport() {
1001 mSupportedLayerGenericMetadata.clear();
1002
1003 std::vector<Hwc2::IComposerClient::LayerGenericMetadataKey> supportedMetadataKeyInfo;
1004 const auto error = mComposer->getLayerGenericMetadataKeys(&supportedMetadataKeyInfo);
1005 if (error != hardware::graphics::composer::V2_4::Error::NONE) {
1006 ALOGE("%s: %s failed: %s (%d)", __FUNCTION__, "getLayerGenericMetadataKeys",
1007 toString(error).c_str(), static_cast<int32_t>(error));
1008 return;
1009 }
1010
1011 for (const auto& [name, mandatory] : supportedMetadataKeyInfo) {
1012 mSupportedLayerGenericMetadata.emplace(name, mandatory);
1013 }
1014}
1015
Peiyong Linbdd08cc2019-12-17 21:35:14 -08001016uint32_t HWComposer::getMaxVirtualDisplayCount() const {
1017 return mComposer->getMaxVirtualDisplayCount();
1018}
1019
Lloyd Pique441d5042018-10-18 16:49:51 -07001020} // namespace impl
Dominik Laskowskif9750f22018-06-06 12:24:53 -07001021} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08001022
1023// TODO(b/129481165): remove the #pragma below and fix conversion issues
1024#pragma clang diagnostic pop // ignored "-Wconversion"