blob: ea8391d6acf8de72a2cd2a022c628cf8cfffc5e9 [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
Mathias Agopiancbb288b2009-09-07 16:32:45 -070058// ----------------------------------------------------------------------------
59
60class Region;
61class SharedBufferStack;
62class SharedClient;
63
64// ----------------------------------------------------------------------------
65
Mathias Agopian1100c8b2010-04-05 16:21:53 -070066// 4 * (11 + 7 + (1 + 2*NUM_RECT_MAX) * NUM_BUFFER_MAX) * NUM_LAYERS_MAX
Mathias Agopiancc08e682010-04-15 18:48:26 -070067// 4 * (11 + 7 + (1 + 2*7)*16) * 31
68// 1032 * 31
69// = ~27 KiB (31992)
Mathias Agopian1100c8b2010-04-05 16:21:53 -070070
Mathias Agopiancbb288b2009-09-07 16:32:45 -070071class SharedBufferStack
72{
73 friend class SharedClient;
74 friend class SharedBufferBase;
75 friend class SharedBufferClient;
76 friend class SharedBufferServer;
77
78public:
Mathias Agopianbb641242010-05-18 17:06:55 -070079 // When changing these values, the COMPILE_TIME_ASSERT at the end of this
80 // file need to be updated.
81 static const unsigned int NUM_LAYERS_MAX = 31;
82 static const unsigned int NUM_BUFFER_MAX = 16;
Mathias Agopianf10d7fd2010-05-21 14:19:50 -070083 static const unsigned int NUM_BUFFER_MIN = 2;
Mathias Agopianbb641242010-05-18 17:06:55 -070084 static const unsigned int NUM_DISPLAY_MAX = 4;
85
Mathias Agopian86f73292009-09-17 01:35:28 -070086 struct Statistics { // 4 longs
87 typedef int32_t usecs_t;
88 usecs_t totalTime;
89 usecs_t reserved[3];
90 };
Mathias Agopiancc08e682010-04-15 18:48:26 -070091
92 struct SmallRect {
93 uint16_t l, t, r, b;
94 };
95
96 struct FlatRegion { // 52 bytes = 4 * (1 + 2*N)
97 static const unsigned int NUM_RECT_MAX = 6;
98 uint32_t count;
99 SmallRect rects[NUM_RECT_MAX];
100 };
101
102 struct BufferData {
103 FlatRegion dirtyRegion;
104 SmallRect crop;
105 };
Mathias Agopian86f73292009-09-17 01:35:28 -0700106
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700107 SharedBufferStack();
Mathias Agopian48d819a2009-09-10 19:41:18 -0700108 void init(int32_t identity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700109 status_t setDirtyRegion(int buffer, const Region& reg);
Mathias Agopiancc08e682010-04-15 18:48:26 -0700110 status_t setCrop(int buffer, const Rect& reg);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700111 Region getDirtyRegion(int buffer) const;
112
113 // these attributes are part of the conditions/updates
114 volatile int32_t head; // server's current front buffer
115 volatile int32_t available; // number of dequeue-able buffers
116 volatile int32_t queued; // number of buffers waiting for post
117 volatile int32_t inUse; // buffer currently in use by SF
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700118 volatile status_t status; // surface's status code
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700119
120 // not part of the conditions
121 volatile int32_t reallocMask;
Mathias Agopianc0a91642010-04-27 21:08:20 -0700122 volatile int8_t index[NUM_BUFFER_MAX];
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700123
124 int32_t identity; // surface's identity (const)
Mathias Agopianc0a91642010-04-27 21:08:20 -0700125 int32_t reserved32[2];
Mathias Agopian86f73292009-09-17 01:35:28 -0700126 Statistics stats;
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700127 int32_t reserved;
Mathias Agopiancc08e682010-04-15 18:48:26 -0700128 BufferData buffers[NUM_BUFFER_MAX]; // 960 bytes
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700129};
130
131// ----------------------------------------------------------------------------
132
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700133// 32 KB max
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700134class SharedClient
135{
136public:
137 SharedClient();
138 ~SharedClient();
139
140 status_t validate(size_t token) const;
141 uint32_t getIdentity(size_t token) const;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700142
143private:
144 friend class SharedBufferBase;
145 friend class SharedBufferClient;
146 friend class SharedBufferServer;
147
148 // FIXME: this should be replaced by a lock-less primitive
149 Mutex lock;
150 Condition cv;
Mathias Agopianbb641242010-05-18 17:06:55 -0700151 SharedBufferStack surfaces[ SharedBufferStack::NUM_LAYERS_MAX ];
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700152};
153
154// ============================================================================
155
156class SharedBufferBase
157{
158public:
Mathias Agopianbb641242010-05-18 17:06:55 -0700159 SharedBufferBase(SharedClient* sharedClient, int surface,
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700160 int32_t identity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700161 ~SharedBufferBase();
162 uint32_t getIdentity();
Mathias Agopian0b3ad462009-10-02 18:12:30 -0700163 status_t getStatus() const;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700164 size_t getFrontBuffer() const;
165 String8 dump(char const* prefix) const;
166
167protected:
168 SharedClient* const mSharedClient;
169 SharedBufferStack* const mSharedStack;
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700170 const int mIdentity;
Mathias Agopiancbb288b2009-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) { }
Mathias Agopianb2965332010-04-27 16:41:19 -0700179 virtual ~ConditionBase() { };
180 virtual bool operator()() const = 0;
181 virtual const char* name() const = 0;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700182 };
Mathias Agopianb2965332010-04-27 16:41:19 -0700183 status_t waitForCondition(const ConditionBase& condition);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700184
185 struct UpdateBase {
186 SharedBufferStack& stack;
187 inline UpdateBase(SharedBufferBase* sbb)
188 : stack(*sbb->mSharedStack) { }
189 };
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700190 template <typename T>
191 status_t updateCondition(T update);
192};
193
194template <typename T>
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700195status_t SharedBufferBase::updateCondition(T update) {
196 SharedClient& client( *mSharedClient );
197 Mutex::Autolock _l(client.lock);
198 ssize_t result = update();
199 client.cv.broadcast();
200 return result;
201}
202
203// ----------------------------------------------------------------------------
204
205class SharedBufferClient : public SharedBufferBase
206{
207public:
Mathias Agopian9ec430a2009-10-06 19:00:57 -0700208 SharedBufferClient(SharedClient* sharedClient, int surface, int num,
209 int32_t identity);
210
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700211 ssize_t dequeue();
212 status_t undoDequeue(int buf);
213
214 status_t lock(int buf);
215 status_t queue(int buf);
216 bool needNewBuffer(int buffer) const;
217 status_t setDirtyRegion(int buffer, const Region& reg);
Mathias Agopiancc08e682010-04-15 18:48:26 -0700218 status_t setCrop(int buffer, const Rect& reg);
Mathias Agopian86f73292009-09-17 01:35:28 -0700219
Mathias Agopianbb641242010-05-18 17:06:55 -0700220
221 class SetBufferCountCallback {
222 friend class SharedBufferClient;
223 virtual status_t operator()(int bufferCount) const = 0;
224 protected:
225 virtual ~SetBufferCountCallback() { }
226 };
227 status_t setBufferCount(int bufferCount, const SetBufferCountCallback& ipc);
228
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700229private:
230 friend struct Condition;
231 friend struct DequeueCondition;
232 friend struct LockCondition;
233
234 struct QueueUpdate : public UpdateBase {
235 inline QueueUpdate(SharedBufferBase* sbb);
236 inline ssize_t operator()();
237 };
238
239 struct UndoDequeueUpdate : public UpdateBase {
240 inline UndoDequeueUpdate(SharedBufferBase* sbb);
241 inline ssize_t operator()();
242 };
243
244 // --
245
246 struct DequeueCondition : public ConditionBase {
247 inline DequeueCondition(SharedBufferClient* sbc);
Mathias Agopianb2965332010-04-27 16:41:19 -0700248 inline bool operator()() const;
249 inline const char* name() const { return "DequeueCondition"; }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700250 };
251
252 struct LockCondition : public ConditionBase {
253 int buf;
254 inline LockCondition(SharedBufferClient* sbc, int buf);
Mathias Agopianb2965332010-04-27 16:41:19 -0700255 inline bool operator()() const;
256 inline const char* name() const { return "LockCondition"; }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700257 };
258
Mathias Agopianbb641242010-05-18 17:06:55 -0700259 int32_t computeTail() const;
260
261 mutable RWLock mLock;
262 int mNumBuffers;
263
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700264 int32_t tail;
Mathias Agopian0a8cd062010-04-27 16:11:38 -0700265 int32_t undoDequeueTail;
Mathias Agopianc0a91642010-04-27 21:08:20 -0700266 int32_t queued_head;
Mathias Agopian86f73292009-09-17 01:35:28 -0700267 // statistics...
Mathias Agopianbb641242010-05-18 17:06:55 -0700268 nsecs_t mDequeueTime[SharedBufferStack::NUM_BUFFER_MAX];
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700269};
270
271// ----------------------------------------------------------------------------
272
273class SharedBufferServer : public SharedBufferBase
274{
275public:
Mathias Agopian48d819a2009-09-10 19:41:18 -0700276 SharedBufferServer(SharedClient* sharedClient, int surface, int num,
277 int32_t identity);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700278
279 ssize_t retireAndLock();
280 status_t unlock(int buffer);
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700281 void setStatus(status_t status);
Mathias Agopiana138f892010-05-21 17:24:35 -0700282 status_t reallocateAll();
283 status_t reallocateAllExcept(int buffer);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700284 status_t assertReallocate(int buffer);
Mathias Agopiane7005012009-10-07 16:44:10 -0700285 int32_t getQueuedCount() const;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700286 Region getDirtyRegion(int buffer) const;
287
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700288 status_t resize(int newNumBuffers);
289
Mathias Agopian86f73292009-09-17 01:35:28 -0700290 SharedBufferStack::Statistics getStats() const;
291
292
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700293private:
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700294 /*
295 * BufferList is basically a fixed-capacity sorted-vector of
296 * unsigned 5-bits ints using a 32-bits int as storage.
297 * it has efficient iterators to find items in the list and not in the list.
298 */
299 class BufferList {
300 size_t mCapacity;
301 uint32_t mList;
302 public:
Mathias Agopianbb641242010-05-18 17:06:55 -0700303 BufferList(size_t c = SharedBufferStack::NUM_BUFFER_MAX)
304 : mCapacity(c), mList(0) { }
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700305 status_t add(int value);
306 status_t remove(int value);
Mathias Agopiana0b3f1d2010-05-21 14:51:33 -0700307 uint32_t getMask() const { return mList; }
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700308
309 class const_iterator {
310 friend class BufferList;
311 uint32_t mask, curr;
312 const_iterator(uint32_t mask) :
Mathias Agopiand6297f72010-05-17 17:27:26 -0700313 mask(mask), curr(__builtin_clz(mask)) {
314 }
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700315 public:
316 inline bool operator == (const const_iterator& rhs) const {
317 return mask == rhs.mask;
318 }
319 inline bool operator != (const const_iterator& rhs) const {
320 return mask != rhs.mask;
321 }
322 inline int operator *() const { return curr; }
Mathias Agopiand6297f72010-05-17 17:27:26 -0700323 inline const const_iterator& operator ++() {
324 mask &= ~(1<<(31-curr));
325 curr = __builtin_clz(mask);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700326 return *this;
327 }
328 };
329
330 inline const_iterator begin() const {
331 return const_iterator(mList);
332 }
333 inline const_iterator end() const {
334 return const_iterator(0);
335 }
336 inline const_iterator free_begin() const {
337 uint32_t mask = (1 << (32-mCapacity)) - 1;
338 return const_iterator( ~(mList | mask) );
339 }
340 };
341
Mathias Agopianbb641242010-05-18 17:06:55 -0700342 // this protects mNumBuffers and mBufferList
343 mutable RWLock mLock;
344 int mNumBuffers;
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700345 BufferList mBufferList;
346
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700347 struct UnlockUpdate : public UpdateBase {
348 const int lockedBuffer;
349 inline UnlockUpdate(SharedBufferBase* sbb, int lockedBuffer);
350 inline ssize_t operator()();
351 };
352
353 struct RetireUpdate : public UpdateBase {
354 const int numBuffers;
355 inline RetireUpdate(SharedBufferBase* sbb, int numBuffers);
356 inline ssize_t operator()();
357 };
358
Mathias Agopianb58b5d72009-09-10 16:55:13 -0700359 struct StatusUpdate : public UpdateBase {
360 const status_t status;
361 inline StatusUpdate(SharedBufferBase* sbb, status_t status);
362 inline ssize_t operator()();
363 };
364
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700365 struct ReallocateCondition : public ConditionBase {
366 int buf;
367 inline ReallocateCondition(SharedBufferBase* sbb, int buf);
Mathias Agopianb2965332010-04-27 16:41:19 -0700368 inline bool operator()() const;
369 inline const char* name() const { return "ReallocateCondition"; }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700370 };
371};
372
373// ===========================================================================
374
375struct display_cblk_t
376{
377 uint16_t w;
378 uint16_t h;
379 uint8_t format;
380 uint8_t orientation;
381 uint8_t reserved[2];
382 float fps;
383 float density;
384 float xdpi;
385 float ydpi;
386 uint32_t pad[2];
387};
388
389struct surface_flinger_cblk_t // 4KB max
390{
391 uint8_t connected;
392 uint8_t reserved[3];
393 uint32_t pad[7];
Mathias Agopianbb641242010-05-18 17:06:55 -0700394 display_cblk_t displays[SharedBufferStack::NUM_DISPLAY_MAX];
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700395};
396
397// ---------------------------------------------------------------------------
398
Mathias Agopian1100c8b2010-04-05 16:21:53 -0700399COMPILE_TIME_ASSERT(sizeof(SharedClient) <= 32768)
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700400COMPILE_TIME_ASSERT(sizeof(surface_flinger_cblk_t) <= 4096)
401
402// ---------------------------------------------------------------------------
403}; // namespace android
404
Mathias Agopian9cce3252010-02-09 17:46:37 -0800405#endif /* ANDROID_SF_SHARED_BUFFER_STACK_H */