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