blob: 0a7009b9a13917933bc6e1dabfe9f6c88b8e0cb6 [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
128private:
129 HWC2::ComposerCallback* mCallback;
130 const int32_t mSequenceId;
131 const bool mVsyncSwitchingSupported;
132};
133
134} // namespace
135
Mathias Agopiana350ff92010-08-10 17:14:02 -0700136namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -0700137
Lloyd Pique441d5042018-10-18 16:49:51 -0700138HWComposer::~HWComposer() = default;
139
140namespace impl {
141
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800142HWComposer::HWComposer(std::unique_ptr<Hwc2::Composer> composer) : mComposer(std::move(composer)) {
143 loadCapabilities();
144}
145
146HWComposer::HWComposer(const std::string& composerServiceName)
147 : mComposer(std::make_unique<Hwc2::impl::Composer>(composerServiceName)) {
148 loadCapabilities();
149}
Mathias Agopianbef42c52013-08-21 17:45:46 -0700150
Dominik Laskowskib04f98a2018-11-07 21:07:16 -0800151HWComposer::~HWComposer() {
152 mDisplayData.clear();
153}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800154
Steven Thomas94e35b92017-07-26 18:48:28 -0700155void HWComposer::registerCallback(HWC2::ComposerCallback* callback,
156 int32_t sequenceId) {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800157 if (mRegisteredCallback) {
158 ALOGW("Callback already registered. Ignored extra registration attempt.");
159 return;
160 }
161 mRegisteredCallback = true;
162 sp<ComposerCallbackBridge> callbackBridge(
163 new ComposerCallbackBridge(callback, sequenceId,
164 mComposer->isVsyncPeriodSwitchSupported()));
165 mComposer->registerCallback(callbackBridge);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800166}
167
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700168bool HWComposer::getDisplayIdentificationData(hwc2_display_t hwcDisplayId, uint8_t* outPort,
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700169 DisplayIdentificationData* outData) const {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800170 const auto error = static_cast<HWC2::Error>(
171 mComposer->getDisplayIdentificationData(hwcDisplayId, outPort, outData));
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700172 if (error != HWC2::Error::None) {
Chia-I Wud0aff9d2018-06-21 13:39:09 +0800173 if (error != HWC2::Error::Unsupported) {
174 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, to_string(error).c_str());
175 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700176 return false;
177 }
178 return true;
179}
180
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800181bool HWComposer::hasCapability(HWC2::Capability capability) const {
182 return mCapabilities.count(capability) > 0;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700183}
184
Peiyong Lined531a32018-10-26 18:27:56 -0700185bool HWComposer::hasDisplayCapability(const std::optional<DisplayId>& displayId,
186 HWC2::DisplayCapability capability) const {
187 if (!displayId) {
Peiyong Lindf65fd22019-01-03 15:17:05 -0800188 // Checkout global capabilities for displays without a corresponding HWC display.
189 if (capability == HWC2::DisplayCapability::SkipClientColorTransform) {
190 return hasCapability(HWC2::Capability::SkipClientColorTransform);
191 }
Peiyong Lined531a32018-10-26 18:27:56 -0700192 return false;
193 }
194 RETURN_IF_INVALID_DISPLAY(*displayId, false);
195 return mDisplayData.at(*displayId).hwcDisplay->getCapabilities().count(capability) > 0;
196}
197
Dominik Laskowski075d3172018-05-24 15:50:06 -0700198std::optional<DisplayIdentificationInfo> HWComposer::onHotplug(hwc2_display_t hwcDisplayId,
199 HWC2::Connection connection) {
200 std::optional<DisplayIdentificationInfo> info;
Lloyd Pique715a2c12017-12-14 17:18:08 -0800201
Dominik Laskowski075d3172018-05-24 15:50:06 -0700202 if (const auto displayId = toPhysicalDisplayId(hwcDisplayId)) {
203 info = DisplayIdentificationInfo{*displayId, std::string()};
204 } else {
205 if (connection == HWC2::Connection::Disconnected) {
206 ALOGE("Ignoring disconnection of invalid HWC display %" PRIu64, hwcDisplayId);
207 return {};
Lloyd Pique438e9e72018-09-04 18:06:08 -0700208 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700209
210 info = onHotplugConnect(hwcDisplayId);
211 if (!info) return {};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700212 }
213
Dominik Laskowski34157762018-10-31 13:07:19 -0700214 ALOGV("%s: %s %s display %s with HWC ID %" PRIu64, __FUNCTION__, to_string(connection).c_str(),
215 hwcDisplayId == mInternalHwcDisplayId ? "internal" : "external",
216 to_string(info->id).c_str(), hwcDisplayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700217
Lloyd Pique715a2c12017-12-14 17:18:08 -0800218 if (connection == HWC2::Connection::Connected) {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800219 auto& displayData = mDisplayData[info->id];
220 // If we get a hotplug connected event for a display we already have,
221 // destroy the display and recreate it. This will force us to requery
222 // the display params and recreate all layers on that display.
223 if (displayData.hwcDisplay != nullptr && displayData.hwcDisplay->isConnected()) {
224 ALOGI("Hotplug connecting an already connected display."
225 " Clearing old display state.");
226 }
227 displayData.hwcDisplay.reset();
228 auto newDisplay =
229 std::make_unique<HWC2::impl::Display>(*mComposer.get(), mCapabilities, hwcDisplayId,
230 HWC2::DisplayType::Physical);
231 newDisplay->setConnected(true);
232 displayData.hwcDisplay = std::move(newDisplay);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700233 mPhysicalDisplayIdMap[hwcDisplayId] = info->id;
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800234 } else if (connection == HWC2::Connection::Disconnected) {
235 // The display will later be destroyed by a call to
236 // destroyDisplay(). For now we just mark it disconnected.
237 auto& displayData = mDisplayData[info->id];
238 if (displayData.hwcDisplay) {
239 displayData.hwcDisplay->setConnected(false);
240 } else {
241 ALOGW("Attempted to disconnect unknown display %" PRIu64, hwcDisplayId);
242 }
243 // The cleanup of Disconnect is handled through HWComposer::disconnectDisplay
244 // via SurfaceFlinger's onHotplugReceived callback handling
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700245 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700246
Dominik Laskowski075d3172018-05-24 15:50:06 -0700247 return info;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700248}
249
Dominik Laskowski075d3172018-05-24 15:50:06 -0700250bool HWComposer::onVsync(hwc2_display_t hwcDisplayId, int64_t timestamp) {
251 const auto displayId = toPhysicalDisplayId(hwcDisplayId);
252 if (!displayId) {
253 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, "Invalid HWC display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700254 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700255 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700256
Dominik Laskowski075d3172018-05-24 15:50:06 -0700257 RETURN_IF_INVALID_DISPLAY(*displayId, false);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700258
Dominik Laskowski1af47932018-11-12 10:20:46 -0800259 auto& displayData = mDisplayData[*displayId];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700260 if (displayData.isVirtual) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700261 LOG_DISPLAY_ERROR(*displayId, "Invalid operation on virtual display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700262 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700263 }
264
Dan Stoza9e56aa02015-11-02 13:00:03 -0800265 {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800266 std::lock_guard lock(displayData.lastHwVsyncLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700267
Dan Stoza9e56aa02015-11-02 13:00:03 -0800268 // There have been reports of HWCs that signal several vsync events
269 // with the same timestamp when turning the display off and on. This
270 // is a bug in the HWC implementation, but filter the extra events
271 // out here so they don't cause havoc downstream.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800272 if (timestamp == displayData.lastHwVsync) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700273 ALOGW("Ignoring duplicate VSYNC event from HWC for display %s (t=%" PRId64 ")",
274 to_string(*displayId).c_str(), timestamp);
Steven Thomas94e35b92017-07-26 18:48:28 -0700275 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800276 }
277
Dominik Laskowski1af47932018-11-12 10:20:46 -0800278 displayData.lastHwVsync = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700279 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800280
Dominik Laskowski34157762018-10-31 13:07:19 -0700281 const auto tag = "HW_VSYNC_" + to_string(*displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800282 ATRACE_INT(tag.c_str(), displayData.vsyncTraceToggle);
283 displayData.vsyncTraceToggle = !displayData.vsyncTraceToggle;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800284
Steven Thomas94e35b92017-07-26 18:48:28 -0700285 return true;
Jesse Hall1c569c42013-04-05 13:44:52 -0700286}
287
Dominik Laskowski075d3172018-05-24 15:50:06 -0700288std::optional<DisplayId> HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
289 ui::PixelFormat* format) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800290 if (mRemainingHwcVirtualDisplays == 0) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700291 ALOGE("%s: No remaining virtual displays", __FUNCTION__);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700292 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700293 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700294
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800295 if (SurfaceFlinger::maxVirtualDisplaySize != 0 &&
296 (width > SurfaceFlinger::maxVirtualDisplaySize ||
297 height > SurfaceFlinger::maxVirtualDisplaySize)) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700298 ALOGE("%s: Display size %ux%u exceeds maximum dimension of %" PRIu64, __FUNCTION__, width,
299 height, SurfaceFlinger::maxVirtualDisplaySize);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700300 return {};
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800301 }
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800302 hwc2_display_t hwcDisplayId = 0;
303 const auto error = static_cast<HWC2::Error>(
304 mComposer->createVirtualDisplay(width, height, format, &hwcDisplayId));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800305 if (error != HWC2::Error::None) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700306 ALOGE("%s: Failed to create HWC virtual display", __FUNCTION__);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700307 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700308 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800309
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800310 auto display = std::make_unique<HWC2::impl::Display>(*mComposer.get(), mCapabilities,
311 hwcDisplayId, HWC2::DisplayType::Virtual);
312 display->setConnected(true);
313
Dominik Laskowski075d3172018-05-24 15:50:06 -0700314 DisplayId displayId;
315 if (mFreeVirtualDisplayIds.empty()) {
316 displayId = getVirtualDisplayId(mNextVirtualDisplayId++);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800317 } else {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700318 displayId = *mFreeVirtualDisplayIds.begin();
319 mFreeVirtualDisplayIds.erase(displayId);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700320 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800321
Dominik Laskowski075d3172018-05-24 15:50:06 -0700322 auto& displayData = mDisplayData[displayId];
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800323 displayData.hwcDisplay = std::move(display);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700324 displayData.isVirtual = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800325
326 --mRemainingHwcVirtualDisplays;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700327 return displayId;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700328}
329
Dominik Laskowski075d3172018-05-24 15:50:06 -0700330HWC2::Layer* HWComposer::createLayer(DisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700331 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
332
Steven Thomas94e35b92017-07-26 18:48:28 -0700333 HWC2::Layer* layer;
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800334 auto error = mDisplayData[displayId].hwcDisplay->createLayer(&layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700335 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800336 return layer;
337}
338
Dominik Laskowski075d3172018-05-24 15:50:06 -0700339void HWComposer::destroyLayer(DisplayId displayId, HWC2::Layer* layer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700340 RETURN_IF_INVALID_DISPLAY(displayId);
341
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800342 auto error = mDisplayData[displayId].hwcDisplay->destroyLayer(layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700343 RETURN_IF_HWC_ERROR(error, displayId);
Steven Thomas94e35b92017-07-26 18:48:28 -0700344}
345
Dominik Laskowski075d3172018-05-24 15:50:06 -0700346nsecs_t HWComposer::getRefreshTimestamp(DisplayId displayId) const {
347 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800348 const auto& displayData = mDisplayData.at(displayId);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700349 // this returns the last refresh timestamp.
350 // if the last one is not available, we estimate it based on
351 // the refresh period and whatever closest timestamp we have.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800352 std::lock_guard lock(displayData.lastHwVsyncLock);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700353 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800354 auto vsyncPeriodNanos = getDisplayVsyncPeriod(displayId);
355 return now - ((now - displayData.lastHwVsync) % vsyncPeriodNanos);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700356}
357
Dominik Laskowski075d3172018-05-24 15:50:06 -0700358bool HWComposer::isConnected(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700359 RETURN_IF_INVALID_DISPLAY(displayId, false);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700360 return mDisplayData.at(displayId).hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700361}
362
Dominik Laskowski075d3172018-05-24 15:50:06 -0700363std::vector<std::shared_ptr<const HWC2::Display::Config>> HWComposer::getConfigs(
364 DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700365 RETURN_IF_INVALID_DISPLAY(displayId, {});
366
Dominik Laskowski075d3172018-05-24 15:50:06 -0700367 const auto& displayData = mDisplayData.at(displayId);
368 auto configs = displayData.hwcDisplay->getConfigs();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800369 if (displayData.configMap.empty()) {
370 for (size_t i = 0; i < configs.size(); ++i) {
371 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700372 }
373 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800374 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700375}
376
Dominik Laskowski075d3172018-05-24 15:50:06 -0700377std::shared_ptr<const HWC2::Display::Config> HWComposer::getActiveConfig(
378 DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700379 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
380
Dan Stoza9e56aa02015-11-02 13:00:03 -0800381 std::shared_ptr<const HWC2::Display::Config> config;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700382 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfig(&config);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800383 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700384 LOG_DISPLAY_ERROR(displayId, "No active config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800385 return nullptr;
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700386 }
387
388 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
389
390 if (!config) {
391 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800392 return nullptr;
393 }
394
395 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700396}
397
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800398// Composer 2.4
399
400bool HWComposer::isVsyncPeriodSwitchSupported(DisplayId displayId) const {
401 return mDisplayData.at(displayId).hwcDisplay->isVsyncPeriodSwitchSupported();
402}
403
404nsecs_t HWComposer::getDisplayVsyncPeriod(DisplayId displayId) const {
405 nsecs_t vsyncPeriodNanos;
406 auto error = mDisplayData.at(displayId).hwcDisplay->getDisplayVsyncPeriod(&vsyncPeriodNanos);
407 if (error != HWC2::Error::None) {
408 LOG_DISPLAY_ERROR(displayId, "Failed to get Vsync Period");
409 return 0;
410 }
411
412 return vsyncPeriodNanos;
413}
414
Dominik Laskowski075d3172018-05-24 15:50:06 -0700415int HWComposer::getActiveConfigIndex(DisplayId displayId) const {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700416 RETURN_IF_INVALID_DISPLAY(displayId, -1);
417
Lloyd Pique3c085a02018-05-09 19:38:32 -0700418 int index;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700419 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfigIndex(&index);
Lloyd Pique3c085a02018-05-09 19:38:32 -0700420 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700421 LOG_DISPLAY_ERROR(displayId, "No active config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700422 return -1;
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700423 }
424
425 RETURN_IF_HWC_ERROR(error, displayId, -1);
426
427 if (index < 0) {
428 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700429 return -1;
430 }
431
432 return index;
433}
434
Dominik Laskowski075d3172018-05-24 15:50:06 -0700435std::vector<ui::ColorMode> HWComposer::getColorModes(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700436 RETURN_IF_INVALID_DISPLAY(displayId, {});
437
Peiyong Linfd997e02018-03-28 15:29:00 -0700438 std::vector<ui::ColorMode> modes;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700439 auto error = mDisplayData.at(displayId).hwcDisplay->getColorModes(&modes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700440 RETURN_IF_HWC_ERROR(error, displayId, {});
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600441 return modes;
442}
443
Dominik Laskowski075d3172018-05-24 15:50:06 -0700444status_t HWComposer::setActiveColorMode(DisplayId displayId, ui::ColorMode mode,
445 ui::RenderIntent renderIntent) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700446 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Michael Wright28f24d02016-07-12 13:30:53 -0700447
448 auto& displayData = mDisplayData[displayId];
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700449 auto error = displayData.hwcDisplay->setColorMode(mode, renderIntent);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700450 RETURN_IF_HWC_ERROR_FOR(("setColorMode(" + decodeColorMode(mode) + ", " +
451 decodeRenderIntent(renderIntent) + ")")
452 .c_str(),
453 error, displayId, UNKNOWN_ERROR);
Michael Wright28f24d02016-07-12 13:30:53 -0700454
455 return NO_ERROR;
456}
457
Dominik Laskowski075d3172018-05-24 15:50:06 -0700458void HWComposer::setVsyncEnabled(DisplayId displayId, HWC2::Vsync enabled) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700459 RETURN_IF_INVALID_DISPLAY(displayId);
460 auto& displayData = mDisplayData[displayId];
461
462 if (displayData.isVirtual) {
463 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800464 return;
465 }
466
Dan Stoza9e56aa02015-11-02 13:00:03 -0800467 // NOTE: we use our own internal lock here because we have to call
468 // into the HWC with the lock held, and we want to make sure
469 // that even if HWC blocks (which it shouldn't), it won't
470 // affect other threads.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800471 std::lock_guard lock(displayData.vsyncEnabledLock);
472 if (enabled == displayData.vsyncEnabled) {
473 return;
Colin Cross10fbdb62012-07-12 17:56:34 -0700474 }
Dominik Laskowski1af47932018-11-12 10:20:46 -0800475
476 ATRACE_CALL();
477 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
478 RETURN_IF_HWC_ERROR(error, displayId);
479
480 displayData.vsyncEnabled = enabled;
481
482 const auto tag = "HW_VSYNC_ON_" + to_string(displayId);
483 ATRACE_INT(tag.c_str(), enabled == HWC2::Vsync::Enable ? 1 : 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700484}
485
Dominik Laskowski075d3172018-05-24 15:50:06 -0700486status_t HWComposer::setClientTarget(DisplayId displayId, uint32_t slot,
487 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
488 ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700489 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Jesse Hall851cfe82013-03-20 13:44:00 -0700490
Dominik Laskowski34157762018-10-31 13:07:19 -0700491 ALOGV("%s for display %s", __FUNCTION__, to_string(displayId).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800492 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400493 auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700494 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Jesse Hall851cfe82013-03-20 13:44:00 -0700495 return NO_ERROR;
496}
497
Lloyd Pique66d68602019-02-13 14:23:31 -0800498status_t HWComposer::getDeviceCompositionChanges(
499 DisplayId displayId, bool frameUsesClientComposition,
500 std::optional<android::HWComposer::DeviceRequestedChanges>* outChanges) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800501 ATRACE_CALL();
502
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700503 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800504
505 auto& displayData = mDisplayData[displayId];
506 auto& hwcDisplay = displayData.hwcDisplay;
507 if (!hwcDisplay->isConnected()) {
508 return NO_ERROR;
509 }
510
511 uint32_t numTypes = 0;
512 uint32_t numRequests = 0;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700513
514 HWC2::Error error = HWC2::Error::None;
515
Chia-I Wu41b98d42017-12-11 11:04:36 -0800516 // First try to skip validate altogether when there is no client
517 // composition. When there is client composition, since we haven't
518 // rendered to the client target yet, we should not attempt to skip
519 // validate.
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700520 displayData.validateWasSkipped = false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800521 if (!frameUsesClientComposition) {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800522 sp<Fence> outPresentFence;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700523 uint32_t state = UINT32_MAX;
524 error = hwcDisplay->presentOrValidate(&numTypes, &numRequests, &outPresentFence , &state);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700525 if (error != HWC2::Error::HasChanges) {
526 RETURN_IF_HWC_ERROR_FOR("presentOrValidate", error, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700527 }
528 if (state == 1) { //Present Succeeded.
Steven Thomas94e35b92017-07-26 18:48:28 -0700529 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700530 error = hwcDisplay->getReleaseFences(&releaseFences);
531 displayData.releaseFences = std::move(releaseFences);
532 displayData.lastPresentFence = outPresentFence;
533 displayData.validateWasSkipped = true;
534 displayData.presentError = error;
535 return NO_ERROR;
536 }
537 // Present failed but Validate ran.
538 } else {
539 error = hwcDisplay->validate(&numTypes, &numRequests);
540 }
541 ALOGV("SkipValidate failed, Falling back to SLOW validate/present");
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700542 if (error != HWC2::Error::HasChanges) {
543 RETURN_IF_HWC_ERROR_FOR("validate", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800544 }
545
Lloyd Pique66d68602019-02-13 14:23:31 -0800546 android::HWComposer::DeviceRequestedChanges::ChangedTypes changedTypes;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800547 changedTypes.reserve(numTypes);
548 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700549 RETURN_IF_HWC_ERROR_FOR("getChangedCompositionTypes", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800550
Lloyd Pique66d68602019-02-13 14:23:31 -0800551 auto displayRequests = static_cast<HWC2::DisplayRequest>(0);
552 android::HWComposer::DeviceRequestedChanges::LayerRequests layerRequests;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800553 layerRequests.reserve(numRequests);
Lloyd Pique66d68602019-02-13 14:23:31 -0800554 error = hwcDisplay->getRequests(&displayRequests, &layerRequests);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700555 RETURN_IF_HWC_ERROR_FOR("getRequests", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800556
Lloyd Pique66d68602019-02-13 14:23:31 -0800557 outChanges->emplace(DeviceRequestedChanges{std::move(changedTypes), std::move(displayRequests),
558 std::move(layerRequests)});
Dan Stoza9e56aa02015-11-02 13:00:03 -0800559
560 error = hwcDisplay->acceptChanges();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700561 RETURN_IF_HWC_ERROR_FOR("acceptChanges", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800562
563 return NO_ERROR;
564}
565
Dominik Laskowski075d3172018-05-24 15:50:06 -0700566sp<Fence> HWComposer::getPresentFence(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700567 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700568 return mDisplayData.at(displayId).lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700569}
570
Dominik Laskowski075d3172018-05-24 15:50:06 -0700571sp<Fence> HWComposer::getLayerReleaseFence(DisplayId displayId, HWC2::Layer* layer) 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 auto displayFences = mDisplayData.at(displayId).releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800574 if (displayFences.count(layer) == 0) {
575 ALOGV("getLayerReleaseFence: Release fence not found");
576 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700577 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800578 return displayFences[layer];
Riley Andrews03414a12014-07-01 14:22:59 -0700579}
580
Dominik Laskowski075d3172018-05-24 15:50:06 -0700581status_t HWComposer::presentAndGetReleaseFences(DisplayId displayId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800582 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700583
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700584 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700585
Dan Stoza9e56aa02015-11-02 13:00:03 -0800586 auto& displayData = mDisplayData[displayId];
587 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700588
589 if (displayData.validateWasSkipped) {
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700590 // explicitly flush all pending commands
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800591 auto error = static_cast<HWC2::Error>(mComposer->executeCommands());
592 RETURN_IF_HWC_ERROR_FOR("executeCommands", error, displayId, UNKNOWN_ERROR);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700593 RETURN_IF_HWC_ERROR_FOR("present", displayData.presentError, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700594 return NO_ERROR;
595 }
596
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800597 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700598 RETURN_IF_HWC_ERROR_FOR("present", error, displayId, UNKNOWN_ERROR);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700599
Steven Thomas94e35b92017-07-26 18:48:28 -0700600 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800601 error = hwcDisplay->getReleaseFences(&releaseFences);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700602 RETURN_IF_HWC_ERROR_FOR("getReleaseFences", error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800603
604 displayData.releaseFences = std::move(releaseFences);
605
606 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700607}
608
Dominik Laskowski075d3172018-05-24 15:50:06 -0700609status_t HWComposer::setPowerMode(DisplayId displayId, int32_t intMode) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700610 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
611
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700612 const auto& displayData = mDisplayData[displayId];
613 if (displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700614 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
615 return INVALID_OPERATION;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800616 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700617
Dan Stoza9e56aa02015-11-02 13:00:03 -0800618 auto mode = static_cast<HWC2::PowerMode>(intMode);
619 if (mode == HWC2::PowerMode::Off) {
620 setVsyncEnabled(displayId, HWC2::Vsync::Disable);
621 }
622
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700623 auto& hwcDisplay = displayData.hwcDisplay;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800624 switch (mode) {
625 case HWC2::PowerMode::Off:
626 case HWC2::PowerMode::On:
627 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
628 {
629 auto error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700630 if (error != HWC2::Error::None) {
631 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
632 error, displayId);
633 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700634 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800635 break;
636 case HWC2::PowerMode::Doze:
637 case HWC2::PowerMode::DozeSuspend:
638 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
639 {
640 bool supportsDoze = false;
641 auto error = hwcDisplay->supportsDoze(&supportsDoze);
Peiyong Lin306e4992018-05-07 16:18:22 -0700642 if (error != HWC2::Error::None) {
643 LOG_HWC_ERROR("supportsDoze", error, displayId);
644 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700645
Dan Stoza9e56aa02015-11-02 13:00:03 -0800646 if (!supportsDoze) {
647 mode = HWC2::PowerMode::On;
648 }
649
650 error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700651 if (error != HWC2::Error::None) {
652 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
653 error, displayId);
654 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800655 }
656 break;
657 default:
658 ALOGV("setPowerMode: Not calling HWC");
659 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700660 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800661
662 return NO_ERROR;
663}
664
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800665status_t HWComposer::setActiveConfigWithConstraints(
666 DisplayId displayId, size_t configId, const HWC2::VsyncPeriodChangeConstraints& constraints,
667 HWC2::VsyncPeriodChangeTimeline* outTimeline) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700668 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800669
670 auto& displayData = mDisplayData[displayId];
671 if (displayData.configMap.count(configId) == 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700672 LOG_DISPLAY_ERROR(displayId, ("Invalid config " + std::to_string(configId)).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800673 return BAD_INDEX;
674 }
675
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800676 auto error =
677 displayData.hwcDisplay->setActiveConfigWithConstraints(displayData.configMap[configId],
678 constraints, outTimeline);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700679 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800680 return NO_ERROR;
681}
682
Dominik Laskowski075d3172018-05-24 15:50:06 -0700683status_t HWComposer::setColorTransform(DisplayId displayId, const mat4& transform) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700684 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700685
686 auto& displayData = mDisplayData[displayId];
687 bool isIdentity = transform == mat4();
688 auto error = displayData.hwcDisplay->setColorTransform(transform,
689 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY :
690 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700691 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700692 return NO_ERROR;
693}
694
Dominik Laskowski075d3172018-05-24 15:50:06 -0700695void HWComposer::disconnectDisplay(DisplayId displayId) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700696 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800697 auto& displayData = mDisplayData[displayId];
698
Dan Stoza9e56aa02015-11-02 13:00:03 -0800699 // If this was a virtual display, add its slot back for reuse by future
700 // virtual displays
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700701 if (displayData.isVirtual) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700702 mFreeVirtualDisplayIds.insert(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800703 ++mRemainingHwcVirtualDisplays;
704 }
705
Dominik Laskowski7e045462018-05-30 13:02:02 -0700706 const auto hwcDisplayId = displayData.hwcDisplay->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700707
708 // TODO(b/74619554): Select internal/external display from remaining displays.
709 if (hwcDisplayId == mInternalHwcDisplayId) {
710 mInternalHwcDisplayId.reset();
711 } else if (hwcDisplayId == mExternalHwcDisplayId) {
712 mExternalHwcDisplayId.reset();
713 }
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800714 mPhysicalDisplayIdMap.erase(hwcDisplayId);
715 mDisplayData.erase(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800716}
717
Dominik Laskowski075d3172018-05-24 15:50:06 -0700718status_t HWComposer::setOutputBuffer(DisplayId displayId, const sp<Fence>& acquireFence,
719 const sp<GraphicBuffer>& buffer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700720 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700721 const auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800722
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700723 if (!displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700724 LOG_DISPLAY_ERROR(displayId, "Invalid operation on physical display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800725 return INVALID_OPERATION;
726 }
727
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700728 auto error = displayData.hwcDisplay->setOutputBuffer(buffer, acquireFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700729 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800730 return NO_ERROR;
731}
732
Dominik Laskowski075d3172018-05-24 15:50:06 -0700733void HWComposer::clearReleaseFences(DisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700734 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800735 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700736}
737
Dominik Laskowski075d3172018-05-24 15:50:06 -0700738status_t HWComposer::getHdrCapabilities(DisplayId displayId, HdrCapabilities* outCapabilities) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700739 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stozac4f471e2016-03-24 09:31:08 -0700740
741 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Peiyong Lin62665892018-04-16 11:07:44 -0700742 auto error = hwcDisplay->getHdrCapabilities(outCapabilities);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700743 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Peiyong Lin62665892018-04-16 11:07:44 -0700744 return NO_ERROR;
Dan Stozac4f471e2016-03-24 09:31:08 -0700745}
746
Dominik Laskowski075d3172018-05-24 15:50:06 -0700747int32_t HWComposer::getSupportedPerFrameMetadata(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700748 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700749 return mDisplayData.at(displayId).hwcDisplay->getSupportedPerFrameMetadata();
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700750}
751
Dominik Laskowski075d3172018-05-24 15:50:06 -0700752std::vector<ui::RenderIntent> HWComposer::getRenderIntents(DisplayId displayId,
753 ui::ColorMode colorMode) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700754 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700755
756 std::vector<ui::RenderIntent> renderIntents;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700757 auto error = mDisplayData.at(displayId).hwcDisplay->getRenderIntents(colorMode, &renderIntents);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700758 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700759 return renderIntents;
760}
761
Dominik Laskowski075d3172018-05-24 15:50:06 -0700762mat4 HWComposer::getDataspaceSaturationMatrix(DisplayId displayId, ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700763 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700764
765 mat4 matrix;
766 auto error = mDisplayData[displayId].hwcDisplay->getDataspaceSaturationMatrix(dataspace,
767 &matrix);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700768 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700769 return matrix;
770}
771
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700772status_t HWComposer::getDisplayedContentSamplingAttributes(DisplayId displayId,
773 ui::PixelFormat* outFormat,
774 ui::Dataspace* outDataspace,
775 uint8_t* outComponentMask) {
776 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
777 const auto error =
778 mDisplayData[displayId]
779 .hwcDisplay->getDisplayedContentSamplingAttributes(outFormat, outDataspace,
780 outComponentMask);
781 if (error == HWC2::Error::Unsupported) RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
782 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
783 return NO_ERROR;
784}
785
Kevin DuBois74e53772018-11-19 10:52:38 -0800786status_t HWComposer::setDisplayContentSamplingEnabled(DisplayId displayId, bool enabled,
787 uint8_t componentMask, uint64_t maxFrames) {
788 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
789 const auto error =
790 mDisplayData[displayId].hwcDisplay->setDisplayContentSamplingEnabled(enabled,
791 componentMask,
792 maxFrames);
793
794 if (error == HWC2::Error::Unsupported) RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
795 if (error == HWC2::Error::BadParameter) RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
796 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
797 return NO_ERROR;
798}
799
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700800status_t HWComposer::getDisplayedContentSample(DisplayId displayId, uint64_t maxFrames,
801 uint64_t timestamp, DisplayedFrameStats* outStats) {
802 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
803 const auto error =
804 mDisplayData[displayId].hwcDisplay->getDisplayedContentSample(maxFrames, timestamp,
805 outStats);
806 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
807 return NO_ERROR;
808}
809
Dan Gittik57e63c52019-01-18 16:37:54 +0000810status_t HWComposer::setDisplayBrightness(DisplayId displayId, float brightness) {
811 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
812 const auto error = mDisplayData[displayId].hwcDisplay->setDisplayBrightness(brightness);
813 if (error == HWC2::Error::Unsupported) {
814 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
815 }
816 if (error == HWC2::Error::BadParameter) {
817 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
818 }
819 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
820 return NO_ERROR;
821}
822
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800823bool HWComposer::isUsingVrComposer() const {
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800824 return getComposer()->isUsingVrComposer();
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800825}
826
Galia Peycheva5492cb52019-10-30 14:13:16 +0100827status_t HWComposer::setAutoLowLatencyMode(DisplayId displayId, bool on) {
828 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
829 const auto error = mDisplayData[displayId].hwcDisplay->setAutoLowLatencyMode(on);
830 if (error == HWC2::Error::Unsupported) {
831 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
832 }
833 if (error == HWC2::Error::BadParameter) {
834 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
835 }
836 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
837 return NO_ERROR;
838}
839
840status_t HWComposer::getSupportedContentTypes(
841 DisplayId displayId, std::vector<HWC2::ContentType>* outSupportedContentTypes) {
842 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
843 const auto error =
844 mDisplayData[displayId].hwcDisplay->getSupportedContentTypes(outSupportedContentTypes);
845
846 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
847
848 return NO_ERROR;
849}
850
851status_t HWComposer::setContentType(DisplayId displayId, HWC2::ContentType contentType) {
852 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
853 const auto error = mDisplayData[displayId].hwcDisplay->setContentType(contentType);
854 if (error == HWC2::Error::Unsupported) {
855 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
856 }
857 if (error == HWC2::Error::BadParameter) {
858 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
859 }
860 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
861
862 return NO_ERROR;
863}
864
Yiwei Zhang5434a782018-12-05 18:06:32 -0800865void HWComposer::dump(std::string& result) const {
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800866 result.append(mComposer->dumpDebugInfo());
Mathias Agopian83727852010-09-23 18:13:21 -0700867}
868
Dominik Laskowski075d3172018-05-24 15:50:06 -0700869std::optional<DisplayId> HWComposer::toPhysicalDisplayId(hwc2_display_t hwcDisplayId) const {
870 if (const auto it = mPhysicalDisplayIdMap.find(hwcDisplayId);
871 it != mPhysicalDisplayIdMap.end()) {
872 return it->second;
873 }
874 return {};
875}
876
877std::optional<hwc2_display_t> HWComposer::fromPhysicalDisplayId(DisplayId displayId) const {
878 if (const auto it = mDisplayData.find(displayId);
879 it != mDisplayData.end() && !it->second.isVirtual) {
880 return it->second.hwcDisplay->getId();
881 }
882 return {};
883}
884
885std::optional<DisplayIdentificationInfo> HWComposer::onHotplugConnect(hwc2_display_t hwcDisplayId) {
886 if (isUsingVrComposer() && mInternalHwcDisplayId) {
887 ALOGE("Ignoring connection of external display %" PRIu64 " in VR mode", hwcDisplayId);
Steven Thomas6e8f7062017-11-22 14:15:29 -0800888 return {};
889 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700890
891 uint8_t port;
892 DisplayIdentificationData data;
893 const bool hasMultiDisplaySupport = getDisplayIdentificationData(hwcDisplayId, &port, &data);
894
895 if (mPhysicalDisplayIdMap.empty()) {
896 mHasMultiDisplaySupport = hasMultiDisplaySupport;
897 ALOGI("Switching to %s multi-display mode",
898 hasMultiDisplaySupport ? "generalized" : "legacy");
899 } else if (mHasMultiDisplaySupport && !hasMultiDisplaySupport) {
900 ALOGE("Ignoring connection of display %" PRIu64 " without identification data",
901 hwcDisplayId);
902 return {};
903 }
904
905 std::optional<DisplayIdentificationInfo> info;
906
907 if (mHasMultiDisplaySupport) {
908 info = parseDisplayIdentificationData(port, data);
909 ALOGE_IF(!info, "Failed to parse identification data for display %" PRIu64, hwcDisplayId);
910 } else if (mInternalHwcDisplayId && mExternalHwcDisplayId) {
911 ALOGE("Ignoring connection of tertiary display %" PRIu64, hwcDisplayId);
912 return {};
913 } else {
914 ALOGW_IF(hasMultiDisplaySupport, "Ignoring identification data for display %" PRIu64,
915 hwcDisplayId);
916 port = mInternalHwcDisplayId ? HWC_DISPLAY_EXTERNAL : HWC_DISPLAY_PRIMARY;
917 }
918
919 if (!mInternalHwcDisplayId) {
920 mInternalHwcDisplayId = hwcDisplayId;
921 } else if (!mExternalHwcDisplayId) {
922 mExternalHwcDisplayId = hwcDisplayId;
923 }
924
925 if (info) return info;
926
927 return DisplayIdentificationInfo{getFallbackDisplayId(port),
928 hwcDisplayId == mInternalHwcDisplayId ? "Internal display"
929 : "External display"};
Steven Thomas6e8f7062017-11-22 14:15:29 -0800930}
931
Peiyong Linbdd08cc2019-12-17 21:35:14 -0800932void HWComposer::loadCapabilities() {
933 static_assert(sizeof(HWC2::Capability) == sizeof(int32_t), "Capability size has changed");
934 auto capabilities = mComposer->getCapabilities();
935 for (auto capability : capabilities) {
936 mCapabilities.emplace(static_cast<HWC2::Capability>(capability));
937 }
938}
939
940uint32_t HWComposer::getMaxVirtualDisplayCount() const {
941 return mComposer->getMaxVirtualDisplayCount();
942}
943
Lloyd Pique441d5042018-10-18 16:49:51 -0700944} // namespace impl
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700945} // namespace android