blob: 1960f431a848c1b7e9ff481fbf865501dcdc834c [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
Dan Stoza9e56aa02015-11-02 13:00:03 -080017// #define LOG_NDEBUG 0
18
19#undef LOG_TAG
20#define LOG_TAG "HWComposer"
Mathias Agopian2965b262012-04-08 15:13:32 -070021#define ATRACE_TAG ATRACE_TAG_GRAPHICS
22
Lloyd Pique66d68602019-02-13 14:23:31 -080023#include "HWComposer.h"
24
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080025#include <compositionengine/Output.h>
26#include <compositionengine/OutputLayer.h>
27#include <compositionengine/impl/OutputLayerCompositionState.h>
28#include <log/log.h>
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070029#include <ui/DebugUtils.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070030#include <ui/GraphicBuffer.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080031#include <utils/Errors.h>
32#include <utils/Trace.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070033
Lloyd Pique66d68602019-02-13 14:23:31 -080034#include "../Layer.h" // needed only for debugging
Mathias Agopian33ceeb32013-04-01 16:54:58 -070035#include "../SurfaceFlinger.h"
Lloyd Pique66d68602019-02-13 14:23:31 -080036#include "ComposerHal.h"
37#include "HWC2.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070038
Dominik Laskowskic1f18f62018-06-13 15:17:55 -070039#define LOG_HWC_DISPLAY_ERROR(hwcDisplayId, msg) \
40 ALOGE("%s failed for HWC display %" PRIu64 ": %s", __FUNCTION__, hwcDisplayId, msg)
41
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070042#define LOG_DISPLAY_ERROR(displayId, msg) \
Dominik Laskowski34157762018-10-31 13:07:19 -070043 ALOGE("%s failed for display %s: %s", __FUNCTION__, to_string(displayId).c_str(), msg)
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070044
Dominik Laskowski34157762018-10-31 13:07:19 -070045#define LOG_HWC_ERROR(what, error, displayId) \
46 ALOGE("%s: %s failed for display %s: %s (%d)", __FUNCTION__, what, \
47 to_string(displayId).c_str(), to_string(error).c_str(), static_cast<int32_t>(error))
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070048
49#define RETURN_IF_INVALID_DISPLAY(displayId, ...) \
50 do { \
Dominik Laskowski075d3172018-05-24 15:50:06 -070051 if (mDisplayData.count(displayId) == 0) { \
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070052 LOG_DISPLAY_ERROR(displayId, "Invalid display"); \
53 return __VA_ARGS__; \
54 } \
55 } while (false)
56
57#define RETURN_IF_HWC_ERROR_FOR(what, error, displayId, ...) \
58 do { \
59 if (error != HWC2::Error::None) { \
60 LOG_HWC_ERROR(what, error, displayId); \
61 return __VA_ARGS__; \
62 } \
63 } while (false)
64
65#define RETURN_IF_HWC_ERROR(error, displayId, ...) \
66 RETURN_IF_HWC_ERROR_FOR(__FUNCTION__, error, displayId, __VA_ARGS__)
67
Peiyong Linbdd08cc2019-12-17 21:35:14 -080068namespace {
69
70using android::hardware::Return;
71using android::hardware::Void;
72
73class ComposerCallbackBridge : public android::Hwc2::IComposerCallback {
74public:
75 ComposerCallbackBridge(HWC2::ComposerCallback* callback, int32_t sequenceId,
76 bool vsyncSwitchingSupported)
77 : mCallback(callback),
78 mSequenceId(sequenceId),
79 mVsyncSwitchingSupported(vsyncSwitchingSupported) {}
80
81 android::hardware::Return<void> onHotplug(
82 android::Hwc2::Display display,
83 android::Hwc2::IComposerCallback::Connection conn) override {
84 HWC2::Connection connection = static_cast<HWC2::Connection>(conn);
85 mCallback->onHotplugReceived(mSequenceId, display, connection);
86 return android::hardware::Void();
87 }
88
89 android::hardware::Return<void> onRefresh(android::Hwc2::Display display) override {
90 mCallback->onRefreshReceived(mSequenceId, display);
91 return android::hardware::Void();
92 }
93
94 android::hardware::Return<void> onVsync(android::Hwc2::Display display,
95 int64_t timestamp) override {
96 if (!mVsyncSwitchingSupported) {
97 mCallback->onVsyncReceived(mSequenceId, display, timestamp, std::nullopt);
98 } else {
99 ALOGW("Unexpected onVsync callback on composer >= 2.4, ignoring.");
100 }
101 return android::hardware::Void();
102 }
103
104 android::hardware::Return<void> onVsync_2_4(
105 android::Hwc2::Display display, int64_t timestamp,
106 android::Hwc2::VsyncPeriodNanos vsyncPeriodNanos) override {
107 if (mVsyncSwitchingSupported) {
108 // TODO(b/140201379): use vsyncPeriodNanos in the new DispSync
109 mCallback->onVsyncReceived(mSequenceId, display, timestamp,
110 std::make_optional(vsyncPeriodNanos));
111 } else {
112 ALOGW("Unexpected onVsync_2_4 callback on composer <= 2.3, ignoring.");
113 }
114 return android::hardware::Void();
115 }
116
117 android::hardware::Return<void> onVsyncPeriodTimingChanged(
118 android::Hwc2::Display display,
119 const android::Hwc2::VsyncPeriodChangeTimeline& updatedTimeline) override {
120 hwc_vsync_period_change_timeline_t timeline;
121 timeline.newVsyncAppliedTimeNanos = updatedTimeline.newVsyncAppliedTimeNanos;
122 timeline.refreshRequired = updatedTimeline.refreshRequired;
123 timeline.refreshTimeNanos = updatedTimeline.refreshTimeNanos;
124 mCallback->onVsyncPeriodTimingChangedReceived(mSequenceId, display, timeline);
125 return android::hardware::Void();
126 }
127
Ady Abrahamb0433bc2020-01-08 17:31:06 -0800128 android::hardware::Return<void> onSeamlessPossible(android::Hwc2::Display display) override {
129 mCallback->onSeamlessPossible(mSequenceId, display);
130 return android::hardware::Void();
131 }
132
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800133private:
134 HWC2::ComposerCallback* mCallback;
135 const int32_t mSequenceId;
136 const bool mVsyncSwitchingSupported;
137};
138
139} // namespace
140
Mathias Agopiana350ff92010-08-10 17:14:02 -0700141namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -0700142
Lloyd Pique441d5042018-10-18 16:49:51 -0700143HWComposer::~HWComposer() = default;
144
145namespace impl {
146
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800147HWComposer::HWComposer(std::unique_ptr<Hwc2::Composer> composer) : mComposer(std::move(composer)) {
148 loadCapabilities();
149}
150
151HWComposer::HWComposer(const std::string& composerServiceName)
152 : mComposer(std::make_unique<Hwc2::impl::Composer>(composerServiceName)) {
153 loadCapabilities();
154}
Mathias Agopianbef42c52013-08-21 17:45:46 -0700155
Dominik Laskowskib04f98a2018-11-07 21:07:16 -0800156HWComposer::~HWComposer() {
157 mDisplayData.clear();
158}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800159
Steven Thomas94e35b92017-07-26 18:48:28 -0700160void HWComposer::registerCallback(HWC2::ComposerCallback* callback,
161 int32_t sequenceId) {
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
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700173bool HWComposer::getDisplayIdentificationData(hwc2_display_t hwcDisplayId, uint8_t* outPort,
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700174 DisplayIdentificationData* outData) const {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800175 const auto error = static_cast<HWC2::Error>(
176 mComposer->getDisplayIdentificationData(hwcDisplayId, outPort, outData));
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700177 if (error != HWC2::Error::None) {
Chia-I Wud0aff9d2018-06-21 13:39:09 +0800178 if (error != HWC2::Error::Unsupported) {
179 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 Linbdd08cc2019-12-17 21:35:14 -0800186bool HWComposer::hasCapability(HWC2::Capability capability) const {
187 return mCapabilities.count(capability) > 0;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700188}
189
Peiyong Lined531a32018-10-26 18:27:56 -0700190bool HWComposer::hasDisplayCapability(const std::optional<DisplayId>& displayId,
191 HWC2::DisplayCapability capability) const {
192 if (!displayId) {
Peiyong Lindf65fd22019-01-03 15:17:05 -0800193 // Checkout global capabilities for displays without a corresponding HWC display.
194 if (capability == HWC2::DisplayCapability::SkipClientColorTransform) {
195 return hasCapability(HWC2::Capability::SkipClientColorTransform);
196 }
Peiyong Lined531a32018-10-26 18:27:56 -0700197 return false;
198 }
199 RETURN_IF_INVALID_DISPLAY(*displayId, false);
200 return mDisplayData.at(*displayId).hwcDisplay->getCapabilities().count(capability) > 0;
201}
202
Dominik Laskowski075d3172018-05-24 15:50:06 -0700203std::optional<DisplayIdentificationInfo> HWComposer::onHotplug(hwc2_display_t hwcDisplayId,
204 HWC2::Connection connection) {
205 std::optional<DisplayIdentificationInfo> info;
Lloyd Pique715a2c12017-12-14 17:18:08 -0800206
Dominik Laskowski075d3172018-05-24 15:50:06 -0700207 if (const auto displayId = toPhysicalDisplayId(hwcDisplayId)) {
208 info = DisplayIdentificationInfo{*displayId, std::string()};
209 } else {
210 if (connection == HWC2::Connection::Disconnected) {
211 ALOGE("Ignoring disconnection of invalid HWC display %" PRIu64, hwcDisplayId);
212 return {};
Lloyd Pique438e9e72018-09-04 18:06:08 -0700213 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700214
215 info = onHotplugConnect(hwcDisplayId);
216 if (!info) return {};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700217 }
218
Dominik Laskowski34157762018-10-31 13:07:19 -0700219 ALOGV("%s: %s %s display %s with HWC ID %" PRIu64, __FUNCTION__, to_string(connection).c_str(),
220 hwcDisplayId == mInternalHwcDisplayId ? "internal" : "external",
221 to_string(info->id).c_str(), hwcDisplayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700222
Lloyd Pique715a2c12017-12-14 17:18:08 -0800223 if (connection == HWC2::Connection::Connected) {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800224 auto& displayData = mDisplayData[info->id];
225 // If we get a hotplug connected event for a display we already have,
226 // destroy the display and recreate it. This will force us to requery
227 // the display params and recreate all layers on that display.
228 if (displayData.hwcDisplay != nullptr && displayData.hwcDisplay->isConnected()) {
229 ALOGI("Hotplug connecting an already connected display."
230 " Clearing old display state.");
231 }
232 displayData.hwcDisplay.reset();
233 auto newDisplay =
234 std::make_unique<HWC2::impl::Display>(*mComposer.get(), mCapabilities, hwcDisplayId,
235 HWC2::DisplayType::Physical);
236 newDisplay->setConnected(true);
237 displayData.hwcDisplay = std::move(newDisplay);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700238 mPhysicalDisplayIdMap[hwcDisplayId] = info->id;
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800239 } else if (connection == HWC2::Connection::Disconnected) {
240 // The display will later be destroyed by a call to
241 // destroyDisplay(). For now we just mark it disconnected.
242 auto& displayData = mDisplayData[info->id];
243 if (displayData.hwcDisplay) {
244 displayData.hwcDisplay->setConnected(false);
245 } else {
246 ALOGW("Attempted to disconnect unknown display %" PRIu64, hwcDisplayId);
247 }
248 // The cleanup of Disconnect is handled through HWComposer::disconnectDisplay
249 // via SurfaceFlinger's onHotplugReceived callback handling
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700250 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700251
Dominik Laskowski075d3172018-05-24 15:50:06 -0700252 return info;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700253}
254
Dominik Laskowski075d3172018-05-24 15:50:06 -0700255bool HWComposer::onVsync(hwc2_display_t hwcDisplayId, int64_t timestamp) {
256 const auto displayId = toPhysicalDisplayId(hwcDisplayId);
257 if (!displayId) {
258 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, "Invalid HWC display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700259 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700260 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700261
Dominik Laskowski075d3172018-05-24 15:50:06 -0700262 RETURN_IF_INVALID_DISPLAY(*displayId, false);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700263
Dominik Laskowski1af47932018-11-12 10:20:46 -0800264 auto& displayData = mDisplayData[*displayId];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700265 if (displayData.isVirtual) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700266 LOG_DISPLAY_ERROR(*displayId, "Invalid operation on virtual display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700267 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700268 }
269
Dan Stoza9e56aa02015-11-02 13:00:03 -0800270 {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800271 std::lock_guard lock(displayData.lastHwVsyncLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700272
Dan Stoza9e56aa02015-11-02 13:00:03 -0800273 // There have been reports of HWCs that signal several vsync events
274 // with the same timestamp when turning the display off and on. This
275 // is a bug in the HWC implementation, but filter the extra events
276 // out here so they don't cause havoc downstream.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800277 if (timestamp == displayData.lastHwVsync) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700278 ALOGW("Ignoring duplicate VSYNC event from HWC for display %s (t=%" PRId64 ")",
279 to_string(*displayId).c_str(), timestamp);
Steven Thomas94e35b92017-07-26 18:48:28 -0700280 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800281 }
282
Dominik Laskowski1af47932018-11-12 10:20:46 -0800283 displayData.lastHwVsync = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700284 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800285
Dominik Laskowski34157762018-10-31 13:07:19 -0700286 const auto tag = "HW_VSYNC_" + to_string(*displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800287 ATRACE_INT(tag.c_str(), displayData.vsyncTraceToggle);
288 displayData.vsyncTraceToggle = !displayData.vsyncTraceToggle;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800289
Steven Thomas94e35b92017-07-26 18:48:28 -0700290 return true;
Jesse Hall1c569c42013-04-05 13:44:52 -0700291}
292
Dominik Laskowski075d3172018-05-24 15:50:06 -0700293std::optional<DisplayId> HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
294 ui::PixelFormat* format) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800295 if (mRemainingHwcVirtualDisplays == 0) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700296 ALOGE("%s: No remaining virtual displays", __FUNCTION__);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700297 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700298 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700299
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800300 if (SurfaceFlinger::maxVirtualDisplaySize != 0 &&
301 (width > SurfaceFlinger::maxVirtualDisplaySize ||
302 height > SurfaceFlinger::maxVirtualDisplaySize)) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700303 ALOGE("%s: Display size %ux%u exceeds maximum dimension of %" PRIu64, __FUNCTION__, width,
304 height, SurfaceFlinger::maxVirtualDisplaySize);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700305 return {};
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800306 }
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800307 hwc2_display_t hwcDisplayId = 0;
308 const auto error = static_cast<HWC2::Error>(
309 mComposer->createVirtualDisplay(width, height, format, &hwcDisplayId));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800310 if (error != HWC2::Error::None) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700311 ALOGE("%s: Failed to create HWC virtual display", __FUNCTION__);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700312 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700313 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800314
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800315 auto display = std::make_unique<HWC2::impl::Display>(*mComposer.get(), mCapabilities,
316 hwcDisplayId, HWC2::DisplayType::Virtual);
317 display->setConnected(true);
318
Dominik Laskowski075d3172018-05-24 15:50:06 -0700319 DisplayId displayId;
320 if (mFreeVirtualDisplayIds.empty()) {
321 displayId = getVirtualDisplayId(mNextVirtualDisplayId++);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800322 } else {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700323 displayId = *mFreeVirtualDisplayIds.begin();
324 mFreeVirtualDisplayIds.erase(displayId);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700325 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800326
Dominik Laskowski075d3172018-05-24 15:50:06 -0700327 auto& displayData = mDisplayData[displayId];
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800328 displayData.hwcDisplay = std::move(display);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700329 displayData.isVirtual = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800330
331 --mRemainingHwcVirtualDisplays;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700332 return displayId;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700333}
334
Dominik Laskowski075d3172018-05-24 15:50:06 -0700335HWC2::Layer* HWComposer::createLayer(DisplayId 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
Dominik Laskowski075d3172018-05-24 15:50:06 -0700344void HWComposer::destroyLayer(DisplayId 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
Dominik Laskowski075d3172018-05-24 15:50:06 -0700351nsecs_t HWComposer::getRefreshTimestamp(DisplayId displayId) const {
352 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
Dominik Laskowski075d3172018-05-24 15:50:06 -0700363bool HWComposer::isConnected(DisplayId 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
Dominik Laskowski075d3172018-05-24 15:50:06 -0700368std::vector<std::shared_ptr<const HWC2::Display::Config>> HWComposer::getConfigs(
369 DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700370 RETURN_IF_INVALID_DISPLAY(displayId, {});
371
Dominik Laskowski075d3172018-05-24 15:50:06 -0700372 const auto& displayData = mDisplayData.at(displayId);
373 auto configs = displayData.hwcDisplay->getConfigs();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800374 if (displayData.configMap.empty()) {
375 for (size_t i = 0; i < configs.size(); ++i) {
376 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700377 }
378 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800379 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700380}
381
Dominik Laskowski075d3172018-05-24 15:50:06 -0700382std::shared_ptr<const HWC2::Display::Config> HWComposer::getActiveConfig(
383 DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700384 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
385
Dan Stoza9e56aa02015-11-02 13:00:03 -0800386 std::shared_ptr<const HWC2::Display::Config> config;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700387 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfig(&config);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800388 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700389 LOG_DISPLAY_ERROR(displayId, "No active config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800390 return nullptr;
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700391 }
392
393 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
394
395 if (!config) {
396 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800397 return nullptr;
398 }
399
400 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700401}
402
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800403// Composer 2.4
404
405bool HWComposer::isVsyncPeriodSwitchSupported(DisplayId displayId) const {
406 return mDisplayData.at(displayId).hwcDisplay->isVsyncPeriodSwitchSupported();
407}
408
409nsecs_t HWComposer::getDisplayVsyncPeriod(DisplayId displayId) const {
410 nsecs_t vsyncPeriodNanos;
411 auto error = mDisplayData.at(displayId).hwcDisplay->getDisplayVsyncPeriod(&vsyncPeriodNanos);
412 if (error != HWC2::Error::None) {
413 LOG_DISPLAY_ERROR(displayId, "Failed to get Vsync Period");
414 return 0;
415 }
416
417 return vsyncPeriodNanos;
418}
419
Dominik Laskowski075d3172018-05-24 15:50:06 -0700420int HWComposer::getActiveConfigIndex(DisplayId displayId) const {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700421 RETURN_IF_INVALID_DISPLAY(displayId, -1);
422
Lloyd Pique3c085a02018-05-09 19:38:32 -0700423 int index;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700424 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfigIndex(&index);
Lloyd Pique3c085a02018-05-09 19:38:32 -0700425 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700426 LOG_DISPLAY_ERROR(displayId, "No active config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700427 return -1;
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700428 }
429
430 RETURN_IF_HWC_ERROR(error, displayId, -1);
431
432 if (index < 0) {
433 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700434 return -1;
435 }
436
437 return index;
438}
439
Dominik Laskowski075d3172018-05-24 15:50:06 -0700440std::vector<ui::ColorMode> HWComposer::getColorModes(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700441 RETURN_IF_INVALID_DISPLAY(displayId, {});
442
Peiyong Linfd997e02018-03-28 15:29:00 -0700443 std::vector<ui::ColorMode> modes;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700444 auto error = mDisplayData.at(displayId).hwcDisplay->getColorModes(&modes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700445 RETURN_IF_HWC_ERROR(error, displayId, {});
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600446 return modes;
447}
448
Dominik Laskowski075d3172018-05-24 15:50:06 -0700449status_t HWComposer::setActiveColorMode(DisplayId displayId, ui::ColorMode mode,
450 ui::RenderIntent renderIntent) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700451 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Michael Wright28f24d02016-07-12 13:30:53 -0700452
453 auto& displayData = mDisplayData[displayId];
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700454 auto error = displayData.hwcDisplay->setColorMode(mode, renderIntent);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700455 RETURN_IF_HWC_ERROR_FOR(("setColorMode(" + decodeColorMode(mode) + ", " +
456 decodeRenderIntent(renderIntent) + ")")
457 .c_str(),
458 error, displayId, UNKNOWN_ERROR);
Michael Wright28f24d02016-07-12 13:30:53 -0700459
460 return NO_ERROR;
461}
462
Dominik Laskowski075d3172018-05-24 15:50:06 -0700463void HWComposer::setVsyncEnabled(DisplayId displayId, HWC2::Vsync enabled) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700464 RETURN_IF_INVALID_DISPLAY(displayId);
465 auto& displayData = mDisplayData[displayId];
466
467 if (displayData.isVirtual) {
468 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800469 return;
470 }
471
Dan Stoza9e56aa02015-11-02 13:00:03 -0800472 // NOTE: we use our own internal lock here because we have to call
473 // into the HWC with the lock held, and we want to make sure
474 // that even if HWC blocks (which it shouldn't), it won't
475 // affect other threads.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800476 std::lock_guard lock(displayData.vsyncEnabledLock);
477 if (enabled == displayData.vsyncEnabled) {
478 return;
Colin Cross10fbdb62012-07-12 17:56:34 -0700479 }
Dominik Laskowski1af47932018-11-12 10:20:46 -0800480
481 ATRACE_CALL();
482 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
483 RETURN_IF_HWC_ERROR(error, displayId);
484
485 displayData.vsyncEnabled = enabled;
486
487 const auto tag = "HW_VSYNC_ON_" + to_string(displayId);
488 ATRACE_INT(tag.c_str(), enabled == HWC2::Vsync::Enable ? 1 : 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700489}
490
Dominik Laskowski075d3172018-05-24 15:50:06 -0700491status_t HWComposer::setClientTarget(DisplayId displayId, uint32_t slot,
492 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
493 ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700494 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Jesse Hall851cfe82013-03-20 13:44:00 -0700495
Dominik Laskowski34157762018-10-31 13:07:19 -0700496 ALOGV("%s for display %s", __FUNCTION__, to_string(displayId).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800497 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400498 auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700499 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Jesse Hall851cfe82013-03-20 13:44:00 -0700500 return NO_ERROR;
501}
502
Lloyd Pique66d68602019-02-13 14:23:31 -0800503status_t HWComposer::getDeviceCompositionChanges(
504 DisplayId displayId, bool frameUsesClientComposition,
505 std::optional<android::HWComposer::DeviceRequestedChanges>* outChanges) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800506 ATRACE_CALL();
507
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700508 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800509
510 auto& displayData = mDisplayData[displayId];
511 auto& hwcDisplay = displayData.hwcDisplay;
512 if (!hwcDisplay->isConnected()) {
513 return NO_ERROR;
514 }
515
516 uint32_t numTypes = 0;
517 uint32_t numRequests = 0;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700518
519 HWC2::Error error = HWC2::Error::None;
520
Chia-I Wu41b98d42017-12-11 11:04:36 -0800521 // First try to skip validate altogether when there is no client
522 // composition. When there is client composition, since we haven't
523 // rendered to the client target yet, we should not attempt to skip
524 // validate.
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700525 displayData.validateWasSkipped = false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800526 if (!frameUsesClientComposition) {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800527 sp<Fence> outPresentFence;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700528 uint32_t state = UINT32_MAX;
529 error = hwcDisplay->presentOrValidate(&numTypes, &numRequests, &outPresentFence , &state);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700530 if (error != HWC2::Error::HasChanges) {
531 RETURN_IF_HWC_ERROR_FOR("presentOrValidate", error, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700532 }
533 if (state == 1) { //Present Succeeded.
Steven Thomas94e35b92017-07-26 18:48:28 -0700534 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700535 error = hwcDisplay->getReleaseFences(&releaseFences);
536 displayData.releaseFences = std::move(releaseFences);
537 displayData.lastPresentFence = outPresentFence;
538 displayData.validateWasSkipped = true;
539 displayData.presentError = error;
540 return NO_ERROR;
541 }
542 // Present failed but Validate ran.
543 } else {
544 error = hwcDisplay->validate(&numTypes, &numRequests);
545 }
546 ALOGV("SkipValidate failed, Falling back to SLOW validate/present");
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700547 if (error != HWC2::Error::HasChanges) {
548 RETURN_IF_HWC_ERROR_FOR("validate", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800549 }
550
Lloyd Pique66d68602019-02-13 14:23:31 -0800551 android::HWComposer::DeviceRequestedChanges::ChangedTypes changedTypes;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800552 changedTypes.reserve(numTypes);
553 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700554 RETURN_IF_HWC_ERROR_FOR("getChangedCompositionTypes", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800555
Lloyd Pique66d68602019-02-13 14:23:31 -0800556 auto displayRequests = static_cast<HWC2::DisplayRequest>(0);
557 android::HWComposer::DeviceRequestedChanges::LayerRequests layerRequests;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800558 layerRequests.reserve(numRequests);
Lloyd Pique66d68602019-02-13 14:23:31 -0800559 error = hwcDisplay->getRequests(&displayRequests, &layerRequests);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700560 RETURN_IF_HWC_ERROR_FOR("getRequests", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800561
Lloyd Pique66d68602019-02-13 14:23:31 -0800562 outChanges->emplace(DeviceRequestedChanges{std::move(changedTypes), std::move(displayRequests),
563 std::move(layerRequests)});
Dan Stoza9e56aa02015-11-02 13:00:03 -0800564
565 error = hwcDisplay->acceptChanges();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700566 RETURN_IF_HWC_ERROR_FOR("acceptChanges", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800567
568 return NO_ERROR;
569}
570
Dominik Laskowski075d3172018-05-24 15:50:06 -0700571sp<Fence> HWComposer::getPresentFence(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700572 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700573 return mDisplayData.at(displayId).lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700574}
575
Dominik Laskowski075d3172018-05-24 15:50:06 -0700576sp<Fence> HWComposer::getLayerReleaseFence(DisplayId displayId, HWC2::Layer* layer) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700577 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700578 auto displayFences = mDisplayData.at(displayId).releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800579 if (displayFences.count(layer) == 0) {
580 ALOGV("getLayerReleaseFence: Release fence not found");
581 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700582 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800583 return displayFences[layer];
Riley Andrews03414a12014-07-01 14:22:59 -0700584}
585
Dominik Laskowski075d3172018-05-24 15:50:06 -0700586status_t HWComposer::presentAndGetReleaseFences(DisplayId displayId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800587 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700588
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700589 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700590
Dan Stoza9e56aa02015-11-02 13:00:03 -0800591 auto& displayData = mDisplayData[displayId];
592 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700593
594 if (displayData.validateWasSkipped) {
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700595 // explicitly flush all pending commands
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800596 auto error = static_cast<HWC2::Error>(mComposer->executeCommands());
597 RETURN_IF_HWC_ERROR_FOR("executeCommands", error, displayId, UNKNOWN_ERROR);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700598 RETURN_IF_HWC_ERROR_FOR("present", displayData.presentError, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700599 return NO_ERROR;
600 }
601
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800602 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700603 RETURN_IF_HWC_ERROR_FOR("present", error, displayId, UNKNOWN_ERROR);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700604
Steven Thomas94e35b92017-07-26 18:48:28 -0700605 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800606 error = hwcDisplay->getReleaseFences(&releaseFences);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700607 RETURN_IF_HWC_ERROR_FOR("getReleaseFences", error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800608
609 displayData.releaseFences = std::move(releaseFences);
610
611 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700612}
613
Dominik Laskowski075d3172018-05-24 15:50:06 -0700614status_t HWComposer::setPowerMode(DisplayId displayId, int32_t intMode) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700615 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
616
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700617 const auto& displayData = mDisplayData[displayId];
618 if (displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700619 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
620 return INVALID_OPERATION;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800621 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700622
Dan Stoza9e56aa02015-11-02 13:00:03 -0800623 auto mode = static_cast<HWC2::PowerMode>(intMode);
624 if (mode == HWC2::PowerMode::Off) {
625 setVsyncEnabled(displayId, HWC2::Vsync::Disable);
626 }
627
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700628 auto& hwcDisplay = displayData.hwcDisplay;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800629 switch (mode) {
630 case HWC2::PowerMode::Off:
631 case HWC2::PowerMode::On:
632 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
633 {
634 auto error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700635 if (error != HWC2::Error::None) {
636 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
637 error, displayId);
638 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700639 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800640 break;
641 case HWC2::PowerMode::Doze:
642 case HWC2::PowerMode::DozeSuspend:
643 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
644 {
645 bool supportsDoze = false;
646 auto error = hwcDisplay->supportsDoze(&supportsDoze);
Peiyong Lin306e4992018-05-07 16:18:22 -0700647 if (error != HWC2::Error::None) {
648 LOG_HWC_ERROR("supportsDoze", error, displayId);
649 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700650
Dan Stoza9e56aa02015-11-02 13:00:03 -0800651 if (!supportsDoze) {
652 mode = HWC2::PowerMode::On;
653 }
654
655 error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700656 if (error != HWC2::Error::None) {
657 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
658 error, displayId);
659 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800660 }
661 break;
662 default:
663 ALOGV("setPowerMode: Not calling HWC");
664 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700665 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800666
667 return NO_ERROR;
668}
669
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800670status_t HWComposer::setActiveConfigWithConstraints(
671 DisplayId displayId, size_t configId, const HWC2::VsyncPeriodChangeConstraints& constraints,
672 HWC2::VsyncPeriodChangeTimeline* outTimeline) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700673 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800674
675 auto& displayData = mDisplayData[displayId];
676 if (displayData.configMap.count(configId) == 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700677 LOG_DISPLAY_ERROR(displayId, ("Invalid config " + std::to_string(configId)).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800678 return BAD_INDEX;
679 }
680
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800681 auto error =
682 displayData.hwcDisplay->setActiveConfigWithConstraints(displayData.configMap[configId],
683 constraints, outTimeline);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700684 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800685 return NO_ERROR;
686}
687
Dominik Laskowski075d3172018-05-24 15:50:06 -0700688status_t HWComposer::setColorTransform(DisplayId displayId, const mat4& transform) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700689 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700690
691 auto& displayData = mDisplayData[displayId];
692 bool isIdentity = transform == mat4();
693 auto error = displayData.hwcDisplay->setColorTransform(transform,
694 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY :
695 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700696 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700697 return NO_ERROR;
698}
699
Dominik Laskowski075d3172018-05-24 15:50:06 -0700700void HWComposer::disconnectDisplay(DisplayId displayId) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700701 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800702 auto& displayData = mDisplayData[displayId];
703
Dan Stoza9e56aa02015-11-02 13:00:03 -0800704 // If this was a virtual display, add its slot back for reuse by future
705 // virtual displays
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700706 if (displayData.isVirtual) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700707 mFreeVirtualDisplayIds.insert(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800708 ++mRemainingHwcVirtualDisplays;
709 }
710
Dominik Laskowski7e045462018-05-30 13:02:02 -0700711 const auto hwcDisplayId = displayData.hwcDisplay->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700712
713 // TODO(b/74619554): Select internal/external display from remaining displays.
714 if (hwcDisplayId == mInternalHwcDisplayId) {
715 mInternalHwcDisplayId.reset();
716 } else if (hwcDisplayId == mExternalHwcDisplayId) {
717 mExternalHwcDisplayId.reset();
718 }
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800719 mPhysicalDisplayIdMap.erase(hwcDisplayId);
720 mDisplayData.erase(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800721}
722
Dominik Laskowski075d3172018-05-24 15:50:06 -0700723status_t HWComposer::setOutputBuffer(DisplayId displayId, const sp<Fence>& acquireFence,
724 const sp<GraphicBuffer>& buffer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700725 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700726 const auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800727
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700728 if (!displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700729 LOG_DISPLAY_ERROR(displayId, "Invalid operation on physical display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800730 return INVALID_OPERATION;
731 }
732
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700733 auto error = displayData.hwcDisplay->setOutputBuffer(buffer, acquireFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700734 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800735 return NO_ERROR;
736}
737
Dominik Laskowski075d3172018-05-24 15:50:06 -0700738void HWComposer::clearReleaseFences(DisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700739 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800740 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700741}
742
Dominik Laskowski075d3172018-05-24 15:50:06 -0700743status_t HWComposer::getHdrCapabilities(DisplayId displayId, HdrCapabilities* outCapabilities) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700744 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stozac4f471e2016-03-24 09:31:08 -0700745
746 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Peiyong Lin62665892018-04-16 11:07:44 -0700747 auto error = hwcDisplay->getHdrCapabilities(outCapabilities);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700748 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Peiyong Lin62665892018-04-16 11:07:44 -0700749 return NO_ERROR;
Dan Stozac4f471e2016-03-24 09:31:08 -0700750}
751
Dominik Laskowski075d3172018-05-24 15:50:06 -0700752int32_t HWComposer::getSupportedPerFrameMetadata(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700753 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700754 return mDisplayData.at(displayId).hwcDisplay->getSupportedPerFrameMetadata();
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700755}
756
Dominik Laskowski075d3172018-05-24 15:50:06 -0700757std::vector<ui::RenderIntent> HWComposer::getRenderIntents(DisplayId displayId,
758 ui::ColorMode colorMode) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700759 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700760
761 std::vector<ui::RenderIntent> renderIntents;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700762 auto error = mDisplayData.at(displayId).hwcDisplay->getRenderIntents(colorMode, &renderIntents);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700763 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700764 return renderIntents;
765}
766
Dominik Laskowski075d3172018-05-24 15:50:06 -0700767mat4 HWComposer::getDataspaceSaturationMatrix(DisplayId displayId, ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700768 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700769
770 mat4 matrix;
771 auto error = mDisplayData[displayId].hwcDisplay->getDataspaceSaturationMatrix(dataspace,
772 &matrix);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700773 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700774 return matrix;
775}
776
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700777status_t HWComposer::getDisplayedContentSamplingAttributes(DisplayId displayId,
778 ui::PixelFormat* outFormat,
779 ui::Dataspace* outDataspace,
780 uint8_t* outComponentMask) {
781 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
782 const auto error =
783 mDisplayData[displayId]
784 .hwcDisplay->getDisplayedContentSamplingAttributes(outFormat, outDataspace,
785 outComponentMask);
786 if (error == HWC2::Error::Unsupported) RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
787 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
788 return NO_ERROR;
789}
790
Kevin DuBois74e53772018-11-19 10:52:38 -0800791status_t HWComposer::setDisplayContentSamplingEnabled(DisplayId displayId, bool enabled,
792 uint8_t componentMask, uint64_t maxFrames) {
793 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
794 const auto error =
795 mDisplayData[displayId].hwcDisplay->setDisplayContentSamplingEnabled(enabled,
796 componentMask,
797 maxFrames);
798
799 if (error == HWC2::Error::Unsupported) RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
800 if (error == HWC2::Error::BadParameter) RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
801 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
802 return NO_ERROR;
803}
804
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700805status_t HWComposer::getDisplayedContentSample(DisplayId displayId, uint64_t maxFrames,
806 uint64_t timestamp, DisplayedFrameStats* outStats) {
807 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
808 const auto error =
809 mDisplayData[displayId].hwcDisplay->getDisplayedContentSample(maxFrames, timestamp,
810 outStats);
811 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
812 return NO_ERROR;
813}
814
Dan Gittik57e63c52019-01-18 16:37:54 +0000815status_t HWComposer::setDisplayBrightness(DisplayId displayId, float brightness) {
816 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
817 const auto error = mDisplayData[displayId].hwcDisplay->setDisplayBrightness(brightness);
818 if (error == HWC2::Error::Unsupported) {
819 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
820 }
821 if (error == HWC2::Error::BadParameter) {
822 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
823 }
824 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
825 return NO_ERROR;
826}
827
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800828bool HWComposer::isUsingVrComposer() const {
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800829 return getComposer()->isUsingVrComposer();
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800830}
831
Galia Peycheva5492cb52019-10-30 14:13:16 +0100832status_t HWComposer::setAutoLowLatencyMode(DisplayId displayId, bool on) {
833 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
834 const auto error = mDisplayData[displayId].hwcDisplay->setAutoLowLatencyMode(on);
835 if (error == HWC2::Error::Unsupported) {
836 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
837 }
838 if (error == HWC2::Error::BadParameter) {
839 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(
846 DisplayId displayId, std::vector<HWC2::ContentType>* outSupportedContentTypes) {
847 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
856status_t HWComposer::setContentType(DisplayId displayId, HWC2::ContentType contentType) {
857 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
858 const auto error = mDisplayData[displayId].hwcDisplay->setContentType(contentType);
859 if (error == HWC2::Error::Unsupported) {
860 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
861 }
862 if (error == HWC2::Error::BadParameter) {
863 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
Yiwei Zhang5434a782018-12-05 18:06:32 -0800870void HWComposer::dump(std::string& result) const {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800871 result.append(mComposer->dumpDebugInfo());
Mathias Agopian83727852010-09-23 18:13:21 -0700872}
873
Dominik Laskowski075d3172018-05-24 15:50:06 -0700874std::optional<DisplayId> HWComposer::toPhysicalDisplayId(hwc2_display_t hwcDisplayId) const {
875 if (const auto it = mPhysicalDisplayIdMap.find(hwcDisplayId);
876 it != mPhysicalDisplayIdMap.end()) {
877 return it->second;
878 }
879 return {};
880}
881
882std::optional<hwc2_display_t> HWComposer::fromPhysicalDisplayId(DisplayId displayId) const {
883 if (const auto it = mDisplayData.find(displayId);
884 it != mDisplayData.end() && !it->second.isVirtual) {
885 return it->second.hwcDisplay->getId();
886 }
887 return {};
888}
889
890std::optional<DisplayIdentificationInfo> HWComposer::onHotplugConnect(hwc2_display_t hwcDisplayId) {
891 if (isUsingVrComposer() && mInternalHwcDisplayId) {
892 ALOGE("Ignoring connection of external display %" PRIu64 " in VR mode", hwcDisplayId);
Steven Thomas6e8f7062017-11-22 14:15:29 -0800893 return {};
894 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700895
896 uint8_t port;
897 DisplayIdentificationData data;
898 const bool hasMultiDisplaySupport = getDisplayIdentificationData(hwcDisplayId, &port, &data);
899
900 if (mPhysicalDisplayIdMap.empty()) {
901 mHasMultiDisplaySupport = hasMultiDisplaySupport;
902 ALOGI("Switching to %s multi-display mode",
903 hasMultiDisplaySupport ? "generalized" : "legacy");
904 } else if (mHasMultiDisplaySupport && !hasMultiDisplaySupport) {
905 ALOGE("Ignoring connection of display %" PRIu64 " without identification data",
906 hwcDisplayId);
907 return {};
908 }
909
910 std::optional<DisplayIdentificationInfo> info;
911
912 if (mHasMultiDisplaySupport) {
913 info = parseDisplayIdentificationData(port, data);
914 ALOGE_IF(!info, "Failed to parse identification data for display %" PRIu64, hwcDisplayId);
915 } else if (mInternalHwcDisplayId && mExternalHwcDisplayId) {
916 ALOGE("Ignoring connection of tertiary display %" PRIu64, hwcDisplayId);
917 return {};
918 } else {
919 ALOGW_IF(hasMultiDisplaySupport, "Ignoring identification data for display %" PRIu64,
920 hwcDisplayId);
921 port = mInternalHwcDisplayId ? HWC_DISPLAY_EXTERNAL : HWC_DISPLAY_PRIMARY;
922 }
923
924 if (!mInternalHwcDisplayId) {
925 mInternalHwcDisplayId = hwcDisplayId;
926 } else if (!mExternalHwcDisplayId) {
927 mExternalHwcDisplayId = hwcDisplayId;
928 }
929
930 if (info) return info;
931
932 return DisplayIdentificationInfo{getFallbackDisplayId(port),
933 hwcDisplayId == mInternalHwcDisplayId ? "Internal display"
934 : "External display"};
Steven Thomas6e8f7062017-11-22 14:15:29 -0800935}
936
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800937void HWComposer::loadCapabilities() {
938 static_assert(sizeof(HWC2::Capability) == sizeof(int32_t), "Capability size has changed");
939 auto capabilities = mComposer->getCapabilities();
940 for (auto capability : capabilities) {
941 mCapabilities.emplace(static_cast<HWC2::Capability>(capability));
942 }
943}
944
945uint32_t HWComposer::getMaxVirtualDisplayCount() const {
946 return mComposer->getMaxVirtualDisplayCount();
947}
948
Lloyd Pique441d5042018-10-18 16:49:51 -0700949} // namespace impl
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700950} // namespace android