VirtualDisplaySurface.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright 2013 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_SF_VIRTUAL_DISPLAY_SURFACE_H
  17. #define ANDROID_SF_VIRTUAL_DISPLAY_SURFACE_H
  18. #include <gui/ConsumerBase.h>
  19. #include <gui/IGraphicBufferProducer.h>
  20. #include "DisplaySurface.h"
  21. // ---------------------------------------------------------------------------
  22. namespace android {
  23. // ---------------------------------------------------------------------------
  24. class HWComposer;
  25. class IProducerListener;
  26. /* This DisplaySurface implementation supports virtual displays, where GLES
  27. * and/or HWC compose into a buffer that is then passed to an arbitrary
  28. * consumer (the sink) running in another process.
  29. *
  30. * The simplest case is when the virtual display will never use the h/w
  31. * composer -- either the h/w composer doesn't support writing to buffers, or
  32. * there are more virtual displays than it supports simultaneously. In this
  33. * case, the GLES driver works directly with the output buffer queue, and
  34. * calls to the VirtualDisplay from SurfaceFlinger and DisplayHardware do
  35. * nothing.
  36. *
  37. * If h/w composer might be used, then each frame will fall into one of three
  38. * configurations: GLES-only, HWC-only, and MIXED composition. In all of these,
  39. * we must provide a FB target buffer and output buffer for the HWC set() call.
  40. *
  41. * In GLES-only composition, the GLES driver is given a buffer from the sink to
  42. * render into. When the GLES driver queues the buffer to the
  43. * VirtualDisplaySurface, the VirtualDisplaySurface holds onto it instead of
  44. * immediately queueing it to the sink. The buffer is used as both the FB
  45. * target and output buffer for HWC, though on these frames the HWC doesn't
  46. * do any work for this display and doesn't write to the output buffer. After
  47. * composition is complete, the buffer is queued to the sink.
  48. *
  49. * In HWC-only composition, the VirtualDisplaySurface dequeues a buffer from
  50. * the sink and passes it to HWC as both the FB target buffer and output
  51. * buffer. The HWC doesn't need to read from the FB target buffer, but does
  52. * write to the output buffer. After composition is complete, the buffer is
  53. * queued to the sink.
  54. *
  55. * On MIXED frames, things become more complicated, since some h/w composer
  56. * implementations can't read from and write to the same buffer. This class has
  57. * an internal BufferQueue that it uses as a scratch buffer pool. The GLES
  58. * driver is given a scratch buffer to render into. When it finishes rendering,
  59. * the buffer is queued and then immediately acquired by the
  60. * VirtualDisplaySurface. The scratch buffer is then used as the FB target
  61. * buffer for HWC, and a separate buffer is dequeued from the sink and used as
  62. * the HWC output buffer. When HWC composition is complete, the scratch buffer
  63. * is released and the output buffer is queued to the sink.
  64. */
  65. class VirtualDisplaySurface : public DisplaySurface,
  66. public BnGraphicBufferProducer,
  67. private ConsumerBase {
  68. public:
  69. #ifdef QTI_BSP
  70. friend class ExVirtualDisplaySurface;
  71. #endif
  72. VirtualDisplaySurface(HWComposer& hwc, int32_t dispId,
  73. const sp<IGraphicBufferProducer>& sink,
  74. const sp<IGraphicBufferProducer>& bqProducer,
  75. const sp<IGraphicBufferConsumer>& bqConsumer,
  76. const String8& name);
  77. //
  78. // DisplaySurface interface
  79. //
  80. virtual status_t beginFrame(bool mustRecompose);
  81. virtual status_t prepareFrame(CompositionType compositionType);
  82. virtual status_t compositionComplete();
  83. virtual status_t advanceFrame();
  84. virtual void onFrameCommitted();
  85. virtual void dumpAsString(String8& result) const;
  86. virtual void resizeBuffers(const uint32_t w, const uint32_t h);
  87. private:
  88. enum Source {SOURCE_SINK = 0, SOURCE_SCRATCH = 1};
  89. virtual ~VirtualDisplaySurface();
  90. //
  91. // IGraphicBufferProducer interface, used by the GLES driver.
  92. //
  93. virtual status_t requestBuffer(int pslot, sp<GraphicBuffer>* outBuf);
  94. virtual status_t setBufferCount(int bufferCount);
  95. virtual status_t dequeueBuffer(int* pslot, sp<Fence>* fence, bool async,
  96. uint32_t w, uint32_t h, PixelFormat format, uint32_t usage);
  97. virtual status_t detachBuffer(int slot);
  98. virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer,
  99. sp<Fence>* outFence);
  100. virtual status_t attachBuffer(int* slot, const sp<GraphicBuffer>& buffer);
  101. virtual status_t queueBuffer(int pslot,
  102. const QueueBufferInput& input, QueueBufferOutput* output);
  103. virtual void cancelBuffer(int pslot, const sp<Fence>& fence);
  104. virtual int query(int what, int* value);
  105. virtual status_t connect(const sp<IProducerListener>& listener,
  106. int api, bool producerControlledByApp, QueueBufferOutput* output);
  107. virtual status_t disconnect(int api);
  108. virtual status_t setSidebandStream(const sp<NativeHandle>& stream);
  109. virtual void allocateBuffers(bool async, uint32_t width, uint32_t height,
  110. PixelFormat format, uint32_t usage);
  111. virtual status_t allowAllocation(bool allow);
  112. virtual status_t setGenerationNumber(uint32_t generationNumber);
  113. virtual String8 getConsumerName() const override;
  114. virtual void setOutputUsage(uint32_t flag);
  115. //
  116. // Utility methods
  117. //
  118. static Source fbSourceForCompositionType(CompositionType type);
  119. status_t dequeueBuffer(Source source, PixelFormat format, uint32_t usage,
  120. int* sslot, sp<Fence>* fence);
  121. void updateQueueBufferOutput(const QueueBufferOutput& qbo);
  122. void resetPerFrameState();
  123. status_t refreshOutputBuffer();
  124. // Both the sink and scratch buffer pools have their own set of slots
  125. // ("source slots", or "sslot"). We have to merge these into the single
  126. // set of slots used by the GLES producer ("producer slots" or "pslot") and
  127. // internally in the VirtualDisplaySurface. To minimize the number of times
  128. // a producer slot switches which source it comes from, we map source slot
  129. // numbers to producer slot numbers differently for each source.
  130. static int mapSource2ProducerSlot(Source source, int sslot);
  131. static int mapProducer2SourceSlot(Source source, int pslot);
  132. //
  133. // Immutable after construction
  134. //
  135. HWComposer& mHwc;
  136. const int32_t mDisplayId;
  137. const String8 mDisplayName;
  138. sp<IGraphicBufferProducer> mSource[2]; // indexed by SOURCE_*
  139. uint32_t mDefaultOutputFormat;
  140. //
  141. // Inter-frame state
  142. //
  143. // To avoid buffer reallocations, we track the buffer usage and format
  144. // we used on the previous frame and use it again on the new frame. If
  145. // the composition type changes or the GLES driver starts requesting
  146. // different usage/format, we'll get a new buffer.
  147. uint32_t mOutputFormat;
  148. uint32_t mOutputUsage;
  149. // Since we present a single producer interface to the GLES driver, but
  150. // are internally muxing between the sink and scratch producers, we have
  151. // to keep track of which source last returned each producer slot from
  152. // dequeueBuffer. Each bit in mProducerSlotSource corresponds to a producer
  153. // slot. Both mProducerSlotSource and mProducerBuffers are indexed by a
  154. // "producer slot"; see the mapSlot*() functions.
  155. uint64_t mProducerSlotSource;
  156. sp<GraphicBuffer> mProducerBuffers[BufferQueue::NUM_BUFFER_SLOTS];
  157. // The QueueBufferOutput with the latest info from the sink, and with the
  158. // transform hint cleared. Since we defer queueBuffer from the GLES driver
  159. // to the sink, we have to return the previous version.
  160. QueueBufferOutput mQueueBufferOutput;
  161. // Details of the current sink buffer. These become valid when a buffer is
  162. // dequeued from the sink, and are used when queueing the buffer.
  163. uint32_t mSinkBufferWidth, mSinkBufferHeight;
  164. //
  165. // Intra-frame state
  166. //
  167. // Composition type and GLES buffer source for the current frame.
  168. // Valid after prepareFrame(), cleared in onFrameCommitted.
  169. CompositionType mCompositionType;
  170. // mFbFence is the fence HWC should wait for before reading the framebuffer
  171. // target buffer.
  172. sp<Fence> mFbFence;
  173. // mOutputFence is the fence HWC should wait for before writing to the
  174. // output buffer.
  175. sp<Fence> mOutputFence;
  176. // Producer slot numbers for the buffers to use for HWC framebuffer target
  177. // and output.
  178. int mFbProducerSlot;
  179. int mOutputProducerSlot;
  180. // Debug only -- track the sequence of events in each frame so we can make
  181. // sure they happen in the order we expect. This class implicitly models
  182. // a state machine; this enum/variable makes it explicit.
  183. //
  184. // +-----------+-------------------+-------------+
  185. // | State | Event || Next State |
  186. // +-----------+-------------------+-------------+
  187. // | IDLE | beginFrame || BEGUN |
  188. // | BEGUN | prepareFrame || PREPARED |
  189. // | PREPARED | dequeueBuffer [1] || GLES |
  190. // | PREPARED | advanceFrame [2] || HWC |
  191. // | GLES | queueBuffer || GLES_DONE |
  192. // | GLES_DONE | advanceFrame || HWC |
  193. // | HWC | onFrameCommitted || IDLE |
  194. // +-----------+-------------------++------------+
  195. // [1] COMPOSITION_GLES and COMPOSITION_MIXED frames.
  196. // [2] COMPOSITION_HWC frames.
  197. //
  198. enum DbgState {
  199. // no buffer dequeued, don't know anything about the next frame
  200. DBG_STATE_IDLE,
  201. // output buffer dequeued, framebuffer source not yet known
  202. DBG_STATE_BEGUN,
  203. // output buffer dequeued, framebuffer source known but not provided
  204. // to GLES yet.
  205. DBG_STATE_PREPARED,
  206. // GLES driver has a buffer dequeued
  207. DBG_STATE_GLES,
  208. // GLES driver has queued the buffer, we haven't sent it to HWC yet
  209. DBG_STATE_GLES_DONE,
  210. // HWC has the buffer for this frame
  211. DBG_STATE_HWC,
  212. };
  213. DbgState mDbgState;
  214. CompositionType mDbgLastCompositionType;
  215. const char* dbgStateStr() const;
  216. static const char* dbgSourceStr(Source s);
  217. bool mMustRecompose;
  218. };
  219. // ---------------------------------------------------------------------------
  220. } // namespace android
  221. // ---------------------------------------------------------------------------
  222. #endif // ANDROID_SF_VIRTUAL_DISPLAY_SURFACE_H