Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 1 | /* |
| 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 Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 17 | // #define LOG_NDEBUG 0 |
| 18 | |
| 19 | #undef LOG_TAG |
| 20 | #define LOG_TAG "HWComposer" |
Mathias Agopian | 2965b26 | 2012-04-08 15:13:32 -0700 | [diff] [blame] | 21 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 22 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 23 | #include <inttypes.h> |
| 24 | #include <math.h> |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 25 | #include <stdint.h> |
Mathias Agopian | f1352df | 2010-08-11 17:31:33 -0700 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | |
| 31 | #include <utils/Errors.h> |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 32 | #include <utils/misc.h> |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 33 | #include <utils/NativeHandle.h> |
Mathias Agopian | 8372785 | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 34 | #include <utils/String8.h> |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 35 | #include <utils/Thread.h> |
Mathias Agopian | 2965b26 | 2012-04-08 15:13:32 -0700 | [diff] [blame] | 36 | #include <utils/Trace.h> |
Mathias Agopian | 22da60c | 2011-09-09 00:49:11 -0700 | [diff] [blame] | 37 | #include <utils/Vector.h> |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 38 | |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 39 | #include <ui/GraphicBuffer.h> |
| 40 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 41 | #include <hardware/hardware.h> |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 42 | #include <hardware/hwcomposer.h> |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 43 | |
Jesse Hall | 1c569c4 | 2013-04-05 13:44:52 -0700 | [diff] [blame] | 44 | #include <android/configuration.h> |
| 45 | |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 46 | #include <cutils/properties.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 47 | #include <log/log.h> |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 48 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 49 | #include "HWComposer.h" |
Fabien Sanglard | e29055f | 2017-03-08 11:36:46 -0800 | [diff] [blame] | 50 | #include "hwc2on1adapter/HWC2On1Adapter.h" |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 51 | #include "HWC2.h" |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 52 | #include "ComposerHal.h" |
Mathias Agopian | 33ceeb3 | 2013-04-01 16:54:58 -0700 | [diff] [blame] | 53 | |
| 54 | #include "../Layer.h" // needed only for debugging |
| 55 | #include "../SurfaceFlinger.h" |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 56 | |
| 57 | namespace android { |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 58 | |
Jesse Hall | 7296051 | 2013-01-10 16:19:56 -0800 | [diff] [blame] | 59 | #define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION |
Jesse Hall | 9eb1eb5 | 2012-08-29 10:39:38 -0700 | [diff] [blame] | 60 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 61 | // --------------------------------------------------------------------------- |
| 62 | |
Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 63 | HWComposer::HWComposer(bool useVrComposer) |
| 64 | : mAdapter(), |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 65 | mHwcDevice(), |
| 66 | mDisplayData(2), |
| 67 | mFreeDisplaySlots(), |
| 68 | mHwcDisplaySlots(), |
| 69 | mCBContext(), |
| 70 | mEventHandler(nullptr), |
| 71 | mVSyncCounts(), |
Chia-I Wu | 843460d | 2017-03-01 16:08:51 -0800 | [diff] [blame] | 72 | mRemainingHwcVirtualDisplays(0), |
| 73 | mDumpMayLockUp(false) |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 74 | { |
Mathias Agopian | bef42c5 | 2013-08-21 17:45:46 -0700 | [diff] [blame] | 75 | for (size_t i=0 ; i<HWC_NUM_PHYSICAL_DISPLAY_TYPES ; i++) { |
| 76 | mLastHwVSync[i] = 0; |
| 77 | mVSyncCounts[i] = 0; |
| 78 | } |
| 79 | |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 80 | loadHwcModule(useVrComposer); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 83 | HWComposer::~HWComposer() {} |
| 84 | |
| 85 | void HWComposer::setEventHandler(EventHandler* handler) |
| 86 | { |
| 87 | if (handler == nullptr) { |
| 88 | ALOGE("setEventHandler: Rejected attempt to clear handler"); |
| 89 | return; |
Andy McFadden | babba18 | 2012-09-12 13:14:51 -0700 | [diff] [blame] | 90 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 91 | |
| 92 | bool wasNull = (mEventHandler == nullptr); |
| 93 | mEventHandler = handler; |
| 94 | |
| 95 | if (wasNull) { |
| 96 | auto hotplugHook = std::bind(&HWComposer::hotplug, this, |
| 97 | std::placeholders::_1, std::placeholders::_2); |
| 98 | mHwcDevice->registerHotplugCallback(hotplugHook); |
| 99 | auto invalidateHook = std::bind(&HWComposer::invalidate, this, |
| 100 | std::placeholders::_1); |
| 101 | mHwcDevice->registerRefreshCallback(invalidateHook); |
| 102 | auto vsyncHook = std::bind(&HWComposer::vsync, this, |
| 103 | std::placeholders::_1, std::placeholders::_2); |
| 104 | mHwcDevice->registerVsyncCallback(vsyncHook); |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 105 | } |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Andy McFadden | b0d1dd3 | 2012-09-10 14:08:09 -0700 | [diff] [blame] | 108 | // Load and prepare the hardware composer module. Sets mHwc. |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 109 | void HWComposer::loadHwcModule(bool useVrComposer) |
Andy McFadden | b0d1dd3 | 2012-09-10 14:08:09 -0700 | [diff] [blame] | 110 | { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 111 | ALOGV("loadHwcModule"); |
| 112 | |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 113 | #ifdef BYPASS_IHWC |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 114 | (void)useVrComposer; // Silence unused parameter warning. |
| 115 | |
Andy McFadden | b0d1dd3 | 2012-09-10 14:08:09 -0700 | [diff] [blame] | 116 | hw_module_t const* module; |
| 117 | |
| 118 | if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 119 | ALOGE("%s module not found, aborting", HWC_HARDWARE_MODULE_ID); |
| 120 | abort(); |
Andy McFadden | b0d1dd3 | 2012-09-10 14:08:09 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Dan Stoza | 355f604 | 2016-04-14 14:30:41 -0700 | [diff] [blame] | 123 | hw_device_t* device = nullptr; |
| 124 | int error = module->methods->open(module, HWC_HARDWARE_COMPOSER, &device); |
| 125 | if (error != 0) { |
| 126 | ALOGE("Failed to open HWC device (%s), aborting", strerror(-error)); |
| 127 | abort(); |
| 128 | } |
| 129 | |
| 130 | uint32_t majorVersion = (device->version >> 24) & 0xF; |
| 131 | if (majorVersion == 2) { |
| 132 | mHwcDevice = std::make_unique<HWC2::Device>( |
| 133 | reinterpret_cast<hwc2_device_t*>(device)); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 134 | } else { |
Dan Stoza | 355f604 | 2016-04-14 14:30:41 -0700 | [diff] [blame] | 135 | mAdapter = std::make_unique<HWC2On1Adapter>( |
| 136 | reinterpret_cast<hwc_composer_device_1_t*>(device)); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 137 | uint8_t minorVersion = mAdapter->getHwc1MinorVersion(); |
| 138 | if (minorVersion < 1) { |
| 139 | ALOGE("Cannot adapt to HWC version %d.%d", |
| 140 | static_cast<int32_t>((minorVersion >> 8) & 0xF), |
| 141 | static_cast<int32_t>(minorVersion & 0xF)); |
| 142 | abort(); |
| 143 | } |
| 144 | mHwcDevice = std::make_unique<HWC2::Device>( |
| 145 | static_cast<hwc2_device_t*>(mAdapter.get())); |
Andy McFadden | b0d1dd3 | 2012-09-10 14:08:09 -0700 | [diff] [blame] | 146 | } |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 147 | #else |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 148 | mHwcDevice = std::make_unique<HWC2::Device>(useVrComposer); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 149 | #endif |
Andy McFadden | b0d1dd3 | 2012-09-10 14:08:09 -0700 | [diff] [blame] | 150 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 151 | mRemainingHwcVirtualDisplays = mHwcDevice->getMaxVirtualDisplayCount(); |
| 152 | } |
| 153 | |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 154 | bool HWComposer::hasCapability(HWC2::Capability capability) const |
| 155 | { |
| 156 | return mHwcDevice->getCapabilities().count(capability) > 0; |
| 157 | } |
| 158 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 159 | bool HWComposer::isValidDisplay(int32_t displayId) const { |
| 160 | return static_cast<size_t>(displayId) < mDisplayData.size() && |
| 161 | mDisplayData[displayId].hwcDisplay; |
| 162 | } |
| 163 | |
| 164 | void HWComposer::validateChange(HWC2::Composition from, HWC2::Composition to) { |
| 165 | bool valid = true; |
| 166 | switch (from) { |
| 167 | case HWC2::Composition::Client: |
| 168 | valid = false; |
| 169 | break; |
| 170 | case HWC2::Composition::Device: |
| 171 | case HWC2::Composition::SolidColor: |
| 172 | valid = (to == HWC2::Composition::Client); |
| 173 | break; |
| 174 | case HWC2::Composition::Cursor: |
| 175 | case HWC2::Composition::Sideband: |
| 176 | valid = (to == HWC2::Composition::Client || |
| 177 | to == HWC2::Composition::Device); |
| 178 | break; |
| 179 | default: |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | if (!valid) { |
| 184 | ALOGE("Invalid layer type change: %s --> %s", to_string(from).c_str(), |
| 185 | to_string(to).c_str()); |
Andy McFadden | b0d1dd3 | 2012-09-10 14:08:09 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 189 | void HWComposer::hotplug(const std::shared_ptr<HWC2::Display>& display, |
| 190 | HWC2::Connection connected) { |
| 191 | ALOGV("hotplug: %" PRIu64 ", %s", display->getId(), |
| 192 | to_string(connected).c_str()); |
| 193 | int32_t disp = 0; |
| 194 | if (!mDisplayData[0].hwcDisplay) { |
| 195 | ALOGE_IF(connected != HWC2::Connection::Connected, "Assumed primary" |
| 196 | " display would be connected"); |
| 197 | mDisplayData[0].hwcDisplay = display; |
| 198 | mHwcDisplaySlots[display->getId()] = 0; |
| 199 | disp = DisplayDevice::DISPLAY_PRIMARY; |
| 200 | } else { |
| 201 | // Disconnect is handled through HWComposer::disconnectDisplay via |
| 202 | // SurfaceFlinger's onHotplugReceived callback handling |
| 203 | if (connected == HWC2::Connection::Connected) { |
| 204 | mDisplayData[1].hwcDisplay = display; |
| 205 | mHwcDisplaySlots[display->getId()] = 1; |
| 206 | } |
| 207 | disp = DisplayDevice::DISPLAY_EXTERNAL; |
Andy McFadden | b0d1dd3 | 2012-09-10 14:08:09 -0700 | [diff] [blame] | 208 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 209 | mEventHandler->onHotplugReceived(disp, |
| 210 | connected == HWC2::Connection::Connected); |
Andy McFadden | b0d1dd3 | 2012-09-10 14:08:09 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 213 | void HWComposer::invalidate(const std::shared_ptr<HWC2::Display>& /*display*/) { |
Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 214 | mEventHandler->onInvalidateReceived(this); |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 217 | void HWComposer::vsync(const std::shared_ptr<HWC2::Display>& display, |
| 218 | int64_t timestamp) { |
| 219 | auto displayType = HWC2::DisplayType::Invalid; |
| 220 | auto error = display->getType(&displayType); |
| 221 | if (error != HWC2::Error::None) { |
| 222 | ALOGE("vsync: Failed to determine type of display %" PRIu64, |
| 223 | display->getId()); |
Jesse Hall | 1bd20e0 | 2012-08-29 10:47:52 -0700 | [diff] [blame] | 224 | return; |
| 225 | } |
Jesse Hall | 1bd20e0 | 2012-08-29 10:47:52 -0700 | [diff] [blame] | 226 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 227 | if (displayType == HWC2::DisplayType::Virtual) { |
| 228 | ALOGE("Virtual display %" PRIu64 " passed to vsync callback", |
| 229 | display->getId()); |
| 230 | return; |
Jesse Hall | 1bd20e0 | 2012-08-29 10:47:52 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 233 | if (mHwcDisplaySlots.count(display->getId()) == 0) { |
| 234 | ALOGE("Unknown physical display %" PRIu64 " passed to vsync callback", |
| 235 | display->getId()); |
| 236 | return; |
Jesse Hall | 1bd20e0 | 2012-08-29 10:47:52 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 239 | int32_t disp = mHwcDisplaySlots[display->getId()]; |
| 240 | { |
| 241 | Mutex::Autolock _l(mLock); |
Jesse Hall | 1bd20e0 | 2012-08-29 10:47:52 -0700 | [diff] [blame] | 242 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 243 | // There have been reports of HWCs that signal several vsync events |
| 244 | // with the same timestamp when turning the display off and on. This |
| 245 | // is a bug in the HWC implementation, but filter the extra events |
| 246 | // out here so they don't cause havoc downstream. |
| 247 | if (timestamp == mLastHwVSync[disp]) { |
| 248 | ALOGW("Ignoring duplicate VSYNC event from HWC (t=%" PRId64 ")", |
| 249 | timestamp); |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | mLastHwVSync[disp] = timestamp; |
Jesse Hall | 1c569c4 | 2013-04-05 13:44:52 -0700 | [diff] [blame] | 254 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 255 | |
| 256 | char tag[16]; |
| 257 | snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", disp); |
| 258 | ATRACE_INT(tag, ++mVSyncCounts[disp] & 1); |
| 259 | |
Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 260 | mEventHandler->onVSyncReceived(this, disp, timestamp); |
Jesse Hall | 1c569c4 | 2013-04-05 13:44:52 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 263 | status_t HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height, |
Dan Stoza | 5cf424b | 2016-05-20 14:02:39 -0700 | [diff] [blame] | 264 | android_pixel_format_t* format, int32_t *outId) { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 265 | if (mRemainingHwcVirtualDisplays == 0) { |
| 266 | ALOGE("allocateVirtualDisplay: No remaining virtual displays"); |
Mathias Agopian | e60b068 | 2012-08-21 23:34:09 -0700 | [diff] [blame] | 267 | return NO_MEMORY; |
| 268 | } |
Mathias Agopian | e60b068 | 2012-08-21 23:34:09 -0700 | [diff] [blame] | 269 | |
Fabien Sanglard | e29055f | 2017-03-08 11:36:46 -0800 | [diff] [blame] | 270 | if (MAX_VIRTUAL_DISPLAY_DIMENSION != 0 && |
| 271 | (width > MAX_VIRTUAL_DISPLAY_DIMENSION || |
| 272 | height > MAX_VIRTUAL_DISPLAY_DIMENSION)) { |
| 273 | ALOGE("createVirtualDisplay: Can't create a virtual display with" |
| 274 | " a dimension > %u (tried %u x %u)", |
| 275 | MAX_VIRTUAL_DISPLAY_DIMENSION, width, height); |
| 276 | return INVALID_OPERATION; |
| 277 | } |
| 278 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 279 | std::shared_ptr<HWC2::Display> display; |
Dan Stoza | 5cf424b | 2016-05-20 14:02:39 -0700 | [diff] [blame] | 280 | auto error = mHwcDevice->createVirtualDisplay(width, height, format, |
| 281 | &display); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 282 | if (error != HWC2::Error::None) { |
| 283 | ALOGE("allocateVirtualDisplay: Failed to create HWC virtual display"); |
| 284 | return NO_MEMORY; |
Mathias Agopian | e60b068 | 2012-08-21 23:34:09 -0700 | [diff] [blame] | 285 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 286 | |
| 287 | size_t displaySlot = 0; |
| 288 | if (!mFreeDisplaySlots.empty()) { |
| 289 | displaySlot = *mFreeDisplaySlots.begin(); |
| 290 | mFreeDisplaySlots.erase(displaySlot); |
| 291 | } else if (mDisplayData.size() < INT32_MAX) { |
| 292 | // Don't bother allocating a slot larger than we can return |
| 293 | displaySlot = mDisplayData.size(); |
| 294 | mDisplayData.resize(displaySlot + 1); |
| 295 | } else { |
| 296 | ALOGE("allocateVirtualDisplay: Unable to allocate a display slot"); |
| 297 | return NO_MEMORY; |
Mathias Agopian | e60b068 | 2012-08-21 23:34:09 -0700 | [diff] [blame] | 298 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 299 | |
| 300 | mDisplayData[displaySlot].hwcDisplay = display; |
| 301 | |
| 302 | --mRemainingHwcVirtualDisplays; |
| 303 | *outId = static_cast<int32_t>(displaySlot); |
| 304 | |
Mathias Agopian | e60b068 | 2012-08-21 23:34:09 -0700 | [diff] [blame] | 305 | return NO_ERROR; |
| 306 | } |
| 307 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 308 | std::shared_ptr<HWC2::Layer> HWComposer::createLayer(int32_t displayId) { |
| 309 | if (!isValidDisplay(displayId)) { |
| 310 | ALOGE("Failed to create layer on invalid display %d", displayId); |
| 311 | return nullptr; |
| 312 | } |
| 313 | auto display = mDisplayData[displayId].hwcDisplay; |
| 314 | std::shared_ptr<HWC2::Layer> layer; |
| 315 | auto error = display->createLayer(&layer); |
| 316 | if (error != HWC2::Error::None) { |
| 317 | ALOGE("Failed to create layer on display %d: %s (%d)", displayId, |
| 318 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 319 | return nullptr; |
| 320 | } |
| 321 | return layer; |
| 322 | } |
| 323 | |
Fabien Sanglard | df0b705 | 2016-11-30 15:51:53 -0800 | [diff] [blame] | 324 | nsecs_t HWComposer::getRefreshTimestamp(int32_t displayId) const { |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 325 | // this returns the last refresh timestamp. |
| 326 | // if the last one is not available, we estimate it based on |
| 327 | // the refresh period and whatever closest timestamp we have. |
| 328 | Mutex::Autolock _l(mLock); |
| 329 | nsecs_t now = systemTime(CLOCK_MONOTONIC); |
Fabien Sanglard | df0b705 | 2016-11-30 15:51:53 -0800 | [diff] [blame] | 330 | auto vsyncPeriod = getActiveConfig(displayId)->getVsyncPeriod(); |
| 331 | return now - ((now - mLastHwVSync[displayId]) % vsyncPeriod); |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Fabien Sanglard | df0b705 | 2016-11-30 15:51:53 -0800 | [diff] [blame] | 334 | bool HWComposer::isConnected(int32_t displayId) const { |
| 335 | if (!isValidDisplay(displayId)) { |
| 336 | ALOGE("isConnected: Attempted to access invalid display %d", displayId); |
Mathias Agopian | e60b068 | 2012-08-21 23:34:09 -0700 | [diff] [blame] | 337 | return false; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 338 | } |
Fabien Sanglard | df0b705 | 2016-11-30 15:51:53 -0800 | [diff] [blame] | 339 | return mDisplayData[displayId].hwcDisplay->isConnected(); |
Mathias Agopian | 9c6e297 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 342 | std::vector<std::shared_ptr<const HWC2::Display::Config>> |
| 343 | HWComposer::getConfigs(int32_t displayId) const { |
| 344 | if (!isValidDisplay(displayId)) { |
| 345 | ALOGE("getConfigs: Attempted to access invalid display %d", displayId); |
| 346 | return {}; |
| 347 | } |
| 348 | auto& displayData = mDisplayData[displayId]; |
| 349 | auto configs = mDisplayData[displayId].hwcDisplay->getConfigs(); |
| 350 | if (displayData.configMap.empty()) { |
| 351 | for (size_t i = 0; i < configs.size(); ++i) { |
| 352 | displayData.configMap[i] = configs[i]; |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 353 | } |
| 354 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 355 | return configs; |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 358 | std::shared_ptr<const HWC2::Display::Config> |
| 359 | HWComposer::getActiveConfig(int32_t displayId) const { |
| 360 | if (!isValidDisplay(displayId)) { |
Fabien Sanglard | b7432cc | 2016-11-11 09:40:27 -0800 | [diff] [blame] | 361 | ALOGV("getActiveConfigs: Attempted to access invalid display %d", |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 362 | displayId); |
| 363 | return nullptr; |
Mathias Agopian | 5895934 | 2010-10-07 14:57:04 -0700 | [diff] [blame] | 364 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 365 | std::shared_ptr<const HWC2::Display::Config> config; |
| 366 | auto error = mDisplayData[displayId].hwcDisplay->getActiveConfig(&config); |
| 367 | if (error == HWC2::Error::BadConfig) { |
Fabien Sanglard | b7432cc | 2016-11-11 09:40:27 -0800 | [diff] [blame] | 368 | ALOGE("getActiveConfig: No config active, returning null"); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 369 | return nullptr; |
| 370 | } else if (error != HWC2::Error::None) { |
| 371 | ALOGE("getActiveConfig failed for display %d: %s (%d)", displayId, |
| 372 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 373 | return nullptr; |
| 374 | } else if (!config) { |
| 375 | ALOGE("getActiveConfig returned an unknown config for display %d", |
| 376 | displayId); |
| 377 | return nullptr; |
| 378 | } |
| 379 | |
| 380 | return config; |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 383 | std::vector<android_color_mode_t> HWComposer::getColorModes(int32_t displayId) const { |
| 384 | std::vector<android_color_mode_t> modes; |
Courtney Goeltzenleuchter | fad9d8c | 2016-06-23 11:49:50 -0600 | [diff] [blame] | 385 | |
| 386 | if (!isValidDisplay(displayId)) { |
| 387 | ALOGE("getColorModes: Attempted to access invalid display %d", |
| 388 | displayId); |
| 389 | return modes; |
| 390 | } |
| 391 | const std::shared_ptr<HWC2::Display>& hwcDisplay = |
| 392 | mDisplayData[displayId].hwcDisplay; |
| 393 | |
| 394 | auto error = hwcDisplay->getColorModes(&modes); |
| 395 | if (error != HWC2::Error::None) { |
| 396 | ALOGE("getColorModes failed for display %d: %s (%d)", displayId, |
| 397 | to_string(error).c_str(), static_cast<int32_t>(error)); |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 398 | return std::vector<android_color_mode_t>(); |
Courtney Goeltzenleuchter | fad9d8c | 2016-06-23 11:49:50 -0600 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | return modes; |
| 402 | } |
| 403 | |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 404 | status_t HWComposer::setActiveColorMode(int32_t displayId, android_color_mode_t mode) { |
| 405 | if (!isValidDisplay(displayId)) { |
| 406 | ALOGE("setActiveColorMode: Display %d is not valid", displayId); |
| 407 | return BAD_INDEX; |
| 408 | } |
| 409 | |
| 410 | auto& displayData = mDisplayData[displayId]; |
| 411 | auto error = displayData.hwcDisplay->setColorMode(mode); |
| 412 | if (error != HWC2::Error::None) { |
| 413 | ALOGE("setActiveConfig: Failed to set color mode %d on display %d: " |
| 414 | "%s (%d)", mode, displayId, to_string(error).c_str(), |
| 415 | static_cast<int32_t>(error)); |
| 416 | return UNKNOWN_ERROR; |
| 417 | } |
| 418 | |
| 419 | return NO_ERROR; |
| 420 | } |
| 421 | |
| 422 | |
Fabien Sanglard | df0b705 | 2016-11-30 15:51:53 -0800 | [diff] [blame] | 423 | void HWComposer::setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled) { |
| 424 | if (displayId < 0 || displayId >= HWC_DISPLAY_VIRTUAL) { |
| 425 | ALOGD("setVsyncEnabled: Ignoring for virtual display %d", displayId); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 426 | return; |
| 427 | } |
| 428 | |
Fabien Sanglard | df0b705 | 2016-11-30 15:51:53 -0800 | [diff] [blame] | 429 | if (!isValidDisplay(displayId)) { |
| 430 | ALOGE("setVsyncEnabled: Attempted to access invalid display %d", |
| 431 | displayId); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | |
| 435 | // NOTE: we use our own internal lock here because we have to call |
| 436 | // into the HWC with the lock held, and we want to make sure |
| 437 | // that even if HWC blocks (which it shouldn't), it won't |
| 438 | // affect other threads. |
| 439 | Mutex::Autolock _l(mVsyncLock); |
Fabien Sanglard | df0b705 | 2016-11-30 15:51:53 -0800 | [diff] [blame] | 440 | auto& displayData = mDisplayData[displayId]; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 441 | if (enabled != displayData.vsyncEnabled) { |
| 442 | ATRACE_CALL(); |
| 443 | auto error = displayData.hwcDisplay->setVsyncEnabled(enabled); |
| 444 | if (error == HWC2::Error::None) { |
| 445 | displayData.vsyncEnabled = enabled; |
| 446 | |
| 447 | char tag[16]; |
Fabien Sanglard | df0b705 | 2016-11-30 15:51:53 -0800 | [diff] [blame] | 448 | snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", displayId); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 449 | ATRACE_INT(tag, enabled == HWC2::Vsync::Enable ? 1 : 0); |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 450 | } else { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 451 | ALOGE("setVsyncEnabled: Failed to set vsync to %s on %d/%" PRIu64 |
Fabien Sanglard | df0b705 | 2016-11-30 15:51:53 -0800 | [diff] [blame] | 452 | ": %s (%d)", to_string(enabled).c_str(), displayId, |
| 453 | mDisplayData[displayId].hwcDisplay->getId(), |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 454 | to_string(error).c_str(), static_cast<int32_t>(error)); |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 455 | } |
Colin Cross | 10fbdb6 | 2012-07-12 17:56:34 -0700 | [diff] [blame] | 456 | } |
Colin Cross | 10fbdb6 | 2012-07-12 17:56:34 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Chia-I Wu | 06d63de | 2017-01-04 14:58:51 +0800 | [diff] [blame] | 459 | status_t HWComposer::setClientTarget(int32_t displayId, uint32_t slot, |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 460 | const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target, |
| 461 | android_dataspace_t dataspace) { |
| 462 | if (!isValidDisplay(displayId)) { |
Jesse Hall | 851cfe8 | 2013-03-20 13:44:00 -0700 | [diff] [blame] | 463 | return BAD_INDEX; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 464 | } |
Jesse Hall | 851cfe8 | 2013-03-20 13:44:00 -0700 | [diff] [blame] | 465 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 466 | ALOGV("setClientTarget for display %d", displayId); |
| 467 | auto& hwcDisplay = mDisplayData[displayId].hwcDisplay; |
| 468 | buffer_handle_t handle = nullptr; |
| 469 | if ((target != nullptr) && target->getNativeBuffer()) { |
| 470 | handle = target->getNativeBuffer()->handle; |
| 471 | } |
Chia-I Wu | 06d63de | 2017-01-04 14:58:51 +0800 | [diff] [blame] | 472 | auto error = hwcDisplay->setClientTarget(slot, handle, |
| 473 | acquireFence, dataspace); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 474 | if (error != HWC2::Error::None) { |
| 475 | ALOGE("Failed to set client target for display %d: %s (%d)", displayId, |
| 476 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 477 | return BAD_VALUE; |
| 478 | } |
| 479 | |
Jesse Hall | 851cfe8 | 2013-03-20 13:44:00 -0700 | [diff] [blame] | 480 | return NO_ERROR; |
| 481 | } |
| 482 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 483 | status_t HWComposer::prepare(DisplayDevice& displayDevice) { |
| 484 | ATRACE_CALL(); |
| 485 | |
| 486 | Mutex::Autolock _l(mDisplayLock); |
| 487 | auto displayId = displayDevice.getHwcDisplayId(); |
Dan Stoza | ec0f717 | 2016-07-21 11:09:40 -0700 | [diff] [blame] | 488 | if (displayId == DisplayDevice::DISPLAY_ID_INVALID) { |
| 489 | ALOGV("Skipping HWComposer prepare for non-HWC display"); |
| 490 | return NO_ERROR; |
| 491 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 492 | if (!isValidDisplay(displayId)) { |
| 493 | return BAD_INDEX; |
| 494 | } |
| 495 | |
| 496 | auto& displayData = mDisplayData[displayId]; |
| 497 | auto& hwcDisplay = displayData.hwcDisplay; |
| 498 | if (!hwcDisplay->isConnected()) { |
| 499 | return NO_ERROR; |
| 500 | } |
| 501 | |
Chia-I Wu | 843460d | 2017-03-01 16:08:51 -0800 | [diff] [blame] | 502 | mDumpMayLockUp = true; |
| 503 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 504 | uint32_t numTypes = 0; |
| 505 | uint32_t numRequests = 0; |
| 506 | auto error = hwcDisplay->validate(&numTypes, &numRequests); |
| 507 | if (error != HWC2::Error::None && error != HWC2::Error::HasChanges) { |
| 508 | ALOGE("prepare: validate failed for display %d: %s (%d)", displayId, |
| 509 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 510 | return BAD_INDEX; |
| 511 | } |
| 512 | |
| 513 | std::unordered_map<std::shared_ptr<HWC2::Layer>, HWC2::Composition> |
| 514 | changedTypes; |
| 515 | changedTypes.reserve(numTypes); |
| 516 | error = hwcDisplay->getChangedCompositionTypes(&changedTypes); |
| 517 | if (error != HWC2::Error::None) { |
| 518 | ALOGE("prepare: getChangedCompositionTypes failed on display %d: " |
| 519 | "%s (%d)", displayId, to_string(error).c_str(), |
| 520 | static_cast<int32_t>(error)); |
| 521 | return BAD_INDEX; |
| 522 | } |
| 523 | |
| 524 | |
| 525 | displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0); |
| 526 | std::unordered_map<std::shared_ptr<HWC2::Layer>, HWC2::LayerRequest> |
| 527 | layerRequests; |
| 528 | layerRequests.reserve(numRequests); |
| 529 | error = hwcDisplay->getRequests(&displayData.displayRequests, |
| 530 | &layerRequests); |
| 531 | if (error != HWC2::Error::None) { |
| 532 | ALOGE("prepare: getRequests failed on display %d: %s (%d)", displayId, |
| 533 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 534 | return BAD_INDEX; |
| 535 | } |
| 536 | |
| 537 | displayData.hasClientComposition = false; |
| 538 | displayData.hasDeviceComposition = false; |
| 539 | for (auto& layer : displayDevice.getVisibleLayersSortedByZ()) { |
| 540 | auto hwcLayer = layer->getHwcLayer(displayId); |
| 541 | |
| 542 | if (changedTypes.count(hwcLayer) != 0) { |
| 543 | // We pass false so we only update our state and don't call back |
| 544 | // into the HWC device |
| 545 | validateChange(layer->getCompositionType(displayId), |
| 546 | changedTypes[hwcLayer]); |
| 547 | layer->setCompositionType(displayId, changedTypes[hwcLayer], false); |
| 548 | } |
| 549 | |
| 550 | switch (layer->getCompositionType(displayId)) { |
| 551 | case HWC2::Composition::Client: |
| 552 | displayData.hasClientComposition = true; |
| 553 | break; |
| 554 | case HWC2::Composition::Device: |
| 555 | case HWC2::Composition::SolidColor: |
| 556 | case HWC2::Composition::Cursor: |
| 557 | case HWC2::Composition::Sideband: |
| 558 | displayData.hasDeviceComposition = true; |
| 559 | break; |
| 560 | default: |
| 561 | break; |
| 562 | } |
| 563 | |
| 564 | if (layerRequests.count(hwcLayer) != 0 && |
| 565 | layerRequests[hwcLayer] == |
| 566 | HWC2::LayerRequest::ClearClientTarget) { |
| 567 | layer->setClearClientTarget(displayId, true); |
| 568 | } else { |
| 569 | if (layerRequests.count(hwcLayer) != 0) { |
| 570 | ALOGE("prepare: Unknown layer request: %s", |
| 571 | to_string(layerRequests[hwcLayer]).c_str()); |
| 572 | } |
| 573 | layer->setClearClientTarget(displayId, false); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | error = hwcDisplay->acceptChanges(); |
| 578 | if (error != HWC2::Error::None) { |
| 579 | ALOGE("prepare: acceptChanges failed: %s", to_string(error).c_str()); |
| 580 | return BAD_INDEX; |
| 581 | } |
| 582 | |
| 583 | return NO_ERROR; |
| 584 | } |
| 585 | |
| 586 | bool HWComposer::hasDeviceComposition(int32_t displayId) const { |
Dan Stoza | ec0f717 | 2016-07-21 11:09:40 -0700 | [diff] [blame] | 587 | if (displayId == DisplayDevice::DISPLAY_ID_INVALID) { |
| 588 | // Displays without a corresponding HWC display are never composed by |
| 589 | // the device |
| 590 | return false; |
| 591 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 592 | if (!isValidDisplay(displayId)) { |
| 593 | ALOGE("hasDeviceComposition: Invalid display %d", displayId); |
| 594 | return false; |
| 595 | } |
| 596 | return mDisplayData[displayId].hasDeviceComposition; |
| 597 | } |
| 598 | |
| 599 | bool HWComposer::hasClientComposition(int32_t displayId) const { |
Dan Stoza | ec0f717 | 2016-07-21 11:09:40 -0700 | [diff] [blame] | 600 | if (displayId == DisplayDevice::DISPLAY_ID_INVALID) { |
| 601 | // Displays without a corresponding HWC display are always composed by |
| 602 | // the client |
| 603 | return true; |
| 604 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 605 | if (!isValidDisplay(displayId)) { |
| 606 | ALOGE("hasClientComposition: Invalid display %d", displayId); |
| 607 | return true; |
| 608 | } |
| 609 | return mDisplayData[displayId].hasClientComposition; |
| 610 | } |
| 611 | |
Fabien Sanglard | 11d0fc3 | 2016-12-01 15:43:01 -0800 | [diff] [blame] | 612 | sp<Fence> HWComposer::getPresentFence(int32_t displayId) const { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 613 | if (!isValidDisplay(displayId)) { |
Fabien Sanglard | 11d0fc3 | 2016-12-01 15:43:01 -0800 | [diff] [blame] | 614 | ALOGE("getPresentFence failed for invalid display %d", displayId); |
Jesse Hall | 851cfe8 | 2013-03-20 13:44:00 -0700 | [diff] [blame] | 615 | return Fence::NO_FENCE; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 616 | } |
Fabien Sanglard | 11d0fc3 | 2016-12-01 15:43:01 -0800 | [diff] [blame] | 617 | return mDisplayData[displayId].lastPresentFence; |
Jesse Hall | 851cfe8 | 2013-03-20 13:44:00 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 620 | bool HWComposer::presentFenceRepresentsStartOfScanout() const { |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 621 | return mAdapter ? false : true; |
| 622 | } |
| 623 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 624 | sp<Fence> HWComposer::getLayerReleaseFence(int32_t displayId, |
| 625 | const std::shared_ptr<HWC2::Layer>& layer) const { |
| 626 | if (!isValidDisplay(displayId)) { |
| 627 | ALOGE("getLayerReleaseFence: Invalid display"); |
| 628 | return Fence::NO_FENCE; |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 629 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 630 | auto displayFences = mDisplayData[displayId].releaseFences; |
| 631 | if (displayFences.count(layer) == 0) { |
| 632 | ALOGV("getLayerReleaseFence: Release fence not found"); |
| 633 | return Fence::NO_FENCE; |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 634 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 635 | return displayFences[layer]; |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Fabien Sanglard | a87aa7b | 2016-11-30 16:27:22 -0800 | [diff] [blame] | 638 | status_t HWComposer::presentAndGetReleaseFences(int32_t displayId) { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 639 | ATRACE_CALL(); |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 640 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 641 | if (!isValidDisplay(displayId)) { |
| 642 | return BAD_INDEX; |
Mathias Agopian | c397360 | 2012-08-31 17:51:25 -0700 | [diff] [blame] | 643 | } |
Pablo Ceballos | d814cf2 | 2015-09-11 14:37:39 -0700 | [diff] [blame] | 644 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 645 | auto& displayData = mDisplayData[displayId]; |
| 646 | auto& hwcDisplay = displayData.hwcDisplay; |
Fabien Sanglard | 11d0fc3 | 2016-12-01 15:43:01 -0800 | [diff] [blame] | 647 | auto error = hwcDisplay->present(&displayData.lastPresentFence); |
Chia-I Wu | 843460d | 2017-03-01 16:08:51 -0800 | [diff] [blame] | 648 | |
| 649 | mDumpMayLockUp = false; |
| 650 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 651 | if (error != HWC2::Error::None) { |
Fabien Sanglard | a87aa7b | 2016-11-30 16:27:22 -0800 | [diff] [blame] | 652 | ALOGE("presentAndGetReleaseFences: failed for display %d: %s (%d)", |
| 653 | displayId, to_string(error).c_str(), static_cast<int32_t>(error)); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 654 | return UNKNOWN_ERROR; |
| 655 | } |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 656 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 657 | std::unordered_map<std::shared_ptr<HWC2::Layer>, sp<Fence>> releaseFences; |
| 658 | error = hwcDisplay->getReleaseFences(&releaseFences); |
| 659 | if (error != HWC2::Error::None) { |
Fabien Sanglard | a87aa7b | 2016-11-30 16:27:22 -0800 | [diff] [blame] | 660 | ALOGE("presentAndGetReleaseFences: Failed to get release fences " |
| 661 | "for display %d: %s (%d)", |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 662 | displayId, to_string(error).c_str(), |
| 663 | static_cast<int32_t>(error)); |
| 664 | return UNKNOWN_ERROR; |
Mathias Agopian | f435863 | 2012-08-22 17:16:19 -0700 | [diff] [blame] | 665 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 666 | |
| 667 | displayData.releaseFences = std::move(releaseFences); |
| 668 | |
| 669 | return NO_ERROR; |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 672 | status_t HWComposer::setPowerMode(int32_t displayId, int32_t intMode) { |
| 673 | ALOGV("setPowerMode(%d, %d)", displayId, intMode); |
| 674 | if (!isValidDisplay(displayId)) { |
| 675 | ALOGE("setPowerMode: Bad display"); |
| 676 | return BAD_INDEX; |
| 677 | } |
| 678 | if (displayId >= VIRTUAL_DISPLAY_ID_BASE) { |
| 679 | ALOGE("setPowerMode: Virtual display %d passed in, returning", |
| 680 | displayId); |
| 681 | return BAD_INDEX; |
| 682 | } |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 683 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 684 | auto mode = static_cast<HWC2::PowerMode>(intMode); |
| 685 | if (mode == HWC2::PowerMode::Off) { |
| 686 | setVsyncEnabled(displayId, HWC2::Vsync::Disable); |
| 687 | } |
| 688 | |
| 689 | auto& hwcDisplay = mDisplayData[displayId].hwcDisplay; |
| 690 | switch (mode) { |
| 691 | case HWC2::PowerMode::Off: |
| 692 | case HWC2::PowerMode::On: |
| 693 | ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str()); |
| 694 | { |
| 695 | auto error = hwcDisplay->setPowerMode(mode); |
| 696 | if (error != HWC2::Error::None) { |
| 697 | ALOGE("setPowerMode: Unable to set power mode %s for " |
| 698 | "display %d: %s (%d)", to_string(mode).c_str(), |
| 699 | displayId, to_string(error).c_str(), |
| 700 | static_cast<int32_t>(error)); |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 701 | } |
| 702 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 703 | break; |
| 704 | case HWC2::PowerMode::Doze: |
| 705 | case HWC2::PowerMode::DozeSuspend: |
| 706 | ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str()); |
| 707 | { |
| 708 | bool supportsDoze = false; |
| 709 | auto error = hwcDisplay->supportsDoze(&supportsDoze); |
| 710 | if (error != HWC2::Error::None) { |
| 711 | ALOGE("setPowerMode: Unable to query doze support for " |
| 712 | "display %d: %s (%d)", displayId, |
| 713 | to_string(error).c_str(), |
| 714 | static_cast<int32_t>(error)); |
| 715 | } |
| 716 | if (!supportsDoze) { |
| 717 | mode = HWC2::PowerMode::On; |
| 718 | } |
| 719 | |
| 720 | error = hwcDisplay->setPowerMode(mode); |
| 721 | if (error != HWC2::Error::None) { |
| 722 | ALOGE("setPowerMode: Unable to set power mode %s for " |
| 723 | "display %d: %s (%d)", to_string(mode).c_str(), |
| 724 | displayId, to_string(error).c_str(), |
| 725 | static_cast<int32_t>(error)); |
| 726 | } |
| 727 | } |
| 728 | break; |
| 729 | default: |
| 730 | ALOGV("setPowerMode: Not calling HWC"); |
| 731 | break; |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 732 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 733 | |
| 734 | return NO_ERROR; |
| 735 | } |
| 736 | |
| 737 | status_t HWComposer::setActiveConfig(int32_t displayId, size_t configId) { |
| 738 | if (!isValidDisplay(displayId)) { |
| 739 | ALOGE("setActiveConfig: Display %d is not valid", displayId); |
| 740 | return BAD_INDEX; |
| 741 | } |
| 742 | |
| 743 | auto& displayData = mDisplayData[displayId]; |
| 744 | if (displayData.configMap.count(configId) == 0) { |
| 745 | ALOGE("setActiveConfig: Invalid config %zd", configId); |
| 746 | return BAD_INDEX; |
| 747 | } |
| 748 | |
| 749 | auto error = displayData.hwcDisplay->setActiveConfig( |
| 750 | displayData.configMap[configId]); |
| 751 | if (error != HWC2::Error::None) { |
| 752 | ALOGE("setActiveConfig: Failed to set config %zu on display %d: " |
| 753 | "%s (%d)", configId, displayId, to_string(error).c_str(), |
| 754 | static_cast<int32_t>(error)); |
| 755 | return UNKNOWN_ERROR; |
| 756 | } |
| 757 | |
| 758 | return NO_ERROR; |
| 759 | } |
| 760 | |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 761 | status_t HWComposer::setColorTransform(int32_t displayId, |
| 762 | const mat4& transform) { |
| 763 | if (!isValidDisplay(displayId)) { |
| 764 | ALOGE("setColorTransform: Display %d is not valid", displayId); |
| 765 | return BAD_INDEX; |
| 766 | } |
| 767 | |
| 768 | auto& displayData = mDisplayData[displayId]; |
| 769 | bool isIdentity = transform == mat4(); |
| 770 | auto error = displayData.hwcDisplay->setColorTransform(transform, |
| 771 | isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY : |
| 772 | HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX); |
| 773 | if (error != HWC2::Error::None) { |
| 774 | ALOGE("setColorTransform: Failed to set transform on display %d: " |
| 775 | "%s (%d)", displayId, to_string(error).c_str(), |
| 776 | static_cast<int32_t>(error)); |
| 777 | return UNKNOWN_ERROR; |
| 778 | } |
| 779 | |
| 780 | return NO_ERROR; |
| 781 | } |
| 782 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 783 | void HWComposer::disconnectDisplay(int displayId) { |
| 784 | LOG_ALWAYS_FATAL_IF(displayId < 0); |
| 785 | auto& displayData = mDisplayData[displayId]; |
| 786 | |
| 787 | auto displayType = HWC2::DisplayType::Invalid; |
| 788 | auto error = displayData.hwcDisplay->getType(&displayType); |
| 789 | if (error != HWC2::Error::None) { |
| 790 | ALOGE("disconnectDisplay: Failed to determine type of display %d", |
| 791 | displayId); |
| 792 | return; |
| 793 | } |
| 794 | |
| 795 | // If this was a virtual display, add its slot back for reuse by future |
| 796 | // virtual displays |
| 797 | if (displayType == HWC2::DisplayType::Virtual) { |
| 798 | mFreeDisplaySlots.insert(displayId); |
| 799 | ++mRemainingHwcVirtualDisplays; |
| 800 | } |
| 801 | |
| 802 | auto hwcId = displayData.hwcDisplay->getId(); |
| 803 | mHwcDisplaySlots.erase(hwcId); |
| 804 | displayData.reset(); |
| 805 | } |
| 806 | |
| 807 | status_t HWComposer::setOutputBuffer(int32_t displayId, |
| 808 | const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) { |
| 809 | if (!isValidDisplay(displayId)) { |
| 810 | ALOGE("setOutputBuffer: Display %d is not valid", displayId); |
| 811 | return BAD_INDEX; |
| 812 | } |
| 813 | |
| 814 | auto& hwcDisplay = mDisplayData[displayId].hwcDisplay; |
| 815 | auto displayType = HWC2::DisplayType::Invalid; |
| 816 | auto error = hwcDisplay->getType(&displayType); |
| 817 | if (error != HWC2::Error::None) { |
| 818 | ALOGE("setOutputBuffer: Failed to determine type of display %d", |
| 819 | displayId); |
| 820 | return NAME_NOT_FOUND; |
| 821 | } |
| 822 | |
| 823 | if (displayType != HWC2::DisplayType::Virtual) { |
| 824 | ALOGE("setOutputBuffer: Display %d is not virtual", displayId); |
| 825 | return INVALID_OPERATION; |
| 826 | } |
| 827 | |
| 828 | error = hwcDisplay->setOutputBuffer(buffer, acquireFence); |
| 829 | if (error != HWC2::Error::None) { |
| 830 | ALOGE("setOutputBuffer: Failed to set buffer on display %d: %s (%d)", |
| 831 | displayId, to_string(error).c_str(), |
| 832 | static_cast<int32_t>(error)); |
| 833 | return UNKNOWN_ERROR; |
| 834 | } |
| 835 | |
| 836 | return NO_ERROR; |
| 837 | } |
| 838 | |
| 839 | void HWComposer::clearReleaseFences(int32_t displayId) { |
| 840 | if (!isValidDisplay(displayId)) { |
| 841 | ALOGE("clearReleaseFences: Display %d is not valid", displayId); |
| 842 | return; |
| 843 | } |
| 844 | mDisplayData[displayId].releaseFences.clear(); |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Dan Stoza | c4f471e | 2016-03-24 09:31:08 -0700 | [diff] [blame] | 847 | std::unique_ptr<HdrCapabilities> HWComposer::getHdrCapabilities( |
| 848 | int32_t displayId) { |
| 849 | if (!isValidDisplay(displayId)) { |
| 850 | ALOGE("getHdrCapabilities: Display %d is not valid", displayId); |
| 851 | return nullptr; |
| 852 | } |
| 853 | |
| 854 | auto& hwcDisplay = mDisplayData[displayId].hwcDisplay; |
| 855 | std::unique_ptr<HdrCapabilities> capabilities; |
| 856 | auto error = hwcDisplay->getHdrCapabilities(&capabilities); |
| 857 | if (error != HWC2::Error::None) { |
| 858 | ALOGE("getOutputCapabilities: Failed to get capabilities on display %d:" |
| 859 | " %s (%d)", displayId, to_string(error).c_str(), |
| 860 | static_cast<int32_t>(error)); |
| 861 | return nullptr; |
| 862 | } |
| 863 | |
| 864 | return capabilities; |
| 865 | } |
| 866 | |
Andy McFadden | 4df87bd | 2014-04-21 18:08:54 -0700 | [diff] [blame] | 867 | // Converts a PixelFormat to a human-readable string. Max 11 chars. |
| 868 | // (Could use a table of prefab String8 objects.) |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 869 | /* |
Andy McFadden | 4df87bd | 2014-04-21 18:08:54 -0700 | [diff] [blame] | 870 | static String8 getFormatStr(PixelFormat format) { |
| 871 | switch (format) { |
| 872 | case PIXEL_FORMAT_RGBA_8888: return String8("RGBA_8888"); |
| 873 | case PIXEL_FORMAT_RGBX_8888: return String8("RGBx_8888"); |
| 874 | case PIXEL_FORMAT_RGB_888: return String8("RGB_888"); |
| 875 | case PIXEL_FORMAT_RGB_565: return String8("RGB_565"); |
| 876 | case PIXEL_FORMAT_BGRA_8888: return String8("BGRA_8888"); |
Andy McFadden | f0058ca | 2014-05-20 13:28:50 -0700 | [diff] [blame] | 877 | case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: |
| 878 | return String8("ImplDef"); |
Andy McFadden | 4df87bd | 2014-04-21 18:08:54 -0700 | [diff] [blame] | 879 | default: |
| 880 | String8 result; |
| 881 | result.appendFormat("? %08x", format); |
| 882 | return result; |
| 883 | } |
| 884 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 885 | */ |
Andy McFadden | 4df87bd | 2014-04-21 18:08:54 -0700 | [diff] [blame] | 886 | |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 887 | bool HWComposer::isUsingVrComposer() const { |
| 888 | #ifdef BYPASS_IHWC |
| 889 | return false; |
| 890 | #else |
| 891 | return getComposer()->isUsingVrComposer(); |
| 892 | #endif |
| 893 | } |
| 894 | |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 895 | void HWComposer::dump(String8& result) const { |
Chia-I Wu | 843460d | 2017-03-01 16:08:51 -0800 | [diff] [blame] | 896 | if (mDumpMayLockUp) { |
| 897 | result.append("HWComposer dump skipped because present in progress"); |
| 898 | return; |
| 899 | } |
| 900 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 901 | // TODO: In order to provide a dump equivalent to HWC1, we need to shadow |
| 902 | // all the state going into the layers. This is probably better done in |
| 903 | // Layer itself, but it's going to take a bit of work to get there. |
| 904 | result.append(mHwcDevice->dump().c_str()); |
Mathias Agopian | 8372785 | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 905 | } |
| 906 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 907 | // --------------------------------------------------------------------------- |
Mathias Agopian | 2965b26 | 2012-04-08 15:13:32 -0700 | [diff] [blame] | 908 | |
Jesse Hall | a9a1b00 | 2013-02-27 16:39:25 -0800 | [diff] [blame] | 909 | HWComposer::DisplayData::DisplayData() |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 910 | : hasClientComposition(false), |
| 911 | hasDeviceComposition(false), |
| 912 | hwcDisplay(), |
Fabien Sanglard | 11d0fc3 | 2016-12-01 15:43:01 -0800 | [diff] [blame] | 913 | lastPresentFence(Fence::NO_FENCE), |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 914 | outbufHandle(nullptr), |
| 915 | outbufAcquireFence(Fence::NO_FENCE), |
| 916 | vsyncEnabled(HWC2::Vsync::Disable) { |
| 917 | ALOGV("Created new DisplayData"); |
| 918 | } |
Jesse Hall | a9a1b00 | 2013-02-27 16:39:25 -0800 | [diff] [blame] | 919 | |
| 920 | HWComposer::DisplayData::~DisplayData() { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | void HWComposer::DisplayData::reset() { |
| 924 | ALOGV("DisplayData reset"); |
| 925 | *this = DisplayData(); |
Jesse Hall | a9a1b00 | 2013-02-27 16:39:25 -0800 | [diff] [blame] | 926 | } |
| 927 | |
Mathias Agopian | 2965b26 | 2012-04-08 15:13:32 -0700 | [diff] [blame] | 928 | // --------------------------------------------------------------------------- |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 929 | }; // namespace android |