blob: 7f063aaa80380f513dd27eca2566a33240ee7b42 [file] [log] [blame]
Dan Stoza289ade12014-02-28 11:17:17 -08001/*
2 * Copyright 2014 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_GUI_BUFFERQUEUEPRODUCER_H
18#define ANDROID_GUI_BUFFERQUEUEPRODUCER_H
19
20#include <gui/BufferQueueCore.h>
21#include <gui/IGraphicBufferProducer.h>
22
23namespace android {
24
25class BufferSlot;
26
27class BufferQueueProducer : public BnGraphicBufferProducer,
28 private IBinder::DeathRecipient {
29
30public:
31 BufferQueueProducer(const sp<BufferQueueCore>& core);
32 virtual ~BufferQueueProducer();
33
34 // requestBuffer returns the GraphicBuffer for slot N.
35 //
36 // In normal operation, this is called the first time slot N is returned
37 // by dequeueBuffer. It must be called again if dequeueBuffer returns
38 // flags indicating that previously-returned buffers are no longer valid.
39 virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf);
40
41 // setBufferCount updates the number of available buffer slots. If this
42 // method succeeds, buffer slots will be both unallocated and owned by
43 // the BufferQueue object (i.e. they are not owned by the producer or
44 // consumer).
45 //
46 // This will fail if the producer has dequeued any buffers, or if
47 // bufferCount is invalid. bufferCount must generally be a value
48 // between the minimum undequeued buffer count (exclusive) and NUM_BUFFER_SLOTS
49 // (inclusive). It may also be set to zero (the default) to indicate
50 // that the producer does not wish to set a value. The minimum value
51 // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
52 // ...).
53 //
54 // This may only be called by the producer. The consumer will be told
55 // to discard buffers through the onBuffersReleased callback.
56 virtual status_t setBufferCount(int bufferCount);
57
58 // dequeueBuffer gets the next buffer slot index for the producer to use.
59 // If a buffer slot is available then that slot index is written to the
60 // location pointed to by the buf argument and a status of OK is returned.
61 // If no slot is available then a status of -EBUSY is returned and buf is
62 // unmodified.
63 //
64 // The outFence parameter will be updated to hold the fence associated with
65 // the buffer. The contents of the buffer must not be overwritten until the
66 // fence signals. If the fence is Fence::NO_FENCE, the buffer may be
67 // written immediately.
68 //
69 // The width and height parameters must be no greater than the minimum of
70 // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
71 // An error due to invalid dimensions might not be reported until
72 // updateTexImage() is called. If width and height are both zero, the
73 // default values specified by setDefaultBufferSize() are used instead.
74 //
75 // The pixel formats are enumerated in graphics.h, e.g.
76 // HAL_PIXEL_FORMAT_RGBA_8888. If the format is 0, the default format
77 // will be used.
78 //
79 // The usage argument specifies gralloc buffer usage flags. The values
80 // are enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER. These
81 // will be merged with the usage flags specified by setConsumerUsageBits.
82 //
83 // The return value may be a negative error value or a non-negative
84 // collection of flags. If the flags are set, the return values are
85 // valid, but additional actions must be performed.
86 //
87 // If IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION is set, the
88 // producer must discard cached GraphicBuffer references for the slot
89 // returned in buf.
90 // If IGraphicBufferProducer::RELEASE_ALL_BUFFERS is set, the producer
91 // must discard cached GraphicBuffer references for all slots.
92 //
93 // In both cases, the producer will need to call requestBuffer to get a
94 // GraphicBuffer handle for the returned slot.
95 virtual status_t dequeueBuffer(int *outSlot, sp<Fence>* outFence, bool async,
96 uint32_t width, uint32_t height, uint32_t format, uint32_t usage);
97
98 // queueBuffer returns a filled buffer to the BufferQueue.
99 //
100 // Additional data is provided in the QueueBufferInput struct. Notably,
101 // a timestamp must be provided for the buffer. The timestamp is in
102 // nanoseconds, and must be monotonically increasing. Its other semantics
103 // (zero point, etc) are producer-specific and should be documented by the
104 // producer.
105 //
106 // The caller may provide a fence that signals when all rendering
107 // operations have completed. Alternatively, NO_FENCE may be used,
108 // indicating that the buffer is ready immediately.
109 //
110 // Some values are returned in the output struct: the current settings
111 // for default width and height, the current transform hint, and the
112 // number of queued buffers.
113 virtual status_t queueBuffer(int slot,
114 const QueueBufferInput& input, QueueBufferOutput* output);
115
116 // cancelBuffer returns a dequeued buffer to the BufferQueue, but doesn't
117 // queue it for use by the consumer.
118 //
119 // The buffer will not be overwritten until the fence signals. The fence
120 // will usually be the one obtained from dequeueBuffer.
121 virtual void cancelBuffer(int slot, const sp<Fence>& fence);
122
123 // Query native window attributes. The "what" values are enumerated in
124 // window.h (e.g. NATIVE_WINDOW_FORMAT).
125 virtual int query(int what, int* outValue);
126
127 // connect attempts to connect a producer API to the BufferQueue. This
128 // must be called before any other IGraphicBufferProducer methods are
129 // called except for getAllocator. A consumer must already be connected.
130 //
131 // This method will fail if connect was previously called on the
132 // BufferQueue and no corresponding disconnect call was made (i.e. if
133 // it's still connected to a producer).
134 //
135 // APIs are enumerated in window.h (e.g. NATIVE_WINDOW_API_CPU).
136 virtual status_t connect(const sp<IBinder>& token,
137 int api, bool producerControlledByApp, QueueBufferOutput* output);
138
139 // disconnect attempts to disconnect a producer API from the BufferQueue.
140 // Calling this method will cause any subsequent calls to other
141 // IGraphicBufferProducer methods to fail except for getAllocator and connect.
142 // Successfully calling connect after this will allow the other methods to
143 // succeed again.
144 //
145 // This method will fail if the the BufferQueue is not currently
146 // connected to the specified producer API.
147 virtual status_t disconnect(int api);
148
149private:
150 // This is required by the IBinder::DeathRecipient interface
151 virtual void binderDied(const wp<IBinder>& who);
152
153 sp<BufferQueueCore> mCore;
154 BufferQueueCore::SlotsType& mSlots;
155
156 // This is a cached copy of the name stored in the BufferQueueCore.
157 // It's updated during connect and dequeueBuffer (which should catch
158 // most updates).
159 String8 mConsumerName;
160
161}; // class BufferQueueProducer
162
163} // namespace android
164
165#endif