Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 1 | /* |
| 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 Ahmed | c7faa70 | 2012-10-04 15:10:30 -0400 | [diff] [blame] | 26 | #include <sys/ioctl.h> |
| 27 | #include <linux/msm_mdp.h> |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 28 | #include <sys/resource.h> |
| 29 | #include <sys/prctl.h> |
| 30 | #include "hwc_utils.h" |
| 31 | #include "string.h" |
| 32 | #include "external.h" |
| 33 | |
| 34 | |
| 35 | namespace qhwc { |
| 36 | |
| 37 | static 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 Ahmed | c7faa70 | 2012-10-04 15:10:30 -0400 | [diff] [blame] | 41 | int dpy = HWC_DISPLAY_PRIMARY; |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 42 | |
| 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 | |
Naseer Ahmed | 4014777 | 2012-10-04 19:19:55 -0400 | [diff] [blame^] | 50 | const int MAX_DATA = 64; |
| 51 | const int MAX_RETRY_COUNT = 100; |
| 52 | static char vdata[MAX_DATA]; |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 53 | |
| 54 | uint64_t cur_timestamp=0; |
Iliyan Malchev | eac8965 | 2012-10-04 10:38:28 -0700 | [diff] [blame] | 55 | ssize_t len = -1; |
| 56 | int fd_timestamp = -1; |
Naseer Ahmed | c7faa70 | 2012-10-04 15:10:30 -0400 | [diff] [blame] | 57 | int ret = 0; |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 58 | bool fb1_vsync = false; |
Naseer Ahmed | c7faa70 | 2012-10-04 15:10:30 -0400 | [diff] [blame] | 59 | bool enabled = false; |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 60 | |
| 61 | /* Currently read vsync timestamp from drivers |
| 62 | e.g. VSYNC=41800875994 |
| 63 | */ |
| 64 | |
| 65 | do { |
| 66 | pthread_mutex_lock(&ctx->vstate.lock); |
Naseer Ahmed | c7faa70 | 2012-10-04 15:10:30 -0400 | [diff] [blame] | 67 | while (ctx->vstate.enable == false) { |
| 68 | if(enabled) { |
| 69 | int e = 0; |
| 70 | if(ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL, |
| 71 | &e) < 0) { |
| 72 | ALOGE("%s: vsync control failed. Dpy=%d, enabled=%d : %s", |
| 73 | __FUNCTION__, dpy, enabled, strerror(errno)); |
| 74 | ret = -errno; |
| 75 | } |
| 76 | enabled = false; |
| 77 | } |
Iliyan Malchev | eac8965 | 2012-10-04 10:38:28 -0700 | [diff] [blame] | 78 | pthread_cond_wait(&ctx->vstate.cond, &ctx->vstate.lock); |
Naseer Ahmed | c7faa70 | 2012-10-04 15:10:30 -0400 | [diff] [blame] | 79 | } |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 80 | pthread_mutex_unlock(&ctx->vstate.lock); |
| 81 | |
Naseer Ahmed | c7faa70 | 2012-10-04 15:10:30 -0400 | [diff] [blame] | 82 | if (!enabled) { |
| 83 | int e = 1; |
| 84 | if(ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL, |
| 85 | &e) < 0) { |
| 86 | ALOGE("%s: vsync control failed. Dpy=%d, enabled=%d : %s", |
| 87 | __FUNCTION__, dpy, enabled, strerror(errno)); |
| 88 | ret = -errno; |
| 89 | } |
| 90 | enabled = true; |
| 91 | } |
| 92 | |
Naseer Ahmed | 4014777 | 2012-10-04 19:19:55 -0400 | [diff] [blame^] | 93 | if(fd_timestamp < 0) { |
| 94 | fd_timestamp = open(vsync_timestamp_fb0, O_RDONLY); |
| 95 | if (fd_timestamp < 0) { |
| 96 | ALOGE ("FATAL:%s:not able to open file:%s, %s", __FUNCTION__, |
| 97 | (fb1_vsync) ? vsync_timestamp_fb1 : vsync_timestamp_fb0, |
| 98 | strerror(errno)); |
| 99 | return NULL; |
| 100 | } |
| 101 | } |
| 102 | |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 103 | // Open success - read now |
Naseer Ahmed | 4014777 | 2012-10-04 19:19:55 -0400 | [diff] [blame^] | 104 | lseek(fd_timestamp, 0, SEEK_SET); |
| 105 | for(int i = 0; i < MAX_RETRY_COUNT; i++) { |
| 106 | len = read(fd_timestamp, vdata, MAX_DATA); |
| 107 | if(len < 0 && errno == EAGAIN) { |
| 108 | ALOGW("%s: vsync read: EAGAIN, retry (%d/%d).", |
| 109 | __FUNCTION__, i, MAX_RETRY_COUNT); |
| 110 | continue; |
| 111 | } else { |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 116 | if (len < 0){ |
| 117 | ALOGE ("FATAL:%s:not able to read file:%s, %s", __FUNCTION__, |
| 118 | vsync_timestamp_fb0, strerror(errno)); |
Iliyan Malchev | eac8965 | 2012-10-04 10:38:28 -0700 | [diff] [blame] | 119 | close (fd_timestamp); |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 120 | return NULL; |
| 121 | } |
| 122 | |
Iliyan Malchev | eac8965 | 2012-10-04 10:38:28 -0700 | [diff] [blame] | 123 | // extract timestamp |
| 124 | const char *str = vdata; |
| 125 | if (!strncmp(str, "VSYNC=", strlen("VSYNC="))) { |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 126 | cur_timestamp = strtoull(str + strlen("VSYNC="), NULL, 0); |
Iliyan Malchev | eac8965 | 2012-10-04 10:38:28 -0700 | [diff] [blame] | 127 | } else { |
Saurabh Shah | ae823e7 | 2012-10-05 00:08:11 -0400 | [diff] [blame] | 128 | ALOGE ("FATAL: %s: vsync timestamp not in correct format: [%s]", |
| 129 | __FUNCTION__, |
| 130 | str); |
Iliyan Malchev | eac8965 | 2012-10-04 10:38:28 -0700 | [diff] [blame] | 131 | } |
| 132 | // send timestamp to HAL |
| 133 | ALOGD_IF (VSYNC_DEBUG, "%s: timestamp %llu sent to HWC for %s", |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 134 | __FUNCTION__, cur_timestamp, "fb0"); |
Naseer Ahmed | c7faa70 | 2012-10-04 15:10:30 -0400 | [diff] [blame] | 135 | ctx->proc->vsync(ctx->proc, dpy, cur_timestamp); |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 136 | |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 137 | } while (true); |
Naseer Ahmed | 4014777 | 2012-10-04 19:19:55 -0400 | [diff] [blame^] | 138 | if(fd_timestamp >= 0) |
| 139 | close (fd_timestamp); |
Iliyan Malchev | eac8965 | 2012-10-04 10:38:28 -0700 | [diff] [blame] | 140 | |
| 141 | return NULL; |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void init_vsync_thread(hwc_context_t* ctx) |
| 145 | { |
| 146 | pthread_t vsync_thread; |
| 147 | ALOGI("Initializing VSYNC Thread"); |
| 148 | pthread_create(&vsync_thread, NULL, vsync_loop, (void*) ctx); |
| 149 | } |
| 150 | |
| 151 | }; //namespace |