| Mathias Agopian | cbb288b | 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 | 9cce325 | 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 | cbb288b | 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. | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 46 | * | 
|  | 47 | */ | 
|  | 48 |  | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 49 | // ---------------------------------------------------------------------------- | 
|  | 50 |  | 
|  | 51 | class Region; | 
|  | 52 | class SharedBufferStack; | 
|  | 53 | class SharedClient; | 
|  | 54 |  | 
|  | 55 | // ---------------------------------------------------------------------------- | 
|  | 56 |  | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 57 | class SharedBufferStack | 
|  | 58 | { | 
|  | 59 | friend class SharedClient; | 
|  | 60 | friend class SharedBufferBase; | 
|  | 61 | friend class SharedBufferClient; | 
|  | 62 | friend class SharedBufferServer; | 
|  | 63 |  | 
|  | 64 | public: | 
| Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 65 | // 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 Agopian | f10d7fd | 2010-05-21 14:19:50 -0700 | [diff] [blame] | 69 | static const unsigned int NUM_BUFFER_MIN  = 2; | 
| Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 70 | static const unsigned int NUM_DISPLAY_MAX = 4; | 
|  | 71 |  | 
| Mathias Agopian | 86f7329 | 2009-09-17 01:35:28 -0700 | [diff] [blame] | 72 | struct Statistics { // 4 longs | 
|  | 73 | typedef int32_t usecs_t; | 
|  | 74 | usecs_t  totalTime; | 
|  | 75 | usecs_t  reserved[3]; | 
|  | 76 | }; | 
| Mathias Agopian | cc08e68 | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 77 |  | 
|  | 78 | struct SmallRect { | 
|  | 79 | uint16_t l, t, r, b; | 
|  | 80 | }; | 
|  | 81 |  | 
|  | 82 | struct FlatRegion { // 52 bytes = 4 * (1 + 2*N) | 
| Mathias Agopian | b661d66 | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 83 | static const unsigned int NUM_RECT_MAX = 5; | 
| Mathias Agopian | cc08e68 | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 84 | uint32_t    count; | 
|  | 85 | SmallRect   rects[NUM_RECT_MAX]; | 
|  | 86 | }; | 
|  | 87 |  | 
|  | 88 | struct BufferData { | 
|  | 89 | FlatRegion dirtyRegion; | 
|  | 90 | SmallRect  crop; | 
| Mathias Agopian | b661d66 | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 91 | uint8_t transform; | 
|  | 92 | uint8_t reserved[3]; | 
| Mathias Agopian | cc08e68 | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 93 | }; | 
| Mathias Agopian | 86f7329 | 2009-09-17 01:35:28 -0700 | [diff] [blame] | 94 |  | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 95 | SharedBufferStack(); | 
| Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 96 | void init(int32_t identity); | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 97 | status_t setDirtyRegion(int buffer, const Region& reg); | 
| Mathias Agopian | cc08e68 | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 98 | status_t setCrop(int buffer, const Rect& reg); | 
| Mathias Agopian | b661d66 | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 99 | status_t setTransform(int buffer, uint8_t transform); | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 100 | Region getDirtyRegion(int buffer) const; | 
| Mathias Agopian | b661d66 | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 101 | Rect getCrop(int buffer) const; | 
|  | 102 | uint32_t getTransform(int buffer) const; | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 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 | 
| Mathias Agopian | 2bd1d95 | 2011-01-19 18:02:20 -0800 | [diff] [blame] | 108 | volatile int32_t reserved1; | 
| Mathias Agopian | b58b5d7 | 2009-09-10 16:55:13 -0700 | [diff] [blame] | 109 | volatile status_t status;   // surface's status code | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 110 |  | 
|  | 111 | // not part of the conditions | 
|  | 112 | volatile int32_t reallocMask; | 
| Mathias Agopian | c0a9164 | 2010-04-27 21:08:20 -0700 | [diff] [blame] | 113 | volatile int8_t index[NUM_BUFFER_MAX]; | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 114 |  | 
|  | 115 | int32_t     identity;       // surface's identity (const) | 
| Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 116 | int32_t     token;          // surface's token (for debugging) | 
| Mathias Agopian | 86f7329 | 2009-09-17 01:35:28 -0700 | [diff] [blame] | 117 | Statistics  stats; | 
| Mathias Agopian | 1995755 | 2010-10-01 16:22:41 -0700 | [diff] [blame] | 118 | int8_t      headBuf;        // last retired buffer | 
|  | 119 | uint8_t     reservedBytes[3]; | 
| Mathias Agopian | 1100c8b | 2010-04-05 16:21:53 -0700 | [diff] [blame] | 120 | int32_t     reserved; | 
| Mathias Agopian | b661d66 | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 121 | BufferData  buffers[NUM_BUFFER_MAX];     // 1024 bytes | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 122 | }; | 
|  | 123 |  | 
|  | 124 | // ---------------------------------------------------------------------------- | 
|  | 125 |  | 
| Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 126 | // 32 KB max | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 127 | class SharedClient | 
|  | 128 | { | 
|  | 129 | public: | 
|  | 130 | SharedClient(); | 
|  | 131 | ~SharedClient(); | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 132 | status_t validate(size_t token) const; | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 133 |  | 
|  | 134 | private: | 
|  | 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 Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 142 | SharedBufferStack surfaces[ SharedBufferStack::NUM_LAYERS_MAX ]; | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 143 | }; | 
|  | 144 |  | 
|  | 145 | // ============================================================================ | 
|  | 146 |  | 
|  | 147 | class SharedBufferBase | 
|  | 148 | { | 
|  | 149 | public: | 
| Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 150 | SharedBufferBase(SharedClient* sharedClient, int surface, | 
| Mathias Agopian | 9ec430a | 2009-10-06 19:00:57 -0700 | [diff] [blame] | 151 | int32_t identity); | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 152 | ~SharedBufferBase(); | 
| Mathias Agopian | 0b3ad46 | 2009-10-02 18:12:30 -0700 | [diff] [blame] | 153 | status_t getStatus() const; | 
| Mathias Agopian | 7e27f05 | 2010-05-28 14:22:23 -0700 | [diff] [blame] | 154 | int32_t getIdentity() const; | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 155 | String8 dump(char const* prefix) const; | 
|  | 156 |  | 
|  | 157 | protected: | 
|  | 158 | SharedClient* const mSharedClient; | 
|  | 159 | SharedBufferStack* const mSharedStack; | 
| Mathias Agopian | 9ec430a | 2009-10-06 19:00:57 -0700 | [diff] [blame] | 160 | const int mIdentity; | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 161 |  | 
|  | 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 Agopian | b296533 | 2010-04-27 16:41:19 -0700 | [diff] [blame] | 169 | virtual ~ConditionBase() { }; | 
|  | 170 | virtual bool operator()() const = 0; | 
|  | 171 | virtual const char* name() const = 0; | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 172 | }; | 
| Mathias Agopian | b296533 | 2010-04-27 16:41:19 -0700 | [diff] [blame] | 173 | status_t waitForCondition(const ConditionBase& condition); | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 174 |  | 
|  | 175 | struct UpdateBase { | 
|  | 176 | SharedBufferStack& stack; | 
|  | 177 | inline UpdateBase(SharedBufferBase* sbb) | 
|  | 178 | : stack(*sbb->mSharedStack) { } | 
|  | 179 | }; | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 180 | template <typename T> | 
|  | 181 | status_t updateCondition(T update); | 
|  | 182 | }; | 
|  | 183 |  | 
|  | 184 | template <typename T> | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 185 | status_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 |  | 
|  | 195 | class SharedBufferClient : public SharedBufferBase | 
|  | 196 | { | 
|  | 197 | public: | 
| Mathias Agopian | 9ec430a | 2009-10-06 19:00:57 -0700 | [diff] [blame] | 198 | SharedBufferClient(SharedClient* sharedClient, int surface, int num, | 
|  | 199 | int32_t identity); | 
|  | 200 |  | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 201 | ssize_t dequeue(); | 
|  | 202 | status_t undoDequeue(int buf); | 
|  | 203 |  | 
|  | 204 | status_t lock(int buf); | 
| Mathias Agopian | 1995755 | 2010-10-01 16:22:41 -0700 | [diff] [blame] | 205 | status_t cancel(int buf); | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 206 | status_t queue(int buf); | 
|  | 207 | bool needNewBuffer(int buffer) const; | 
|  | 208 | status_t setDirtyRegion(int buffer, const Region& reg); | 
| Mathias Agopian | cc08e68 | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 209 | status_t setCrop(int buffer, const Rect& reg); | 
| Mathias Agopian | b661d66 | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 210 | status_t setTransform(int buffer, uint32_t transform); | 
| Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 211 |  | 
|  | 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 Agopian | cbb288b | 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 |  | 
| Mathias Agopian | 0e7f429 | 2010-08-26 17:42:27 -0700 | [diff] [blame] | 230 | struct DequeueUpdate : public UpdateBase { | 
|  | 231 | inline DequeueUpdate(SharedBufferBase* sbb); | 
|  | 232 | inline ssize_t operator()(); | 
|  | 233 | }; | 
|  | 234 |  | 
| Mathias Agopian | 1995755 | 2010-10-01 16:22:41 -0700 | [diff] [blame] | 235 | struct CancelUpdate : public UpdateBase { | 
|  | 236 | int tail, buf; | 
|  | 237 | inline CancelUpdate(SharedBufferBase* sbb, int tail, int buf); | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 238 | inline ssize_t operator()(); | 
|  | 239 | }; | 
|  | 240 |  | 
|  | 241 | // -- | 
|  | 242 |  | 
|  | 243 | struct DequeueCondition : public ConditionBase { | 
|  | 244 | inline DequeueCondition(SharedBufferClient* sbc); | 
| Mathias Agopian | b296533 | 2010-04-27 16:41:19 -0700 | [diff] [blame] | 245 | inline bool operator()() const; | 
|  | 246 | inline const char* name() const { return "DequeueCondition"; } | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 247 | }; | 
|  | 248 |  | 
|  | 249 | struct LockCondition : public ConditionBase { | 
|  | 250 | int buf; | 
|  | 251 | inline LockCondition(SharedBufferClient* sbc, int buf); | 
| Mathias Agopian | b296533 | 2010-04-27 16:41:19 -0700 | [diff] [blame] | 252 | inline bool operator()() const; | 
|  | 253 | inline const char* name() const { return "LockCondition"; } | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 254 | }; | 
|  | 255 |  | 
| Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 256 | int32_t computeTail() const; | 
|  | 257 |  | 
|  | 258 | mutable RWLock mLock; | 
|  | 259 | int mNumBuffers; | 
|  | 260 |  | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 261 | int32_t tail; | 
| Mathias Agopian | c0a9164 | 2010-04-27 21:08:20 -0700 | [diff] [blame] | 262 | int32_t queued_head; | 
| Mathias Agopian | 86f7329 | 2009-09-17 01:35:28 -0700 | [diff] [blame] | 263 | // statistics... | 
| Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 264 | nsecs_t mDequeueTime[SharedBufferStack::NUM_BUFFER_MAX]; | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 265 | }; | 
|  | 266 |  | 
|  | 267 | // ---------------------------------------------------------------------------- | 
|  | 268 |  | 
| Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 269 | class SharedBufferServer | 
|  | 270 | : public SharedBufferBase, | 
|  | 271 | public LightRefBase<SharedBufferServer> | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 272 | { | 
|  | 273 | public: | 
| Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 274 | SharedBufferServer(SharedClient* sharedClient, int surface, int num, | 
|  | 275 | int32_t identity); | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 276 |  | 
|  | 277 | ssize_t retireAndLock(); | 
| Mathias Agopian | b58b5d7 | 2009-09-10 16:55:13 -0700 | [diff] [blame] | 278 | void setStatus(status_t status); | 
| Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 279 | status_t reallocateAll(); | 
|  | 280 | status_t reallocateAllExcept(int buffer); | 
| Mathias Agopian | e700501 | 2009-10-07 16:44:10 -0700 | [diff] [blame] | 281 | int32_t getQueuedCount() const; | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 282 | Region getDirtyRegion(int buffer) const; | 
| Mathias Agopian | b661d66 | 2010-08-19 17:01:19 -0700 | [diff] [blame] | 283 | Rect getCrop(int buffer) const; | 
|  | 284 | uint32_t getTransform(int buffer) const; | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 285 |  | 
| Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 286 | status_t resize(int newNumBuffers); | 
| Jamie Gennis | 54cc83e | 2010-11-02 11:51:32 -0700 | [diff] [blame] | 287 | status_t grow(int newNumBuffers); | 
|  | 288 | status_t shrink(int newNumBuffers); | 
| Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 289 |  | 
| Mathias Agopian | 86f7329 | 2009-09-17 01:35:28 -0700 | [diff] [blame] | 290 | SharedBufferStack::Statistics getStats() const; | 
|  | 291 |  | 
|  | 292 |  | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 293 | private: | 
| Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 294 | friend class LightRefBase<SharedBufferServer>; | 
|  | 295 | ~SharedBufferServer(); | 
|  | 296 |  | 
| Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 297 | /* | 
|  | 298 | * BufferList is basically a fixed-capacity sorted-vector of | 
|  | 299 | * unsigned 5-bits ints using a 32-bits int as storage. | 
|  | 300 | * it has efficient iterators to find items in the list and not in the list. | 
|  | 301 | */ | 
|  | 302 | class BufferList { | 
|  | 303 | size_t mCapacity; | 
|  | 304 | uint32_t mList; | 
|  | 305 | public: | 
| Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 306 | BufferList(size_t c = SharedBufferStack::NUM_BUFFER_MAX) | 
|  | 307 | : mCapacity(c), mList(0) { } | 
| Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 308 | status_t add(int value); | 
|  | 309 | status_t remove(int value); | 
| Mathias Agopian | a0b3f1d | 2010-05-21 14:51:33 -0700 | [diff] [blame] | 310 | uint32_t getMask() const { return mList; } | 
| Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 311 |  | 
|  | 312 | class const_iterator { | 
|  | 313 | friend class BufferList; | 
|  | 314 | uint32_t mask, curr; | 
|  | 315 | const_iterator(uint32_t mask) : | 
| Mathias Agopian | d6297f7 | 2010-05-17 17:27:26 -0700 | [diff] [blame] | 316 | mask(mask), curr(__builtin_clz(mask)) { | 
|  | 317 | } | 
| Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 318 | public: | 
|  | 319 | inline bool operator == (const const_iterator& rhs) const { | 
|  | 320 | return mask == rhs.mask; | 
|  | 321 | } | 
|  | 322 | inline bool operator != (const const_iterator& rhs) const { | 
|  | 323 | return mask != rhs.mask; | 
|  | 324 | } | 
|  | 325 | inline int operator *() const { return curr; } | 
| Mathias Agopian | d6297f7 | 2010-05-17 17:27:26 -0700 | [diff] [blame] | 326 | inline const const_iterator& operator ++() { | 
|  | 327 | mask &= ~(1<<(31-curr)); | 
|  | 328 | curr = __builtin_clz(mask); | 
| Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 329 | return *this; | 
|  | 330 | } | 
|  | 331 | }; | 
|  | 332 |  | 
|  | 333 | inline const_iterator begin() const { | 
|  | 334 | return const_iterator(mList); | 
|  | 335 | } | 
|  | 336 | inline const_iterator end() const   { | 
|  | 337 | return const_iterator(0); | 
|  | 338 | } | 
|  | 339 | inline const_iterator free_begin() const { | 
|  | 340 | uint32_t mask = (1 << (32-mCapacity)) - 1; | 
|  | 341 | return const_iterator( ~(mList | mask) ); | 
|  | 342 | } | 
|  | 343 | }; | 
|  | 344 |  | 
| Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 345 | // this protects mNumBuffers and mBufferList | 
|  | 346 | mutable RWLock mLock; | 
|  | 347 | int mNumBuffers; | 
| Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 348 | BufferList mBufferList; | 
|  | 349 |  | 
| Jamie Gennis | 54cc83e | 2010-11-02 11:51:32 -0700 | [diff] [blame] | 350 | struct BuffersAvailableCondition : public ConditionBase { | 
|  | 351 | int mNumBuffers; | 
|  | 352 | inline BuffersAvailableCondition(SharedBufferServer* sbs, | 
|  | 353 | int numBuffers); | 
|  | 354 | inline bool operator()() const; | 
|  | 355 | inline const char* name() const { return "BuffersAvailableCondition"; } | 
|  | 356 | }; | 
|  | 357 |  | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 358 | struct RetireUpdate : public UpdateBase { | 
|  | 359 | const int numBuffers; | 
|  | 360 | inline RetireUpdate(SharedBufferBase* sbb, int numBuffers); | 
|  | 361 | inline ssize_t operator()(); | 
|  | 362 | }; | 
|  | 363 |  | 
| Mathias Agopian | b58b5d7 | 2009-09-10 16:55:13 -0700 | [diff] [blame] | 364 | struct StatusUpdate : public UpdateBase { | 
|  | 365 | const status_t status; | 
|  | 366 | inline StatusUpdate(SharedBufferBase* sbb, status_t status); | 
|  | 367 | inline ssize_t operator()(); | 
|  | 368 | }; | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 369 | }; | 
|  | 370 |  | 
|  | 371 | // =========================================================================== | 
|  | 372 |  | 
|  | 373 | struct 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 |  | 
|  | 387 | struct surface_flinger_cblk_t   // 4KB max | 
|  | 388 | { | 
|  | 389 | uint8_t         connected; | 
|  | 390 | uint8_t         reserved[3]; | 
|  | 391 | uint32_t        pad[7]; | 
| Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 392 | display_cblk_t  displays[SharedBufferStack::NUM_DISPLAY_MAX]; | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 393 | }; | 
|  | 394 |  | 
|  | 395 | // --------------------------------------------------------------------------- | 
|  | 396 |  | 
| Mathias Agopian | 1100c8b | 2010-04-05 16:21:53 -0700 | [diff] [blame] | 397 | COMPILE_TIME_ASSERT(sizeof(SharedClient) <= 32768) | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 398 | COMPILE_TIME_ASSERT(sizeof(surface_flinger_cblk_t) <= 4096) | 
|  | 399 |  | 
|  | 400 | // --------------------------------------------------------------------------- | 
|  | 401 | }; // namespace android | 
|  | 402 |  | 
| Mathias Agopian | 9cce325 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 403 | #endif /* ANDROID_SF_SHARED_BUFFER_STACK_H */ |