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 | |
| 17 | #define LOG_TAG "SharedBufferStack" |
| 18 | |
| 19 | #include <stdint.h> |
| 20 | #include <sys/types.h> |
| 21 | |
| 22 | #include <utils/Debug.h> |
| 23 | #include <utils/Log.h> |
| 24 | #include <utils/threads.h> |
| 25 | |
| 26 | #include <private/ui/SharedBufferStack.h> |
| 27 | |
| 28 | #include <ui/Rect.h> |
| 29 | #include <ui/Region.h> |
| 30 | |
| 31 | #define DEBUG_ATOMICS 0 |
| 32 | |
| 33 | namespace android { |
| 34 | // ---------------------------------------------------------------------------- |
| 35 | |
| 36 | SharedClient::SharedClient() |
| 37 | : lock(Mutex::SHARED) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | SharedClient::~SharedClient() { |
| 42 | } |
| 43 | |
| 44 | |
| 45 | // these functions are used by the clients |
| 46 | status_t SharedClient::validate(size_t i) const { |
| 47 | if (uint32_t(i) >= uint32_t(NUM_LAYERS_MAX)) |
| 48 | return BAD_INDEX; |
| 49 | return surfaces[i].status; |
| 50 | } |
| 51 | |
| 52 | uint32_t SharedClient::getIdentity(size_t token) const { |
| 53 | return uint32_t(surfaces[token].identity); |
| 54 | } |
| 55 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 56 | // ---------------------------------------------------------------------------- |
| 57 | |
| 58 | |
| 59 | SharedBufferStack::SharedBufferStack() |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 60 | { |
| 61 | } |
| 62 | |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 63 | void SharedBufferStack::init(int32_t i) |
| 64 | { |
| 65 | inUse = -1; |
| 66 | status = NO_ERROR; |
| 67 | identity = i; |
| 68 | } |
| 69 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 70 | status_t SharedBufferStack::setDirtyRegion(int buffer, const Region& dirty) |
| 71 | { |
| 72 | if (uint32_t(buffer) >= NUM_BUFFER_MAX) |
| 73 | return BAD_INDEX; |
| 74 | |
| 75 | // in the current implementation we only send a single rectangle |
| 76 | const Rect bounds(dirty.getBounds()); |
| 77 | FlatRegion& reg(dirtyRegion[buffer]); |
| 78 | reg.count = 1; |
| 79 | reg.rects[0] = uint16_t(bounds.left); |
| 80 | reg.rects[1] = uint16_t(bounds.top); |
| 81 | reg.rects[2] = uint16_t(bounds.right); |
| 82 | reg.rects[3] = uint16_t(bounds.bottom); |
| 83 | return NO_ERROR; |
| 84 | } |
| 85 | |
| 86 | Region SharedBufferStack::getDirtyRegion(int buffer) const |
| 87 | { |
| 88 | Region res; |
| 89 | if (uint32_t(buffer) >= NUM_BUFFER_MAX) |
| 90 | return res; |
| 91 | |
| 92 | const FlatRegion& reg(dirtyRegion[buffer]); |
| 93 | res.set(Rect(reg.rects[0], reg.rects[1], reg.rects[2], reg.rects[3])); |
| 94 | return res; |
| 95 | } |
| 96 | |
| 97 | // ---------------------------------------------------------------------------- |
| 98 | |
| 99 | SharedBufferBase::SharedBufferBase(SharedClient* sharedClient, |
| 100 | int surface, int num) |
| 101 | : mSharedClient(sharedClient), |
| 102 | mSharedStack(sharedClient->surfaces + surface), |
| 103 | mNumBuffers(num) |
| 104 | { |
| 105 | } |
| 106 | |
| 107 | SharedBufferBase::~SharedBufferBase() |
| 108 | { |
| 109 | } |
| 110 | |
| 111 | uint32_t SharedBufferBase::getIdentity() |
| 112 | { |
| 113 | SharedBufferStack& stack( *mSharedStack ); |
| 114 | return stack.identity; |
| 115 | } |
| 116 | |
| 117 | size_t SharedBufferBase::getFrontBuffer() const |
| 118 | { |
| 119 | SharedBufferStack& stack( *mSharedStack ); |
| 120 | return size_t( stack.head ); |
| 121 | } |
| 122 | |
| 123 | String8 SharedBufferBase::dump(char const* prefix) const |
| 124 | { |
| 125 | const size_t SIZE = 1024; |
| 126 | char buffer[SIZE]; |
| 127 | String8 result; |
| 128 | SharedBufferStack& stack( *mSharedStack ); |
| 129 | snprintf(buffer, SIZE, |
| 130 | "%s[ head=%2d, available=%2d, queued=%2d ] " |
| 131 | "reallocMask=%08x, inUse=%2d, identity=%d, status=%d\n", |
| 132 | prefix, stack.head, stack.available, stack.queued, |
| 133 | stack.reallocMask, stack.inUse, stack.identity, stack.status); |
| 134 | result.append(buffer); |
| 135 | return result; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | // ============================================================================ |
| 140 | // conditions and updates |
| 141 | // ============================================================================ |
| 142 | |
| 143 | SharedBufferClient::DequeueCondition::DequeueCondition( |
| 144 | SharedBufferClient* sbc) : ConditionBase(sbc) { |
| 145 | } |
| 146 | bool SharedBufferClient::DequeueCondition::operator()() { |
| 147 | return stack.available > 0; |
| 148 | } |
| 149 | |
| 150 | SharedBufferClient::LockCondition::LockCondition( |
| 151 | SharedBufferClient* sbc, int buf) : ConditionBase(sbc), buf(buf) { |
| 152 | } |
| 153 | bool SharedBufferClient::LockCondition::operator()() { |
| 154 | return (buf != stack.head || |
| 155 | (stack.queued > 0 && stack.inUse != buf)); |
| 156 | } |
| 157 | |
| 158 | SharedBufferServer::ReallocateCondition::ReallocateCondition( |
| 159 | SharedBufferBase* sbb, int buf) : ConditionBase(sbb), buf(buf) { |
| 160 | } |
| 161 | bool SharedBufferServer::ReallocateCondition::operator()() { |
| 162 | // TODO: we should also check that buf has been dequeued |
| 163 | return (buf != stack.head); |
| 164 | } |
| 165 | |
| 166 | // ---------------------------------------------------------------------------- |
| 167 | |
| 168 | SharedBufferClient::QueueUpdate::QueueUpdate(SharedBufferBase* sbb) |
| 169 | : UpdateBase(sbb) { |
| 170 | } |
| 171 | ssize_t SharedBufferClient::QueueUpdate::operator()() { |
| 172 | android_atomic_inc(&stack.queued); |
| 173 | return NO_ERROR; |
| 174 | } |
| 175 | |
| 176 | SharedBufferClient::UndoDequeueUpdate::UndoDequeueUpdate(SharedBufferBase* sbb) |
| 177 | : UpdateBase(sbb) { |
| 178 | } |
| 179 | ssize_t SharedBufferClient::UndoDequeueUpdate::operator()() { |
| 180 | android_atomic_inc(&stack.available); |
| 181 | return NO_ERROR; |
| 182 | } |
| 183 | |
| 184 | SharedBufferServer::UnlockUpdate::UnlockUpdate( |
| 185 | SharedBufferBase* sbb, int lockedBuffer) |
| 186 | : UpdateBase(sbb), lockedBuffer(lockedBuffer) { |
| 187 | } |
| 188 | ssize_t SharedBufferServer::UnlockUpdate::operator()() { |
| 189 | if (stack.inUse != lockedBuffer) { |
| 190 | LOGE("unlocking %d, but currently locked buffer is %d", |
| 191 | lockedBuffer, stack.inUse); |
| 192 | return BAD_VALUE; |
| 193 | } |
| 194 | android_atomic_write(-1, &stack.inUse); |
| 195 | return NO_ERROR; |
| 196 | } |
| 197 | |
| 198 | SharedBufferServer::RetireUpdate::RetireUpdate( |
| 199 | SharedBufferBase* sbb, int numBuffers) |
| 200 | : UpdateBase(sbb), numBuffers(numBuffers) { |
| 201 | } |
| 202 | ssize_t SharedBufferServer::RetireUpdate::operator()() { |
| 203 | // head is only written in this function, which is single-thread. |
| 204 | int32_t head = stack.head; |
| 205 | |
| 206 | // Preventively lock the current buffer before updating queued. |
| 207 | android_atomic_write(head, &stack.inUse); |
| 208 | |
| 209 | // Decrement the number of queued buffers |
| 210 | int32_t queued; |
| 211 | do { |
| 212 | queued = stack.queued; |
| 213 | if (queued == 0) { |
| 214 | return NOT_ENOUGH_DATA; |
| 215 | } |
| 216 | } while (android_atomic_cmpxchg(queued, queued-1, &stack.queued)); |
| 217 | |
| 218 | // update the head pointer |
| 219 | head = ((head+1 >= numBuffers) ? 0 : head+1); |
| 220 | |
| 221 | // lock the buffer before advancing head, which automatically unlocks |
| 222 | // the buffer we preventively locked upon entering this function |
| 223 | android_atomic_write(head, &stack.inUse); |
| 224 | |
| 225 | // advance head |
| 226 | android_atomic_write(head, &stack.head); |
| 227 | |
| 228 | // now that head has moved, we can increment the number of available buffers |
| 229 | android_atomic_inc(&stack.available); |
| 230 | return head; |
| 231 | } |
| 232 | |
Mathias Agopian | b58b5d7 | 2009-09-10 16:55:13 -0700 | [diff] [blame] | 233 | SharedBufferServer::StatusUpdate::StatusUpdate( |
| 234 | SharedBufferBase* sbb, status_t status) |
| 235 | : UpdateBase(sbb), status(status) { |
| 236 | } |
| 237 | |
| 238 | ssize_t SharedBufferServer::StatusUpdate::operator()() { |
| 239 | android_atomic_write(status, &stack.status); |
| 240 | return NO_ERROR; |
| 241 | } |
| 242 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 243 | // ============================================================================ |
| 244 | |
| 245 | SharedBufferClient::SharedBufferClient(SharedClient* sharedClient, |
| 246 | int surface, int num) |
| 247 | : SharedBufferBase(sharedClient, surface, num), tail(0) |
| 248 | { |
Mathias Agopian | c7d5601 | 2009-09-14 15:48:42 -0700 | [diff] [blame^] | 249 | tail = computeTail(); |
| 250 | } |
| 251 | |
| 252 | int32_t SharedBufferClient::computeTail() const |
| 253 | { |
Mathias Agopian | 40d5799 | 2009-09-11 19:18:20 -0700 | [diff] [blame] | 254 | SharedBufferStack& stack( *mSharedStack ); |
Mathias Agopian | 40d5799 | 2009-09-11 19:18:20 -0700 | [diff] [blame] | 255 | // we need to make sure we read available and head coherently, |
| 256 | // w.r.t RetireUpdate. |
Mathias Agopian | c7d5601 | 2009-09-14 15:48:42 -0700 | [diff] [blame^] | 257 | int32_t newTail; |
| 258 | int32_t avail; |
| 259 | int32_t head; |
Mathias Agopian | 40d5799 | 2009-09-11 19:18:20 -0700 | [diff] [blame] | 260 | do { |
| 261 | avail = stack.available; |
| 262 | head = stack.head; |
| 263 | } while (stack.available != avail); |
Mathias Agopian | c7d5601 | 2009-09-14 15:48:42 -0700 | [diff] [blame^] | 264 | newTail = head - avail + 1; |
| 265 | if (newTail < 0) { |
| 266 | newTail += mNumBuffers; |
Mathias Agopian | 40d5799 | 2009-09-11 19:18:20 -0700 | [diff] [blame] | 267 | } |
Mathias Agopian | c7d5601 | 2009-09-14 15:48:42 -0700 | [diff] [blame^] | 268 | return newTail; |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | ssize_t SharedBufferClient::dequeue() |
| 272 | { |
Mathias Agopian | 40d5799 | 2009-09-11 19:18:20 -0700 | [diff] [blame] | 273 | SharedBufferStack& stack( *mSharedStack ); |
| 274 | |
| 275 | if (stack.head == tail && stack.available == 2) { |
| 276 | LOGW("dequeue: tail=%d, head=%d, avail=%d, queued=%d", |
| 277 | tail, stack.head, stack.available, stack.queued); |
| 278 | } |
| 279 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 280 | //LOGD("[%d] about to dequeue a buffer", |
| 281 | // mSharedStack->identity); |
| 282 | DequeueCondition condition(this); |
| 283 | status_t err = waitForCondition(condition); |
| 284 | if (err != NO_ERROR) |
| 285 | return ssize_t(err); |
| 286 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 287 | // NOTE: 'stack.available' is part of the conditions, however |
| 288 | // decrementing it, never changes any conditions, so we don't need |
| 289 | // to do this as part of an update. |
| 290 | if (android_atomic_dec(&stack.available) == 0) { |
| 291 | LOGW("dequeue probably called from multiple threads!"); |
| 292 | } |
| 293 | |
| 294 | int dequeued = tail; |
| 295 | tail = ((tail+1 >= mNumBuffers) ? 0 : tail+1); |
| 296 | LOGD_IF(DEBUG_ATOMICS, "dequeued=%d, tail=%d, %s", |
| 297 | dequeued, tail, dump("").string()); |
Mathias Agopian | 40d5799 | 2009-09-11 19:18:20 -0700 | [diff] [blame] | 298 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 299 | return dequeued; |
| 300 | } |
| 301 | |
| 302 | status_t SharedBufferClient::undoDequeue(int buf) |
| 303 | { |
| 304 | UndoDequeueUpdate update(this); |
| 305 | status_t err = updateCondition( update ); |
Mathias Agopian | c7d5601 | 2009-09-14 15:48:42 -0700 | [diff] [blame^] | 306 | if (err == NO_ERROR) { |
| 307 | tail = computeTail(); |
| 308 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 309 | return err; |
| 310 | } |
| 311 | |
| 312 | status_t SharedBufferClient::lock(int buf) |
| 313 | { |
| 314 | LockCondition condition(this, buf); |
| 315 | status_t err = waitForCondition(condition); |
| 316 | return err; |
| 317 | } |
| 318 | |
| 319 | status_t SharedBufferClient::queue(int buf) |
| 320 | { |
| 321 | QueueUpdate update(this); |
| 322 | status_t err = updateCondition( update ); |
| 323 | LOGD_IF(DEBUG_ATOMICS, "queued=%d, %s", buf, dump("").string()); |
| 324 | return err; |
| 325 | } |
| 326 | |
| 327 | bool SharedBufferClient::needNewBuffer(int buffer) const |
| 328 | { |
| 329 | SharedBufferStack& stack( *mSharedStack ); |
| 330 | const uint32_t mask = 1<<buffer; |
| 331 | return (android_atomic_and(~mask, &stack.reallocMask) & mask) != 0; |
| 332 | } |
| 333 | |
| 334 | status_t SharedBufferClient::setDirtyRegion(int buffer, const Region& reg) |
| 335 | { |
| 336 | SharedBufferStack& stack( *mSharedStack ); |
| 337 | return stack.setDirtyRegion(buffer, reg); |
| 338 | } |
| 339 | |
| 340 | // ---------------------------------------------------------------------------- |
| 341 | |
| 342 | SharedBufferServer::SharedBufferServer(SharedClient* sharedClient, |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 343 | int surface, int num, int32_t identity) |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 344 | : SharedBufferBase(sharedClient, surface, num) |
| 345 | { |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 346 | mSharedStack->init(identity); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 347 | mSharedStack->head = num-1; |
| 348 | mSharedStack->available = num; |
| 349 | mSharedStack->queued = 0; |
| 350 | mSharedStack->reallocMask = 0; |
| 351 | memset(mSharedStack->dirtyRegion, 0, sizeof(mSharedStack->dirtyRegion)); |
| 352 | } |
| 353 | |
| 354 | ssize_t SharedBufferServer::retireAndLock() |
| 355 | { |
| 356 | RetireUpdate update(this, mNumBuffers); |
| 357 | ssize_t buf = updateCondition( update ); |
Mathias Agopian | 40d5799 | 2009-09-11 19:18:20 -0700 | [diff] [blame] | 358 | LOGD_IF(DEBUG_ATOMICS && buf>=0, "retire=%d, %s", int(buf), dump("").string()); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 359 | return buf; |
| 360 | } |
| 361 | |
| 362 | status_t SharedBufferServer::unlock(int buffer) |
| 363 | { |
| 364 | UnlockUpdate update(this, buffer); |
| 365 | status_t err = updateCondition( update ); |
| 366 | return err; |
| 367 | } |
| 368 | |
Mathias Agopian | b58b5d7 | 2009-09-10 16:55:13 -0700 | [diff] [blame] | 369 | void SharedBufferServer::setStatus(status_t status) |
| 370 | { |
| 371 | StatusUpdate update(this, status); |
| 372 | updateCondition( update ); |
| 373 | } |
| 374 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 375 | status_t SharedBufferServer::reallocate() |
| 376 | { |
| 377 | SharedBufferStack& stack( *mSharedStack ); |
| 378 | uint32_t mask = (1<<mNumBuffers)-1; |
| 379 | android_atomic_or(mask, &stack.reallocMask); |
| 380 | return NO_ERROR; |
| 381 | } |
| 382 | |
| 383 | status_t SharedBufferServer::assertReallocate(int buffer) |
| 384 | { |
| 385 | ReallocateCondition condition(this, buffer); |
| 386 | status_t err = waitForCondition(condition); |
| 387 | return err; |
| 388 | } |
| 389 | |
| 390 | Region SharedBufferServer::getDirtyRegion(int buffer) const |
| 391 | { |
| 392 | SharedBufferStack& stack( *mSharedStack ); |
| 393 | return stack.getDirtyRegion(buffer); |
| 394 | } |
| 395 | |
| 396 | // --------------------------------------------------------------------------- |
| 397 | }; // namespace android |