blob: 633b5436d6b9465c96781316698dd44ac34759df [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 Agopiancdaaf322010-04-05 16:21:53 -070057// 4 * (11 + 7 + (1 + 2*NUM_RECT_MAX) * NUM_BUFFER_MAX) * NUM_LAYERS_MAX
Mathias Agopiane9e4d542010-04-15 18:48:26 -070058// 4 * (11 + 7 + (1 + 2*7)*16) * 31
59// 1032 * 31
60// = ~27 KiB (31992)
Mathias Agopiancdaaf322010-04-05 16:21:53 -070061
Mathias Agopian81e2a522009-09-07 16:32:45 -070062class SharedBufferStack
63{
64 friend class SharedClient;
65 friend class SharedBufferBase;
66 friend class SharedBufferClient;
67 friend class SharedBufferServer;
68
69public:
Mathias Agopiandd9a3a72010-05-18 17:06:55 -070070 // When changing these values, the COMPILE_TIME_ASSERT at the end of this
71 // file need to be updated.
72 static const unsigned int NUM_LAYERS_MAX = 31;
73 static const unsigned int NUM_BUFFER_MAX = 16;
Mathias Agopian082a4d82010-05-21 14:19:50 -070074 static const unsigned int NUM_BUFFER_MIN = 2;
Mathias Agopiandd9a3a72010-05-18 17:06:55 -070075 static const unsigned int NUM_DISPLAY_MAX = 4;
76
Mathias Agopian9e3ebf82009-09-17 01:35:28 -070077 struct Statistics { // 4 longs
78 typedef int32_t usecs_t;
79 usecs_t totalTime;
80 usecs_t reserved[3];
81 };
Mathias Agopiane9e4d542010-04-15 18:48:26 -070082
83 struct SmallRect {
84 uint16_t l, t, r, b;
85 };
86
87 struct FlatRegion { // 52 bytes = 4 * (1 + 2*N)
88 static const unsigned int NUM_RECT_MAX = 6;
89 uint32_t count;
90 SmallRect rects[NUM_RECT_MAX];
91 };
92
93 struct BufferData {
94 FlatRegion dirtyRegion;
95 SmallRect crop;
96 };
Mathias Agopian9e3ebf82009-09-17 01:35:28 -070097
Mathias Agopian81e2a522009-09-07 16:32:45 -070098 SharedBufferStack();
Mathias Agopian4fc61bf2009-09-10 19:41:18 -070099 void init(int32_t identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700100 status_t setDirtyRegion(int buffer, const Region& reg);
Mathias Agopiane9e4d542010-04-15 18:48:26 -0700101 status_t setCrop(int buffer, const Rect& reg);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700102 Region getDirtyRegion(int buffer) const;
103
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)
117 int32_t reserved32[1];
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700118 Statistics stats;
Mathias Agopiancdaaf322010-04-05 16:21:53 -0700119 int32_t reserved;
Mathias Agopiane9e4d542010-04-15 18:48:26 -0700120 BufferData buffers[NUM_BUFFER_MAX]; // 960 bytes
Mathias Agopian81e2a522009-09-07 16:32:45 -0700121};
122
123// ----------------------------------------------------------------------------
124
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700125// 32 KB max
Mathias Agopian81e2a522009-09-07 16:32:45 -0700126class SharedClient
127{
128public:
129 SharedClient();
130 ~SharedClient();
Mathias Agopian81e2a522009-09-07 16:32:45 -0700131 status_t validate(size_t token) const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700132
133private:
134 friend class SharedBufferBase;
135 friend class SharedBufferClient;
136 friend class SharedBufferServer;
137
138 // FIXME: this should be replaced by a lock-less primitive
139 Mutex lock;
140 Condition cv;
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700141 SharedBufferStack surfaces[ SharedBufferStack::NUM_LAYERS_MAX ];
Mathias Agopian81e2a522009-09-07 16:32:45 -0700142};
143
144// ============================================================================
145
146class SharedBufferBase
147{
148public:
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700149 SharedBufferBase(SharedClient* sharedClient, int surface,
Mathias Agopiand46758b2009-10-06 19:00:57 -0700150 int32_t identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700151 ~SharedBufferBase();
Mathias Agopiandefd1bd2009-10-02 18:12:30 -0700152 status_t getStatus() const;
Mathias Agopianba0fab32010-05-28 14:22:23 -0700153 int32_t getIdentity() const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700154 size_t getFrontBuffer() const;
155 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);
205 status_t queue(int buf);
206 bool needNewBuffer(int buffer) const;
207 status_t setDirtyRegion(int buffer, const Region& reg);
Mathias Agopiane9e4d542010-04-15 18:48:26 -0700208 status_t setCrop(int buffer, const Rect& reg);
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700209
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700210
211 class SetBufferCountCallback {
212 friend class SharedBufferClient;
213 virtual status_t operator()(int bufferCount) const = 0;
214 protected:
215 virtual ~SetBufferCountCallback() { }
216 };
217 status_t setBufferCount(int bufferCount, const SetBufferCountCallback& ipc);
218
Mathias Agopian81e2a522009-09-07 16:32:45 -0700219private:
220 friend struct Condition;
221 friend struct DequeueCondition;
222 friend struct LockCondition;
223
224 struct QueueUpdate : public UpdateBase {
225 inline QueueUpdate(SharedBufferBase* sbb);
226 inline ssize_t operator()();
227 };
228
229 struct UndoDequeueUpdate : public UpdateBase {
230 inline UndoDequeueUpdate(SharedBufferBase* sbb);
231 inline ssize_t operator()();
232 };
233
234 // --
235
236 struct DequeueCondition : public ConditionBase {
237 inline DequeueCondition(SharedBufferClient* sbc);
Mathias Agopian41623bf2010-04-27 16:41:19 -0700238 inline bool operator()() const;
239 inline const char* name() const { return "DequeueCondition"; }
Mathias Agopian81e2a522009-09-07 16:32:45 -0700240 };
241
242 struct LockCondition : public ConditionBase {
243 int buf;
244 inline LockCondition(SharedBufferClient* sbc, int buf);
Mathias Agopian41623bf2010-04-27 16:41:19 -0700245 inline bool operator()() const;
246 inline const char* name() const { return "LockCondition"; }
Mathias Agopian81e2a522009-09-07 16:32:45 -0700247 };
248
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700249 int32_t computeTail() const;
250
251 mutable RWLock mLock;
252 int mNumBuffers;
253
Mathias Agopian81e2a522009-09-07 16:32:45 -0700254 int32_t tail;
Mathias Agopian43d8a282010-04-27 16:11:38 -0700255 int32_t undoDequeueTail;
Mathias Agopian86f69c12010-04-27 21:08:20 -0700256 int32_t queued_head;
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700257 // statistics...
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700258 nsecs_t mDequeueTime[SharedBufferStack::NUM_BUFFER_MAX];
Mathias Agopian81e2a522009-09-07 16:32:45 -0700259};
260
261// ----------------------------------------------------------------------------
262
Mathias Agopian36ef8cf2010-06-08 19:54:15 -0700263class SharedBufferServer
264 : public SharedBufferBase,
265 public LightRefBase<SharedBufferServer>
Mathias Agopian81e2a522009-09-07 16:32:45 -0700266{
267public:
Mathias Agopian4fc61bf2009-09-10 19:41:18 -0700268 SharedBufferServer(SharedClient* sharedClient, int surface, int num,
269 int32_t identity);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700270
271 ssize_t retireAndLock();
272 status_t unlock(int buffer);
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700273 void setStatus(status_t status);
Mathias Agopianfb6ae662010-05-21 17:24:35 -0700274 status_t reallocateAll();
275 status_t reallocateAllExcept(int buffer);
Mathias Agopian81e2a522009-09-07 16:32:45 -0700276 status_t assertReallocate(int buffer);
Mathias Agopian68174b12009-10-07 16:44:10 -0700277 int32_t getQueuedCount() const;
Mathias Agopian81e2a522009-09-07 16:32:45 -0700278 Region getDirtyRegion(int buffer) const;
279
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700280 status_t resize(int newNumBuffers);
281
Mathias Agopian9e3ebf82009-09-17 01:35:28 -0700282 SharedBufferStack::Statistics getStats() const;
283
284
Mathias Agopian81e2a522009-09-07 16:32:45 -0700285private:
Mathias Agopian36ef8cf2010-06-08 19:54:15 -0700286 friend class LightRefBase<SharedBufferServer>;
287 ~SharedBufferServer();
288
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700289 /*
290 * BufferList is basically a fixed-capacity sorted-vector of
291 * unsigned 5-bits ints using a 32-bits int as storage.
292 * it has efficient iterators to find items in the list and not in the list.
293 */
294 class BufferList {
295 size_t mCapacity;
296 uint32_t mList;
297 public:
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700298 BufferList(size_t c = SharedBufferStack::NUM_BUFFER_MAX)
299 : mCapacity(c), mList(0) { }
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700300 status_t add(int value);
301 status_t remove(int value);
Mathias Agopian68f929b2010-05-21 14:51:33 -0700302 uint32_t getMask() const { return mList; }
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700303
304 class const_iterator {
305 friend class BufferList;
306 uint32_t mask, curr;
307 const_iterator(uint32_t mask) :
Mathias Agopiane1f61052010-05-17 17:27:26 -0700308 mask(mask), curr(__builtin_clz(mask)) {
309 }
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700310 public:
311 inline bool operator == (const const_iterator& rhs) const {
312 return mask == rhs.mask;
313 }
314 inline bool operator != (const const_iterator& rhs) const {
315 return mask != rhs.mask;
316 }
317 inline int operator *() const { return curr; }
Mathias Agopiane1f61052010-05-17 17:27:26 -0700318 inline const const_iterator& operator ++() {
319 mask &= ~(1<<(31-curr));
320 curr = __builtin_clz(mask);
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700321 return *this;
322 }
323 };
324
325 inline const_iterator begin() const {
326 return const_iterator(mList);
327 }
328 inline const_iterator end() const {
329 return const_iterator(0);
330 }
331 inline const_iterator free_begin() const {
332 uint32_t mask = (1 << (32-mCapacity)) - 1;
333 return const_iterator( ~(mList | mask) );
334 }
335 };
336
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700337 // this protects mNumBuffers and mBufferList
338 mutable RWLock mLock;
339 int mNumBuffers;
Mathias Agopian5cc61b12010-05-07 15:58:44 -0700340 BufferList mBufferList;
341
Mathias Agopian81e2a522009-09-07 16:32:45 -0700342 struct UnlockUpdate : public UpdateBase {
343 const int lockedBuffer;
344 inline UnlockUpdate(SharedBufferBase* sbb, int lockedBuffer);
345 inline ssize_t operator()();
346 };
347
348 struct RetireUpdate : public UpdateBase {
349 const int numBuffers;
350 inline RetireUpdate(SharedBufferBase* sbb, int numBuffers);
351 inline ssize_t operator()();
352 };
353
Mathias Agopian3dbf98c2009-09-10 16:55:13 -0700354 struct StatusUpdate : public UpdateBase {
355 const status_t status;
356 inline StatusUpdate(SharedBufferBase* sbb, status_t status);
357 inline ssize_t operator()();
358 };
359
Mathias Agopian81e2a522009-09-07 16:32:45 -0700360 struct ReallocateCondition : public ConditionBase {
361 int buf;
362 inline ReallocateCondition(SharedBufferBase* sbb, int buf);
Mathias Agopian41623bf2010-04-27 16:41:19 -0700363 inline bool operator()() const;
364 inline const char* name() const { return "ReallocateCondition"; }
Mathias Agopian81e2a522009-09-07 16:32:45 -0700365 };
366};
367
368// ===========================================================================
369
370struct display_cblk_t
371{
372 uint16_t w;
373 uint16_t h;
374 uint8_t format;
375 uint8_t orientation;
376 uint8_t reserved[2];
377 float fps;
378 float density;
379 float xdpi;
380 float ydpi;
381 uint32_t pad[2];
382};
383
384struct surface_flinger_cblk_t // 4KB max
385{
386 uint8_t connected;
387 uint8_t reserved[3];
388 uint32_t pad[7];
Mathias Agopiandd9a3a72010-05-18 17:06:55 -0700389 display_cblk_t displays[SharedBufferStack::NUM_DISPLAY_MAX];
Mathias Agopian81e2a522009-09-07 16:32:45 -0700390};
391
392// ---------------------------------------------------------------------------
393
Mathias Agopiancdaaf322010-04-05 16:21:53 -0700394COMPILE_TIME_ASSERT(sizeof(SharedClient) <= 32768)
Mathias Agopian81e2a522009-09-07 16:32:45 -0700395COMPILE_TIME_ASSERT(sizeof(surface_flinger_cblk_t) <= 4096)
396
397// ---------------------------------------------------------------------------
398}; // namespace android
399
Mathias Agopian3cf61352010-02-09 17:46:37 -0800400#endif /* ANDROID_SF_SHARED_BUFFER_STACK_H */