blob: 6d801731807eae0b7fb931ccd8601b2c41b73773 [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
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080029#include <compositionengine/Output.h>
30#include <compositionengine/OutputLayer.h>
31#include <compositionengine/impl/OutputLayerCompositionState.h>
32#include <log/log.h>
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070033#include <ui/DebugUtils.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070034#include <ui/GraphicBuffer.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080035#include <utils/Errors.h>
36#include <utils/Trace.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070037
Lloyd Pique66d68602019-02-13 14:23:31 -080038#include "../Layer.h" // needed only for debugging
Mathias Agopian33ceeb32013-04-01 16:54:58 -070039#include "../SurfaceFlinger.h"
Lloyd Pique66d68602019-02-13 14:23:31 -080040#include "ComposerHal.h"
41#include "HWC2.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070042
Dominik Laskowskic1f18f62018-06-13 15:17:55 -070043#define LOG_HWC_DISPLAY_ERROR(hwcDisplayId, msg) \
44 ALOGE("%s failed for HWC display %" PRIu64 ": %s", __FUNCTION__, hwcDisplayId, msg)
45
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070046#define LOG_DISPLAY_ERROR(displayId, msg) \
Dominik Laskowski34157762018-10-31 13:07:19 -070047 ALOGE("%s failed for display %s: %s", __FUNCTION__, to_string(displayId).c_str(), msg)
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070048
Dominik Laskowski34157762018-10-31 13:07:19 -070049#define LOG_HWC_ERROR(what, error, displayId) \
50 ALOGE("%s: %s failed for display %s: %s (%d)", __FUNCTION__, what, \
51 to_string(displayId).c_str(), to_string(error).c_str(), static_cast<int32_t>(error))
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070052
53#define RETURN_IF_INVALID_DISPLAY(displayId, ...) \
54 do { \
Dominik Laskowski075d3172018-05-24 15:50:06 -070055 if (mDisplayData.count(displayId) == 0) { \
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070056 LOG_DISPLAY_ERROR(displayId, "Invalid display"); \
57 return __VA_ARGS__; \
58 } \
59 } while (false)
60
61#define RETURN_IF_HWC_ERROR_FOR(what, error, displayId, ...) \
62 do { \
63 if (error != HWC2::Error::None) { \
64 LOG_HWC_ERROR(what, error, displayId); \
65 return __VA_ARGS__; \
66 } \
67 } while (false)
68
69#define RETURN_IF_HWC_ERROR(error, displayId, ...) \
70 RETURN_IF_HWC_ERROR_FOR(__FUNCTION__, error, displayId, __VA_ARGS__)
71
Peiyong Linbdd08cc2019-12-17 21:35:14 -080072namespace {
73
74using android::hardware::Return;
75using android::hardware::Void;
76
77class ComposerCallbackBridge : public android::Hwc2::IComposerCallback {
78public:
79 ComposerCallbackBridge(HWC2::ComposerCallback* callback, int32_t sequenceId,
80 bool vsyncSwitchingSupported)
81 : mCallback(callback),
82 mSequenceId(sequenceId),
83 mVsyncSwitchingSupported(vsyncSwitchingSupported) {}
84
85 android::hardware::Return<void> onHotplug(
86 android::Hwc2::Display display,
87 android::Hwc2::IComposerCallback::Connection conn) override {
88 HWC2::Connection connection = static_cast<HWC2::Connection>(conn);
89 mCallback->onHotplugReceived(mSequenceId, display, connection);
90 return android::hardware::Void();
91 }
92
93 android::hardware::Return<void> onRefresh(android::Hwc2::Display display) override {
94 mCallback->onRefreshReceived(mSequenceId, display);
95 return android::hardware::Void();
96 }
97
98 android::hardware::Return<void> onVsync(android::Hwc2::Display display,
99 int64_t timestamp) override {
100 if (!mVsyncSwitchingSupported) {
101 mCallback->onVsyncReceived(mSequenceId, display, timestamp, std::nullopt);
102 } else {
103 ALOGW("Unexpected onVsync callback on composer >= 2.4, ignoring.");
104 }
105 return android::hardware::Void();
106 }
107
108 android::hardware::Return<void> onVsync_2_4(
109 android::Hwc2::Display display, int64_t timestamp,
110 android::Hwc2::VsyncPeriodNanos vsyncPeriodNanos) override {
111 if (mVsyncSwitchingSupported) {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800112 mCallback->onVsyncReceived(mSequenceId, display, timestamp,
113 std::make_optional(vsyncPeriodNanos));
114 } else {
115 ALOGW("Unexpected onVsync_2_4 callback on composer <= 2.3, ignoring.");
116 }
117 return android::hardware::Void();
118 }
119
120 android::hardware::Return<void> onVsyncPeriodTimingChanged(
121 android::Hwc2::Display display,
122 const android::Hwc2::VsyncPeriodChangeTimeline& updatedTimeline) override {
123 hwc_vsync_period_change_timeline_t timeline;
124 timeline.newVsyncAppliedTimeNanos = updatedTimeline.newVsyncAppliedTimeNanos;
125 timeline.refreshRequired = updatedTimeline.refreshRequired;
126 timeline.refreshTimeNanos = updatedTimeline.refreshTimeNanos;
127 mCallback->onVsyncPeriodTimingChangedReceived(mSequenceId, display, timeline);
128 return android::hardware::Void();
129 }
130
Ady Abrahamb0433bc2020-01-08 17:31:06 -0800131 android::hardware::Return<void> onSeamlessPossible(android::Hwc2::Display display) override {
132 mCallback->onSeamlessPossible(mSequenceId, display);
133 return android::hardware::Void();
134 }
135
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800136private:
137 HWC2::ComposerCallback* mCallback;
138 const int32_t mSequenceId;
139 const bool mVsyncSwitchingSupported;
140};
141
142} // namespace
143
Mathias Agopiana350ff92010-08-10 17:14:02 -0700144namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -0700145
Lloyd Pique441d5042018-10-18 16:49:51 -0700146HWComposer::~HWComposer() = default;
147
148namespace impl {
149
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800150HWComposer::HWComposer(std::unique_ptr<Hwc2::Composer> composer) : mComposer(std::move(composer)) {
151 loadCapabilities();
152}
153
154HWComposer::HWComposer(const std::string& composerServiceName)
155 : mComposer(std::make_unique<Hwc2::impl::Composer>(composerServiceName)) {
156 loadCapabilities();
157}
Mathias Agopianbef42c52013-08-21 17:45:46 -0700158
Dominik Laskowskib04f98a2018-11-07 21:07:16 -0800159HWComposer::~HWComposer() {
160 mDisplayData.clear();
161}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800162
Steven Thomas94e35b92017-07-26 18:48:28 -0700163void HWComposer::registerCallback(HWC2::ComposerCallback* callback,
164 int32_t sequenceId) {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800165 if (mRegisteredCallback) {
166 ALOGW("Callback already registered. Ignored extra registration attempt.");
167 return;
168 }
169 mRegisteredCallback = true;
170 sp<ComposerCallbackBridge> callbackBridge(
171 new ComposerCallbackBridge(callback, sequenceId,
172 mComposer->isVsyncPeriodSwitchSupported()));
173 mComposer->registerCallback(callbackBridge);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800174}
175
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700176bool HWComposer::getDisplayIdentificationData(hwc2_display_t hwcDisplayId, uint8_t* outPort,
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700177 DisplayIdentificationData* outData) const {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800178 const auto error = static_cast<HWC2::Error>(
179 mComposer->getDisplayIdentificationData(hwcDisplayId, outPort, outData));
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700180 if (error != HWC2::Error::None) {
Chia-I Wud0aff9d2018-06-21 13:39:09 +0800181 if (error != HWC2::Error::Unsupported) {
182 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, to_string(error).c_str());
183 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700184 return false;
185 }
186 return true;
187}
188
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800189bool HWComposer::hasCapability(HWC2::Capability capability) const {
190 return mCapabilities.count(capability) > 0;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700191}
192
Peiyong Lined531a32018-10-26 18:27:56 -0700193bool HWComposer::hasDisplayCapability(const std::optional<DisplayId>& displayId,
194 HWC2::DisplayCapability capability) const {
195 if (!displayId) {
Peiyong Lindf65fd22019-01-03 15:17:05 -0800196 // Checkout global capabilities for displays without a corresponding HWC display.
197 if (capability == HWC2::DisplayCapability::SkipClientColorTransform) {
198 return hasCapability(HWC2::Capability::SkipClientColorTransform);
199 }
Peiyong Lined531a32018-10-26 18:27:56 -0700200 return false;
201 }
202 RETURN_IF_INVALID_DISPLAY(*displayId, false);
203 return mDisplayData.at(*displayId).hwcDisplay->getCapabilities().count(capability) > 0;
204}
205
Dominik Laskowski075d3172018-05-24 15:50:06 -0700206std::optional<DisplayIdentificationInfo> HWComposer::onHotplug(hwc2_display_t hwcDisplayId,
207 HWC2::Connection connection) {
208 std::optional<DisplayIdentificationInfo> info;
Lloyd Pique715a2c12017-12-14 17:18:08 -0800209
Dominik Laskowski075d3172018-05-24 15:50:06 -0700210 if (const auto displayId = toPhysicalDisplayId(hwcDisplayId)) {
211 info = DisplayIdentificationInfo{*displayId, std::string()};
212 } else {
213 if (connection == HWC2::Connection::Disconnected) {
214 ALOGE("Ignoring disconnection of invalid HWC display %" PRIu64, hwcDisplayId);
215 return {};
Lloyd Pique438e9e72018-09-04 18:06:08 -0700216 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700217
218 info = onHotplugConnect(hwcDisplayId);
219 if (!info) return {};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700220 }
221
Dominik Laskowski34157762018-10-31 13:07:19 -0700222 ALOGV("%s: %s %s display %s with HWC ID %" PRIu64, __FUNCTION__, to_string(connection).c_str(),
223 hwcDisplayId == mInternalHwcDisplayId ? "internal" : "external",
224 to_string(info->id).c_str(), hwcDisplayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700225
Lloyd Pique715a2c12017-12-14 17:18:08 -0800226 if (connection == HWC2::Connection::Connected) {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800227 auto& displayData = mDisplayData[info->id];
228 // If we get a hotplug connected event for a display we already have,
229 // destroy the display and recreate it. This will force us to requery
230 // the display params and recreate all layers on that display.
231 if (displayData.hwcDisplay != nullptr && displayData.hwcDisplay->isConnected()) {
232 ALOGI("Hotplug connecting an already connected display."
233 " Clearing old display state.");
234 }
235 displayData.hwcDisplay.reset();
236 auto newDisplay =
237 std::make_unique<HWC2::impl::Display>(*mComposer.get(), mCapabilities, hwcDisplayId,
238 HWC2::DisplayType::Physical);
239 newDisplay->setConnected(true);
240 displayData.hwcDisplay = std::move(newDisplay);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700241 mPhysicalDisplayIdMap[hwcDisplayId] = info->id;
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800242 } else if (connection == HWC2::Connection::Disconnected) {
243 // The display will later be destroyed by a call to
244 // destroyDisplay(). For now we just mark it disconnected.
245 auto& displayData = mDisplayData[info->id];
246 if (displayData.hwcDisplay) {
247 displayData.hwcDisplay->setConnected(false);
248 } else {
249 ALOGW("Attempted to disconnect unknown display %" PRIu64, hwcDisplayId);
250 }
251 // The cleanup of Disconnect is handled through HWComposer::disconnectDisplay
252 // via SurfaceFlinger's onHotplugReceived callback handling
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700253 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700254
Dominik Laskowski075d3172018-05-24 15:50:06 -0700255 return info;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700256}
257
Dominik Laskowski075d3172018-05-24 15:50:06 -0700258bool HWComposer::onVsync(hwc2_display_t hwcDisplayId, int64_t timestamp) {
259 const auto displayId = toPhysicalDisplayId(hwcDisplayId);
260 if (!displayId) {
261 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, "Invalid HWC display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700262 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700263 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700264
Dominik Laskowski075d3172018-05-24 15:50:06 -0700265 RETURN_IF_INVALID_DISPLAY(*displayId, false);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700266
Dominik Laskowski1af47932018-11-12 10:20:46 -0800267 auto& displayData = mDisplayData[*displayId];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700268 if (displayData.isVirtual) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700269 LOG_DISPLAY_ERROR(*displayId, "Invalid operation on virtual display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700270 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700271 }
272
Dan Stoza9e56aa02015-11-02 13:00:03 -0800273 {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800274 std::lock_guard lock(displayData.lastHwVsyncLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700275
Dan Stoza9e56aa02015-11-02 13:00:03 -0800276 // There have been reports of HWCs that signal several vsync events
277 // with the same timestamp when turning the display off and on. This
278 // is a bug in the HWC implementation, but filter the extra events
279 // out here so they don't cause havoc downstream.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800280 if (timestamp == displayData.lastHwVsync) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700281 ALOGW("Ignoring duplicate VSYNC event from HWC for display %s (t=%" PRId64 ")",
282 to_string(*displayId).c_str(), timestamp);
Steven Thomas94e35b92017-07-26 18:48:28 -0700283 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800284 }
285
Dominik Laskowski1af47932018-11-12 10:20:46 -0800286 displayData.lastHwVsync = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700287 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800288
Dominik Laskowski34157762018-10-31 13:07:19 -0700289 const auto tag = "HW_VSYNC_" + to_string(*displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800290 ATRACE_INT(tag.c_str(), displayData.vsyncTraceToggle);
291 displayData.vsyncTraceToggle = !displayData.vsyncTraceToggle;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800292
Steven Thomas94e35b92017-07-26 18:48:28 -0700293 return true;
Jesse Hall1c569c42013-04-05 13:44:52 -0700294}
295
Dominik Laskowski075d3172018-05-24 15:50:06 -0700296std::optional<DisplayId> HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
297 ui::PixelFormat* format) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800298 if (mRemainingHwcVirtualDisplays == 0) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700299 ALOGE("%s: No remaining virtual displays", __FUNCTION__);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700300 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700301 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700302
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800303 if (SurfaceFlinger::maxVirtualDisplaySize != 0 &&
304 (width > SurfaceFlinger::maxVirtualDisplaySize ||
305 height > SurfaceFlinger::maxVirtualDisplaySize)) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700306 ALOGE("%s: Display size %ux%u exceeds maximum dimension of %" PRIu64, __FUNCTION__, width,
307 height, SurfaceFlinger::maxVirtualDisplaySize);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700308 return {};
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800309 }
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800310 hwc2_display_t hwcDisplayId = 0;
311 const auto error = static_cast<HWC2::Error>(
312 mComposer->createVirtualDisplay(width, height, format, &hwcDisplayId));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800313 if (error != HWC2::Error::None) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700314 ALOGE("%s: Failed to create HWC virtual display", __FUNCTION__);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700315 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700316 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800317
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800318 auto display = std::make_unique<HWC2::impl::Display>(*mComposer.get(), mCapabilities,
319 hwcDisplayId, HWC2::DisplayType::Virtual);
320 display->setConnected(true);
321
Dominik Laskowski075d3172018-05-24 15:50:06 -0700322 DisplayId displayId;
323 if (mFreeVirtualDisplayIds.empty()) {
324 displayId = getVirtualDisplayId(mNextVirtualDisplayId++);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800325 } else {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700326 displayId = *mFreeVirtualDisplayIds.begin();
327 mFreeVirtualDisplayIds.erase(displayId);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700328 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800329
Dominik Laskowski075d3172018-05-24 15:50:06 -0700330 auto& displayData = mDisplayData[displayId];
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800331 displayData.hwcDisplay = std::move(display);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700332 displayData.isVirtual = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800333
334 --mRemainingHwcVirtualDisplays;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700335 return displayId;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700336}
337
Dominik Laskowski075d3172018-05-24 15:50:06 -0700338HWC2::Layer* HWComposer::createLayer(DisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700339 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
340
Steven Thomas94e35b92017-07-26 18:48:28 -0700341 HWC2::Layer* layer;
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800342 auto error = mDisplayData[displayId].hwcDisplay->createLayer(&layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700343 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800344 return layer;
345}
346
Dominik Laskowski075d3172018-05-24 15:50:06 -0700347void HWComposer::destroyLayer(DisplayId displayId, HWC2::Layer* layer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700348 RETURN_IF_INVALID_DISPLAY(displayId);
349
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800350 auto error = mDisplayData[displayId].hwcDisplay->destroyLayer(layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700351 RETURN_IF_HWC_ERROR(error, displayId);
Steven Thomas94e35b92017-07-26 18:48:28 -0700352}
353
Dominik Laskowski075d3172018-05-24 15:50:06 -0700354nsecs_t HWComposer::getRefreshTimestamp(DisplayId displayId) const {
355 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800356 const auto& displayData = mDisplayData.at(displayId);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700357 // this returns the last refresh timestamp.
358 // if the last one is not available, we estimate it based on
359 // the refresh period and whatever closest timestamp we have.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800360 std::lock_guard lock(displayData.lastHwVsyncLock);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700361 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800362 auto vsyncPeriodNanos = getDisplayVsyncPeriod(displayId);
363 return now - ((now - displayData.lastHwVsync) % vsyncPeriodNanos);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700364}
365
Dominik Laskowski075d3172018-05-24 15:50:06 -0700366bool HWComposer::isConnected(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700367 RETURN_IF_INVALID_DISPLAY(displayId, false);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700368 return mDisplayData.at(displayId).hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700369}
370
Dominik Laskowski075d3172018-05-24 15:50:06 -0700371std::vector<std::shared_ptr<const HWC2::Display::Config>> HWComposer::getConfigs(
372 DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700373 RETURN_IF_INVALID_DISPLAY(displayId, {});
374
Dominik Laskowski075d3172018-05-24 15:50:06 -0700375 const auto& displayData = mDisplayData.at(displayId);
376 auto configs = displayData.hwcDisplay->getConfigs();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800377 if (displayData.configMap.empty()) {
378 for (size_t i = 0; i < configs.size(); ++i) {
379 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700380 }
381 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800382 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700383}
384
Dominik Laskowski075d3172018-05-24 15:50:06 -0700385std::shared_ptr<const HWC2::Display::Config> HWComposer::getActiveConfig(
386 DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700387 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
388
Dan Stoza9e56aa02015-11-02 13:00:03 -0800389 std::shared_ptr<const HWC2::Display::Config> config;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700390 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfig(&config);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800391 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700392 LOG_DISPLAY_ERROR(displayId, "No active config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800393 return nullptr;
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700394 }
395
396 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
397
398 if (!config) {
399 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800400 return nullptr;
401 }
402
403 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700404}
405
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800406// Composer 2.4
407
408bool HWComposer::isVsyncPeriodSwitchSupported(DisplayId displayId) const {
409 return mDisplayData.at(displayId).hwcDisplay->isVsyncPeriodSwitchSupported();
410}
411
412nsecs_t HWComposer::getDisplayVsyncPeriod(DisplayId displayId) const {
413 nsecs_t vsyncPeriodNanos;
414 auto error = mDisplayData.at(displayId).hwcDisplay->getDisplayVsyncPeriod(&vsyncPeriodNanos);
415 if (error != HWC2::Error::None) {
416 LOG_DISPLAY_ERROR(displayId, "Failed to get Vsync Period");
417 return 0;
418 }
419
420 return vsyncPeriodNanos;
421}
422
Dominik Laskowski075d3172018-05-24 15:50:06 -0700423int HWComposer::getActiveConfigIndex(DisplayId displayId) const {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700424 RETURN_IF_INVALID_DISPLAY(displayId, -1);
425
Lloyd Pique3c085a02018-05-09 19:38:32 -0700426 int index;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700427 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfigIndex(&index);
Lloyd Pique3c085a02018-05-09 19:38:32 -0700428 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700429 LOG_DISPLAY_ERROR(displayId, "No active config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700430 return -1;
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700431 }
432
433 RETURN_IF_HWC_ERROR(error, displayId, -1);
434
435 if (index < 0) {
436 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700437 return -1;
438 }
439
440 return index;
441}
442
Dominik Laskowski075d3172018-05-24 15:50:06 -0700443std::vector<ui::ColorMode> HWComposer::getColorModes(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700444 RETURN_IF_INVALID_DISPLAY(displayId, {});
445
Peiyong Linfd997e02018-03-28 15:29:00 -0700446 std::vector<ui::ColorMode> modes;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700447 auto error = mDisplayData.at(displayId).hwcDisplay->getColorModes(&modes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700448 RETURN_IF_HWC_ERROR(error, displayId, {});
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600449 return modes;
450}
451
Dominik Laskowski075d3172018-05-24 15:50:06 -0700452status_t HWComposer::setActiveColorMode(DisplayId displayId, ui::ColorMode mode,
453 ui::RenderIntent renderIntent) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700454 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Michael Wright28f24d02016-07-12 13:30:53 -0700455
456 auto& displayData = mDisplayData[displayId];
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700457 auto error = displayData.hwcDisplay->setColorMode(mode, renderIntent);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700458 RETURN_IF_HWC_ERROR_FOR(("setColorMode(" + decodeColorMode(mode) + ", " +
459 decodeRenderIntent(renderIntent) + ")")
460 .c_str(),
461 error, displayId, UNKNOWN_ERROR);
Michael Wright28f24d02016-07-12 13:30:53 -0700462
463 return NO_ERROR;
464}
465
Dominik Laskowski075d3172018-05-24 15:50:06 -0700466void HWComposer::setVsyncEnabled(DisplayId displayId, HWC2::Vsync enabled) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700467 RETURN_IF_INVALID_DISPLAY(displayId);
468 auto& displayData = mDisplayData[displayId];
469
470 if (displayData.isVirtual) {
471 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800472 return;
473 }
474
Dan Stoza9e56aa02015-11-02 13:00:03 -0800475 // NOTE: we use our own internal lock here because we have to call
476 // into the HWC with the lock held, and we want to make sure
477 // that even if HWC blocks (which it shouldn't), it won't
478 // affect other threads.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800479 std::lock_guard lock(displayData.vsyncEnabledLock);
480 if (enabled == displayData.vsyncEnabled) {
481 return;
Colin Cross10fbdb62012-07-12 17:56:34 -0700482 }
Dominik Laskowski1af47932018-11-12 10:20:46 -0800483
484 ATRACE_CALL();
485 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
486 RETURN_IF_HWC_ERROR(error, displayId);
487
488 displayData.vsyncEnabled = enabled;
489
490 const auto tag = "HW_VSYNC_ON_" + to_string(displayId);
491 ATRACE_INT(tag.c_str(), enabled == HWC2::Vsync::Enable ? 1 : 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700492}
493
Dominik Laskowski075d3172018-05-24 15:50:06 -0700494status_t HWComposer::setClientTarget(DisplayId displayId, uint32_t slot,
495 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
496 ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700497 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Jesse Hall851cfe82013-03-20 13:44:00 -0700498
Dominik Laskowski34157762018-10-31 13:07:19 -0700499 ALOGV("%s for display %s", __FUNCTION__, to_string(displayId).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800500 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400501 auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700502 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Jesse Hall851cfe82013-03-20 13:44:00 -0700503 return NO_ERROR;
504}
505
Lloyd Pique66d68602019-02-13 14:23:31 -0800506status_t HWComposer::getDeviceCompositionChanges(
507 DisplayId displayId, bool frameUsesClientComposition,
508 std::optional<android::HWComposer::DeviceRequestedChanges>* outChanges) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800509 ATRACE_CALL();
510
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700511 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800512
513 auto& displayData = mDisplayData[displayId];
514 auto& hwcDisplay = displayData.hwcDisplay;
515 if (!hwcDisplay->isConnected()) {
516 return NO_ERROR;
517 }
518
519 uint32_t numTypes = 0;
520 uint32_t numRequests = 0;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700521
522 HWC2::Error error = HWC2::Error::None;
523
Chia-I Wu41b98d42017-12-11 11:04:36 -0800524 // First try to skip validate altogether when there is no client
525 // composition. When there is client composition, since we haven't
526 // rendered to the client target yet, we should not attempt to skip
527 // validate.
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700528 displayData.validateWasSkipped = false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800529 if (!frameUsesClientComposition) {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800530 sp<Fence> outPresentFence;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700531 uint32_t state = UINT32_MAX;
532 error = hwcDisplay->presentOrValidate(&numTypes, &numRequests, &outPresentFence , &state);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700533 if (error != HWC2::Error::HasChanges) {
534 RETURN_IF_HWC_ERROR_FOR("presentOrValidate", error, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700535 }
536 if (state == 1) { //Present Succeeded.
Steven Thomas94e35b92017-07-26 18:48:28 -0700537 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700538 error = hwcDisplay->getReleaseFences(&releaseFences);
539 displayData.releaseFences = std::move(releaseFences);
540 displayData.lastPresentFence = outPresentFence;
541 displayData.validateWasSkipped = true;
542 displayData.presentError = error;
543 return NO_ERROR;
544 }
545 // Present failed but Validate ran.
546 } else {
547 error = hwcDisplay->validate(&numTypes, &numRequests);
548 }
549 ALOGV("SkipValidate failed, Falling back to SLOW validate/present");
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700550 if (error != HWC2::Error::HasChanges) {
551 RETURN_IF_HWC_ERROR_FOR("validate", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800552 }
553
Lloyd Pique66d68602019-02-13 14:23:31 -0800554 android::HWComposer::DeviceRequestedChanges::ChangedTypes changedTypes;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800555 changedTypes.reserve(numTypes);
556 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700557 RETURN_IF_HWC_ERROR_FOR("getChangedCompositionTypes", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800558
Lloyd Pique66d68602019-02-13 14:23:31 -0800559 auto displayRequests = static_cast<HWC2::DisplayRequest>(0);
560 android::HWComposer::DeviceRequestedChanges::LayerRequests layerRequests;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800561 layerRequests.reserve(numRequests);
Lloyd Pique66d68602019-02-13 14:23:31 -0800562 error = hwcDisplay->getRequests(&displayRequests, &layerRequests);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700563 RETURN_IF_HWC_ERROR_FOR("getRequests", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800564
Lloyd Pique66d68602019-02-13 14:23:31 -0800565 outChanges->emplace(DeviceRequestedChanges{std::move(changedTypes), std::move(displayRequests),
566 std::move(layerRequests)});
Dan Stoza9e56aa02015-11-02 13:00:03 -0800567
568 error = hwcDisplay->acceptChanges();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700569 RETURN_IF_HWC_ERROR_FOR("acceptChanges", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800570
571 return NO_ERROR;
572}
573
Dominik Laskowski075d3172018-05-24 15:50:06 -0700574sp<Fence> HWComposer::getPresentFence(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700575 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700576 return mDisplayData.at(displayId).lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700577}
578
Dominik Laskowski075d3172018-05-24 15:50:06 -0700579sp<Fence> HWComposer::getLayerReleaseFence(DisplayId displayId, HWC2::Layer* layer) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700580 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700581 auto displayFences = mDisplayData.at(displayId).releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800582 if (displayFences.count(layer) == 0) {
583 ALOGV("getLayerReleaseFence: Release fence not found");
584 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700585 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800586 return displayFences[layer];
Riley Andrews03414a12014-07-01 14:22:59 -0700587}
588
Dominik Laskowski075d3172018-05-24 15:50:06 -0700589status_t HWComposer::presentAndGetReleaseFences(DisplayId displayId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800590 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700591
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700592 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700593
Dan Stoza9e56aa02015-11-02 13:00:03 -0800594 auto& displayData = mDisplayData[displayId];
595 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700596
597 if (displayData.validateWasSkipped) {
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700598 // explicitly flush all pending commands
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800599 auto error = static_cast<HWC2::Error>(mComposer->executeCommands());
600 RETURN_IF_HWC_ERROR_FOR("executeCommands", error, displayId, UNKNOWN_ERROR);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700601 RETURN_IF_HWC_ERROR_FOR("present", displayData.presentError, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700602 return NO_ERROR;
603 }
604
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800605 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700606 RETURN_IF_HWC_ERROR_FOR("present", error, displayId, UNKNOWN_ERROR);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700607
Steven Thomas94e35b92017-07-26 18:48:28 -0700608 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800609 error = hwcDisplay->getReleaseFences(&releaseFences);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700610 RETURN_IF_HWC_ERROR_FOR("getReleaseFences", error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800611
612 displayData.releaseFences = std::move(releaseFences);
613
614 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700615}
616
Dominik Laskowski075d3172018-05-24 15:50:06 -0700617status_t HWComposer::setPowerMode(DisplayId displayId, int32_t intMode) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700618 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
619
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700620 const auto& displayData = mDisplayData[displayId];
621 if (displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700622 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
623 return INVALID_OPERATION;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800624 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700625
Dan Stoza9e56aa02015-11-02 13:00:03 -0800626 auto mode = static_cast<HWC2::PowerMode>(intMode);
627 if (mode == HWC2::PowerMode::Off) {
628 setVsyncEnabled(displayId, HWC2::Vsync::Disable);
629 }
630
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700631 auto& hwcDisplay = displayData.hwcDisplay;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800632 switch (mode) {
633 case HWC2::PowerMode::Off:
634 case HWC2::PowerMode::On:
635 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
636 {
637 auto error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700638 if (error != HWC2::Error::None) {
639 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
640 error, displayId);
641 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700642 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800643 break;
644 case HWC2::PowerMode::Doze:
645 case HWC2::PowerMode::DozeSuspend:
646 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
647 {
648 bool supportsDoze = false;
649 auto error = hwcDisplay->supportsDoze(&supportsDoze);
Peiyong Lin306e4992018-05-07 16:18:22 -0700650 if (error != HWC2::Error::None) {
651 LOG_HWC_ERROR("supportsDoze", error, displayId);
652 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700653
Dan Stoza9e56aa02015-11-02 13:00:03 -0800654 if (!supportsDoze) {
655 mode = HWC2::PowerMode::On;
656 }
657
658 error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700659 if (error != HWC2::Error::None) {
660 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
661 error, displayId);
662 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800663 }
664 break;
665 default:
666 ALOGV("setPowerMode: Not calling HWC");
667 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700668 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800669
670 return NO_ERROR;
671}
672
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800673status_t HWComposer::setActiveConfigWithConstraints(
674 DisplayId displayId, size_t configId, const HWC2::VsyncPeriodChangeConstraints& constraints,
675 HWC2::VsyncPeriodChangeTimeline* outTimeline) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700676 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800677
678 auto& displayData = mDisplayData[displayId];
679 if (displayData.configMap.count(configId) == 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700680 LOG_DISPLAY_ERROR(displayId, ("Invalid config " + std::to_string(configId)).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800681 return BAD_INDEX;
682 }
683
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800684 auto error =
685 displayData.hwcDisplay->setActiveConfigWithConstraints(displayData.configMap[configId],
686 constraints, outTimeline);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700687 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800688 return NO_ERROR;
689}
690
Dominik Laskowski075d3172018-05-24 15:50:06 -0700691status_t HWComposer::setColorTransform(DisplayId displayId, const mat4& transform) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700692 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700693
694 auto& displayData = mDisplayData[displayId];
695 bool isIdentity = transform == mat4();
696 auto error = displayData.hwcDisplay->setColorTransform(transform,
697 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY :
698 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700699 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700700 return NO_ERROR;
701}
702
Dominik Laskowski075d3172018-05-24 15:50:06 -0700703void HWComposer::disconnectDisplay(DisplayId displayId) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700704 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800705 auto& displayData = mDisplayData[displayId];
706
Dan Stoza9e56aa02015-11-02 13:00:03 -0800707 // If this was a virtual display, add its slot back for reuse by future
708 // virtual displays
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700709 if (displayData.isVirtual) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700710 mFreeVirtualDisplayIds.insert(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800711 ++mRemainingHwcVirtualDisplays;
712 }
713
Dominik Laskowski7e045462018-05-30 13:02:02 -0700714 const auto hwcDisplayId = displayData.hwcDisplay->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700715
716 // TODO(b/74619554): Select internal/external display from remaining displays.
717 if (hwcDisplayId == mInternalHwcDisplayId) {
718 mInternalHwcDisplayId.reset();
719 } else if (hwcDisplayId == mExternalHwcDisplayId) {
720 mExternalHwcDisplayId.reset();
721 }
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800722 mPhysicalDisplayIdMap.erase(hwcDisplayId);
723 mDisplayData.erase(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800724}
725
Dominik Laskowski075d3172018-05-24 15:50:06 -0700726status_t HWComposer::setOutputBuffer(DisplayId displayId, const sp<Fence>& acquireFence,
727 const sp<GraphicBuffer>& buffer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700728 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700729 const auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800730
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700731 if (!displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700732 LOG_DISPLAY_ERROR(displayId, "Invalid operation on physical display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800733 return INVALID_OPERATION;
734 }
735
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700736 auto error = displayData.hwcDisplay->setOutputBuffer(buffer, acquireFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700737 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800738 return NO_ERROR;
739}
740
Dominik Laskowski075d3172018-05-24 15:50:06 -0700741void HWComposer::clearReleaseFences(DisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700742 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800743 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700744}
745
Dominik Laskowski075d3172018-05-24 15:50:06 -0700746status_t HWComposer::getHdrCapabilities(DisplayId displayId, HdrCapabilities* outCapabilities) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700747 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stozac4f471e2016-03-24 09:31:08 -0700748
749 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Peiyong Lin62665892018-04-16 11:07:44 -0700750 auto error = hwcDisplay->getHdrCapabilities(outCapabilities);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700751 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Peiyong Lin62665892018-04-16 11:07:44 -0700752 return NO_ERROR;
Dan Stozac4f471e2016-03-24 09:31:08 -0700753}
754
Dominik Laskowski075d3172018-05-24 15:50:06 -0700755int32_t HWComposer::getSupportedPerFrameMetadata(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700756 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700757 return mDisplayData.at(displayId).hwcDisplay->getSupportedPerFrameMetadata();
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700758}
759
Dominik Laskowski075d3172018-05-24 15:50:06 -0700760std::vector<ui::RenderIntent> HWComposer::getRenderIntents(DisplayId displayId,
761 ui::ColorMode colorMode) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700762 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700763
764 std::vector<ui::RenderIntent> renderIntents;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700765 auto error = mDisplayData.at(displayId).hwcDisplay->getRenderIntents(colorMode, &renderIntents);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700766 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700767 return renderIntents;
768}
769
Dominik Laskowski075d3172018-05-24 15:50:06 -0700770mat4 HWComposer::getDataspaceSaturationMatrix(DisplayId displayId, ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700771 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700772
773 mat4 matrix;
774 auto error = mDisplayData[displayId].hwcDisplay->getDataspaceSaturationMatrix(dataspace,
775 &matrix);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700776 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700777 return matrix;
778}
779
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700780status_t HWComposer::getDisplayedContentSamplingAttributes(DisplayId displayId,
781 ui::PixelFormat* outFormat,
782 ui::Dataspace* outDataspace,
783 uint8_t* outComponentMask) {
784 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
785 const auto error =
786 mDisplayData[displayId]
787 .hwcDisplay->getDisplayedContentSamplingAttributes(outFormat, outDataspace,
788 outComponentMask);
789 if (error == HWC2::Error::Unsupported) RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
790 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
791 return NO_ERROR;
792}
793
Kevin DuBois74e53772018-11-19 10:52:38 -0800794status_t HWComposer::setDisplayContentSamplingEnabled(DisplayId displayId, bool enabled,
795 uint8_t componentMask, uint64_t maxFrames) {
796 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
797 const auto error =
798 mDisplayData[displayId].hwcDisplay->setDisplayContentSamplingEnabled(enabled,
799 componentMask,
800 maxFrames);
801
802 if (error == HWC2::Error::Unsupported) RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
803 if (error == HWC2::Error::BadParameter) RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
804 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
805 return NO_ERROR;
806}
807
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700808status_t HWComposer::getDisplayedContentSample(DisplayId displayId, uint64_t maxFrames,
809 uint64_t timestamp, DisplayedFrameStats* outStats) {
810 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
811 const auto error =
812 mDisplayData[displayId].hwcDisplay->getDisplayedContentSample(maxFrames, timestamp,
813 outStats);
814 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
815 return NO_ERROR;
816}
817
Dan Gittik57e63c52019-01-18 16:37:54 +0000818status_t HWComposer::setDisplayBrightness(DisplayId displayId, float brightness) {
819 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
820 const auto error = mDisplayData[displayId].hwcDisplay->setDisplayBrightness(brightness);
821 if (error == HWC2::Error::Unsupported) {
822 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
823 }
824 if (error == HWC2::Error::BadParameter) {
825 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
826 }
827 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
828 return NO_ERROR;
829}
830
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800831bool HWComposer::isUsingVrComposer() const {
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800832 return getComposer()->isUsingVrComposer();
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800833}
834
Galia Peycheva5492cb52019-10-30 14:13:16 +0100835status_t HWComposer::setAutoLowLatencyMode(DisplayId displayId, bool on) {
836 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
837 const auto error = mDisplayData[displayId].hwcDisplay->setAutoLowLatencyMode(on);
838 if (error == HWC2::Error::Unsupported) {
839 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
840 }
841 if (error == HWC2::Error::BadParameter) {
842 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
843 }
844 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
845 return NO_ERROR;
846}
847
848status_t HWComposer::getSupportedContentTypes(
849 DisplayId displayId, std::vector<HWC2::ContentType>* outSupportedContentTypes) {
850 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
851 const auto error =
852 mDisplayData[displayId].hwcDisplay->getSupportedContentTypes(outSupportedContentTypes);
853
854 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
855
856 return NO_ERROR;
857}
858
859status_t HWComposer::setContentType(DisplayId displayId, HWC2::ContentType contentType) {
860 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
861 const auto error = mDisplayData[displayId].hwcDisplay->setContentType(contentType);
862 if (error == HWC2::Error::Unsupported) {
863 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
864 }
865 if (error == HWC2::Error::BadParameter) {
866 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
867 }
868 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
869
870 return NO_ERROR;
871}
872
Yiwei Zhang5434a782018-12-05 18:06:32 -0800873void HWComposer::dump(std::string& result) const {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800874 result.append(mComposer->dumpDebugInfo());
Mathias Agopian83727852010-09-23 18:13:21 -0700875}
876
Dominik Laskowski075d3172018-05-24 15:50:06 -0700877std::optional<DisplayId> HWComposer::toPhysicalDisplayId(hwc2_display_t hwcDisplayId) const {
878 if (const auto it = mPhysicalDisplayIdMap.find(hwcDisplayId);
879 it != mPhysicalDisplayIdMap.end()) {
880 return it->second;
881 }
882 return {};
883}
884
885std::optional<hwc2_display_t> HWComposer::fromPhysicalDisplayId(DisplayId displayId) const {
886 if (const auto it = mDisplayData.find(displayId);
887 it != mDisplayData.end() && !it->second.isVirtual) {
888 return it->second.hwcDisplay->getId();
889 }
890 return {};
891}
892
893std::optional<DisplayIdentificationInfo> HWComposer::onHotplugConnect(hwc2_display_t hwcDisplayId) {
894 if (isUsingVrComposer() && mInternalHwcDisplayId) {
895 ALOGE("Ignoring connection of external display %" PRIu64 " in VR mode", hwcDisplayId);
Steven Thomas6e8f7062017-11-22 14:15:29 -0800896 return {};
897 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700898
899 uint8_t port;
900 DisplayIdentificationData data;
901 const bool hasMultiDisplaySupport = getDisplayIdentificationData(hwcDisplayId, &port, &data);
902
903 if (mPhysicalDisplayIdMap.empty()) {
904 mHasMultiDisplaySupport = hasMultiDisplaySupport;
905 ALOGI("Switching to %s multi-display mode",
906 hasMultiDisplaySupport ? "generalized" : "legacy");
907 } else if (mHasMultiDisplaySupport && !hasMultiDisplaySupport) {
908 ALOGE("Ignoring connection of display %" PRIu64 " without identification data",
909 hwcDisplayId);
910 return {};
911 }
912
913 std::optional<DisplayIdentificationInfo> info;
914
915 if (mHasMultiDisplaySupport) {
916 info = parseDisplayIdentificationData(port, data);
917 ALOGE_IF(!info, "Failed to parse identification data for display %" PRIu64, hwcDisplayId);
918 } else if (mInternalHwcDisplayId && mExternalHwcDisplayId) {
919 ALOGE("Ignoring connection of tertiary display %" PRIu64, hwcDisplayId);
920 return {};
921 } else {
922 ALOGW_IF(hasMultiDisplaySupport, "Ignoring identification data for display %" PRIu64,
923 hwcDisplayId);
924 port = mInternalHwcDisplayId ? HWC_DISPLAY_EXTERNAL : HWC_DISPLAY_PRIMARY;
925 }
926
927 if (!mInternalHwcDisplayId) {
928 mInternalHwcDisplayId = hwcDisplayId;
929 } else if (!mExternalHwcDisplayId) {
930 mExternalHwcDisplayId = hwcDisplayId;
931 }
932
933 if (info) return info;
934
935 return DisplayIdentificationInfo{getFallbackDisplayId(port),
936 hwcDisplayId == mInternalHwcDisplayId ? "Internal display"
937 : "External display"};
Steven Thomas6e8f7062017-11-22 14:15:29 -0800938}
939
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800940void HWComposer::loadCapabilities() {
941 static_assert(sizeof(HWC2::Capability) == sizeof(int32_t), "Capability size has changed");
942 auto capabilities = mComposer->getCapabilities();
943 for (auto capability : capabilities) {
944 mCapabilities.emplace(static_cast<HWC2::Capability>(capability));
945 }
946}
947
948uint32_t HWComposer::getMaxVirtualDisplayCount() const {
949 return mComposer->getMaxVirtualDisplayCount();
950}
951
Lloyd Pique441d5042018-10-18 16:49:51 -0700952} // namespace impl
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700953} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800954
955// TODO(b/129481165): remove the #pragma below and fix conversion issues
956#pragma clang diagnostic pop // ignored "-Wconversion"