blob: 0f49e6137baf3b438a2c699fc3feca550ff90381 [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
Mathias Agopian921e6ac2012-07-23 23:11:29 -070039#include <ui/GraphicBuffer.h>
40
Mathias Agopiana350ff92010-08-10 17:14:02 -070041#include <hardware/hardware.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070042#include <hardware/hwcomposer.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070043
Jesse Hall1c569c42013-04-05 13:44:52 -070044#include <android/configuration.h>
45
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070046#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070047#include <log/log.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070048
Mathias Agopiana350ff92010-08-10 17:14:02 -070049#include "HWComposer.h"
Fabien Sanglarde29055f2017-03-08 11:36:46 -080050#include "hwc2on1adapter/HWC2On1Adapter.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
57namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -070058
Jesse Hall72960512013-01-10 16:19:56 -080059#define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION
Jesse Hall9eb1eb52012-08-29 10:39:38 -070060
Mathias Agopiana350ff92010-08-10 17:14:02 -070061// ---------------------------------------------------------------------------
62
Steven Thomas3cfac282017-02-06 12:29:30 -080063HWComposer::HWComposer(bool useVrComposer)
64 : mAdapter(),
Dan Stoza9e56aa02015-11-02 13:00:03 -080065 mHwcDevice(),
66 mDisplayData(2),
67 mFreeDisplaySlots(),
68 mHwcDisplaySlots(),
69 mCBContext(),
70 mEventHandler(nullptr),
71 mVSyncCounts(),
Chia-I Wu15b27e02017-05-11 16:06:07 -070072 mRemainingHwcVirtualDisplays(0)
Mathias Agopiana350ff92010-08-10 17:14:02 -070073{
Mathias Agopianbef42c52013-08-21 17:45:46 -070074 for (size_t i=0 ; i<HWC_NUM_PHYSICAL_DISPLAY_TYPES ; i++) {
75 mLastHwVSync[i] = 0;
76 mVSyncCounts[i] = 0;
77 }
78
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -080079 loadHwcModule(useVrComposer);
Mathias Agopiana350ff92010-08-10 17:14:02 -070080}
81
Dan Stoza9e56aa02015-11-02 13:00:03 -080082HWComposer::~HWComposer() {}
83
84void HWComposer::setEventHandler(EventHandler* handler)
85{
86 if (handler == nullptr) {
87 ALOGE("setEventHandler: Rejected attempt to clear handler");
88 return;
Andy McFaddenbabba182012-09-12 13:14:51 -070089 }
Dan Stoza9e56aa02015-11-02 13:00:03 -080090
91 bool wasNull = (mEventHandler == nullptr);
92 mEventHandler = handler;
93
94 if (wasNull) {
95 auto hotplugHook = std::bind(&HWComposer::hotplug, this,
96 std::placeholders::_1, std::placeholders::_2);
97 mHwcDevice->registerHotplugCallback(hotplugHook);
98 auto invalidateHook = std::bind(&HWComposer::invalidate, this,
99 std::placeholders::_1);
100 mHwcDevice->registerRefreshCallback(invalidateHook);
101 auto vsyncHook = std::bind(&HWComposer::vsync, this,
102 std::placeholders::_1, std::placeholders::_2);
103 mHwcDevice->registerVsyncCallback(vsyncHook);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700104 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700105}
106
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700107// Load and prepare the hardware composer module. Sets mHwc.
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800108void HWComposer::loadHwcModule(bool useVrComposer)
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700109{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800110 ALOGV("loadHwcModule");
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800111 mHwcDevice = std::make_unique<HWC2::Device>(useVrComposer);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800112 mRemainingHwcVirtualDisplays = mHwcDevice->getMaxVirtualDisplayCount();
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
Dan Stoza9e56aa02015-11-02 13:00:03 -0800150void HWComposer::hotplug(const std::shared_ptr<HWC2::Display>& display,
151 HWC2::Connection connected) {
152 ALOGV("hotplug: %" PRIu64 ", %s", display->getId(),
153 to_string(connected).c_str());
154 int32_t disp = 0;
155 if (!mDisplayData[0].hwcDisplay) {
156 ALOGE_IF(connected != HWC2::Connection::Connected, "Assumed primary"
157 " display would be connected");
158 mDisplayData[0].hwcDisplay = display;
159 mHwcDisplaySlots[display->getId()] = 0;
160 disp = DisplayDevice::DISPLAY_PRIMARY;
161 } else {
162 // Disconnect is handled through HWComposer::disconnectDisplay via
163 // SurfaceFlinger's onHotplugReceived callback handling
164 if (connected == HWC2::Connection::Connected) {
165 mDisplayData[1].hwcDisplay = display;
166 mHwcDisplaySlots[display->getId()] = 1;
167 }
168 disp = DisplayDevice::DISPLAY_EXTERNAL;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700169 }
Stephen Kiazyk7d3dcb92017-04-05 16:46:49 -0700170 mEventHandler->onHotplugReceived(this, disp,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800171 connected == HWC2::Connection::Connected);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700172}
173
Dan Stoza9e56aa02015-11-02 13:00:03 -0800174void HWComposer::invalidate(const std::shared_ptr<HWC2::Display>& /*display*/) {
Steven Thomas3cfac282017-02-06 12:29:30 -0800175 mEventHandler->onInvalidateReceived(this);
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700176}
177
Dan Stoza9e56aa02015-11-02 13:00:03 -0800178void HWComposer::vsync(const std::shared_ptr<HWC2::Display>& display,
179 int64_t timestamp) {
180 auto displayType = HWC2::DisplayType::Invalid;
181 auto error = display->getType(&displayType);
182 if (error != HWC2::Error::None) {
183 ALOGE("vsync: Failed to determine type of display %" PRIu64,
184 display->getId());
Jesse Hall1bd20e02012-08-29 10:47:52 -0700185 return;
186 }
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());
191 return;
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());
197 return;
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);
211 return;
212 }
213
214 mLastHwVSync[disp] = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700215 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800216
217 char tag[16];
218 snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", disp);
219 ATRACE_INT(tag, ++mVSyncCounts[disp] & 1);
220
Steven Thomas3cfac282017-02-06 12:29:30 -0800221 mEventHandler->onVSyncReceived(this, disp, timestamp);
Jesse Hall1c569c42013-04-05 13:44:52 -0700222}
223
Dan Stoza9e56aa02015-11-02 13:00:03 -0800224status_t HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
Dan Stoza5cf424b2016-05-20 14:02:39 -0700225 android_pixel_format_t* format, int32_t *outId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800226 if (mRemainingHwcVirtualDisplays == 0) {
227 ALOGE("allocateVirtualDisplay: No remaining virtual displays");
Mathias Agopiane60b0682012-08-21 23:34:09 -0700228 return NO_MEMORY;
229 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700230
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800231 if (SurfaceFlinger::maxVirtualDisplaySize != 0 &&
232 (width > SurfaceFlinger::maxVirtualDisplaySize ||
233 height > SurfaceFlinger::maxVirtualDisplaySize)) {
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800234 ALOGE("createVirtualDisplay: Can't create a virtual display with"
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800235 " a dimension > %" PRIu64 " (tried %u x %u)",
236 SurfaceFlinger::maxVirtualDisplaySize, width, height);
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800237 return INVALID_OPERATION;
238 }
239
Dan Stoza9e56aa02015-11-02 13:00:03 -0800240 std::shared_ptr<HWC2::Display> display;
Dan Stoza5cf424b2016-05-20 14:02:39 -0700241 auto error = mHwcDevice->createVirtualDisplay(width, height, format,
242 &display);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800243 if (error != HWC2::Error::None) {
244 ALOGE("allocateVirtualDisplay: Failed to create HWC virtual display");
245 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700246 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800247
248 size_t displaySlot = 0;
249 if (!mFreeDisplaySlots.empty()) {
250 displaySlot = *mFreeDisplaySlots.begin();
251 mFreeDisplaySlots.erase(displaySlot);
252 } else if (mDisplayData.size() < INT32_MAX) {
253 // Don't bother allocating a slot larger than we can return
254 displaySlot = mDisplayData.size();
255 mDisplayData.resize(displaySlot + 1);
256 } else {
257 ALOGE("allocateVirtualDisplay: Unable to allocate a display slot");
258 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700259 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800260
261 mDisplayData[displaySlot].hwcDisplay = display;
262
263 --mRemainingHwcVirtualDisplays;
264 *outId = static_cast<int32_t>(displaySlot);
265
Mathias Agopiane60b0682012-08-21 23:34:09 -0700266 return NO_ERROR;
267}
268
Dan Stoza9e56aa02015-11-02 13:00:03 -0800269std::shared_ptr<HWC2::Layer> HWComposer::createLayer(int32_t displayId) {
270 if (!isValidDisplay(displayId)) {
271 ALOGE("Failed to create layer on invalid display %d", displayId);
272 return nullptr;
273 }
274 auto display = mDisplayData[displayId].hwcDisplay;
275 std::shared_ptr<HWC2::Layer> layer;
276 auto error = display->createLayer(&layer);
277 if (error != HWC2::Error::None) {
278 ALOGE("Failed to create layer on display %d: %s (%d)", displayId,
279 to_string(error).c_str(), static_cast<int32_t>(error));
280 return nullptr;
281 }
282 return layer;
283}
284
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800285nsecs_t HWComposer::getRefreshTimestamp(int32_t displayId) const {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700286 // this returns the last refresh timestamp.
287 // if the last one is not available, we estimate it based on
288 // the refresh period and whatever closest timestamp we have.
289 Mutex::Autolock _l(mLock);
290 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800291 auto vsyncPeriod = getActiveConfig(displayId)->getVsyncPeriod();
292 return now - ((now - mLastHwVSync[displayId]) % vsyncPeriod);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700293}
294
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800295bool HWComposer::isConnected(int32_t displayId) const {
296 if (!isValidDisplay(displayId)) {
297 ALOGE("isConnected: Attempted to access invalid display %d", displayId);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700298 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800299 }
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800300 return mDisplayData[displayId].hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700301}
302
Dan Stoza9e56aa02015-11-02 13:00:03 -0800303std::vector<std::shared_ptr<const HWC2::Display::Config>>
304 HWComposer::getConfigs(int32_t displayId) const {
305 if (!isValidDisplay(displayId)) {
306 ALOGE("getConfigs: Attempted to access invalid display %d", displayId);
307 return {};
308 }
309 auto& displayData = mDisplayData[displayId];
310 auto configs = mDisplayData[displayId].hwcDisplay->getConfigs();
311 if (displayData.configMap.empty()) {
312 for (size_t i = 0; i < configs.size(); ++i) {
313 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700314 }
315 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800316 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700317}
318
Dan Stoza9e56aa02015-11-02 13:00:03 -0800319std::shared_ptr<const HWC2::Display::Config>
320 HWComposer::getActiveConfig(int32_t displayId) const {
321 if (!isValidDisplay(displayId)) {
Fabien Sanglardb7432cc2016-11-11 09:40:27 -0800322 ALOGV("getActiveConfigs: Attempted to access invalid display %d",
Dan Stoza9e56aa02015-11-02 13:00:03 -0800323 displayId);
324 return nullptr;
Mathias Agopian58959342010-10-07 14:57:04 -0700325 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800326 std::shared_ptr<const HWC2::Display::Config> config;
327 auto error = mDisplayData[displayId].hwcDisplay->getActiveConfig(&config);
328 if (error == HWC2::Error::BadConfig) {
Fabien Sanglardb7432cc2016-11-11 09:40:27 -0800329 ALOGE("getActiveConfig: No config active, returning null");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800330 return nullptr;
331 } else if (error != HWC2::Error::None) {
332 ALOGE("getActiveConfig failed for display %d: %s (%d)", displayId,
333 to_string(error).c_str(), static_cast<int32_t>(error));
334 return nullptr;
335 } else if (!config) {
336 ALOGE("getActiveConfig returned an unknown config for display %d",
337 displayId);
338 return nullptr;
339 }
340
341 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700342}
343
Michael Wright28f24d02016-07-12 13:30:53 -0700344std::vector<android_color_mode_t> HWComposer::getColorModes(int32_t displayId) const {
345 std::vector<android_color_mode_t> modes;
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600346
347 if (!isValidDisplay(displayId)) {
348 ALOGE("getColorModes: Attempted to access invalid display %d",
349 displayId);
350 return modes;
351 }
352 const std::shared_ptr<HWC2::Display>& hwcDisplay =
353 mDisplayData[displayId].hwcDisplay;
354
355 auto error = hwcDisplay->getColorModes(&modes);
356 if (error != HWC2::Error::None) {
357 ALOGE("getColorModes failed for display %d: %s (%d)", displayId,
358 to_string(error).c_str(), static_cast<int32_t>(error));
Michael Wright28f24d02016-07-12 13:30:53 -0700359 return std::vector<android_color_mode_t>();
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600360 }
361
362 return modes;
363}
364
Michael Wright28f24d02016-07-12 13:30:53 -0700365status_t HWComposer::setActiveColorMode(int32_t displayId, android_color_mode_t mode) {
366 if (!isValidDisplay(displayId)) {
367 ALOGE("setActiveColorMode: Display %d is not valid", displayId);
368 return BAD_INDEX;
369 }
370
371 auto& displayData = mDisplayData[displayId];
372 auto error = displayData.hwcDisplay->setColorMode(mode);
373 if (error != HWC2::Error::None) {
374 ALOGE("setActiveConfig: Failed to set color mode %d on display %d: "
375 "%s (%d)", mode, displayId, to_string(error).c_str(),
376 static_cast<int32_t>(error));
377 return UNKNOWN_ERROR;
378 }
379
380 return NO_ERROR;
381}
382
383
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800384void HWComposer::setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled) {
385 if (displayId < 0 || displayId >= HWC_DISPLAY_VIRTUAL) {
386 ALOGD("setVsyncEnabled: Ignoring for virtual display %d", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800387 return;
388 }
389
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800390 if (!isValidDisplay(displayId)) {
391 ALOGE("setVsyncEnabled: Attempted to access invalid display %d",
392 displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800393 return;
394 }
395
396 // NOTE: we use our own internal lock here because we have to call
397 // into the HWC with the lock held, and we want to make sure
398 // that even if HWC blocks (which it shouldn't), it won't
399 // affect other threads.
400 Mutex::Autolock _l(mVsyncLock);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800401 auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800402 if (enabled != displayData.vsyncEnabled) {
403 ATRACE_CALL();
404 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
405 if (error == HWC2::Error::None) {
406 displayData.vsyncEnabled = enabled;
407
408 char tag[16];
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800409 snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800410 ATRACE_INT(tag, enabled == HWC2::Vsync::Enable ? 1 : 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700411 } else {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800412 ALOGE("setVsyncEnabled: Failed to set vsync to %s on %d/%" PRIu64
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800413 ": %s (%d)", to_string(enabled).c_str(), displayId,
414 mDisplayData[displayId].hwcDisplay->getId(),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800415 to_string(error).c_str(), static_cast<int32_t>(error));
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700416 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700417 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700418}
419
Chia-I Wu06d63de2017-01-04 14:58:51 +0800420status_t HWComposer::setClientTarget(int32_t displayId, uint32_t slot,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800421 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
422 android_dataspace_t dataspace) {
423 if (!isValidDisplay(displayId)) {
Jesse Hall851cfe82013-03-20 13:44:00 -0700424 return BAD_INDEX;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800425 }
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);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800430 if (error != HWC2::Error::None) {
431 ALOGE("Failed to set client target for display %d: %s (%d)", displayId,
432 to_string(error).c_str(), static_cast<int32_t>(error));
433 return BAD_VALUE;
434 }
435
Jesse Hall851cfe82013-03-20 13:44:00 -0700436 return NO_ERROR;
437}
438
Dan Stoza9e56aa02015-11-02 13:00:03 -0800439status_t HWComposer::prepare(DisplayDevice& displayDevice) {
440 ATRACE_CALL();
441
442 Mutex::Autolock _l(mDisplayLock);
443 auto displayId = displayDevice.getHwcDisplayId();
Dan Stozaec0f7172016-07-21 11:09:40 -0700444 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
445 ALOGV("Skipping HWComposer prepare for non-HWC display");
446 return NO_ERROR;
447 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800448 if (!isValidDisplay(displayId)) {
449 return BAD_INDEX;
450 }
451
452 auto& displayData = mDisplayData[displayId];
453 auto& hwcDisplay = displayData.hwcDisplay;
454 if (!hwcDisplay->isConnected()) {
455 return NO_ERROR;
456 }
457
458 uint32_t numTypes = 0;
459 uint32_t numRequests = 0;
460 auto error = hwcDisplay->validate(&numTypes, &numRequests);
461 if (error != HWC2::Error::None && error != HWC2::Error::HasChanges) {
462 ALOGE("prepare: validate failed for display %d: %s (%d)", displayId,
463 to_string(error).c_str(), static_cast<int32_t>(error));
464 return BAD_INDEX;
465 }
466
467 std::unordered_map<std::shared_ptr<HWC2::Layer>, HWC2::Composition>
468 changedTypes;
469 changedTypes.reserve(numTypes);
470 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
471 if (error != HWC2::Error::None) {
472 ALOGE("prepare: getChangedCompositionTypes failed on display %d: "
473 "%s (%d)", displayId, to_string(error).c_str(),
474 static_cast<int32_t>(error));
475 return BAD_INDEX;
476 }
477
478
479 displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0);
480 std::unordered_map<std::shared_ptr<HWC2::Layer>, HWC2::LayerRequest>
481 layerRequests;
482 layerRequests.reserve(numRequests);
483 error = hwcDisplay->getRequests(&displayData.displayRequests,
484 &layerRequests);
485 if (error != HWC2::Error::None) {
486 ALOGE("prepare: getRequests failed on display %d: %s (%d)", displayId,
487 to_string(error).c_str(), static_cast<int32_t>(error));
488 return BAD_INDEX;
489 }
490
491 displayData.hasClientComposition = false;
492 displayData.hasDeviceComposition = false;
493 for (auto& layer : displayDevice.getVisibleLayersSortedByZ()) {
494 auto hwcLayer = layer->getHwcLayer(displayId);
495
496 if (changedTypes.count(hwcLayer) != 0) {
497 // We pass false so we only update our state and don't call back
498 // into the HWC device
499 validateChange(layer->getCompositionType(displayId),
500 changedTypes[hwcLayer]);
501 layer->setCompositionType(displayId, changedTypes[hwcLayer], false);
502 }
503
504 switch (layer->getCompositionType(displayId)) {
505 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
518 if (layerRequests.count(hwcLayer) != 0 &&
519 layerRequests[hwcLayer] ==
520 HWC2::LayerRequest::ClearClientTarget) {
521 layer->setClearClientTarget(displayId, true);
522 } else {
523 if (layerRequests.count(hwcLayer) != 0) {
524 ALOGE("prepare: Unknown layer request: %s",
525 to_string(layerRequests[hwcLayer]).c_str());
526 }
527 layer->setClearClientTarget(displayId, false);
528 }
529 }
530
531 error = hwcDisplay->acceptChanges();
532 if (error != HWC2::Error::None) {
533 ALOGE("prepare: acceptChanges failed: %s", to_string(error).c_str());
534 return BAD_INDEX;
535 }
536
537 return NO_ERROR;
538}
539
540bool HWComposer::hasDeviceComposition(int32_t displayId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700541 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
542 // Displays without a corresponding HWC display are never composed by
543 // the device
544 return false;
545 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800546 if (!isValidDisplay(displayId)) {
547 ALOGE("hasDeviceComposition: Invalid display %d", displayId);
548 return false;
549 }
550 return mDisplayData[displayId].hasDeviceComposition;
551}
552
553bool HWComposer::hasClientComposition(int32_t displayId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700554 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
555 // Displays without a corresponding HWC display are always composed by
556 // the client
557 return true;
558 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800559 if (!isValidDisplay(displayId)) {
560 ALOGE("hasClientComposition: Invalid display %d", displayId);
561 return true;
562 }
563 return mDisplayData[displayId].hasClientComposition;
564}
565
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800566sp<Fence> HWComposer::getPresentFence(int32_t displayId) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800567 if (!isValidDisplay(displayId)) {
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800568 ALOGE("getPresentFence failed for invalid display %d", displayId);
Jesse Hall851cfe82013-03-20 13:44:00 -0700569 return Fence::NO_FENCE;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800570 }
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800571 return mDisplayData[displayId].lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700572}
573
Dan Stoza9e56aa02015-11-02 13:00:03 -0800574sp<Fence> HWComposer::getLayerReleaseFence(int32_t displayId,
575 const std::shared_ptr<HWC2::Layer>& layer) const {
576 if (!isValidDisplay(displayId)) {
577 ALOGE("getLayerReleaseFence: Invalid display");
578 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700579 }
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
Dan Stoza9e56aa02015-11-02 13:00:03 -0800591 if (!isValidDisplay(displayId)) {
592 return BAD_INDEX;
Mathias Agopianc3973602012-08-31 17:51:25 -0700593 }
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700594
Dan Stoza9e56aa02015-11-02 13:00:03 -0800595 auto& displayData = mDisplayData[displayId];
596 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800597 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800598 if (error != HWC2::Error::None) {
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800599 ALOGE("presentAndGetReleaseFences: failed for display %d: %s (%d)",
600 displayId, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800601 return UNKNOWN_ERROR;
602 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700603
Dan Stoza9e56aa02015-11-02 13:00:03 -0800604 std::unordered_map<std::shared_ptr<HWC2::Layer>, sp<Fence>> releaseFences;
605 error = hwcDisplay->getReleaseFences(&releaseFences);
606 if (error != HWC2::Error::None) {
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800607 ALOGE("presentAndGetReleaseFences: Failed to get release fences "
608 "for display %d: %s (%d)",
Dan Stoza9e56aa02015-11-02 13:00:03 -0800609 displayId, to_string(error).c_str(),
610 static_cast<int32_t>(error));
611 return UNKNOWN_ERROR;
Mathias Agopianf4358632012-08-22 17:16:19 -0700612 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800613
614 displayData.releaseFences = std::move(releaseFences);
615
616 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700617}
618
Dan Stoza9e56aa02015-11-02 13:00:03 -0800619status_t HWComposer::setPowerMode(int32_t displayId, int32_t intMode) {
620 ALOGV("setPowerMode(%d, %d)", displayId, intMode);
621 if (!isValidDisplay(displayId)) {
622 ALOGE("setPowerMode: Bad display");
623 return BAD_INDEX;
624 }
625 if (displayId >= VIRTUAL_DISPLAY_ID_BASE) {
626 ALOGE("setPowerMode: Virtual display %d passed in, returning",
627 displayId);
628 return BAD_INDEX;
629 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700630
Dan Stoza9e56aa02015-11-02 13:00:03 -0800631 auto mode = static_cast<HWC2::PowerMode>(intMode);
632 if (mode == HWC2::PowerMode::Off) {
633 setVsyncEnabled(displayId, HWC2::Vsync::Disable);
634 }
635
636 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
637 switch (mode) {
638 case HWC2::PowerMode::Off:
639 case HWC2::PowerMode::On:
640 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
641 {
642 auto error = hwcDisplay->setPowerMode(mode);
643 if (error != HWC2::Error::None) {
644 ALOGE("setPowerMode: Unable to set power mode %s for "
645 "display %d: %s (%d)", to_string(mode).c_str(),
646 displayId, to_string(error).c_str(),
647 static_cast<int32_t>(error));
Mathias Agopianda27af92012-09-13 18:17:13 -0700648 }
649 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800650 break;
651 case HWC2::PowerMode::Doze:
652 case HWC2::PowerMode::DozeSuspend:
653 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
654 {
655 bool supportsDoze = false;
656 auto error = hwcDisplay->supportsDoze(&supportsDoze);
657 if (error != HWC2::Error::None) {
658 ALOGE("setPowerMode: Unable to query doze support for "
659 "display %d: %s (%d)", displayId,
660 to_string(error).c_str(),
661 static_cast<int32_t>(error));
662 }
663 if (!supportsDoze) {
664 mode = HWC2::PowerMode::On;
665 }
666
667 error = hwcDisplay->setPowerMode(mode);
668 if (error != HWC2::Error::None) {
669 ALOGE("setPowerMode: Unable to set power mode %s for "
670 "display %d: %s (%d)", to_string(mode).c_str(),
671 displayId, to_string(error).c_str(),
672 static_cast<int32_t>(error));
673 }
674 }
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) {
685 if (!isValidDisplay(displayId)) {
686 ALOGE("setActiveConfig: Display %d is not valid", displayId);
687 return BAD_INDEX;
688 }
689
690 auto& displayData = mDisplayData[displayId];
691 if (displayData.configMap.count(configId) == 0) {
692 ALOGE("setActiveConfig: Invalid config %zd", configId);
693 return BAD_INDEX;
694 }
695
696 auto error = displayData.hwcDisplay->setActiveConfig(
697 displayData.configMap[configId]);
698 if (error != HWC2::Error::None) {
699 ALOGE("setActiveConfig: Failed to set config %zu on display %d: "
700 "%s (%d)", configId, displayId, to_string(error).c_str(),
701 static_cast<int32_t>(error));
702 return UNKNOWN_ERROR;
703 }
704
705 return NO_ERROR;
706}
707
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700708status_t HWComposer::setColorTransform(int32_t displayId,
709 const mat4& transform) {
710 if (!isValidDisplay(displayId)) {
711 ALOGE("setColorTransform: Display %d is not valid", displayId);
712 return BAD_INDEX;
713 }
714
715 auto& displayData = mDisplayData[displayId];
716 bool isIdentity = transform == mat4();
717 auto error = displayData.hwcDisplay->setColorTransform(transform,
718 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY :
719 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX);
720 if (error != HWC2::Error::None) {
721 ALOGE("setColorTransform: Failed to set transform on display %d: "
722 "%s (%d)", displayId, to_string(error).c_str(),
723 static_cast<int32_t>(error));
724 return UNKNOWN_ERROR;
725 }
726
727 return NO_ERROR;
728}
729
Dan Stoza9e56aa02015-11-02 13:00:03 -0800730void HWComposer::disconnectDisplay(int displayId) {
731 LOG_ALWAYS_FATAL_IF(displayId < 0);
732 auto& displayData = mDisplayData[displayId];
733
734 auto displayType = HWC2::DisplayType::Invalid;
735 auto error = displayData.hwcDisplay->getType(&displayType);
736 if (error != HWC2::Error::None) {
737 ALOGE("disconnectDisplay: Failed to determine type of display %d",
738 displayId);
739 return;
740 }
741
742 // If this was a virtual display, add its slot back for reuse by future
743 // virtual displays
744 if (displayType == HWC2::DisplayType::Virtual) {
745 mFreeDisplaySlots.insert(displayId);
746 ++mRemainingHwcVirtualDisplays;
747 }
748
749 auto hwcId = displayData.hwcDisplay->getId();
750 mHwcDisplaySlots.erase(hwcId);
751 displayData.reset();
752}
753
754status_t HWComposer::setOutputBuffer(int32_t displayId,
755 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
756 if (!isValidDisplay(displayId)) {
757 ALOGE("setOutputBuffer: Display %d is not valid", displayId);
758 return BAD_INDEX;
759 }
760
761 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
762 auto displayType = HWC2::DisplayType::Invalid;
763 auto error = hwcDisplay->getType(&displayType);
764 if (error != HWC2::Error::None) {
765 ALOGE("setOutputBuffer: Failed to determine type of display %d",
766 displayId);
767 return NAME_NOT_FOUND;
768 }
769
770 if (displayType != HWC2::DisplayType::Virtual) {
771 ALOGE("setOutputBuffer: Display %d is not virtual", displayId);
772 return INVALID_OPERATION;
773 }
774
775 error = hwcDisplay->setOutputBuffer(buffer, acquireFence);
776 if (error != HWC2::Error::None) {
777 ALOGE("setOutputBuffer: Failed to set buffer on display %d: %s (%d)",
778 displayId, to_string(error).c_str(),
779 static_cast<int32_t>(error));
780 return UNKNOWN_ERROR;
781 }
782
783 return NO_ERROR;
784}
785
786void HWComposer::clearReleaseFences(int32_t displayId) {
787 if (!isValidDisplay(displayId)) {
788 ALOGE("clearReleaseFences: Display %d is not valid", displayId);
789 return;
790 }
791 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700792}
793
Dan Stozac4f471e2016-03-24 09:31:08 -0700794std::unique_ptr<HdrCapabilities> HWComposer::getHdrCapabilities(
795 int32_t displayId) {
796 if (!isValidDisplay(displayId)) {
797 ALOGE("getHdrCapabilities: Display %d is not valid", displayId);
798 return nullptr;
799 }
800
801 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
802 std::unique_ptr<HdrCapabilities> capabilities;
803 auto error = hwcDisplay->getHdrCapabilities(&capabilities);
804 if (error != HWC2::Error::None) {
805 ALOGE("getOutputCapabilities: Failed to get capabilities on display %d:"
806 " %s (%d)", displayId, to_string(error).c_str(),
807 static_cast<int32_t>(error));
808 return nullptr;
809 }
810
811 return capabilities;
812}
813
Andy McFadden4df87bd2014-04-21 18:08:54 -0700814// Converts a PixelFormat to a human-readable string. Max 11 chars.
815// (Could use a table of prefab String8 objects.)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800816/*
Andy McFadden4df87bd2014-04-21 18:08:54 -0700817static String8 getFormatStr(PixelFormat format) {
818 switch (format) {
819 case PIXEL_FORMAT_RGBA_8888: return String8("RGBA_8888");
820 case PIXEL_FORMAT_RGBX_8888: return String8("RGBx_8888");
821 case PIXEL_FORMAT_RGB_888: return String8("RGB_888");
822 case PIXEL_FORMAT_RGB_565: return String8("RGB_565");
823 case PIXEL_FORMAT_BGRA_8888: return String8("BGRA_8888");
Andy McFaddenf0058ca2014-05-20 13:28:50 -0700824 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
825 return String8("ImplDef");
Andy McFadden4df87bd2014-04-21 18:08:54 -0700826 default:
827 String8 result;
828 result.appendFormat("? %08x", format);
829 return result;
830 }
831}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800832*/
Andy McFadden4df87bd2014-04-21 18:08:54 -0700833
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800834bool HWComposer::isUsingVrComposer() const {
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800835 return getComposer()->isUsingVrComposer();
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800836}
837
Mathias Agopian74d211a2013-04-22 16:55:35 +0200838void HWComposer::dump(String8& result) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800839 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow
840 // all the state going into the layers. This is probably better done in
841 // Layer itself, but it's going to take a bit of work to get there.
842 result.append(mHwcDevice->dump().c_str());
Mathias Agopian83727852010-09-23 18:13:21 -0700843}
844
Mathias Agopiana350ff92010-08-10 17:14:02 -0700845// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -0700846
Jesse Halla9a1b002013-02-27 16:39:25 -0800847HWComposer::DisplayData::DisplayData()
Dan Stoza9e56aa02015-11-02 13:00:03 -0800848 : hasClientComposition(false),
849 hasDeviceComposition(false),
850 hwcDisplay(),
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800851 lastPresentFence(Fence::NO_FENCE),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800852 outbufHandle(nullptr),
853 outbufAcquireFence(Fence::NO_FENCE),
854 vsyncEnabled(HWC2::Vsync::Disable) {
855 ALOGV("Created new DisplayData");
856}
Jesse Halla9a1b002013-02-27 16:39:25 -0800857
858HWComposer::DisplayData::~DisplayData() {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800859}
860
861void HWComposer::DisplayData::reset() {
862 ALOGV("DisplayData reset");
863 *this = DisplayData();
Jesse Halla9a1b002013-02-27 16:39:25 -0800864}
865
Mathias Agopian2965b262012-04-08 15:13:32 -0700866// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -0700867}; // namespace android