VirtualDisplaySurface.h revision 9e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9
16a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen/* 26a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * Copyright 2013 The Android Open Source Project 36a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * 46a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * Licensed under the Apache License, Version 2.0 (the "License"); 56a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * you may not use this file except in compliance with the License. 66a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * You may obtain a copy of the License at 76a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * 86a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * http://www.apache.org/licenses/LICENSE-2.0 96a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * 106a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * Unless required by applicable law or agreed to in writing, software 116a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * distributed under the License is distributed on an "AS IS" BASIS, 126a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 136a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * See the License for the specific language governing permissions and 146a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * limitations under the License. 156a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen */ 166a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen 176a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen#ifndef ANDROID_SF_VIRTUAL_DISPLAY_SURFACE_H 186a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen#define ANDROID_SF_VIRTUAL_DISPLAY_SURFACE_H 193bbc90992645934df523762f7dcdb097eae366d5Joseph Wen 203bbc90992645934df523762f7dcdb097eae366d5Joseph Wen#include <gui/ConsumerBase.h> 216a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen#include <gui/IGraphicBufferProducer.h> 226a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen 236a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen#include "DisplaySurface.h" 246a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen 253bbc90992645934df523762f7dcdb097eae366d5Joseph Wen// --------------------------------------------------------------------------- 263bbc90992645934df523762f7dcdb097eae366d5Joseph Wennamespace android { 276a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen// --------------------------------------------------------------------------- 286a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen 296a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wenclass HWComposer; 306a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wenclass IProducerListener; 316a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen 326a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen/* This DisplaySurface implementation supports virtual displays, where GLES 336a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * and/or HWC compose into a buffer that is then passed to an arbitrary 346a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * consumer (the sink) running in another process. 353bbc90992645934df523762f7dcdb097eae366d5Joseph Wen * 363bbc90992645934df523762f7dcdb097eae366d5Joseph Wen * The simplest case is when the virtual display will never use the h/w 373bbc90992645934df523762f7dcdb097eae366d5Joseph Wen * composer -- either the h/w composer doesn't support writing to buffers, or 386a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * there are more virtual displays than it supports simultaneously. In this 396a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * case, the GLES driver works directly with the output buffer queue, and 406a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * calls to the VirtualDisplay from SurfaceFlinger and DisplayHardware do 416a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * nothing. 423bbc90992645934df523762f7dcdb097eae366d5Joseph Wen * 436a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * If h/w composer might be used, then each frame will fall into one of three 446a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * configurations: GLES-only, HWC-only, and MIXED composition. In all of these, 456a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * we must provide a FB target buffer and output buffer for the HWC set() call. 463bbc90992645934df523762f7dcdb097eae366d5Joseph Wen * 473bbc90992645934df523762f7dcdb097eae366d5Joseph Wen * In GLES-only composition, the GLES driver is given a buffer from the sink to 483bbc90992645934df523762f7dcdb097eae366d5Joseph Wen * render into. When the GLES driver queues the buffer to the 493bbc90992645934df523762f7dcdb097eae366d5Joseph Wen * VirtualDisplaySurface, the VirtualDisplaySurface holds onto it instead of 503bbc90992645934df523762f7dcdb097eae366d5Joseph Wen * immediately queueing it to the sink. The buffer is used as both the FB 513bbc90992645934df523762f7dcdb097eae366d5Joseph Wen * target and output buffer for HWC, though on these frames the HWC doesn't 523bbc90992645934df523762f7dcdb097eae366d5Joseph Wen * do any work for this display and doesn't write to the output buffer. After 533bbc90992645934df523762f7dcdb097eae366d5Joseph Wen * composition is complete, the buffer is queued to the sink. 543bbc90992645934df523762f7dcdb097eae366d5Joseph Wen * 553bbc90992645934df523762f7dcdb097eae366d5Joseph Wen * In HWC-only composition, the VirtualDisplaySurface dequeues a buffer from 563bbc90992645934df523762f7dcdb097eae366d5Joseph Wen * the sink and passes it to HWC as both the FB target buffer and output 573bbc90992645934df523762f7dcdb097eae366d5Joseph Wen * buffer. The HWC doesn't need to read from the FB target buffer, but does 586a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * write to the output buffer. After composition is complete, the buffer is 596a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * queued to the sink. 606a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * 613bbc90992645934df523762f7dcdb097eae366d5Joseph Wen * On MIXED frames, things become more complicated, since some h/w composer 626a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * implementations can't read from and write to the same buffer. This class has 636a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * an internal BufferQueue that it uses as a scratch buffer pool. The GLES 646a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * driver is given a scratch buffer to render into. When it finishes rendering, 656a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * the buffer is queued and then immediately acquired by the 666a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * VirtualDisplaySurface. The scratch buffer is then used as the FB target 676a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * buffer for HWC, and a separate buffer is dequeued from the sink and used as 686a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * the HWC output buffer. When HWC composition is complete, the scratch buffer 696a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen * is released and the output buffer is queued to the sink. 703bbc90992645934df523762f7dcdb097eae366d5Joseph Wen */ 713bbc90992645934df523762f7dcdb097eae366d5Joseph Wenclass VirtualDisplaySurface : public DisplaySurface, 723bbc90992645934df523762f7dcdb097eae366d5Joseph Wen public BnGraphicBufferProducer, 733bbc90992645934df523762f7dcdb097eae366d5Joseph Wen private ConsumerBase { 743bbc90992645934df523762f7dcdb097eae366d5Joseph Wenpublic: 753bbc90992645934df523762f7dcdb097eae366d5Joseph Wen VirtualDisplaySurface(HWComposer& hwc, int32_t dispId, 763bbc90992645934df523762f7dcdb097eae366d5Joseph Wen const sp<IGraphicBufferProducer>& sink, 773bbc90992645934df523762f7dcdb097eae366d5Joseph Wen const sp<IGraphicBufferProducer>& bqProducer, 783bbc90992645934df523762f7dcdb097eae366d5Joseph Wen const sp<IGraphicBufferConsumer>& bqConsumer, 793bbc90992645934df523762f7dcdb097eae366d5Joseph Wen const String8& name); 803bbc90992645934df523762f7dcdb097eae366d5Joseph Wen 813bbc90992645934df523762f7dcdb097eae366d5Joseph Wen // 826a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen // DisplaySurface interface 836a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen // 843bbc90992645934df523762f7dcdb097eae366d5Joseph Wen virtual status_t beginFrame(bool mustRecompose); 853bbc90992645934df523762f7dcdb097eae366d5Joseph Wen virtual status_t prepareFrame(CompositionType compositionType); 863bbc90992645934df523762f7dcdb097eae366d5Joseph Wen#ifndef USE_HWC2 876a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen virtual status_t compositionComplete(); 886a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen#endif 896a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen virtual status_t advanceFrame(); 903bbc90992645934df523762f7dcdb097eae366d5Joseph Wen virtual void onFrameCommitted(); 913bbc90992645934df523762f7dcdb097eae366d5Joseph Wen virtual void dumpAsString(String8& result) const; 923bbc90992645934df523762f7dcdb097eae366d5Joseph Wen virtual void resizeBuffers(const uint32_t w, const uint32_t h); 933bbc90992645934df523762f7dcdb097eae366d5Joseph Wen#ifdef USE_HWC2 943bbc90992645934df523762f7dcdb097eae366d5Joseph Wen virtual const sp<Fence>& getClientTargetAcquireFence() const override; 953bbc90992645934df523762f7dcdb097eae366d5Joseph Wen#endif 963bbc90992645934df523762f7dcdb097eae366d5Joseph Wen 973bbc90992645934df523762f7dcdb097eae366d5Joseph Wenprivate: 983bbc90992645934df523762f7dcdb097eae366d5Joseph Wen enum Source {SOURCE_SINK = 0, SOURCE_SCRATCH = 1}; 993bbc90992645934df523762f7dcdb097eae366d5Joseph Wen 1003bbc90992645934df523762f7dcdb097eae366d5Joseph Wen virtual ~VirtualDisplaySurface(); 1013bbc90992645934df523762f7dcdb097eae366d5Joseph Wen 1023bbc90992645934df523762f7dcdb097eae366d5Joseph Wen // 1036a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen // IGraphicBufferProducer interface, used by the GLES driver. 1046a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen // 1056a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen virtual status_t requestBuffer(int pslot, sp<GraphicBuffer>* outBuf); 1066a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen virtual status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers); 1076a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen virtual status_t setAsyncMode(bool async); 1086a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen virtual status_t dequeueBuffer(int* pslot, sp<Fence>* fence, uint32_t w, 1096a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen uint32_t h, PixelFormat format, uint32_t usage); 1106a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen virtual status_t detachBuffer(int slot); 1116a34bb2d6a6cbc7a70bdf0c53d238dc28e0b1d58Joseph Wen virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer, 112 sp<Fence>* outFence); 113 virtual status_t attachBuffer(int* slot, const sp<GraphicBuffer>& buffer); 114 virtual status_t queueBuffer(int pslot, 115 const QueueBufferInput& input, QueueBufferOutput* output); 116 virtual status_t cancelBuffer(int pslot, const sp<Fence>& fence); 117 virtual int query(int what, int* value); 118 virtual status_t connect(const sp<IProducerListener>& listener, 119 int api, bool producerControlledByApp, QueueBufferOutput* output); 120 virtual status_t disconnect(int api); 121 virtual status_t setSidebandStream(const sp<NativeHandle>& stream); 122 virtual void allocateBuffers(uint32_t width, uint32_t height, 123 PixelFormat format, uint32_t usage); 124 virtual status_t allowAllocation(bool allow); 125 virtual status_t setGenerationNumber(uint32_t generationNumber); 126 virtual String8 getConsumerName() const override; 127 virtual uint64_t getNextFrameNumber() const override; 128 virtual status_t setSingleBufferMode(bool singleBufferMode) override; 129 virtual status_t setAutoRefresh(bool autoRefresh) override; 130 virtual status_t setDequeueTimeout(nsecs_t timeout) override; 131 132 // 133 // Utility methods 134 // 135 static Source fbSourceForCompositionType(CompositionType type); 136 status_t dequeueBuffer(Source source, PixelFormat format, uint32_t usage, 137 int* sslot, sp<Fence>* fence); 138 void updateQueueBufferOutput(const QueueBufferOutput& qbo); 139 void resetPerFrameState(); 140 status_t refreshOutputBuffer(); 141 142 // Both the sink and scratch buffer pools have their own set of slots 143 // ("source slots", or "sslot"). We have to merge these into the single 144 // set of slots used by the GLES producer ("producer slots" or "pslot") and 145 // internally in the VirtualDisplaySurface. To minimize the number of times 146 // a producer slot switches which source it comes from, we map source slot 147 // numbers to producer slot numbers differently for each source. 148 static int mapSource2ProducerSlot(Source source, int sslot); 149 static int mapProducer2SourceSlot(Source source, int pslot); 150 151 // 152 // Immutable after construction 153 // 154 HWComposer& mHwc; 155 const int32_t mDisplayId; 156 const String8 mDisplayName; 157 sp<IGraphicBufferProducer> mSource[2]; // indexed by SOURCE_* 158 uint32_t mDefaultOutputFormat; 159 160 // 161 // Inter-frame state 162 // 163 164 // To avoid buffer reallocations, we track the buffer usage and format 165 // we used on the previous frame and use it again on the new frame. If 166 // the composition type changes or the GLES driver starts requesting 167 // different usage/format, we'll get a new buffer. 168 uint32_t mOutputFormat; 169 uint32_t mOutputUsage; 170 171 // Since we present a single producer interface to the GLES driver, but 172 // are internally muxing between the sink and scratch producers, we have 173 // to keep track of which source last returned each producer slot from 174 // dequeueBuffer. Each bit in mProducerSlotSource corresponds to a producer 175 // slot. Both mProducerSlotSource and mProducerBuffers are indexed by a 176 // "producer slot"; see the mapSlot*() functions. 177 uint64_t mProducerSlotSource; 178 sp<GraphicBuffer> mProducerBuffers[BufferQueue::NUM_BUFFER_SLOTS]; 179 180 // The QueueBufferOutput with the latest info from the sink, and with the 181 // transform hint cleared. Since we defer queueBuffer from the GLES driver 182 // to the sink, we have to return the previous version. 183 QueueBufferOutput mQueueBufferOutput; 184 185 // Details of the current sink buffer. These become valid when a buffer is 186 // dequeued from the sink, and are used when queueing the buffer. 187 uint32_t mSinkBufferWidth, mSinkBufferHeight; 188 189 // 190 // Intra-frame state 191 // 192 193 // Composition type and GLES buffer source for the current frame. 194 // Valid after prepareFrame(), cleared in onFrameCommitted. 195 CompositionType mCompositionType; 196 197 // mFbFence is the fence HWC should wait for before reading the framebuffer 198 // target buffer. 199 sp<Fence> mFbFence; 200 201 // mOutputFence is the fence HWC should wait for before writing to the 202 // output buffer. 203 sp<Fence> mOutputFence; 204 205 // Producer slot numbers for the buffers to use for HWC framebuffer target 206 // and output. 207 int mFbProducerSlot; 208 int mOutputProducerSlot; 209 210 // Debug only -- track the sequence of events in each frame so we can make 211 // sure they happen in the order we expect. This class implicitly models 212 // a state machine; this enum/variable makes it explicit. 213 // 214 // +-----------+-------------------+-------------+ 215 // | State | Event || Next State | 216 // +-----------+-------------------+-------------+ 217 // | IDLE | beginFrame || BEGUN | 218 // | BEGUN | prepareFrame || PREPARED | 219 // | PREPARED | dequeueBuffer [1] || GLES | 220 // | PREPARED | advanceFrame [2] || HWC | 221 // | GLES | queueBuffer || GLES_DONE | 222 // | GLES_DONE | advanceFrame || HWC | 223 // | HWC | onFrameCommitted || IDLE | 224 // +-----------+-------------------++------------+ 225 // [1] COMPOSITION_GLES and COMPOSITION_MIXED frames. 226 // [2] COMPOSITION_HWC frames. 227 // 228 enum DbgState { 229 // no buffer dequeued, don't know anything about the next frame 230 DBG_STATE_IDLE, 231 // output buffer dequeued, framebuffer source not yet known 232 DBG_STATE_BEGUN, 233 // output buffer dequeued, framebuffer source known but not provided 234 // to GLES yet. 235 DBG_STATE_PREPARED, 236 // GLES driver has a buffer dequeued 237 DBG_STATE_GLES, 238 // GLES driver has queued the buffer, we haven't sent it to HWC yet 239 DBG_STATE_GLES_DONE, 240 // HWC has the buffer for this frame 241 DBG_STATE_HWC, 242 }; 243 DbgState mDbgState; 244 CompositionType mDbgLastCompositionType; 245 246 const char* dbgStateStr() const; 247 static const char* dbgSourceStr(Source s); 248 249 bool mMustRecompose; 250}; 251 252// --------------------------------------------------------------------------- 253} // namespace android 254// --------------------------------------------------------------------------- 255 256#endif // ANDROID_SF_VIRTUAL_DISPLAY_SURFACE_H 257 258