| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2014,2016 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_SERVERS_STREAMSPLITTER_H | 
|  | 18 | #define ANDROID_SERVERS_STREAMSPLITTER_H | 
|  | 19 |  | 
| Emilian Peev | 6f82372 | 2017-12-07 18:05:49 +0000 | [diff] [blame^] | 20 | #include <unordered_set> | 
|  | 21 |  | 
| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 22 | #include <gui/IConsumerListener.h> | 
|  | 23 | #include <gui/IProducerListener.h> | 
|  | 24 | #include <gui/BufferItemConsumer.h> | 
|  | 25 |  | 
|  | 26 | #include <utils/Condition.h> | 
|  | 27 | #include <utils/Mutex.h> | 
|  | 28 | #include <utils/StrongPointer.h> | 
|  | 29 | #include <utils/Timers.h> | 
|  | 30 |  | 
| Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 31 | #define SP_LOGV(x, ...) ALOGV("[%s] " x, mConsumerName.string(), ##__VA_ARGS__) | 
|  | 32 | #define SP_LOGI(x, ...) ALOGI("[%s] " x, mConsumerName.string(), ##__VA_ARGS__) | 
|  | 33 | #define SP_LOGW(x, ...) ALOGW("[%s] " x, mConsumerName.string(), ##__VA_ARGS__) | 
|  | 34 | #define SP_LOGE(x, ...) ALOGE("[%s] " x, mConsumerName.string(), ##__VA_ARGS__) | 
|  | 35 |  | 
| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 36 | namespace android { | 
|  | 37 |  | 
|  | 38 | class GraphicBuffer; | 
|  | 39 | class IGraphicBufferConsumer; | 
|  | 40 | class IGraphicBufferProducer; | 
|  | 41 |  | 
|  | 42 | // Camera3StreamSplitter is an autonomous class that manages one input BufferQueue | 
|  | 43 | // and multiple output BufferQueues. By using the buffer attach and detach logic | 
|  | 44 | // in BufferQueue, it is able to present the illusion of a single split | 
|  | 45 | // BufferQueue, where each buffer queued to the input is available to be | 
|  | 46 | // acquired by each of the outputs, and is able to be dequeued by the input | 
|  | 47 | // again only once all of the outputs have released it. | 
|  | 48 | class Camera3StreamSplitter : public BnConsumerListener { | 
|  | 49 | public: | 
|  | 50 |  | 
|  | 51 | // Constructor | 
|  | 52 | Camera3StreamSplitter() = default; | 
|  | 53 |  | 
|  | 54 | // Connect to the stream splitter by creating buffer queue and connecting it | 
|  | 55 | // with output surfaces. | 
| Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 56 | status_t connect(const std::unordered_map<size_t, sp<Surface>> &surfaces, | 
|  | 57 | uint64_t consumerUsage, uint64_t producerUsage, size_t halMaxBuffers, uint32_t width, | 
|  | 58 | uint32_t height, android::PixelFormat format, sp<Surface>* consumer); | 
| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 59 |  | 
|  | 60 | // addOutput adds an output BufferQueue to the splitter. The splitter | 
|  | 61 | // connects to outputQueue as a CPU producer, and any buffers queued | 
| Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 62 | // to the input will be queued to each output. If any  output is abandoned | 
|  | 63 | // by its consumer, the splitter will abandon its input queue (see onAbandoned). | 
| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 64 | // | 
|  | 65 | // A return value other than NO_ERROR means that an error has occurred and | 
|  | 66 | // outputQueue has not been added to the splitter. BAD_VALUE is returned if | 
|  | 67 | // outputQueue is NULL. See IGraphicBufferProducer::connect for explanations | 
|  | 68 | // of other error codes. | 
| Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 69 | status_t addOutput(size_t surfaceId, const sp<Surface>& outputQueue); | 
|  | 70 |  | 
|  | 71 | //removeOutput will remove a BufferQueue that was previously added to | 
|  | 72 | //the splitter outputs. Any pending buffers in the BufferQueue will get | 
|  | 73 | //reclaimed. | 
|  | 74 | status_t removeOutput(size_t surfaceId); | 
| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 75 |  | 
| Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 76 | // Notification that the graphic buffer has been released to the input | 
|  | 77 | // BufferQueue. The buffer should be reused by the camera device instead of | 
|  | 78 | // queuing to the outputs. | 
|  | 79 | status_t notifyBufferReleased(const sp<GraphicBuffer>& buffer); | 
|  | 80 |  | 
|  | 81 | // Attach a buffer to the specified outputs. This call reserves a buffer | 
|  | 82 | // slot in the output queue. | 
|  | 83 | status_t attachBufferToOutputs(ANativeWindowBuffer* anb, | 
|  | 84 | const std::vector<size_t>& surface_ids); | 
|  | 85 |  | 
|  | 86 | // Get return value of onFrameAvailable to work around problem that | 
|  | 87 | // onFrameAvailable is void. This function should be called by the producer | 
|  | 88 | // right after calling queueBuffer(). | 
|  | 89 | status_t getOnFrameAvailableResult(); | 
| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 90 |  | 
|  | 91 | // Disconnect the buffer queue from output surfaces. | 
|  | 92 | void disconnect(); | 
|  | 93 |  | 
|  | 94 | private: | 
|  | 95 | // From IConsumerListener | 
|  | 96 | // | 
|  | 97 | // During this callback, we store some tracking information, detach the | 
|  | 98 | // buffer from the input, and attach it to each of the outputs. This call | 
|  | 99 | // can block if there are too many outstanding buffers. If it blocks, it | 
|  | 100 | // will resume when onBufferReleasedByOutput releases a buffer back to the | 
|  | 101 | // input. | 
|  | 102 | void onFrameAvailable(const BufferItem& item) override; | 
|  | 103 |  | 
|  | 104 | // From IConsumerListener | 
|  | 105 | // We don't care about released buffers because we detach each buffer as | 
|  | 106 | // soon as we acquire it. See the comment for onBufferReleased below for | 
|  | 107 | // some clarifying notes about the name. | 
|  | 108 | void onBuffersReleased() override {} | 
|  | 109 |  | 
|  | 110 | // From IConsumerListener | 
|  | 111 | // We don't care about sideband streams, since we won't be splitting them | 
|  | 112 | void onSidebandStreamChanged() override {} | 
|  | 113 |  | 
|  | 114 | // This is the implementation of the onBufferReleased callback from | 
|  | 115 | // IProducerListener. It gets called from an OutputListener (see below), and | 
|  | 116 | // 'from' is which producer interface from which the callback was received. | 
|  | 117 | // | 
|  | 118 | // During this callback, we detach the buffer from the output queue that | 
|  | 119 | // generated the callback, update our state tracking to see if this is the | 
|  | 120 | // last output releasing the buffer, and if so, release it to the input. | 
|  | 121 | // If we release the buffer to the input, we allow a blocked | 
|  | 122 | // onFrameAvailable call to proceed. | 
|  | 123 | void onBufferReleasedByOutput(const sp<IGraphicBufferProducer>& from); | 
|  | 124 |  | 
| Shuzhen Wang | a141c5f | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 125 | // This is the implementation of onBufferReleasedByOutput without the mutex locked. | 
|  | 126 | // It could either be called from onBufferReleasedByOutput or from | 
|  | 127 | // onFrameAvailable when a buffer in the async buffer queue is overwritten. | 
| Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 128 | void onBufferReleasedByOutputLocked(const sp<IGraphicBufferProducer>& from, size_t surfaceId); | 
| Shuzhen Wang | a141c5f | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 129 |  | 
| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 130 | // When this is called, the splitter disconnects from (i.e., abandons) its | 
|  | 131 | // input queue and signals any waiting onFrameAvailable calls to wake up. | 
|  | 132 | // It still processes callbacks from other outputs, but only detaches their | 
|  | 133 | // buffers so they can continue operating until they run out of buffers to | 
|  | 134 | // acquire. This must be called with mMutex locked. | 
|  | 135 | void onAbandonedLocked(); | 
|  | 136 |  | 
| Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 137 | // Decrement the buffer's reference count. Once the reference count becomes | 
|  | 138 | // 0, return the buffer back to the input BufferQueue. | 
| Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 139 | void decrementBufRefCountLocked(uint64_t id, size_t surfaceId); | 
| Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 140 |  | 
| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 141 | // This is a thin wrapper class that lets us determine which BufferQueue | 
|  | 142 | // the IProducerListener::onBufferReleased callback is associated with. We | 
|  | 143 | // create one of these per output BufferQueue, and then pass the producer | 
|  | 144 | // into onBufferReleasedByOutput above. | 
|  | 145 | class OutputListener : public BnProducerListener, | 
|  | 146 | public IBinder::DeathRecipient { | 
|  | 147 | public: | 
|  | 148 | OutputListener(wp<Camera3StreamSplitter> splitter, | 
|  | 149 | wp<IGraphicBufferProducer> output); | 
|  | 150 | virtual ~OutputListener() = default; | 
|  | 151 |  | 
|  | 152 | // From IProducerListener | 
|  | 153 | void onBufferReleased() override; | 
|  | 154 |  | 
|  | 155 | // From IBinder::DeathRecipient | 
|  | 156 | void binderDied(const wp<IBinder>& who) override; | 
|  | 157 |  | 
|  | 158 | private: | 
|  | 159 | wp<Camera3StreamSplitter> mSplitter; | 
|  | 160 | wp<IGraphicBufferProducer> mOutput; | 
|  | 161 | }; | 
|  | 162 |  | 
|  | 163 | class BufferTracker { | 
|  | 164 | public: | 
| Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 165 | BufferTracker(const sp<GraphicBuffer>& buffer, | 
|  | 166 | const std::vector<size_t>& requestedSurfaces); | 
| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 167 | ~BufferTracker() = default; | 
|  | 168 |  | 
|  | 169 | const sp<GraphicBuffer>& getBuffer() const { return mBuffer; } | 
|  | 170 | const sp<Fence>& getMergedFence() const { return mMergedFence; } | 
|  | 171 |  | 
|  | 172 | void mergeFence(const sp<Fence>& with); | 
|  | 173 |  | 
|  | 174 | // Returns the new value | 
|  | 175 | // Only called while mMutex is held | 
| Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 176 | size_t decrementReferenceCountLocked(size_t surfaceId); | 
| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 177 |  | 
| Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 178 | const std::vector<size_t> requestedSurfaces() const { return mRequestedSurfaces; } | 
|  | 179 |  | 
| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 180 | private: | 
|  | 181 |  | 
|  | 182 | // Disallow copying | 
|  | 183 | BufferTracker(const BufferTracker& other); | 
|  | 184 | BufferTracker& operator=(const BufferTracker& other); | 
|  | 185 |  | 
|  | 186 | sp<GraphicBuffer> mBuffer; // One instance that holds this native handle | 
|  | 187 | sp<Fence> mMergedFence; | 
| Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 188 |  | 
|  | 189 | // Request surfaces for a particular buffer. And when the buffer becomes | 
|  | 190 | // available from the input queue, the registered surfaces are used to decide | 
|  | 191 | // which output is the buffer sent to. | 
|  | 192 | std::vector<size_t> mRequestedSurfaces; | 
| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 193 | size_t mReferenceCount; | 
|  | 194 | }; | 
|  | 195 |  | 
| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 196 | // Must be accessed through RefBase | 
|  | 197 | virtual ~Camera3StreamSplitter(); | 
|  | 198 |  | 
| Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 199 | status_t addOutputLocked(size_t surfaceId, const sp<Surface>& outputQueue); | 
|  | 200 |  | 
|  | 201 | status_t removeOutputLocked(size_t surfaceId); | 
| Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 202 |  | 
|  | 203 | // Send a buffer to particular output, and increment the reference count | 
|  | 204 | // of the buffer. If this output is abandoned, the buffer's reference count | 
|  | 205 | // won't be incremented. | 
|  | 206 | status_t outputBufferLocked(const sp<IGraphicBufferProducer>& output, | 
| Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 207 | const BufferItem& bufferItem, size_t surfaceId); | 
| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 208 |  | 
|  | 209 | // Get unique name for the buffer queue consumer | 
| Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 210 | String8 getUniqueConsumerName(); | 
| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 211 |  | 
| Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 212 | // Helper function to get the BufferQueue slot where a particular buffer is attached to. | 
|  | 213 | int getSlotForOutputLocked(const sp<IGraphicBufferProducer>& gbp, | 
|  | 214 | const sp<GraphicBuffer>& gb); | 
| Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 215 |  | 
|  | 216 | // Sum of max consumer buffers for all outputs | 
|  | 217 | size_t mMaxConsumerBuffers = 0; | 
|  | 218 | size_t mMaxHalBuffers = 0; | 
| Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 219 | uint32_t mWidth = 0; | 
|  | 220 | uint32_t mHeight = 0; | 
|  | 221 | android::PixelFormat mFormat = android::PIXEL_FORMAT_NONE; | 
|  | 222 | uint64_t mProducerUsage = 0; | 
| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 223 |  | 
|  | 224 | static const nsecs_t kDequeueBufferTimeout   = s2ns(1); // 1 sec | 
|  | 225 |  | 
| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 226 | Mutex mMutex; | 
| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 227 |  | 
|  | 228 | sp<IGraphicBufferProducer> mProducer; | 
|  | 229 | sp<IGraphicBufferConsumer> mConsumer; | 
|  | 230 | sp<BufferItemConsumer> mBufferItemConsumer; | 
|  | 231 | sp<Surface> mSurface; | 
|  | 232 |  | 
| Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 233 | //Map graphic buffer ids -> buffer items | 
|  | 234 | std::unordered_map<uint64_t, BufferItem> mInputSlots; | 
|  | 235 |  | 
|  | 236 | //Map surface ids -> gbp outputs | 
|  | 237 | std::unordered_map<int, sp<IGraphicBufferProducer> > mOutputs; | 
|  | 238 |  | 
|  | 239 | //Map surface ids -> consumer buffer count | 
|  | 240 | std::unordered_map<int, size_t > mConsumerBufferCount; | 
|  | 241 |  | 
| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 242 | // Map of GraphicBuffer IDs (GraphicBuffer::getId()) to buffer tracking | 
|  | 243 | // objects (which are mostly for counting how many outputs have released the | 
|  | 244 | // buffer, but also contain merged release fences). | 
|  | 245 | std::unordered_map<uint64_t, std::unique_ptr<BufferTracker> > mBuffers; | 
| Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 246 |  | 
|  | 247 | struct GBPHash { | 
|  | 248 | std::size_t operator()(const sp<IGraphicBufferProducer>& producer) const { | 
|  | 249 | return std::hash<IGraphicBufferProducer *>{}(producer.get()); | 
|  | 250 | } | 
|  | 251 | }; | 
|  | 252 |  | 
|  | 253 | std::unordered_map<sp<IGraphicBufferProducer>, sp<OutputListener>, | 
|  | 254 | GBPHash> mNotifiers; | 
|  | 255 |  | 
|  | 256 | typedef std::vector<sp<GraphicBuffer>> OutputSlots; | 
|  | 257 | std::unordered_map<sp<IGraphicBufferProducer>, std::unique_ptr<OutputSlots>, | 
|  | 258 | GBPHash> mOutputSlots; | 
|  | 259 |  | 
| Emilian Peev | 6f82372 | 2017-12-07 18:05:49 +0000 | [diff] [blame^] | 260 | //A set of buffers that could potentially stay in some of the outputs after removal | 
|  | 261 | //and therefore should be detached from the input queue. | 
|  | 262 | std::unordered_set<uint64_t> mDetachedBuffers; | 
|  | 263 |  | 
| Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 264 | // Latest onFrameAvailable return value | 
|  | 265 | std::atomic<status_t> mOnFrameAvailableRes{0}; | 
|  | 266 |  | 
|  | 267 | String8 mConsumerName; | 
| Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 268 | }; | 
|  | 269 |  | 
|  | 270 | } // namespace android | 
|  | 271 |  | 
|  | 272 | #endif |