blob: 873de251edd3a3cda9147e9ea74f3c6d08da61a1 [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
163 uint8_t port;
164 DisplayIdentificationData data;
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700165 if (getDisplayIdentificationData(hwcDisplayId, &port, &data)) {
166 displayId = generateDisplayId(port, data);
167 ALOGE_IF(!displayId, "Failed to generate stable ID for display %" PRIu64, hwcDisplayId);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700168 }
169
Lloyd Pique715a2c12017-12-14 17:18:08 -0800170 // Disconnect is handled through HWComposer::disconnectDisplay via
171 // SurfaceFlinger's onHotplugReceived callback handling
172 if (connection == HWC2::Connection::Connected) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700173 mDisplayData[displayType].hwcDisplay = mHwcDevice->getDisplayById(hwcDisplayId);
174 mHwcDisplaySlots[hwcDisplayId] = displayType;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700175 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700176
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700177 return displayId;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700178}
179
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700180bool HWComposer::onVsync(hwc2_display_t hwcDisplayId, int64_t timestamp, int32_t* outDisplayId) {
181 const auto it = mHwcDisplaySlots.find(hwcDisplayId);
182 if (it == mHwcDisplaySlots.end()) {
183 LOG_HWC_DISPLAY_ERROR(hwcDisplayId, "Invalid display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700184 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700185 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700186
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700187 const int32_t displayId = it->second;
188 RETURN_IF_INVALID_DISPLAY(displayId, false);
189
190 const auto& displayData = mDisplayData[displayId];
191 if (displayData.isVirtual) {
192 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
Steven Thomas94e35b92017-07-26 18:48:28 -0700193 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700194 }
195
Dan Stoza9e56aa02015-11-02 13:00:03 -0800196 {
197 Mutex::Autolock _l(mLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700198
Dan Stoza9e56aa02015-11-02 13:00:03 -0800199 // There have been reports of HWCs that signal several vsync events
200 // with the same timestamp when turning the display off and on. This
201 // is a bug in the HWC implementation, but filter the extra events
202 // out here so they don't cause havoc downstream.
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700203 if (timestamp == mLastHwVSync[displayId]) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800204 ALOGW("Ignoring duplicate VSYNC event from HWC (t=%" PRId64 ")",
205 timestamp);
Steven Thomas94e35b92017-07-26 18:48:28 -0700206 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800207 }
208
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700209 mLastHwVSync[displayId] = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700210 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800211
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700212 if (outDisplayId) {
213 *outDisplayId = displayId;
Steven Thomas94e35b92017-07-26 18:48:28 -0700214 }
215
Dan Stoza9e56aa02015-11-02 13:00:03 -0800216 char tag[16];
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700217 snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", displayId);
218 ATRACE_INT(tag, ++mVSyncCounts[displayId] & 1);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800219
Steven Thomas94e35b92017-07-26 18:48:28 -0700220 return true;
Jesse Hall1c569c42013-04-05 13:44:52 -0700221}
222
Dan Stoza9e56aa02015-11-02 13:00:03 -0800223status_t HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700224 ui::PixelFormat* format, int32_t *outId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800225 if (mRemainingHwcVirtualDisplays == 0) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700226 ALOGE("%s: No remaining virtual displays", __FUNCTION__);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700227 return NO_MEMORY;
228 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700229
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800230 if (SurfaceFlinger::maxVirtualDisplaySize != 0 &&
231 (width > SurfaceFlinger::maxVirtualDisplaySize ||
232 height > SurfaceFlinger::maxVirtualDisplaySize)) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700233 ALOGE("%s: Display size %ux%u exceeds maximum dimension of %" PRIu64, __FUNCTION__, width,
234 height, SurfaceFlinger::maxVirtualDisplaySize);
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800235 return INVALID_OPERATION;
236 }
237
Steven Thomas94e35b92017-07-26 18:48:28 -0700238 HWC2::Display* display;
Dan Stoza5cf424b2016-05-20 14:02:39 -0700239 auto error = mHwcDevice->createVirtualDisplay(width, height, format,
240 &display);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800241 if (error != HWC2::Error::None) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700242 ALOGE("%s: Failed to create HWC virtual display", __FUNCTION__);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800243 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700244 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800245
246 size_t displaySlot = 0;
247 if (!mFreeDisplaySlots.empty()) {
248 displaySlot = *mFreeDisplaySlots.begin();
249 mFreeDisplaySlots.erase(displaySlot);
250 } else if (mDisplayData.size() < INT32_MAX) {
251 // Don't bother allocating a slot larger than we can return
252 displaySlot = mDisplayData.size();
253 mDisplayData.resize(displaySlot + 1);
254 } else {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700255 ALOGE("%s: Unable to allocate a display slot", __FUNCTION__);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800256 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700257 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800258
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700259 auto& displayData = mDisplayData[displaySlot];
260 displayData.hwcDisplay = display;
261 displayData.isVirtual = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800262
263 --mRemainingHwcVirtualDisplays;
264 *outId = static_cast<int32_t>(displaySlot);
265
Mathias Agopiane60b0682012-08-21 23:34:09 -0700266 return NO_ERROR;
267}
268
Steven Thomas94e35b92017-07-26 18:48:28 -0700269HWC2::Layer* HWComposer::createLayer(int32_t displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700270 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
271
Dan Stoza9e56aa02015-11-02 13:00:03 -0800272 auto display = mDisplayData[displayId].hwcDisplay;
Steven Thomas94e35b92017-07-26 18:48:28 -0700273 HWC2::Layer* layer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800274 auto error = display->createLayer(&layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700275 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800276 return layer;
277}
278
Steven Thomas94e35b92017-07-26 18:48:28 -0700279void HWComposer::destroyLayer(int32_t displayId, HWC2::Layer* layer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700280 RETURN_IF_INVALID_DISPLAY(displayId);
281
Steven Thomas94e35b92017-07-26 18:48:28 -0700282 auto display = mDisplayData[displayId].hwcDisplay;
283 auto error = display->destroyLayer(layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700284 RETURN_IF_HWC_ERROR(error, displayId);
Steven Thomas94e35b92017-07-26 18:48:28 -0700285}
286
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800287nsecs_t HWComposer::getRefreshTimestamp(int32_t displayId) const {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700288 // this returns the last refresh timestamp.
289 // if the last one is not available, we estimate it based on
290 // the refresh period and whatever closest timestamp we have.
291 Mutex::Autolock _l(mLock);
292 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800293 auto vsyncPeriod = getActiveConfig(displayId)->getVsyncPeriod();
294 return now - ((now - mLastHwVSync[displayId]) % vsyncPeriod);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700295}
296
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800297bool HWComposer::isConnected(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700298 RETURN_IF_INVALID_DISPLAY(displayId, false);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800299 return mDisplayData[displayId].hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700300}
301
Dan Stoza9e56aa02015-11-02 13:00:03 -0800302std::vector<std::shared_ptr<const HWC2::Display::Config>>
303 HWComposer::getConfigs(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700304 RETURN_IF_INVALID_DISPLAY(displayId, {});
305
Dan Stoza9e56aa02015-11-02 13:00:03 -0800306 auto& displayData = mDisplayData[displayId];
307 auto configs = mDisplayData[displayId].hwcDisplay->getConfigs();
308 if (displayData.configMap.empty()) {
309 for (size_t i = 0; i < configs.size(); ++i) {
310 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700311 }
312 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800313 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700314}
315
Dan Stoza9e56aa02015-11-02 13:00:03 -0800316std::shared_ptr<const HWC2::Display::Config>
317 HWComposer::getActiveConfig(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700318 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
319
Dan Stoza9e56aa02015-11-02 13:00:03 -0800320 std::shared_ptr<const HWC2::Display::Config> config;
321 auto error = mDisplayData[displayId].hwcDisplay->getActiveConfig(&config);
322 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700323 LOG_DISPLAY_ERROR(displayId, "No active config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800324 return nullptr;
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700325 }
326
327 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
328
329 if (!config) {
330 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800331 return nullptr;
332 }
333
334 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700335}
336
Lloyd Pique3c085a02018-05-09 19:38:32 -0700337int HWComposer::getActiveConfigIndex(int32_t displayId) const {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700338 RETURN_IF_INVALID_DISPLAY(displayId, -1);
339
Lloyd Pique3c085a02018-05-09 19:38:32 -0700340 int index;
341 auto error = mDisplayData[displayId].hwcDisplay->getActiveConfigIndex(&index);
342 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700343 LOG_DISPLAY_ERROR(displayId, "No active config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700344 return -1;
Dominik Laskowskif3749f82018-06-13 15:49:25 -0700345 }
346
347 RETURN_IF_HWC_ERROR(error, displayId, -1);
348
349 if (index < 0) {
350 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Lloyd Pique3c085a02018-05-09 19:38:32 -0700351 return -1;
352 }
353
354 return index;
355}
356
Peiyong Linfd997e02018-03-28 15:29:00 -0700357std::vector<ui::ColorMode> HWComposer::getColorModes(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700358 RETURN_IF_INVALID_DISPLAY(displayId, {});
359
Peiyong Linfd997e02018-03-28 15:29:00 -0700360 std::vector<ui::ColorMode> modes;
Steven Thomas94e35b92017-07-26 18:48:28 -0700361 auto error = mDisplayData[displayId].hwcDisplay->getColorModes(&modes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700362 RETURN_IF_HWC_ERROR(error, displayId, {});
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600363 return modes;
364}
365
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700366status_t HWComposer::setActiveColorMode(int32_t displayId, ui::ColorMode mode,
367 ui::RenderIntent renderIntent) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700368 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Michael Wright28f24d02016-07-12 13:30:53 -0700369
370 auto& displayData = mDisplayData[displayId];
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700371 auto error = displayData.hwcDisplay->setColorMode(mode, renderIntent);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700372 RETURN_IF_HWC_ERROR_FOR(("setColorMode(" + decodeColorMode(mode) + ", " +
373 decodeRenderIntent(renderIntent) + ")")
374 .c_str(),
375 error, displayId, UNKNOWN_ERROR);
Michael Wright28f24d02016-07-12 13:30:53 -0700376
377 return NO_ERROR;
378}
379
380
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800381void HWComposer::setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled) {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700382 RETURN_IF_INVALID_DISPLAY(displayId);
383 auto& displayData = mDisplayData[displayId];
384
385 if (displayData.isVirtual) {
386 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800387 return;
388 }
389
Dan Stoza9e56aa02015-11-02 13:00:03 -0800390 // NOTE: we use our own internal lock here because we have to call
391 // into the HWC with the lock held, and we want to make sure
392 // that even if HWC blocks (which it shouldn't), it won't
393 // affect other threads.
394 Mutex::Autolock _l(mVsyncLock);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800395 if (enabled != displayData.vsyncEnabled) {
396 ATRACE_CALL();
397 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700398 RETURN_IF_HWC_ERROR(error, displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800399
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700400 displayData.vsyncEnabled = enabled;
401
402 char tag[16];
403 snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", displayId);
404 ATRACE_INT(tag, enabled == HWC2::Vsync::Enable ? 1 : 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700405 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700406}
407
Chia-I Wu06d63de2017-01-04 14:58:51 +0800408status_t HWComposer::setClientTarget(int32_t displayId, uint32_t slot,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800409 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700410 ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700411 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Jesse Hall851cfe82013-03-20 13:44:00 -0700412
Dan Stoza9e56aa02015-11-02 13:00:03 -0800413 ALOGV("setClientTarget for display %d", displayId);
414 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400415 auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700416 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Jesse Hall851cfe82013-03-20 13:44:00 -0700417 return NO_ERROR;
418}
419
David Sodmanfb95bcc2017-12-22 15:45:30 -0800420status_t HWComposer::prepare(DisplayDevice& display,
421 std::vector<CompositionInfo>& compositionData) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800422 ATRACE_CALL();
423
424 Mutex::Autolock _l(mDisplayLock);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700425 const auto displayId = display.getId();
Dan Stozaec0f7172016-07-21 11:09:40 -0700426 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
427 ALOGV("Skipping HWComposer prepare for non-HWC display");
428 return NO_ERROR;
429 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700430
431 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800432
433 auto& displayData = mDisplayData[displayId];
434 auto& hwcDisplay = displayData.hwcDisplay;
435 if (!hwcDisplay->isConnected()) {
436 return NO_ERROR;
437 }
438
439 uint32_t numTypes = 0;
440 uint32_t numRequests = 0;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700441
442 HWC2::Error error = HWC2::Error::None;
443
Chia-I Wu41b98d42017-12-11 11:04:36 -0800444 // First try to skip validate altogether when there is no client
445 // composition. When there is client composition, since we haven't
446 // rendered to the client target yet, we should not attempt to skip
447 // validate.
448 //
449 // displayData.hasClientComposition hasn't been updated for this frame.
450 // The check below is incorrect. We actually rely on HWC here to fall
451 // back to validate when there is any client layer.
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700452 displayData.validateWasSkipped = false;
Chia-I Wu41b98d42017-12-11 11:04:36 -0800453 if (!displayData.hasClientComposition) {
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700454 sp<android::Fence> outPresentFence;
455 uint32_t state = UINT32_MAX;
456 error = hwcDisplay->presentOrValidate(&numTypes, &numRequests, &outPresentFence , &state);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700457 if (error != HWC2::Error::HasChanges) {
458 RETURN_IF_HWC_ERROR_FOR("presentOrValidate", error, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700459 }
460 if (state == 1) { //Present Succeeded.
Steven Thomas94e35b92017-07-26 18:48:28 -0700461 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700462 error = hwcDisplay->getReleaseFences(&releaseFences);
463 displayData.releaseFences = std::move(releaseFences);
464 displayData.lastPresentFence = outPresentFence;
465 displayData.validateWasSkipped = true;
466 displayData.presentError = error;
467 return NO_ERROR;
468 }
469 // Present failed but Validate ran.
470 } else {
471 error = hwcDisplay->validate(&numTypes, &numRequests);
472 }
473 ALOGV("SkipValidate failed, Falling back to SLOW validate/present");
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700474 if (error != HWC2::Error::HasChanges) {
475 RETURN_IF_HWC_ERROR_FOR("validate", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800476 }
477
Steven Thomas94e35b92017-07-26 18:48:28 -0700478 std::unordered_map<HWC2::Layer*, HWC2::Composition> changedTypes;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800479 changedTypes.reserve(numTypes);
480 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700481 RETURN_IF_HWC_ERROR_FOR("getChangedCompositionTypes", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800482
483 displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0);
Steven Thomas94e35b92017-07-26 18:48:28 -0700484 std::unordered_map<HWC2::Layer*, HWC2::LayerRequest> layerRequests;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800485 layerRequests.reserve(numRequests);
486 error = hwcDisplay->getRequests(&displayData.displayRequests,
487 &layerRequests);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700488 RETURN_IF_HWC_ERROR_FOR("getRequests", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800489
490 displayData.hasClientComposition = false;
491 displayData.hasDeviceComposition = false;
David Sodmanfb95bcc2017-12-22 15:45:30 -0800492 for (auto& compositionInfo : compositionData) {
493 auto hwcLayer = compositionInfo.hwc.hwcLayer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800494
David Sodmanfb95bcc2017-12-22 15:45:30 -0800495 if (changedTypes.count(&*hwcLayer) != 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800496 // We pass false so we only update our state and don't call back
497 // into the HWC device
David Sodmanfb95bcc2017-12-22 15:45:30 -0800498 validateChange(compositionInfo.compositionType,
499 changedTypes[&*hwcLayer]);
500 compositionInfo.compositionType = changedTypes[&*hwcLayer];
501 compositionInfo.layer->mLayer->setCompositionType(displayId, compositionInfo.compositionType, false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800502 }
503
David Sodmanfb95bcc2017-12-22 15:45:30 -0800504 switch (compositionInfo.compositionType) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800505 case HWC2::Composition::Client:
506 displayData.hasClientComposition = true;
507 break;
508 case HWC2::Composition::Device:
509 case HWC2::Composition::SolidColor:
510 case HWC2::Composition::Cursor:
511 case HWC2::Composition::Sideband:
512 displayData.hasDeviceComposition = true;
513 break;
514 default:
515 break;
516 }
517
David Sodmanfb95bcc2017-12-22 15:45:30 -0800518 if (layerRequests.count(&*hwcLayer) != 0 &&
519 layerRequests[&*hwcLayer] ==
Dan Stoza9e56aa02015-11-02 13:00:03 -0800520 HWC2::LayerRequest::ClearClientTarget) {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800521 compositionInfo.hwc.clearClientTarget = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800522 } else {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800523 if (layerRequests.count(&*hwcLayer) != 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700524 LOG_DISPLAY_ERROR(displayId,
David Sodmanfb95bcc2017-12-22 15:45:30 -0800525 ("Unknown layer request " + to_string(layerRequests[&*hwcLayer]))
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700526 .c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800527 }
David Sodmanfb95bcc2017-12-22 15:45:30 -0800528 compositionInfo.hwc.clearClientTarget = false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800529 }
530 }
531
532 error = hwcDisplay->acceptChanges();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700533 RETURN_IF_HWC_ERROR_FOR("acceptChanges", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800534
535 return NO_ERROR;
536}
537
538bool HWComposer::hasDeviceComposition(int32_t displayId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700539 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
540 // Displays without a corresponding HWC display are never composed by
541 // the device
542 return false;
543 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700544
545 RETURN_IF_INVALID_DISPLAY(displayId, false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800546 return mDisplayData[displayId].hasDeviceComposition;
547}
548
Madhuri Athota88a905b2017-05-04 16:58:15 +0530549bool HWComposer::hasFlipClientTargetRequest(int32_t displayId) const {
550 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
551 // Displays without a corresponding HWC display are never composed by
552 // the device
553 return false;
554 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700555
556 RETURN_IF_INVALID_DISPLAY(displayId, false);
Madhuri Athota88a905b2017-05-04 16:58:15 +0530557 return ((static_cast<uint32_t>(mDisplayData[displayId].displayRequests) &
558 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0);
559}
560
Dan Stoza9e56aa02015-11-02 13:00:03 -0800561bool HWComposer::hasClientComposition(int32_t displayId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700562 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
563 // Displays without a corresponding HWC display are always composed by
564 // the client
565 return true;
566 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700567
568 RETURN_IF_INVALID_DISPLAY(displayId, true);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800569 return mDisplayData[displayId].hasClientComposition;
570}
571
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800572sp<Fence> HWComposer::getPresentFence(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700573 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800574 return mDisplayData[displayId].lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700575}
576
Dan Stoza9e56aa02015-11-02 13:00:03 -0800577sp<Fence> HWComposer::getLayerReleaseFence(int32_t displayId,
Steven Thomas94e35b92017-07-26 18:48:28 -0700578 HWC2::Layer* layer) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700579 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800580 auto displayFences = mDisplayData[displayId].releaseFences;
581 if (displayFences.count(layer) == 0) {
582 ALOGV("getLayerReleaseFence: Release fence not found");
583 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700584 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800585 return displayFences[layer];
Riley Andrews03414a12014-07-01 14:22:59 -0700586}
587
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800588status_t HWComposer::presentAndGetReleaseFences(int32_t displayId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800589 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700590
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700591 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700592
Dan Stoza9e56aa02015-11-02 13:00:03 -0800593 auto& displayData = mDisplayData[displayId];
594 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700595
596 if (displayData.validateWasSkipped) {
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700597 // explicitly flush all pending commands
598 auto error = mHwcDevice->flushCommands();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700599 RETURN_IF_HWC_ERROR_FOR("flushCommands", error, displayId, UNKNOWN_ERROR);
600 RETURN_IF_HWC_ERROR_FOR("present", displayData.presentError, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700601 return NO_ERROR;
602 }
603
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800604 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700605 RETURN_IF_HWC_ERROR_FOR("present", error, displayId, UNKNOWN_ERROR);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700606
Steven Thomas94e35b92017-07-26 18:48:28 -0700607 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800608 error = hwcDisplay->getReleaseFences(&releaseFences);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700609 RETURN_IF_HWC_ERROR_FOR("getReleaseFences", error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800610
611 displayData.releaseFences = std::move(releaseFences);
612
613 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700614}
615
Dan Stoza9e56aa02015-11-02 13:00:03 -0800616status_t HWComposer::setPowerMode(int32_t displayId, int32_t intMode) {
617 ALOGV("setPowerMode(%d, %d)", displayId, intMode);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700618 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
619
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700620 const auto& displayData = mDisplayData[displayId];
621 if (displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700622 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
623 return INVALID_OPERATION;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800624 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700625
Dan Stoza9e56aa02015-11-02 13:00:03 -0800626 auto mode = static_cast<HWC2::PowerMode>(intMode);
627 if (mode == HWC2::PowerMode::Off) {
628 setVsyncEnabled(displayId, HWC2::Vsync::Disable);
629 }
630
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700631 auto& hwcDisplay = displayData.hwcDisplay;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800632 switch (mode) {
633 case HWC2::PowerMode::Off:
634 case HWC2::PowerMode::On:
635 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
636 {
637 auto error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700638 if (error != HWC2::Error::None) {
639 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
640 error, displayId);
641 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700642 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800643 break;
644 case HWC2::PowerMode::Doze:
645 case HWC2::PowerMode::DozeSuspend:
646 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
647 {
648 bool supportsDoze = false;
649 auto error = hwcDisplay->supportsDoze(&supportsDoze);
Peiyong Lin306e4992018-05-07 16:18:22 -0700650 if (error != HWC2::Error::None) {
651 LOG_HWC_ERROR("supportsDoze", error, displayId);
652 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700653
Dan Stoza9e56aa02015-11-02 13:00:03 -0800654 if (!supportsDoze) {
655 mode = HWC2::PowerMode::On;
656 }
657
658 error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700659 if (error != HWC2::Error::None) {
660 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
661 error, displayId);
662 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800663 }
664 break;
665 default:
666 ALOGV("setPowerMode: Not calling HWC");
667 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700668 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800669
670 return NO_ERROR;
671}
672
673status_t HWComposer::setActiveConfig(int32_t displayId, size_t configId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700674 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800675
676 auto& displayData = mDisplayData[displayId];
677 if (displayData.configMap.count(configId) == 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700678 LOG_DISPLAY_ERROR(displayId, ("Invalid config " + std::to_string(configId)).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800679 return BAD_INDEX;
680 }
681
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700682 auto error = displayData.hwcDisplay->setActiveConfig(displayData.configMap[configId]);
683 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800684 return NO_ERROR;
685}
686
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700687status_t HWComposer::setColorTransform(int32_t displayId,
688 const mat4& transform) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700689 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700690
691 auto& displayData = mDisplayData[displayId];
692 bool isIdentity = transform == mat4();
693 auto error = displayData.hwcDisplay->setColorTransform(transform,
694 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY :
695 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700696 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700697 return NO_ERROR;
698}
699
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700700void HWComposer::disconnectDisplay(int32_t displayId) {
701 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800702 auto& displayData = mDisplayData[displayId];
703
Dan Stoza9e56aa02015-11-02 13:00:03 -0800704 // If this was a virtual display, add its slot back for reuse by future
705 // virtual displays
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700706 if (displayData.isVirtual) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800707 mFreeDisplaySlots.insert(displayId);
708 ++mRemainingHwcVirtualDisplays;
709 }
710
Dominik Laskowski7e045462018-05-30 13:02:02 -0700711 const auto hwcDisplayId = displayData.hwcDisplay->getId();
712 mHwcDisplaySlots.erase(hwcDisplayId);
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700713 displayData = DisplayData();
Steven Thomas94e35b92017-07-26 18:48:28 -0700714
Dominik Laskowski7e045462018-05-30 13:02:02 -0700715 mHwcDevice->destroyDisplay(hwcDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800716}
717
718status_t HWComposer::setOutputBuffer(int32_t displayId,
719 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700720 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700721 const auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800722
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700723 if (!displayData.isVirtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700724 LOG_DISPLAY_ERROR(displayId, "Invalid operation on physical display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800725 return INVALID_OPERATION;
726 }
727
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700728 auto error = displayData.hwcDisplay->setOutputBuffer(buffer, acquireFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700729 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800730 return NO_ERROR;
731}
732
733void HWComposer::clearReleaseFences(int32_t displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700734 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800735 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700736}
737
Peiyong Lin62665892018-04-16 11:07:44 -0700738status_t HWComposer::getHdrCapabilities(
739 int32_t displayId, HdrCapabilities* outCapabilities) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700740 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stozac4f471e2016-03-24 09:31:08 -0700741
742 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Peiyong Lin62665892018-04-16 11:07:44 -0700743 auto error = hwcDisplay->getHdrCapabilities(outCapabilities);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700744 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Peiyong Lin62665892018-04-16 11:07:44 -0700745 return NO_ERROR;
Dan Stozac4f471e2016-03-24 09:31:08 -0700746}
747
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700748int32_t HWComposer::getSupportedPerFrameMetadata(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700749 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Chia-I Wud7e01d72018-06-21 13:39:09 +0800750 return mDisplayData[displayId].hwcDisplay->getSupportedPerFrameMetadata();
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700751}
752
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700753std::vector<ui::RenderIntent> HWComposer::getRenderIntents(int32_t displayId,
754 ui::ColorMode colorMode) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700755 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700756
757 std::vector<ui::RenderIntent> renderIntents;
758 auto error = mDisplayData[displayId].hwcDisplay->getRenderIntents(colorMode, &renderIntents);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700759 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700760 return renderIntents;
761}
762
763mat4 HWComposer::getDataspaceSaturationMatrix(int32_t displayId, ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700764 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700765
766 mat4 matrix;
767 auto error = mDisplayData[displayId].hwcDisplay->getDataspaceSaturationMatrix(dataspace,
768 &matrix);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700769 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700770 return matrix;
771}
772
Andy McFadden4df87bd2014-04-21 18:08:54 -0700773// Converts a PixelFormat to a human-readable string. Max 11 chars.
774// (Could use a table of prefab String8 objects.)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800775/*
Andy McFadden4df87bd2014-04-21 18:08:54 -0700776static String8 getFormatStr(PixelFormat format) {
777 switch (format) {
778 case PIXEL_FORMAT_RGBA_8888: return String8("RGBA_8888");
779 case PIXEL_FORMAT_RGBX_8888: return String8("RGBx_8888");
780 case PIXEL_FORMAT_RGB_888: return String8("RGB_888");
781 case PIXEL_FORMAT_RGB_565: return String8("RGB_565");
782 case PIXEL_FORMAT_BGRA_8888: return String8("BGRA_8888");
Andy McFaddenf0058ca2014-05-20 13:28:50 -0700783 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
784 return String8("ImplDef");
Andy McFadden4df87bd2014-04-21 18:08:54 -0700785 default:
786 String8 result;
787 result.appendFormat("? %08x", format);
788 return result;
789 }
790}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800791*/
Andy McFadden4df87bd2014-04-21 18:08:54 -0700792
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800793bool HWComposer::isUsingVrComposer() const {
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800794 return getComposer()->isUsingVrComposer();
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800795}
796
Mathias Agopian74d211a2013-04-22 16:55:35 +0200797void HWComposer::dump(String8& result) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800798 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow
799 // all the state going into the layers. This is probably better done in
800 // Layer itself, but it's going to take a bit of work to get there.
801 result.append(mHwcDevice->dump().c_str());
Mathias Agopian83727852010-09-23 18:13:21 -0700802}
803
Steven Thomas6e8f7062017-11-22 14:15:29 -0800804std::optional<hwc2_display_t>
805HWComposer::getHwcDisplayId(int32_t displayId) const {
806 if (!isValidDisplay(displayId)) {
807 return {};
808 }
809 return mDisplayData[displayId].hwcDisplay->getId();
810}
811
David Sodman44b5de02018-08-21 16:28:53 -0700812void HWComposer::setDisplayFrequencyScaleParameters(
813 HWC2::Device::FrequencyScaler frequencyScaler)
814{
815 mHwcDevice->setDisplayFrequencyScaleParameters(frequencyScaler);
816}
817
818HWC2::Device::FrequencyScaler HWComposer::getDisplayFrequencyScaleParameters()
819{
820 return mHwcDevice->getDisplayFrequencyScaleParameters();
821}
822
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700823} // namespace android