blob: 816619217ffc73661cf29bce9060710fe257b6bc [file] [log] [blame]
Simon Wilsonef53c1c2014-04-09 11:15:13 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Simon Wilsona653efe2014-04-09 11:15:26 -07003 * Copyright (C) 2012-2014, The Linux Foundation. All rights reserved.
Simon Wilsonef53c1c2014-04-09 11:15:13 -07004 *
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
21#include <cutils/properties.h>
22#include <utils/Log.h>
23#include <fcntl.h>
24#include <sys/ioctl.h>
25#include <linux/msm_mdp.h>
26#include <sys/resource.h>
27#include <sys/prctl.h>
28#include <poll.h>
29#include "hwc_utils.h"
30#include "string.h"
31#include "external.h"
Simon Wilsona653efe2014-04-09 11:15:26 -070032#include "overlay.h"
Simon Wilsona653efe2014-04-09 11:15:26 -070033#include <inttypes.h>
Simon Wilsonef53c1c2014-04-09 11:15:13 -070034
35namespace qhwc {
36
37#define HWC_VSYNC_THREAD_NAME "hwcVsyncThread"
38#define MAX_SYSFS_FILE_PATH 255
Naseer Ahmedfafce762014-03-10 17:20:02 -040039#define PANEL_ON_STR "panel_power_on ="
40#define ARRAY_LENGTH(array) (sizeof((array))/sizeof((array)[0]))
41const int MAX_DATA = 64;
Simon Wilsonef53c1c2014-04-09 11:15:13 -070042
43int hwc_vsync_control(hwc_context_t* ctx, int dpy, int enable)
44{
45 int ret = 0;
46 if(!ctx->vstate.fakevsync &&
47 ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL,
48 &enable) < 0) {
49 ALOGE("%s: vsync control failed. Dpy=%d, enable=%d : %s",
50 __FUNCTION__, dpy, enable, strerror(errno));
51 ret = -errno;
52 }
53 return ret;
54}
55
Naseer Ahmedfafce762014-03-10 17:20:02 -040056static void handle_vsync_event(hwc_context_t* ctx, int dpy, char *data)
57{
58 // extract timestamp
59 uint64_t timestamp = 0;
60 if (!strncmp(data, "VSYNC=", strlen("VSYNC="))) {
61 timestamp = strtoull(data + strlen("VSYNC="), NULL, 0);
62 }
63 // send timestamp to SurfaceFlinger
Prashant Malanicde4bf92016-03-03 23:24:36 -080064 ALOGD_IF (ctx->vstate.debug, "%s: timestamp %" PRIu64 " sent to SF for dpy=%d",
Naseer Ahmedfafce762014-03-10 17:20:02 -040065 __FUNCTION__, timestamp, dpy);
66 ctx->proc->vsync(ctx->proc, dpy, timestamp);
67}
68
69static void handle_blank_event(hwc_context_t* ctx, int dpy, char *data)
70{
71 if (!strncmp(data, PANEL_ON_STR, strlen(PANEL_ON_STR))) {
Shalaj Jaincfb96882014-06-15 13:47:47 -070072 unsigned long int poweron = strtoul(data + strlen(PANEL_ON_STR), NULL, 0);
73 ALOGI("%s: dpy:%d panel power state: %ld", __FUNCTION__, dpy, poweron);
Naseer Ahmedfafce762014-03-10 17:20:02 -040074 ctx->dpyAttr[dpy].isActive = poweron ? true: false;
75 }
76}
77
78struct event {
79 const char* name;
80 void (*callback)(hwc_context_t* ctx, int dpy, char *data);
81};
82
83struct event event_list[] = {
84 { "vsync_event", handle_vsync_event },
85 { "show_blank_event", handle_blank_event },
86};
87
88#define num_events ARRAY_LENGTH(event_list)
Naseer Ahmedced6b792014-06-04 21:19:41 -040089#define VSYNC_FAILURE_THRESHOLD 2
Naseer Ahmedfafce762014-03-10 17:20:02 -040090
Simon Wilsonef53c1c2014-04-09 11:15:13 -070091static void *vsync_loop(void *param)
92{
93 hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
94
95 char thread_name[64] = HWC_VSYNC_THREAD_NAME;
96 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
97 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY +
98 android::PRIORITY_MORE_FAVORABLE);
99
Simon Wilsonef53c1c2014-04-09 11:15:13 -0700100 char vdata[MAX_DATA];
Naseer Ahmedfafce762014-03-10 17:20:02 -0400101 //Number of physical displays
102 //We poll on all the nodes.
103 int num_displays = HWC_NUM_DISPLAY_TYPES - 1;
104 struct pollfd pfd[num_displays][num_events];
Simon Wilsonef53c1c2014-04-09 11:15:13 -0700105
106 char property[PROPERTY_VALUE_MAX];
107 if(property_get("debug.hwc.fakevsync", property, NULL) > 0) {
108 if(atoi(property) == 1)
109 ctx->vstate.fakevsync = true;
110 }
111
Naseer Ahmedfafce762014-03-10 17:20:02 -0400112 char node_path[MAX_SYSFS_FILE_PATH];
Naseer Ahmedced6b792014-06-04 21:19:41 -0400113#ifdef VSYNC_FAILURE_FALLBACK
114 int fake_vsync_switch_cnt = 0;
115#endif
Simon Wilsonef53c1c2014-04-09 11:15:13 -0700116
Simon Wilsonef53c1c2014-04-09 11:15:13 -0700117 for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) {
Naseer Ahmedfafce762014-03-10 17:20:02 -0400118 for(size_t ev = 0; ev < num_events; ev++) {
119 snprintf(node_path, sizeof(node_path),
120 "/sys/class/graphics/fb%d/%s",
121 dpy == HWC_DISPLAY_PRIMARY ? 0 :
122 overlay::Overlay::getInstance()->
123 getFbForDpy(HWC_DISPLAY_EXTERNAL),
124 event_list[ev].name);
Simon Wilsonef53c1c2014-04-09 11:15:13 -0700125
Naseer Ahmedfafce762014-03-10 17:20:02 -0400126 ALOGI("%s: Reading event %zu for dpy %d from %s", __FUNCTION__,
127 ev, dpy, node_path);
128 pfd[dpy][ev].fd = open(node_path, O_RDONLY);
129
130 if (dpy == HWC_DISPLAY_PRIMARY && pfd[dpy][ev].fd < 0) {
131 // Make sure fb device is opened before starting
132 // this thread so this never happens.
133 ALOGE ("%s:unable to open event node for dpy=%d event=%zu, %s",
134 __FUNCTION__, dpy, ev, strerror(errno));
135 if (ev == 0) {
136 ctx->vstate.fakevsync = true;
137 //XXX: Blank events don't work with fake vsync,
138 //but we shouldn't be running on fake vsync anyway.
139 break;
140 }
Simon Wilsonef53c1c2014-04-09 11:15:13 -0700141 }
Simon Wilsonef53c1c2014-04-09 11:15:13 -0700142
Naseer Ahmedfafce762014-03-10 17:20:02 -0400143 pread(pfd[dpy][ev].fd, vdata , MAX_DATA, 0);
144 if (pfd[dpy][ev].fd >= 0)
145 pfd[dpy][ev].events = POLLPRI | POLLERR;
146 }
Simon Wilsonef53c1c2014-04-09 11:15:13 -0700147 }
148
149 if (LIKELY(!ctx->vstate.fakevsync)) {
150 do {
Shalaj Jaincfb96882014-06-15 13:47:47 -0700151 int err = poll(*pfd, (int)(num_displays * num_events), -1);
Simon Wilsonef53c1c2014-04-09 11:15:13 -0700152 if(err > 0) {
153 for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) {
Naseer Ahmedfafce762014-03-10 17:20:02 -0400154 for(size_t ev = 0; ev < num_events; ev++) {
155 if (pfd[dpy][ev].revents & POLLPRI) {
Praveena Pachipulusu4217cbf2014-02-17 10:42:28 +0530156 ssize_t len = pread(pfd[dpy][ev].fd, vdata, MAX_DATA, 0);
157 if (UNLIKELY(len < 0)) {
Naseer Ahmedfafce762014-03-10 17:20:02 -0400158 // If the read was just interrupted - it is not
159 // a fatal error. Just continue in this case
160 ALOGE ("%s: Unable to read event:%zu for \
161 dpy=%d : %s",
162 __FUNCTION__, ev, dpy, strerror(errno));
Naseer Ahmedced6b792014-06-04 21:19:41 -0400163#ifdef VSYNC_FAILURE_FALLBACK
164 fake_vsync_switch_cnt++;
165 if (fake_vsync_switch_cnt >
166 VSYNC_FAILURE_THRESHOLD) {
167 ALOGE("%s: Too many errors, falling back to fake vsync ",
168 __FUNCTION__);
169 ctx->vstate.fakevsync = true;
170 goto vsync_failed;
171 }
172#endif
Naseer Ahmedfafce762014-03-10 17:20:02 -0400173 continue;
174 }
175 event_list[ev].callback(ctx, dpy, vdata);
Simon Wilsonef53c1c2014-04-09 11:15:13 -0700176 }
Simon Wilsonef53c1c2014-04-09 11:15:13 -0700177 }
178 }
Simon Wilsonef53c1c2014-04-09 11:15:13 -0700179 } else {
Naseer Ahmedfafce762014-03-10 17:20:02 -0400180 ALOGE("%s: poll failed errno: %s", __FUNCTION__,
Simon Wilsonef53c1c2014-04-09 11:15:13 -0700181 strerror(errno));
182 continue;
183 }
184 } while (true);
185
186 } else {
187
Naseer Ahmedced6b792014-06-04 21:19:41 -0400188#ifdef VSYNC_FAILURE_FALLBACK
189vsync_failed:
190#endif
Simon Wilsonef53c1c2014-04-09 11:15:13 -0700191 //Fake vsync is used only when set explicitly through a property or when
192 //the vsync timestamp node cannot be opened at bootup. There is no
193 //fallback to fake vsync from the true vsync loop, ever, as the
194 //condition can easily escape detection.
195 //Also, fake vsync is delivered only for the primary display.
196 do {
197 usleep(16666);
Naseer Ahmedfafce762014-03-10 17:20:02 -0400198 uint64_t timestamp = systemTime();
199 ctx->proc->vsync(ctx->proc, HWC_DISPLAY_PRIMARY, timestamp);
Simon Wilsonef53c1c2014-04-09 11:15:13 -0700200
201 } while (true);
202 }
203
204 for (int dpy = HWC_DISPLAY_PRIMARY; dpy <= HWC_DISPLAY_EXTERNAL; dpy++ ) {
Naseer Ahmedfafce762014-03-10 17:20:02 -0400205 for( size_t event = 0; event < num_events; event++) {
206 if(pfd[dpy][event].fd >= 0)
207 close (pfd[dpy][event].fd);
208 }
Simon Wilsonef53c1c2014-04-09 11:15:13 -0700209 }
210
211 return NULL;
212}
213
214void init_vsync_thread(hwc_context_t* ctx)
215{
216 int ret;
217 pthread_t vsync_thread;
218 ALOGI("Initializing VSYNC Thread");
219 ret = pthread_create(&vsync_thread, NULL, vsync_loop, (void*) ctx);
220 if (ret) {
221 ALOGE("%s: failed to create %s: %s", __FUNCTION__,
222 HWC_VSYNC_THREAD_NAME, strerror(ret));
223 }
224}
225
226}; //namespace