1 /*
2 * Copyright (C) 2010 The Android Open Source Project
3 * Copyright (C) 2012-2014, The Linux Foundation. All rights reserved.
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
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 "hdmi.h"
31 #include "qd_utils.h"
32 #include "string.h"
33 #include "overlay.h"
34 #include <inttypes.h>
35
36 using namespace qdutils;
37 namespace qhwc {
38
39 #define HWC_VSYNC_THREAD_NAME "hwcVsyncThread"
40 #define PANEL_ON_STR "panel_power_on ="
41 #define ARRAY_LENGTH(array) (sizeof((array))/sizeof((array)[0]))
42 #define MAX_THERMAL_LEVEL 3
43 const int MAX_DATA = 64;
44
hwc_vsync_control(hwc_context_t * ctx,int dpy,int enable)45 int 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
handle_vsync_event(hwc_context_t * ctx,int dpy,char * data)58 static 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 (ctx->vstate.debug, "%s: timestamp %" PRIu64 " sent to SF for dpy=%d",
67 __FUNCTION__, timestamp, dpy);
68 ctx->proc->vsync(ctx->proc, dpy, timestamp);
69 }
70
handle_blank_event(hwc_context_t * ctx,int dpy,char * data)71 static 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 unsigned long int poweron = strtoul(data + strlen(PANEL_ON_STR), NULL, 0);
75 ALOGI("%s: dpy:%d panel power state: %ld", __FUNCTION__, dpy, poweron);
76 if (!ctx->mHDMIDisplay->isHDMIPrimaryDisplay()) {
77 ctx->dpyAttr[dpy].isActive = poweron ? true: false;
78 }
79 }
80 }
81
handle_thermal_event(hwc_context_t * ctx,int dpy,char * data)82 static void handle_thermal_event(hwc_context_t* ctx, int dpy, char *data)
83 {
84 // extract thermal level
85 uint64_t thermalLevel = 0;
86 if (!strncmp(data, "thermal_level=", strlen("thermal_level="))) {
87 thermalLevel = strtoull(data + strlen("thermal_level="), NULL, 0);
88 }
89
90 if (thermalLevel >= MAX_THERMAL_LEVEL) {
91 ALOGD("%s: dpy:%d thermal_level=%" PRIu64 "",__FUNCTION__,dpy,thermalLevel);
92 ctx->mThermalBurstMode = true;
93 } else
94 ctx->mThermalBurstMode = false;
95 }
96
97 struct event {
98 const char* name;
99 void (*callback)(hwc_context_t* ctx, int dpy, char *data);
100 };
101
102 struct event event_list[] = {
103 { "vsync_event", handle_vsync_event },
104 { "show_blank_event", handle_blank_event },
105 { "msm_fb_thermal_level", handle_thermal_event },
106 };
107
108 #define num_events ARRAY_LENGTH(event_list)
109
vsync_loop(void * param)110 static void *vsync_loop(void *param)
111 {
112 hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
113
114 char thread_name[64] = HWC_VSYNC_THREAD_NAME;
115 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
116 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY +
117 android::PRIORITY_MORE_FAVORABLE);
118
119 char vdata[MAX_DATA];
120 //Number of physical displays
121 //We poll on all the nodes.
122 int num_displays = HWC_NUM_DISPLAY_TYPES - 1;
123 struct pollfd pfd[num_displays][num_events];
124
125 char property[PROPERTY_VALUE_MAX];
126 if(property_get("debug.hwc.fakevsync", property, NULL) > 0) {
127 if(atoi(property) == 1)
128 ctx->vstate.fakevsync = true;
129 }
130
131 char node_path[MAX_SYSFS_FILE_PATH];
132
133 for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) {
134 for(size_t ev = 0; ev < num_events; ev++) {
135 snprintf(node_path, sizeof(node_path),
136 "/sys/class/graphics/fb%d/%s",
137 dpy == HWC_DISPLAY_PRIMARY ? 0 :
138 overlay::Overlay::getInstance()->
139 getFbForDpy(HWC_DISPLAY_EXTERNAL),
140 event_list[ev].name);
141
142 ALOGI("%s: Reading event %zu for dpy %d from %s", __FUNCTION__,
143 ev, dpy, node_path);
144 pfd[dpy][ev].fd = open(node_path, O_RDONLY);
145
146 if (dpy == HWC_DISPLAY_PRIMARY && pfd[dpy][ev].fd < 0) {
147 // Make sure fb device is opened before starting
148 // this thread so this never happens.
149 ALOGE ("%s:unable to open event node for dpy=%d event=%zu, %s",
150 __FUNCTION__, dpy, ev, strerror(errno));
151 if (ev == 0) {
152 ctx->vstate.fakevsync = true;
153 //XXX: Blank events don't work with fake vsync,
154 //but we shouldn't be running on fake vsync anyway.
155 break;
156 }
157 }
158
159 memset(&vdata, '\0', sizeof(vdata));
160 // Read once from the fds to clear the first notify
161 pread(pfd[dpy][ev].fd, vdata , MAX_DATA - 1, 0);
162 if (pfd[dpy][ev].fd >= 0)
163 pfd[dpy][ev].events = POLLPRI | POLLERR;
164 }
165 }
166
167 if (LIKELY(!ctx->vstate.fakevsync)) {
168 do {
169 int err = poll(*pfd, (int)(num_displays * num_events), -1);
170 if(err > 0) {
171 for (int dpy = HWC_DISPLAY_PRIMARY; dpy < num_displays; dpy++) {
172 for(size_t ev = 0; ev < num_events; ev++) {
173 if (pfd[dpy][ev].revents & POLLPRI) {
174 // Clear vdata before writing into it
175 memset(&vdata, '\0', sizeof(vdata));
176 ssize_t len = pread(pfd[dpy][ev].fd, vdata,
177 MAX_DATA - 1, 0);
178 if (UNLIKELY(len < 0)) {
179 // If the read was just interrupted - it is not
180 // a fatal error. Just continue in this case
181 ALOGE ("%s: Unable to read event:%zu for \
182 dpy=%d : %s",
183 __FUNCTION__, ev, dpy, strerror(errno));
184 continue;
185 }
186 vdata[len] = '\0';
187 event_list[ev].callback(ctx, dpy, vdata);
188 }
189 }
190 }
191 } else {
192 ALOGE("%s: poll failed errno: %s", __FUNCTION__,
193 strerror(errno));
194 continue;
195 }
196 } while (true);
197
198 } else {
199
200 //Fake vsync is used only when set explicitly through a property or when
201 //the vsync timestamp node cannot be opened at bootup. There is no
202 //fallback to fake vsync from the true vsync loop, ever, as the
203 //condition can easily escape detection.
204 //Also, fake vsync is delivered only for the primary display.
205 do {
206 usleep(16666);
207 uint64_t timestamp = systemTime();
208 ctx->proc->vsync(ctx->proc, HWC_DISPLAY_PRIMARY, timestamp);
209
210 } while (true);
211 }
212
213 for (int dpy = HWC_DISPLAY_PRIMARY; dpy <= HWC_DISPLAY_EXTERNAL; dpy++ ) {
214 for( size_t event = 0; event < num_events; event++) {
215 if(pfd[dpy][event].fd >= 0)
216 close (pfd[dpy][event].fd);
217 }
218 }
219
220 return NULL;
221 }
222
init_vsync_thread(hwc_context_t * ctx)223 void init_vsync_thread(hwc_context_t* ctx)
224 {
225 int ret;
226 pthread_t vsync_thread;
227 ALOGI("Initializing VSYNC Thread");
228 ret = pthread_create(&vsync_thread, NULL, vsync_loop, (void*) ctx);
229 if (ret) {
230 ALOGE("%s: failed to create %s: %s", __FUNCTION__,
231 HWC_VSYNC_THREAD_NAME, strerror(ret));
232 }
233 }
234
235 }; //namespace
236