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