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