blob: c02b2e7659062b3d57f9888cf8b6a089c96a7a86 [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
Mathias Agopian81e2a522009-09-07 16:32:45 -070072// should be 128 bytes (32 longs)
73class SharedBufferStack
74{
75 friend class SharedClient;
76 friend class SharedBufferBase;
77 friend class SharedBufferClient;
78 friend class SharedBufferServer;
79
80public:
Mathias Agopian9e3ebf82009-09-17 01:35:28 -070081 struct FlatRegion { // 12 bytes
82 static const unsigned int NUM_RECT_MAX = 1;
83 uint32_t count;
84 uint16_t rects[4*NUM_RECT_MAX];
85 };
86
87 struct Statistics { // 4 longs
88 typedef int32_t usecs_t;
89 usecs_t totalTime;
90 usecs_t reserved[3];
91 };
92
Mathias Agopian81e2a522009-09-07 16:32:45 -070093 SharedBufferStack();
Mathias Agopian4fc61bf2009-09-10 19:41:18 -070094 void init(int32_t identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -070095 status_t setDirtyRegion(int buffer, const Region& reg);
96 Region getDirtyRegion(int buffer) const;
97
98 // these attributes are part of the conditions/updates
99 volatile int32_t head; // server's current front buffer
100 volatile int32_t available; // number of dequeue-able buffers
101 volatile int32_t queued; // number of buffers waiting for post
102 volatile int32_t inUse; // buffer currently in use by SF
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700103 volatile status_t status; // surface's status code
Mathias Agopian81e2a522009-09-07 16:32:45 -0700104
105 // not part of the conditions
106 volatile int32_t reallocMask;
107
108 int32_t identity; // surface's identity (const)
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700109 int32_t reserved32[9];
110 Statistics stats;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700111 FlatRegion dirtyRegion[NUM_BUFFER_MAX]; // 12*4=48 bytes
112};
113
114// ----------------------------------------------------------------------------
115
116// 4 KB max
117class SharedClient
118{
119public:
120 SharedClient();
121 ~SharedClient();
122
123 status_t validate(size_t token) const;
124 uint32_t getIdentity(size_t token) const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700125
126private:
127 friend class SharedBufferBase;
128 friend class SharedBufferClient;
129 friend class SharedBufferServer;
130
131 // FIXME: this should be replaced by a lock-less primitive
132 Mutex lock;
133 Condition cv;
134 SharedBufferStack surfaces[ NUM_LAYERS_MAX ];
135};
136
137// ============================================================================
138
139class SharedBufferBase
140{
141public:
142 SharedBufferBase(SharedClient* sharedClient, int surface, int num);
143 ~SharedBufferBase();
144 uint32_t getIdentity();
145 size_t getFrontBuffer() const;
146 String8 dump(char const* prefix) const;
147
148protected:
149 SharedClient* const mSharedClient;
150 SharedBufferStack* const mSharedStack;
151 const int mNumBuffers;
152
153 friend struct Update;
154 friend struct QueueUpdate;
155
156 struct ConditionBase {
157 SharedBufferStack& stack;
158 inline ConditionBase(SharedBufferBase* sbc)
159 : stack(*sbc->mSharedStack) { }
160 };
161
162 struct UpdateBase {
163 SharedBufferStack& stack;
164 inline UpdateBase(SharedBufferBase* sbb)
165 : stack(*sbb->mSharedStack) { }
166 };
167
168 template <typename T>
169 status_t waitForCondition(T condition);
170
171 template <typename T>
172 status_t updateCondition(T update);
173};
174
175template <typename T>
176status_t SharedBufferBase::waitForCondition(T condition)
177{
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700178 const SharedBufferStack& stack( *mSharedStack );
Mathias Agopian81e2a522009-09-07 16:32:45 -0700179 SharedClient& client( *mSharedClient );
180 const nsecs_t TIMEOUT = s2ns(1);
181 Mutex::Autolock _l(client.lock);
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700182 while ((condition()==false) && (stack.status == NO_ERROR)) {
Mathias Agopian81e2a522009-09-07 16:32:45 -0700183 status_t err = client.cv.waitRelative(client.lock, TIMEOUT);
184
185 // handle errors and timeouts
186 if (CC_UNLIKELY(err != NO_ERROR)) {
187 if (err == TIMED_OUT) {
188 if (condition()) {
189 LOGE("waitForCondition(%s) timed out (identity=%d), "
190 "but condition is true! We recovered but it "
191 "shouldn't happen." ,
192 T::name(), mSharedStack->identity);
193 break;
194 } else {
195 LOGW("waitForCondition(%s) timed out (identity=%d). "
196 "CPU may be pegged. trying again.",
197 T::name(), mSharedStack->identity);
198 }
199 } else {
200 LOGE("waitForCondition(%s) error (%s) ",
201 T::name(), strerror(-err));
202 return err;
203 }
204 }
205 }
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700206 return stack.status;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700207}
208
209
210template <typename T>
211status_t SharedBufferBase::updateCondition(T update) {
212 SharedClient& client( *mSharedClient );
213 Mutex::Autolock _l(client.lock);
214 ssize_t result = update();
215 client.cv.broadcast();
216 return result;
217}
218
219// ----------------------------------------------------------------------------
220
221class SharedBufferClient : public SharedBufferBase
222{
223public:
224 SharedBufferClient(SharedClient* sharedClient, int surface, int num);
225
226 ssize_t dequeue();
227 status_t undoDequeue(int buf);
228
229 status_t lock(int buf);
230 status_t queue(int buf);
231 bool needNewBuffer(int buffer) const;
232 status_t setDirtyRegion(int buffer, const Region& reg);
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700233
Mathias Agopian81e2a522009-09-07 16:32:45 -0700234private:
235 friend struct Condition;
236 friend struct DequeueCondition;
237 friend struct LockCondition;
Mathias Agopian5a37cc52009-09-14 15:48:42 -0700238
239 int32_t computeTail() const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700240
241 struct QueueUpdate : public UpdateBase {
242 inline QueueUpdate(SharedBufferBase* sbb);
243 inline ssize_t operator()();
244 };
245
246 struct UndoDequeueUpdate : public UpdateBase {
247 inline UndoDequeueUpdate(SharedBufferBase* sbb);
248 inline ssize_t operator()();
249 };
250
251 // --
252
253 struct DequeueCondition : public ConditionBase {
254 inline DequeueCondition(SharedBufferClient* sbc);
255 inline bool operator()();
256 static inline const char* name() { return "DequeueCondition"; }
257 };
258
259 struct LockCondition : public ConditionBase {
260 int buf;
261 inline LockCondition(SharedBufferClient* sbc, int buf);
262 inline bool operator()();
263 static inline const char* name() { return "LockCondition"; }
264 };
265
266 int32_t tail;
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700267 // statistics...
268 nsecs_t mDequeueTime[NUM_BUFFER_MAX];
Mathias Agopian81e2a522009-09-07 16:32:45 -0700269};
270
271// ----------------------------------------------------------------------------
272
273class SharedBufferServer : public SharedBufferBase
274{
275public:
Mathias Agopian4fc61bf2009-09-10 19:41:18 -0700276 SharedBufferServer(SharedClient* sharedClient, int surface, int num,
277 int32_t identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700278
279 ssize_t retireAndLock();
280 status_t unlock(int buffer);
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700281 void setStatus(status_t status);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700282 status_t reallocate();
283 status_t assertReallocate(int buffer);
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700284
Mathias Agopian81e2a522009-09-07 16:32:45 -0700285 Region getDirtyRegion(int buffer) const;
286
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700287 SharedBufferStack::Statistics getStats() const;
288
289
Mathias Agopian81e2a522009-09-07 16:32:45 -0700290private:
291 struct UnlockUpdate : public UpdateBase {
292 const int lockedBuffer;
293 inline UnlockUpdate(SharedBufferBase* sbb, int lockedBuffer);
294 inline ssize_t operator()();
295 };
296
297 struct RetireUpdate : public UpdateBase {
298 const int numBuffers;
299 inline RetireUpdate(SharedBufferBase* sbb, int numBuffers);
300 inline ssize_t operator()();
301 };
302
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700303 struct StatusUpdate : public UpdateBase {
304 const status_t status;
305 inline StatusUpdate(SharedBufferBase* sbb, status_t status);
306 inline ssize_t operator()();
307 };
308
Mathias Agopian81e2a522009-09-07 16:32:45 -0700309 struct ReallocateCondition : public ConditionBase {
310 int buf;
311 inline ReallocateCondition(SharedBufferBase* sbb, int buf);
312 inline bool operator()();
313 static inline const char* name() { return "ReallocateCondition"; }
314 };
315};
316
317// ===========================================================================
318
319struct display_cblk_t
320{
321 uint16_t w;
322 uint16_t h;
323 uint8_t format;
324 uint8_t orientation;
325 uint8_t reserved[2];
326 float fps;
327 float density;
328 float xdpi;
329 float ydpi;
330 uint32_t pad[2];
331};
332
333struct surface_flinger_cblk_t // 4KB max
334{
335 uint8_t connected;
336 uint8_t reserved[3];
337 uint32_t pad[7];
338 display_cblk_t displays[NUM_DISPLAY_MAX];
339};
340
341// ---------------------------------------------------------------------------
342
343COMPILE_TIME_ASSERT(sizeof(SharedClient) <= 4096)
344COMPILE_TIME_ASSERT(sizeof(SharedBufferStack) == 128)
345COMPILE_TIME_ASSERT(sizeof(surface_flinger_cblk_t) <= 4096)
346
347// ---------------------------------------------------------------------------
348}; // namespace android
349
350#endif /* ANDROID_UI_SHARED_BUFFER_STACK_H */