blob: 9d589cf9988a0c23327429c35ddc6ef28e400389 [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.
Mathias Agopian81e2a522009-09-07 16:32:45 -070046 *
47 */
48
Mathias Agopian81e2a522009-09-07 16:32:45 -070049// ----------------------------------------------------------------------------
50
51class Region;
52class SharedBufferStack;
53class SharedClient;
54
55// ----------------------------------------------------------------------------
56
Mathias Agopian81e2a522009-09-07 16:32:45 -070057class SharedBufferStack
58{
59 friend class SharedClient;
60 friend class SharedBufferBase;
61 friend class SharedBufferClient;
62 friend class SharedBufferServer;
63
64public:
Mathias Agopiandd9a3a72010-05-18 17:06:55 -070065 // When changing these values, the COMPILE_TIME_ASSERT at the end of this
66 // file need to be updated.
67 static const unsigned int NUM_LAYERS_MAX = 31;
68 static const unsigned int NUM_BUFFER_MAX = 16;
Mathias Agopian082a4d82010-05-21 14:19:50 -070069 static const unsigned int NUM_BUFFER_MIN = 2;
Mathias Agopiandd9a3a72010-05-18 17:06:55 -070070 static const unsigned int NUM_DISPLAY_MAX = 4;
71
Mathias Agopian9e3ebf82009-09-17 01:35:28 -070072 struct Statistics { // 4 longs
73 typedef int32_t usecs_t;
74 usecs_t totalTime;
75 usecs_t reserved[3];
76 };
Mathias Agopiane9e4d542010-04-15 18:48:26 -070077
78 struct SmallRect {
79 uint16_t l, t, r, b;
80 };
81
82 struct FlatRegion { // 52 bytes = 4 * (1 + 2*N)
Mathias Agopian92235642010-08-19 17:01:19 -070083 static const unsigned int NUM_RECT_MAX = 5;
Mathias Agopiane9e4d542010-04-15 18:48:26 -070084 uint32_t count;
85 SmallRect rects[NUM_RECT_MAX];
86 };
87
88 struct BufferData {
89 FlatRegion dirtyRegion;
90 SmallRect crop;
Mathias Agopian92235642010-08-19 17:01:19 -070091 uint8_t transform;
92 uint8_t reserved[3];
Mathias Agopiane9e4d542010-04-15 18:48:26 -070093 };
Mathias Agopian9e3ebf82009-09-17 01:35:28 -070094
Mathias Agopian81e2a522009-09-07 16:32:45 -070095 SharedBufferStack();
Mathias Agopian4fc61bf2009-09-10 19:41:18 -070096 void init(int32_t identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -070097 status_t setDirtyRegion(int buffer, const Region& reg);
Mathias Agopiane9e4d542010-04-15 18:48:26 -070098 status_t setCrop(int buffer, const Rect& reg);
Mathias Agopian92235642010-08-19 17:01:19 -070099 status_t setTransform(int buffer, uint8_t transform);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700100 Region getDirtyRegion(int buffer) const;
Mathias Agopian92235642010-08-19 17:01:19 -0700101 Rect getCrop(int buffer) const;
102 uint32_t getTransform(int buffer) const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700103
104 // these attributes are part of the conditions/updates
105 volatile int32_t head; // server's current front buffer
106 volatile int32_t available; // number of dequeue-able buffers
107 volatile int32_t queued; // number of buffers waiting for post
108 volatile int32_t inUse; // buffer currently in use by SF
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700109 volatile status_t status; // surface's status code
Mathias Agopian81e2a522009-09-07 16:32:45 -0700110
111 // not part of the conditions
112 volatile int32_t reallocMask;
Mathias Agopian86f69c12010-04-27 21:08:20 -0700113 volatile int8_t index[NUM_BUFFER_MAX];
Mathias Agopian81e2a522009-09-07 16:32:45 -0700114
115 int32_t identity; // surface's identity (const)
Mathias Agopiancbbf27f2010-06-01 15:12:58 -0700116 int32_t token; // surface's token (for debugging)
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700117 Statistics stats;
Mathias Agopian50817932010-10-01 16:22:41 -0700118 int8_t headBuf; // last retired buffer
119 uint8_t reservedBytes[3];
Mathias Agopiancdaaf322010-04-05 16:21:53 -0700120 int32_t reserved;
Mathias Agopian92235642010-08-19 17:01:19 -0700121 BufferData buffers[NUM_BUFFER_MAX]; // 1024 bytes
Mathias Agopian81e2a522009-09-07 16:32:45 -0700122};
123
124// ----------------------------------------------------------------------------
125
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700126// 32 KB max
Mathias Agopian81e2a522009-09-07 16:32:45 -0700127class SharedClient
128{
129public:
130 SharedClient();
131 ~SharedClient();
Mathias Agopian81e2a522009-09-07 16:32:45 -0700132 status_t validate(size_t token) const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700133
134private:
135 friend class SharedBufferBase;
136 friend class SharedBufferClient;
137 friend class SharedBufferServer;
138
139 // FIXME: this should be replaced by a lock-less primitive
140 Mutex lock;
141 Condition cv;
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700142 SharedBufferStack surfaces[ SharedBufferStack::NUM_LAYERS_MAX ];
Mathias Agopian81e2a522009-09-07 16:32:45 -0700143};
144
145// ============================================================================
146
147class SharedBufferBase
148{
149public:
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700150 SharedBufferBase(SharedClient* sharedClient, int surface,
Mathias Agopiand46758b2009-10-06 19:00:57 -0700151 int32_t identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700152 ~SharedBufferBase();
Mathias Agopiandefd1bd2009-10-02 18:12:30 -0700153 status_t getStatus() const;
Mathias Agopianba0fab32010-05-28 14:22:23 -0700154 int32_t getIdentity() const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700155 String8 dump(char const* prefix) const;
156
157protected:
158 SharedClient* const mSharedClient;
159 SharedBufferStack* const mSharedStack;
Mathias Agopiand46758b2009-10-06 19:00:57 -0700160 const int mIdentity;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700161
162 friend struct Update;
163 friend struct QueueUpdate;
164
165 struct ConditionBase {
166 SharedBufferStack& stack;
167 inline ConditionBase(SharedBufferBase* sbc)
168 : stack(*sbc->mSharedStack) { }
Mathias Agopian41623bf2010-04-27 16:41:19 -0700169 virtual ~ConditionBase() { };
170 virtual bool operator()() const = 0;
171 virtual const char* name() const = 0;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700172 };
Mathias Agopian41623bf2010-04-27 16:41:19 -0700173 status_t waitForCondition(const ConditionBase& condition);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700174
175 struct UpdateBase {
176 SharedBufferStack& stack;
177 inline UpdateBase(SharedBufferBase* sbb)
178 : stack(*sbb->mSharedStack) { }
179 };
Mathias Agopian81e2a522009-09-07 16:32:45 -0700180 template <typename T>
181 status_t updateCondition(T update);
182};
183
184template <typename T>
Mathias Agopian81e2a522009-09-07 16:32:45 -0700185status_t SharedBufferBase::updateCondition(T update) {
186 SharedClient& client( *mSharedClient );
187 Mutex::Autolock _l(client.lock);
188 ssize_t result = update();
189 client.cv.broadcast();
190 return result;
191}
192
193// ----------------------------------------------------------------------------
194
195class SharedBufferClient : public SharedBufferBase
196{
197public:
Mathias Agopiand46758b2009-10-06 19:00:57 -0700198 SharedBufferClient(SharedClient* sharedClient, int surface, int num,
199 int32_t identity);
200
Mathias Agopian81e2a522009-09-07 16:32:45 -0700201 ssize_t dequeue();
202 status_t undoDequeue(int buf);
203
204 status_t lock(int buf);
Mathias Agopian50817932010-10-01 16:22:41 -0700205 status_t cancel(int buf);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700206 status_t queue(int buf);
207 bool needNewBuffer(int buffer) const;
208 status_t setDirtyRegion(int buffer, const Region& reg);
Mathias Agopiane9e4d542010-04-15 18:48:26 -0700209 status_t setCrop(int buffer, const Rect& reg);
Mathias Agopian92235642010-08-19 17:01:19 -0700210 status_t setTransform(int buffer, uint32_t transform);
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700211
212 class SetBufferCountCallback {
213 friend class SharedBufferClient;
214 virtual status_t operator()(int bufferCount) const = 0;
215 protected:
216 virtual ~SetBufferCountCallback() { }
217 };
218 status_t setBufferCount(int bufferCount, const SetBufferCountCallback& ipc);
219
Mathias Agopian81e2a522009-09-07 16:32:45 -0700220private:
221 friend struct Condition;
222 friend struct DequeueCondition;
223 friend struct LockCondition;
224
225 struct QueueUpdate : public UpdateBase {
226 inline QueueUpdate(SharedBufferBase* sbb);
227 inline ssize_t operator()();
228 };
229
Mathias Agopiand7c43d32010-08-26 17:42:27 -0700230 struct DequeueUpdate : public UpdateBase {
231 inline DequeueUpdate(SharedBufferBase* sbb);
232 inline ssize_t operator()();
233 };
234
Mathias Agopian50817932010-10-01 16:22:41 -0700235 struct CancelUpdate : public UpdateBase {
236 int tail, buf;
237 inline CancelUpdate(SharedBufferBase* sbb, int tail, int buf);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700238 inline ssize_t operator()();
239 };
240
241 // --
242
243 struct DequeueCondition : public ConditionBase {
244 inline DequeueCondition(SharedBufferClient* sbc);
Mathias Agopian41623bf2010-04-27 16:41:19 -0700245 inline bool operator()() const;
246 inline const char* name() const { return "DequeueCondition"; }
Mathias Agopian81e2a522009-09-07 16:32:45 -0700247 };
248
249 struct LockCondition : public ConditionBase {
250 int buf;
251 inline LockCondition(SharedBufferClient* sbc, int buf);
Mathias Agopian41623bf2010-04-27 16:41:19 -0700252 inline bool operator()() const;
253 inline const char* name() const { return "LockCondition"; }
Mathias Agopian81e2a522009-09-07 16:32:45 -0700254 };
255
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700256 int32_t computeTail() const;
257
258 mutable RWLock mLock;
259 int mNumBuffers;
260
Mathias Agopian81e2a522009-09-07 16:32:45 -0700261 int32_t tail;
Mathias Agopian86f69c12010-04-27 21:08:20 -0700262 int32_t queued_head;
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700263 // statistics...
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700264 nsecs_t mDequeueTime[SharedBufferStack::NUM_BUFFER_MAX];
Mathias Agopian81e2a522009-09-07 16:32:45 -0700265};
266
267// ----------------------------------------------------------------------------
268
Mathias Agopian36ef8cf2010-06-08 19:54:15 -0700269class SharedBufferServer
270 : public SharedBufferBase,
271 public LightRefBase<SharedBufferServer>
Mathias Agopian81e2a522009-09-07 16:32:45 -0700272{
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 Agopian68174b12009-10-07 16:44:10 -0700282 int32_t getQueuedCount() const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700283 Region getDirtyRegion(int buffer) const;
Mathias Agopian92235642010-08-19 17:01:19 -0700284 Rect getCrop(int buffer) const;
285 uint32_t getTransform(int buffer) const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700286
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700287 status_t resize(int newNumBuffers);
Jamie Genniseffae7f2010-11-02 11:51:32 -0700288 status_t grow(int newNumBuffers);
289 status_t shrink(int newNumBuffers);
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700290
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700291 SharedBufferStack::Statistics getStats() const;
292
293
Mathias Agopian81e2a522009-09-07 16:32:45 -0700294private:
Mathias Agopian36ef8cf2010-06-08 19:54:15 -0700295 friend class LightRefBase<SharedBufferServer>;
296 ~SharedBufferServer();
297
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700298 /*
299 * BufferList is basically a fixed-capacity sorted-vector of
300 * unsigned 5-bits ints using a 32-bits int as storage.
301 * it has efficient iterators to find items in the list and not in the list.
302 */
303 class BufferList {
304 size_t mCapacity;
305 uint32_t mList;
306 public:
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700307 BufferList(size_t c = SharedBufferStack::NUM_BUFFER_MAX)
308 : mCapacity(c), mList(0) { }
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700309 status_t add(int value);
310 status_t remove(int value);
Mathias Agopian68f929b2010-05-21 14:51:33 -0700311 uint32_t getMask() const { return mList; }
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700312
313 class const_iterator {
314 friend class BufferList;
315 uint32_t mask, curr;
316 const_iterator(uint32_t mask) :
Mathias Agopiane1f61052010-05-17 17:27:26 -0700317 mask(mask), curr(__builtin_clz(mask)) {
318 }
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700319 public:
320 inline bool operator == (const const_iterator& rhs) const {
321 return mask == rhs.mask;
322 }
323 inline bool operator != (const const_iterator& rhs) const {
324 return mask != rhs.mask;
325 }
326 inline int operator *() const { return curr; }
Mathias Agopiane1f61052010-05-17 17:27:26 -0700327 inline const const_iterator& operator ++() {
328 mask &= ~(1<<(31-curr));
329 curr = __builtin_clz(mask);
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700330 return *this;
331 }
332 };
333
334 inline const_iterator begin() const {
335 return const_iterator(mList);
336 }
337 inline const_iterator end() const {
338 return const_iterator(0);
339 }
340 inline const_iterator free_begin() const {
341 uint32_t mask = (1 << (32-mCapacity)) - 1;
342 return const_iterator( ~(mList | mask) );
343 }
344 };
345
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700346 // this protects mNumBuffers and mBufferList
347 mutable RWLock mLock;
348 int mNumBuffers;
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700349 BufferList mBufferList;
350
Jamie Genniseffae7f2010-11-02 11:51:32 -0700351 struct BuffersAvailableCondition : public ConditionBase {
352 int mNumBuffers;
353 inline BuffersAvailableCondition(SharedBufferServer* sbs,
354 int numBuffers);
355 inline bool operator()() const;
356 inline const char* name() const { return "BuffersAvailableCondition"; }
357 };
358
Mathias Agopian81e2a522009-09-07 16:32:45 -0700359 struct UnlockUpdate : public UpdateBase {
360 const int lockedBuffer;
361 inline UnlockUpdate(SharedBufferBase* sbb, int lockedBuffer);
362 inline ssize_t operator()();
363 };
364
365 struct RetireUpdate : public UpdateBase {
366 const int numBuffers;
367 inline RetireUpdate(SharedBufferBase* sbb, int numBuffers);
368 inline ssize_t operator()();
369 };
370
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700371 struct StatusUpdate : public UpdateBase {
372 const status_t status;
373 inline StatusUpdate(SharedBufferBase* sbb, status_t status);
374 inline ssize_t operator()();
375 };
Mathias Agopian81e2a522009-09-07 16:32:45 -0700376};
377
378// ===========================================================================
379
380struct display_cblk_t
381{
382 uint16_t w;
383 uint16_t h;
384 uint8_t format;
385 uint8_t orientation;
386 uint8_t reserved[2];
387 float fps;
388 float density;
389 float xdpi;
390 float ydpi;
391 uint32_t pad[2];
392};
393
394struct surface_flinger_cblk_t // 4KB max
395{
396 uint8_t connected;
397 uint8_t reserved[3];
398 uint32_t pad[7];
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700399 display_cblk_t displays[SharedBufferStack::NUM_DISPLAY_MAX];
Mathias Agopian81e2a522009-09-07 16:32:45 -0700400};
401
402// ---------------------------------------------------------------------------
403
Mathias Agopiancdaaf322010-04-05 16:21:53 -0700404COMPILE_TIME_ASSERT(sizeof(SharedClient) <= 32768)
Mathias Agopian81e2a522009-09-07 16:32:45 -0700405COMPILE_TIME_ASSERT(sizeof(surface_flinger_cblk_t) <= 4096)
406
407// ---------------------------------------------------------------------------
408}; // namespace android
409
Mathias Agopian3cf61352010-02-09 17:46:37 -0800410#endif /* ANDROID_SF_SHARED_BUFFER_STACK_H */