blob: 39ef3a186eeb36a911f54c6bb33d065b3b82f9ce [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
Mathias Agopian3cf61352010-02-09 17:46:37 -080017#ifndef ANDROID_SF_SHARED_BUFFER_STACK_H
18#define ANDROID_SF_SHARED_BUFFER_STACK_H
Mathias Agopian81e2a522009-09-07 16:32:45 -070019
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;
Mathias Agopiancdaaf322010-04-05 16:21:53 -070061const unsigned int NUM_BUFFER_MAX = 16;
Mathias Agopian81e2a522009-09-07 16:32:45 -070062const unsigned int NUM_DISPLAY_MAX = 4;
63
64// ----------------------------------------------------------------------------
65
66class Region;
67class SharedBufferStack;
68class SharedClient;
69
70// ----------------------------------------------------------------------------
71
Mathias Agopiancdaaf322010-04-05 16:21:53 -070072// 4 * (11 + 7 + (1 + 2*NUM_RECT_MAX) * NUM_BUFFER_MAX) * NUM_LAYERS_MAX
Mathias Agopiane9e4d542010-04-15 18:48:26 -070073// 4 * (11 + 7 + (1 + 2*7)*16) * 31
74// 1032 * 31
75// = ~27 KiB (31992)
Mathias Agopiancdaaf322010-04-05 16:21:53 -070076
Mathias Agopian81e2a522009-09-07 16:32:45 -070077class SharedBufferStack
78{
79 friend class SharedClient;
80 friend class SharedBufferBase;
81 friend class SharedBufferClient;
82 friend class SharedBufferServer;
83
84public:
Mathias Agopian9e3ebf82009-09-17 01:35:28 -070085 struct Statistics { // 4 longs
86 typedef int32_t usecs_t;
87 usecs_t totalTime;
88 usecs_t reserved[3];
89 };
Mathias Agopiane9e4d542010-04-15 18:48:26 -070090
91 struct SmallRect {
92 uint16_t l, t, r, b;
93 };
94
95 struct FlatRegion { // 52 bytes = 4 * (1 + 2*N)
96 static const unsigned int NUM_RECT_MAX = 6;
97 uint32_t count;
98 SmallRect rects[NUM_RECT_MAX];
99 };
100
101 struct BufferData {
102 FlatRegion dirtyRegion;
103 SmallRect crop;
104 };
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700105
Mathias Agopian81e2a522009-09-07 16:32:45 -0700106 SharedBufferStack();
Mathias Agopian4fc61bf2009-09-10 19:41:18 -0700107 void init(int32_t identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700108 status_t setDirtyRegion(int buffer, const Region& reg);
Mathias Agopiane9e4d542010-04-15 18:48:26 -0700109 status_t setCrop(int buffer, const Rect& reg);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700110 Region getDirtyRegion(int buffer) const;
111
112 // these attributes are part of the conditions/updates
113 volatile int32_t head; // server's current front buffer
114 volatile int32_t available; // number of dequeue-able buffers
115 volatile int32_t queued; // number of buffers waiting for post
116 volatile int32_t inUse; // buffer currently in use by SF
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700117 volatile status_t status; // surface's status code
Mathias Agopian81e2a522009-09-07 16:32:45 -0700118
119 // not part of the conditions
120 volatile int32_t reallocMask;
121
122 int32_t identity; // surface's identity (const)
Mathias Agopiancdaaf322010-04-05 16:21:53 -0700123 int32_t reserved32[6];
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700124 Statistics stats;
Mathias Agopiancdaaf322010-04-05 16:21:53 -0700125 int32_t reserved;
Mathias Agopiane9e4d542010-04-15 18:48:26 -0700126 BufferData buffers[NUM_BUFFER_MAX]; // 960 bytes
Mathias Agopian81e2a522009-09-07 16:32:45 -0700127};
128
129// ----------------------------------------------------------------------------
130
131// 4 KB max
132class SharedClient
133{
134public:
135 SharedClient();
136 ~SharedClient();
137
138 status_t validate(size_t token) const;
139 uint32_t getIdentity(size_t token) const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700140
141private:
142 friend class SharedBufferBase;
143 friend class SharedBufferClient;
144 friend class SharedBufferServer;
145
146 // FIXME: this should be replaced by a lock-less primitive
147 Mutex lock;
148 Condition cv;
149 SharedBufferStack surfaces[ NUM_LAYERS_MAX ];
150};
151
152// ============================================================================
153
154class SharedBufferBase
155{
156public:
Mathias Agopiand46758b2009-10-06 19:00:57 -0700157 SharedBufferBase(SharedClient* sharedClient, int surface, int num,
158 int32_t identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700159 ~SharedBufferBase();
160 uint32_t getIdentity();
Mathias Agopiandefd1bd2009-10-02 18:12:30 -0700161 status_t getStatus() const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700162 size_t getFrontBuffer() const;
163 String8 dump(char const* prefix) const;
164
165protected:
166 SharedClient* const mSharedClient;
167 SharedBufferStack* const mSharedStack;
168 const int mNumBuffers;
Mathias Agopiand46758b2009-10-06 19:00:57 -0700169 const int mIdentity;
Mathias Agopian43d8a282010-04-27 16:11:38 -0700170 int32_t computeTail() const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700171
172 friend struct Update;
173 friend struct QueueUpdate;
174
175 struct ConditionBase {
176 SharedBufferStack& stack;
177 inline ConditionBase(SharedBufferBase* sbc)
178 : stack(*sbc->mSharedStack) { }
179 };
180
181 struct UpdateBase {
182 SharedBufferStack& stack;
183 inline UpdateBase(SharedBufferBase* sbb)
184 : stack(*sbb->mSharedStack) { }
185 };
186
187 template <typename T>
188 status_t waitForCondition(T condition);
189
190 template <typename T>
191 status_t updateCondition(T update);
192};
193
194template <typename T>
195status_t SharedBufferBase::waitForCondition(T condition)
196{
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700197 const SharedBufferStack& stack( *mSharedStack );
Mathias Agopian81e2a522009-09-07 16:32:45 -0700198 SharedClient& client( *mSharedClient );
Mathias Agopiandefd1bd2009-10-02 18:12:30 -0700199 const nsecs_t TIMEOUT = s2ns(1);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700200 Mutex::Autolock _l(client.lock);
Mathias Agopiand46758b2009-10-06 19:00:57 -0700201 while ((condition()==false) &&
202 (stack.identity == mIdentity) &&
203 (stack.status == NO_ERROR))
204 {
Mathias Agopian81e2a522009-09-07 16:32:45 -0700205 status_t err = client.cv.waitRelative(client.lock, TIMEOUT);
206
207 // handle errors and timeouts
208 if (CC_UNLIKELY(err != NO_ERROR)) {
209 if (err == TIMED_OUT) {
210 if (condition()) {
211 LOGE("waitForCondition(%s) timed out (identity=%d), "
Mathias Agopiandefd1bd2009-10-02 18:12:30 -0700212 "but condition is true! We recovered but it "
213 "shouldn't happen." , T::name(),
Mathias Agopiand46758b2009-10-06 19:00:57 -0700214 stack.identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700215 break;
216 } else {
Mathias Agopiandefd1bd2009-10-02 18:12:30 -0700217 LOGW("waitForCondition(%s) timed out "
218 "(identity=%d, status=%d). "
219 "CPU may be pegged. trying again.", T::name(),
Mathias Agopiand46758b2009-10-06 19:00:57 -0700220 stack.identity, stack.status);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700221 }
222 } else {
223 LOGE("waitForCondition(%s) error (%s) ",
224 T::name(), strerror(-err));
225 return err;
226 }
227 }
228 }
Mathias Agopiand46758b2009-10-06 19:00:57 -0700229 return (stack.identity != mIdentity) ? status_t(BAD_INDEX) : stack.status;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700230}
231
232
233template <typename T>
234status_t SharedBufferBase::updateCondition(T update) {
235 SharedClient& client( *mSharedClient );
236 Mutex::Autolock _l(client.lock);
237 ssize_t result = update();
238 client.cv.broadcast();
239 return result;
240}
241
242// ----------------------------------------------------------------------------
243
244class SharedBufferClient : public SharedBufferBase
245{
246public:
Mathias Agopiand46758b2009-10-06 19:00:57 -0700247 SharedBufferClient(SharedClient* sharedClient, int surface, int num,
248 int32_t identity);
249
Mathias Agopian81e2a522009-09-07 16:32:45 -0700250 ssize_t dequeue();
251 status_t undoDequeue(int buf);
252
253 status_t lock(int buf);
254 status_t queue(int buf);
255 bool needNewBuffer(int buffer) const;
256 status_t setDirtyRegion(int buffer, const Region& reg);
Mathias Agopiane9e4d542010-04-15 18:48:26 -0700257 status_t setCrop(int buffer, const Rect& reg);
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700258
Mathias Agopian81e2a522009-09-07 16:32:45 -0700259private:
260 friend struct Condition;
261 friend struct DequeueCondition;
262 friend struct LockCondition;
263
264 struct QueueUpdate : public UpdateBase {
265 inline QueueUpdate(SharedBufferBase* sbb);
266 inline ssize_t operator()();
267 };
268
269 struct UndoDequeueUpdate : public UpdateBase {
270 inline UndoDequeueUpdate(SharedBufferBase* sbb);
271 inline ssize_t operator()();
272 };
273
274 // --
275
276 struct DequeueCondition : public ConditionBase {
277 inline DequeueCondition(SharedBufferClient* sbc);
278 inline bool operator()();
279 static inline const char* name() { return "DequeueCondition"; }
280 };
281
282 struct LockCondition : public ConditionBase {
283 int buf;
284 inline LockCondition(SharedBufferClient* sbc, int buf);
285 inline bool operator()();
286 static inline const char* name() { return "LockCondition"; }
287 };
288
289 int32_t tail;
Mathias Agopian43d8a282010-04-27 16:11:38 -0700290 int32_t undoDequeueTail;
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700291 // statistics...
292 nsecs_t mDequeueTime[NUM_BUFFER_MAX];
Mathias Agopian81e2a522009-09-07 16:32:45 -0700293};
294
295// ----------------------------------------------------------------------------
296
297class SharedBufferServer : public SharedBufferBase
298{
299public:
Mathias Agopian4fc61bf2009-09-10 19:41:18 -0700300 SharedBufferServer(SharedClient* sharedClient, int surface, int num,
301 int32_t identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700302
303 ssize_t retireAndLock();
304 status_t unlock(int buffer);
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700305 void setStatus(status_t status);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700306 status_t reallocate();
307 status_t assertReallocate(int buffer);
Mathias Agopian68174b12009-10-07 16:44:10 -0700308 int32_t getQueuedCount() const;
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700309
Mathias Agopian81e2a522009-09-07 16:32:45 -0700310 Region getDirtyRegion(int buffer) const;
311
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700312 SharedBufferStack::Statistics getStats() const;
313
314
Mathias Agopian81e2a522009-09-07 16:32:45 -0700315private:
316 struct UnlockUpdate : public UpdateBase {
317 const int lockedBuffer;
318 inline UnlockUpdate(SharedBufferBase* sbb, int lockedBuffer);
319 inline ssize_t operator()();
320 };
321
322 struct RetireUpdate : public UpdateBase {
323 const int numBuffers;
324 inline RetireUpdate(SharedBufferBase* sbb, int numBuffers);
325 inline ssize_t operator()();
326 };
327
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700328 struct StatusUpdate : public UpdateBase {
329 const status_t status;
330 inline StatusUpdate(SharedBufferBase* sbb, status_t status);
331 inline ssize_t operator()();
332 };
333
Mathias Agopian81e2a522009-09-07 16:32:45 -0700334 struct ReallocateCondition : public ConditionBase {
335 int buf;
336 inline ReallocateCondition(SharedBufferBase* sbb, int buf);
337 inline bool operator()();
338 static inline const char* name() { return "ReallocateCondition"; }
339 };
340};
341
342// ===========================================================================
343
344struct display_cblk_t
345{
346 uint16_t w;
347 uint16_t h;
348 uint8_t format;
349 uint8_t orientation;
350 uint8_t reserved[2];
351 float fps;
352 float density;
353 float xdpi;
354 float ydpi;
355 uint32_t pad[2];
356};
357
358struct surface_flinger_cblk_t // 4KB max
359{
360 uint8_t connected;
361 uint8_t reserved[3];
362 uint32_t pad[7];
363 display_cblk_t displays[NUM_DISPLAY_MAX];
364};
365
366// ---------------------------------------------------------------------------
367
Mathias Agopiancdaaf322010-04-05 16:21:53 -0700368COMPILE_TIME_ASSERT(sizeof(SharedClient) <= 32768)
Mathias Agopian81e2a522009-09-07 16:32:45 -0700369COMPILE_TIME_ASSERT(sizeof(surface_flinger_cblk_t) <= 4096)
370
371// ---------------------------------------------------------------------------
372}; // namespace android
373
Mathias Agopian3cf61352010-02-09 17:46:37 -0800374#endif /* ANDROID_SF_SHARED_BUFFER_STACK_H */