HWC2On1Adapter.h revision e29055f5fe4a2bc0f3f75d952ff417462dc607cc
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#ifndef ANDROID_SF_HWC2_ON_1_ADAPTER_H 18#define ANDROID_SF_HWC2_ON_1_ADAPTER_H 19 20#define HWC2_INCLUDE_STRINGIFICATION 21#define HWC2_USE_CPP11 22#include <hardware/hwcomposer2.h> 23#undef HWC2_INCLUDE_STRINGIFICATION 24#undef HWC2_USE_CPP11 25 26#include "MiniFence.h" 27 28#include <atomic> 29#include <map> 30#include <mutex> 31#include <queue> 32#include <set> 33#include <unordered_map> 34#include <unordered_set> 35#include <vector> 36 37struct hwc_composer_device_1; 38struct hwc_display_contents_1; 39struct hwc_layer_1; 40 41namespace android { 42 43// For devices unable to provide an implementation of HWC2 (see hwcomposer2.h), 44// we provide an adapter able to talk to HWC1 (see hwcomposer.h). It translates 45// streamed function calls ala HWC2 model to batched array of structs calls ala 46// HWC1 model. 47class HWC2On1Adapter : public hwc2_device_t 48{ 49public: 50 explicit HWC2On1Adapter(struct hwc_composer_device_1* hwc1Device); 51 ~HWC2On1Adapter(); 52 53 struct hwc_composer_device_1* getHwc1Device() const { return mHwc1Device; } 54 uint8_t getHwc1MinorVersion() const { return mHwc1MinorVersion; } 55 56private: 57 static inline HWC2On1Adapter* getAdapter(hwc2_device_t* device) { 58 return static_cast<HWC2On1Adapter*>(device); 59 } 60 61 // getCapabilities 62 63 void doGetCapabilities(uint32_t* outCount, 64 int32_t* /*hwc2_capability_t*/ outCapabilities); 65 static void getCapabilitiesHook(hwc2_device_t* device, uint32_t* outCount, 66 int32_t* /*hwc2_capability_t*/ outCapabilities) { 67 getAdapter(device)->doGetCapabilities(outCount, outCapabilities); 68 } 69 70 bool supportsBackgroundColor() { 71 return mHwc1SupportsBackgroundColor; 72 } 73 74 // getFunction 75 76 hwc2_function_pointer_t doGetFunction(HWC2::FunctionDescriptor descriptor); 77 static hwc2_function_pointer_t getFunctionHook(hwc2_device_t* device, 78 int32_t intDesc) { 79 auto descriptor = static_cast<HWC2::FunctionDescriptor>(intDesc); 80 return getAdapter(device)->doGetFunction(descriptor); 81 } 82 83 // Device functions 84 85 HWC2::Error createVirtualDisplay(uint32_t width, uint32_t height, 86 hwc2_display_t* outDisplay); 87 static int32_t createVirtualDisplayHook(hwc2_device_t* device, 88 uint32_t width, uint32_t height, int32_t* /*format*/, 89 hwc2_display_t* outDisplay) { 90 // HWC1 implementations cannot override the buffer format requested by 91 // the consumer 92 auto error = getAdapter(device)->createVirtualDisplay(width, height, 93 outDisplay); 94 return static_cast<int32_t>(error); 95 } 96 97 HWC2::Error destroyVirtualDisplay(hwc2_display_t display); 98 static int32_t destroyVirtualDisplayHook(hwc2_device_t* device, 99 hwc2_display_t display) { 100 auto error = getAdapter(device)->destroyVirtualDisplay(display); 101 return static_cast<int32_t>(error); 102 } 103 104 std::string mDumpString; 105 void dump(uint32_t* outSize, char* outBuffer); 106 static void dumpHook(hwc2_device_t* device, uint32_t* outSize, 107 char* outBuffer) { 108 getAdapter(device)->dump(outSize, outBuffer); 109 } 110 111 uint32_t getMaxVirtualDisplayCount(); 112 static uint32_t getMaxVirtualDisplayCountHook(hwc2_device_t* device) { 113 return getAdapter(device)->getMaxVirtualDisplayCount(); 114 } 115 116 HWC2::Error registerCallback(HWC2::Callback descriptor, 117 hwc2_callback_data_t callbackData, hwc2_function_pointer_t pointer); 118 static int32_t registerCallbackHook(hwc2_device_t* device, 119 int32_t intDesc, hwc2_callback_data_t callbackData, 120 hwc2_function_pointer_t pointer) { 121 auto descriptor = static_cast<HWC2::Callback>(intDesc); 122 auto error = getAdapter(device)->registerCallback(descriptor, 123 callbackData, pointer); 124 return static_cast<int32_t>(error); 125 } 126 127 // Display functions 128 129 class Layer; 130 131 class SortLayersByZ { 132 public: 133 bool operator()(const std::shared_ptr<Layer>& lhs, 134 const std::shared_ptr<Layer>& rhs); 135 }; 136 137 // The semantics of the fences returned by the device differ between 138 // hwc1.set() and hwc2.present(). Read hwcomposer.h and hwcomposer2.h 139 // for more information. 140 // 141 // Release fences in hwc1 are obtained on set() for a frame n and signaled 142 // when the layer buffer is not needed for read operations anymore 143 // (typically on frame n+1). In HWC2, release fences are obtained with a 144 // special call after present() for frame n. These fences signal 145 // on frame n: More specifically, the fence for a given buffer provided in 146 // frame n will signal when the prior buffer is no longer required. 147 // 148 // A retire fence (HWC1) is signaled when a composition is replaced 149 // on the panel whereas a present fence (HWC2) is signaled when a 150 // composition starts to be displayed on a panel. 151 // 152 // The HWC2to1Adapter emulates the new fence semantics for a frame 153 // n by returning the fence from frame n-1. For frame 0, the adapter 154 // returns NO_FENCE. 155 class DeferredFence { 156 public: 157 DeferredFence() 158 : mFences({MiniFence::NO_FENCE, MiniFence::NO_FENCE}) {} 159 160 void add(int32_t fenceFd) { 161 mFences.emplace(new MiniFence(fenceFd)); 162 mFences.pop(); 163 } 164 165 const sp<MiniFence>& get() const { 166 return mFences.front(); 167 } 168 169 private: 170 // There are always two fences in this queue. 171 std::queue<sp<MiniFence>> mFences; 172 }; 173 174 class FencedBuffer { 175 public: 176 FencedBuffer() : mBuffer(nullptr), mFence(MiniFence::NO_FENCE) {} 177 178 void setBuffer(buffer_handle_t buffer) { mBuffer = buffer; } 179 void setFence(int fenceFd) { mFence = new MiniFence(fenceFd); } 180 181 buffer_handle_t getBuffer() const { return mBuffer; } 182 int getFence() const { return mFence->dup(); } 183 184 private: 185 buffer_handle_t mBuffer; 186 sp<MiniFence> mFence; 187 }; 188 189 class Display { 190 public: 191 Display(HWC2On1Adapter& device, HWC2::DisplayType type); 192 193 hwc2_display_t getId() const { return mId; } 194 HWC2On1Adapter& getDevice() const { return mDevice; } 195 196 // Does not require locking because it is set before adding the 197 // Displays to the Adapter's list of displays 198 void setHwc1Id(int32_t id) { mHwc1Id = id; } 199 int32_t getHwc1Id() const { return mHwc1Id; } 200 201 // HWC2 Display functions 202 HWC2::Error acceptChanges(); 203 HWC2::Error createLayer(hwc2_layer_t* outLayerId); 204 HWC2::Error destroyLayer(hwc2_layer_t layerId); 205 HWC2::Error getActiveConfig(hwc2_config_t* outConfigId); 206 HWC2::Error getAttribute(hwc2_config_t configId, 207 HWC2::Attribute attribute, int32_t* outValue); 208 HWC2::Error getChangedCompositionTypes(uint32_t* outNumElements, 209 hwc2_layer_t* outLayers, int32_t* outTypes); 210 HWC2::Error getColorModes(uint32_t* outNumModes, int32_t* outModes); 211 HWC2::Error getConfigs(uint32_t* outNumConfigs, 212 hwc2_config_t* outConfigIds); 213 HWC2::Error getDozeSupport(int32_t* outSupport); 214 HWC2::Error getHdrCapabilities(uint32_t* outNumTypes, 215 int32_t* outTypes, float* outMaxLuminance, 216 float* outMaxAverageLuminance, float* outMinLuminance); 217 HWC2::Error getName(uint32_t* outSize, char* outName); 218 HWC2::Error getReleaseFences(uint32_t* outNumElements, 219 hwc2_layer_t* outLayers, int32_t* outFences); 220 HWC2::Error getRequests(int32_t* outDisplayRequests, 221 uint32_t* outNumElements, hwc2_layer_t* outLayers, 222 int32_t* outLayerRequests); 223 HWC2::Error getType(int32_t* outType); 224 225 // Since HWC1 "presents" (called "set" in HWC1) all Displays 226 // at once, the first call to any Display::present will trigger 227 // present() on all Displays in the Device. Subsequent calls without 228 // first calling validate() are noop (except for duping/returning 229 // the retire fence). 230 HWC2::Error present(int32_t* outRetireFence); 231 232 HWC2::Error setActiveConfig(hwc2_config_t configId); 233 HWC2::Error setClientTarget(buffer_handle_t target, 234 int32_t acquireFence, int32_t dataspace, 235 hwc_region_t damage); 236 HWC2::Error setColorMode(android_color_mode_t mode); 237 HWC2::Error setColorTransform(android_color_transform_t hint); 238 HWC2::Error setOutputBuffer(buffer_handle_t buffer, 239 int32_t releaseFence); 240 HWC2::Error setPowerMode(HWC2::PowerMode mode); 241 HWC2::Error setVsyncEnabled(HWC2::Vsync enabled); 242 243 // Since HWC1 "validates" (called "prepare" in HWC1) all Displays 244 // at once, the first call to any Display::validate() will trigger 245 // validate() on all other Displays in the Device. 246 HWC2::Error validate(uint32_t* outNumTypes, 247 uint32_t* outNumRequests); 248 249 HWC2::Error updateLayerZ(hwc2_layer_t layerId, uint32_t z); 250 251 // Read configs from HWC1 device 252 void populateConfigs(); 253 254 // Set configs for a virtual display 255 void populateConfigs(uint32_t width, uint32_t height); 256 257 bool prepare(); 258 259 // Called after hwc.prepare() with responses from the device. 260 void generateChanges(); 261 262 bool hasChanges() const; 263 HWC2::Error set(hwc_display_contents_1& hwcContents); 264 void addRetireFence(int fenceFd); 265 void addReleaseFences(const hwc_display_contents_1& hwcContents); 266 267 bool hasColorTransform() const; 268 269 std::string dump() const; 270 271 // Return a rect from the pool allocated during validate() 272 hwc_rect_t* GetRects(size_t numRects); 273 274 hwc_display_contents_1* getDisplayContents(); 275 276 void markGeometryChanged() { mGeometryChanged = true; } 277 void resetGeometryMarker() { mGeometryChanged = false;} 278 private: 279 class Config { 280 public: 281 Config(Display& display) 282 : mDisplay(display), 283 mId(0), 284 mAttributes() {} 285 286 bool isOnDisplay(const Display& display) const { 287 return display.getId() == mDisplay.getId(); 288 } 289 290 void setAttribute(HWC2::Attribute attribute, int32_t value); 291 int32_t getAttribute(HWC2::Attribute attribute) const; 292 293 void setHwc1Id(uint32_t id); 294 bool hasHwc1Id(uint32_t id) const; 295 HWC2::Error getColorModeForHwc1Id(uint32_t id, 296 android_color_mode_t *outMode) const; 297 HWC2::Error getHwc1IdForColorMode(android_color_mode_t mode, 298 uint32_t* outId) const; 299 300 void setId(hwc2_config_t id) { mId = id; } 301 hwc2_config_t getId() const { return mId; } 302 303 // Attempts to merge two configs that differ only in color 304 // mode. Returns whether the merge was successful 305 bool merge(const Config& other); 306 307 std::set<android_color_mode_t> getColorModes() const; 308 309 // splitLine divides the output into two lines suitable for 310 // dumpsys SurfaceFlinger 311 std::string toString(bool splitLine = false) const; 312 313 private: 314 Display& mDisplay; 315 hwc2_config_t mId; 316 std::unordered_map<HWC2::Attribute, int32_t> mAttributes; 317 318 // Maps from color transform to HWC1 config ID 319 std::unordered_map<android_color_mode_t, uint32_t> mHwc1Ids; 320 }; 321 322 // Stores changes requested from the device upon calling prepare(). 323 // Handles change request to: 324 // - Layer composition type. 325 // - Layer hints. 326 class Changes { 327 public: 328 uint32_t getNumTypes() const { 329 return static_cast<uint32_t>(mTypeChanges.size()); 330 } 331 332 uint32_t getNumLayerRequests() const { 333 return static_cast<uint32_t>(mLayerRequests.size()); 334 } 335 336 const std::unordered_map<hwc2_layer_t, HWC2::Composition>& 337 getTypeChanges() const { 338 return mTypeChanges; 339 } 340 341 const std::unordered_map<hwc2_layer_t, HWC2::LayerRequest>& 342 getLayerRequests() const { 343 return mLayerRequests; 344 } 345 346 void addTypeChange(hwc2_layer_t layerId, 347 HWC2::Composition type) { 348 mTypeChanges.insert({layerId, type}); 349 } 350 351 void clearTypeChanges() { mTypeChanges.clear(); } 352 353 void addLayerRequest(hwc2_layer_t layerId, 354 HWC2::LayerRequest request) { 355 mLayerRequests.insert({layerId, request}); 356 } 357 358 private: 359 std::unordered_map<hwc2_layer_t, HWC2::Composition> 360 mTypeChanges; 361 std::unordered_map<hwc2_layer_t, HWC2::LayerRequest> 362 mLayerRequests; 363 }; 364 365 std::shared_ptr<const Config> 366 getConfig(hwc2_config_t configId) const; 367 368 void populateColorModes(); 369 void initializeActiveConfig(); 370 371 // Creates a bi-directional mapping between index in HWC1 372 // prepare/set array and Layer object. Stores mapping in 373 // mHwc1LayerMap and also updates Layer's attribute mHwc1Id. 374 void assignHwc1LayerIds(); 375 376 // Called after a response to prepare() has been received: 377 // Ingest composition type changes requested by the device. 378 void updateTypeChanges(const struct hwc_layer_1& hwc1Layer, 379 const Layer& layer); 380 381 // Called after a response to prepare() has been received: 382 // Ingest layer hint changes requested by the device. 383 void updateLayerRequests(const struct hwc_layer_1& hwc1Layer, 384 const Layer& layer); 385 386 // Set all fields in HWC1 comm array for layer containing the 387 // HWC_FRAMEBUFFER_TARGET (always the last layer). 388 void prepareFramebufferTarget(); 389 390 // Display ID generator. 391 static std::atomic<hwc2_display_t> sNextId; 392 const hwc2_display_t mId; 393 394 395 HWC2On1Adapter& mDevice; 396 397 // The state of this display should only be modified from 398 // SurfaceFlinger's main loop, with the exception of when dump is 399 // called. To prevent a bad state from crashing us during a dump 400 // call, all public calls into Display must acquire this mutex. 401 // 402 // It is recursive because we don't want to deadlock in validate 403 // (or present) when we call HWC2On1Adapter::prepareAllDisplays 404 // (or setAllDisplays), which calls back into Display functions 405 // which require locking. 406 mutable std::recursive_mutex mStateMutex; 407 408 // Allocate RAM able to store all layers and rects used for 409 // communication with HWC1. Place allocated RAM in variable 410 // mHwc1RequestedContents. 411 void allocateRequestedContents(); 412 413 // Array of structs exchanged between client and hwc1 device. 414 // Sent to device upon calling prepare(). 415 std::unique_ptr<hwc_display_contents_1> mHwc1RequestedContents; 416 private: 417 DeferredFence mRetireFence; 418 419 // Will only be non-null after the Display has been validated and 420 // before it has been presented 421 std::unique_ptr<Changes> mChanges; 422 423 int32_t mHwc1Id; 424 425 std::vector<std::shared_ptr<Config>> mConfigs; 426 std::shared_ptr<const Config> mActiveConfig; 427 std::set<android_color_mode_t> mColorModes; 428 android_color_mode_t mActiveColorMode; 429 std::string mName; 430 HWC2::DisplayType mType; 431 HWC2::PowerMode mPowerMode; 432 HWC2::Vsync mVsyncEnabled; 433 434 // Used to populate HWC1 HWC_FRAMEBUFFER_TARGET layer 435 FencedBuffer mClientTarget; 436 437 438 FencedBuffer mOutputBuffer; 439 440 bool mHasColorTransform; 441 442 // All layers this Display is aware of. 443 std::multiset<std::shared_ptr<Layer>, SortLayersByZ> mLayers; 444 445 // Mapping between layer index in array of hwc_display_contents_1* 446 // passed to HWC1 during validate/set and Layer object. 447 std::unordered_map<size_t, std::shared_ptr<Layer>> mHwc1LayerMap; 448 449 // All communication with HWC1 via prepare/set is done with one 450 // alloc. This pointer is pointing to a pool of hwc_rect_t. 451 size_t mNumAvailableRects; 452 hwc_rect_t* mNextAvailableRect; 453 454 // True if any of the Layers contained in this Display have been 455 // updated with anything other than a buffer since last call to 456 // Display::set() 457 bool mGeometryChanged; 458 }; 459 460 // Utility template calling a Display object method directly based on the 461 // hwc2_display_t displayId parameter. 462 template <typename ...Args> 463 static int32_t callDisplayFunction(hwc2_device_t* device, 464 hwc2_display_t displayId, HWC2::Error (Display::*member)(Args...), 465 Args... args) { 466 auto display = getAdapter(device)->getDisplay(displayId); 467 if (!display) { 468 return static_cast<int32_t>(HWC2::Error::BadDisplay); 469 } 470 auto error = ((*display).*member)(std::forward<Args>(args)...); 471 return static_cast<int32_t>(error); 472 } 473 474 template <typename MF, MF memFunc, typename ...Args> 475 static int32_t displayHook(hwc2_device_t* device, hwc2_display_t displayId, 476 Args... args) { 477 return HWC2On1Adapter::callDisplayFunction(device, displayId, memFunc, 478 std::forward<Args>(args)...); 479 } 480 481 static int32_t getDisplayAttributeHook(hwc2_device_t* device, 482 hwc2_display_t display, hwc2_config_t config, 483 int32_t intAttribute, int32_t* outValue) { 484 auto attribute = static_cast<HWC2::Attribute>(intAttribute); 485 return callDisplayFunction(device, display, &Display::getAttribute, 486 config, attribute, outValue); 487 } 488 489 static int32_t setColorTransformHook(hwc2_device_t* device, 490 hwc2_display_t display, const float* /*matrix*/, 491 int32_t /*android_color_transform_t*/ intHint) { 492 // We intentionally throw away the matrix, because if the hint is 493 // anything other than IDENTITY, we have to fall back to client 494 // composition anyway 495 auto hint = static_cast<android_color_transform_t>(intHint); 496 return callDisplayFunction(device, display, &Display::setColorTransform, 497 hint); 498 } 499 500 static int32_t setColorModeHook(hwc2_device_t* device, 501 hwc2_display_t display, int32_t /*android_color_mode_t*/ intMode) { 502 auto mode = static_cast<android_color_mode_t>(intMode); 503 return callDisplayFunction(device, display, &Display::setColorMode, 504 mode); 505 } 506 507 static int32_t setPowerModeHook(hwc2_device_t* device, 508 hwc2_display_t display, int32_t intMode) { 509 auto mode = static_cast<HWC2::PowerMode>(intMode); 510 return callDisplayFunction(device, display, &Display::setPowerMode, 511 mode); 512 } 513 514 static int32_t setVsyncEnabledHook(hwc2_device_t* device, 515 hwc2_display_t display, int32_t intEnabled) { 516 auto enabled = static_cast<HWC2::Vsync>(intEnabled); 517 return callDisplayFunction(device, display, &Display::setVsyncEnabled, 518 enabled); 519 } 520 521 class Layer { 522 public: 523 explicit Layer(Display& display); 524 525 bool operator==(const Layer& other) { return mId == other.mId; } 526 bool operator!=(const Layer& other) { return !(*this == other); } 527 528 hwc2_layer_t getId() const { return mId; } 529 Display& getDisplay() const { return mDisplay; } 530 531 // HWC2 Layer functions 532 HWC2::Error setBuffer(buffer_handle_t buffer, int32_t acquireFence); 533 HWC2::Error setCursorPosition(int32_t x, int32_t y); 534 HWC2::Error setSurfaceDamage(hwc_region_t damage); 535 536 // HWC2 Layer state functions 537 HWC2::Error setBlendMode(HWC2::BlendMode mode); 538 HWC2::Error setColor(hwc_color_t color); 539 HWC2::Error setCompositionType(HWC2::Composition type); 540 HWC2::Error setDataspace(android_dataspace_t dataspace); 541 HWC2::Error setDisplayFrame(hwc_rect_t frame); 542 HWC2::Error setPlaneAlpha(float alpha); 543 HWC2::Error setSidebandStream(const native_handle_t* stream); 544 HWC2::Error setSourceCrop(hwc_frect_t crop); 545 HWC2::Error setTransform(HWC2::Transform transform); 546 HWC2::Error setVisibleRegion(hwc_region_t visible); 547 HWC2::Error setZ(uint32_t z); 548 549 HWC2::Composition getCompositionType() const { 550 return mCompositionType; 551 } 552 uint32_t getZ() const { return mZ; } 553 554 void addReleaseFence(int fenceFd); 555 const sp<MiniFence>& getReleaseFence() const; 556 557 void setHwc1Id(size_t id) { mHwc1Id = id; } 558 size_t getHwc1Id() const { return mHwc1Id; } 559 560 // Write state to HWC1 communication struct. 561 void applyState(struct hwc_layer_1& hwc1Layer); 562 563 std::string dump() const; 564 565 std::size_t getNumVisibleRegions() { return mVisibleRegion.size(); } 566 567 std::size_t getNumSurfaceDamages() { return mSurfaceDamage.size(); } 568 569 // True if a layer cannot be properly rendered by the device due 570 // to usage of SolidColor (a.k.a BackgroundColor in HWC1). 571 bool hasUnsupportedBackgroundColor() { 572 return (mCompositionType == HWC2::Composition::SolidColor && 573 !mDisplay.getDevice().supportsBackgroundColor()); 574 } 575 private: 576 void applyCommonState(struct hwc_layer_1& hwc1Layer); 577 void applySolidColorState(struct hwc_layer_1& hwc1Layer); 578 void applySidebandState(struct hwc_layer_1& hwc1Layer); 579 void applyBufferState(struct hwc_layer_1& hwc1Layer); 580 void applyCompositionType(struct hwc_layer_1& hwc1Layer); 581 582 static std::atomic<hwc2_layer_t> sNextId; 583 const hwc2_layer_t mId; 584 Display& mDisplay; 585 586 FencedBuffer mBuffer; 587 std::vector<hwc_rect_t> mSurfaceDamage; 588 589 HWC2::BlendMode mBlendMode; 590 hwc_color_t mColor; 591 HWC2::Composition mCompositionType; 592 hwc_rect_t mDisplayFrame; 593 float mPlaneAlpha; 594 const native_handle_t* mSidebandStream; 595 hwc_frect_t mSourceCrop; 596 HWC2::Transform mTransform; 597 std::vector<hwc_rect_t> mVisibleRegion; 598 599 uint32_t mZ; 600 601 DeferredFence mReleaseFence; 602 603 size_t mHwc1Id; 604 bool mHasUnsupportedPlaneAlpha; 605 }; 606 607 // Utility tempate calling a Layer object method based on ID parameters: 608 // hwc2_display_t displayId 609 // and 610 // hwc2_layer_t layerId 611 template <typename ...Args> 612 static int32_t callLayerFunction(hwc2_device_t* device, 613 hwc2_display_t displayId, hwc2_layer_t layerId, 614 HWC2::Error (Layer::*member)(Args...), Args... args) { 615 auto result = getAdapter(device)->getLayer(displayId, layerId); 616 auto error = std::get<HWC2::Error>(result); 617 if (error == HWC2::Error::None) { 618 auto layer = std::get<Layer*>(result); 619 error = ((*layer).*member)(std::forward<Args>(args)...); 620 } 621 return static_cast<int32_t>(error); 622 } 623 624 template <typename MF, MF memFunc, typename ...Args> 625 static int32_t layerHook(hwc2_device_t* device, hwc2_display_t displayId, 626 hwc2_layer_t layerId, Args... args) { 627 return HWC2On1Adapter::callLayerFunction(device, displayId, layerId, 628 memFunc, std::forward<Args>(args)...); 629 } 630 631 // Layer state functions 632 633 static int32_t setLayerBlendModeHook(hwc2_device_t* device, 634 hwc2_display_t display, hwc2_layer_t layer, int32_t intMode) { 635 auto mode = static_cast<HWC2::BlendMode>(intMode); 636 return callLayerFunction(device, display, layer, 637 &Layer::setBlendMode, mode); 638 } 639 640 static int32_t setLayerCompositionTypeHook(hwc2_device_t* device, 641 hwc2_display_t display, hwc2_layer_t layer, int32_t intType) { 642 auto type = static_cast<HWC2::Composition>(intType); 643 return callLayerFunction(device, display, layer, 644 &Layer::setCompositionType, type); 645 } 646 647 static int32_t setLayerDataspaceHook(hwc2_device_t* device, 648 hwc2_display_t display, hwc2_layer_t layer, int32_t intDataspace) { 649 auto dataspace = static_cast<android_dataspace_t>(intDataspace); 650 return callLayerFunction(device, display, layer, &Layer::setDataspace, 651 dataspace); 652 } 653 654 static int32_t setLayerTransformHook(hwc2_device_t* device, 655 hwc2_display_t display, hwc2_layer_t layer, int32_t intTransform) { 656 auto transform = static_cast<HWC2::Transform>(intTransform); 657 return callLayerFunction(device, display, layer, &Layer::setTransform, 658 transform); 659 } 660 661 static int32_t setLayerZOrderHook(hwc2_device_t* device, 662 hwc2_display_t display, hwc2_layer_t layer, uint32_t z) { 663 return callDisplayFunction(device, display, &Display::updateLayerZ, 664 layer, z); 665 } 666 667 // Adapter internals 668 669 void populateCapabilities(); 670 Display* getDisplay(hwc2_display_t id); 671 std::tuple<Layer*, HWC2::Error> getLayer(hwc2_display_t displayId, 672 hwc2_layer_t layerId); 673 void populatePrimary(); 674 675 bool prepareAllDisplays(); 676 std::vector<struct hwc_display_contents_1*> mHwc1Contents; 677 HWC2::Error setAllDisplays(); 678 679 // Callbacks 680 void hwc1Invalidate(); 681 void hwc1Vsync(int hwc1DisplayId, int64_t timestamp); 682 void hwc1Hotplug(int hwc1DisplayId, int connected); 683 684 // These are set in the constructor and before any asynchronous events are 685 // possible 686 687 struct hwc_composer_device_1* const mHwc1Device; 688 const uint8_t mHwc1MinorVersion; 689 bool mHwc1SupportsVirtualDisplays; 690 bool mHwc1SupportsBackgroundColor; 691 692 class Callbacks; 693 const std::unique_ptr<Callbacks> mHwc1Callbacks; 694 695 std::unordered_set<HWC2::Capability> mCapabilities; 696 697 // These are only accessed from the main SurfaceFlinger thread (not from 698 // callbacks or dump 699 700 std::map<hwc2_layer_t, std::shared_ptr<Layer>> mLayers; 701 702 // A HWC1 supports only one virtual display. 703 std::shared_ptr<Display> mHwc1VirtualDisplay; 704 705 // These are potentially accessed from multiple threads, and are protected 706 // by this mutex. This needs to be recursive, since the HWC1 implementation 707 // can call back into the invalidate callback on the same thread that is 708 // calling prepare. 709 std::recursive_timed_mutex mStateMutex; 710 711 struct CallbackInfo { 712 hwc2_callback_data_t data; 713 hwc2_function_pointer_t pointer; 714 }; 715 std::unordered_map<HWC2::Callback, CallbackInfo> mCallbacks; 716 bool mHasPendingInvalidate; 717 718 // There is a small gap between the time the HWC1 module is started and 719 // when the callbacks for vsync and hotplugs are registered by the 720 // HWC2on1Adapter. To prevent losing events they are stored in these arrays 721 // and fed to the callback as soon as possible. 722 std::vector<std::pair<int, int64_t>> mPendingVsyncs; 723 std::vector<std::pair<int, int>> mPendingHotplugs; 724 725 // Mapping between HWC1 display id and Display objects. 726 std::map<hwc2_display_t, std::shared_ptr<Display>> mDisplays; 727 728 // Map HWC1 display type (HWC_DISPLAY_PRIMARY, HWC_DISPLAY_EXTERNAL, 729 // HWC_DISPLAY_VIRTUAL) to Display IDs generated by HWC2on1Adapter objects. 730 std::unordered_map<int, hwc2_display_t> mHwc1DisplayMap; 731}; 732 733} // namespace android 734 735#endif 736