blob: 106f4f0518fc9caf616411acdcf4e1c20f5c7fd7 [file] [log] [blame]
Naseer Ahmedff4f0252012-10-01 13:03:01 -04001/*
2 * Copyright (C) 2010 The Android Open Source Project
Naseer Ahmed24f20052013-02-13 11:47:25 -05003 * Copyright (C) 2012-2013, 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 Chipeperekwaa04a34a2014-08-13 10:53:49 -070030#include "qd_utils.h"
Naseer Ahmedff4f0252012-10-01 13:03:01 -040031#include "string.h"
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070032#include "overlay.h"
Naseer Ahmedff4f0252012-10-01 13:03:01 -040033
Tatenda Chipeperekwaa04a34a2014-08-13 10:53:49 -070034using namespace qdutils;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040035namespace qhwc {
36
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070037#define HWC_VSYNC_THREAD_NAME "hwcVsyncThread"
Naseer Ahmeda192cd22014-03-10 17:20:02 -040038#define PANEL_ON_STR "panel_power_on ="
39#define ARRAY_LENGTH(array) (sizeof((array))/sizeof((array)[0]))
40const int MAX_DATA = 64;
41bool logvsync = false;
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070042
Naseer Ahmed56601cd2013-03-05 11:34:14 -050043int 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 Ahmeda192cd22014-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
64 ALOGD_IF (logvsync, "%s: timestamp %llu sent to SF for dpy=%d",
65 __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))) {
72 uint32_t poweron = strtoul(data + strlen(PANEL_ON_STR), NULL, 0);
73 ALOGI("%s: dpy:%d panel power state: %d", __FUNCTION__, dpy, poweron);
74 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)
89
Naseer Ahmedff4f0252012-10-01 13:03:01 -040090static void *vsync_loop(void *param)
91{
Naseer Ahmedff4f0252012-10-01 13:03:01 -040092 hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
93
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070094 char thread_name[64] = HWC_VSYNC_THREAD_NAME;
Naseer Ahmedff4f0252012-10-01 13:03:01 -040095 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
96 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY +
97 android::PRIORITY_MORE_FAVORABLE);
98
Naseer Ahmede13a6002013-07-11 13:43:18 -040099 char vdata[MAX_DATA];
Naseer Ahmeda192cd22014-03-10 17:20:02 -0400100 //Number of physical displays
101 //We poll on all the nodes.
102 int num_displays = HWC_NUM_DISPLAY_TYPES - 1;
103 struct pollfd pfd[num_displays][num_events];
Naseer Ahmede13a6002013-07-11 13:43:18 -0400104
Naseer Ahmedda10c142012-11-16 20:16:31 -0500105 char property[PROPERTY_VALUE_MAX];
106 if(property_get("debug.hwc.fakevsync", property, NULL) > 0) {
107 if(atoi(property) == 1)
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500108 ctx->vstate.fakevsync = true;
109 }
110
111 if(property_get("debug.hwc.logvsync", property, 0) > 0) {
112 if(atoi(property) == 1)
113 logvsync = true;
Naseer Ahmedda10c142012-11-16 20:16:31 -0500114 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400115
Naseer Ahmeda192cd22014-03-10 17:20:02 -0400116 char node_path[MAX_SYSFS_FILE_PATH];
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400117
Naseer Ahmede13a6002013-07-11 13:43:18 -0400118 for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) {
Naseer Ahmeda192cd22014-03-10 17:20:02 -0400119 for(size_t ev = 0; ev < num_events; ev++) {
120 snprintf(node_path, sizeof(node_path),
121 "/sys/class/graphics/fb%d/%s",
122 dpy == HWC_DISPLAY_PRIMARY ? 0 :
123 overlay::Overlay::getInstance()->
124 getFbForDpy(HWC_DISPLAY_EXTERNAL),
125 event_list[ev].name);
Naseer Ahmede13a6002013-07-11 13:43:18 -0400126
Naseer Ahmeda192cd22014-03-10 17:20:02 -0400127 ALOGI("%s: Reading event %zu for dpy %d from %s", __FUNCTION__,
128 ev, dpy, node_path);
129 pfd[dpy][ev].fd = open(node_path, O_RDONLY);
130
131 if (dpy == HWC_DISPLAY_PRIMARY && pfd[dpy][ev].fd < 0) {
132 // Make sure fb device is opened before starting
133 // this thread so this never happens.
134 ALOGE ("%s:unable to open event node for dpy=%d event=%zu, %s",
135 __FUNCTION__, dpy, ev, strerror(errno));
136 if (ev == 0) {
137 ctx->vstate.fakevsync = true;
138 //XXX: Blank events don't work with fake vsync,
139 //but we shouldn't be running on fake vsync anyway.
140 break;
141 }
Naseer Ahmede13a6002013-07-11 13:43:18 -0400142 }
Naseer Ahmede13a6002013-07-11 13:43:18 -0400143
Naseer Ahmeda192cd22014-03-10 17:20:02 -0400144 pread(pfd[dpy][ev].fd, vdata , MAX_DATA, 0);
145 if (pfd[dpy][ev].fd >= 0)
146 pfd[dpy][ev].events = POLLPRI | POLLERR;
147 }
Naseer Ahmed08212c02012-10-17 13:51:56 -0400148 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400149
Naseer Ahmede13a6002013-07-11 13:43:18 -0400150 if (LIKELY(!ctx->vstate.fakevsync)) {
151 do {
Naseer Ahmeda192cd22014-03-10 17:20:02 -0400152 int err = poll(*pfd, num_displays * num_events, -1);
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400153 if(err > 0) {
Naseer Ahmede13a6002013-07-11 13:43:18 -0400154 for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) {
Naseer Ahmeda192cd22014-03-10 17:20:02 -0400155 for(size_t ev = 0; ev < num_events; ev++) {
156 if (pfd[dpy][ev].revents & POLLPRI) {
157 err = pread(pfd[dpy][ev].fd, vdata, MAX_DATA, 0);
158 if (UNLIKELY(err < 0)) {
159 // If the read was just interrupted - it is not
160 // a fatal error. Just continue in this case
161 ALOGE ("%s: Unable to read event:%zu for \
162 dpy=%d : %s",
163 __FUNCTION__, ev, dpy, strerror(errno));
164 continue;
165 }
166 event_list[ev].callback(ctx, dpy, vdata);
Naseer Ahmede13a6002013-07-11 13:43:18 -0400167 }
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400168 }
Naseer Ahmed8bb8f9d2013-05-11 07:29:45 -0400169 }
Naseer Ahmed8fec5c32013-06-06 17:00:15 -0400170 } else {
Naseer Ahmeda192cd22014-03-10 17:20:02 -0400171 ALOGE("%s: poll failed errno: %s", __FUNCTION__,
Naseer Ahmede13a6002013-07-11 13:43:18 -0400172 strerror(errno));
Naseer Ahmed8bb8f9d2013-05-11 07:29:45 -0400173 continue;
Naseer Ahmedda10c142012-11-16 20:16:31 -0500174 }
Naseer Ahmede13a6002013-07-11 13:43:18 -0400175 } while (true);
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400176
Naseer Ahmede13a6002013-07-11 13:43:18 -0400177 } else {
178
179 //Fake vsync is used only when set explicitly through a property or when
180 //the vsync timestamp node cannot be opened at bootup. There is no
181 //fallback to fake vsync from the true vsync loop, ever, as the
182 //condition can easily escape detection.
183 //Also, fake vsync is delivered only for the primary display.
184 do {
185 usleep(16666);
Naseer Ahmeda192cd22014-03-10 17:20:02 -0400186 uint64_t timestamp = systemTime();
187 ctx->proc->vsync(ctx->proc, HWC_DISPLAY_PRIMARY, timestamp);
Naseer Ahmede13a6002013-07-11 13:43:18 -0400188
189 } while (true);
190 }
191
192 for (int dpy = HWC_DISPLAY_PRIMARY; dpy <= HWC_DISPLAY_EXTERNAL; dpy++ ) {
Naseer Ahmeda192cd22014-03-10 17:20:02 -0400193 for( size_t event = 0; event < num_events; event++) {
194 if(pfd[dpy][event].fd >= 0)
195 close (pfd[dpy][event].fd);
196 }
Naseer Ahmede13a6002013-07-11 13:43:18 -0400197 }
Iliyan Malcheveac89652012-10-04 10:38:28 -0700198
199 return NULL;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400200}
201
202void init_vsync_thread(hwc_context_t* ctx)
203{
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700204 int ret;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400205 pthread_t vsync_thread;
206 ALOGI("Initializing VSYNC Thread");
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700207 ret = pthread_create(&vsync_thread, NULL, vsync_loop, (void*) ctx);
208 if (ret) {
209 ALOGE("%s: failed to create %s: %s", __FUNCTION__,
Naseer Ahmedda10c142012-11-16 20:16:31 -0500210 HWC_VSYNC_THREAD_NAME, strerror(ret));
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700211 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400212}
213
214}; //namespace