blob: 168b27cf53ed6a8b0fb3fac95fb898e7b5bec0db [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
Dominik Laskowski1af47932018-11-12 10:20:46 -080069HWComposer::HWComposer(std::unique_ptr<Hwc2::Composer> composer)
Lloyd Piquea822d522017-12-20 16:42:57 -080070 : mHwcDevice(std::make_unique<HWC2::Device>(std::move(composer))) {}
Mathias Agopianbef42c52013-08-21 17:45:46 -070071
Dominik Laskowskib04f98a2018-11-07 21:07:16 -080072HWComposer::~HWComposer() {
73 mDisplayData.clear();
74}
Dan Stoza9e56aa02015-11-02 13:00:03 -080075
Steven Thomas94e35b92017-07-26 18:48:28 -070076void HWComposer::registerCallback(HWC2::ComposerCallback* callback,
77 int32_t sequenceId) {
78 mHwcDevice->registerCallback(callback, sequenceId);
Dan Stoza9e56aa02015-11-02 13:00:03 -080079}
80
Dominik Laskowskia2edf612018-06-01 13:15:16 -070081bool HWComposer::getDisplayIdentificationData(hwc2_display_t hwcDisplayId, uint8_t* outPort,
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070082 DisplayIdentificationData* outData) const {
Dominik Laskowski0954b1d2018-06-13 14:58:49 -070083 const auto error = mHwcDevice->getDisplayIdentificationData(hwcDisplayId, outPort, outData);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070084 if (error != HWC2::Error::None) {
Chia-I Wud0aff9d2018-06-21 13:39:09 +080085 if (error != HWC2::Error::Unsupported) {
86 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, to_string(error).c_str());
87 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070088 return false;
89 }
90 return true;
91}
92
Dan Stoza9f26a9c2016-06-22 14:51:09 -070093bool HWComposer::hasCapability(HWC2::Capability capability) const
94{
95 return mHwcDevice->getCapabilities().count(capability) > 0;
96}
97
Peiyong Lined531a32018-10-26 18:27:56 -070098bool HWComposer::hasDisplayCapability(const std::optional<DisplayId>& displayId,
99 HWC2::DisplayCapability capability) const {
100 if (!displayId) {
Peiyong Lindf65fd22019-01-03 15:17:05 -0800101 // Checkout global capabilities for displays without a corresponding HWC display.
102 if (capability == HWC2::DisplayCapability::SkipClientColorTransform) {
103 return hasCapability(HWC2::Capability::SkipClientColorTransform);
104 }
Peiyong Lined531a32018-10-26 18:27:56 -0700105 return false;
106 }
107 RETURN_IF_INVALID_DISPLAY(*displayId, false);
108 return mDisplayData.at(*displayId).hwcDisplay->getCapabilities().count(capability) > 0;
109}
110
Dan Stoza9e56aa02015-11-02 13:00:03 -0800111void HWComposer::validateChange(HWC2::Composition from, HWC2::Composition to) {
112 bool valid = true;
113 switch (from) {
114 case HWC2::Composition::Client:
115 valid = false;
116 break;
117 case HWC2::Composition::Device:
118 case HWC2::Composition::SolidColor:
119 valid = (to == HWC2::Composition::Client);
120 break;
121 case HWC2::Composition::Cursor:
122 case HWC2::Composition::Sideband:
123 valid = (to == HWC2::Composition::Client ||
124 to == HWC2::Composition::Device);
125 break;
126 default:
127 break;
128 }
129
130 if (!valid) {
131 ALOGE("Invalid layer type change: %s --> %s", to_string(from).c_str(),
132 to_string(to).c_str());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700133 }
134}
135
Dominik Laskowski075d3172018-05-24 15:50:06 -0700136std::optional<DisplayIdentificationInfo> HWComposer::onHotplug(hwc2_display_t hwcDisplayId,
137 HWC2::Connection connection) {
138 std::optional<DisplayIdentificationInfo> info;
Lloyd Pique715a2c12017-12-14 17:18:08 -0800139
Dominik Laskowski075d3172018-05-24 15:50:06 -0700140 if (const auto displayId = toPhysicalDisplayId(hwcDisplayId)) {
141 info = DisplayIdentificationInfo{*displayId, std::string()};
142 } else {
143 if (connection == HWC2::Connection::Disconnected) {
144 ALOGE("Ignoring disconnection of invalid HWC display %" PRIu64, hwcDisplayId);
145 return {};
Lloyd Pique438e9e72018-09-04 18:06:08 -0700146 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700147
148 info = onHotplugConnect(hwcDisplayId);
149 if (!info) return {};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700150 }
151
Dominik Laskowski34157762018-10-31 13:07:19 -0700152 ALOGV("%s: %s %s display %s with HWC ID %" PRIu64, __FUNCTION__, to_string(connection).c_str(),
153 hwcDisplayId == mInternalHwcDisplayId ? "internal" : "external",
154 to_string(info->id).c_str(), hwcDisplayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700155
156 mHwcDevice->onHotplug(hwcDisplayId, connection);
157
Lloyd Pique715a2c12017-12-14 17:18:08 -0800158 // Disconnect is handled through HWComposer::disconnectDisplay via
159 // SurfaceFlinger's onHotplugReceived callback handling
160 if (connection == HWC2::Connection::Connected) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700161 mDisplayData[info->id].hwcDisplay = mHwcDevice->getDisplayById(hwcDisplayId);
162 mPhysicalDisplayIdMap[hwcDisplayId] = info->id;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700163 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700164
Dominik Laskowski075d3172018-05-24 15:50:06 -0700165 return info;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700166}
167
Dominik Laskowski075d3172018-05-24 15:50:06 -0700168bool HWComposer::onVsync(hwc2_display_t hwcDisplayId, int64_t timestamp) {
169 const auto displayId = toPhysicalDisplayId(hwcDisplayId);
170 if (!displayId) {
171 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, "Invalid HWC display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700172 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700173 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700174
Dominik Laskowski075d3172018-05-24 15:50:06 -0700175 RETURN_IF_INVALID_DISPLAY(*displayId, false);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700176
Dominik Laskowski1af47932018-11-12 10:20:46 -0800177 auto& displayData = mDisplayData[*displayId];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700178 if (displayData.isVirtual) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700179 LOG_DISPLAY_ERROR(*displayId, "Invalid operation on virtual display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700180 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700181 }
182
Dan Stoza9e56aa02015-11-02 13:00:03 -0800183 {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800184 std::lock_guard lock(displayData.lastHwVsyncLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700185
Dan Stoza9e56aa02015-11-02 13:00:03 -0800186 // There have been reports of HWCs that signal several vsync events
187 // with the same timestamp when turning the display off and on. This
188 // is a bug in the HWC implementation, but filter the extra events
189 // out here so they don't cause havoc downstream.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800190 if (timestamp == displayData.lastHwVsync) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700191 ALOGW("Ignoring duplicate VSYNC event from HWC for display %s (t=%" PRId64 ")",
192 to_string(*displayId).c_str(), timestamp);
Steven Thomas94e35b92017-07-26 18:48:28 -0700193 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800194 }
195
Dominik Laskowski1af47932018-11-12 10:20:46 -0800196 displayData.lastHwVsync = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700197 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800198
Dominik Laskowski34157762018-10-31 13:07:19 -0700199 const auto tag = "HW_VSYNC_" + to_string(*displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800200 ATRACE_INT(tag.c_str(), displayData.vsyncTraceToggle);
201 displayData.vsyncTraceToggle = !displayData.vsyncTraceToggle;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800202
Steven Thomas94e35b92017-07-26 18:48:28 -0700203 return true;
Jesse Hall1c569c42013-04-05 13:44:52 -0700204}
205
Dominik Laskowski075d3172018-05-24 15:50:06 -0700206std::optional<DisplayId> HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
207 ui::PixelFormat* format) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800208 if (mRemainingHwcVirtualDisplays == 0) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700209 ALOGE("%s: No remaining virtual displays", __FUNCTION__);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700210 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700211 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700212
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800213 if (SurfaceFlinger::maxVirtualDisplaySize != 0 &&
214 (width > SurfaceFlinger::maxVirtualDisplaySize ||
215 height > SurfaceFlinger::maxVirtualDisplaySize)) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700216 ALOGE("%s: Display size %ux%u exceeds maximum dimension of %" PRIu64, __FUNCTION__, width,
217 height, SurfaceFlinger::maxVirtualDisplaySize);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700218 return {};
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800219 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700220 HWC2::Display* display;
Dan Stoza5cf424b2016-05-20 14:02:39 -0700221 auto error = mHwcDevice->createVirtualDisplay(width, height, format,
222 &display);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800223 if (error != HWC2::Error::None) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700224 ALOGE("%s: Failed to create HWC virtual display", __FUNCTION__);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700225 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700226 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800227
Dominik Laskowski075d3172018-05-24 15:50:06 -0700228 DisplayId displayId;
229 if (mFreeVirtualDisplayIds.empty()) {
230 displayId = getVirtualDisplayId(mNextVirtualDisplayId++);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800231 } else {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700232 displayId = *mFreeVirtualDisplayIds.begin();
233 mFreeVirtualDisplayIds.erase(displayId);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700234 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800235
Dominik Laskowski075d3172018-05-24 15:50:06 -0700236 auto& displayData = mDisplayData[displayId];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700237 displayData.hwcDisplay = display;
238 displayData.isVirtual = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800239
240 --mRemainingHwcVirtualDisplays;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700241 return displayId;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700242}
243
Dominik Laskowski075d3172018-05-24 15:50:06 -0700244HWC2::Layer* HWComposer::createLayer(DisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700245 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
246
Dan Stoza9e56aa02015-11-02 13:00:03 -0800247 auto display = mDisplayData[displayId].hwcDisplay;
Steven Thomas94e35b92017-07-26 18:48:28 -0700248 HWC2::Layer* layer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800249 auto error = display->createLayer(&layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700250 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800251 return layer;
252}
253
Dominik Laskowski075d3172018-05-24 15:50:06 -0700254void HWComposer::destroyLayer(DisplayId displayId, HWC2::Layer* layer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700255 RETURN_IF_INVALID_DISPLAY(displayId);
256
Steven Thomas94e35b92017-07-26 18:48:28 -0700257 auto display = mDisplayData[displayId].hwcDisplay;
258 auto error = display->destroyLayer(layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700259 RETURN_IF_HWC_ERROR(error, displayId);
Steven Thomas94e35b92017-07-26 18:48:28 -0700260}
261
Dominik Laskowski075d3172018-05-24 15:50:06 -0700262nsecs_t HWComposer::getRefreshTimestamp(DisplayId displayId) const {
263 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800264 const auto& displayData = mDisplayData.at(displayId);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700265 // this returns the last refresh timestamp.
266 // if the last one is not available, we estimate it based on
267 // the refresh period and whatever closest timestamp we have.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800268 std::lock_guard lock(displayData.lastHwVsyncLock);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700269 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800270 auto vsyncPeriod = getActiveConfig(displayId)->getVsyncPeriod();
Dominik Laskowski1af47932018-11-12 10:20:46 -0800271 return now - ((now - displayData.lastHwVsync) % vsyncPeriod);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700272}
273
Dominik Laskowski075d3172018-05-24 15:50:06 -0700274bool HWComposer::isConnected(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700275 RETURN_IF_INVALID_DISPLAY(displayId, false);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700276 return mDisplayData.at(displayId).hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700277}
278
Dominik Laskowski075d3172018-05-24 15:50:06 -0700279std::vector<std::shared_ptr<const HWC2::Display::Config>> HWComposer::getConfigs(
280 DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700281 RETURN_IF_INVALID_DISPLAY(displayId, {});
282
Dominik Laskowski075d3172018-05-24 15:50:06 -0700283 const auto& displayData = mDisplayData.at(displayId);
284 auto configs = displayData.hwcDisplay->getConfigs();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800285 if (displayData.configMap.empty()) {
286 for (size_t i = 0; i < configs.size(); ++i) {
287 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700288 }
289 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800290 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700291}
292
Dominik Laskowski075d3172018-05-24 15:50:06 -0700293std::shared_ptr<const HWC2::Display::Config> HWComposer::getActiveConfig(
294 DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700295 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
296
Dan Stoza9e56aa02015-11-02 13:00:03 -0800297 std::shared_ptr<const HWC2::Display::Config> config;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700298 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfig(&config);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800299 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700300 LOG_DISPLAY_ERROR(displayId, "No active config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800301 return nullptr;
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700302 }
303
304 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
305
306 if (!config) {
307 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800308 return nullptr;
309 }
310
311 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700312}
313
Dominik Laskowski075d3172018-05-24 15:50:06 -0700314int HWComposer::getActiveConfigIndex(DisplayId displayId) const {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700315 RETURN_IF_INVALID_DISPLAY(displayId, -1);
316
Lloyd Pique3c085a02018-05-09 19:38:32 -0700317 int index;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700318 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfigIndex(&index);
Lloyd Pique3c085a02018-05-09 19:38:32 -0700319 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700320 LOG_DISPLAY_ERROR(displayId, "No active config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700321 return -1;
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700322 }
323
324 RETURN_IF_HWC_ERROR(error, displayId, -1);
325
326 if (index < 0) {
327 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700328 return -1;
329 }
330
331 return index;
332}
333
Dominik Laskowski075d3172018-05-24 15:50:06 -0700334std::vector<ui::ColorMode> HWComposer::getColorModes(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700335 RETURN_IF_INVALID_DISPLAY(displayId, {});
336
Peiyong Linfd997e02018-03-28 15:29:00 -0700337 std::vector<ui::ColorMode> modes;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700338 auto error = mDisplayData.at(displayId).hwcDisplay->getColorModes(&modes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700339 RETURN_IF_HWC_ERROR(error, displayId, {});
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600340 return modes;
341}
342
Dominik Laskowski075d3172018-05-24 15:50:06 -0700343status_t HWComposer::setActiveColorMode(DisplayId displayId, ui::ColorMode mode,
344 ui::RenderIntent renderIntent) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700345 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Michael Wright28f24d02016-07-12 13:30:53 -0700346
347 auto& displayData = mDisplayData[displayId];
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700348 auto error = displayData.hwcDisplay->setColorMode(mode, renderIntent);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700349 RETURN_IF_HWC_ERROR_FOR(("setColorMode(" + decodeColorMode(mode) + ", " +
350 decodeRenderIntent(renderIntent) + ")")
351 .c_str(),
352 error, displayId, UNKNOWN_ERROR);
Michael Wright28f24d02016-07-12 13:30:53 -0700353
354 return NO_ERROR;
355}
356
Dominik Laskowski075d3172018-05-24 15:50:06 -0700357void HWComposer::setVsyncEnabled(DisplayId displayId, HWC2::Vsync enabled) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700358 RETURN_IF_INVALID_DISPLAY(displayId);
359 auto& displayData = mDisplayData[displayId];
360
361 if (displayData.isVirtual) {
362 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800363 return;
364 }
365
Dan Stoza9e56aa02015-11-02 13:00:03 -0800366 // NOTE: we use our own internal lock here because we have to call
367 // into the HWC with the lock held, and we want to make sure
368 // that even if HWC blocks (which it shouldn't), it won't
369 // affect other threads.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800370 std::lock_guard lock(displayData.vsyncEnabledLock);
371 if (enabled == displayData.vsyncEnabled) {
372 return;
Colin Cross10fbdb62012-07-12 17:56:34 -0700373 }
Dominik Laskowski1af47932018-11-12 10:20:46 -0800374
375 ATRACE_CALL();
376 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
377 RETURN_IF_HWC_ERROR(error, displayId);
378
379 displayData.vsyncEnabled = enabled;
380
381 const auto tag = "HW_VSYNC_ON_" + to_string(displayId);
382 ATRACE_INT(tag.c_str(), enabled == HWC2::Vsync::Enable ? 1 : 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700383}
384
Dominik Laskowski075d3172018-05-24 15:50:06 -0700385status_t HWComposer::setClientTarget(DisplayId displayId, uint32_t slot,
386 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
387 ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700388 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Jesse Hall851cfe82013-03-20 13:44:00 -0700389
Dominik Laskowski34157762018-10-31 13:07:19 -0700390 ALOGV("%s for display %s", __FUNCTION__, to_string(displayId).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800391 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400392 auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700393 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Jesse Hall851cfe82013-03-20 13:44:00 -0700394 return NO_ERROR;
395}
396
Dominik Laskowski075d3172018-05-24 15:50:06 -0700397status_t HWComposer::prepare(DisplayId displayId, std::vector<CompositionInfo>& compositionData) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800398 ATRACE_CALL();
399
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700400 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800401
402 auto& displayData = mDisplayData[displayId];
403 auto& hwcDisplay = displayData.hwcDisplay;
404 if (!hwcDisplay->isConnected()) {
405 return NO_ERROR;
406 }
407
408 uint32_t numTypes = 0;
409 uint32_t numRequests = 0;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700410
411 HWC2::Error error = HWC2::Error::None;
412
Chia-I Wu41b98d42017-12-11 11:04:36 -0800413 // First try to skip validate altogether when there is no client
414 // composition. When there is client composition, since we haven't
415 // rendered to the client target yet, we should not attempt to skip
416 // validate.
417 //
418 // displayData.hasClientComposition hasn't been updated for this frame.
419 // The check below is incorrect. We actually rely on HWC here to fall
420 // back to validate when there is any client layer.
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700421 displayData.validateWasSkipped = false;
Chia-I Wu41b98d42017-12-11 11:04:36 -0800422 if (!displayData.hasClientComposition) {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800423 sp<Fence> outPresentFence;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700424 uint32_t state = UINT32_MAX;
425 error = hwcDisplay->presentOrValidate(&numTypes, &numRequests, &outPresentFence , &state);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700426 if (error != HWC2::Error::HasChanges) {
427 RETURN_IF_HWC_ERROR_FOR("presentOrValidate", error, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700428 }
429 if (state == 1) { //Present Succeeded.
Steven Thomas94e35b92017-07-26 18:48:28 -0700430 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700431 error = hwcDisplay->getReleaseFences(&releaseFences);
432 displayData.releaseFences = std::move(releaseFences);
433 displayData.lastPresentFence = outPresentFence;
434 displayData.validateWasSkipped = true;
435 displayData.presentError = error;
436 return NO_ERROR;
437 }
438 // Present failed but Validate ran.
439 } else {
440 error = hwcDisplay->validate(&numTypes, &numRequests);
441 }
442 ALOGV("SkipValidate failed, Falling back to SLOW validate/present");
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700443 if (error != HWC2::Error::HasChanges) {
444 RETURN_IF_HWC_ERROR_FOR("validate", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800445 }
446
Steven Thomas94e35b92017-07-26 18:48:28 -0700447 std::unordered_map<HWC2::Layer*, HWC2::Composition> changedTypes;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800448 changedTypes.reserve(numTypes);
449 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700450 RETURN_IF_HWC_ERROR_FOR("getChangedCompositionTypes", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800451
452 displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0);
Steven Thomas94e35b92017-07-26 18:48:28 -0700453 std::unordered_map<HWC2::Layer*, HWC2::LayerRequest> layerRequests;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800454 layerRequests.reserve(numRequests);
455 error = hwcDisplay->getRequests(&displayData.displayRequests,
456 &layerRequests);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700457 RETURN_IF_HWC_ERROR_FOR("getRequests", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800458
459 displayData.hasClientComposition = false;
460 displayData.hasDeviceComposition = false;
David Sodmanfb95bcc2017-12-22 15:45:30 -0800461 for (auto& compositionInfo : compositionData) {
462 auto hwcLayer = compositionInfo.hwc.hwcLayer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800463
David Sodmanfb95bcc2017-12-22 15:45:30 -0800464 if (changedTypes.count(&*hwcLayer) != 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800465 // We pass false so we only update our state and don't call back
466 // into the HWC device
David Sodmanfb95bcc2017-12-22 15:45:30 -0800467 validateChange(compositionInfo.compositionType,
468 changedTypes[&*hwcLayer]);
469 compositionInfo.compositionType = changedTypes[&*hwcLayer];
David Sodmanc1498e62018-09-12 14:36:26 -0700470 compositionInfo.layer->mLayer->setCompositionType(displayId,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700471 compositionInfo.compositionType,
472 false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800473 }
474
David Sodmanfb95bcc2017-12-22 15:45:30 -0800475 switch (compositionInfo.compositionType) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800476 case HWC2::Composition::Client:
477 displayData.hasClientComposition = true;
478 break;
479 case HWC2::Composition::Device:
480 case HWC2::Composition::SolidColor:
481 case HWC2::Composition::Cursor:
482 case HWC2::Composition::Sideband:
483 displayData.hasDeviceComposition = true;
484 break;
485 default:
486 break;
487 }
488
David Sodmanfb95bcc2017-12-22 15:45:30 -0800489 if (layerRequests.count(&*hwcLayer) != 0 &&
490 layerRequests[&*hwcLayer] ==
Dan Stoza9e56aa02015-11-02 13:00:03 -0800491 HWC2::LayerRequest::ClearClientTarget) {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800492 compositionInfo.hwc.clearClientTarget = true;
David Sodmanc1498e62018-09-12 14:36:26 -0700493 compositionInfo.layer->mLayer->setClearClientTarget(displayId, true);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800494 } else {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800495 if (layerRequests.count(&*hwcLayer) != 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700496 LOG_DISPLAY_ERROR(displayId,
David Sodmanfb95bcc2017-12-22 15:45:30 -0800497 ("Unknown layer request " + to_string(layerRequests[&*hwcLayer]))
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700498 .c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800499 }
David Sodmanfb95bcc2017-12-22 15:45:30 -0800500 compositionInfo.hwc.clearClientTarget = false;
David Sodmanc1498e62018-09-12 14:36:26 -0700501 compositionInfo.layer->mLayer->setClearClientTarget(displayId, false);
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
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800787bool HWComposer::isUsingVrComposer() const {
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800788 return getComposer()->isUsingVrComposer();
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800789}
790
Yiwei Zhang5434a782018-12-05 18:06:32 -0800791void HWComposer::dump(std::string& result) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800792 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow
793 // all the state going into the layers. This is probably better done in
794 // Layer itself, but it's going to take a bit of work to get there.
Yiwei Zhang5434a782018-12-05 18:06:32 -0800795 result.append(mHwcDevice->dump());
Mathias Agopian83727852010-09-23 18:13:21 -0700796}
797
Dominik Laskowski075d3172018-05-24 15:50:06 -0700798std::optional<DisplayId> HWComposer::toPhysicalDisplayId(hwc2_display_t hwcDisplayId) const {
799 if (const auto it = mPhysicalDisplayIdMap.find(hwcDisplayId);
800 it != mPhysicalDisplayIdMap.end()) {
801 return it->second;
802 }
803 return {};
804}
805
806std::optional<hwc2_display_t> HWComposer::fromPhysicalDisplayId(DisplayId displayId) const {
807 if (const auto it = mDisplayData.find(displayId);
808 it != mDisplayData.end() && !it->second.isVirtual) {
809 return it->second.hwcDisplay->getId();
810 }
811 return {};
812}
813
814std::optional<DisplayIdentificationInfo> HWComposer::onHotplugConnect(hwc2_display_t hwcDisplayId) {
815 if (isUsingVrComposer() && mInternalHwcDisplayId) {
816 ALOGE("Ignoring connection of external display %" PRIu64 " in VR mode", hwcDisplayId);
Steven Thomas6e8f7062017-11-22 14:15:29 -0800817 return {};
818 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700819
820 uint8_t port;
821 DisplayIdentificationData data;
822 const bool hasMultiDisplaySupport = getDisplayIdentificationData(hwcDisplayId, &port, &data);
823
824 if (mPhysicalDisplayIdMap.empty()) {
825 mHasMultiDisplaySupport = hasMultiDisplaySupport;
826 ALOGI("Switching to %s multi-display mode",
827 hasMultiDisplaySupport ? "generalized" : "legacy");
828 } else if (mHasMultiDisplaySupport && !hasMultiDisplaySupport) {
829 ALOGE("Ignoring connection of display %" PRIu64 " without identification data",
830 hwcDisplayId);
831 return {};
832 }
833
834 std::optional<DisplayIdentificationInfo> info;
835
836 if (mHasMultiDisplaySupport) {
837 info = parseDisplayIdentificationData(port, data);
838 ALOGE_IF(!info, "Failed to parse identification data for display %" PRIu64, hwcDisplayId);
839 } else if (mInternalHwcDisplayId && mExternalHwcDisplayId) {
840 ALOGE("Ignoring connection of tertiary display %" PRIu64, hwcDisplayId);
841 return {};
842 } else {
843 ALOGW_IF(hasMultiDisplaySupport, "Ignoring identification data for display %" PRIu64,
844 hwcDisplayId);
845 port = mInternalHwcDisplayId ? HWC_DISPLAY_EXTERNAL : HWC_DISPLAY_PRIMARY;
846 }
847
848 if (!mInternalHwcDisplayId) {
849 mInternalHwcDisplayId = hwcDisplayId;
850 } else if (!mExternalHwcDisplayId) {
851 mExternalHwcDisplayId = hwcDisplayId;
852 }
853
854 if (info) return info;
855
856 return DisplayIdentificationInfo{getFallbackDisplayId(port),
857 hwcDisplayId == mInternalHwcDisplayId ? "Internal display"
858 : "External display"};
Steven Thomas6e8f7062017-11-22 14:15:29 -0800859}
860
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700861} // namespace android