StreamSplitter.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright 2014 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. #ifndef ANDROID_GUI_STREAMSPLITTER_H
  17. #define ANDROID_GUI_STREAMSPLITTER_H
  18. #include <gui/IConsumerListener.h>
  19. #include <gui/IProducerListener.h>
  20. #include <utils/Condition.h>
  21. #include <utils/KeyedVector.h>
  22. #include <utils/Mutex.h>
  23. #include <utils/StrongPointer.h>
  24. namespace android {
  25. class GraphicBuffer;
  26. class IGraphicBufferConsumer;
  27. class IGraphicBufferProducer;
  28. // StreamSplitter is an autonomous class that manages one input BufferQueue
  29. // and multiple output BufferQueues. By using the buffer attach and detach logic
  30. // in BufferQueue, it is able to present the illusion of a single split
  31. // BufferQueue, where each buffer queued to the input is available to be
  32. // acquired by each of the outputs, and is able to be dequeued by the input
  33. // again only once all of the outputs have released it.
  34. class StreamSplitter : public BnConsumerListener {
  35. public:
  36. // createSplitter creates a new splitter, outSplitter, using inputQueue as
  37. // the input BufferQueue. Output BufferQueues must be added using addOutput
  38. // before queueing any buffers to the input.
  39. //
  40. // A return value other than NO_ERROR means that an error has occurred and
  41. // outSplitter has not been modified. BAD_VALUE is returned if inputQueue or
  42. // outSplitter is NULL. See IGraphicBufferConsumer::consumerConnect for
  43. // explanations of other error codes.
  44. static status_t createSplitter(const sp<IGraphicBufferConsumer>& inputQueue,
  45. sp<StreamSplitter>* outSplitter);
  46. // addOutput adds an output BufferQueue to the splitter. The splitter
  47. // connects to outputQueue as a CPU producer, and any buffers queued
  48. // to the input will be queued to each output. It is assumed that all of the
  49. // outputs are added before any buffers are queued on the input. If any
  50. // output is abandoned by its consumer, the splitter will abandon its input
  51. // queue (see onAbandoned).
  52. //
  53. // A return value other than NO_ERROR means that an error has occurred and
  54. // outputQueue has not been added to the splitter. BAD_VALUE is returned if
  55. // outputQueue is NULL. See IGraphicBufferProducer::connect for explanations
  56. // of other error codes.
  57. status_t addOutput(const sp<IGraphicBufferProducer>& outputQueue);
  58. // setName sets the consumer name of the input queue
  59. void setName(const String8& name);
  60. private:
  61. // From IConsumerListener
  62. //
  63. // During this callback, we store some tracking information, detach the
  64. // buffer from the input, and attach it to each of the outputs. This call
  65. // can block if there are too many outstanding buffers. If it blocks, it
  66. // will resume when onBufferReleasedByOutput releases a buffer back to the
  67. // input.
  68. virtual void onFrameAvailable(const BufferItem& item);
  69. // From IConsumerListener
  70. // We don't care about released buffers because we detach each buffer as
  71. // soon as we acquire it. See the comment for onBufferReleased below for
  72. // some clarifying notes about the name.
  73. virtual void onBuffersReleased() {}
  74. // From IConsumerListener
  75. // We don't care about sideband streams, since we won't be splitting them
  76. virtual void onSidebandStreamChanged() {}
  77. // This is the implementation of the onBufferReleased callback from
  78. // IProducerListener. It gets called from an OutputListener (see below), and
  79. // 'from' is which producer interface from which the callback was received.
  80. //
  81. // During this callback, we detach the buffer from the output queue that
  82. // generated the callback, update our state tracking to see if this is the
  83. // last output releasing the buffer, and if so, release it to the input.
  84. // If we release the buffer to the input, we allow a blocked
  85. // onFrameAvailable call to proceed.
  86. void onBufferReleasedByOutput(const sp<IGraphicBufferProducer>& from);
  87. // When this is called, the splitter disconnects from (i.e., abandons) its
  88. // input queue and signals any waiting onFrameAvailable calls to wake up.
  89. // It still processes callbacks from other outputs, but only detaches their
  90. // buffers so they can continue operating until they run out of buffers to
  91. // acquire. This must be called with mMutex locked.
  92. void onAbandonedLocked();
  93. // This is a thin wrapper class that lets us determine which BufferQueue
  94. // the IProducerListener::onBufferReleased callback is associated with. We
  95. // create one of these per output BufferQueue, and then pass the producer
  96. // into onBufferReleasedByOutput above.
  97. class OutputListener : public BnProducerListener,
  98. public IBinder::DeathRecipient {
  99. public:
  100. OutputListener(const sp<StreamSplitter>& splitter,
  101. const sp<IGraphicBufferProducer>& output);
  102. virtual ~OutputListener();
  103. // From IProducerListener
  104. virtual void onBufferReleased();
  105. // From IBinder::DeathRecipient
  106. virtual void binderDied(const wp<IBinder>& who);
  107. private:
  108. sp<StreamSplitter> mSplitter;
  109. sp<IGraphicBufferProducer> mOutput;
  110. };
  111. class BufferTracker : public LightRefBase<BufferTracker> {
  112. public:
  113. BufferTracker(const sp<GraphicBuffer>& buffer);
  114. const sp<GraphicBuffer>& getBuffer() const { return mBuffer; }
  115. const sp<Fence>& getMergedFence() const { return mMergedFence; }
  116. void mergeFence(const sp<Fence>& with);
  117. // Returns the new value
  118. // Only called while mMutex is held
  119. size_t incrementReleaseCountLocked() { return ++mReleaseCount; }
  120. private:
  121. // Only destroy through LightRefBase
  122. friend LightRefBase<BufferTracker>;
  123. ~BufferTracker();
  124. // Disallow copying
  125. BufferTracker(const BufferTracker& other);
  126. BufferTracker& operator=(const BufferTracker& other);
  127. sp<GraphicBuffer> mBuffer; // One instance that holds this native handle
  128. sp<Fence> mMergedFence;
  129. size_t mReleaseCount;
  130. };
  131. // Only called from createSplitter
  132. StreamSplitter(const sp<IGraphicBufferConsumer>& inputQueue);
  133. // Must be accessed through RefBase
  134. virtual ~StreamSplitter();
  135. static const int MAX_OUTSTANDING_BUFFERS = 2;
  136. // mIsAbandoned is set to true when an output dies. Once the StreamSplitter
  137. // has been abandoned, it will continue to detach buffers from other
  138. // outputs, but it will disconnect from the input and not attempt to
  139. // communicate with it further.
  140. bool mIsAbandoned;
  141. Mutex mMutex;
  142. Condition mReleaseCondition;
  143. int mOutstandingBuffers;
  144. sp<IGraphicBufferConsumer> mInput;
  145. Vector<sp<IGraphicBufferProducer> > mOutputs;
  146. // Map of GraphicBuffer IDs (GraphicBuffer::getId()) to buffer tracking
  147. // objects (which are mostly for counting how many outputs have released the
  148. // buffer, but also contain merged release fences).
  149. KeyedVector<uint64_t, sp<BufferTracker> > mBuffers;
  150. };
  151. } // namespace android
  152. #endif