blob: 59cf31c89c717b4f6233cdf711380463a5f46f6b [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();
Mathias Agopiandefd1bd2009-10-02 18:12:30 -0700145 status_t getStatus() const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700146 size_t getFrontBuffer() const;
147 String8 dump(char const* prefix) const;
148
149protected:
150 SharedClient* const mSharedClient;
151 SharedBufferStack* const mSharedStack;
152 const int mNumBuffers;
153
154 friend struct Update;
155 friend struct QueueUpdate;
156
157 struct ConditionBase {
158 SharedBufferStack& stack;
159 inline ConditionBase(SharedBufferBase* sbc)
160 : stack(*sbc->mSharedStack) { }
161 };
162
163 struct UpdateBase {
164 SharedBufferStack& stack;
165 inline UpdateBase(SharedBufferBase* sbb)
166 : stack(*sbb->mSharedStack) { }
167 };
168
169 template <typename T>
170 status_t waitForCondition(T condition);
171
172 template <typename T>
173 status_t updateCondition(T update);
174};
175
176template <typename T>
177status_t SharedBufferBase::waitForCondition(T condition)
178{
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700179 const SharedBufferStack& stack( *mSharedStack );
Mathias Agopian81e2a522009-09-07 16:32:45 -0700180 SharedClient& client( *mSharedClient );
Mathias Agopiandefd1bd2009-10-02 18:12:30 -0700181 const nsecs_t TIMEOUT = s2ns(1);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700182 Mutex::Autolock _l(client.lock);
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700183 while ((condition()==false) && (stack.status == NO_ERROR)) {
Mathias Agopian81e2a522009-09-07 16:32:45 -0700184 status_t err = client.cv.waitRelative(client.lock, TIMEOUT);
185
186 // handle errors and timeouts
187 if (CC_UNLIKELY(err != NO_ERROR)) {
188 if (err == TIMED_OUT) {
189 if (condition()) {
190 LOGE("waitForCondition(%s) timed out (identity=%d), "
Mathias Agopiandefd1bd2009-10-02 18:12:30 -0700191 "but condition is true! We recovered but it "
192 "shouldn't happen." , T::name(),
193 mSharedStack->identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700194 break;
195 } else {
Mathias Agopiandefd1bd2009-10-02 18:12:30 -0700196 LOGW("waitForCondition(%s) timed out "
197 "(identity=%d, status=%d). "
198 "CPU may be pegged. trying again.", T::name(),
199 mSharedStack->identity, mSharedStack->status);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700200 }
201 } else {
202 LOGE("waitForCondition(%s) error (%s) ",
203 T::name(), strerror(-err));
204 return err;
205 }
206 }
207 }
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700208 return stack.status;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700209}
210
211
212template <typename T>
213status_t SharedBufferBase::updateCondition(T update) {
214 SharedClient& client( *mSharedClient );
215 Mutex::Autolock _l(client.lock);
216 ssize_t result = update();
217 client.cv.broadcast();
218 return result;
219}
220
221// ----------------------------------------------------------------------------
222
223class SharedBufferClient : public SharedBufferBase
224{
225public:
226 SharedBufferClient(SharedClient* sharedClient, int surface, int num);
227
228 ssize_t dequeue();
229 status_t undoDequeue(int buf);
230
231 status_t lock(int buf);
232 status_t queue(int buf);
233 bool needNewBuffer(int buffer) const;
234 status_t setDirtyRegion(int buffer, const Region& reg);
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700235
Mathias Agopian81e2a522009-09-07 16:32:45 -0700236private:
237 friend struct Condition;
238 friend struct DequeueCondition;
239 friend struct LockCondition;
Mathias Agopian5a37cc52009-09-14 15:48:42 -0700240
241 int32_t computeTail() const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700242
243 struct QueueUpdate : public UpdateBase {
244 inline QueueUpdate(SharedBufferBase* sbb);
245 inline ssize_t operator()();
246 };
247
248 struct UndoDequeueUpdate : public UpdateBase {
249 inline UndoDequeueUpdate(SharedBufferBase* sbb);
250 inline ssize_t operator()();
251 };
252
253 // --
254
255 struct DequeueCondition : public ConditionBase {
256 inline DequeueCondition(SharedBufferClient* sbc);
257 inline bool operator()();
258 static inline const char* name() { return "DequeueCondition"; }
259 };
260
261 struct LockCondition : public ConditionBase {
262 int buf;
263 inline LockCondition(SharedBufferClient* sbc, int buf);
264 inline bool operator()();
265 static inline const char* name() { return "LockCondition"; }
266 };
267
268 int32_t tail;
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700269 // statistics...
270 nsecs_t mDequeueTime[NUM_BUFFER_MAX];
Mathias Agopian81e2a522009-09-07 16:32:45 -0700271};
272
273// ----------------------------------------------------------------------------
274
275class SharedBufferServer : public SharedBufferBase
276{
277public:
Mathias Agopian4fc61bf2009-09-10 19:41:18 -0700278 SharedBufferServer(SharedClient* sharedClient, int surface, int num,
279 int32_t identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700280
281 ssize_t retireAndLock();
282 status_t unlock(int buffer);
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700283 void setStatus(status_t status);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700284 status_t reallocate();
285 status_t assertReallocate(int buffer);
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700286
Mathias Agopian81e2a522009-09-07 16:32:45 -0700287 Region getDirtyRegion(int buffer) const;
288
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700289 SharedBufferStack::Statistics getStats() const;
290
291
Mathias Agopian81e2a522009-09-07 16:32:45 -0700292private:
293 struct UnlockUpdate : public UpdateBase {
294 const int lockedBuffer;
295 inline UnlockUpdate(SharedBufferBase* sbb, int lockedBuffer);
296 inline ssize_t operator()();
297 };
298
299 struct RetireUpdate : public UpdateBase {
300 const int numBuffers;
301 inline RetireUpdate(SharedBufferBase* sbb, int numBuffers);
302 inline ssize_t operator()();
303 };
304
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700305 struct StatusUpdate : public UpdateBase {
306 const status_t status;
307 inline StatusUpdate(SharedBufferBase* sbb, status_t status);
308 inline ssize_t operator()();
309 };
310
Mathias Agopian81e2a522009-09-07 16:32:45 -0700311 struct ReallocateCondition : public ConditionBase {
312 int buf;
313 inline ReallocateCondition(SharedBufferBase* sbb, int buf);
314 inline bool operator()();
315 static inline const char* name() { return "ReallocateCondition"; }
316 };
317};
318
319// ===========================================================================
320
321struct display_cblk_t
322{
323 uint16_t w;
324 uint16_t h;
325 uint8_t format;
326 uint8_t orientation;
327 uint8_t reserved[2];
328 float fps;
329 float density;
330 float xdpi;
331 float ydpi;
332 uint32_t pad[2];
333};
334
335struct surface_flinger_cblk_t // 4KB max
336{
337 uint8_t connected;
338 uint8_t reserved[3];
339 uint32_t pad[7];
340 display_cblk_t displays[NUM_DISPLAY_MAX];
341};
342
343// ---------------------------------------------------------------------------
344
345COMPILE_TIME_ASSERT(sizeof(SharedClient) <= 4096)
346COMPILE_TIME_ASSERT(sizeof(SharedBufferStack) == 128)
347COMPILE_TIME_ASSERT(sizeof(surface_flinger_cblk_t) <= 4096)
348
349// ---------------------------------------------------------------------------
350}; // namespace android
351
352#endif /* ANDROID_UI_SHARED_BUFFER_STACK_H */