blob: 2bd534456d8ccfde2e308cfb6a6ca716dd211c62 [file] [log] [blame]
Mathias Agopian81e2a522009-09-07 16:32:45 -07001/*
2 * Copyright (C) 2007 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_UI_SHARED_BUFFER_STACK_H
18#define ANDROID_UI_SHARED_BUFFER_STACK_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <cutils/compiler.h>
24
25#include <utils/Debug.h>
26#include <utils/threads.h>
27#include <utils/String8.h>
28
29#include <ui/Rect.h>
30
31namespace android {
32// ---------------------------------------------------------------------------
33
34/*
35 * These classes manage a stack of buffers in shared memory.
36 *
37 * SharedClient: represents a client with several stacks
38 * SharedBufferStack: represents a stack of buffers
39 * SharedBufferClient: manipulates the SharedBufferStack from the client side
40 * SharedBufferServer: manipulates the SharedBufferStack from the server side
41 *
42 * Buffers can be dequeued until there are none available, they can be locked
43 * unless they are in use by the server, which is only the case for the last
44 * dequeue-able buffer. When these various conditions are not met, the caller
45 * waits until the condition is met.
46 *
47 *
48 * CAVEATS:
49 *
50 * In the current implementation there are several limitations:
51 * - buffers must be locked in the same order they've been dequeued
52 * - buffers must be enqueued in the same order they've been locked
53 * - dequeue() is not reentrant
54 * - no error checks are done on the condition above
55 *
56 */
57
58// When changing these values, the COMPILE_TIME_ASSERT at the end of this
59// file need to be updated.
60const unsigned int NUM_LAYERS_MAX = 31;
61const unsigned int NUM_BUFFER_MAX = 4;
62const unsigned int NUM_DISPLAY_MAX = 4;
63
64// ----------------------------------------------------------------------------
65
66class Region;
67class SharedBufferStack;
68class SharedClient;
69
70// ----------------------------------------------------------------------------
71
72struct FlatRegion { // 12 bytes
73 static const unsigned int NUM_RECT_MAX = 1;
74 uint32_t count;
75 uint16_t rects[4*NUM_RECT_MAX];
76};
77
78// should be 128 bytes (32 longs)
79class SharedBufferStack
80{
81 friend class SharedClient;
82 friend class SharedBufferBase;
83 friend class SharedBufferClient;
84 friend class SharedBufferServer;
85
86public:
87 SharedBufferStack();
88 status_t setDirtyRegion(int buffer, const Region& reg);
89 Region getDirtyRegion(int buffer) const;
90
91 // these attributes are part of the conditions/updates
92 volatile int32_t head; // server's current front buffer
93 volatile int32_t available; // number of dequeue-able buffers
94 volatile int32_t queued; // number of buffers waiting for post
95 volatile int32_t inUse; // buffer currently in use by SF
96
97 // not part of the conditions
98 volatile int32_t reallocMask;
99
100 int32_t identity; // surface's identity (const)
101 status_t status; // surface's status code
102 int32_t reserved32[13];
103 FlatRegion dirtyRegion[NUM_BUFFER_MAX]; // 12*4=48 bytes
104};
105
106// ----------------------------------------------------------------------------
107
108// 4 KB max
109class SharedClient
110{
111public:
112 SharedClient();
113 ~SharedClient();
114
115 status_t validate(size_t token) const;
116 uint32_t getIdentity(size_t token) const;
117 status_t setIdentity(size_t token, uint32_t identity);
118
119private:
120 friend class SharedBufferBase;
121 friend class SharedBufferClient;
122 friend class SharedBufferServer;
123
124 // FIXME: this should be replaced by a lock-less primitive
125 Mutex lock;
126 Condition cv;
127 SharedBufferStack surfaces[ NUM_LAYERS_MAX ];
128};
129
130// ============================================================================
131
132class SharedBufferBase
133{
134public:
135 SharedBufferBase(SharedClient* sharedClient, int surface, int num);
136 ~SharedBufferBase();
137 uint32_t getIdentity();
138 size_t getFrontBuffer() const;
139 String8 dump(char const* prefix) const;
140
141protected:
142 SharedClient* const mSharedClient;
143 SharedBufferStack* const mSharedStack;
144 const int mNumBuffers;
145
146 friend struct Update;
147 friend struct QueueUpdate;
148
149 struct ConditionBase {
150 SharedBufferStack& stack;
151 inline ConditionBase(SharedBufferBase* sbc)
152 : stack(*sbc->mSharedStack) { }
153 };
154
155 struct UpdateBase {
156 SharedBufferStack& stack;
157 inline UpdateBase(SharedBufferBase* sbb)
158 : stack(*sbb->mSharedStack) { }
159 };
160
161 template <typename T>
162 status_t waitForCondition(T condition);
163
164 template <typename T>
165 status_t updateCondition(T update);
166};
167
168template <typename T>
169status_t SharedBufferBase::waitForCondition(T condition)
170{
171 SharedClient& client( *mSharedClient );
172 const nsecs_t TIMEOUT = s2ns(1);
173 Mutex::Autolock _l(client.lock);
174 while (!condition()) {
175 status_t err = client.cv.waitRelative(client.lock, TIMEOUT);
176
177 // handle errors and timeouts
178 if (CC_UNLIKELY(err != NO_ERROR)) {
179 if (err == TIMED_OUT) {
180 if (condition()) {
181 LOGE("waitForCondition(%s) timed out (identity=%d), "
182 "but condition is true! We recovered but it "
183 "shouldn't happen." ,
184 T::name(), mSharedStack->identity);
185 break;
186 } else {
187 LOGW("waitForCondition(%s) timed out (identity=%d). "
188 "CPU may be pegged. trying again.",
189 T::name(), mSharedStack->identity);
190 }
191 } else {
192 LOGE("waitForCondition(%s) error (%s) ",
193 T::name(), strerror(-err));
194 return err;
195 }
196 }
197 }
198 return NO_ERROR;
199}
200
201
202template <typename T>
203status_t SharedBufferBase::updateCondition(T update) {
204 SharedClient& client( *mSharedClient );
205 Mutex::Autolock _l(client.lock);
206 ssize_t result = update();
207 client.cv.broadcast();
208 return result;
209}
210
211// ----------------------------------------------------------------------------
212
213class SharedBufferClient : public SharedBufferBase
214{
215public:
216 SharedBufferClient(SharedClient* sharedClient, int surface, int num);
217
218 ssize_t dequeue();
219 status_t undoDequeue(int buf);
220
221 status_t lock(int buf);
222 status_t queue(int buf);
223 bool needNewBuffer(int buffer) const;
224 status_t setDirtyRegion(int buffer, const Region& reg);
225
226private:
227 friend struct Condition;
228 friend struct DequeueCondition;
229 friend struct LockCondition;
230
231 struct QueueUpdate : public UpdateBase {
232 inline QueueUpdate(SharedBufferBase* sbb);
233 inline ssize_t operator()();
234 };
235
236 struct UndoDequeueUpdate : public UpdateBase {
237 inline UndoDequeueUpdate(SharedBufferBase* sbb);
238 inline ssize_t operator()();
239 };
240
241 // --
242
243 struct DequeueCondition : public ConditionBase {
244 inline DequeueCondition(SharedBufferClient* sbc);
245 inline bool operator()();
246 static inline const char* name() { return "DequeueCondition"; }
247 };
248
249 struct LockCondition : public ConditionBase {
250 int buf;
251 inline LockCondition(SharedBufferClient* sbc, int buf);
252 inline bool operator()();
253 static inline const char* name() { return "LockCondition"; }
254 };
255
256 int32_t tail;
257};
258
259// ----------------------------------------------------------------------------
260
261class SharedBufferServer : public SharedBufferBase
262{
263public:
264 SharedBufferServer(SharedClient* sharedClient, int surface, int num);
265
266 ssize_t retireAndLock();
267 status_t unlock(int buffer);
268 status_t reallocate();
269 status_t assertReallocate(int buffer);
270
271 Region getDirtyRegion(int buffer) const;
272
273private:
274 struct UnlockUpdate : public UpdateBase {
275 const int lockedBuffer;
276 inline UnlockUpdate(SharedBufferBase* sbb, int lockedBuffer);
277 inline ssize_t operator()();
278 };
279
280 struct RetireUpdate : public UpdateBase {
281 const int numBuffers;
282 inline RetireUpdate(SharedBufferBase* sbb, int numBuffers);
283 inline ssize_t operator()();
284 };
285
286 struct ReallocateCondition : public ConditionBase {
287 int buf;
288 inline ReallocateCondition(SharedBufferBase* sbb, int buf);
289 inline bool operator()();
290 static inline const char* name() { return "ReallocateCondition"; }
291 };
292};
293
294// ===========================================================================
295
296struct display_cblk_t
297{
298 uint16_t w;
299 uint16_t h;
300 uint8_t format;
301 uint8_t orientation;
302 uint8_t reserved[2];
303 float fps;
304 float density;
305 float xdpi;
306 float ydpi;
307 uint32_t pad[2];
308};
309
310struct surface_flinger_cblk_t // 4KB max
311{
312 uint8_t connected;
313 uint8_t reserved[3];
314 uint32_t pad[7];
315 display_cblk_t displays[NUM_DISPLAY_MAX];
316};
317
318// ---------------------------------------------------------------------------
319
320COMPILE_TIME_ASSERT(sizeof(SharedClient) <= 4096)
321COMPILE_TIME_ASSERT(sizeof(SharedBufferStack) == 128)
322COMPILE_TIME_ASSERT(sizeof(surface_flinger_cblk_t) <= 4096)
323
324// ---------------------------------------------------------------------------
325}; // namespace android
326
327#endif /* ANDROID_UI_SHARED_BUFFER_STACK_H */