Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #pragma once |
| 18 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 19 | #include "BufferLayer.h" |
| 20 | #include "Layer.h" |
| 21 | |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 22 | #include <renderengine/Image.h> |
| 23 | #include <renderengine/RenderEngine.h> |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 24 | #include <system/window.h> |
| 25 | #include <utils/String8.h> |
| 26 | |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 27 | #include <stack> |
| 28 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | |
Valerie Hau | 6449968 | 2019-04-10 11:04:29 -0700 | [diff] [blame] | 31 | class SlotGenerationTest; |
| 32 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 33 | class BufferStateLayer : public BufferLayer { |
| 34 | public: |
Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 35 | explicit BufferStateLayer(const LayerCreationArgs&); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 36 | |
Alec Mouri | 4545a8a | 2019-08-08 20:05:32 -0700 | [diff] [blame] | 37 | ~BufferStateLayer() override; |
| 38 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 39 | // ----------------------------------------------------------------------- |
| 40 | // Interface implementation for Layer |
| 41 | // ----------------------------------------------------------------------- |
chaviw | 8a01fa4 | 2019-08-19 12:39:31 -0700 | [diff] [blame] | 42 | const char* getType() const override { return "BufferStateLayer"; } |
| 43 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 44 | void onLayerDisplayed(const sp<Fence>& releaseFence) override; |
| 45 | void setTransformHint(uint32_t orientation) const override; |
| 46 | void releasePendingBuffer(nsecs_t dequeueReadyTime) override; |
| 47 | |
Ana Krulec | 010d219 | 2018-10-08 06:29:54 -0700 | [diff] [blame] | 48 | bool shouldPresentNow(nsecs_t expectedPresentTime) const override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 49 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 50 | uint32_t doTransactionResize(uint32_t flags, Layer::State* /*stateToCommit*/) override { |
| 51 | return flags; |
| 52 | } |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 53 | void pushPendingState() override; |
| 54 | bool applyPendingStates(Layer::State* stateToCommit) override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 55 | |
| 56 | uint32_t getActiveWidth(const Layer::State& s) const override { return s.active.w; } |
| 57 | uint32_t getActiveHeight(const Layer::State& s) const override { return s.active.h; } |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 58 | ui::Transform getActiveTransform(const Layer::State& s) const override { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 59 | return s.active.transform; |
| 60 | } |
| 61 | Region getActiveTransparentRegion(const Layer::State& s) const override { |
| 62 | return s.transparentRegionHint; |
| 63 | } |
| 64 | Rect getCrop(const Layer::State& s) const; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 65 | |
Valerie Hau | 1acd696 | 2019-10-28 16:35:48 -0700 | [diff] [blame] | 66 | uint32_t getTransformHint() const { return mTransformHint; } |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 67 | bool setTransform(uint32_t transform) override; |
| 68 | bool setTransformToDisplayInverse(bool transformToDisplayInverse) override; |
| 69 | bool setCrop(const Rect& crop) override; |
| 70 | bool setFrame(const Rect& frame) override; |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 71 | bool setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime, nsecs_t desiredPresentTime, |
| 72 | const client_cache_t& clientCacheId) override; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 73 | bool setAcquireFence(const sp<Fence>& fence) override; |
| 74 | bool setDataspace(ui::Dataspace dataspace) override; |
| 75 | bool setHdrMetadata(const HdrMetadata& hdrMetadata) override; |
| 76 | bool setSurfaceDamageRegion(const Region& surfaceDamage) override; |
| 77 | bool setApi(int32_t api) override; |
| 78 | bool setSidebandStream(const sp<NativeHandle>& sidebandStream) override; |
| 79 | bool setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) override; |
Valerie Hau | 7618b23 | 2020-01-09 16:03:08 -0800 | [diff] [blame] | 80 | void forceSendCallbacks() override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 81 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 82 | // Override to ignore legacy layer state properties that are not used by BufferStateLayer |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 83 | bool setSize(uint32_t /*w*/, uint32_t /*h*/) override { return false; } |
chaviw | 214c89d | 2019-09-04 16:03:53 -0700 | [diff] [blame] | 84 | bool setPosition(float /*x*/, float /*y*/) override { return false; } |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 85 | bool setTransparentRegionHint(const Region& transparent) override; |
| 86 | bool setMatrix(const layer_state_t::matrix22_t& /*matrix*/, |
| 87 | bool /*allowNonRectPreservingTransforms*/) override { |
| 88 | return false; |
| 89 | } |
chaviw | 214c89d | 2019-09-04 16:03:53 -0700 | [diff] [blame] | 90 | bool setCrop_legacy(const Rect& /*crop*/) override { return false; } |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 91 | bool setOverrideScalingMode(int32_t /*overrideScalingMode*/) override { return false; } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 92 | void deferTransactionUntil_legacy(const sp<IBinder>& /*barrierHandle*/, |
| 93 | uint64_t /*frameNumber*/) override {} |
| 94 | void deferTransactionUntil_legacy(const sp<Layer>& /*barrierLayer*/, |
| 95 | uint64_t /*frameNumber*/) override {} |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 96 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 97 | Rect getBufferSize(const State& s) const override; |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 98 | FloatRect computeSourceBounds(const FloatRect& parentBounds) const override; |
Valerie Hau | bc6ddb1 | 2019-03-08 11:10:15 -0800 | [diff] [blame] | 99 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 100 | // ----------------------------------------------------------------------- |
| 101 | |
| 102 | // ----------------------------------------------------------------------- |
| 103 | // Interface implementation for BufferLayer |
| 104 | // ----------------------------------------------------------------------- |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 105 | bool fenceHasSignaled() const override; |
Dominik Laskowski | a8955dd | 2019-07-10 10:19:09 -0700 | [diff] [blame] | 106 | bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 107 | |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 108 | protected: |
| 109 | void gatherBufferInfo() override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 110 | |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 111 | private: |
Dominik Laskowski | a8955dd | 2019-07-10 10:19:09 -0700 | [diff] [blame] | 112 | uint64_t getFrameNumber(nsecs_t expectedPresentTime) const override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 113 | |
| 114 | bool getAutoRefresh() const override; |
| 115 | bool getSidebandStreamChanged() const override; |
| 116 | |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 117 | bool latchSidebandStream(bool& recomputeVisibleRegions) override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 118 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 119 | bool hasFrameUpdate() const override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 120 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 121 | status_t bindTextureImage() override; |
Dominik Laskowski | a8955dd | 2019-07-10 10:19:09 -0700 | [diff] [blame] | 122 | status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime, |
| 123 | nsecs_t expectedPresentTime) override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 124 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 125 | status_t updateActiveBuffer() override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 126 | status_t updateFrameNumber(nsecs_t latchTime) override; |
| 127 | |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 128 | void latchPerFrameState(compositionengine::LayerFECompositionState&) const override; |
chaviw | b4c6e58 | 2019-08-16 14:35:07 -0700 | [diff] [blame] | 129 | sp<Layer> createClone() override; |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 130 | |
chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 131 | // Crop that applies to the buffer |
| 132 | Rect computeCrop(const State& s); |
| 133 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 134 | private: |
Valerie Hau | 6449968 | 2019-04-10 11:04:29 -0700 | [diff] [blame] | 135 | friend class SlotGenerationTest; |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 136 | bool willPresentCurrentTransaction() const; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 137 | |
Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 138 | static const std::array<float, 16> IDENTITY_MATRIX; |
| 139 | |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 140 | std::unique_ptr<renderengine::Image> mTextureImage; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 141 | |
Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 142 | std::atomic<bool> mSidebandStreamChanged{false}; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 143 | |
Valerie Hau | da03256 | 2020-01-22 12:22:57 -0800 | [diff] [blame^] | 144 | mutable uint64_t mFrameNumber{0}; |
Valerie Hau | 2f54d64 | 2020-01-22 09:37:03 -0800 | [diff] [blame] | 145 | uint64_t mFrameCounter{0}; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 146 | |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 147 | sp<Fence> mPreviousReleaseFence; |
Mikael Pessa | 2e1608f | 2019-07-19 11:25:35 -0700 | [diff] [blame] | 148 | uint64_t mPreviousBufferId = 0; |
Mikael Pessa | 2e1608f | 2019-07-19 11:25:35 -0700 | [diff] [blame] | 149 | uint64_t mPreviousReleasedFrameNumber = 0; |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 150 | |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 151 | mutable bool mCurrentStateModified = false; |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 152 | bool mReleasePreviousBuffer = false; |
| 153 | nsecs_t mCallbackHandleAcquireTime = -1; |
Marissa Wall | 024a191 | 2018-08-13 13:55:35 -0700 | [diff] [blame] | 154 | |
Valerie Hau | 32cdc1f | 2019-10-21 14:45:54 -0700 | [diff] [blame] | 155 | mutable uint32_t mTransformHint = 0; |
| 156 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 157 | // TODO(marissaw): support sticky transform for LEGACY camera mode |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 158 | |
| 159 | class HwcSlotGenerator : public ClientCache::ErasedRecipient { |
| 160 | public: |
| 161 | HwcSlotGenerator() { |
| 162 | for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { |
| 163 | mFreeHwcCacheSlots.push(i); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void bufferErased(const client_cache_t& clientCacheId); |
| 168 | |
| 169 | uint32_t getHwcCacheSlot(const client_cache_t& clientCacheId); |
| 170 | |
| 171 | private: |
Valerie Hau | 6449968 | 2019-04-10 11:04:29 -0700 | [diff] [blame] | 172 | friend class SlotGenerationTest; |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 173 | uint32_t addCachedBuffer(const client_cache_t& clientCacheId) REQUIRES(mMutex); |
| 174 | uint32_t getFreeHwcCacheSlot() REQUIRES(mMutex); |
| 175 | void evictLeastRecentlyUsed() REQUIRES(mMutex); |
| 176 | void eraseBufferLocked(const client_cache_t& clientCacheId) REQUIRES(mMutex); |
| 177 | |
| 178 | struct CachedBufferHash { |
| 179 | std::size_t operator()(const client_cache_t& clientCacheId) const { |
| 180 | return std::hash<uint64_t>{}(clientCacheId.id); |
| 181 | } |
| 182 | }; |
| 183 | |
| 184 | std::mutex mMutex; |
| 185 | |
| 186 | std::unordered_map<client_cache_t, |
| 187 | std::pair<uint32_t /*HwcCacheSlot*/, uint32_t /*counter*/>, |
| 188 | CachedBufferHash> |
| 189 | mCachedBuffers GUARDED_BY(mMutex); |
| 190 | std::stack<uint32_t /*HwcCacheSlot*/> mFreeHwcCacheSlots GUARDED_BY(mMutex); |
| 191 | |
| 192 | // The cache increments this counter value when a slot is updated or used. |
| 193 | // Used to track the least recently-used buffer |
| 194 | uint64_t mCounter = 0; |
| 195 | }; |
| 196 | |
| 197 | sp<HwcSlotGenerator> mHwcSlotGenerator; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | } // namespace android |