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> 30 #include <thread> 31 #include <queue> 32 33 namespace android { 34 35 class BLASTBufferQueue; 36 class BufferItemConsumer; 37 38 class BLASTBufferItemConsumer : public BufferItemConsumer { 39 public: BLASTBufferItemConsumer(const sp<IGraphicBufferConsumer> & consumer,uint64_t consumerUsage,int bufferCount,bool controlledByApp,wp<BLASTBufferQueue> bbq)40 BLASTBufferItemConsumer(const sp<IGraphicBufferConsumer>& consumer, uint64_t consumerUsage, 41 int bufferCount, bool controlledByApp, wp<BLASTBufferQueue> bbq) 42 : BufferItemConsumer(consumer, consumerUsage, bufferCount, controlledByApp), 43 mBLASTBufferQueue(std::move(bbq)), 44 mCurrentlyConnected(false), 45 mPreviouslyConnected(false) {} 46 47 void onDisconnect() override; 48 void addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps, 49 FrameEventHistoryDelta* outDelta) override REQUIRES(mMutex); 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, 54 nsecs_t dequeueReadyTime) REQUIRES(mMutex); 55 void getConnectionEvents(uint64_t frameNumber, bool* needsDisconnect); 56 57 protected: 58 void onSidebandStreamChanged() override REQUIRES(mMutex); 59 60 private: 61 const wp<BLASTBufferQueue> mBLASTBufferQueue; 62 63 uint64_t mCurrentFrameNumber = 0; 64 65 Mutex mMutex; 66 ConsumerFrameEventHistory mFrameEventHistory GUARDED_BY(mMutex); 67 std::queue<uint64_t> mDisconnectEvents GUARDED_BY(mMutex); 68 bool mCurrentlyConnected GUARDED_BY(mMutex); 69 bool mPreviouslyConnected GUARDED_BY(mMutex); 70 }; 71 72 class BLASTBufferQueue 73 : public ConsumerBase::FrameAvailableListener, public BufferItemConsumer::BufferFreedListener 74 { 75 public: 76 BLASTBufferQueue(const std::string& name); 77 BLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface, int width, 78 int height, int32_t format); 79 getIGraphicBufferProducer()80 sp<IGraphicBufferProducer> getIGraphicBufferProducer() const { 81 return mProducer; 82 } 83 sp<Surface> getSurface(bool includeSurfaceControlHandle); 84 onBufferFreed(const wp<GraphicBuffer> &)85 void onBufferFreed(const wp<GraphicBuffer>&/* graphicBuffer*/) override { /* TODO */ } 86 void onFrameReplaced(const BufferItem& item) override; 87 void onFrameAvailable(const BufferItem& item) override; 88 void onFrameDequeued(const uint64_t) override; 89 void onFrameCancelled(const uint64_t) override; 90 91 void transactionCommittedCallback(nsecs_t latchTime, const sp<Fence>& presentFence, 92 const std::vector<SurfaceControlStats>& stats); 93 void transactionCallback(nsecs_t latchTime, const sp<Fence>& presentFence, 94 const std::vector<SurfaceControlStats>& stats); 95 void releaseBufferCallback(const ReleaseCallbackId& id, const sp<Fence>& releaseFence, 96 uint32_t transformHint, uint32_t currentMaxAcquiredBufferCount); 97 void releaseBufferCallbackLocked(const ReleaseCallbackId& id, const sp<Fence>& releaseFence, 98 uint32_t transformHint, uint32_t currentMaxAcquiredBufferCount); 99 void setNextTransaction(SurfaceComposerClient::Transaction *t); 100 void mergeWithNextTransaction(SurfaceComposerClient::Transaction* t, uint64_t frameNumber); 101 void setTransactionCompleteCallback(uint64_t frameNumber, 102 std::function<void(int64_t)>&& transactionCompleteCallback); 103 104 void update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height, int32_t format, 105 SurfaceComposerClient::Transaction* outTransaction = nullptr); 106 107 status_t setFrameRate(float frameRate, int8_t compatibility, bool shouldBeSeamless); 108 status_t setFrameTimelineInfo(const FrameTimelineInfo& info); 109 110 void setSidebandStream(const sp<NativeHandle>& stream); 111 112 uint32_t getLastTransformHint() const; 113 void flushShadowQueue(); 114 115 uint64_t getLastAcquiredFrameNum(); 116 117 virtual ~BLASTBufferQueue(); 118 119 private: 120 friend class BLASTBufferQueueHelper; 121 122 // can't be copied 123 BLASTBufferQueue& operator = (const BLASTBufferQueue& rhs); 124 BLASTBufferQueue(const BLASTBufferQueue& rhs); 125 void createBufferQueue(sp<IGraphicBufferProducer>* outProducer, 126 sp<IGraphicBufferConsumer>* outConsumer); 127 128 void acquireNextBufferLocked( 129 const std::optional<SurfaceComposerClient::Transaction*> transaction) REQUIRES(mMutex); 130 Rect computeCrop(const BufferItem& item) REQUIRES(mMutex); 131 // Return true if we need to reject the buffer based on the scaling mode and the buffer size. 132 bool rejectBuffer(const BufferItem& item) REQUIRES(mMutex); 133 bool maxBuffersAcquired(bool includeExtraAcquire) const REQUIRES(mMutex); 134 static PixelFormat convertBufferFormat(PixelFormat& format); 135 136 void flushShadowQueueLocked() REQUIRES(mMutex); 137 void acquireAndReleaseBuffer() REQUIRES(mMutex); 138 139 std::string mName; 140 // Represents the queued buffer count from buffer queue, 141 // pre-BLAST. This is mNumFrameAvailable (buffers that queued to blast) + 142 // mNumAcquired (buffers that queued to SF) mPendingRelease.size() (buffers that are held by 143 // blast). This counter is read by android studio profiler. 144 std::string mQueuedBufferTrace; 145 sp<SurfaceControl> mSurfaceControl; 146 147 std::mutex mMutex; 148 std::condition_variable mCallbackCV; 149 150 // BufferQueue internally allows 1 more than 151 // the max to be acquired 152 int32_t mMaxAcquiredBuffers = 1; 153 154 int32_t mNumFrameAvailable GUARDED_BY(mMutex); 155 int32_t mNumAcquired GUARDED_BY(mMutex); 156 157 // Keep a reference to the submitted buffers so we can release when surfaceflinger drops the 158 // buffer or the buffer has been presented and a new buffer is ready to be presented. 159 std::unordered_map<ReleaseCallbackId, BufferItem, ReleaseBufferCallbackIdHash> mSubmitted 160 GUARDED_BY(mMutex); 161 162 // Keep a queue of the released buffers instead of immediately releasing 163 // the buffers back to the buffer queue. This would be controlled by SF 164 // setting the max acquired buffer count. 165 struct ReleasedBuffer { 166 ReleaseCallbackId callbackId; 167 sp<Fence> releaseFence; 168 bool operator==(const ReleasedBuffer& rhs) const { 169 // Only compare Id so if we somehow got two callbacks 170 // with different fences we don't decrement mNumAcquired 171 // too far. 172 return rhs.callbackId == callbackId; 173 } 174 }; 175 std::deque<ReleasedBuffer> mPendingRelease GUARDED_BY(mMutex); 176 177 ui::Size mSize GUARDED_BY(mMutex); 178 ui::Size mRequestedSize GUARDED_BY(mMutex); 179 int32_t mFormat GUARDED_BY(mMutex); 180 181 struct BufferInfo { 182 bool hasBuffer = false; 183 uint32_t width; 184 uint32_t height; 185 uint32_t transform; 186 // This is used to check if we should update the blast layer size immediately or wait until 187 // we get the next buffer. This will support scenarios where the layer can change sizes 188 // and the buffer will scale to fit the new size. 189 uint32_t scalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW; 190 Rect crop; 191 updateBufferInfo192 void update(bool hasBuffer, uint32_t width, uint32_t height, uint32_t transform, 193 uint32_t scalingMode, const Rect& crop) { 194 this->hasBuffer = hasBuffer; 195 this->width = width; 196 this->height = height; 197 this->transform = transform; 198 this->scalingMode = scalingMode; 199 if (!crop.isEmpty()) { 200 this->crop = crop; 201 } else { 202 this->crop = Rect(width, height); 203 } 204 } 205 }; 206 207 // Last acquired buffer's info. This is used to calculate the correct scale when size change is 208 // requested. We need to use the old buffer's info to determine what scale we need to apply to 209 // ensure the correct size. 210 BufferInfo mLastBufferInfo GUARDED_BY(mMutex); 211 void setMatrix(SurfaceComposerClient::Transaction* t, const BufferInfo& bufferInfo) 212 REQUIRES(mMutex); 213 214 uint32_t mTransformHint GUARDED_BY(mMutex); 215 216 sp<IGraphicBufferConsumer> mConsumer; 217 sp<IGraphicBufferProducer> mProducer; 218 sp<BLASTBufferItemConsumer> mBufferItemConsumer; 219 220 SurfaceComposerClient::Transaction* mNextTransaction GUARDED_BY(mMutex); 221 std::vector<std::tuple<uint64_t /* framenumber */, SurfaceComposerClient::Transaction>> 222 mPendingTransactions GUARDED_BY(mMutex); 223 224 // Last requested auto refresh state set by the producer. The state indicates that the consumer 225 // should acquire the next frame as soon as it can and not wait for a frame to become available. 226 // This is only relevant for shared buffer mode. 227 bool mAutoRefresh GUARDED_BY(mMutex) = false; 228 229 std::queue<FrameTimelineInfo> mNextFrameTimelineInfoQueue GUARDED_BY(mMutex); 230 231 // Tracks the last acquired frame number 232 uint64_t mLastAcquiredFrameNumber GUARDED_BY(mMutex) = 0; 233 234 std::function<void(int64_t)> mTransactionCompleteCallback GUARDED_BY(mMutex) = nullptr; GUARDED_BY(mMutex)235 uint64_t mTransactionCompleteFrameNumber GUARDED_BY(mMutex){0}; 236 237 // Queues up transactions using this token in SurfaceFlinger. This prevents queued up 238 // transactions from other parts of the client from blocking this transaction. 239 const sp<IBinder> mApplyToken GUARDED_BY(mMutex) = new BBinder(); 240 241 // Guards access to mDequeueTimestamps since we cannot hold to mMutex in onFrameDequeued or 242 // we will deadlock. 243 std::mutex mTimestampMutex; 244 // Tracks buffer dequeue times by the client. This info is sent to SurfaceFlinger which uses 245 // it for debugging purposes. 246 std::unordered_map<uint64_t /* bufferId */, nsecs_t> mDequeueTimestamps 247 GUARDED_BY(mTimestampMutex); 248 249 // Keep track of SurfaceControls that have submitted a transaction and BBQ is waiting on a 250 // callback for them. 251 std::queue<sp<SurfaceControl>> mSurfaceControlsWithPendingCallback GUARDED_BY(mMutex); 252 253 uint32_t mCurrentMaxAcquiredBufferCount; 254 bool mWaitForTransactionCallback = false; 255 }; 256 257 } // namespace android 258 259 #endif // ANDROID_GUI_SURFACE_H 260