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> |
Naseer Ahmed | 8fec5c3 | 2013-06-06 17:00:15 -0400 | [diff] [blame] | 28 | #include <poll.h> |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 29 | #include "hwc_utils.h" |
| 30 | #include "string.h" |
| 31 | #include "external.h" |
| 32 | |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 33 | namespace qhwc { |
| 34 | |
Iliyan Malchev | 0f9c397 | 2012-10-11 15:16:15 -0700 | [diff] [blame] | 35 | #define HWC_VSYNC_THREAD_NAME "hwcVsyncThread" |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 36 | #define MAX_SYSFS_FILE_PATH 255 |
Iliyan Malchev | 0f9c397 | 2012-10-11 15:16:15 -0700 | [diff] [blame] | 37 | |
Naseer Ahmed | 56601cd | 2013-03-05 11:34:14 -0500 | [diff] [blame] | 38 | int hwc_vsync_control(hwc_context_t* ctx, int dpy, int enable) |
| 39 | { |
| 40 | int ret = 0; |
| 41 | if(!ctx->vstate.fakevsync && |
| 42 | ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL, |
| 43 | &enable) < 0) { |
| 44 | ALOGE("%s: vsync control failed. Dpy=%d, enable=%d : %s", |
| 45 | __FUNCTION__, dpy, enable, strerror(errno)); |
| 46 | ret = -errno; |
| 47 | } |
| 48 | return ret; |
| 49 | } |
| 50 | |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 51 | static void *vsync_loop(void *param) |
| 52 | { |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 53 | hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param); |
| 54 | |
Iliyan Malchev | 0f9c397 | 2012-10-11 15:16:15 -0700 | [diff] [blame] | 55 | char thread_name[64] = HWC_VSYNC_THREAD_NAME; |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 56 | prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0); |
| 57 | setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY + |
| 58 | android::PRIORITY_MORE_FAVORABLE); |
| 59 | |
Naseer Ahmed | 4014777 | 2012-10-04 19:19:55 -0400 | [diff] [blame] | 60 | const int MAX_DATA = 64; |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 61 | char vdata[MAX_DATA]; |
Naseer Ahmed | 56601cd | 2013-03-05 11:34:14 -0500 | [diff] [blame] | 62 | bool logvsync = false; |
Naseer Ahmed | da10c14 | 2012-11-16 20:16:31 -0500 | [diff] [blame] | 63 | |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 64 | struct pollfd pfd[2]; |
| 65 | int fb_fd[2]; |
| 66 | uint64_t timestamp[2]; |
| 67 | int num_displays; |
| 68 | |
Naseer Ahmed | da10c14 | 2012-11-16 20:16:31 -0500 | [diff] [blame] | 69 | char property[PROPERTY_VALUE_MAX]; |
| 70 | if(property_get("debug.hwc.fakevsync", property, NULL) > 0) { |
| 71 | if(atoi(property) == 1) |
Naseer Ahmed | 56601cd | 2013-03-05 11:34:14 -0500 | [diff] [blame] | 72 | ctx->vstate.fakevsync = true; |
| 73 | } |
| 74 | |
| 75 | if(property_get("debug.hwc.logvsync", property, 0) > 0) { |
| 76 | if(atoi(property) == 1) |
| 77 | logvsync = true; |
Naseer Ahmed | da10c14 | 2012-11-16 20:16:31 -0500 | [diff] [blame] | 78 | } |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 79 | |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 80 | if (ctx->mExtDisplay->getHDMIIndex() > 0) |
| 81 | num_displays = 2; |
| 82 | else |
| 83 | num_displays = 1; |
Naseer Ahmed | 8fec5c3 | 2013-06-06 17:00:15 -0400 | [diff] [blame] | 84 | |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 85 | char vsync_node_path[MAX_SYSFS_FILE_PATH]; |
| 86 | for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) { |
| 87 | snprintf(vsync_node_path, sizeof(vsync_node_path), |
| 88 | "/sys/class/graphics/fb%d/vsync_event", |
| 89 | dpy == HWC_DISPLAY_PRIMARY ? 0 : |
| 90 | ctx->mExtDisplay->getHDMIIndex()); |
| 91 | ALOGI("%s: Reading vsync for dpy=%d from %s", __FUNCTION__, dpy, |
| 92 | vsync_node_path); |
| 93 | fb_fd[dpy] = open(vsync_node_path, O_RDONLY); |
| 94 | |
| 95 | if (fb_fd[dpy] < 0) { |
| 96 | // Make sure fb device is opened before starting this thread so this |
| 97 | // never happens. |
| 98 | ALOGE ("%s:not able to open vsync node for dpy=%d, %s", |
| 99 | __FUNCTION__, dpy, strerror(errno)); |
| 100 | if (dpy == HWC_DISPLAY_PRIMARY) { |
| 101 | ctx->vstate.fakevsync = true; |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | pfd[dpy].fd = fb_fd[dpy]; |
| 107 | if (pfd[dpy].fd >= 0) |
| 108 | pfd[dpy].events = POLLPRI | POLLERR; |
Naseer Ahmed | 08212c0 | 2012-10-17 13:51:56 -0400 | [diff] [blame] | 109 | } |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 110 | |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 111 | if (LIKELY(!ctx->vstate.fakevsync)) { |
| 112 | do { |
| 113 | int err = poll(pfd, num_displays, -1); |
Naseer Ahmed | 8fec5c3 | 2013-06-06 17:00:15 -0400 | [diff] [blame] | 114 | if(err > 0) { |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 115 | for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) { |
| 116 | if (pfd[dpy].revents & POLLPRI) { |
| 117 | int len = pread(pfd[dpy].fd, vdata, MAX_DATA, 0); |
| 118 | if (UNLIKELY(len < 0)) { |
| 119 | // If the read was just interrupted - it is not a |
| 120 | // fatal error. Just continue in this case |
| 121 | ALOGE ("%s:Unable to read vsync for dpy=%d :%s", |
| 122 | __FUNCTION__, dpy, strerror(errno)); |
| 123 | continue; |
| 124 | } |
| 125 | // extract timestamp |
| 126 | if (!strncmp(vdata, "VSYNC=", strlen("VSYNC="))) { |
| 127 | timestamp[dpy] = strtoull(vdata + strlen("VSYNC="), |
| 128 | NULL, 0); |
| 129 | } |
| 130 | // send timestamp to SurfaceFlinger |
| 131 | ALOGD_IF (logvsync, |
| 132 | "%s: timestamp %llu sent to SF for dpy=%d", |
| 133 | __FUNCTION__, timestamp[dpy], dpy); |
| 134 | ctx->proc->vsync(ctx->proc, dpy, timestamp[dpy]); |
Naseer Ahmed | 8fec5c3 | 2013-06-06 17:00:15 -0400 | [diff] [blame] | 135 | } |
Naseer Ahmed | 8bb8f9d | 2013-05-11 07:29:45 -0400 | [diff] [blame] | 136 | } |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 137 | |
Naseer Ahmed | 8fec5c3 | 2013-06-06 17:00:15 -0400 | [diff] [blame] | 138 | } else { |
| 139 | ALOGE("%s: vsync poll failed errno: %s", __FUNCTION__, |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 140 | strerror(errno)); |
Naseer Ahmed | 8bb8f9d | 2013-05-11 07:29:45 -0400 | [diff] [blame] | 141 | continue; |
Naseer Ahmed | da10c14 | 2012-11-16 20:16:31 -0500 | [diff] [blame] | 142 | } |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 143 | } while (true); |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 144 | |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 145 | } else { |
| 146 | |
| 147 | //Fake vsync is used only when set explicitly through a property or when |
| 148 | //the vsync timestamp node cannot be opened at bootup. There is no |
| 149 | //fallback to fake vsync from the true vsync loop, ever, as the |
| 150 | //condition can easily escape detection. |
| 151 | //Also, fake vsync is delivered only for the primary display. |
| 152 | do { |
| 153 | usleep(16666); |
| 154 | timestamp[HWC_DISPLAY_PRIMARY] = systemTime(); |
| 155 | ctx->proc->vsync(ctx->proc, HWC_DISPLAY_PRIMARY, |
| 156 | timestamp[HWC_DISPLAY_PRIMARY]); |
| 157 | |
| 158 | } while (true); |
| 159 | } |
| 160 | |
| 161 | for (int dpy = HWC_DISPLAY_PRIMARY; dpy <= HWC_DISPLAY_EXTERNAL; dpy++ ) { |
| 162 | if(fb_fd[dpy] >= 0) |
| 163 | close (fb_fd[dpy]); |
| 164 | } |
Iliyan Malchev | eac8965 | 2012-10-04 10:38:28 -0700 | [diff] [blame] | 165 | |
| 166 | return NULL; |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void init_vsync_thread(hwc_context_t* ctx) |
| 170 | { |
Iliyan Malchev | 0f9c397 | 2012-10-11 15:16:15 -0700 | [diff] [blame] | 171 | int ret; |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 172 | pthread_t vsync_thread; |
| 173 | ALOGI("Initializing VSYNC Thread"); |
Iliyan Malchev | 0f9c397 | 2012-10-11 15:16:15 -0700 | [diff] [blame] | 174 | ret = pthread_create(&vsync_thread, NULL, vsync_loop, (void*) ctx); |
| 175 | if (ret) { |
| 176 | ALOGE("%s: failed to create %s: %s", __FUNCTION__, |
Naseer Ahmed | da10c14 | 2012-11-16 20:16:31 -0500 | [diff] [blame] | 177 | HWC_VSYNC_THREAD_NAME, strerror(ret)); |
Iliyan Malchev | 0f9c397 | 2012-10-11 15:16:15 -0700 | [diff] [blame] | 178 | } |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | }; //namespace |