blob: 2e8fa12a4d6107720535bf12e8817ac616058c69 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070028#include <binder/IPCThreadState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <utils/Log.h>
30
31#include <private/ui/SurfaceFlingerSynchro.h>
32
33namespace android {
34
35// ---------------------------------------------------------------------------
36
37SurfaceFlingerSynchro::Barrier::Barrier()
38 : state(CLOSED) {
39}
40
41SurfaceFlingerSynchro::Barrier::~Barrier() {
42}
43
44void SurfaceFlingerSynchro::Barrier::open() {
45 asm volatile ("":::"memory");
46 Mutex::Autolock _l(lock);
47 state = OPENED;
48 cv.broadcast();
49}
50
51void SurfaceFlingerSynchro::Barrier::close() {
52 Mutex::Autolock _l(lock);
53 state = CLOSED;
54}
55
56void 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
67status_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
83SurfaceFlingerSynchro::SurfaceFlingerSynchro(const sp<ISurfaceComposer>& flinger)
84 : mSurfaceComposer(flinger)
85{
86}
87
88SurfaceFlingerSynchro::SurfaceFlingerSynchro()
89{
90}
91
92SurfaceFlingerSynchro::~SurfaceFlingerSynchro()
93{
94}
95
96status_t SurfaceFlingerSynchro::signal()
97{
98 mSurfaceComposer->signal();
99 return NO_ERROR;
100}
101
102status_t SurfaceFlingerSynchro::wait()
103{
104 mBarrier.waitAndClose();
105 return NO_ERROR;
106}
107
108status_t SurfaceFlingerSynchro::wait(nsecs_t timeout)
109{
110 if (timeout == 0)
111 return SurfaceFlingerSynchro::wait();
112 return mBarrier.waitAndClose(timeout);
113}
114
115void SurfaceFlingerSynchro::open()
116{
117 mBarrier.open();
118}
119
120// ---------------------------------------------------------------------------
121
122}; // namespace android
123