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 | |
| 22 | #include <gui/GLConsumer.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 23 | #include <renderengine/Image.h> |
| 24 | #include <renderengine/RenderEngine.h> |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 25 | #include <system/window.h> |
| 26 | #include <utils/String8.h> |
| 27 | |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 28 | #include <stack> |
| 29 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | |
Valerie Hau | 6449968 | 2019-04-10 11:04:29 -0700 | [diff] [blame] | 32 | class SlotGenerationTest; |
| 33 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 34 | class BufferStateLayer : public BufferLayer { |
| 35 | public: |
Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 36 | explicit BufferStateLayer(const LayerCreationArgs&); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 37 | |
Alec Mouri | 4545a8a | 2019-08-08 20:05:32 -0700 | [diff] [blame] | 38 | ~BufferStateLayer() override; |
| 39 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 40 | // ----------------------------------------------------------------------- |
| 41 | // Interface implementation for Layer |
| 42 | // ----------------------------------------------------------------------- |
| 43 | void onLayerDisplayed(const sp<Fence>& releaseFence) override; |
| 44 | void setTransformHint(uint32_t orientation) const override; |
| 45 | void releasePendingBuffer(nsecs_t dequeueReadyTime) override; |
| 46 | |
Ana Krulec | 010d219 | 2018-10-08 06:29:54 -0700 | [diff] [blame] | 47 | bool shouldPresentNow(nsecs_t expectedPresentTime) const override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 48 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 49 | bool getTransformToDisplayInverse() const override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 50 | |
| 51 | uint32_t doTransactionResize(uint32_t flags, Layer::State* /*stateToCommit*/) override { |
| 52 | return flags; |
| 53 | } |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 54 | void pushPendingState() override; |
| 55 | bool applyPendingStates(Layer::State* stateToCommit) override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 56 | |
| 57 | uint32_t getActiveWidth(const Layer::State& s) const override { return s.active.w; } |
| 58 | 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] | 59 | ui::Transform getActiveTransform(const Layer::State& s) const override { |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 60 | return s.active.transform; |
| 61 | } |
| 62 | Region getActiveTransparentRegion(const Layer::State& s) const override { |
| 63 | return s.transparentRegionHint; |
| 64 | } |
| 65 | Rect getCrop(const Layer::State& s) const; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 66 | |
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; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 80 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 81 | // 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] | 82 | bool setSize(uint32_t /*w*/, uint32_t /*h*/) override { return false; } |
| 83 | bool setPosition(float /*x*/, float /*y*/, bool /*immediate*/) override { return false; } |
| 84 | bool setTransparentRegionHint(const Region& transparent) override; |
| 85 | bool setMatrix(const layer_state_t::matrix22_t& /*matrix*/, |
| 86 | bool /*allowNonRectPreservingTransforms*/) override { |
| 87 | return false; |
| 88 | } |
| 89 | bool setCrop_legacy(const Rect& /*crop*/, bool /*immediate*/) override { return false; } |
| 90 | bool setOverrideScalingMode(int32_t /*overrideScalingMode*/) override { return false; } |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 91 | void deferTransactionUntil_legacy(const sp<IBinder>& /*barrierHandle*/, |
| 92 | uint64_t /*frameNumber*/) override {} |
| 93 | void deferTransactionUntil_legacy(const sp<Layer>& /*barrierLayer*/, |
| 94 | uint64_t /*frameNumber*/) override {} |
Marissa Wall | 861616d | 2018-10-22 12:52:23 -0700 | [diff] [blame] | 95 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 96 | Rect getBufferSize(const State& s) const override; |
Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 97 | FloatRect computeSourceBounds(const FloatRect& parentBounds) const override; |
Valerie Hau | bc6ddb1 | 2019-03-08 11:10:15 -0800 | [diff] [blame] | 98 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 99 | // ----------------------------------------------------------------------- |
| 100 | |
| 101 | // ----------------------------------------------------------------------- |
| 102 | // Interface implementation for BufferLayer |
| 103 | // ----------------------------------------------------------------------- |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 104 | bool fenceHasSignaled() const override; |
Dominik Laskowski | a8955dd | 2019-07-10 10:19:09 -0700 | [diff] [blame] | 105 | bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 106 | |
| 107 | private: |
| 108 | nsecs_t getDesiredPresentTime() override; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 109 | std::shared_ptr<FenceTime> getCurrentFenceTime() const override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 110 | |
| 111 | void getDrawingTransformMatrix(float *matrix) override; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 112 | uint32_t getDrawingTransform() const override; |
| 113 | ui::Dataspace getDrawingDataSpace() const override; |
| 114 | Rect getDrawingCrop() const override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 115 | uint32_t getDrawingScalingMode() const override; |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 116 | Region getDrawingSurfaceDamage() const override; |
| 117 | const HdrMetadata& getDrawingHdrMetadata() const override; |
| 118 | int getDrawingApi() const override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 119 | PixelFormat getPixelFormat() const override; |
| 120 | |
Dominik Laskowski | a8955dd | 2019-07-10 10:19:09 -0700 | [diff] [blame] | 121 | uint64_t getFrameNumber(nsecs_t expectedPresentTime) const override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 122 | |
| 123 | bool getAutoRefresh() const override; |
| 124 | bool getSidebandStreamChanged() const override; |
| 125 | |
Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 126 | bool latchSidebandStream(bool& recomputeVisibleRegions) override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 127 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 128 | bool hasFrameUpdate() const override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 129 | |
| 130 | void setFilteringEnabled(bool enabled) override; |
| 131 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 132 | status_t bindTextureImage() override; |
Dominik Laskowski | a8955dd | 2019-07-10 10:19:09 -0700 | [diff] [blame] | 133 | status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime, |
| 134 | nsecs_t expectedPresentTime) override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 135 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 136 | status_t updateActiveBuffer() override; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 137 | status_t updateFrameNumber(nsecs_t latchTime) override; |
| 138 | |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 139 | void latchPerFrameState(compositionengine::LayerFECompositionState&) const override; |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 140 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 141 | private: |
Valerie Hau | 6449968 | 2019-04-10 11:04:29 -0700 | [diff] [blame] | 142 | friend class SlotGenerationTest; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 143 | void onFirstRef() override; |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 144 | bool willPresentCurrentTransaction() const; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 145 | |
Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 146 | static const std::array<float, 16> IDENTITY_MATRIX; |
| 147 | |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 148 | std::unique_ptr<renderengine::Image> mTextureImage; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 149 | |
Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 150 | std::array<float, 16> mTransformMatrix{IDENTITY_MATRIX}; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 151 | |
Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 152 | std::atomic<bool> mSidebandStreamChanged{false}; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 153 | |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 154 | mutable uint32_t mFrameNumber{0}; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 155 | |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 156 | sp<Fence> mPreviousReleaseFence; |
| 157 | |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 158 | mutable bool mCurrentStateModified = false; |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 159 | bool mReleasePreviousBuffer = false; |
| 160 | nsecs_t mCallbackHandleAcquireTime = -1; |
Marissa Wall | 024a191 | 2018-08-13 13:55:35 -0700 | [diff] [blame] | 161 | |
Valerie Hau | bc6ddb1 | 2019-03-08 11:10:15 -0800 | [diff] [blame] | 162 | nsecs_t mDesiredPresentTime = -1; |
| 163 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 164 | // TODO(marissaw): support sticky transform for LEGACY camera mode |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 165 | |
| 166 | class HwcSlotGenerator : public ClientCache::ErasedRecipient { |
| 167 | public: |
| 168 | HwcSlotGenerator() { |
| 169 | for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { |
| 170 | mFreeHwcCacheSlots.push(i); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void bufferErased(const client_cache_t& clientCacheId); |
| 175 | |
| 176 | uint32_t getHwcCacheSlot(const client_cache_t& clientCacheId); |
| 177 | |
| 178 | private: |
Valerie Hau | 6449968 | 2019-04-10 11:04:29 -0700 | [diff] [blame] | 179 | friend class SlotGenerationTest; |
Marissa Wall | 947d34e | 2019-03-29 14:03:53 -0700 | [diff] [blame] | 180 | uint32_t addCachedBuffer(const client_cache_t& clientCacheId) REQUIRES(mMutex); |
| 181 | uint32_t getFreeHwcCacheSlot() REQUIRES(mMutex); |
| 182 | void evictLeastRecentlyUsed() REQUIRES(mMutex); |
| 183 | void eraseBufferLocked(const client_cache_t& clientCacheId) REQUIRES(mMutex); |
| 184 | |
| 185 | struct CachedBufferHash { |
| 186 | std::size_t operator()(const client_cache_t& clientCacheId) const { |
| 187 | return std::hash<uint64_t>{}(clientCacheId.id); |
| 188 | } |
| 189 | }; |
| 190 | |
| 191 | std::mutex mMutex; |
| 192 | |
| 193 | std::unordered_map<client_cache_t, |
| 194 | std::pair<uint32_t /*HwcCacheSlot*/, uint32_t /*counter*/>, |
| 195 | CachedBufferHash> |
| 196 | mCachedBuffers GUARDED_BY(mMutex); |
| 197 | std::stack<uint32_t /*HwcCacheSlot*/> mFreeHwcCacheSlots GUARDED_BY(mMutex); |
| 198 | |
| 199 | // The cache increments this counter value when a slot is updated or used. |
| 200 | // Used to track the least recently-used buffer |
| 201 | uint64_t mCounter = 0; |
| 202 | }; |
| 203 | |
| 204 | sp<HwcSlotGenerator> mHwcSlotGenerator; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | } // namespace android |