blob: ebae006ee580946ece794f0d24565357287666f5 [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 Laskowskia2edf612018-06-01 13:15:16 -070099bool HWComposer::getDisplayIdentificationData(hwc2_display_t hwcDisplayId, uint8_t* outPort,
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700100 DisplayIdentificationData* outData) const {
Dominik Laskowski0954b1d2018-06-13 14:58:49 -0700101 const auto error = mHwcDevice->getDisplayIdentificationData(hwcDisplayId, outPort, outData);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700102 if (error != HWC2::Error::None) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700103 ALOGE("%s failed for display %" PRIu64, __FUNCTION__, hwcDisplayId);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700104 return false;
105 }
106 return true;
107}
108
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700109bool HWComposer::hasCapability(HWC2::Capability capability) const
110{
111 return mHwcDevice->getCapabilities().count(capability) > 0;
112}
113
Dan Stoza9e56aa02015-11-02 13:00:03 -0800114bool HWComposer::isValidDisplay(int32_t displayId) const {
115 return static_cast<size_t>(displayId) < mDisplayData.size() &&
116 mDisplayData[displayId].hwcDisplay;
117}
118
119void HWComposer::validateChange(HWC2::Composition from, HWC2::Composition to) {
120 bool valid = true;
121 switch (from) {
122 case HWC2::Composition::Client:
123 valid = false;
124 break;
125 case HWC2::Composition::Device:
126 case HWC2::Composition::SolidColor:
127 valid = (to == HWC2::Composition::Client);
128 break;
129 case HWC2::Composition::Cursor:
130 case HWC2::Composition::Sideband:
131 valid = (to == HWC2::Composition::Client ||
132 to == HWC2::Composition::Device);
133 break;
134 default:
135 break;
136 }
137
138 if (!valid) {
139 ALOGE("Invalid layer type change: %s --> %s", to_string(from).c_str(),
140 to_string(to).c_str());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700141 }
142}
143
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700144std::optional<DisplayId> HWComposer::onHotplug(hwc2_display_t hwcDisplayId, int32_t displayType,
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700145 HWC2::Connection connection) {
Lloyd Pique715a2c12017-12-14 17:18:08 -0800146 if (displayType >= HWC_NUM_PHYSICAL_DISPLAY_TYPES) {
147 ALOGE("Invalid display type of %d", displayType);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700148 return {};
Lloyd Pique715a2c12017-12-14 17:18:08 -0800149 }
150
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700151 ALOGV("hotplug: %" PRIu64 ", %s %s", hwcDisplayId,
152 displayType == DisplayDevice::DISPLAY_PRIMARY ? "primary" : "external",
153 to_string(connection).c_str());
154 mHwcDevice->onHotplug(hwcDisplayId, connection);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700155
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700156 std::optional<DisplayId> displayId;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700157
158 uint8_t port;
159 DisplayIdentificationData data;
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700160 if (getDisplayIdentificationData(hwcDisplayId, &port, &data)) {
161 displayId = generateDisplayId(port, data);
162 ALOGE_IF(!displayId, "Failed to generate stable ID for display %" PRIu64, hwcDisplayId);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700163 }
164
Lloyd Pique715a2c12017-12-14 17:18:08 -0800165 // Disconnect is handled through HWComposer::disconnectDisplay via
166 // SurfaceFlinger's onHotplugReceived callback handling
167 if (connection == HWC2::Connection::Connected) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700168 mDisplayData[displayType].hwcDisplay = mHwcDevice->getDisplayById(hwcDisplayId);
169 mHwcDisplaySlots[hwcDisplayId] = displayType;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700170 }
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700171
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700172 return displayId;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700173}
174
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700175bool HWComposer::onVsync(hwc2_display_t hwcDisplayId, int64_t timestamp, int32_t* outDisplay) {
176 auto display = mHwcDevice->getDisplayById(hwcDisplayId);
Steven Thomas94e35b92017-07-26 18:48:28 -0700177 if (!display) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700178 ALOGE("%s: Failed to find display %" PRIu64, __FUNCTION__, hwcDisplayId);
Steven Thomas94e35b92017-07-26 18:48:28 -0700179 return false;
180 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800181 auto displayType = HWC2::DisplayType::Invalid;
182 auto error = display->getType(&displayType);
183 if (error != HWC2::Error::None) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700184 ALOGE("%s: Failed to determine type of display %" PRIu64, __FUNCTION__, display->getId());
Steven Thomas94e35b92017-07-26 18:48:28 -0700185 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700186 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700187
Dan Stoza9e56aa02015-11-02 13:00:03 -0800188 if (displayType == HWC2::DisplayType::Virtual) {
189 ALOGE("Virtual display %" PRIu64 " passed to vsync callback",
190 display->getId());
Steven Thomas94e35b92017-07-26 18:48:28 -0700191 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700192 }
193
Dan Stoza9e56aa02015-11-02 13:00:03 -0800194 if (mHwcDisplaySlots.count(display->getId()) == 0) {
195 ALOGE("Unknown physical display %" PRIu64 " passed to vsync callback",
196 display->getId());
Steven Thomas94e35b92017-07-26 18:48:28 -0700197 return false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700198 }
199
Dan Stoza9e56aa02015-11-02 13:00:03 -0800200 int32_t disp = mHwcDisplaySlots[display->getId()];
201 {
202 Mutex::Autolock _l(mLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700203
Dan Stoza9e56aa02015-11-02 13:00:03 -0800204 // There have been reports of HWCs that signal several vsync events
205 // with the same timestamp when turning the display off and on. This
206 // is a bug in the HWC implementation, but filter the extra events
207 // out here so they don't cause havoc downstream.
208 if (timestamp == mLastHwVSync[disp]) {
209 ALOGW("Ignoring duplicate VSYNC event from HWC (t=%" PRId64 ")",
210 timestamp);
Steven Thomas94e35b92017-07-26 18:48:28 -0700211 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800212 }
213
214 mLastHwVSync[disp] = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700215 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800216
Steven Thomas94e35b92017-07-26 18:48:28 -0700217 if (outDisplay) {
218 *outDisplay = disp;
219 }
220
Dan Stoza9e56aa02015-11-02 13:00:03 -0800221 char tag[16];
222 snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", disp);
223 ATRACE_INT(tag, ++mVSyncCounts[disp] & 1);
224
Steven Thomas94e35b92017-07-26 18:48:28 -0700225 return true;
Jesse Hall1c569c42013-04-05 13:44:52 -0700226}
227
Dan Stoza9e56aa02015-11-02 13:00:03 -0800228status_t HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700229 ui::PixelFormat* format, int32_t *outId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800230 if (mRemainingHwcVirtualDisplays == 0) {
231 ALOGE("allocateVirtualDisplay: No remaining virtual displays");
Mathias Agopiane60b0682012-08-21 23:34:09 -0700232 return NO_MEMORY;
233 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700234
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800235 if (SurfaceFlinger::maxVirtualDisplaySize != 0 &&
236 (width > SurfaceFlinger::maxVirtualDisplaySize ||
237 height > SurfaceFlinger::maxVirtualDisplaySize)) {
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800238 ALOGE("createVirtualDisplay: Can't create a virtual display with"
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800239 " a dimension > %" PRIu64 " (tried %u x %u)",
240 SurfaceFlinger::maxVirtualDisplaySize, width, height);
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800241 return INVALID_OPERATION;
242 }
243
Steven Thomas94e35b92017-07-26 18:48:28 -0700244 HWC2::Display* display;
Dan Stoza5cf424b2016-05-20 14:02:39 -0700245 auto error = mHwcDevice->createVirtualDisplay(width, height, format,
246 &display);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800247 if (error != HWC2::Error::None) {
248 ALOGE("allocateVirtualDisplay: Failed to create HWC virtual display");
249 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700250 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800251
252 size_t displaySlot = 0;
253 if (!mFreeDisplaySlots.empty()) {
254 displaySlot = *mFreeDisplaySlots.begin();
255 mFreeDisplaySlots.erase(displaySlot);
256 } else if (mDisplayData.size() < INT32_MAX) {
257 // Don't bother allocating a slot larger than we can return
258 displaySlot = mDisplayData.size();
259 mDisplayData.resize(displaySlot + 1);
260 } else {
261 ALOGE("allocateVirtualDisplay: Unable to allocate a display slot");
262 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700263 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800264
265 mDisplayData[displaySlot].hwcDisplay = display;
266
267 --mRemainingHwcVirtualDisplays;
268 *outId = static_cast<int32_t>(displaySlot);
269
Mathias Agopiane60b0682012-08-21 23:34:09 -0700270 return NO_ERROR;
271}
272
Steven Thomas94e35b92017-07-26 18:48:28 -0700273HWC2::Layer* HWComposer::createLayer(int32_t displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700274 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
275
Dan Stoza9e56aa02015-11-02 13:00:03 -0800276 auto display = mDisplayData[displayId].hwcDisplay;
Steven Thomas94e35b92017-07-26 18:48:28 -0700277 HWC2::Layer* layer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800278 auto error = display->createLayer(&layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700279 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800280 return layer;
281}
282
Steven Thomas94e35b92017-07-26 18:48:28 -0700283void HWComposer::destroyLayer(int32_t displayId, HWC2::Layer* layer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700284 RETURN_IF_INVALID_DISPLAY(displayId);
285
Steven Thomas94e35b92017-07-26 18:48:28 -0700286 auto display = mDisplayData[displayId].hwcDisplay;
287 auto error = display->destroyLayer(layer);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700288 RETURN_IF_HWC_ERROR(error, displayId);
Steven Thomas94e35b92017-07-26 18:48:28 -0700289}
290
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800291nsecs_t HWComposer::getRefreshTimestamp(int32_t displayId) const {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700292 // this returns the last refresh timestamp.
293 // if the last one is not available, we estimate it based on
294 // the refresh period and whatever closest timestamp we have.
295 Mutex::Autolock _l(mLock);
296 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800297 auto vsyncPeriod = getActiveConfig(displayId)->getVsyncPeriod();
298 return now - ((now - mLastHwVSync[displayId]) % vsyncPeriod);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700299}
300
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800301bool HWComposer::isConnected(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700302 RETURN_IF_INVALID_DISPLAY(displayId, false);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800303 return mDisplayData[displayId].hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700304}
305
Dan Stoza9e56aa02015-11-02 13:00:03 -0800306std::vector<std::shared_ptr<const HWC2::Display::Config>>
307 HWComposer::getConfigs(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700308 RETURN_IF_INVALID_DISPLAY(displayId, {});
309
Dan Stoza9e56aa02015-11-02 13:00:03 -0800310 auto& displayData = mDisplayData[displayId];
311 auto configs = mDisplayData[displayId].hwcDisplay->getConfigs();
312 if (displayData.configMap.empty()) {
313 for (size_t i = 0; i < configs.size(); ++i) {
314 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700315 }
316 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800317 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700318}
319
Dan Stoza9e56aa02015-11-02 13:00:03 -0800320std::shared_ptr<const HWC2::Display::Config>
321 HWComposer::getActiveConfig(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700322 RETURN_IF_INVALID_DISPLAY(displayId, nullptr);
323
Dan Stoza9e56aa02015-11-02 13:00:03 -0800324 std::shared_ptr<const HWC2::Display::Config> config;
325 auto error = mDisplayData[displayId].hwcDisplay->getActiveConfig(&config);
326 if (error == HWC2::Error::BadConfig) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700327 LOG_DISPLAY_ERROR(displayId, "No active config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800328 return nullptr;
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700329 }
330
331 RETURN_IF_HWC_ERROR(error, displayId, nullptr);
332
333 if (!config) {
334 LOG_DISPLAY_ERROR(displayId, "Unknown config");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800335 return nullptr;
336 }
337
338 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700339}
340
Lloyd Pique3c085a02018-05-09 19:38:32 -0700341int HWComposer::getActiveConfigIndex(int32_t displayId) const {
342 if (!isValidDisplay(displayId)) {
343 ALOGV("getActiveConfigIndex: Attempted to access invalid display %d", displayId);
344 return -1;
345 }
346 int index;
347 auto error = mDisplayData[displayId].hwcDisplay->getActiveConfigIndex(&index);
348 if (error == HWC2::Error::BadConfig) {
349 ALOGE("getActiveConfigIndex: No config active, returning -1");
350 return -1;
351 } else if (error != HWC2::Error::None) {
352 ALOGE("getActiveConfigIndex failed for display %d: %s (%d)", displayId,
353 to_string(error).c_str(), static_cast<int32_t>(error));
354 return -1;
355 } else if (index < 0) {
356 ALOGE("getActiveConfigIndex returned an unknown config for display %d", displayId);
357 return -1;
358 }
359
360 return index;
361}
362
Peiyong Linfd997e02018-03-28 15:29:00 -0700363std::vector<ui::ColorMode> HWComposer::getColorModes(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700364 RETURN_IF_INVALID_DISPLAY(displayId, {});
365
Peiyong Linfd997e02018-03-28 15:29:00 -0700366 std::vector<ui::ColorMode> modes;
Steven Thomas94e35b92017-07-26 18:48:28 -0700367 auto error = mDisplayData[displayId].hwcDisplay->getColorModes(&modes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700368 RETURN_IF_HWC_ERROR(error, displayId, {});
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600369 return modes;
370}
371
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700372status_t HWComposer::setActiveColorMode(int32_t displayId, ui::ColorMode mode,
373 ui::RenderIntent renderIntent) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700374 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Michael Wright28f24d02016-07-12 13:30:53 -0700375
376 auto& displayData = mDisplayData[displayId];
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700377 auto error = displayData.hwcDisplay->setColorMode(mode, renderIntent);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700378 RETURN_IF_HWC_ERROR_FOR(("setColorMode(" + decodeColorMode(mode) + ", " +
379 decodeRenderIntent(renderIntent) + ")")
380 .c_str(),
381 error, displayId, UNKNOWN_ERROR);
Michael Wright28f24d02016-07-12 13:30:53 -0700382
383 return NO_ERROR;
384}
385
386
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800387void HWComposer::setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled) {
388 if (displayId < 0 || displayId >= HWC_DISPLAY_VIRTUAL) {
389 ALOGD("setVsyncEnabled: Ignoring for virtual display %d", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800390 return;
391 }
392
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700393 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800394
395 // NOTE: we use our own internal lock here because we have to call
396 // into the HWC with the lock held, and we want to make sure
397 // that even if HWC blocks (which it shouldn't), it won't
398 // affect other threads.
399 Mutex::Autolock _l(mVsyncLock);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800400 auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800401 if (enabled != displayData.vsyncEnabled) {
402 ATRACE_CALL();
403 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700404 RETURN_IF_HWC_ERROR(error, displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800405
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700406 displayData.vsyncEnabled = enabled;
407
408 char tag[16];
409 snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", displayId);
410 ATRACE_INT(tag, enabled == HWC2::Vsync::Enable ? 1 : 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700411 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700412}
413
Chia-I Wu06d63de2017-01-04 14:58:51 +0800414status_t HWComposer::setClientTarget(int32_t displayId, uint32_t slot,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800415 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700416 ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700417 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Jesse Hall851cfe82013-03-20 13:44:00 -0700418
Dan Stoza9e56aa02015-11-02 13:00:03 -0800419 ALOGV("setClientTarget for display %d", displayId);
420 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400421 auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700422 RETURN_IF_HWC_ERROR(error, displayId, BAD_VALUE);
Jesse Hall851cfe82013-03-20 13:44:00 -0700423 return NO_ERROR;
424}
425
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700426status_t HWComposer::prepare(DisplayDevice& display) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800427 ATRACE_CALL();
428
429 Mutex::Autolock _l(mDisplayLock);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700430 const auto displayId = display.getId();
Dan Stozaec0f7172016-07-21 11:09:40 -0700431 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
432 ALOGV("Skipping HWComposer prepare for non-HWC display");
433 return NO_ERROR;
434 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700435
436 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800437
438 auto& displayData = mDisplayData[displayId];
439 auto& hwcDisplay = displayData.hwcDisplay;
440 if (!hwcDisplay->isConnected()) {
441 return NO_ERROR;
442 }
443
444 uint32_t numTypes = 0;
445 uint32_t numRequests = 0;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700446
447 HWC2::Error error = HWC2::Error::None;
448
Chia-I Wu41b98d42017-12-11 11:04:36 -0800449 // First try to skip validate altogether when there is no client
450 // composition. When there is client composition, since we haven't
451 // rendered to the client target yet, we should not attempt to skip
452 // validate.
453 //
454 // displayData.hasClientComposition hasn't been updated for this frame.
455 // The check below is incorrect. We actually rely on HWC here to fall
456 // back to validate when there is any client layer.
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700457 displayData.validateWasSkipped = false;
Chia-I Wu41b98d42017-12-11 11:04:36 -0800458 if (!displayData.hasClientComposition) {
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700459 sp<android::Fence> outPresentFence;
460 uint32_t state = UINT32_MAX;
461 error = hwcDisplay->presentOrValidate(&numTypes, &numRequests, &outPresentFence , &state);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700462 if (error != HWC2::Error::HasChanges) {
463 RETURN_IF_HWC_ERROR_FOR("presentOrValidate", error, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700464 }
465 if (state == 1) { //Present Succeeded.
Steven Thomas94e35b92017-07-26 18:48:28 -0700466 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700467 error = hwcDisplay->getReleaseFences(&releaseFences);
468 displayData.releaseFences = std::move(releaseFences);
469 displayData.lastPresentFence = outPresentFence;
470 displayData.validateWasSkipped = true;
471 displayData.presentError = error;
472 return NO_ERROR;
473 }
474 // Present failed but Validate ran.
475 } else {
476 error = hwcDisplay->validate(&numTypes, &numRequests);
477 }
478 ALOGV("SkipValidate failed, Falling back to SLOW validate/present");
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700479 if (error != HWC2::Error::HasChanges) {
480 RETURN_IF_HWC_ERROR_FOR("validate", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800481 }
482
Steven Thomas94e35b92017-07-26 18:48:28 -0700483 std::unordered_map<HWC2::Layer*, HWC2::Composition> changedTypes;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800484 changedTypes.reserve(numTypes);
485 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700486 RETURN_IF_HWC_ERROR_FOR("getChangedCompositionTypes", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800487
488 displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0);
Steven Thomas94e35b92017-07-26 18:48:28 -0700489 std::unordered_map<HWC2::Layer*, HWC2::LayerRequest> layerRequests;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800490 layerRequests.reserve(numRequests);
491 error = hwcDisplay->getRequests(&displayData.displayRequests,
492 &layerRequests);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700493 RETURN_IF_HWC_ERROR_FOR("getRequests", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800494
495 displayData.hasClientComposition = false;
496 displayData.hasDeviceComposition = false;
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700497 for (auto& layer : display.getVisibleLayersSortedByZ()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800498 auto hwcLayer = layer->getHwcLayer(displayId);
499
500 if (changedTypes.count(hwcLayer) != 0) {
501 // We pass false so we only update our state and don't call back
502 // into the HWC device
503 validateChange(layer->getCompositionType(displayId),
504 changedTypes[hwcLayer]);
505 layer->setCompositionType(displayId, changedTypes[hwcLayer], false);
506 }
507
508 switch (layer->getCompositionType(displayId)) {
509 case HWC2::Composition::Client:
510 displayData.hasClientComposition = true;
511 break;
512 case HWC2::Composition::Device:
513 case HWC2::Composition::SolidColor:
514 case HWC2::Composition::Cursor:
515 case HWC2::Composition::Sideband:
516 displayData.hasDeviceComposition = true;
517 break;
518 default:
519 break;
520 }
521
522 if (layerRequests.count(hwcLayer) != 0 &&
523 layerRequests[hwcLayer] ==
524 HWC2::LayerRequest::ClearClientTarget) {
525 layer->setClearClientTarget(displayId, true);
526 } else {
527 if (layerRequests.count(hwcLayer) != 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700528 LOG_DISPLAY_ERROR(displayId,
529 ("Unknown layer request " + to_string(layerRequests[hwcLayer]))
530 .c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800531 }
532 layer->setClearClientTarget(displayId, false);
533 }
534 }
535
536 error = hwcDisplay->acceptChanges();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700537 RETURN_IF_HWC_ERROR_FOR("acceptChanges", error, displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800538
539 return NO_ERROR;
540}
541
542bool HWComposer::hasDeviceComposition(int32_t displayId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700543 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
544 // Displays without a corresponding HWC display are never composed by
545 // the device
546 return false;
547 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700548
549 RETURN_IF_INVALID_DISPLAY(displayId, false);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800550 return mDisplayData[displayId].hasDeviceComposition;
551}
552
Madhuri Athota88a905b2017-05-04 16:58:15 +0530553bool HWComposer::hasFlipClientTargetRequest(int32_t displayId) const {
554 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
555 // Displays without a corresponding HWC display are never composed by
556 // the device
557 return false;
558 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700559
560 RETURN_IF_INVALID_DISPLAY(displayId, false);
Madhuri Athota88a905b2017-05-04 16:58:15 +0530561 return ((static_cast<uint32_t>(mDisplayData[displayId].displayRequests) &
562 static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0);
563}
564
Dan Stoza9e56aa02015-11-02 13:00:03 -0800565bool HWComposer::hasClientComposition(int32_t displayId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700566 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
567 // Displays without a corresponding HWC display are always composed by
568 // the client
569 return true;
570 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700571
572 RETURN_IF_INVALID_DISPLAY(displayId, true);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800573 return mDisplayData[displayId].hasClientComposition;
574}
575
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800576sp<Fence> HWComposer::getPresentFence(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700577 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800578 return mDisplayData[displayId].lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700579}
580
Dan Stoza9e56aa02015-11-02 13:00:03 -0800581sp<Fence> HWComposer::getLayerReleaseFence(int32_t displayId,
Steven Thomas94e35b92017-07-26 18:48:28 -0700582 HWC2::Layer* layer) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700583 RETURN_IF_INVALID_DISPLAY(displayId, Fence::NO_FENCE);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800584 auto displayFences = mDisplayData[displayId].releaseFences;
585 if (displayFences.count(layer) == 0) {
586 ALOGV("getLayerReleaseFence: Release fence not found");
587 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700588 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800589 return displayFences[layer];
Riley Andrews03414a12014-07-01 14:22:59 -0700590}
591
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800592status_t HWComposer::presentAndGetReleaseFences(int32_t displayId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800593 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700594
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700595 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700596
Dan Stoza9e56aa02015-11-02 13:00:03 -0800597 auto& displayData = mDisplayData[displayId];
598 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700599
600 if (displayData.validateWasSkipped) {
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700601 // explicitly flush all pending commands
602 auto error = mHwcDevice->flushCommands();
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700603 RETURN_IF_HWC_ERROR_FOR("flushCommands", error, displayId, UNKNOWN_ERROR);
604 RETURN_IF_HWC_ERROR_FOR("present", displayData.presentError, displayId, UNKNOWN_ERROR);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700605 return NO_ERROR;
606 }
607
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800608 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700609 RETURN_IF_HWC_ERROR_FOR("present", error, displayId, UNKNOWN_ERROR);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700610
Steven Thomas94e35b92017-07-26 18:48:28 -0700611 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800612 error = hwcDisplay->getReleaseFences(&releaseFences);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700613 RETURN_IF_HWC_ERROR_FOR("getReleaseFences", error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800614
615 displayData.releaseFences = std::move(releaseFences);
616
617 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700618}
619
Dan Stoza9e56aa02015-11-02 13:00:03 -0800620status_t HWComposer::setPowerMode(int32_t displayId, int32_t intMode) {
621 ALOGV("setPowerMode(%d, %d)", displayId, intMode);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700622 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
623
Dan Stoza9e56aa02015-11-02 13:00:03 -0800624 if (displayId >= VIRTUAL_DISPLAY_ID_BASE) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700625 LOG_DISPLAY_ERROR(displayId, "Invalid operation on virtual display");
626 return INVALID_OPERATION;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800627 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700628
Dan Stoza9e56aa02015-11-02 13:00:03 -0800629 auto mode = static_cast<HWC2::PowerMode>(intMode);
630 if (mode == HWC2::PowerMode::Off) {
631 setVsyncEnabled(displayId, HWC2::Vsync::Disable);
632 }
633
634 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
635 switch (mode) {
636 case HWC2::PowerMode::Off:
637 case HWC2::PowerMode::On:
638 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
639 {
640 auto error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700641 if (error != HWC2::Error::None) {
642 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
643 error, displayId);
644 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700645 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800646 break;
647 case HWC2::PowerMode::Doze:
648 case HWC2::PowerMode::DozeSuspend:
649 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
650 {
651 bool supportsDoze = false;
652 auto error = hwcDisplay->supportsDoze(&supportsDoze);
Peiyong Lin306e4992018-05-07 16:18:22 -0700653 if (error != HWC2::Error::None) {
654 LOG_HWC_ERROR("supportsDoze", error, displayId);
655 }
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700656
Dan Stoza9e56aa02015-11-02 13:00:03 -0800657 if (!supportsDoze) {
658 mode = HWC2::PowerMode::On;
659 }
660
661 error = hwcDisplay->setPowerMode(mode);
Peiyong Lin306e4992018-05-07 16:18:22 -0700662 if (error != HWC2::Error::None) {
663 LOG_HWC_ERROR(("setPowerMode(" + to_string(mode) + ")").c_str(),
664 error, displayId);
665 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800666 }
667 break;
668 default:
669 ALOGV("setPowerMode: Not calling HWC");
670 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700671 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800672
673 return NO_ERROR;
674}
675
676status_t HWComposer::setActiveConfig(int32_t displayId, size_t configId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700677 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800678
679 auto& displayData = mDisplayData[displayId];
680 if (displayData.configMap.count(configId) == 0) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700681 LOG_DISPLAY_ERROR(displayId, ("Invalid config " + std::to_string(configId)).c_str());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800682 return BAD_INDEX;
683 }
684
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700685 auto error = displayData.hwcDisplay->setActiveConfig(displayData.configMap[configId]);
686 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800687 return NO_ERROR;
688}
689
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700690status_t HWComposer::setColorTransform(int32_t displayId,
691 const mat4& transform) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700692 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700693
694 auto& displayData = mDisplayData[displayId];
695 bool isIdentity = transform == mat4();
696 auto error = displayData.hwcDisplay->setColorTransform(transform,
697 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY :
698 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700699 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700700 return NO_ERROR;
701}
702
Dan Stoza9e56aa02015-11-02 13:00:03 -0800703void HWComposer::disconnectDisplay(int displayId) {
704 LOG_ALWAYS_FATAL_IF(displayId < 0);
705 auto& displayData = mDisplayData[displayId];
706
707 auto displayType = HWC2::DisplayType::Invalid;
708 auto error = displayData.hwcDisplay->getType(&displayType);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700709 RETURN_IF_HWC_ERROR_FOR("getType", error, displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800710
711 // If this was a virtual display, add its slot back for reuse by future
712 // virtual displays
713 if (displayType == HWC2::DisplayType::Virtual) {
714 mFreeDisplaySlots.insert(displayId);
715 ++mRemainingHwcVirtualDisplays;
716 }
717
Dominik Laskowski7e045462018-05-30 13:02:02 -0700718 const auto hwcDisplayId = displayData.hwcDisplay->getId();
719 mHwcDisplaySlots.erase(hwcDisplayId);
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700720 displayData = DisplayData();
Steven Thomas94e35b92017-07-26 18:48:28 -0700721
Dominik Laskowski7e045462018-05-30 13:02:02 -0700722 mHwcDevice->destroyDisplay(hwcDisplayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800723}
724
725status_t HWComposer::setOutputBuffer(int32_t displayId,
726 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700727 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800728
729 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
730 auto displayType = HWC2::DisplayType::Invalid;
731 auto error = hwcDisplay->getType(&displayType);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700732 RETURN_IF_HWC_ERROR_FOR("getType", error, displayId, NAME_NOT_FOUND);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800733
734 if (displayType != HWC2::DisplayType::Virtual) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700735 LOG_DISPLAY_ERROR(displayId, "Invalid operation on physical display");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800736 return INVALID_OPERATION;
737 }
738
739 error = hwcDisplay->setOutputBuffer(buffer, acquireFence);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700740 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800741 return NO_ERROR;
742}
743
744void HWComposer::clearReleaseFences(int32_t displayId) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700745 RETURN_IF_INVALID_DISPLAY(displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800746 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700747}
748
Peiyong Lin62665892018-04-16 11:07:44 -0700749status_t HWComposer::getHdrCapabilities(
750 int32_t displayId, HdrCapabilities* outCapabilities) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700751 RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
Dan Stozac4f471e2016-03-24 09:31:08 -0700752
753 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Peiyong Lin62665892018-04-16 11:07:44 -0700754 auto error = hwcDisplay->getHdrCapabilities(outCapabilities);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700755 RETURN_IF_HWC_ERROR(error, displayId, UNKNOWN_ERROR);
Peiyong Lin62665892018-04-16 11:07:44 -0700756 return NO_ERROR;
Dan Stozac4f471e2016-03-24 09:31:08 -0700757}
758
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700759int32_t HWComposer::getSupportedPerFrameMetadata(int32_t displayId) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700760 RETURN_IF_INVALID_DISPLAY(displayId, 0);
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700761
762 int32_t supportedMetadata;
763 auto error = mDisplayData[displayId].hwcDisplay->getSupportedPerFrameMetadata(
764 &supportedMetadata);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700765 RETURN_IF_HWC_ERROR(error, displayId, 0);
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700766 return supportedMetadata;
767}
768
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700769std::vector<ui::RenderIntent> HWComposer::getRenderIntents(int32_t displayId,
770 ui::ColorMode colorMode) const {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700771 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700772
773 std::vector<ui::RenderIntent> renderIntents;
774 auto error = mDisplayData[displayId].hwcDisplay->getRenderIntents(colorMode, &renderIntents);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700775 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700776 return renderIntents;
777}
778
779mat4 HWComposer::getDataspaceSaturationMatrix(int32_t displayId, ui::Dataspace dataspace) {
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700780 RETURN_IF_INVALID_DISPLAY(displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700781
782 mat4 matrix;
783 auto error = mDisplayData[displayId].hwcDisplay->getDataspaceSaturationMatrix(dataspace,
784 &matrix);
Dominik Laskowskifc2c0322018-04-19 14:47:33 -0700785 RETURN_IF_HWC_ERROR(error, displayId, {});
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700786 return matrix;
787}
788
Andy McFadden4df87bd2014-04-21 18:08:54 -0700789// Converts a PixelFormat to a human-readable string. Max 11 chars.
790// (Could use a table of prefab String8 objects.)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800791/*
Andy McFadden4df87bd2014-04-21 18:08:54 -0700792static String8 getFormatStr(PixelFormat format) {
793 switch (format) {
794 case PIXEL_FORMAT_RGBA_8888: return String8("RGBA_8888");
795 case PIXEL_FORMAT_RGBX_8888: return String8("RGBx_8888");
796 case PIXEL_FORMAT_RGB_888: return String8("RGB_888");
797 case PIXEL_FORMAT_RGB_565: return String8("RGB_565");
798 case PIXEL_FORMAT_BGRA_8888: return String8("BGRA_8888");
Andy McFaddenf0058ca2014-05-20 13:28:50 -0700799 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
800 return String8("ImplDef");
Andy McFadden4df87bd2014-04-21 18:08:54 -0700801 default:
802 String8 result;
803 result.appendFormat("? %08x", format);
804 return result;
805 }
806}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800807*/
Andy McFadden4df87bd2014-04-21 18:08:54 -0700808
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800809bool HWComposer::isUsingVrComposer() const {
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800810 return getComposer()->isUsingVrComposer();
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800811}
812
Mathias Agopian74d211a2013-04-22 16:55:35 +0200813void HWComposer::dump(String8& result) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800814 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow
815 // all the state going into the layers. This is probably better done in
816 // Layer itself, but it's going to take a bit of work to get there.
817 result.append(mHwcDevice->dump().c_str());
Mathias Agopian83727852010-09-23 18:13:21 -0700818}
819
Steven Thomas6e8f7062017-11-22 14:15:29 -0800820std::optional<hwc2_display_t>
821HWComposer::getHwcDisplayId(int32_t displayId) const {
822 if (!isValidDisplay(displayId)) {
823 return {};
824 }
825 return mDisplayData[displayId].hwcDisplay->getId();
826}
827
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700828} // namespace android