blob: 3b9768b6b7db3d0deadb1410451cc5712d660998 [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
73// 4 * (11 + 7 + (1 + 2*6)*16) * 31
74// 904 * 31
75// = ~27 KiB (28024)
76
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 Agopiancdaaf322010-04-05 16:21:53 -070085 struct FlatRegion { // 52 bytes = 4 * (1 + 2*N)
86 static const unsigned int NUM_RECT_MAX = 6;
Mathias Agopian9e3ebf82009-09-17 01:35:28 -070087 uint32_t count;
88 uint16_t rects[4*NUM_RECT_MAX];
89 };
90
91 struct Statistics { // 4 longs
92 typedef int32_t usecs_t;
93 usecs_t totalTime;
94 usecs_t reserved[3];
95 };
96
Mathias Agopian81e2a522009-09-07 16:32:45 -070097 SharedBufferStack();
Mathias Agopian4fc61bf2009-09-10 19:41:18 -070098 void init(int32_t identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -070099 status_t setDirtyRegion(int buffer, const Region& reg);
100 Region getDirtyRegion(int buffer) const;
101
102 // these attributes are part of the conditions/updates
103 volatile int32_t head; // server's current front buffer
104 volatile int32_t available; // number of dequeue-able buffers
105 volatile int32_t queued; // number of buffers waiting for post
106 volatile int32_t inUse; // buffer currently in use by SF
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700107 volatile status_t status; // surface's status code
Mathias Agopian81e2a522009-09-07 16:32:45 -0700108
109 // not part of the conditions
110 volatile int32_t reallocMask;
111
112 int32_t identity; // surface's identity (const)
Mathias Agopiancdaaf322010-04-05 16:21:53 -0700113 int32_t reserved32[6];
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700114 Statistics stats;
Mathias Agopiancdaaf322010-04-05 16:21:53 -0700115 int32_t reserved;
116 FlatRegion dirtyRegion[NUM_BUFFER_MAX]; // 832 bytes
Mathias Agopian81e2a522009-09-07 16:32:45 -0700117};
118
119// ----------------------------------------------------------------------------
120
121// 4 KB max
122class SharedClient
123{
124public:
125 SharedClient();
126 ~SharedClient();
127
128 status_t validate(size_t token) const;
129 uint32_t getIdentity(size_t token) const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700130
131private:
132 friend class SharedBufferBase;
133 friend class SharedBufferClient;
134 friend class SharedBufferServer;
135
136 // FIXME: this should be replaced by a lock-less primitive
137 Mutex lock;
138 Condition cv;
139 SharedBufferStack surfaces[ NUM_LAYERS_MAX ];
140};
141
142// ============================================================================
143
144class SharedBufferBase
145{
146public:
Mathias Agopiand46758b2009-10-06 19:00:57 -0700147 SharedBufferBase(SharedClient* sharedClient, int surface, int num,
148 int32_t identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700149 ~SharedBufferBase();
150 uint32_t getIdentity();
Mathias Agopiandefd1bd2009-10-02 18:12:30 -0700151 status_t getStatus() const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700152 size_t getFrontBuffer() const;
153 String8 dump(char const* prefix) const;
154
155protected:
156 SharedClient* const mSharedClient;
157 SharedBufferStack* const mSharedStack;
158 const int mNumBuffers;
Mathias Agopiand46758b2009-10-06 19:00:57 -0700159 const int mIdentity;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700160
161 friend struct Update;
162 friend struct QueueUpdate;
163
164 struct ConditionBase {
165 SharedBufferStack& stack;
166 inline ConditionBase(SharedBufferBase* sbc)
167 : stack(*sbc->mSharedStack) { }
168 };
169
170 struct UpdateBase {
171 SharedBufferStack& stack;
172 inline UpdateBase(SharedBufferBase* sbb)
173 : stack(*sbb->mSharedStack) { }
174 };
175
176 template <typename T>
177 status_t waitForCondition(T condition);
178
179 template <typename T>
180 status_t updateCondition(T update);
181};
182
183template <typename T>
184status_t SharedBufferBase::waitForCondition(T condition)
185{
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700186 const SharedBufferStack& stack( *mSharedStack );
Mathias Agopian81e2a522009-09-07 16:32:45 -0700187 SharedClient& client( *mSharedClient );
Mathias Agopiandefd1bd2009-10-02 18:12:30 -0700188 const nsecs_t TIMEOUT = s2ns(1);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700189 Mutex::Autolock _l(client.lock);
Mathias Agopiand46758b2009-10-06 19:00:57 -0700190 while ((condition()==false) &&
191 (stack.identity == mIdentity) &&
192 (stack.status == NO_ERROR))
193 {
Mathias Agopian81e2a522009-09-07 16:32:45 -0700194 status_t err = client.cv.waitRelative(client.lock, TIMEOUT);
195
196 // handle errors and timeouts
197 if (CC_UNLIKELY(err != NO_ERROR)) {
198 if (err == TIMED_OUT) {
199 if (condition()) {
200 LOGE("waitForCondition(%s) timed out (identity=%d), "
Mathias Agopiandefd1bd2009-10-02 18:12:30 -0700201 "but condition is true! We recovered but it "
202 "shouldn't happen." , T::name(),
Mathias Agopiand46758b2009-10-06 19:00:57 -0700203 stack.identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700204 break;
205 } else {
Mathias Agopiandefd1bd2009-10-02 18:12:30 -0700206 LOGW("waitForCondition(%s) timed out "
207 "(identity=%d, status=%d). "
208 "CPU may be pegged. trying again.", T::name(),
Mathias Agopiand46758b2009-10-06 19:00:57 -0700209 stack.identity, stack.status);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700210 }
211 } else {
212 LOGE("waitForCondition(%s) error (%s) ",
213 T::name(), strerror(-err));
214 return err;
215 }
216 }
217 }
Mathias Agopiand46758b2009-10-06 19:00:57 -0700218 return (stack.identity != mIdentity) ? status_t(BAD_INDEX) : stack.status;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700219}
220
221
222template <typename T>
223status_t SharedBufferBase::updateCondition(T update) {
224 SharedClient& client( *mSharedClient );
225 Mutex::Autolock _l(client.lock);
226 ssize_t result = update();
227 client.cv.broadcast();
228 return result;
229}
230
231// ----------------------------------------------------------------------------
232
233class SharedBufferClient : public SharedBufferBase
234{
235public:
Mathias Agopiand46758b2009-10-06 19:00:57 -0700236 SharedBufferClient(SharedClient* sharedClient, int surface, int num,
237 int32_t identity);
238
Mathias Agopian81e2a522009-09-07 16:32:45 -0700239 ssize_t dequeue();
240 status_t undoDequeue(int buf);
241
242 status_t lock(int buf);
243 status_t queue(int buf);
244 bool needNewBuffer(int buffer) const;
245 status_t setDirtyRegion(int buffer, const Region& reg);
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700246
Mathias Agopian81e2a522009-09-07 16:32:45 -0700247private:
248 friend struct Condition;
249 friend struct DequeueCondition;
250 friend struct LockCondition;
Mathias Agopian5a37cc52009-09-14 15:48:42 -0700251
252 int32_t computeTail() const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700253
254 struct QueueUpdate : public UpdateBase {
255 inline QueueUpdate(SharedBufferBase* sbb);
256 inline ssize_t operator()();
257 };
258
259 struct UndoDequeueUpdate : public UpdateBase {
260 inline UndoDequeueUpdate(SharedBufferBase* sbb);
261 inline ssize_t operator()();
262 };
263
264 // --
265
266 struct DequeueCondition : public ConditionBase {
267 inline DequeueCondition(SharedBufferClient* sbc);
268 inline bool operator()();
269 static inline const char* name() { return "DequeueCondition"; }
270 };
271
272 struct LockCondition : public ConditionBase {
273 int buf;
274 inline LockCondition(SharedBufferClient* sbc, int buf);
275 inline bool operator()();
276 static inline const char* name() { return "LockCondition"; }
277 };
278
279 int32_t tail;
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700280 // statistics...
281 nsecs_t mDequeueTime[NUM_BUFFER_MAX];
Mathias Agopian81e2a522009-09-07 16:32:45 -0700282};
283
284// ----------------------------------------------------------------------------
285
286class SharedBufferServer : public SharedBufferBase
287{
288public:
Mathias Agopian4fc61bf2009-09-10 19:41:18 -0700289 SharedBufferServer(SharedClient* sharedClient, int surface, int num,
290 int32_t identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700291
292 ssize_t retireAndLock();
293 status_t unlock(int buffer);
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700294 void setStatus(status_t status);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700295 status_t reallocate();
296 status_t assertReallocate(int buffer);
Mathias Agopian68174b12009-10-07 16:44:10 -0700297 int32_t getQueuedCount() const;
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700298
Mathias Agopian81e2a522009-09-07 16:32:45 -0700299 Region getDirtyRegion(int buffer) const;
300
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700301 SharedBufferStack::Statistics getStats() const;
302
303
Mathias Agopian81e2a522009-09-07 16:32:45 -0700304private:
305 struct UnlockUpdate : public UpdateBase {
306 const int lockedBuffer;
307 inline UnlockUpdate(SharedBufferBase* sbb, int lockedBuffer);
308 inline ssize_t operator()();
309 };
310
311 struct RetireUpdate : public UpdateBase {
312 const int numBuffers;
313 inline RetireUpdate(SharedBufferBase* sbb, int numBuffers);
314 inline ssize_t operator()();
315 };
316
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700317 struct StatusUpdate : public UpdateBase {
318 const status_t status;
319 inline StatusUpdate(SharedBufferBase* sbb, status_t status);
320 inline ssize_t operator()();
321 };
322
Mathias Agopian81e2a522009-09-07 16:32:45 -0700323 struct ReallocateCondition : public ConditionBase {
324 int buf;
325 inline ReallocateCondition(SharedBufferBase* sbb, int buf);
326 inline bool operator()();
327 static inline const char* name() { return "ReallocateCondition"; }
328 };
329};
330
331// ===========================================================================
332
333struct display_cblk_t
334{
335 uint16_t w;
336 uint16_t h;
337 uint8_t format;
338 uint8_t orientation;
339 uint8_t reserved[2];
340 float fps;
341 float density;
342 float xdpi;
343 float ydpi;
344 uint32_t pad[2];
345};
346
347struct surface_flinger_cblk_t // 4KB max
348{
349 uint8_t connected;
350 uint8_t reserved[3];
351 uint32_t pad[7];
352 display_cblk_t displays[NUM_DISPLAY_MAX];
353};
354
355// ---------------------------------------------------------------------------
356
Mathias Agopiancdaaf322010-04-05 16:21:53 -0700357COMPILE_TIME_ASSERT(sizeof(SharedClient) <= 32768)
Mathias Agopian81e2a522009-09-07 16:32:45 -0700358COMPILE_TIME_ASSERT(sizeof(surface_flinger_cblk_t) <= 4096)
359
360// ---------------------------------------------------------------------------
361}; // namespace android
362
Mathias Agopian3cf61352010-02-09 17:46:37 -0800363#endif /* ANDROID_SF_SHARED_BUFFER_STACK_H */