blob: dc6faad41457688b2f447b6c89d24b80e97dc8d5 [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) {
101 return false;
102 }
103 RETURN_IF_INVALID_DISPLAY(*displayId, false);
104 return mDisplayData.at(*displayId).hwcDisplay->getCapabilities().count(capability) > 0;
105}
106
Dan Stoza9e56aa02015-11-02 13:00:03 -0800107void HWComposer::validateChange(HWC2::Composition from, HWC2::Composition to) {
108 bool valid = true;
109 switch (from) {
110 case HWC2::Composition::Client:
111 valid = false;
112 break;
113 case HWC2::Composition::Device:
114 case HWC2::Composition::SolidColor:
115 valid = (to == HWC2::Composition::Client);
116 break;
117 case HWC2::Composition::Cursor:
118 case HWC2::Composition::Sideband:
119 valid = (to == HWC2::Composition::Client ||
120 to == HWC2::Composition::Device);
121 break;
122 default:
123 break;
124 }
125
126 if (!valid) {
127 ALOGE("Invalid layer type change: %s --> %s", to_string(from).c_str(),
128 to_string(to).c_str());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700129 }
130}
131
Dominik Laskowski075d3172018-05-24 15:50:06 -0700132std::optional<DisplayIdentificationInfo> HWComposer::onHotplug(hwc2_display_t hwcDisplayId,
133 HWC2::Connection connection) {
134 std::optional<DisplayIdentificationInfo> info;
Lloyd Pique715a2c12017-12-14 17:18:08 -0800135
Dominik Laskowski075d3172018-05-24 15:50:06 -0700136 if (const auto displayId = toPhysicalDisplayId(hwcDisplayId)) {
137 info = DisplayIdentificationInfo{*displayId, std::string()};
138 } else {
139 if (connection == HWC2::Connection::Disconnected) {
140 ALOGE("Ignoring disconnection of invalid HWC display %" PRIu64, hwcDisplayId);
141 return {};
Lloyd Pique438e9e72018-09-04 18:06:08 -0700142 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700143
144 info = onHotplugConnect(hwcDisplayId);
145 if (!info) return {};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700146 }
147
Dominik Laskowski34157762018-10-31 13:07:19 -0700148 ALOGV("%s: %s %s display %s with HWC ID %" PRIu64, __FUNCTION__, to_string(connection).c_str(),
149 hwcDisplayId == mInternalHwcDisplayId ? "internal" : "external",
150 to_string(info->id).c_str(), hwcDisplayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700151
152 mHwcDevice->onHotplug(hwcDisplayId, connection);
153
Lloyd Pique715a2c12017-12-14 17:18:08 -0800154 // Disconnect is handled through HWComposer::disconnectDisplay via
155 // SurfaceFlinger's onHotplugReceived callback handling
156 if (connection == HWC2::Connection::Connected) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700157 mDisplayData[info->id].hwcDisplay = mHwcDevice->getDisplayById(hwcDisplayId);
158 mPhysicalDisplayIdMap[hwcDisplayId] = info->id;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700159 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700160
Dominik Laskowski075d3172018-05-24 15:50:06 -0700161 return info;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700162}
163
Dominik Laskowski075d3172018-05-24 15:50:06 -0700164bool HWComposer::onVsync(hwc2_display_t hwcDisplayId, int64_t timestamp) {
165 const auto displayId = toPhysicalDisplayId(hwcDisplayId);
166 if (!displayId) {
167 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, "Invalid HWC display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700168 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700169 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700170
Dominik Laskowski075d3172018-05-24 15:50:06 -0700171 RETURN_IF_INVALID_DISPLAY(*displayId, false);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700172
Dominik Laskowski1af47932018-11-12 10:20:46 -0800173 auto& displayData = mDisplayData[*displayId];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700174 if (displayData.isVirtual) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700175 LOG_DISPLAY_ERROR(*displayId, "Invalid operation on virtual display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700176 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700177 }
178
Dan Stoza9e56aa02015-11-02 13:00:03 -0800179 {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800180 std::lock_guard lock(displayData.lastHwVsyncLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700181
Dan Stoza9e56aa02015-11-02 13:00:03 -0800182 // There have been reports of HWCs that signal several vsync events
183 // with the same timestamp when turning the display off and on. This
184 // is a bug in the HWC implementation, but filter the extra events
185 // out here so they don't cause havoc downstream.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800186 if (timestamp == displayData.lastHwVsync) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700187 ALOGW("Ignoring duplicate VSYNC event from HWC for display %s (t=%" PRId64 ")",
188 to_string(*displayId).c_str(), timestamp);
Steven Thomas94e35b92017-07-26 18:48:28 -0700189 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800190 }
191
Dominik Laskowski1af47932018-11-12 10:20:46 -0800192 displayData.lastHwVsync = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700193 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800194
Dominik Laskowski34157762018-10-31 13:07:19 -0700195 const auto tag = "HW_VSYNC_" + to_string(*displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800196 ATRACE_INT(tag.c_str(), displayData.vsyncTraceToggle);
197 displayData.vsyncTraceToggle = !displayData.vsyncTraceToggle;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800198
Steven Thomas94e35b92017-07-26 18:48:28 -0700199 return true;
Jesse Hall1c569c42013-04-05 13:44:52 -0700200}
201
Dominik Laskowski075d3172018-05-24 15:50:06 -0700202std::optional<DisplayId> HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
203 ui::PixelFormat* format) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800204 if (mRemainingHwcVirtualDisplays == 0) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700205 ALOGE("%s: No remaining virtual displays", __FUNCTION__);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700206 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700207 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700208
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800209 if (SurfaceFlinger::maxVirtualDisplaySize != 0 &&
210 (width > SurfaceFlinger::maxVirtualDisplaySize ||
211 height > SurfaceFlinger::maxVirtualDisplaySize)) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700212 ALOGE("%s: Display size %ux%u exceeds maximum dimension of %" PRIu64, __FUNCTION__, width,
213 height, SurfaceFlinger::maxVirtualDisplaySize);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700214 return {};
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800215 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700216 HWC2::Display* display;
Dan Stoza5cf424b2016-05-20 14:02:39 -0700217 auto error = mHwcDevice->createVirtualDisplay(width, height, format,
218 &display);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800219 if (error != HWC2::Error::None) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700220 ALOGE("%s: Failed to create HWC virtual display", __FUNCTION__);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700221 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700222 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800223
Dominik Laskowski075d3172018-05-24 15:50:06 -0700224 DisplayId displayId;
225 if (mFreeVirtualDisplayIds.empty()) {
226 displayId = getVirtualDisplayId(mNextVirtualDisplayId++);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800227 } else {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700228 displayId = *mFreeVirtualDisplayIds.begin();
229 mFreeVirtualDisplayIds.erase(displayId);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700230 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800231
Dominik Laskowski075d3172018-05-24 15:50:06 -0700232 auto& displayData = mDisplayData[displayId];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700233 displayData.hwcDisplay = display;
234 displayData.isVirtual = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800235
236 --mRemainingHwcVirtualDisplays;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700237 return displayId;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700238}
239
Dominik Laskowski075d3172018-05-24 15:50:06 -0700240HWC2::Layer* HWComposer::createLayer(DisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700241 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
242
Dan Stoza9e56aa02015-11-02 13:00:03 -0800243 auto display = mDisplayData[displayId].hwcDisplay;
Steven Thomas94e35b92017-07-26 18:48:28 -0700244 HWC2::Layer* layer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800245 auto error = display->createLayer(&layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700246 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800247 return layer;
248}
249
Dominik Laskowski075d3172018-05-24 15:50:06 -0700250void HWComposer::destroyLayer(DisplayId displayId, HWC2::Layer* layer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700251 RETURN_IF_INVALID_DISPLAY(displayId);
252
Steven Thomas94e35b92017-07-26 18:48:28 -0700253 auto display = mDisplayData[displayId].hwcDisplay;
254 auto error = display->destroyLayer(layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700255 RETURN_IF_HWC_ERROR(error, displayId);
Steven Thomas94e35b92017-07-26 18:48:28 -0700256}
257
Dominik Laskowski075d3172018-05-24 15:50:06 -0700258nsecs_t HWComposer::getRefreshTimestamp(DisplayId displayId) const {
259 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800260 const auto& displayData = mDisplayData.at(displayId);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700261 // this returns the last refresh timestamp.
262 // if the last one is not available, we estimate it based on
263 // the refresh period and whatever closest timestamp we have.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800264 std::lock_guard lock(displayData.lastHwVsyncLock);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700265 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800266 auto vsyncPeriod = getActiveConfig(displayId)->getVsyncPeriod();
Dominik Laskowski1af47932018-11-12 10:20:46 -0800267 return now - ((now - displayData.lastHwVsync) % vsyncPeriod);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700268}
269
Dominik Laskowski075d3172018-05-24 15:50:06 -0700270bool HWComposer::isConnected(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700271 RETURN_IF_INVALID_DISPLAY(displayId, false);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700272 return mDisplayData.at(displayId).hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700273}
274
Dominik Laskowski075d3172018-05-24 15:50:06 -0700275std::vector<std::shared_ptr<const HWC2::Display::Config>> HWComposer::getConfigs(
276 DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700277 RETURN_IF_INVALID_DISPLAY(displayId, {});
278
Dominik Laskowski075d3172018-05-24 15:50:06 -0700279 const auto& displayData = mDisplayData.at(displayId);
280 auto configs = displayData.hwcDisplay->getConfigs();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800281 if (displayData.configMap.empty()) {
282 for (size_t i = 0; i < configs.size(); ++i) {
283 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700284 }
285 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800286 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700287}
288
Dominik Laskowski075d3172018-05-24 15:50:06 -0700289std::shared_ptr<const HWC2::Display::Config> HWComposer::getActiveConfig(
290 DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700291 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
292
Dan Stoza9e56aa02015-11-02 13:00:03 -0800293 std::shared_ptr<const HWC2::Display::Config> config;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700294 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfig(&config);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800295 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700296 LOG_DISPLAY_ERROR(displayId, "No active config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800297 return nullptr;
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700298 }
299
300 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
301
302 if (!config) {
303 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800304 return nullptr;
305 }
306
307 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700308}
309
Dominik Laskowski075d3172018-05-24 15:50:06 -0700310int HWComposer::getActiveConfigIndex(DisplayId displayId) const {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700311 RETURN_IF_INVALID_DISPLAY(displayId, -1);
312
Lloyd Pique3c085a02018-05-09 19:38:32 -0700313 int index;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700314 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfigIndex(&index);
Lloyd Pique3c085a02018-05-09 19:38:32 -0700315 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700316 LOG_DISPLAY_ERROR(displayId, "No active config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700317 return -1;
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700318 }
319
320 RETURN_IF_HWC_ERROR(error, displayId, -1);
321
322 if (index < 0) {
323 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700324 return -1;
325 }
326
327 return index;
328}
329
Dominik Laskowski075d3172018-05-24 15:50:06 -0700330std::vector<ui::ColorMode> HWComposer::getColorModes(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700331 RETURN_IF_INVALID_DISPLAY(displayId, {});
332
Peiyong Linfd997e02018-03-28 15:29:00 -0700333 std::vector<ui::ColorMode> modes;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700334 auto error = mDisplayData.at(displayId).hwcDisplay->getColorModes(&modes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700335 RETURN_IF_HWC_ERROR(error, displayId, {});
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600336 return modes;
337}
338
Dominik Laskowski075d3172018-05-24 15:50:06 -0700339status_t HWComposer::setActiveColorMode(DisplayId displayId, ui::ColorMode mode,
340 ui::RenderIntent renderIntent) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700341 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Michael Wright28f24d02016-07-12 13:30:53 -0700342
343 auto& displayData = mDisplayData[displayId];
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700344 auto error = displayData.hwcDisplay->setColorMode(mode, renderIntent);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700345 RETURN_IF_HWC_ERROR_FOR(("setColorMode(" + decodeColorMode(mode) + ", " +
346 decodeRenderIntent(renderIntent) + ")")
347 .c_str(),
348 error, displayId, UNKNOWN_ERROR);
Michael Wright28f24d02016-07-12 13:30:53 -0700349
350 return NO_ERROR;
351}
352
Dominik Laskowski075d3172018-05-24 15:50:06 -0700353void HWComposer::setVsyncEnabled(DisplayId displayId, HWC2::Vsync enabled) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700354 RETURN_IF_INVALID_DISPLAY(displayId);
355 auto& displayData = mDisplayData[displayId];
356
357 if (displayData.isVirtual) {
358 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800359 return;
360 }
361
Dan Stoza9e56aa02015-11-02 13:00:03 -0800362 // NOTE: we use our own internal lock here because we have to call
363 // into the HWC with the lock held, and we want to make sure
364 // that even if HWC blocks (which it shouldn't), it won't
365 // affect other threads.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800366 std::lock_guard lock(displayData.vsyncEnabledLock);
367 if (enabled == displayData.vsyncEnabled) {
368 return;
Colin Cross10fbdb62012-07-12 17:56:34 -0700369 }
Dominik Laskowski1af47932018-11-12 10:20:46 -0800370
371 ATRACE_CALL();
372 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
373 RETURN_IF_HWC_ERROR(error, displayId);
374
375 displayData.vsyncEnabled = enabled;
376
377 const auto tag = "HW_VSYNC_ON_" + to_string(displayId);
378 ATRACE_INT(tag.c_str(), enabled == HWC2::Vsync::Enable ? 1 : 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700379}
380
Dominik Laskowski075d3172018-05-24 15:50:06 -0700381status_t HWComposer::setClientTarget(DisplayId displayId, uint32_t slot,
382 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
383 ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700384 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Jesse Hall851cfe82013-03-20 13:44:00 -0700385
Dominik Laskowski34157762018-10-31 13:07:19 -0700386 ALOGV("%s for display %s", __FUNCTION__, to_string(displayId).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800387 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400388 auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700389 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Jesse Hall851cfe82013-03-20 13:44:00 -0700390 return NO_ERROR;
391}
392
Dominik Laskowski075d3172018-05-24 15:50:06 -0700393status_t HWComposer::prepare(DisplayId displayId, std::vector<CompositionInfo>& compositionData) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800394 ATRACE_CALL();
395
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700396 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800397
398 auto& displayData = mDisplayData[displayId];
399 auto& hwcDisplay = displayData.hwcDisplay;
400 if (!hwcDisplay->isConnected()) {
401 return NO_ERROR;
402 }
403
404 uint32_t numTypes = 0;
405 uint32_t numRequests = 0;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700406
407 HWC2::Error error = HWC2::Error::None;
408
Chia-I Wu41b98d42017-12-11 11:04:36 -0800409 // First try to skip validate altogether when there is no client
410 // composition. When there is client composition, since we haven't
411 // rendered to the client target yet, we should not attempt to skip
412 // validate.
413 //
414 // displayData.hasClientComposition hasn't been updated for this frame.
415 // The check below is incorrect. We actually rely on HWC here to fall
416 // back to validate when there is any client layer.
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700417 displayData.validateWasSkipped = false;
Chia-I Wu41b98d42017-12-11 11:04:36 -0800418 if (!displayData.hasClientComposition) {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800419 sp<Fence> outPresentFence;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700420 uint32_t state = UINT32_MAX;
421 error = hwcDisplay->presentOrValidate(&numTypes, &numRequests, &outPresentFence , &state);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700422 if (error != HWC2::Error::HasChanges) {
423 RETURN_IF_HWC_ERROR_FOR("presentOrValidate", error, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700424 }
425 if (state == 1) { //Present Succeeded.
Steven Thomas94e35b92017-07-26 18:48:28 -0700426 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700427 error = hwcDisplay->getReleaseFences(&releaseFences);
428 displayData.releaseFences = std::move(releaseFences);
429 displayData.lastPresentFence = outPresentFence;
430 displayData.validateWasSkipped = true;
431 displayData.presentError = error;
432 return NO_ERROR;
433 }
434 // Present failed but Validate ran.
435 } else {
436 error = hwcDisplay->validate(&numTypes, &numRequests);
437 }
438 ALOGV("SkipValidate failed, Falling back to SLOW validate/present");
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700439 if (error != HWC2::Error::HasChanges) {
440 RETURN_IF_HWC_ERROR_FOR("validate", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800441 }
442
Steven Thomas94e35b92017-07-26 18:48:28 -0700443 std::unordered_map<HWC2::Layer*, HWC2::Composition> changedTypes;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800444 changedTypes.reserve(numTypes);
445 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700446 RETURN_IF_HWC_ERROR_FOR("getChangedCompositionTypes", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800447
448 displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0);
Steven Thomas94e35b92017-07-26 18:48:28 -0700449 std::unordered_map<HWC2::Layer*, HWC2::LayerRequest> layerRequests;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800450 layerRequests.reserve(numRequests);
451 error = hwcDisplay->getRequests(&displayData.displayRequests,
452 &layerRequests);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700453 RETURN_IF_HWC_ERROR_FOR("getRequests", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800454
455 displayData.hasClientComposition = false;
456 displayData.hasDeviceComposition = false;
David Sodmanfb95bcc2017-12-22 15:45:30 -0800457 for (auto& compositionInfo : compositionData) {
458 auto hwcLayer = compositionInfo.hwc.hwcLayer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800459
David Sodmanfb95bcc2017-12-22 15:45:30 -0800460 if (changedTypes.count(&*hwcLayer) != 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800461 // We pass false so we only update our state and don't call back
462 // into the HWC device
David Sodmanfb95bcc2017-12-22 15:45:30 -0800463 validateChange(compositionInfo.compositionType,
464 changedTypes[&*hwcLayer]);
465 compositionInfo.compositionType = changedTypes[&*hwcLayer];
David Sodmanc1498e62018-09-12 14:36:26 -0700466 compositionInfo.layer->mLayer->setCompositionType(displayId,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700467 compositionInfo.compositionType,
468 false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800469 }
470
David Sodmanfb95bcc2017-12-22 15:45:30 -0800471 switch (compositionInfo.compositionType) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800472 case HWC2::Composition::Client:
473 displayData.hasClientComposition = true;
474 break;
475 case HWC2::Composition::Device:
476 case HWC2::Composition::SolidColor:
477 case HWC2::Composition::Cursor:
478 case HWC2::Composition::Sideband:
479 displayData.hasDeviceComposition = true;
480 break;
481 default:
482 break;
483 }
484
David Sodmanfb95bcc2017-12-22 15:45:30 -0800485 if (layerRequests.count(&*hwcLayer) != 0 &&
486 layerRequests[&*hwcLayer] ==
Dan Stoza9e56aa02015-11-02 13:00:03 -0800487 HWC2::LayerRequest::ClearClientTarget) {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800488 compositionInfo.hwc.clearClientTarget = true;
David Sodmanc1498e62018-09-12 14:36:26 -0700489 compositionInfo.layer->mLayer->setClearClientTarget(displayId, true);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800490 } else {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800491 if (layerRequests.count(&*hwcLayer) != 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700492 LOG_DISPLAY_ERROR(displayId,
David Sodmanfb95bcc2017-12-22 15:45:30 -0800493 ("Unknown layer request " + to_string(layerRequests[&*hwcLayer]))
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700494 .c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800495 }
David Sodmanfb95bcc2017-12-22 15:45:30 -0800496 compositionInfo.hwc.clearClientTarget = false;
David Sodmanc1498e62018-09-12 14:36:26 -0700497 compositionInfo.layer->mLayer->setClearClientTarget(displayId, false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800498 }
499 }
500
501 error = hwcDisplay->acceptChanges();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700502 RETURN_IF_HWC_ERROR_FOR("acceptChanges", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800503
504 return NO_ERROR;
505}
506
Dominik Laskowski075d3172018-05-24 15:50:06 -0700507bool HWComposer::hasDeviceComposition(const std::optional<DisplayId>& displayId) const {
508 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700509 // Displays without a corresponding HWC display are never composed by
510 // the device
511 return false;
512 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700513
Dominik Laskowski075d3172018-05-24 15:50:06 -0700514 RETURN_IF_INVALID_DISPLAY(*displayId, false);
515 return mDisplayData.at(*displayId).hasDeviceComposition;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800516}
517
Dominik Laskowski075d3172018-05-24 15:50:06 -0700518bool HWComposer::hasFlipClientTargetRequest(const std::optional<DisplayId>& displayId) const {
519 if (!displayId) {
Madhuri Athota88a905b2017-05-04 16:58:15 +0530520 // Displays without a corresponding HWC display are never composed by
521 // the device
522 return false;
523 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700524
Dominik Laskowski075d3172018-05-24 15:50:06 -0700525 RETURN_IF_INVALID_DISPLAY(*displayId, false);
526 return ((static_cast<uint32_t>(mDisplayData.at(*displayId).displayRequests) &
Madhuri Athota88a905b2017-05-04 16:58:15 +0530527 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0);
528}
529
Dominik Laskowski075d3172018-05-24 15:50:06 -0700530bool HWComposer::hasClientComposition(const std::optional<DisplayId>& displayId) const {
531 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700532 // Displays without a corresponding HWC display are always composed by
533 // the client
534 return true;
535 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700536
Dominik Laskowski075d3172018-05-24 15:50:06 -0700537 RETURN_IF_INVALID_DISPLAY(*displayId, true);
538 return mDisplayData.at(*displayId).hasClientComposition;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800539}
540
Dominik Laskowski075d3172018-05-24 15:50:06 -0700541sp<Fence> HWComposer::getPresentFence(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700542 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700543 return mDisplayData.at(displayId).lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700544}
545
Dominik Laskowski075d3172018-05-24 15:50:06 -0700546sp<Fence> HWComposer::getLayerReleaseFence(DisplayId displayId, HWC2::Layer* layer) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700547 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700548 auto displayFences = mDisplayData.at(displayId).releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800549 if (displayFences.count(layer) == 0) {
550 ALOGV("getLayerReleaseFence: Release fence not found");
551 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700552 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800553 return displayFences[layer];
Riley Andrews03414a12014-07-01 14:22:59 -0700554}
555
Dominik Laskowski075d3172018-05-24 15:50:06 -0700556status_t HWComposer::presentAndGetReleaseFences(DisplayId displayId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800557 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700558
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700559 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700560
Dan Stoza9e56aa02015-11-02 13:00:03 -0800561 auto& displayData = mDisplayData[displayId];
562 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700563
564 if (displayData.validateWasSkipped) {
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700565 // explicitly flush all pending commands
566 auto error = mHwcDevice->flushCommands();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700567 RETURN_IF_HWC_ERROR_FOR("flushCommands", error, displayId, UNKNOWN_ERROR);
568 RETURN_IF_HWC_ERROR_FOR("present", displayData.presentError, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700569 return NO_ERROR;
570 }
571
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800572 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700573 RETURN_IF_HWC_ERROR_FOR("present", error, displayId, UNKNOWN_ERROR);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700574
Steven Thomas94e35b92017-07-26 18:48:28 -0700575 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800576 error = hwcDisplay->getReleaseFences(&releaseFences);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700577 RETURN_IF_HWC_ERROR_FOR("getReleaseFences", error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800578
579 displayData.releaseFences = std::move(releaseFences);
580
581 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700582}
583
Dominik Laskowski075d3172018-05-24 15:50:06 -0700584status_t HWComposer::setPowerMode(DisplayId displayId, int32_t intMode) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700585 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
586
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700587 const auto& displayData = mDisplayData[displayId];
588 if (displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700589 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
590 return INVALID_OPERATION;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800591 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700592
Dan Stoza9e56aa02015-11-02 13:00:03 -0800593 auto mode = static_cast<HWC2::PowerMode>(intMode);
594 if (mode == HWC2::PowerMode::Off) {
595 setVsyncEnabled(displayId, HWC2::Vsync::Disable);
596 }
597
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700598 auto& hwcDisplay = displayData.hwcDisplay;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800599 switch (mode) {
600 case HWC2::PowerMode::Off:
601 case HWC2::PowerMode::On:
602 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
603 {
604 auto error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700605 if (error != HWC2::Error::None) {
606 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
607 error, displayId);
608 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700609 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800610 break;
611 case HWC2::PowerMode::Doze:
612 case HWC2::PowerMode::DozeSuspend:
613 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
614 {
615 bool supportsDoze = false;
616 auto error = hwcDisplay->supportsDoze(&supportsDoze);
Peiyong Lin306e4992018-05-07 16:18:22 -0700617 if (error != HWC2::Error::None) {
618 LOG_HWC_ERROR("supportsDoze", error, displayId);
619 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700620
Dan Stoza9e56aa02015-11-02 13:00:03 -0800621 if (!supportsDoze) {
622 mode = HWC2::PowerMode::On;
623 }
624
625 error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700626 if (error != HWC2::Error::None) {
627 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
628 error, displayId);
629 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800630 }
631 break;
632 default:
633 ALOGV("setPowerMode: Not calling HWC");
634 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700635 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800636
637 return NO_ERROR;
638}
639
Dominik Laskowski075d3172018-05-24 15:50:06 -0700640status_t HWComposer::setActiveConfig(DisplayId displayId, size_t configId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700641 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800642
643 auto& displayData = mDisplayData[displayId];
644 if (displayData.configMap.count(configId) == 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700645 LOG_DISPLAY_ERROR(displayId, ("Invalid config " + std::to_string(configId)).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800646 return BAD_INDEX;
647 }
648
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700649 auto error = displayData.hwcDisplay->setActiveConfig(displayData.configMap[configId]);
650 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800651 return NO_ERROR;
652}
653
Dominik Laskowski075d3172018-05-24 15:50:06 -0700654status_t HWComposer::setColorTransform(DisplayId displayId, const mat4& transform) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700655 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700656
657 auto& displayData = mDisplayData[displayId];
658 bool isIdentity = transform == mat4();
659 auto error = displayData.hwcDisplay->setColorTransform(transform,
660 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY :
661 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700662 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700663 return NO_ERROR;
664}
665
Dominik Laskowski075d3172018-05-24 15:50:06 -0700666void HWComposer::disconnectDisplay(DisplayId displayId) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700667 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800668 auto& displayData = mDisplayData[displayId];
669
Dan Stoza9e56aa02015-11-02 13:00:03 -0800670 // If this was a virtual display, add its slot back for reuse by future
671 // virtual displays
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700672 if (displayData.isVirtual) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700673 mFreeVirtualDisplayIds.insert(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800674 ++mRemainingHwcVirtualDisplays;
675 }
676
Dominik Laskowski7e045462018-05-30 13:02:02 -0700677 const auto hwcDisplayId = displayData.hwcDisplay->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700678 mPhysicalDisplayIdMap.erase(hwcDisplayId);
679 mDisplayData.erase(displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700680
681 // TODO(b/74619554): Select internal/external display from remaining displays.
682 if (hwcDisplayId == mInternalHwcDisplayId) {
683 mInternalHwcDisplayId.reset();
684 } else if (hwcDisplayId == mExternalHwcDisplayId) {
685 mExternalHwcDisplayId.reset();
686 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700687
Dominik Laskowski7e045462018-05-30 13:02:02 -0700688 mHwcDevice->destroyDisplay(hwcDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800689}
690
Dominik Laskowski075d3172018-05-24 15:50:06 -0700691status_t HWComposer::setOutputBuffer(DisplayId displayId, const sp<Fence>& acquireFence,
692 const sp<GraphicBuffer>& buffer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700693 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700694 const auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800695
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700696 if (!displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700697 LOG_DISPLAY_ERROR(displayId, "Invalid operation on physical display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800698 return INVALID_OPERATION;
699 }
700
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700701 auto error = displayData.hwcDisplay->setOutputBuffer(buffer, acquireFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700702 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800703 return NO_ERROR;
704}
705
Dominik Laskowski075d3172018-05-24 15:50:06 -0700706void HWComposer::clearReleaseFences(DisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700707 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800708 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700709}
710
Dominik Laskowski075d3172018-05-24 15:50:06 -0700711status_t HWComposer::getHdrCapabilities(DisplayId displayId, HdrCapabilities* outCapabilities) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700712 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stozac4f471e2016-03-24 09:31:08 -0700713
714 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Peiyong Lin62665892018-04-16 11:07:44 -0700715 auto error = hwcDisplay->getHdrCapabilities(outCapabilities);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700716 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Peiyong Lin62665892018-04-16 11:07:44 -0700717 return NO_ERROR;
Dan Stozac4f471e2016-03-24 09:31:08 -0700718}
719
Dominik Laskowski075d3172018-05-24 15:50:06 -0700720int32_t HWComposer::getSupportedPerFrameMetadata(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700721 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700722 return mDisplayData.at(displayId).hwcDisplay->getSupportedPerFrameMetadata();
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700723}
724
Dominik Laskowski075d3172018-05-24 15:50:06 -0700725std::vector<ui::RenderIntent> HWComposer::getRenderIntents(DisplayId displayId,
726 ui::ColorMode colorMode) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700727 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700728
729 std::vector<ui::RenderIntent> renderIntents;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700730 auto error = mDisplayData.at(displayId).hwcDisplay->getRenderIntents(colorMode, &renderIntents);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700731 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700732 return renderIntents;
733}
734
Dominik Laskowski075d3172018-05-24 15:50:06 -0700735mat4 HWComposer::getDataspaceSaturationMatrix(DisplayId displayId, ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700736 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700737
738 mat4 matrix;
739 auto error = mDisplayData[displayId].hwcDisplay->getDataspaceSaturationMatrix(dataspace,
740 &matrix);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700741 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700742 return matrix;
743}
744
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700745status_t HWComposer::getDisplayedContentSamplingAttributes(DisplayId displayId,
746 ui::PixelFormat* outFormat,
747 ui::Dataspace* outDataspace,
748 uint8_t* outComponentMask) {
749 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
750 const auto error =
751 mDisplayData[displayId]
752 .hwcDisplay->getDisplayedContentSamplingAttributes(outFormat, outDataspace,
753 outComponentMask);
754 if (error == HWC2::Error::Unsupported) RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
755 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
756 return NO_ERROR;
757}
758
Kevin DuBois74e53772018-11-19 10:52:38 -0800759status_t HWComposer::setDisplayContentSamplingEnabled(DisplayId displayId, bool enabled,
760 uint8_t componentMask, uint64_t maxFrames) {
761 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
762 const auto error =
763 mDisplayData[displayId].hwcDisplay->setDisplayContentSamplingEnabled(enabled,
764 componentMask,
765 maxFrames);
766
767 if (error == HWC2::Error::Unsupported) RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
768 if (error == HWC2::Error::BadParameter) RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
769 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
770 return NO_ERROR;
771}
772
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700773status_t HWComposer::getDisplayedContentSample(DisplayId displayId, uint64_t maxFrames,
774 uint64_t timestamp, DisplayedFrameStats* outStats) {
775 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
776 const auto error =
777 mDisplayData[displayId].hwcDisplay->getDisplayedContentSample(maxFrames, timestamp,
778 outStats);
779 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
780 return NO_ERROR;
781}
782
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800783bool HWComposer::isUsingVrComposer() const {
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800784 return getComposer()->isUsingVrComposer();
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800785}
786
Mathias Agopian74d211a2013-04-22 16:55:35 +0200787void HWComposer::dump(String8& result) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800788 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow
789 // all the state going into the layers. This is probably better done in
790 // Layer itself, but it's going to take a bit of work to get there.
791 result.append(mHwcDevice->dump().c_str());
Mathias Agopian83727852010-09-23 18:13:21 -0700792}
793
Dominik Laskowski075d3172018-05-24 15:50:06 -0700794std::optional<DisplayId> HWComposer::toPhysicalDisplayId(hwc2_display_t hwcDisplayId) const {
795 if (const auto it = mPhysicalDisplayIdMap.find(hwcDisplayId);
796 it != mPhysicalDisplayIdMap.end()) {
797 return it->second;
798 }
799 return {};
800}
801
802std::optional<hwc2_display_t> HWComposer::fromPhysicalDisplayId(DisplayId displayId) const {
803 if (const auto it = mDisplayData.find(displayId);
804 it != mDisplayData.end() && !it->second.isVirtual) {
805 return it->second.hwcDisplay->getId();
806 }
807 return {};
808}
809
810std::optional<DisplayIdentificationInfo> HWComposer::onHotplugConnect(hwc2_display_t hwcDisplayId) {
811 if (isUsingVrComposer() && mInternalHwcDisplayId) {
812 ALOGE("Ignoring connection of external display %" PRIu64 " in VR mode", hwcDisplayId);
Steven Thomas6e8f7062017-11-22 14:15:29 -0800813 return {};
814 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700815
816 uint8_t port;
817 DisplayIdentificationData data;
818 const bool hasMultiDisplaySupport = getDisplayIdentificationData(hwcDisplayId, &port, &data);
819
820 if (mPhysicalDisplayIdMap.empty()) {
821 mHasMultiDisplaySupport = hasMultiDisplaySupport;
822 ALOGI("Switching to %s multi-display mode",
823 hasMultiDisplaySupport ? "generalized" : "legacy");
824 } else if (mHasMultiDisplaySupport && !hasMultiDisplaySupport) {
825 ALOGE("Ignoring connection of display %" PRIu64 " without identification data",
826 hwcDisplayId);
827 return {};
828 }
829
830 std::optional<DisplayIdentificationInfo> info;
831
832 if (mHasMultiDisplaySupport) {
833 info = parseDisplayIdentificationData(port, data);
834 ALOGE_IF(!info, "Failed to parse identification data for display %" PRIu64, hwcDisplayId);
835 } else if (mInternalHwcDisplayId && mExternalHwcDisplayId) {
836 ALOGE("Ignoring connection of tertiary display %" PRIu64, hwcDisplayId);
837 return {};
838 } else {
839 ALOGW_IF(hasMultiDisplaySupport, "Ignoring identification data for display %" PRIu64,
840 hwcDisplayId);
841 port = mInternalHwcDisplayId ? HWC_DISPLAY_EXTERNAL : HWC_DISPLAY_PRIMARY;
842 }
843
844 if (!mInternalHwcDisplayId) {
845 mInternalHwcDisplayId = hwcDisplayId;
846 } else if (!mExternalHwcDisplayId) {
847 mExternalHwcDisplayId = hwcDisplayId;
848 }
849
850 if (info) return info;
851
852 return DisplayIdentificationInfo{getFallbackDisplayId(port),
853 hwcDisplayId == mInternalHwcDisplayId ? "Internal display"
854 : "External display"};
Steven Thomas6e8f7062017-11-22 14:15:29 -0800855}
856
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700857} // namespace android