| 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> | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 31 |  | 
|  | 32 | namespace android { | 
|  | 33 |  | 
|  | 34 | class BufferItemConsumer; | 
|  | 35 |  | 
| Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 36 | class BLASTBufferItemConsumer : public BufferItemConsumer { | 
|  | 37 | public: | 
|  | 38 | BLASTBufferItemConsumer(const sp<IGraphicBufferConsumer>& consumer, uint64_t consumerUsage, | 
|  | 39 | int bufferCount, bool controlledByApp) | 
|  | 40 | : BufferItemConsumer(consumer, consumerUsage, bufferCount, controlledByApp), | 
|  | 41 | mCurrentlyConnected(false), | 
|  | 42 | mPreviouslyConnected(false) {} | 
|  | 43 |  | 
|  | 44 | void onDisconnect() override; | 
|  | 45 | void addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps, | 
|  | 46 | FrameEventHistoryDelta* outDelta) override | 
|  | 47 | REQUIRES(mFrameEventHistoryMutex); | 
|  | 48 | void updateFrameTimestamps(uint64_t frameNumber, nsecs_t refreshStartTime, | 
|  | 49 | const sp<Fence>& gpuCompositionDoneFence, | 
|  | 50 | const sp<Fence>& presentFence, const sp<Fence>& prevReleaseFence, | 
|  | 51 | CompositorTiming compositorTiming, nsecs_t latchTime, | 
|  | 52 | nsecs_t dequeueReadyTime) REQUIRES(mFrameEventHistoryMutex); | 
|  | 53 | void getConnectionEvents(uint64_t frameNumber, bool* needsDisconnect); | 
|  | 54 |  | 
|  | 55 | private: | 
|  | 56 | uint64_t mCurrentFrameNumber = 0; | 
|  | 57 |  | 
|  | 58 | Mutex mFrameEventHistoryMutex; | 
|  | 59 | ConsumerFrameEventHistory mFrameEventHistory GUARDED_BY(mFrameEventHistoryMutex); | 
|  | 60 | std::queue<uint64_t> mDisconnectEvents GUARDED_BY(mFrameEventHistoryMutex); | 
|  | 61 | bool mCurrentlyConnected GUARDED_BY(mFrameEventHistoryMutex); | 
|  | 62 | bool mPreviouslyConnected GUARDED_BY(mFrameEventHistoryMutex); | 
|  | 63 | }; | 
|  | 64 |  | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 65 | class BLASTBufferQueue | 
|  | 66 | : public ConsumerBase::FrameAvailableListener, public BufferItemConsumer::BufferFreedListener | 
|  | 67 | { | 
|  | 68 | public: | 
| Vishnu Nair | dab9409 | 2020-09-29 16:09:04 -0700 | [diff] [blame^] | 69 | BLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface, int width, | 
|  | 70 | int height, bool enableTripleBuffering = true); | 
| Robert Carr | cedef05 | 2020-04-22 15:58:23 -0700 | [diff] [blame] | 71 |  | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 72 | sp<IGraphicBufferProducer> getIGraphicBufferProducer() const { | 
|  | 73 | return mProducer; | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | void onBufferFreed(const wp<GraphicBuffer>&/* graphicBuffer*/) override { /* TODO */ } | 
|  | 77 | void onFrameReplaced(const BufferItem& item) override {onFrameAvailable(item);} | 
|  | 78 | void onFrameAvailable(const BufferItem& item) override; | 
|  | 79 |  | 
|  | 80 | void transactionCallback(nsecs_t latchTime, const sp<Fence>& presentFence, | 
|  | 81 | const std::vector<SurfaceControlStats>& stats); | 
|  | 82 | void setNextTransaction(SurfaceComposerClient::Transaction *t); | 
|  | 83 |  | 
| Vishnu Nair | dab9409 | 2020-09-29 16:09:04 -0700 | [diff] [blame^] | 84 | void update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height); | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 85 |  | 
|  | 86 | virtual ~BLASTBufferQueue() = default; | 
|  | 87 |  | 
|  | 88 | private: | 
| Valerie Hau | c5011f9 | 2019-10-11 09:52:07 -0700 | [diff] [blame] | 89 | friend class BLASTBufferQueueHelper; | 
|  | 90 |  | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 91 | // can't be copied | 
|  | 92 | BLASTBufferQueue& operator = (const BLASTBufferQueue& rhs); | 
|  | 93 | BLASTBufferQueue(const BLASTBufferQueue& rhs); | 
|  | 94 |  | 
| Robert Carr | 255acdc | 2020-04-17 14:08:55 -0700 | [diff] [blame] | 95 | void processNextBufferLocked(bool useNextTransaction) REQUIRES(mMutex); | 
| Valerie Hau | 45e4b3b | 2019-12-03 10:49:17 -0800 | [diff] [blame] | 96 | Rect computeCrop(const BufferItem& item); | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 97 |  | 
| Vishnu Nair | dab9409 | 2020-09-29 16:09:04 -0700 | [diff] [blame^] | 98 | std::string mName; | 
| Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 99 | sp<SurfaceControl> mSurfaceControl; | 
|  | 100 |  | 
|  | 101 | std::mutex mMutex; | 
|  | 102 | std::condition_variable mCallbackCV; | 
| Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 103 |  | 
| Valerie Hau | 65b8e87 | 2020-02-13 09:45:14 -0800 | [diff] [blame] | 104 | // BufferQueue internally allows 1 more than | 
|  | 105 | // the max to be acquired | 
|  | 106 | static const int MAX_ACQUIRED_BUFFERS = 1; | 
| Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 107 |  | 
|  | 108 | int32_t mNumFrameAvailable GUARDED_BY(mMutex); | 
|  | 109 | int32_t mNumAcquired GUARDED_BY(mMutex); | 
| Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 110 | struct PendingReleaseItem { | 
|  | 111 | BufferItem item; | 
|  | 112 | sp<Fence> releaseFence; | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 113 | }; | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 114 |  | 
| Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 115 | std::queue<const BufferItem> mSubmitted GUARDED_BY(mMutex); | 
| Vishnu Nair | dab9409 | 2020-09-29 16:09:04 -0700 | [diff] [blame^] | 116 | // Keep a reference to the currently presented buffer so we can release it when the next buffer | 
|  | 117 | // is ready to be presented. | 
| Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 118 | PendingReleaseItem mPendingReleaseItem GUARDED_BY(mMutex); | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 119 |  | 
| Vishnu Nair | dab9409 | 2020-09-29 16:09:04 -0700 | [diff] [blame^] | 120 | uint32_t mWidth GUARDED_BY(mMutex); | 
|  | 121 | uint32_t mHeight GUARDED_BY(mMutex); | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 122 |  | 
| Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 123 | uint32_t mTransformHint GUARDED_BY(mMutex); | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 124 |  | 
|  | 125 | sp<IGraphicBufferConsumer> mConsumer; | 
|  | 126 | sp<IGraphicBufferProducer> mProducer; | 
| Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 127 | sp<BLASTBufferItemConsumer> mBufferItemConsumer; | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 128 |  | 
| Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 129 | SurfaceComposerClient::Transaction* mNextTransaction GUARDED_BY(mMutex); | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 130 | }; | 
|  | 131 |  | 
|  | 132 | } // namespace android | 
|  | 133 |  | 
|  | 134 | #endif  // ANDROID_GUI_SURFACE_H |