blob: d9dc27c72bca02b5d88dcd8b7250e4579faae357 [file] [log] [blame]
Naseer Ahmedff4f0252012-10-01 13:03:01 -04001/*
2 * Copyright (C) 2010 The Android Open Source Project
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -08003 * Copyright (C) 2012-2014, The Linux Foundation. All rights reserved.
Naseer Ahmedff4f0252012-10-01 13:03:01 -04004 *
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 Ahmedda10c142012-11-16 20:16:31 -050021#include <cutils/properties.h>
Naseer Ahmedff4f0252012-10-01 13:03:01 -040022#include <utils/Log.h>
23#include <fcntl.h>
Naseer Ahmedc7faa702012-10-04 15:10:30 -040024#include <sys/ioctl.h>
25#include <linux/msm_mdp.h>
Naseer Ahmedff4f0252012-10-01 13:03:01 -040026#include <sys/resource.h>
27#include <sys/prctl.h>
Naseer Ahmed8fec5c32013-06-06 17:00:15 -040028#include <poll.h>
Naseer Ahmedff4f0252012-10-01 13:03:01 -040029#include "hwc_utils.h"
Tatenda Chipeperekwad80b6172014-09-23 11:29:37 -070030#include "hdmi.h"
Tatenda Chipeperekwace8d5c42014-08-13 10:53:49 -070031#include "qd_utils.h"
Naseer Ahmedff4f0252012-10-01 13:03:01 -040032#include "string.h"
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070033#include "overlay.h"
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -080034#define __STDC_FORMAT_MACROS 1
35#include <inttypes.h>
Naseer Ahmedff4f0252012-10-01 13:03:01 -040036
Tatenda Chipeperekwace8d5c42014-08-13 10:53:49 -070037using namespace qdutils;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040038namespace qhwc {
39
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070040#define HWC_VSYNC_THREAD_NAME "hwcVsyncThread"
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -040041#define PANEL_ON_STR "panel_power_on ="
42#define ARRAY_LENGTH(array) (sizeof((array))/sizeof((array)[0]))
Dileep Kumar Reddia25a9182014-07-24 20:01:44 +053043#define MAX_THERMAL_LEVEL 3
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -040044const int MAX_DATA = 64;
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070045
Naseer Ahmed56601cd2013-03-05 11:34:14 -050046int hwc_vsync_control(hwc_context_t* ctx, int dpy, int enable)
47{
48 int ret = 0;
49 if(!ctx->vstate.fakevsync &&
50 ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL,
51 &enable) < 0) {
52 ALOGE("%s: vsync control failed. Dpy=%d, enable=%d : %s",
53 __FUNCTION__, dpy, enable, strerror(errno));
54 ret = -errno;
55 }
56 return ret;
57}
58
Naseer Ahmed7a7b66d2014-07-23 17:56:26 -040059static void handle_vsync_event(hwc_context_t* ctx, int dpy, char *data,
60 ssize_t len __unused)
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -040061{
62 // extract timestamp
63 uint64_t timestamp = 0;
64 if (!strncmp(data, "VSYNC=", strlen("VSYNC="))) {
65 timestamp = strtoull(data + strlen("VSYNC="), NULL, 0);
66 }
67 // send timestamp to SurfaceFlinger
Naseer Ahmed35a268c2014-06-24 19:07:13 -040068 ALOGD_IF (ctx->vstate.debug, "%s: timestamp %"PRIu64" sent to SF for dpy=%d",
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -040069 __FUNCTION__, timestamp, dpy);
70 ctx->proc->vsync(ctx->proc, dpy, timestamp);
71}
72
Naseer Ahmed7a7b66d2014-07-23 17:56:26 -040073static void handle_blank_event(hwc_context_t* ctx, int dpy, char *data,
74 ssize_t len __unused)
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -040075{
76 if (!strncmp(data, PANEL_ON_STR, strlen(PANEL_ON_STR))) {
Shalaj Jaina70b4352014-06-15 13:47:47 -070077 unsigned long int poweron = strtoul(data + strlen(PANEL_ON_STR), NULL, 0);
78 ALOGI("%s: dpy:%d panel power state: %ld", __FUNCTION__, dpy, poweron);
Tatenda Chipeperekwad80b6172014-09-23 11:29:37 -070079 if (!ctx->mHDMIDisplay->isHDMIPrimaryDisplay()) {
80 ctx->dpyAttr[dpy].isActive = poweron ? true: false;
81 }
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -040082 }
83}
84
Naseer Ahmed7a7b66d2014-07-23 17:56:26 -040085static void handle_thermal_event(hwc_context_t* ctx, int dpy, char *data,
86 ssize_t len __unused)
Dileep Kumar Reddia25a9182014-07-24 20:01:44 +053087{
88 // extract thermal level
89 uint64_t thermalLevel = 0;
90 if (!strncmp(data, "thermal_level=", strlen("thermal_level="))) {
91 thermalLevel = strtoull(data + strlen("thermal_level="), NULL, 0);
92 }
93
Ramakant Singh8595cca2014-11-06 16:17:08 +053094 if (thermalLevel >= MAX_THERMAL_LEVEL) {
95 ALOGD("%s: dpy:%d thermal_level=%"PRIu64"",__FUNCTION__,dpy,thermalLevel);
Dileep Kumar Reddia25a9182014-07-24 20:01:44 +053096 ctx->mThermalBurstMode = true;
Ramakant Singh8595cca2014-11-06 16:17:08 +053097 } else
Dileep Kumar Reddia25a9182014-07-24 20:01:44 +053098 ctx->mThermalBurstMode = false;
99}
100
Naseer Ahmed7a7b66d2014-07-23 17:56:26 -0400101static void handle_cec_event(hwc_context_t* ctx, int dpy, char *data,
102 ssize_t len)
103{
104 ALOGD("%s: Got CEC event from driver dpy:%d", __FUNCTION__, dpy);
105 ctx->mQService->onCECMessageReceived(data, len);
106}
107
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400108struct event {
109 const char* name;
Naseer Ahmed7a7b66d2014-07-23 17:56:26 -0400110 void (*callback)(hwc_context_t* ctx, int dpy, char *data, ssize_t len);
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400111};
112
113struct event event_list[] = {
114 { "vsync_event", handle_vsync_event },
115 { "show_blank_event", handle_blank_event },
Dileep Kumar Reddia25a9182014-07-24 20:01:44 +0530116 { "msm_fb_thermal_level", handle_thermal_event },
Naseer Ahmed7a7b66d2014-07-23 17:56:26 -0400117 { "cec/rd_msg", handle_cec_event },
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400118};
119
120#define num_events ARRAY_LENGTH(event_list)
121
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400122static void *vsync_loop(void *param)
123{
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400124 hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
125
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700126 char thread_name[64] = HWC_VSYNC_THREAD_NAME;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400127 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
128 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY +
129 android::PRIORITY_MORE_FAVORABLE);
130
Naseer Ahmede13a6002013-07-11 13:43:18 -0400131 char vdata[MAX_DATA];
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400132 //Number of physical displays
133 //We poll on all the nodes.
134 int num_displays = HWC_NUM_DISPLAY_TYPES - 1;
135 struct pollfd pfd[num_displays][num_events];
Naseer Ahmede13a6002013-07-11 13:43:18 -0400136
Naseer Ahmedda10c142012-11-16 20:16:31 -0500137 char property[PROPERTY_VALUE_MAX];
138 if(property_get("debug.hwc.fakevsync", property, NULL) > 0) {
139 if(atoi(property) == 1)
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500140 ctx->vstate.fakevsync = true;
141 }
142
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400143 char node_path[MAX_SYSFS_FILE_PATH];
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400144
Naseer Ahmede13a6002013-07-11 13:43:18 -0400145 for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) {
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400146 for(size_t ev = 0; ev < num_events; ev++) {
147 snprintf(node_path, sizeof(node_path),
148 "/sys/class/graphics/fb%d/%s",
149 dpy == HWC_DISPLAY_PRIMARY ? 0 :
150 overlay::Overlay::getInstance()->
151 getFbForDpy(HWC_DISPLAY_EXTERNAL),
152 event_list[ev].name);
Naseer Ahmede13a6002013-07-11 13:43:18 -0400153
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400154 ALOGI("%s: Reading event %zu for dpy %d from %s", __FUNCTION__,
155 ev, dpy, node_path);
156 pfd[dpy][ev].fd = open(node_path, O_RDONLY);
157
158 if (dpy == HWC_DISPLAY_PRIMARY && pfd[dpy][ev].fd < 0) {
159 // Make sure fb device is opened before starting
160 // this thread so this never happens.
161 ALOGE ("%s:unable to open event node for dpy=%d event=%zu, %s",
162 __FUNCTION__, dpy, ev, strerror(errno));
163 if (ev == 0) {
164 ctx->vstate.fakevsync = true;
165 //XXX: Blank events don't work with fake vsync,
166 //but we shouldn't be running on fake vsync anyway.
167 break;
168 }
Naseer Ahmede13a6002013-07-11 13:43:18 -0400169 }
Naseer Ahmede13a6002013-07-11 13:43:18 -0400170
Praveena Pachipulusu6ddb0de2015-01-30 13:45:13 +0530171 memset(&vdata, '\0', sizeof(vdata));
172 // Read once from the fds to clear the first notify
173 pread(pfd[dpy][ev].fd, vdata , MAX_DATA - 1, 0);
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400174 if (pfd[dpy][ev].fd >= 0)
175 pfd[dpy][ev].events = POLLPRI | POLLERR;
176 }
Naseer Ahmed08212c02012-10-17 13:51:56 -0400177 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400178
Naseer Ahmede13a6002013-07-11 13:43:18 -0400179 if (LIKELY(!ctx->vstate.fakevsync)) {
180 do {
Shalaj Jaina70b4352014-06-15 13:47:47 -0700181 int err = poll(*pfd, (int)(num_displays * num_events), -1);
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400182 if(err > 0) {
Naseer Ahmede13a6002013-07-11 13:43:18 -0400183 for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) {
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400184 for(size_t ev = 0; ev < num_events; ev++) {
185 if (pfd[dpy][ev].revents & POLLPRI) {
Praveena Pachipulusu6ddb0de2015-01-30 13:45:13 +0530186 // Clear vdata before writing into it
187 memset(&vdata, '\0', sizeof(vdata));
188 ssize_t len = pread(pfd[dpy][ev].fd, vdata,
189 MAX_DATA - 1, 0);
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530190 if (UNLIKELY(len < 0)) {
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400191 // If the read was just interrupted - it is not
192 // a fatal error. Just continue in this case
193 ALOGE ("%s: Unable to read event:%zu for \
194 dpy=%d : %s",
195 __FUNCTION__, ev, dpy, strerror(errno));
196 continue;
197 }
Praveena Pachipulusu6ddb0de2015-01-30 13:45:13 +0530198 vdata[len] = '\0';
Naseer Ahmed7a7b66d2014-07-23 17:56:26 -0400199 event_list[ev].callback(ctx, dpy, vdata, len);
Naseer Ahmede13a6002013-07-11 13:43:18 -0400200 }
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400201 }
Naseer Ahmed8bb8f9d2013-05-11 07:29:45 -0400202 }
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400203 } else {
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400204 ALOGE("%s: poll failed errno: %s", __FUNCTION__,
Naseer Ahmede13a6002013-07-11 13:43:18 -0400205 strerror(errno));
Naseer Ahmed8bb8f9d2013-05-11 07:29:45 -0400206 continue;
Naseer Ahmedda10c142012-11-16 20:16:31 -0500207 }
Naseer Ahmede13a6002013-07-11 13:43:18 -0400208 } while (true);
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400209
Naseer Ahmede13a6002013-07-11 13:43:18 -0400210 } else {
211
212 //Fake vsync is used only when set explicitly through a property or when
213 //the vsync timestamp node cannot be opened at bootup. There is no
214 //fallback to fake vsync from the true vsync loop, ever, as the
215 //condition can easily escape detection.
216 //Also, fake vsync is delivered only for the primary display.
217 do {
218 usleep(16666);
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400219 uint64_t timestamp = systemTime();
220 ctx->proc->vsync(ctx->proc, HWC_DISPLAY_PRIMARY, timestamp);
Naseer Ahmede13a6002013-07-11 13:43:18 -0400221
222 } while (true);
223 }
224
225 for (int dpy = HWC_DISPLAY_PRIMARY; dpy <= HWC_DISPLAY_EXTERNAL; dpy++ ) {
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400226 for( size_t event = 0; event < num_events; event++) {
227 if(pfd[dpy][event].fd >= 0)
228 close (pfd[dpy][event].fd);
229 }
Naseer Ahmede13a6002013-07-11 13:43:18 -0400230 }
Iliyan Malcheveac89652012-10-04 10:38:28 -0700231
232 return NULL;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400233}
234
235void init_vsync_thread(hwc_context_t* ctx)
236{
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700237 int ret;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400238 pthread_t vsync_thread;
239 ALOGI("Initializing VSYNC Thread");
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700240 ret = pthread_create(&vsync_thread, NULL, vsync_loop, (void*) ctx);
241 if (ret) {
242 ALOGE("%s: failed to create %s: %s", __FUNCTION__,
Naseer Ahmedda10c142012-11-16 20:16:31 -0500243 HWC_VSYNC_THREAD_NAME, strerror(ret));
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700244 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400245}
246
247}; //namespace