2014-02-28 19:17:17 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 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_GUI_BUFFERQUEUECORE_H
|
|
|
|
#define ANDROID_GUI_BUFFERQUEUECORE_H
|
|
|
|
|
2014-03-03 18:16:19 +00:00
|
|
|
#include <gui/BufferQueueDefs.h>
|
2014-02-28 19:17:17 +00:00
|
|
|
#include <gui/BufferSlot.h>
|
|
|
|
|
|
|
|
#include <utils/Condition.h>
|
|
|
|
#include <utils/Mutex.h>
|
|
|
|
#include <utils/RefBase.h>
|
|
|
|
#include <utils/String8.h>
|
|
|
|
#include <utils/StrongPointer.h>
|
|
|
|
#include <utils/Trace.h>
|
|
|
|
#include <utils/Vector.h>
|
|
|
|
|
|
|
|
#define BQ_LOGV(x, ...) ALOGV("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
|
|
|
|
#define BQ_LOGD(x, ...) ALOGD("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
|
|
|
|
#define BQ_LOGI(x, ...) ALOGI("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
|
|
|
|
#define BQ_LOGW(x, ...) ALOGW("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
|
|
|
|
#define BQ_LOGE(x, ...) ALOGE("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
|
|
|
|
|
|
|
|
#define ATRACE_BUFFER_INDEX(index) \
|
|
|
|
if (ATRACE_ENABLED()) { \
|
|
|
|
char ___traceBuf[1024]; \
|
|
|
|
snprintf(___traceBuf, 1024, "%s: %d", \
|
|
|
|
mCore->mConsumerName.string(), (index)); \
|
|
|
|
android::ScopedTrace ___bufTracer(ATRACE_TAG, ___traceBuf); \
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
|
|
|
class BufferItem;
|
|
|
|
class IBinder;
|
|
|
|
class IConsumerListener;
|
|
|
|
class IGraphicBufferAlloc;
|
|
|
|
|
|
|
|
class BufferQueueCore : public virtual RefBase {
|
|
|
|
|
|
|
|
friend class BufferQueueProducer;
|
|
|
|
friend class BufferQueueConsumer;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Used as a placeholder slot number when the value isn't pointing to an
|
|
|
|
// existing buffer.
|
|
|
|
enum { INVALID_BUFFER_SLOT = -1 }; // TODO: Extract from IGBC::BufferItem
|
|
|
|
|
|
|
|
// We reserve two slots in order to guarantee that the producer and
|
|
|
|
// consumer can run asynchronously.
|
2014-03-03 18:16:19 +00:00
|
|
|
enum { MAX_MAX_ACQUIRED_BUFFERS = BufferQueueDefs::NUM_BUFFER_SLOTS - 2 };
|
2014-02-28 19:17:17 +00:00
|
|
|
|
|
|
|
// The default API number used to indicate that no producer is connected
|
|
|
|
enum { NO_CONNECTED_API = 0 };
|
|
|
|
|
|
|
|
typedef Vector<BufferItem> Fifo;
|
|
|
|
|
|
|
|
// BufferQueueCore manages a pool of gralloc memory slots to be used by
|
|
|
|
// producers and consumers. allocator is used to allocate all the needed
|
|
|
|
// gralloc buffers.
|
|
|
|
BufferQueueCore(const sp<IGraphicBufferAlloc>& allocator = NULL);
|
|
|
|
virtual ~BufferQueueCore();
|
|
|
|
|
|
|
|
private:
|
2014-03-03 18:16:19 +00:00
|
|
|
// Dump our state in a string
|
2014-02-28 19:17:17 +00:00
|
|
|
void dump(String8& result, const char* prefix) const;
|
|
|
|
|
2014-03-03 18:16:19 +00:00
|
|
|
// getMinUndequeuedBufferCountLocked returns the minimum number of buffers
|
|
|
|
// that must remain in a state other than DEQUEUED. The async parameter
|
|
|
|
// tells whether we're in asynchronous mode.
|
2014-02-28 19:17:17 +00:00
|
|
|
int getMinUndequeuedBufferCountLocked(bool async) const;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// getMinMaxBufferCountLocked returns the minimum number of buffers allowed
|
|
|
|
// given the current BufferQueue state. The async parameter tells whether
|
|
|
|
// we're in asynchonous mode.
|
2014-02-28 19:17:17 +00:00
|
|
|
int getMinMaxBufferCountLocked(bool async) const;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// getMaxBufferCountLocked returns the maximum number of buffers that can be
|
|
|
|
// allocated at once. This value depends on the following member variables:
|
|
|
|
//
|
|
|
|
// mDequeueBufferCannotBlock
|
|
|
|
// mMaxAcquiredBufferCount
|
|
|
|
// mDefaultMaxBufferCount
|
|
|
|
// mOverrideMaxBufferCount
|
|
|
|
// async parameter
|
|
|
|
//
|
|
|
|
// Any time one of these member variables is changed while a producer is
|
|
|
|
// connected, mDequeueCondition must be broadcast.
|
2014-02-28 19:17:17 +00:00
|
|
|
int getMaxBufferCountLocked(bool async) const;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// setDefaultMaxBufferCountLocked sets the maximum number of buffer slots
|
|
|
|
// that will be used if the producer does not override the buffer slot
|
|
|
|
// count. The count must be between 2 and NUM_BUFFER_SLOTS, inclusive. The
|
|
|
|
// initial default is 2.
|
2014-02-28 19:17:17 +00:00
|
|
|
status_t setDefaultMaxBufferCountLocked(int count);
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// freeBufferLocked frees the GraphicBuffer and sync resources for the
|
|
|
|
// given slot.
|
2014-02-28 19:17:17 +00:00
|
|
|
void freeBufferLocked(int slot);
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// freeAllBuffersLocked frees the GraphicBuffer and sync resources for
|
|
|
|
// all slots.
|
2014-02-28 19:17:17 +00:00
|
|
|
void freeAllBuffersLocked();
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// stillTracking returns true iff the buffer item is still being tracked
|
|
|
|
// in one of the slots.
|
2014-02-28 19:17:17 +00:00
|
|
|
bool stillTracking(const BufferItem* item) const;
|
|
|
|
|
2014-03-03 18:16:19 +00:00
|
|
|
// mAllocator is the connection to SurfaceFlinger that is used to allocate
|
|
|
|
// new GraphicBuffer objects.
|
|
|
|
sp<IGraphicBufferAlloc> mAllocator;
|
|
|
|
|
|
|
|
// mMutex is the mutex used to prevent concurrent access to the member
|
|
|
|
// variables of BufferQueueCore objects. It must be locked whenever any
|
|
|
|
// member variable is accessed.
|
2014-02-28 19:17:17 +00:00
|
|
|
mutable Mutex mMutex;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// mIsAbandoned indicates that the BufferQueue will no longer be used to
|
|
|
|
// consume image buffers pushed to it using the IGraphicBufferProducer
|
|
|
|
// interface. It is initialized to false, and set to true in the
|
|
|
|
// consumerDisconnect method. A BufferQueue that is abandoned will return
|
|
|
|
// the NO_INIT error from all IGraphicBufferProducer methods capable of
|
|
|
|
// returning an error.
|
2014-02-28 19:17:17 +00:00
|
|
|
bool mIsAbandoned;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// mConsumerControlledByApp indicates whether the connected consumer is
|
|
|
|
// controlled by the application.
|
2014-02-28 19:17:17 +00:00
|
|
|
bool mConsumerControlledByApp;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// mConsumerName is a string used to identify the BufferQueue in log
|
|
|
|
// messages. It is set by the IGraphicBufferConsumer::setConsumerName
|
|
|
|
// method.
|
2014-02-28 19:17:17 +00:00
|
|
|
String8 mConsumerName;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// mConsumerListener is used to notify the connected consumer of
|
|
|
|
// asynchronous events that it may wish to react to. It is initially
|
|
|
|
// set to NULL and is written by consumerConnect and consumerDisconnect.
|
2014-02-28 19:17:17 +00:00
|
|
|
sp<IConsumerListener> mConsumerListener;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// mConsumerUsageBits contains flags that the consumer wants for
|
|
|
|
// GraphicBuffers.
|
2014-02-28 19:17:17 +00:00
|
|
|
uint32_t mConsumerUsageBits;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// mConnectedApi indicates the producer API that is currently connected
|
|
|
|
// to this BufferQueue. It defaults to NO_CONNECTED_API, and gets updated
|
|
|
|
// by the connect and disconnect methods.
|
2014-02-28 19:17:17 +00:00
|
|
|
int mConnectedApi;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// mConnectedProducerToken is used to set a binder death notification on
|
|
|
|
// the producer.
|
2014-02-28 19:17:17 +00:00
|
|
|
sp<IBinder> mConnectedProducerToken;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// mSlots is an array of buffer slots that must be mirrored on the producer
|
|
|
|
// side. This allows buffer ownership to be transferred between the producer
|
|
|
|
// and consumer without sending a GraphicBuffer over Binder. The entire
|
|
|
|
// array is initialized to NULL at construction time, and buffers are
|
|
|
|
// allocated for a slot when requestBuffer is called with that slot's index.
|
|
|
|
BufferQueueDefs::SlotsType mSlots;
|
|
|
|
|
|
|
|
// mQueue is a FIFO of queued buffers used in synchronous mode.
|
2014-02-28 19:17:17 +00:00
|
|
|
Fifo mQueue;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// mOverrideMaxBufferCount is the limit on the number of buffers that will
|
|
|
|
// be allocated at one time. This value is set by the producer by calling
|
|
|
|
// setBufferCount. The default is 0, which means that the producer doesn't
|
|
|
|
// care about the number of buffers in the pool. In that case,
|
|
|
|
// mDefaultMaxBufferCount is used as the limit.
|
2014-02-28 19:17:17 +00:00
|
|
|
int mOverrideMaxBufferCount;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// mDequeueCondition is a condition variable used for dequeueBuffer in
|
|
|
|
// synchronous mode.
|
2014-02-28 19:17:17 +00:00
|
|
|
mutable Condition mDequeueCondition;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// mUseAsyncBuffer indicates whether an extra buffer is used in async mode
|
|
|
|
// to prevent dequeueBuffer from blocking.
|
2014-02-28 19:17:17 +00:00
|
|
|
bool mUseAsyncBuffer;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// mDequeueBufferCannotBlock indicates whether dequeueBuffer is allowed to
|
|
|
|
// block. This flag is set during connect when both the producer and
|
|
|
|
// consumer are controlled by the application.
|
2014-02-28 19:17:17 +00:00
|
|
|
bool mDequeueBufferCannotBlock;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// mDefaultBufferFormat can be set so it will override the buffer format
|
|
|
|
// when it isn't specified in dequeueBuffer.
|
2014-02-28 19:17:17 +00:00
|
|
|
uint32_t mDefaultBufferFormat;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// mDefaultWidth holds the default width of allocated buffers. It is used
|
|
|
|
// in dequeueBuffer if a width and height of 0 are specified.
|
2014-02-28 19:17:17 +00:00
|
|
|
int mDefaultWidth;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// mDefaultHeight holds the default height of allocated buffers. It is used
|
|
|
|
// in dequeueBuffer if a width and height of 0 are specified.
|
2014-02-28 19:17:17 +00:00
|
|
|
int mDefaultHeight;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// mDefaultMaxBufferCount is the default limit on the number of buffers that
|
|
|
|
// will be allocated at one time. This default limit is set by the consumer.
|
|
|
|
// The limit (as opposed to the default limit) may be overriden by the
|
|
|
|
// producer.
|
2014-02-28 19:17:17 +00:00
|
|
|
int mDefaultMaxBufferCount;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// mMaxAcquiredBufferCount is the number of buffers that the consumer may
|
|
|
|
// acquire at one time. It defaults to 1, and can be changed by the consumer
|
|
|
|
// via setMaxAcquiredBufferCount, but this may only be done while no
|
|
|
|
// producer is connected to the BufferQueue. This value is used to derive
|
|
|
|
// the value returned for the MIN_UNDEQUEUED_BUFFERS query to the producer.
|
2014-02-28 19:17:17 +00:00
|
|
|
int mMaxAcquiredBufferCount;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// mBufferHasBeenQueued is true once a buffer has been queued. It is reset
|
|
|
|
// when something causes all buffers to be freed (e.g., changing the buffer
|
|
|
|
// count).
|
2014-02-28 19:17:17 +00:00
|
|
|
bool mBufferHasBeenQueued;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// mFrameCounter is the free running counter, incremented on every
|
|
|
|
// successful queueBuffer call and buffer allocation.
|
2014-02-28 19:17:17 +00:00
|
|
|
uint64_t mFrameCounter;
|
2014-03-03 18:16:19 +00:00
|
|
|
|
|
|
|
// mTransformHint is used to optimize for screen rotations.
|
2014-02-28 19:17:17 +00:00
|
|
|
uint32_t mTransformHint;
|
|
|
|
|
|
|
|
}; // class BufferQueueCore
|
|
|
|
|
|
|
|
} // namespace android
|
|
|
|
|
|
|
|
#endif
|