blob: 7bde83bfa71ff5af7062a57d8007fdd8a8870193 [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"
30#include "string.h"
31#include "external.h"
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070032#include "overlay.h"
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -080033#define __STDC_FORMAT_MACROS 1
34#include <inttypes.h>
Naseer Ahmedff4f0252012-10-01 13:03:01 -040035
Naseer Ahmedff4f0252012-10-01 13:03:01 -040036namespace qhwc {
37
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070038#define HWC_VSYNC_THREAD_NAME "hwcVsyncThread"
Naseer Ahmede13a6002013-07-11 13:43:18 -040039#define MAX_SYSFS_FILE_PATH 255
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -040040#define PANEL_ON_STR "panel_power_on ="
41#define ARRAY_LENGTH(array) (sizeof((array))/sizeof((array)[0]))
42const int MAX_DATA = 64;
43bool logvsync = false;
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070044
Naseer Ahmed56601cd2013-03-05 11:34:14 -050045int hwc_vsync_control(hwc_context_t* ctx, int dpy, int enable)
46{
47 int ret = 0;
48 if(!ctx->vstate.fakevsync &&
49 ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL,
50 &enable) < 0) {
51 ALOGE("%s: vsync control failed. Dpy=%d, enable=%d : %s",
52 __FUNCTION__, dpy, enable, strerror(errno));
53 ret = -errno;
54 }
55 return ret;
56}
57
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -040058static void handle_vsync_event(hwc_context_t* ctx, int dpy, char *data)
59{
60 // extract timestamp
61 uint64_t timestamp = 0;
62 if (!strncmp(data, "VSYNC=", strlen("VSYNC="))) {
63 timestamp = strtoull(data + strlen("VSYNC="), NULL, 0);
64 }
65 // send timestamp to SurfaceFlinger
66 ALOGD_IF (logvsync, "%s: timestamp %"PRIu64" sent to SF for dpy=%d",
67 __FUNCTION__, timestamp, dpy);
68 ctx->proc->vsync(ctx->proc, dpy, timestamp);
69}
70
71static void handle_blank_event(hwc_context_t* ctx, int dpy, char *data)
72{
73 if (!strncmp(data, PANEL_ON_STR, strlen(PANEL_ON_STR))) {
74 uint32_t poweron = strtoul(data + strlen(PANEL_ON_STR), NULL, 0);
75 ALOGI("%s: dpy:%d panel power state: %d", __FUNCTION__, dpy, poweron);
76 ctx->dpyAttr[dpy].isActive = poweron ? true: false;
77 }
78}
79
80struct event {
81 const char* name;
82 void (*callback)(hwc_context_t* ctx, int dpy, char *data);
83};
84
85struct event event_list[] = {
86 { "vsync_event", handle_vsync_event },
87 { "show_blank_event", handle_blank_event },
88};
89
90#define num_events ARRAY_LENGTH(event_list)
91
Naseer Ahmedff4f0252012-10-01 13:03:01 -040092static void *vsync_loop(void *param)
93{
Naseer Ahmedff4f0252012-10-01 13:03:01 -040094 hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
95
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070096 char thread_name[64] = HWC_VSYNC_THREAD_NAME;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040097 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
98 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY +
99 android::PRIORITY_MORE_FAVORABLE);
100
Naseer Ahmede13a6002013-07-11 13:43:18 -0400101 char vdata[MAX_DATA];
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400102 //Number of physical displays
103 //We poll on all the nodes.
104 int num_displays = HWC_NUM_DISPLAY_TYPES - 1;
105 struct pollfd pfd[num_displays][num_events];
Naseer Ahmede13a6002013-07-11 13:43:18 -0400106
Naseer Ahmedda10c142012-11-16 20:16:31 -0500107 char property[PROPERTY_VALUE_MAX];
108 if(property_get("debug.hwc.fakevsync", property, NULL) > 0) {
109 if(atoi(property) == 1)
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500110 ctx->vstate.fakevsync = true;
111 }
112
113 if(property_get("debug.hwc.logvsync", property, 0) > 0) {
114 if(atoi(property) == 1)
115 logvsync = true;
Naseer Ahmedda10c142012-11-16 20:16:31 -0500116 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400117
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400118 char node_path[MAX_SYSFS_FILE_PATH];
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400119
Naseer Ahmede13a6002013-07-11 13:43:18 -0400120 for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) {
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400121 for(size_t ev = 0; ev < num_events; ev++) {
122 snprintf(node_path, sizeof(node_path),
123 "/sys/class/graphics/fb%d/%s",
124 dpy == HWC_DISPLAY_PRIMARY ? 0 :
125 overlay::Overlay::getInstance()->
126 getFbForDpy(HWC_DISPLAY_EXTERNAL),
127 event_list[ev].name);
Naseer Ahmede13a6002013-07-11 13:43:18 -0400128
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400129 ALOGI("%s: Reading event %zu for dpy %d from %s", __FUNCTION__,
130 ev, dpy, node_path);
131 pfd[dpy][ev].fd = open(node_path, O_RDONLY);
132
133 if (dpy == HWC_DISPLAY_PRIMARY && pfd[dpy][ev].fd < 0) {
134 // Make sure fb device is opened before starting
135 // this thread so this never happens.
136 ALOGE ("%s:unable to open event node for dpy=%d event=%zu, %s",
137 __FUNCTION__, dpy, ev, strerror(errno));
138 if (ev == 0) {
139 ctx->vstate.fakevsync = true;
140 //XXX: Blank events don't work with fake vsync,
141 //but we shouldn't be running on fake vsync anyway.
142 break;
143 }
Naseer Ahmede13a6002013-07-11 13:43:18 -0400144 }
Naseer Ahmede13a6002013-07-11 13:43:18 -0400145
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400146 pread(pfd[dpy][ev].fd, vdata , MAX_DATA, 0);
147 if (pfd[dpy][ev].fd >= 0)
148 pfd[dpy][ev].events = POLLPRI | POLLERR;
149 }
Naseer Ahmed08212c02012-10-17 13:51:56 -0400150 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400151
Naseer Ahmede13a6002013-07-11 13:43:18 -0400152 if (LIKELY(!ctx->vstate.fakevsync)) {
153 do {
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400154 int err = poll(*pfd, num_displays * num_events, -1);
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400155 if(err > 0) {
Naseer Ahmede13a6002013-07-11 13:43:18 -0400156 for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) {
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400157 for(size_t ev = 0; ev < num_events; ev++) {
158 if (pfd[dpy][ev].revents & POLLPRI) {
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530159 ssize_t len = pread(pfd[dpy][ev].fd, vdata, MAX_DATA, 0);
160 if (UNLIKELY(len < 0)) {
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400161 // If the read was just interrupted - it is not
162 // a fatal error. Just continue in this case
163 ALOGE ("%s: Unable to read event:%zu for \
164 dpy=%d : %s",
165 __FUNCTION__, ev, dpy, strerror(errno));
166 continue;
167 }
168 event_list[ev].callback(ctx, dpy, vdata);
Naseer Ahmede13a6002013-07-11 13:43:18 -0400169 }
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400170 }
Naseer Ahmed8bb8f9d2013-05-11 07:29:45 -0400171 }
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400172 } else {
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400173 ALOGE("%s: poll failed errno: %s", __FUNCTION__,
Naseer Ahmede13a6002013-07-11 13:43:18 -0400174 strerror(errno));
Naseer Ahmed8bb8f9d2013-05-11 07:29:45 -0400175 continue;
Naseer Ahmedda10c142012-11-16 20:16:31 -0500176 }
Naseer Ahmede13a6002013-07-11 13:43:18 -0400177 } while (true);
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400178
Naseer Ahmede13a6002013-07-11 13:43:18 -0400179 } else {
180
181 //Fake vsync is used only when set explicitly through a property or when
182 //the vsync timestamp node cannot be opened at bootup. There is no
183 //fallback to fake vsync from the true vsync loop, ever, as the
184 //condition can easily escape detection.
185 //Also, fake vsync is delivered only for the primary display.
186 do {
187 usleep(16666);
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400188 uint64_t timestamp = systemTime();
189 ctx->proc->vsync(ctx->proc, HWC_DISPLAY_PRIMARY, timestamp);
Naseer Ahmede13a6002013-07-11 13:43:18 -0400190
191 } while (true);
192 }
193
194 for (int dpy = HWC_DISPLAY_PRIMARY; dpy <= HWC_DISPLAY_EXTERNAL; dpy++ ) {
Naseer Ahmed3fdb2eb2014-03-10 17:20:02 -0400195 for( size_t event = 0; event < num_events; event++) {
196 if(pfd[dpy][event].fd >= 0)
197 close (pfd[dpy][event].fd);
198 }
Naseer Ahmede13a6002013-07-11 13:43:18 -0400199 }
Iliyan Malcheveac89652012-10-04 10:38:28 -0700200
201 return NULL;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400202}
203
204void init_vsync_thread(hwc_context_t* ctx)
205{
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700206 int ret;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400207 pthread_t vsync_thread;
208 ALOGI("Initializing VSYNC Thread");
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700209 ret = pthread_create(&vsync_thread, NULL, vsync_loop, (void*) ctx);
210 if (ret) {
211 ALOGE("%s: failed to create %s: %s", __FUNCTION__,
Naseer Ahmedda10c142012-11-16 20:16:31 -0500212 HWC_VSYNC_THREAD_NAME, strerror(ret));
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700213 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400214}
215
216}; //namespace