blob: 3729f21e7d86c6fd7ac20ca6b446e2e32e951a66 [file] [log] [blame]
Naseer Ahmedff4f0252012-10-01 13:03:01 -04001/*
2 * Copyright (C) 2010 The Android Open Source Project
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -08003 * Copyright (C) 2012-2014, 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"
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -080033#define __STDC_FORMAT_MACROS 1
34#include <inttypes.h>
Naseer Ahmedff4f0252012-10-01 13:03:01 -040035
Naseer Ahmedff4f0252012-10-01 13:03:01 -040036namespace qhwc {
37
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070038#define HWC_VSYNC_THREAD_NAME "hwcVsyncThread"
Naseer Ahmede13a6002013-07-11 13:43:18 -040039#define MAX_SYSFS_FILE_PATH 255
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070040
Naseer Ahmed56601cd2013-03-05 11:34:14 -050041int hwc_vsync_control(hwc_context_t* ctx, int dpy, int enable)
42{
43 int ret = 0;
44 if(!ctx->vstate.fakevsync &&
45 ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL,
46 &enable) < 0) {
47 ALOGE("%s: vsync control failed. Dpy=%d, enable=%d : %s",
48 __FUNCTION__, dpy, enable, strerror(errno));
49 ret = -errno;
50 }
51 return ret;
52}
53
Naseer Ahmedff4f0252012-10-01 13:03:01 -040054static void *vsync_loop(void *param)
55{
Naseer Ahmedff4f0252012-10-01 13:03:01 -040056 hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
57
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070058 char thread_name[64] = HWC_VSYNC_THREAD_NAME;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040059 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
60 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY +
61 android::PRIORITY_MORE_FAVORABLE);
62
Naseer Ahmed40147772012-10-04 19:19:55 -040063 const int MAX_DATA = 64;
Naseer Ahmede13a6002013-07-11 13:43:18 -040064 char vdata[MAX_DATA];
Naseer Ahmed56601cd2013-03-05 11:34:14 -050065 bool logvsync = false;
Naseer Ahmedda10c142012-11-16 20:16:31 -050066
Naseer Ahmede13a6002013-07-11 13:43:18 -040067 struct pollfd pfd[2];
68 int fb_fd[2];
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070069 uint64_t timestamp[2] = {0,0};
Naseer Ahmede13a6002013-07-11 13:43:18 -040070 int num_displays;
71
Naseer Ahmedda10c142012-11-16 20:16:31 -050072 char property[PROPERTY_VALUE_MAX];
73 if(property_get("debug.hwc.fakevsync", property, NULL) > 0) {
74 if(atoi(property) == 1)
Naseer Ahmed56601cd2013-03-05 11:34:14 -050075 ctx->vstate.fakevsync = true;
76 }
77
78 if(property_get("debug.hwc.logvsync", property, 0) > 0) {
79 if(atoi(property) == 1)
80 logvsync = true;
Naseer Ahmedda10c142012-11-16 20:16:31 -050081 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -040082
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070083 if (ctx->mExtDisplay->isConnected())
Naseer Ahmede13a6002013-07-11 13:43:18 -040084 num_displays = 2;
85 else
86 num_displays = 1;
Naseer Ahmed8fec5c32013-06-06 17:00:15 -040087
Naseer Ahmede13a6002013-07-11 13:43:18 -040088 char vsync_node_path[MAX_SYSFS_FILE_PATH];
89 for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) {
90 snprintf(vsync_node_path, sizeof(vsync_node_path),
91 "/sys/class/graphics/fb%d/vsync_event",
92 dpy == HWC_DISPLAY_PRIMARY ? 0 :
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070093 overlay::Overlay::getInstance()->
94 getFbForDpy(HWC_DISPLAY_EXTERNAL));
Naseer Ahmede13a6002013-07-11 13:43:18 -040095 ALOGI("%s: Reading vsync for dpy=%d from %s", __FUNCTION__, dpy,
96 vsync_node_path);
97 fb_fd[dpy] = open(vsync_node_path, O_RDONLY);
98
99 if (fb_fd[dpy] < 0) {
100 // Make sure fb device is opened before starting this thread so this
101 // never happens.
102 ALOGE ("%s:not able to open vsync node for dpy=%d, %s",
103 __FUNCTION__, dpy, strerror(errno));
104 if (dpy == HWC_DISPLAY_PRIMARY) {
105 ctx->vstate.fakevsync = true;
106 break;
107 }
108 }
Naseer Ahmed8aface02013-08-27 11:30:41 -0400109 // Read once from the fds to clear the first notify
110 pread(fb_fd[dpy], vdata , MAX_DATA, 0);
Naseer Ahmede13a6002013-07-11 13:43:18 -0400111
112 pfd[dpy].fd = fb_fd[dpy];
113 if (pfd[dpy].fd >= 0)
114 pfd[dpy].events = POLLPRI | POLLERR;
Naseer Ahmed08212c02012-10-17 13:51:56 -0400115 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400116
Naseer Ahmede13a6002013-07-11 13:43:18 -0400117 if (LIKELY(!ctx->vstate.fakevsync)) {
118 do {
119 int err = poll(pfd, num_displays, -1);
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400120 if(err > 0) {
Naseer Ahmede13a6002013-07-11 13:43:18 -0400121 for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) {
122 if (pfd[dpy].revents & POLLPRI) {
123 int len = pread(pfd[dpy].fd, vdata, MAX_DATA, 0);
124 if (UNLIKELY(len < 0)) {
125 // If the read was just interrupted - it is not a
126 // fatal error. Just continue in this case
Naseer Ahmed8aface02013-08-27 11:30:41 -0400127 ALOGE ("%s: Unable to read vsync for dpy=%d : %s",
Naseer Ahmede13a6002013-07-11 13:43:18 -0400128 __FUNCTION__, dpy, strerror(errno));
129 continue;
130 }
131 // extract timestamp
132 if (!strncmp(vdata, "VSYNC=", strlen("VSYNC="))) {
133 timestamp[dpy] = strtoull(vdata + strlen("VSYNC="),
134 NULL, 0);
135 }
136 // send timestamp to SurfaceFlinger
137 ALOGD_IF (logvsync,
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -0800138 "%s: timestamp %"PRIu64" sent to SF for dpy=%d",
139 __FUNCTION__, timestamp[dpy], dpy);
Naseer Ahmede13a6002013-07-11 13:43:18 -0400140 ctx->proc->vsync(ctx->proc, dpy, timestamp[dpy]);
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400141 }
Naseer Ahmed8bb8f9d2013-05-11 07:29:45 -0400142 }
Naseer Ahmede13a6002013-07-11 13:43:18 -0400143
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400144 } else {
145 ALOGE("%s: vsync poll failed errno: %s", __FUNCTION__,
Naseer Ahmede13a6002013-07-11 13:43:18 -0400146 strerror(errno));
Naseer Ahmed8bb8f9d2013-05-11 07:29:45 -0400147 continue;
Naseer Ahmedda10c142012-11-16 20:16:31 -0500148 }
Naseer Ahmede13a6002013-07-11 13:43:18 -0400149 } while (true);
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400150
Naseer Ahmede13a6002013-07-11 13:43:18 -0400151 } else {
152
153 //Fake vsync is used only when set explicitly through a property or when
154 //the vsync timestamp node cannot be opened at bootup. There is no
155 //fallback to fake vsync from the true vsync loop, ever, as the
156 //condition can easily escape detection.
157 //Also, fake vsync is delivered only for the primary display.
158 do {
159 usleep(16666);
160 timestamp[HWC_DISPLAY_PRIMARY] = systemTime();
161 ctx->proc->vsync(ctx->proc, HWC_DISPLAY_PRIMARY,
162 timestamp[HWC_DISPLAY_PRIMARY]);
163
164 } while (true);
165 }
166
167 for (int dpy = HWC_DISPLAY_PRIMARY; dpy <= HWC_DISPLAY_EXTERNAL; dpy++ ) {
168 if(fb_fd[dpy] >= 0)
169 close (fb_fd[dpy]);
170 }
Iliyan Malcheveac89652012-10-04 10:38:28 -0700171
172 return NULL;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400173}
174
175void init_vsync_thread(hwc_context_t* ctx)
176{
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700177 int ret;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400178 pthread_t vsync_thread;
179 ALOGI("Initializing VSYNC Thread");
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700180 ret = pthread_create(&vsync_thread, NULL, vsync_loop, (void*) ctx);
181 if (ret) {
182 ALOGE("%s: failed to create %s: %s", __FUNCTION__,
Naseer Ahmedda10c142012-11-16 20:16:31 -0500183 HWC_VSYNC_THREAD_NAME, strerror(ret));
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700184 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400185}
186
187}; //namespace