blob: 0f25b52f03f8e9102ddeb1ea096a8704f8242214 [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
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070023#include <inttypes.h>
24#include <math.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070025#include <stdint.h>
Mathias Agopianf1352df2010-08-11 17:31:33 -070026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070029#include <sys/types.h>
30
31#include <utils/Errors.h>
Mathias Agopianda27af92012-09-13 18:17:13 -070032#include <utils/misc.h>
Jesse Hall399184a2014-03-03 15:42:54 -080033#include <utils/NativeHandle.h>
Mathias Agopian83727852010-09-23 18:13:21 -070034#include <utils/String8.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070035#include <utils/Thread.h>
Mathias Agopian2965b262012-04-08 15:13:32 -070036#include <utils/Trace.h>
Mathias Agopian22da60c2011-09-09 00:49:11 -070037#include <utils/Vector.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070038
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070039#include <ui/DebugUtils.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070040#include <ui/GraphicBuffer.h>
41
Mathias Agopiana350ff92010-08-10 17:14:02 -070042#include <hardware/hardware.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070043#include <hardware/hwcomposer.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070044
Jesse Hall1c569c42013-04-05 13:44:52 -070045#include <android/configuration.h>
46
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070047#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070048#include <log/log.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070049
Mathias Agopiana350ff92010-08-10 17:14:02 -070050#include "HWComposer.h"
Dan Stoza9e56aa02015-11-02 13:00:03 -080051#include "HWC2.h"
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -080052#include "ComposerHal.h"
Mathias Agopian33ceeb32013-04-01 16:54:58 -070053
54#include "../Layer.h" // needed only for debugging
55#include "../SurfaceFlinger.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070056
Dominik Laskowskic1f18f62018-06-13 15:17:55 -070057#define LOG_HWC_DISPLAY_ERROR(hwcDisplayId, msg) \
58 ALOGE("%s failed for HWC display %" PRIu64 ": %s", __FUNCTION__, hwcDisplayId, msg)
59
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070060#define LOG_DISPLAY_ERROR(displayId, msg) \
Dominik Laskowski34157762018-10-31 13:07:19 -070061 ALOGE("%s failed for display %s: %s", __FUNCTION__, to_string(displayId).c_str(), msg)
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070062
Dominik Laskowski34157762018-10-31 13:07:19 -070063#define LOG_HWC_ERROR(what, error, displayId) \
64 ALOGE("%s: %s failed for display %s: %s (%d)", __FUNCTION__, what, \
65 to_string(displayId).c_str(), to_string(error).c_str(), static_cast<int32_t>(error))
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070066
67#define RETURN_IF_INVALID_DISPLAY(displayId, ...) \
68 do { \
Dominik Laskowski075d3172018-05-24 15:50:06 -070069 if (mDisplayData.count(displayId) == 0) { \
Dominik Laskowskifc2c0322018-04-19 14:47:33 -070070 LOG_DISPLAY_ERROR(displayId, "Invalid display"); \
71 return __VA_ARGS__; \
72 } \
73 } while (false)
74
75#define RETURN_IF_HWC_ERROR_FOR(what, error, displayId, ...) \
76 do { \
77 if (error != HWC2::Error::None) { \
78 LOG_HWC_ERROR(what, error, displayId); \
79 return __VA_ARGS__; \
80 } \
81 } while (false)
82
83#define RETURN_IF_HWC_ERROR(error, displayId, ...) \
84 RETURN_IF_HWC_ERROR_FOR(__FUNCTION__, error, displayId, __VA_ARGS__)
85
Mathias Agopiana350ff92010-08-10 17:14:02 -070086namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -070087
Jesse Hall72960512013-01-10 16:19:56 -080088#define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION
Jesse Hall9eb1eb52012-08-29 10:39:38 -070089
Mathias Agopiana350ff92010-08-10 17:14:02 -070090// ---------------------------------------------------------------------------
91
Lloyd Piquea822d522017-12-20 16:42:57 -080092HWComposer::HWComposer(std::unique_ptr<android::Hwc2::Composer> composer)
93 : mHwcDevice(std::make_unique<HWC2::Device>(std::move(composer))) {}
Mathias Agopianbef42c52013-08-21 17:45:46 -070094
Dominik Laskowskib04f98a2018-11-07 21:07:16 -080095HWComposer::~HWComposer() {
96 mDisplayData.clear();
97}
Dan Stoza9e56aa02015-11-02 13:00:03 -080098
Steven Thomas94e35b92017-07-26 18:48:28 -070099void HWComposer::registerCallback(HWC2::ComposerCallback* callback,
100 int32_t sequenceId) {
101 mHwcDevice->registerCallback(callback, sequenceId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800102}
103
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700104bool HWComposer::getDisplayIdentificationData(hwc2_display_t hwcDisplayId, uint8_t* outPort,
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700105 DisplayIdentificationData* outData) const {
Dominik Laskowski0954b1d2018-06-13 14:58:49 -0700106 const auto error = mHwcDevice->getDisplayIdentificationData(hwcDisplayId, outPort, outData);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700107 if (error != HWC2::Error::None) {
Chia-I Wud0aff9d2018-06-21 13:39:09 +0800108 if (error != HWC2::Error::Unsupported) {
109 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, to_string(error).c_str());
110 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700111 return false;
112 }
113 return true;
114}
115
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700116bool HWComposer::hasCapability(HWC2::Capability capability) const
117{
118 return mHwcDevice->getCapabilities().count(capability) > 0;
119}
120
Dan Stoza9e56aa02015-11-02 13:00:03 -0800121void HWComposer::validateChange(HWC2::Composition from, HWC2::Composition to) {
122 bool valid = true;
123 switch (from) {
124 case HWC2::Composition::Client:
125 valid = false;
126 break;
127 case HWC2::Composition::Device:
128 case HWC2::Composition::SolidColor:
129 valid = (to == HWC2::Composition::Client);
130 break;
131 case HWC2::Composition::Cursor:
132 case HWC2::Composition::Sideband:
133 valid = (to == HWC2::Composition::Client ||
134 to == HWC2::Composition::Device);
135 break;
136 default:
137 break;
138 }
139
140 if (!valid) {
141 ALOGE("Invalid layer type change: %s --> %s", to_string(from).c_str(),
142 to_string(to).c_str());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700143 }
144}
145
Dominik Laskowski075d3172018-05-24 15:50:06 -0700146std::optional<DisplayIdentificationInfo> HWComposer::onHotplug(hwc2_display_t hwcDisplayId,
147 HWC2::Connection connection) {
148 std::optional<DisplayIdentificationInfo> info;
Lloyd Pique715a2c12017-12-14 17:18:08 -0800149
Dominik Laskowski075d3172018-05-24 15:50:06 -0700150 if (const auto displayId = toPhysicalDisplayId(hwcDisplayId)) {
151 info = DisplayIdentificationInfo{*displayId, std::string()};
152 } else {
153 if (connection == HWC2::Connection::Disconnected) {
154 ALOGE("Ignoring disconnection of invalid HWC display %" PRIu64, hwcDisplayId);
155 return {};
Lloyd Pique438e9e72018-09-04 18:06:08 -0700156 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700157
158 info = onHotplugConnect(hwcDisplayId);
159 if (!info) return {};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700160 }
161
Dominik Laskowski34157762018-10-31 13:07:19 -0700162 ALOGV("%s: %s %s display %s with HWC ID %" PRIu64, __FUNCTION__, to_string(connection).c_str(),
163 hwcDisplayId == mInternalHwcDisplayId ? "internal" : "external",
164 to_string(info->id).c_str(), hwcDisplayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700165
166 mHwcDevice->onHotplug(hwcDisplayId, connection);
167
Lloyd Pique715a2c12017-12-14 17:18:08 -0800168 // Disconnect is handled through HWComposer::disconnectDisplay via
169 // SurfaceFlinger's onHotplugReceived callback handling
170 if (connection == HWC2::Connection::Connected) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700171 mDisplayData[info->id].hwcDisplay = mHwcDevice->getDisplayById(hwcDisplayId);
172 mPhysicalDisplayIdMap[hwcDisplayId] = info->id;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700173 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700174
Dominik Laskowski075d3172018-05-24 15:50:06 -0700175 return info;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700176}
177
Dominik Laskowski075d3172018-05-24 15:50:06 -0700178bool HWComposer::onVsync(hwc2_display_t hwcDisplayId, int64_t timestamp) {
179 const auto displayId = toPhysicalDisplayId(hwcDisplayId);
180 if (!displayId) {
181 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, "Invalid HWC display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700182 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700183 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700184
Dominik Laskowski075d3172018-05-24 15:50:06 -0700185 RETURN_IF_INVALID_DISPLAY(*displayId, false);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700186
Dominik Laskowski075d3172018-05-24 15:50:06 -0700187 const auto& displayData = mDisplayData[*displayId];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700188 if (displayData.isVirtual) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700189 LOG_DISPLAY_ERROR(*displayId, "Invalid operation on virtual display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700190 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700191 }
192
Dan Stoza9e56aa02015-11-02 13:00:03 -0800193 {
194 Mutex::Autolock _l(mLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700195
Dan Stoza9e56aa02015-11-02 13:00:03 -0800196 // There have been reports of HWCs that signal several vsync events
197 // with the same timestamp when turning the display off and on. This
198 // is a bug in the HWC implementation, but filter the extra events
199 // out here so they don't cause havoc downstream.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700200 if (timestamp == mLastHwVSync[*displayId]) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700201 ALOGW("Ignoring duplicate VSYNC event from HWC for display %s (t=%" PRId64 ")",
202 to_string(*displayId).c_str(), timestamp);
Steven Thomas94e35b92017-07-26 18:48:28 -0700203 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800204 }
205
Dominik Laskowski075d3172018-05-24 15:50:06 -0700206 mLastHwVSync[*displayId] = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700207 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800208
Dominik Laskowski34157762018-10-31 13:07:19 -0700209 const auto tag = "HW_VSYNC_" + to_string(*displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700210 ATRACE_INT(tag.c_str(), ++mVSyncCounts[*displayId] & 1);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800211
Steven Thomas94e35b92017-07-26 18:48:28 -0700212 return true;
Jesse Hall1c569c42013-04-05 13:44:52 -0700213}
214
Dominik Laskowski075d3172018-05-24 15:50:06 -0700215std::optional<DisplayId> HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
216 ui::PixelFormat* format) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800217 if (mRemainingHwcVirtualDisplays == 0) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700218 ALOGE("%s: No remaining virtual displays", __FUNCTION__);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700219 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700220 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700221
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800222 if (SurfaceFlinger::maxVirtualDisplaySize != 0 &&
223 (width > SurfaceFlinger::maxVirtualDisplaySize ||
224 height > SurfaceFlinger::maxVirtualDisplaySize)) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700225 ALOGE("%s: Display size %ux%u exceeds maximum dimension of %" PRIu64, __FUNCTION__, width,
226 height, SurfaceFlinger::maxVirtualDisplaySize);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700227 return {};
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800228 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700229 HWC2::Display* display;
Dan Stoza5cf424b2016-05-20 14:02:39 -0700230 auto error = mHwcDevice->createVirtualDisplay(width, height, format,
231 &display);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800232 if (error != HWC2::Error::None) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700233 ALOGE("%s: Failed to create HWC virtual display", __FUNCTION__);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700234 return {};
Mathias Agopiane60b0682012-08-21 23:34:09 -0700235 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800236
Dominik Laskowski075d3172018-05-24 15:50:06 -0700237 DisplayId displayId;
238 if (mFreeVirtualDisplayIds.empty()) {
239 displayId = getVirtualDisplayId(mNextVirtualDisplayId++);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800240 } else {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700241 displayId = *mFreeVirtualDisplayIds.begin();
242 mFreeVirtualDisplayIds.erase(displayId);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700243 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800244
Dominik Laskowski075d3172018-05-24 15:50:06 -0700245 auto& displayData = mDisplayData[displayId];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700246 displayData.hwcDisplay = display;
247 displayData.isVirtual = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800248
249 --mRemainingHwcVirtualDisplays;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700250 return displayId;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700251}
252
Dominik Laskowski075d3172018-05-24 15:50:06 -0700253HWC2::Layer* HWComposer::createLayer(DisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700254 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
255
Dan Stoza9e56aa02015-11-02 13:00:03 -0800256 auto display = mDisplayData[displayId].hwcDisplay;
Steven Thomas94e35b92017-07-26 18:48:28 -0700257 HWC2::Layer* layer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800258 auto error = display->createLayer(&layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700259 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800260 return layer;
261}
262
Dominik Laskowski075d3172018-05-24 15:50:06 -0700263void HWComposer::destroyLayer(DisplayId displayId, HWC2::Layer* layer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700264 RETURN_IF_INVALID_DISPLAY(displayId);
265
Steven Thomas94e35b92017-07-26 18:48:28 -0700266 auto display = mDisplayData[displayId].hwcDisplay;
267 auto error = display->destroyLayer(layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700268 RETURN_IF_HWC_ERROR(error, displayId);
Steven Thomas94e35b92017-07-26 18:48:28 -0700269}
270
Dominik Laskowski075d3172018-05-24 15:50:06 -0700271nsecs_t HWComposer::getRefreshTimestamp(DisplayId displayId) const {
272 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700273 // this returns the last refresh timestamp.
274 // if the last one is not available, we estimate it based on
275 // the refresh period and whatever closest timestamp we have.
276 Mutex::Autolock _l(mLock);
277 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800278 auto vsyncPeriod = getActiveConfig(displayId)->getVsyncPeriod();
279 return now - ((now - mLastHwVSync[displayId]) % vsyncPeriod);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700280}
281
Dominik Laskowski075d3172018-05-24 15:50:06 -0700282bool HWComposer::isConnected(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700283 RETURN_IF_INVALID_DISPLAY(displayId, false);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700284 return mDisplayData.at(displayId).hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700285}
286
Dominik Laskowski075d3172018-05-24 15:50:06 -0700287std::vector<std::shared_ptr<const HWC2::Display::Config>> HWComposer::getConfigs(
288 DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700289 RETURN_IF_INVALID_DISPLAY(displayId, {});
290
Dominik Laskowski075d3172018-05-24 15:50:06 -0700291 const auto& displayData = mDisplayData.at(displayId);
292 auto configs = displayData.hwcDisplay->getConfigs();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800293 if (displayData.configMap.empty()) {
294 for (size_t i = 0; i < configs.size(); ++i) {
295 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700296 }
297 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800298 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700299}
300
Dominik Laskowski075d3172018-05-24 15:50:06 -0700301std::shared_ptr<const HWC2::Display::Config> HWComposer::getActiveConfig(
302 DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700303 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
304
Dan Stoza9e56aa02015-11-02 13:00:03 -0800305 std::shared_ptr<const HWC2::Display::Config> config;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700306 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfig(&config);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800307 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700308 LOG_DISPLAY_ERROR(displayId, "No active config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800309 return nullptr;
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700310 }
311
312 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
313
314 if (!config) {
315 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800316 return nullptr;
317 }
318
319 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700320}
321
Dominik Laskowski075d3172018-05-24 15:50:06 -0700322int HWComposer::getActiveConfigIndex(DisplayId displayId) const {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700323 RETURN_IF_INVALID_DISPLAY(displayId, -1);
324
Lloyd Pique3c085a02018-05-09 19:38:32 -0700325 int index;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700326 auto error = mDisplayData.at(displayId).hwcDisplay->getActiveConfigIndex(&index);
Lloyd Pique3c085a02018-05-09 19:38:32 -0700327 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700328 LOG_DISPLAY_ERROR(displayId, "No active config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700329 return -1;
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700330 }
331
332 RETURN_IF_HWC_ERROR(error, displayId, -1);
333
334 if (index < 0) {
335 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700336 return -1;
337 }
338
339 return index;
340}
341
Dominik Laskowski075d3172018-05-24 15:50:06 -0700342std::vector<ui::ColorMode> HWComposer::getColorModes(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700343 RETURN_IF_INVALID_DISPLAY(displayId, {});
344
Peiyong Linfd997e02018-03-28 15:29:00 -0700345 std::vector<ui::ColorMode> modes;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700346 auto error = mDisplayData.at(displayId).hwcDisplay->getColorModes(&modes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700347 RETURN_IF_HWC_ERROR(error, displayId, {});
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600348 return modes;
349}
350
Dominik Laskowski075d3172018-05-24 15:50:06 -0700351status_t HWComposer::setActiveColorMode(DisplayId displayId, ui::ColorMode mode,
352 ui::RenderIntent renderIntent) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700353 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Michael Wright28f24d02016-07-12 13:30:53 -0700354
355 auto& displayData = mDisplayData[displayId];
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700356 auto error = displayData.hwcDisplay->setColorMode(mode, renderIntent);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700357 RETURN_IF_HWC_ERROR_FOR(("setColorMode(" + decodeColorMode(mode) + ", " +
358 decodeRenderIntent(renderIntent) + ")")
359 .c_str(),
360 error, displayId, UNKNOWN_ERROR);
Michael Wright28f24d02016-07-12 13:30:53 -0700361
362 return NO_ERROR;
363}
364
Dominik Laskowski075d3172018-05-24 15:50:06 -0700365void HWComposer::setVsyncEnabled(DisplayId displayId, HWC2::Vsync enabled) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700366 RETURN_IF_INVALID_DISPLAY(displayId);
367 auto& displayData = mDisplayData[displayId];
368
369 if (displayData.isVirtual) {
370 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800371 return;
372 }
373
Dan Stoza9e56aa02015-11-02 13:00:03 -0800374 // NOTE: we use our own internal lock here because we have to call
375 // into the HWC with the lock held, and we want to make sure
376 // that even if HWC blocks (which it shouldn't), it won't
377 // affect other threads.
378 Mutex::Autolock _l(mVsyncLock);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800379 if (enabled != displayData.vsyncEnabled) {
380 ATRACE_CALL();
381 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700382 RETURN_IF_HWC_ERROR(error, displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800383
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700384 displayData.vsyncEnabled = enabled;
385
Dominik Laskowski34157762018-10-31 13:07:19 -0700386 const auto tag = "HW_VSYNC_ON_" + to_string(displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700387 ATRACE_INT(tag.c_str(), enabled == HWC2::Vsync::Enable ? 1 : 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700388 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700389}
390
Dominik Laskowski075d3172018-05-24 15:50:06 -0700391status_t HWComposer::setClientTarget(DisplayId displayId, uint32_t slot,
392 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
393 ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700394 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Jesse Hall851cfe82013-03-20 13:44:00 -0700395
Dominik Laskowski34157762018-10-31 13:07:19 -0700396 ALOGV("%s for display %s", __FUNCTION__, to_string(displayId).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800397 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400398 auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700399 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Jesse Hall851cfe82013-03-20 13:44:00 -0700400 return NO_ERROR;
401}
402
Dominik Laskowski075d3172018-05-24 15:50:06 -0700403status_t HWComposer::prepare(DisplayId displayId, std::vector<CompositionInfo>& compositionData) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800404 ATRACE_CALL();
405
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700406 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800407
Dominik Laskowski075d3172018-05-24 15:50:06 -0700408 Mutex::Autolock _l(mDisplayLock);
409
Dan Stoza9e56aa02015-11-02 13:00:03 -0800410 auto& displayData = mDisplayData[displayId];
411 auto& hwcDisplay = displayData.hwcDisplay;
412 if (!hwcDisplay->isConnected()) {
413 return NO_ERROR;
414 }
415
416 uint32_t numTypes = 0;
417 uint32_t numRequests = 0;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700418
419 HWC2::Error error = HWC2::Error::None;
420
Chia-I Wu41b98d42017-12-11 11:04:36 -0800421 // First try to skip validate altogether when there is no client
422 // composition. When there is client composition, since we haven't
423 // rendered to the client target yet, we should not attempt to skip
424 // validate.
425 //
426 // displayData.hasClientComposition hasn't been updated for this frame.
427 // The check below is incorrect. We actually rely on HWC here to fall
428 // back to validate when there is any client layer.
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700429 displayData.validateWasSkipped = false;
Chia-I Wu41b98d42017-12-11 11:04:36 -0800430 if (!displayData.hasClientComposition) {
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700431 sp<android::Fence> outPresentFence;
432 uint32_t state = UINT32_MAX;
433 error = hwcDisplay->presentOrValidate(&numTypes, &numRequests, &outPresentFence , &state);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700434 if (error != HWC2::Error::HasChanges) {
435 RETURN_IF_HWC_ERROR_FOR("presentOrValidate", error, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700436 }
437 if (state == 1) { //Present Succeeded.
Steven Thomas94e35b92017-07-26 18:48:28 -0700438 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700439 error = hwcDisplay->getReleaseFences(&releaseFences);
440 displayData.releaseFences = std::move(releaseFences);
441 displayData.lastPresentFence = outPresentFence;
442 displayData.validateWasSkipped = true;
443 displayData.presentError = error;
444 return NO_ERROR;
445 }
446 // Present failed but Validate ran.
447 } else {
448 error = hwcDisplay->validate(&numTypes, &numRequests);
449 }
450 ALOGV("SkipValidate failed, Falling back to SLOW validate/present");
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700451 if (error != HWC2::Error::HasChanges) {
452 RETURN_IF_HWC_ERROR_FOR("validate", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800453 }
454
Steven Thomas94e35b92017-07-26 18:48:28 -0700455 std::unordered_map<HWC2::Layer*, HWC2::Composition> changedTypes;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800456 changedTypes.reserve(numTypes);
457 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700458 RETURN_IF_HWC_ERROR_FOR("getChangedCompositionTypes", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800459
460 displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0);
Steven Thomas94e35b92017-07-26 18:48:28 -0700461 std::unordered_map<HWC2::Layer*, HWC2::LayerRequest> layerRequests;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800462 layerRequests.reserve(numRequests);
463 error = hwcDisplay->getRequests(&displayData.displayRequests,
464 &layerRequests);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700465 RETURN_IF_HWC_ERROR_FOR("getRequests", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800466
467 displayData.hasClientComposition = false;
468 displayData.hasDeviceComposition = false;
David Sodmanfb95bcc2017-12-22 15:45:30 -0800469 for (auto& compositionInfo : compositionData) {
470 auto hwcLayer = compositionInfo.hwc.hwcLayer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800471
David Sodmanfb95bcc2017-12-22 15:45:30 -0800472 if (changedTypes.count(&*hwcLayer) != 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800473 // We pass false so we only update our state and don't call back
474 // into the HWC device
David Sodmanfb95bcc2017-12-22 15:45:30 -0800475 validateChange(compositionInfo.compositionType,
476 changedTypes[&*hwcLayer]);
477 compositionInfo.compositionType = changedTypes[&*hwcLayer];
David Sodmanc1498e62018-09-12 14:36:26 -0700478 compositionInfo.layer->mLayer->setCompositionType(displayId,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700479 compositionInfo.compositionType,
480 false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800481 }
482
David Sodmanfb95bcc2017-12-22 15:45:30 -0800483 switch (compositionInfo.compositionType) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800484 case HWC2::Composition::Client:
485 displayData.hasClientComposition = true;
486 break;
487 case HWC2::Composition::Device:
488 case HWC2::Composition::SolidColor:
489 case HWC2::Composition::Cursor:
490 case HWC2::Composition::Sideband:
491 displayData.hasDeviceComposition = true;
492 break;
493 default:
494 break;
495 }
496
David Sodmanfb95bcc2017-12-22 15:45:30 -0800497 if (layerRequests.count(&*hwcLayer) != 0 &&
498 layerRequests[&*hwcLayer] ==
Dan Stoza9e56aa02015-11-02 13:00:03 -0800499 HWC2::LayerRequest::ClearClientTarget) {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800500 compositionInfo.hwc.clearClientTarget = true;
David Sodmanc1498e62018-09-12 14:36:26 -0700501 compositionInfo.layer->mLayer->setClearClientTarget(displayId, true);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800502 } else {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800503 if (layerRequests.count(&*hwcLayer) != 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700504 LOG_DISPLAY_ERROR(displayId,
David Sodmanfb95bcc2017-12-22 15:45:30 -0800505 ("Unknown layer request " + to_string(layerRequests[&*hwcLayer]))
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700506 .c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800507 }
David Sodmanfb95bcc2017-12-22 15:45:30 -0800508 compositionInfo.hwc.clearClientTarget = false;
David Sodmanc1498e62018-09-12 14:36:26 -0700509 compositionInfo.layer->mLayer->setClearClientTarget(displayId, false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800510 }
511 }
512
513 error = hwcDisplay->acceptChanges();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700514 RETURN_IF_HWC_ERROR_FOR("acceptChanges", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800515
516 return NO_ERROR;
517}
518
Dominik Laskowski075d3172018-05-24 15:50:06 -0700519bool HWComposer::hasDeviceComposition(const std::optional<DisplayId>& displayId) const {
520 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700521 // Displays without a corresponding HWC display are never composed by
522 // the device
523 return false;
524 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700525
Dominik Laskowski075d3172018-05-24 15:50:06 -0700526 RETURN_IF_INVALID_DISPLAY(*displayId, false);
527 return mDisplayData.at(*displayId).hasDeviceComposition;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800528}
529
Dominik Laskowski075d3172018-05-24 15:50:06 -0700530bool HWComposer::hasFlipClientTargetRequest(const std::optional<DisplayId>& displayId) const {
531 if (!displayId) {
Madhuri Athota88a905b2017-05-04 16:58:15 +0530532 // Displays without a corresponding HWC display are never composed by
533 // the device
534 return false;
535 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700536
Dominik Laskowski075d3172018-05-24 15:50:06 -0700537 RETURN_IF_INVALID_DISPLAY(*displayId, false);
538 return ((static_cast<uint32_t>(mDisplayData.at(*displayId).displayRequests) &
Madhuri Athota88a905b2017-05-04 16:58:15 +0530539 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0);
540}
541
Dominik Laskowski075d3172018-05-24 15:50:06 -0700542bool HWComposer::hasClientComposition(const std::optional<DisplayId>& displayId) const {
543 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700544 // Displays without a corresponding HWC display are always composed by
545 // the client
546 return true;
547 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700548
Dominik Laskowski075d3172018-05-24 15:50:06 -0700549 RETURN_IF_INVALID_DISPLAY(*displayId, true);
550 return mDisplayData.at(*displayId).hasClientComposition;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800551}
552
Dominik Laskowski075d3172018-05-24 15:50:06 -0700553sp<Fence> HWComposer::getPresentFence(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700554 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700555 return mDisplayData.at(displayId).lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700556}
557
Dominik Laskowski075d3172018-05-24 15:50:06 -0700558sp<Fence> HWComposer::getLayerReleaseFence(DisplayId displayId, HWC2::Layer* layer) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700559 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700560 auto displayFences = mDisplayData.at(displayId).releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800561 if (displayFences.count(layer) == 0) {
562 ALOGV("getLayerReleaseFence: Release fence not found");
563 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700564 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800565 return displayFences[layer];
Riley Andrews03414a12014-07-01 14:22:59 -0700566}
567
Dominik Laskowski075d3172018-05-24 15:50:06 -0700568status_t HWComposer::presentAndGetReleaseFences(DisplayId displayId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800569 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700570
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700571 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700572
Dan Stoza9e56aa02015-11-02 13:00:03 -0800573 auto& displayData = mDisplayData[displayId];
574 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700575
576 if (displayData.validateWasSkipped) {
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700577 // explicitly flush all pending commands
578 auto error = mHwcDevice->flushCommands();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700579 RETURN_IF_HWC_ERROR_FOR("flushCommands", error, displayId, UNKNOWN_ERROR);
580 RETURN_IF_HWC_ERROR_FOR("present", displayData.presentError, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700581 return NO_ERROR;
582 }
583
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800584 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700585 RETURN_IF_HWC_ERROR_FOR("present", error, displayId, UNKNOWN_ERROR);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700586
Steven Thomas94e35b92017-07-26 18:48:28 -0700587 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800588 error = hwcDisplay->getReleaseFences(&releaseFences);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700589 RETURN_IF_HWC_ERROR_FOR("getReleaseFences", error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800590
591 displayData.releaseFences = std::move(releaseFences);
592
593 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700594}
595
Dominik Laskowski075d3172018-05-24 15:50:06 -0700596status_t HWComposer::setPowerMode(DisplayId displayId, int32_t intMode) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700597 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
598
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700599 const auto& displayData = mDisplayData[displayId];
600 if (displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700601 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
602 return INVALID_OPERATION;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800603 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700604
Dan Stoza9e56aa02015-11-02 13:00:03 -0800605 auto mode = static_cast<HWC2::PowerMode>(intMode);
606 if (mode == HWC2::PowerMode::Off) {
607 setVsyncEnabled(displayId, HWC2::Vsync::Disable);
608 }
609
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700610 auto& hwcDisplay = displayData.hwcDisplay;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800611 switch (mode) {
612 case HWC2::PowerMode::Off:
613 case HWC2::PowerMode::On:
614 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
615 {
616 auto error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700617 if (error != HWC2::Error::None) {
618 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
619 error, displayId);
620 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700621 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800622 break;
623 case HWC2::PowerMode::Doze:
624 case HWC2::PowerMode::DozeSuspend:
625 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
626 {
627 bool supportsDoze = false;
628 auto error = hwcDisplay->supportsDoze(&supportsDoze);
Peiyong Lin306e4992018-05-07 16:18:22 -0700629 if (error != HWC2::Error::None) {
630 LOG_HWC_ERROR("supportsDoze", error, displayId);
631 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700632
Dan Stoza9e56aa02015-11-02 13:00:03 -0800633 if (!supportsDoze) {
634 mode = HWC2::PowerMode::On;
635 }
636
637 error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700638 if (error != HWC2::Error::None) {
639 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
640 error, displayId);
641 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800642 }
643 break;
644 default:
645 ALOGV("setPowerMode: Not calling HWC");
646 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700647 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800648
649 return NO_ERROR;
650}
651
Dominik Laskowski075d3172018-05-24 15:50:06 -0700652status_t HWComposer::setActiveConfig(DisplayId displayId, size_t configId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700653 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800654
655 auto& displayData = mDisplayData[displayId];
656 if (displayData.configMap.count(configId) == 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700657 LOG_DISPLAY_ERROR(displayId, ("Invalid config " + std::to_string(configId)).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800658 return BAD_INDEX;
659 }
660
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700661 auto error = displayData.hwcDisplay->setActiveConfig(displayData.configMap[configId]);
662 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800663 return NO_ERROR;
664}
665
Dominik Laskowski075d3172018-05-24 15:50:06 -0700666status_t HWComposer::setColorTransform(DisplayId displayId, const mat4& transform) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700667 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700668
669 auto& displayData = mDisplayData[displayId];
670 bool isIdentity = transform == mat4();
671 auto error = displayData.hwcDisplay->setColorTransform(transform,
672 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY :
673 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700674 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700675 return NO_ERROR;
676}
677
Dominik Laskowski075d3172018-05-24 15:50:06 -0700678void HWComposer::disconnectDisplay(DisplayId displayId) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700679 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800680 auto& displayData = mDisplayData[displayId];
681
Dan Stoza9e56aa02015-11-02 13:00:03 -0800682 // If this was a virtual display, add its slot back for reuse by future
683 // virtual displays
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700684 if (displayData.isVirtual) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700685 mFreeVirtualDisplayIds.insert(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800686 ++mRemainingHwcVirtualDisplays;
687 }
688
Dominik Laskowski7e045462018-05-30 13:02:02 -0700689 const auto hwcDisplayId = displayData.hwcDisplay->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700690 mPhysicalDisplayIdMap.erase(hwcDisplayId);
691 mDisplayData.erase(displayId);
692 mVSyncCounts.erase(displayId);
693
694 // TODO(b/74619554): Select internal/external display from remaining displays.
695 if (hwcDisplayId == mInternalHwcDisplayId) {
696 mInternalHwcDisplayId.reset();
697 } else if (hwcDisplayId == mExternalHwcDisplayId) {
698 mExternalHwcDisplayId.reset();
699 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700700
Dominik Laskowski7e045462018-05-30 13:02:02 -0700701 mHwcDevice->destroyDisplay(hwcDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800702}
703
Dominik Laskowski075d3172018-05-24 15:50:06 -0700704status_t HWComposer::setOutputBuffer(DisplayId displayId, const sp<Fence>& acquireFence,
705 const sp<GraphicBuffer>& buffer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700706 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700707 const auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800708
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700709 if (!displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700710 LOG_DISPLAY_ERROR(displayId, "Invalid operation on physical display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800711 return INVALID_OPERATION;
712 }
713
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700714 auto error = displayData.hwcDisplay->setOutputBuffer(buffer, acquireFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700715 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800716 return NO_ERROR;
717}
718
Dominik Laskowski075d3172018-05-24 15:50:06 -0700719void HWComposer::clearReleaseFences(DisplayId displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700720 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800721 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700722}
723
Dominik Laskowski075d3172018-05-24 15:50:06 -0700724status_t HWComposer::getHdrCapabilities(DisplayId displayId, HdrCapabilities* outCapabilities) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700725 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stozac4f471e2016-03-24 09:31:08 -0700726
727 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Peiyong Lin62665892018-04-16 11:07:44 -0700728 auto error = hwcDisplay->getHdrCapabilities(outCapabilities);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700729 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Peiyong Lin62665892018-04-16 11:07:44 -0700730 return NO_ERROR;
Dan Stozac4f471e2016-03-24 09:31:08 -0700731}
732
Dominik Laskowski075d3172018-05-24 15:50:06 -0700733int32_t HWComposer::getSupportedPerFrameMetadata(DisplayId displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700734 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700735 return mDisplayData.at(displayId).hwcDisplay->getSupportedPerFrameMetadata();
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700736}
737
Dominik Laskowski075d3172018-05-24 15:50:06 -0700738std::vector<ui::RenderIntent> HWComposer::getRenderIntents(DisplayId displayId,
739 ui::ColorMode colorMode) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700740 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700741
742 std::vector<ui::RenderIntent> renderIntents;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700743 auto error = mDisplayData.at(displayId).hwcDisplay->getRenderIntents(colorMode, &renderIntents);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700744 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700745 return renderIntents;
746}
747
Dominik Laskowski075d3172018-05-24 15:50:06 -0700748mat4 HWComposer::getDataspaceSaturationMatrix(DisplayId displayId, ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700749 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700750
751 mat4 matrix;
752 auto error = mDisplayData[displayId].hwcDisplay->getDataspaceSaturationMatrix(dataspace,
753 &matrix);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700754 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700755 return matrix;
756}
757
Andy McFadden4df87bd2014-04-21 18:08:54 -0700758// Converts a PixelFormat to a human-readable string. Max 11 chars.
759// (Could use a table of prefab String8 objects.)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800760/*
Andy McFadden4df87bd2014-04-21 18:08:54 -0700761static String8 getFormatStr(PixelFormat format) {
762 switch (format) {
763 case PIXEL_FORMAT_RGBA_8888: return String8("RGBA_8888");
764 case PIXEL_FORMAT_RGBX_8888: return String8("RGBx_8888");
765 case PIXEL_FORMAT_RGB_888: return String8("RGB_888");
766 case PIXEL_FORMAT_RGB_565: return String8("RGB_565");
767 case PIXEL_FORMAT_BGRA_8888: return String8("BGRA_8888");
Andy McFaddenf0058ca2014-05-20 13:28:50 -0700768 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
769 return String8("ImplDef");
Andy McFadden4df87bd2014-04-21 18:08:54 -0700770 default:
771 String8 result;
772 result.appendFormat("? %08x", format);
773 return result;
774 }
775}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800776*/
Andy McFadden4df87bd2014-04-21 18:08:54 -0700777
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800778bool HWComposer::isUsingVrComposer() const {
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800779 return getComposer()->isUsingVrComposer();
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800780}
781
Mathias Agopian74d211a2013-04-22 16:55:35 +0200782void HWComposer::dump(String8& result) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800783 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow
784 // all the state going into the layers. This is probably better done in
785 // Layer itself, but it's going to take a bit of work to get there.
786 result.append(mHwcDevice->dump().c_str());
Mathias Agopian83727852010-09-23 18:13:21 -0700787}
788
Dominik Laskowski075d3172018-05-24 15:50:06 -0700789std::optional<DisplayId> HWComposer::toPhysicalDisplayId(hwc2_display_t hwcDisplayId) const {
790 if (const auto it = mPhysicalDisplayIdMap.find(hwcDisplayId);
791 it != mPhysicalDisplayIdMap.end()) {
792 return it->second;
793 }
794 return {};
795}
796
797std::optional<hwc2_display_t> HWComposer::fromPhysicalDisplayId(DisplayId displayId) const {
798 if (const auto it = mDisplayData.find(displayId);
799 it != mDisplayData.end() && !it->second.isVirtual) {
800 return it->second.hwcDisplay->getId();
801 }
802 return {};
803}
804
805std::optional<DisplayIdentificationInfo> HWComposer::onHotplugConnect(hwc2_display_t hwcDisplayId) {
806 if (isUsingVrComposer() && mInternalHwcDisplayId) {
807 ALOGE("Ignoring connection of external display %" PRIu64 " in VR mode", hwcDisplayId);
Steven Thomas6e8f7062017-11-22 14:15:29 -0800808 return {};
809 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700810
811 uint8_t port;
812 DisplayIdentificationData data;
813 const bool hasMultiDisplaySupport = getDisplayIdentificationData(hwcDisplayId, &port, &data);
814
815 if (mPhysicalDisplayIdMap.empty()) {
816 mHasMultiDisplaySupport = hasMultiDisplaySupport;
817 ALOGI("Switching to %s multi-display mode",
818 hasMultiDisplaySupport ? "generalized" : "legacy");
819 } else if (mHasMultiDisplaySupport && !hasMultiDisplaySupport) {
820 ALOGE("Ignoring connection of display %" PRIu64 " without identification data",
821 hwcDisplayId);
822 return {};
823 }
824
825 std::optional<DisplayIdentificationInfo> info;
826
827 if (mHasMultiDisplaySupport) {
828 info = parseDisplayIdentificationData(port, data);
829 ALOGE_IF(!info, "Failed to parse identification data for display %" PRIu64, hwcDisplayId);
830 } else if (mInternalHwcDisplayId && mExternalHwcDisplayId) {
831 ALOGE("Ignoring connection of tertiary display %" PRIu64, hwcDisplayId);
832 return {};
833 } else {
834 ALOGW_IF(hasMultiDisplaySupport, "Ignoring identification data for display %" PRIu64,
835 hwcDisplayId);
836 port = mInternalHwcDisplayId ? HWC_DISPLAY_EXTERNAL : HWC_DISPLAY_PRIMARY;
837 }
838
839 if (!mInternalHwcDisplayId) {
840 mInternalHwcDisplayId = hwcDisplayId;
841 } else if (!mExternalHwcDisplayId) {
842 mExternalHwcDisplayId = hwcDisplayId;
843 }
844
845 if (info) return info;
846
847 return DisplayIdentificationInfo{getFallbackDisplayId(port),
848 hwcDisplayId == mInternalHwcDisplayId ? "Internal display"
849 : "External display"};
Steven Thomas6e8f7062017-11-22 14:15:29 -0800850}
851
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700852} // namespace android