blob: 9accefb7e117b3b8b3f56077fc67c3643dfd8a82 [file] [log] [blame]
Mathias Agopiana350ff92010-08-10 17:14:02 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017// #define LOG_NDEBUG 0
18
19#undef LOG_TAG
20#define LOG_TAG "HWComposer"
Mathias Agopian2965b262012-04-08 15:13:32 -070021#define ATRACE_TAG ATRACE_TAG_GRAPHICS
22
Lloyd Pique66d68602019-02-13 14:23:31 -080023#include "HWComposer.h"
24
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080025#include <compositionengine/Output.h>
26#include <compositionengine/OutputLayer.h>
27#include <compositionengine/impl/OutputLayerCompositionState.h>
28#include <log/log.h>
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070029#include <ui/DebugUtils.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070030#include <ui/GraphicBuffer.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080031#include <utils/Errors.h>
32#include <utils/Trace.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070033
Lloyd Pique66d68602019-02-13 14:23:31 -080034#include "../Layer.h" // needed only for debugging
Mathias Agopian33ceeb32013-04-01 16:54:58 -070035#include "../SurfaceFlinger.h"
Lloyd Pique66d68602019-02-13 14:23:31 -080036#include "ComposerHal.h"
37#include "HWC2.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070038
Dominik Laskowskic1f18f62018-06-13 15:17:55 -070039#define LOG_HWC_DISPLAY_ERROR(hwcDisplayId, msg) \
40 ALOGE("%s failed for HWC display %" PRIu64 ": %s", __FUNCTION__, hwcDisplayId, msg)
41
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070042#define LOG_DISPLAY_ERROR(displayId, msg) \
Dominik Laskowski34157762018-10-31 13:07:19 -070043 ALOGE("%s failed for display %s: %s", __FUNCTION__, to_string(displayId).c_str(), msg)
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070044
Dominik Laskowski34157762018-10-31 13:07:19 -070045#define LOG_HWC_ERROR(what, error, displayId) \
46 ALOGE("%s: %s failed for display %s: %s (%d)", __FUNCTION__, what, \
47 to_string(displayId).c_str(), to_string(error).c_str(), static_cast<int32_t>(error))
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070048
49#define RETURN_IF_INVALID_DISPLAY(displayId, ...) \
50 do { \
Dominik Laskowski075d3172018-05-24 15:50:06 -070051 if (mDisplayData.count(displayId) == 0) { \
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070052 LOG_DISPLAY_ERROR(displayId, "Invalid display"); \
53 return __VA_ARGS__; \
54 } \
55 } while (false)
56
57#define RETURN_IF_HWC_ERROR_FOR(what, error, displayId, ...) \
58 do { \
59 if (error != HWC2::Error::None) { \
60 LOG_HWC_ERROR(what, error, displayId); \
61 return __VA_ARGS__; \
62 } \
63 } while (false)
64
65#define RETURN_IF_HWC_ERROR(error, displayId, ...) \
66 RETURN_IF_HWC_ERROR_FOR(__FUNCTION__, error, displayId, __VA_ARGS__)
67
Mathias Agopiana350ff92010-08-10 17:14:02 -070068namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -070069
Lloyd Pique441d5042018-10-18 16:49:51 -070070HWComposer::~HWComposer() = default;
71
72namespace impl {
73
Dominik Laskowski1af47932018-11-12 10:20:46 -080074HWComposer::HWComposer(std::unique_ptr<Hwc2::Composer> composer)
Lloyd Piquea822d522017-12-20 16:42:57 -080075 : mHwcDevice(std::make_unique<HWC2::Device>(std::move(composer))) {}
Mathias Agopianbef42c52013-08-21 17:45:46 -070076
Dominik Laskowskib04f98a2018-11-07 21:07:16 -080077HWComposer::~HWComposer() {
78 mDisplayData.clear();
79}
Dan Stoza9e56aa02015-11-02 13:00:03 -080080
Steven Thomas94e35b92017-07-26 18:48:28 -070081void HWComposer::registerCallback(HWC2::ComposerCallback* callback,
82 int32_t sequenceId) {
83 mHwcDevice->registerCallback(callback, sequenceId);
Dan Stoza9e56aa02015-11-02 13:00:03 -080084}
85
Dominik Laskowskia2edf612018-06-01 13:15:16 -070086bool HWComposer::getDisplayIdentificationData(hwc2_display_t hwcDisplayId, uint8_t* outPort,
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070087 DisplayIdentificationData* outData) const {
Dominik Laskowski0954b1d2018-06-13 14:58:49 -070088 const auto error = mHwcDevice->getDisplayIdentificationData(hwcDisplayId, outPort, outData);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070089 if (error != HWC2::Error::None) {
Chia-I Wud0aff9d2018-06-21 13:39:09 +080090 if (error != HWC2::Error::Unsupported) {
91 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, to_string(error).c_str());
92 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070093 return false;
94 }
95 return true;
96}
97
Dan Stoza9f26a9c2016-06-22 14:51:09 -070098bool HWComposer::hasCapability(HWC2::Capability capability) const
99{
100 return mHwcDevice->getCapabilities().count(capability) > 0;
101}
102
Peiyong Lined531a32018-10-26 18:27:56 -0700103bool HWComposer::hasDisplayCapability(const std::optional<DisplayId>& displayId,
104 HWC2::DisplayCapability capability) const {
105 if (!displayId) {
Peiyong Lindf65fd22019-01-03 15:17:05 -0800106 // Checkout global capabilities for displays without a corresponding HWC display.
107 if (capability == HWC2::DisplayCapability::SkipClientColorTransform) {
108 return hasCapability(HWC2::Capability::SkipClientColorTransform);
109 }
Peiyong Lined531a32018-10-26 18:27:56 -0700110 return false;
111 }
112 RETURN_IF_INVALID_DISPLAY(*displayId, false);
113 return mDisplayData.at(*displayId).hwcDisplay->getCapabilities().count(capability) > 0;
114}
115
Dominik Laskowski075d3172018-05-24 15:50:06 -0700116std::optional<DisplayIdentificationInfo> HWComposer::onHotplug(hwc2_display_t hwcDisplayId,
117 HWC2::Connection connection) {
118 std::optional<DisplayIdentificationInfo> info;
Lloyd Pique715a2c12017-12-14 17:18:08 -0800119
Dominik Laskowski075d3172018-05-24 15:50:06 -0700120 if (const auto displayId = toPhysicalDisplayId(hwcDisplayId)) {
121 info = DisplayIdentificationInfo{*displayId, std::string()};
122 } else {
123 if (connection == HWC2::Connection::Disconnected) {
124 ALOGE("Ignoring disconnection of invalid HWC display %" PRIu64, hwcDisplayId);
125 return {};
Lloyd Pique438e9e72018-09-04 18:06:08 -0700126 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700127
128 info = onHotplugConnect(hwcDisplayId);
129 if (!info) return {};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700130 }
131
Dominik Laskowski34157762018-10-31 13:07:19 -0700132 ALOGV("%s: %s %s display %s with HWC ID %" PRIu64, __FUNCTION__, to_string(connection).c_str(),
133 hwcDisplayId == mInternalHwcDisplayId ? "internal" : "external",
134 to_string(info->id).c_str(), hwcDisplayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700135
136 mHwcDevice->onHotplug(hwcDisplayId, connection);
137
Lloyd Pique715a2c12017-12-14 17:18:08 -0800138 // Disconnect is handled through HWComposer::disconnectDisplay via
139 // SurfaceFlinger's onHotplugReceived callback handling
140 if (connection == HWC2::Connection::Connected) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700141 mDisplayData[info->id].hwcDisplay = mHwcDevice->getDisplayById(hwcDisplayId);
142 mPhysicalDisplayIdMap[hwcDisplayId] = info->id;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700143 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700144
Dominik Laskowski075d3172018-05-24 15:50:06 -0700145 return info;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700146}
147
Dominik Laskowski075d3172018-05-24 15:50:06 -0700148bool HWComposer::onVsync(hwc2_display_t hwcDisplayId, int64_t timestamp) {
149 const auto displayId = toPhysicalDisplayId(hwcDisplayId);
150 if (!displayId) {
151 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, "Invalid HWC display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700152 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700153 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700154
Dominik Laskowski075d3172018-05-24 15:50:06 -0700155 RETURN_IF_INVALID_DISPLAY(*displayId, false);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700156
Dominik Laskowski1af47932018-11-12 10:20:46 -0800157 auto& displayData = mDisplayData[*displayId];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700158 if (displayData.isVirtual) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700159 LOG_DISPLAY_ERROR(*displayId, "Invalid operation on virtual display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700160 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700161 }
162
Dan Stoza9e56aa02015-11-02 13:00:03 -0800163 {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800164 std::lock_guard lock(displayData.lastHwVsyncLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700165
Dan Stoza9e56aa02015-11-02 13:00:03 -0800166 // There have been reports of HWCs that signal several vsync events
167 // with the same timestamp when turning the display off and on. This
168 // is a bug in the HWC implementation, but filter the extra events
169 // out here so they don't cause havoc downstream.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800170 if (timestamp == displayData.lastHwVsync) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700171 ALOGW("Ignoring duplicate VSYNC event from HWC for display %s (t=%" PRId64 ")",
172 to_string(*displayId).c_str(), timestamp);
Steven Thomas94e35b92017-07-26 18:48:28 -0700173 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800174 }
175
Dominik Laskowski1af47932018-11-12 10:20:46 -0800176 displayData.lastHwVsync = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700177 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800178
Dominik Laskowski34157762018-10-31 13:07:19 -0700179 const auto tag = "HW_VSYNC_" + to_string(*displayId);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800180 ATRACE_INT(tag.c_str(), displayData.vsyncTraceToggle);
181 displayData.vsyncTraceToggle = !displayData.vsyncTraceToggle;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800182
Steven Thomas94e35b92017-07-26 18:48:28 -0700183 return true;
Jesse Hall1c569c42013-04-05 13:44:52 -0700184}
185
Dominik Laskowski075d3172018-05-24 15:50:06 -0700186std::optional<DisplayId> HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
187 ui::PixelFormat* format) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800188 if (mRemainingHwcVirtualDisplays == 0) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700189 ALOGE("%s: No remaining virtual displays", __FUNCTION__);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700190 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700191 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700192
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800193 if (SurfaceFlinger::maxVirtualDisplaySize != 0 &&
194 (width > SurfaceFlinger::maxVirtualDisplaySize ||
195 height > SurfaceFlinger::maxVirtualDisplaySize)) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700196 ALOGE("%s: Display size %ux%u exceeds maximum dimension of %" PRIu64, __FUNCTION__, width,
197 height, SurfaceFlinger::maxVirtualDisplaySize);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700198 return {};
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800199 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700200 HWC2::Display* display;
Dan Stoza5cf424b2016-05-20 14:02:39 -0700201 auto error = mHwcDevice->createVirtualDisplay(width, height, format,
202 &display);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800203 if (error != HWC2::Error::None) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700204 ALOGE("%s: Failed to create HWC virtual display", __FUNCTION__);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700205 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700206 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800207
Dominik Laskowski075d3172018-05-24 15:50:06 -0700208 DisplayId displayId;
209 if (mFreeVirtualDisplayIds.empty()) {
210 displayId = getVirtualDisplayId(mNextVirtualDisplayId++);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800211 } else {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700212 displayId = *mFreeVirtualDisplayIds.begin();
213 mFreeVirtualDisplayIds.erase(displayId);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700214 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800215
Dominik Laskowski075d3172018-05-24 15:50:06 -0700216 auto& displayData = mDisplayData[displayId];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700217 displayData.hwcDisplay = display;
218 displayData.isVirtual = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800219
220 --mRemainingHwcVirtualDisplays;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700221 return displayId;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700222}
223
Dominik Laskowski075d3172018-05-24 15:50:06 -0700224HWC2::Layer* HWComposer::createLayer(DisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700225 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
226
Dan Stoza9e56aa02015-11-02 13:00:03 -0800227 auto display = mDisplayData[displayId].hwcDisplay;
Steven Thomas94e35b92017-07-26 18:48:28 -0700228 HWC2::Layer* layer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800229 auto error = display->createLayer(&layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700230 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800231 return layer;
232}
233
Dominik Laskowski075d3172018-05-24 15:50:06 -0700234void HWComposer::destroyLayer(DisplayId displayId, HWC2::Layer* layer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700235 RETURN_IF_INVALID_DISPLAY(displayId);
236
Steven Thomas94e35b92017-07-26 18:48:28 -0700237 auto display = mDisplayData[displayId].hwcDisplay;
238 auto error = display->destroyLayer(layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700239 RETURN_IF_HWC_ERROR(error, displayId);
Steven Thomas94e35b92017-07-26 18:48:28 -0700240}
241
Dominik Laskowski075d3172018-05-24 15:50:06 -0700242nsecs_t HWComposer::getRefreshTimestamp(DisplayId displayId) const {
243 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski1af47932018-11-12 10:20:46 -0800244 const auto& displayData = mDisplayData.at(displayId);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700245 // this returns the last refresh timestamp.
246 // if the last one is not available, we estimate it based on
247 // the refresh period and whatever closest timestamp we have.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800248 std::lock_guard lock(displayData.lastHwVsyncLock);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700249 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800250 auto vsyncPeriodNanos = getDisplayVsyncPeriod(displayId);
251 return now - ((now - displayData.lastHwVsync) % vsyncPeriodNanos);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700252}
253
Dominik Laskowski075d3172018-05-24 15:50:06 -0700254bool HWComposer::isConnected(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700255 RETURN_IF_INVALID_DISPLAY(displayId, false);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700256 return mDisplayData.at(displayId).hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700257}
258
Dominik Laskowski075d3172018-05-24 15:50:06 -0700259std::vector<std::shared_ptr<const HWC2::Display::Config>> HWComposer::getConfigs(
260 DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700261 RETURN_IF_INVALID_DISPLAY(displayId, {});
262
Dominik Laskowski075d3172018-05-24 15:50:06 -0700263 const auto& displayData = mDisplayData.at(displayId);
264 auto configs = displayData.hwcDisplay->getConfigs();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800265 if (displayData.configMap.empty()) {
266 for (size_t i = 0; i < configs.size(); ++i) {
267 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700268 }
269 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800270 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700271}
272
Dominik Laskowski075d3172018-05-24 15:50:06 -0700273std::shared_ptr<const HWC2::Display::Config> HWComposer::getActiveConfig(
274 DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700275 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
276
Dan Stoza9e56aa02015-11-02 13:00:03 -0800277 std::shared_ptr<const HWC2::Display::Config> config;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700278 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfig(&config);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800279 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700280 LOG_DISPLAY_ERROR(displayId, "No active config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800281 return nullptr;
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700282 }
283
284 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
285
286 if (!config) {
287 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800288 return nullptr;
289 }
290
291 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700292}
293
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800294// Composer 2.4
295
296bool HWComposer::isVsyncPeriodSwitchSupported(DisplayId displayId) const {
297 return mDisplayData.at(displayId).hwcDisplay->isVsyncPeriodSwitchSupported();
298}
299
300nsecs_t HWComposer::getDisplayVsyncPeriod(DisplayId displayId) const {
301 nsecs_t vsyncPeriodNanos;
302 auto error = mDisplayData.at(displayId).hwcDisplay->getDisplayVsyncPeriod(&vsyncPeriodNanos);
303 if (error != HWC2::Error::None) {
304 LOG_DISPLAY_ERROR(displayId, "Failed to get Vsync Period");
305 return 0;
306 }
307
308 return vsyncPeriodNanos;
309}
310
Dominik Laskowski075d3172018-05-24 15:50:06 -0700311int HWComposer::getActiveConfigIndex(DisplayId displayId) const {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700312 RETURN_IF_INVALID_DISPLAY(displayId, -1);
313
Lloyd Pique3c085a02018-05-09 19:38:32 -0700314 int index;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700315 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfigIndex(&index);
Lloyd Pique3c085a02018-05-09 19:38:32 -0700316 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700317 LOG_DISPLAY_ERROR(displayId, "No active config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700318 return -1;
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700319 }
320
321 RETURN_IF_HWC_ERROR(error, displayId, -1);
322
323 if (index < 0) {
324 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700325 return -1;
326 }
327
328 return index;
329}
330
Dominik Laskowski075d3172018-05-24 15:50:06 -0700331std::vector<ui::ColorMode> HWComposer::getColorModes(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700332 RETURN_IF_INVALID_DISPLAY(displayId, {});
333
Peiyong Linfd997e02018-03-28 15:29:00 -0700334 std::vector<ui::ColorMode> modes;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700335 auto error = mDisplayData.at(displayId).hwcDisplay->getColorModes(&modes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700336 RETURN_IF_HWC_ERROR(error, displayId, {});
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600337 return modes;
338}
339
Dominik Laskowski075d3172018-05-24 15:50:06 -0700340status_t HWComposer::setActiveColorMode(DisplayId displayId, ui::ColorMode mode,
341 ui::RenderIntent renderIntent) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700342 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Michael Wright28f24d02016-07-12 13:30:53 -0700343
344 auto& displayData = mDisplayData[displayId];
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700345 auto error = displayData.hwcDisplay->setColorMode(mode, renderIntent);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700346 RETURN_IF_HWC_ERROR_FOR(("setColorMode(" + decodeColorMode(mode) + ", " +
347 decodeRenderIntent(renderIntent) + ")")
348 .c_str(),
349 error, displayId, UNKNOWN_ERROR);
Michael Wright28f24d02016-07-12 13:30:53 -0700350
351 return NO_ERROR;
352}
353
Dominik Laskowski075d3172018-05-24 15:50:06 -0700354void HWComposer::setVsyncEnabled(DisplayId displayId, HWC2::Vsync enabled) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700355 RETURN_IF_INVALID_DISPLAY(displayId);
356 auto& displayData = mDisplayData[displayId];
357
358 if (displayData.isVirtual) {
359 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800360 return;
361 }
362
Dan Stoza9e56aa02015-11-02 13:00:03 -0800363 // NOTE: we use our own internal lock here because we have to call
364 // into the HWC with the lock held, and we want to make sure
365 // that even if HWC blocks (which it shouldn't), it won't
366 // affect other threads.
Dominik Laskowski1af47932018-11-12 10:20:46 -0800367 std::lock_guard lock(displayData.vsyncEnabledLock);
368 if (enabled == displayData.vsyncEnabled) {
369 return;
Colin Cross10fbdb62012-07-12 17:56:34 -0700370 }
Dominik Laskowski1af47932018-11-12 10:20:46 -0800371
372 ATRACE_CALL();
373 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
374 RETURN_IF_HWC_ERROR(error, displayId);
375
376 displayData.vsyncEnabled = enabled;
377
378 const auto tag = "HW_VSYNC_ON_" + to_string(displayId);
379 ATRACE_INT(tag.c_str(), enabled == HWC2::Vsync::Enable ? 1 : 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700380}
381
Dominik Laskowski075d3172018-05-24 15:50:06 -0700382status_t HWComposer::setClientTarget(DisplayId displayId, uint32_t slot,
383 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
384 ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700385 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Jesse Hall851cfe82013-03-20 13:44:00 -0700386
Dominik Laskowski34157762018-10-31 13:07:19 -0700387 ALOGV("%s for display %s", __FUNCTION__, to_string(displayId).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800388 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400389 auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700390 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Jesse Hall851cfe82013-03-20 13:44:00 -0700391 return NO_ERROR;
392}
393
Lloyd Pique66d68602019-02-13 14:23:31 -0800394status_t HWComposer::getDeviceCompositionChanges(
395 DisplayId displayId, bool frameUsesClientComposition,
396 std::optional<android::HWComposer::DeviceRequestedChanges>* outChanges) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800397 ATRACE_CALL();
398
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700399 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800400
401 auto& displayData = mDisplayData[displayId];
402 auto& hwcDisplay = displayData.hwcDisplay;
403 if (!hwcDisplay->isConnected()) {
404 return NO_ERROR;
405 }
406
407 uint32_t numTypes = 0;
408 uint32_t numRequests = 0;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700409
410 HWC2::Error error = HWC2::Error::None;
411
Chia-I Wu41b98d42017-12-11 11:04:36 -0800412 // First try to skip validate altogether when there is no client
413 // composition. When there is client composition, since we haven't
414 // rendered to the client target yet, we should not attempt to skip
415 // validate.
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700416 displayData.validateWasSkipped = false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800417 if (!frameUsesClientComposition) {
Dominik Laskowski1af47932018-11-12 10:20:46 -0800418 sp<Fence> outPresentFence;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700419 uint32_t state = UINT32_MAX;
420 error = hwcDisplay->presentOrValidate(&numTypes, &numRequests, &outPresentFence , &state);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700421 if (error != HWC2::Error::HasChanges) {
422 RETURN_IF_HWC_ERROR_FOR("presentOrValidate", error, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700423 }
424 if (state == 1) { //Present Succeeded.
Steven Thomas94e35b92017-07-26 18:48:28 -0700425 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700426 error = hwcDisplay->getReleaseFences(&releaseFences);
427 displayData.releaseFences = std::move(releaseFences);
428 displayData.lastPresentFence = outPresentFence;
429 displayData.validateWasSkipped = true;
430 displayData.presentError = error;
431 return NO_ERROR;
432 }
433 // Present failed but Validate ran.
434 } else {
435 error = hwcDisplay->validate(&numTypes, &numRequests);
436 }
437 ALOGV("SkipValidate failed, Falling back to SLOW validate/present");
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700438 if (error != HWC2::Error::HasChanges) {
439 RETURN_IF_HWC_ERROR_FOR("validate", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800440 }
441
Lloyd Pique66d68602019-02-13 14:23:31 -0800442 android::HWComposer::DeviceRequestedChanges::ChangedTypes changedTypes;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800443 changedTypes.reserve(numTypes);
444 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700445 RETURN_IF_HWC_ERROR_FOR("getChangedCompositionTypes", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800446
Lloyd Pique66d68602019-02-13 14:23:31 -0800447 auto displayRequests = static_cast<HWC2::DisplayRequest>(0);
448 android::HWComposer::DeviceRequestedChanges::LayerRequests layerRequests;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800449 layerRequests.reserve(numRequests);
Lloyd Pique66d68602019-02-13 14:23:31 -0800450 error = hwcDisplay->getRequests(&displayRequests, &layerRequests);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700451 RETURN_IF_HWC_ERROR_FOR("getRequests", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800452
Lloyd Pique66d68602019-02-13 14:23:31 -0800453 outChanges->emplace(DeviceRequestedChanges{std::move(changedTypes), std::move(displayRequests),
454 std::move(layerRequests)});
Dan Stoza9e56aa02015-11-02 13:00:03 -0800455
456 error = hwcDisplay->acceptChanges();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700457 RETURN_IF_HWC_ERROR_FOR("acceptChanges", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800458
459 return NO_ERROR;
460}
461
Dominik Laskowski075d3172018-05-24 15:50:06 -0700462sp<Fence> HWComposer::getPresentFence(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700463 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700464 return mDisplayData.at(displayId).lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700465}
466
Dominik Laskowski075d3172018-05-24 15:50:06 -0700467sp<Fence> HWComposer::getLayerReleaseFence(DisplayId displayId, HWC2::Layer* layer) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700468 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700469 auto displayFences = mDisplayData.at(displayId).releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800470 if (displayFences.count(layer) == 0) {
471 ALOGV("getLayerReleaseFence: Release fence not found");
472 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700473 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800474 return displayFences[layer];
Riley Andrews03414a12014-07-01 14:22:59 -0700475}
476
Dominik Laskowski075d3172018-05-24 15:50:06 -0700477status_t HWComposer::presentAndGetReleaseFences(DisplayId displayId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800478 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700479
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700480 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700481
Dan Stoza9e56aa02015-11-02 13:00:03 -0800482 auto& displayData = mDisplayData[displayId];
483 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700484
485 if (displayData.validateWasSkipped) {
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700486 // explicitly flush all pending commands
487 auto error = mHwcDevice->flushCommands();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700488 RETURN_IF_HWC_ERROR_FOR("flushCommands", error, displayId, UNKNOWN_ERROR);
489 RETURN_IF_HWC_ERROR_FOR("present", displayData.presentError, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700490 return NO_ERROR;
491 }
492
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800493 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700494 RETURN_IF_HWC_ERROR_FOR("present", error, displayId, UNKNOWN_ERROR);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700495
Steven Thomas94e35b92017-07-26 18:48:28 -0700496 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800497 error = hwcDisplay->getReleaseFences(&releaseFences);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700498 RETURN_IF_HWC_ERROR_FOR("getReleaseFences", error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800499
500 displayData.releaseFences = std::move(releaseFences);
501
502 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700503}
504
Dominik Laskowski075d3172018-05-24 15:50:06 -0700505status_t HWComposer::setPowerMode(DisplayId displayId, int32_t intMode) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700506 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
507
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700508 const auto& displayData = mDisplayData[displayId];
509 if (displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700510 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
511 return INVALID_OPERATION;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800512 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700513
Dan Stoza9e56aa02015-11-02 13:00:03 -0800514 auto mode = static_cast<HWC2::PowerMode>(intMode);
515 if (mode == HWC2::PowerMode::Off) {
516 setVsyncEnabled(displayId, HWC2::Vsync::Disable);
517 }
518
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700519 auto& hwcDisplay = displayData.hwcDisplay;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800520 switch (mode) {
521 case HWC2::PowerMode::Off:
522 case HWC2::PowerMode::On:
523 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
524 {
525 auto error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700526 if (error != HWC2::Error::None) {
527 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
528 error, displayId);
529 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700530 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800531 break;
532 case HWC2::PowerMode::Doze:
533 case HWC2::PowerMode::DozeSuspend:
534 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
535 {
536 bool supportsDoze = false;
537 auto error = hwcDisplay->supportsDoze(&supportsDoze);
Peiyong Lin306e4992018-05-07 16:18:22 -0700538 if (error != HWC2::Error::None) {
539 LOG_HWC_ERROR("supportsDoze", error, displayId);
540 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700541
Dan Stoza9e56aa02015-11-02 13:00:03 -0800542 if (!supportsDoze) {
543 mode = HWC2::PowerMode::On;
544 }
545
546 error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700547 if (error != HWC2::Error::None) {
548 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
549 error, displayId);
550 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800551 }
552 break;
553 default:
554 ALOGV("setPowerMode: Not calling HWC");
555 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700556 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800557
558 return NO_ERROR;
559}
560
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800561status_t HWComposer::setActiveConfigWithConstraints(
562 DisplayId displayId, size_t configId, const HWC2::VsyncPeriodChangeConstraints& constraints,
563 HWC2::VsyncPeriodChangeTimeline* outTimeline) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700564 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800565
566 auto& displayData = mDisplayData[displayId];
567 if (displayData.configMap.count(configId) == 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700568 LOG_DISPLAY_ERROR(displayId, ("Invalid config " + std::to_string(configId)).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800569 return BAD_INDEX;
570 }
571
Ady Abraham3a77a7b2019-12-02 18:46:59 -0800572 auto error =
573 displayData.hwcDisplay->setActiveConfigWithConstraints(displayData.configMap[configId],
574 constraints, outTimeline);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700575 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800576 return NO_ERROR;
577}
578
Dominik Laskowski075d3172018-05-24 15:50:06 -0700579status_t HWComposer::setColorTransform(DisplayId displayId, const mat4& transform) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700580 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700581
582 auto& displayData = mDisplayData[displayId];
583 bool isIdentity = transform == mat4();
584 auto error = displayData.hwcDisplay->setColorTransform(transform,
585 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY :
586 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700587 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700588 return NO_ERROR;
589}
590
Dominik Laskowski075d3172018-05-24 15:50:06 -0700591void HWComposer::disconnectDisplay(DisplayId displayId) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700592 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800593 auto& displayData = mDisplayData[displayId];
594
Dan Stoza9e56aa02015-11-02 13:00:03 -0800595 // If this was a virtual display, add its slot back for reuse by future
596 // virtual displays
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700597 if (displayData.isVirtual) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700598 mFreeVirtualDisplayIds.insert(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800599 ++mRemainingHwcVirtualDisplays;
600 }
601
Dominik Laskowski7e045462018-05-30 13:02:02 -0700602 const auto hwcDisplayId = displayData.hwcDisplay->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700603 mPhysicalDisplayIdMap.erase(hwcDisplayId);
604 mDisplayData.erase(displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700605
606 // TODO(b/74619554): Select internal/external display from remaining displays.
607 if (hwcDisplayId == mInternalHwcDisplayId) {
608 mInternalHwcDisplayId.reset();
609 } else if (hwcDisplayId == mExternalHwcDisplayId) {
610 mExternalHwcDisplayId.reset();
611 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700612
Dominik Laskowski7e045462018-05-30 13:02:02 -0700613 mHwcDevice->destroyDisplay(hwcDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800614}
615
Dominik Laskowski075d3172018-05-24 15:50:06 -0700616status_t HWComposer::setOutputBuffer(DisplayId displayId, const sp<Fence>& acquireFence,
617 const sp<GraphicBuffer>& buffer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700618 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700619 const auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800620
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700621 if (!displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700622 LOG_DISPLAY_ERROR(displayId, "Invalid operation on physical display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800623 return INVALID_OPERATION;
624 }
625
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700626 auto error = displayData.hwcDisplay->setOutputBuffer(buffer, acquireFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700627 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800628 return NO_ERROR;
629}
630
Dominik Laskowski075d3172018-05-24 15:50:06 -0700631void HWComposer::clearReleaseFences(DisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700632 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800633 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700634}
635
Dominik Laskowski075d3172018-05-24 15:50:06 -0700636status_t HWComposer::getHdrCapabilities(DisplayId displayId, HdrCapabilities* outCapabilities) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700637 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stozac4f471e2016-03-24 09:31:08 -0700638
639 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Peiyong Lin62665892018-04-16 11:07:44 -0700640 auto error = hwcDisplay->getHdrCapabilities(outCapabilities);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700641 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Peiyong Lin62665892018-04-16 11:07:44 -0700642 return NO_ERROR;
Dan Stozac4f471e2016-03-24 09:31:08 -0700643}
644
Dominik Laskowski075d3172018-05-24 15:50:06 -0700645int32_t HWComposer::getSupportedPerFrameMetadata(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700646 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700647 return mDisplayData.at(displayId).hwcDisplay->getSupportedPerFrameMetadata();
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700648}
649
Dominik Laskowski075d3172018-05-24 15:50:06 -0700650std::vector<ui::RenderIntent> HWComposer::getRenderIntents(DisplayId displayId,
651 ui::ColorMode colorMode) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700652 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700653
654 std::vector<ui::RenderIntent> renderIntents;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700655 auto error = mDisplayData.at(displayId).hwcDisplay->getRenderIntents(colorMode, &renderIntents);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700656 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700657 return renderIntents;
658}
659
Dominik Laskowski075d3172018-05-24 15:50:06 -0700660mat4 HWComposer::getDataspaceSaturationMatrix(DisplayId displayId, ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700661 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700662
663 mat4 matrix;
664 auto error = mDisplayData[displayId].hwcDisplay->getDataspaceSaturationMatrix(dataspace,
665 &matrix);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700666 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700667 return matrix;
668}
669
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700670status_t HWComposer::getDisplayedContentSamplingAttributes(DisplayId displayId,
671 ui::PixelFormat* outFormat,
672 ui::Dataspace* outDataspace,
673 uint8_t* outComponentMask) {
674 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
675 const auto error =
676 mDisplayData[displayId]
677 .hwcDisplay->getDisplayedContentSamplingAttributes(outFormat, outDataspace,
678 outComponentMask);
679 if (error == HWC2::Error::Unsupported) RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
680 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
681 return NO_ERROR;
682}
683
Kevin DuBois74e53772018-11-19 10:52:38 -0800684status_t HWComposer::setDisplayContentSamplingEnabled(DisplayId displayId, bool enabled,
685 uint8_t componentMask, uint64_t maxFrames) {
686 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
687 const auto error =
688 mDisplayData[displayId].hwcDisplay->setDisplayContentSamplingEnabled(enabled,
689 componentMask,
690 maxFrames);
691
692 if (error == HWC2::Error::Unsupported) RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
693 if (error == HWC2::Error::BadParameter) RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
694 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
695 return NO_ERROR;
696}
697
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700698status_t HWComposer::getDisplayedContentSample(DisplayId displayId, uint64_t maxFrames,
699 uint64_t timestamp, DisplayedFrameStats* outStats) {
700 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
701 const auto error =
702 mDisplayData[displayId].hwcDisplay->getDisplayedContentSample(maxFrames, timestamp,
703 outStats);
704 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
705 return NO_ERROR;
706}
707
Dan Gittik57e63c52019-01-18 16:37:54 +0000708status_t HWComposer::setDisplayBrightness(DisplayId displayId, float brightness) {
709 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
710 const auto error = mDisplayData[displayId].hwcDisplay->setDisplayBrightness(brightness);
711 if (error == HWC2::Error::Unsupported) {
712 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
713 }
714 if (error == HWC2::Error::BadParameter) {
715 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
716 }
717 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
718 return NO_ERROR;
719}
720
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800721bool HWComposer::isUsingVrComposer() const {
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800722 return getComposer()->isUsingVrComposer();
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800723}
724
Galia Peycheva5492cb52019-10-30 14:13:16 +0100725status_t HWComposer::setAutoLowLatencyMode(DisplayId displayId, bool on) {
726 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
727 const auto error = mDisplayData[displayId].hwcDisplay->setAutoLowLatencyMode(on);
728 if (error == HWC2::Error::Unsupported) {
729 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
730 }
731 if (error == HWC2::Error::BadParameter) {
732 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
733 }
734 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
735 return NO_ERROR;
736}
737
738status_t HWComposer::getSupportedContentTypes(
739 DisplayId displayId, std::vector<HWC2::ContentType>* outSupportedContentTypes) {
740 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
741 const auto error =
742 mDisplayData[displayId].hwcDisplay->getSupportedContentTypes(outSupportedContentTypes);
743
744 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
745
746 return NO_ERROR;
747}
748
749status_t HWComposer::setContentType(DisplayId displayId, HWC2::ContentType contentType) {
750 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
751 const auto error = mDisplayData[displayId].hwcDisplay->setContentType(contentType);
752 if (error == HWC2::Error::Unsupported) {
753 RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
754 }
755 if (error == HWC2::Error::BadParameter) {
756 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
757 }
758 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
759
760 return NO_ERROR;
761}
762
Yiwei Zhang5434a782018-12-05 18:06:32 -0800763void HWComposer::dump(std::string& result) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800764 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow
765 // all the state going into the layers. This is probably better done in
766 // Layer itself, but it's going to take a bit of work to get there.
Yiwei Zhang5434a782018-12-05 18:06:32 -0800767 result.append(mHwcDevice->dump());
Mathias Agopian83727852010-09-23 18:13:21 -0700768}
769
Dominik Laskowski075d3172018-05-24 15:50:06 -0700770std::optional<DisplayId> HWComposer::toPhysicalDisplayId(hwc2_display_t hwcDisplayId) const {
771 if (const auto it = mPhysicalDisplayIdMap.find(hwcDisplayId);
772 it != mPhysicalDisplayIdMap.end()) {
773 return it->second;
774 }
775 return {};
776}
777
778std::optional<hwc2_display_t> HWComposer::fromPhysicalDisplayId(DisplayId displayId) const {
779 if (const auto it = mDisplayData.find(displayId);
780 it != mDisplayData.end() && !it->second.isVirtual) {
781 return it->second.hwcDisplay->getId();
782 }
783 return {};
784}
785
786std::optional<DisplayIdentificationInfo> HWComposer::onHotplugConnect(hwc2_display_t hwcDisplayId) {
787 if (isUsingVrComposer() && mInternalHwcDisplayId) {
788 ALOGE("Ignoring connection of external display %" PRIu64 " in VR mode", hwcDisplayId);
Steven Thomas6e8f7062017-11-22 14:15:29 -0800789 return {};
790 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700791
792 uint8_t port;
793 DisplayIdentificationData data;
794 const bool hasMultiDisplaySupport = getDisplayIdentificationData(hwcDisplayId, &port, &data);
795
796 if (mPhysicalDisplayIdMap.empty()) {
797 mHasMultiDisplaySupport = hasMultiDisplaySupport;
798 ALOGI("Switching to %s multi-display mode",
799 hasMultiDisplaySupport ? "generalized" : "legacy");
800 } else if (mHasMultiDisplaySupport && !hasMultiDisplaySupport) {
801 ALOGE("Ignoring connection of display %" PRIu64 " without identification data",
802 hwcDisplayId);
803 return {};
804 }
805
806 std::optional<DisplayIdentificationInfo> info;
807
808 if (mHasMultiDisplaySupport) {
809 info = parseDisplayIdentificationData(port, data);
810 ALOGE_IF(!info, "Failed to parse identification data for display %" PRIu64, hwcDisplayId);
811 } else if (mInternalHwcDisplayId && mExternalHwcDisplayId) {
812 ALOGE("Ignoring connection of tertiary display %" PRIu64, hwcDisplayId);
813 return {};
814 } else {
815 ALOGW_IF(hasMultiDisplaySupport, "Ignoring identification data for display %" PRIu64,
816 hwcDisplayId);
817 port = mInternalHwcDisplayId ? HWC_DISPLAY_EXTERNAL : HWC_DISPLAY_PRIMARY;
818 }
819
820 if (!mInternalHwcDisplayId) {
821 mInternalHwcDisplayId = hwcDisplayId;
822 } else if (!mExternalHwcDisplayId) {
823 mExternalHwcDisplayId = hwcDisplayId;
824 }
825
826 if (info) return info;
827
828 return DisplayIdentificationInfo{getFallbackDisplayId(port),
829 hwcDisplayId == mInternalHwcDisplayId ? "Internal display"
830 : "External display"};
Steven Thomas6e8f7062017-11-22 14:15:29 -0800831}
832
Lloyd Pique441d5042018-10-18 16:49:51 -0700833} // namespace impl
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700834} // namespace android