blob: 28ac86d3204ff6aac17cd4fde55b8ce601c566d5 [file] [log] [blame]
Naseer Ahmedff4f0252012-10-01 13:03:01 -04001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 * Copyright (C) 2012, Code Aurora Forum. All rights reserved.
4 *
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
24#include <utils/Log.h>
25#include <fcntl.h>
Naseer Ahmedc7faa702012-10-04 15:10:30 -040026#include <sys/ioctl.h>
27#include <linux/msm_mdp.h>
Naseer Ahmedff4f0252012-10-01 13:03:01 -040028#include <sys/resource.h>
29#include <sys/prctl.h>
30#include "hwc_utils.h"
31#include "string.h"
32#include "external.h"
33
34
35namespace qhwc {
36
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070037#define HWC_VSYNC_THREAD_NAME "hwcVsyncThread"
38
Naseer Ahmedff4f0252012-10-01 13:03:01 -040039static void *vsync_loop(void *param)
40{
41 const char* vsync_timestamp_fb0 = "/sys/class/graphics/fb0/vsync_event";
42 const char* vsync_timestamp_fb1 = "/sys/class/graphics/fb1/vsync_event";
Naseer Ahmedc7faa702012-10-04 15:10:30 -040043 int dpy = HWC_DISPLAY_PRIMARY;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040044
45 hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
46
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070047 char thread_name[64] = HWC_VSYNC_THREAD_NAME;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040048 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
49 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY +
50 android::PRIORITY_MORE_FAVORABLE);
51
Naseer Ahmed40147772012-10-04 19:19:55 -040052 const int MAX_DATA = 64;
53 const int MAX_RETRY_COUNT = 100;
54 static char vdata[MAX_DATA];
Naseer Ahmedff4f0252012-10-01 13:03:01 -040055
56 uint64_t cur_timestamp=0;
Iliyan Malcheveac89652012-10-04 10:38:28 -070057 ssize_t len = -1;
58 int fd_timestamp = -1;
Naseer Ahmedc7faa702012-10-04 15:10:30 -040059 int ret = 0;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040060 bool fb1_vsync = false;
Naseer Ahmedc7faa702012-10-04 15:10:30 -040061 bool enabled = false;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040062
63 /* Currently read vsync timestamp from drivers
64 e.g. VSYNC=41800875994
65 */
66
67 do {
68 pthread_mutex_lock(&ctx->vstate.lock);
Naseer Ahmedc7faa702012-10-04 15:10:30 -040069 while (ctx->vstate.enable == false) {
70 if(enabled) {
71 int e = 0;
72 if(ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL,
73 &e) < 0) {
74 ALOGE("%s: vsync control failed. Dpy=%d, enabled=%d : %s",
75 __FUNCTION__, dpy, enabled, strerror(errno));
76 ret = -errno;
77 }
78 enabled = false;
79 }
Iliyan Malcheveac89652012-10-04 10:38:28 -070080 pthread_cond_wait(&ctx->vstate.cond, &ctx->vstate.lock);
Naseer Ahmedc7faa702012-10-04 15:10:30 -040081 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -040082 pthread_mutex_unlock(&ctx->vstate.lock);
83
Naseer Ahmedc7faa702012-10-04 15:10:30 -040084 if (!enabled) {
85 int e = 1;
86 if(ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL,
87 &e) < 0) {
88 ALOGE("%s: vsync control failed. Dpy=%d, enabled=%d : %s",
89 __FUNCTION__, dpy, enabled, strerror(errno));
90 ret = -errno;
91 }
92 enabled = true;
93 }
94
Naseer Ahmed40147772012-10-04 19:19:55 -040095 if(fd_timestamp < 0) {
96 fd_timestamp = open(vsync_timestamp_fb0, O_RDONLY);
97 if (fd_timestamp < 0) {
98 ALOGE ("FATAL:%s:not able to open file:%s, %s", __FUNCTION__,
99 (fb1_vsync) ? vsync_timestamp_fb1 : vsync_timestamp_fb0,
100 strerror(errno));
101 return NULL;
102 }
103 }
104
Naseer Ahmed40147772012-10-04 19:19:55 -0400105 for(int i = 0; i < MAX_RETRY_COUNT; i++) {
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700106 len = pread(fd_timestamp, vdata, MAX_DATA, 0);
107 if(len < 0 && (errno == EAGAIN || errno == EINTR)) {
Naseer Ahmed40147772012-10-04 19:19:55 -0400108 ALOGW("%s: vsync read: EAGAIN, retry (%d/%d).",
109 __FUNCTION__, i, MAX_RETRY_COUNT);
110 continue;
111 } else {
112 break;
113 }
114 }
115
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700116 if (len < 0) {
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400117 ALOGE ("FATAL:%s:not able to read file:%s, %s", __FUNCTION__,
118 vsync_timestamp_fb0, strerror(errno));
Iliyan Malcheveac89652012-10-04 10:38:28 -0700119 close (fd_timestamp);
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400120 return NULL;
121 }
122
Iliyan Malcheveac89652012-10-04 10:38:28 -0700123 // extract timestamp
124 const char *str = vdata;
125 if (!strncmp(str, "VSYNC=", strlen("VSYNC="))) {
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400126 cur_timestamp = strtoull(str + strlen("VSYNC="), NULL, 0);
Iliyan Malcheveac89652012-10-04 10:38:28 -0700127 } else {
Saurabh Shahae823e72012-10-05 00:08:11 -0400128 ALOGE ("FATAL: %s: vsync timestamp not in correct format: [%s]",
129 __FUNCTION__,
130 str);
Iliyan Malcheveac89652012-10-04 10:38:28 -0700131 }
132 // send timestamp to HAL
133 ALOGD_IF (VSYNC_DEBUG, "%s: timestamp %llu sent to HWC for %s",
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400134 __FUNCTION__, cur_timestamp, "fb0");
Naseer Ahmedc7faa702012-10-04 15:10:30 -0400135 ctx->proc->vsync(ctx->proc, dpy, cur_timestamp);
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400136
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400137 } while (true);
Naseer Ahmed40147772012-10-04 19:19:55 -0400138 if(fd_timestamp >= 0)
139 close (fd_timestamp);
Iliyan Malcheveac89652012-10-04 10:38:28 -0700140
141 return NULL;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400142}
143
144void init_vsync_thread(hwc_context_t* ctx)
145{
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700146 int ret;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400147 pthread_t vsync_thread;
148 ALOGI("Initializing VSYNC Thread");
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700149 ret = pthread_create(&vsync_thread, NULL, vsync_loop, (void*) ctx);
150 if (ret) {
151 ALOGE("%s: failed to create %s: %s", __FUNCTION__,
152 HWC_VSYNC_THREAD_NAME, strerror(ret));
153 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400154}
155
156}; //namespace