blob: e4da4f77882718363eb73390876c102dc2ea8bfe [file] [log] [blame]
Naseer Ahmedff4f0252012-10-01 13:03:01 -04001/*
2 * Copyright (C) 2010 The Android Open Source Project
Naseer Ahmed24f20052013-02-13 11:47:25 -05003 * Copyright (C) 2012-2013, The Linux Foundation. All rights reserved.
Naseer Ahmedff4f0252012-10-01 13:03:01 -04004 *
5 * Not a Contribution, Apache license notifications and license are
6 * retained for attribution purposes only.
7
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
Naseer Ahmedda10c142012-11-16 20:16:31 -050021#include <cutils/properties.h>
Naseer Ahmedff4f0252012-10-01 13:03:01 -040022#include <utils/Log.h>
23#include <fcntl.h>
Naseer Ahmedc7faa702012-10-04 15:10:30 -040024#include <sys/ioctl.h>
25#include <linux/msm_mdp.h>
Naseer Ahmedff4f0252012-10-01 13:03:01 -040026#include <sys/resource.h>
27#include <sys/prctl.h>
Naseer Ahmed8fec5c32013-06-06 17:00:15 -040028#include <poll.h>
Naseer Ahmedff4f0252012-10-01 13:03:01 -040029#include "hwc_utils.h"
30#include "string.h"
31#include "external.h"
32
Naseer Ahmedff4f0252012-10-01 13:03:01 -040033namespace qhwc {
34
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070035#define HWC_VSYNC_THREAD_NAME "hwcVsyncThread"
Naseer Ahmede13a6002013-07-11 13:43:18 -040036#define MAX_SYSFS_FILE_PATH 255
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070037
Naseer Ahmed56601cd2013-03-05 11:34:14 -050038int hwc_vsync_control(hwc_context_t* ctx, int dpy, int enable)
39{
40 int ret = 0;
41 if(!ctx->vstate.fakevsync &&
42 ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL,
43 &enable) < 0) {
44 ALOGE("%s: vsync control failed. Dpy=%d, enable=%d : %s",
45 __FUNCTION__, dpy, enable, strerror(errno));
46 ret = -errno;
47 }
48 return ret;
49}
50
Naseer Ahmedff4f0252012-10-01 13:03:01 -040051static void *vsync_loop(void *param)
52{
Naseer Ahmedff4f0252012-10-01 13:03:01 -040053 hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
54
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070055 char thread_name[64] = HWC_VSYNC_THREAD_NAME;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040056 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
57 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY +
58 android::PRIORITY_MORE_FAVORABLE);
59
Naseer Ahmed40147772012-10-04 19:19:55 -040060 const int MAX_DATA = 64;
Naseer Ahmede13a6002013-07-11 13:43:18 -040061 char vdata[MAX_DATA];
Naseer Ahmed56601cd2013-03-05 11:34:14 -050062 bool logvsync = false;
Naseer Ahmedda10c142012-11-16 20:16:31 -050063
Naseer Ahmede13a6002013-07-11 13:43:18 -040064 struct pollfd pfd[2];
65 int fb_fd[2];
66 uint64_t timestamp[2];
67 int num_displays;
68
Naseer Ahmedda10c142012-11-16 20:16:31 -050069 char property[PROPERTY_VALUE_MAX];
70 if(property_get("debug.hwc.fakevsync", property, NULL) > 0) {
71 if(atoi(property) == 1)
Naseer Ahmed56601cd2013-03-05 11:34:14 -050072 ctx->vstate.fakevsync = true;
73 }
74
75 if(property_get("debug.hwc.logvsync", property, 0) > 0) {
76 if(atoi(property) == 1)
77 logvsync = true;
Naseer Ahmedda10c142012-11-16 20:16:31 -050078 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -040079
Naseer Ahmede13a6002013-07-11 13:43:18 -040080 if (ctx->mExtDisplay->getHDMIIndex() > 0)
81 num_displays = 2;
82 else
83 num_displays = 1;
Naseer Ahmed8fec5c32013-06-06 17:00:15 -040084
Naseer Ahmede13a6002013-07-11 13:43:18 -040085 char vsync_node_path[MAX_SYSFS_FILE_PATH];
86 for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) {
87 snprintf(vsync_node_path, sizeof(vsync_node_path),
88 "/sys/class/graphics/fb%d/vsync_event",
89 dpy == HWC_DISPLAY_PRIMARY ? 0 :
90 ctx->mExtDisplay->getHDMIIndex());
91 ALOGI("%s: Reading vsync for dpy=%d from %s", __FUNCTION__, dpy,
92 vsync_node_path);
93 fb_fd[dpy] = open(vsync_node_path, O_RDONLY);
94
95 if (fb_fd[dpy] < 0) {
96 // Make sure fb device is opened before starting this thread so this
97 // never happens.
98 ALOGE ("%s:not able to open vsync node for dpy=%d, %s",
99 __FUNCTION__, dpy, strerror(errno));
100 if (dpy == HWC_DISPLAY_PRIMARY) {
101 ctx->vstate.fakevsync = true;
102 break;
103 }
104 }
105
106 pfd[dpy].fd = fb_fd[dpy];
107 if (pfd[dpy].fd >= 0)
108 pfd[dpy].events = POLLPRI | POLLERR;
Naseer Ahmed08212c02012-10-17 13:51:56 -0400109 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400110
Naseer Ahmede13a6002013-07-11 13:43:18 -0400111 if (LIKELY(!ctx->vstate.fakevsync)) {
112 do {
113 int err = poll(pfd, num_displays, -1);
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400114 if(err > 0) {
Naseer Ahmede13a6002013-07-11 13:43:18 -0400115 for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) {
116 if (pfd[dpy].revents & POLLPRI) {
117 int len = pread(pfd[dpy].fd, vdata, MAX_DATA, 0);
118 if (UNLIKELY(len < 0)) {
119 // If the read was just interrupted - it is not a
120 // fatal error. Just continue in this case
121 ALOGE ("%s:Unable to read vsync for dpy=%d :%s",
122 __FUNCTION__, dpy, strerror(errno));
123 continue;
124 }
125 // extract timestamp
126 if (!strncmp(vdata, "VSYNC=", strlen("VSYNC="))) {
127 timestamp[dpy] = strtoull(vdata + strlen("VSYNC="),
128 NULL, 0);
129 }
130 // send timestamp to SurfaceFlinger
131 ALOGD_IF (logvsync,
132 "%s: timestamp %llu sent to SF for dpy=%d",
133 __FUNCTION__, timestamp[dpy], dpy);
134 ctx->proc->vsync(ctx->proc, dpy, timestamp[dpy]);
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400135 }
Naseer Ahmed8bb8f9d2013-05-11 07:29:45 -0400136 }
Naseer Ahmede13a6002013-07-11 13:43:18 -0400137
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400138 } else {
139 ALOGE("%s: vsync poll failed errno: %s", __FUNCTION__,
Naseer Ahmede13a6002013-07-11 13:43:18 -0400140 strerror(errno));
Naseer Ahmed8bb8f9d2013-05-11 07:29:45 -0400141 continue;
Naseer Ahmedda10c142012-11-16 20:16:31 -0500142 }
Naseer Ahmede13a6002013-07-11 13:43:18 -0400143 } while (true);
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400144
Naseer Ahmede13a6002013-07-11 13:43:18 -0400145 } else {
146
147 //Fake vsync is used only when set explicitly through a property or when
148 //the vsync timestamp node cannot be opened at bootup. There is no
149 //fallback to fake vsync from the true vsync loop, ever, as the
150 //condition can easily escape detection.
151 //Also, fake vsync is delivered only for the primary display.
152 do {
153 usleep(16666);
154 timestamp[HWC_DISPLAY_PRIMARY] = systemTime();
155 ctx->proc->vsync(ctx->proc, HWC_DISPLAY_PRIMARY,
156 timestamp[HWC_DISPLAY_PRIMARY]);
157
158 } while (true);
159 }
160
161 for (int dpy = HWC_DISPLAY_PRIMARY; dpy <= HWC_DISPLAY_EXTERNAL; dpy++ ) {
162 if(fb_fd[dpy] >= 0)
163 close (fb_fd[dpy]);
164 }
Iliyan Malcheveac89652012-10-04 10:38:28 -0700165
166 return NULL;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400167}
168
169void init_vsync_thread(hwc_context_t* ctx)
170{
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700171 int ret;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400172 pthread_t vsync_thread;
173 ALOGI("Initializing VSYNC Thread");
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700174 ret = pthread_create(&vsync_thread, NULL, vsync_loop, (void*) ctx);
175 if (ret) {
176 ALOGE("%s: failed to create %s: %s", __FUNCTION__,
Naseer Ahmedda10c142012-11-16 20:16:31 -0500177 HWC_VSYNC_THREAD_NAME, strerror(ret));
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700178 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400179}
180
181}; //namespace