blob: dcce25e92329be4c4f32aea87877b39fa0d9b2ec [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
Mathias Agopian81e2a522009-09-07 16:32:45 -070058// ----------------------------------------------------------------------------
59
60class Region;
61class SharedBufferStack;
62class SharedClient;
63
64// ----------------------------------------------------------------------------
65
Mathias Agopiancdaaf322010-04-05 16:21:53 -070066// 4 * (11 + 7 + (1 + 2*NUM_RECT_MAX) * NUM_BUFFER_MAX) * NUM_LAYERS_MAX
Mathias Agopiane9e4d542010-04-15 18:48:26 -070067// 4 * (11 + 7 + (1 + 2*7)*16) * 31
68// 1032 * 31
69// = ~27 KiB (31992)
Mathias Agopiancdaaf322010-04-05 16:21:53 -070070
Mathias Agopian81e2a522009-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 Agopiandd9a3a72010-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 Agopian082a4d82010-05-21 14:19:50 -070083 static const unsigned int NUM_BUFFER_MIN = 2;
Mathias Agopiandd9a3a72010-05-18 17:06:55 -070084 static const unsigned int NUM_DISPLAY_MAX = 4;
85
Mathias Agopian9e3ebf82009-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 Agopiane9e4d542010-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 Agopian9e3ebf82009-09-17 01:35:28 -0700106
Mathias Agopian81e2a522009-09-07 16:32:45 -0700107 SharedBufferStack();
Mathias Agopian4fc61bf2009-09-10 19:41:18 -0700108 void init(int32_t identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700109 status_t setDirtyRegion(int buffer, const Region& reg);
Mathias Agopiane9e4d542010-04-15 18:48:26 -0700110 status_t setCrop(int buffer, const Rect& reg);
Mathias Agopian81e2a522009-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 Agopian3dbf98c2009-09-10 16:55:13 -0700118 volatile status_t status; // surface's status code
Mathias Agopian81e2a522009-09-07 16:32:45 -0700119
120 // not part of the conditions
121 volatile int32_t reallocMask;
Mathias Agopian86f69c12010-04-27 21:08:20 -0700122 volatile int8_t index[NUM_BUFFER_MAX];
Mathias Agopian81e2a522009-09-07 16:32:45 -0700123
124 int32_t identity; // surface's identity (const)
Mathias Agopian86f69c12010-04-27 21:08:20 -0700125 int32_t reserved32[2];
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700126 Statistics stats;
Mathias Agopiancdaaf322010-04-05 16:21:53 -0700127 int32_t reserved;
Mathias Agopiane9e4d542010-04-15 18:48:26 -0700128 BufferData buffers[NUM_BUFFER_MAX]; // 960 bytes
Mathias Agopian81e2a522009-09-07 16:32:45 -0700129};
130
131// ----------------------------------------------------------------------------
132
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700133// 32 KB max
Mathias Agopian81e2a522009-09-07 16:32:45 -0700134class SharedClient
135{
136public:
137 SharedClient();
138 ~SharedClient();
Mathias Agopian81e2a522009-09-07 16:32:45 -0700139 status_t validate(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;
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700149 SharedBufferStack surfaces[ SharedBufferStack::NUM_LAYERS_MAX ];
Mathias Agopian81e2a522009-09-07 16:32:45 -0700150};
151
152// ============================================================================
153
154class SharedBufferBase
155{
156public:
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700157 SharedBufferBase(SharedClient* sharedClient, int surface,
Mathias Agopiand46758b2009-10-06 19:00:57 -0700158 int32_t identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700159 ~SharedBufferBase();
Mathias Agopiandefd1bd2009-10-02 18:12:30 -0700160 status_t getStatus() const;
Mathias Agopianba0fab32010-05-28 14:22:23 -0700161 int32_t getIdentity() 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;
Mathias Agopiand46758b2009-10-06 19:00:57 -0700168 const int mIdentity;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700169
170 friend struct Update;
171 friend struct QueueUpdate;
172
173 struct ConditionBase {
174 SharedBufferStack& stack;
175 inline ConditionBase(SharedBufferBase* sbc)
176 : stack(*sbc->mSharedStack) { }
Mathias Agopian41623bf2010-04-27 16:41:19 -0700177 virtual ~ConditionBase() { };
178 virtual bool operator()() const = 0;
179 virtual const char* name() const = 0;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700180 };
Mathias Agopian41623bf2010-04-27 16:41:19 -0700181 status_t waitForCondition(const ConditionBase& condition);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700182
183 struct UpdateBase {
184 SharedBufferStack& stack;
185 inline UpdateBase(SharedBufferBase* sbb)
186 : stack(*sbb->mSharedStack) { }
187 };
Mathias Agopian81e2a522009-09-07 16:32:45 -0700188 template <typename T>
189 status_t updateCondition(T update);
190};
191
192template <typename T>
Mathias Agopian81e2a522009-09-07 16:32:45 -0700193status_t SharedBufferBase::updateCondition(T update) {
194 SharedClient& client( *mSharedClient );
195 Mutex::Autolock _l(client.lock);
196 ssize_t result = update();
197 client.cv.broadcast();
198 return result;
199}
200
201// ----------------------------------------------------------------------------
202
203class SharedBufferClient : public SharedBufferBase
204{
205public:
Mathias Agopiand46758b2009-10-06 19:00:57 -0700206 SharedBufferClient(SharedClient* sharedClient, int surface, int num,
207 int32_t identity);
208
Mathias Agopian81e2a522009-09-07 16:32:45 -0700209 ssize_t dequeue();
210 status_t undoDequeue(int buf);
211
212 status_t lock(int buf);
213 status_t queue(int buf);
214 bool needNewBuffer(int buffer) const;
215 status_t setDirtyRegion(int buffer, const Region& reg);
Mathias Agopiane9e4d542010-04-15 18:48:26 -0700216 status_t setCrop(int buffer, const Rect& reg);
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700217
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700218
219 class SetBufferCountCallback {
220 friend class SharedBufferClient;
221 virtual status_t operator()(int bufferCount) const = 0;
222 protected:
223 virtual ~SetBufferCountCallback() { }
224 };
225 status_t setBufferCount(int bufferCount, const SetBufferCountCallback& ipc);
226
Mathias Agopian81e2a522009-09-07 16:32:45 -0700227private:
228 friend struct Condition;
229 friend struct DequeueCondition;
230 friend struct LockCondition;
231
232 struct QueueUpdate : public UpdateBase {
233 inline QueueUpdate(SharedBufferBase* sbb);
234 inline ssize_t operator()();
235 };
236
237 struct UndoDequeueUpdate : public UpdateBase {
238 inline UndoDequeueUpdate(SharedBufferBase* sbb);
239 inline ssize_t operator()();
240 };
241
242 // --
243
244 struct DequeueCondition : public ConditionBase {
245 inline DequeueCondition(SharedBufferClient* sbc);
Mathias Agopian41623bf2010-04-27 16:41:19 -0700246 inline bool operator()() const;
247 inline const char* name() const { return "DequeueCondition"; }
Mathias Agopian81e2a522009-09-07 16:32:45 -0700248 };
249
250 struct LockCondition : public ConditionBase {
251 int buf;
252 inline LockCondition(SharedBufferClient* sbc, int buf);
Mathias Agopian41623bf2010-04-27 16:41:19 -0700253 inline bool operator()() const;
254 inline const char* name() const { return "LockCondition"; }
Mathias Agopian81e2a522009-09-07 16:32:45 -0700255 };
256
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700257 int32_t computeTail() const;
258
259 mutable RWLock mLock;
260 int mNumBuffers;
261
Mathias Agopian81e2a522009-09-07 16:32:45 -0700262 int32_t tail;
Mathias Agopian43d8a282010-04-27 16:11:38 -0700263 int32_t undoDequeueTail;
Mathias Agopian86f69c12010-04-27 21:08:20 -0700264 int32_t queued_head;
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700265 // statistics...
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700266 nsecs_t mDequeueTime[SharedBufferStack::NUM_BUFFER_MAX];
Mathias Agopian81e2a522009-09-07 16:32:45 -0700267};
268
269// ----------------------------------------------------------------------------
270
271class SharedBufferServer : public SharedBufferBase
272{
273public:
Mathias Agopian4fc61bf2009-09-10 19:41:18 -0700274 SharedBufferServer(SharedClient* sharedClient, int surface, int num,
275 int32_t identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700276
277 ssize_t retireAndLock();
278 status_t unlock(int buffer);
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700279 void setStatus(status_t status);
Mathias Agopianfb6ae662010-05-21 17:24:35 -0700280 status_t reallocateAll();
281 status_t reallocateAllExcept(int buffer);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700282 status_t assertReallocate(int buffer);
Mathias Agopian68174b12009-10-07 16:44:10 -0700283 int32_t getQueuedCount() const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700284 Region getDirtyRegion(int buffer) const;
285
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700286 status_t resize(int newNumBuffers);
287
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700288 SharedBufferStack::Statistics getStats() const;
289
290
Mathias Agopian81e2a522009-09-07 16:32:45 -0700291private:
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700292 /*
293 * BufferList is basically a fixed-capacity sorted-vector of
294 * unsigned 5-bits ints using a 32-bits int as storage.
295 * it has efficient iterators to find items in the list and not in the list.
296 */
297 class BufferList {
298 size_t mCapacity;
299 uint32_t mList;
300 public:
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700301 BufferList(size_t c = SharedBufferStack::NUM_BUFFER_MAX)
302 : mCapacity(c), mList(0) { }
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700303 status_t add(int value);
304 status_t remove(int value);
Mathias Agopian68f929b2010-05-21 14:51:33 -0700305 uint32_t getMask() const { return mList; }
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700306
307 class const_iterator {
308 friend class BufferList;
309 uint32_t mask, curr;
310 const_iterator(uint32_t mask) :
Mathias Agopiane1f61052010-05-17 17:27:26 -0700311 mask(mask), curr(__builtin_clz(mask)) {
312 }
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700313 public:
314 inline bool operator == (const const_iterator& rhs) const {
315 return mask == rhs.mask;
316 }
317 inline bool operator != (const const_iterator& rhs) const {
318 return mask != rhs.mask;
319 }
320 inline int operator *() const { return curr; }
Mathias Agopiane1f61052010-05-17 17:27:26 -0700321 inline const const_iterator& operator ++() {
322 mask &= ~(1<<(31-curr));
323 curr = __builtin_clz(mask);
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700324 return *this;
325 }
326 };
327
328 inline const_iterator begin() const {
329 return const_iterator(mList);
330 }
331 inline const_iterator end() const {
332 return const_iterator(0);
333 }
334 inline const_iterator free_begin() const {
335 uint32_t mask = (1 << (32-mCapacity)) - 1;
336 return const_iterator( ~(mList | mask) );
337 }
338 };
339
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700340 // this protects mNumBuffers and mBufferList
341 mutable RWLock mLock;
342 int mNumBuffers;
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700343 BufferList mBufferList;
344
Mathias Agopian81e2a522009-09-07 16:32:45 -0700345 struct UnlockUpdate : public UpdateBase {
346 const int lockedBuffer;
347 inline UnlockUpdate(SharedBufferBase* sbb, int lockedBuffer);
348 inline ssize_t operator()();
349 };
350
351 struct RetireUpdate : public UpdateBase {
352 const int numBuffers;
353 inline RetireUpdate(SharedBufferBase* sbb, int numBuffers);
354 inline ssize_t operator()();
355 };
356
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700357 struct StatusUpdate : public UpdateBase {
358 const status_t status;
359 inline StatusUpdate(SharedBufferBase* sbb, status_t status);
360 inline ssize_t operator()();
361 };
362
Mathias Agopian81e2a522009-09-07 16:32:45 -0700363 struct ReallocateCondition : public ConditionBase {
364 int buf;
365 inline ReallocateCondition(SharedBufferBase* sbb, int buf);
Mathias Agopian41623bf2010-04-27 16:41:19 -0700366 inline bool operator()() const;
367 inline const char* name() const { return "ReallocateCondition"; }
Mathias Agopian81e2a522009-09-07 16:32:45 -0700368 };
369};
370
371// ===========================================================================
372
373struct display_cblk_t
374{
375 uint16_t w;
376 uint16_t h;
377 uint8_t format;
378 uint8_t orientation;
379 uint8_t reserved[2];
380 float fps;
381 float density;
382 float xdpi;
383 float ydpi;
384 uint32_t pad[2];
385};
386
387struct surface_flinger_cblk_t // 4KB max
388{
389 uint8_t connected;
390 uint8_t reserved[3];
391 uint32_t pad[7];
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700392 display_cblk_t displays[SharedBufferStack::NUM_DISPLAY_MAX];
Mathias Agopian81e2a522009-09-07 16:32:45 -0700393};
394
395// ---------------------------------------------------------------------------
396
Mathias Agopiancdaaf322010-04-05 16:21:53 -0700397COMPILE_TIME_ASSERT(sizeof(SharedClient) <= 32768)
Mathias Agopian81e2a522009-09-07 16:32:45 -0700398COMPILE_TIME_ASSERT(sizeof(surface_flinger_cblk_t) <= 4096)
399
400// ---------------------------------------------------------------------------
401}; // namespace android
402
Mathias Agopian3cf61352010-02-09 17:46:37 -0800403#endif /* ANDROID_SF_SHARED_BUFFER_STACK_H */