blob: 5b4d347aaef35e3b769df9cfb49734f7f099939a [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
Mathias Agopiana350ff92010-08-10 17:14:02 -070023#include <utils/Errors.h>
Mathias Agopian2965b262012-04-08 15:13:32 -070024#include <utils/Trace.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070025
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070026#include <ui/DebugUtils.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070027#include <ui/GraphicBuffer.h>
28
Mark Salyzyn7823e122016-09-29 08:08:05 -070029#include <log/log.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070030
Mathias Agopiana350ff92010-08-10 17:14:02 -070031#include "HWComposer.h"
Dan Stoza9e56aa02015-11-02 13:00:03 -080032#include "HWC2.h"
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -080033#include "ComposerHal.h"
Mathias Agopian33ceeb32013-04-01 16:54:58 -070034
35#include "../Layer.h" // needed only for debugging
36#include "../SurfaceFlinger.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070037
Dominik Laskowskic1f18f62018-06-13 15:17:55 -070038#define LOG_HWC_DISPLAY_ERROR(hwcDisplayId, msg) \
39 ALOGE("%s failed for HWC display %" PRIu64 ": %s", __FUNCTION__, hwcDisplayId, msg)
40
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070041#define LOG_DISPLAY_ERROR(displayId, msg) \
Dominik Laskowski34157762018-10-31 13:07:19 -070042 ALOGE("%s failed for display %s: %s", __FUNCTION__, to_string(displayId).c_str(), msg)
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070043
Dominik Laskowski34157762018-10-31 13:07:19 -070044#define LOG_HWC_ERROR(what, error, displayId) \
45 ALOGE("%s: %s failed for display %s: %s (%d)", __FUNCTION__, what, \
46 to_string(displayId).c_str(), to_string(error).c_str(), static_cast<int32_t>(error))
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070047
48#define RETURN_IF_INVALID_DISPLAY(displayId, ...) \
49 do { \
Dominik Laskowski075d3172018-05-24 15:50:06 -070050 if (mDisplayData.count(displayId) == 0) { \
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070051 LOG_DISPLAY_ERROR(displayId, "Invalid display"); \
52 return __VA_ARGS__; \
53 } \
54 } while (false)
55
56#define RETURN_IF_HWC_ERROR_FOR(what, error, displayId, ...) \
57 do { \
58 if (error != HWC2::Error::None) { \
59 LOG_HWC_ERROR(what, error, displayId); \
60 return __VA_ARGS__; \
61 } \
62 } while (false)
63
64#define RETURN_IF_HWC_ERROR(error, displayId, ...) \
65 RETURN_IF_HWC_ERROR_FOR(__FUNCTION__, error, displayId, __VA_ARGS__)
66
Mathias Agopiana350ff92010-08-10 17:14:02 -070067namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -070068
Lloyd Pique441d5042018-10-18 16:49:51 -070069HWComposer::~HWComposer() = default;
70
71namespace impl {
72
Dominik Laskowski1af47932018-11-12 10:20:46 -080073HWComposer::HWComposer(std::unique_ptr<Hwc2::Composer> composer)
Lloyd Piquea822d522017-12-20 16:42:57 -080074 : mHwcDevice(std::make_unique<HWC2::Device>(std::move(composer))) {}
Mathias Agopianbef42c52013-08-21 17:45:46 -070075
Dominik Laskowskib04f98a2018-11-07 21:07:16 -080076HWComposer::~HWComposer() {
77 mDisplayData.clear();
78}
Dan Stoza9e56aa02015-11-02 13:00:03 -080079
Steven Thomas94e35b92017-07-26 18:48:28 -070080void HWComposer::registerCallback(HWC2::ComposerCallback* callback,
81 int32_t sequenceId) {
82 mHwcDevice->registerCallback(callback, sequenceId);
Dan Stoza9e56aa02015-11-02 13:00:03 -080083}
84
Dominik Laskowskia2edf612018-06-01 13:15:16 -070085bool HWComposer::getDisplayIdentificationData(hwc2_display_t hwcDisplayId, uint8_t* outPort,
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070086 DisplayIdentificationData* outData) const {
Dominik Laskowski0954b1d2018-06-13 14:58:49 -070087 const auto error = mHwcDevice->getDisplayIdentificationData(hwcDisplayId, outPort, outData);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070088 if (error != HWC2::Error::None) {
Chia-I Wud0aff9d2018-06-21 13:39:09 +080089 if (error != HWC2::Error::Unsupported) {
90 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, to_string(error).c_str());
91 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070092 return false;
93 }
94 return true;
95}
96
Dan Stoza9f26a9c2016-06-22 14:51:09 -070097bool HWComposer::hasCapability(HWC2::Capability capability) const
98{
99 return mHwcDevice->getCapabilities().count(capability) > 0;
100}
101
Peiyong Lined531a32018-10-26 18:27:56 -0700102bool HWComposer::hasDisplayCapability(const std::optional<DisplayId>& displayId,
103 HWC2::DisplayCapability capability) const {
104 if (!displayId) {
Peiyong Lindf65fd22019-01-03 15:17:05 -0800105 // Checkout global capabilities for displays without a corresponding HWC display.
106 if (capability == HWC2::DisplayCapability::SkipClientColorTransform) {
107 return hasCapability(HWC2::Capability::SkipClientColorTransform);
108 }
Peiyong Lined531a32018-10-26 18:27:56 -0700109 return false;
110 }
111 RETURN_IF_INVALID_DISPLAY(*displayId, false);
112 return mDisplayData.at(*displayId).hwcDisplay->getCapabilities().count(capability) > 0;
113}
114
Dan Stoza9e56aa02015-11-02 13:00:03 -0800115void HWComposer::validateChange(HWC2::Composition from, HWC2::Composition to) {
116 bool valid = true;
117 switch (from) {
118 case HWC2::Composition::Client:
119 valid = false;
120 break;
121 case HWC2::Composition::Device:
122 case HWC2::Composition::SolidColor:
123 valid = (to == HWC2::Composition::Client);
124 break;
125 case HWC2::Composition::Cursor:
126 case HWC2::Composition::Sideband:
127 valid = (to == HWC2::Composition::Client ||
128 to == HWC2::Composition::Device);
129 break;
130 default:
131 break;
132 }
133
134 if (!valid) {
135 ALOGE("Invalid layer type change: %s --> %s", to_string(from).c_str(),
136 to_string(to).c_str());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700137 }
138}
139
Dominik Laskowski075d3172018-05-24 15:50:06 -0700140std::optional<DisplayIdentificationInfo> HWComposer::onHotplug(hwc2_display_t hwcDisplayId,
141 HWC2::Connection connection) {
142 std::optional<DisplayIdentificationInfo> info;
Lloyd Pique715a2c12017-12-14 17:18:08 -0800143
Dominik Laskowski075d3172018-05-24 15:50:06 -0700144 if (const auto displayId = toPhysicalDisplayId(hwcDisplayId)) {
145 info = DisplayIdentificationInfo{*displayId, std::string()};
146 } else {
147 if (connection == HWC2::Connection::Disconnected) {
148 ALOGE("Ignoring disconnection of invalid HWC display %" PRIu64, hwcDisplayId);
149 return {};
Lloyd Pique438e9e72018-09-04 18:06:08 -0700150 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700151
152 info = onHotplugConnect(hwcDisplayId);
153 if (!info) return {};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700154 }
155
Dominik Laskowski34157762018-10-31 13:07:19 -0700156 ALOGV("%s: %s %s display %s with HWC ID %" PRIu64, __FUNCTION__, to_string(connection).c_str(),
157 hwcDisplayId == mInternalHwcDisplayId ? "internal" : "external",
158 to_string(info->id).c_str(), hwcDisplayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700159
160 mHwcDevice->onHotplug(hwcDisplayId, connection);
161
Lloyd Pique715a2c12017-12-14 17:18:08 -0800162 // Disconnect is handled through HWComposer::disconnectDisplay via
163 // SurfaceFlinger's onHotplugReceived callback handling
164 if (connection == HWC2::Connection::Connected) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700165 mDisplayData[info->id].hwcDisplay = mHwcDevice->getDisplayById(hwcDisplayId);
166 mPhysicalDisplayIdMap[hwcDisplayId] = info->id;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700167 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700168
Dominik Laskowski075d3172018-05-24 15:50:06 -0700169 return info;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700170}
171
Dominik Laskowski075d3172018-05-24 15:50:06 -0700172bool HWComposer::onVsync(hwc2_display_t hwcDisplayId, int64_t timestamp) {
173 const auto displayId = toPhysicalDisplayId(hwcDisplayId);
174 if (!displayId) {
175 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, "Invalid HWC display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700176 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700177 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700178
Dominik Laskowski075d3172018-05-24 15:50:06 -0700179 RETURN_IF_INVALID_DISPLAY(*displayId, false);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700180
Dominik Laskowski1af47932018-11-12 10:20:46 -0800181 auto& displayData = mDisplayData[*displayId];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700182 if (displayData.isVirtual) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700183 LOG_DISPLAY_ERROR(*displayId, "Invalid operation on virtual display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700184 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700185 }
186
Dan Stoza9e56aa02015-11-02 13:00:03 -0800187 {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800188 std::lock_guard lock(displayData.lastHwVsyncLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700189
Dan Stoza9e56aa02015-11-02 13:00:03 -0800190 // There have been reports of HWCs that signal several vsync events
191 // with the same timestamp when turning the display off and on. This
192 // is a bug in the HWC implementation, but filter the extra events
193 // out here so they don't cause havoc downstream.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800194 if (timestamp == displayData.lastHwVsync) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700195 ALOGW("Ignoring duplicate VSYNC event from HWC for display %s (t=%" PRId64 ")",
196 to_string(*displayId).c_str(), timestamp);
Steven Thomas94e35b92017-07-26 18:48:28 -0700197 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800198 }
199
Dominik Laskowski1af47932018-11-12 10:20:46 -0800200 displayData.lastHwVsync = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700201 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800202
Dominik Laskowski34157762018-10-31 13:07:19 -0700203 const auto tag = "HW_VSYNC_" + to_string(*displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800204 ATRACE_INT(tag.c_str(), displayData.vsyncTraceToggle);
205 displayData.vsyncTraceToggle = !displayData.vsyncTraceToggle;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800206
Steven Thomas94e35b92017-07-26 18:48:28 -0700207 return true;
Jesse Hall1c569c42013-04-05 13:44:52 -0700208}
209
Dominik Laskowski075d3172018-05-24 15:50:06 -0700210std::optional<DisplayId> HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
211 ui::PixelFormat* format) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800212 if (mRemainingHwcVirtualDisplays == 0) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700213 ALOGE("%s: No remaining virtual displays", __FUNCTION__);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700214 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700215 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700216
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800217 if (SurfaceFlinger::maxVirtualDisplaySize != 0 &&
218 (width > SurfaceFlinger::maxVirtualDisplaySize ||
219 height > SurfaceFlinger::maxVirtualDisplaySize)) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700220 ALOGE("%s: Display size %ux%u exceeds maximum dimension of %" PRIu64, __FUNCTION__, width,
221 height, SurfaceFlinger::maxVirtualDisplaySize);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700222 return {};
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800223 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700224 HWC2::Display* display;
Dan Stoza5cf424b2016-05-20 14:02:39 -0700225 auto error = mHwcDevice->createVirtualDisplay(width, height, format,
226 &display);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800227 if (error != HWC2::Error::None) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700228 ALOGE("%s: Failed to create HWC virtual display", __FUNCTION__);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700229 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700230 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800231
Dominik Laskowski075d3172018-05-24 15:50:06 -0700232 DisplayId displayId;
233 if (mFreeVirtualDisplayIds.empty()) {
234 displayId = getVirtualDisplayId(mNextVirtualDisplayId++);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800235 } else {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700236 displayId = *mFreeVirtualDisplayIds.begin();
237 mFreeVirtualDisplayIds.erase(displayId);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700238 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800239
Dominik Laskowski075d3172018-05-24 15:50:06 -0700240 auto& displayData = mDisplayData[displayId];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700241 displayData.hwcDisplay = display;
242 displayData.isVirtual = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800243
244 --mRemainingHwcVirtualDisplays;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700245 return displayId;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700246}
247
Dominik Laskowski075d3172018-05-24 15:50:06 -0700248HWC2::Layer* HWComposer::createLayer(DisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700249 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
250
Dan Stoza9e56aa02015-11-02 13:00:03 -0800251 auto display = mDisplayData[displayId].hwcDisplay;
Steven Thomas94e35b92017-07-26 18:48:28 -0700252 HWC2::Layer* layer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800253 auto error = display->createLayer(&layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700254 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800255 return layer;
256}
257
Dominik Laskowski075d3172018-05-24 15:50:06 -0700258void HWComposer::destroyLayer(DisplayId displayId, HWC2::Layer* layer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700259 RETURN_IF_INVALID_DISPLAY(displayId);
260
Steven Thomas94e35b92017-07-26 18:48:28 -0700261 auto display = mDisplayData[displayId].hwcDisplay;
262 auto error = display->destroyLayer(layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700263 RETURN_IF_HWC_ERROR(error, displayId);
Steven Thomas94e35b92017-07-26 18:48:28 -0700264}
265
Dominik Laskowski075d3172018-05-24 15:50:06 -0700266nsecs_t HWComposer::getRefreshTimestamp(DisplayId displayId) const {
267 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800268 const auto& displayData = mDisplayData.at(displayId);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700269 // this returns the last refresh timestamp.
270 // if the last one is not available, we estimate it based on
271 // the refresh period and whatever closest timestamp we have.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800272 std::lock_guard lock(displayData.lastHwVsyncLock);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700273 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800274 auto vsyncPeriod = getActiveConfig(displayId)->getVsyncPeriod();
Dominik Laskowski1af47932018-11-12 10:20:46 -0800275 return now - ((now - displayData.lastHwVsync) % vsyncPeriod);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700276}
277
Dominik Laskowski075d3172018-05-24 15:50:06 -0700278bool HWComposer::isConnected(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700279 RETURN_IF_INVALID_DISPLAY(displayId, false);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700280 return mDisplayData.at(displayId).hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700281}
282
Dominik Laskowski075d3172018-05-24 15:50:06 -0700283std::vector<std::shared_ptr<const HWC2::Display::Config>> HWComposer::getConfigs(
284 DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700285 RETURN_IF_INVALID_DISPLAY(displayId, {});
286
Dominik Laskowski075d3172018-05-24 15:50:06 -0700287 const auto& displayData = mDisplayData.at(displayId);
288 auto configs = displayData.hwcDisplay->getConfigs();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800289 if (displayData.configMap.empty()) {
290 for (size_t i = 0; i < configs.size(); ++i) {
291 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700292 }
293 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800294 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700295}
296
Dominik Laskowski075d3172018-05-24 15:50:06 -0700297std::shared_ptr<const HWC2::Display::Config> HWComposer::getActiveConfig(
298 DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700299 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
300
Dan Stoza9e56aa02015-11-02 13:00:03 -0800301 std::shared_ptr<const HWC2::Display::Config> config;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700302 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfig(&config);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800303 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700304 LOG_DISPLAY_ERROR(displayId, "No active config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800305 return nullptr;
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700306 }
307
308 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
309
310 if (!config) {
311 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800312 return nullptr;
313 }
314
315 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700316}
317
Dominik Laskowski075d3172018-05-24 15:50:06 -0700318int HWComposer::getActiveConfigIndex(DisplayId displayId) const {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700319 RETURN_IF_INVALID_DISPLAY(displayId, -1);
320
Lloyd Pique3c085a02018-05-09 19:38:32 -0700321 int index;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700322 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfigIndex(&index);
Lloyd Pique3c085a02018-05-09 19:38:32 -0700323 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700324 LOG_DISPLAY_ERROR(displayId, "No active config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700325 return -1;
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700326 }
327
328 RETURN_IF_HWC_ERROR(error, displayId, -1);
329
330 if (index < 0) {
331 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700332 return -1;
333 }
334
335 return index;
336}
337
Dominik Laskowski075d3172018-05-24 15:50:06 -0700338std::vector<ui::ColorMode> HWComposer::getColorModes(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700339 RETURN_IF_INVALID_DISPLAY(displayId, {});
340
Peiyong Linfd997e02018-03-28 15:29:00 -0700341 std::vector<ui::ColorMode> modes;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700342 auto error = mDisplayData.at(displayId).hwcDisplay->getColorModes(&modes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700343 RETURN_IF_HWC_ERROR(error, displayId, {});
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600344 return modes;
345}
346
Dominik Laskowski075d3172018-05-24 15:50:06 -0700347status_t HWComposer::setActiveColorMode(DisplayId displayId, ui::ColorMode mode,
348 ui::RenderIntent renderIntent) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700349 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Michael Wright28f24d02016-07-12 13:30:53 -0700350
351 auto& displayData = mDisplayData[displayId];
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700352 auto error = displayData.hwcDisplay->setColorMode(mode, renderIntent);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700353 RETURN_IF_HWC_ERROR_FOR(("setColorMode(" + decodeColorMode(mode) + ", " +
354 decodeRenderIntent(renderIntent) + ")")
355 .c_str(),
356 error, displayId, UNKNOWN_ERROR);
Michael Wright28f24d02016-07-12 13:30:53 -0700357
358 return NO_ERROR;
359}
360
Dominik Laskowski075d3172018-05-24 15:50:06 -0700361void HWComposer::setVsyncEnabled(DisplayId displayId, HWC2::Vsync enabled) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700362 RETURN_IF_INVALID_DISPLAY(displayId);
363 auto& displayData = mDisplayData[displayId];
364
365 if (displayData.isVirtual) {
366 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800367 return;
368 }
369
Dan Stoza9e56aa02015-11-02 13:00:03 -0800370 // NOTE: we use our own internal lock here because we have to call
371 // into the HWC with the lock held, and we want to make sure
372 // that even if HWC blocks (which it shouldn't), it won't
373 // affect other threads.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800374 std::lock_guard lock(displayData.vsyncEnabledLock);
375 if (enabled == displayData.vsyncEnabled) {
376 return;
Colin Cross10fbdb62012-07-12 17:56:34 -0700377 }
Dominik Laskowski1af47932018-11-12 10:20:46 -0800378
379 ATRACE_CALL();
380 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
381 RETURN_IF_HWC_ERROR(error, displayId);
382
383 displayData.vsyncEnabled = enabled;
384
385 const auto tag = "HW_VSYNC_ON_" + to_string(displayId);
386 ATRACE_INT(tag.c_str(), enabled == HWC2::Vsync::Enable ? 1 : 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700387}
388
Dominik Laskowski075d3172018-05-24 15:50:06 -0700389status_t HWComposer::setClientTarget(DisplayId displayId, uint32_t slot,
390 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
391 ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700392 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Jesse Hall851cfe82013-03-20 13:44:00 -0700393
Dominik Laskowski34157762018-10-31 13:07:19 -0700394 ALOGV("%s for display %s", __FUNCTION__, to_string(displayId).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800395 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400396 auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700397 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Jesse Hall851cfe82013-03-20 13:44:00 -0700398 return NO_ERROR;
399}
400
Dominik Laskowski075d3172018-05-24 15:50:06 -0700401status_t HWComposer::prepare(DisplayId displayId, std::vector<CompositionInfo>& compositionData) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800402 ATRACE_CALL();
403
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700404 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800405
406 auto& displayData = mDisplayData[displayId];
407 auto& hwcDisplay = displayData.hwcDisplay;
408 if (!hwcDisplay->isConnected()) {
409 return NO_ERROR;
410 }
411
412 uint32_t numTypes = 0;
413 uint32_t numRequests = 0;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700414
415 HWC2::Error error = HWC2::Error::None;
416
Chia-I Wu41b98d42017-12-11 11:04:36 -0800417 // First try to skip validate altogether when there is no client
418 // composition. When there is client composition, since we haven't
419 // rendered to the client target yet, we should not attempt to skip
420 // validate.
421 //
422 // displayData.hasClientComposition hasn't been updated for this frame.
423 // The check below is incorrect. We actually rely on HWC here to fall
424 // back to validate when there is any client layer.
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700425 displayData.validateWasSkipped = false;
Chia-I Wu41b98d42017-12-11 11:04:36 -0800426 if (!displayData.hasClientComposition) {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800427 sp<Fence> outPresentFence;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700428 uint32_t state = UINT32_MAX;
429 error = hwcDisplay->presentOrValidate(&numTypes, &numRequests, &outPresentFence , &state);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700430 if (error != HWC2::Error::HasChanges) {
431 RETURN_IF_HWC_ERROR_FOR("presentOrValidate", error, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700432 }
433 if (state == 1) { //Present Succeeded.
Steven Thomas94e35b92017-07-26 18:48:28 -0700434 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700435 error = hwcDisplay->getReleaseFences(&releaseFences);
436 displayData.releaseFences = std::move(releaseFences);
437 displayData.lastPresentFence = outPresentFence;
438 displayData.validateWasSkipped = true;
439 displayData.presentError = error;
440 return NO_ERROR;
441 }
442 // Present failed but Validate ran.
443 } else {
444 error = hwcDisplay->validate(&numTypes, &numRequests);
445 }
446 ALOGV("SkipValidate failed, Falling back to SLOW validate/present");
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700447 if (error != HWC2::Error::HasChanges) {
448 RETURN_IF_HWC_ERROR_FOR("validate", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800449 }
450
Steven Thomas94e35b92017-07-26 18:48:28 -0700451 std::unordered_map<HWC2::Layer*, HWC2::Composition> changedTypes;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800452 changedTypes.reserve(numTypes);
453 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700454 RETURN_IF_HWC_ERROR_FOR("getChangedCompositionTypes", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800455
456 displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0);
Steven Thomas94e35b92017-07-26 18:48:28 -0700457 std::unordered_map<HWC2::Layer*, HWC2::LayerRequest> layerRequests;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800458 layerRequests.reserve(numRequests);
459 error = hwcDisplay->getRequests(&displayData.displayRequests,
460 &layerRequests);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700461 RETURN_IF_HWC_ERROR_FOR("getRequests", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800462
463 displayData.hasClientComposition = false;
464 displayData.hasDeviceComposition = false;
David Sodmanfb95bcc2017-12-22 15:45:30 -0800465 for (auto& compositionInfo : compositionData) {
466 auto hwcLayer = compositionInfo.hwc.hwcLayer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800467
David Sodmanfb95bcc2017-12-22 15:45:30 -0800468 if (changedTypes.count(&*hwcLayer) != 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800469 // We pass false so we only update our state and don't call back
470 // into the HWC device
David Sodmanfb95bcc2017-12-22 15:45:30 -0800471 validateChange(compositionInfo.compositionType,
472 changedTypes[&*hwcLayer]);
473 compositionInfo.compositionType = changedTypes[&*hwcLayer];
David Sodmanc1498e62018-09-12 14:36:26 -0700474 compositionInfo.layer->mLayer->setCompositionType(displayId,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700475 compositionInfo.compositionType,
476 false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800477 }
478
David Sodmanfb95bcc2017-12-22 15:45:30 -0800479 switch (compositionInfo.compositionType) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800480 case HWC2::Composition::Client:
481 displayData.hasClientComposition = true;
482 break;
483 case HWC2::Composition::Device:
484 case HWC2::Composition::SolidColor:
485 case HWC2::Composition::Cursor:
486 case HWC2::Composition::Sideband:
487 displayData.hasDeviceComposition = true;
488 break;
489 default:
490 break;
491 }
492
David Sodmanfb95bcc2017-12-22 15:45:30 -0800493 if (layerRequests.count(&*hwcLayer) != 0 &&
494 layerRequests[&*hwcLayer] ==
Dan Stoza9e56aa02015-11-02 13:00:03 -0800495 HWC2::LayerRequest::ClearClientTarget) {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800496 compositionInfo.hwc.clearClientTarget = true;
David Sodmanc1498e62018-09-12 14:36:26 -0700497 compositionInfo.layer->mLayer->setClearClientTarget(displayId, true);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800498 } else {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800499 if (layerRequests.count(&*hwcLayer) != 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700500 LOG_DISPLAY_ERROR(displayId,
David Sodmanfb95bcc2017-12-22 15:45:30 -0800501 ("Unknown layer request " + to_string(layerRequests[&*hwcLayer]))
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700502 .c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800503 }
David Sodmanfb95bcc2017-12-22 15:45:30 -0800504 compositionInfo.hwc.clearClientTarget = false;
David Sodmanc1498e62018-09-12 14:36:26 -0700505 compositionInfo.layer->mLayer->setClearClientTarget(displayId, false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800506 }
507 }
508
509 error = hwcDisplay->acceptChanges();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700510 RETURN_IF_HWC_ERROR_FOR("acceptChanges", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800511
512 return NO_ERROR;
513}
514
Dominik Laskowski075d3172018-05-24 15:50:06 -0700515bool HWComposer::hasDeviceComposition(const std::optional<DisplayId>& displayId) const {
516 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700517 // Displays without a corresponding HWC display are never composed by
518 // the device
519 return false;
520 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700521
Dominik Laskowski075d3172018-05-24 15:50:06 -0700522 RETURN_IF_INVALID_DISPLAY(*displayId, false);
523 return mDisplayData.at(*displayId).hasDeviceComposition;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800524}
525
Dominik Laskowski075d3172018-05-24 15:50:06 -0700526bool HWComposer::hasFlipClientTargetRequest(const std::optional<DisplayId>& displayId) const {
527 if (!displayId) {
Madhuri Athota88a905b2017-05-04 16:58:15 +0530528 // Displays without a corresponding HWC display are never composed by
529 // the device
530 return false;
531 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700532
Dominik Laskowski075d3172018-05-24 15:50:06 -0700533 RETURN_IF_INVALID_DISPLAY(*displayId, false);
534 return ((static_cast<uint32_t>(mDisplayData.at(*displayId).displayRequests) &
Madhuri Athota88a905b2017-05-04 16:58:15 +0530535 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0);
536}
537
Dominik Laskowski075d3172018-05-24 15:50:06 -0700538bool HWComposer::hasClientComposition(const std::optional<DisplayId>& displayId) const {
539 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700540 // Displays without a corresponding HWC display are always composed by
541 // the client
542 return true;
543 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700544
Dominik Laskowski075d3172018-05-24 15:50:06 -0700545 RETURN_IF_INVALID_DISPLAY(*displayId, true);
546 return mDisplayData.at(*displayId).hasClientComposition;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800547}
548
Dominik Laskowski075d3172018-05-24 15:50:06 -0700549sp<Fence> HWComposer::getPresentFence(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700550 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700551 return mDisplayData.at(displayId).lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700552}
553
Dominik Laskowski075d3172018-05-24 15:50:06 -0700554sp<Fence> HWComposer::getLayerReleaseFence(DisplayId displayId, HWC2::Layer* layer) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700555 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700556 auto displayFences = mDisplayData.at(displayId).releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800557 if (displayFences.count(layer) == 0) {
558 ALOGV("getLayerReleaseFence: Release fence not found");
559 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700560 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800561 return displayFences[layer];
Riley Andrews03414a12014-07-01 14:22:59 -0700562}
563
Dominik Laskowski075d3172018-05-24 15:50:06 -0700564status_t HWComposer::presentAndGetReleaseFences(DisplayId displayId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800565 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700566
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700567 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700568
Dan Stoza9e56aa02015-11-02 13:00:03 -0800569 auto& displayData = mDisplayData[displayId];
570 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700571
572 if (displayData.validateWasSkipped) {
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700573 // explicitly flush all pending commands
574 auto error = mHwcDevice->flushCommands();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700575 RETURN_IF_HWC_ERROR_FOR("flushCommands", error, displayId, UNKNOWN_ERROR);
576 RETURN_IF_HWC_ERROR_FOR("present", displayData.presentError, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700577 return NO_ERROR;
578 }
579
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800580 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700581 RETURN_IF_HWC_ERROR_FOR("present", error, displayId, UNKNOWN_ERROR);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700582
Steven Thomas94e35b92017-07-26 18:48:28 -0700583 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800584 error = hwcDisplay->getReleaseFences(&releaseFences);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700585 RETURN_IF_HWC_ERROR_FOR("getReleaseFences", error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800586
587 displayData.releaseFences = std::move(releaseFences);
588
589 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700590}
591
Dominik Laskowski075d3172018-05-24 15:50:06 -0700592status_t HWComposer::setPowerMode(DisplayId displayId, int32_t intMode) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700593 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
594
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700595 const auto& displayData = mDisplayData[displayId];
596 if (displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700597 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
598 return INVALID_OPERATION;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800599 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700600
Dan Stoza9e56aa02015-11-02 13:00:03 -0800601 auto mode = static_cast<HWC2::PowerMode>(intMode);
602 if (mode == HWC2::PowerMode::Off) {
603 setVsyncEnabled(displayId, HWC2::Vsync::Disable);
604 }
605
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700606 auto& hwcDisplay = displayData.hwcDisplay;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800607 switch (mode) {
608 case HWC2::PowerMode::Off:
609 case HWC2::PowerMode::On:
610 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
611 {
612 auto error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700613 if (error != HWC2::Error::None) {
614 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
615 error, displayId);
616 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700617 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800618 break;
619 case HWC2::PowerMode::Doze:
620 case HWC2::PowerMode::DozeSuspend:
621 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
622 {
623 bool supportsDoze = false;
624 auto error = hwcDisplay->supportsDoze(&supportsDoze);
Peiyong Lin306e4992018-05-07 16:18:22 -0700625 if (error != HWC2::Error::None) {
626 LOG_HWC_ERROR("supportsDoze", error, displayId);
627 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700628
Dan Stoza9e56aa02015-11-02 13:00:03 -0800629 if (!supportsDoze) {
630 mode = HWC2::PowerMode::On;
631 }
632
633 error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700634 if (error != HWC2::Error::None) {
635 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
636 error, displayId);
637 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800638 }
639 break;
640 default:
641 ALOGV("setPowerMode: Not calling HWC");
642 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700643 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800644
645 return NO_ERROR;
646}
647
Dominik Laskowski075d3172018-05-24 15:50:06 -0700648status_t HWComposer::setActiveConfig(DisplayId displayId, size_t configId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700649 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800650
651 auto& displayData = mDisplayData[displayId];
652 if (displayData.configMap.count(configId) == 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700653 LOG_DISPLAY_ERROR(displayId, ("Invalid config " + std::to_string(configId)).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800654 return BAD_INDEX;
655 }
656
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700657 auto error = displayData.hwcDisplay->setActiveConfig(displayData.configMap[configId]);
658 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800659 return NO_ERROR;
660}
661
Dominik Laskowski075d3172018-05-24 15:50:06 -0700662status_t HWComposer::setColorTransform(DisplayId displayId, const mat4& transform) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700663 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700664
665 auto& displayData = mDisplayData[displayId];
666 bool isIdentity = transform == mat4();
667 auto error = displayData.hwcDisplay->setColorTransform(transform,
668 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY :
669 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700670 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700671 return NO_ERROR;
672}
673
Dominik Laskowski075d3172018-05-24 15:50:06 -0700674void HWComposer::disconnectDisplay(DisplayId displayId) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700675 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800676 auto& displayData = mDisplayData[displayId];
677
Dan Stoza9e56aa02015-11-02 13:00:03 -0800678 // If this was a virtual display, add its slot back for reuse by future
679 // virtual displays
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700680 if (displayData.isVirtual) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700681 mFreeVirtualDisplayIds.insert(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800682 ++mRemainingHwcVirtualDisplays;
683 }
684
Dominik Laskowski7e045462018-05-30 13:02:02 -0700685 const auto hwcDisplayId = displayData.hwcDisplay->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700686 mPhysicalDisplayIdMap.erase(hwcDisplayId);
687 mDisplayData.erase(displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700688
689 // TODO(b/74619554): Select internal/external display from remaining displays.
690 if (hwcDisplayId == mInternalHwcDisplayId) {
691 mInternalHwcDisplayId.reset();
692 } else if (hwcDisplayId == mExternalHwcDisplayId) {
693 mExternalHwcDisplayId.reset();
694 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700695
Dominik Laskowski7e045462018-05-30 13:02:02 -0700696 mHwcDevice->destroyDisplay(hwcDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800697}
698
Dominik Laskowski075d3172018-05-24 15:50:06 -0700699status_t HWComposer::setOutputBuffer(DisplayId displayId, const sp<Fence>& acquireFence,
700 const sp<GraphicBuffer>& buffer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700701 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700702 const auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800703
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700704 if (!displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700705 LOG_DISPLAY_ERROR(displayId, "Invalid operation on physical display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800706 return INVALID_OPERATION;
707 }
708
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700709 auto error = displayData.hwcDisplay->setOutputBuffer(buffer, acquireFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700710 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800711 return NO_ERROR;
712}
713
Dominik Laskowski075d3172018-05-24 15:50:06 -0700714void HWComposer::clearReleaseFences(DisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700715 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800716 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700717}
718
Dominik Laskowski075d3172018-05-24 15:50:06 -0700719status_t HWComposer::getHdrCapabilities(DisplayId displayId, HdrCapabilities* outCapabilities) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700720 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stozac4f471e2016-03-24 09:31:08 -0700721
722 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Peiyong Lin62665892018-04-16 11:07:44 -0700723 auto error = hwcDisplay->getHdrCapabilities(outCapabilities);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700724 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Peiyong Lin62665892018-04-16 11:07:44 -0700725 return NO_ERROR;
Dan Stozac4f471e2016-03-24 09:31:08 -0700726}
727
Dominik Laskowski075d3172018-05-24 15:50:06 -0700728int32_t HWComposer::getSupportedPerFrameMetadata(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700729 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700730 return mDisplayData.at(displayId).hwcDisplay->getSupportedPerFrameMetadata();
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700731}
732
Dominik Laskowski075d3172018-05-24 15:50:06 -0700733std::vector<ui::RenderIntent> HWComposer::getRenderIntents(DisplayId displayId,
734 ui::ColorMode colorMode) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700735 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700736
737 std::vector<ui::RenderIntent> renderIntents;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700738 auto error = mDisplayData.at(displayId).hwcDisplay->getRenderIntents(colorMode, &renderIntents);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700739 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700740 return renderIntents;
741}
742
Dominik Laskowski075d3172018-05-24 15:50:06 -0700743mat4 HWComposer::getDataspaceSaturationMatrix(DisplayId displayId, ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700744 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700745
746 mat4 matrix;
747 auto error = mDisplayData[displayId].hwcDisplay->getDataspaceSaturationMatrix(dataspace,
748 &matrix);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700749 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700750 return matrix;
751}
752
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700753status_t HWComposer::getDisplayedContentSamplingAttributes(DisplayId displayId,
754 ui::PixelFormat* outFormat,
755 ui::Dataspace* outDataspace,
756 uint8_t* outComponentMask) {
757 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
758 const auto error =
759 mDisplayData[displayId]
760 .hwcDisplay->getDisplayedContentSamplingAttributes(outFormat, outDataspace,
761 outComponentMask);
762 if (error == HWC2::Error::Unsupported) RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
763 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
764 return NO_ERROR;
765}
766
Kevin DuBois74e53772018-11-19 10:52:38 -0800767status_t HWComposer::setDisplayContentSamplingEnabled(DisplayId displayId, bool enabled,
768 uint8_t componentMask, uint64_t maxFrames) {
769 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
770 const auto error =
771 mDisplayData[displayId].hwcDisplay->setDisplayContentSamplingEnabled(enabled,
772 componentMask,
773 maxFrames);
774
775 if (error == HWC2::Error::Unsupported) RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
776 if (error == HWC2::Error::BadParameter) RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
777 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
778 return NO_ERROR;
779}
780
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700781status_t HWComposer::getDisplayedContentSample(DisplayId displayId, uint64_t maxFrames,
782 uint64_t timestamp, DisplayedFrameStats* outStats) {
783 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
784 const auto error =
785 mDisplayData[displayId].hwcDisplay->getDisplayedContentSample(maxFrames, timestamp,
786 outStats);
787 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
788 return NO_ERROR;
789}
790
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800791bool HWComposer::isUsingVrComposer() const {
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800792 return getComposer()->isUsingVrComposer();
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800793}
794
Yiwei Zhang5434a782018-12-05 18:06:32 -0800795void HWComposer::dump(std::string& result) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800796 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow
797 // all the state going into the layers. This is probably better done in
798 // Layer itself, but it's going to take a bit of work to get there.
Yiwei Zhang5434a782018-12-05 18:06:32 -0800799 result.append(mHwcDevice->dump());
Mathias Agopian83727852010-09-23 18:13:21 -0700800}
801
Dominik Laskowski075d3172018-05-24 15:50:06 -0700802std::optional<DisplayId> HWComposer::toPhysicalDisplayId(hwc2_display_t hwcDisplayId) const {
803 if (const auto it = mPhysicalDisplayIdMap.find(hwcDisplayId);
804 it != mPhysicalDisplayIdMap.end()) {
805 return it->second;
806 }
807 return {};
808}
809
810std::optional<hwc2_display_t> HWComposer::fromPhysicalDisplayId(DisplayId displayId) const {
811 if (const auto it = mDisplayData.find(displayId);
812 it != mDisplayData.end() && !it->second.isVirtual) {
813 return it->second.hwcDisplay->getId();
814 }
815 return {};
816}
817
818std::optional<DisplayIdentificationInfo> HWComposer::onHotplugConnect(hwc2_display_t hwcDisplayId) {
819 if (isUsingVrComposer() && mInternalHwcDisplayId) {
820 ALOGE("Ignoring connection of external display %" PRIu64 " in VR mode", hwcDisplayId);
Steven Thomas6e8f7062017-11-22 14:15:29 -0800821 return {};
822 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700823
824 uint8_t port;
825 DisplayIdentificationData data;
826 const bool hasMultiDisplaySupport = getDisplayIdentificationData(hwcDisplayId, &port, &data);
827
828 if (mPhysicalDisplayIdMap.empty()) {
829 mHasMultiDisplaySupport = hasMultiDisplaySupport;
830 ALOGI("Switching to %s multi-display mode",
831 hasMultiDisplaySupport ? "generalized" : "legacy");
832 } else if (mHasMultiDisplaySupport && !hasMultiDisplaySupport) {
833 ALOGE("Ignoring connection of display %" PRIu64 " without identification data",
834 hwcDisplayId);
835 return {};
836 }
837
838 std::optional<DisplayIdentificationInfo> info;
839
840 if (mHasMultiDisplaySupport) {
841 info = parseDisplayIdentificationData(port, data);
842 ALOGE_IF(!info, "Failed to parse identification data for display %" PRIu64, hwcDisplayId);
843 } else if (mInternalHwcDisplayId && mExternalHwcDisplayId) {
844 ALOGE("Ignoring connection of tertiary display %" PRIu64, hwcDisplayId);
845 return {};
846 } else {
847 ALOGW_IF(hasMultiDisplaySupport, "Ignoring identification data for display %" PRIu64,
848 hwcDisplayId);
849 port = mInternalHwcDisplayId ? HWC_DISPLAY_EXTERNAL : HWC_DISPLAY_PRIMARY;
850 }
851
852 if (!mInternalHwcDisplayId) {
853 mInternalHwcDisplayId = hwcDisplayId;
854 } else if (!mExternalHwcDisplayId) {
855 mExternalHwcDisplayId = hwcDisplayId;
856 }
857
858 if (info) return info;
859
860 return DisplayIdentificationInfo{getFallbackDisplayId(port),
861 hwcDisplayId == mInternalHwcDisplayId ? "Internal display"
862 : "External display"};
Steven Thomas6e8f7062017-11-22 14:15:29 -0800863}
864
Lloyd Pique441d5042018-10-18 16:49:51 -0700865} // namespace impl
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700866} // namespace android