blob: 30b85f494ef1f85e254e0eb9990b6a2e8f32e030 [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
37static void *vsync_loop(void *param)
38{
39 const char* vsync_timestamp_fb0 = "/sys/class/graphics/fb0/vsync_event";
40 const char* vsync_timestamp_fb1 = "/sys/class/graphics/fb1/vsync_event";
Naseer Ahmedc7faa702012-10-04 15:10:30 -040041 int dpy = HWC_DISPLAY_PRIMARY;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040042
43 hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
44
45 char thread_name[64] = "hwcVsyncThread";
46 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
47 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY +
48 android::PRIORITY_MORE_FAVORABLE);
49
50 static char vdata[PAGE_SIZE];
51
52 uint64_t cur_timestamp=0;
Iliyan Malcheveac89652012-10-04 10:38:28 -070053 ssize_t len = -1;
54 int fd_timestamp = -1;
Naseer Ahmedc7faa702012-10-04 15:10:30 -040055 int ret = 0;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040056 bool fb1_vsync = false;
Naseer Ahmedc7faa702012-10-04 15:10:30 -040057 bool enabled = false;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040058
59 /* Currently read vsync timestamp from drivers
60 e.g. VSYNC=41800875994
61 */
62
63 do {
64 pthread_mutex_lock(&ctx->vstate.lock);
Naseer Ahmedc7faa702012-10-04 15:10:30 -040065 while (ctx->vstate.enable == false) {
66 if(enabled) {
67 int e = 0;
68 if(ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL,
69 &e) < 0) {
70 ALOGE("%s: vsync control failed. Dpy=%d, enabled=%d : %s",
71 __FUNCTION__, dpy, enabled, strerror(errno));
72 ret = -errno;
73 }
74 enabled = false;
75 }
Iliyan Malcheveac89652012-10-04 10:38:28 -070076 pthread_cond_wait(&ctx->vstate.cond, &ctx->vstate.lock);
Naseer Ahmedc7faa702012-10-04 15:10:30 -040077 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -040078 pthread_mutex_unlock(&ctx->vstate.lock);
79
Naseer Ahmedc7faa702012-10-04 15:10:30 -040080 if (!enabled) {
81 int e = 1;
82 if(ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL,
83 &e) < 0) {
84 ALOGE("%s: vsync control failed. Dpy=%d, enabled=%d : %s",
85 __FUNCTION__, dpy, enabled, strerror(errno));
86 ret = -errno;
87 }
88 enabled = true;
89 }
90
Naseer Ahmedff4f0252012-10-01 13:03:01 -040091 fd_timestamp = open(vsync_timestamp_fb0, O_RDONLY);
Iliyan Malcheveac89652012-10-04 10:38:28 -070092 if (fd_timestamp < 0) {
93 ALOGE ("FATAL:%s:not able to open file:%s, %s", __FUNCTION__,
94 (fb1_vsync) ? vsync_timestamp_fb1 : vsync_timestamp_fb0,
95 strerror(errno));
96 return NULL;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040097 }
98 // Open success - read now
99 len = read(fd_timestamp, vdata, PAGE_SIZE);
100 if (len < 0){
101 ALOGE ("FATAL:%s:not able to read file:%s, %s", __FUNCTION__,
102 vsync_timestamp_fb0, strerror(errno));
Iliyan Malcheveac89652012-10-04 10:38:28 -0700103 close (fd_timestamp);
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400104 return NULL;
105 }
106
Iliyan Malcheveac89652012-10-04 10:38:28 -0700107 // extract timestamp
108 const char *str = vdata;
109 if (!strncmp(str, "VSYNC=", strlen("VSYNC="))) {
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400110 cur_timestamp = strtoull(str + strlen("VSYNC="), NULL, 0);
Iliyan Malcheveac89652012-10-04 10:38:28 -0700111 } else {
112 ALOGE ("FATAL:%s:timestamp data not in correct format",
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400113 __FUNCTION__);
Iliyan Malcheveac89652012-10-04 10:38:28 -0700114 }
115 // send timestamp to HAL
116 ALOGD_IF (VSYNC_DEBUG, "%s: timestamp %llu sent to HWC for %s",
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400117 __FUNCTION__, cur_timestamp, "fb0");
Naseer Ahmedc7faa702012-10-04 15:10:30 -0400118 ctx->proc->vsync(ctx->proc, dpy, cur_timestamp);
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400119
Iliyan Malcheveac89652012-10-04 10:38:28 -0700120 close (fd_timestamp);
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400121 } while (true);
Iliyan Malcheveac89652012-10-04 10:38:28 -0700122
123 return NULL;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400124}
125
126void init_vsync_thread(hwc_context_t* ctx)
127{
128 pthread_t vsync_thread;
129 ALOGI("Initializing VSYNC Thread");
130 pthread_create(&vsync_thread, NULL, vsync_loop, (void*) ctx);
131}
132
133}; //namespace