blob: c0a33c162a7c901becdd3682bd26ee770ac7ab6b [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>
28#include "hwc_utils.h"
29#include "string.h"
30#include "external.h"
31
Naseer Ahmedff4f0252012-10-01 13:03:01 -040032namespace qhwc {
33
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070034#define HWC_VSYNC_THREAD_NAME "hwcVsyncThread"
35
Naseer Ahmed56601cd2013-03-05 11:34:14 -050036int hwc_vsync_control(hwc_context_t* ctx, int dpy, int enable)
37{
38 int ret = 0;
39 if(!ctx->vstate.fakevsync &&
40 ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL,
41 &enable) < 0) {
42 ALOGE("%s: vsync control failed. Dpy=%d, enable=%d : %s",
43 __FUNCTION__, dpy, enable, strerror(errno));
44 ret = -errno;
45 }
46 return ret;
47}
48
Naseer Ahmedff4f0252012-10-01 13:03:01 -040049static void *vsync_loop(void *param)
50{
51 const char* vsync_timestamp_fb0 = "/sys/class/graphics/fb0/vsync_event";
52 const char* vsync_timestamp_fb1 = "/sys/class/graphics/fb1/vsync_event";
Naseer Ahmedc7faa702012-10-04 15:10:30 -040053 int dpy = HWC_DISPLAY_PRIMARY;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040054
55 hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
56
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070057 char thread_name[64] = HWC_VSYNC_THREAD_NAME;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040058 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
59 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY +
60 android::PRIORITY_MORE_FAVORABLE);
61
Naseer Ahmed40147772012-10-04 19:19:55 -040062 const int MAX_DATA = 64;
Naseer Ahmed40147772012-10-04 19:19:55 -040063 static char vdata[MAX_DATA];
Naseer Ahmedff4f0252012-10-01 13:03:01 -040064
65 uint64_t cur_timestamp=0;
Iliyan Malcheveac89652012-10-04 10:38:28 -070066 ssize_t len = -1;
67 int fd_timestamp = -1;
Naseer Ahmedc7faa702012-10-04 15:10:30 -040068 int ret = 0;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040069 bool fb1_vsync = false;
Naseer Ahmed56601cd2013-03-05 11:34:14 -050070 bool logvsync = false;
Naseer Ahmedda10c142012-11-16 20:16:31 -050071
72 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
83 /* Currently read vsync timestamp from drivers
84 e.g. VSYNC=41800875994
Naseer Ahmedda10c142012-11-16 20:16:31 -050085 */
Naseer Ahmed08212c02012-10-17 13:51:56 -040086 fd_timestamp = open(vsync_timestamp_fb0, O_RDONLY);
87 if (fd_timestamp < 0) {
Naseer Ahmed8bb8f9d2013-05-11 07:29:45 -040088 // Make sure fb device is opened before starting this thread so this
89 // never happens.
Naseer Ahmed08212c02012-10-17 13:51:56 -040090 ALOGE ("FATAL:%s:not able to open file:%s, %s", __FUNCTION__,
91 (fb1_vsync) ? vsync_timestamp_fb1 : vsync_timestamp_fb0,
92 strerror(errno));
Naseer Ahmed56601cd2013-03-05 11:34:14 -050093 ctx->vstate.fakevsync = true;
Naseer Ahmed08212c02012-10-17 13:51:56 -040094 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -040095
96 do {
Naseer Ahmed8bb8f9d2013-05-11 07:29:45 -040097 if (LIKELY(!ctx->vstate.fakevsync)) {
98 len = pread(fd_timestamp, vdata, MAX_DATA, 0);
Naseer Ahmedda10c142012-11-16 20:16:31 -050099 if (len < 0) {
Naseer Ahmed8bb8f9d2013-05-11 07:29:45 -0400100 // If the read was just interrupted - it is not a fatal error
101 // In either case, just continue.
102 if (errno != EAGAIN &&
103 errno != EINTR &&
104 errno != EBUSY) {
105 ALOGE ("FATAL:%s:not able to read file:%s, %s",
106 __FUNCTION__,
107 vsync_timestamp_fb0, strerror(errno));
108 }
109 continue;
Naseer Ahmedda10c142012-11-16 20:16:31 -0500110 }
Naseer Ahmedda10c142012-11-16 20:16:31 -0500111 // extract timestamp
112 const char *str = vdata;
113 if (!strncmp(str, "VSYNC=", strlen("VSYNC="))) {
114 cur_timestamp = strtoull(str + strlen("VSYNC="), NULL, 0);
Naseer Ahmedda10c142012-11-16 20:16:31 -0500115 }
Naseer Ahmedda10c142012-11-16 20:16:31 -0500116 } else {
Naseer Ahmed8bb8f9d2013-05-11 07:29:45 -0400117 usleep(16666);
Naseer Ahmedda10c142012-11-16 20:16:31 -0500118 cur_timestamp = systemTime();
119 }
120 // send timestamp to HAL
Naseer Ahmedb19be572013-03-15 21:31:54 -0400121 if(ctx->vstate.enable) {
122 ALOGD_IF (logvsync, "%s: timestamp %llu sent to HWC for %s",
123 __FUNCTION__, cur_timestamp, "fb0");
124 ctx->proc->vsync(ctx->proc, dpy, cur_timestamp);
125 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400126
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400127 } while (true);
Naseer Ahmed40147772012-10-04 19:19:55 -0400128 if(fd_timestamp >= 0)
129 close (fd_timestamp);
Iliyan Malcheveac89652012-10-04 10:38:28 -0700130
131 return NULL;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400132}
133
134void init_vsync_thread(hwc_context_t* ctx)
135{
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700136 int ret;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400137 pthread_t vsync_thread;
138 ALOGI("Initializing VSYNC Thread");
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700139 ret = pthread_create(&vsync_thread, NULL, vsync_loop, (void*) ctx);
140 if (ret) {
141 ALOGE("%s: failed to create %s: %s", __FUNCTION__,
Naseer Ahmedda10c142012-11-16 20:16:31 -0500142 HWC_VSYNC_THREAD_NAME, strerror(ret));
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700143 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400144}
145
146}; //namespace