blob: 5408c8bce83f183a7c38b7e0175a6f03c57230eb [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>
26#include <sys/resource.h>
27#include <sys/prctl.h>
28#include "hwc_utils.h"
29#include "string.h"
30#include "external.h"
31
32
33namespace qhwc {
34
35static void *vsync_loop(void *param)
36{
37 const char* vsync_timestamp_fb0 = "/sys/class/graphics/fb0/vsync_event";
38 const char* vsync_timestamp_fb1 = "/sys/class/graphics/fb1/vsync_event";
39 int display = HWC_DISPLAY_PRIMARY;
40
41 hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
42
43 char thread_name[64] = "hwcVsyncThread";
44 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
45 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY +
46 android::PRIORITY_MORE_FAVORABLE);
47
48 static char vdata[PAGE_SIZE];
49
50 uint64_t cur_timestamp=0;
Iliyan Malcheveac89652012-10-04 10:38:28 -070051 ssize_t len = -1;
52 int fd_timestamp = -1;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040053 bool fb1_vsync = false;
54
55 /* Currently read vsync timestamp from drivers
56 e.g. VSYNC=41800875994
57 */
58
59 do {
60 pthread_mutex_lock(&ctx->vstate.lock);
Iliyan Malcheveac89652012-10-04 10:38:28 -070061 while (ctx->vstate.enable == false)
62 pthread_cond_wait(&ctx->vstate.cond, &ctx->vstate.lock);
Naseer Ahmedff4f0252012-10-01 13:03:01 -040063 pthread_mutex_unlock(&ctx->vstate.lock);
64
Naseer Ahmedff4f0252012-10-01 13:03:01 -040065 fd_timestamp = open(vsync_timestamp_fb0, O_RDONLY);
Iliyan Malcheveac89652012-10-04 10:38:28 -070066 if (fd_timestamp < 0) {
67 ALOGE ("FATAL:%s:not able to open file:%s, %s", __FUNCTION__,
68 (fb1_vsync) ? vsync_timestamp_fb1 : vsync_timestamp_fb0,
69 strerror(errno));
70 return NULL;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040071 }
72 // Open success - read now
73 len = read(fd_timestamp, vdata, PAGE_SIZE);
74 if (len < 0){
75 ALOGE ("FATAL:%s:not able to read file:%s, %s", __FUNCTION__,
76 vsync_timestamp_fb0, strerror(errno));
Iliyan Malcheveac89652012-10-04 10:38:28 -070077 close (fd_timestamp);
Naseer Ahmedff4f0252012-10-01 13:03:01 -040078 return NULL;
79 }
80
Iliyan Malcheveac89652012-10-04 10:38:28 -070081 // extract timestamp
82 const char *str = vdata;
83 if (!strncmp(str, "VSYNC=", strlen("VSYNC="))) {
Naseer Ahmedff4f0252012-10-01 13:03:01 -040084 cur_timestamp = strtoull(str + strlen("VSYNC="), NULL, 0);
Iliyan Malcheveac89652012-10-04 10:38:28 -070085 } else {
86 ALOGE ("FATAL:%s:timestamp data not in correct format",
Naseer Ahmedff4f0252012-10-01 13:03:01 -040087 __FUNCTION__);
Iliyan Malcheveac89652012-10-04 10:38:28 -070088 }
89 // send timestamp to HAL
90 ALOGD_IF (VSYNC_DEBUG, "%s: timestamp %llu sent to HWC for %s",
Naseer Ahmedff4f0252012-10-01 13:03:01 -040091 __FUNCTION__, cur_timestamp, "fb0");
Iliyan Malcheveac89652012-10-04 10:38:28 -070092 ctx->proc->vsync(ctx->proc, display, cur_timestamp);
Naseer Ahmedff4f0252012-10-01 13:03:01 -040093
Iliyan Malcheveac89652012-10-04 10:38:28 -070094 close (fd_timestamp);
Naseer Ahmedff4f0252012-10-01 13:03:01 -040095 } while (true);
Iliyan Malcheveac89652012-10-04 10:38:28 -070096
97 return NULL;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040098}
99
100void init_vsync_thread(hwc_context_t* ctx)
101{
102 pthread_t vsync_thread;
103 ALOGI("Initializing VSYNC Thread");
104 pthread_create(&vsync_thread, NULL, vsync_loop, (void*) ctx);
105}
106
107}; //namespace