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