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