blob: 6181f5576097f3751feed0e09cf9ee6e0c641414 [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;
231
232 struct QueueUpdate : public UpdateBase {
233 inline QueueUpdate(SharedBufferBase* sbb);
234 inline ssize_t operator()();
235 };
236
237 struct UndoDequeueUpdate : public UpdateBase {
238 inline UndoDequeueUpdate(SharedBufferBase* sbb);
239 inline ssize_t operator()();
240 };
241
242 // --
243
244 struct DequeueCondition : public ConditionBase {
245 inline DequeueCondition(SharedBufferClient* sbc);
246 inline bool operator()();
247 static inline const char* name() { return "DequeueCondition"; }
248 };
249
250 struct LockCondition : public ConditionBase {
251 int buf;
252 inline LockCondition(SharedBufferClient* sbc, int buf);
253 inline bool operator()();
254 static inline const char* name() { return "LockCondition"; }
255 };
256
257 int32_t tail;
258};
259
260// ----------------------------------------------------------------------------
261
262class SharedBufferServer : public SharedBufferBase
263{
264public:
Mathias Agopian4fc61bf2009-09-10 19:41:18 -0700265 SharedBufferServer(SharedClient* sharedClient, int surface, int num,
266 int32_t identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700267
268 ssize_t retireAndLock();
269 status_t unlock(int buffer);
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700270 void setStatus(status_t status);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700271 status_t reallocate();
272 status_t assertReallocate(int buffer);
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700273
Mathias Agopian81e2a522009-09-07 16:32:45 -0700274 Region getDirtyRegion(int buffer) const;
275
276private:
277 struct UnlockUpdate : public UpdateBase {
278 const int lockedBuffer;
279 inline UnlockUpdate(SharedBufferBase* sbb, int lockedBuffer);
280 inline ssize_t operator()();
281 };
282
283 struct RetireUpdate : public UpdateBase {
284 const int numBuffers;
285 inline RetireUpdate(SharedBufferBase* sbb, int numBuffers);
286 inline ssize_t operator()();
287 };
288
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700289 struct StatusUpdate : public UpdateBase {
290 const status_t status;
291 inline StatusUpdate(SharedBufferBase* sbb, status_t status);
292 inline ssize_t operator()();
293 };
294
Mathias Agopian81e2a522009-09-07 16:32:45 -0700295 struct ReallocateCondition : public ConditionBase {
296 int buf;
297 inline ReallocateCondition(SharedBufferBase* sbb, int buf);
298 inline bool operator()();
299 static inline const char* name() { return "ReallocateCondition"; }
300 };
301};
302
303// ===========================================================================
304
305struct display_cblk_t
306{
307 uint16_t w;
308 uint16_t h;
309 uint8_t format;
310 uint8_t orientation;
311 uint8_t reserved[2];
312 float fps;
313 float density;
314 float xdpi;
315 float ydpi;
316 uint32_t pad[2];
317};
318
319struct surface_flinger_cblk_t // 4KB max
320{
321 uint8_t connected;
322 uint8_t reserved[3];
323 uint32_t pad[7];
324 display_cblk_t displays[NUM_DISPLAY_MAX];
325};
326
327// ---------------------------------------------------------------------------
328
329COMPILE_TIME_ASSERT(sizeof(SharedClient) <= 4096)
330COMPILE_TIME_ASSERT(sizeof(SharedBufferStack) == 128)
331COMPILE_TIME_ASSERT(sizeof(surface_flinger_cblk_t) <= 4096)
332
333// ---------------------------------------------------------------------------
334}; // namespace android
335
336#endif /* ANDROID_UI_SHARED_BUFFER_STACK_H */