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