blob: 6f3987f88f88becb4aae2e870ab04775f23fc632 [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 Shalamanovbdd59152020-02-14 15:30:06 +0100284 if (!mInternalHwcDisplayId) {
285 mInternalHwcDisplayId = hwcDisplayId;
286 } else if (mInternalHwcDisplayId != hwcDisplayId && !mExternalHwcDisplayId) {
287 mExternalHwcDisplayId = hwcDisplayId;
288 }
289
290 auto& displayData = mDisplayData[displayId];
291 auto newDisplay =
292 std::make_unique<HWC2::impl::Display>(*mComposer.get(), mCapabilities, hwcDisplayId,
Peiyong Line9d809e2020-04-14 13:10:48 -0700293 hal::DisplayType::PHYSICAL);
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100294 newDisplay->setConnected(true);
295 displayData.hwcDisplay = std::move(newDisplay);
Marin Shalamanov6e840172020-12-14 22:13:28 +0100296 displayData.configs = displayData.hwcDisplay->getConfigs();
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100297 mPhysicalDisplayIdMap[hwcDisplayId] = displayId;
298}
299
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200300HWC2::Layer* HWComposer::createLayer(HalDisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700301 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
302
Steven Thomas94e35b92017-07-26 18:48:28 -0700303 HWC2::Layer* layer;
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800304 auto error = mDisplayData[displayId].hwcDisplay->createLayer(&layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700305 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800306 return layer;
307}
308
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200309void HWComposer::destroyLayer(HalDisplayId displayId, HWC2::Layer* layer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700310 RETURN_IF_INVALID_DISPLAY(displayId);
311
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800312 auto error = mDisplayData[displayId].hwcDisplay->destroyLayer(layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700313 RETURN_IF_HWC_ERROR(error, displayId);
Steven Thomas94e35b92017-07-26 18:48:28 -0700314}
315
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200316nsecs_t HWComposer::getRefreshTimestamp(PhysicalDisplayId displayId) const {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700317 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800318 const auto& displayData = mDisplayData.at(displayId);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700319 // this returns the last refresh timestamp.
320 // if the last one is not available, we estimate it based on
321 // the refresh period and whatever closest timestamp we have.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800322 std::lock_guard lock(displayData.lastHwVsyncLock);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700323 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800324 auto vsyncPeriodNanos = getDisplayVsyncPeriod(displayId);
325 return now - ((now - displayData.lastHwVsync) % vsyncPeriodNanos);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700326}
327
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200328bool HWComposer::isConnected(PhysicalDisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700329 RETURN_IF_INVALID_DISPLAY(displayId, false);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700330 return mDisplayData.at(displayId).hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700331}
332
Dominik Laskowski075d3172018-05-24 15:50:06 -0700333std::vector<std::shared_ptr<const HWC2::Display::Config>> HWComposer::getConfigs(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200334 PhysicalDisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700335 RETURN_IF_INVALID_DISPLAY(displayId, {});
336
Marin Shalamanov6e840172020-12-14 22:13:28 +0100337 // We cache the configs when the DisplayData is created on hotplug. If the configs need to
338 // change HWC will send a hotplug event which will recreate displayData.
339 return mDisplayData.at(displayId).configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700340}
341
Dominik Laskowski075d3172018-05-24 15:50:06 -0700342std::shared_ptr<const HWC2::Display::Config> HWComposer::getActiveConfig(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200343 PhysicalDisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700344 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
345
Dan Stoza9e56aa02015-11-02 13:00:03 -0800346 std::shared_ptr<const HWC2::Display::Config> config;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700347 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfig(&config);
Peiyong Line9d809e2020-04-14 13:10:48 -0700348 if (error == hal::Error::BAD_CONFIG) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700349 LOG_DISPLAY_ERROR(displayId, "No active config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800350 return nullptr;
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700351 }
352
353 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
354
355 if (!config) {
356 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800357 return nullptr;
358 }
359
360 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700361}
362
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800363// Composer 2.4
364
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200365DisplayConnectionType HWComposer::getDisplayConnectionType(PhysicalDisplayId displayId) const {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800366 RETURN_IF_INVALID_DISPLAY(displayId, DisplayConnectionType::Internal);
367 const auto& hwcDisplay = mDisplayData.at(displayId).hwcDisplay;
368
369 DisplayConnectionType type;
370 const auto error = hwcDisplay->getConnectionType(&type);
371
372 const auto FALLBACK_TYPE = hwcDisplay->getId() == mInternalHwcDisplayId
373 ? DisplayConnectionType::Internal
374 : DisplayConnectionType::External;
375
376 RETURN_IF_HWC_ERROR(error, displayId, FALLBACK_TYPE);
377 return type;
378}
379
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200380bool HWComposer::isVsyncPeriodSwitchSupported(PhysicalDisplayId displayId) const {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800381 RETURN_IF_INVALID_DISPLAY(displayId, false);
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800382 return mDisplayData.at(displayId).hwcDisplay->isVsyncPeriodSwitchSupported();
383}
384
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200385nsecs_t HWComposer::getDisplayVsyncPeriod(PhysicalDisplayId displayId) const {
Dominik Laskowski55c85402020-01-21 16:25:47 -0800386 RETURN_IF_INVALID_DISPLAY(displayId, 0);
387
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800388 nsecs_t vsyncPeriodNanos;
389 auto error = mDisplayData.at(displayId).hwcDisplay->getDisplayVsyncPeriod(&vsyncPeriodNanos);
Dominik Laskowski55c85402020-01-21 16:25:47 -0800390 RETURN_IF_HWC_ERROR(error, displayId, 0);
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800391 return vsyncPeriodNanos;
392}
393
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200394int HWComposer::getActiveConfigIndex(PhysicalDisplayId displayId) const {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700395 RETURN_IF_INVALID_DISPLAY(displayId, -1);
396
Lloyd Pique3c085a02018-05-09 19:38:32 -0700397 int index;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700398 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfigIndex(&index);
Peiyong Line9d809e2020-04-14 13:10:48 -0700399 if (error == hal::Error::BAD_CONFIG) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700400 LOG_DISPLAY_ERROR(displayId, "No active config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700401 return -1;
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700402 }
403
404 RETURN_IF_HWC_ERROR(error, displayId, -1);
405
406 if (index < 0) {
407 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700408 return -1;
409 }
410
411 return index;
412}
413
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200414std::vector<ui::ColorMode> HWComposer::getColorModes(PhysicalDisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700415 RETURN_IF_INVALID_DISPLAY(displayId, {});
416
Peiyong Linfd997e02018-03-28 15:29:00 -0700417 std::vector<ui::ColorMode> modes;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700418 auto error = mDisplayData.at(displayId).hwcDisplay->getColorModes(&modes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700419 RETURN_IF_HWC_ERROR(error, displayId, {});
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600420 return modes;
421}
422
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200423status_t HWComposer::setActiveColorMode(PhysicalDisplayId displayId, ui::ColorMode mode,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700424 ui::RenderIntent renderIntent) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700425 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Michael Wright28f24d02016-07-12 13:30:53 -0700426
427 auto& displayData = mDisplayData[displayId];
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700428 auto error = displayData.hwcDisplay->setColorMode(mode, renderIntent);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700429 RETURN_IF_HWC_ERROR_FOR(("setColorMode(" + decodeColorMode(mode) + ", " +
430 decodeRenderIntent(renderIntent) + ")")
431 .c_str(),
432 error, displayId, UNKNOWN_ERROR);
Michael Wright28f24d02016-07-12 13:30:53 -0700433
434 return NO_ERROR;
435}
436
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200437void HWComposer::setVsyncEnabled(PhysicalDisplayId displayId, hal::Vsync enabled) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700438 RETURN_IF_INVALID_DISPLAY(displayId);
439 auto& displayData = mDisplayData[displayId];
440
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200441 LOG_FATAL_IF(displayData.isVirtual, "%s: Invalid operation on virtual display with ID %s",
442 __FUNCTION__, to_string(displayId).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800443
Dan Stoza9e56aa02015-11-02 13:00:03 -0800444 // NOTE: we use our own internal lock here because we have to call
445 // into the HWC with the lock held, and we want to make sure
446 // that even if HWC blocks (which it shouldn't), it won't
447 // affect other threads.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800448 std::lock_guard lock(displayData.vsyncEnabledLock);
449 if (enabled == displayData.vsyncEnabled) {
450 return;
Colin Cross10fbdb62012-07-12 17:56:34 -0700451 }
Dominik Laskowski1af47932018-11-12 10:20:46 -0800452
453 ATRACE_CALL();
454 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
455 RETURN_IF_HWC_ERROR(error, displayId);
456
457 displayData.vsyncEnabled = enabled;
458
459 const auto tag = "HW_VSYNC_ON_" + to_string(displayId);
Peiyong Line9d809e2020-04-14 13:10:48 -0700460 ATRACE_INT(tag.c_str(), enabled == hal::Vsync::ENABLE ? 1 : 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700461}
462
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200463status_t HWComposer::setClientTarget(HalDisplayId displayId, uint32_t slot,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700464 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
465 ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700466 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Jesse Hall851cfe82013-03-20 13:44:00 -0700467
Dominik Laskowski34157762018-10-31 13:07:19 -0700468 ALOGV("%s for display %s", __FUNCTION__, to_string(displayId).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800469 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400470 auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700471 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Jesse Hall851cfe82013-03-20 13:44:00 -0700472 return NO_ERROR;
473}
474
Lloyd Pique66d68602019-02-13 14:23:31 -0800475status_t HWComposer::getDeviceCompositionChanges(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200476 HalDisplayId displayId, bool frameUsesClientComposition,
Lloyd Pique66d68602019-02-13 14:23:31 -0800477 std::optional<android::HWComposer::DeviceRequestedChanges>* outChanges) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800478 ATRACE_CALL();
479
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700480 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800481
482 auto& displayData = mDisplayData[displayId];
483 auto& hwcDisplay = displayData.hwcDisplay;
484 if (!hwcDisplay->isConnected()) {
485 return NO_ERROR;
486 }
487
488 uint32_t numTypes = 0;
489 uint32_t numRequests = 0;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700490
Peiyong Line9d809e2020-04-14 13:10:48 -0700491 hal::Error error = hal::Error::NONE;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700492
Chia-I Wu41b98d42017-12-11 11:04:36 -0800493 // First try to skip validate altogether when there is no client
494 // composition. When there is client composition, since we haven't
495 // rendered to the client target yet, we should not attempt to skip
496 // validate.
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700497 displayData.validateWasSkipped = false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800498 if (!frameUsesClientComposition) {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800499 sp<Fence> outPresentFence;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700500 uint32_t state = UINT32_MAX;
501 error = hwcDisplay->presentOrValidate(&numTypes, &numRequests, &outPresentFence , &state);
Peiyong Line9d809e2020-04-14 13:10:48 -0700502 if (!hasChangesError(error)) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700503 RETURN_IF_HWC_ERROR_FOR("presentOrValidate", error, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700504 }
505 if (state == 1) { //Present Succeeded.
Steven Thomas94e35b92017-07-26 18:48:28 -0700506 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700507 error = hwcDisplay->getReleaseFences(&releaseFences);
508 displayData.releaseFences = std::move(releaseFences);
509 displayData.lastPresentFence = outPresentFence;
510 displayData.validateWasSkipped = true;
511 displayData.presentError = error;
512 return NO_ERROR;
513 }
514 // Present failed but Validate ran.
515 } else {
516 error = hwcDisplay->validate(&numTypes, &numRequests);
517 }
518 ALOGV("SkipValidate failed, Falling back to SLOW validate/present");
Peiyong Line9d809e2020-04-14 13:10:48 -0700519 if (!hasChangesError(error)) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700520 RETURN_IF_HWC_ERROR_FOR("validate", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800521 }
522
Lloyd Pique66d68602019-02-13 14:23:31 -0800523 android::HWComposer::DeviceRequestedChanges::ChangedTypes changedTypes;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800524 changedTypes.reserve(numTypes);
525 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700526 RETURN_IF_HWC_ERROR_FOR("getChangedCompositionTypes", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800527
Peiyong Line9d809e2020-04-14 13:10:48 -0700528 auto displayRequests = static_cast<hal::DisplayRequest>(0);
Lloyd Pique66d68602019-02-13 14:23:31 -0800529 android::HWComposer::DeviceRequestedChanges::LayerRequests layerRequests;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800530 layerRequests.reserve(numRequests);
Lloyd Pique66d68602019-02-13 14:23:31 -0800531 error = hwcDisplay->getRequests(&displayRequests, &layerRequests);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700532 RETURN_IF_HWC_ERROR_FOR("getRequests", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800533
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700534 DeviceRequestedChanges::ClientTargetProperty clientTargetProperty;
535 error = hwcDisplay->getClientTargetProperty(&clientTargetProperty);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800536
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700537 outChanges->emplace(DeviceRequestedChanges{std::move(changedTypes), std::move(displayRequests),
538 std::move(layerRequests),
539 std::move(clientTargetProperty)});
Dan Stoza9e56aa02015-11-02 13:00:03 -0800540 error = hwcDisplay->acceptChanges();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700541 RETURN_IF_HWC_ERROR_FOR("acceptChanges", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800542
543 return NO_ERROR;
544}
545
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200546sp<Fence> HWComposer::getPresentFence(HalDisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700547 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700548 return mDisplayData.at(displayId).lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700549}
550
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200551sp<Fence> HWComposer::getLayerReleaseFence(HalDisplayId displayId, HWC2::Layer* layer) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700552 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Tim Murray2d3f8b82019-12-04 16:24:17 -0800553 const auto& displayFences = mDisplayData.at(displayId).releaseFences;
554 auto fence = displayFences.find(layer);
555 if (fence == displayFences.end()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800556 ALOGV("getLayerReleaseFence: Release fence not found");
557 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700558 }
Tim Murray2d3f8b82019-12-04 16:24:17 -0800559 return fence->second;
Riley Andrews03414a12014-07-01 14:22:59 -0700560}
561
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200562status_t HWComposer::presentAndGetReleaseFences(HalDisplayId displayId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800563 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700564
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700565 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700566
Dan Stoza9e56aa02015-11-02 13:00:03 -0800567 auto& displayData = mDisplayData[displayId];
568 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700569
570 if (displayData.validateWasSkipped) {
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700571 // explicitly flush all pending commands
Peiyong Line9d809e2020-04-14 13:10:48 -0700572 auto error = static_cast<hal::Error>(mComposer->executeCommands());
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800573 RETURN_IF_HWC_ERROR_FOR("executeCommands", error, displayId, UNKNOWN_ERROR);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700574 RETURN_IF_HWC_ERROR_FOR("present", displayData.presentError, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700575 return NO_ERROR;
576 }
577
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800578 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700579 RETURN_IF_HWC_ERROR_FOR("present", error, displayId, UNKNOWN_ERROR);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700580
Steven Thomas94e35b92017-07-26 18:48:28 -0700581 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800582 error = hwcDisplay->getReleaseFences(&releaseFences);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700583 RETURN_IF_HWC_ERROR_FOR("getReleaseFences", error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800584
585 displayData.releaseFences = std::move(releaseFences);
586
587 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700588}
589
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200590status_t HWComposer::setPowerMode(PhysicalDisplayId displayId, hal::PowerMode mode) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700591 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
592
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700593 const auto& displayData = mDisplayData[displayId];
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200594 LOG_FATAL_IF(displayData.isVirtual, "%s: Invalid operation on virtual display with ID %s",
595 __FUNCTION__, to_string(displayId).c_str());
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700596
Peiyong Line9d809e2020-04-14 13:10:48 -0700597 if (mode == hal::PowerMode::OFF) {
598 setVsyncEnabled(displayId, hal::Vsync::DISABLE);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800599 }
600
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700601 auto& hwcDisplay = displayData.hwcDisplay;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800602 switch (mode) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700603 case hal::PowerMode::OFF:
604 case hal::PowerMode::ON:
Dan Stoza9e56aa02015-11-02 13:00:03 -0800605 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
606 {
607 auto error = hwcDisplay->setPowerMode(mode);
Peiyong Line9d809e2020-04-14 13:10:48 -0700608 if (error != hal::Error::NONE) {
609 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(), error,
610 displayId);
Peiyong Lin306e4992018-05-07 16:18:22 -0700611 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700612 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800613 break;
Peiyong Line9d809e2020-04-14 13:10:48 -0700614 case hal::PowerMode::DOZE:
615 case hal::PowerMode::DOZE_SUSPEND:
Dan Stoza9e56aa02015-11-02 13:00:03 -0800616 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
617 {
618 bool supportsDoze = false;
619 auto error = hwcDisplay->supportsDoze(&supportsDoze);
Peiyong Line9d809e2020-04-14 13:10:48 -0700620 if (error != hal::Error::NONE) {
Peiyong Lin306e4992018-05-07 16:18:22 -0700621 LOG_HWC_ERROR("supportsDoze", error, displayId);
622 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700623
Dan Stoza9e56aa02015-11-02 13:00:03 -0800624 if (!supportsDoze) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700625 mode = hal::PowerMode::ON;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800626 }
627
628 error = hwcDisplay->setPowerMode(mode);
Peiyong Line9d809e2020-04-14 13:10:48 -0700629 if (error != hal::Error::NONE) {
630 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(), error,
631 displayId);
Peiyong Lin306e4992018-05-07 16:18:22 -0700632 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800633 }
634 break;
635 default:
636 ALOGV("setPowerMode: Not calling HWC");
637 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700638 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800639
640 return NO_ERROR;
641}
642
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800643status_t HWComposer::setActiveConfigWithConstraints(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200644 PhysicalDisplayId displayId, size_t configId,
645 const hal::VsyncPeriodChangeConstraints& constraints,
Peiyong Line9d809e2020-04-14 13:10:48 -0700646 hal::VsyncPeriodChangeTimeline* outTimeline) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700647 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800648
649 auto& displayData = mDisplayData[displayId];
Marin Shalamanov6e840172020-12-14 22:13:28 +0100650 if (configId >= displayData.configs.size()) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700651 LOG_DISPLAY_ERROR(displayId, ("Invalid config " + std::to_string(configId)).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800652 return BAD_INDEX;
653 }
654
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800655 auto error =
Marin Shalamanov6e840172020-12-14 22:13:28 +0100656 displayData.hwcDisplay->setActiveConfigWithConstraints(displayData.configs[configId],
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800657 constraints, outTimeline);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700658 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800659 return NO_ERROR;
660}
661
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200662status_t HWComposer::setColorTransform(HalDisplayId displayId, const mat4& transform) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700663 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700664
665 auto& displayData = mDisplayData[displayId];
666 bool isIdentity = transform == mat4();
Peiyong Line9d809e2020-04-14 13:10:48 -0700667 auto error = displayData.hwcDisplay
668 ->setColorTransform(transform,
669 isIdentity ? hal::ColorTransform::IDENTITY
670 : hal::ColorTransform::ARBITRARY_MATRIX);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700671 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700672 return NO_ERROR;
673}
674
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200675void HWComposer::disconnectDisplay(HalDisplayId displayId) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700676 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800677 auto& displayData = mDisplayData[displayId];
678
Dan Stoza9e56aa02015-11-02 13:00:03 -0800679 // If this was a virtual display, add its slot back for reuse by future
680 // virtual displays
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700681 if (displayData.isVirtual) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200682 mVirtualIdGenerator.markUnused(*HalVirtualDisplayId::tryCast(displayId));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800683 }
684
Dominik Laskowski7e045462018-05-30 13:02:02 -0700685 const auto hwcDisplayId = displayData.hwcDisplay->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700686
687 // TODO(b/74619554): Select internal/external display from remaining displays.
688 if (hwcDisplayId == mInternalHwcDisplayId) {
689 mInternalHwcDisplayId.reset();
690 } else if (hwcDisplayId == mExternalHwcDisplayId) {
691 mExternalHwcDisplayId.reset();
692 }
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800693 mPhysicalDisplayIdMap.erase(hwcDisplayId);
694 mDisplayData.erase(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800695}
696
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200697status_t HWComposer::setOutputBuffer(HalVirtualDisplayId displayId, const sp<Fence>& acquireFence,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700698 const sp<GraphicBuffer>& buffer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700699 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700700 const auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800701
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200702 LOG_FATAL_IF(!displayData.isVirtual, "%s: Invalid operation on physical display with ID %s",
703 __FUNCTION__, to_string(displayId).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800704
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700705 auto error = displayData.hwcDisplay->setOutputBuffer(buffer, acquireFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700706 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800707 return NO_ERROR;
708}
709
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200710void HWComposer::clearReleaseFences(HalDisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700711 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800712 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700713}
714
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200715status_t HWComposer::getHdrCapabilities(HalDisplayId displayId, HdrCapabilities* outCapabilities) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700716 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stozac4f471e2016-03-24 09:31:08 -0700717
718 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Peiyong Lin62665892018-04-16 11:07:44 -0700719 auto error = hwcDisplay->getHdrCapabilities(outCapabilities);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700720 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Peiyong Lin62665892018-04-16 11:07:44 -0700721 return NO_ERROR;
Dan Stozac4f471e2016-03-24 09:31:08 -0700722}
723
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200724int32_t HWComposer::getSupportedPerFrameMetadata(HalDisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700725 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700726 return mDisplayData.at(displayId).hwcDisplay->getSupportedPerFrameMetadata();
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700727}
728
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200729std::vector<ui::RenderIntent> HWComposer::getRenderIntents(HalDisplayId displayId,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700730 ui::ColorMode colorMode) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700731 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700732
733 std::vector<ui::RenderIntent> renderIntents;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700734 auto error = mDisplayData.at(displayId).hwcDisplay->getRenderIntents(colorMode, &renderIntents);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700735 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700736 return renderIntents;
737}
738
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200739mat4 HWComposer::getDataspaceSaturationMatrix(HalDisplayId displayId, ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700740 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700741
742 mat4 matrix;
743 auto error = mDisplayData[displayId].hwcDisplay->getDataspaceSaturationMatrix(dataspace,
744 &matrix);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700745 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700746 return matrix;
747}
748
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200749status_t HWComposer::getDisplayedContentSamplingAttributes(HalDisplayId displayId,
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700750 ui::PixelFormat* outFormat,
751 ui::Dataspace* outDataspace,
752 uint8_t* outComponentMask) {
753 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
754 const auto error =
755 mDisplayData[displayId]
756 .hwcDisplay->getDisplayedContentSamplingAttributes(outFormat, outDataspace,
757 outComponentMask);
Peiyong Line9d809e2020-04-14 13:10:48 -0700758 if (error == hal::Error::UNSUPPORTED) RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700759 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
760 return NO_ERROR;
761}
762
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200763status_t HWComposer::setDisplayContentSamplingEnabled(HalDisplayId displayId, bool enabled,
Kevin DuBois74e53772018-11-19 10:52:38 -0800764 uint8_t componentMask, uint64_t maxFrames) {
765 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
766 const auto error =
767 mDisplayData[displayId].hwcDisplay->setDisplayContentSamplingEnabled(enabled,
768 componentMask,
769 maxFrames);
770
Peiyong Line9d809e2020-04-14 13:10:48 -0700771 if (error == hal::Error::UNSUPPORTED) RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
772 if (error == hal::Error::BAD_PARAMETER) RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Kevin DuBois74e53772018-11-19 10:52:38 -0800773 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
774 return NO_ERROR;
775}
776
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200777status_t HWComposer::getDisplayedContentSample(HalDisplayId displayId, uint64_t maxFrames,
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700778 uint64_t timestamp, DisplayedFrameStats* outStats) {
779 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
780 const auto error =
781 mDisplayData[displayId].hwcDisplay->getDisplayedContentSample(maxFrames, timestamp,
782 outStats);
783 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
784 return NO_ERROR;
785}
786
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200787std::future<status_t> HWComposer::setDisplayBrightness(PhysicalDisplayId displayId,
788 float brightness) {
Dominik Laskowski4e2b71f2020-11-10 15:05:32 -0800789 RETURN_IF_INVALID_DISPLAY(displayId, ftl::yield<status_t>(BAD_INDEX));
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700790 auto& display = mDisplayData[displayId].hwcDisplay;
791
Dominik Laskowski4e2b71f2020-11-10 15:05:32 -0800792 return ftl::chain(display->setDisplayBrightness(brightness))
Dominik Laskowski5690bde2020-04-23 19:04:22 -0700793 .then([displayId](hal::Error error) -> status_t {
794 if (error == hal::Error::UNSUPPORTED) {
795 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
796 }
797 if (error == hal::Error::BAD_PARAMETER) {
798 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
799 }
800 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
801 return NO_ERROR;
802 });
Dan Gittik57e63c52019-01-18 16:37:54 +0000803}
804
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200805status_t HWComposer::setAutoLowLatencyMode(PhysicalDisplayId displayId, bool on) {
Galia Peycheva5492cb52019-10-30 14:13:16 +0100806 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
807 const auto error = mDisplayData[displayId].hwcDisplay->setAutoLowLatencyMode(on);
Peiyong Line9d809e2020-04-14 13:10:48 -0700808 if (error == hal::Error::UNSUPPORTED) {
Galia Peycheva5492cb52019-10-30 14:13:16 +0100809 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
810 }
Peiyong Line9d809e2020-04-14 13:10:48 -0700811 if (error == hal::Error::BAD_PARAMETER) {
Galia Peycheva5492cb52019-10-30 14:13:16 +0100812 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
813 }
814 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
815 return NO_ERROR;
816}
817
818status_t HWComposer::getSupportedContentTypes(
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200819 PhysicalDisplayId displayId, std::vector<hal::ContentType>* outSupportedContentTypes) {
Galia Peycheva5492cb52019-10-30 14:13:16 +0100820 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
821 const auto error =
822 mDisplayData[displayId].hwcDisplay->getSupportedContentTypes(outSupportedContentTypes);
823
824 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
825
826 return NO_ERROR;
827}
828
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200829status_t HWComposer::setContentType(PhysicalDisplayId displayId, hal::ContentType contentType) {
Galia Peycheva5492cb52019-10-30 14:13:16 +0100830 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
831 const auto error = mDisplayData[displayId].hwcDisplay->setContentType(contentType);
Peiyong Line9d809e2020-04-14 13:10:48 -0700832 if (error == hal::Error::UNSUPPORTED) {
Galia Peycheva5492cb52019-10-30 14:13:16 +0100833 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
834 }
Peiyong Line9d809e2020-04-14 13:10:48 -0700835 if (error == hal::Error::BAD_PARAMETER) {
Galia Peycheva5492cb52019-10-30 14:13:16 +0100836 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
837 }
838 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
839
840 return NO_ERROR;
841}
842
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800843const std::unordered_map<std::string, bool>& HWComposer::getSupportedLayerGenericMetadata() const {
844 return mSupportedLayerGenericMetadata;
845}
846
Yiwei Zhang5434a782018-12-05 18:06:32 -0800847void HWComposer::dump(std::string& result) const {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800848 result.append(mComposer->dumpDebugInfo());
Mathias Agopian83727852010-09-23 18:13:21 -0700849}
850
Marin Shalamanova524a092020-07-27 21:39:55 +0200851std::optional<PhysicalDisplayId> HWComposer::toPhysicalDisplayId(
852 hal::HWDisplayId hwcDisplayId) const {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700853 if (const auto it = mPhysicalDisplayIdMap.find(hwcDisplayId);
854 it != mPhysicalDisplayIdMap.end()) {
855 return it->second;
856 }
857 return {};
858}
859
Marin Shalamanova524a092020-07-27 21:39:55 +0200860std::optional<hal::HWDisplayId> HWComposer::fromPhysicalDisplayId(
861 PhysicalDisplayId displayId) const {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700862 if (const auto it = mDisplayData.find(displayId);
863 it != mDisplayData.end() && !it->second.isVirtual) {
864 return it->second.hwcDisplay->getId();
865 }
866 return {};
867}
868
Peiyong Line9d809e2020-04-14 13:10:48 -0700869bool HWComposer::shouldIgnoreHotplugConnect(hal::HWDisplayId hwcDisplayId,
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100870 bool hasDisplayIdentificationData) const {
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100871 if (mHasMultiDisplaySupport && !hasDisplayIdentificationData) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700872 ALOGE("Ignoring connection of display %" PRIu64 " without identification data",
873 hwcDisplayId);
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100874 return true;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700875 }
876
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100877 if (!mHasMultiDisplaySupport && mInternalHwcDisplayId && mExternalHwcDisplayId) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700878 ALOGE("Ignoring connection of tertiary display %" PRIu64, hwcDisplayId);
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100879 return true;
880 }
881
882 return false;
883}
884
Peiyong Line9d809e2020-04-14 13:10:48 -0700885std::optional<DisplayIdentificationInfo> HWComposer::onHotplugConnect(
886 hal::HWDisplayId hwcDisplayId) {
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100887 std::optional<DisplayIdentificationInfo> info;
888 if (const auto displayId = toPhysicalDisplayId(hwcDisplayId)) {
889 info = DisplayIdentificationInfo{.id = *displayId,
890 .name = std::string(),
891 .deviceProductInfo = std::nullopt};
Marin Shalamanovf8c63722020-10-06 13:11:21 +0200892 if (mUpdateDeviceProductInfoOnHotplugReconnect) {
893 uint8_t port;
894 DisplayIdentificationData data;
895 getDisplayIdentificationData(hwcDisplayId, &port, &data);
896 if (auto newInfo = parseDisplayIdentificationData(port, data)) {
897 info->deviceProductInfo = std::move(newInfo->deviceProductInfo);
898 } else {
899 ALOGE("Failed to parse identification data for display %" PRIu64, hwcDisplayId);
900 }
901 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700902 } else {
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100903 uint8_t port;
904 DisplayIdentificationData data;
905 const bool hasDisplayIdentificationData =
906 getDisplayIdentificationData(hwcDisplayId, &port, &data);
907 if (mPhysicalDisplayIdMap.empty()) {
908 mHasMultiDisplaySupport = hasDisplayIdentificationData;
909 ALOGI("Switching to %s multi-display mode",
910 mHasMultiDisplaySupport ? "generalized" : "legacy");
911 }
912
913 if (shouldIgnoreHotplugConnect(hwcDisplayId, hasDisplayIdentificationData)) {
914 return {};
915 }
916
917 info = [this, hwcDisplayId, &port, &data, hasDisplayIdentificationData] {
918 const bool isPrimary = !mInternalHwcDisplayId;
919 if (mHasMultiDisplaySupport) {
920 if (const auto info = parseDisplayIdentificationData(port, data)) {
921 return *info;
922 }
923 ALOGE("Failed to parse identification data for display %" PRIu64, hwcDisplayId);
924 } else {
925 ALOGW_IF(hasDisplayIdentificationData,
926 "Ignoring identification data for display %" PRIu64, hwcDisplayId);
Peiyong Lin65248e02020-04-18 21:15:07 -0700927 port = isPrimary ? LEGACY_DISPLAY_TYPE_PRIMARY : LEGACY_DISPLAY_TYPE_EXTERNAL;
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100928 }
929
Marin Shalamanova524a092020-07-27 21:39:55 +0200930 return DisplayIdentificationInfo{.id = PhysicalDisplayId::fromPort(port),
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100931 .name = isPrimary ? "Internal display"
932 : "External display",
933 .deviceProductInfo = std::nullopt};
934 }();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700935 }
936
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100937 if (!isConnected(info->id)) {
938 allocatePhysicalDisplay(hwcDisplayId, info->id);
939 }
940 return info;
941}
942
943std::optional<DisplayIdentificationInfo> HWComposer::onHotplugDisconnect(
Peiyong Line9d809e2020-04-14 13:10:48 -0700944 hal::HWDisplayId hwcDisplayId) {
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100945 const auto displayId = toPhysicalDisplayId(hwcDisplayId);
946 if (!displayId) {
947 ALOGE("Ignoring disconnection of invalid HWC display %" PRIu64, hwcDisplayId);
948 return {};
Dominik Laskowski075d3172018-05-24 15:50:06 -0700949 }
950
Marin Shalamanovbdd59152020-02-14 15:30:06 +0100951 // The display will later be destroyed by a call to
952 // destroyDisplay(). For now we just mark it disconnected.
953 if (isConnected(*displayId)) {
954 mDisplayData[*displayId].hwcDisplay->setConnected(false);
955 } else {
956 ALOGW("Attempted to disconnect unknown display %" PRIu64, hwcDisplayId);
957 }
958 // The cleanup of Disconnect is handled through HWComposer::disconnectDisplay
959 // via SurfaceFlinger's onHotplugReceived callback handling
960 return DisplayIdentificationInfo{.id = *displayId,
961 .name = std::string(),
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200962 .deviceProductInfo = std::nullopt};
Steven Thomas6e8f7062017-11-22 14:15:29 -0800963}
964
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800965void HWComposer::loadCapabilities() {
Peiyong Line9d809e2020-04-14 13:10:48 -0700966 static_assert(sizeof(hal::Capability) == sizeof(int32_t), "Capability size has changed");
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800967 auto capabilities = mComposer->getCapabilities();
968 for (auto capability : capabilities) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700969 mCapabilities.emplace(static_cast<hal::Capability>(capability));
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800970 }
971}
972
Lloyd Pique4603f3c2020-02-11 12:06:56 -0800973void HWComposer::loadLayerMetadataSupport() {
974 mSupportedLayerGenericMetadata.clear();
975
976 std::vector<Hwc2::IComposerClient::LayerGenericMetadataKey> supportedMetadataKeyInfo;
977 const auto error = mComposer->getLayerGenericMetadataKeys(&supportedMetadataKeyInfo);
978 if (error != hardware::graphics::composer::V2_4::Error::NONE) {
979 ALOGE("%s: %s failed: %s (%d)", __FUNCTION__, "getLayerGenericMetadataKeys",
980 toString(error).c_str(), static_cast<int32_t>(error));
981 return;
982 }
983
984 for (const auto& [name, mandatory] : supportedMetadataKeyInfo) {
985 mSupportedLayerGenericMetadata.emplace(name, mandatory);
986 }
987}
988
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800989uint32_t HWComposer::getMaxVirtualDisplayCount() const {
990 return mComposer->getMaxVirtualDisplayCount();
991}
992
Lloyd Pique441d5042018-10-18 16:49:51 -0700993} // namespace impl
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700994} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800995
996// TODO(b/129481165): remove the #pragma below and fix conversion issues
997#pragma clang diagnostic pop // ignored "-Wconversion"