Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
Naseer Ahmed | 24f2005 | 2013-02-13 11:47:25 -0500 | [diff] [blame] | 3 | * Copyright (C) 2012-2013, The Linux Foundation. All rights reserved. |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 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 | |
Naseer Ahmed | da10c14 | 2012-11-16 20:16:31 -0500 | [diff] [blame] | 21 | #include <cutils/properties.h> |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 22 | #include <utils/Log.h> |
| 23 | #include <fcntl.h> |
Naseer Ahmed | c7faa70 | 2012-10-04 15:10:30 -0400 | [diff] [blame] | 24 | #include <sys/ioctl.h> |
| 25 | #include <linux/msm_mdp.h> |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 26 | #include <sys/resource.h> |
| 27 | #include <sys/prctl.h> |
| 28 | #include "hwc_utils.h" |
| 29 | #include "string.h" |
| 30 | #include "external.h" |
| 31 | |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 32 | namespace qhwc { |
| 33 | |
Iliyan Malchev | 0f9c397 | 2012-10-11 15:16:15 -0700 | [diff] [blame] | 34 | #define HWC_VSYNC_THREAD_NAME "hwcVsyncThread" |
| 35 | |
Naseer Ahmed | 56601cd | 2013-03-05 11:34:14 -0500 | [diff] [blame^] | 36 | int 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 Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 49 | static 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 Ahmed | c7faa70 | 2012-10-04 15:10:30 -0400 | [diff] [blame] | 53 | int dpy = HWC_DISPLAY_PRIMARY; |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 54 | |
| 55 | hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param); |
| 56 | |
Iliyan Malchev | 0f9c397 | 2012-10-11 15:16:15 -0700 | [diff] [blame] | 57 | char thread_name[64] = HWC_VSYNC_THREAD_NAME; |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 58 | 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 Ahmed | 4014777 | 2012-10-04 19:19:55 -0400 | [diff] [blame] | 62 | const int MAX_DATA = 64; |
| 63 | const int MAX_RETRY_COUNT = 100; |
| 64 | static char vdata[MAX_DATA]; |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 65 | |
| 66 | uint64_t cur_timestamp=0; |
Iliyan Malchev | eac8965 | 2012-10-04 10:38:28 -0700 | [diff] [blame] | 67 | ssize_t len = -1; |
| 68 | int fd_timestamp = -1; |
Naseer Ahmed | c7faa70 | 2012-10-04 15:10:30 -0400 | [diff] [blame] | 69 | int ret = 0; |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 70 | bool fb1_vsync = false; |
Naseer Ahmed | 56601cd | 2013-03-05 11:34:14 -0500 | [diff] [blame^] | 71 | bool logvsync = false; |
Naseer Ahmed | da10c14 | 2012-11-16 20:16:31 -0500 | [diff] [blame] | 72 | |
| 73 | char property[PROPERTY_VALUE_MAX]; |
| 74 | if(property_get("debug.hwc.fakevsync", property, NULL) > 0) { |
| 75 | if(atoi(property) == 1) |
Naseer Ahmed | 56601cd | 2013-03-05 11:34:14 -0500 | [diff] [blame^] | 76 | ctx->vstate.fakevsync = true; |
| 77 | } |
| 78 | |
| 79 | if(property_get("debug.hwc.logvsync", property, 0) > 0) { |
| 80 | if(atoi(property) == 1) |
| 81 | logvsync = true; |
Naseer Ahmed | da10c14 | 2012-11-16 20:16:31 -0500 | [diff] [blame] | 82 | } |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 83 | |
| 84 | /* Currently read vsync timestamp from drivers |
| 85 | e.g. VSYNC=41800875994 |
Naseer Ahmed | da10c14 | 2012-11-16 20:16:31 -0500 | [diff] [blame] | 86 | */ |
Naseer Ahmed | 08212c0 | 2012-10-17 13:51:56 -0400 | [diff] [blame] | 87 | fd_timestamp = open(vsync_timestamp_fb0, O_RDONLY); |
| 88 | if (fd_timestamp < 0) { |
| 89 | ALOGE ("FATAL:%s:not able to open file:%s, %s", __FUNCTION__, |
| 90 | (fb1_vsync) ? vsync_timestamp_fb1 : vsync_timestamp_fb0, |
| 91 | strerror(errno)); |
Naseer Ahmed | 56601cd | 2013-03-05 11:34:14 -0500 | [diff] [blame^] | 92 | ctx->vstate.fakevsync = true; |
Naseer Ahmed | 08212c0 | 2012-10-17 13:51:56 -0400 | [diff] [blame] | 93 | } |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 94 | |
| 95 | do { |
| 96 | pthread_mutex_lock(&ctx->vstate.lock); |
Naseer Ahmed | c7faa70 | 2012-10-04 15:10:30 -0400 | [diff] [blame] | 97 | while (ctx->vstate.enable == false) { |
Iliyan Malchev | eac8965 | 2012-10-04 10:38:28 -0700 | [diff] [blame] | 98 | pthread_cond_wait(&ctx->vstate.cond, &ctx->vstate.lock); |
Naseer Ahmed | c7faa70 | 2012-10-04 15:10:30 -0400 | [diff] [blame] | 99 | } |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 100 | pthread_mutex_unlock(&ctx->vstate.lock); |
| 101 | |
Naseer Ahmed | 56601cd | 2013-03-05 11:34:14 -0500 | [diff] [blame^] | 102 | if (!ctx->vstate.fakevsync) { |
Naseer Ahmed | da10c14 | 2012-11-16 20:16:31 -0500 | [diff] [blame] | 103 | for(int i = 0; i < MAX_RETRY_COUNT; i++) { |
| 104 | len = pread(fd_timestamp, vdata, MAX_DATA, 0); |
Naseer Ahmed | 92fc130 | 2012-12-19 15:58:10 -0500 | [diff] [blame] | 105 | if(len < 0 && (errno == EAGAIN || |
| 106 | errno == EINTR || |
| 107 | errno == EBUSY)) { |
| 108 | ALOGW("%s: vsync read: %s, retry (%d/%d).", |
| 109 | __FUNCTION__, strerror(errno), i, MAX_RETRY_COUNT); |
Naseer Ahmed | da10c14 | 2012-11-16 20:16:31 -0500 | [diff] [blame] | 110 | continue; |
| 111 | } else { |
| 112 | break; |
| 113 | } |
| 114 | } |
Naseer Ahmed | 4014777 | 2012-10-04 19:19:55 -0400 | [diff] [blame] | 115 | |
Naseer Ahmed | da10c14 | 2012-11-16 20:16:31 -0500 | [diff] [blame] | 116 | if (len < 0) { |
| 117 | ALOGE ("FATAL:%s:not able to read file:%s, %s", __FUNCTION__, |
| 118 | vsync_timestamp_fb0, strerror(errno)); |
| 119 | close (fd_timestamp); |
Naseer Ahmed | 56601cd | 2013-03-05 11:34:14 -0500 | [diff] [blame^] | 120 | ctx->vstate.fakevsync = true; |
Naseer Ahmed | da10c14 | 2012-11-16 20:16:31 -0500 | [diff] [blame] | 121 | } |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 122 | |
Naseer Ahmed | da10c14 | 2012-11-16 20:16:31 -0500 | [diff] [blame] | 123 | // extract timestamp |
| 124 | const char *str = vdata; |
| 125 | if (!strncmp(str, "VSYNC=", strlen("VSYNC="))) { |
| 126 | cur_timestamp = strtoull(str + strlen("VSYNC="), NULL, 0); |
| 127 | } else { |
| 128 | ALOGE ("FATAL: %s: vsync timestamp not in correct format: [%s]", |
| 129 | __FUNCTION__, |
| 130 | str); |
Naseer Ahmed | 56601cd | 2013-03-05 11:34:14 -0500 | [diff] [blame^] | 131 | ctx->vstate.fakevsync = true; |
Naseer Ahmed | da10c14 | 2012-11-16 20:16:31 -0500 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | } else { |
| 135 | usleep(16000); |
| 136 | cur_timestamp = systemTime(); |
| 137 | } |
| 138 | // send timestamp to HAL |
Naseer Ahmed | 56601cd | 2013-03-05 11:34:14 -0500 | [diff] [blame^] | 139 | ALOGD_IF (logvsync, "%s: timestamp %llu sent to HWC for %s", |
Naseer Ahmed | da10c14 | 2012-11-16 20:16:31 -0500 | [diff] [blame] | 140 | __FUNCTION__, cur_timestamp, "fb0"); |
| 141 | ctx->proc->vsync(ctx->proc, dpy, cur_timestamp); |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 142 | |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 143 | } while (true); |
Naseer Ahmed | 4014777 | 2012-10-04 19:19:55 -0400 | [diff] [blame] | 144 | if(fd_timestamp >= 0) |
| 145 | close (fd_timestamp); |
Iliyan Malchev | eac8965 | 2012-10-04 10:38:28 -0700 | [diff] [blame] | 146 | |
| 147 | return NULL; |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void init_vsync_thread(hwc_context_t* ctx) |
| 151 | { |
Iliyan Malchev | 0f9c397 | 2012-10-11 15:16:15 -0700 | [diff] [blame] | 152 | int ret; |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 153 | pthread_t vsync_thread; |
| 154 | ALOGI("Initializing VSYNC Thread"); |
Iliyan Malchev | 0f9c397 | 2012-10-11 15:16:15 -0700 | [diff] [blame] | 155 | ret = pthread_create(&vsync_thread, NULL, vsync_loop, (void*) ctx); |
| 156 | if (ret) { |
| 157 | ALOGE("%s: failed to create %s: %s", __FUNCTION__, |
Naseer Ahmed | da10c14 | 2012-11-16 20:16:31 -0500 | [diff] [blame] | 158 | HWC_VSYNC_THREAD_NAME, strerror(ret)); |
Iliyan Malchev | 0f9c397 | 2012-10-11 15:16:15 -0700 | [diff] [blame] | 159 | } |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | }; //namespace |