blob: b1daf12c6cb8ec9d7e832b538ead406dcffabbf1 [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
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 -0800121bool HWComposer::isValidDisplay(int32_t displayId) const {
122 return static_cast<size_t>(displayId) < mDisplayData.size() &&
123 mDisplayData[displayId].hwcDisplay;
124}
125
126void HWComposer::validateChange(HWC2::Composition from, HWC2::Composition to) {
127 bool valid = true;
128 switch (from) {
129 case HWC2::Composition::Client:
130 valid = false;
131 break;
132 case HWC2::Composition::Device:
133 case HWC2::Composition::SolidColor:
134 valid = (to == HWC2::Composition::Client);
135 break;
136 case HWC2::Composition::Cursor:
137 case HWC2::Composition::Sideband:
138 valid = (to == HWC2::Composition::Client ||
139 to == HWC2::Composition::Device);
140 break;
141 default:
142 break;
143 }
144
145 if (!valid) {
146 ALOGE("Invalid layer type change: %s --> %s", to_string(from).c_str(),
147 to_string(to).c_str());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700148 }
149}
150
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700151std::optional<DisplayId> HWComposer::onHotplug(hwc2_display_t hwcDisplayId, int32_t displayType,
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700152 HWC2::Connection connection) {
Lloyd Pique715a2c12017-12-14 17:18:08 -0800153 if (displayType >= HWC_NUM_PHYSICAL_DISPLAY_TYPES) {
154 ALOGE("Invalid display type of %d", displayType);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700155 return {};
Lloyd Pique715a2c12017-12-14 17:18:08 -0800156 }
157
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700158 ALOGV("hotplug: %" PRIu64 ", %s %s", hwcDisplayId,
159 displayType == DisplayDevice::DISPLAY_PRIMARY ? "primary" : "external",
160 to_string(connection).c_str());
161 mHwcDevice->onHotplug(hwcDisplayId, connection);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700162
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700163 std::optional<DisplayId> displayId;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700164
Lloyd Pique438e9e72018-09-04 18:06:08 -0700165 if (connection == HWC2::Connection::Connected) {
166 uint8_t port;
167 DisplayIdentificationData data;
168 if (getDisplayIdentificationData(hwcDisplayId, &port, &data)) {
169 displayId = generateDisplayId(port, data);
170 ALOGE_IF(!displayId, "Failed to generate stable ID for display %" PRIu64, hwcDisplayId);
171 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700172 }
173
Lloyd Pique715a2c12017-12-14 17:18:08 -0800174 // Disconnect is handled through HWComposer::disconnectDisplay via
175 // SurfaceFlinger's onHotplugReceived callback handling
176 if (connection == HWC2::Connection::Connected) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700177 mDisplayData[displayType].hwcDisplay = mHwcDevice->getDisplayById(hwcDisplayId);
178 mHwcDisplaySlots[hwcDisplayId] = displayType;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700179 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700180
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700181 return displayId;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700182}
183
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700184bool HWComposer::onVsync(hwc2_display_t hwcDisplayId, int64_t timestamp, int32_t* outDisplayId) {
185 const auto it = mHwcDisplaySlots.find(hwcDisplayId);
186 if (it == mHwcDisplaySlots.end()) {
187 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, "Invalid display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700188 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700189 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700190
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700191 const int32_t displayId = it->second;
192 RETURN_IF_INVALID_DISPLAY(displayId, false);
193
194 const auto& displayData = mDisplayData[displayId];
195 if (displayData.isVirtual) {
196 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700197 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700198 }
199
Dan Stoza9e56aa02015-11-02 13:00:03 -0800200 {
201 Mutex::Autolock _l(mLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700202
Dan Stoza9e56aa02015-11-02 13:00:03 -0800203 // There have been reports of HWCs that signal several vsync events
204 // with the same timestamp when turning the display off and on. This
205 // is a bug in the HWC implementation, but filter the extra events
206 // out here so they don't cause havoc downstream.
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700207 if (timestamp == mLastHwVSync[displayId]) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800208 ALOGW("Ignoring duplicate VSYNC event from HWC (t=%" PRId64 ")",
209 timestamp);
Steven Thomas94e35b92017-07-26 18:48:28 -0700210 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800211 }
212
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700213 mLastHwVSync[displayId] = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700214 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800215
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700216 if (outDisplayId) {
217 *outDisplayId = displayId;
Steven Thomas94e35b92017-07-26 18:48:28 -0700218 }
219
Dan Stoza9e56aa02015-11-02 13:00:03 -0800220 char tag[16];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700221 snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", displayId);
222 ATRACE_INT(tag, ++mVSyncCounts[displayId] & 1);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800223
Steven Thomas94e35b92017-07-26 18:48:28 -0700224 return true;
Jesse Hall1c569c42013-04-05 13:44:52 -0700225}
226
Dan Stoza9e56aa02015-11-02 13:00:03 -0800227status_t HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700228 ui::PixelFormat* format, int32_t *outId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800229 if (mRemainingHwcVirtualDisplays == 0) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700230 ALOGE("%s: No remaining virtual displays", __FUNCTION__);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700231 return NO_MEMORY;
232 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700233
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800234 if (SurfaceFlinger::maxVirtualDisplaySize != 0 &&
235 (width > SurfaceFlinger::maxVirtualDisplaySize ||
236 height > SurfaceFlinger::maxVirtualDisplaySize)) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700237 ALOGE("%s: Display size %ux%u exceeds maximum dimension of %" PRIu64, __FUNCTION__, width,
238 height, SurfaceFlinger::maxVirtualDisplaySize);
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800239 return INVALID_OPERATION;
240 }
241
Steven Thomas94e35b92017-07-26 18:48:28 -0700242 HWC2::Display* display;
Dan Stoza5cf424b2016-05-20 14:02:39 -0700243 auto error = mHwcDevice->createVirtualDisplay(width, height, format,
244 &display);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800245 if (error != HWC2::Error::None) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700246 ALOGE("%s: Failed to create HWC virtual display", __FUNCTION__);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800247 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700248 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800249
250 size_t displaySlot = 0;
251 if (!mFreeDisplaySlots.empty()) {
252 displaySlot = *mFreeDisplaySlots.begin();
253 mFreeDisplaySlots.erase(displaySlot);
254 } else if (mDisplayData.size() < INT32_MAX) {
255 // Don't bother allocating a slot larger than we can return
256 displaySlot = mDisplayData.size();
257 mDisplayData.resize(displaySlot + 1);
258 } else {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700259 ALOGE("%s: Unable to allocate a display slot", __FUNCTION__);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800260 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700261 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800262
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700263 auto& displayData = mDisplayData[displaySlot];
264 displayData.hwcDisplay = display;
265 displayData.isVirtual = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800266
267 --mRemainingHwcVirtualDisplays;
268 *outId = static_cast<int32_t>(displaySlot);
269
Mathias Agopiane60b0682012-08-21 23:34:09 -0700270 return NO_ERROR;
271}
272
Steven Thomas94e35b92017-07-26 18:48:28 -0700273HWC2::Layer* HWComposer::createLayer(int32_t displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700274 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
275
Dan Stoza9e56aa02015-11-02 13:00:03 -0800276 auto display = mDisplayData[displayId].hwcDisplay;
Steven Thomas94e35b92017-07-26 18:48:28 -0700277 HWC2::Layer* layer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800278 auto error = display->createLayer(&layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700279 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800280 return layer;
281}
282
Steven Thomas94e35b92017-07-26 18:48:28 -0700283void HWComposer::destroyLayer(int32_t displayId, HWC2::Layer* layer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700284 RETURN_IF_INVALID_DISPLAY(displayId);
285
Steven Thomas94e35b92017-07-26 18:48:28 -0700286 auto display = mDisplayData[displayId].hwcDisplay;
287 auto error = display->destroyLayer(layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700288 RETURN_IF_HWC_ERROR(error, displayId);
Steven Thomas94e35b92017-07-26 18:48:28 -0700289}
290
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800291nsecs_t HWComposer::getRefreshTimestamp(int32_t displayId) const {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700292 // this returns the last refresh timestamp.
293 // if the last one is not available, we estimate it based on
294 // the refresh period and whatever closest timestamp we have.
295 Mutex::Autolock _l(mLock);
296 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800297 auto vsyncPeriod = getActiveConfig(displayId)->getVsyncPeriod();
298 return now - ((now - mLastHwVSync[displayId]) % vsyncPeriod);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700299}
300
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800301bool HWComposer::isConnected(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700302 RETURN_IF_INVALID_DISPLAY(displayId, false);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800303 return mDisplayData[displayId].hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700304}
305
Dan Stoza9e56aa02015-11-02 13:00:03 -0800306std::vector<std::shared_ptr<const HWC2::Display::Config>>
307 HWComposer::getConfigs(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700308 RETURN_IF_INVALID_DISPLAY(displayId, {});
309
Dan Stoza9e56aa02015-11-02 13:00:03 -0800310 auto& displayData = mDisplayData[displayId];
311 auto configs = mDisplayData[displayId].hwcDisplay->getConfigs();
312 if (displayData.configMap.empty()) {
313 for (size_t i = 0; i < configs.size(); ++i) {
314 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700315 }
316 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800317 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700318}
319
Dan Stoza9e56aa02015-11-02 13:00:03 -0800320std::shared_ptr<const HWC2::Display::Config>
321 HWComposer::getActiveConfig(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700322 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
323
Dan Stoza9e56aa02015-11-02 13:00:03 -0800324 std::shared_ptr<const HWC2::Display::Config> config;
325 auto error = mDisplayData[displayId].hwcDisplay->getActiveConfig(&config);
326 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700327 LOG_DISPLAY_ERROR(displayId, "No active config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800328 return nullptr;
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700329 }
330
331 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
332
333 if (!config) {
334 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800335 return nullptr;
336 }
337
338 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700339}
340
Lloyd Pique3c085a02018-05-09 19:38:32 -0700341int HWComposer::getActiveConfigIndex(int32_t displayId) const {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700342 RETURN_IF_INVALID_DISPLAY(displayId, -1);
343
Lloyd Pique3c085a02018-05-09 19:38:32 -0700344 int index;
345 auto error = mDisplayData[displayId].hwcDisplay->getActiveConfigIndex(&index);
346 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700347 LOG_DISPLAY_ERROR(displayId, "No active config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700348 return -1;
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700349 }
350
351 RETURN_IF_HWC_ERROR(error, displayId, -1);
352
353 if (index < 0) {
354 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700355 return -1;
356 }
357
358 return index;
359}
360
Peiyong Linfd997e02018-03-28 15:29:00 -0700361std::vector<ui::ColorMode> HWComposer::getColorModes(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700362 RETURN_IF_INVALID_DISPLAY(displayId, {});
363
Peiyong Linfd997e02018-03-28 15:29:00 -0700364 std::vector<ui::ColorMode> modes;
Steven Thomas94e35b92017-07-26 18:48:28 -0700365 auto error = mDisplayData[displayId].hwcDisplay->getColorModes(&modes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700366 RETURN_IF_HWC_ERROR(error, displayId, {});
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600367 return modes;
368}
369
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700370status_t HWComposer::setActiveColorMode(int32_t displayId, ui::ColorMode mode,
371 ui::RenderIntent renderIntent) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700372 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Michael Wright28f24d02016-07-12 13:30:53 -0700373
374 auto& displayData = mDisplayData[displayId];
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700375 auto error = displayData.hwcDisplay->setColorMode(mode, renderIntent);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700376 RETURN_IF_HWC_ERROR_FOR(("setColorMode(" + decodeColorMode(mode) + ", " +
377 decodeRenderIntent(renderIntent) + ")")
378 .c_str(),
379 error, displayId, UNKNOWN_ERROR);
Michael Wright28f24d02016-07-12 13:30:53 -0700380
381 return NO_ERROR;
382}
383
384
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800385void HWComposer::setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700386 RETURN_IF_INVALID_DISPLAY(displayId);
387 auto& displayData = mDisplayData[displayId];
388
389 if (displayData.isVirtual) {
390 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800391 return;
392 }
393
Dan Stoza9e56aa02015-11-02 13:00:03 -0800394 // NOTE: we use our own internal lock here because we have to call
395 // into the HWC with the lock held, and we want to make sure
396 // that even if HWC blocks (which it shouldn't), it won't
397 // affect other threads.
398 Mutex::Autolock _l(mVsyncLock);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800399 if (enabled != displayData.vsyncEnabled) {
400 ATRACE_CALL();
401 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700402 RETURN_IF_HWC_ERROR(error, displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800403
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700404 displayData.vsyncEnabled = enabled;
405
406 char tag[16];
407 snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", displayId);
408 ATRACE_INT(tag, enabled == HWC2::Vsync::Enable ? 1 : 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700409 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700410}
411
Chia-I Wu06d63de2017-01-04 14:58:51 +0800412status_t HWComposer::setClientTarget(int32_t displayId, uint32_t slot,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800413 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700414 ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700415 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Jesse Hall851cfe82013-03-20 13:44:00 -0700416
Dan Stoza9e56aa02015-11-02 13:00:03 -0800417 ALOGV("setClientTarget for display %d", displayId);
418 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400419 auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700420 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Jesse Hall851cfe82013-03-20 13:44:00 -0700421 return NO_ERROR;
422}
423
David Sodmanfb95bcc2017-12-22 15:45:30 -0800424status_t HWComposer::prepare(DisplayDevice& display,
425 std::vector<CompositionInfo>& compositionData) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800426 ATRACE_CALL();
427
428 Mutex::Autolock _l(mDisplayLock);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700429 const auto displayId = display.getId();
Dan Stozaec0f7172016-07-21 11:09:40 -0700430 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
431 ALOGV("Skipping HWComposer prepare for non-HWC display");
432 return NO_ERROR;
433 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700434
435 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800436
437 auto& displayData = mDisplayData[displayId];
438 auto& hwcDisplay = displayData.hwcDisplay;
439 if (!hwcDisplay->isConnected()) {
440 return NO_ERROR;
441 }
442
443 uint32_t numTypes = 0;
444 uint32_t numRequests = 0;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700445
446 HWC2::Error error = HWC2::Error::None;
447
Chia-I Wu41b98d42017-12-11 11:04:36 -0800448 // First try to skip validate altogether when there is no client
449 // composition. When there is client composition, since we haven't
450 // rendered to the client target yet, we should not attempt to skip
451 // validate.
452 //
453 // displayData.hasClientComposition hasn't been updated for this frame.
454 // The check below is incorrect. We actually rely on HWC here to fall
455 // back to validate when there is any client layer.
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700456 displayData.validateWasSkipped = false;
Chia-I Wu41b98d42017-12-11 11:04:36 -0800457 if (!displayData.hasClientComposition) {
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700458 sp<android::Fence> outPresentFence;
459 uint32_t state = UINT32_MAX;
460 error = hwcDisplay->presentOrValidate(&numTypes, &numRequests, &outPresentFence , &state);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700461 if (error != HWC2::Error::HasChanges) {
462 RETURN_IF_HWC_ERROR_FOR("presentOrValidate", error, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700463 }
464 if (state == 1) { //Present Succeeded.
Steven Thomas94e35b92017-07-26 18:48:28 -0700465 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700466 error = hwcDisplay->getReleaseFences(&releaseFences);
467 displayData.releaseFences = std::move(releaseFences);
468 displayData.lastPresentFence = outPresentFence;
469 displayData.validateWasSkipped = true;
470 displayData.presentError = error;
471 return NO_ERROR;
472 }
473 // Present failed but Validate ran.
474 } else {
475 error = hwcDisplay->validate(&numTypes, &numRequests);
476 }
477 ALOGV("SkipValidate failed, Falling back to SLOW validate/present");
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700478 if (error != HWC2::Error::HasChanges) {
479 RETURN_IF_HWC_ERROR_FOR("validate", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800480 }
481
Steven Thomas94e35b92017-07-26 18:48:28 -0700482 std::unordered_map<HWC2::Layer*, HWC2::Composition> changedTypes;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800483 changedTypes.reserve(numTypes);
484 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700485 RETURN_IF_HWC_ERROR_FOR("getChangedCompositionTypes", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800486
487 displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0);
Steven Thomas94e35b92017-07-26 18:48:28 -0700488 std::unordered_map<HWC2::Layer*, HWC2::LayerRequest> layerRequests;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800489 layerRequests.reserve(numRequests);
490 error = hwcDisplay->getRequests(&displayData.displayRequests,
491 &layerRequests);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700492 RETURN_IF_HWC_ERROR_FOR("getRequests", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800493
494 displayData.hasClientComposition = false;
495 displayData.hasDeviceComposition = false;
David Sodmanfb95bcc2017-12-22 15:45:30 -0800496 for (auto& compositionInfo : compositionData) {
497 auto hwcLayer = compositionInfo.hwc.hwcLayer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800498
David Sodmanfb95bcc2017-12-22 15:45:30 -0800499 if (changedTypes.count(&*hwcLayer) != 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800500 // We pass false so we only update our state and don't call back
501 // into the HWC device
David Sodmanfb95bcc2017-12-22 15:45:30 -0800502 validateChange(compositionInfo.compositionType,
503 changedTypes[&*hwcLayer]);
504 compositionInfo.compositionType = changedTypes[&*hwcLayer];
David Sodmanc1498e62018-09-12 14:36:26 -0700505 compositionInfo.layer->mLayer->setCompositionType(displayId,
506 compositionInfo.compositionType, false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800507 }
508
David Sodmanfb95bcc2017-12-22 15:45:30 -0800509 switch (compositionInfo.compositionType) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800510 case HWC2::Composition::Client:
511 displayData.hasClientComposition = true;
512 break;
513 case HWC2::Composition::Device:
514 case HWC2::Composition::SolidColor:
515 case HWC2::Composition::Cursor:
516 case HWC2::Composition::Sideband:
517 displayData.hasDeviceComposition = true;
518 break;
519 default:
520 break;
521 }
522
David Sodmanfb95bcc2017-12-22 15:45:30 -0800523 if (layerRequests.count(&*hwcLayer) != 0 &&
524 layerRequests[&*hwcLayer] ==
Dan Stoza9e56aa02015-11-02 13:00:03 -0800525 HWC2::LayerRequest::ClearClientTarget) {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800526 compositionInfo.hwc.clearClientTarget = true;
David Sodmanc1498e62018-09-12 14:36:26 -0700527 compositionInfo.layer->mLayer->setClearClientTarget(displayId, true);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800528 } else {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800529 if (layerRequests.count(&*hwcLayer) != 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700530 LOG_DISPLAY_ERROR(displayId,
David Sodmanfb95bcc2017-12-22 15:45:30 -0800531 ("Unknown layer request " + to_string(layerRequests[&*hwcLayer]))
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700532 .c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800533 }
David Sodmanfb95bcc2017-12-22 15:45:30 -0800534 compositionInfo.hwc.clearClientTarget = false;
David Sodmanc1498e62018-09-12 14:36:26 -0700535 compositionInfo.layer->mLayer->setClearClientTarget(displayId, false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800536 }
537 }
538
539 error = hwcDisplay->acceptChanges();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700540 RETURN_IF_HWC_ERROR_FOR("acceptChanges", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800541
542 return NO_ERROR;
543}
544
545bool HWComposer::hasDeviceComposition(int32_t displayId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700546 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);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800553 return mDisplayData[displayId].hasDeviceComposition;
554}
555
Madhuri Athota88a905b2017-05-04 16:58:15 +0530556bool HWComposer::hasFlipClientTargetRequest(int32_t displayId) const {
557 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
558 // Displays without a corresponding HWC display are never composed by
559 // the device
560 return false;
561 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700562
563 RETURN_IF_INVALID_DISPLAY(displayId, false);
Madhuri Athota88a905b2017-05-04 16:58:15 +0530564 return ((static_cast<uint32_t>(mDisplayData[displayId].displayRequests) &
565 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0);
566}
567
Dan Stoza9e56aa02015-11-02 13:00:03 -0800568bool HWComposer::hasClientComposition(int32_t displayId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700569 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
570 // Displays without a corresponding HWC display are always composed by
571 // the client
572 return true;
573 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700574
575 RETURN_IF_INVALID_DISPLAY(displayId, true);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800576 return mDisplayData[displayId].hasClientComposition;
577}
578
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800579sp<Fence> HWComposer::getPresentFence(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700580 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800581 return mDisplayData[displayId].lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700582}
583
Dan Stoza9e56aa02015-11-02 13:00:03 -0800584sp<Fence> HWComposer::getLayerReleaseFence(int32_t displayId,
Steven Thomas94e35b92017-07-26 18:48:28 -0700585 HWC2::Layer* layer) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700586 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800587 auto displayFences = mDisplayData[displayId].releaseFences;
588 if (displayFences.count(layer) == 0) {
589 ALOGV("getLayerReleaseFence: Release fence not found");
590 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700591 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800592 return displayFences[layer];
Riley Andrews03414a12014-07-01 14:22:59 -0700593}
594
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800595status_t HWComposer::presentAndGetReleaseFences(int32_t displayId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800596 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700597
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700598 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700599
Dan Stoza9e56aa02015-11-02 13:00:03 -0800600 auto& displayData = mDisplayData[displayId];
601 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700602
603 if (displayData.validateWasSkipped) {
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700604 // explicitly flush all pending commands
605 auto error = mHwcDevice->flushCommands();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700606 RETURN_IF_HWC_ERROR_FOR("flushCommands", error, displayId, UNKNOWN_ERROR);
607 RETURN_IF_HWC_ERROR_FOR("present", displayData.presentError, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700608 return NO_ERROR;
609 }
610
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800611 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700612 RETURN_IF_HWC_ERROR_FOR("present", error, displayId, UNKNOWN_ERROR);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700613
Steven Thomas94e35b92017-07-26 18:48:28 -0700614 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800615 error = hwcDisplay->getReleaseFences(&releaseFences);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700616 RETURN_IF_HWC_ERROR_FOR("getReleaseFences", error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800617
618 displayData.releaseFences = std::move(releaseFences);
619
620 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700621}
622
Dan Stoza9e56aa02015-11-02 13:00:03 -0800623status_t HWComposer::setPowerMode(int32_t displayId, int32_t intMode) {
624 ALOGV("setPowerMode(%d, %d)", displayId, intMode);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700625 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
626
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700627 const auto& displayData = mDisplayData[displayId];
628 if (displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700629 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
630 return INVALID_OPERATION;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800631 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700632
Dan Stoza9e56aa02015-11-02 13:00:03 -0800633 auto mode = static_cast<HWC2::PowerMode>(intMode);
634 if (mode == HWC2::PowerMode::Off) {
635 setVsyncEnabled(displayId, HWC2::Vsync::Disable);
636 }
637
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700638 auto& hwcDisplay = displayData.hwcDisplay;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800639 switch (mode) {
640 case HWC2::PowerMode::Off:
641 case HWC2::PowerMode::On:
642 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
643 {
644 auto error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700645 if (error != HWC2::Error::None) {
646 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
647 error, displayId);
648 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700649 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800650 break;
651 case HWC2::PowerMode::Doze:
652 case HWC2::PowerMode::DozeSuspend:
653 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
654 {
655 bool supportsDoze = false;
656 auto error = hwcDisplay->supportsDoze(&supportsDoze);
Peiyong Lin306e4992018-05-07 16:18:22 -0700657 if (error != HWC2::Error::None) {
658 LOG_HWC_ERROR("supportsDoze", error, displayId);
659 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700660
Dan Stoza9e56aa02015-11-02 13:00:03 -0800661 if (!supportsDoze) {
662 mode = HWC2::PowerMode::On;
663 }
664
665 error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700666 if (error != HWC2::Error::None) {
667 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
668 error, displayId);
669 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800670 }
671 break;
672 default:
673 ALOGV("setPowerMode: Not calling HWC");
674 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700675 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800676
677 return NO_ERROR;
678}
679
680status_t HWComposer::setActiveConfig(int32_t displayId, size_t configId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700681 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800682
683 auto& displayData = mDisplayData[displayId];
684 if (displayData.configMap.count(configId) == 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700685 LOG_DISPLAY_ERROR(displayId, ("Invalid config " + std::to_string(configId)).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800686 return BAD_INDEX;
687 }
688
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700689 auto error = displayData.hwcDisplay->setActiveConfig(displayData.configMap[configId]);
690 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800691 return NO_ERROR;
692}
693
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700694status_t HWComposer::setColorTransform(int32_t displayId,
695 const mat4& transform) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700696 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700697
698 auto& displayData = mDisplayData[displayId];
699 bool isIdentity = transform == mat4();
700 auto error = displayData.hwcDisplay->setColorTransform(transform,
701 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY :
702 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700703 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700704 return NO_ERROR;
705}
706
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700707void HWComposer::disconnectDisplay(int32_t displayId) {
708 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800709 auto& displayData = mDisplayData[displayId];
710
Dan Stoza9e56aa02015-11-02 13:00:03 -0800711 // If this was a virtual display, add its slot back for reuse by future
712 // virtual displays
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700713 if (displayData.isVirtual) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800714 mFreeDisplaySlots.insert(displayId);
715 ++mRemainingHwcVirtualDisplays;
716 }
717
Dominik Laskowski7e045462018-05-30 13:02:02 -0700718 const auto hwcDisplayId = displayData.hwcDisplay->getId();
719 mHwcDisplaySlots.erase(hwcDisplayId);
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700720 displayData = DisplayData();
Steven Thomas94e35b92017-07-26 18:48:28 -0700721
Dominik Laskowski7e045462018-05-30 13:02:02 -0700722 mHwcDevice->destroyDisplay(hwcDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800723}
724
725status_t HWComposer::setOutputBuffer(int32_t displayId,
726 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700727 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700728 const auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800729
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700730 if (!displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700731 LOG_DISPLAY_ERROR(displayId, "Invalid operation on physical display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800732 return INVALID_OPERATION;
733 }
734
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700735 auto error = displayData.hwcDisplay->setOutputBuffer(buffer, acquireFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700736 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800737 return NO_ERROR;
738}
739
740void HWComposer::clearReleaseFences(int32_t displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700741 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800742 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700743}
744
Peiyong Lin62665892018-04-16 11:07:44 -0700745status_t HWComposer::getHdrCapabilities(
746 int32_t displayId, HdrCapabilities* outCapabilities) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700747 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stozac4f471e2016-03-24 09:31:08 -0700748
749 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Peiyong Lin62665892018-04-16 11:07:44 -0700750 auto error = hwcDisplay->getHdrCapabilities(outCapabilities);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700751 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Peiyong Lin62665892018-04-16 11:07:44 -0700752 return NO_ERROR;
Dan Stozac4f471e2016-03-24 09:31:08 -0700753}
754
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700755int32_t HWComposer::getSupportedPerFrameMetadata(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700756 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Chia-I Wud7e01d72018-06-21 13:39:09 +0800757 return mDisplayData[displayId].hwcDisplay->getSupportedPerFrameMetadata();
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700758}
759
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700760std::vector<ui::RenderIntent> HWComposer::getRenderIntents(int32_t displayId,
761 ui::ColorMode colorMode) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700762 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700763
764 std::vector<ui::RenderIntent> renderIntents;
765 auto error = mDisplayData[displayId].hwcDisplay->getRenderIntents(colorMode, &renderIntents);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700766 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700767 return renderIntents;
768}
769
770mat4 HWComposer::getDataspaceSaturationMatrix(int32_t displayId, ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700771 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700772
773 mat4 matrix;
774 auto error = mDisplayData[displayId].hwcDisplay->getDataspaceSaturationMatrix(dataspace,
775 &matrix);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700776 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700777 return matrix;
778}
779
Andy McFadden4df87bd2014-04-21 18:08:54 -0700780// Converts a PixelFormat to a human-readable string. Max 11 chars.
781// (Could use a table of prefab String8 objects.)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800782/*
Andy McFadden4df87bd2014-04-21 18:08:54 -0700783static String8 getFormatStr(PixelFormat format) {
784 switch (format) {
785 case PIXEL_FORMAT_RGBA_8888: return String8("RGBA_8888");
786 case PIXEL_FORMAT_RGBX_8888: return String8("RGBx_8888");
787 case PIXEL_FORMAT_RGB_888: return String8("RGB_888");
788 case PIXEL_FORMAT_RGB_565: return String8("RGB_565");
789 case PIXEL_FORMAT_BGRA_8888: return String8("BGRA_8888");
Andy McFaddenf0058ca2014-05-20 13:28:50 -0700790 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
791 return String8("ImplDef");
Andy McFadden4df87bd2014-04-21 18:08:54 -0700792 default:
793 String8 result;
794 result.appendFormat("? %08x", format);
795 return result;
796 }
797}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800798*/
Andy McFadden4df87bd2014-04-21 18:08:54 -0700799
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800800bool HWComposer::isUsingVrComposer() const {
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800801 return getComposer()->isUsingVrComposer();
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800802}
803
Mathias Agopian74d211a2013-04-22 16:55:35 +0200804void HWComposer::dump(String8& result) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800805 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow
806 // all the state going into the layers. This is probably better done in
807 // Layer itself, but it's going to take a bit of work to get there.
808 result.append(mHwcDevice->dump().c_str());
Mathias Agopian83727852010-09-23 18:13:21 -0700809}
810
Steven Thomas6e8f7062017-11-22 14:15:29 -0800811std::optional<hwc2_display_t>
812HWComposer::getHwcDisplayId(int32_t displayId) const {
813 if (!isValidDisplay(displayId)) {
814 return {};
815 }
816 return mDisplayData[displayId].hwcDisplay->getId();
817}
818
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700819} // namespace android