Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | |
| 19 | #undef LOG_TAG |
| 20 | #define LOG_TAG "HWC2On1Adapter" |
| 21 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 22 | |
| 23 | #include "HWC2On1Adapter.h" |
| 24 | |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 25 | #include <inttypes.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 26 | |
| 27 | #include <chrono> |
| 28 | #include <cstdlib> |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 29 | #include <sstream> |
| 30 | |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 31 | #include <hardware/hwcomposer.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 32 | #include <log/log.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 33 | #include <utils/Trace.h> |
| 34 | |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 35 | using namespace std::chrono_literals; |
| 36 | |
| 37 | static bool operator==(const hwc_color_t& lhs, const hwc_color_t& rhs) { |
| 38 | return lhs.r == rhs.r && |
| 39 | lhs.g == rhs.g && |
| 40 | lhs.b == rhs.b && |
| 41 | lhs.a == rhs.a; |
| 42 | } |
| 43 | |
| 44 | static bool operator==(const hwc_rect_t& lhs, const hwc_rect_t& rhs) { |
| 45 | return lhs.left == rhs.left && |
| 46 | lhs.top == rhs.top && |
| 47 | lhs.right == rhs.right && |
| 48 | lhs.bottom == rhs.bottom; |
| 49 | } |
| 50 | |
| 51 | static bool operator==(const hwc_frect_t& lhs, const hwc_frect_t& rhs) { |
| 52 | return lhs.left == rhs.left && |
| 53 | lhs.top == rhs.top && |
| 54 | lhs.right == rhs.right && |
| 55 | lhs.bottom == rhs.bottom; |
| 56 | } |
| 57 | |
| 58 | template <typename T> |
| 59 | static inline bool operator!=(const T& lhs, const T& rhs) |
| 60 | { |
| 61 | return !(lhs == rhs); |
| 62 | } |
| 63 | |
| 64 | static uint8_t getMinorVersion(struct hwc_composer_device_1* device) |
| 65 | { |
| 66 | auto version = device->common.version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK; |
| 67 | return (version >> 16) & 0xF; |
| 68 | } |
| 69 | |
| 70 | template <typename PFN, typename T> |
| 71 | static hwc2_function_pointer_t asFP(T function) |
| 72 | { |
| 73 | static_assert(std::is_same<PFN, T>::value, "Incompatible function pointer"); |
| 74 | return reinterpret_cast<hwc2_function_pointer_t>(function); |
| 75 | } |
| 76 | |
| 77 | using namespace HWC2; |
| 78 | |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 79 | static constexpr Attribute ColorMode = static_cast<Attribute>(6); |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 80 | |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 81 | namespace android { |
| 82 | |
| 83 | void HWC2On1Adapter::DisplayContentsDeleter::operator()( |
| 84 | hwc_display_contents_1_t* contents) |
| 85 | { |
| 86 | if (contents != nullptr) { |
| 87 | for (size_t l = 0; l < contents->numHwLayers; ++l) { |
| 88 | auto& layer = contents->hwLayers[l]; |
| 89 | std::free(const_cast<hwc_rect_t*>(layer.visibleRegionScreen.rects)); |
Fabien Sanglard | ec0a956 | 2016-12-13 11:57:33 -0800 | [diff] [blame] | 90 | std::free(const_cast<hwc_rect_t*>(layer.surfaceDamage.rects)); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | std::free(contents); |
| 94 | } |
| 95 | |
| 96 | class HWC2On1Adapter::Callbacks : public hwc_procs_t { |
| 97 | public: |
Chih-Hung Hsieh | c406791 | 2016-05-03 14:03:27 -0700 | [diff] [blame] | 98 | explicit Callbacks(HWC2On1Adapter& adapter) : mAdapter(adapter) { |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 99 | invalidate = &invalidateHook; |
| 100 | vsync = &vsyncHook; |
| 101 | hotplug = &hotplugHook; |
| 102 | } |
| 103 | |
| 104 | static void invalidateHook(const hwc_procs_t* procs) { |
| 105 | auto callbacks = static_cast<const Callbacks*>(procs); |
| 106 | callbacks->mAdapter.hwc1Invalidate(); |
| 107 | } |
| 108 | |
| 109 | static void vsyncHook(const hwc_procs_t* procs, int display, |
| 110 | int64_t timestamp) { |
| 111 | auto callbacks = static_cast<const Callbacks*>(procs); |
| 112 | callbacks->mAdapter.hwc1Vsync(display, timestamp); |
| 113 | } |
| 114 | |
| 115 | static void hotplugHook(const hwc_procs_t* procs, int display, |
| 116 | int connected) { |
| 117 | auto callbacks = static_cast<const Callbacks*>(procs); |
| 118 | callbacks->mAdapter.hwc1Hotplug(display, connected); |
| 119 | } |
| 120 | |
| 121 | private: |
| 122 | HWC2On1Adapter& mAdapter; |
| 123 | }; |
| 124 | |
| 125 | static int closeHook(hw_device_t* /*device*/) |
| 126 | { |
| 127 | // Do nothing, since the real work is done in the class destructor, but we |
| 128 | // need to provide a valid function pointer for hwc2_close to call |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | HWC2On1Adapter::HWC2On1Adapter(hwc_composer_device_1_t* hwc1Device) |
| 133 | : mDumpString(), |
| 134 | mHwc1Device(hwc1Device), |
| 135 | mHwc1MinorVersion(getMinorVersion(hwc1Device)), |
| 136 | mHwc1SupportsVirtualDisplays(false), |
Fabien Sanglard | eb3db61 | 2016-11-18 16:12:31 -0800 | [diff] [blame] | 137 | mHwc1SupportsBackgroundColor(false), |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 138 | mHwc1Callbacks(std::make_unique<Callbacks>(*this)), |
| 139 | mCapabilities(), |
| 140 | mLayers(), |
| 141 | mHwc1VirtualDisplay(), |
| 142 | mStateMutex(), |
| 143 | mCallbacks(), |
| 144 | mHasPendingInvalidate(false), |
| 145 | mPendingVsyncs(), |
| 146 | mPendingHotplugs(), |
| 147 | mDisplays(), |
| 148 | mHwc1DisplayMap() |
| 149 | { |
| 150 | common.close = closeHook; |
| 151 | getCapabilities = getCapabilitiesHook; |
| 152 | getFunction = getFunctionHook; |
| 153 | populateCapabilities(); |
| 154 | populatePrimary(); |
| 155 | mHwc1Device->registerProcs(mHwc1Device, |
| 156 | static_cast<const hwc_procs_t*>(mHwc1Callbacks.get())); |
| 157 | } |
| 158 | |
| 159 | HWC2On1Adapter::~HWC2On1Adapter() { |
| 160 | hwc_close_1(mHwc1Device); |
| 161 | } |
| 162 | |
| 163 | void HWC2On1Adapter::doGetCapabilities(uint32_t* outCount, |
| 164 | int32_t* outCapabilities) |
| 165 | { |
| 166 | if (outCapabilities == nullptr) { |
| 167 | *outCount = mCapabilities.size(); |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | auto capabilityIter = mCapabilities.cbegin(); |
| 172 | for (size_t written = 0; written < *outCount; ++written) { |
| 173 | if (capabilityIter == mCapabilities.cend()) { |
| 174 | return; |
| 175 | } |
| 176 | outCapabilities[written] = static_cast<int32_t>(*capabilityIter); |
| 177 | ++capabilityIter; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | hwc2_function_pointer_t HWC2On1Adapter::doGetFunction( |
| 182 | FunctionDescriptor descriptor) |
| 183 | { |
| 184 | switch (descriptor) { |
| 185 | // Device functions |
| 186 | case FunctionDescriptor::CreateVirtualDisplay: |
| 187 | return asFP<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>( |
| 188 | createVirtualDisplayHook); |
| 189 | case FunctionDescriptor::DestroyVirtualDisplay: |
| 190 | return asFP<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>( |
| 191 | destroyVirtualDisplayHook); |
| 192 | case FunctionDescriptor::Dump: |
| 193 | return asFP<HWC2_PFN_DUMP>(dumpHook); |
| 194 | case FunctionDescriptor::GetMaxVirtualDisplayCount: |
| 195 | return asFP<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>( |
| 196 | getMaxVirtualDisplayCountHook); |
| 197 | case FunctionDescriptor::RegisterCallback: |
| 198 | return asFP<HWC2_PFN_REGISTER_CALLBACK>(registerCallbackHook); |
| 199 | |
| 200 | // Display functions |
| 201 | case FunctionDescriptor::AcceptDisplayChanges: |
| 202 | return asFP<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>( |
| 203 | displayHook<decltype(&Display::acceptChanges), |
| 204 | &Display::acceptChanges>); |
| 205 | case FunctionDescriptor::CreateLayer: |
| 206 | return asFP<HWC2_PFN_CREATE_LAYER>( |
| 207 | displayHook<decltype(&Display::createLayer), |
| 208 | &Display::createLayer, hwc2_layer_t*>); |
| 209 | case FunctionDescriptor::DestroyLayer: |
| 210 | return asFP<HWC2_PFN_DESTROY_LAYER>( |
| 211 | displayHook<decltype(&Display::destroyLayer), |
| 212 | &Display::destroyLayer, hwc2_layer_t>); |
| 213 | case FunctionDescriptor::GetActiveConfig: |
| 214 | return asFP<HWC2_PFN_GET_ACTIVE_CONFIG>( |
| 215 | displayHook<decltype(&Display::getActiveConfig), |
| 216 | &Display::getActiveConfig, hwc2_config_t*>); |
| 217 | case FunctionDescriptor::GetChangedCompositionTypes: |
| 218 | return asFP<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>( |
| 219 | displayHook<decltype(&Display::getChangedCompositionTypes), |
| 220 | &Display::getChangedCompositionTypes, uint32_t*, |
| 221 | hwc2_layer_t*, int32_t*>); |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 222 | case FunctionDescriptor::GetColorModes: |
| 223 | return asFP<HWC2_PFN_GET_COLOR_MODES>( |
| 224 | displayHook<decltype(&Display::getColorModes), |
| 225 | &Display::getColorModes, uint32_t*, int32_t*>); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 226 | case FunctionDescriptor::GetDisplayAttribute: |
| 227 | return asFP<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>( |
| 228 | getDisplayAttributeHook); |
| 229 | case FunctionDescriptor::GetDisplayConfigs: |
| 230 | return asFP<HWC2_PFN_GET_DISPLAY_CONFIGS>( |
| 231 | displayHook<decltype(&Display::getConfigs), |
| 232 | &Display::getConfigs, uint32_t*, hwc2_config_t*>); |
| 233 | case FunctionDescriptor::GetDisplayName: |
| 234 | return asFP<HWC2_PFN_GET_DISPLAY_NAME>( |
| 235 | displayHook<decltype(&Display::getName), |
| 236 | &Display::getName, uint32_t*, char*>); |
| 237 | case FunctionDescriptor::GetDisplayRequests: |
| 238 | return asFP<HWC2_PFN_GET_DISPLAY_REQUESTS>( |
| 239 | displayHook<decltype(&Display::getRequests), |
| 240 | &Display::getRequests, int32_t*, uint32_t*, hwc2_layer_t*, |
| 241 | int32_t*>); |
| 242 | case FunctionDescriptor::GetDisplayType: |
| 243 | return asFP<HWC2_PFN_GET_DISPLAY_TYPE>( |
| 244 | displayHook<decltype(&Display::getType), |
| 245 | &Display::getType, int32_t*>); |
| 246 | case FunctionDescriptor::GetDozeSupport: |
| 247 | return asFP<HWC2_PFN_GET_DOZE_SUPPORT>( |
| 248 | displayHook<decltype(&Display::getDozeSupport), |
| 249 | &Display::getDozeSupport, int32_t*>); |
Dan Stoza | ed40eba | 2016-03-16 12:33:52 -0700 | [diff] [blame] | 250 | case FunctionDescriptor::GetHdrCapabilities: |
| 251 | return asFP<HWC2_PFN_GET_HDR_CAPABILITIES>( |
| 252 | displayHook<decltype(&Display::getHdrCapabilities), |
| 253 | &Display::getHdrCapabilities, uint32_t*, int32_t*, float*, |
| 254 | float*, float*>); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 255 | case FunctionDescriptor::GetReleaseFences: |
| 256 | return asFP<HWC2_PFN_GET_RELEASE_FENCES>( |
| 257 | displayHook<decltype(&Display::getReleaseFences), |
| 258 | &Display::getReleaseFences, uint32_t*, hwc2_layer_t*, |
| 259 | int32_t*>); |
| 260 | case FunctionDescriptor::PresentDisplay: |
| 261 | return asFP<HWC2_PFN_PRESENT_DISPLAY>( |
| 262 | displayHook<decltype(&Display::present), |
| 263 | &Display::present, int32_t*>); |
| 264 | case FunctionDescriptor::SetActiveConfig: |
| 265 | return asFP<HWC2_PFN_SET_ACTIVE_CONFIG>( |
| 266 | displayHook<decltype(&Display::setActiveConfig), |
| 267 | &Display::setActiveConfig, hwc2_config_t>); |
| 268 | case FunctionDescriptor::SetClientTarget: |
| 269 | return asFP<HWC2_PFN_SET_CLIENT_TARGET>( |
| 270 | displayHook<decltype(&Display::setClientTarget), |
| 271 | &Display::setClientTarget, buffer_handle_t, int32_t, |
Dan Stoza | 5cf424b | 2016-05-20 14:02:39 -0700 | [diff] [blame] | 272 | int32_t, hwc_region_t>); |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 273 | case FunctionDescriptor::SetColorMode: |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 274 | return asFP<HWC2_PFN_SET_COLOR_MODE>(setColorModeHook); |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame] | 275 | case FunctionDescriptor::SetColorTransform: |
| 276 | return asFP<HWC2_PFN_SET_COLOR_TRANSFORM>(setColorTransformHook); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 277 | case FunctionDescriptor::SetOutputBuffer: |
| 278 | return asFP<HWC2_PFN_SET_OUTPUT_BUFFER>( |
| 279 | displayHook<decltype(&Display::setOutputBuffer), |
| 280 | &Display::setOutputBuffer, buffer_handle_t, int32_t>); |
| 281 | case FunctionDescriptor::SetPowerMode: |
| 282 | return asFP<HWC2_PFN_SET_POWER_MODE>(setPowerModeHook); |
| 283 | case FunctionDescriptor::SetVsyncEnabled: |
| 284 | return asFP<HWC2_PFN_SET_VSYNC_ENABLED>(setVsyncEnabledHook); |
| 285 | case FunctionDescriptor::ValidateDisplay: |
| 286 | return asFP<HWC2_PFN_VALIDATE_DISPLAY>( |
| 287 | displayHook<decltype(&Display::validate), |
| 288 | &Display::validate, uint32_t*, uint32_t*>); |
| 289 | |
| 290 | // Layer functions |
| 291 | case FunctionDescriptor::SetCursorPosition: |
| 292 | return asFP<HWC2_PFN_SET_CURSOR_POSITION>( |
| 293 | layerHook<decltype(&Layer::setCursorPosition), |
| 294 | &Layer::setCursorPosition, int32_t, int32_t>); |
| 295 | case FunctionDescriptor::SetLayerBuffer: |
| 296 | return asFP<HWC2_PFN_SET_LAYER_BUFFER>( |
| 297 | layerHook<decltype(&Layer::setBuffer), &Layer::setBuffer, |
| 298 | buffer_handle_t, int32_t>); |
| 299 | case FunctionDescriptor::SetLayerSurfaceDamage: |
| 300 | return asFP<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>( |
| 301 | layerHook<decltype(&Layer::setSurfaceDamage), |
| 302 | &Layer::setSurfaceDamage, hwc_region_t>); |
| 303 | |
| 304 | // Layer state functions |
| 305 | case FunctionDescriptor::SetLayerBlendMode: |
| 306 | return asFP<HWC2_PFN_SET_LAYER_BLEND_MODE>( |
| 307 | setLayerBlendModeHook); |
| 308 | case FunctionDescriptor::SetLayerColor: |
| 309 | return asFP<HWC2_PFN_SET_LAYER_COLOR>( |
| 310 | layerHook<decltype(&Layer::setColor), &Layer::setColor, |
| 311 | hwc_color_t>); |
| 312 | case FunctionDescriptor::SetLayerCompositionType: |
| 313 | return asFP<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>( |
| 314 | setLayerCompositionTypeHook); |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame] | 315 | case FunctionDescriptor::SetLayerDataspace: |
| 316 | return asFP<HWC2_PFN_SET_LAYER_DATASPACE>(setLayerDataspaceHook); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 317 | case FunctionDescriptor::SetLayerDisplayFrame: |
| 318 | return asFP<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>( |
| 319 | layerHook<decltype(&Layer::setDisplayFrame), |
| 320 | &Layer::setDisplayFrame, hwc_rect_t>); |
| 321 | case FunctionDescriptor::SetLayerPlaneAlpha: |
| 322 | return asFP<HWC2_PFN_SET_LAYER_PLANE_ALPHA>( |
| 323 | layerHook<decltype(&Layer::setPlaneAlpha), |
| 324 | &Layer::setPlaneAlpha, float>); |
| 325 | case FunctionDescriptor::SetLayerSidebandStream: |
| 326 | return asFP<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>( |
| 327 | layerHook<decltype(&Layer::setSidebandStream), |
| 328 | &Layer::setSidebandStream, const native_handle_t*>); |
| 329 | case FunctionDescriptor::SetLayerSourceCrop: |
| 330 | return asFP<HWC2_PFN_SET_LAYER_SOURCE_CROP>( |
| 331 | layerHook<decltype(&Layer::setSourceCrop), |
| 332 | &Layer::setSourceCrop, hwc_frect_t>); |
| 333 | case FunctionDescriptor::SetLayerTransform: |
| 334 | return asFP<HWC2_PFN_SET_LAYER_TRANSFORM>(setLayerTransformHook); |
| 335 | case FunctionDescriptor::SetLayerVisibleRegion: |
| 336 | return asFP<HWC2_PFN_SET_LAYER_VISIBLE_REGION>( |
| 337 | layerHook<decltype(&Layer::setVisibleRegion), |
| 338 | &Layer::setVisibleRegion, hwc_region_t>); |
| 339 | case FunctionDescriptor::SetLayerZOrder: |
| 340 | return asFP<HWC2_PFN_SET_LAYER_Z_ORDER>(setLayerZOrderHook); |
| 341 | |
| 342 | default: |
| 343 | ALOGE("doGetFunction: Unknown function descriptor: %d (%s)", |
| 344 | static_cast<int32_t>(descriptor), |
| 345 | to_string(descriptor).c_str()); |
| 346 | return nullptr; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | // Device functions |
| 351 | |
| 352 | Error HWC2On1Adapter::createVirtualDisplay(uint32_t width, |
| 353 | uint32_t height, hwc2_display_t* outDisplay) |
| 354 | { |
Dan Stoza | fc4e202 | 2016-02-23 11:43:19 -0800 | [diff] [blame] | 355 | std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 356 | |
| 357 | if (mHwc1VirtualDisplay) { |
| 358 | // We have already allocated our only HWC1 virtual display |
| 359 | ALOGE("createVirtualDisplay: HWC1 virtual display already allocated"); |
| 360 | return Error::NoResources; |
| 361 | } |
| 362 | |
| 363 | if (MAX_VIRTUAL_DISPLAY_DIMENSION != 0 && |
| 364 | (width > MAX_VIRTUAL_DISPLAY_DIMENSION || |
| 365 | height > MAX_VIRTUAL_DISPLAY_DIMENSION)) { |
| 366 | ALOGE("createVirtualDisplay: Can't create a virtual display with" |
| 367 | " a dimension > %u (tried %u x %u)", |
| 368 | MAX_VIRTUAL_DISPLAY_DIMENSION, width, height); |
| 369 | return Error::NoResources; |
| 370 | } |
| 371 | |
| 372 | mHwc1VirtualDisplay = std::make_shared<HWC2On1Adapter::Display>(*this, |
| 373 | HWC2::DisplayType::Virtual); |
| 374 | mHwc1VirtualDisplay->populateConfigs(width, height); |
| 375 | const auto displayId = mHwc1VirtualDisplay->getId(); |
| 376 | mHwc1DisplayMap[HWC_DISPLAY_VIRTUAL] = displayId; |
| 377 | mHwc1VirtualDisplay->setHwc1Id(HWC_DISPLAY_VIRTUAL); |
| 378 | mDisplays.emplace(displayId, mHwc1VirtualDisplay); |
| 379 | *outDisplay = displayId; |
| 380 | |
| 381 | return Error::None; |
| 382 | } |
| 383 | |
| 384 | Error HWC2On1Adapter::destroyVirtualDisplay(hwc2_display_t displayId) |
| 385 | { |
Dan Stoza | fc4e202 | 2016-02-23 11:43:19 -0800 | [diff] [blame] | 386 | std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 387 | |
| 388 | if (!mHwc1VirtualDisplay || (mHwc1VirtualDisplay->getId() != displayId)) { |
| 389 | return Error::BadDisplay; |
| 390 | } |
| 391 | |
| 392 | mHwc1VirtualDisplay.reset(); |
| 393 | mHwc1DisplayMap.erase(HWC_DISPLAY_VIRTUAL); |
| 394 | mDisplays.erase(displayId); |
| 395 | |
| 396 | return Error::None; |
| 397 | } |
| 398 | |
| 399 | void HWC2On1Adapter::dump(uint32_t* outSize, char* outBuffer) |
| 400 | { |
| 401 | if (outBuffer != nullptr) { |
| 402 | auto copiedBytes = mDumpString.copy(outBuffer, *outSize); |
| 403 | *outSize = static_cast<uint32_t>(copiedBytes); |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | std::stringstream output; |
| 408 | |
| 409 | output << "-- HWC2On1Adapter --\n"; |
| 410 | |
| 411 | output << "Adapting to a HWC 1." << static_cast<int>(mHwc1MinorVersion) << |
| 412 | " device\n"; |
| 413 | |
| 414 | // Attempt to acquire the lock for 1 second, but proceed without the lock |
| 415 | // after that, so we can still get some information if we're deadlocked |
Dan Stoza | fc4e202 | 2016-02-23 11:43:19 -0800 | [diff] [blame] | 416 | std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex, |
| 417 | std::defer_lock); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 418 | lock.try_lock_for(1s); |
| 419 | |
| 420 | if (mCapabilities.empty()) { |
| 421 | output << "Capabilities: None\n"; |
| 422 | } else { |
| 423 | output << "Capabilities:\n"; |
| 424 | for (auto capability : mCapabilities) { |
| 425 | output << " " << to_string(capability) << '\n'; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | output << "Displays:\n"; |
| 430 | for (const auto& element : mDisplays) { |
| 431 | const auto& display = element.second; |
| 432 | output << display->dump(); |
| 433 | } |
| 434 | output << '\n'; |
| 435 | |
Dan Stoza | fc4e202 | 2016-02-23 11:43:19 -0800 | [diff] [blame] | 436 | // Release the lock before calling into HWC1, and since we no longer require |
| 437 | // mutual exclusion to access mCapabilities or mDisplays |
| 438 | lock.unlock(); |
| 439 | |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 440 | if (mHwc1Device->dump) { |
| 441 | output << "HWC1 dump:\n"; |
| 442 | std::vector<char> hwc1Dump(4096); |
| 443 | // Call with size - 1 to preserve a null character at the end |
| 444 | mHwc1Device->dump(mHwc1Device, hwc1Dump.data(), |
| 445 | static_cast<int>(hwc1Dump.size() - 1)); |
| 446 | output << hwc1Dump.data(); |
| 447 | } |
| 448 | |
| 449 | mDumpString = output.str(); |
| 450 | *outSize = static_cast<uint32_t>(mDumpString.size()); |
| 451 | } |
| 452 | |
| 453 | uint32_t HWC2On1Adapter::getMaxVirtualDisplayCount() |
| 454 | { |
| 455 | return mHwc1SupportsVirtualDisplays ? 1 : 0; |
| 456 | } |
| 457 | |
| 458 | static bool isValid(Callback descriptor) { |
| 459 | switch (descriptor) { |
| 460 | case Callback::Hotplug: // Fall-through |
| 461 | case Callback::Refresh: // Fall-through |
| 462 | case Callback::Vsync: return true; |
| 463 | default: return false; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | Error HWC2On1Adapter::registerCallback(Callback descriptor, |
| 468 | hwc2_callback_data_t callbackData, hwc2_function_pointer_t pointer) |
| 469 | { |
| 470 | if (!isValid(descriptor)) { |
| 471 | return Error::BadParameter; |
| 472 | } |
| 473 | |
| 474 | ALOGV("registerCallback(%s, %p, %p)", to_string(descriptor).c_str(), |
| 475 | callbackData, pointer); |
| 476 | |
Dan Stoza | fc4e202 | 2016-02-23 11:43:19 -0800 | [diff] [blame] | 477 | std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 478 | |
| 479 | mCallbacks[descriptor] = {callbackData, pointer}; |
| 480 | |
| 481 | bool hasPendingInvalidate = false; |
| 482 | std::vector<hwc2_display_t> displayIds; |
| 483 | std::vector<std::pair<hwc2_display_t, int64_t>> pendingVsyncs; |
| 484 | std::vector<std::pair<hwc2_display_t, int>> pendingHotplugs; |
| 485 | |
| 486 | if (descriptor == Callback::Refresh) { |
| 487 | hasPendingInvalidate = mHasPendingInvalidate; |
| 488 | if (hasPendingInvalidate) { |
| 489 | for (auto& displayPair : mDisplays) { |
| 490 | displayIds.emplace_back(displayPair.first); |
| 491 | } |
| 492 | } |
| 493 | mHasPendingInvalidate = false; |
| 494 | } else if (descriptor == Callback::Vsync) { |
| 495 | for (auto pending : mPendingVsyncs) { |
| 496 | auto hwc1DisplayId = pending.first; |
| 497 | if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) { |
| 498 | ALOGE("hwc1Vsync: Couldn't find display for HWC1 id %d", |
| 499 | hwc1DisplayId); |
| 500 | continue; |
| 501 | } |
| 502 | auto displayId = mHwc1DisplayMap[hwc1DisplayId]; |
| 503 | auto timestamp = pending.second; |
| 504 | pendingVsyncs.emplace_back(displayId, timestamp); |
| 505 | } |
| 506 | mPendingVsyncs.clear(); |
| 507 | } else if (descriptor == Callback::Hotplug) { |
| 508 | // Hotplug the primary display |
| 509 | pendingHotplugs.emplace_back(mHwc1DisplayMap[HWC_DISPLAY_PRIMARY], |
| 510 | static_cast<int32_t>(Connection::Connected)); |
| 511 | |
| 512 | for (auto pending : mPendingHotplugs) { |
| 513 | auto hwc1DisplayId = pending.first; |
| 514 | if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) { |
| 515 | ALOGE("hwc1Hotplug: Couldn't find display for HWC1 id %d", |
| 516 | hwc1DisplayId); |
| 517 | continue; |
| 518 | } |
| 519 | auto displayId = mHwc1DisplayMap[hwc1DisplayId]; |
| 520 | auto connected = pending.second; |
| 521 | pendingHotplugs.emplace_back(displayId, connected); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | // Call pending callbacks without the state lock held |
| 526 | lock.unlock(); |
| 527 | |
| 528 | if (hasPendingInvalidate) { |
| 529 | auto refresh = reinterpret_cast<HWC2_PFN_REFRESH>(pointer); |
| 530 | for (auto displayId : displayIds) { |
| 531 | refresh(callbackData, displayId); |
| 532 | } |
| 533 | } |
| 534 | if (!pendingVsyncs.empty()) { |
| 535 | auto vsync = reinterpret_cast<HWC2_PFN_VSYNC>(pointer); |
| 536 | for (auto& pendingVsync : pendingVsyncs) { |
| 537 | vsync(callbackData, pendingVsync.first, pendingVsync.second); |
| 538 | } |
| 539 | } |
| 540 | if (!pendingHotplugs.empty()) { |
| 541 | auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(pointer); |
| 542 | for (auto& pendingHotplug : pendingHotplugs) { |
| 543 | hotplug(callbackData, pendingHotplug.first, pendingHotplug.second); |
| 544 | } |
| 545 | } |
| 546 | return Error::None; |
| 547 | } |
| 548 | |
| 549 | // Display functions |
| 550 | |
| 551 | std::atomic<hwc2_display_t> HWC2On1Adapter::Display::sNextId(1); |
| 552 | |
| 553 | HWC2On1Adapter::Display::Display(HWC2On1Adapter& device, HWC2::DisplayType type) |
| 554 | : mId(sNextId++), |
| 555 | mDevice(device), |
| 556 | mDirtyCount(0), |
| 557 | mStateMutex(), |
| 558 | mZIsDirty(false), |
| 559 | mHwc1RequestedContents(nullptr), |
| 560 | mHwc1ReceivedContents(nullptr), |
| 561 | mRetireFence(), |
| 562 | mChanges(), |
| 563 | mHwc1Id(-1), |
| 564 | mConfigs(), |
| 565 | mActiveConfig(nullptr), |
Michael Wright | c75ca51 | 2016-07-20 21:34:48 +0100 | [diff] [blame] | 566 | mActiveColorMode(static_cast<android_color_mode_t>(-1)), |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 567 | mName(), |
| 568 | mType(type), |
| 569 | mPowerMode(PowerMode::Off), |
| 570 | mVsyncEnabled(Vsync::Invalid), |
| 571 | mClientTarget(), |
| 572 | mOutputBuffer(), |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame] | 573 | mHasColorTransform(false), |
Dan Stoza | fc4e202 | 2016-02-23 11:43:19 -0800 | [diff] [blame] | 574 | mLayers(), |
| 575 | mHwc1LayerMap() {} |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 576 | |
| 577 | Error HWC2On1Adapter::Display::acceptChanges() |
| 578 | { |
| 579 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 580 | |
| 581 | if (!mChanges) { |
| 582 | ALOGV("[%" PRIu64 "] acceptChanges failed, not validated", mId); |
| 583 | return Error::NotValidated; |
| 584 | } |
| 585 | |
| 586 | ALOGV("[%" PRIu64 "] acceptChanges", mId); |
| 587 | |
| 588 | for (auto& change : mChanges->getTypeChanges()) { |
| 589 | auto layerId = change.first; |
| 590 | auto type = change.second; |
| 591 | auto layer = mDevice.mLayers[layerId]; |
| 592 | layer->setCompositionType(type); |
| 593 | } |
| 594 | |
| 595 | mChanges->clearTypeChanges(); |
| 596 | |
| 597 | mHwc1RequestedContents = std::move(mHwc1ReceivedContents); |
| 598 | |
| 599 | return Error::None; |
| 600 | } |
| 601 | |
| 602 | Error HWC2On1Adapter::Display::createLayer(hwc2_layer_t* outLayerId) |
| 603 | { |
| 604 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 605 | |
| 606 | auto layer = *mLayers.emplace(std::make_shared<Layer>(*this)); |
| 607 | mDevice.mLayers.emplace(std::make_pair(layer->getId(), layer)); |
| 608 | *outLayerId = layer->getId(); |
| 609 | ALOGV("[%" PRIu64 "] created layer %" PRIu64, mId, *outLayerId); |
Fabien Sanglard | 03be681 | 2017-01-31 12:14:57 -0800 | [diff] [blame^] | 610 | mZIsDirty = true; |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 611 | return Error::None; |
| 612 | } |
| 613 | |
| 614 | Error HWC2On1Adapter::Display::destroyLayer(hwc2_layer_t layerId) |
| 615 | { |
| 616 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 617 | |
| 618 | const auto mapLayer = mDevice.mLayers.find(layerId); |
| 619 | if (mapLayer == mDevice.mLayers.end()) { |
| 620 | ALOGV("[%" PRIu64 "] destroyLayer(%" PRIu64 ") failed: no such layer", |
| 621 | mId, layerId); |
| 622 | return Error::BadLayer; |
| 623 | } |
| 624 | const auto layer = mapLayer->second; |
| 625 | mDevice.mLayers.erase(mapLayer); |
| 626 | const auto zRange = mLayers.equal_range(layer); |
| 627 | for (auto current = zRange.first; current != zRange.second; ++current) { |
| 628 | if (**current == *layer) { |
| 629 | current = mLayers.erase(current); |
| 630 | break; |
| 631 | } |
| 632 | } |
| 633 | ALOGV("[%" PRIu64 "] destroyed layer %" PRIu64, mId, layerId); |
Fabien Sanglard | 03be681 | 2017-01-31 12:14:57 -0800 | [diff] [blame^] | 634 | mZIsDirty = true; |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 635 | return Error::None; |
| 636 | } |
| 637 | |
| 638 | Error HWC2On1Adapter::Display::getActiveConfig(hwc2_config_t* outConfig) |
| 639 | { |
| 640 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 641 | |
| 642 | if (!mActiveConfig) { |
| 643 | ALOGV("[%" PRIu64 "] getActiveConfig --> %s", mId, |
| 644 | to_string(Error::BadConfig).c_str()); |
| 645 | return Error::BadConfig; |
| 646 | } |
| 647 | auto configId = mActiveConfig->getId(); |
| 648 | ALOGV("[%" PRIu64 "] getActiveConfig --> %u", mId, configId); |
| 649 | *outConfig = configId; |
| 650 | return Error::None; |
| 651 | } |
| 652 | |
| 653 | Error HWC2On1Adapter::Display::getAttribute(hwc2_config_t configId, |
| 654 | Attribute attribute, int32_t* outValue) |
| 655 | { |
| 656 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 657 | |
| 658 | if (configId > mConfigs.size() || !mConfigs[configId]->isOnDisplay(*this)) { |
| 659 | ALOGV("[%" PRIu64 "] getAttribute failed: bad config (%u)", mId, |
| 660 | configId); |
| 661 | return Error::BadConfig; |
| 662 | } |
| 663 | *outValue = mConfigs[configId]->getAttribute(attribute); |
| 664 | ALOGV("[%" PRIu64 "] getAttribute(%u, %s) --> %d", mId, configId, |
| 665 | to_string(attribute).c_str(), *outValue); |
| 666 | return Error::None; |
| 667 | } |
| 668 | |
| 669 | Error HWC2On1Adapter::Display::getChangedCompositionTypes( |
| 670 | uint32_t* outNumElements, hwc2_layer_t* outLayers, int32_t* outTypes) |
| 671 | { |
| 672 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 673 | |
| 674 | if (!mChanges) { |
| 675 | ALOGE("[%" PRIu64 "] getChangedCompositionTypes failed: not validated", |
| 676 | mId); |
| 677 | return Error::NotValidated; |
| 678 | } |
| 679 | |
| 680 | if ((outLayers == nullptr) || (outTypes == nullptr)) { |
| 681 | *outNumElements = mChanges->getTypeChanges().size(); |
| 682 | return Error::None; |
| 683 | } |
| 684 | |
| 685 | uint32_t numWritten = 0; |
| 686 | for (const auto& element : mChanges->getTypeChanges()) { |
| 687 | if (numWritten == *outNumElements) { |
| 688 | break; |
| 689 | } |
| 690 | auto layerId = element.first; |
| 691 | auto intType = static_cast<int32_t>(element.second); |
| 692 | ALOGV("Adding %" PRIu64 " %s", layerId, |
| 693 | to_string(element.second).c_str()); |
| 694 | outLayers[numWritten] = layerId; |
| 695 | outTypes[numWritten] = intType; |
| 696 | ++numWritten; |
| 697 | } |
| 698 | *outNumElements = numWritten; |
| 699 | |
| 700 | return Error::None; |
| 701 | } |
| 702 | |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 703 | Error HWC2On1Adapter::Display::getColorModes(uint32_t* outNumModes, |
| 704 | int32_t* outModes) |
| 705 | { |
| 706 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 707 | |
| 708 | if (!outModes) { |
| 709 | *outNumModes = mColorModes.size(); |
| 710 | return Error::None; |
| 711 | } |
| 712 | uint32_t numModes = std::min(*outNumModes, |
| 713 | static_cast<uint32_t>(mColorModes.size())); |
| 714 | std::copy_n(mColorModes.cbegin(), numModes, outModes); |
| 715 | *outNumModes = numModes; |
| 716 | return Error::None; |
| 717 | } |
| 718 | |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 719 | Error HWC2On1Adapter::Display::getConfigs(uint32_t* outNumConfigs, |
| 720 | hwc2_config_t* outConfigs) |
| 721 | { |
| 722 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 723 | |
| 724 | if (!outConfigs) { |
| 725 | *outNumConfigs = mConfigs.size(); |
| 726 | return Error::None; |
| 727 | } |
| 728 | uint32_t numWritten = 0; |
| 729 | for (const auto& config : mConfigs) { |
| 730 | if (numWritten == *outNumConfigs) { |
| 731 | break; |
| 732 | } |
| 733 | outConfigs[numWritten] = config->getId(); |
| 734 | ++numWritten; |
| 735 | } |
| 736 | *outNumConfigs = numWritten; |
| 737 | return Error::None; |
| 738 | } |
| 739 | |
| 740 | Error HWC2On1Adapter::Display::getDozeSupport(int32_t* outSupport) |
| 741 | { |
| 742 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 743 | |
| 744 | if (mDevice.mHwc1MinorVersion < 4 || mHwc1Id != 0) { |
| 745 | *outSupport = 0; |
| 746 | } else { |
| 747 | *outSupport = 1; |
| 748 | } |
| 749 | return Error::None; |
| 750 | } |
| 751 | |
Dan Stoza | ed40eba | 2016-03-16 12:33:52 -0700 | [diff] [blame] | 752 | Error HWC2On1Adapter::Display::getHdrCapabilities(uint32_t* outNumTypes, |
| 753 | int32_t* /*outTypes*/, float* /*outMaxLuminance*/, |
| 754 | float* /*outMaxAverageLuminance*/, float* /*outMinLuminance*/) |
| 755 | { |
| 756 | // This isn't supported on HWC1, so per the HWC2 header, return numTypes = 0 |
| 757 | *outNumTypes = 0; |
| 758 | return Error::None; |
| 759 | } |
| 760 | |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 761 | Error HWC2On1Adapter::Display::getName(uint32_t* outSize, char* outName) |
| 762 | { |
| 763 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 764 | |
| 765 | if (!outName) { |
| 766 | *outSize = mName.size(); |
| 767 | return Error::None; |
| 768 | } |
| 769 | auto numCopied = mName.copy(outName, *outSize); |
| 770 | *outSize = numCopied; |
| 771 | return Error::None; |
| 772 | } |
| 773 | |
| 774 | Error HWC2On1Adapter::Display::getReleaseFences(uint32_t* outNumElements, |
| 775 | hwc2_layer_t* outLayers, int32_t* outFences) |
| 776 | { |
| 777 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 778 | |
| 779 | uint32_t numWritten = 0; |
| 780 | bool outputsNonNull = (outLayers != nullptr) && (outFences != nullptr); |
| 781 | for (const auto& layer : mLayers) { |
| 782 | if (outputsNonNull && (numWritten == *outNumElements)) { |
| 783 | break; |
| 784 | } |
| 785 | |
| 786 | auto releaseFence = layer->getReleaseFence(); |
| 787 | if (releaseFence != Fence::NO_FENCE) { |
| 788 | if (outputsNonNull) { |
| 789 | outLayers[numWritten] = layer->getId(); |
| 790 | outFences[numWritten] = releaseFence->dup(); |
| 791 | } |
| 792 | ++numWritten; |
| 793 | } |
| 794 | } |
| 795 | *outNumElements = numWritten; |
| 796 | |
| 797 | return Error::None; |
| 798 | } |
| 799 | |
| 800 | Error HWC2On1Adapter::Display::getRequests(int32_t* outDisplayRequests, |
| 801 | uint32_t* outNumElements, hwc2_layer_t* outLayers, |
| 802 | int32_t* outLayerRequests) |
| 803 | { |
| 804 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 805 | |
| 806 | if (!mChanges) { |
| 807 | return Error::NotValidated; |
| 808 | } |
| 809 | |
| 810 | if (outLayers == nullptr || outLayerRequests == nullptr) { |
| 811 | *outNumElements = mChanges->getNumLayerRequests(); |
| 812 | return Error::None; |
| 813 | } |
| 814 | |
Fabien Sanglard | 601938c | 2016-11-29 11:10:40 -0800 | [diff] [blame] | 815 | // Display requests (HWC2::DisplayRequest) are not supported by hwc1: |
| 816 | // A hwc1 has always zero requests for the client. |
| 817 | *outDisplayRequests = 0; |
| 818 | |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 819 | uint32_t numWritten = 0; |
| 820 | for (const auto& request : mChanges->getLayerRequests()) { |
| 821 | if (numWritten == *outNumElements) { |
| 822 | break; |
| 823 | } |
| 824 | outLayers[numWritten] = request.first; |
| 825 | outLayerRequests[numWritten] = static_cast<int32_t>(request.second); |
| 826 | ++numWritten; |
| 827 | } |
| 828 | |
| 829 | return Error::None; |
| 830 | } |
| 831 | |
| 832 | Error HWC2On1Adapter::Display::getType(int32_t* outType) |
| 833 | { |
| 834 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 835 | |
| 836 | *outType = static_cast<int32_t>(mType); |
| 837 | return Error::None; |
| 838 | } |
| 839 | |
| 840 | Error HWC2On1Adapter::Display::present(int32_t* outRetireFence) |
| 841 | { |
| 842 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 843 | |
| 844 | if (mChanges) { |
| 845 | Error error = mDevice.setAllDisplays(); |
| 846 | if (error != Error::None) { |
| 847 | ALOGE("[%" PRIu64 "] present: setAllDisplaysFailed (%s)", mId, |
| 848 | to_string(error).c_str()); |
| 849 | return error; |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | *outRetireFence = mRetireFence.get()->dup(); |
| 854 | ALOGV("[%" PRIu64 "] present returning retire fence %d", mId, |
| 855 | *outRetireFence); |
| 856 | |
| 857 | return Error::None; |
| 858 | } |
| 859 | |
| 860 | Error HWC2On1Adapter::Display::setActiveConfig(hwc2_config_t configId) |
| 861 | { |
| 862 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 863 | |
| 864 | auto config = getConfig(configId); |
| 865 | if (!config) { |
| 866 | return Error::BadConfig; |
| 867 | } |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 868 | if (config == mActiveConfig) { |
| 869 | return Error::None; |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 870 | } |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 871 | |
| 872 | if (mDevice.mHwc1MinorVersion >= 4) { |
| 873 | uint32_t hwc1Id = 0; |
| 874 | auto error = config->getHwc1IdForColorMode(mActiveColorMode, &hwc1Id); |
| 875 | if (error != Error::None) { |
| 876 | return error; |
| 877 | } |
| 878 | |
| 879 | int intError = mDevice.mHwc1Device->setActiveConfig(mDevice.mHwc1Device, |
| 880 | mHwc1Id, static_cast<int>(hwc1Id)); |
| 881 | if (intError != 0) { |
| 882 | ALOGE("setActiveConfig: Failed to set active config on HWC1 (%d)", |
| 883 | intError); |
| 884 | return Error::BadConfig; |
| 885 | } |
| 886 | mActiveConfig = config; |
| 887 | } |
| 888 | |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 889 | return Error::None; |
| 890 | } |
| 891 | |
| 892 | Error HWC2On1Adapter::Display::setClientTarget(buffer_handle_t target, |
Dan Stoza | 5cf424b | 2016-05-20 14:02:39 -0700 | [diff] [blame] | 893 | int32_t acquireFence, int32_t /*dataspace*/, hwc_region_t /*damage*/) |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 894 | { |
| 895 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 896 | |
| 897 | ALOGV("[%" PRIu64 "] setClientTarget(%p, %d)", mId, target, acquireFence); |
| 898 | mClientTarget.setBuffer(target); |
| 899 | mClientTarget.setFence(acquireFence); |
Dan Stoza | 5cf424b | 2016-05-20 14:02:39 -0700 | [diff] [blame] | 900 | // dataspace and damage can't be used by HWC1, so ignore them |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 901 | return Error::None; |
| 902 | } |
| 903 | |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 904 | Error HWC2On1Adapter::Display::setColorMode(android_color_mode_t mode) |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 905 | { |
| 906 | std::unique_lock<std::recursive_mutex> lock (mStateMutex); |
| 907 | |
| 908 | ALOGV("[%" PRIu64 "] setColorMode(%d)", mId, mode); |
| 909 | |
| 910 | if (mode == mActiveColorMode) { |
| 911 | return Error::None; |
| 912 | } |
| 913 | if (mColorModes.count(mode) == 0) { |
| 914 | ALOGE("[%" PRIu64 "] Mode %d not found in mColorModes", mId, mode); |
| 915 | return Error::Unsupported; |
| 916 | } |
| 917 | |
| 918 | uint32_t hwc1Config = 0; |
| 919 | auto error = mActiveConfig->getHwc1IdForColorMode(mode, &hwc1Config); |
| 920 | if (error != Error::None) { |
| 921 | return error; |
| 922 | } |
| 923 | |
| 924 | ALOGV("[%" PRIu64 "] Setting HWC1 config %u", mId, hwc1Config); |
| 925 | int intError = mDevice.mHwc1Device->setActiveConfig(mDevice.mHwc1Device, |
| 926 | mHwc1Id, hwc1Config); |
| 927 | if (intError != 0) { |
| 928 | ALOGE("[%" PRIu64 "] Failed to set HWC1 config (%d)", mId, intError); |
| 929 | return Error::Unsupported; |
| 930 | } |
| 931 | |
| 932 | mActiveColorMode = mode; |
| 933 | return Error::None; |
| 934 | } |
| 935 | |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame] | 936 | Error HWC2On1Adapter::Display::setColorTransform(android_color_transform_t hint) |
| 937 | { |
| 938 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 939 | |
| 940 | ALOGV("%" PRIu64 "] setColorTransform(%d)", mId, |
| 941 | static_cast<int32_t>(hint)); |
| 942 | mHasColorTransform = (hint != HAL_COLOR_TRANSFORM_IDENTITY); |
| 943 | return Error::None; |
| 944 | } |
| 945 | |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 946 | Error HWC2On1Adapter::Display::setOutputBuffer(buffer_handle_t buffer, |
| 947 | int32_t releaseFence) |
| 948 | { |
| 949 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 950 | |
| 951 | ALOGV("[%" PRIu64 "] setOutputBuffer(%p, %d)", mId, buffer, releaseFence); |
| 952 | mOutputBuffer.setBuffer(buffer); |
| 953 | mOutputBuffer.setFence(releaseFence); |
| 954 | return Error::None; |
| 955 | } |
| 956 | |
| 957 | static bool isValid(PowerMode mode) |
| 958 | { |
| 959 | switch (mode) { |
| 960 | case PowerMode::Off: // Fall-through |
| 961 | case PowerMode::DozeSuspend: // Fall-through |
| 962 | case PowerMode::Doze: // Fall-through |
| 963 | case PowerMode::On: return true; |
| 964 | default: return false; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | static int getHwc1PowerMode(PowerMode mode) |
| 969 | { |
| 970 | switch (mode) { |
| 971 | case PowerMode::Off: return HWC_POWER_MODE_OFF; |
| 972 | case PowerMode::DozeSuspend: return HWC_POWER_MODE_DOZE_SUSPEND; |
| 973 | case PowerMode::Doze: return HWC_POWER_MODE_DOZE; |
| 974 | case PowerMode::On: return HWC_POWER_MODE_NORMAL; |
| 975 | default: return HWC_POWER_MODE_OFF; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | Error HWC2On1Adapter::Display::setPowerMode(PowerMode mode) |
| 980 | { |
| 981 | if (!isValid(mode)) { |
| 982 | return Error::BadParameter; |
| 983 | } |
| 984 | if (mode == mPowerMode) { |
| 985 | return Error::None; |
| 986 | } |
| 987 | |
| 988 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 989 | |
| 990 | int error = 0; |
| 991 | if (mDevice.mHwc1MinorVersion < 4) { |
| 992 | error = mDevice.mHwc1Device->blank(mDevice.mHwc1Device, mHwc1Id, |
| 993 | mode == PowerMode::Off); |
| 994 | } else { |
| 995 | error = mDevice.mHwc1Device->setPowerMode(mDevice.mHwc1Device, |
| 996 | mHwc1Id, getHwc1PowerMode(mode)); |
| 997 | } |
| 998 | ALOGE_IF(error != 0, "setPowerMode: Failed to set power mode on HWC1 (%d)", |
| 999 | error); |
| 1000 | |
| 1001 | ALOGV("[%" PRIu64 "] setPowerMode(%s)", mId, to_string(mode).c_str()); |
| 1002 | mPowerMode = mode; |
| 1003 | return Error::None; |
| 1004 | } |
| 1005 | |
| 1006 | static bool isValid(Vsync enable) { |
| 1007 | switch (enable) { |
| 1008 | case Vsync::Enable: // Fall-through |
| 1009 | case Vsync::Disable: return true; |
| 1010 | default: return false; |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | Error HWC2On1Adapter::Display::setVsyncEnabled(Vsync enable) |
| 1015 | { |
| 1016 | if (!isValid(enable)) { |
| 1017 | return Error::BadParameter; |
| 1018 | } |
| 1019 | if (enable == mVsyncEnabled) { |
| 1020 | return Error::None; |
| 1021 | } |
| 1022 | |
| 1023 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 1024 | |
| 1025 | int error = mDevice.mHwc1Device->eventControl(mDevice.mHwc1Device, |
| 1026 | mHwc1Id, HWC_EVENT_VSYNC, enable == Vsync::Enable); |
| 1027 | ALOGE_IF(error != 0, "setVsyncEnabled: Failed to set vsync on HWC1 (%d)", |
| 1028 | error); |
| 1029 | |
| 1030 | mVsyncEnabled = enable; |
| 1031 | return Error::None; |
| 1032 | } |
| 1033 | |
| 1034 | Error HWC2On1Adapter::Display::validate(uint32_t* outNumTypes, |
| 1035 | uint32_t* outNumRequests) |
| 1036 | { |
| 1037 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 1038 | |
| 1039 | ALOGV("[%" PRIu64 "] Entering validate", mId); |
| 1040 | |
| 1041 | if (!mChanges) { |
| 1042 | if (!mDevice.prepareAllDisplays()) { |
| 1043 | return Error::BadDisplay; |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | *outNumTypes = mChanges->getNumTypes(); |
| 1048 | *outNumRequests = mChanges->getNumLayerRequests(); |
| 1049 | ALOGV("[%" PRIu64 "] validate --> %u types, %u requests", mId, *outNumTypes, |
| 1050 | *outNumRequests); |
| 1051 | for (auto request : mChanges->getTypeChanges()) { |
| 1052 | ALOGV("Layer %" PRIu64 " --> %s", request.first, |
| 1053 | to_string(request.second).c_str()); |
| 1054 | } |
| 1055 | return *outNumTypes > 0 ? Error::HasChanges : Error::None; |
| 1056 | } |
| 1057 | |
| 1058 | // Display helpers |
| 1059 | |
| 1060 | Error HWC2On1Adapter::Display::updateLayerZ(hwc2_layer_t layerId, uint32_t z) |
| 1061 | { |
| 1062 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 1063 | |
| 1064 | const auto mapLayer = mDevice.mLayers.find(layerId); |
| 1065 | if (mapLayer == mDevice.mLayers.end()) { |
| 1066 | ALOGE("[%" PRIu64 "] updateLayerZ failed to find layer", mId); |
| 1067 | return Error::BadLayer; |
| 1068 | } |
| 1069 | |
| 1070 | const auto layer = mapLayer->second; |
| 1071 | const auto zRange = mLayers.equal_range(layer); |
| 1072 | bool layerOnDisplay = false; |
| 1073 | for (auto current = zRange.first; current != zRange.second; ++current) { |
| 1074 | if (**current == *layer) { |
| 1075 | if ((*current)->getZ() == z) { |
| 1076 | // Don't change anything if the Z hasn't changed |
| 1077 | return Error::None; |
| 1078 | } |
| 1079 | current = mLayers.erase(current); |
| 1080 | layerOnDisplay = true; |
| 1081 | break; |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | if (!layerOnDisplay) { |
| 1086 | ALOGE("[%" PRIu64 "] updateLayerZ failed to find layer on display", |
| 1087 | mId); |
| 1088 | return Error::BadLayer; |
| 1089 | } |
| 1090 | |
| 1091 | layer->setZ(z); |
| 1092 | mLayers.emplace(std::move(layer)); |
| 1093 | mZIsDirty = true; |
| 1094 | |
| 1095 | return Error::None; |
| 1096 | } |
| 1097 | |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1098 | static constexpr uint32_t ATTRIBUTES_WITH_COLOR[] = { |
| 1099 | HWC_DISPLAY_VSYNC_PERIOD, |
| 1100 | HWC_DISPLAY_WIDTH, |
| 1101 | HWC_DISPLAY_HEIGHT, |
| 1102 | HWC_DISPLAY_DPI_X, |
| 1103 | HWC_DISPLAY_DPI_Y, |
| 1104 | HWC_DISPLAY_COLOR_TRANSFORM, |
| 1105 | HWC_DISPLAY_NO_ATTRIBUTE, |
| 1106 | }; |
| 1107 | |
| 1108 | static constexpr uint32_t ATTRIBUTES_WITHOUT_COLOR[] = { |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1109 | HWC_DISPLAY_VSYNC_PERIOD, |
| 1110 | HWC_DISPLAY_WIDTH, |
| 1111 | HWC_DISPLAY_HEIGHT, |
| 1112 | HWC_DISPLAY_DPI_X, |
| 1113 | HWC_DISPLAY_DPI_Y, |
| 1114 | HWC_DISPLAY_NO_ATTRIBUTE, |
| 1115 | }; |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1116 | |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1117 | static constexpr size_t NUM_ATTRIBUTES_WITH_COLOR = |
| 1118 | sizeof(ATTRIBUTES_WITH_COLOR) / sizeof(uint32_t); |
| 1119 | static_assert(sizeof(ATTRIBUTES_WITH_COLOR) > sizeof(ATTRIBUTES_WITHOUT_COLOR), |
| 1120 | "Attribute tables have unexpected sizes"); |
| 1121 | |
| 1122 | static constexpr uint32_t ATTRIBUTE_MAP_WITH_COLOR[] = { |
| 1123 | 6, // HWC_DISPLAY_NO_ATTRIBUTE = 0 |
| 1124 | 0, // HWC_DISPLAY_VSYNC_PERIOD = 1, |
| 1125 | 1, // HWC_DISPLAY_WIDTH = 2, |
| 1126 | 2, // HWC_DISPLAY_HEIGHT = 3, |
| 1127 | 3, // HWC_DISPLAY_DPI_X = 4, |
| 1128 | 4, // HWC_DISPLAY_DPI_Y = 5, |
| 1129 | 5, // HWC_DISPLAY_COLOR_TRANSFORM = 6, |
| 1130 | }; |
| 1131 | |
| 1132 | static constexpr uint32_t ATTRIBUTE_MAP_WITHOUT_COLOR[] = { |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1133 | 5, // HWC_DISPLAY_NO_ATTRIBUTE = 0 |
| 1134 | 0, // HWC_DISPLAY_VSYNC_PERIOD = 1, |
| 1135 | 1, // HWC_DISPLAY_WIDTH = 2, |
| 1136 | 2, // HWC_DISPLAY_HEIGHT = 3, |
| 1137 | 3, // HWC_DISPLAY_DPI_X = 4, |
| 1138 | 4, // HWC_DISPLAY_DPI_Y = 5, |
| 1139 | }; |
| 1140 | |
| 1141 | template <uint32_t attribute> |
| 1142 | static constexpr bool attributesMatch() |
| 1143 | { |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1144 | bool match = (attribute == |
| 1145 | ATTRIBUTES_WITH_COLOR[ATTRIBUTE_MAP_WITH_COLOR[attribute]]); |
| 1146 | if (attribute == HWC_DISPLAY_COLOR_TRANSFORM) { |
| 1147 | return match; |
| 1148 | } |
| 1149 | |
| 1150 | return match && (attribute == |
| 1151 | ATTRIBUTES_WITHOUT_COLOR[ATTRIBUTE_MAP_WITHOUT_COLOR[attribute]]); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1152 | } |
| 1153 | static_assert(attributesMatch<HWC_DISPLAY_VSYNC_PERIOD>(), |
| 1154 | "Tables out of sync"); |
| 1155 | static_assert(attributesMatch<HWC_DISPLAY_WIDTH>(), "Tables out of sync"); |
| 1156 | static_assert(attributesMatch<HWC_DISPLAY_HEIGHT>(), "Tables out of sync"); |
| 1157 | static_assert(attributesMatch<HWC_DISPLAY_DPI_X>(), "Tables out of sync"); |
| 1158 | static_assert(attributesMatch<HWC_DISPLAY_DPI_Y>(), "Tables out of sync"); |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1159 | static_assert(attributesMatch<HWC_DISPLAY_COLOR_TRANSFORM>(), |
| 1160 | "Tables out of sync"); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1161 | |
| 1162 | void HWC2On1Adapter::Display::populateConfigs() |
| 1163 | { |
| 1164 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 1165 | |
| 1166 | ALOGV("[%" PRIu64 "] populateConfigs", mId); |
| 1167 | |
| 1168 | if (mHwc1Id == -1) { |
| 1169 | ALOGE("populateConfigs: HWC1 ID not set"); |
| 1170 | return; |
| 1171 | } |
| 1172 | |
| 1173 | const size_t MAX_NUM_CONFIGS = 128; |
| 1174 | uint32_t configs[MAX_NUM_CONFIGS] = {}; |
| 1175 | size_t numConfigs = MAX_NUM_CONFIGS; |
| 1176 | mDevice.mHwc1Device->getDisplayConfigs(mDevice.mHwc1Device, mHwc1Id, |
| 1177 | configs, &numConfigs); |
| 1178 | |
| 1179 | for (size_t c = 0; c < numConfigs; ++c) { |
| 1180 | uint32_t hwc1ConfigId = configs[c]; |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1181 | auto newConfig = std::make_shared<Config>(*this); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1182 | |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1183 | int32_t values[NUM_ATTRIBUTES_WITH_COLOR] = {}; |
| 1184 | bool hasColor = true; |
| 1185 | auto result = mDevice.mHwc1Device->getDisplayAttributes( |
| 1186 | mDevice.mHwc1Device, mHwc1Id, hwc1ConfigId, |
| 1187 | ATTRIBUTES_WITH_COLOR, values); |
| 1188 | if (result != 0) { |
| 1189 | mDevice.mHwc1Device->getDisplayAttributes(mDevice.mHwc1Device, |
| 1190 | mHwc1Id, hwc1ConfigId, ATTRIBUTES_WITHOUT_COLOR, values); |
| 1191 | hasColor = false; |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1192 | } |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1193 | |
| 1194 | auto attributeMap = hasColor ? |
| 1195 | ATTRIBUTE_MAP_WITH_COLOR : ATTRIBUTE_MAP_WITHOUT_COLOR; |
| 1196 | |
| 1197 | newConfig->setAttribute(Attribute::VsyncPeriod, |
| 1198 | values[attributeMap[HWC_DISPLAY_VSYNC_PERIOD]]); |
| 1199 | newConfig->setAttribute(Attribute::Width, |
| 1200 | values[attributeMap[HWC_DISPLAY_WIDTH]]); |
| 1201 | newConfig->setAttribute(Attribute::Height, |
| 1202 | values[attributeMap[HWC_DISPLAY_HEIGHT]]); |
| 1203 | newConfig->setAttribute(Attribute::DpiX, |
| 1204 | values[attributeMap[HWC_DISPLAY_DPI_X]]); |
| 1205 | newConfig->setAttribute(Attribute::DpiY, |
| 1206 | values[attributeMap[HWC_DISPLAY_DPI_Y]]); |
| 1207 | if (hasColor) { |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1208 | // In HWC1, color modes are referred to as color transforms. To avoid confusion with |
| 1209 | // the HWC2 concept of color transforms, we internally refer to them as color modes for |
| 1210 | // both HWC1 and 2. |
| 1211 | newConfig->setAttribute(ColorMode, |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1212 | values[attributeMap[HWC_DISPLAY_COLOR_TRANSFORM]]); |
| 1213 | } |
| 1214 | |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1215 | // We can only do this after attempting to read the color mode |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1216 | newConfig->setHwc1Id(hwc1ConfigId); |
| 1217 | |
| 1218 | for (auto& existingConfig : mConfigs) { |
| 1219 | if (existingConfig->merge(*newConfig)) { |
| 1220 | ALOGV("Merged config %d with existing config %u: %s", |
| 1221 | hwc1ConfigId, existingConfig->getId(), |
| 1222 | existingConfig->toString().c_str()); |
| 1223 | newConfig.reset(); |
| 1224 | break; |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | // If it wasn't merged with any existing config, add it to the end |
| 1229 | if (newConfig) { |
| 1230 | newConfig->setId(static_cast<hwc2_config_t>(mConfigs.size())); |
| 1231 | ALOGV("Found new config %u: %s", newConfig->getId(), |
| 1232 | newConfig->toString().c_str()); |
| 1233 | mConfigs.emplace_back(std::move(newConfig)); |
| 1234 | } |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1235 | } |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1236 | |
| 1237 | initializeActiveConfig(); |
| 1238 | populateColorModes(); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | void HWC2On1Adapter::Display::populateConfigs(uint32_t width, uint32_t height) |
| 1242 | { |
| 1243 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 1244 | |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1245 | mConfigs.emplace_back(std::make_shared<Config>(*this)); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1246 | auto& config = mConfigs[0]; |
| 1247 | |
| 1248 | config->setAttribute(Attribute::Width, static_cast<int32_t>(width)); |
| 1249 | config->setAttribute(Attribute::Height, static_cast<int32_t>(height)); |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1250 | config->setHwc1Id(0); |
| 1251 | config->setId(0); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1252 | mActiveConfig = config; |
| 1253 | } |
| 1254 | |
| 1255 | bool HWC2On1Adapter::Display::prepare() |
| 1256 | { |
| 1257 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 1258 | |
| 1259 | // Only prepare display contents for displays HWC1 knows about |
| 1260 | if (mHwc1Id == -1) { |
| 1261 | return true; |
| 1262 | } |
| 1263 | |
| 1264 | // It doesn't make sense to prepare a display for which there is no active |
| 1265 | // config, so return early |
| 1266 | if (!mActiveConfig) { |
| 1267 | ALOGE("[%" PRIu64 "] Attempted to prepare, but no config active", mId); |
| 1268 | return false; |
| 1269 | } |
| 1270 | |
| 1271 | ALOGV("[%" PRIu64 "] Entering prepare", mId); |
| 1272 | |
| 1273 | auto currentCount = mHwc1RequestedContents ? |
| 1274 | mHwc1RequestedContents->numHwLayers : 0; |
| 1275 | auto requiredCount = mLayers.size() + 1; |
| 1276 | ALOGV("[%" PRIu64 "] Requires %zd layers, %zd allocated in %p", mId, |
| 1277 | requiredCount, currentCount, mHwc1RequestedContents.get()); |
| 1278 | |
| 1279 | bool layerCountChanged = (currentCount != requiredCount); |
| 1280 | if (layerCountChanged) { |
| 1281 | reallocateHwc1Contents(); |
| 1282 | } |
| 1283 | |
| 1284 | bool applyAllState = false; |
| 1285 | if (layerCountChanged || mZIsDirty) { |
| 1286 | assignHwc1LayerIds(); |
| 1287 | mZIsDirty = false; |
| 1288 | applyAllState = true; |
| 1289 | } |
| 1290 | |
| 1291 | mHwc1RequestedContents->retireFenceFd = -1; |
| 1292 | mHwc1RequestedContents->flags = 0; |
| 1293 | if (isDirty() || applyAllState) { |
| 1294 | mHwc1RequestedContents->flags |= HWC_GEOMETRY_CHANGED; |
| 1295 | } |
| 1296 | |
| 1297 | for (auto& layer : mLayers) { |
| 1298 | auto& hwc1Layer = mHwc1RequestedContents->hwLayers[layer->getHwc1Id()]; |
| 1299 | hwc1Layer.releaseFenceFd = -1; |
| 1300 | layer->applyState(hwc1Layer, applyAllState); |
| 1301 | } |
| 1302 | |
| 1303 | mHwc1RequestedContents->outbuf = mOutputBuffer.getBuffer(); |
| 1304 | mHwc1RequestedContents->outbufAcquireFenceFd = mOutputBuffer.getFence(); |
| 1305 | |
| 1306 | prepareFramebufferTarget(); |
| 1307 | |
| 1308 | return true; |
| 1309 | } |
| 1310 | |
| 1311 | static void cloneHWCRegion(hwc_region_t& region) |
| 1312 | { |
| 1313 | auto size = sizeof(hwc_rect_t) * region.numRects; |
| 1314 | auto newRects = static_cast<hwc_rect_t*>(std::malloc(size)); |
| 1315 | std::copy_n(region.rects, region.numRects, newRects); |
| 1316 | region.rects = newRects; |
| 1317 | } |
| 1318 | |
| 1319 | HWC2On1Adapter::Display::HWC1Contents |
| 1320 | HWC2On1Adapter::Display::cloneRequestedContents() const |
| 1321 | { |
| 1322 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 1323 | |
| 1324 | size_t size = sizeof(hwc_display_contents_1_t) + |
| 1325 | sizeof(hwc_layer_1_t) * (mHwc1RequestedContents->numHwLayers); |
| 1326 | auto contents = static_cast<hwc_display_contents_1_t*>(std::malloc(size)); |
| 1327 | std::memcpy(contents, mHwc1RequestedContents.get(), size); |
| 1328 | for (size_t layerId = 0; layerId < contents->numHwLayers; ++layerId) { |
| 1329 | auto& layer = contents->hwLayers[layerId]; |
| 1330 | // Deep copy the regions to avoid double-frees |
| 1331 | cloneHWCRegion(layer.visibleRegionScreen); |
| 1332 | cloneHWCRegion(layer.surfaceDamage); |
| 1333 | } |
| 1334 | return HWC1Contents(contents); |
| 1335 | } |
| 1336 | |
| 1337 | void HWC2On1Adapter::Display::setReceivedContents(HWC1Contents contents) |
| 1338 | { |
| 1339 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 1340 | |
| 1341 | mHwc1ReceivedContents = std::move(contents); |
| 1342 | |
| 1343 | mChanges.reset(new Changes); |
| 1344 | |
| 1345 | size_t numLayers = mHwc1ReceivedContents->numHwLayers; |
| 1346 | for (size_t hwc1Id = 0; hwc1Id < numLayers; ++hwc1Id) { |
| 1347 | const auto& receivedLayer = mHwc1ReceivedContents->hwLayers[hwc1Id]; |
| 1348 | if (mHwc1LayerMap.count(hwc1Id) == 0) { |
| 1349 | ALOGE_IF(receivedLayer.compositionType != HWC_FRAMEBUFFER_TARGET, |
| 1350 | "setReceivedContents: HWC1 layer %zd doesn't have a" |
| 1351 | " matching HWC2 layer, and isn't the framebuffer target", |
| 1352 | hwc1Id); |
| 1353 | continue; |
| 1354 | } |
| 1355 | |
| 1356 | Layer& layer = *mHwc1LayerMap[hwc1Id]; |
| 1357 | updateTypeChanges(receivedLayer, layer); |
| 1358 | updateLayerRequests(receivedLayer, layer); |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | bool HWC2On1Adapter::Display::hasChanges() const |
| 1363 | { |
| 1364 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 1365 | return mChanges != nullptr; |
| 1366 | } |
| 1367 | |
| 1368 | Error HWC2On1Adapter::Display::set(hwc_display_contents_1& hwcContents) |
| 1369 | { |
| 1370 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 1371 | |
| 1372 | if (!mChanges || (mChanges->getNumTypes() > 0)) { |
| 1373 | ALOGE("[%" PRIu64 "] set failed: not validated", mId); |
| 1374 | return Error::NotValidated; |
| 1375 | } |
| 1376 | |
| 1377 | // Set up the client/framebuffer target |
| 1378 | auto numLayers = hwcContents.numHwLayers; |
| 1379 | |
| 1380 | // Close acquire fences on FRAMEBUFFER layers, since they will not be used |
| 1381 | // by HWC |
| 1382 | for (size_t l = 0; l < numLayers - 1; ++l) { |
| 1383 | auto& layer = hwcContents.hwLayers[l]; |
| 1384 | if (layer.compositionType == HWC_FRAMEBUFFER) { |
| 1385 | ALOGV("Closing fence %d for layer %zd", layer.acquireFenceFd, l); |
| 1386 | close(layer.acquireFenceFd); |
| 1387 | layer.acquireFenceFd = -1; |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | auto& clientTargetLayer = hwcContents.hwLayers[numLayers - 1]; |
| 1392 | if (clientTargetLayer.compositionType == HWC_FRAMEBUFFER_TARGET) { |
| 1393 | clientTargetLayer.handle = mClientTarget.getBuffer(); |
| 1394 | clientTargetLayer.acquireFenceFd = mClientTarget.getFence(); |
| 1395 | } else { |
| 1396 | ALOGE("[%" PRIu64 "] set: last HWC layer wasn't FRAMEBUFFER_TARGET", |
| 1397 | mId); |
| 1398 | } |
| 1399 | |
| 1400 | mChanges.reset(); |
| 1401 | |
| 1402 | return Error::None; |
| 1403 | } |
| 1404 | |
| 1405 | void HWC2On1Adapter::Display::addRetireFence(int fenceFd) |
| 1406 | { |
| 1407 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 1408 | mRetireFence.add(fenceFd); |
| 1409 | } |
| 1410 | |
| 1411 | void HWC2On1Adapter::Display::addReleaseFences( |
| 1412 | const hwc_display_contents_1_t& hwcContents) |
| 1413 | { |
| 1414 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 1415 | |
| 1416 | size_t numLayers = hwcContents.numHwLayers; |
| 1417 | for (size_t hwc1Id = 0; hwc1Id < numLayers; ++hwc1Id) { |
| 1418 | const auto& receivedLayer = hwcContents.hwLayers[hwc1Id]; |
| 1419 | if (mHwc1LayerMap.count(hwc1Id) == 0) { |
| 1420 | if (receivedLayer.compositionType != HWC_FRAMEBUFFER_TARGET) { |
| 1421 | ALOGE("addReleaseFences: HWC1 layer %zd doesn't have a" |
| 1422 | " matching HWC2 layer, and isn't the framebuffer" |
| 1423 | " target", hwc1Id); |
| 1424 | } |
| 1425 | // Close the framebuffer target release fence since we will use the |
| 1426 | // display retire fence instead |
| 1427 | if (receivedLayer.releaseFenceFd != -1) { |
| 1428 | close(receivedLayer.releaseFenceFd); |
| 1429 | } |
| 1430 | continue; |
| 1431 | } |
| 1432 | |
| 1433 | Layer& layer = *mHwc1LayerMap[hwc1Id]; |
| 1434 | ALOGV("Adding release fence %d to layer %" PRIu64, |
| 1435 | receivedLayer.releaseFenceFd, layer.getId()); |
| 1436 | layer.addReleaseFence(receivedLayer.releaseFenceFd); |
| 1437 | } |
| 1438 | } |
| 1439 | |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame] | 1440 | bool HWC2On1Adapter::Display::hasColorTransform() const |
| 1441 | { |
| 1442 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 1443 | return mHasColorTransform; |
| 1444 | } |
| 1445 | |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1446 | static std::string hwc1CompositionString(int32_t type) |
| 1447 | { |
| 1448 | switch (type) { |
| 1449 | case HWC_FRAMEBUFFER: return "Framebuffer"; |
| 1450 | case HWC_OVERLAY: return "Overlay"; |
| 1451 | case HWC_BACKGROUND: return "Background"; |
| 1452 | case HWC_FRAMEBUFFER_TARGET: return "FramebufferTarget"; |
| 1453 | case HWC_SIDEBAND: return "Sideband"; |
| 1454 | case HWC_CURSOR_OVERLAY: return "CursorOverlay"; |
| 1455 | default: |
| 1456 | return std::string("Unknown (") + std::to_string(type) + ")"; |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | static std::string hwc1TransformString(int32_t transform) |
| 1461 | { |
| 1462 | switch (transform) { |
| 1463 | case 0: return "None"; |
| 1464 | case HWC_TRANSFORM_FLIP_H: return "FlipH"; |
| 1465 | case HWC_TRANSFORM_FLIP_V: return "FlipV"; |
| 1466 | case HWC_TRANSFORM_ROT_90: return "Rotate90"; |
| 1467 | case HWC_TRANSFORM_ROT_180: return "Rotate180"; |
| 1468 | case HWC_TRANSFORM_ROT_270: return "Rotate270"; |
| 1469 | case HWC_TRANSFORM_FLIP_H_ROT_90: return "FlipHRotate90"; |
| 1470 | case HWC_TRANSFORM_FLIP_V_ROT_90: return "FlipVRotate90"; |
| 1471 | default: |
| 1472 | return std::string("Unknown (") + std::to_string(transform) + ")"; |
| 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | static std::string hwc1BlendModeString(int32_t mode) |
| 1477 | { |
| 1478 | switch (mode) { |
| 1479 | case HWC_BLENDING_NONE: return "None"; |
| 1480 | case HWC_BLENDING_PREMULT: return "Premultiplied"; |
| 1481 | case HWC_BLENDING_COVERAGE: return "Coverage"; |
| 1482 | default: |
| 1483 | return std::string("Unknown (") + std::to_string(mode) + ")"; |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | static std::string rectString(hwc_rect_t rect) |
| 1488 | { |
| 1489 | std::stringstream output; |
| 1490 | output << "[" << rect.left << ", " << rect.top << ", "; |
| 1491 | output << rect.right << ", " << rect.bottom << "]"; |
| 1492 | return output.str(); |
| 1493 | } |
| 1494 | |
| 1495 | static std::string approximateFloatString(float f) |
| 1496 | { |
| 1497 | if (static_cast<int32_t>(f) == f) { |
| 1498 | return std::to_string(static_cast<int32_t>(f)); |
| 1499 | } |
| 1500 | int32_t truncated = static_cast<int32_t>(f * 10); |
| 1501 | bool approximate = (static_cast<float>(truncated) != f * 10); |
| 1502 | const size_t BUFFER_SIZE = 32; |
| 1503 | char buffer[BUFFER_SIZE] = {}; |
| 1504 | auto bytesWritten = snprintf(buffer, BUFFER_SIZE, |
| 1505 | "%s%.1f", approximate ? "~" : "", f); |
| 1506 | return std::string(buffer, bytesWritten); |
| 1507 | } |
| 1508 | |
| 1509 | static std::string frectString(hwc_frect_t frect) |
| 1510 | { |
| 1511 | std::stringstream output; |
| 1512 | output << "[" << approximateFloatString(frect.left) << ", "; |
| 1513 | output << approximateFloatString(frect.top) << ", "; |
| 1514 | output << approximateFloatString(frect.right) << ", "; |
| 1515 | output << approximateFloatString(frect.bottom) << "]"; |
| 1516 | return output.str(); |
| 1517 | } |
| 1518 | |
| 1519 | static std::string colorString(hwc_color_t color) |
| 1520 | { |
| 1521 | std::stringstream output; |
| 1522 | output << "RGBA ["; |
| 1523 | output << static_cast<int32_t>(color.r) << ", "; |
| 1524 | output << static_cast<int32_t>(color.g) << ", "; |
| 1525 | output << static_cast<int32_t>(color.b) << ", "; |
| 1526 | output << static_cast<int32_t>(color.a) << "]"; |
| 1527 | return output.str(); |
| 1528 | } |
| 1529 | |
| 1530 | static std::string alphaString(float f) |
| 1531 | { |
| 1532 | const size_t BUFFER_SIZE = 8; |
| 1533 | char buffer[BUFFER_SIZE] = {}; |
| 1534 | auto bytesWritten = snprintf(buffer, BUFFER_SIZE, "%.3f", f); |
| 1535 | return std::string(buffer, bytesWritten); |
| 1536 | } |
| 1537 | |
| 1538 | static std::string to_string(const hwc_layer_1_t& hwcLayer, |
| 1539 | int32_t hwc1MinorVersion) |
| 1540 | { |
| 1541 | const char* fill = " "; |
| 1542 | |
| 1543 | std::stringstream output; |
| 1544 | |
| 1545 | output << " Composition: " << |
| 1546 | hwc1CompositionString(hwcLayer.compositionType); |
| 1547 | |
| 1548 | if (hwcLayer.compositionType == HWC_BACKGROUND) { |
| 1549 | output << " Color: " << colorString(hwcLayer.backgroundColor) << '\n'; |
| 1550 | } else if (hwcLayer.compositionType == HWC_SIDEBAND) { |
| 1551 | output << " Stream: " << hwcLayer.sidebandStream << '\n'; |
| 1552 | } else { |
| 1553 | output << " Buffer: " << hwcLayer.handle << "/" << |
| 1554 | hwcLayer.acquireFenceFd << '\n'; |
| 1555 | } |
| 1556 | |
| 1557 | output << fill << "Display frame: " << rectString(hwcLayer.displayFrame) << |
| 1558 | '\n'; |
| 1559 | |
| 1560 | output << fill << "Source crop: "; |
| 1561 | if (hwc1MinorVersion >= 3) { |
| 1562 | output << frectString(hwcLayer.sourceCropf) << '\n'; |
| 1563 | } else { |
| 1564 | output << rectString(hwcLayer.sourceCropi) << '\n'; |
| 1565 | } |
| 1566 | |
| 1567 | output << fill << "Transform: " << hwc1TransformString(hwcLayer.transform); |
| 1568 | output << " Blend mode: " << hwc1BlendModeString(hwcLayer.blending); |
| 1569 | if (hwcLayer.planeAlpha != 0xFF) { |
| 1570 | output << " Alpha: " << alphaString(hwcLayer.planeAlpha / 255.0f); |
| 1571 | } |
| 1572 | output << '\n'; |
| 1573 | |
| 1574 | if (hwcLayer.hints != 0) { |
| 1575 | output << fill << "Hints:"; |
| 1576 | if ((hwcLayer.hints & HWC_HINT_TRIPLE_BUFFER) != 0) { |
| 1577 | output << " TripleBuffer"; |
| 1578 | } |
| 1579 | if ((hwcLayer.hints & HWC_HINT_CLEAR_FB) != 0) { |
| 1580 | output << " ClearFB"; |
| 1581 | } |
| 1582 | output << '\n'; |
| 1583 | } |
| 1584 | |
| 1585 | if (hwcLayer.flags != 0) { |
| 1586 | output << fill << "Flags:"; |
| 1587 | if ((hwcLayer.flags & HWC_SKIP_LAYER) != 0) { |
| 1588 | output << " SkipLayer"; |
| 1589 | } |
| 1590 | if ((hwcLayer.flags & HWC_IS_CURSOR_LAYER) != 0) { |
| 1591 | output << " IsCursorLayer"; |
| 1592 | } |
| 1593 | output << '\n'; |
| 1594 | } |
| 1595 | |
| 1596 | return output.str(); |
| 1597 | } |
| 1598 | |
| 1599 | static std::string to_string(const hwc_display_contents_1_t& hwcContents, |
| 1600 | int32_t hwc1MinorVersion) |
| 1601 | { |
| 1602 | const char* fill = " "; |
| 1603 | |
| 1604 | std::stringstream output; |
| 1605 | output << fill << "Geometry changed: " << |
| 1606 | ((hwcContents.flags & HWC_GEOMETRY_CHANGED) != 0 ? "Y\n" : "N\n"); |
| 1607 | |
| 1608 | output << fill << hwcContents.numHwLayers << " Layer" << |
| 1609 | ((hwcContents.numHwLayers == 1) ? "\n" : "s\n"); |
| 1610 | for (size_t layer = 0; layer < hwcContents.numHwLayers; ++layer) { |
| 1611 | output << fill << " Layer " << layer; |
| 1612 | output << to_string(hwcContents.hwLayers[layer], hwc1MinorVersion); |
| 1613 | } |
| 1614 | |
| 1615 | if (hwcContents.outbuf != nullptr) { |
| 1616 | output << fill << "Output buffer: " << hwcContents.outbuf << "/" << |
| 1617 | hwcContents.outbufAcquireFenceFd << '\n'; |
| 1618 | } |
| 1619 | |
| 1620 | return output.str(); |
| 1621 | } |
| 1622 | |
| 1623 | std::string HWC2On1Adapter::Display::dump() const |
| 1624 | { |
| 1625 | std::unique_lock<std::recursive_mutex> lock(mStateMutex); |
| 1626 | |
| 1627 | std::stringstream output; |
| 1628 | |
| 1629 | output << " Display " << mId << ": "; |
| 1630 | output << to_string(mType) << " "; |
| 1631 | output << "HWC1 ID: " << mHwc1Id << " "; |
| 1632 | output << "Power mode: " << to_string(mPowerMode) << " "; |
| 1633 | output << "Vsync: " << to_string(mVsyncEnabled) << '\n'; |
| 1634 | |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1635 | output << " Color modes [active]:"; |
| 1636 | for (const auto& mode : mColorModes) { |
| 1637 | if (mode == mActiveColorMode) { |
| 1638 | output << " [" << mode << ']'; |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1639 | } else { |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1640 | output << " " << mode; |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1641 | } |
| 1642 | } |
| 1643 | output << '\n'; |
| 1644 | |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1645 | output << " " << mConfigs.size() << " Config" << |
| 1646 | (mConfigs.size() == 1 ? "" : "s") << " (* active)\n"; |
| 1647 | for (const auto& config : mConfigs) { |
| 1648 | output << (config == mActiveConfig ? " * " : " "); |
| 1649 | output << config->toString(true) << '\n'; |
| 1650 | } |
| 1651 | |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1652 | output << " " << mLayers.size() << " Layer" << |
| 1653 | (mLayers.size() == 1 ? "" : "s") << '\n'; |
| 1654 | for (const auto& layer : mLayers) { |
| 1655 | output << layer->dump(); |
| 1656 | } |
| 1657 | |
| 1658 | output << " Client target: " << mClientTarget.getBuffer() << '\n'; |
| 1659 | |
| 1660 | if (mOutputBuffer.getBuffer() != nullptr) { |
| 1661 | output << " Output buffer: " << mOutputBuffer.getBuffer() << '\n'; |
| 1662 | } |
| 1663 | |
| 1664 | if (mHwc1ReceivedContents) { |
| 1665 | output << " Last received HWC1 state\n"; |
| 1666 | output << to_string(*mHwc1ReceivedContents, mDevice.mHwc1MinorVersion); |
| 1667 | } else if (mHwc1RequestedContents) { |
| 1668 | output << " Last requested HWC1 state\n"; |
| 1669 | output << to_string(*mHwc1RequestedContents, mDevice.mHwc1MinorVersion); |
| 1670 | } |
| 1671 | |
| 1672 | return output.str(); |
| 1673 | } |
| 1674 | |
| 1675 | void HWC2On1Adapter::Display::Config::setAttribute(HWC2::Attribute attribute, |
| 1676 | int32_t value) |
| 1677 | { |
| 1678 | mAttributes[attribute] = value; |
| 1679 | } |
| 1680 | |
| 1681 | int32_t HWC2On1Adapter::Display::Config::getAttribute(Attribute attribute) const |
| 1682 | { |
| 1683 | if (mAttributes.count(attribute) == 0) { |
| 1684 | return -1; |
| 1685 | } |
| 1686 | return mAttributes.at(attribute); |
| 1687 | } |
| 1688 | |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1689 | void HWC2On1Adapter::Display::Config::setHwc1Id(uint32_t id) |
| 1690 | { |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1691 | android_color_mode_t colorMode = static_cast<android_color_mode_t>(getAttribute(ColorMode)); |
| 1692 | mHwc1Ids.emplace(colorMode, id); |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | bool HWC2On1Adapter::Display::Config::hasHwc1Id(uint32_t id) const |
| 1696 | { |
| 1697 | for (const auto& idPair : mHwc1Ids) { |
| 1698 | if (id == idPair.second) { |
| 1699 | return true; |
| 1700 | } |
| 1701 | } |
| 1702 | return false; |
| 1703 | } |
| 1704 | |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1705 | Error HWC2On1Adapter::Display::Config::getColorModeForHwc1Id( |
| 1706 | uint32_t id, android_color_mode_t* outMode) const |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1707 | { |
| 1708 | for (const auto& idPair : mHwc1Ids) { |
| 1709 | if (id == idPair.second) { |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1710 | *outMode = idPair.first; |
| 1711 | return Error::None; |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1712 | } |
| 1713 | } |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1714 | ALOGE("Unable to find color mode for HWC ID %" PRIu32 " on config %u", id, mId); |
| 1715 | return Error::BadParameter; |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1718 | Error HWC2On1Adapter::Display::Config::getHwc1IdForColorMode(android_color_mode_t mode, |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1719 | uint32_t* outId) const |
| 1720 | { |
| 1721 | for (const auto& idPair : mHwc1Ids) { |
| 1722 | if (mode == idPair.first) { |
| 1723 | *outId = idPair.second; |
| 1724 | return Error::None; |
| 1725 | } |
| 1726 | } |
| 1727 | ALOGE("Unable to find HWC1 ID for color mode %d on config %u", mode, mId); |
| 1728 | return Error::BadParameter; |
| 1729 | } |
| 1730 | |
| 1731 | bool HWC2On1Adapter::Display::Config::merge(const Config& other) |
| 1732 | { |
| 1733 | auto attributes = {HWC2::Attribute::Width, HWC2::Attribute::Height, |
| 1734 | HWC2::Attribute::VsyncPeriod, HWC2::Attribute::DpiX, |
| 1735 | HWC2::Attribute::DpiY}; |
| 1736 | for (auto attribute : attributes) { |
| 1737 | if (getAttribute(attribute) != other.getAttribute(attribute)) { |
| 1738 | return false; |
| 1739 | } |
| 1740 | } |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1741 | android_color_mode_t otherColorMode = |
| 1742 | static_cast<android_color_mode_t>(other.getAttribute(ColorMode)); |
| 1743 | if (mHwc1Ids.count(otherColorMode) != 0) { |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1744 | ALOGE("Attempted to merge two configs (%u and %u) which appear to be " |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1745 | "identical", mHwc1Ids.at(otherColorMode), |
| 1746 | other.mHwc1Ids.at(otherColorMode)); |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1747 | return false; |
| 1748 | } |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1749 | mHwc1Ids.emplace(otherColorMode, |
| 1750 | other.mHwc1Ids.at(otherColorMode)); |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1751 | return true; |
| 1752 | } |
| 1753 | |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1754 | std::set<android_color_mode_t> HWC2On1Adapter::Display::Config::getColorModes() const |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1755 | { |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1756 | std::set<android_color_mode_t> colorModes; |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1757 | for (const auto& idPair : mHwc1Ids) { |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1758 | colorModes.emplace(idPair.first); |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1759 | } |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1760 | return colorModes; |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | std::string HWC2On1Adapter::Display::Config::toString(bool splitLine) const |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1764 | { |
| 1765 | std::string output; |
| 1766 | |
| 1767 | const size_t BUFFER_SIZE = 100; |
| 1768 | char buffer[BUFFER_SIZE] = {}; |
| 1769 | auto writtenBytes = snprintf(buffer, BUFFER_SIZE, |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1770 | "%u x %u", mAttributes.at(HWC2::Attribute::Width), |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1771 | mAttributes.at(HWC2::Attribute::Height)); |
| 1772 | output.append(buffer, writtenBytes); |
| 1773 | |
| 1774 | if (mAttributes.count(HWC2::Attribute::VsyncPeriod) != 0) { |
| 1775 | std::memset(buffer, 0, BUFFER_SIZE); |
| 1776 | writtenBytes = snprintf(buffer, BUFFER_SIZE, " @ %.1f Hz", |
| 1777 | 1e9 / mAttributes.at(HWC2::Attribute::VsyncPeriod)); |
| 1778 | output.append(buffer, writtenBytes); |
| 1779 | } |
| 1780 | |
| 1781 | if (mAttributes.count(HWC2::Attribute::DpiX) != 0 && |
| 1782 | mAttributes.at(HWC2::Attribute::DpiX) != -1) { |
| 1783 | std::memset(buffer, 0, BUFFER_SIZE); |
| 1784 | writtenBytes = snprintf(buffer, BUFFER_SIZE, |
| 1785 | ", DPI: %.1f x %.1f", |
| 1786 | mAttributes.at(HWC2::Attribute::DpiX) / 1000.0f, |
| 1787 | mAttributes.at(HWC2::Attribute::DpiY) / 1000.0f); |
| 1788 | output.append(buffer, writtenBytes); |
| 1789 | } |
| 1790 | |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1791 | std::memset(buffer, 0, BUFFER_SIZE); |
| 1792 | if (splitLine) { |
| 1793 | writtenBytes = snprintf(buffer, BUFFER_SIZE, |
| 1794 | "\n HWC1 ID/Color transform:"); |
| 1795 | } else { |
| 1796 | writtenBytes = snprintf(buffer, BUFFER_SIZE, |
| 1797 | ", HWC1 ID/Color transform:"); |
| 1798 | } |
| 1799 | output.append(buffer, writtenBytes); |
| 1800 | |
| 1801 | |
| 1802 | for (const auto& id : mHwc1Ids) { |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1803 | android_color_mode_t colorMode = id.first; |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1804 | uint32_t hwc1Id = id.second; |
| 1805 | std::memset(buffer, 0, BUFFER_SIZE); |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1806 | if (colorMode == mDisplay.mActiveColorMode) { |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1807 | writtenBytes = snprintf(buffer, BUFFER_SIZE, " [%u/%d]", hwc1Id, |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1808 | colorMode); |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1809 | } else { |
| 1810 | writtenBytes = snprintf(buffer, BUFFER_SIZE, " %u/%d", hwc1Id, |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1811 | colorMode); |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1812 | } |
| 1813 | output.append(buffer, writtenBytes); |
| 1814 | } |
| 1815 | |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1816 | return output; |
| 1817 | } |
| 1818 | |
| 1819 | std::shared_ptr<const HWC2On1Adapter::Display::Config> |
| 1820 | HWC2On1Adapter::Display::getConfig(hwc2_config_t configId) const |
| 1821 | { |
| 1822 | if (configId > mConfigs.size() || !mConfigs[configId]->isOnDisplay(*this)) { |
| 1823 | return nullptr; |
| 1824 | } |
| 1825 | return mConfigs[configId]; |
| 1826 | } |
| 1827 | |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1828 | void HWC2On1Adapter::Display::populateColorModes() |
| 1829 | { |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1830 | mColorModes = mConfigs[0]->getColorModes(); |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1831 | for (const auto& config : mConfigs) { |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1832 | std::set<android_color_mode_t> intersection; |
| 1833 | auto configModes = config->getColorModes(); |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1834 | std::set_intersection(mColorModes.cbegin(), mColorModes.cend(), |
| 1835 | configModes.cbegin(), configModes.cend(), |
| 1836 | std::inserter(intersection, intersection.begin())); |
| 1837 | std::swap(intersection, mColorModes); |
| 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | void HWC2On1Adapter::Display::initializeActiveConfig() |
| 1842 | { |
| 1843 | if (mDevice.mHwc1Device->getActiveConfig == nullptr) { |
| 1844 | ALOGV("getActiveConfig is null, choosing config 0"); |
| 1845 | mActiveConfig = mConfigs[0]; |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1846 | mActiveColorMode = HAL_COLOR_MODE_NATIVE; |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1847 | return; |
| 1848 | } |
| 1849 | |
| 1850 | auto activeConfig = mDevice.mHwc1Device->getActiveConfig( |
| 1851 | mDevice.mHwc1Device, mHwc1Id); |
Fabien Sanglard | b7432cc | 2016-11-11 09:40:27 -0800 | [diff] [blame] | 1852 | |
| 1853 | // Some devices startup without an activeConfig: |
| 1854 | // We need to set one ourselves. |
| 1855 | if (activeConfig == HWC_ERROR) { |
| 1856 | ALOGV("There is no active configuration: Picking the first one: 0."); |
| 1857 | const int defaultIndex = 0; |
| 1858 | mDevice.mHwc1Device->setActiveConfig(mDevice.mHwc1Device, mHwc1Id, defaultIndex); |
| 1859 | activeConfig = defaultIndex; |
| 1860 | } |
| 1861 | |
| 1862 | for (const auto& config : mConfigs) { |
| 1863 | if (config->hasHwc1Id(activeConfig)) { |
| 1864 | ALOGE("Setting active config to %d for HWC1 config %u", config->getId(), activeConfig); |
| 1865 | mActiveConfig = config; |
| 1866 | if (config->getColorModeForHwc1Id(activeConfig, &mActiveColorMode) != Error::None) { |
| 1867 | // This should never happen since we checked for the config's presence before |
| 1868 | // setting it as active. |
| 1869 | ALOGE("Unable to find color mode for active HWC1 config %d", config->getId()); |
| 1870 | mActiveColorMode = HAL_COLOR_MODE_NATIVE; |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1871 | } |
Fabien Sanglard | b7432cc | 2016-11-11 09:40:27 -0800 | [diff] [blame] | 1872 | break; |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1873 | } |
| 1874 | } |
Fabien Sanglard | b7432cc | 2016-11-11 09:40:27 -0800 | [diff] [blame] | 1875 | if (!mActiveConfig) { |
| 1876 | ALOGV("Unable to find active HWC1 config %u, defaulting to " |
| 1877 | "config 0", activeConfig); |
| 1878 | mActiveConfig = mConfigs[0]; |
| 1879 | mActiveColorMode = HAL_COLOR_MODE_NATIVE; |
| 1880 | } |
| 1881 | |
| 1882 | |
| 1883 | |
| 1884 | |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 1885 | } |
| 1886 | |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1887 | void HWC2On1Adapter::Display::reallocateHwc1Contents() |
| 1888 | { |
| 1889 | // Allocate an additional layer for the framebuffer target |
| 1890 | auto numLayers = mLayers.size() + 1; |
| 1891 | size_t size = sizeof(hwc_display_contents_1_t) + |
| 1892 | sizeof(hwc_layer_1_t) * numLayers; |
| 1893 | ALOGV("[%" PRIu64 "] reallocateHwc1Contents creating %zd layer%s", mId, |
| 1894 | numLayers, numLayers != 1 ? "s" : ""); |
| 1895 | auto contents = |
| 1896 | static_cast<hwc_display_contents_1_t*>(std::calloc(size, 1)); |
| 1897 | contents->numHwLayers = numLayers; |
| 1898 | mHwc1RequestedContents.reset(contents); |
| 1899 | } |
| 1900 | |
| 1901 | void HWC2On1Adapter::Display::assignHwc1LayerIds() |
| 1902 | { |
| 1903 | mHwc1LayerMap.clear(); |
| 1904 | size_t nextHwc1Id = 0; |
| 1905 | for (auto& layer : mLayers) { |
| 1906 | mHwc1LayerMap[nextHwc1Id] = layer; |
| 1907 | layer->setHwc1Id(nextHwc1Id++); |
| 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | void HWC2On1Adapter::Display::updateTypeChanges(const hwc_layer_1_t& hwc1Layer, |
| 1912 | const Layer& layer) |
| 1913 | { |
| 1914 | auto layerId = layer.getId(); |
| 1915 | switch (hwc1Layer.compositionType) { |
| 1916 | case HWC_FRAMEBUFFER: |
| 1917 | if (layer.getCompositionType() != Composition::Client) { |
| 1918 | mChanges->addTypeChange(layerId, Composition::Client); |
| 1919 | } |
| 1920 | break; |
| 1921 | case HWC_OVERLAY: |
| 1922 | if (layer.getCompositionType() != Composition::Device) { |
| 1923 | mChanges->addTypeChange(layerId, Composition::Device); |
| 1924 | } |
| 1925 | break; |
| 1926 | case HWC_BACKGROUND: |
| 1927 | ALOGE_IF(layer.getCompositionType() != Composition::SolidColor, |
| 1928 | "updateTypeChanges: HWC1 requested BACKGROUND, but HWC2" |
| 1929 | " wasn't expecting SolidColor"); |
| 1930 | break; |
| 1931 | case HWC_FRAMEBUFFER_TARGET: |
| 1932 | // Do nothing, since it shouldn't be modified by HWC1 |
| 1933 | break; |
| 1934 | case HWC_SIDEBAND: |
| 1935 | ALOGE_IF(layer.getCompositionType() != Composition::Sideband, |
| 1936 | "updateTypeChanges: HWC1 requested SIDEBAND, but HWC2" |
| 1937 | " wasn't expecting Sideband"); |
| 1938 | break; |
| 1939 | case HWC_CURSOR_OVERLAY: |
| 1940 | ALOGE_IF(layer.getCompositionType() != Composition::Cursor, |
| 1941 | "updateTypeChanges: HWC1 requested CURSOR_OVERLAY, but" |
| 1942 | " HWC2 wasn't expecting Cursor"); |
| 1943 | break; |
| 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | void HWC2On1Adapter::Display::updateLayerRequests( |
| 1948 | const hwc_layer_1_t& hwc1Layer, const Layer& layer) |
| 1949 | { |
| 1950 | if ((hwc1Layer.hints & HWC_HINT_CLEAR_FB) != 0) { |
| 1951 | mChanges->addLayerRequest(layer.getId(), |
| 1952 | LayerRequest::ClearClientTarget); |
| 1953 | } |
| 1954 | } |
| 1955 | |
| 1956 | void HWC2On1Adapter::Display::prepareFramebufferTarget() |
| 1957 | { |
| 1958 | // We check that mActiveConfig is valid in Display::prepare |
| 1959 | int32_t width = mActiveConfig->getAttribute(Attribute::Width); |
| 1960 | int32_t height = mActiveConfig->getAttribute(Attribute::Height); |
| 1961 | |
| 1962 | auto& hwc1Target = mHwc1RequestedContents->hwLayers[mLayers.size()]; |
| 1963 | hwc1Target.compositionType = HWC_FRAMEBUFFER_TARGET; |
| 1964 | hwc1Target.releaseFenceFd = -1; |
| 1965 | hwc1Target.hints = 0; |
| 1966 | hwc1Target.flags = 0; |
| 1967 | hwc1Target.transform = 0; |
| 1968 | hwc1Target.blending = HWC_BLENDING_PREMULT; |
| 1969 | if (mDevice.getHwc1MinorVersion() < 3) { |
| 1970 | hwc1Target.sourceCropi = {0, 0, width, height}; |
| 1971 | } else { |
| 1972 | hwc1Target.sourceCropf = {0.0f, 0.0f, static_cast<float>(width), |
| 1973 | static_cast<float>(height)}; |
| 1974 | } |
| 1975 | hwc1Target.displayFrame = {0, 0, width, height}; |
| 1976 | hwc1Target.planeAlpha = 255; |
| 1977 | hwc1Target.visibleRegionScreen.numRects = 1; |
| 1978 | auto rects = static_cast<hwc_rect_t*>(std::malloc(sizeof(hwc_rect_t))); |
| 1979 | rects[0].left = 0; |
| 1980 | rects[0].top = 0; |
| 1981 | rects[0].right = width; |
| 1982 | rects[0].bottom = height; |
| 1983 | hwc1Target.visibleRegionScreen.rects = rects; |
| 1984 | |
| 1985 | // We will set this to the correct value in set |
| 1986 | hwc1Target.acquireFenceFd = -1; |
| 1987 | } |
| 1988 | |
| 1989 | // Layer functions |
| 1990 | |
| 1991 | std::atomic<hwc2_layer_t> HWC2On1Adapter::Layer::sNextId(1); |
| 1992 | |
| 1993 | HWC2On1Adapter::Layer::Layer(Display& display) |
| 1994 | : mId(sNextId++), |
| 1995 | mDisplay(display), |
Dan Stoza | fc4e202 | 2016-02-23 11:43:19 -0800 | [diff] [blame] | 1996 | mDirtyCount(0), |
| 1997 | mBuffer(), |
| 1998 | mSurfaceDamage(), |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 1999 | mBlendMode(*this, BlendMode::None), |
| 2000 | mColor(*this, {0, 0, 0, 0}), |
| 2001 | mCompositionType(*this, Composition::Invalid), |
| 2002 | mDisplayFrame(*this, {0, 0, -1, -1}), |
| 2003 | mPlaneAlpha(*this, 0.0f), |
| 2004 | mSidebandStream(*this, nullptr), |
| 2005 | mSourceCrop(*this, {0.0f, 0.0f, -1.0f, -1.0f}), |
| 2006 | mTransform(*this, Transform::None), |
| 2007 | mVisibleRegion(*this, std::vector<hwc_rect_t>()), |
| 2008 | mZ(0), |
Dan Stoza | fc4e202 | 2016-02-23 11:43:19 -0800 | [diff] [blame] | 2009 | mReleaseFence(), |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 2010 | mHwc1Id(0), |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame] | 2011 | mHasUnsupportedDataspace(false), |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 2012 | mHasUnsupportedPlaneAlpha(false) {} |
| 2013 | |
| 2014 | bool HWC2On1Adapter::SortLayersByZ::operator()( |
| 2015 | const std::shared_ptr<Layer>& lhs, const std::shared_ptr<Layer>& rhs) |
| 2016 | { |
| 2017 | return lhs->getZ() < rhs->getZ(); |
| 2018 | } |
| 2019 | |
| 2020 | Error HWC2On1Adapter::Layer::setBuffer(buffer_handle_t buffer, |
| 2021 | int32_t acquireFence) |
| 2022 | { |
| 2023 | ALOGV("Setting acquireFence to %d for layer %" PRIu64, acquireFence, mId); |
| 2024 | mBuffer.setBuffer(buffer); |
| 2025 | mBuffer.setFence(acquireFence); |
| 2026 | return Error::None; |
| 2027 | } |
| 2028 | |
| 2029 | Error HWC2On1Adapter::Layer::setCursorPosition(int32_t x, int32_t y) |
| 2030 | { |
| 2031 | if (mCompositionType.getValue() != Composition::Cursor) { |
| 2032 | return Error::BadLayer; |
| 2033 | } |
| 2034 | |
| 2035 | if (mDisplay.hasChanges()) { |
| 2036 | return Error::NotValidated; |
| 2037 | } |
| 2038 | |
| 2039 | auto displayId = mDisplay.getHwc1Id(); |
| 2040 | auto hwc1Device = mDisplay.getDevice().getHwc1Device(); |
| 2041 | hwc1Device->setCursorPositionAsync(hwc1Device, displayId, x, y); |
| 2042 | return Error::None; |
| 2043 | } |
| 2044 | |
| 2045 | Error HWC2On1Adapter::Layer::setSurfaceDamage(hwc_region_t damage) |
| 2046 | { |
| 2047 | mSurfaceDamage.resize(damage.numRects); |
| 2048 | std::copy_n(damage.rects, damage.numRects, mSurfaceDamage.begin()); |
| 2049 | return Error::None; |
| 2050 | } |
| 2051 | |
| 2052 | // Layer state functions |
| 2053 | |
| 2054 | Error HWC2On1Adapter::Layer::setBlendMode(BlendMode mode) |
| 2055 | { |
| 2056 | mBlendMode.setPending(mode); |
| 2057 | return Error::None; |
| 2058 | } |
| 2059 | |
| 2060 | Error HWC2On1Adapter::Layer::setColor(hwc_color_t color) |
| 2061 | { |
| 2062 | mColor.setPending(color); |
| 2063 | return Error::None; |
| 2064 | } |
| 2065 | |
| 2066 | Error HWC2On1Adapter::Layer::setCompositionType(Composition type) |
| 2067 | { |
| 2068 | mCompositionType.setPending(type); |
| 2069 | return Error::None; |
| 2070 | } |
| 2071 | |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame] | 2072 | Error HWC2On1Adapter::Layer::setDataspace(android_dataspace_t dataspace) |
| 2073 | { |
| 2074 | mHasUnsupportedDataspace = (dataspace != HAL_DATASPACE_UNKNOWN); |
| 2075 | return Error::None; |
| 2076 | } |
| 2077 | |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 2078 | Error HWC2On1Adapter::Layer::setDisplayFrame(hwc_rect_t frame) |
| 2079 | { |
| 2080 | mDisplayFrame.setPending(frame); |
| 2081 | return Error::None; |
| 2082 | } |
| 2083 | |
| 2084 | Error HWC2On1Adapter::Layer::setPlaneAlpha(float alpha) |
| 2085 | { |
| 2086 | mPlaneAlpha.setPending(alpha); |
| 2087 | return Error::None; |
| 2088 | } |
| 2089 | |
| 2090 | Error HWC2On1Adapter::Layer::setSidebandStream(const native_handle_t* stream) |
| 2091 | { |
| 2092 | mSidebandStream.setPending(stream); |
| 2093 | return Error::None; |
| 2094 | } |
| 2095 | |
| 2096 | Error HWC2On1Adapter::Layer::setSourceCrop(hwc_frect_t crop) |
| 2097 | { |
| 2098 | mSourceCrop.setPending(crop); |
| 2099 | return Error::None; |
| 2100 | } |
| 2101 | |
| 2102 | Error HWC2On1Adapter::Layer::setTransform(Transform transform) |
| 2103 | { |
| 2104 | mTransform.setPending(transform); |
| 2105 | return Error::None; |
| 2106 | } |
| 2107 | |
| 2108 | Error HWC2On1Adapter::Layer::setVisibleRegion(hwc_region_t rawVisible) |
| 2109 | { |
| 2110 | std::vector<hwc_rect_t> visible(rawVisible.rects, |
| 2111 | rawVisible.rects + rawVisible.numRects); |
| 2112 | mVisibleRegion.setPending(std::move(visible)); |
| 2113 | return Error::None; |
| 2114 | } |
| 2115 | |
| 2116 | Error HWC2On1Adapter::Layer::setZ(uint32_t z) |
| 2117 | { |
| 2118 | mZ = z; |
| 2119 | return Error::None; |
| 2120 | } |
| 2121 | |
| 2122 | void HWC2On1Adapter::Layer::addReleaseFence(int fenceFd) |
| 2123 | { |
| 2124 | ALOGV("addReleaseFence %d to layer %" PRIu64, fenceFd, mId); |
| 2125 | mReleaseFence.add(fenceFd); |
| 2126 | } |
| 2127 | |
| 2128 | const sp<Fence>& HWC2On1Adapter::Layer::getReleaseFence() const |
| 2129 | { |
| 2130 | return mReleaseFence.get(); |
| 2131 | } |
| 2132 | |
| 2133 | void HWC2On1Adapter::Layer::applyState(hwc_layer_1_t& hwc1Layer, |
| 2134 | bool applyAllState) |
| 2135 | { |
| 2136 | applyCommonState(hwc1Layer, applyAllState); |
| 2137 | auto compositionType = mCompositionType.getPendingValue(); |
| 2138 | if (compositionType == Composition::SolidColor) { |
| 2139 | applySolidColorState(hwc1Layer, applyAllState); |
| 2140 | } else if (compositionType == Composition::Sideband) { |
| 2141 | applySidebandState(hwc1Layer, applyAllState); |
| 2142 | } else { |
| 2143 | applyBufferState(hwc1Layer); |
| 2144 | } |
| 2145 | applyCompositionType(hwc1Layer, applyAllState); |
| 2146 | } |
| 2147 | |
| 2148 | // Layer dump helpers |
| 2149 | |
| 2150 | static std::string regionStrings(const std::vector<hwc_rect_t>& visibleRegion, |
| 2151 | const std::vector<hwc_rect_t>& surfaceDamage) |
| 2152 | { |
| 2153 | std::string regions; |
| 2154 | regions += " Visible Region"; |
| 2155 | regions.resize(40, ' '); |
| 2156 | regions += "Surface Damage\n"; |
| 2157 | |
| 2158 | size_t numPrinted = 0; |
| 2159 | size_t maxSize = std::max(visibleRegion.size(), surfaceDamage.size()); |
| 2160 | while (numPrinted < maxSize) { |
| 2161 | std::string line(" "); |
| 2162 | if (visibleRegion.empty() && numPrinted == 0) { |
| 2163 | line += "None"; |
| 2164 | } else if (numPrinted < visibleRegion.size()) { |
| 2165 | line += rectString(visibleRegion[numPrinted]); |
| 2166 | } |
| 2167 | line.resize(40, ' '); |
| 2168 | if (surfaceDamage.empty() && numPrinted == 0) { |
| 2169 | line += "None"; |
| 2170 | } else if (numPrinted < surfaceDamage.size()) { |
| 2171 | line += rectString(surfaceDamage[numPrinted]); |
| 2172 | } |
| 2173 | line += '\n'; |
| 2174 | regions += line; |
| 2175 | ++numPrinted; |
| 2176 | } |
| 2177 | return regions; |
| 2178 | } |
| 2179 | |
| 2180 | std::string HWC2On1Adapter::Layer::dump() const |
| 2181 | { |
| 2182 | std::stringstream output; |
| 2183 | const char* fill = " "; |
| 2184 | |
| 2185 | output << fill << to_string(mCompositionType.getPendingValue()); |
| 2186 | output << " Layer HWC2/1: " << mId << "/" << mHwc1Id << " "; |
| 2187 | output << "Z: " << mZ; |
| 2188 | if (mCompositionType.getValue() == HWC2::Composition::SolidColor) { |
| 2189 | output << " " << colorString(mColor.getValue()); |
| 2190 | } else if (mCompositionType.getValue() == HWC2::Composition::Sideband) { |
| 2191 | output << " Handle: " << mSidebandStream.getValue() << '\n'; |
| 2192 | } else { |
| 2193 | output << " Buffer: " << mBuffer.getBuffer() << "/" << |
| 2194 | mBuffer.getFence() << '\n'; |
| 2195 | output << fill << " Display frame [LTRB]: " << |
| 2196 | rectString(mDisplayFrame.getValue()) << '\n'; |
| 2197 | output << fill << " Source crop: " << |
| 2198 | frectString(mSourceCrop.getValue()) << '\n'; |
| 2199 | output << fill << " Transform: " << to_string(mTransform.getValue()); |
| 2200 | output << " Blend mode: " << to_string(mBlendMode.getValue()); |
| 2201 | if (mPlaneAlpha.getValue() != 1.0f) { |
| 2202 | output << " Alpha: " << |
| 2203 | alphaString(mPlaneAlpha.getValue()) << '\n'; |
| 2204 | } else { |
| 2205 | output << '\n'; |
| 2206 | } |
| 2207 | output << regionStrings(mVisibleRegion.getValue(), mSurfaceDamage); |
| 2208 | } |
| 2209 | return output.str(); |
| 2210 | } |
| 2211 | |
| 2212 | static int getHwc1Blending(HWC2::BlendMode blendMode) |
| 2213 | { |
| 2214 | switch (blendMode) { |
| 2215 | case BlendMode::Coverage: return HWC_BLENDING_COVERAGE; |
| 2216 | case BlendMode::Premultiplied: return HWC_BLENDING_PREMULT; |
| 2217 | default: return HWC_BLENDING_NONE; |
| 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | void HWC2On1Adapter::Layer::applyCommonState(hwc_layer_1_t& hwc1Layer, |
| 2222 | bool applyAllState) |
| 2223 | { |
| 2224 | auto minorVersion = mDisplay.getDevice().getHwc1MinorVersion(); |
| 2225 | if (applyAllState || mBlendMode.isDirty()) { |
| 2226 | hwc1Layer.blending = getHwc1Blending(mBlendMode.getPendingValue()); |
| 2227 | mBlendMode.latch(); |
| 2228 | } |
| 2229 | if (applyAllState || mDisplayFrame.isDirty()) { |
| 2230 | hwc1Layer.displayFrame = mDisplayFrame.getPendingValue(); |
| 2231 | mDisplayFrame.latch(); |
| 2232 | } |
| 2233 | if (applyAllState || mPlaneAlpha.isDirty()) { |
| 2234 | auto pendingAlpha = mPlaneAlpha.getPendingValue(); |
| 2235 | if (minorVersion < 2) { |
| 2236 | mHasUnsupportedPlaneAlpha = pendingAlpha < 1.0f; |
| 2237 | } else { |
| 2238 | hwc1Layer.planeAlpha = |
| 2239 | static_cast<uint8_t>(255.0f * pendingAlpha + 0.5f); |
| 2240 | } |
| 2241 | mPlaneAlpha.latch(); |
| 2242 | } |
| 2243 | if (applyAllState || mSourceCrop.isDirty()) { |
| 2244 | if (minorVersion < 3) { |
| 2245 | auto pending = mSourceCrop.getPendingValue(); |
| 2246 | hwc1Layer.sourceCropi.left = |
| 2247 | static_cast<int32_t>(std::ceil(pending.left)); |
| 2248 | hwc1Layer.sourceCropi.top = |
| 2249 | static_cast<int32_t>(std::ceil(pending.top)); |
| 2250 | hwc1Layer.sourceCropi.right = |
| 2251 | static_cast<int32_t>(std::floor(pending.right)); |
| 2252 | hwc1Layer.sourceCropi.bottom = |
| 2253 | static_cast<int32_t>(std::floor(pending.bottom)); |
| 2254 | } else { |
| 2255 | hwc1Layer.sourceCropf = mSourceCrop.getPendingValue(); |
| 2256 | } |
| 2257 | mSourceCrop.latch(); |
| 2258 | } |
| 2259 | if (applyAllState || mTransform.isDirty()) { |
| 2260 | hwc1Layer.transform = |
| 2261 | static_cast<uint32_t>(mTransform.getPendingValue()); |
| 2262 | mTransform.latch(); |
| 2263 | } |
| 2264 | if (applyAllState || mVisibleRegion.isDirty()) { |
| 2265 | auto& hwc1VisibleRegion = hwc1Layer.visibleRegionScreen; |
| 2266 | |
| 2267 | std::free(const_cast<hwc_rect_t*>(hwc1VisibleRegion.rects)); |
| 2268 | |
| 2269 | auto pending = mVisibleRegion.getPendingValue(); |
| 2270 | hwc_rect_t* newRects = static_cast<hwc_rect_t*>( |
| 2271 | std::malloc(sizeof(hwc_rect_t) * pending.size())); |
| 2272 | std::copy(pending.begin(), pending.end(), newRects); |
| 2273 | hwc1VisibleRegion.rects = const_cast<const hwc_rect_t*>(newRects); |
| 2274 | hwc1VisibleRegion.numRects = pending.size(); |
| 2275 | mVisibleRegion.latch(); |
| 2276 | } |
| 2277 | } |
| 2278 | |
| 2279 | void HWC2On1Adapter::Layer::applySolidColorState(hwc_layer_1_t& hwc1Layer, |
| 2280 | bool applyAllState) |
| 2281 | { |
| 2282 | if (applyAllState || mColor.isDirty()) { |
Fabien Sanglard | eb3db61 | 2016-11-18 16:12:31 -0800 | [diff] [blame] | 2283 | // If the device does not support background color it is likely to make |
| 2284 | // assumption regarding backgroundColor and handle (both fields occupy |
| 2285 | // the same location in hwc_layer_1_t union). |
| 2286 | // To not confuse these devices we don't set background color and we |
| 2287 | // make sure handle is a null pointer. |
| 2288 | if (mDisplay.getDevice().supportsBackgroundColor()) { |
| 2289 | hwc1Layer.backgroundColor = mColor.getPendingValue(); |
| 2290 | mHasUnsupportedBackgroundColor = false; |
| 2291 | } else { |
| 2292 | hwc1Layer.handle = nullptr; |
| 2293 | mHasUnsupportedBackgroundColor = true; |
| 2294 | } |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 2295 | mColor.latch(); |
| 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | void HWC2On1Adapter::Layer::applySidebandState(hwc_layer_1_t& hwc1Layer, |
| 2300 | bool applyAllState) |
| 2301 | { |
| 2302 | if (applyAllState || mSidebandStream.isDirty()) { |
| 2303 | hwc1Layer.sidebandStream = mSidebandStream.getPendingValue(); |
| 2304 | mSidebandStream.latch(); |
| 2305 | } |
| 2306 | } |
| 2307 | |
| 2308 | void HWC2On1Adapter::Layer::applyBufferState(hwc_layer_1_t& hwc1Layer) |
| 2309 | { |
| 2310 | hwc1Layer.handle = mBuffer.getBuffer(); |
| 2311 | hwc1Layer.acquireFenceFd = mBuffer.getFence(); |
| 2312 | } |
| 2313 | |
| 2314 | void HWC2On1Adapter::Layer::applyCompositionType(hwc_layer_1_t& hwc1Layer, |
| 2315 | bool applyAllState) |
| 2316 | { |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame] | 2317 | // HWC1 never supports color transforms or dataspaces and only sometimes |
| 2318 | // supports plane alpha (depending on the version). These require us to drop |
| 2319 | // some or all layers to client composition. |
| 2320 | if (mHasUnsupportedDataspace || mHasUnsupportedPlaneAlpha || |
Fabien Sanglard | eb3db61 | 2016-11-18 16:12:31 -0800 | [diff] [blame] | 2321 | mDisplay.hasColorTransform() || mHasUnsupportedBackgroundColor) { |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 2322 | hwc1Layer.compositionType = HWC_FRAMEBUFFER; |
| 2323 | hwc1Layer.flags = HWC_SKIP_LAYER; |
| 2324 | return; |
| 2325 | } |
| 2326 | |
| 2327 | if (applyAllState || mCompositionType.isDirty()) { |
| 2328 | hwc1Layer.flags = 0; |
| 2329 | switch (mCompositionType.getPendingValue()) { |
| 2330 | case Composition::Client: |
| 2331 | hwc1Layer.compositionType = HWC_FRAMEBUFFER; |
| 2332 | hwc1Layer.flags |= HWC_SKIP_LAYER; |
| 2333 | break; |
| 2334 | case Composition::Device: |
| 2335 | hwc1Layer.compositionType = HWC_FRAMEBUFFER; |
| 2336 | break; |
| 2337 | case Composition::SolidColor: |
Dan Stoza | 5df47cb | 2016-09-15 16:38:42 -0700 | [diff] [blame] | 2338 | // In theory the following line should work, but since the HWC1 |
| 2339 | // version of SurfaceFlinger never used HWC_BACKGROUND, HWC1 |
| 2340 | // devices may not work correctly. To be on the safe side, we |
| 2341 | // fall back to client composition. |
| 2342 | // |
| 2343 | // hwc1Layer.compositionType = HWC_BACKGROUND; |
| 2344 | hwc1Layer.compositionType = HWC_FRAMEBUFFER; |
| 2345 | hwc1Layer.flags |= HWC_SKIP_LAYER; |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 2346 | break; |
| 2347 | case Composition::Cursor: |
| 2348 | hwc1Layer.compositionType = HWC_FRAMEBUFFER; |
| 2349 | if (mDisplay.getDevice().getHwc1MinorVersion() >= 4) { |
| 2350 | hwc1Layer.hints |= HWC_IS_CURSOR_LAYER; |
| 2351 | } |
| 2352 | break; |
| 2353 | case Composition::Sideband: |
| 2354 | if (mDisplay.getDevice().getHwc1MinorVersion() < 4) { |
| 2355 | hwc1Layer.compositionType = HWC_SIDEBAND; |
| 2356 | } else { |
| 2357 | hwc1Layer.compositionType = HWC_FRAMEBUFFER; |
| 2358 | hwc1Layer.flags |= HWC_SKIP_LAYER; |
| 2359 | } |
| 2360 | break; |
| 2361 | default: |
| 2362 | hwc1Layer.compositionType = HWC_FRAMEBUFFER; |
| 2363 | hwc1Layer.flags |= HWC_SKIP_LAYER; |
| 2364 | break; |
| 2365 | } |
| 2366 | ALOGV("Layer %" PRIu64 " %s set to %d", mId, |
| 2367 | to_string(mCompositionType.getPendingValue()).c_str(), |
| 2368 | hwc1Layer.compositionType); |
| 2369 | ALOGV_IF(hwc1Layer.flags & HWC_SKIP_LAYER, " and skipping"); |
| 2370 | mCompositionType.latch(); |
| 2371 | } |
| 2372 | } |
| 2373 | |
| 2374 | // Adapter helpers |
| 2375 | |
| 2376 | void HWC2On1Adapter::populateCapabilities() |
| 2377 | { |
| 2378 | ALOGV("populateCapabilities"); |
| 2379 | if (mHwc1MinorVersion >= 3U) { |
| 2380 | int supportedTypes = 0; |
| 2381 | auto result = mHwc1Device->query(mHwc1Device, |
| 2382 | HWC_DISPLAY_TYPES_SUPPORTED, &supportedTypes); |
Fred Fettinger | c50c01e | 2016-06-14 17:53:10 -0500 | [diff] [blame] | 2383 | if ((result == 0) && ((supportedTypes & HWC_DISPLAY_VIRTUAL_BIT) != 0)) { |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 2384 | ALOGI("Found support for HWC virtual displays"); |
| 2385 | mHwc1SupportsVirtualDisplays = true; |
| 2386 | } |
| 2387 | } |
| 2388 | if (mHwc1MinorVersion >= 4U) { |
| 2389 | mCapabilities.insert(Capability::SidebandStream); |
| 2390 | } |
Fabien Sanglard | eb3db61 | 2016-11-18 16:12:31 -0800 | [diff] [blame] | 2391 | |
| 2392 | // Check for HWC background color layer support. |
| 2393 | if (mHwc1MinorVersion >= 1U) { |
| 2394 | int backgroundColorSupported = 0; |
| 2395 | auto result = mHwc1Device->query(mHwc1Device, |
| 2396 | HWC_BACKGROUND_LAYER_SUPPORTED, |
| 2397 | &backgroundColorSupported); |
| 2398 | if ((result == 0) && (backgroundColorSupported == 1)) { |
| 2399 | ALOGV("Found support for HWC background color"); |
| 2400 | mHwc1SupportsBackgroundColor = true; |
| 2401 | } |
| 2402 | } |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 2403 | } |
| 2404 | |
| 2405 | HWC2On1Adapter::Display* HWC2On1Adapter::getDisplay(hwc2_display_t id) |
| 2406 | { |
Dan Stoza | fc4e202 | 2016-02-23 11:43:19 -0800 | [diff] [blame] | 2407 | std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 2408 | |
| 2409 | auto display = mDisplays.find(id); |
| 2410 | if (display == mDisplays.end()) { |
| 2411 | return nullptr; |
| 2412 | } |
| 2413 | |
| 2414 | return display->second.get(); |
| 2415 | } |
| 2416 | |
| 2417 | std::tuple<HWC2On1Adapter::Layer*, Error> HWC2On1Adapter::getLayer( |
| 2418 | hwc2_display_t displayId, hwc2_layer_t layerId) |
| 2419 | { |
| 2420 | auto display = getDisplay(displayId); |
| 2421 | if (!display) { |
| 2422 | return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadDisplay); |
| 2423 | } |
| 2424 | |
| 2425 | auto layerEntry = mLayers.find(layerId); |
| 2426 | if (layerEntry == mLayers.end()) { |
| 2427 | return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadLayer); |
| 2428 | } |
| 2429 | |
| 2430 | auto layer = layerEntry->second; |
| 2431 | if (layer->getDisplay().getId() != displayId) { |
| 2432 | return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadLayer); |
| 2433 | } |
| 2434 | return std::make_tuple(layer.get(), Error::None); |
| 2435 | } |
| 2436 | |
| 2437 | void HWC2On1Adapter::populatePrimary() |
| 2438 | { |
| 2439 | ALOGV("populatePrimary"); |
| 2440 | |
Dan Stoza | fc4e202 | 2016-02-23 11:43:19 -0800 | [diff] [blame] | 2441 | std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 2442 | |
| 2443 | auto display = |
| 2444 | std::make_shared<Display>(*this, HWC2::DisplayType::Physical); |
| 2445 | mHwc1DisplayMap[HWC_DISPLAY_PRIMARY] = display->getId(); |
| 2446 | display->setHwc1Id(HWC_DISPLAY_PRIMARY); |
| 2447 | display->populateConfigs(); |
| 2448 | mDisplays.emplace(display->getId(), std::move(display)); |
| 2449 | } |
| 2450 | |
| 2451 | bool HWC2On1Adapter::prepareAllDisplays() |
| 2452 | { |
| 2453 | ATRACE_CALL(); |
| 2454 | |
Dan Stoza | fc4e202 | 2016-02-23 11:43:19 -0800 | [diff] [blame] | 2455 | std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 2456 | |
| 2457 | for (const auto& displayPair : mDisplays) { |
| 2458 | auto& display = displayPair.second; |
| 2459 | if (!display->prepare()) { |
| 2460 | return false; |
| 2461 | } |
| 2462 | } |
| 2463 | |
| 2464 | if (mHwc1DisplayMap.count(0) == 0) { |
| 2465 | ALOGE("prepareAllDisplays: Unable to find primary HWC1 display"); |
| 2466 | return false; |
| 2467 | } |
| 2468 | |
| 2469 | // Always push the primary display |
| 2470 | std::vector<HWC2On1Adapter::Display::HWC1Contents> requestedContents; |
| 2471 | auto primaryDisplayId = mHwc1DisplayMap[HWC_DISPLAY_PRIMARY]; |
| 2472 | auto& primaryDisplay = mDisplays[primaryDisplayId]; |
| 2473 | auto primaryDisplayContents = primaryDisplay->cloneRequestedContents(); |
| 2474 | requestedContents.push_back(std::move(primaryDisplayContents)); |
| 2475 | |
| 2476 | // Push the external display, if present |
| 2477 | if (mHwc1DisplayMap.count(HWC_DISPLAY_EXTERNAL) != 0) { |
| 2478 | auto externalDisplayId = mHwc1DisplayMap[HWC_DISPLAY_EXTERNAL]; |
| 2479 | auto& externalDisplay = mDisplays[externalDisplayId]; |
| 2480 | auto externalDisplayContents = |
| 2481 | externalDisplay->cloneRequestedContents(); |
| 2482 | requestedContents.push_back(std::move(externalDisplayContents)); |
| 2483 | } else { |
| 2484 | // Even if an external display isn't present, we still need to send |
| 2485 | // at least two displays down to HWC1 |
| 2486 | requestedContents.push_back(nullptr); |
| 2487 | } |
| 2488 | |
| 2489 | // Push the hardware virtual display, if supported and present |
| 2490 | if (mHwc1MinorVersion >= 3) { |
| 2491 | if (mHwc1DisplayMap.count(HWC_DISPLAY_VIRTUAL) != 0) { |
| 2492 | auto virtualDisplayId = mHwc1DisplayMap[HWC_DISPLAY_VIRTUAL]; |
| 2493 | auto& virtualDisplay = mDisplays[virtualDisplayId]; |
| 2494 | auto virtualDisplayContents = |
| 2495 | virtualDisplay->cloneRequestedContents(); |
| 2496 | requestedContents.push_back(std::move(virtualDisplayContents)); |
| 2497 | } else { |
| 2498 | requestedContents.push_back(nullptr); |
| 2499 | } |
| 2500 | } |
| 2501 | |
| 2502 | mHwc1Contents.clear(); |
| 2503 | for (auto& displayContents : requestedContents) { |
| 2504 | mHwc1Contents.push_back(displayContents.get()); |
| 2505 | if (!displayContents) { |
| 2506 | continue; |
| 2507 | } |
| 2508 | |
| 2509 | ALOGV("Display %zd layers:", mHwc1Contents.size() - 1); |
| 2510 | for (size_t l = 0; l < displayContents->numHwLayers; ++l) { |
| 2511 | auto& layer = displayContents->hwLayers[l]; |
| 2512 | ALOGV(" %zd: %d", l, layer.compositionType); |
| 2513 | } |
| 2514 | } |
| 2515 | |
| 2516 | ALOGV("Calling HWC1 prepare"); |
| 2517 | { |
| 2518 | ATRACE_NAME("HWC1 prepare"); |
| 2519 | mHwc1Device->prepare(mHwc1Device, mHwc1Contents.size(), |
| 2520 | mHwc1Contents.data()); |
| 2521 | } |
| 2522 | |
| 2523 | for (size_t c = 0; c < mHwc1Contents.size(); ++c) { |
| 2524 | auto& contents = mHwc1Contents[c]; |
| 2525 | if (!contents) { |
| 2526 | continue; |
| 2527 | } |
| 2528 | ALOGV("Display %zd layers:", c); |
| 2529 | for (size_t l = 0; l < contents->numHwLayers; ++l) { |
| 2530 | ALOGV(" %zd: %d", l, contents->hwLayers[l].compositionType); |
| 2531 | } |
| 2532 | } |
| 2533 | |
| 2534 | // Return the received contents to their respective displays |
| 2535 | for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) { |
| 2536 | if (mHwc1Contents[hwc1Id] == nullptr) { |
| 2537 | continue; |
| 2538 | } |
| 2539 | |
| 2540 | auto displayId = mHwc1DisplayMap[hwc1Id]; |
| 2541 | auto& display = mDisplays[displayId]; |
| 2542 | display->setReceivedContents(std::move(requestedContents[hwc1Id])); |
| 2543 | } |
| 2544 | |
| 2545 | return true; |
| 2546 | } |
| 2547 | |
| 2548 | Error HWC2On1Adapter::setAllDisplays() |
| 2549 | { |
| 2550 | ATRACE_CALL(); |
| 2551 | |
Dan Stoza | fc4e202 | 2016-02-23 11:43:19 -0800 | [diff] [blame] | 2552 | std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 2553 | |
| 2554 | // Make sure we're ready to validate |
| 2555 | for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) { |
| 2556 | if (mHwc1Contents[hwc1Id] == nullptr) { |
| 2557 | continue; |
| 2558 | } |
| 2559 | |
| 2560 | auto displayId = mHwc1DisplayMap[hwc1Id]; |
| 2561 | auto& display = mDisplays[displayId]; |
| 2562 | Error error = display->set(*mHwc1Contents[hwc1Id]); |
| 2563 | if (error != Error::None) { |
| 2564 | ALOGE("setAllDisplays: Failed to set display %zd: %s", hwc1Id, |
| 2565 | to_string(error).c_str()); |
| 2566 | return error; |
| 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | ALOGV("Calling HWC1 set"); |
| 2571 | { |
| 2572 | ATRACE_NAME("HWC1 set"); |
| 2573 | mHwc1Device->set(mHwc1Device, mHwc1Contents.size(), |
| 2574 | mHwc1Contents.data()); |
| 2575 | } |
| 2576 | |
| 2577 | // Add retire and release fences |
| 2578 | for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) { |
| 2579 | if (mHwc1Contents[hwc1Id] == nullptr) { |
| 2580 | continue; |
| 2581 | } |
| 2582 | |
| 2583 | auto displayId = mHwc1DisplayMap[hwc1Id]; |
| 2584 | auto& display = mDisplays[displayId]; |
| 2585 | auto retireFenceFd = mHwc1Contents[hwc1Id]->retireFenceFd; |
| 2586 | ALOGV("setAllDisplays: Adding retire fence %d to display %zd", |
| 2587 | retireFenceFd, hwc1Id); |
| 2588 | display->addRetireFence(mHwc1Contents[hwc1Id]->retireFenceFd); |
| 2589 | display->addReleaseFences(*mHwc1Contents[hwc1Id]); |
| 2590 | } |
| 2591 | |
| 2592 | return Error::None; |
| 2593 | } |
| 2594 | |
| 2595 | void HWC2On1Adapter::hwc1Invalidate() |
| 2596 | { |
| 2597 | ALOGV("Received hwc1Invalidate"); |
| 2598 | |
Dan Stoza | fc4e202 | 2016-02-23 11:43:19 -0800 | [diff] [blame] | 2599 | std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 2600 | |
| 2601 | // If the HWC2-side callback hasn't been registered yet, buffer this until |
| 2602 | // it is registered |
| 2603 | if (mCallbacks.count(Callback::Refresh) == 0) { |
| 2604 | mHasPendingInvalidate = true; |
| 2605 | return; |
| 2606 | } |
| 2607 | |
| 2608 | const auto& callbackInfo = mCallbacks[Callback::Refresh]; |
| 2609 | std::vector<hwc2_display_t> displays; |
| 2610 | for (const auto& displayPair : mDisplays) { |
| 2611 | displays.emplace_back(displayPair.first); |
| 2612 | } |
| 2613 | |
| 2614 | // Call back without the state lock held |
| 2615 | lock.unlock(); |
| 2616 | |
| 2617 | auto refresh = reinterpret_cast<HWC2_PFN_REFRESH>(callbackInfo.pointer); |
| 2618 | for (auto display : displays) { |
| 2619 | refresh(callbackInfo.data, display); |
| 2620 | } |
| 2621 | } |
| 2622 | |
| 2623 | void HWC2On1Adapter::hwc1Vsync(int hwc1DisplayId, int64_t timestamp) |
| 2624 | { |
| 2625 | ALOGV("Received hwc1Vsync(%d, %" PRId64 ")", hwc1DisplayId, timestamp); |
| 2626 | |
Dan Stoza | fc4e202 | 2016-02-23 11:43:19 -0800 | [diff] [blame] | 2627 | std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 2628 | |
| 2629 | // If the HWC2-side callback hasn't been registered yet, buffer this until |
| 2630 | // it is registered |
| 2631 | if (mCallbacks.count(Callback::Vsync) == 0) { |
| 2632 | mPendingVsyncs.emplace_back(hwc1DisplayId, timestamp); |
| 2633 | return; |
| 2634 | } |
| 2635 | |
| 2636 | if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) { |
| 2637 | ALOGE("hwc1Vsync: Couldn't find display for HWC1 id %d", hwc1DisplayId); |
| 2638 | return; |
| 2639 | } |
| 2640 | |
| 2641 | const auto& callbackInfo = mCallbacks[Callback::Vsync]; |
| 2642 | auto displayId = mHwc1DisplayMap[hwc1DisplayId]; |
| 2643 | |
| 2644 | // Call back without the state lock held |
| 2645 | lock.unlock(); |
| 2646 | |
| 2647 | auto vsync = reinterpret_cast<HWC2_PFN_VSYNC>(callbackInfo.pointer); |
| 2648 | vsync(callbackInfo.data, displayId, timestamp); |
| 2649 | } |
| 2650 | |
| 2651 | void HWC2On1Adapter::hwc1Hotplug(int hwc1DisplayId, int connected) |
| 2652 | { |
| 2653 | ALOGV("Received hwc1Hotplug(%d, %d)", hwc1DisplayId, connected); |
| 2654 | |
| 2655 | if (hwc1DisplayId != HWC_DISPLAY_EXTERNAL) { |
| 2656 | ALOGE("hwc1Hotplug: Received hotplug for non-external display"); |
| 2657 | return; |
| 2658 | } |
| 2659 | |
Dan Stoza | fc4e202 | 2016-02-23 11:43:19 -0800 | [diff] [blame] | 2660 | std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 2661 | |
| 2662 | // If the HWC2-side callback hasn't been registered yet, buffer this until |
| 2663 | // it is registered |
| 2664 | if (mCallbacks.count(Callback::Hotplug) == 0) { |
| 2665 | mPendingHotplugs.emplace_back(hwc1DisplayId, connected); |
| 2666 | return; |
| 2667 | } |
| 2668 | |
| 2669 | hwc2_display_t displayId = UINT64_MAX; |
| 2670 | if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) { |
| 2671 | if (connected == 0) { |
| 2672 | ALOGW("hwc1Hotplug: Received disconnect for unconnected display"); |
| 2673 | return; |
| 2674 | } |
| 2675 | |
| 2676 | // Create a new display on connect |
| 2677 | auto display = std::make_shared<HWC2On1Adapter::Display>(*this, |
| 2678 | HWC2::DisplayType::Physical); |
| 2679 | display->setHwc1Id(HWC_DISPLAY_EXTERNAL); |
| 2680 | display->populateConfigs(); |
| 2681 | displayId = display->getId(); |
| 2682 | mHwc1DisplayMap[HWC_DISPLAY_EXTERNAL] = displayId; |
| 2683 | mDisplays.emplace(displayId, std::move(display)); |
| 2684 | } else { |
| 2685 | if (connected != 0) { |
| 2686 | ALOGW("hwc1Hotplug: Received connect for previously connected " |
| 2687 | "display"); |
| 2688 | return; |
| 2689 | } |
| 2690 | |
| 2691 | // Disconnect an existing display |
| 2692 | displayId = mHwc1DisplayMap[hwc1DisplayId]; |
| 2693 | mHwc1DisplayMap.erase(HWC_DISPLAY_EXTERNAL); |
| 2694 | mDisplays.erase(displayId); |
| 2695 | } |
| 2696 | |
| 2697 | const auto& callbackInfo = mCallbacks[Callback::Hotplug]; |
| 2698 | |
| 2699 | // Call back without the state lock held |
| 2700 | lock.unlock(); |
| 2701 | |
| 2702 | auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(callbackInfo.pointer); |
| 2703 | auto hwc2Connected = (connected == 0) ? |
| 2704 | HWC2::Connection::Disconnected : HWC2::Connection::Connected; |
| 2705 | hotplug(callbackInfo.data, displayId, static_cast<int32_t>(hwc2Connected)); |
| 2706 | } |
| 2707 | |
| 2708 | } // namespace android |