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 | #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 <ui/Fence.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 | |
| 37 | struct hwc_composer_device_1; |
| 38 | struct hwc_display_contents_1; |
| 39 | struct hwc_layer_1; |
| 40 | |
| 41 | namespace android { |
| 42 | |
| 43 | class HWC2On1Adapter : public hwc2_device_t |
| 44 | { |
| 45 | public: |
| 46 | HWC2On1Adapter(struct hwc_composer_device_1* hwc1Device); |
| 47 | ~HWC2On1Adapter(); |
| 48 | |
| 49 | struct hwc_composer_device_1* getHwc1Device() const { return mHwc1Device; } |
| 50 | uint8_t getHwc1MinorVersion() const { return mHwc1MinorVersion; } |
| 51 | |
| 52 | private: |
| 53 | static inline HWC2On1Adapter* getAdapter(hwc2_device_t* device) { |
| 54 | return static_cast<HWC2On1Adapter*>(device); |
| 55 | } |
| 56 | |
| 57 | // getCapabilities |
| 58 | |
| 59 | void doGetCapabilities(uint32_t* outCount, |
| 60 | int32_t* /*hwc2_capability_t*/ outCapabilities); |
| 61 | static void getCapabilitiesHook(hwc2_device_t* device, uint32_t* outCount, |
| 62 | int32_t* /*hwc2_capability_t*/ outCapabilities) { |
| 63 | getAdapter(device)->doGetCapabilities(outCount, outCapabilities); |
| 64 | } |
| 65 | |
| 66 | // getFunction |
| 67 | |
| 68 | hwc2_function_pointer_t doGetFunction(HWC2::FunctionDescriptor descriptor); |
| 69 | static hwc2_function_pointer_t getFunctionHook(hwc2_device_t* device, |
| 70 | int32_t intDesc) { |
| 71 | auto descriptor = static_cast<HWC2::FunctionDescriptor>(intDesc); |
| 72 | return getAdapter(device)->doGetFunction(descriptor); |
| 73 | } |
| 74 | |
| 75 | // Device functions |
| 76 | |
| 77 | HWC2::Error createVirtualDisplay(uint32_t width, uint32_t height, |
| 78 | hwc2_display_t* outDisplay); |
| 79 | static int32_t createVirtualDisplayHook(hwc2_device_t* device, |
| 80 | uint32_t width, uint32_t height, hwc2_display_t* outDisplay) { |
| 81 | auto error = getAdapter(device)->createVirtualDisplay(width, height, |
| 82 | outDisplay); |
| 83 | return static_cast<int32_t>(error); |
| 84 | } |
| 85 | |
| 86 | HWC2::Error destroyVirtualDisplay(hwc2_display_t display); |
| 87 | static int32_t destroyVirtualDisplayHook(hwc2_device_t* device, |
| 88 | hwc2_display_t display) { |
| 89 | auto error = getAdapter(device)->destroyVirtualDisplay(display); |
| 90 | return static_cast<int32_t>(error); |
| 91 | } |
| 92 | |
| 93 | std::string mDumpString; |
| 94 | void dump(uint32_t* outSize, char* outBuffer); |
| 95 | static void dumpHook(hwc2_device_t* device, uint32_t* outSize, |
| 96 | char* outBuffer) { |
| 97 | getAdapter(device)->dump(outSize, outBuffer); |
| 98 | } |
| 99 | |
| 100 | uint32_t getMaxVirtualDisplayCount(); |
| 101 | static uint32_t getMaxVirtualDisplayCountHook(hwc2_device_t* device) { |
| 102 | return getAdapter(device)->getMaxVirtualDisplayCount(); |
| 103 | } |
| 104 | |
| 105 | HWC2::Error registerCallback(HWC2::Callback descriptor, |
| 106 | hwc2_callback_data_t callbackData, hwc2_function_pointer_t pointer); |
| 107 | static int32_t registerCallbackHook(hwc2_device_t* device, |
| 108 | int32_t intDesc, hwc2_callback_data_t callbackData, |
| 109 | hwc2_function_pointer_t pointer) { |
| 110 | auto descriptor = static_cast<HWC2::Callback>(intDesc); |
| 111 | auto error = getAdapter(device)->registerCallback(descriptor, |
| 112 | callbackData, pointer); |
| 113 | return static_cast<int32_t>(error); |
| 114 | } |
| 115 | |
| 116 | // Display functions |
| 117 | |
| 118 | class Layer; |
| 119 | |
| 120 | class SortLayersByZ { |
| 121 | public: |
| 122 | bool operator()(const std::shared_ptr<Layer>& lhs, |
| 123 | const std::shared_ptr<Layer>& rhs); |
| 124 | }; |
| 125 | |
| 126 | class DisplayContentsDeleter { |
| 127 | public: |
| 128 | void operator()(struct hwc_display_contents_1* contents); |
| 129 | }; |
| 130 | |
| 131 | class DeferredFence { |
| 132 | public: |
| 133 | DeferredFence() |
| 134 | : mMutex(), |
| 135 | mFences({Fence::NO_FENCE, Fence::NO_FENCE}) {} |
| 136 | |
| 137 | void add(int32_t fenceFd) { |
| 138 | mFences.emplace(new Fence(fenceFd)); |
| 139 | mFences.pop(); |
| 140 | } |
| 141 | |
| 142 | const sp<Fence>& get() const { |
| 143 | return mFences.front(); |
| 144 | } |
| 145 | |
| 146 | private: |
| 147 | mutable std::mutex mMutex; |
| 148 | std::queue<sp<Fence>> mFences; |
| 149 | }; |
| 150 | |
| 151 | class FencedBuffer { |
| 152 | public: |
| 153 | FencedBuffer() : mBuffer(nullptr), mFence(Fence::NO_FENCE) {} |
| 154 | |
| 155 | void setBuffer(buffer_handle_t buffer) { mBuffer = buffer; } |
| 156 | void setFence(int fenceFd) { mFence = new Fence(fenceFd); } |
| 157 | |
| 158 | buffer_handle_t getBuffer() const { return mBuffer; } |
| 159 | int getFence() const { return mFence->dup(); } |
| 160 | |
| 161 | private: |
| 162 | buffer_handle_t mBuffer; |
| 163 | sp<Fence> mFence; |
| 164 | }; |
| 165 | |
| 166 | class Display { |
| 167 | public: |
| 168 | typedef std::unique_ptr<hwc_display_contents_1, |
| 169 | DisplayContentsDeleter> HWC1Contents; |
| 170 | |
| 171 | Display(HWC2On1Adapter& device, HWC2::DisplayType type); |
| 172 | |
| 173 | hwc2_display_t getId() const { return mId; } |
| 174 | HWC2On1Adapter& getDevice() const { return mDevice; } |
| 175 | |
| 176 | // Does not require locking because it is set before adding the |
| 177 | // Displays to the Adapter's list of displays |
| 178 | void setHwc1Id(int32_t id) { mHwc1Id = id; } |
| 179 | int32_t getHwc1Id() const { return mHwc1Id; } |
| 180 | |
| 181 | void incDirty() { ++mDirtyCount; } |
| 182 | void decDirty() { --mDirtyCount; } |
| 183 | bool isDirty() const { return mDirtyCount > 0 || mZIsDirty; } |
| 184 | |
| 185 | // HWC2 Display functions |
| 186 | HWC2::Error acceptChanges(); |
| 187 | HWC2::Error createLayer(hwc2_layer_t* outLayerId); |
| 188 | HWC2::Error destroyLayer(hwc2_layer_t layerId); |
| 189 | HWC2::Error getActiveConfig(hwc2_config_t* outConfigId); |
| 190 | HWC2::Error getAttribute(hwc2_config_t configId, |
| 191 | HWC2::Attribute attribute, int32_t* outValue); |
| 192 | HWC2::Error getChangedCompositionTypes(uint32_t* outNumElements, |
| 193 | hwc2_layer_t* outLayers, int32_t* outTypes); |
| 194 | HWC2::Error getConfigs(uint32_t* outNumConfigs, |
| 195 | hwc2_config_t* outConfigIds); |
| 196 | HWC2::Error getDozeSupport(int32_t* outSupport); |
Dan Stoza | ed40eba | 2016-03-16 12:33:52 -0700 | [diff] [blame] | 197 | HWC2::Error getHdrCapabilities(uint32_t* outNumTypes, |
| 198 | int32_t* outTypes, float* outMaxLuminance, |
| 199 | float* outMaxAverageLuminance, float* outMinLuminance); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 200 | HWC2::Error getName(uint32_t* outSize, char* outName); |
| 201 | HWC2::Error getReleaseFences(uint32_t* outNumElements, |
| 202 | hwc2_layer_t* outLayers, int32_t* outFences); |
| 203 | HWC2::Error getRequests(int32_t* outDisplayRequests, |
| 204 | uint32_t* outNumElements, hwc2_layer_t* outLayers, |
| 205 | int32_t* outLayerRequests); |
| 206 | HWC2::Error getType(int32_t* outType); |
| 207 | HWC2::Error present(int32_t* outRetireFence); |
| 208 | HWC2::Error setActiveConfig(hwc2_config_t configId); |
| 209 | HWC2::Error setClientTarget(buffer_handle_t target, |
| 210 | int32_t acquireFence, int32_t dataspace); |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame^] | 211 | HWC2::Error setColorTransform(android_color_transform_t hint); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 212 | HWC2::Error setOutputBuffer(buffer_handle_t buffer, |
| 213 | int32_t releaseFence); |
| 214 | HWC2::Error setPowerMode(HWC2::PowerMode mode); |
| 215 | HWC2::Error setVsyncEnabled(HWC2::Vsync enabled); |
| 216 | HWC2::Error validate(uint32_t* outNumTypes, |
| 217 | uint32_t* outNumRequests); |
| 218 | |
| 219 | HWC2::Error updateLayerZ(hwc2_layer_t layerId, uint32_t z); |
| 220 | |
| 221 | // Read configs from HWC1 device |
| 222 | void populateConfigs(); |
| 223 | |
| 224 | // Set configs for a virtual display |
| 225 | void populateConfigs(uint32_t width, uint32_t height); |
| 226 | |
| 227 | bool prepare(); |
| 228 | HWC1Contents cloneRequestedContents() const; |
| 229 | void setReceivedContents(HWC1Contents contents); |
| 230 | bool hasChanges() const; |
| 231 | HWC2::Error set(hwc_display_contents_1& hwcContents); |
| 232 | void addRetireFence(int fenceFd); |
| 233 | void addReleaseFences(const hwc_display_contents_1& hwcContents); |
| 234 | |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame^] | 235 | bool hasColorTransform() const; |
| 236 | |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 237 | std::string dump() const; |
| 238 | |
| 239 | private: |
| 240 | class Config { |
| 241 | public: |
| 242 | Config(Display& display, hwc2_config_t id, uint32_t hwcId) |
| 243 | : mDisplay(display), |
| 244 | mId(id), |
Dan Stoza | fc4e202 | 2016-02-23 11:43:19 -0800 | [diff] [blame] | 245 | mHwcId(hwcId), |
| 246 | mAttributes() {} |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 247 | |
| 248 | bool isOnDisplay(const Display& display) const { |
| 249 | return display.getId() == mDisplay.getId(); |
| 250 | } |
| 251 | |
| 252 | hwc2_config_t getId() const { return mId; } |
| 253 | uint32_t getHwcId() const { return mHwcId; } |
| 254 | |
| 255 | void setAttribute(HWC2::Attribute attribute, int32_t value); |
| 256 | int32_t getAttribute(HWC2::Attribute attribute) const; |
| 257 | |
| 258 | std::string toString() const; |
| 259 | |
| 260 | private: |
| 261 | Display& mDisplay; |
| 262 | const hwc2_config_t mId; |
| 263 | const uint32_t mHwcId; |
| 264 | std::unordered_map<HWC2::Attribute, int32_t> mAttributes; |
| 265 | }; |
| 266 | |
| 267 | class Changes { |
| 268 | public: |
| 269 | uint32_t getNumTypes() const { |
| 270 | return static_cast<uint32_t>(mTypeChanges.size()); |
| 271 | } |
| 272 | |
| 273 | uint32_t getNumLayerRequests() const { |
| 274 | return static_cast<uint32_t>(mLayerRequests.size()); |
| 275 | } |
| 276 | |
| 277 | const std::unordered_map<hwc2_layer_t, HWC2::Composition>& |
| 278 | getTypeChanges() const { |
| 279 | return mTypeChanges; |
| 280 | } |
| 281 | |
| 282 | const std::unordered_map<hwc2_layer_t, HWC2::LayerRequest>& |
| 283 | getLayerRequests() const { |
| 284 | return mLayerRequests; |
| 285 | } |
| 286 | |
| 287 | int32_t getDisplayRequests() const { |
| 288 | int32_t requests = 0; |
| 289 | for (auto request : mDisplayRequests) { |
| 290 | requests |= static_cast<int32_t>(request); |
| 291 | } |
| 292 | return requests; |
| 293 | } |
| 294 | |
| 295 | void addTypeChange(hwc2_layer_t layerId, |
| 296 | HWC2::Composition type) { |
| 297 | mTypeChanges.insert({layerId, type}); |
| 298 | } |
| 299 | |
| 300 | void clearTypeChanges() { mTypeChanges.clear(); } |
| 301 | |
| 302 | void addLayerRequest(hwc2_layer_t layerId, |
| 303 | HWC2::LayerRequest request) { |
| 304 | mLayerRequests.insert({layerId, request}); |
| 305 | } |
| 306 | |
| 307 | private: |
| 308 | std::unordered_map<hwc2_layer_t, HWC2::Composition> |
| 309 | mTypeChanges; |
| 310 | std::unordered_map<hwc2_layer_t, HWC2::LayerRequest> |
| 311 | mLayerRequests; |
| 312 | std::unordered_set<HWC2::DisplayRequest> mDisplayRequests; |
| 313 | }; |
| 314 | |
| 315 | std::shared_ptr<const Config> |
| 316 | getConfig(hwc2_config_t configId) const; |
| 317 | |
| 318 | void reallocateHwc1Contents(); |
| 319 | void assignHwc1LayerIds(); |
| 320 | |
| 321 | void updateTypeChanges(const struct hwc_layer_1& hwc1Layer, |
| 322 | const Layer& layer); |
| 323 | void updateLayerRequests(const struct hwc_layer_1& hwc1Layer, |
| 324 | const Layer& layer); |
| 325 | |
| 326 | void prepareFramebufferTarget(); |
| 327 | |
| 328 | static std::atomic<hwc2_display_t> sNextId; |
| 329 | const hwc2_display_t mId; |
| 330 | HWC2On1Adapter& mDevice; |
| 331 | |
| 332 | std::atomic<size_t> mDirtyCount; |
| 333 | |
| 334 | // The state of this display should only be modified from |
| 335 | // SurfaceFlinger's main loop, with the exception of when dump is |
| 336 | // called. To prevent a bad state from crashing us during a dump |
| 337 | // call, all public calls into Display must acquire this mutex. |
| 338 | // |
| 339 | // It is recursive because we don't want to deadlock in validate |
| 340 | // (or present) when we call HWC2On1Adapter::prepareAllDisplays |
| 341 | // (or setAllDisplays), which calls back into Display functions |
| 342 | // which require locking. |
| 343 | mutable std::recursive_mutex mStateMutex; |
| 344 | |
| 345 | bool mZIsDirty; |
| 346 | HWC1Contents mHwc1RequestedContents; |
| 347 | HWC1Contents mHwc1ReceivedContents; |
| 348 | DeferredFence mRetireFence; |
| 349 | |
| 350 | // Will only be non-null after the layer has been validated but |
| 351 | // before it has been presented |
| 352 | std::unique_ptr<Changes> mChanges; |
| 353 | |
| 354 | int32_t mHwc1Id; |
| 355 | std::vector<std::shared_ptr<Config>> mConfigs; |
| 356 | std::shared_ptr<const Config> mActiveConfig; |
| 357 | std::string mName; |
| 358 | HWC2::DisplayType mType; |
| 359 | HWC2::PowerMode mPowerMode; |
| 360 | HWC2::Vsync mVsyncEnabled; |
| 361 | |
| 362 | FencedBuffer mClientTarget; |
| 363 | FencedBuffer mOutputBuffer; |
| 364 | |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame^] | 365 | bool mHasColorTransform; |
| 366 | |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 367 | std::multiset<std::shared_ptr<Layer>, SortLayersByZ> mLayers; |
| 368 | std::unordered_map<size_t, std::shared_ptr<Layer>> mHwc1LayerMap; |
| 369 | }; |
| 370 | |
| 371 | template <typename ...Args> |
| 372 | static int32_t callDisplayFunction(hwc2_device_t* device, |
| 373 | hwc2_display_t displayId, HWC2::Error (Display::*member)(Args...), |
| 374 | Args... args) { |
| 375 | auto display = getAdapter(device)->getDisplay(displayId); |
| 376 | if (!display) { |
| 377 | return static_cast<int32_t>(HWC2::Error::BadDisplay); |
| 378 | } |
| 379 | auto error = ((*display).*member)(std::forward<Args>(args)...); |
| 380 | return static_cast<int32_t>(error); |
| 381 | } |
| 382 | |
| 383 | template <typename MF, MF memFunc, typename ...Args> |
| 384 | static int32_t displayHook(hwc2_device_t* device, hwc2_display_t displayId, |
| 385 | Args... args) { |
| 386 | return HWC2On1Adapter::callDisplayFunction(device, displayId, memFunc, |
| 387 | std::forward<Args>(args)...); |
| 388 | } |
| 389 | |
| 390 | static int32_t getDisplayAttributeHook(hwc2_device_t* device, |
| 391 | hwc2_display_t display, hwc2_config_t config, |
| 392 | int32_t intAttribute, int32_t* outValue) { |
| 393 | auto attribute = static_cast<HWC2::Attribute>(intAttribute); |
| 394 | return callDisplayFunction(device, display, &Display::getAttribute, |
| 395 | config, attribute, outValue); |
| 396 | } |
| 397 | |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame^] | 398 | static int32_t setColorTransformHook(hwc2_device_t* device, |
| 399 | hwc2_display_t display, const float* /*matrix*/, |
| 400 | int32_t /*android_color_transform_t*/ intHint) { |
| 401 | // We intentionally throw away the matrix, because if the hint is |
| 402 | // anything other than IDENTITY, we have to fall back to client |
| 403 | // composition anyway |
| 404 | auto hint = static_cast<android_color_transform_t>(intHint); |
| 405 | return callDisplayFunction(device, display, &Display::setColorTransform, |
| 406 | hint); |
| 407 | } |
| 408 | |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 409 | static int32_t setPowerModeHook(hwc2_device_t* device, |
| 410 | hwc2_display_t display, int32_t intMode) { |
| 411 | auto mode = static_cast<HWC2::PowerMode>(intMode); |
| 412 | return callDisplayFunction(device, display, &Display::setPowerMode, |
| 413 | mode); |
| 414 | } |
| 415 | |
| 416 | static int32_t setVsyncEnabledHook(hwc2_device_t* device, |
| 417 | hwc2_display_t display, int32_t intEnabled) { |
| 418 | auto enabled = static_cast<HWC2::Vsync>(intEnabled); |
| 419 | return callDisplayFunction(device, display, &Display::setVsyncEnabled, |
| 420 | enabled); |
| 421 | } |
| 422 | |
| 423 | // Layer functions |
| 424 | |
| 425 | template <typename T> |
| 426 | class LatchedState { |
| 427 | public: |
| 428 | LatchedState(Layer& parent, T initialValue) |
| 429 | : mParent(parent), |
| 430 | mPendingValue(initialValue), |
| 431 | mValue(initialValue) {} |
| 432 | |
| 433 | void setPending(T value) { |
| 434 | if (value == mPendingValue) { |
| 435 | return; |
| 436 | } |
| 437 | if (mPendingValue == mValue) { |
| 438 | mParent.incDirty(); |
| 439 | } else if (value == mValue) { |
| 440 | mParent.decDirty(); |
| 441 | } |
| 442 | mPendingValue = value; |
| 443 | } |
| 444 | |
| 445 | T getValue() const { return mValue; } |
| 446 | T getPendingValue() const { return mPendingValue; } |
| 447 | |
| 448 | bool isDirty() const { return mPendingValue != mValue; } |
| 449 | |
| 450 | void latch() { |
| 451 | if (isDirty()) { |
| 452 | mValue = mPendingValue; |
| 453 | mParent.decDirty(); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | private: |
| 458 | Layer& mParent; |
| 459 | T mPendingValue; |
| 460 | T mValue; |
| 461 | }; |
| 462 | |
| 463 | class Layer { |
| 464 | public: |
| 465 | Layer(Display& display); |
| 466 | |
| 467 | bool operator==(const Layer& other) { return mId == other.mId; } |
| 468 | bool operator!=(const Layer& other) { return !(*this == other); } |
| 469 | |
| 470 | hwc2_layer_t getId() const { return mId; } |
| 471 | Display& getDisplay() const { return mDisplay; } |
| 472 | |
| 473 | void incDirty() { if (mDirtyCount++ == 0) mDisplay.incDirty(); } |
| 474 | void decDirty() { if (--mDirtyCount == 0) mDisplay.decDirty(); } |
| 475 | bool isDirty() const { return mDirtyCount > 0; } |
| 476 | |
| 477 | // HWC2 Layer functions |
| 478 | HWC2::Error setBuffer(buffer_handle_t buffer, int32_t acquireFence); |
| 479 | HWC2::Error setCursorPosition(int32_t x, int32_t y); |
| 480 | HWC2::Error setSurfaceDamage(hwc_region_t damage); |
| 481 | |
| 482 | // HWC2 Layer state functions |
| 483 | HWC2::Error setBlendMode(HWC2::BlendMode mode); |
| 484 | HWC2::Error setColor(hwc_color_t color); |
| 485 | HWC2::Error setCompositionType(HWC2::Composition type); |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame^] | 486 | HWC2::Error setDataspace(android_dataspace_t dataspace); |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 487 | HWC2::Error setDisplayFrame(hwc_rect_t frame); |
| 488 | HWC2::Error setPlaneAlpha(float alpha); |
| 489 | HWC2::Error setSidebandStream(const native_handle_t* stream); |
| 490 | HWC2::Error setSourceCrop(hwc_frect_t crop); |
| 491 | HWC2::Error setTransform(HWC2::Transform transform); |
| 492 | HWC2::Error setVisibleRegion(hwc_region_t visible); |
| 493 | HWC2::Error setZ(uint32_t z); |
| 494 | |
| 495 | HWC2::Composition getCompositionType() const { |
| 496 | return mCompositionType.getValue(); |
| 497 | } |
| 498 | uint32_t getZ() const { return mZ; } |
| 499 | |
| 500 | void addReleaseFence(int fenceFd); |
| 501 | const sp<Fence>& getReleaseFence() const; |
| 502 | |
| 503 | void setHwc1Id(size_t id) { mHwc1Id = id; } |
| 504 | size_t getHwc1Id() const { return mHwc1Id; } |
| 505 | |
| 506 | void applyState(struct hwc_layer_1& hwc1Layer, bool applyAllState); |
| 507 | |
| 508 | std::string dump() const; |
| 509 | |
| 510 | private: |
| 511 | void applyCommonState(struct hwc_layer_1& hwc1Layer, |
| 512 | bool applyAllState); |
| 513 | void applySolidColorState(struct hwc_layer_1& hwc1Layer, |
| 514 | bool applyAllState); |
| 515 | void applySidebandState(struct hwc_layer_1& hwc1Layer, |
| 516 | bool applyAllState); |
| 517 | void applyBufferState(struct hwc_layer_1& hwc1Layer); |
| 518 | void applyCompositionType(struct hwc_layer_1& hwc1Layer, |
| 519 | bool applyAllState); |
| 520 | |
| 521 | static std::atomic<hwc2_layer_t> sNextId; |
| 522 | const hwc2_layer_t mId; |
| 523 | Display& mDisplay; |
| 524 | size_t mDirtyCount; |
| 525 | |
| 526 | FencedBuffer mBuffer; |
| 527 | std::vector<hwc_rect_t> mSurfaceDamage; |
| 528 | |
| 529 | LatchedState<HWC2::BlendMode> mBlendMode; |
| 530 | LatchedState<hwc_color_t> mColor; |
| 531 | LatchedState<HWC2::Composition> mCompositionType; |
| 532 | LatchedState<hwc_rect_t> mDisplayFrame; |
| 533 | LatchedState<float> mPlaneAlpha; |
| 534 | LatchedState<const native_handle_t*> mSidebandStream; |
| 535 | LatchedState<hwc_frect_t> mSourceCrop; |
| 536 | LatchedState<HWC2::Transform> mTransform; |
| 537 | LatchedState<std::vector<hwc_rect_t>> mVisibleRegion; |
| 538 | uint32_t mZ; |
| 539 | |
| 540 | DeferredFence mReleaseFence; |
| 541 | |
| 542 | size_t mHwc1Id; |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame^] | 543 | bool mHasUnsupportedDataspace; |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 544 | bool mHasUnsupportedPlaneAlpha; |
| 545 | }; |
| 546 | |
| 547 | template <typename ...Args> |
| 548 | static int32_t callLayerFunction(hwc2_device_t* device, |
| 549 | hwc2_display_t displayId, hwc2_layer_t layerId, |
| 550 | HWC2::Error (Layer::*member)(Args...), Args... args) { |
| 551 | auto result = getAdapter(device)->getLayer(displayId, layerId); |
| 552 | auto error = std::get<HWC2::Error>(result); |
| 553 | if (error == HWC2::Error::None) { |
| 554 | auto layer = std::get<Layer*>(result); |
| 555 | error = ((*layer).*member)(std::forward<Args>(args)...); |
| 556 | } |
| 557 | return static_cast<int32_t>(error); |
| 558 | } |
| 559 | |
| 560 | template <typename MF, MF memFunc, typename ...Args> |
| 561 | static int32_t layerHook(hwc2_device_t* device, hwc2_display_t displayId, |
| 562 | hwc2_layer_t layerId, Args... args) { |
| 563 | return HWC2On1Adapter::callLayerFunction(device, displayId, layerId, |
| 564 | memFunc, std::forward<Args>(args)...); |
| 565 | } |
| 566 | |
| 567 | // Layer state functions |
| 568 | |
| 569 | static int32_t setLayerBlendModeHook(hwc2_device_t* device, |
| 570 | hwc2_display_t display, hwc2_layer_t layer, int32_t intMode) { |
| 571 | auto mode = static_cast<HWC2::BlendMode>(intMode); |
| 572 | return callLayerFunction(device, display, layer, |
| 573 | &Layer::setBlendMode, mode); |
| 574 | } |
| 575 | |
| 576 | static int32_t setLayerCompositionTypeHook(hwc2_device_t* device, |
| 577 | hwc2_display_t display, hwc2_layer_t layer, int32_t intType) { |
| 578 | auto type = static_cast<HWC2::Composition>(intType); |
| 579 | return callLayerFunction(device, display, layer, |
| 580 | &Layer::setCompositionType, type); |
| 581 | } |
| 582 | |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame^] | 583 | static int32_t setLayerDataspaceHook(hwc2_device_t* device, |
| 584 | hwc2_display_t display, hwc2_layer_t layer, int32_t intDataspace) { |
| 585 | auto dataspace = static_cast<android_dataspace_t>(intDataspace); |
| 586 | return callLayerFunction(device, display, layer, &Layer::setDataspace, |
| 587 | dataspace); |
| 588 | } |
| 589 | |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 590 | static int32_t setLayerTransformHook(hwc2_device_t* device, |
| 591 | hwc2_display_t display, hwc2_layer_t layer, int32_t intTransform) { |
| 592 | auto transform = static_cast<HWC2::Transform>(intTransform); |
| 593 | return callLayerFunction(device, display, layer, &Layer::setTransform, |
| 594 | transform); |
| 595 | } |
| 596 | |
| 597 | static int32_t setLayerZOrderHook(hwc2_device_t* device, |
| 598 | hwc2_display_t display, hwc2_layer_t layer, uint32_t z) { |
| 599 | return callDisplayFunction(device, display, &Display::updateLayerZ, |
| 600 | layer, z); |
| 601 | } |
| 602 | |
| 603 | // Adapter internals |
| 604 | |
| 605 | void populateCapabilities(); |
| 606 | Display* getDisplay(hwc2_display_t id); |
| 607 | std::tuple<Layer*, HWC2::Error> getLayer(hwc2_display_t displayId, |
| 608 | hwc2_layer_t layerId); |
| 609 | void populatePrimary(); |
| 610 | |
| 611 | bool prepareAllDisplays(); |
| 612 | std::vector<struct hwc_display_contents_1*> mHwc1Contents; |
| 613 | HWC2::Error setAllDisplays(); |
| 614 | |
| 615 | void hwc1Invalidate(); |
| 616 | void hwc1Vsync(int hwc1DisplayId, int64_t timestamp); |
| 617 | void hwc1Hotplug(int hwc1DisplayId, int connected); |
| 618 | |
| 619 | // These are set in the constructor and before any asynchronous events are |
| 620 | // possible |
| 621 | |
| 622 | struct hwc_composer_device_1* const mHwc1Device; |
| 623 | const uint8_t mHwc1MinorVersion; |
| 624 | bool mHwc1SupportsVirtualDisplays; |
| 625 | |
| 626 | class Callbacks; |
| 627 | const std::unique_ptr<Callbacks> mHwc1Callbacks; |
| 628 | |
| 629 | std::unordered_set<HWC2::Capability> mCapabilities; |
| 630 | |
| 631 | // These are only accessed from the main SurfaceFlinger thread (not from |
| 632 | // callbacks or dump |
| 633 | |
| 634 | std::map<hwc2_layer_t, std::shared_ptr<Layer>> mLayers; |
| 635 | std::shared_ptr<Display> mHwc1VirtualDisplay; |
| 636 | |
| 637 | // These are potentially accessed from multiple threads, and are protected |
Dan Stoza | fc4e202 | 2016-02-23 11:43:19 -0800 | [diff] [blame] | 638 | // by this mutex. This needs to be recursive, since the HWC1 implementation |
| 639 | // can call back into the invalidate callback on the same thread that is |
| 640 | // calling prepare. |
| 641 | std::recursive_timed_mutex mStateMutex; |
Dan Stoza | c6998d2 | 2015-09-24 17:03:36 -0700 | [diff] [blame] | 642 | |
| 643 | struct CallbackInfo { |
| 644 | hwc2_callback_data_t data; |
| 645 | hwc2_function_pointer_t pointer; |
| 646 | }; |
| 647 | std::unordered_map<HWC2::Callback, CallbackInfo> mCallbacks; |
| 648 | bool mHasPendingInvalidate; |
| 649 | std::vector<std::pair<int, int64_t>> mPendingVsyncs; |
| 650 | std::vector<std::pair<int, int>> mPendingHotplugs; |
| 651 | |
| 652 | std::map<hwc2_display_t, std::shared_ptr<Display>> mDisplays; |
| 653 | std::unordered_map<int, hwc2_display_t> mHwc1DisplayMap; |
| 654 | }; |
| 655 | |
| 656 | } // namespace android |
| 657 | |
| 658 | #endif |