blob: 4e98b883c7e0ffa9f27ea49f610b4bc699bb5fb6 [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) \
61 ALOGE("%s failed for display %d: %s", __FUNCTION__, displayId, msg)
62
63#define LOG_HWC_ERROR(what, error, displayId) \
64 ALOGE("%s: %s failed for display %d: %s (%d)", __FUNCTION__, what, displayId, \
65 to_string(error).c_str(), static_cast<int32_t>(error))
66
67#define RETURN_IF_INVALID_DISPLAY(displayId, ...) \
68 do { \
69 if (!isValidDisplay(displayId)) { \
70 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
Lloyd Piquea822d522017-12-20 16:42:57 -080095HWComposer::~HWComposer() = default;
Dan Stoza9e56aa02015-11-02 13:00:03 -080096
Steven Thomas94e35b92017-07-26 18:48:28 -070097void HWComposer::registerCallback(HWC2::ComposerCallback* callback,
98 int32_t sequenceId) {
99 mHwcDevice->registerCallback(callback, sequenceId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800100}
101
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700102bool HWComposer::getDisplayIdentificationData(hwc2_display_t hwcDisplayId, uint8_t* outPort,
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700103 DisplayIdentificationData* outData) const {
Dominik Laskowski0954b1d2018-06-13 14:58:49 -0700104 const auto error = mHwcDevice->getDisplayIdentificationData(hwcDisplayId, outPort, outData);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700105 if (error != HWC2::Error::None) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700106 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, to_string(error).c_str());
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700107 return false;
108 }
109 return true;
110}
111
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700112bool HWComposer::hasCapability(HWC2::Capability capability) const
113{
114 return mHwcDevice->getCapabilities().count(capability) > 0;
115}
116
Dan Stoza9e56aa02015-11-02 13:00:03 -0800117bool HWComposer::isValidDisplay(int32_t displayId) const {
118 return static_cast<size_t>(displayId) < mDisplayData.size() &&
119 mDisplayData[displayId].hwcDisplay;
120}
121
122void HWComposer::validateChange(HWC2::Composition from, HWC2::Composition to) {
123 bool valid = true;
124 switch (from) {
125 case HWC2::Composition::Client:
126 valid = false;
127 break;
128 case HWC2::Composition::Device:
129 case HWC2::Composition::SolidColor:
130 valid = (to == HWC2::Composition::Client);
131 break;
132 case HWC2::Composition::Cursor:
133 case HWC2::Composition::Sideband:
134 valid = (to == HWC2::Composition::Client ||
135 to == HWC2::Composition::Device);
136 break;
137 default:
138 break;
139 }
140
141 if (!valid) {
142 ALOGE("Invalid layer type change: %s --> %s", to_string(from).c_str(),
143 to_string(to).c_str());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700144 }
145}
146
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700147std::optional<DisplayId> HWComposer::onHotplug(hwc2_display_t hwcDisplayId, int32_t displayType,
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700148 HWC2::Connection connection) {
Lloyd Pique715a2c12017-12-14 17:18:08 -0800149 if (displayType >= HWC_NUM_PHYSICAL_DISPLAY_TYPES) {
150 ALOGE("Invalid display type of %d", displayType);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700151 return {};
Lloyd Pique715a2c12017-12-14 17:18:08 -0800152 }
153
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700154 ALOGV("hotplug: %" PRIu64 ", %s %s", hwcDisplayId,
155 displayType == DisplayDevice::DISPLAY_PRIMARY ? "primary" : "external",
156 to_string(connection).c_str());
157 mHwcDevice->onHotplug(hwcDisplayId, connection);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700158
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700159 std::optional<DisplayId> displayId;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700160
161 uint8_t port;
162 DisplayIdentificationData data;
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700163 if (getDisplayIdentificationData(hwcDisplayId, &port, &data)) {
164 displayId = generateDisplayId(port, data);
165 ALOGE_IF(!displayId, "Failed to generate stable ID for display %" PRIu64, hwcDisplayId);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700166 }
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 Laskowskia2edf612018-06-01 13:15:16 -0700171 mDisplayData[displayType].hwcDisplay = mHwcDevice->getDisplayById(hwcDisplayId);
172 mHwcDisplaySlots[hwcDisplayId] = displayType;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700173 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700174
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700175 return displayId;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700176}
177
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700178bool HWComposer::onVsync(hwc2_display_t hwcDisplayId, int64_t timestamp, int32_t* outDisplayId) {
179 const auto it = mHwcDisplaySlots.find(hwcDisplayId);
180 if (it == mHwcDisplaySlots.end()) {
181 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, "Invalid 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 Laskowskic1f18f62018-06-13 15:17:55 -0700185 const int32_t displayId = it->second;
186 RETURN_IF_INVALID_DISPLAY(displayId, false);
187
188 const auto& displayData = mDisplayData[displayId];
189 if (displayData.isVirtual) {
190 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700191 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700192 }
193
Dan Stoza9e56aa02015-11-02 13:00:03 -0800194 {
195 Mutex::Autolock _l(mLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700196
Dan Stoza9e56aa02015-11-02 13:00:03 -0800197 // There have been reports of HWCs that signal several vsync events
198 // with the same timestamp when turning the display off and on. This
199 // is a bug in the HWC implementation, but filter the extra events
200 // out here so they don't cause havoc downstream.
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700201 if (timestamp == mLastHwVSync[displayId]) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800202 ALOGW("Ignoring duplicate VSYNC event from HWC (t=%" PRId64 ")",
203 timestamp);
Steven Thomas94e35b92017-07-26 18:48:28 -0700204 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800205 }
206
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700207 mLastHwVSync[displayId] = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700208 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800209
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700210 if (outDisplayId) {
211 *outDisplayId = displayId;
Steven Thomas94e35b92017-07-26 18:48:28 -0700212 }
213
Dan Stoza9e56aa02015-11-02 13:00:03 -0800214 char tag[16];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700215 snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", displayId);
216 ATRACE_INT(tag, ++mVSyncCounts[displayId] & 1);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800217
Steven Thomas94e35b92017-07-26 18:48:28 -0700218 return true;
Jesse Hall1c569c42013-04-05 13:44:52 -0700219}
220
Dan Stoza9e56aa02015-11-02 13:00:03 -0800221status_t HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700222 ui::PixelFormat* format, int32_t *outId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800223 if (mRemainingHwcVirtualDisplays == 0) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700224 ALOGE("%s: No remaining virtual displays", __FUNCTION__);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700225 return NO_MEMORY;
226 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700227
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800228 if (SurfaceFlinger::maxVirtualDisplaySize != 0 &&
229 (width > SurfaceFlinger::maxVirtualDisplaySize ||
230 height > SurfaceFlinger::maxVirtualDisplaySize)) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700231 ALOGE("%s: Display size %ux%u exceeds maximum dimension of %" PRIu64, __FUNCTION__, width,
232 height, SurfaceFlinger::maxVirtualDisplaySize);
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800233 return INVALID_OPERATION;
234 }
235
Steven Thomas94e35b92017-07-26 18:48:28 -0700236 HWC2::Display* display;
Dan Stoza5cf424b2016-05-20 14:02:39 -0700237 auto error = mHwcDevice->createVirtualDisplay(width, height, format,
238 &display);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800239 if (error != HWC2::Error::None) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700240 ALOGE("%s: Failed to create HWC virtual display", __FUNCTION__);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800241 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700242 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800243
244 size_t displaySlot = 0;
245 if (!mFreeDisplaySlots.empty()) {
246 displaySlot = *mFreeDisplaySlots.begin();
247 mFreeDisplaySlots.erase(displaySlot);
248 } else if (mDisplayData.size() < INT32_MAX) {
249 // Don't bother allocating a slot larger than we can return
250 displaySlot = mDisplayData.size();
251 mDisplayData.resize(displaySlot + 1);
252 } else {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700253 ALOGE("%s: Unable to allocate a display slot", __FUNCTION__);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800254 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700255 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800256
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700257 auto& displayData = mDisplayData[displaySlot];
258 displayData.hwcDisplay = display;
259 displayData.isVirtual = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800260
261 --mRemainingHwcVirtualDisplays;
262 *outId = static_cast<int32_t>(displaySlot);
263
Mathias Agopiane60b0682012-08-21 23:34:09 -0700264 return NO_ERROR;
265}
266
Steven Thomas94e35b92017-07-26 18:48:28 -0700267HWC2::Layer* HWComposer::createLayer(int32_t displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700268 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
269
Dan Stoza9e56aa02015-11-02 13:00:03 -0800270 auto display = mDisplayData[displayId].hwcDisplay;
Steven Thomas94e35b92017-07-26 18:48:28 -0700271 HWC2::Layer* layer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800272 auto error = display->createLayer(&layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700273 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800274 return layer;
275}
276
Steven Thomas94e35b92017-07-26 18:48:28 -0700277void HWComposer::destroyLayer(int32_t displayId, HWC2::Layer* layer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700278 RETURN_IF_INVALID_DISPLAY(displayId);
279
Steven Thomas94e35b92017-07-26 18:48:28 -0700280 auto display = mDisplayData[displayId].hwcDisplay;
281 auto error = display->destroyLayer(layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700282 RETURN_IF_HWC_ERROR(error, displayId);
Steven Thomas94e35b92017-07-26 18:48:28 -0700283}
284
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800285nsecs_t HWComposer::getRefreshTimestamp(int32_t displayId) const {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700286 // this returns the last refresh timestamp.
287 // if the last one is not available, we estimate it based on
288 // the refresh period and whatever closest timestamp we have.
289 Mutex::Autolock _l(mLock);
290 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800291 auto vsyncPeriod = getActiveConfig(displayId)->getVsyncPeriod();
292 return now - ((now - mLastHwVSync[displayId]) % vsyncPeriod);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700293}
294
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800295bool HWComposer::isConnected(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700296 RETURN_IF_INVALID_DISPLAY(displayId, false);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800297 return mDisplayData[displayId].hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700298}
299
Dan Stoza9e56aa02015-11-02 13:00:03 -0800300std::vector<std::shared_ptr<const HWC2::Display::Config>>
301 HWComposer::getConfigs(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700302 RETURN_IF_INVALID_DISPLAY(displayId, {});
303
Dan Stoza9e56aa02015-11-02 13:00:03 -0800304 auto& displayData = mDisplayData[displayId];
305 auto configs = mDisplayData[displayId].hwcDisplay->getConfigs();
306 if (displayData.configMap.empty()) {
307 for (size_t i = 0; i < configs.size(); ++i) {
308 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700309 }
310 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800311 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700312}
313
Dan Stoza9e56aa02015-11-02 13:00:03 -0800314std::shared_ptr<const HWC2::Display::Config>
315 HWComposer::getActiveConfig(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700316 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
317
Dan Stoza9e56aa02015-11-02 13:00:03 -0800318 std::shared_ptr<const HWC2::Display::Config> config;
319 auto error = mDisplayData[displayId].hwcDisplay->getActiveConfig(&config);
320 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700321 LOG_DISPLAY_ERROR(displayId, "No active config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800322 return nullptr;
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700323 }
324
325 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
326
327 if (!config) {
328 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800329 return nullptr;
330 }
331
332 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700333}
334
Lloyd Pique3c085a02018-05-09 19:38:32 -0700335int HWComposer::getActiveConfigIndex(int32_t displayId) const {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700336 RETURN_IF_INVALID_DISPLAY(displayId, -1);
337
Lloyd Pique3c085a02018-05-09 19:38:32 -0700338 int index;
339 auto error = mDisplayData[displayId].hwcDisplay->getActiveConfigIndex(&index);
340 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700341 LOG_DISPLAY_ERROR(displayId, "No active config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700342 return -1;
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700343 }
344
345 RETURN_IF_HWC_ERROR(error, displayId, -1);
346
347 if (index < 0) {
348 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700349 return -1;
350 }
351
352 return index;
353}
354
Peiyong Linfd997e02018-03-28 15:29:00 -0700355std::vector<ui::ColorMode> HWComposer::getColorModes(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700356 RETURN_IF_INVALID_DISPLAY(displayId, {});
357
Peiyong Linfd997e02018-03-28 15:29:00 -0700358 std::vector<ui::ColorMode> modes;
Steven Thomas94e35b92017-07-26 18:48:28 -0700359 auto error = mDisplayData[displayId].hwcDisplay->getColorModes(&modes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700360 RETURN_IF_HWC_ERROR(error, displayId, {});
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600361 return modes;
362}
363
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700364status_t HWComposer::setActiveColorMode(int32_t displayId, ui::ColorMode mode,
365 ui::RenderIntent renderIntent) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700366 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Michael Wright28f24d02016-07-12 13:30:53 -0700367
368 auto& displayData = mDisplayData[displayId];
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700369 auto error = displayData.hwcDisplay->setColorMode(mode, renderIntent);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700370 RETURN_IF_HWC_ERROR_FOR(("setColorMode(" + decodeColorMode(mode) + ", " +
371 decodeRenderIntent(renderIntent) + ")")
372 .c_str(),
373 error, displayId, UNKNOWN_ERROR);
Michael Wright28f24d02016-07-12 13:30:53 -0700374
375 return NO_ERROR;
376}
377
378
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800379void HWComposer::setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700380 RETURN_IF_INVALID_DISPLAY(displayId);
381 auto& displayData = mDisplayData[displayId];
382
383 if (displayData.isVirtual) {
384 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800385 return;
386 }
387
Dan Stoza9e56aa02015-11-02 13:00:03 -0800388 // NOTE: we use our own internal lock here because we have to call
389 // into the HWC with the lock held, and we want to make sure
390 // that even if HWC blocks (which it shouldn't), it won't
391 // affect other threads.
392 Mutex::Autolock _l(mVsyncLock);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800393 if (enabled != displayData.vsyncEnabled) {
394 ATRACE_CALL();
395 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700396 RETURN_IF_HWC_ERROR(error, displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800397
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700398 displayData.vsyncEnabled = enabled;
399
400 char tag[16];
401 snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", displayId);
402 ATRACE_INT(tag, enabled == HWC2::Vsync::Enable ? 1 : 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700403 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700404}
405
Chia-I Wu06d63de2017-01-04 14:58:51 +0800406status_t HWComposer::setClientTarget(int32_t displayId, uint32_t slot,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800407 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700408 ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700409 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Jesse Hall851cfe82013-03-20 13:44:00 -0700410
Dan Stoza9e56aa02015-11-02 13:00:03 -0800411 ALOGV("setClientTarget for display %d", displayId);
412 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400413 auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700414 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Jesse Hall851cfe82013-03-20 13:44:00 -0700415 return NO_ERROR;
416}
417
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700418status_t HWComposer::prepare(DisplayDevice& display) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800419 ATRACE_CALL();
420
421 Mutex::Autolock _l(mDisplayLock);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700422 const auto displayId = display.getId();
Dan Stozaec0f7172016-07-21 11:09:40 -0700423 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
424 ALOGV("Skipping HWComposer prepare for non-HWC display");
425 return NO_ERROR;
426 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700427
428 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800429
430 auto& displayData = mDisplayData[displayId];
431 auto& hwcDisplay = displayData.hwcDisplay;
432 if (!hwcDisplay->isConnected()) {
433 return NO_ERROR;
434 }
435
436 uint32_t numTypes = 0;
437 uint32_t numRequests = 0;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700438
439 HWC2::Error error = HWC2::Error::None;
440
Chia-I Wu41b98d42017-12-11 11:04:36 -0800441 // First try to skip validate altogether when there is no client
442 // composition. When there is client composition, since we haven't
443 // rendered to the client target yet, we should not attempt to skip
444 // validate.
445 //
446 // displayData.hasClientComposition hasn't been updated for this frame.
447 // The check below is incorrect. We actually rely on HWC here to fall
448 // back to validate when there is any client layer.
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700449 displayData.validateWasSkipped = false;
Chia-I Wu41b98d42017-12-11 11:04:36 -0800450 if (!displayData.hasClientComposition) {
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700451 sp<android::Fence> outPresentFence;
452 uint32_t state = UINT32_MAX;
453 error = hwcDisplay->presentOrValidate(&numTypes, &numRequests, &outPresentFence , &state);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700454 if (error != HWC2::Error::HasChanges) {
455 RETURN_IF_HWC_ERROR_FOR("presentOrValidate", error, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700456 }
457 if (state == 1) { //Present Succeeded.
Steven Thomas94e35b92017-07-26 18:48:28 -0700458 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700459 error = hwcDisplay->getReleaseFences(&releaseFences);
460 displayData.releaseFences = std::move(releaseFences);
461 displayData.lastPresentFence = outPresentFence;
462 displayData.validateWasSkipped = true;
463 displayData.presentError = error;
464 return NO_ERROR;
465 }
466 // Present failed but Validate ran.
467 } else {
468 error = hwcDisplay->validate(&numTypes, &numRequests);
469 }
470 ALOGV("SkipValidate failed, Falling back to SLOW validate/present");
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700471 if (error != HWC2::Error::HasChanges) {
472 RETURN_IF_HWC_ERROR_FOR("validate", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800473 }
474
Steven Thomas94e35b92017-07-26 18:48:28 -0700475 std::unordered_map<HWC2::Layer*, HWC2::Composition> changedTypes;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800476 changedTypes.reserve(numTypes);
477 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700478 RETURN_IF_HWC_ERROR_FOR("getChangedCompositionTypes", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800479
480 displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0);
Steven Thomas94e35b92017-07-26 18:48:28 -0700481 std::unordered_map<HWC2::Layer*, HWC2::LayerRequest> layerRequests;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800482 layerRequests.reserve(numRequests);
483 error = hwcDisplay->getRequests(&displayData.displayRequests,
484 &layerRequests);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700485 RETURN_IF_HWC_ERROR_FOR("getRequests", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800486
487 displayData.hasClientComposition = false;
488 displayData.hasDeviceComposition = false;
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700489 for (auto& layer : display.getVisibleLayersSortedByZ()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800490 auto hwcLayer = layer->getHwcLayer(displayId);
491
492 if (changedTypes.count(hwcLayer) != 0) {
493 // We pass false so we only update our state and don't call back
494 // into the HWC device
495 validateChange(layer->getCompositionType(displayId),
496 changedTypes[hwcLayer]);
497 layer->setCompositionType(displayId, changedTypes[hwcLayer], false);
498 }
499
500 switch (layer->getCompositionType(displayId)) {
501 case HWC2::Composition::Client:
502 displayData.hasClientComposition = true;
503 break;
504 case HWC2::Composition::Device:
505 case HWC2::Composition::SolidColor:
506 case HWC2::Composition::Cursor:
507 case HWC2::Composition::Sideband:
508 displayData.hasDeviceComposition = true;
509 break;
510 default:
511 break;
512 }
513
514 if (layerRequests.count(hwcLayer) != 0 &&
515 layerRequests[hwcLayer] ==
516 HWC2::LayerRequest::ClearClientTarget) {
517 layer->setClearClientTarget(displayId, true);
518 } else {
519 if (layerRequests.count(hwcLayer) != 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700520 LOG_DISPLAY_ERROR(displayId,
521 ("Unknown layer request " + to_string(layerRequests[hwcLayer]))
522 .c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800523 }
524 layer->setClearClientTarget(displayId, false);
525 }
526 }
527
528 error = hwcDisplay->acceptChanges();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700529 RETURN_IF_HWC_ERROR_FOR("acceptChanges", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800530
531 return NO_ERROR;
532}
533
534bool HWComposer::hasDeviceComposition(int32_t displayId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700535 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
536 // Displays without a corresponding HWC display are never composed by
537 // the device
538 return false;
539 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700540
541 RETURN_IF_INVALID_DISPLAY(displayId, false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800542 return mDisplayData[displayId].hasDeviceComposition;
543}
544
Madhuri Athota88a905b2017-05-04 16:58:15 +0530545bool HWComposer::hasFlipClientTargetRequest(int32_t displayId) const {
546 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
547 // Displays without a corresponding HWC display are never composed by
548 // the device
549 return false;
550 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700551
552 RETURN_IF_INVALID_DISPLAY(displayId, false);
Madhuri Athota88a905b2017-05-04 16:58:15 +0530553 return ((static_cast<uint32_t>(mDisplayData[displayId].displayRequests) &
554 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0);
555}
556
Dan Stoza9e56aa02015-11-02 13:00:03 -0800557bool HWComposer::hasClientComposition(int32_t displayId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700558 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
559 // Displays without a corresponding HWC display are always composed by
560 // the client
561 return true;
562 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700563
564 RETURN_IF_INVALID_DISPLAY(displayId, true);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800565 return mDisplayData[displayId].hasClientComposition;
566}
567
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800568sp<Fence> HWComposer::getPresentFence(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700569 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800570 return mDisplayData[displayId].lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700571}
572
Dan Stoza9e56aa02015-11-02 13:00:03 -0800573sp<Fence> HWComposer::getLayerReleaseFence(int32_t displayId,
Steven Thomas94e35b92017-07-26 18:48:28 -0700574 HWC2::Layer* layer) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700575 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800576 auto displayFences = mDisplayData[displayId].releaseFences;
577 if (displayFences.count(layer) == 0) {
578 ALOGV("getLayerReleaseFence: Release fence not found");
579 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700580 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800581 return displayFences[layer];
Riley Andrews03414a12014-07-01 14:22:59 -0700582}
583
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800584status_t HWComposer::presentAndGetReleaseFences(int32_t displayId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800585 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700586
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700587 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700588
Dan Stoza9e56aa02015-11-02 13:00:03 -0800589 auto& displayData = mDisplayData[displayId];
590 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700591
592 if (displayData.validateWasSkipped) {
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700593 // explicitly flush all pending commands
594 auto error = mHwcDevice->flushCommands();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700595 RETURN_IF_HWC_ERROR_FOR("flushCommands", error, displayId, UNKNOWN_ERROR);
596 RETURN_IF_HWC_ERROR_FOR("present", displayData.presentError, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700597 return NO_ERROR;
598 }
599
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800600 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700601 RETURN_IF_HWC_ERROR_FOR("present", error, displayId, UNKNOWN_ERROR);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700602
Steven Thomas94e35b92017-07-26 18:48:28 -0700603 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800604 error = hwcDisplay->getReleaseFences(&releaseFences);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700605 RETURN_IF_HWC_ERROR_FOR("getReleaseFences", error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800606
607 displayData.releaseFences = std::move(releaseFences);
608
609 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700610}
611
Dan Stoza9e56aa02015-11-02 13:00:03 -0800612status_t HWComposer::setPowerMode(int32_t displayId, int32_t intMode) {
613 ALOGV("setPowerMode(%d, %d)", displayId, intMode);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700614 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
615
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700616 const auto& displayData = mDisplayData[displayId];
617 if (displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700618 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
619 return INVALID_OPERATION;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800620 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700621
Dan Stoza9e56aa02015-11-02 13:00:03 -0800622 auto mode = static_cast<HWC2::PowerMode>(intMode);
623 if (mode == HWC2::PowerMode::Off) {
624 setVsyncEnabled(displayId, HWC2::Vsync::Disable);
625 }
626
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700627 auto& hwcDisplay = displayData.hwcDisplay;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800628 switch (mode) {
629 case HWC2::PowerMode::Off:
630 case HWC2::PowerMode::On:
631 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
632 {
633 auto error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700634 if (error != HWC2::Error::None) {
635 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
636 error, displayId);
637 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700638 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800639 break;
640 case HWC2::PowerMode::Doze:
641 case HWC2::PowerMode::DozeSuspend:
642 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
643 {
644 bool supportsDoze = false;
645 auto error = hwcDisplay->supportsDoze(&supportsDoze);
Peiyong Lin306e4992018-05-07 16:18:22 -0700646 if (error != HWC2::Error::None) {
647 LOG_HWC_ERROR("supportsDoze", error, displayId);
648 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700649
Dan Stoza9e56aa02015-11-02 13:00:03 -0800650 if (!supportsDoze) {
651 mode = HWC2::PowerMode::On;
652 }
653
654 error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700655 if (error != HWC2::Error::None) {
656 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
657 error, displayId);
658 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800659 }
660 break;
661 default:
662 ALOGV("setPowerMode: Not calling HWC");
663 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700664 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800665
666 return NO_ERROR;
667}
668
669status_t HWComposer::setActiveConfig(int32_t displayId, size_t configId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700670 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800671
672 auto& displayData = mDisplayData[displayId];
673 if (displayData.configMap.count(configId) == 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700674 LOG_DISPLAY_ERROR(displayId, ("Invalid config " + std::to_string(configId)).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800675 return BAD_INDEX;
676 }
677
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700678 auto error = displayData.hwcDisplay->setActiveConfig(displayData.configMap[configId]);
679 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800680 return NO_ERROR;
681}
682
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700683status_t HWComposer::setColorTransform(int32_t displayId,
684 const mat4& transform) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700685 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700686
687 auto& displayData = mDisplayData[displayId];
688 bool isIdentity = transform == mat4();
689 auto error = displayData.hwcDisplay->setColorTransform(transform,
690 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY :
691 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700692 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700693 return NO_ERROR;
694}
695
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700696void HWComposer::disconnectDisplay(int32_t displayId) {
697 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800698 auto& displayData = mDisplayData[displayId];
699
Dan Stoza9e56aa02015-11-02 13:00:03 -0800700 // If this was a virtual display, add its slot back for reuse by future
701 // virtual displays
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700702 if (displayData.isVirtual) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800703 mFreeDisplaySlots.insert(displayId);
704 ++mRemainingHwcVirtualDisplays;
705 }
706
Dominik Laskowski7e045462018-05-30 13:02:02 -0700707 const auto hwcDisplayId = displayData.hwcDisplay->getId();
708 mHwcDisplaySlots.erase(hwcDisplayId);
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700709 displayData = DisplayData();
Steven Thomas94e35b92017-07-26 18:48:28 -0700710
Dominik Laskowski7e045462018-05-30 13:02:02 -0700711 mHwcDevice->destroyDisplay(hwcDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800712}
713
714status_t HWComposer::setOutputBuffer(int32_t displayId,
715 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700716 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700717 const auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800718
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700719 if (!displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700720 LOG_DISPLAY_ERROR(displayId, "Invalid operation on physical display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800721 return INVALID_OPERATION;
722 }
723
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700724 auto error = displayData.hwcDisplay->setOutputBuffer(buffer, acquireFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700725 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800726 return NO_ERROR;
727}
728
729void HWComposer::clearReleaseFences(int32_t displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700730 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800731 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700732}
733
Peiyong Lin62665892018-04-16 11:07:44 -0700734status_t HWComposer::getHdrCapabilities(
735 int32_t displayId, HdrCapabilities* outCapabilities) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700736 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stozac4f471e2016-03-24 09:31:08 -0700737
738 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Peiyong Lin62665892018-04-16 11:07:44 -0700739 auto error = hwcDisplay->getHdrCapabilities(outCapabilities);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700740 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Peiyong Lin62665892018-04-16 11:07:44 -0700741 return NO_ERROR;
Dan Stozac4f471e2016-03-24 09:31:08 -0700742}
743
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700744int32_t HWComposer::getSupportedPerFrameMetadata(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700745 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700746
747 int32_t supportedMetadata;
748 auto error = mDisplayData[displayId].hwcDisplay->getSupportedPerFrameMetadata(
749 &supportedMetadata);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700750 RETURN_IF_HWC_ERROR(error, displayId, 0);
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700751 return supportedMetadata;
752}
753
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700754std::vector<ui::RenderIntent> HWComposer::getRenderIntents(int32_t displayId,
755 ui::ColorMode colorMode) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700756 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700757
758 std::vector<ui::RenderIntent> renderIntents;
759 auto error = mDisplayData[displayId].hwcDisplay->getRenderIntents(colorMode, &renderIntents);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700760 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700761 return renderIntents;
762}
763
764mat4 HWComposer::getDataspaceSaturationMatrix(int32_t displayId, ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700765 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700766
767 mat4 matrix;
768 auto error = mDisplayData[displayId].hwcDisplay->getDataspaceSaturationMatrix(dataspace,
769 &matrix);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700770 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700771 return matrix;
772}
773
Andy McFadden4df87bd2014-04-21 18:08:54 -0700774// Converts a PixelFormat to a human-readable string. Max 11 chars.
775// (Could use a table of prefab String8 objects.)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800776/*
Andy McFadden4df87bd2014-04-21 18:08:54 -0700777static String8 getFormatStr(PixelFormat format) {
778 switch (format) {
779 case PIXEL_FORMAT_RGBA_8888: return String8("RGBA_8888");
780 case PIXEL_FORMAT_RGBX_8888: return String8("RGBx_8888");
781 case PIXEL_FORMAT_RGB_888: return String8("RGB_888");
782 case PIXEL_FORMAT_RGB_565: return String8("RGB_565");
783 case PIXEL_FORMAT_BGRA_8888: return String8("BGRA_8888");
Andy McFaddenf0058ca2014-05-20 13:28:50 -0700784 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
785 return String8("ImplDef");
Andy McFadden4df87bd2014-04-21 18:08:54 -0700786 default:
787 String8 result;
788 result.appendFormat("? %08x", format);
789 return result;
790 }
791}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800792*/
Andy McFadden4df87bd2014-04-21 18:08:54 -0700793
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800794bool HWComposer::isUsingVrComposer() const {
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800795 return getComposer()->isUsingVrComposer();
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800796}
797
Mathias Agopian74d211a2013-04-22 16:55:35 +0200798void HWComposer::dump(String8& result) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800799 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow
800 // all the state going into the layers. This is probably better done in
801 // Layer itself, but it's going to take a bit of work to get there.
802 result.append(mHwcDevice->dump().c_str());
Mathias Agopian83727852010-09-23 18:13:21 -0700803}
804
Steven Thomas6e8f7062017-11-22 14:15:29 -0800805std::optional<hwc2_display_t>
806HWComposer::getHwcDisplayId(int32_t displayId) const {
807 if (!isValidDisplay(displayId)) {
808 return {};
809 }
810 return mDisplayData[displayId].hwcDisplay->getId();
811}
812
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700813} // namespace android