2013-03-14 21:29:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ANDROID_SF_VIRTUAL_DISPLAY_SURFACE_H
|
|
|
|
#define ANDROID_SF_VIRTUAL_DISPLAY_SURFACE_H
|
|
|
|
|
2013-04-07 06:12:29 +00:00
|
|
|
#include <gui/ConsumerBase.h>
|
|
|
|
#include <gui/IGraphicBufferProducer.h>
|
|
|
|
|
2013-03-14 21:29:29 +00:00
|
|
|
#include "DisplaySurface.h"
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
namespace android {
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class HWComposer;
|
2014-03-21 20:05:51 +00:00
|
|
|
class IProducerListener;
|
2013-03-14 21:29:29 +00:00
|
|
|
|
2013-04-07 06:12:29 +00:00
|
|
|
/* This DisplaySurface implementation supports virtual displays, where GLES
|
|
|
|
* and/or HWC compose into a buffer that is then passed to an arbitrary
|
|
|
|
* consumer (the sink) running in another process.
|
|
|
|
*
|
|
|
|
* The simplest case is when the virtual display will never use the h/w
|
|
|
|
* composer -- either the h/w composer doesn't support writing to buffers, or
|
|
|
|
* there are more virtual displays than it supports simultaneously. In this
|
|
|
|
* case, the GLES driver works directly with the output buffer queue, and
|
|
|
|
* calls to the VirtualDisplay from SurfaceFlinger and DisplayHardware do
|
|
|
|
* nothing.
|
|
|
|
*
|
|
|
|
* If h/w composer might be used, then each frame will fall into one of three
|
|
|
|
* configurations: GLES-only, HWC-only, and MIXED composition. In all of these,
|
|
|
|
* we must provide a FB target buffer and output buffer for the HWC set() call.
|
|
|
|
*
|
|
|
|
* In GLES-only composition, the GLES driver is given a buffer from the sink to
|
|
|
|
* render into. When the GLES driver queues the buffer to the
|
|
|
|
* VirtualDisplaySurface, the VirtualDisplaySurface holds onto it instead of
|
|
|
|
* immediately queueing it to the sink. The buffer is used as both the FB
|
|
|
|
* target and output buffer for HWC, though on these frames the HWC doesn't
|
|
|
|
* do any work for this display and doesn't write to the output buffer. After
|
|
|
|
* composition is complete, the buffer is queued to the sink.
|
|
|
|
*
|
|
|
|
* In HWC-only composition, the VirtualDisplaySurface dequeues a buffer from
|
|
|
|
* the sink and passes it to HWC as both the FB target buffer and output
|
|
|
|
* buffer. The HWC doesn't need to read from the FB target buffer, but does
|
|
|
|
* write to the output buffer. After composition is complete, the buffer is
|
|
|
|
* queued to the sink.
|
|
|
|
*
|
|
|
|
* On MIXED frames, things become more complicated, since some h/w composer
|
|
|
|
* implementations can't read from and write to the same buffer. This class has
|
|
|
|
* an internal BufferQueue that it uses as a scratch buffer pool. The GLES
|
|
|
|
* driver is given a scratch buffer to render into. When it finishes rendering,
|
|
|
|
* the buffer is queued and then immediately acquired by the
|
|
|
|
* VirtualDisplaySurface. The scratch buffer is then used as the FB target
|
|
|
|
* buffer for HWC, and a separate buffer is dequeued from the sink and used as
|
|
|
|
* the HWC output buffer. When HWC composition is complete, the scratch buffer
|
|
|
|
* is released and the output buffer is queued to the sink.
|
2013-03-15 19:32:10 +00:00
|
|
|
*/
|
2013-04-07 06:12:29 +00:00
|
|
|
class VirtualDisplaySurface : public DisplaySurface,
|
2013-08-02 08:40:18 +00:00
|
|
|
public BnGraphicBufferProducer,
|
2013-04-07 06:12:29 +00:00
|
|
|
private ConsumerBase {
|
2013-03-14 21:29:29 +00:00
|
|
|
public:
|
2015-10-12 23:44:11 +00:00
|
|
|
#ifdef QTI_BSP
|
2015-06-23 19:55:15 +00:00
|
|
|
friend class ExVirtualDisplaySurface;
|
2015-10-12 23:44:11 +00:00
|
|
|
#endif
|
2015-06-23 19:55:15 +00:00
|
|
|
|
2013-03-22 22:13:48 +00:00
|
|
|
VirtualDisplaySurface(HWComposer& hwc, int32_t dispId,
|
2013-03-14 21:29:29 +00:00
|
|
|
const sp<IGraphicBufferProducer>& sink,
|
2014-03-13 18:55:57 +00:00
|
|
|
const sp<IGraphicBufferProducer>& bqProducer,
|
|
|
|
const sp<IGraphicBufferConsumer>& bqConsumer,
|
2013-03-14 21:29:29 +00:00
|
|
|
const String8& name);
|
|
|
|
|
2013-04-07 06:12:29 +00:00
|
|
|
//
|
|
|
|
// DisplaySurface interface
|
|
|
|
//
|
2014-02-05 00:22:36 +00:00
|
|
|
virtual status_t beginFrame(bool mustRecompose);
|
2013-04-07 06:12:29 +00:00
|
|
|
virtual status_t prepareFrame(CompositionType compositionType);
|
2013-03-14 21:29:29 +00:00
|
|
|
virtual status_t compositionComplete();
|
|
|
|
virtual status_t advanceFrame();
|
2013-03-20 20:44:00 +00:00
|
|
|
virtual void onFrameCommitted();
|
2014-11-11 18:32:31 +00:00
|
|
|
virtual void dumpAsString(String8& result) const;
|
2014-07-18 22:34:25 +00:00
|
|
|
virtual void resizeBuffers(const uint32_t w, const uint32_t h);
|
2013-03-14 21:29:29 +00:00
|
|
|
|
|
|
|
private:
|
2013-04-07 06:12:29 +00:00
|
|
|
enum Source {SOURCE_SINK = 0, SOURCE_SCRATCH = 1};
|
|
|
|
|
2013-03-14 21:29:29 +00:00
|
|
|
virtual ~VirtualDisplaySurface();
|
|
|
|
|
2013-04-07 06:12:29 +00:00
|
|
|
//
|
|
|
|
// IGraphicBufferProducer interface, used by the GLES driver.
|
|
|
|
//
|
|
|
|
virtual status_t requestBuffer(int pslot, sp<GraphicBuffer>* outBuf);
|
|
|
|
virtual status_t setBufferCount(int bufferCount);
|
2013-07-19 05:10:56 +00:00
|
|
|
virtual status_t dequeueBuffer(int* pslot, sp<Fence>* fence, bool async,
|
2014-11-18 18:24:03 +00:00
|
|
|
uint32_t w, uint32_t h, PixelFormat format, uint32_t usage);
|
2014-03-06 23:14:33 +00:00
|
|
|
virtual status_t detachBuffer(int slot);
|
2014-03-28 22:25:31 +00:00
|
|
|
virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer,
|
|
|
|
sp<Fence>* outFence);
|
2014-03-06 23:14:33 +00:00
|
|
|
virtual status_t attachBuffer(int* slot, const sp<GraphicBuffer>& buffer);
|
2013-04-07 06:12:29 +00:00
|
|
|
virtual status_t queueBuffer(int pslot,
|
|
|
|
const QueueBufferInput& input, QueueBufferOutput* output);
|
|
|
|
virtual void cancelBuffer(int pslot, const sp<Fence>& fence);
|
|
|
|
virtual int query(int what, int* value);
|
2014-03-21 20:05:51 +00:00
|
|
|
virtual status_t connect(const sp<IProducerListener>& listener,
|
2013-09-12 02:35:45 +00:00
|
|
|
int api, bool producerControlledByApp, QueueBufferOutput* output);
|
2013-04-07 06:12:29 +00:00
|
|
|
virtual status_t disconnect(int api);
|
2014-03-03 23:42:54 +00:00
|
|
|
virtual status_t setSidebandStream(const sp<NativeHandle>& stream);
|
2014-06-20 20:13:57 +00:00
|
|
|
virtual void allocateBuffers(bool async, uint32_t width, uint32_t height,
|
2014-11-18 18:24:03 +00:00
|
|
|
PixelFormat format, uint32_t usage);
|
2015-04-17 00:28:43 +00:00
|
|
|
virtual status_t allowAllocation(bool allow);
|
2015-06-02 22:45:22 +00:00
|
|
|
virtual status_t setGenerationNumber(uint32_t generationNumber);
|
2015-06-08 16:32:50 +00:00
|
|
|
virtual String8 getConsumerName() const override;
|
2015-06-23 19:55:15 +00:00
|
|
|
virtual void setOutputUsage(uint32_t flag);
|
2013-04-07 06:12:29 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Utility methods
|
|
|
|
//
|
|
|
|
static Source fbSourceForCompositionType(CompositionType type);
|
2014-11-18 18:24:03 +00:00
|
|
|
status_t dequeueBuffer(Source source, PixelFormat format, uint32_t usage,
|
2013-08-29 23:03:50 +00:00
|
|
|
int* sslot, sp<Fence>* fence);
|
2013-04-07 06:12:29 +00:00
|
|
|
void updateQueueBufferOutput(const QueueBufferOutput& qbo);
|
|
|
|
void resetPerFrameState();
|
2013-08-20 23:35:32 +00:00
|
|
|
status_t refreshOutputBuffer();
|
2013-04-07 06:12:29 +00:00
|
|
|
|
|
|
|
// Both the sink and scratch buffer pools have their own set of slots
|
|
|
|
// ("source slots", or "sslot"). We have to merge these into the single
|
|
|
|
// set of slots used by the GLES producer ("producer slots" or "pslot") and
|
|
|
|
// internally in the VirtualDisplaySurface. To minimize the number of times
|
|
|
|
// a producer slot switches which source it comes from, we map source slot
|
|
|
|
// numbers to producer slot numbers differently for each source.
|
|
|
|
static int mapSource2ProducerSlot(Source source, int sslot);
|
|
|
|
static int mapProducer2SourceSlot(Source source, int pslot);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Immutable after construction
|
|
|
|
//
|
|
|
|
HWComposer& mHwc;
|
|
|
|
const int32_t mDisplayId;
|
|
|
|
const String8 mDisplayName;
|
|
|
|
sp<IGraphicBufferProducer> mSource[2]; // indexed by SOURCE_*
|
2013-11-05 00:43:03 +00:00
|
|
|
uint32_t mDefaultOutputFormat;
|
2013-04-07 06:12:29 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Inter-frame state
|
|
|
|
//
|
|
|
|
|
2013-09-27 16:05:09 +00:00
|
|
|
// To avoid buffer reallocations, we track the buffer usage and format
|
|
|
|
// we used on the previous frame and use it again on the new frame. If
|
|
|
|
// the composition type changes or the GLES driver starts requesting
|
|
|
|
// different usage/format, we'll get a new buffer.
|
|
|
|
uint32_t mOutputFormat;
|
|
|
|
uint32_t mOutputUsage;
|
2013-04-07 06:12:29 +00:00
|
|
|
|
|
|
|
// Since we present a single producer interface to the GLES driver, but
|
|
|
|
// are internally muxing between the sink and scratch producers, we have
|
|
|
|
// to keep track of which source last returned each producer slot from
|
2014-04-09 23:14:51 +00:00
|
|
|
// dequeueBuffer. Each bit in mProducerSlotSource corresponds to a producer
|
2013-04-07 06:12:29 +00:00
|
|
|
// slot. Both mProducerSlotSource and mProducerBuffers are indexed by a
|
|
|
|
// "producer slot"; see the mapSlot*() functions.
|
2014-04-09 23:14:51 +00:00
|
|
|
uint64_t mProducerSlotSource;
|
2013-04-07 06:12:29 +00:00
|
|
|
sp<GraphicBuffer> mProducerBuffers[BufferQueue::NUM_BUFFER_SLOTS];
|
|
|
|
|
|
|
|
// The QueueBufferOutput with the latest info from the sink, and with the
|
|
|
|
// transform hint cleared. Since we defer queueBuffer from the GLES driver
|
|
|
|
// to the sink, we have to return the previous version.
|
|
|
|
QueueBufferOutput mQueueBufferOutput;
|
|
|
|
|
2014-07-18 22:34:25 +00:00
|
|
|
// Details of the current sink buffer. These become valid when a buffer is
|
|
|
|
// dequeued from the sink, and are used when queueing the buffer.
|
|
|
|
uint32_t mSinkBufferWidth, mSinkBufferHeight;
|
|
|
|
|
2013-04-07 06:12:29 +00:00
|
|
|
//
|
|
|
|
// Intra-frame state
|
|
|
|
//
|
|
|
|
|
|
|
|
// Composition type and GLES buffer source for the current frame.
|
|
|
|
// Valid after prepareFrame(), cleared in onFrameCommitted.
|
|
|
|
CompositionType mCompositionType;
|
|
|
|
|
|
|
|
// mFbFence is the fence HWC should wait for before reading the framebuffer
|
|
|
|
// target buffer.
|
|
|
|
sp<Fence> mFbFence;
|
|
|
|
|
2013-08-20 23:35:32 +00:00
|
|
|
// mOutputFence is the fence HWC should wait for before writing to the
|
|
|
|
// output buffer.
|
|
|
|
sp<Fence> mOutputFence;
|
|
|
|
|
2013-04-07 06:12:29 +00:00
|
|
|
// Producer slot numbers for the buffers to use for HWC framebuffer target
|
|
|
|
// and output.
|
|
|
|
int mFbProducerSlot;
|
|
|
|
int mOutputProducerSlot;
|
|
|
|
|
|
|
|
// Debug only -- track the sequence of events in each frame so we can make
|
|
|
|
// sure they happen in the order we expect. This class implicitly models
|
|
|
|
// a state machine; this enum/variable makes it explicit.
|
|
|
|
//
|
|
|
|
// +-----------+-------------------+-------------+
|
|
|
|
// | State | Event || Next State |
|
|
|
|
// +-----------+-------------------+-------------+
|
2013-08-20 23:35:32 +00:00
|
|
|
// | IDLE | beginFrame || BEGUN |
|
|
|
|
// | BEGUN | prepareFrame || PREPARED |
|
2013-04-07 06:12:29 +00:00
|
|
|
// | PREPARED | dequeueBuffer [1] || GLES |
|
|
|
|
// | PREPARED | advanceFrame [2] || HWC |
|
|
|
|
// | GLES | queueBuffer || GLES_DONE |
|
|
|
|
// | GLES_DONE | advanceFrame || HWC |
|
|
|
|
// | HWC | onFrameCommitted || IDLE |
|
|
|
|
// +-----------+-------------------++------------+
|
|
|
|
// [1] COMPOSITION_GLES and COMPOSITION_MIXED frames.
|
|
|
|
// [2] COMPOSITION_HWC frames.
|
|
|
|
//
|
|
|
|
enum DbgState {
|
|
|
|
// no buffer dequeued, don't know anything about the next frame
|
|
|
|
DBG_STATE_IDLE,
|
2013-08-20 23:35:32 +00:00
|
|
|
// output buffer dequeued, framebuffer source not yet known
|
|
|
|
DBG_STATE_BEGUN,
|
|
|
|
// output buffer dequeued, framebuffer source known but not provided
|
|
|
|
// to GLES yet.
|
2013-04-07 06:12:29 +00:00
|
|
|
DBG_STATE_PREPARED,
|
|
|
|
// GLES driver has a buffer dequeued
|
|
|
|
DBG_STATE_GLES,
|
|
|
|
// GLES driver has queued the buffer, we haven't sent it to HWC yet
|
|
|
|
DBG_STATE_GLES_DONE,
|
|
|
|
// HWC has the buffer for this frame
|
|
|
|
DBG_STATE_HWC,
|
|
|
|
};
|
|
|
|
DbgState mDbgState;
|
|
|
|
CompositionType mDbgLastCompositionType;
|
|
|
|
|
|
|
|
const char* dbgStateStr() const;
|
|
|
|
static const char* dbgSourceStr(Source s);
|
2014-02-05 00:22:36 +00:00
|
|
|
|
|
|
|
bool mMustRecompose;
|
2013-03-14 21:29:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
} // namespace android
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#endif // ANDROID_SF_VIRTUAL_DISPLAY_SURFACE_H
|
|
|
|
|