blob: 9bbc37fe878210ad2a0d4fd7d256c2b43dc77a04 [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
Dan Stoza9e56aa02015-11-02 13:00:03 -080098void HWComposer::validateChange(HWC2::Composition from, HWC2::Composition to) {
99 bool valid = true;
100 switch (from) {
101 case HWC2::Composition::Client:
102 valid = false;
103 break;
104 case HWC2::Composition::Device:
105 case HWC2::Composition::SolidColor:
106 valid = (to == HWC2::Composition::Client);
107 break;
108 case HWC2::Composition::Cursor:
109 case HWC2::Composition::Sideband:
110 valid = (to == HWC2::Composition::Client ||
111 to == HWC2::Composition::Device);
112 break;
113 default:
114 break;
115 }
116
117 if (!valid) {
118 ALOGE("Invalid layer type change: %s --> %s", to_string(from).c_str(),
119 to_string(to).c_str());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700120 }
121}
122
Dominik Laskowski075d3172018-05-24 15:50:06 -0700123std::optional<DisplayIdentificationInfo> HWComposer::onHotplug(hwc2_display_t hwcDisplayId,
124 HWC2::Connection connection) {
125 std::optional<DisplayIdentificationInfo> info;
Lloyd Pique715a2c12017-12-14 17:18:08 -0800126
Dominik Laskowski075d3172018-05-24 15:50:06 -0700127 if (const auto displayId = toPhysicalDisplayId(hwcDisplayId)) {
128 info = DisplayIdentificationInfo{*displayId, std::string()};
129 } else {
130 if (connection == HWC2::Connection::Disconnected) {
131 ALOGE("Ignoring disconnection of invalid HWC display %" PRIu64, hwcDisplayId);
132 return {};
Lloyd Pique438e9e72018-09-04 18:06:08 -0700133 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700134
135 info = onHotplugConnect(hwcDisplayId);
136 if (!info) return {};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700137 }
138
Dominik Laskowski34157762018-10-31 13:07:19 -0700139 ALOGV("%s: %s %s display %s with HWC ID %" PRIu64, __FUNCTION__, to_string(connection).c_str(),
140 hwcDisplayId == mInternalHwcDisplayId ? "internal" : "external",
141 to_string(info->id).c_str(), hwcDisplayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700142
143 mHwcDevice->onHotplug(hwcDisplayId, connection);
144
Lloyd Pique715a2c12017-12-14 17:18:08 -0800145 // Disconnect is handled through HWComposer::disconnectDisplay via
146 // SurfaceFlinger's onHotplugReceived callback handling
147 if (connection == HWC2::Connection::Connected) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700148 mDisplayData[info->id].hwcDisplay = mHwcDevice->getDisplayById(hwcDisplayId);
149 mPhysicalDisplayIdMap[hwcDisplayId] = info->id;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700150 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700151
Dominik Laskowski075d3172018-05-24 15:50:06 -0700152 return info;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700153}
154
Dominik Laskowski075d3172018-05-24 15:50:06 -0700155bool HWComposer::onVsync(hwc2_display_t hwcDisplayId, int64_t timestamp) {
156 const auto displayId = toPhysicalDisplayId(hwcDisplayId);
157 if (!displayId) {
158 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, "Invalid HWC display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700159 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700160 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700161
Dominik Laskowski075d3172018-05-24 15:50:06 -0700162 RETURN_IF_INVALID_DISPLAY(*displayId, false);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700163
Dominik Laskowski1af47932018-11-12 10:20:46 -0800164 auto& displayData = mDisplayData[*displayId];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700165 if (displayData.isVirtual) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700166 LOG_DISPLAY_ERROR(*displayId, "Invalid operation on virtual display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700167 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700168 }
169
Dan Stoza9e56aa02015-11-02 13:00:03 -0800170 {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800171 std::lock_guard lock(displayData.lastHwVsyncLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700172
Dan Stoza9e56aa02015-11-02 13:00:03 -0800173 // There have been reports of HWCs that signal several vsync events
174 // with the same timestamp when turning the display off and on. This
175 // is a bug in the HWC implementation, but filter the extra events
176 // out here so they don't cause havoc downstream.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800177 if (timestamp == displayData.lastHwVsync) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700178 ALOGW("Ignoring duplicate VSYNC event from HWC for display %s (t=%" PRId64 ")",
179 to_string(*displayId).c_str(), timestamp);
Steven Thomas94e35b92017-07-26 18:48:28 -0700180 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800181 }
182
Dominik Laskowski1af47932018-11-12 10:20:46 -0800183 displayData.lastHwVsync = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700184 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800185
Dominik Laskowski34157762018-10-31 13:07:19 -0700186 const auto tag = "HW_VSYNC_" + to_string(*displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800187 ATRACE_INT(tag.c_str(), displayData.vsyncTraceToggle);
188 displayData.vsyncTraceToggle = !displayData.vsyncTraceToggle;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800189
Steven Thomas94e35b92017-07-26 18:48:28 -0700190 return true;
Jesse Hall1c569c42013-04-05 13:44:52 -0700191}
192
Dominik Laskowski075d3172018-05-24 15:50:06 -0700193std::optional<DisplayId> HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
194 ui::PixelFormat* format) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800195 if (mRemainingHwcVirtualDisplays == 0) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700196 ALOGE("%s: No remaining virtual displays", __FUNCTION__);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700197 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700198 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700199
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800200 if (SurfaceFlinger::maxVirtualDisplaySize != 0 &&
201 (width > SurfaceFlinger::maxVirtualDisplaySize ||
202 height > SurfaceFlinger::maxVirtualDisplaySize)) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700203 ALOGE("%s: Display size %ux%u exceeds maximum dimension of %" PRIu64, __FUNCTION__, width,
204 height, SurfaceFlinger::maxVirtualDisplaySize);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700205 return {};
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800206 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700207 HWC2::Display* display;
Dan Stoza5cf424b2016-05-20 14:02:39 -0700208 auto error = mHwcDevice->createVirtualDisplay(width, height, format,
209 &display);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800210 if (error != HWC2::Error::None) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700211 ALOGE("%s: Failed to create HWC virtual display", __FUNCTION__);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700212 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700213 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800214
Dominik Laskowski075d3172018-05-24 15:50:06 -0700215 DisplayId displayId;
216 if (mFreeVirtualDisplayIds.empty()) {
217 displayId = getVirtualDisplayId(mNextVirtualDisplayId++);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800218 } else {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700219 displayId = *mFreeVirtualDisplayIds.begin();
220 mFreeVirtualDisplayIds.erase(displayId);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700221 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800222
Dominik Laskowski075d3172018-05-24 15:50:06 -0700223 auto& displayData = mDisplayData[displayId];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700224 displayData.hwcDisplay = display;
225 displayData.isVirtual = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800226
227 --mRemainingHwcVirtualDisplays;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700228 return displayId;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700229}
230
Dominik Laskowski075d3172018-05-24 15:50:06 -0700231HWC2::Layer* HWComposer::createLayer(DisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700232 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
233
Dan Stoza9e56aa02015-11-02 13:00:03 -0800234 auto display = mDisplayData[displayId].hwcDisplay;
Steven Thomas94e35b92017-07-26 18:48:28 -0700235 HWC2::Layer* layer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800236 auto error = display->createLayer(&layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700237 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800238 return layer;
239}
240
Dominik Laskowski075d3172018-05-24 15:50:06 -0700241void HWComposer::destroyLayer(DisplayId displayId, HWC2::Layer* layer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700242 RETURN_IF_INVALID_DISPLAY(displayId);
243
Steven Thomas94e35b92017-07-26 18:48:28 -0700244 auto display = mDisplayData[displayId].hwcDisplay;
245 auto error = display->destroyLayer(layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700246 RETURN_IF_HWC_ERROR(error, displayId);
Steven Thomas94e35b92017-07-26 18:48:28 -0700247}
248
Dominik Laskowski075d3172018-05-24 15:50:06 -0700249nsecs_t HWComposer::getRefreshTimestamp(DisplayId displayId) const {
250 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800251 const auto& displayData = mDisplayData.at(displayId);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700252 // this returns the last refresh timestamp.
253 // if the last one is not available, we estimate it based on
254 // the refresh period and whatever closest timestamp we have.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800255 std::lock_guard lock(displayData.lastHwVsyncLock);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700256 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800257 auto vsyncPeriod = getActiveConfig(displayId)->getVsyncPeriod();
Dominik Laskowski1af47932018-11-12 10:20:46 -0800258 return now - ((now - displayData.lastHwVsync) % vsyncPeriod);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700259}
260
Dominik Laskowski075d3172018-05-24 15:50:06 -0700261bool HWComposer::isConnected(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700262 RETURN_IF_INVALID_DISPLAY(displayId, false);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700263 return mDisplayData.at(displayId).hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700264}
265
Dominik Laskowski075d3172018-05-24 15:50:06 -0700266std::vector<std::shared_ptr<const HWC2::Display::Config>> HWComposer::getConfigs(
267 DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700268 RETURN_IF_INVALID_DISPLAY(displayId, {});
269
Dominik Laskowski075d3172018-05-24 15:50:06 -0700270 const auto& displayData = mDisplayData.at(displayId);
271 auto configs = displayData.hwcDisplay->getConfigs();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800272 if (displayData.configMap.empty()) {
273 for (size_t i = 0; i < configs.size(); ++i) {
274 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700275 }
276 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800277 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700278}
279
Dominik Laskowski075d3172018-05-24 15:50:06 -0700280std::shared_ptr<const HWC2::Display::Config> HWComposer::getActiveConfig(
281 DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700282 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
283
Dan Stoza9e56aa02015-11-02 13:00:03 -0800284 std::shared_ptr<const HWC2::Display::Config> config;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700285 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfig(&config);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800286 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700287 LOG_DISPLAY_ERROR(displayId, "No active config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800288 return nullptr;
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700289 }
290
291 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
292
293 if (!config) {
294 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800295 return nullptr;
296 }
297
298 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700299}
300
Dominik Laskowski075d3172018-05-24 15:50:06 -0700301int HWComposer::getActiveConfigIndex(DisplayId displayId) const {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700302 RETURN_IF_INVALID_DISPLAY(displayId, -1);
303
Lloyd Pique3c085a02018-05-09 19:38:32 -0700304 int index;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700305 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfigIndex(&index);
Lloyd Pique3c085a02018-05-09 19:38:32 -0700306 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700307 LOG_DISPLAY_ERROR(displayId, "No active config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700308 return -1;
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700309 }
310
311 RETURN_IF_HWC_ERROR(error, displayId, -1);
312
313 if (index < 0) {
314 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700315 return -1;
316 }
317
318 return index;
319}
320
Dominik Laskowski075d3172018-05-24 15:50:06 -0700321std::vector<ui::ColorMode> HWComposer::getColorModes(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700322 RETURN_IF_INVALID_DISPLAY(displayId, {});
323
Peiyong Linfd997e02018-03-28 15:29:00 -0700324 std::vector<ui::ColorMode> modes;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700325 auto error = mDisplayData.at(displayId).hwcDisplay->getColorModes(&modes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700326 RETURN_IF_HWC_ERROR(error, displayId, {});
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600327 return modes;
328}
329
Dominik Laskowski075d3172018-05-24 15:50:06 -0700330status_t HWComposer::setActiveColorMode(DisplayId displayId, ui::ColorMode mode,
331 ui::RenderIntent renderIntent) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700332 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Michael Wright28f24d02016-07-12 13:30:53 -0700333
334 auto& displayData = mDisplayData[displayId];
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700335 auto error = displayData.hwcDisplay->setColorMode(mode, renderIntent);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700336 RETURN_IF_HWC_ERROR_FOR(("setColorMode(" + decodeColorMode(mode) + ", " +
337 decodeRenderIntent(renderIntent) + ")")
338 .c_str(),
339 error, displayId, UNKNOWN_ERROR);
Michael Wright28f24d02016-07-12 13:30:53 -0700340
341 return NO_ERROR;
342}
343
Dominik Laskowski075d3172018-05-24 15:50:06 -0700344void HWComposer::setVsyncEnabled(DisplayId displayId, HWC2::Vsync enabled) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700345 RETURN_IF_INVALID_DISPLAY(displayId);
346 auto& displayData = mDisplayData[displayId];
347
348 if (displayData.isVirtual) {
349 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800350 return;
351 }
352
Dan Stoza9e56aa02015-11-02 13:00:03 -0800353 // NOTE: we use our own internal lock here because we have to call
354 // into the HWC with the lock held, and we want to make sure
355 // that even if HWC blocks (which it shouldn't), it won't
356 // affect other threads.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800357 std::lock_guard lock(displayData.vsyncEnabledLock);
358 if (enabled == displayData.vsyncEnabled) {
359 return;
Colin Cross10fbdb62012-07-12 17:56:34 -0700360 }
Dominik Laskowski1af47932018-11-12 10:20:46 -0800361
362 ATRACE_CALL();
363 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
364 RETURN_IF_HWC_ERROR(error, displayId);
365
366 displayData.vsyncEnabled = enabled;
367
368 const auto tag = "HW_VSYNC_ON_" + to_string(displayId);
369 ATRACE_INT(tag.c_str(), enabled == HWC2::Vsync::Enable ? 1 : 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700370}
371
Dominik Laskowski075d3172018-05-24 15:50:06 -0700372status_t HWComposer::setClientTarget(DisplayId displayId, uint32_t slot,
373 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
374 ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700375 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Jesse Hall851cfe82013-03-20 13:44:00 -0700376
Dominik Laskowski34157762018-10-31 13:07:19 -0700377 ALOGV("%s for display %s", __FUNCTION__, to_string(displayId).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800378 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400379 auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700380 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Jesse Hall851cfe82013-03-20 13:44:00 -0700381 return NO_ERROR;
382}
383
Dominik Laskowski075d3172018-05-24 15:50:06 -0700384status_t HWComposer::prepare(DisplayId displayId, std::vector<CompositionInfo>& compositionData) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800385 ATRACE_CALL();
386
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700387 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800388
389 auto& displayData = mDisplayData[displayId];
390 auto& hwcDisplay = displayData.hwcDisplay;
391 if (!hwcDisplay->isConnected()) {
392 return NO_ERROR;
393 }
394
395 uint32_t numTypes = 0;
396 uint32_t numRequests = 0;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700397
398 HWC2::Error error = HWC2::Error::None;
399
Chia-I Wu41b98d42017-12-11 11:04:36 -0800400 // First try to skip validate altogether when there is no client
401 // composition. When there is client composition, since we haven't
402 // rendered to the client target yet, we should not attempt to skip
403 // validate.
404 //
405 // displayData.hasClientComposition hasn't been updated for this frame.
406 // The check below is incorrect. We actually rely on HWC here to fall
407 // back to validate when there is any client layer.
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700408 displayData.validateWasSkipped = false;
Chia-I Wu41b98d42017-12-11 11:04:36 -0800409 if (!displayData.hasClientComposition) {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800410 sp<Fence> outPresentFence;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700411 uint32_t state = UINT32_MAX;
412 error = hwcDisplay->presentOrValidate(&numTypes, &numRequests, &outPresentFence , &state);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700413 if (error != HWC2::Error::HasChanges) {
414 RETURN_IF_HWC_ERROR_FOR("presentOrValidate", error, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700415 }
416 if (state == 1) { //Present Succeeded.
Steven Thomas94e35b92017-07-26 18:48:28 -0700417 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700418 error = hwcDisplay->getReleaseFences(&releaseFences);
419 displayData.releaseFences = std::move(releaseFences);
420 displayData.lastPresentFence = outPresentFence;
421 displayData.validateWasSkipped = true;
422 displayData.presentError = error;
423 return NO_ERROR;
424 }
425 // Present failed but Validate ran.
426 } else {
427 error = hwcDisplay->validate(&numTypes, &numRequests);
428 }
429 ALOGV("SkipValidate failed, Falling back to SLOW validate/present");
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700430 if (error != HWC2::Error::HasChanges) {
431 RETURN_IF_HWC_ERROR_FOR("validate", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800432 }
433
Steven Thomas94e35b92017-07-26 18:48:28 -0700434 std::unordered_map<HWC2::Layer*, HWC2::Composition> changedTypes;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800435 changedTypes.reserve(numTypes);
436 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700437 RETURN_IF_HWC_ERROR_FOR("getChangedCompositionTypes", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800438
439 displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0);
Steven Thomas94e35b92017-07-26 18:48:28 -0700440 std::unordered_map<HWC2::Layer*, HWC2::LayerRequest> layerRequests;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800441 layerRequests.reserve(numRequests);
442 error = hwcDisplay->getRequests(&displayData.displayRequests,
443 &layerRequests);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700444 RETURN_IF_HWC_ERROR_FOR("getRequests", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800445
446 displayData.hasClientComposition = false;
447 displayData.hasDeviceComposition = false;
David Sodmanfb95bcc2017-12-22 15:45:30 -0800448 for (auto& compositionInfo : compositionData) {
449 auto hwcLayer = compositionInfo.hwc.hwcLayer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800450
David Sodmanfb95bcc2017-12-22 15:45:30 -0800451 if (changedTypes.count(&*hwcLayer) != 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800452 // We pass false so we only update our state and don't call back
453 // into the HWC device
David Sodmanfb95bcc2017-12-22 15:45:30 -0800454 validateChange(compositionInfo.compositionType,
455 changedTypes[&*hwcLayer]);
456 compositionInfo.compositionType = changedTypes[&*hwcLayer];
David Sodmanc1498e62018-09-12 14:36:26 -0700457 compositionInfo.layer->mLayer->setCompositionType(displayId,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700458 compositionInfo.compositionType,
459 false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800460 }
461
David Sodmanfb95bcc2017-12-22 15:45:30 -0800462 switch (compositionInfo.compositionType) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800463 case HWC2::Composition::Client:
464 displayData.hasClientComposition = true;
465 break;
466 case HWC2::Composition::Device:
467 case HWC2::Composition::SolidColor:
468 case HWC2::Composition::Cursor:
469 case HWC2::Composition::Sideband:
470 displayData.hasDeviceComposition = true;
471 break;
472 default:
473 break;
474 }
475
David Sodmanfb95bcc2017-12-22 15:45:30 -0800476 if (layerRequests.count(&*hwcLayer) != 0 &&
477 layerRequests[&*hwcLayer] ==
Dan Stoza9e56aa02015-11-02 13:00:03 -0800478 HWC2::LayerRequest::ClearClientTarget) {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800479 compositionInfo.hwc.clearClientTarget = true;
David Sodmanc1498e62018-09-12 14:36:26 -0700480 compositionInfo.layer->mLayer->setClearClientTarget(displayId, true);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800481 } else {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800482 if (layerRequests.count(&*hwcLayer) != 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700483 LOG_DISPLAY_ERROR(displayId,
David Sodmanfb95bcc2017-12-22 15:45:30 -0800484 ("Unknown layer request " + to_string(layerRequests[&*hwcLayer]))
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700485 .c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800486 }
David Sodmanfb95bcc2017-12-22 15:45:30 -0800487 compositionInfo.hwc.clearClientTarget = false;
David Sodmanc1498e62018-09-12 14:36:26 -0700488 compositionInfo.layer->mLayer->setClearClientTarget(displayId, false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800489 }
490 }
491
492 error = hwcDisplay->acceptChanges();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700493 RETURN_IF_HWC_ERROR_FOR("acceptChanges", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800494
495 return NO_ERROR;
496}
497
Dominik Laskowski075d3172018-05-24 15:50:06 -0700498bool HWComposer::hasDeviceComposition(const std::optional<DisplayId>& displayId) const {
499 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700500 // Displays without a corresponding HWC display are never composed by
501 // the device
502 return false;
503 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700504
Dominik Laskowski075d3172018-05-24 15:50:06 -0700505 RETURN_IF_INVALID_DISPLAY(*displayId, false);
506 return mDisplayData.at(*displayId).hasDeviceComposition;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800507}
508
Dominik Laskowski075d3172018-05-24 15:50:06 -0700509bool HWComposer::hasFlipClientTargetRequest(const std::optional<DisplayId>& displayId) const {
510 if (!displayId) {
Madhuri Athota88a905b2017-05-04 16:58:15 +0530511 // Displays without a corresponding HWC display are never composed by
512 // the device
513 return false;
514 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700515
Dominik Laskowski075d3172018-05-24 15:50:06 -0700516 RETURN_IF_INVALID_DISPLAY(*displayId, false);
517 return ((static_cast<uint32_t>(mDisplayData.at(*displayId).displayRequests) &
Madhuri Athota88a905b2017-05-04 16:58:15 +0530518 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0);
519}
520
Dominik Laskowski075d3172018-05-24 15:50:06 -0700521bool HWComposer::hasClientComposition(const std::optional<DisplayId>& displayId) const {
522 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700523 // Displays without a corresponding HWC display are always composed by
524 // the client
525 return true;
526 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700527
Dominik Laskowski075d3172018-05-24 15:50:06 -0700528 RETURN_IF_INVALID_DISPLAY(*displayId, true);
529 return mDisplayData.at(*displayId).hasClientComposition;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800530}
531
Dominik Laskowski075d3172018-05-24 15:50:06 -0700532sp<Fence> HWComposer::getPresentFence(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700533 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700534 return mDisplayData.at(displayId).lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700535}
536
Dominik Laskowski075d3172018-05-24 15:50:06 -0700537sp<Fence> HWComposer::getLayerReleaseFence(DisplayId displayId, HWC2::Layer* layer) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700538 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700539 auto displayFences = mDisplayData.at(displayId).releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800540 if (displayFences.count(layer) == 0) {
541 ALOGV("getLayerReleaseFence: Release fence not found");
542 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700543 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800544 return displayFences[layer];
Riley Andrews03414a12014-07-01 14:22:59 -0700545}
546
Dominik Laskowski075d3172018-05-24 15:50:06 -0700547status_t HWComposer::presentAndGetReleaseFences(DisplayId displayId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800548 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700549
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700550 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700551
Dan Stoza9e56aa02015-11-02 13:00:03 -0800552 auto& displayData = mDisplayData[displayId];
553 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700554
555 if (displayData.validateWasSkipped) {
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700556 // explicitly flush all pending commands
557 auto error = mHwcDevice->flushCommands();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700558 RETURN_IF_HWC_ERROR_FOR("flushCommands", error, displayId, UNKNOWN_ERROR);
559 RETURN_IF_HWC_ERROR_FOR("present", displayData.presentError, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700560 return NO_ERROR;
561 }
562
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800563 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700564 RETURN_IF_HWC_ERROR_FOR("present", error, displayId, UNKNOWN_ERROR);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700565
Steven Thomas94e35b92017-07-26 18:48:28 -0700566 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800567 error = hwcDisplay->getReleaseFences(&releaseFences);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700568 RETURN_IF_HWC_ERROR_FOR("getReleaseFences", error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800569
570 displayData.releaseFences = std::move(releaseFences);
571
572 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700573}
574
Dominik Laskowski075d3172018-05-24 15:50:06 -0700575status_t HWComposer::setPowerMode(DisplayId displayId, int32_t intMode) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700576 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
577
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700578 const auto& displayData = mDisplayData[displayId];
579 if (displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700580 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
581 return INVALID_OPERATION;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800582 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700583
Dan Stoza9e56aa02015-11-02 13:00:03 -0800584 auto mode = static_cast<HWC2::PowerMode>(intMode);
585 if (mode == HWC2::PowerMode::Off) {
586 setVsyncEnabled(displayId, HWC2::Vsync::Disable);
587 }
588
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700589 auto& hwcDisplay = displayData.hwcDisplay;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800590 switch (mode) {
591 case HWC2::PowerMode::Off:
592 case HWC2::PowerMode::On:
593 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
594 {
595 auto error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700596 if (error != HWC2::Error::None) {
597 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
598 error, displayId);
599 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700600 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800601 break;
602 case HWC2::PowerMode::Doze:
603 case HWC2::PowerMode::DozeSuspend:
604 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
605 {
606 bool supportsDoze = false;
607 auto error = hwcDisplay->supportsDoze(&supportsDoze);
Peiyong Lin306e4992018-05-07 16:18:22 -0700608 if (error != HWC2::Error::None) {
609 LOG_HWC_ERROR("supportsDoze", error, displayId);
610 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700611
Dan Stoza9e56aa02015-11-02 13:00:03 -0800612 if (!supportsDoze) {
613 mode = HWC2::PowerMode::On;
614 }
615
616 error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700617 if (error != HWC2::Error::None) {
618 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
619 error, displayId);
620 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800621 }
622 break;
623 default:
624 ALOGV("setPowerMode: Not calling HWC");
625 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700626 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800627
628 return NO_ERROR;
629}
630
Dominik Laskowski075d3172018-05-24 15:50:06 -0700631status_t HWComposer::setActiveConfig(DisplayId displayId, size_t configId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700632 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800633
634 auto& displayData = mDisplayData[displayId];
635 if (displayData.configMap.count(configId) == 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700636 LOG_DISPLAY_ERROR(displayId, ("Invalid config " + std::to_string(configId)).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800637 return BAD_INDEX;
638 }
639
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700640 auto error = displayData.hwcDisplay->setActiveConfig(displayData.configMap[configId]);
641 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800642 return NO_ERROR;
643}
644
Dominik Laskowski075d3172018-05-24 15:50:06 -0700645status_t HWComposer::setColorTransform(DisplayId displayId, const mat4& transform) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700646 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700647
648 auto& displayData = mDisplayData[displayId];
649 bool isIdentity = transform == mat4();
650 auto error = displayData.hwcDisplay->setColorTransform(transform,
651 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY :
652 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700653 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700654 return NO_ERROR;
655}
656
Dominik Laskowski075d3172018-05-24 15:50:06 -0700657void HWComposer::disconnectDisplay(DisplayId displayId) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700658 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800659 auto& displayData = mDisplayData[displayId];
660
Dan Stoza9e56aa02015-11-02 13:00:03 -0800661 // If this was a virtual display, add its slot back for reuse by future
662 // virtual displays
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700663 if (displayData.isVirtual) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700664 mFreeVirtualDisplayIds.insert(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800665 ++mRemainingHwcVirtualDisplays;
666 }
667
Dominik Laskowski7e045462018-05-30 13:02:02 -0700668 const auto hwcDisplayId = displayData.hwcDisplay->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700669 mPhysicalDisplayIdMap.erase(hwcDisplayId);
670 mDisplayData.erase(displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700671
672 // TODO(b/74619554): Select internal/external display from remaining displays.
673 if (hwcDisplayId == mInternalHwcDisplayId) {
674 mInternalHwcDisplayId.reset();
675 } else if (hwcDisplayId == mExternalHwcDisplayId) {
676 mExternalHwcDisplayId.reset();
677 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700678
Dominik Laskowski7e045462018-05-30 13:02:02 -0700679 mHwcDevice->destroyDisplay(hwcDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800680}
681
Dominik Laskowski075d3172018-05-24 15:50:06 -0700682status_t HWComposer::setOutputBuffer(DisplayId displayId, const sp<Fence>& acquireFence,
683 const sp<GraphicBuffer>& buffer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700684 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700685 const auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800686
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700687 if (!displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700688 LOG_DISPLAY_ERROR(displayId, "Invalid operation on physical display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800689 return INVALID_OPERATION;
690 }
691
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700692 auto error = displayData.hwcDisplay->setOutputBuffer(buffer, acquireFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700693 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800694 return NO_ERROR;
695}
696
Dominik Laskowski075d3172018-05-24 15:50:06 -0700697void HWComposer::clearReleaseFences(DisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700698 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800699 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700700}
701
Dominik Laskowski075d3172018-05-24 15:50:06 -0700702status_t HWComposer::getHdrCapabilities(DisplayId displayId, HdrCapabilities* outCapabilities) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700703 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stozac4f471e2016-03-24 09:31:08 -0700704
705 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Peiyong Lin62665892018-04-16 11:07:44 -0700706 auto error = hwcDisplay->getHdrCapabilities(outCapabilities);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700707 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Peiyong Lin62665892018-04-16 11:07:44 -0700708 return NO_ERROR;
Dan Stozac4f471e2016-03-24 09:31:08 -0700709}
710
Dominik Laskowski075d3172018-05-24 15:50:06 -0700711int32_t HWComposer::getSupportedPerFrameMetadata(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700712 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700713 return mDisplayData.at(displayId).hwcDisplay->getSupportedPerFrameMetadata();
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700714}
715
Dominik Laskowski075d3172018-05-24 15:50:06 -0700716std::vector<ui::RenderIntent> HWComposer::getRenderIntents(DisplayId displayId,
717 ui::ColorMode colorMode) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700718 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700719
720 std::vector<ui::RenderIntent> renderIntents;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700721 auto error = mDisplayData.at(displayId).hwcDisplay->getRenderIntents(colorMode, &renderIntents);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700722 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700723 return renderIntents;
724}
725
Dominik Laskowski075d3172018-05-24 15:50:06 -0700726mat4 HWComposer::getDataspaceSaturationMatrix(DisplayId displayId, ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700727 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700728
729 mat4 matrix;
730 auto error = mDisplayData[displayId].hwcDisplay->getDataspaceSaturationMatrix(dataspace,
731 &matrix);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700732 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700733 return matrix;
734}
735
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800736bool HWComposer::isUsingVrComposer() const {
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800737 return getComposer()->isUsingVrComposer();
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800738}
739
Mathias Agopian74d211a2013-04-22 16:55:35 +0200740void HWComposer::dump(String8& result) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800741 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow
742 // all the state going into the layers. This is probably better done in
743 // Layer itself, but it's going to take a bit of work to get there.
744 result.append(mHwcDevice->dump().c_str());
Mathias Agopian83727852010-09-23 18:13:21 -0700745}
746
Dominik Laskowski075d3172018-05-24 15:50:06 -0700747std::optional<DisplayId> HWComposer::toPhysicalDisplayId(hwc2_display_t hwcDisplayId) const {
748 if (const auto it = mPhysicalDisplayIdMap.find(hwcDisplayId);
749 it != mPhysicalDisplayIdMap.end()) {
750 return it->second;
751 }
752 return {};
753}
754
755std::optional<hwc2_display_t> HWComposer::fromPhysicalDisplayId(DisplayId displayId) const {
756 if (const auto it = mDisplayData.find(displayId);
757 it != mDisplayData.end() && !it->second.isVirtual) {
758 return it->second.hwcDisplay->getId();
759 }
760 return {};
761}
762
763std::optional<DisplayIdentificationInfo> HWComposer::onHotplugConnect(hwc2_display_t hwcDisplayId) {
764 if (isUsingVrComposer() && mInternalHwcDisplayId) {
765 ALOGE("Ignoring connection of external display %" PRIu64 " in VR mode", hwcDisplayId);
Steven Thomas6e8f7062017-11-22 14:15:29 -0800766 return {};
767 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700768
769 uint8_t port;
770 DisplayIdentificationData data;
771 const bool hasMultiDisplaySupport = getDisplayIdentificationData(hwcDisplayId, &port, &data);
772
773 if (mPhysicalDisplayIdMap.empty()) {
774 mHasMultiDisplaySupport = hasMultiDisplaySupport;
775 ALOGI("Switching to %s multi-display mode",
776 hasMultiDisplaySupport ? "generalized" : "legacy");
777 } else if (mHasMultiDisplaySupport && !hasMultiDisplaySupport) {
778 ALOGE("Ignoring connection of display %" PRIu64 " without identification data",
779 hwcDisplayId);
780 return {};
781 }
782
783 std::optional<DisplayIdentificationInfo> info;
784
785 if (mHasMultiDisplaySupport) {
786 info = parseDisplayIdentificationData(port, data);
787 ALOGE_IF(!info, "Failed to parse identification data for display %" PRIu64, hwcDisplayId);
788 } else if (mInternalHwcDisplayId && mExternalHwcDisplayId) {
789 ALOGE("Ignoring connection of tertiary display %" PRIu64, hwcDisplayId);
790 return {};
791 } else {
792 ALOGW_IF(hasMultiDisplaySupport, "Ignoring identification data for display %" PRIu64,
793 hwcDisplayId);
794 port = mInternalHwcDisplayId ? HWC_DISPLAY_EXTERNAL : HWC_DISPLAY_PRIMARY;
795 }
796
797 if (!mInternalHwcDisplayId) {
798 mInternalHwcDisplayId = hwcDisplayId;
799 } else if (!mExternalHwcDisplayId) {
800 mExternalHwcDisplayId = hwcDisplayId;
801 }
802
803 if (info) return info;
804
805 return DisplayIdentificationInfo{getFallbackDisplayId(port),
806 hwcDisplayId == mInternalHwcDisplayId ? "Internal display"
807 : "External display"};
Steven Thomas6e8f7062017-11-22 14:15:29 -0800808}
809
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700810} // namespace android