blob: cd0635af48afb20989addc4f46783325f846ffd8 [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 Laskowskifc2c0322018-04-19 14:47:33 -070057#define LOG_DISPLAY_ERROR(displayId, msg) \
58 ALOGE("%s failed for display %d: %s", __FUNCTION__, displayId, msg)
59
60#define LOG_HWC_ERROR(what, error, displayId) \
61 ALOGE("%s: %s failed for display %d: %s (%d)", __FUNCTION__, what, displayId, \
62 to_string(error).c_str(), static_cast<int32_t>(error))
63
64#define RETURN_IF_INVALID_DISPLAY(displayId, ...) \
65 do { \
66 if (!isValidDisplay(displayId)) { \
67 LOG_DISPLAY_ERROR(displayId, "Invalid display"); \
68 return __VA_ARGS__; \
69 } \
70 } while (false)
71
72#define RETURN_IF_HWC_ERROR_FOR(what, error, displayId, ...) \
73 do { \
74 if (error != HWC2::Error::None) { \
75 LOG_HWC_ERROR(what, error, displayId); \
76 return __VA_ARGS__; \
77 } \
78 } while (false)
79
80#define RETURN_IF_HWC_ERROR(error, displayId, ...) \
81 RETURN_IF_HWC_ERROR_FOR(__FUNCTION__, error, displayId, __VA_ARGS__)
82
Mathias Agopiana350ff92010-08-10 17:14:02 -070083namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -070084
Jesse Hall72960512013-01-10 16:19:56 -080085#define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION
Jesse Hall9eb1eb52012-08-29 10:39:38 -070086
Mathias Agopiana350ff92010-08-10 17:14:02 -070087// ---------------------------------------------------------------------------
88
Lloyd Piquea822d522017-12-20 16:42:57 -080089HWComposer::HWComposer(std::unique_ptr<android::Hwc2::Composer> composer)
90 : mHwcDevice(std::make_unique<HWC2::Device>(std::move(composer))) {}
Mathias Agopianbef42c52013-08-21 17:45:46 -070091
Lloyd Piquea822d522017-12-20 16:42:57 -080092HWComposer::~HWComposer() = default;
Dan Stoza9e56aa02015-11-02 13:00:03 -080093
Steven Thomas94e35b92017-07-26 18:48:28 -070094void HWComposer::registerCallback(HWC2::ComposerCallback* callback,
95 int32_t sequenceId) {
96 mHwcDevice->registerCallback(callback, sequenceId);
Dan Stoza9e56aa02015-11-02 13:00:03 -080097}
98
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070099bool HWComposer::getDisplayIdentificationData(hwc2_display_t displayId, uint8_t* outPort,
100 DisplayIdentificationData* outData) const {
101 HWC2::Display* display = mHwcDevice->getDisplayById(displayId);
102 if (!display) {
103 ALOGE("getDisplayIdentificationData: Attempted to access invalid display %" PRIu64,
104 displayId);
105 return false;
106 }
107 const auto error = display->getIdentificationData(outPort, outData);
108 if (error != HWC2::Error::None) {
109 ALOGE("getDisplayIdentificationData failed for display %" PRIu64, displayId);
110 return false;
111 }
112 return true;
113}
114
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700115bool HWComposer::hasCapability(HWC2::Capability capability) const
116{
117 return mHwcDevice->getCapabilities().count(capability) > 0;
118}
119
Dan Stoza9e56aa02015-11-02 13:00:03 -0800120bool HWComposer::isValidDisplay(int32_t displayId) const {
121 return static_cast<size_t>(displayId) < mDisplayData.size() &&
122 mDisplayData[displayId].hwcDisplay;
123}
124
125void HWComposer::validateChange(HWC2::Composition from, HWC2::Composition to) {
126 bool valid = true;
127 switch (from) {
128 case HWC2::Composition::Client:
129 valid = false;
130 break;
131 case HWC2::Composition::Device:
132 case HWC2::Composition::SolidColor:
133 valid = (to == HWC2::Composition::Client);
134 break;
135 case HWC2::Composition::Cursor:
136 case HWC2::Composition::Sideband:
137 valid = (to == HWC2::Composition::Client ||
138 to == HWC2::Composition::Device);
139 break;
140 default:
141 break;
142 }
143
144 if (!valid) {
145 ALOGE("Invalid layer type change: %s --> %s", to_string(from).c_str(),
146 to_string(to).c_str());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700147 }
148}
149
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700150std::optional<DisplayId> HWComposer::onHotplug(hwc2_display_t displayId, int32_t displayType,
151 HWC2::Connection connection) {
Lloyd Pique715a2c12017-12-14 17:18:08 -0800152 if (displayType >= HWC_NUM_PHYSICAL_DISPLAY_TYPES) {
153 ALOGE("Invalid display type of %d", displayType);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700154 return {};
Lloyd Pique715a2c12017-12-14 17:18:08 -0800155 }
156
157 ALOGV("hotplug: %" PRIu64 ", %s %s", displayId,
158 displayType == DisplayDevice::DISPLAY_PRIMARY ? "primary" : "external",
Steven Thomas94e35b92017-07-26 18:48:28 -0700159 to_string(connection).c_str());
160 mHwcDevice->onHotplug(displayId, connection);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700161
162 std::optional<DisplayId> stableId;
163
164 uint8_t port;
165 DisplayIdentificationData data;
166 if (getDisplayIdentificationData(displayId, &port, &data)) {
167 stableId = generateDisplayId(port, data);
168 ALOGE_IF(!stableId, "Failed to generate stable ID for display %" PRIu64, displayId);
169 }
170
Lloyd Pique715a2c12017-12-14 17:18:08 -0800171 // Disconnect is handled through HWComposer::disconnectDisplay via
172 // SurfaceFlinger's onHotplugReceived callback handling
173 if (connection == HWC2::Connection::Connected) {
174 mDisplayData[displayType].hwcDisplay = mHwcDevice->getDisplayById(displayId);
175 mHwcDisplaySlots[displayId] = displayType;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700176 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700177
178 return stableId;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700179}
180
Steven Thomas94e35b92017-07-26 18:48:28 -0700181bool HWComposer::onVsync(hwc2_display_t displayId, int64_t timestamp,
182 int32_t* outDisplay) {
183 auto display = mHwcDevice->getDisplayById(displayId);
184 if (!display) {
185 ALOGE("onVsync Failed to find display %" PRIu64, displayId);
186 return false;
187 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800188 auto displayType = HWC2::DisplayType::Invalid;
189 auto error = display->getType(&displayType);
190 if (error != HWC2::Error::None) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700191 ALOGE("onVsync: Failed to determine type of display %" PRIu64,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800192 display->getId());
Steven Thomas94e35b92017-07-26 18:48:28 -0700193 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700194 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700195
Dan Stoza9e56aa02015-11-02 13:00:03 -0800196 if (displayType == HWC2::DisplayType::Virtual) {
197 ALOGE("Virtual display %" PRIu64 " passed to vsync callback",
198 display->getId());
Steven Thomas94e35b92017-07-26 18:48:28 -0700199 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700200 }
201
Dan Stoza9e56aa02015-11-02 13:00:03 -0800202 if (mHwcDisplaySlots.count(display->getId()) == 0) {
203 ALOGE("Unknown physical display %" PRIu64 " passed to vsync callback",
204 display->getId());
Steven Thomas94e35b92017-07-26 18:48:28 -0700205 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700206 }
207
Dan Stoza9e56aa02015-11-02 13:00:03 -0800208 int32_t disp = mHwcDisplaySlots[display->getId()];
209 {
210 Mutex::Autolock _l(mLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700211
Dan Stoza9e56aa02015-11-02 13:00:03 -0800212 // There have been reports of HWCs that signal several vsync events
213 // with the same timestamp when turning the display off and on. This
214 // is a bug in the HWC implementation, but filter the extra events
215 // out here so they don't cause havoc downstream.
216 if (timestamp == mLastHwVSync[disp]) {
217 ALOGW("Ignoring duplicate VSYNC event from HWC (t=%" PRId64 ")",
218 timestamp);
Steven Thomas94e35b92017-07-26 18:48:28 -0700219 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800220 }
221
222 mLastHwVSync[disp] = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700223 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800224
Steven Thomas94e35b92017-07-26 18:48:28 -0700225 if (outDisplay) {
226 *outDisplay = disp;
227 }
228
Dan Stoza9e56aa02015-11-02 13:00:03 -0800229 char tag[16];
230 snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", disp);
231 ATRACE_INT(tag, ++mVSyncCounts[disp] & 1);
232
Steven Thomas94e35b92017-07-26 18:48:28 -0700233 return true;
Jesse Hall1c569c42013-04-05 13:44:52 -0700234}
235
Dan Stoza9e56aa02015-11-02 13:00:03 -0800236status_t HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700237 ui::PixelFormat* format, int32_t *outId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800238 if (mRemainingHwcVirtualDisplays == 0) {
239 ALOGE("allocateVirtualDisplay: No remaining virtual displays");
Mathias Agopiane60b0682012-08-21 23:34:09 -0700240 return NO_MEMORY;
241 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700242
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800243 if (SurfaceFlinger::maxVirtualDisplaySize != 0 &&
244 (width > SurfaceFlinger::maxVirtualDisplaySize ||
245 height > SurfaceFlinger::maxVirtualDisplaySize)) {
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800246 ALOGE("createVirtualDisplay: Can't create a virtual display with"
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800247 " a dimension > %" PRIu64 " (tried %u x %u)",
248 SurfaceFlinger::maxVirtualDisplaySize, width, height);
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800249 return INVALID_OPERATION;
250 }
251
Steven Thomas94e35b92017-07-26 18:48:28 -0700252 HWC2::Display* display;
Dan Stoza5cf424b2016-05-20 14:02:39 -0700253 auto error = mHwcDevice->createVirtualDisplay(width, height, format,
254 &display);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800255 if (error != HWC2::Error::None) {
256 ALOGE("allocateVirtualDisplay: Failed to create HWC virtual display");
257 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700258 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800259
260 size_t displaySlot = 0;
261 if (!mFreeDisplaySlots.empty()) {
262 displaySlot = *mFreeDisplaySlots.begin();
263 mFreeDisplaySlots.erase(displaySlot);
264 } else if (mDisplayData.size() < INT32_MAX) {
265 // Don't bother allocating a slot larger than we can return
266 displaySlot = mDisplayData.size();
267 mDisplayData.resize(displaySlot + 1);
268 } else {
269 ALOGE("allocateVirtualDisplay: Unable to allocate a display slot");
270 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700271 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800272
273 mDisplayData[displaySlot].hwcDisplay = display;
274
275 --mRemainingHwcVirtualDisplays;
276 *outId = static_cast<int32_t>(displaySlot);
277
Mathias Agopiane60b0682012-08-21 23:34:09 -0700278 return NO_ERROR;
279}
280
Steven Thomas94e35b92017-07-26 18:48:28 -0700281HWC2::Layer* HWComposer::createLayer(int32_t displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700282 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
283
Dan Stoza9e56aa02015-11-02 13:00:03 -0800284 auto display = mDisplayData[displayId].hwcDisplay;
Steven Thomas94e35b92017-07-26 18:48:28 -0700285 HWC2::Layer* layer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800286 auto error = display->createLayer(&layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700287 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800288 return layer;
289}
290
Steven Thomas94e35b92017-07-26 18:48:28 -0700291void HWComposer::destroyLayer(int32_t displayId, HWC2::Layer* layer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700292 RETURN_IF_INVALID_DISPLAY(displayId);
293
Steven Thomas94e35b92017-07-26 18:48:28 -0700294 auto display = mDisplayData[displayId].hwcDisplay;
295 auto error = display->destroyLayer(layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700296 RETURN_IF_HWC_ERROR(error, displayId);
Steven Thomas94e35b92017-07-26 18:48:28 -0700297}
298
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800299nsecs_t HWComposer::getRefreshTimestamp(int32_t displayId) const {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700300 // this returns the last refresh timestamp.
301 // if the last one is not available, we estimate it based on
302 // the refresh period and whatever closest timestamp we have.
303 Mutex::Autolock _l(mLock);
304 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800305 auto vsyncPeriod = getActiveConfig(displayId)->getVsyncPeriod();
306 return now - ((now - mLastHwVSync[displayId]) % vsyncPeriod);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700307}
308
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800309bool HWComposer::isConnected(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700310 RETURN_IF_INVALID_DISPLAY(displayId, false);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800311 return mDisplayData[displayId].hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700312}
313
Dan Stoza9e56aa02015-11-02 13:00:03 -0800314std::vector<std::shared_ptr<const HWC2::Display::Config>>
315 HWComposer::getConfigs(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700316 RETURN_IF_INVALID_DISPLAY(displayId, {});
317
Dan Stoza9e56aa02015-11-02 13:00:03 -0800318 auto& displayData = mDisplayData[displayId];
319 auto configs = mDisplayData[displayId].hwcDisplay->getConfigs();
320 if (displayData.configMap.empty()) {
321 for (size_t i = 0; i < configs.size(); ++i) {
322 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700323 }
324 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800325 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700326}
327
Dan Stoza9e56aa02015-11-02 13:00:03 -0800328std::shared_ptr<const HWC2::Display::Config>
329 HWComposer::getActiveConfig(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700330 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
331
Dan Stoza9e56aa02015-11-02 13:00:03 -0800332 std::shared_ptr<const HWC2::Display::Config> config;
333 auto error = mDisplayData[displayId].hwcDisplay->getActiveConfig(&config);
334 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700335 LOG_DISPLAY_ERROR(displayId, "No active config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800336 return nullptr;
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700337 }
338
339 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
340
341 if (!config) {
342 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800343 return nullptr;
344 }
345
346 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700347}
348
Lloyd Pique3c085a02018-05-09 19:38:32 -0700349int HWComposer::getActiveConfigIndex(int32_t displayId) const {
350 if (!isValidDisplay(displayId)) {
351 ALOGV("getActiveConfigIndex: Attempted to access invalid display %d", displayId);
352 return -1;
353 }
354 int index;
355 auto error = mDisplayData[displayId].hwcDisplay->getActiveConfigIndex(&index);
356 if (error == HWC2::Error::BadConfig) {
357 ALOGE("getActiveConfigIndex: No config active, returning -1");
358 return -1;
359 } else if (error != HWC2::Error::None) {
360 ALOGE("getActiveConfigIndex failed for display %d: %s (%d)", displayId,
361 to_string(error).c_str(), static_cast<int32_t>(error));
362 return -1;
363 } else if (index < 0) {
364 ALOGE("getActiveConfigIndex returned an unknown config for display %d", displayId);
365 return -1;
366 }
367
368 return index;
369}
370
Peiyong Linfd997e02018-03-28 15:29:00 -0700371std::vector<ui::ColorMode> HWComposer::getColorModes(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700372 RETURN_IF_INVALID_DISPLAY(displayId, {});
373
Peiyong Linfd997e02018-03-28 15:29:00 -0700374 std::vector<ui::ColorMode> modes;
Steven Thomas94e35b92017-07-26 18:48:28 -0700375 auto error = mDisplayData[displayId].hwcDisplay->getColorModes(&modes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700376 RETURN_IF_HWC_ERROR(error, displayId, {});
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600377 return modes;
378}
379
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700380status_t HWComposer::setActiveColorMode(int32_t displayId, ui::ColorMode mode,
381 ui::RenderIntent renderIntent) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700382 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Michael Wright28f24d02016-07-12 13:30:53 -0700383
384 auto& displayData = mDisplayData[displayId];
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700385 auto error = displayData.hwcDisplay->setColorMode(mode, renderIntent);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700386 RETURN_IF_HWC_ERROR_FOR(("setColorMode(" + decodeColorMode(mode) + ", " +
387 decodeRenderIntent(renderIntent) + ")")
388 .c_str(),
389 error, displayId, UNKNOWN_ERROR);
Michael Wright28f24d02016-07-12 13:30:53 -0700390
391 return NO_ERROR;
392}
393
394
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800395void HWComposer::setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled) {
396 if (displayId < 0 || displayId >= HWC_DISPLAY_VIRTUAL) {
397 ALOGD("setVsyncEnabled: Ignoring for virtual display %d", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800398 return;
399 }
400
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700401 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800402
403 // NOTE: we use our own internal lock here because we have to call
404 // into the HWC with the lock held, and we want to make sure
405 // that even if HWC blocks (which it shouldn't), it won't
406 // affect other threads.
407 Mutex::Autolock _l(mVsyncLock);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800408 auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800409 if (enabled != displayData.vsyncEnabled) {
410 ATRACE_CALL();
411 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700412 RETURN_IF_HWC_ERROR(error, displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800413
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700414 displayData.vsyncEnabled = enabled;
415
416 char tag[16];
417 snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", displayId);
418 ATRACE_INT(tag, enabled == HWC2::Vsync::Enable ? 1 : 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700419 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700420}
421
Chia-I Wu06d63de2017-01-04 14:58:51 +0800422status_t HWComposer::setClientTarget(int32_t displayId, uint32_t slot,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800423 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700424 ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700425 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Jesse Hall851cfe82013-03-20 13:44:00 -0700426
Dan Stoza9e56aa02015-11-02 13:00:03 -0800427 ALOGV("setClientTarget for display %d", displayId);
428 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400429 auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700430 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Jesse Hall851cfe82013-03-20 13:44:00 -0700431 return NO_ERROR;
432}
433
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700434status_t HWComposer::prepare(DisplayDevice& display) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800435 ATRACE_CALL();
436
437 Mutex::Autolock _l(mDisplayLock);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700438 const auto displayId = display.getId();
Dan Stozaec0f7172016-07-21 11:09:40 -0700439 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
440 ALOGV("Skipping HWComposer prepare for non-HWC display");
441 return NO_ERROR;
442 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700443
444 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800445
446 auto& displayData = mDisplayData[displayId];
447 auto& hwcDisplay = displayData.hwcDisplay;
448 if (!hwcDisplay->isConnected()) {
449 return NO_ERROR;
450 }
451
452 uint32_t numTypes = 0;
453 uint32_t numRequests = 0;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700454
455 HWC2::Error error = HWC2::Error::None;
456
Chia-I Wu41b98d42017-12-11 11:04:36 -0800457 // First try to skip validate altogether when there is no client
458 // composition. When there is client composition, since we haven't
459 // rendered to the client target yet, we should not attempt to skip
460 // validate.
461 //
462 // displayData.hasClientComposition hasn't been updated for this frame.
463 // The check below is incorrect. We actually rely on HWC here to fall
464 // back to validate when there is any client layer.
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700465 displayData.validateWasSkipped = false;
Chia-I Wu41b98d42017-12-11 11:04:36 -0800466 if (!displayData.hasClientComposition) {
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700467 sp<android::Fence> outPresentFence;
468 uint32_t state = UINT32_MAX;
469 error = hwcDisplay->presentOrValidate(&numTypes, &numRequests, &outPresentFence , &state);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700470 if (error != HWC2::Error::HasChanges) {
471 RETURN_IF_HWC_ERROR_FOR("presentOrValidate", error, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700472 }
473 if (state == 1) { //Present Succeeded.
Steven Thomas94e35b92017-07-26 18:48:28 -0700474 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700475 error = hwcDisplay->getReleaseFences(&releaseFences);
476 displayData.releaseFences = std::move(releaseFences);
477 displayData.lastPresentFence = outPresentFence;
478 displayData.validateWasSkipped = true;
479 displayData.presentError = error;
480 return NO_ERROR;
481 }
482 // Present failed but Validate ran.
483 } else {
484 error = hwcDisplay->validate(&numTypes, &numRequests);
485 }
486 ALOGV("SkipValidate failed, Falling back to SLOW validate/present");
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700487 if (error != HWC2::Error::HasChanges) {
488 RETURN_IF_HWC_ERROR_FOR("validate", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800489 }
490
Steven Thomas94e35b92017-07-26 18:48:28 -0700491 std::unordered_map<HWC2::Layer*, HWC2::Composition> changedTypes;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800492 changedTypes.reserve(numTypes);
493 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700494 RETURN_IF_HWC_ERROR_FOR("getChangedCompositionTypes", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800495
496 displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0);
Steven Thomas94e35b92017-07-26 18:48:28 -0700497 std::unordered_map<HWC2::Layer*, HWC2::LayerRequest> layerRequests;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800498 layerRequests.reserve(numRequests);
499 error = hwcDisplay->getRequests(&displayData.displayRequests,
500 &layerRequests);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700501 RETURN_IF_HWC_ERROR_FOR("getRequests", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800502
503 displayData.hasClientComposition = false;
504 displayData.hasDeviceComposition = false;
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700505 for (auto& layer : display.getVisibleLayersSortedByZ()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800506 auto hwcLayer = layer->getHwcLayer(displayId);
507
508 if (changedTypes.count(hwcLayer) != 0) {
509 // We pass false so we only update our state and don't call back
510 // into the HWC device
511 validateChange(layer->getCompositionType(displayId),
512 changedTypes[hwcLayer]);
513 layer->setCompositionType(displayId, changedTypes[hwcLayer], false);
514 }
515
516 switch (layer->getCompositionType(displayId)) {
517 case HWC2::Composition::Client:
518 displayData.hasClientComposition = true;
519 break;
520 case HWC2::Composition::Device:
521 case HWC2::Composition::SolidColor:
522 case HWC2::Composition::Cursor:
523 case HWC2::Composition::Sideband:
524 displayData.hasDeviceComposition = true;
525 break;
526 default:
527 break;
528 }
529
530 if (layerRequests.count(hwcLayer) != 0 &&
531 layerRequests[hwcLayer] ==
532 HWC2::LayerRequest::ClearClientTarget) {
533 layer->setClearClientTarget(displayId, true);
534 } else {
535 if (layerRequests.count(hwcLayer) != 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700536 LOG_DISPLAY_ERROR(displayId,
537 ("Unknown layer request " + to_string(layerRequests[hwcLayer]))
538 .c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800539 }
540 layer->setClearClientTarget(displayId, false);
541 }
542 }
543
544 error = hwcDisplay->acceptChanges();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700545 RETURN_IF_HWC_ERROR_FOR("acceptChanges", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800546
547 return NO_ERROR;
548}
549
550bool HWComposer::hasDeviceComposition(int32_t displayId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700551 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
552 // Displays without a corresponding HWC display are never composed by
553 // the device
554 return false;
555 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700556
557 RETURN_IF_INVALID_DISPLAY(displayId, false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800558 return mDisplayData[displayId].hasDeviceComposition;
559}
560
Madhuri Athota88a905b2017-05-04 16:58:15 +0530561bool HWComposer::hasFlipClientTargetRequest(int32_t displayId) const {
562 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
563 // Displays without a corresponding HWC display are never composed by
564 // the device
565 return false;
566 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700567
568 RETURN_IF_INVALID_DISPLAY(displayId, false);
Madhuri Athota88a905b2017-05-04 16:58:15 +0530569 return ((static_cast<uint32_t>(mDisplayData[displayId].displayRequests) &
570 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0);
571}
572
Dan Stoza9e56aa02015-11-02 13:00:03 -0800573bool HWComposer::hasClientComposition(int32_t displayId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700574 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
575 // Displays without a corresponding HWC display are always composed by
576 // the client
577 return true;
578 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700579
580 RETURN_IF_INVALID_DISPLAY(displayId, true);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800581 return mDisplayData[displayId].hasClientComposition;
582}
583
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800584sp<Fence> HWComposer::getPresentFence(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700585 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800586 return mDisplayData[displayId].lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700587}
588
Dan Stoza9e56aa02015-11-02 13:00:03 -0800589sp<Fence> HWComposer::getLayerReleaseFence(int32_t displayId,
Steven Thomas94e35b92017-07-26 18:48:28 -0700590 HWC2::Layer* layer) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700591 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800592 auto displayFences = mDisplayData[displayId].releaseFences;
593 if (displayFences.count(layer) == 0) {
594 ALOGV("getLayerReleaseFence: Release fence not found");
595 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700596 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800597 return displayFences[layer];
Riley Andrews03414a12014-07-01 14:22:59 -0700598}
599
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800600status_t HWComposer::presentAndGetReleaseFences(int32_t displayId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800601 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700602
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700603 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700604
Dan Stoza9e56aa02015-11-02 13:00:03 -0800605 auto& displayData = mDisplayData[displayId];
606 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700607
608 if (displayData.validateWasSkipped) {
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700609 // explicitly flush all pending commands
610 auto error = mHwcDevice->flushCommands();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700611 RETURN_IF_HWC_ERROR_FOR("flushCommands", error, displayId, UNKNOWN_ERROR);
612 RETURN_IF_HWC_ERROR_FOR("present", displayData.presentError, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700613 return NO_ERROR;
614 }
615
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800616 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700617 RETURN_IF_HWC_ERROR_FOR("present", error, displayId, UNKNOWN_ERROR);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700618
Steven Thomas94e35b92017-07-26 18:48:28 -0700619 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800620 error = hwcDisplay->getReleaseFences(&releaseFences);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700621 RETURN_IF_HWC_ERROR_FOR("getReleaseFences", error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800622
623 displayData.releaseFences = std::move(releaseFences);
624
625 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700626}
627
Dan Stoza9e56aa02015-11-02 13:00:03 -0800628status_t HWComposer::setPowerMode(int32_t displayId, int32_t intMode) {
629 ALOGV("setPowerMode(%d, %d)", displayId, intMode);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700630 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
631
Dan Stoza9e56aa02015-11-02 13:00:03 -0800632 if (displayId >= VIRTUAL_DISPLAY_ID_BASE) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700633 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
634 return INVALID_OPERATION;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800635 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700636
Dan Stoza9e56aa02015-11-02 13:00:03 -0800637 auto mode = static_cast<HWC2::PowerMode>(intMode);
638 if (mode == HWC2::PowerMode::Off) {
639 setVsyncEnabled(displayId, HWC2::Vsync::Disable);
640 }
641
642 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
643 switch (mode) {
644 case HWC2::PowerMode::Off:
645 case HWC2::PowerMode::On:
646 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
647 {
648 auto error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700649 if (error != HWC2::Error::None) {
650 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
651 error, displayId);
652 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700653 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800654 break;
655 case HWC2::PowerMode::Doze:
656 case HWC2::PowerMode::DozeSuspend:
657 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
658 {
659 bool supportsDoze = false;
660 auto error = hwcDisplay->supportsDoze(&supportsDoze);
Peiyong Lin306e4992018-05-07 16:18:22 -0700661 if (error != HWC2::Error::None) {
662 LOG_HWC_ERROR("supportsDoze", error, displayId);
663 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700664
Dan Stoza9e56aa02015-11-02 13:00:03 -0800665 if (!supportsDoze) {
666 mode = HWC2::PowerMode::On;
667 }
668
669 error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700670 if (error != HWC2::Error::None) {
671 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
672 error, displayId);
673 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800674 }
675 break;
676 default:
677 ALOGV("setPowerMode: Not calling HWC");
678 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700679 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800680
681 return NO_ERROR;
682}
683
684status_t HWComposer::setActiveConfig(int32_t displayId, size_t configId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700685 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800686
687 auto& displayData = mDisplayData[displayId];
688 if (displayData.configMap.count(configId) == 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700689 LOG_DISPLAY_ERROR(displayId, ("Invalid config " + std::to_string(configId)).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800690 return BAD_INDEX;
691 }
692
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700693 auto error = displayData.hwcDisplay->setActiveConfig(displayData.configMap[configId]);
694 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800695 return NO_ERROR;
696}
697
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700698status_t HWComposer::setColorTransform(int32_t displayId,
699 const mat4& transform) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700700 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700701
702 auto& displayData = mDisplayData[displayId];
703 bool isIdentity = transform == mat4();
704 auto error = displayData.hwcDisplay->setColorTransform(transform,
705 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY :
706 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700707 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700708 return NO_ERROR;
709}
710
Dan Stoza9e56aa02015-11-02 13:00:03 -0800711void HWComposer::disconnectDisplay(int displayId) {
712 LOG_ALWAYS_FATAL_IF(displayId < 0);
713 auto& displayData = mDisplayData[displayId];
714
715 auto displayType = HWC2::DisplayType::Invalid;
716 auto error = displayData.hwcDisplay->getType(&displayType);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700717 RETURN_IF_HWC_ERROR_FOR("getType", error, displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800718
719 // If this was a virtual display, add its slot back for reuse by future
720 // virtual displays
721 if (displayType == HWC2::DisplayType::Virtual) {
722 mFreeDisplaySlots.insert(displayId);
723 ++mRemainingHwcVirtualDisplays;
724 }
725
Dominik Laskowski7e045462018-05-30 13:02:02 -0700726 const auto hwcDisplayId = displayData.hwcDisplay->getId();
727 mHwcDisplaySlots.erase(hwcDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800728 displayData.reset();
Steven Thomas94e35b92017-07-26 18:48:28 -0700729
Dominik Laskowski7e045462018-05-30 13:02:02 -0700730 mHwcDevice->destroyDisplay(hwcDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800731}
732
733status_t HWComposer::setOutputBuffer(int32_t displayId,
734 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700735 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800736
737 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
738 auto displayType = HWC2::DisplayType::Invalid;
739 auto error = hwcDisplay->getType(&displayType);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700740 RETURN_IF_HWC_ERROR_FOR("getType", error, displayId, NAME_NOT_FOUND);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800741
742 if (displayType != HWC2::DisplayType::Virtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700743 LOG_DISPLAY_ERROR(displayId, "Invalid operation on physical display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800744 return INVALID_OPERATION;
745 }
746
747 error = hwcDisplay->setOutputBuffer(buffer, acquireFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700748 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800749 return NO_ERROR;
750}
751
752void HWComposer::clearReleaseFences(int32_t displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700753 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800754 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700755}
756
Peiyong Lin62665892018-04-16 11:07:44 -0700757status_t HWComposer::getHdrCapabilities(
758 int32_t displayId, HdrCapabilities* outCapabilities) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700759 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stozac4f471e2016-03-24 09:31:08 -0700760
761 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Peiyong Lin62665892018-04-16 11:07:44 -0700762 auto error = hwcDisplay->getHdrCapabilities(outCapabilities);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700763 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Peiyong Lin62665892018-04-16 11:07:44 -0700764 return NO_ERROR;
Dan Stozac4f471e2016-03-24 09:31:08 -0700765}
766
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700767int32_t HWComposer::getSupportedPerFrameMetadata(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700768 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700769
770 int32_t supportedMetadata;
771 auto error = mDisplayData[displayId].hwcDisplay->getSupportedPerFrameMetadata(
772 &supportedMetadata);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700773 RETURN_IF_HWC_ERROR(error, displayId, 0);
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700774 return supportedMetadata;
775}
776
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700777std::vector<ui::RenderIntent> HWComposer::getRenderIntents(int32_t displayId,
778 ui::ColorMode colorMode) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700779 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700780
781 std::vector<ui::RenderIntent> renderIntents;
782 auto error = mDisplayData[displayId].hwcDisplay->getRenderIntents(colorMode, &renderIntents);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700783 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700784 return renderIntents;
785}
786
787mat4 HWComposer::getDataspaceSaturationMatrix(int32_t displayId, ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700788 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700789
790 mat4 matrix;
791 auto error = mDisplayData[displayId].hwcDisplay->getDataspaceSaturationMatrix(dataspace,
792 &matrix);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700793 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700794 return matrix;
795}
796
Andy McFadden4df87bd2014-04-21 18:08:54 -0700797// Converts a PixelFormat to a human-readable string. Max 11 chars.
798// (Could use a table of prefab String8 objects.)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800799/*
Andy McFadden4df87bd2014-04-21 18:08:54 -0700800static String8 getFormatStr(PixelFormat format) {
801 switch (format) {
802 case PIXEL_FORMAT_RGBA_8888: return String8("RGBA_8888");
803 case PIXEL_FORMAT_RGBX_8888: return String8("RGBx_8888");
804 case PIXEL_FORMAT_RGB_888: return String8("RGB_888");
805 case PIXEL_FORMAT_RGB_565: return String8("RGB_565");
806 case PIXEL_FORMAT_BGRA_8888: return String8("BGRA_8888");
Andy McFaddenf0058ca2014-05-20 13:28:50 -0700807 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
808 return String8("ImplDef");
Andy McFadden4df87bd2014-04-21 18:08:54 -0700809 default:
810 String8 result;
811 result.appendFormat("? %08x", format);
812 return result;
813 }
814}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800815*/
Andy McFadden4df87bd2014-04-21 18:08:54 -0700816
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800817bool HWComposer::isUsingVrComposer() const {
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800818 return getComposer()->isUsingVrComposer();
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800819}
820
Mathias Agopian74d211a2013-04-22 16:55:35 +0200821void HWComposer::dump(String8& result) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800822 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow
823 // all the state going into the layers. This is probably better done in
824 // Layer itself, but it's going to take a bit of work to get there.
825 result.append(mHwcDevice->dump().c_str());
Mathias Agopian83727852010-09-23 18:13:21 -0700826}
827
Steven Thomas6e8f7062017-11-22 14:15:29 -0800828std::optional<hwc2_display_t>
829HWComposer::getHwcDisplayId(int32_t displayId) const {
830 if (!isValidDisplay(displayId)) {
831 return {};
832 }
833 return mDisplayData[displayId].hwcDisplay->getId();
834}
835
Mathias Agopiana350ff92010-08-10 17:14:02 -0700836// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -0700837
Jesse Halla9a1b002013-02-27 16:39:25 -0800838HWComposer::DisplayData::DisplayData()
Dan Stoza9e56aa02015-11-02 13:00:03 -0800839 : hasClientComposition(false),
840 hasDeviceComposition(false),
Steven Thomas94e35b92017-07-26 18:48:28 -0700841 hwcDisplay(nullptr),
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800842 lastPresentFence(Fence::NO_FENCE),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800843 outbufHandle(nullptr),
844 outbufAcquireFence(Fence::NO_FENCE),
845 vsyncEnabled(HWC2::Vsync::Disable) {
846 ALOGV("Created new DisplayData");
847}
Jesse Halla9a1b002013-02-27 16:39:25 -0800848
849HWComposer::DisplayData::~DisplayData() {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800850}
851
852void HWComposer::DisplayData::reset() {
853 ALOGV("DisplayData reset");
854 *this = DisplayData();
Jesse Halla9a1b002013-02-27 16:39:25 -0800855}
856
Mathias Agopian2965b262012-04-08 15:13:32 -0700857// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -0700858}; // namespace android