The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 "SurfaceFlingerSynchro" |
| 18 | |
| 19 | #include <stdint.h> |
| 20 | #include <string.h> |
| 21 | #include <unistd.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <errno.h> |
| 24 | #include <limits.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
| 27 | |
| 28 | #include <utils/IPCThreadState.h> |
| 29 | #include <utils/Log.h> |
| 30 | |
| 31 | #include <private/ui/SurfaceFlingerSynchro.h> |
| 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | // --------------------------------------------------------------------------- |
| 36 | |
| 37 | SurfaceFlingerSynchro::Barrier::Barrier() |
| 38 | : state(CLOSED) { |
| 39 | } |
| 40 | |
| 41 | SurfaceFlingerSynchro::Barrier::~Barrier() { |
| 42 | } |
| 43 | |
| 44 | void SurfaceFlingerSynchro::Barrier::open() { |
| 45 | asm volatile ("":::"memory"); |
| 46 | Mutex::Autolock _l(lock); |
| 47 | state = OPENED; |
| 48 | cv.broadcast(); |
| 49 | } |
| 50 | |
| 51 | void SurfaceFlingerSynchro::Barrier::close() { |
| 52 | Mutex::Autolock _l(lock); |
| 53 | state = CLOSED; |
| 54 | } |
| 55 | |
| 56 | void SurfaceFlingerSynchro::Barrier::waitAndClose() |
| 57 | { |
| 58 | Mutex::Autolock _l(lock); |
| 59 | while (state == CLOSED) { |
| 60 | // we're about to wait, flush the binder command buffer |
| 61 | IPCThreadState::self()->flushCommands(); |
| 62 | cv.wait(lock); |
| 63 | } |
| 64 | state = CLOSED; |
| 65 | } |
| 66 | |
| 67 | status_t SurfaceFlingerSynchro::Barrier::waitAndClose(nsecs_t timeout) |
| 68 | { |
| 69 | Mutex::Autolock _l(lock); |
| 70 | while (state == CLOSED) { |
| 71 | // we're about to wait, flush the binder command buffer |
| 72 | IPCThreadState::self()->flushCommands(); |
| 73 | int err = cv.waitRelative(lock, timeout); |
| 74 | if (err != 0) |
| 75 | return err; |
| 76 | } |
| 77 | state = CLOSED; |
| 78 | return NO_ERROR; |
| 79 | } |
| 80 | |
| 81 | // --------------------------------------------------------------------------- |
| 82 | |
| 83 | SurfaceFlingerSynchro::SurfaceFlingerSynchro(const sp<ISurfaceComposer>& flinger) |
| 84 | : mSurfaceComposer(flinger) |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | SurfaceFlingerSynchro::SurfaceFlingerSynchro() |
| 89 | { |
| 90 | } |
| 91 | |
| 92 | SurfaceFlingerSynchro::~SurfaceFlingerSynchro() |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | status_t SurfaceFlingerSynchro::signal() |
| 97 | { |
| 98 | mSurfaceComposer->signal(); |
| 99 | return NO_ERROR; |
| 100 | } |
| 101 | |
| 102 | status_t SurfaceFlingerSynchro::wait() |
| 103 | { |
| 104 | mBarrier.waitAndClose(); |
| 105 | return NO_ERROR; |
| 106 | } |
| 107 | |
| 108 | status_t SurfaceFlingerSynchro::wait(nsecs_t timeout) |
| 109 | { |
| 110 | if (timeout == 0) |
| 111 | return SurfaceFlingerSynchro::wait(); |
| 112 | return mBarrier.waitAndClose(timeout); |
| 113 | } |
| 114 | |
| 115 | void SurfaceFlingerSynchro::open() |
| 116 | { |
| 117 | mBarrier.open(); |
| 118 | } |
| 119 | |
| 120 | // --------------------------------------------------------------------------- |
| 121 | |
| 122 | }; // namespace android |
| 123 | |