blob: f83b6d734cea0675c4c80af394013c088a59844e [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
21// WARNING : Excessive logging, if VSYNC_DEBUG enabled
22#define VSYNC_DEBUG 0
23
Naseer Ahmedda10c142012-11-16 20:16:31 -050024#include <cutils/properties.h>
Naseer Ahmedff4f0252012-10-01 13:03:01 -040025#include <utils/Log.h>
26#include <fcntl.h>
Naseer Ahmedc7faa702012-10-04 15:10:30 -040027#include <sys/ioctl.h>
28#include <linux/msm_mdp.h>
Naseer Ahmedff4f0252012-10-01 13:03:01 -040029#include <sys/resource.h>
30#include <sys/prctl.h>
31#include "hwc_utils.h"
32#include "string.h"
33#include "external.h"
34
Naseer Ahmed24f20052013-02-13 11:47:25 -050035using android::LogIfSlow;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040036namespace qhwc {
37
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070038#define HWC_VSYNC_THREAD_NAME "hwcVsyncThread"
39
Naseer Ahmedff4f0252012-10-01 13:03:01 -040040static void *vsync_loop(void *param)
41{
42 const char* vsync_timestamp_fb0 = "/sys/class/graphics/fb0/vsync_event";
43 const char* vsync_timestamp_fb1 = "/sys/class/graphics/fb1/vsync_event";
Naseer Ahmedc7faa702012-10-04 15:10:30 -040044 int dpy = HWC_DISPLAY_PRIMARY;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040045
46 hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
47
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070048 char thread_name[64] = HWC_VSYNC_THREAD_NAME;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040049 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
50 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY +
51 android::PRIORITY_MORE_FAVORABLE);
52
Naseer Ahmed40147772012-10-04 19:19:55 -040053 const int MAX_DATA = 64;
54 const int MAX_RETRY_COUNT = 100;
55 static char vdata[MAX_DATA];
Naseer Ahmedff4f0252012-10-01 13:03:01 -040056
57 uint64_t cur_timestamp=0;
Iliyan Malcheveac89652012-10-04 10:38:28 -070058 ssize_t len = -1;
59 int fd_timestamp = -1;
Naseer Ahmedc7faa702012-10-04 15:10:30 -040060 int ret = 0;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040061 bool fb1_vsync = false;
Naseer Ahmedc7faa702012-10-04 15:10:30 -040062 bool enabled = false;
Naseer Ahmedda10c142012-11-16 20:16:31 -050063 bool fakevsync = false;
Naseer Ahmed24f20052013-02-13 11:47:25 -050064 uint32_t log_threshold = ns2ms(ctx->dpyAttr[dpy].vsync_period)*2;
Naseer Ahmedda10c142012-11-16 20:16:31 -050065
66 char property[PROPERTY_VALUE_MAX];
67 if(property_get("debug.hwc.fakevsync", property, NULL) > 0) {
68 if(atoi(property) == 1)
69 fakevsync = true;
70 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -040071
72 /* Currently read vsync timestamp from drivers
73 e.g. VSYNC=41800875994
Naseer Ahmedda10c142012-11-16 20:16:31 -050074 */
Naseer Ahmed08212c02012-10-17 13:51:56 -040075 fd_timestamp = open(vsync_timestamp_fb0, O_RDONLY);
76 if (fd_timestamp < 0) {
77 ALOGE ("FATAL:%s:not able to open file:%s, %s", __FUNCTION__,
78 (fb1_vsync) ? vsync_timestamp_fb1 : vsync_timestamp_fb0,
79 strerror(errno));
Naseer Ahmedda10c142012-11-16 20:16:31 -050080 fakevsync = true;
Naseer Ahmed08212c02012-10-17 13:51:56 -040081 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -040082
83 do {
84 pthread_mutex_lock(&ctx->vstate.lock);
Naseer Ahmedc7faa702012-10-04 15:10:30 -040085 while (ctx->vstate.enable == false) {
86 if(enabled) {
87 int e = 0;
Naseer Ahmedda10c142012-11-16 20:16:31 -050088 if(!fakevsync &&
89 ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL,
Naseer Ahmedc7faa702012-10-04 15:10:30 -040090 &e) < 0) {
91 ALOGE("%s: vsync control failed. Dpy=%d, enabled=%d : %s",
92 __FUNCTION__, dpy, enabled, strerror(errno));
93 ret = -errno;
94 }
95 enabled = false;
96 }
Iliyan Malcheveac89652012-10-04 10:38:28 -070097 pthread_cond_wait(&ctx->vstate.cond, &ctx->vstate.lock);
Naseer Ahmedc7faa702012-10-04 15:10:30 -040098 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -040099 pthread_mutex_unlock(&ctx->vstate.lock);
100
Naseer Ahmedc7faa702012-10-04 15:10:30 -0400101 if (!enabled) {
102 int e = 1;
Naseer Ahmedda10c142012-11-16 20:16:31 -0500103 if(!fakevsync &&
104 ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL,
105 &e) < 0) {
Naseer Ahmedc7faa702012-10-04 15:10:30 -0400106 ALOGE("%s: vsync control failed. Dpy=%d, enabled=%d : %s",
107 __FUNCTION__, dpy, enabled, strerror(errno));
108 ret = -errno;
109 }
110 enabled = true;
111 }
112
Naseer Ahmedda10c142012-11-16 20:16:31 -0500113 if(!fakevsync) {
114 for(int i = 0; i < MAX_RETRY_COUNT; i++) {
Naseer Ahmed24f20052013-02-13 11:47:25 -0500115 ALOGD_IF_SLOW(log_threshold, "Excessive delay reading vsync");
Naseer Ahmedda10c142012-11-16 20:16:31 -0500116 len = pread(fd_timestamp, vdata, MAX_DATA, 0);
Naseer Ahmed92fc1302012-12-19 15:58:10 -0500117 if(len < 0 && (errno == EAGAIN ||
118 errno == EINTR ||
119 errno == EBUSY)) {
120 ALOGW("%s: vsync read: %s, retry (%d/%d).",
121 __FUNCTION__, strerror(errno), i, MAX_RETRY_COUNT);
Naseer Ahmedda10c142012-11-16 20:16:31 -0500122 continue;
123 } else {
124 break;
125 }
126 }
Naseer Ahmed40147772012-10-04 19:19:55 -0400127
Naseer Ahmedda10c142012-11-16 20:16:31 -0500128 if (len < 0) {
129 ALOGE ("FATAL:%s:not able to read file:%s, %s", __FUNCTION__,
130 vsync_timestamp_fb0, strerror(errno));
131 close (fd_timestamp);
132 fakevsync = true;
133 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400134
Naseer Ahmedda10c142012-11-16 20:16:31 -0500135 // extract timestamp
136 const char *str = vdata;
137 if (!strncmp(str, "VSYNC=", strlen("VSYNC="))) {
138 cur_timestamp = strtoull(str + strlen("VSYNC="), NULL, 0);
139 } else {
140 ALOGE ("FATAL: %s: vsync timestamp not in correct format: [%s]",
141 __FUNCTION__,
142 str);
143 fakevsync = true;
144 }
145
146 } else {
147 usleep(16000);
148 cur_timestamp = systemTime();
149 }
150 // send timestamp to HAL
151 ALOGD_IF (VSYNC_DEBUG, "%s: timestamp %llu sent to HWC for %s",
152 __FUNCTION__, cur_timestamp, "fb0");
153 ctx->proc->vsync(ctx->proc, dpy, cur_timestamp);
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400154
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400155 } while (true);
Naseer Ahmed40147772012-10-04 19:19:55 -0400156 if(fd_timestamp >= 0)
157 close (fd_timestamp);
Iliyan Malcheveac89652012-10-04 10:38:28 -0700158
159 return NULL;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400160}
161
162void init_vsync_thread(hwc_context_t* ctx)
163{
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700164 int ret;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400165 pthread_t vsync_thread;
166 ALOGI("Initializing VSYNC Thread");
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700167 ret = pthread_create(&vsync_thread, NULL, vsync_loop, (void*) ctx);
168 if (ret) {
169 ALOGE("%s: failed to create %s: %s", __FUNCTION__,
Naseer Ahmedda10c142012-11-16 20:16:31 -0500170 HWC_VSYNC_THREAD_NAME, strerror(ret));
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700171 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400172}
173
174}; //namespace