| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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_GUI_BLAST_BUFFER_QUEUE_H |
| 18 | #define ANDROID_GUI_BLAST_BUFFER_QUEUE_H |
| 19 | |
| 20 | #include <gui/IGraphicBufferProducer.h> |
| 21 | #include <gui/BufferItemConsumer.h> |
| 22 | #include <gui/BufferItem.h> |
| 23 | #include <gui/SurfaceComposerClient.h> |
| 24 | |
| 25 | #include <utils/Condition.h> |
| 26 | #include <utils/Mutex.h> |
| 27 | #include <utils/RefBase.h> |
| 28 | |
| 29 | #include <system/window.h> |
| Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 30 | #include <thread> |
| Jorim Jaggi | a3fe67b | 2020-12-01 00:24:33 +0100 | [diff] [blame] | 31 | #include <queue> |
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | |
| Hongguang Chen | 621ec58 | 2021-02-16 15:42:35 -0800 | [diff] [blame^] | 35 | class BLASTBufferQueue; |
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 36 | class BufferItemConsumer; |
| 37 | |
| Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 38 | class BLASTBufferItemConsumer : public BufferItemConsumer { |
| 39 | public: |
| 40 | BLASTBufferItemConsumer(const sp<IGraphicBufferConsumer>& consumer, uint64_t consumerUsage, |
| 41 | int bufferCount, bool controlledByApp) |
| 42 | : BufferItemConsumer(consumer, consumerUsage, bufferCount, controlledByApp), |
| 43 | mCurrentlyConnected(false), |
| Hongguang Chen | 621ec58 | 2021-02-16 15:42:35 -0800 | [diff] [blame^] | 44 | mPreviouslyConnected(false), |
| 45 | mBLASTBufferQueue(nullptr) {} |
| Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 46 | |
| 47 | void onDisconnect() override; |
| 48 | void addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps, |
| Hongguang Chen | 621ec58 | 2021-02-16 15:42:35 -0800 | [diff] [blame^] | 49 | FrameEventHistoryDelta* outDelta) override REQUIRES(mMutex); |
| Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 50 | void updateFrameTimestamps(uint64_t frameNumber, nsecs_t refreshStartTime, |
| 51 | const sp<Fence>& gpuCompositionDoneFence, |
| 52 | const sp<Fence>& presentFence, const sp<Fence>& prevReleaseFence, |
| 53 | CompositorTiming compositorTiming, nsecs_t latchTime, |
| Hongguang Chen | 621ec58 | 2021-02-16 15:42:35 -0800 | [diff] [blame^] | 54 | nsecs_t dequeueReadyTime) REQUIRES(mMutex); |
| Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 55 | void getConnectionEvents(uint64_t frameNumber, bool* needsDisconnect); |
| Hongguang Chen | 621ec58 | 2021-02-16 15:42:35 -0800 | [diff] [blame^] | 56 | void setBlastBufferQueue(BLASTBufferQueue* blastbufferqueue) REQUIRES(mMutex); |
| 57 | |
| 58 | protected: |
| 59 | void onSidebandStreamChanged() override REQUIRES(mMutex); |
| Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 60 | |
| 61 | private: |
| 62 | uint64_t mCurrentFrameNumber = 0; |
| 63 | |
| Hongguang Chen | 621ec58 | 2021-02-16 15:42:35 -0800 | [diff] [blame^] | 64 | Mutex mMutex; |
| 65 | ConsumerFrameEventHistory mFrameEventHistory GUARDED_BY(mMutex); |
| 66 | std::queue<uint64_t> mDisconnectEvents GUARDED_BY(mMutex); |
| 67 | bool mCurrentlyConnected GUARDED_BY(mMutex); |
| 68 | bool mPreviouslyConnected GUARDED_BY(mMutex); |
| 69 | BLASTBufferQueue* mBLASTBufferQueue GUARDED_BY(mMutex); |
| Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 70 | }; |
| 71 | |
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 72 | class BLASTBufferQueue |
| 73 | : public ConsumerBase::FrameAvailableListener, public BufferItemConsumer::BufferFreedListener |
| 74 | { |
| 75 | public: |
| Vishnu Nair | dab9409 | 2020-09-29 16:09:04 -0700 | [diff] [blame] | 76 | BLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface, int width, |
| chaviw | 565ee54 | 2021-01-14 10:21:23 -0800 | [diff] [blame] | 77 | int height, int32_t format, bool enableTripleBuffering = true); |
| Robert Carr | cedef05 | 2020-04-22 15:58:23 -0700 | [diff] [blame] | 78 | |
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 79 | sp<IGraphicBufferProducer> getIGraphicBufferProducer() const { |
| 80 | return mProducer; |
| 81 | } |
| Vishnu Nair | 992496b | 2020-10-22 17:27:21 -0700 | [diff] [blame] | 82 | sp<Surface> getSurface(bool includeSurfaceControlHandle); |
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 83 | |
| 84 | void onBufferFreed(const wp<GraphicBuffer>&/* graphicBuffer*/) override { /* TODO */ } |
| Vishnu Nair | aef1de9 | 2020-10-22 12:15:53 -0700 | [diff] [blame] | 85 | void onFrameReplaced(const BufferItem& item) override; |
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 86 | void onFrameAvailable(const BufferItem& item) override; |
| Vishnu Nair | adf632b | 2021-01-07 14:05:08 -0800 | [diff] [blame] | 87 | void onFrameDequeued(const uint64_t) override; |
| 88 | void onFrameCancelled(const uint64_t) override; |
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 89 | |
| 90 | void transactionCallback(nsecs_t latchTime, const sp<Fence>& presentFence, |
| 91 | const std::vector<SurfaceControlStats>& stats); |
| 92 | void setNextTransaction(SurfaceComposerClient::Transaction *t); |
| Vishnu Nair | c4a40c1 | 2020-12-23 09:14:32 -0800 | [diff] [blame] | 93 | void mergeWithNextTransaction(SurfaceComposerClient::Transaction* t, uint64_t frameNumber); |
| chaviw | 71c2cc4 | 2020-10-23 16:42:02 -0700 | [diff] [blame] | 94 | void setTransactionCompleteCallback(uint64_t frameNumber, |
| 95 | std::function<void(int64_t)>&& transactionCompleteCallback); |
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 96 | |
| chaviw | 565ee54 | 2021-01-14 10:21:23 -0800 | [diff] [blame] | 97 | void update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height, int32_t format); |
| Vishnu Nair | 7eb670a | 2020-10-15 12:16:10 -0700 | [diff] [blame] | 98 | void flushShadowQueue() { mFlushShadowQueue = true; } |
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 99 | |
| Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 100 | status_t setFrameRate(float frameRate, int8_t compatibility, bool shouldBeSeamless); |
| Siarhei Vishniakou | fc434ac | 2021-01-13 10:28:00 -1000 | [diff] [blame] | 101 | status_t setFrameTimelineInfo(const FrameTimelineInfo& info); |
| Robert Carr | 9c006e0 | 2020-10-14 13:41:57 -0700 | [diff] [blame] | 102 | |
| Hongguang Chen | 621ec58 | 2021-02-16 15:42:35 -0800 | [diff] [blame^] | 103 | void setSidebandStream(const sp<NativeHandle>& stream); |
| 104 | |
| Vishnu Nair | c4a40c1 | 2020-12-23 09:14:32 -0800 | [diff] [blame] | 105 | virtual ~BLASTBufferQueue(); |
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 106 | |
| 107 | private: |
| Valerie Hau | c5011f9 | 2019-10-11 09:52:07 -0700 | [diff] [blame] | 108 | friend class BLASTBufferQueueHelper; |
| 109 | |
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 110 | // can't be copied |
| 111 | BLASTBufferQueue& operator = (const BLASTBufferQueue& rhs); |
| 112 | BLASTBufferQueue(const BLASTBufferQueue& rhs); |
| Vishnu Nair | 8949612 | 2020-12-14 17:14:53 -0800 | [diff] [blame] | 113 | void createBufferQueue(sp<IGraphicBufferProducer>* outProducer, |
| 114 | sp<IGraphicBufferConsumer>* outConsumer); |
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 115 | |
| Robert Carr | 255acdc | 2020-04-17 14:08:55 -0700 | [diff] [blame] | 116 | void processNextBufferLocked(bool useNextTransaction) REQUIRES(mMutex); |
| Vishnu Nair | bf25577 | 2020-10-16 10:54:41 -0700 | [diff] [blame] | 117 | Rect computeCrop(const BufferItem& item) REQUIRES(mMutex); |
| Vishnu Nair | 670b3f7 | 2020-09-29 17:52:18 -0700 | [diff] [blame] | 118 | // Return true if we need to reject the buffer based on the scaling mode and the buffer size. |
| Vishnu Nair | ea0de00 | 2020-11-17 17:42:37 -0800 | [diff] [blame] | 119 | bool rejectBuffer(const BufferItem& item) REQUIRES(mMutex); |
| Vishnu Nair | 8b30dd1 | 2021-01-25 14:16:54 -0800 | [diff] [blame] | 120 | bool maxBuffersAcquired(bool includeExtraAcquire) const REQUIRES(mMutex); |
| chaviw | 497e81c | 2021-02-04 17:09:47 -0800 | [diff] [blame] | 121 | static PixelFormat convertBufferFormat(PixelFormat& format); |
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 122 | |
| Vishnu Nair | dab9409 | 2020-09-29 16:09:04 -0700 | [diff] [blame] | 123 | std::string mName; |
| Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 124 | sp<SurfaceControl> mSurfaceControl; |
| 125 | |
| 126 | std::mutex mMutex; |
| 127 | std::condition_variable mCallbackCV; |
| Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 128 | |
| Valerie Hau | 65b8e87 | 2020-02-13 09:45:14 -0800 | [diff] [blame] | 129 | // BufferQueue internally allows 1 more than |
| 130 | // the max to be acquired |
| 131 | static const int MAX_ACQUIRED_BUFFERS = 1; |
| Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 132 | |
| 133 | int32_t mNumFrameAvailable GUARDED_BY(mMutex); |
| 134 | int32_t mNumAcquired GUARDED_BY(mMutex); |
| Vishnu Nair | 670b3f7 | 2020-09-29 17:52:18 -0700 | [diff] [blame] | 135 | bool mInitialCallbackReceived GUARDED_BY(mMutex) = false; |
| Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 136 | struct PendingReleaseItem { |
| 137 | BufferItem item; |
| 138 | sp<Fence> releaseFence; |
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 139 | }; |
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 140 | |
| Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 141 | std::queue<const BufferItem> mSubmitted GUARDED_BY(mMutex); |
| Vishnu Nair | dab9409 | 2020-09-29 16:09:04 -0700 | [diff] [blame] | 142 | // Keep a reference to the currently presented buffer so we can release it when the next buffer |
| 143 | // is ready to be presented. |
| Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 144 | PendingReleaseItem mPendingReleaseItem GUARDED_BY(mMutex); |
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 145 | |
| Vishnu Nair | ea0de00 | 2020-11-17 17:42:37 -0800 | [diff] [blame] | 146 | ui::Size mSize GUARDED_BY(mMutex); |
| 147 | ui::Size mRequestedSize GUARDED_BY(mMutex); |
| chaviw | 565ee54 | 2021-01-14 10:21:23 -0800 | [diff] [blame] | 148 | int32_t mFormat GUARDED_BY(mMutex); |
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 149 | |
| Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 150 | uint32_t mTransformHint GUARDED_BY(mMutex); |
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 151 | |
| 152 | sp<IGraphicBufferConsumer> mConsumer; |
| 153 | sp<IGraphicBufferProducer> mProducer; |
| Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 154 | sp<BLASTBufferItemConsumer> mBufferItemConsumer; |
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 155 | |
| Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 156 | SurfaceComposerClient::Transaction* mNextTransaction GUARDED_BY(mMutex); |
| Vishnu Nair | c4a40c1 | 2020-12-23 09:14:32 -0800 | [diff] [blame] | 157 | std::vector<std::tuple<uint64_t /* framenumber */, SurfaceComposerClient::Transaction>> |
| 158 | mPendingTransactions GUARDED_BY(mMutex); |
| 159 | |
| Vishnu Nair | 7eb670a | 2020-10-15 12:16:10 -0700 | [diff] [blame] | 160 | // If set to true, the next queue buffer will wait until the shadow queue has been processed by |
| 161 | // the adapter. |
| 162 | bool mFlushShadowQueue = false; |
| Vishnu Nair | cf26a0a | 2020-11-13 12:56:20 -0800 | [diff] [blame] | 163 | // Last requested auto refresh state set by the producer. The state indicates that the consumer |
| 164 | // should acquire the next frame as soon as it can and not wait for a frame to become available. |
| 165 | // This is only relevant for shared buffer mode. |
| 166 | bool mAutoRefresh GUARDED_BY(mMutex) = false; |
| Jorim Jaggi | a3fe67b | 2020-12-01 00:24:33 +0100 | [diff] [blame] | 167 | |
| Siarhei Vishniakou | fc434ac | 2021-01-13 10:28:00 -1000 | [diff] [blame] | 168 | std::queue<FrameTimelineInfo> mNextFrameTimelineInfoQueue GUARDED_BY(mMutex); |
| Vishnu Nair | 53c936c | 2020-12-03 11:46:37 -0800 | [diff] [blame] | 169 | |
| 170 | // Last acquired buffer's scaling mode. This is used to check if we should update the blast |
| 171 | // layer size immediately or wait until we get the next buffer. This will support scenarios |
| 172 | // where the layer can change sizes and the buffer will scale to fit the new size. |
| 173 | uint32_t mLastBufferScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW; |
| chaviw | 71c2cc4 | 2020-10-23 16:42:02 -0700 | [diff] [blame] | 174 | |
| Vishnu Nair | c4a40c1 | 2020-12-23 09:14:32 -0800 | [diff] [blame] | 175 | // Tracks the last acquired frame number |
| 176 | uint64_t mLastAcquiredFrameNumber GUARDED_BY(mMutex) = 0; |
| 177 | |
| chaviw | 71c2cc4 | 2020-10-23 16:42:02 -0700 | [diff] [blame] | 178 | std::function<void(int64_t)> mTransactionCompleteCallback GUARDED_BY(mMutex) = nullptr; |
| 179 | uint64_t mTransactionCompleteFrameNumber GUARDED_BY(mMutex){0}; |
| Vishnu Nair | 277142c | 2021-01-05 18:35:29 -0800 | [diff] [blame] | 180 | |
| 181 | // Queues up transactions using this token in SurfaceFlinger. This prevents queued up |
| 182 | // transactions from other parts of the client from blocking this transaction. |
| Vishnu Nair | adf632b | 2021-01-07 14:05:08 -0800 | [diff] [blame] | 183 | const sp<IBinder> mApplyToken GUARDED_BY(mMutex) = new BBinder(); |
| 184 | |
| 185 | // Guards access to mDequeueTimestamps since we cannot hold to mMutex in onFrameDequeued or |
| 186 | // we will deadlock. |
| 187 | std::mutex mTimestampMutex; |
| 188 | // Tracks buffer dequeue times by the client. This info is sent to SurfaceFlinger which uses |
| 189 | // it for debugging purposes. |
| 190 | std::unordered_map<uint64_t /* bufferId */, nsecs_t> mDequeueTimestamps |
| 191 | GUARDED_BY(mTimestampMutex); |
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | } // namespace android |
| 195 | |
| 196 | #endif // ANDROID_GUI_SURFACE_H |