Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
Arun Kumar K.R | 2aa44c6 | 2014-01-21 23:08:28 -0800 | [diff] [blame] | 3 | * Copyright (C) 2012-2014, 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" |
Tatenda Chipeperekwa | d80b617 | 2014-09-23 11:29:37 -0700 | [diff] [blame] | 30 | #include "hdmi.h" |
Tatenda Chipeperekwa | ce8d5c4 | 2014-08-13 10:53:49 -0700 | [diff] [blame] | 31 | #include "qd_utils.h" |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 32 | #include "string.h" |
Jeykumar Sankaran | 27dee26 | 2013-08-01 17:09:54 -0700 | [diff] [blame] | 33 | #include "overlay.h" |
Arun Kumar K.R | 2aa44c6 | 2014-01-21 23:08:28 -0800 | [diff] [blame] | 34 | #define __STDC_FORMAT_MACROS 1 |
| 35 | #include <inttypes.h> |
Naseer Ahmed | 251c030 | 2014-10-10 12:53:13 -0400 | [diff] [blame] | 36 | #define DEBUG 0 |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 37 | |
Tatenda Chipeperekwa | ce8d5c4 | 2014-08-13 10:53:49 -0700 | [diff] [blame] | 38 | using namespace qdutils; |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 39 | namespace qhwc { |
| 40 | |
Iliyan Malchev | 0f9c397 | 2012-10-11 15:16:15 -0700 | [diff] [blame] | 41 | #define HWC_VSYNC_THREAD_NAME "hwcVsyncThread" |
Naseer Ahmed | 3fdb2eb | 2014-03-10 17:20:02 -0400 | [diff] [blame] | 42 | #define PANEL_ON_STR "panel_power_on =" |
| 43 | #define ARRAY_LENGTH(array) (sizeof((array))/sizeof((array)[0])) |
Dileep Kumar Reddi | a25a918 | 2014-07-24 20:01:44 +0530 | [diff] [blame] | 44 | #define MAX_THERMAL_LEVEL 3 |
Naseer Ahmed | 3fdb2eb | 2014-03-10 17:20:02 -0400 | [diff] [blame] | 45 | const int MAX_DATA = 64; |
Iliyan Malchev | 0f9c397 | 2012-10-11 15:16:15 -0700 | [diff] [blame] | 46 | |
Naseer Ahmed | 56601cd | 2013-03-05 11:34:14 -0500 | [diff] [blame] | 47 | int hwc_vsync_control(hwc_context_t* ctx, int dpy, int enable) |
| 48 | { |
| 49 | int ret = 0; |
| 50 | if(!ctx->vstate.fakevsync && |
| 51 | ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL, |
| 52 | &enable) < 0) { |
| 53 | ALOGE("%s: vsync control failed. Dpy=%d, enable=%d : %s", |
| 54 | __FUNCTION__, dpy, enable, strerror(errno)); |
| 55 | ret = -errno; |
| 56 | } |
| 57 | return ret; |
| 58 | } |
| 59 | |
Naseer Ahmed | 7a7b66d | 2014-07-23 17:56:26 -0400 | [diff] [blame] | 60 | static void handle_vsync_event(hwc_context_t* ctx, int dpy, char *data, |
| 61 | ssize_t len __unused) |
Naseer Ahmed | 3fdb2eb | 2014-03-10 17:20:02 -0400 | [diff] [blame] | 62 | { |
| 63 | // extract timestamp |
| 64 | uint64_t timestamp = 0; |
| 65 | if (!strncmp(data, "VSYNC=", strlen("VSYNC="))) { |
| 66 | timestamp = strtoull(data + strlen("VSYNC="), NULL, 0); |
| 67 | } |
| 68 | // send timestamp to SurfaceFlinger |
Naseer Ahmed | 35a268c | 2014-06-24 19:07:13 -0400 | [diff] [blame] | 69 | ALOGD_IF (ctx->vstate.debug, "%s: timestamp %"PRIu64" sent to SF for dpy=%d", |
Naseer Ahmed | 3fdb2eb | 2014-03-10 17:20:02 -0400 | [diff] [blame] | 70 | __FUNCTION__, timestamp, dpy); |
| 71 | ctx->proc->vsync(ctx->proc, dpy, timestamp); |
| 72 | } |
| 73 | |
Naseer Ahmed | 7a7b66d | 2014-07-23 17:56:26 -0400 | [diff] [blame] | 74 | static void handle_blank_event(hwc_context_t* ctx, int dpy, char *data, |
| 75 | ssize_t len __unused) |
Naseer Ahmed | 3fdb2eb | 2014-03-10 17:20:02 -0400 | [diff] [blame] | 76 | { |
| 77 | if (!strncmp(data, PANEL_ON_STR, strlen(PANEL_ON_STR))) { |
Shalaj Jain | a70b435 | 2014-06-15 13:47:47 -0700 | [diff] [blame] | 78 | unsigned long int poweron = strtoul(data + strlen(PANEL_ON_STR), NULL, 0); |
| 79 | ALOGI("%s: dpy:%d panel power state: %ld", __FUNCTION__, dpy, poweron); |
Tatenda Chipeperekwa | d80b617 | 2014-09-23 11:29:37 -0700 | [diff] [blame] | 80 | if (!ctx->mHDMIDisplay->isHDMIPrimaryDisplay()) { |
| 81 | ctx->dpyAttr[dpy].isActive = poweron ? true: false; |
| 82 | } |
Naseer Ahmed | 3fdb2eb | 2014-03-10 17:20:02 -0400 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Naseer Ahmed | 7a7b66d | 2014-07-23 17:56:26 -0400 | [diff] [blame] | 86 | static void handle_thermal_event(hwc_context_t* ctx, int dpy, char *data, |
| 87 | ssize_t len __unused) |
Dileep Kumar Reddi | a25a918 | 2014-07-24 20:01:44 +0530 | [diff] [blame] | 88 | { |
| 89 | // extract thermal level |
| 90 | uint64_t thermalLevel = 0; |
| 91 | if (!strncmp(data, "thermal_level=", strlen("thermal_level="))) { |
| 92 | thermalLevel = strtoull(data + strlen("thermal_level="), NULL, 0); |
| 93 | } |
| 94 | |
Ramakant Singh | 8595cca | 2014-11-06 16:17:08 +0530 | [diff] [blame] | 95 | if (thermalLevel >= MAX_THERMAL_LEVEL) { |
| 96 | ALOGD("%s: dpy:%d thermal_level=%"PRIu64"",__FUNCTION__,dpy,thermalLevel); |
Dileep Kumar Reddi | a25a918 | 2014-07-24 20:01:44 +0530 | [diff] [blame] | 97 | ctx->mThermalBurstMode = true; |
Ramakant Singh | 8595cca | 2014-11-06 16:17:08 +0530 | [diff] [blame] | 98 | } else |
Dileep Kumar Reddi | a25a918 | 2014-07-24 20:01:44 +0530 | [diff] [blame] | 99 | ctx->mThermalBurstMode = false; |
| 100 | } |
| 101 | |
Naseer Ahmed | 7a7b66d | 2014-07-23 17:56:26 -0400 | [diff] [blame] | 102 | static void handle_cec_event(hwc_context_t* ctx, int dpy, char *data, |
| 103 | ssize_t len) |
| 104 | { |
Naseer Ahmed | 251c030 | 2014-10-10 12:53:13 -0400 | [diff] [blame] | 105 | ALOGD_IF(DEBUG, "%s: Got CEC event from driver dpy:%d", |
| 106 | __FUNCTION__, dpy); |
Naseer Ahmed | 7a7b66d | 2014-07-23 17:56:26 -0400 | [diff] [blame] | 107 | ctx->mQService->onCECMessageReceived(data, len); |
| 108 | } |
| 109 | |
Naseer Ahmed | 3fdb2eb | 2014-03-10 17:20:02 -0400 | [diff] [blame] | 110 | struct event { |
| 111 | const char* name; |
Naseer Ahmed | 7a7b66d | 2014-07-23 17:56:26 -0400 | [diff] [blame] | 112 | void (*callback)(hwc_context_t* ctx, int dpy, char *data, ssize_t len); |
Naseer Ahmed | 3fdb2eb | 2014-03-10 17:20:02 -0400 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | struct event event_list[] = { |
| 116 | { "vsync_event", handle_vsync_event }, |
| 117 | { "show_blank_event", handle_blank_event }, |
Dileep Kumar Reddi | a25a918 | 2014-07-24 20:01:44 +0530 | [diff] [blame] | 118 | { "msm_fb_thermal_level", handle_thermal_event }, |
Naseer Ahmed | 7a7b66d | 2014-07-23 17:56:26 -0400 | [diff] [blame] | 119 | { "cec/rd_msg", handle_cec_event }, |
Naseer Ahmed | 3fdb2eb | 2014-03-10 17:20:02 -0400 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | #define num_events ARRAY_LENGTH(event_list) |
| 123 | |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 124 | static void *vsync_loop(void *param) |
| 125 | { |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 126 | hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param); |
| 127 | |
Iliyan Malchev | 0f9c397 | 2012-10-11 15:16:15 -0700 | [diff] [blame] | 128 | char thread_name[64] = HWC_VSYNC_THREAD_NAME; |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 129 | prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0); |
| 130 | setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY + |
| 131 | android::PRIORITY_MORE_FAVORABLE); |
| 132 | |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 133 | char vdata[MAX_DATA]; |
Naseer Ahmed | 3fdb2eb | 2014-03-10 17:20:02 -0400 | [diff] [blame] | 134 | //Number of physical displays |
| 135 | //We poll on all the nodes. |
| 136 | int num_displays = HWC_NUM_DISPLAY_TYPES - 1; |
| 137 | struct pollfd pfd[num_displays][num_events]; |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 138 | |
Naseer Ahmed | da10c14 | 2012-11-16 20:16:31 -0500 | [diff] [blame] | 139 | char property[PROPERTY_VALUE_MAX]; |
| 140 | if(property_get("debug.hwc.fakevsync", property, NULL) > 0) { |
| 141 | if(atoi(property) == 1) |
Naseer Ahmed | 56601cd | 2013-03-05 11:34:14 -0500 | [diff] [blame] | 142 | ctx->vstate.fakevsync = true; |
| 143 | } |
| 144 | |
Naseer Ahmed | 3fdb2eb | 2014-03-10 17:20:02 -0400 | [diff] [blame] | 145 | char node_path[MAX_SYSFS_FILE_PATH]; |
Naseer Ahmed | 8fec5c3 | 2013-06-06 17:00:15 -0400 | [diff] [blame] | 146 | |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 147 | for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) { |
Naseer Ahmed | 3fdb2eb | 2014-03-10 17:20:02 -0400 | [diff] [blame] | 148 | for(size_t ev = 0; ev < num_events; ev++) { |
| 149 | snprintf(node_path, sizeof(node_path), |
| 150 | "/sys/class/graphics/fb%d/%s", |
| 151 | dpy == HWC_DISPLAY_PRIMARY ? 0 : |
| 152 | overlay::Overlay::getInstance()-> |
| 153 | getFbForDpy(HWC_DISPLAY_EXTERNAL), |
| 154 | event_list[ev].name); |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 155 | |
Naseer Ahmed | 3fdb2eb | 2014-03-10 17:20:02 -0400 | [diff] [blame] | 156 | ALOGI("%s: Reading event %zu for dpy %d from %s", __FUNCTION__, |
| 157 | ev, dpy, node_path); |
| 158 | pfd[dpy][ev].fd = open(node_path, O_RDONLY); |
| 159 | |
| 160 | if (dpy == HWC_DISPLAY_PRIMARY && pfd[dpy][ev].fd < 0) { |
| 161 | // Make sure fb device is opened before starting |
| 162 | // this thread so this never happens. |
| 163 | ALOGE ("%s:unable to open event node for dpy=%d event=%zu, %s", |
| 164 | __FUNCTION__, dpy, ev, strerror(errno)); |
| 165 | if (ev == 0) { |
| 166 | ctx->vstate.fakevsync = true; |
| 167 | //XXX: Blank events don't work with fake vsync, |
| 168 | //but we shouldn't be running on fake vsync anyway. |
| 169 | break; |
| 170 | } |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 171 | } |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 172 | |
Praveena Pachipulusu | 6ddb0de | 2015-01-30 13:45:13 +0530 | [diff] [blame] | 173 | memset(&vdata, '\0', sizeof(vdata)); |
| 174 | // Read once from the fds to clear the first notify |
| 175 | pread(pfd[dpy][ev].fd, vdata , MAX_DATA - 1, 0); |
Naseer Ahmed | 3fdb2eb | 2014-03-10 17:20:02 -0400 | [diff] [blame] | 176 | if (pfd[dpy][ev].fd >= 0) |
| 177 | pfd[dpy][ev].events = POLLPRI | POLLERR; |
| 178 | } |
Naseer Ahmed | 08212c0 | 2012-10-17 13:51:56 -0400 | [diff] [blame] | 179 | } |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 180 | |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 181 | if (LIKELY(!ctx->vstate.fakevsync)) { |
| 182 | do { |
Shalaj Jain | a70b435 | 2014-06-15 13:47:47 -0700 | [diff] [blame] | 183 | int err = poll(*pfd, (int)(num_displays * num_events), -1); |
Naseer Ahmed | 8fec5c3 | 2013-06-06 17:00:15 -0400 | [diff] [blame] | 184 | if(err > 0) { |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 185 | for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) { |
Naseer Ahmed | 3fdb2eb | 2014-03-10 17:20:02 -0400 | [diff] [blame] | 186 | for(size_t ev = 0; ev < num_events; ev++) { |
| 187 | if (pfd[dpy][ev].revents & POLLPRI) { |
Praveena Pachipulusu | 6ddb0de | 2015-01-30 13:45:13 +0530 | [diff] [blame] | 188 | // Clear vdata before writing into it |
| 189 | memset(&vdata, '\0', sizeof(vdata)); |
| 190 | ssize_t len = pread(pfd[dpy][ev].fd, vdata, |
| 191 | MAX_DATA - 1, 0); |
Praveena Pachipulusu | d9443c7 | 2014-02-17 10:42:28 +0530 | [diff] [blame] | 192 | if (UNLIKELY(len < 0)) { |
Naseer Ahmed | 3fdb2eb | 2014-03-10 17:20:02 -0400 | [diff] [blame] | 193 | // If the read was just interrupted - it is not |
| 194 | // a fatal error. Just continue in this case |
| 195 | ALOGE ("%s: Unable to read event:%zu for \ |
| 196 | dpy=%d : %s", |
| 197 | __FUNCTION__, ev, dpy, strerror(errno)); |
| 198 | continue; |
| 199 | } |
Praveena Pachipulusu | 6ddb0de | 2015-01-30 13:45:13 +0530 | [diff] [blame] | 200 | vdata[len] = '\0'; |
Naseer Ahmed | 7a7b66d | 2014-07-23 17:56:26 -0400 | [diff] [blame] | 201 | event_list[ev].callback(ctx, dpy, vdata, len); |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 202 | } |
Naseer Ahmed | 8fec5c3 | 2013-06-06 17:00:15 -0400 | [diff] [blame] | 203 | } |
Naseer Ahmed | 8bb8f9d | 2013-05-11 07:29:45 -0400 | [diff] [blame] | 204 | } |
Naseer Ahmed | 8fec5c3 | 2013-06-06 17:00:15 -0400 | [diff] [blame] | 205 | } else { |
Naseer Ahmed | 3fdb2eb | 2014-03-10 17:20:02 -0400 | [diff] [blame] | 206 | ALOGE("%s: poll failed errno: %s", __FUNCTION__, |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 207 | strerror(errno)); |
Naseer Ahmed | 8bb8f9d | 2013-05-11 07:29:45 -0400 | [diff] [blame] | 208 | continue; |
Naseer Ahmed | da10c14 | 2012-11-16 20:16:31 -0500 | [diff] [blame] | 209 | } |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 210 | } while (true); |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 211 | |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 212 | } else { |
| 213 | |
| 214 | //Fake vsync is used only when set explicitly through a property or when |
| 215 | //the vsync timestamp node cannot be opened at bootup. There is no |
| 216 | //fallback to fake vsync from the true vsync loop, ever, as the |
| 217 | //condition can easily escape detection. |
| 218 | //Also, fake vsync is delivered only for the primary display. |
| 219 | do { |
| 220 | usleep(16666); |
Naseer Ahmed | 3fdb2eb | 2014-03-10 17:20:02 -0400 | [diff] [blame] | 221 | uint64_t timestamp = systemTime(); |
| 222 | ctx->proc->vsync(ctx->proc, HWC_DISPLAY_PRIMARY, timestamp); |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 223 | |
| 224 | } while (true); |
| 225 | } |
| 226 | |
| 227 | for (int dpy = HWC_DISPLAY_PRIMARY; dpy <= HWC_DISPLAY_EXTERNAL; dpy++ ) { |
Naseer Ahmed | 3fdb2eb | 2014-03-10 17:20:02 -0400 | [diff] [blame] | 228 | for( size_t event = 0; event < num_events; event++) { |
| 229 | if(pfd[dpy][event].fd >= 0) |
| 230 | close (pfd[dpy][event].fd); |
| 231 | } |
Naseer Ahmed | e13a600 | 2013-07-11 13:43:18 -0400 | [diff] [blame] | 232 | } |
Iliyan Malchev | eac8965 | 2012-10-04 10:38:28 -0700 | [diff] [blame] | 233 | |
| 234 | return NULL; |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void init_vsync_thread(hwc_context_t* ctx) |
| 238 | { |
Iliyan Malchev | 0f9c397 | 2012-10-11 15:16:15 -0700 | [diff] [blame] | 239 | int ret; |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 240 | pthread_t vsync_thread; |
| 241 | ALOGI("Initializing VSYNC Thread"); |
Iliyan Malchev | 0f9c397 | 2012-10-11 15:16:15 -0700 | [diff] [blame] | 242 | ret = pthread_create(&vsync_thread, NULL, vsync_loop, (void*) ctx); |
| 243 | if (ret) { |
| 244 | ALOGE("%s: failed to create %s: %s", __FUNCTION__, |
Naseer Ahmed | da10c14 | 2012-11-16 20:16:31 -0500 | [diff] [blame] | 245 | HWC_VSYNC_THREAD_NAME, strerror(ret)); |
Iliyan Malchev | 0f9c397 | 2012-10-11 15:16:15 -0700 | [diff] [blame] | 246 | } |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | }; //namespace |