blob: 2c4b43fa19b796ca7a2b533d58606c88e6131810 [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_BUFFERSLOT_H
18#define ANDROID_GUI_BUFFERSLOT_H
19
20#include <ui/Fence.h>
21#include <ui/GraphicBuffer.h>
22
23#include <EGL/egl.h>
24#include <EGL/eglext.h>
25
26#include <utils/StrongPointer.h>
27
28namespace android {
29
30class Fence;
31
32struct BufferSlot {
33
34 BufferSlot()
35 : mEglDisplay(EGL_NO_DISPLAY),
36 mBufferState(BufferSlot::FREE),
37 mRequestBufferCalled(false),
38 mFrameNumber(0),
39 mEglFence(EGL_NO_SYNC_KHR),
40 mAcquireCalled(false),
41 mNeedsCleanupOnRelease(false) {
42 }
43
44 // mGraphicBuffer points to the buffer allocated for this slot or is NULL
45 // if no buffer has been allocated.
46 sp<GraphicBuffer> mGraphicBuffer;
47
48 // mEglDisplay is the EGLDisplay used to create EGLSyncKHR objects.
49 EGLDisplay mEglDisplay;
50
51 // BufferState represents the different states in which a buffer slot
52 // can be. All slots are initially FREE.
53 enum BufferState {
54 // FREE indicates that the buffer is available to be dequeued
55 // by the producer. The buffer may be in use by the consumer for
56 // a finite time, so the buffer must not be modified until the
57 // associated fence is signaled.
58 //
59 // The slot is "owned" by BufferQueue. It transitions to DEQUEUED
60 // when dequeueBuffer is called.
61 FREE = 0,
62
63 // DEQUEUED indicates that the buffer has been dequeued by the
64 // producer, but has not yet been queued or canceled. The
65 // producer may modify the buffer's contents as soon as the
66 // associated ready fence is signaled.
67 //
68 // The slot is "owned" by the producer. It can transition to
69 // QUEUED (via queueBuffer) or back to FREE (via cancelBuffer).
70 DEQUEUED = 1,
71
72 // QUEUED indicates that the buffer has been filled by the
73 // producer and queued for use by the consumer. The buffer
74 // contents may continue to be modified for a finite time, so
75 // the contents must not be accessed until the associated fence
76 // is signaled.
77 //
78 // The slot is "owned" by BufferQueue. It can transition to
79 // ACQUIRED (via acquireBuffer) or to FREE (if another buffer is
80 // queued in asynchronous mode).
81 QUEUED = 2,
82
83 // ACQUIRED indicates that the buffer has been acquired by the
84 // consumer. As with QUEUED, the contents must not be accessed
85 // by the consumer until the fence is signaled.
86 //
87 // The slot is "owned" by the consumer. It transitions to FREE
88 // when releaseBuffer is called.
89 ACQUIRED = 3
90 };
91
92 static const char* bufferStateName(BufferState state);
93
94 // mBufferState is the current state of this buffer slot.
95 BufferState mBufferState;
96
97 // mRequestBufferCalled is used for validating that the producer did
98 // call requestBuffer() when told to do so. Technically this is not
99 // needed but useful for debugging and catching producer bugs.
100 bool mRequestBufferCalled;
101
102 // mFrameNumber is the number of the queued frame for this slot. This
103 // is used to dequeue buffers in LRU order (useful because buffers
104 // may be released before their release fence is signaled).
105 uint64_t mFrameNumber;
106
107 // mEglFence is the EGL sync object that must signal before the buffer
108 // associated with this buffer slot may be dequeued. It is initialized
109 // to EGL_NO_SYNC_KHR when the buffer is created and may be set to a
110 // new sync object in releaseBuffer. (This is deprecated in favor of
111 // mFence, below.)
112 EGLSyncKHR mEglFence;
113
114 // mFence is a fence which will signal when work initiated by the
115 // previous owner of the buffer is finished. When the buffer is FREE,
116 // the fence indicates when the consumer has finished reading
117 // from the buffer, or when the producer has finished writing if it
118 // called cancelBuffer after queueing some writes. When the buffer is
119 // QUEUED, it indicates when the producer has finished filling the
120 // buffer. When the buffer is DEQUEUED or ACQUIRED, the fence has been
121 // passed to the consumer or producer along with ownership of the
122 // buffer, and mFence is set to NO_FENCE.
123 sp<Fence> mFence;
124
125 // Indicates whether this buffer has been seen by a consumer yet
126 bool mAcquireCalled;
127
128 // Indicates whether this buffer needs to be cleaned up by the
129 // consumer. This is set when a buffer in ACQUIRED state is freed.
130 // It causes releaseBuffer to return STALE_BUFFER_SLOT.
131 bool mNeedsCleanupOnRelease;
132};
133
134} // namespace android
135
136#endif