Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1 | /* |
| 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 Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 17 | #ifndef ANDROID_SF_SHARED_BUFFER_STACK_H |
| 18 | #define ANDROID_SF_SHARED_BUFFER_STACK_H |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 19 | |
| 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 | |
| 31 | namespace 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. |
| 60 | const unsigned int NUM_LAYERS_MAX = 31; |
Mathias Agopian | cdaaf32 | 2010-04-05 16:21:53 -0700 | [diff] [blame] | 61 | const unsigned int NUM_BUFFER_MAX = 16; |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 62 | const unsigned int NUM_DISPLAY_MAX = 4; |
| 63 | |
| 64 | // ---------------------------------------------------------------------------- |
| 65 | |
| 66 | class Region; |
| 67 | class SharedBufferStack; |
| 68 | class SharedClient; |
| 69 | |
| 70 | // ---------------------------------------------------------------------------- |
| 71 | |
Mathias Agopian | cdaaf32 | 2010-04-05 16:21:53 -0700 | [diff] [blame] | 72 | // 4 * (11 + 7 + (1 + 2*NUM_RECT_MAX) * NUM_BUFFER_MAX) * NUM_LAYERS_MAX |
Mathias Agopian | e9e4d54 | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 73 | // 4 * (11 + 7 + (1 + 2*7)*16) * 31 |
| 74 | // 1032 * 31 |
| 75 | // = ~27 KiB (31992) |
Mathias Agopian | cdaaf32 | 2010-04-05 16:21:53 -0700 | [diff] [blame] | 76 | |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 77 | class SharedBufferStack |
| 78 | { |
| 79 | friend class SharedClient; |
| 80 | friend class SharedBufferBase; |
| 81 | friend class SharedBufferClient; |
| 82 | friend class SharedBufferServer; |
| 83 | |
| 84 | public: |
Mathias Agopian | 9e3ebf8 | 2009-09-17 01:35:28 -0700 | [diff] [blame] | 85 | struct Statistics { // 4 longs |
| 86 | typedef int32_t usecs_t; |
| 87 | usecs_t totalTime; |
| 88 | usecs_t reserved[3]; |
| 89 | }; |
Mathias Agopian | e9e4d54 | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 90 | |
| 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 Agopian | 9e3ebf8 | 2009-09-17 01:35:28 -0700 | [diff] [blame] | 105 | |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 106 | SharedBufferStack(); |
Mathias Agopian | 4fc61bf | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 107 | void init(int32_t identity); |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 108 | status_t setDirtyRegion(int buffer, const Region& reg); |
Mathias Agopian | e9e4d54 | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 109 | status_t setCrop(int buffer, const Rect& reg); |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 110 | 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 Agopian | 3dbf98c | 2009-09-10 16:55:13 -0700 | [diff] [blame] | 117 | volatile status_t status; // surface's status code |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 118 | |
| 119 | // not part of the conditions |
| 120 | volatile int32_t reallocMask; |
| 121 | |
| 122 | int32_t identity; // surface's identity (const) |
Mathias Agopian | cdaaf32 | 2010-04-05 16:21:53 -0700 | [diff] [blame] | 123 | int32_t reserved32[6]; |
Mathias Agopian | 9e3ebf8 | 2009-09-17 01:35:28 -0700 | [diff] [blame] | 124 | Statistics stats; |
Mathias Agopian | cdaaf32 | 2010-04-05 16:21:53 -0700 | [diff] [blame] | 125 | int32_t reserved; |
Mathias Agopian | e9e4d54 | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 126 | BufferData buffers[NUM_BUFFER_MAX]; // 960 bytes |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | // ---------------------------------------------------------------------------- |
| 130 | |
| 131 | // 4 KB max |
| 132 | class SharedClient |
| 133 | { |
| 134 | public: |
| 135 | SharedClient(); |
| 136 | ~SharedClient(); |
| 137 | |
| 138 | status_t validate(size_t token) const; |
| 139 | uint32_t getIdentity(size_t token) const; |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 140 | |
| 141 | private: |
| 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; |
| 149 | SharedBufferStack surfaces[ NUM_LAYERS_MAX ]; |
| 150 | }; |
| 151 | |
| 152 | // ============================================================================ |
| 153 | |
| 154 | class SharedBufferBase |
| 155 | { |
| 156 | public: |
Mathias Agopian | d46758b | 2009-10-06 19:00:57 -0700 | [diff] [blame] | 157 | SharedBufferBase(SharedClient* sharedClient, int surface, int num, |
| 158 | int32_t identity); |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 159 | ~SharedBufferBase(); |
| 160 | uint32_t getIdentity(); |
Mathias Agopian | defd1bd | 2009-10-02 18:12:30 -0700 | [diff] [blame] | 161 | status_t getStatus() const; |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 162 | size_t getFrontBuffer() const; |
| 163 | String8 dump(char const* prefix) const; |
| 164 | |
| 165 | protected: |
| 166 | SharedClient* const mSharedClient; |
| 167 | SharedBufferStack* const mSharedStack; |
| 168 | const int mNumBuffers; |
Mathias Agopian | d46758b | 2009-10-06 19:00:57 -0700 | [diff] [blame] | 169 | const int mIdentity; |
Mathias Agopian | 43d8a28 | 2010-04-27 16:11:38 -0700 | [diff] [blame] | 170 | int32_t computeTail() const; |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 171 | |
| 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 Agopian | 41623bf | 2010-04-27 16:41:19 -0700 | [diff] [blame^] | 179 | virtual ~ConditionBase() { }; |
| 180 | virtual bool operator()() const = 0; |
| 181 | virtual const char* name() const = 0; |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 182 | }; |
Mathias Agopian | 41623bf | 2010-04-27 16:41:19 -0700 | [diff] [blame^] | 183 | status_t waitForCondition(const ConditionBase& condition); |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 184 | |
| 185 | struct UpdateBase { |
| 186 | SharedBufferStack& stack; |
| 187 | inline UpdateBase(SharedBufferBase* sbb) |
| 188 | : stack(*sbb->mSharedStack) { } |
| 189 | }; |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 190 | template <typename T> |
| 191 | status_t updateCondition(T update); |
| 192 | }; |
| 193 | |
| 194 | template <typename T> |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 195 | status_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 | |
| 205 | class SharedBufferClient : public SharedBufferBase |
| 206 | { |
| 207 | public: |
Mathias Agopian | d46758b | 2009-10-06 19:00:57 -0700 | [diff] [blame] | 208 | SharedBufferClient(SharedClient* sharedClient, int surface, int num, |
| 209 | int32_t identity); |
| 210 | |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 211 | 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 Agopian | e9e4d54 | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 218 | status_t setCrop(int buffer, const Rect& reg); |
Mathias Agopian | 9e3ebf8 | 2009-09-17 01:35:28 -0700 | [diff] [blame] | 219 | |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 220 | private: |
| 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 | |
| 230 | struct UndoDequeueUpdate : public UpdateBase { |
| 231 | inline UndoDequeueUpdate(SharedBufferBase* sbb); |
| 232 | inline ssize_t operator()(); |
| 233 | }; |
| 234 | |
| 235 | // -- |
| 236 | |
| 237 | struct DequeueCondition : public ConditionBase { |
| 238 | inline DequeueCondition(SharedBufferClient* sbc); |
Mathias Agopian | 41623bf | 2010-04-27 16:41:19 -0700 | [diff] [blame^] | 239 | inline bool operator()() const; |
| 240 | inline const char* name() const { return "DequeueCondition"; } |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | struct LockCondition : public ConditionBase { |
| 244 | int buf; |
| 245 | inline LockCondition(SharedBufferClient* sbc, int buf); |
Mathias Agopian | 41623bf | 2010-04-27 16:41:19 -0700 | [diff] [blame^] | 246 | inline bool operator()() const; |
| 247 | inline const char* name() const { return "LockCondition"; } |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | int32_t tail; |
Mathias Agopian | 43d8a28 | 2010-04-27 16:11:38 -0700 | [diff] [blame] | 251 | int32_t undoDequeueTail; |
Mathias Agopian | 9e3ebf8 | 2009-09-17 01:35:28 -0700 | [diff] [blame] | 252 | // statistics... |
| 253 | nsecs_t mDequeueTime[NUM_BUFFER_MAX]; |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 254 | }; |
| 255 | |
| 256 | // ---------------------------------------------------------------------------- |
| 257 | |
| 258 | class SharedBufferServer : public SharedBufferBase |
| 259 | { |
| 260 | public: |
Mathias Agopian | 4fc61bf | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 261 | SharedBufferServer(SharedClient* sharedClient, int surface, int num, |
| 262 | int32_t identity); |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 263 | |
| 264 | ssize_t retireAndLock(); |
| 265 | status_t unlock(int buffer); |
Mathias Agopian | 3dbf98c | 2009-09-10 16:55:13 -0700 | [diff] [blame] | 266 | void setStatus(status_t status); |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 267 | status_t reallocate(); |
| 268 | status_t assertReallocate(int buffer); |
Mathias Agopian | 68174b1 | 2009-10-07 16:44:10 -0700 | [diff] [blame] | 269 | int32_t getQueuedCount() const; |
Mathias Agopian | 3dbf98c | 2009-09-10 16:55:13 -0700 | [diff] [blame] | 270 | |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 271 | Region getDirtyRegion(int buffer) const; |
| 272 | |
Mathias Agopian | 9e3ebf8 | 2009-09-17 01:35:28 -0700 | [diff] [blame] | 273 | SharedBufferStack::Statistics getStats() const; |
| 274 | |
| 275 | |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 276 | private: |
| 277 | struct UnlockUpdate : public UpdateBase { |
| 278 | const int lockedBuffer; |
| 279 | inline UnlockUpdate(SharedBufferBase* sbb, int lockedBuffer); |
| 280 | inline ssize_t operator()(); |
| 281 | }; |
| 282 | |
| 283 | struct RetireUpdate : public UpdateBase { |
| 284 | const int numBuffers; |
| 285 | inline RetireUpdate(SharedBufferBase* sbb, int numBuffers); |
| 286 | inline ssize_t operator()(); |
| 287 | }; |
| 288 | |
Mathias Agopian | 3dbf98c | 2009-09-10 16:55:13 -0700 | [diff] [blame] | 289 | struct StatusUpdate : public UpdateBase { |
| 290 | const status_t status; |
| 291 | inline StatusUpdate(SharedBufferBase* sbb, status_t status); |
| 292 | inline ssize_t operator()(); |
| 293 | }; |
| 294 | |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 295 | struct ReallocateCondition : public ConditionBase { |
| 296 | int buf; |
| 297 | inline ReallocateCondition(SharedBufferBase* sbb, int buf); |
Mathias Agopian | 41623bf | 2010-04-27 16:41:19 -0700 | [diff] [blame^] | 298 | inline bool operator()() const; |
| 299 | inline const char* name() const { return "ReallocateCondition"; } |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 300 | }; |
| 301 | }; |
| 302 | |
| 303 | // =========================================================================== |
| 304 | |
| 305 | struct display_cblk_t |
| 306 | { |
| 307 | uint16_t w; |
| 308 | uint16_t h; |
| 309 | uint8_t format; |
| 310 | uint8_t orientation; |
| 311 | uint8_t reserved[2]; |
| 312 | float fps; |
| 313 | float density; |
| 314 | float xdpi; |
| 315 | float ydpi; |
| 316 | uint32_t pad[2]; |
| 317 | }; |
| 318 | |
| 319 | struct surface_flinger_cblk_t // 4KB max |
| 320 | { |
| 321 | uint8_t connected; |
| 322 | uint8_t reserved[3]; |
| 323 | uint32_t pad[7]; |
| 324 | display_cblk_t displays[NUM_DISPLAY_MAX]; |
| 325 | }; |
| 326 | |
| 327 | // --------------------------------------------------------------------------- |
| 328 | |
Mathias Agopian | cdaaf32 | 2010-04-05 16:21:53 -0700 | [diff] [blame] | 329 | COMPILE_TIME_ASSERT(sizeof(SharedClient) <= 32768) |
Mathias Agopian | 81e2a52 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 330 | COMPILE_TIME_ASSERT(sizeof(surface_flinger_cblk_t) <= 4096) |
| 331 | |
| 332 | // --------------------------------------------------------------------------- |
| 333 | }; // namespace android |
| 334 | |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 335 | #endif /* ANDROID_SF_SHARED_BUFFER_STACK_H */ |