blob: 595b7f2c00546ce1bdd39846a8b58c596e074b87 [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"
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070032#include "overlay.h"
Naseer Ahmedff4f0252012-10-01 13:03:01 -040033
Naseer Ahmedff4f0252012-10-01 13:03:01 -040034namespace qhwc {
35
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070036#define HWC_VSYNC_THREAD_NAME "hwcVsyncThread"
Naseer Ahmede13a6002013-07-11 13:43:18 -040037#define MAX_SYSFS_FILE_PATH 255
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070038
Naseer Ahmed56601cd2013-03-05 11:34:14 -050039int hwc_vsync_control(hwc_context_t* ctx, int dpy, int enable)
40{
41 int ret = 0;
42 if(!ctx->vstate.fakevsync &&
43 ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL,
44 &enable) < 0) {
45 ALOGE("%s: vsync control failed. Dpy=%d, enable=%d : %s",
46 __FUNCTION__, dpy, enable, strerror(errno));
47 ret = -errno;
48 }
49 return ret;
50}
51
Naseer Ahmedff4f0252012-10-01 13:03:01 -040052static void *vsync_loop(void *param)
53{
Naseer Ahmedff4f0252012-10-01 13:03:01 -040054 hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
55
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070056 char thread_name[64] = HWC_VSYNC_THREAD_NAME;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040057 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
58 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY +
59 android::PRIORITY_MORE_FAVORABLE);
60
Naseer Ahmed40147772012-10-04 19:19:55 -040061 const int MAX_DATA = 64;
Naseer Ahmede13a6002013-07-11 13:43:18 -040062 char vdata[MAX_DATA];
Naseer Ahmed56601cd2013-03-05 11:34:14 -050063 bool logvsync = false;
Naseer Ahmedda10c142012-11-16 20:16:31 -050064
Naseer Ahmede13a6002013-07-11 13:43:18 -040065 struct pollfd pfd[2];
66 int fb_fd[2];
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070067 uint64_t timestamp[2] = {0,0};
Naseer Ahmede13a6002013-07-11 13:43:18 -040068 int num_displays;
69
Naseer Ahmedda10c142012-11-16 20:16:31 -050070 char property[PROPERTY_VALUE_MAX];
71 if(property_get("debug.hwc.fakevsync", property, NULL) > 0) {
72 if(atoi(property) == 1)
Naseer Ahmed56601cd2013-03-05 11:34:14 -050073 ctx->vstate.fakevsync = true;
74 }
75
76 if(property_get("debug.hwc.logvsync", property, 0) > 0) {
77 if(atoi(property) == 1)
78 logvsync = true;
Naseer Ahmedda10c142012-11-16 20:16:31 -050079 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -040080
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070081 if (ctx->mExtDisplay->isConnected())
Naseer Ahmede13a6002013-07-11 13:43:18 -040082 num_displays = 2;
83 else
84 num_displays = 1;
Naseer Ahmed8fec5c32013-06-06 17:00:15 -040085
Naseer Ahmede13a6002013-07-11 13:43:18 -040086 char vsync_node_path[MAX_SYSFS_FILE_PATH];
87 for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) {
88 snprintf(vsync_node_path, sizeof(vsync_node_path),
89 "/sys/class/graphics/fb%d/vsync_event",
90 dpy == HWC_DISPLAY_PRIMARY ? 0 :
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070091 overlay::Overlay::getInstance()->
92 getFbForDpy(HWC_DISPLAY_EXTERNAL));
Naseer Ahmede13a6002013-07-11 13:43:18 -040093 ALOGI("%s: Reading vsync for dpy=%d from %s", __FUNCTION__, dpy,
94 vsync_node_path);
95 fb_fd[dpy] = open(vsync_node_path, O_RDONLY);
96
97 if (fb_fd[dpy] < 0) {
98 // Make sure fb device is opened before starting this thread so this
99 // never happens.
100 ALOGE ("%s:not able to open vsync node for dpy=%d, %s",
101 __FUNCTION__, dpy, strerror(errno));
102 if (dpy == HWC_DISPLAY_PRIMARY) {
103 ctx->vstate.fakevsync = true;
104 break;
105 }
106 }
Naseer Ahmed8aface02013-08-27 11:30:41 -0400107 // Read once from the fds to clear the first notify
108 pread(fb_fd[dpy], vdata , MAX_DATA, 0);
Naseer Ahmede13a6002013-07-11 13:43:18 -0400109
110 pfd[dpy].fd = fb_fd[dpy];
111 if (pfd[dpy].fd >= 0)
112 pfd[dpy].events = POLLPRI | POLLERR;
Naseer Ahmed08212c02012-10-17 13:51:56 -0400113 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400114
Naseer Ahmede13a6002013-07-11 13:43:18 -0400115 if (LIKELY(!ctx->vstate.fakevsync)) {
116 do {
117 int err = poll(pfd, num_displays, -1);
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400118 if(err > 0) {
Naseer Ahmede13a6002013-07-11 13:43:18 -0400119 for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) {
120 if (pfd[dpy].revents & POLLPRI) {
121 int len = pread(pfd[dpy].fd, vdata, MAX_DATA, 0);
122 if (UNLIKELY(len < 0)) {
123 // If the read was just interrupted - it is not a
124 // fatal error. Just continue in this case
Naseer Ahmed8aface02013-08-27 11:30:41 -0400125 ALOGE ("%s: Unable to read vsync for dpy=%d : %s",
Naseer Ahmede13a6002013-07-11 13:43:18 -0400126 __FUNCTION__, dpy, strerror(errno));
127 continue;
128 }
129 // extract timestamp
130 if (!strncmp(vdata, "VSYNC=", strlen("VSYNC="))) {
131 timestamp[dpy] = strtoull(vdata + strlen("VSYNC="),
132 NULL, 0);
133 }
134 // send timestamp to SurfaceFlinger
135 ALOGD_IF (logvsync,
136 "%s: timestamp %llu sent to SF for dpy=%d",
137 __FUNCTION__, timestamp[dpy], dpy);
138 ctx->proc->vsync(ctx->proc, dpy, timestamp[dpy]);
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400139 }
Naseer Ahmed8bb8f9d2013-05-11 07:29:45 -0400140 }
Naseer Ahmede13a6002013-07-11 13:43:18 -0400141
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400142 } else {
143 ALOGE("%s: vsync poll failed errno: %s", __FUNCTION__,
Naseer Ahmede13a6002013-07-11 13:43:18 -0400144 strerror(errno));
Naseer Ahmed8bb8f9d2013-05-11 07:29:45 -0400145 continue;
Naseer Ahmedda10c142012-11-16 20:16:31 -0500146 }
Naseer Ahmede13a6002013-07-11 13:43:18 -0400147 } while (true);
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400148
Naseer Ahmede13a6002013-07-11 13:43:18 -0400149 } else {
150
151 //Fake vsync is used only when set explicitly through a property or when
152 //the vsync timestamp node cannot be opened at bootup. There is no
153 //fallback to fake vsync from the true vsync loop, ever, as the
154 //condition can easily escape detection.
155 //Also, fake vsync is delivered only for the primary display.
156 do {
157 usleep(16666);
158 timestamp[HWC_DISPLAY_PRIMARY] = systemTime();
159 ctx->proc->vsync(ctx->proc, HWC_DISPLAY_PRIMARY,
160 timestamp[HWC_DISPLAY_PRIMARY]);
161
162 } while (true);
163 }
164
165 for (int dpy = HWC_DISPLAY_PRIMARY; dpy <= HWC_DISPLAY_EXTERNAL; dpy++ ) {
166 if(fb_fd[dpy] >= 0)
167 close (fb_fd[dpy]);
168 }
Iliyan Malcheveac89652012-10-04 10:38:28 -0700169
170 return NULL;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400171}
172
173void init_vsync_thread(hwc_context_t* ctx)
174{
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700175 int ret;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400176 pthread_t vsync_thread;
177 ALOGI("Initializing VSYNC Thread");
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700178 ret = pthread_create(&vsync_thread, NULL, vsync_loop, (void*) ctx);
179 if (ret) {
180 ALOGE("%s: failed to create %s: %s", __FUNCTION__,
Naseer Ahmedda10c142012-11-16 20:16:31 -0500181 HWC_VSYNC_THREAD_NAME, strerror(ret));
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700182 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400183}
184
185}; //namespace