blob: c23832d4925b4c2d71dbe0e428b6abfd3fe97179 [file] [log] [blame]
Mathias Agopiancbb288b2009-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 Agopian9cce3252010-02-09 17:46:37 -080017#ifndef ANDROID_SF_SHARED_BUFFER_STACK_H
18#define ANDROID_SF_SHARED_BUFFER_STACK_H
Mathias Agopiancbb288b2009-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 Agopian1100c8b2010-04-05 16:21:53 -070061const unsigned int NUM_BUFFER_MAX = 16;
Mathias Agopiancbb288b2009-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 Agopian1100c8b2010-04-05 16:21:53 -070072// 4 * (11 + 7 + (1 + 2*NUM_RECT_MAX) * NUM_BUFFER_MAX) * NUM_LAYERS_MAX
Mathias Agopiancc08e682010-04-15 18:48:26 -070073// 4 * (11 + 7 + (1 + 2*7)*16) * 31
74// 1032 * 31
75// = ~27 KiB (31992)
Mathias Agopian1100c8b2010-04-05 16:21:53 -070076
Mathias Agopiancbb288b2009-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 Agopian86f73292009-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 Agopiancc08e682010-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 Agopian86f73292009-09-17 01:35:28 -0700105
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700106 SharedBufferStack();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700107 void init(int32_t identity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700108 status_t setDirtyRegion(int buffer, const Region& reg);
Mathias Agopiancc08e682010-04-15 18:48:26 -0700109 status_t setCrop(int buffer, const Rect& reg);
Mathias Agopiancbb288b2009-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 Agopianb58b5d72009-09-10 16:55:13 -0700117 volatile status_t status; // surface's status code
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700118
119 // not part of the conditions
120 volatile int32_t reallocMask;
Mathias Agopianc0a91642010-04-27 21:08:20 -0700121 volatile int8_t index[NUM_BUFFER_MAX];
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700122
123 int32_t identity; // surface's identity (const)
Mathias Agopianc0a91642010-04-27 21:08:20 -0700124 int32_t reserved32[2];
Mathias Agopian86f73292009-09-17 01:35:28 -0700125 Statistics stats;
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700126 int32_t reserved;
Mathias Agopiancc08e682010-04-15 18:48:26 -0700127 BufferData buffers[NUM_BUFFER_MAX]; // 960 bytes
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700128};
129
130// ----------------------------------------------------------------------------
131
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700132// 32 KB max
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700133class SharedClient
134{
135public:
136 SharedClient();
137 ~SharedClient();
138
139 status_t validate(size_t token) const;
140 uint32_t getIdentity(size_t token) const;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700141
142private:
143 friend class SharedBufferBase;
144 friend class SharedBufferClient;
145 friend class SharedBufferServer;
146
147 // FIXME: this should be replaced by a lock-less primitive
148 Mutex lock;
149 Condition cv;
150 SharedBufferStack surfaces[ NUM_LAYERS_MAX ];
151};
152
153// ============================================================================
154
155class SharedBufferBase
156{
157public:
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700158 SharedBufferBase(SharedClient* sharedClient, int surface, int num,
159 int32_t identity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700160 ~SharedBufferBase();
161 uint32_t getIdentity();
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700162 status_t getStatus() const;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700163 size_t getFrontBuffer() const;
164 String8 dump(char const* prefix) const;
165
166protected:
167 SharedClient* const mSharedClient;
168 SharedBufferStack* const mSharedStack;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700169 int mNumBuffers;
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700170 const int mIdentity;
Mathias Agopian0a8cd062010-04-27 16:11:38 -0700171 int32_t computeTail() const;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700172
173 friend struct Update;
174 friend struct QueueUpdate;
175
176 struct ConditionBase {
177 SharedBufferStack& stack;
178 inline ConditionBase(SharedBufferBase* sbc)
179 : stack(*sbc->mSharedStack) { }
Mathias Agopianb2965332010-04-27 16:41:19 -0700180 virtual ~ConditionBase() { };
181 virtual bool operator()() const = 0;
182 virtual const char* name() const = 0;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700183 };
Mathias Agopianb2965332010-04-27 16:41:19 -0700184 status_t waitForCondition(const ConditionBase& condition);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700185
186 struct UpdateBase {
187 SharedBufferStack& stack;
188 inline UpdateBase(SharedBufferBase* sbb)
189 : stack(*sbb->mSharedStack) { }
190 };
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700191 template <typename T>
192 status_t updateCondition(T update);
193};
194
195template <typename T>
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700196status_t SharedBufferBase::updateCondition(T update) {
197 SharedClient& client( *mSharedClient );
198 Mutex::Autolock _l(client.lock);
199 ssize_t result = update();
200 client.cv.broadcast();
201 return result;
202}
203
204// ----------------------------------------------------------------------------
205
206class SharedBufferClient : public SharedBufferBase
207{
208public:
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700209 SharedBufferClient(SharedClient* sharedClient, int surface, int num,
210 int32_t identity);
211
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700212 ssize_t dequeue();
213 status_t undoDequeue(int buf);
214
215 status_t lock(int buf);
216 status_t queue(int buf);
217 bool needNewBuffer(int buffer) const;
218 status_t setDirtyRegion(int buffer, const Region& reg);
Mathias Agopiancc08e682010-04-15 18:48:26 -0700219 status_t setCrop(int buffer, const Rect& reg);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700220 status_t setBufferCount(int bufferCount);
Mathias Agopian86f73292009-09-17 01:35:28 -0700221
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700222private:
223 friend struct Condition;
224 friend struct DequeueCondition;
225 friend struct LockCondition;
226
227 struct QueueUpdate : public UpdateBase {
228 inline QueueUpdate(SharedBufferBase* sbb);
229 inline ssize_t operator()();
230 };
231
232 struct UndoDequeueUpdate : public UpdateBase {
233 inline UndoDequeueUpdate(SharedBufferBase* sbb);
234 inline ssize_t operator()();
235 };
236
237 // --
238
239 struct DequeueCondition : public ConditionBase {
240 inline DequeueCondition(SharedBufferClient* sbc);
Mathias Agopianb2965332010-04-27 16:41:19 -0700241 inline bool operator()() const;
242 inline const char* name() const { return "DequeueCondition"; }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700243 };
244
245 struct LockCondition : public ConditionBase {
246 int buf;
247 inline LockCondition(SharedBufferClient* sbc, int buf);
Mathias Agopianb2965332010-04-27 16:41:19 -0700248 inline bool operator()() const;
249 inline const char* name() const { return "LockCondition"; }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700250 };
251
252 int32_t tail;
Mathias Agopian0a8cd062010-04-27 16:11:38 -0700253 int32_t undoDequeueTail;
Mathias Agopianc0a91642010-04-27 21:08:20 -0700254 int32_t queued_head;
Mathias Agopian86f73292009-09-17 01:35:28 -0700255 // statistics...
256 nsecs_t mDequeueTime[NUM_BUFFER_MAX];
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700257};
258
259// ----------------------------------------------------------------------------
260
261class SharedBufferServer : public SharedBufferBase
262{
263public:
Mathias Agopian48d819a2009-09-10 19:41:18 -0700264 SharedBufferServer(SharedClient* sharedClient, int surface, int num,
265 int32_t identity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700266
267 ssize_t retireAndLock();
268 status_t unlock(int buffer);
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700269 void setStatus(status_t status);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700270 status_t reallocate();
271 status_t assertReallocate(int buffer);
Mathias Agopiane7005012009-10-07 16:44:10 -0700272 int32_t getQueuedCount() const;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700273 Region getDirtyRegion(int buffer) const;
274
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700275 status_t resize(int newNumBuffers);
276
Mathias Agopian86f73292009-09-17 01:35:28 -0700277 SharedBufferStack::Statistics getStats() const;
278
279
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700280private:
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700281 /*
282 * BufferList is basically a fixed-capacity sorted-vector of
283 * unsigned 5-bits ints using a 32-bits int as storage.
284 * it has efficient iterators to find items in the list and not in the list.
285 */
286 class BufferList {
287 size_t mCapacity;
288 uint32_t mList;
289 public:
290 BufferList(size_t c = NUM_BUFFER_MAX) : mCapacity(c), mList(0) { }
291 status_t add(int value);
292 status_t remove(int value);
293
294 class const_iterator {
295 friend class BufferList;
296 uint32_t mask, curr;
297 const_iterator(uint32_t mask) :
298 mask(mask), curr(31 - __builtin_clz(mask)) { }
299 public:
300 inline bool operator == (const const_iterator& rhs) const {
301 return mask == rhs.mask;
302 }
303 inline bool operator != (const const_iterator& rhs) const {
304 return mask != rhs.mask;
305 }
306 inline int operator *() const { return curr; }
307 inline const const_iterator& operator ++(int) {
308 mask &= ~curr;
309 curr = 31 - __builtin_clz(mask);
310 return *this;
311 }
312 };
313
314 inline const_iterator begin() const {
315 return const_iterator(mList);
316 }
317 inline const_iterator end() const {
318 return const_iterator(0);
319 }
320 inline const_iterator free_begin() const {
321 uint32_t mask = (1 << (32-mCapacity)) - 1;
322 return const_iterator( ~(mList | mask) );
323 }
324 };
325
326 BufferList mBufferList;
327
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700328 struct UnlockUpdate : public UpdateBase {
329 const int lockedBuffer;
330 inline UnlockUpdate(SharedBufferBase* sbb, int lockedBuffer);
331 inline ssize_t operator()();
332 };
333
334 struct RetireUpdate : public UpdateBase {
335 const int numBuffers;
336 inline RetireUpdate(SharedBufferBase* sbb, int numBuffers);
337 inline ssize_t operator()();
338 };
339
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700340 struct StatusUpdate : public UpdateBase {
341 const status_t status;
342 inline StatusUpdate(SharedBufferBase* sbb, status_t status);
343 inline ssize_t operator()();
344 };
345
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700346 struct ReallocateCondition : public ConditionBase {
347 int buf;
348 inline ReallocateCondition(SharedBufferBase* sbb, int buf);
Mathias Agopianb2965332010-04-27 16:41:19 -0700349 inline bool operator()() const;
350 inline const char* name() const { return "ReallocateCondition"; }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700351 };
352};
353
354// ===========================================================================
355
356struct display_cblk_t
357{
358 uint16_t w;
359 uint16_t h;
360 uint8_t format;
361 uint8_t orientation;
362 uint8_t reserved[2];
363 float fps;
364 float density;
365 float xdpi;
366 float ydpi;
367 uint32_t pad[2];
368};
369
370struct surface_flinger_cblk_t // 4KB max
371{
372 uint8_t connected;
373 uint8_t reserved[3];
374 uint32_t pad[7];
375 display_cblk_t displays[NUM_DISPLAY_MAX];
376};
377
378// ---------------------------------------------------------------------------
379
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700380COMPILE_TIME_ASSERT(sizeof(SharedClient) <= 32768)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700381COMPILE_TIME_ASSERT(sizeof(surface_flinger_cblk_t) <= 4096)
382
383// ---------------------------------------------------------------------------
384}; // namespace android
385
Mathias Agopian9cce3252010-02-09 17:46:37 -0800386#endif /* ANDROID_SF_SHARED_BUFFER_STACK_H */