blob: 187da203c5457ab202072c2ab255004c261258ac [file] [log] [blame]
Mathias Agopian79f39eb2011-11-17 17:49:17 -08001/*
2 * Copyright (C) 2011 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#include <stdint.h>
18#include <sys/types.h>
19
20#include <utils/Errors.h>
21
22#include <fcntl.h>
23#include <sys/ioctl.h>
24#include <linux/fb.h>
25
26#include "DisplayHardware/VSyncBarrier.h"
27
28#ifndef FBIO_WAITFORVSYNC
29#define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32)
30#endif
31
32namespace android {
33// ---------------------------------------------------------------------------
34
35VSyncBarrier::VSyncBarrier() : mFd(-EINVAL) {
36#if HAS_WAITFORVSYNC
37 mFd = open("/dev/graphics/fb0", O_RDWR);
38 if (mFd < 0) {
39 mFd = -errno;
40 }
41 // try to see if FBIO_WAITFORVSYNC is supported
42 uint32_t crt = 0;
43 int err = ioctl(mFd, FBIO_WAITFORVSYNC, &crt);
44 if (err < 0) {
45 close(mFd);
46 mFd = -EINVAL;
47 }
48#endif
49}
50
51VSyncBarrier::~VSyncBarrier() {
52 if (mFd >= 0) {
53 close(mFd);
54 }
55}
56
57status_t VSyncBarrier::initCheck() const {
58 return mFd < 0 ? mFd : status_t(NO_ERROR);
59}
60
61// this must be thread-safe
62status_t VSyncBarrier::wait(nsecs_t* timestamp) const {
63 if (mFd < 0) {
64 return mFd;
65 }
66
67 int err;
68 uint32_t crt = 0;
69 do {
70 err = ioctl(mFd, FBIO_WAITFORVSYNC, &crt);
71 } while (err<0 && errno==EINTR);
72 if (err < 0) {
73 return -errno;
74 }
75 // ideally this would come from the driver
76 timestamp[0] = systemTime();
77 return NO_ERROR;
78}
79
80// ---------------------------------------------------------------------------
81}; // namespace android