blob: 6b56ca4e368fe2d750ccca7904c58cdd413b4472 [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) {
Chia-I Wud0aff9d2018-06-21 13:39:09 +0800106 if (error != HWC2::Error::Unsupported) {
107 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, to_string(error).c_str());
108 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700109 return false;
110 }
111 return true;
112}
113
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700114bool HWComposer::hasCapability(HWC2::Capability capability) const
115{
116 return mHwcDevice->getCapabilities().count(capability) > 0;
117}
118
Dan Stoza9e56aa02015-11-02 13:00:03 -0800119bool HWComposer::isValidDisplay(int32_t displayId) const {
120 return static_cast<size_t>(displayId) < mDisplayData.size() &&
121 mDisplayData[displayId].hwcDisplay;
122}
123
124void HWComposer::validateChange(HWC2::Composition from, HWC2::Composition to) {
125 bool valid = true;
126 switch (from) {
127 case HWC2::Composition::Client:
128 valid = false;
129 break;
130 case HWC2::Composition::Device:
131 case HWC2::Composition::SolidColor:
132 valid = (to == HWC2::Composition::Client);
133 break;
134 case HWC2::Composition::Cursor:
135 case HWC2::Composition::Sideband:
136 valid = (to == HWC2::Composition::Client ||
137 to == HWC2::Composition::Device);
138 break;
139 default:
140 break;
141 }
142
143 if (!valid) {
144 ALOGE("Invalid layer type change: %s --> %s", to_string(from).c_str(),
145 to_string(to).c_str());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700146 }
147}
148
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700149std::optional<DisplayId> HWComposer::onHotplug(hwc2_display_t hwcDisplayId, int32_t displayType,
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700150 HWC2::Connection connection) {
Lloyd Pique715a2c12017-12-14 17:18:08 -0800151 if (displayType >= HWC_NUM_PHYSICAL_DISPLAY_TYPES) {
152 ALOGE("Invalid display type of %d", displayType);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700153 return {};
Lloyd Pique715a2c12017-12-14 17:18:08 -0800154 }
155
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700156 ALOGV("hotplug: %" PRIu64 ", %s %s", hwcDisplayId,
157 displayType == DisplayDevice::DISPLAY_PRIMARY ? "primary" : "external",
158 to_string(connection).c_str());
159 mHwcDevice->onHotplug(hwcDisplayId, connection);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700160
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700161 std::optional<DisplayId> displayId;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700162
Lloyd Pique438e9e72018-09-04 18:06:08 -0700163 if (connection == HWC2::Connection::Connected) {
164 uint8_t port;
165 DisplayIdentificationData data;
166 if (getDisplayIdentificationData(hwcDisplayId, &port, &data)) {
167 displayId = generateDisplayId(port, data);
168 ALOGE_IF(!displayId, "Failed to generate stable ID for display %" PRIu64, hwcDisplayId);
169 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700170 }
171
Lloyd Pique715a2c12017-12-14 17:18:08 -0800172 // Disconnect is handled through HWComposer::disconnectDisplay via
173 // SurfaceFlinger's onHotplugReceived callback handling
174 if (connection == HWC2::Connection::Connected) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700175 mDisplayData[displayType].hwcDisplay = mHwcDevice->getDisplayById(hwcDisplayId);
176 mHwcDisplaySlots[hwcDisplayId] = displayType;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700177 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700178
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700179 return displayId;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700180}
181
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700182bool HWComposer::onVsync(hwc2_display_t hwcDisplayId, int64_t timestamp, int32_t* outDisplayId) {
183 const auto it = mHwcDisplaySlots.find(hwcDisplayId);
184 if (it == mHwcDisplaySlots.end()) {
185 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, "Invalid display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700186 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700187 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700188
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700189 const int32_t displayId = it->second;
190 RETURN_IF_INVALID_DISPLAY(displayId, false);
191
192 const auto& displayData = mDisplayData[displayId];
193 if (displayData.isVirtual) {
194 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700195 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700196 }
197
Dan Stoza9e56aa02015-11-02 13:00:03 -0800198 {
199 Mutex::Autolock _l(mLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700200
Dan Stoza9e56aa02015-11-02 13:00:03 -0800201 // There have been reports of HWCs that signal several vsync events
202 // with the same timestamp when turning the display off and on. This
203 // is a bug in the HWC implementation, but filter the extra events
204 // out here so they don't cause havoc downstream.
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700205 if (timestamp == mLastHwVSync[displayId]) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800206 ALOGW("Ignoring duplicate VSYNC event from HWC (t=%" PRId64 ")",
207 timestamp);
Steven Thomas94e35b92017-07-26 18:48:28 -0700208 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800209 }
210
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700211 mLastHwVSync[displayId] = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700212 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800213
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700214 if (outDisplayId) {
215 *outDisplayId = displayId;
Steven Thomas94e35b92017-07-26 18:48:28 -0700216 }
217
Dan Stoza9e56aa02015-11-02 13:00:03 -0800218 char tag[16];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700219 snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", displayId);
220 ATRACE_INT(tag, ++mVSyncCounts[displayId] & 1);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800221
Steven Thomas94e35b92017-07-26 18:48:28 -0700222 return true;
Jesse Hall1c569c42013-04-05 13:44:52 -0700223}
224
Dan Stoza9e56aa02015-11-02 13:00:03 -0800225status_t HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700226 ui::PixelFormat* format, int32_t *outId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800227 if (mRemainingHwcVirtualDisplays == 0) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700228 ALOGE("%s: No remaining virtual displays", __FUNCTION__);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700229 return NO_MEMORY;
230 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700231
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800232 if (SurfaceFlinger::maxVirtualDisplaySize != 0 &&
233 (width > SurfaceFlinger::maxVirtualDisplaySize ||
234 height > SurfaceFlinger::maxVirtualDisplaySize)) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700235 ALOGE("%s: Display size %ux%u exceeds maximum dimension of %" PRIu64, __FUNCTION__, width,
236 height, SurfaceFlinger::maxVirtualDisplaySize);
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800237 return INVALID_OPERATION;
238 }
239
Steven Thomas94e35b92017-07-26 18:48:28 -0700240 HWC2::Display* display;
Dan Stoza5cf424b2016-05-20 14:02:39 -0700241 auto error = mHwcDevice->createVirtualDisplay(width, height, format,
242 &display);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800243 if (error != HWC2::Error::None) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700244 ALOGE("%s: Failed to create HWC virtual display", __FUNCTION__);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800245 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700246 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800247
248 size_t displaySlot = 0;
249 if (!mFreeDisplaySlots.empty()) {
250 displaySlot = *mFreeDisplaySlots.begin();
251 mFreeDisplaySlots.erase(displaySlot);
252 } else if (mDisplayData.size() < INT32_MAX) {
253 // Don't bother allocating a slot larger than we can return
254 displaySlot = mDisplayData.size();
255 mDisplayData.resize(displaySlot + 1);
256 } else {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700257 ALOGE("%s: Unable to allocate a display slot", __FUNCTION__);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800258 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700259 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800260
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700261 auto& displayData = mDisplayData[displaySlot];
262 displayData.hwcDisplay = display;
263 displayData.isVirtual = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800264
265 --mRemainingHwcVirtualDisplays;
266 *outId = static_cast<int32_t>(displaySlot);
267
Mathias Agopiane60b0682012-08-21 23:34:09 -0700268 return NO_ERROR;
269}
270
Steven Thomas94e35b92017-07-26 18:48:28 -0700271HWC2::Layer* HWComposer::createLayer(int32_t displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700272 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
273
Dan Stoza9e56aa02015-11-02 13:00:03 -0800274 auto display = mDisplayData[displayId].hwcDisplay;
Steven Thomas94e35b92017-07-26 18:48:28 -0700275 HWC2::Layer* layer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800276 auto error = display->createLayer(&layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700277 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800278 return layer;
279}
280
Steven Thomas94e35b92017-07-26 18:48:28 -0700281void HWComposer::destroyLayer(int32_t displayId, HWC2::Layer* layer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700282 RETURN_IF_INVALID_DISPLAY(displayId);
283
Steven Thomas94e35b92017-07-26 18:48:28 -0700284 auto display = mDisplayData[displayId].hwcDisplay;
285 auto error = display->destroyLayer(layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700286 RETURN_IF_HWC_ERROR(error, displayId);
Steven Thomas94e35b92017-07-26 18:48:28 -0700287}
288
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800289nsecs_t HWComposer::getRefreshTimestamp(int32_t displayId) const {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700290 // this returns the last refresh timestamp.
291 // if the last one is not available, we estimate it based on
292 // the refresh period and whatever closest timestamp we have.
293 Mutex::Autolock _l(mLock);
294 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800295 auto vsyncPeriod = getActiveConfig(displayId)->getVsyncPeriod();
296 return now - ((now - mLastHwVSync[displayId]) % vsyncPeriod);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700297}
298
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800299bool HWComposer::isConnected(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700300 RETURN_IF_INVALID_DISPLAY(displayId, false);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800301 return mDisplayData[displayId].hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700302}
303
Dan Stoza9e56aa02015-11-02 13:00:03 -0800304std::vector<std::shared_ptr<const HWC2::Display::Config>>
305 HWComposer::getConfigs(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700306 RETURN_IF_INVALID_DISPLAY(displayId, {});
307
Dan Stoza9e56aa02015-11-02 13:00:03 -0800308 auto& displayData = mDisplayData[displayId];
309 auto configs = mDisplayData[displayId].hwcDisplay->getConfigs();
310 if (displayData.configMap.empty()) {
311 for (size_t i = 0; i < configs.size(); ++i) {
312 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700313 }
314 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800315 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700316}
317
Dan Stoza9e56aa02015-11-02 13:00:03 -0800318std::shared_ptr<const HWC2::Display::Config>
319 HWComposer::getActiveConfig(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700320 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
321
Dan Stoza9e56aa02015-11-02 13:00:03 -0800322 std::shared_ptr<const HWC2::Display::Config> config;
323 auto error = mDisplayData[displayId].hwcDisplay->getActiveConfig(&config);
324 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700325 LOG_DISPLAY_ERROR(displayId, "No active config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800326 return nullptr;
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700327 }
328
329 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
330
331 if (!config) {
332 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800333 return nullptr;
334 }
335
336 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700337}
338
Lloyd Pique3c085a02018-05-09 19:38:32 -0700339int HWComposer::getActiveConfigIndex(int32_t displayId) const {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700340 RETURN_IF_INVALID_DISPLAY(displayId, -1);
341
Lloyd Pique3c085a02018-05-09 19:38:32 -0700342 int index;
343 auto error = mDisplayData[displayId].hwcDisplay->getActiveConfigIndex(&index);
344 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700345 LOG_DISPLAY_ERROR(displayId, "No active config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700346 return -1;
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700347 }
348
349 RETURN_IF_HWC_ERROR(error, displayId, -1);
350
351 if (index < 0) {
352 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700353 return -1;
354 }
355
356 return index;
357}
358
Peiyong Linfd997e02018-03-28 15:29:00 -0700359std::vector<ui::ColorMode> HWComposer::getColorModes(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700360 RETURN_IF_INVALID_DISPLAY(displayId, {});
361
Peiyong Linfd997e02018-03-28 15:29:00 -0700362 std::vector<ui::ColorMode> modes;
Steven Thomas94e35b92017-07-26 18:48:28 -0700363 auto error = mDisplayData[displayId].hwcDisplay->getColorModes(&modes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700364 RETURN_IF_HWC_ERROR(error, displayId, {});
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600365 return modes;
366}
367
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700368status_t HWComposer::setActiveColorMode(int32_t displayId, ui::ColorMode mode,
369 ui::RenderIntent renderIntent) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700370 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Michael Wright28f24d02016-07-12 13:30:53 -0700371
372 auto& displayData = mDisplayData[displayId];
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700373 auto error = displayData.hwcDisplay->setColorMode(mode, renderIntent);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700374 RETURN_IF_HWC_ERROR_FOR(("setColorMode(" + decodeColorMode(mode) + ", " +
375 decodeRenderIntent(renderIntent) + ")")
376 .c_str(),
377 error, displayId, UNKNOWN_ERROR);
Michael Wright28f24d02016-07-12 13:30:53 -0700378
379 return NO_ERROR;
380}
381
382
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800383void HWComposer::setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700384 RETURN_IF_INVALID_DISPLAY(displayId);
385 auto& displayData = mDisplayData[displayId];
386
387 if (displayData.isVirtual) {
388 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800389 return;
390 }
391
Dan Stoza9e56aa02015-11-02 13:00:03 -0800392 // NOTE: we use our own internal lock here because we have to call
393 // into the HWC with the lock held, and we want to make sure
394 // that even if HWC blocks (which it shouldn't), it won't
395 // affect other threads.
396 Mutex::Autolock _l(mVsyncLock);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800397 if (enabled != displayData.vsyncEnabled) {
398 ATRACE_CALL();
399 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700400 RETURN_IF_HWC_ERROR(error, displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800401
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700402 displayData.vsyncEnabled = enabled;
403
404 char tag[16];
405 snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", displayId);
406 ATRACE_INT(tag, enabled == HWC2::Vsync::Enable ? 1 : 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700407 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700408}
409
Chia-I Wu06d63de2017-01-04 14:58:51 +0800410status_t HWComposer::setClientTarget(int32_t displayId, uint32_t slot,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800411 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700412 ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700413 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Jesse Hall851cfe82013-03-20 13:44:00 -0700414
Dan Stoza9e56aa02015-11-02 13:00:03 -0800415 ALOGV("setClientTarget for display %d", displayId);
416 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400417 auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700418 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Jesse Hall851cfe82013-03-20 13:44:00 -0700419 return NO_ERROR;
420}
421
David Sodmanfb95bcc2017-12-22 15:45:30 -0800422status_t HWComposer::prepare(DisplayDevice& display,
423 std::vector<CompositionInfo>& compositionData) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800424 ATRACE_CALL();
425
426 Mutex::Autolock _l(mDisplayLock);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700427 const auto displayId = display.getId();
Dan Stozaec0f7172016-07-21 11:09:40 -0700428 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
429 ALOGV("Skipping HWComposer prepare for non-HWC display");
430 return NO_ERROR;
431 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700432
433 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800434
435 auto& displayData = mDisplayData[displayId];
436 auto& hwcDisplay = displayData.hwcDisplay;
437 if (!hwcDisplay->isConnected()) {
438 return NO_ERROR;
439 }
440
441 uint32_t numTypes = 0;
442 uint32_t numRequests = 0;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700443
444 HWC2::Error error = HWC2::Error::None;
445
Chia-I Wu41b98d42017-12-11 11:04:36 -0800446 // First try to skip validate altogether when there is no client
447 // composition. When there is client composition, since we haven't
448 // rendered to the client target yet, we should not attempt to skip
449 // validate.
450 //
451 // displayData.hasClientComposition hasn't been updated for this frame.
452 // The check below is incorrect. We actually rely on HWC here to fall
453 // back to validate when there is any client layer.
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700454 displayData.validateWasSkipped = false;
Chia-I Wu41b98d42017-12-11 11:04:36 -0800455 if (!displayData.hasClientComposition) {
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700456 sp<android::Fence> outPresentFence;
457 uint32_t state = UINT32_MAX;
458 error = hwcDisplay->presentOrValidate(&numTypes, &numRequests, &outPresentFence , &state);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700459 if (error != HWC2::Error::HasChanges) {
460 RETURN_IF_HWC_ERROR_FOR("presentOrValidate", error, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700461 }
462 if (state == 1) { //Present Succeeded.
Steven Thomas94e35b92017-07-26 18:48:28 -0700463 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700464 error = hwcDisplay->getReleaseFences(&releaseFences);
465 displayData.releaseFences = std::move(releaseFences);
466 displayData.lastPresentFence = outPresentFence;
467 displayData.validateWasSkipped = true;
468 displayData.presentError = error;
469 return NO_ERROR;
470 }
471 // Present failed but Validate ran.
472 } else {
473 error = hwcDisplay->validate(&numTypes, &numRequests);
474 }
475 ALOGV("SkipValidate failed, Falling back to SLOW validate/present");
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700476 if (error != HWC2::Error::HasChanges) {
477 RETURN_IF_HWC_ERROR_FOR("validate", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800478 }
479
Steven Thomas94e35b92017-07-26 18:48:28 -0700480 std::unordered_map<HWC2::Layer*, HWC2::Composition> changedTypes;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800481 changedTypes.reserve(numTypes);
482 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700483 RETURN_IF_HWC_ERROR_FOR("getChangedCompositionTypes", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800484
485 displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0);
Steven Thomas94e35b92017-07-26 18:48:28 -0700486 std::unordered_map<HWC2::Layer*, HWC2::LayerRequest> layerRequests;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800487 layerRequests.reserve(numRequests);
488 error = hwcDisplay->getRequests(&displayData.displayRequests,
489 &layerRequests);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700490 RETURN_IF_HWC_ERROR_FOR("getRequests", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800491
492 displayData.hasClientComposition = false;
493 displayData.hasDeviceComposition = false;
David Sodmanfb95bcc2017-12-22 15:45:30 -0800494 for (auto& compositionInfo : compositionData) {
495 auto hwcLayer = compositionInfo.hwc.hwcLayer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800496
David Sodmanfb95bcc2017-12-22 15:45:30 -0800497 if (changedTypes.count(&*hwcLayer) != 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800498 // We pass false so we only update our state and don't call back
499 // into the HWC device
David Sodmanfb95bcc2017-12-22 15:45:30 -0800500 validateChange(compositionInfo.compositionType,
501 changedTypes[&*hwcLayer]);
502 compositionInfo.compositionType = changedTypes[&*hwcLayer];
David Sodmanc1498e62018-09-12 14:36:26 -0700503 compositionInfo.layer->mLayer->setCompositionType(displayId,
504 compositionInfo.compositionType, false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800505 }
506
David Sodmanfb95bcc2017-12-22 15:45:30 -0800507 switch (compositionInfo.compositionType) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800508 case HWC2::Composition::Client:
509 displayData.hasClientComposition = true;
510 break;
511 case HWC2::Composition::Device:
512 case HWC2::Composition::SolidColor:
513 case HWC2::Composition::Cursor:
514 case HWC2::Composition::Sideband:
515 displayData.hasDeviceComposition = true;
516 break;
517 default:
518 break;
519 }
520
David Sodmanfb95bcc2017-12-22 15:45:30 -0800521 if (layerRequests.count(&*hwcLayer) != 0 &&
522 layerRequests[&*hwcLayer] ==
Dan Stoza9e56aa02015-11-02 13:00:03 -0800523 HWC2::LayerRequest::ClearClientTarget) {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800524 compositionInfo.hwc.clearClientTarget = true;
David Sodmanc1498e62018-09-12 14:36:26 -0700525 compositionInfo.layer->mLayer->setClearClientTarget(displayId, true);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800526 } else {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800527 if (layerRequests.count(&*hwcLayer) != 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700528 LOG_DISPLAY_ERROR(displayId,
David Sodmanfb95bcc2017-12-22 15:45:30 -0800529 ("Unknown layer request " + to_string(layerRequests[&*hwcLayer]))
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700530 .c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800531 }
David Sodmanfb95bcc2017-12-22 15:45:30 -0800532 compositionInfo.hwc.clearClientTarget = false;
David Sodmanc1498e62018-09-12 14:36:26 -0700533 compositionInfo.layer->mLayer->setClearClientTarget(displayId, false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800534 }
535 }
536
537 error = hwcDisplay->acceptChanges();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700538 RETURN_IF_HWC_ERROR_FOR("acceptChanges", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800539
540 return NO_ERROR;
541}
542
543bool HWComposer::hasDeviceComposition(int32_t displayId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700544 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
545 // Displays without a corresponding HWC display are never composed by
546 // the device
547 return false;
548 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700549
550 RETURN_IF_INVALID_DISPLAY(displayId, false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800551 return mDisplayData[displayId].hasDeviceComposition;
552}
553
Madhuri Athota88a905b2017-05-04 16:58:15 +0530554bool HWComposer::hasFlipClientTargetRequest(int32_t displayId) const {
555 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
556 // Displays without a corresponding HWC display are never composed by
557 // the device
558 return false;
559 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700560
561 RETURN_IF_INVALID_DISPLAY(displayId, false);
Madhuri Athota88a905b2017-05-04 16:58:15 +0530562 return ((static_cast<uint32_t>(mDisplayData[displayId].displayRequests) &
563 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0);
564}
565
Dan Stoza9e56aa02015-11-02 13:00:03 -0800566bool HWComposer::hasClientComposition(int32_t displayId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700567 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
568 // Displays without a corresponding HWC display are always composed by
569 // the client
570 return true;
571 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700572
573 RETURN_IF_INVALID_DISPLAY(displayId, true);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800574 return mDisplayData[displayId].hasClientComposition;
575}
576
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800577sp<Fence> HWComposer::getPresentFence(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700578 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800579 return mDisplayData[displayId].lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700580}
581
Dan Stoza9e56aa02015-11-02 13:00:03 -0800582sp<Fence> HWComposer::getLayerReleaseFence(int32_t displayId,
Steven Thomas94e35b92017-07-26 18:48:28 -0700583 HWC2::Layer* layer) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700584 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800585 auto displayFences = mDisplayData[displayId].releaseFences;
586 if (displayFences.count(layer) == 0) {
587 ALOGV("getLayerReleaseFence: Release fence not found");
588 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700589 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800590 return displayFences[layer];
Riley Andrews03414a12014-07-01 14:22:59 -0700591}
592
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800593status_t HWComposer::presentAndGetReleaseFences(int32_t displayId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800594 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700595
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700596 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700597
Dan Stoza9e56aa02015-11-02 13:00:03 -0800598 auto& displayData = mDisplayData[displayId];
599 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700600
601 if (displayData.validateWasSkipped) {
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700602 // explicitly flush all pending commands
603 auto error = mHwcDevice->flushCommands();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700604 RETURN_IF_HWC_ERROR_FOR("flushCommands", error, displayId, UNKNOWN_ERROR);
605 RETURN_IF_HWC_ERROR_FOR("present", displayData.presentError, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700606 return NO_ERROR;
607 }
608
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800609 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700610 RETURN_IF_HWC_ERROR_FOR("present", error, displayId, UNKNOWN_ERROR);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700611
Steven Thomas94e35b92017-07-26 18:48:28 -0700612 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800613 error = hwcDisplay->getReleaseFences(&releaseFences);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700614 RETURN_IF_HWC_ERROR_FOR("getReleaseFences", error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800615
616 displayData.releaseFences = std::move(releaseFences);
617
618 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700619}
620
Dan Stoza9e56aa02015-11-02 13:00:03 -0800621status_t HWComposer::setPowerMode(int32_t displayId, int32_t intMode) {
622 ALOGV("setPowerMode(%d, %d)", displayId, intMode);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700623 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
624
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700625 const auto& displayData = mDisplayData[displayId];
626 if (displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700627 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
628 return INVALID_OPERATION;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800629 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700630
Dan Stoza9e56aa02015-11-02 13:00:03 -0800631 auto mode = static_cast<HWC2::PowerMode>(intMode);
632 if (mode == HWC2::PowerMode::Off) {
633 setVsyncEnabled(displayId, HWC2::Vsync::Disable);
634 }
635
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700636 auto& hwcDisplay = displayData.hwcDisplay;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800637 switch (mode) {
638 case HWC2::PowerMode::Off:
639 case HWC2::PowerMode::On:
640 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
641 {
642 auto error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700643 if (error != HWC2::Error::None) {
644 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
645 error, displayId);
646 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700647 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800648 break;
649 case HWC2::PowerMode::Doze:
650 case HWC2::PowerMode::DozeSuspend:
651 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
652 {
653 bool supportsDoze = false;
654 auto error = hwcDisplay->supportsDoze(&supportsDoze);
Peiyong Lin306e4992018-05-07 16:18:22 -0700655 if (error != HWC2::Error::None) {
656 LOG_HWC_ERROR("supportsDoze", error, displayId);
657 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700658
Dan Stoza9e56aa02015-11-02 13:00:03 -0800659 if (!supportsDoze) {
660 mode = HWC2::PowerMode::On;
661 }
662
663 error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700664 if (error != HWC2::Error::None) {
665 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
666 error, displayId);
667 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800668 }
669 break;
670 default:
671 ALOGV("setPowerMode: Not calling HWC");
672 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700673 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800674
675 return NO_ERROR;
676}
677
678status_t HWComposer::setActiveConfig(int32_t displayId, size_t configId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700679 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800680
681 auto& displayData = mDisplayData[displayId];
682 if (displayData.configMap.count(configId) == 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700683 LOG_DISPLAY_ERROR(displayId, ("Invalid config " + std::to_string(configId)).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800684 return BAD_INDEX;
685 }
686
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700687 auto error = displayData.hwcDisplay->setActiveConfig(displayData.configMap[configId]);
688 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800689 return NO_ERROR;
690}
691
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700692status_t HWComposer::setColorTransform(int32_t displayId,
693 const mat4& transform) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700694 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700695
696 auto& displayData = mDisplayData[displayId];
697 bool isIdentity = transform == mat4();
698 auto error = displayData.hwcDisplay->setColorTransform(transform,
699 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY :
700 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700701 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700702 return NO_ERROR;
703}
704
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700705void HWComposer::disconnectDisplay(int32_t displayId) {
706 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800707 auto& displayData = mDisplayData[displayId];
708
Dan Stoza9e56aa02015-11-02 13:00:03 -0800709 // If this was a virtual display, add its slot back for reuse by future
710 // virtual displays
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700711 if (displayData.isVirtual) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800712 mFreeDisplaySlots.insert(displayId);
713 ++mRemainingHwcVirtualDisplays;
714 }
715
Dominik Laskowski7e045462018-05-30 13:02:02 -0700716 const auto hwcDisplayId = displayData.hwcDisplay->getId();
717 mHwcDisplaySlots.erase(hwcDisplayId);
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700718 displayData = DisplayData();
Steven Thomas94e35b92017-07-26 18:48:28 -0700719
Dominik Laskowski7e045462018-05-30 13:02:02 -0700720 mHwcDevice->destroyDisplay(hwcDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800721}
722
723status_t HWComposer::setOutputBuffer(int32_t displayId,
724 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700725 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700726 const auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800727
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700728 if (!displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700729 LOG_DISPLAY_ERROR(displayId, "Invalid operation on physical display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800730 return INVALID_OPERATION;
731 }
732
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700733 auto error = displayData.hwcDisplay->setOutputBuffer(buffer, acquireFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700734 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800735 return NO_ERROR;
736}
737
738void HWComposer::clearReleaseFences(int32_t displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700739 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800740 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700741}
742
Peiyong Lin62665892018-04-16 11:07:44 -0700743status_t HWComposer::getHdrCapabilities(
744 int32_t displayId, HdrCapabilities* outCapabilities) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700745 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stozac4f471e2016-03-24 09:31:08 -0700746
747 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Peiyong Lin62665892018-04-16 11:07:44 -0700748 auto error = hwcDisplay->getHdrCapabilities(outCapabilities);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700749 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Peiyong Lin62665892018-04-16 11:07:44 -0700750 return NO_ERROR;
Dan Stozac4f471e2016-03-24 09:31:08 -0700751}
752
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700753int32_t HWComposer::getSupportedPerFrameMetadata(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700754 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Chia-I Wud7e01d72018-06-21 13:39:09 +0800755 return mDisplayData[displayId].hwcDisplay->getSupportedPerFrameMetadata();
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700756}
757
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700758std::vector<ui::RenderIntent> HWComposer::getRenderIntents(int32_t displayId,
759 ui::ColorMode colorMode) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700760 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700761
762 std::vector<ui::RenderIntent> renderIntents;
763 auto error = mDisplayData[displayId].hwcDisplay->getRenderIntents(colorMode, &renderIntents);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700764 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700765 return renderIntents;
766}
767
768mat4 HWComposer::getDataspaceSaturationMatrix(int32_t displayId, ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700769 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700770
771 mat4 matrix;
772 auto error = mDisplayData[displayId].hwcDisplay->getDataspaceSaturationMatrix(dataspace,
773 &matrix);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700774 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700775 return matrix;
776}
777
Andy McFadden4df87bd2014-04-21 18:08:54 -0700778// Converts a PixelFormat to a human-readable string. Max 11 chars.
779// (Could use a table of prefab String8 objects.)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800780/*
Andy McFadden4df87bd2014-04-21 18:08:54 -0700781static String8 getFormatStr(PixelFormat format) {
782 switch (format) {
783 case PIXEL_FORMAT_RGBA_8888: return String8("RGBA_8888");
784 case PIXEL_FORMAT_RGBX_8888: return String8("RGBx_8888");
785 case PIXEL_FORMAT_RGB_888: return String8("RGB_888");
786 case PIXEL_FORMAT_RGB_565: return String8("RGB_565");
787 case PIXEL_FORMAT_BGRA_8888: return String8("BGRA_8888");
Andy McFaddenf0058ca2014-05-20 13:28:50 -0700788 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
789 return String8("ImplDef");
Andy McFadden4df87bd2014-04-21 18:08:54 -0700790 default:
791 String8 result;
792 result.appendFormat("? %08x", format);
793 return result;
794 }
795}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800796*/
Andy McFadden4df87bd2014-04-21 18:08:54 -0700797
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800798bool HWComposer::isUsingVrComposer() const {
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800799 return getComposer()->isUsingVrComposer();
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800800}
801
Mathias Agopian74d211a2013-04-22 16:55:35 +0200802void HWComposer::dump(String8& result) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800803 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow
804 // all the state going into the layers. This is probably better done in
805 // Layer itself, but it's going to take a bit of work to get there.
806 result.append(mHwcDevice->dump().c_str());
Mathias Agopian83727852010-09-23 18:13:21 -0700807}
808
Steven Thomas6e8f7062017-11-22 14:15:29 -0800809std::optional<hwc2_display_t>
810HWComposer::getHwcDisplayId(int32_t displayId) const {
811 if (!isValidDisplay(displayId)) {
812 return {};
813 }
814 return mDisplayData[displayId].hwcDisplay->getId();
815}
816
David Sodman44b5de02018-08-21 16:28:53 -0700817void HWComposer::setDisplayFrequencyScaleParameters(
818 HWC2::Device::FrequencyScaler frequencyScaler)
819{
820 mHwcDevice->setDisplayFrequencyScaleParameters(frequencyScaler);
821}
822
823HWC2::Device::FrequencyScaler HWComposer::getDisplayFrequencyScaleParameters()
824{
825 return mHwcDevice->getDisplayFrequencyScaleParameters();
826}
827
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700828} // namespace android