blob: 1099041b4b069e9e2e50130990b4853339a61ed6 [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 Pique37c2c9b2018-12-04 17:25:10 -080023#include <compositionengine/Output.h>
24#include <compositionengine/OutputLayer.h>
25#include <compositionengine/impl/OutputLayerCompositionState.h>
26#include <log/log.h>
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070027#include <ui/DebugUtils.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070028#include <ui/GraphicBuffer.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080029#include <utils/Errors.h>
30#include <utils/Trace.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070031
Mathias Agopiana350ff92010-08-10 17:14:02 -070032#include "HWComposer.h"
Dan Stoza9e56aa02015-11-02 13:00:03 -080033#include "HWC2.h"
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -080034#include "ComposerHal.h"
Mathias Agopian33ceeb32013-04-01 16:54:58 -070035
36#include "../Layer.h" // needed only for debugging
37#include "../SurfaceFlinger.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
Mathias Agopiana350ff92010-08-10 17:14:02 -070068namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -070069
Lloyd Pique441d5042018-10-18 16:49:51 -070070HWComposer::~HWComposer() = default;
71
72namespace impl {
73
Dominik Laskowski1af47932018-11-12 10:20:46 -080074HWComposer::HWComposer(std::unique_ptr<Hwc2::Composer> composer)
Lloyd Piquea822d522017-12-20 16:42:57 -080075 : mHwcDevice(std::make_unique<HWC2::Device>(std::move(composer))) {}
Mathias Agopianbef42c52013-08-21 17:45:46 -070076
Dominik Laskowskib04f98a2018-11-07 21:07:16 -080077HWComposer::~HWComposer() {
78 mDisplayData.clear();
79}
Dan Stoza9e56aa02015-11-02 13:00:03 -080080
Steven Thomas94e35b92017-07-26 18:48:28 -070081void HWComposer::registerCallback(HWC2::ComposerCallback* callback,
82 int32_t sequenceId) {
83 mHwcDevice->registerCallback(callback, sequenceId);
Dan Stoza9e56aa02015-11-02 13:00:03 -080084}
85
Dominik Laskowskia2edf612018-06-01 13:15:16 -070086bool HWComposer::getDisplayIdentificationData(hwc2_display_t hwcDisplayId, uint8_t* outPort,
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070087 DisplayIdentificationData* outData) const {
Dominik Laskowski0954b1d2018-06-13 14:58:49 -070088 const auto error = mHwcDevice->getDisplayIdentificationData(hwcDisplayId, outPort, outData);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070089 if (error != HWC2::Error::None) {
Chia-I Wud0aff9d2018-06-21 13:39:09 +080090 if (error != HWC2::Error::Unsupported) {
91 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, to_string(error).c_str());
92 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070093 return false;
94 }
95 return true;
96}
97
Dan Stoza9f26a9c2016-06-22 14:51:09 -070098bool HWComposer::hasCapability(HWC2::Capability capability) const
99{
100 return mHwcDevice->getCapabilities().count(capability) > 0;
101}
102
Peiyong Lined531a32018-10-26 18:27:56 -0700103bool HWComposer::hasDisplayCapability(const std::optional<DisplayId>& displayId,
104 HWC2::DisplayCapability capability) const {
105 if (!displayId) {
Peiyong Lindf65fd22019-01-03 15:17:05 -0800106 // Checkout global capabilities for displays without a corresponding HWC display.
107 if (capability == HWC2::DisplayCapability::SkipClientColorTransform) {
108 return hasCapability(HWC2::Capability::SkipClientColorTransform);
109 }
Peiyong Lined531a32018-10-26 18:27:56 -0700110 return false;
111 }
112 RETURN_IF_INVALID_DISPLAY(*displayId, false);
113 return mDisplayData.at(*displayId).hwcDisplay->getCapabilities().count(capability) > 0;
114}
115
Dan Stoza9e56aa02015-11-02 13:00:03 -0800116void HWComposer::validateChange(HWC2::Composition from, HWC2::Composition to) {
117 bool valid = true;
118 switch (from) {
119 case HWC2::Composition::Client:
120 valid = false;
121 break;
122 case HWC2::Composition::Device:
123 case HWC2::Composition::SolidColor:
124 valid = (to == HWC2::Composition::Client);
125 break;
126 case HWC2::Composition::Cursor:
127 case HWC2::Composition::Sideband:
128 valid = (to == HWC2::Composition::Client ||
129 to == HWC2::Composition::Device);
130 break;
131 default:
132 break;
133 }
134
135 if (!valid) {
136 ALOGE("Invalid layer type change: %s --> %s", to_string(from).c_str(),
137 to_string(to).c_str());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700138 }
139}
140
Dominik Laskowski075d3172018-05-24 15:50:06 -0700141std::optional<DisplayIdentificationInfo> HWComposer::onHotplug(hwc2_display_t hwcDisplayId,
142 HWC2::Connection connection) {
143 std::optional<DisplayIdentificationInfo> info;
Lloyd Pique715a2c12017-12-14 17:18:08 -0800144
Dominik Laskowski075d3172018-05-24 15:50:06 -0700145 if (const auto displayId = toPhysicalDisplayId(hwcDisplayId)) {
146 info = DisplayIdentificationInfo{*displayId, std::string()};
147 } else {
148 if (connection == HWC2::Connection::Disconnected) {
149 ALOGE("Ignoring disconnection of invalid HWC display %" PRIu64, hwcDisplayId);
150 return {};
Lloyd Pique438e9e72018-09-04 18:06:08 -0700151 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700152
153 info = onHotplugConnect(hwcDisplayId);
154 if (!info) return {};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700155 }
156
Dominik Laskowski34157762018-10-31 13:07:19 -0700157 ALOGV("%s: %s %s display %s with HWC ID %" PRIu64, __FUNCTION__, to_string(connection).c_str(),
158 hwcDisplayId == mInternalHwcDisplayId ? "internal" : "external",
159 to_string(info->id).c_str(), hwcDisplayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700160
161 mHwcDevice->onHotplug(hwcDisplayId, connection);
162
Lloyd Pique715a2c12017-12-14 17:18:08 -0800163 // Disconnect is handled through HWComposer::disconnectDisplay via
164 // SurfaceFlinger's onHotplugReceived callback handling
165 if (connection == HWC2::Connection::Connected) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700166 mDisplayData[info->id].hwcDisplay = mHwcDevice->getDisplayById(hwcDisplayId);
167 mPhysicalDisplayIdMap[hwcDisplayId] = info->id;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700168 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700169
Dominik Laskowski075d3172018-05-24 15:50:06 -0700170 return info;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700171}
172
Dominik Laskowski075d3172018-05-24 15:50:06 -0700173bool HWComposer::onVsync(hwc2_display_t hwcDisplayId, int64_t timestamp) {
174 const auto displayId = toPhysicalDisplayId(hwcDisplayId);
175 if (!displayId) {
176 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, "Invalid HWC display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700177 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700178 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700179
Dominik Laskowski075d3172018-05-24 15:50:06 -0700180 RETURN_IF_INVALID_DISPLAY(*displayId, false);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700181
Dominik Laskowski1af47932018-11-12 10:20:46 -0800182 auto& displayData = mDisplayData[*displayId];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700183 if (displayData.isVirtual) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700184 LOG_DISPLAY_ERROR(*displayId, "Invalid operation on virtual display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700185 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700186 }
187
Dan Stoza9e56aa02015-11-02 13:00:03 -0800188 {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800189 std::lock_guard lock(displayData.lastHwVsyncLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700190
Dan Stoza9e56aa02015-11-02 13:00:03 -0800191 // There have been reports of HWCs that signal several vsync events
192 // with the same timestamp when turning the display off and on. This
193 // is a bug in the HWC implementation, but filter the extra events
194 // out here so they don't cause havoc downstream.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800195 if (timestamp == displayData.lastHwVsync) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700196 ALOGW("Ignoring duplicate VSYNC event from HWC for display %s (t=%" PRId64 ")",
197 to_string(*displayId).c_str(), timestamp);
Steven Thomas94e35b92017-07-26 18:48:28 -0700198 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800199 }
200
Dominik Laskowski1af47932018-11-12 10:20:46 -0800201 displayData.lastHwVsync = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700202 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800203
Dominik Laskowski34157762018-10-31 13:07:19 -0700204 const auto tag = "HW_VSYNC_" + to_string(*displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800205 ATRACE_INT(tag.c_str(), displayData.vsyncTraceToggle);
206 displayData.vsyncTraceToggle = !displayData.vsyncTraceToggle;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800207
Steven Thomas94e35b92017-07-26 18:48:28 -0700208 return true;
Jesse Hall1c569c42013-04-05 13:44:52 -0700209}
210
Dominik Laskowski075d3172018-05-24 15:50:06 -0700211std::optional<DisplayId> HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
212 ui::PixelFormat* format) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800213 if (mRemainingHwcVirtualDisplays == 0) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700214 ALOGE("%s: No remaining virtual displays", __FUNCTION__);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700215 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700216 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700217
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800218 if (SurfaceFlinger::maxVirtualDisplaySize != 0 &&
219 (width > SurfaceFlinger::maxVirtualDisplaySize ||
220 height > SurfaceFlinger::maxVirtualDisplaySize)) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700221 ALOGE("%s: Display size %ux%u exceeds maximum dimension of %" PRIu64, __FUNCTION__, width,
222 height, SurfaceFlinger::maxVirtualDisplaySize);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700223 return {};
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800224 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700225 HWC2::Display* display;
Dan Stoza5cf424b2016-05-20 14:02:39 -0700226 auto error = mHwcDevice->createVirtualDisplay(width, height, format,
227 &display);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800228 if (error != HWC2::Error::None) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700229 ALOGE("%s: Failed to create HWC virtual display", __FUNCTION__);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700230 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700231 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800232
Dominik Laskowski075d3172018-05-24 15:50:06 -0700233 DisplayId displayId;
234 if (mFreeVirtualDisplayIds.empty()) {
235 displayId = getVirtualDisplayId(mNextVirtualDisplayId++);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800236 } else {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700237 displayId = *mFreeVirtualDisplayIds.begin();
238 mFreeVirtualDisplayIds.erase(displayId);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700239 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800240
Dominik Laskowski075d3172018-05-24 15:50:06 -0700241 auto& displayData = mDisplayData[displayId];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700242 displayData.hwcDisplay = display;
243 displayData.isVirtual = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800244
245 --mRemainingHwcVirtualDisplays;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700246 return displayId;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700247}
248
Dominik Laskowski075d3172018-05-24 15:50:06 -0700249HWC2::Layer* HWComposer::createLayer(DisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700250 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
251
Dan Stoza9e56aa02015-11-02 13:00:03 -0800252 auto display = mDisplayData[displayId].hwcDisplay;
Steven Thomas94e35b92017-07-26 18:48:28 -0700253 HWC2::Layer* layer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800254 auto error = display->createLayer(&layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700255 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800256 return layer;
257}
258
Dominik Laskowski075d3172018-05-24 15:50:06 -0700259void HWComposer::destroyLayer(DisplayId displayId, HWC2::Layer* layer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700260 RETURN_IF_INVALID_DISPLAY(displayId);
261
Steven Thomas94e35b92017-07-26 18:48:28 -0700262 auto display = mDisplayData[displayId].hwcDisplay;
263 auto error = display->destroyLayer(layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700264 RETURN_IF_HWC_ERROR(error, displayId);
Steven Thomas94e35b92017-07-26 18:48:28 -0700265}
266
Dominik Laskowski075d3172018-05-24 15:50:06 -0700267nsecs_t HWComposer::getRefreshTimestamp(DisplayId displayId) const {
268 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800269 const auto& displayData = mDisplayData.at(displayId);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700270 // this returns the last refresh timestamp.
271 // if the last one is not available, we estimate it based on
272 // the refresh period and whatever closest timestamp we have.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800273 std::lock_guard lock(displayData.lastHwVsyncLock);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700274 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800275 auto vsyncPeriod = getActiveConfig(displayId)->getVsyncPeriod();
Dominik Laskowski1af47932018-11-12 10:20:46 -0800276 return now - ((now - displayData.lastHwVsync) % vsyncPeriod);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700277}
278
Dominik Laskowski075d3172018-05-24 15:50:06 -0700279bool HWComposer::isConnected(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700280 RETURN_IF_INVALID_DISPLAY(displayId, false);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700281 return mDisplayData.at(displayId).hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700282}
283
Dominik Laskowski075d3172018-05-24 15:50:06 -0700284std::vector<std::shared_ptr<const HWC2::Display::Config>> HWComposer::getConfigs(
285 DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700286 RETURN_IF_INVALID_DISPLAY(displayId, {});
287
Dominik Laskowski075d3172018-05-24 15:50:06 -0700288 const auto& displayData = mDisplayData.at(displayId);
289 auto configs = displayData.hwcDisplay->getConfigs();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800290 if (displayData.configMap.empty()) {
291 for (size_t i = 0; i < configs.size(); ++i) {
292 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700293 }
294 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800295 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700296}
297
Dominik Laskowski075d3172018-05-24 15:50:06 -0700298std::shared_ptr<const HWC2::Display::Config> HWComposer::getActiveConfig(
299 DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700300 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
301
Dan Stoza9e56aa02015-11-02 13:00:03 -0800302 std::shared_ptr<const HWC2::Display::Config> config;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700303 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfig(&config);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800304 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700305 LOG_DISPLAY_ERROR(displayId, "No active config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800306 return nullptr;
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700307 }
308
309 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
310
311 if (!config) {
312 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800313 return nullptr;
314 }
315
316 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700317}
318
Dominik Laskowski075d3172018-05-24 15:50:06 -0700319int HWComposer::getActiveConfigIndex(DisplayId displayId) const {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700320 RETURN_IF_INVALID_DISPLAY(displayId, -1);
321
Lloyd Pique3c085a02018-05-09 19:38:32 -0700322 int index;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700323 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfigIndex(&index);
Lloyd Pique3c085a02018-05-09 19:38:32 -0700324 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700325 LOG_DISPLAY_ERROR(displayId, "No active config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700326 return -1;
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700327 }
328
329 RETURN_IF_HWC_ERROR(error, displayId, -1);
330
331 if (index < 0) {
332 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700333 return -1;
334 }
335
336 return index;
337}
338
Dominik Laskowski075d3172018-05-24 15:50:06 -0700339std::vector<ui::ColorMode> HWComposer::getColorModes(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700340 RETURN_IF_INVALID_DISPLAY(displayId, {});
341
Peiyong Linfd997e02018-03-28 15:29:00 -0700342 std::vector<ui::ColorMode> modes;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700343 auto error = mDisplayData.at(displayId).hwcDisplay->getColorModes(&modes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700344 RETURN_IF_HWC_ERROR(error, displayId, {});
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600345 return modes;
346}
347
Dominik Laskowski075d3172018-05-24 15:50:06 -0700348status_t HWComposer::setActiveColorMode(DisplayId displayId, ui::ColorMode mode,
349 ui::RenderIntent renderIntent) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700350 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Michael Wright28f24d02016-07-12 13:30:53 -0700351
352 auto& displayData = mDisplayData[displayId];
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700353 auto error = displayData.hwcDisplay->setColorMode(mode, renderIntent);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700354 RETURN_IF_HWC_ERROR_FOR(("setColorMode(" + decodeColorMode(mode) + ", " +
355 decodeRenderIntent(renderIntent) + ")")
356 .c_str(),
357 error, displayId, UNKNOWN_ERROR);
Michael Wright28f24d02016-07-12 13:30:53 -0700358
359 return NO_ERROR;
360}
361
Dominik Laskowski075d3172018-05-24 15:50:06 -0700362void HWComposer::setVsyncEnabled(DisplayId displayId, HWC2::Vsync enabled) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700363 RETURN_IF_INVALID_DISPLAY(displayId);
364 auto& displayData = mDisplayData[displayId];
365
366 if (displayData.isVirtual) {
367 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800368 return;
369 }
370
Dan Stoza9e56aa02015-11-02 13:00:03 -0800371 // NOTE: we use our own internal lock here because we have to call
372 // into the HWC with the lock held, and we want to make sure
373 // that even if HWC blocks (which it shouldn't), it won't
374 // affect other threads.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800375 std::lock_guard lock(displayData.vsyncEnabledLock);
376 if (enabled == displayData.vsyncEnabled) {
377 return;
Colin Cross10fbdb62012-07-12 17:56:34 -0700378 }
Dominik Laskowski1af47932018-11-12 10:20:46 -0800379
380 ATRACE_CALL();
381 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
382 RETURN_IF_HWC_ERROR(error, displayId);
383
384 displayData.vsyncEnabled = enabled;
385
386 const auto tag = "HW_VSYNC_ON_" + to_string(displayId);
387 ATRACE_INT(tag.c_str(), enabled == HWC2::Vsync::Enable ? 1 : 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700388}
389
Dominik Laskowski075d3172018-05-24 15:50:06 -0700390status_t HWComposer::setClientTarget(DisplayId displayId, uint32_t slot,
391 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
392 ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700393 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Jesse Hall851cfe82013-03-20 13:44:00 -0700394
Dominik Laskowski34157762018-10-31 13:07:19 -0700395 ALOGV("%s for display %s", __FUNCTION__, to_string(displayId).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800396 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400397 auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700398 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Jesse Hall851cfe82013-03-20 13:44:00 -0700399 return NO_ERROR;
400}
401
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800402status_t HWComposer::prepare(DisplayId displayId, const compositionengine::Output& output) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800403 ATRACE_CALL();
404
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700405 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800406
407 auto& displayData = mDisplayData[displayId];
408 auto& hwcDisplay = displayData.hwcDisplay;
409 if (!hwcDisplay->isConnected()) {
410 return NO_ERROR;
411 }
412
413 uint32_t numTypes = 0;
414 uint32_t numRequests = 0;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700415
416 HWC2::Error error = HWC2::Error::None;
417
Chia-I Wu41b98d42017-12-11 11:04:36 -0800418 // First try to skip validate altogether when there is no client
419 // composition. When there is client composition, since we haven't
420 // rendered to the client target yet, we should not attempt to skip
421 // validate.
422 //
423 // displayData.hasClientComposition hasn't been updated for this frame.
424 // The check below is incorrect. We actually rely on HWC here to fall
425 // back to validate when there is any client layer.
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700426 displayData.validateWasSkipped = false;
Chia-I Wu41b98d42017-12-11 11:04:36 -0800427 if (!displayData.hasClientComposition) {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800428 sp<Fence> outPresentFence;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700429 uint32_t state = UINT32_MAX;
430 error = hwcDisplay->presentOrValidate(&numTypes, &numRequests, &outPresentFence , &state);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700431 if (error != HWC2::Error::HasChanges) {
432 RETURN_IF_HWC_ERROR_FOR("presentOrValidate", error, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700433 }
434 if (state == 1) { //Present Succeeded.
Steven Thomas94e35b92017-07-26 18:48:28 -0700435 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700436 error = hwcDisplay->getReleaseFences(&releaseFences);
437 displayData.releaseFences = std::move(releaseFences);
438 displayData.lastPresentFence = outPresentFence;
439 displayData.validateWasSkipped = true;
440 displayData.presentError = error;
441 return NO_ERROR;
442 }
443 // Present failed but Validate ran.
444 } else {
445 error = hwcDisplay->validate(&numTypes, &numRequests);
446 }
447 ALOGV("SkipValidate failed, Falling back to SLOW validate/present");
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700448 if (error != HWC2::Error::HasChanges) {
449 RETURN_IF_HWC_ERROR_FOR("validate", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800450 }
451
Steven Thomas94e35b92017-07-26 18:48:28 -0700452 std::unordered_map<HWC2::Layer*, HWC2::Composition> changedTypes;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800453 changedTypes.reserve(numTypes);
454 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700455 RETURN_IF_HWC_ERROR_FOR("getChangedCompositionTypes", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800456
457 displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0);
Steven Thomas94e35b92017-07-26 18:48:28 -0700458 std::unordered_map<HWC2::Layer*, HWC2::LayerRequest> layerRequests;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800459 layerRequests.reserve(numRequests);
460 error = hwcDisplay->getRequests(&displayData.displayRequests,
461 &layerRequests);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700462 RETURN_IF_HWC_ERROR_FOR("getRequests", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800463
464 displayData.hasClientComposition = false;
465 displayData.hasDeviceComposition = false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800466 for (auto& outputLayer : output.getOutputLayersOrderedByZ()) {
467 auto& state = outputLayer->editState();
468 LOG_FATAL_IF(!state.hwc.);
469 auto hwcLayer = (*state.hwc).hwcLayer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800470
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800471 if (auto it = changedTypes.find(hwcLayer.get()); it != changedTypes.end()) {
472 auto newCompositionType = it->second;
473 validateChange(static_cast<HWC2::Composition>((*state.hwc).hwcCompositionType),
474 newCompositionType);
475 (*state.hwc).hwcCompositionType =
476 static_cast<Hwc2::IComposerClient::Composition>(newCompositionType);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800477 }
478
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800479 switch ((*state.hwc).hwcCompositionType) {
480 case Hwc2::IComposerClient::Composition::CLIENT:
Dan Stoza9e56aa02015-11-02 13:00:03 -0800481 displayData.hasClientComposition = true;
482 break;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800483 case Hwc2::IComposerClient::Composition::DEVICE:
484 case Hwc2::IComposerClient::Composition::SOLID_COLOR:
485 case Hwc2::IComposerClient::Composition::CURSOR:
486 case Hwc2::IComposerClient::Composition::SIDEBAND:
Dan Stoza9e56aa02015-11-02 13:00:03 -0800487 displayData.hasDeviceComposition = true;
488 break;
489 default:
490 break;
491 }
492
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800493 state.clearClientTarget = false;
494 if (auto it = layerRequests.find(hwcLayer.get()); it != layerRequests.end()) {
495 auto request = it->second;
496 if (request == HWC2::LayerRequest::ClearClientTarget) {
497 state.clearClientTarget = true;
498 } else {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700499 LOG_DISPLAY_ERROR(displayId,
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800500 ("Unknown layer request " + to_string(request)).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800501 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800502 }
503 }
504
505 error = hwcDisplay->acceptChanges();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700506 RETURN_IF_HWC_ERROR_FOR("acceptChanges", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800507
508 return NO_ERROR;
509}
510
Dominik Laskowski075d3172018-05-24 15:50:06 -0700511bool HWComposer::hasDeviceComposition(const std::optional<DisplayId>& displayId) const {
512 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700513 // Displays without a corresponding HWC display are never composed by
514 // the device
515 return false;
516 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700517
Dominik Laskowski075d3172018-05-24 15:50:06 -0700518 RETURN_IF_INVALID_DISPLAY(*displayId, false);
519 return mDisplayData.at(*displayId).hasDeviceComposition;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800520}
521
Dominik Laskowski075d3172018-05-24 15:50:06 -0700522bool HWComposer::hasFlipClientTargetRequest(const std::optional<DisplayId>& displayId) const {
523 if (!displayId) {
Madhuri Athota88a905b2017-05-04 16:58:15 +0530524 // Displays without a corresponding HWC display are never composed by
525 // the device
526 return false;
527 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700528
Dominik Laskowski075d3172018-05-24 15:50:06 -0700529 RETURN_IF_INVALID_DISPLAY(*displayId, false);
530 return ((static_cast<uint32_t>(mDisplayData.at(*displayId).displayRequests) &
Madhuri Athota88a905b2017-05-04 16:58:15 +0530531 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0);
532}
533
Dominik Laskowski075d3172018-05-24 15:50:06 -0700534bool HWComposer::hasClientComposition(const std::optional<DisplayId>& displayId) const {
535 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700536 // Displays without a corresponding HWC display are always composed by
537 // the client
538 return true;
539 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700540
Dominik Laskowski075d3172018-05-24 15:50:06 -0700541 RETURN_IF_INVALID_DISPLAY(*displayId, true);
542 return mDisplayData.at(*displayId).hasClientComposition;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800543}
544
Dominik Laskowski075d3172018-05-24 15:50:06 -0700545sp<Fence> HWComposer::getPresentFence(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700546 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700547 return mDisplayData.at(displayId).lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700548}
549
Dominik Laskowski075d3172018-05-24 15:50:06 -0700550sp<Fence> HWComposer::getLayerReleaseFence(DisplayId displayId, HWC2::Layer* layer) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700551 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700552 auto displayFences = mDisplayData.at(displayId).releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800553 if (displayFences.count(layer) == 0) {
554 ALOGV("getLayerReleaseFence: Release fence not found");
555 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700556 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800557 return displayFences[layer];
Riley Andrews03414a12014-07-01 14:22:59 -0700558}
559
Dominik Laskowski075d3172018-05-24 15:50:06 -0700560status_t HWComposer::presentAndGetReleaseFences(DisplayId displayId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800561 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700562
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700563 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700564
Dan Stoza9e56aa02015-11-02 13:00:03 -0800565 auto& displayData = mDisplayData[displayId];
566 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700567
568 if (displayData.validateWasSkipped) {
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700569 // explicitly flush all pending commands
570 auto error = mHwcDevice->flushCommands();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700571 RETURN_IF_HWC_ERROR_FOR("flushCommands", error, displayId, UNKNOWN_ERROR);
572 RETURN_IF_HWC_ERROR_FOR("present", displayData.presentError, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700573 return NO_ERROR;
574 }
575
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800576 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700577 RETURN_IF_HWC_ERROR_FOR("present", error, displayId, UNKNOWN_ERROR);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700578
Steven Thomas94e35b92017-07-26 18:48:28 -0700579 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800580 error = hwcDisplay->getReleaseFences(&releaseFences);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700581 RETURN_IF_HWC_ERROR_FOR("getReleaseFences", error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800582
583 displayData.releaseFences = std::move(releaseFences);
584
585 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700586}
587
Dominik Laskowski075d3172018-05-24 15:50:06 -0700588status_t HWComposer::setPowerMode(DisplayId displayId, int32_t intMode) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700589 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
590
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700591 const auto& displayData = mDisplayData[displayId];
592 if (displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700593 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
594 return INVALID_OPERATION;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800595 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700596
Dan Stoza9e56aa02015-11-02 13:00:03 -0800597 auto mode = static_cast<HWC2::PowerMode>(intMode);
598 if (mode == HWC2::PowerMode::Off) {
599 setVsyncEnabled(displayId, HWC2::Vsync::Disable);
600 }
601
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700602 auto& hwcDisplay = displayData.hwcDisplay;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800603 switch (mode) {
604 case HWC2::PowerMode::Off:
605 case HWC2::PowerMode::On:
606 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
607 {
608 auto error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700609 if (error != HWC2::Error::None) {
610 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
611 error, displayId);
612 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700613 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800614 break;
615 case HWC2::PowerMode::Doze:
616 case HWC2::PowerMode::DozeSuspend:
617 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
618 {
619 bool supportsDoze = false;
620 auto error = hwcDisplay->supportsDoze(&supportsDoze);
Peiyong Lin306e4992018-05-07 16:18:22 -0700621 if (error != HWC2::Error::None) {
622 LOG_HWC_ERROR("supportsDoze", error, displayId);
623 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700624
Dan Stoza9e56aa02015-11-02 13:00:03 -0800625 if (!supportsDoze) {
626 mode = HWC2::PowerMode::On;
627 }
628
629 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 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800634 }
635 break;
636 default:
637 ALOGV("setPowerMode: Not calling HWC");
638 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700639 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800640
641 return NO_ERROR;
642}
643
Dominik Laskowski075d3172018-05-24 15:50:06 -0700644status_t HWComposer::setActiveConfig(DisplayId displayId, size_t configId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700645 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800646
647 auto& displayData = mDisplayData[displayId];
648 if (displayData.configMap.count(configId) == 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700649 LOG_DISPLAY_ERROR(displayId, ("Invalid config " + std::to_string(configId)).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800650 return BAD_INDEX;
651 }
652
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700653 auto error = displayData.hwcDisplay->setActiveConfig(displayData.configMap[configId]);
654 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800655 return NO_ERROR;
656}
657
Dominik Laskowski075d3172018-05-24 15:50:06 -0700658status_t HWComposer::setColorTransform(DisplayId displayId, const mat4& transform) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700659 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700660
661 auto& displayData = mDisplayData[displayId];
662 bool isIdentity = transform == mat4();
663 auto error = displayData.hwcDisplay->setColorTransform(transform,
664 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY :
665 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700666 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700667 return NO_ERROR;
668}
669
Dominik Laskowski075d3172018-05-24 15:50:06 -0700670void HWComposer::disconnectDisplay(DisplayId displayId) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700671 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800672 auto& displayData = mDisplayData[displayId];
673
Dan Stoza9e56aa02015-11-02 13:00:03 -0800674 // If this was a virtual display, add its slot back for reuse by future
675 // virtual displays
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700676 if (displayData.isVirtual) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700677 mFreeVirtualDisplayIds.insert(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800678 ++mRemainingHwcVirtualDisplays;
679 }
680
Dominik Laskowski7e045462018-05-30 13:02:02 -0700681 const auto hwcDisplayId = displayData.hwcDisplay->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700682 mPhysicalDisplayIdMap.erase(hwcDisplayId);
683 mDisplayData.erase(displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700684
685 // TODO(b/74619554): Select internal/external display from remaining displays.
686 if (hwcDisplayId == mInternalHwcDisplayId) {
687 mInternalHwcDisplayId.reset();
688 } else if (hwcDisplayId == mExternalHwcDisplayId) {
689 mExternalHwcDisplayId.reset();
690 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700691
Dominik Laskowski7e045462018-05-30 13:02:02 -0700692 mHwcDevice->destroyDisplay(hwcDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800693}
694
Dominik Laskowski075d3172018-05-24 15:50:06 -0700695status_t HWComposer::setOutputBuffer(DisplayId displayId, const sp<Fence>& acquireFence,
696 const sp<GraphicBuffer>& buffer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700697 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700698 const auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800699
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700700 if (!displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700701 LOG_DISPLAY_ERROR(displayId, "Invalid operation on physical display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800702 return INVALID_OPERATION;
703 }
704
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700705 auto error = displayData.hwcDisplay->setOutputBuffer(buffer, acquireFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700706 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800707 return NO_ERROR;
708}
709
Dominik Laskowski075d3172018-05-24 15:50:06 -0700710void HWComposer::clearReleaseFences(DisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700711 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800712 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700713}
714
Dominik Laskowski075d3172018-05-24 15:50:06 -0700715status_t HWComposer::getHdrCapabilities(DisplayId displayId, HdrCapabilities* outCapabilities) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700716 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stozac4f471e2016-03-24 09:31:08 -0700717
718 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Peiyong Lin62665892018-04-16 11:07:44 -0700719 auto error = hwcDisplay->getHdrCapabilities(outCapabilities);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700720 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Peiyong Lin62665892018-04-16 11:07:44 -0700721 return NO_ERROR;
Dan Stozac4f471e2016-03-24 09:31:08 -0700722}
723
Dominik Laskowski075d3172018-05-24 15:50:06 -0700724int32_t HWComposer::getSupportedPerFrameMetadata(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700725 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700726 return mDisplayData.at(displayId).hwcDisplay->getSupportedPerFrameMetadata();
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700727}
728
Dominik Laskowski075d3172018-05-24 15:50:06 -0700729std::vector<ui::RenderIntent> HWComposer::getRenderIntents(DisplayId displayId,
730 ui::ColorMode colorMode) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700731 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700732
733 std::vector<ui::RenderIntent> renderIntents;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700734 auto error = mDisplayData.at(displayId).hwcDisplay->getRenderIntents(colorMode, &renderIntents);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700735 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700736 return renderIntents;
737}
738
Dominik Laskowski075d3172018-05-24 15:50:06 -0700739mat4 HWComposer::getDataspaceSaturationMatrix(DisplayId displayId, ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700740 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700741
742 mat4 matrix;
743 auto error = mDisplayData[displayId].hwcDisplay->getDataspaceSaturationMatrix(dataspace,
744 &matrix);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700745 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700746 return matrix;
747}
748
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700749status_t HWComposer::getDisplayedContentSamplingAttributes(DisplayId displayId,
750 ui::PixelFormat* outFormat,
751 ui::Dataspace* outDataspace,
752 uint8_t* outComponentMask) {
753 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
754 const auto error =
755 mDisplayData[displayId]
756 .hwcDisplay->getDisplayedContentSamplingAttributes(outFormat, outDataspace,
757 outComponentMask);
758 if (error == HWC2::Error::Unsupported) RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
759 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
760 return NO_ERROR;
761}
762
Kevin DuBois74e53772018-11-19 10:52:38 -0800763status_t HWComposer::setDisplayContentSamplingEnabled(DisplayId displayId, bool enabled,
764 uint8_t componentMask, uint64_t maxFrames) {
765 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
766 const auto error =
767 mDisplayData[displayId].hwcDisplay->setDisplayContentSamplingEnabled(enabled,
768 componentMask,
769 maxFrames);
770
771 if (error == HWC2::Error::Unsupported) RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
772 if (error == HWC2::Error::BadParameter) RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
773 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
774 return NO_ERROR;
775}
776
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700777status_t HWComposer::getDisplayedContentSample(DisplayId displayId, uint64_t maxFrames,
778 uint64_t timestamp, DisplayedFrameStats* outStats) {
779 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
780 const auto error =
781 mDisplayData[displayId].hwcDisplay->getDisplayedContentSample(maxFrames, timestamp,
782 outStats);
783 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
784 return NO_ERROR;
785}
786
Dan Gittik57e63c52019-01-18 16:37:54 +0000787status_t HWComposer::setDisplayBrightness(DisplayId displayId, float brightness) {
788 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
789 const auto error = mDisplayData[displayId].hwcDisplay->setDisplayBrightness(brightness);
790 if (error == HWC2::Error::Unsupported) {
791 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
792 }
793 if (error == HWC2::Error::BadParameter) {
794 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
795 }
796 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
797 return NO_ERROR;
798}
799
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800800bool HWComposer::isUsingVrComposer() const {
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800801 return getComposer()->isUsingVrComposer();
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800802}
803
Yiwei Zhang5434a782018-12-05 18:06:32 -0800804void HWComposer::dump(std::string& result) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800805 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow
806 // all the state going into the layers. This is probably better done in
807 // Layer itself, but it's going to take a bit of work to get there.
Yiwei Zhang5434a782018-12-05 18:06:32 -0800808 result.append(mHwcDevice->dump());
Mathias Agopian83727852010-09-23 18:13:21 -0700809}
810
Dominik Laskowski075d3172018-05-24 15:50:06 -0700811std::optional<DisplayId> HWComposer::toPhysicalDisplayId(hwc2_display_t hwcDisplayId) const {
812 if (const auto it = mPhysicalDisplayIdMap.find(hwcDisplayId);
813 it != mPhysicalDisplayIdMap.end()) {
814 return it->second;
815 }
816 return {};
817}
818
819std::optional<hwc2_display_t> HWComposer::fromPhysicalDisplayId(DisplayId displayId) const {
820 if (const auto it = mDisplayData.find(displayId);
821 it != mDisplayData.end() && !it->second.isVirtual) {
822 return it->second.hwcDisplay->getId();
823 }
824 return {};
825}
826
827std::optional<DisplayIdentificationInfo> HWComposer::onHotplugConnect(hwc2_display_t hwcDisplayId) {
828 if (isUsingVrComposer() && mInternalHwcDisplayId) {
829 ALOGE("Ignoring connection of external display %" PRIu64 " in VR mode", hwcDisplayId);
Steven Thomas6e8f7062017-11-22 14:15:29 -0800830 return {};
831 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700832
833 uint8_t port;
834 DisplayIdentificationData data;
835 const bool hasMultiDisplaySupport = getDisplayIdentificationData(hwcDisplayId, &port, &data);
836
837 if (mPhysicalDisplayIdMap.empty()) {
838 mHasMultiDisplaySupport = hasMultiDisplaySupport;
839 ALOGI("Switching to %s multi-display mode",
840 hasMultiDisplaySupport ? "generalized" : "legacy");
841 } else if (mHasMultiDisplaySupport && !hasMultiDisplaySupport) {
842 ALOGE("Ignoring connection of display %" PRIu64 " without identification data",
843 hwcDisplayId);
844 return {};
845 }
846
847 std::optional<DisplayIdentificationInfo> info;
848
849 if (mHasMultiDisplaySupport) {
850 info = parseDisplayIdentificationData(port, data);
851 ALOGE_IF(!info, "Failed to parse identification data for display %" PRIu64, hwcDisplayId);
852 } else if (mInternalHwcDisplayId && mExternalHwcDisplayId) {
853 ALOGE("Ignoring connection of tertiary display %" PRIu64, hwcDisplayId);
854 return {};
855 } else {
856 ALOGW_IF(hasMultiDisplaySupport, "Ignoring identification data for display %" PRIu64,
857 hwcDisplayId);
858 port = mInternalHwcDisplayId ? HWC_DISPLAY_EXTERNAL : HWC_DISPLAY_PRIMARY;
859 }
860
861 if (!mInternalHwcDisplayId) {
862 mInternalHwcDisplayId = hwcDisplayId;
863 } else if (!mExternalHwcDisplayId) {
864 mExternalHwcDisplayId = hwcDisplayId;
865 }
866
867 if (info) return info;
868
869 return DisplayIdentificationInfo{getFallbackDisplayId(port),
870 hwcDisplayId == mInternalHwcDisplayId ? "Internal display"
871 : "External display"};
Steven Thomas6e8f7062017-11-22 14:15:29 -0800872}
873
Lloyd Pique441d5042018-10-18 16:49:51 -0700874} // namespace impl
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700875} // namespace android