1e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala/*
2e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala * Copyright (C) 2012 The Android Open Source Project
3e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala *
4e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala * Licensed under the Apache License, Version 2.0 (the "License");
5e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala * you may not use this file except in compliance with the License.
6e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala * You may obtain a copy of the License at
7e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala *
8e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala *      http://www.apache.org/licenses/LICENSE-2.0
9e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala *
10e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala * Unless required by applicable law or agreed to in writing, software
11e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala * distributed under the License is distributed on an "AS IS" BASIS,
12e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala * See the License for the specific language governing permissions and
14e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala * limitations under the License.
15e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala */
16e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala
17e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala#ifndef ANDROID_GUI_BUFFERITEMCONSUMER_H
18e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala#define ANDROID_GUI_BUFFERITEMCONSUMER_H
19e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala
20e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala#include <gui/ConsumerBase.h>
21e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala
22e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala#include <ui/GraphicBuffer.h>
23e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala
24e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala#include <utils/String8.h>
25e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala#include <utils/Vector.h>
26e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala#include <utils/threads.h>
27e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala
28e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala#define ANDROID_GRAPHICS_BUFFERITEMCONSUMER_JNI_ID "mBufferItemConsumer"
29e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala
30e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvalanamespace android {
31e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala
328f938a53385a3c6d1c6aa24b3f38437bb2cc47aeMathias Agopianclass BufferQueue;
338f938a53385a3c6d1c6aa24b3f38437bb2cc47aeMathias Agopian
34e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala/**
35e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala * BufferItemConsumer is a BufferQueue consumer endpoint that allows clients
36e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala * access to the whole BufferItem entry from BufferQueue. Multiple buffers may
37e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala * be acquired at once, to be used concurrently by the client. This consumer can
38e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala * operate either in synchronous or asynchronous mode.
39e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala */
40e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvalaclass BufferItemConsumer: public ConsumerBase
41e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala{
42e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala  public:
43e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    typedef ConsumerBase::FrameAvailableListener FrameAvailableListener;
44e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala
45e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    typedef BufferQueue::BufferItem BufferItem;
46e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala
47fe50d2a0a13116238d5321e16c06e6ed533618dcDan Stoza    enum { DEFAULT_MAX_BUFFERS = -1 };
48e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    enum { INVALID_BUFFER_SLOT = BufferQueue::INVALID_BUFFER_SLOT };
49e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    enum { NO_BUFFER_AVAILABLE = BufferQueue::NO_BUFFER_AVAILABLE };
50e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala
51e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // Create a new buffer item consumer. The consumerUsage parameter determines
52e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // the consumer usage flags passed to the graphics allocator. The
53e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // bufferCount parameter specifies how many buffers can be locked for user
54e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // access at the same time.
55595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian    // controlledByApp tells whether this consumer is controlled by the
56595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian    // application.
57fe50d2a0a13116238d5321e16c06e6ed533618dcDan Stoza    BufferItemConsumer(const sp<IGraphicBufferConsumer>& consumer,
58fe50d2a0a13116238d5321e16c06e6ed533618dcDan Stoza            uint32_t consumerUsage, int bufferCount = DEFAULT_MAX_BUFFERS,
59595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian            bool controlledByApp = false);
60e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala
61e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    virtual ~BufferItemConsumer();
62e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala
63e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // set the name of the BufferItemConsumer that will be used to identify it in
64e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // log messages.
65e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    void setName(const String8& name);
66e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala
67e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // Gets the next graphics buffer from the producer, filling out the
68e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // passed-in BufferItem structure. Returns NO_BUFFER_AVAILABLE if the queue
69e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // of buffers is empty, and INVALID_OPERATION if the maximum number of
70e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // buffers is already acquired.
71e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    //
72e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // Only a fixed number of buffers can be acquired at a time, determined by
73e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // the construction-time bufferCount parameter. If INVALID_OPERATION is
74e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // returned by acquireBuffer, then old buffers must be returned to the
75e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // queue by calling releaseBuffer before more buffers can be acquired.
76e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    //
77e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // If waitForFence is true, and the acquired BufferItem has a valid fence object,
78e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // acquireBuffer will wait on the fence with no timeout before returning.
791585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden    status_t acquireBuffer(BufferItem *item, nsecs_t presentWhen,
801585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden        bool waitForFence = true);
81e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala
82e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // Returns an acquired buffer to the queue, allowing it to be reused. Since
83e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // only a fixed number of buffers may be acquired at a time, old buffers
84e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // must be released by calling releaseBuffer to ensure new buffers can be
85e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // acquired by acquireBuffer. Once a BufferItem is released, the caller must
86e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // not access any members of the BufferItem, and should immediately remove
87e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    // all of its references to the BufferItem itself.
88e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala    status_t releaseBuffer(const BufferItem &item,
89e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala            const sp<Fence>& releaseFence = Fence::NO_FENCE);
90e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala
9187d1e3427b536b5cb1123ddc357539cb4075687aIgor Murashkin    // setDefaultBufferSize is used to set the size of buffers returned by
9287d1e3427b536b5cb1123ddc357539cb4075687aIgor Murashkin    // requestBuffers when a with and height of zero is requested.
9387d1e3427b536b5cb1123ddc357539cb4075687aIgor Murashkin    status_t setDefaultBufferSize(uint32_t w, uint32_t h);
9487d1e3427b536b5cb1123ddc357539cb4075687aIgor Murashkin
9587d1e3427b536b5cb1123ddc357539cb4075687aIgor Murashkin    // setDefaultBufferFormat allows the BufferQueue to create
9687d1e3427b536b5cb1123ddc357539cb4075687aIgor Murashkin    // GraphicBuffers of a defaultFormat if no format is specified
9787d1e3427b536b5cb1123ddc357539cb4075687aIgor Murashkin    // in dequeueBuffer
9887d1e3427b536b5cb1123ddc357539cb4075687aIgor Murashkin    status_t setDefaultBufferFormat(uint32_t defaultFormat);
99e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala};
100e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala
101e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala} // namespace android
102e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala
103e232fdca2a62dc5e81b550f5be8710e36174e7a6Eino-Ville Talvala#endif // ANDROID_GUI_CPUCONSUMER_H
104