blob: 42be935c71c15ca7ebbc93ae2e81becbfc4b423d [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"
Dan Stoza9e56aa02015-11-02 13:00:03 -080050#include "HWC2.h"
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -080051#include "ComposerHal.h"
Mathias Agopian33ceeb32013-04-01 16:54:58 -070052
53#include "../Layer.h" // needed only for debugging
54#include "../SurfaceFlinger.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070055
56namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -070057
Jesse Hall72960512013-01-10 16:19:56 -080058#define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION
Jesse Hall9eb1eb52012-08-29 10:39:38 -070059
Mathias Agopiana350ff92010-08-10 17:14:02 -070060// ---------------------------------------------------------------------------
61
Steven Thomas3cfac282017-02-06 12:29:30 -080062HWComposer::HWComposer(bool useVrComposer)
Logan Chien23429772017-05-16 10:18:17 +080063 : mHwcDevice(),
Dan Stoza9e56aa02015-11-02 13:00:03 -080064 mDisplayData(2),
65 mFreeDisplaySlots(),
66 mHwcDisplaySlots(),
67 mCBContext(),
68 mEventHandler(nullptr),
69 mVSyncCounts(),
Chia-I Wu15b27e02017-05-11 16:06:07 -070070 mRemainingHwcVirtualDisplays(0)
Mathias Agopiana350ff92010-08-10 17:14:02 -070071{
Mathias Agopianbef42c52013-08-21 17:45:46 -070072 for (size_t i=0 ; i<HWC_NUM_PHYSICAL_DISPLAY_TYPES ; i++) {
73 mLastHwVSync[i] = 0;
74 mVSyncCounts[i] = 0;
75 }
76
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -080077 loadHwcModule(useVrComposer);
Mathias Agopiana350ff92010-08-10 17:14:02 -070078}
79
Dan Stoza9e56aa02015-11-02 13:00:03 -080080HWComposer::~HWComposer() {}
81
82void HWComposer::setEventHandler(EventHandler* handler)
83{
84 if (handler == nullptr) {
85 ALOGE("setEventHandler: Rejected attempt to clear handler");
86 return;
Andy McFaddenbabba182012-09-12 13:14:51 -070087 }
Dan Stoza9e56aa02015-11-02 13:00:03 -080088
89 bool wasNull = (mEventHandler == nullptr);
90 mEventHandler = handler;
91
92 if (wasNull) {
93 auto hotplugHook = std::bind(&HWComposer::hotplug, this,
94 std::placeholders::_1, std::placeholders::_2);
95 mHwcDevice->registerHotplugCallback(hotplugHook);
96 auto invalidateHook = std::bind(&HWComposer::invalidate, this,
97 std::placeholders::_1);
98 mHwcDevice->registerRefreshCallback(invalidateHook);
99 auto vsyncHook = std::bind(&HWComposer::vsync, this,
100 std::placeholders::_1, std::placeholders::_2);
101 mHwcDevice->registerVsyncCallback(vsyncHook);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700102 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700103}
104
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700105// Load and prepare the hardware composer module. Sets mHwc.
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800106void HWComposer::loadHwcModule(bool useVrComposer)
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700107{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800108 ALOGV("loadHwcModule");
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800109 mHwcDevice = std::make_unique<HWC2::Device>(useVrComposer);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800110 mRemainingHwcVirtualDisplays = mHwcDevice->getMaxVirtualDisplayCount();
111}
112
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700113bool HWComposer::hasCapability(HWC2::Capability capability) const
114{
115 return mHwcDevice->getCapabilities().count(capability) > 0;
116}
117
Dan Stoza9e56aa02015-11-02 13:00:03 -0800118bool HWComposer::isValidDisplay(int32_t displayId) const {
119 return static_cast<size_t>(displayId) < mDisplayData.size() &&
120 mDisplayData[displayId].hwcDisplay;
121}
122
123void HWComposer::validateChange(HWC2::Composition from, HWC2::Composition to) {
124 bool valid = true;
125 switch (from) {
126 case HWC2::Composition::Client:
127 valid = false;
128 break;
129 case HWC2::Composition::Device:
130 case HWC2::Composition::SolidColor:
131 valid = (to == HWC2::Composition::Client);
132 break;
133 case HWC2::Composition::Cursor:
134 case HWC2::Composition::Sideband:
135 valid = (to == HWC2::Composition::Client ||
136 to == HWC2::Composition::Device);
137 break;
138 default:
139 break;
140 }
141
142 if (!valid) {
143 ALOGE("Invalid layer type change: %s --> %s", to_string(from).c_str(),
144 to_string(to).c_str());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700145 }
146}
147
Dan Stoza9e56aa02015-11-02 13:00:03 -0800148void HWComposer::hotplug(const std::shared_ptr<HWC2::Display>& display,
149 HWC2::Connection connected) {
150 ALOGV("hotplug: %" PRIu64 ", %s", display->getId(),
151 to_string(connected).c_str());
152 int32_t disp = 0;
153 if (!mDisplayData[0].hwcDisplay) {
154 ALOGE_IF(connected != HWC2::Connection::Connected, "Assumed primary"
155 " display would be connected");
156 mDisplayData[0].hwcDisplay = display;
157 mHwcDisplaySlots[display->getId()] = 0;
158 disp = DisplayDevice::DISPLAY_PRIMARY;
159 } else {
160 // Disconnect is handled through HWComposer::disconnectDisplay via
161 // SurfaceFlinger's onHotplugReceived callback handling
162 if (connected == HWC2::Connection::Connected) {
163 mDisplayData[1].hwcDisplay = display;
164 mHwcDisplaySlots[display->getId()] = 1;
165 }
166 disp = DisplayDevice::DISPLAY_EXTERNAL;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700167 }
Stephen Kiazyk7d3dcb92017-04-05 16:46:49 -0700168 mEventHandler->onHotplugReceived(this, disp,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800169 connected == HWC2::Connection::Connected);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700170}
171
Dan Stoza9e56aa02015-11-02 13:00:03 -0800172void HWComposer::invalidate(const std::shared_ptr<HWC2::Display>& /*display*/) {
Steven Thomas3cfac282017-02-06 12:29:30 -0800173 mEventHandler->onInvalidateReceived(this);
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700174}
175
Dan Stoza9e56aa02015-11-02 13:00:03 -0800176void HWComposer::vsync(const std::shared_ptr<HWC2::Display>& display,
177 int64_t timestamp) {
178 auto displayType = HWC2::DisplayType::Invalid;
179 auto error = display->getType(&displayType);
180 if (error != HWC2::Error::None) {
181 ALOGE("vsync: Failed to determine type of display %" PRIu64,
182 display->getId());
Jesse Hall1bd20e02012-08-29 10:47:52 -0700183 return;
184 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700185
Dan Stoza9e56aa02015-11-02 13:00:03 -0800186 if (displayType == HWC2::DisplayType::Virtual) {
187 ALOGE("Virtual display %" PRIu64 " passed to vsync callback",
188 display->getId());
189 return;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700190 }
191
Dan Stoza9e56aa02015-11-02 13:00:03 -0800192 if (mHwcDisplaySlots.count(display->getId()) == 0) {
193 ALOGE("Unknown physical display %" PRIu64 " passed to vsync callback",
194 display->getId());
195 return;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700196 }
197
Dan Stoza9e56aa02015-11-02 13:00:03 -0800198 int32_t disp = mHwcDisplaySlots[display->getId()];
199 {
200 Mutex::Autolock _l(mLock);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700201
Dan Stoza9e56aa02015-11-02 13:00:03 -0800202 // There have been reports of HWCs that signal several vsync events
203 // with the same timestamp when turning the display off and on. This
204 // is a bug in the HWC implementation, but filter the extra events
205 // out here so they don't cause havoc downstream.
206 if (timestamp == mLastHwVSync[disp]) {
207 ALOGW("Ignoring duplicate VSYNC event from HWC (t=%" PRId64 ")",
208 timestamp);
209 return;
210 }
211
212 mLastHwVSync[disp] = timestamp;
Jesse Hall1c569c42013-04-05 13:44:52 -0700213 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800214
215 char tag[16];
216 snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", disp);
217 ATRACE_INT(tag, ++mVSyncCounts[disp] & 1);
218
Steven Thomas3cfac282017-02-06 12:29:30 -0800219 mEventHandler->onVSyncReceived(this, disp, timestamp);
Jesse Hall1c569c42013-04-05 13:44:52 -0700220}
221
Dan Stoza9e56aa02015-11-02 13:00:03 -0800222status_t HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height,
Dan Stoza5cf424b2016-05-20 14:02:39 -0700223 android_pixel_format_t* format, int32_t *outId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800224 if (mRemainingHwcVirtualDisplays == 0) {
225 ALOGE("allocateVirtualDisplay: No remaining virtual displays");
Mathias Agopiane60b0682012-08-21 23:34:09 -0700226 return NO_MEMORY;
227 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700228
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800229 if (SurfaceFlinger::maxVirtualDisplaySize != 0 &&
230 (width > SurfaceFlinger::maxVirtualDisplaySize ||
231 height > SurfaceFlinger::maxVirtualDisplaySize)) {
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800232 ALOGE("createVirtualDisplay: Can't create a virtual display with"
Fabien Sanglardc8e387e2017-03-10 10:30:28 -0800233 " a dimension > %" PRIu64 " (tried %u x %u)",
234 SurfaceFlinger::maxVirtualDisplaySize, width, height);
Fabien Sanglarde29055f2017-03-08 11:36:46 -0800235 return INVALID_OPERATION;
236 }
237
Dan Stoza9e56aa02015-11-02 13:00:03 -0800238 std::shared_ptr<HWC2::Display> display;
Dan Stoza5cf424b2016-05-20 14:02:39 -0700239 auto error = mHwcDevice->createVirtualDisplay(width, height, format,
240 &display);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800241 if (error != HWC2::Error::None) {
242 ALOGE("allocateVirtualDisplay: Failed to create HWC virtual display");
243 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700244 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800245
246 size_t displaySlot = 0;
247 if (!mFreeDisplaySlots.empty()) {
248 displaySlot = *mFreeDisplaySlots.begin();
249 mFreeDisplaySlots.erase(displaySlot);
250 } else if (mDisplayData.size() < INT32_MAX) {
251 // Don't bother allocating a slot larger than we can return
252 displaySlot = mDisplayData.size();
253 mDisplayData.resize(displaySlot + 1);
254 } else {
255 ALOGE("allocateVirtualDisplay: Unable to allocate a display slot");
256 return NO_MEMORY;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700257 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800258
259 mDisplayData[displaySlot].hwcDisplay = display;
260
261 --mRemainingHwcVirtualDisplays;
262 *outId = static_cast<int32_t>(displaySlot);
263
Mathias Agopiane60b0682012-08-21 23:34:09 -0700264 return NO_ERROR;
265}
266
Dan Stoza9e56aa02015-11-02 13:00:03 -0800267std::shared_ptr<HWC2::Layer> HWComposer::createLayer(int32_t displayId) {
268 if (!isValidDisplay(displayId)) {
269 ALOGE("Failed to create layer on invalid display %d", displayId);
270 return nullptr;
271 }
272 auto display = mDisplayData[displayId].hwcDisplay;
273 std::shared_ptr<HWC2::Layer> layer;
274 auto error = display->createLayer(&layer);
275 if (error != HWC2::Error::None) {
276 ALOGE("Failed to create layer on display %d: %s (%d)", displayId,
277 to_string(error).c_str(), static_cast<int32_t>(error));
278 return nullptr;
279 }
280 return layer;
281}
282
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800283nsecs_t HWComposer::getRefreshTimestamp(int32_t displayId) const {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700284 // this returns the last refresh timestamp.
285 // if the last one is not available, we estimate it based on
286 // the refresh period and whatever closest timestamp we have.
287 Mutex::Autolock _l(mLock);
288 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800289 auto vsyncPeriod = getActiveConfig(displayId)->getVsyncPeriod();
290 return now - ((now - mLastHwVSync[displayId]) % vsyncPeriod);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700291}
292
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800293bool HWComposer::isConnected(int32_t displayId) const {
294 if (!isValidDisplay(displayId)) {
295 ALOGE("isConnected: Attempted to access invalid display %d", displayId);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700296 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800297 }
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800298 return mDisplayData[displayId].hwcDisplay->isConnected();
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700299}
300
Dan Stoza9e56aa02015-11-02 13:00:03 -0800301std::vector<std::shared_ptr<const HWC2::Display::Config>>
302 HWComposer::getConfigs(int32_t displayId) const {
303 if (!isValidDisplay(displayId)) {
304 ALOGE("getConfigs: Attempted to access invalid display %d", displayId);
305 return {};
306 }
307 auto& displayData = mDisplayData[displayId];
308 auto configs = mDisplayData[displayId].hwcDisplay->getConfigs();
309 if (displayData.configMap.empty()) {
310 for (size_t i = 0; i < configs.size(); ++i) {
311 displayData.configMap[i] = configs[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700312 }
313 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800314 return configs;
Mathias Agopianda27af92012-09-13 18:17:13 -0700315}
316
Dan Stoza9e56aa02015-11-02 13:00:03 -0800317std::shared_ptr<const HWC2::Display::Config>
318 HWComposer::getActiveConfig(int32_t displayId) const {
319 if (!isValidDisplay(displayId)) {
Fabien Sanglardb7432cc2016-11-11 09:40:27 -0800320 ALOGV("getActiveConfigs: Attempted to access invalid display %d",
Dan Stoza9e56aa02015-11-02 13:00:03 -0800321 displayId);
322 return nullptr;
Mathias Agopian58959342010-10-07 14:57:04 -0700323 }
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) {
Fabien Sanglardb7432cc2016-11-11 09:40:27 -0800327 ALOGE("getActiveConfig: No config active, returning null");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800328 return nullptr;
329 } else if (error != HWC2::Error::None) {
330 ALOGE("getActiveConfig failed for display %d: %s (%d)", displayId,
331 to_string(error).c_str(), static_cast<int32_t>(error));
332 return nullptr;
333 } else if (!config) {
334 ALOGE("getActiveConfig returned an unknown config for display %d",
335 displayId);
336 return nullptr;
337 }
338
339 return config;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700340}
341
Michael Wright28f24d02016-07-12 13:30:53 -0700342std::vector<android_color_mode_t> HWComposer::getColorModes(int32_t displayId) const {
343 std::vector<android_color_mode_t> modes;
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600344
345 if (!isValidDisplay(displayId)) {
346 ALOGE("getColorModes: Attempted to access invalid display %d",
347 displayId);
348 return modes;
349 }
350 const std::shared_ptr<HWC2::Display>& hwcDisplay =
351 mDisplayData[displayId].hwcDisplay;
352
353 auto error = hwcDisplay->getColorModes(&modes);
354 if (error != HWC2::Error::None) {
355 ALOGE("getColorModes failed for display %d: %s (%d)", displayId,
356 to_string(error).c_str(), static_cast<int32_t>(error));
Michael Wright28f24d02016-07-12 13:30:53 -0700357 return std::vector<android_color_mode_t>();
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600358 }
359
360 return modes;
361}
362
Michael Wright28f24d02016-07-12 13:30:53 -0700363status_t HWComposer::setActiveColorMode(int32_t displayId, android_color_mode_t mode) {
364 if (!isValidDisplay(displayId)) {
365 ALOGE("setActiveColorMode: Display %d is not valid", displayId);
366 return BAD_INDEX;
367 }
368
369 auto& displayData = mDisplayData[displayId];
370 auto error = displayData.hwcDisplay->setColorMode(mode);
371 if (error != HWC2::Error::None) {
372 ALOGE("setActiveConfig: Failed to set color mode %d on display %d: "
373 "%s (%d)", mode, displayId, to_string(error).c_str(),
374 static_cast<int32_t>(error));
375 return UNKNOWN_ERROR;
376 }
377
378 return NO_ERROR;
379}
380
381
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800382void HWComposer::setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled) {
383 if (displayId < 0 || displayId >= HWC_DISPLAY_VIRTUAL) {
384 ALOGD("setVsyncEnabled: Ignoring for virtual display %d", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800385 return;
386 }
387
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800388 if (!isValidDisplay(displayId)) {
389 ALOGE("setVsyncEnabled: Attempted to access invalid display %d",
390 displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800391 return;
392 }
393
394 // NOTE: we use our own internal lock here because we have to call
395 // into the HWC with the lock held, and we want to make sure
396 // that even if HWC blocks (which it shouldn't), it won't
397 // affect other threads.
398 Mutex::Autolock _l(mVsyncLock);
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800399 auto& displayData = mDisplayData[displayId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800400 if (enabled != displayData.vsyncEnabled) {
401 ATRACE_CALL();
402 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled);
403 if (error == HWC2::Error::None) {
404 displayData.vsyncEnabled = enabled;
405
406 char tag[16];
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800407 snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800408 ATRACE_INT(tag, enabled == HWC2::Vsync::Enable ? 1 : 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700409 } else {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800410 ALOGE("setVsyncEnabled: Failed to set vsync to %s on %d/%" PRIu64
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800411 ": %s (%d)", to_string(enabled).c_str(), displayId,
412 mDisplayData[displayId].hwcDisplay->getId(),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800413 to_string(error).c_str(), static_cast<int32_t>(error));
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700414 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700415 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700416}
417
Chia-I Wu06d63de2017-01-04 14:58:51 +0800418status_t HWComposer::setClientTarget(int32_t displayId, uint32_t slot,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800419 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target,
420 android_dataspace_t dataspace) {
421 if (!isValidDisplay(displayId)) {
Jesse Hall851cfe82013-03-20 13:44:00 -0700422 return BAD_INDEX;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800423 }
Jesse Hall851cfe82013-03-20 13:44:00 -0700424
Dan Stoza9e56aa02015-11-02 13:00:03 -0800425 ALOGV("setClientTarget for display %d", displayId);
426 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400427 auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800428 if (error != HWC2::Error::None) {
429 ALOGE("Failed to set client target for display %d: %s (%d)", displayId,
430 to_string(error).c_str(), static_cast<int32_t>(error));
431 return BAD_VALUE;
432 }
433
Jesse Hall851cfe82013-03-20 13:44:00 -0700434 return NO_ERROR;
435}
436
Dan Stoza9e56aa02015-11-02 13:00:03 -0800437status_t HWComposer::prepare(DisplayDevice& displayDevice) {
438 ATRACE_CALL();
439
440 Mutex::Autolock _l(mDisplayLock);
441 auto displayId = displayDevice.getHwcDisplayId();
Dan Stozaec0f7172016-07-21 11:09:40 -0700442 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
443 ALOGV("Skipping HWComposer prepare for non-HWC display");
444 return NO_ERROR;
445 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800446 if (!isValidDisplay(displayId)) {
447 return BAD_INDEX;
448 }
449
450 auto& displayData = mDisplayData[displayId];
451 auto& hwcDisplay = displayData.hwcDisplay;
452 if (!hwcDisplay->isConnected()) {
453 return NO_ERROR;
454 }
455
456 uint32_t numTypes = 0;
457 uint32_t numRequests = 0;
458 auto error = hwcDisplay->validate(&numTypes, &numRequests);
459 if (error != HWC2::Error::None && error != HWC2::Error::HasChanges) {
460 ALOGE("prepare: validate failed for display %d: %s (%d)", displayId,
461 to_string(error).c_str(), static_cast<int32_t>(error));
462 return BAD_INDEX;
463 }
464
465 std::unordered_map<std::shared_ptr<HWC2::Layer>, HWC2::Composition>
466 changedTypes;
467 changedTypes.reserve(numTypes);
468 error = hwcDisplay->getChangedCompositionTypes(&changedTypes);
469 if (error != HWC2::Error::None) {
470 ALOGE("prepare: getChangedCompositionTypes failed on display %d: "
471 "%s (%d)", displayId, to_string(error).c_str(),
472 static_cast<int32_t>(error));
473 return BAD_INDEX;
474 }
475
476
477 displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0);
478 std::unordered_map<std::shared_ptr<HWC2::Layer>, HWC2::LayerRequest>
479 layerRequests;
480 layerRequests.reserve(numRequests);
481 error = hwcDisplay->getRequests(&displayData.displayRequests,
482 &layerRequests);
483 if (error != HWC2::Error::None) {
484 ALOGE("prepare: getRequests failed on display %d: %s (%d)", displayId,
485 to_string(error).c_str(), static_cast<int32_t>(error));
486 return BAD_INDEX;
487 }
488
489 displayData.hasClientComposition = false;
490 displayData.hasDeviceComposition = false;
491 for (auto& layer : displayDevice.getVisibleLayersSortedByZ()) {
492 auto hwcLayer = layer->getHwcLayer(displayId);
493
494 if (changedTypes.count(hwcLayer) != 0) {
495 // We pass false so we only update our state and don't call back
496 // into the HWC device
497 validateChange(layer->getCompositionType(displayId),
498 changedTypes[hwcLayer]);
499 layer->setCompositionType(displayId, changedTypes[hwcLayer], false);
500 }
501
502 switch (layer->getCompositionType(displayId)) {
503 case HWC2::Composition::Client:
504 displayData.hasClientComposition = true;
505 break;
506 case HWC2::Composition::Device:
507 case HWC2::Composition::SolidColor:
508 case HWC2::Composition::Cursor:
509 case HWC2::Composition::Sideband:
510 displayData.hasDeviceComposition = true;
511 break;
512 default:
513 break;
514 }
515
516 if (layerRequests.count(hwcLayer) != 0 &&
517 layerRequests[hwcLayer] ==
518 HWC2::LayerRequest::ClearClientTarget) {
519 layer->setClearClientTarget(displayId, true);
520 } else {
521 if (layerRequests.count(hwcLayer) != 0) {
522 ALOGE("prepare: Unknown layer request: %s",
523 to_string(layerRequests[hwcLayer]).c_str());
524 }
525 layer->setClearClientTarget(displayId, false);
526 }
527 }
528
529 error = hwcDisplay->acceptChanges();
530 if (error != HWC2::Error::None) {
531 ALOGE("prepare: acceptChanges failed: %s", to_string(error).c_str());
532 return BAD_INDEX;
533 }
534
535 return NO_ERROR;
536}
537
538bool HWComposer::hasDeviceComposition(int32_t displayId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700539 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
540 // Displays without a corresponding HWC display are never composed by
541 // the device
542 return false;
543 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800544 if (!isValidDisplay(displayId)) {
545 ALOGE("hasDeviceComposition: Invalid display %d", displayId);
546 return false;
547 }
548 return mDisplayData[displayId].hasDeviceComposition;
549}
550
551bool HWComposer::hasClientComposition(int32_t displayId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700552 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
553 // Displays without a corresponding HWC display are always composed by
554 // the client
555 return true;
556 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800557 if (!isValidDisplay(displayId)) {
558 ALOGE("hasClientComposition: Invalid display %d", displayId);
559 return true;
560 }
561 return mDisplayData[displayId].hasClientComposition;
562}
563
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800564sp<Fence> HWComposer::getPresentFence(int32_t displayId) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800565 if (!isValidDisplay(displayId)) {
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800566 ALOGE("getPresentFence failed for invalid display %d", displayId);
Jesse Hall851cfe82013-03-20 13:44:00 -0700567 return Fence::NO_FENCE;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800568 }
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800569 return mDisplayData[displayId].lastPresentFence;
Jesse Hall851cfe82013-03-20 13:44:00 -0700570}
571
Dan Stoza9e56aa02015-11-02 13:00:03 -0800572sp<Fence> HWComposer::getLayerReleaseFence(int32_t displayId,
573 const std::shared_ptr<HWC2::Layer>& layer) const {
574 if (!isValidDisplay(displayId)) {
575 ALOGE("getLayerReleaseFence: Invalid display");
576 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700577 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800578 auto displayFences = mDisplayData[displayId].releaseFences;
579 if (displayFences.count(layer) == 0) {
580 ALOGV("getLayerReleaseFence: Release fence not found");
581 return Fence::NO_FENCE;
Riley Andrews03414a12014-07-01 14:22:59 -0700582 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800583 return displayFences[layer];
Riley Andrews03414a12014-07-01 14:22:59 -0700584}
585
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800586status_t HWComposer::presentAndGetReleaseFences(int32_t displayId) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800587 ATRACE_CALL();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700588
Dan Stoza9e56aa02015-11-02 13:00:03 -0800589 if (!isValidDisplay(displayId)) {
590 return BAD_INDEX;
Mathias Agopianc3973602012-08-31 17:51:25 -0700591 }
Pablo Ceballosd814cf22015-09-11 14:37:39 -0700592
Dan Stoza9e56aa02015-11-02 13:00:03 -0800593 auto& displayData = mDisplayData[displayId];
594 auto& hwcDisplay = displayData.hwcDisplay;
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800595 auto error = hwcDisplay->present(&displayData.lastPresentFence);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800596 if (error != HWC2::Error::None) {
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800597 ALOGE("presentAndGetReleaseFences: failed for display %d: %s (%d)",
598 displayId, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800599 return UNKNOWN_ERROR;
600 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700601
Dan Stoza9e56aa02015-11-02 13:00:03 -0800602 std::unordered_map<std::shared_ptr<HWC2::Layer>, sp<Fence>> releaseFences;
603 error = hwcDisplay->getReleaseFences(&releaseFences);
604 if (error != HWC2::Error::None) {
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800605 ALOGE("presentAndGetReleaseFences: Failed to get release fences "
606 "for display %d: %s (%d)",
Dan Stoza9e56aa02015-11-02 13:00:03 -0800607 displayId, to_string(error).c_str(),
608 static_cast<int32_t>(error));
609 return UNKNOWN_ERROR;
Mathias Agopianf4358632012-08-22 17:16:19 -0700610 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800611
612 displayData.releaseFences = std::move(releaseFences);
613
614 return NO_ERROR;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700615}
616
Dan Stoza9e56aa02015-11-02 13:00:03 -0800617status_t HWComposer::setPowerMode(int32_t displayId, int32_t intMode) {
618 ALOGV("setPowerMode(%d, %d)", displayId, intMode);
619 if (!isValidDisplay(displayId)) {
620 ALOGE("setPowerMode: Bad display");
621 return BAD_INDEX;
622 }
623 if (displayId >= VIRTUAL_DISPLAY_ID_BASE) {
624 ALOGE("setPowerMode: Virtual display %d passed in, returning",
625 displayId);
626 return BAD_INDEX;
627 }
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);
641 if (error != HWC2::Error::None) {
642 ALOGE("setPowerMode: Unable to set power mode %s for "
643 "display %d: %s (%d)", to_string(mode).c_str(),
644 displayId, to_string(error).c_str(),
645 static_cast<int32_t>(error));
Mathias Agopianda27af92012-09-13 18:17:13 -0700646 }
647 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800648 break;
649 case HWC2::PowerMode::Doze:
650 case HWC2::PowerMode::DozeSuspend:
651 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str());
652 {
653 bool supportsDoze = false;
654 auto error = hwcDisplay->supportsDoze(&supportsDoze);
655 if (error != HWC2::Error::None) {
656 ALOGE("setPowerMode: Unable to query doze support for "
657 "display %d: %s (%d)", displayId,
658 to_string(error).c_str(),
659 static_cast<int32_t>(error));
660 }
661 if (!supportsDoze) {
662 mode = HWC2::PowerMode::On;
663 }
664
665 error = hwcDisplay->setPowerMode(mode);
666 if (error != HWC2::Error::None) {
667 ALOGE("setPowerMode: Unable to set power mode %s for "
668 "display %d: %s (%d)", to_string(mode).c_str(),
669 displayId, to_string(error).c_str(),
670 static_cast<int32_t>(error));
671 }
672 }
673 break;
674 default:
675 ALOGV("setPowerMode: Not calling HWC");
676 break;
Mathias Agopianda27af92012-09-13 18:17:13 -0700677 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800678
679 return NO_ERROR;
680}
681
682status_t HWComposer::setActiveConfig(int32_t displayId, size_t configId) {
683 if (!isValidDisplay(displayId)) {
684 ALOGE("setActiveConfig: Display %d is not valid", displayId);
685 return BAD_INDEX;
686 }
687
688 auto& displayData = mDisplayData[displayId];
689 if (displayData.configMap.count(configId) == 0) {
690 ALOGE("setActiveConfig: Invalid config %zd", configId);
691 return BAD_INDEX;
692 }
693
694 auto error = displayData.hwcDisplay->setActiveConfig(
695 displayData.configMap[configId]);
696 if (error != HWC2::Error::None) {
697 ALOGE("setActiveConfig: Failed to set config %zu on display %d: "
698 "%s (%d)", configId, displayId, to_string(error).c_str(),
699 static_cast<int32_t>(error));
700 return UNKNOWN_ERROR;
701 }
702
703 return NO_ERROR;
704}
705
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700706status_t HWComposer::setColorTransform(int32_t displayId,
707 const mat4& transform) {
708 if (!isValidDisplay(displayId)) {
709 ALOGE("setColorTransform: Display %d is not valid", displayId);
710 return BAD_INDEX;
711 }
712
713 auto& displayData = mDisplayData[displayId];
714 bool isIdentity = transform == mat4();
715 auto error = displayData.hwcDisplay->setColorTransform(transform,
716 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY :
717 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX);
718 if (error != HWC2::Error::None) {
719 ALOGE("setColorTransform: Failed to set transform on display %d: "
720 "%s (%d)", displayId, to_string(error).c_str(),
721 static_cast<int32_t>(error));
722 return UNKNOWN_ERROR;
723 }
724
725 return NO_ERROR;
726}
727
Dan Stoza9e56aa02015-11-02 13:00:03 -0800728void HWComposer::disconnectDisplay(int displayId) {
729 LOG_ALWAYS_FATAL_IF(displayId < 0);
730 auto& displayData = mDisplayData[displayId];
731
732 auto displayType = HWC2::DisplayType::Invalid;
733 auto error = displayData.hwcDisplay->getType(&displayType);
734 if (error != HWC2::Error::None) {
735 ALOGE("disconnectDisplay: Failed to determine type of display %d",
736 displayId);
737 return;
738 }
739
740 // If this was a virtual display, add its slot back for reuse by future
741 // virtual displays
742 if (displayType == HWC2::DisplayType::Virtual) {
743 mFreeDisplaySlots.insert(displayId);
744 ++mRemainingHwcVirtualDisplays;
745 }
746
747 auto hwcId = displayData.hwcDisplay->getId();
748 mHwcDisplaySlots.erase(hwcId);
749 displayData.reset();
750}
751
752status_t HWComposer::setOutputBuffer(int32_t displayId,
753 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
754 if (!isValidDisplay(displayId)) {
755 ALOGE("setOutputBuffer: Display %d is not valid", displayId);
756 return BAD_INDEX;
757 }
758
759 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
760 auto displayType = HWC2::DisplayType::Invalid;
761 auto error = hwcDisplay->getType(&displayType);
762 if (error != HWC2::Error::None) {
763 ALOGE("setOutputBuffer: Failed to determine type of display %d",
764 displayId);
765 return NAME_NOT_FOUND;
766 }
767
768 if (displayType != HWC2::DisplayType::Virtual) {
769 ALOGE("setOutputBuffer: Display %d is not virtual", displayId);
770 return INVALID_OPERATION;
771 }
772
773 error = hwcDisplay->setOutputBuffer(buffer, acquireFence);
774 if (error != HWC2::Error::None) {
775 ALOGE("setOutputBuffer: Failed to set buffer on display %d: %s (%d)",
776 displayId, to_string(error).c_str(),
777 static_cast<int32_t>(error));
778 return UNKNOWN_ERROR;
779 }
780
781 return NO_ERROR;
782}
783
784void HWComposer::clearReleaseFences(int32_t displayId) {
785 if (!isValidDisplay(displayId)) {
786 ALOGE("clearReleaseFences: Display %d is not valid", displayId);
787 return;
788 }
789 mDisplayData[displayId].releaseFences.clear();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700790}
791
Dan Stozac4f471e2016-03-24 09:31:08 -0700792std::unique_ptr<HdrCapabilities> HWComposer::getHdrCapabilities(
793 int32_t displayId) {
794 if (!isValidDisplay(displayId)) {
795 ALOGE("getHdrCapabilities: Display %d is not valid", displayId);
796 return nullptr;
797 }
798
799 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay;
800 std::unique_ptr<HdrCapabilities> capabilities;
801 auto error = hwcDisplay->getHdrCapabilities(&capabilities);
802 if (error != HWC2::Error::None) {
803 ALOGE("getOutputCapabilities: Failed to get capabilities on display %d:"
804 " %s (%d)", displayId, to_string(error).c_str(),
805 static_cast<int32_t>(error));
806 return nullptr;
807 }
808
809 return capabilities;
810}
811
Andy McFadden4df87bd2014-04-21 18:08:54 -0700812// Converts a PixelFormat to a human-readable string. Max 11 chars.
813// (Could use a table of prefab String8 objects.)
Dan Stoza9e56aa02015-11-02 13:00:03 -0800814/*
Andy McFadden4df87bd2014-04-21 18:08:54 -0700815static String8 getFormatStr(PixelFormat format) {
816 switch (format) {
817 case PIXEL_FORMAT_RGBA_8888: return String8("RGBA_8888");
818 case PIXEL_FORMAT_RGBX_8888: return String8("RGBx_8888");
819 case PIXEL_FORMAT_RGB_888: return String8("RGB_888");
820 case PIXEL_FORMAT_RGB_565: return String8("RGB_565");
821 case PIXEL_FORMAT_BGRA_8888: return String8("BGRA_8888");
Andy McFaddenf0058ca2014-05-20 13:28:50 -0700822 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
823 return String8("ImplDef");
Andy McFadden4df87bd2014-04-21 18:08:54 -0700824 default:
825 String8 result;
826 result.appendFormat("? %08x", format);
827 return result;
828 }
829}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800830*/
Andy McFadden4df87bd2014-04-21 18:08:54 -0700831
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800832bool HWComposer::isUsingVrComposer() const {
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800833 return getComposer()->isUsingVrComposer();
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800834}
835
Mathias Agopian74d211a2013-04-22 16:55:35 +0200836void HWComposer::dump(String8& result) const {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800837 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow
838 // all the state going into the layers. This is probably better done in
839 // Layer itself, but it's going to take a bit of work to get there.
840 result.append(mHwcDevice->dump().c_str());
Mathias Agopian83727852010-09-23 18:13:21 -0700841}
842
Mathias Agopiana350ff92010-08-10 17:14:02 -0700843// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -0700844
Jesse Halla9a1b002013-02-27 16:39:25 -0800845HWComposer::DisplayData::DisplayData()
Dan Stoza9e56aa02015-11-02 13:00:03 -0800846 : hasClientComposition(false),
847 hasDeviceComposition(false),
848 hwcDisplay(),
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800849 lastPresentFence(Fence::NO_FENCE),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800850 outbufHandle(nullptr),
851 outbufAcquireFence(Fence::NO_FENCE),
852 vsyncEnabled(HWC2::Vsync::Disable) {
853 ALOGV("Created new DisplayData");
854}
Jesse Halla9a1b002013-02-27 16:39:25 -0800855
856HWComposer::DisplayData::~DisplayData() {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800857}
858
859void HWComposer::DisplayData::reset() {
860 ALOGV("DisplayData reset");
861 *this = DisplayData();
Jesse Halla9a1b002013-02-27 16:39:25 -0800862}
863
Mathias Agopian2965b262012-04-08 15:13:32 -0700864// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -0700865}; // namespace android