blob: 8b0f154f5c99fa7d5e8cea21b176e13319bc53ac [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();
Mathias Agopian4fc61bf2009-09-10 19:41:18 -070088 void init(int32_t identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -070089 status_t setDirtyRegion(int buffer, const Region& reg);
90 Region getDirtyRegion(int buffer) const;
91
92 // these attributes are part of the conditions/updates
93 volatile int32_t head; // server's current front buffer
94 volatile int32_t available; // number of dequeue-able buffers
95 volatile int32_t queued; // number of buffers waiting for post
96 volatile int32_t inUse; // buffer currently in use by SF
Mathias Agopian3dbf98c2009-09-10 16:55:13 -070097 volatile status_t status; // surface's status code
Mathias Agopian81e2a522009-09-07 16:32:45 -070098
99 // not part of the conditions
100 volatile int32_t reallocMask;
101
102 int32_t identity; // surface's identity (const)
Mathias Agopian81e2a522009-09-07 16:32:45 -0700103 int32_t reserved32[13];
104 FlatRegion dirtyRegion[NUM_BUFFER_MAX]; // 12*4=48 bytes
105};
106
107// ----------------------------------------------------------------------------
108
109// 4 KB max
110class SharedClient
111{
112public:
113 SharedClient();
114 ~SharedClient();
115
116 status_t validate(size_t token) const;
117 uint32_t getIdentity(size_t token) const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700118
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{
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700171 const SharedBufferStack& stack( *mSharedStack );
Mathias Agopian81e2a522009-09-07 16:32:45 -0700172 SharedClient& client( *mSharedClient );
173 const nsecs_t TIMEOUT = s2ns(1);
174 Mutex::Autolock _l(client.lock);
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700175 while ((condition()==false) && (stack.status == NO_ERROR)) {
Mathias Agopian81e2a522009-09-07 16:32:45 -0700176 status_t err = client.cv.waitRelative(client.lock, TIMEOUT);
177
178 // handle errors and timeouts
179 if (CC_UNLIKELY(err != NO_ERROR)) {
180 if (err == TIMED_OUT) {
181 if (condition()) {
182 LOGE("waitForCondition(%s) timed out (identity=%d), "
183 "but condition is true! We recovered but it "
184 "shouldn't happen." ,
185 T::name(), mSharedStack->identity);
186 break;
187 } else {
188 LOGW("waitForCondition(%s) timed out (identity=%d). "
189 "CPU may be pegged. trying again.",
190 T::name(), mSharedStack->identity);
191 }
192 } else {
193 LOGE("waitForCondition(%s) error (%s) ",
194 T::name(), strerror(-err));
195 return err;
196 }
197 }
198 }
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700199 return stack.status;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700200}
201
202
203template <typename T>
204status_t SharedBufferBase::updateCondition(T update) {
205 SharedClient& client( *mSharedClient );
206 Mutex::Autolock _l(client.lock);
207 ssize_t result = update();
208 client.cv.broadcast();
209 return result;
210}
211
212// ----------------------------------------------------------------------------
213
214class SharedBufferClient : public SharedBufferBase
215{
216public:
217 SharedBufferClient(SharedClient* sharedClient, int surface, int num);
218
219 ssize_t dequeue();
220 status_t undoDequeue(int buf);
221
222 status_t lock(int buf);
223 status_t queue(int buf);
224 bool needNewBuffer(int buffer) const;
225 status_t setDirtyRegion(int buffer, const Region& reg);
226
227private:
228 friend struct Condition;
229 friend struct DequeueCondition;
230 friend struct LockCondition;
Mathias Agopian5a37cc52009-09-14 15:48:42 -0700231
232 int32_t computeTail() const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700233
234 struct QueueUpdate : public UpdateBase {
235 inline QueueUpdate(SharedBufferBase* sbb);
236 inline ssize_t operator()();
237 };
238
239 struct UndoDequeueUpdate : public UpdateBase {
240 inline UndoDequeueUpdate(SharedBufferBase* sbb);
241 inline ssize_t operator()();
242 };
243
244 // --
245
246 struct DequeueCondition : public ConditionBase {
247 inline DequeueCondition(SharedBufferClient* sbc);
248 inline bool operator()();
249 static inline const char* name() { return "DequeueCondition"; }
250 };
251
252 struct LockCondition : public ConditionBase {
253 int buf;
254 inline LockCondition(SharedBufferClient* sbc, int buf);
255 inline bool operator()();
256 static inline const char* name() { return "LockCondition"; }
257 };
258
259 int32_t tail;
260};
261
262// ----------------------------------------------------------------------------
263
264class SharedBufferServer : public SharedBufferBase
265{
266public:
Mathias Agopian4fc61bf2009-09-10 19:41:18 -0700267 SharedBufferServer(SharedClient* sharedClient, int surface, int num,
268 int32_t identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700269
270 ssize_t retireAndLock();
271 status_t unlock(int buffer);
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700272 void setStatus(status_t status);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700273 status_t reallocate();
274 status_t assertReallocate(int buffer);
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700275
Mathias Agopian81e2a522009-09-07 16:32:45 -0700276 Region getDirtyRegion(int buffer) const;
277
278private:
279 struct UnlockUpdate : public UpdateBase {
280 const int lockedBuffer;
281 inline UnlockUpdate(SharedBufferBase* sbb, int lockedBuffer);
282 inline ssize_t operator()();
283 };
284
285 struct RetireUpdate : public UpdateBase {
286 const int numBuffers;
287 inline RetireUpdate(SharedBufferBase* sbb, int numBuffers);
288 inline ssize_t operator()();
289 };
290
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700291 struct StatusUpdate : public UpdateBase {
292 const status_t status;
293 inline StatusUpdate(SharedBufferBase* sbb, status_t status);
294 inline ssize_t operator()();
295 };
296
Mathias Agopian81e2a522009-09-07 16:32:45 -0700297 struct ReallocateCondition : public ConditionBase {
298 int buf;
299 inline ReallocateCondition(SharedBufferBase* sbb, int buf);
300 inline bool operator()();
301 static inline const char* name() { return "ReallocateCondition"; }
302 };
303};
304
305// ===========================================================================
306
307struct display_cblk_t
308{
309 uint16_t w;
310 uint16_t h;
311 uint8_t format;
312 uint8_t orientation;
313 uint8_t reserved[2];
314 float fps;
315 float density;
316 float xdpi;
317 float ydpi;
318 uint32_t pad[2];
319};
320
321struct surface_flinger_cblk_t // 4KB max
322{
323 uint8_t connected;
324 uint8_t reserved[3];
325 uint32_t pad[7];
326 display_cblk_t displays[NUM_DISPLAY_MAX];
327};
328
329// ---------------------------------------------------------------------------
330
331COMPILE_TIME_ASSERT(sizeof(SharedClient) <= 4096)
332COMPILE_TIME_ASSERT(sizeof(SharedBufferStack) == 128)
333COMPILE_TIME_ASSERT(sizeof(surface_flinger_cblk_t) <= 4096)
334
335// ---------------------------------------------------------------------------
336}; // namespace android
337
338#endif /* ANDROID_UI_SHARED_BUFFER_STACK_H */