blob: 9d35e7472d8c3a7f3c97b43f9577fee18408c9ab [file] [log] [blame]
Arun Kumar K.Rf6f49a12012-11-27 11:46:50 -08001
Naseer Ahmed72cf9762012-07-21 12:17:13 -07002/*
3 * Copyright (C) 2010 The Android Open Source Project
Saurabh Shah56f610d2012-08-07 15:27:06 -07004 * Copyright (C) 2012, The Linux Foundation. All rights reserved.
Naseer Ahmed72cf9762012-07-21 12:17:13 -07005 *
6 * Not a Contribution, Apache license notifications and license are
7 * retained for attribution purposes only.
8
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Saurabh Shah649cda62012-09-16 16:05:58 -070021#define UEVENT_DEBUG 0
Naseer Ahmed72cf9762012-07-21 12:17:13 -070022#include <hardware_legacy/uevent.h>
23#include <utils/Log.h>
24#include <sys/resource.h>
Naseer Ahmedff4f0252012-10-01 13:03:01 -040025#include <sys/prctl.h>
Naseer Ahmed72cf9762012-07-21 12:17:13 -070026#include <string.h>
27#include <stdlib.h>
28#include "hwc_utils.h"
Saurabh Shahcf053c62012-12-13 12:32:55 -080029#include "hwc_fbupdate.h"
Jeykumar Sankaran85977e32013-02-25 17:06:08 -080030#include "hwc_mdpcomp.h"
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080031#include "hwc_copybit.h"
32#include "comptype.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070033#include "external.h"
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070034#include "virtual.h"
Saurabh Shah67a38c32013-06-10 16:23:15 -070035#include "mdp_version.h"
Amara Venkata Mastan Manoj Kumar5cbac942013-08-13 19:00:14 -070036using namespace overlay;
Naseer Ahmed72cf9762012-07-21 12:17:13 -070037namespace qhwc {
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -080038#define HWC_UEVENT_SWITCH_STR "change@/devices/virtual/switch/"
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070039#define HWC_UEVENT_THREAD_NAME "hwcUeventThread"
40
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -080041/* External Display states */
42enum {
43 EXTERNAL_OFFLINE = 0,
44 EXTERNAL_ONLINE,
45 EXTERNAL_PAUSE,
46 EXTERNAL_RESUME
47};
48
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070049static void setup(hwc_context_t* ctx, int dpy)
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -080050{
Saurabh Shah88e4d272013-09-03 13:31:29 -070051 ctx->mFBUpdate[dpy] = IFBUpdate::getObject(ctx, dpy);
52 ctx->mMDPComp[dpy] = MDPComp::getObject(ctx, dpy);
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -080053}
54
55static void clear(hwc_context_t* ctx, int dpy)
56{
57 if(ctx->mFBUpdate[dpy]) {
58 delete ctx->mFBUpdate[dpy];
59 ctx->mFBUpdate[dpy] = NULL;
60 }
Jeykumar Sankaran85977e32013-02-25 17:06:08 -080061 if(ctx->mMDPComp[dpy]) {
62 delete ctx->mMDPComp[dpy];
63 ctx->mMDPComp[dpy] = NULL;
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -080064 }
65}
66
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070067/* Parse uevent data for devices which we are interested */
68static int getConnectedDisplay(const char* strUdata)
69{
70 if(strcasestr("change@/devices/virtual/switch/hdmi", strUdata))
71 return HWC_DISPLAY_EXTERNAL;
72 if(strcasestr("change@/devices/virtual/switch/wfd", strUdata))
73 return HWC_DISPLAY_VIRTUAL;
74 return -1;
75}
76
Veera Sankaran0dadfb32013-07-22 16:07:48 -070077static bool getPanelResetStatus(hwc_context_t* ctx, const char* strUdata, int len)
78{
79 const char* iter_str = strUdata;
80 if (strcasestr("change@/devices/virtual/graphics/fb0", strUdata)) {
81 while(((iter_str - strUdata) <= len) && (*iter_str)) {
82 char* pstr = strstr(iter_str, "PANEL_ALIVE=0");
83 if (pstr != NULL) {
Arpita Banerjeed8965982013-11-08 17:27:33 -080084 ALOGI("%s: got change event in fb0 with PANEL_ALIVE=0",
Veera Sankaran0dadfb32013-07-22 16:07:48 -070085 __FUNCTION__);
86 ctx->mPanelResetStatus = true;
87 return true;
88 }
89 iter_str += strlen(iter_str)+1;
90 }
91 }
92 return false;
93}
94
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070095/* Parse uevent data for action requested for the display */
96static int getConnectedState(const char* strUdata, int len)
97{
98 const char* iter_str = strUdata;
99 while(((iter_str - strUdata) <= len) && (*iter_str)) {
100 char* pstr = strstr(iter_str, "SWITCH_STATE=");
101 if (pstr != NULL) {
102 return (atoi(pstr + strlen("SWITCH_STATE=")));
103 }
104 iter_str += strlen(iter_str)+1;
105 }
106 return -1;
107}
108
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700109static void handle_uevent(hwc_context_t* ctx, const char* udata, int len)
110{
Veera Sankaran0dadfb32013-07-22 16:07:48 -0700111 bool bpanelReset = getPanelResetStatus(ctx, udata, len);
112 if (bpanelReset) {
radhakrishna5d24c252013-12-19 18:03:56 +0530113 ctx->proc->invalidate(ctx->proc);
Veera Sankaran0dadfb32013-07-22 16:07:48 -0700114 return;
115 }
116
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700117 int dpy = getConnectedDisplay(udata);
118 if(dpy < 0) {
119 ALOGD_IF(UEVENT_DEBUG, "%s: Not disp Event ", __FUNCTION__);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700120 return;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700121 }
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800122
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700123 int switch_state = getConnectedState(udata, len);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800124
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700125 ALOGE_IF(UEVENT_DEBUG,"%s: uevent recieved: %s switch state: %d",
126 __FUNCTION__,udata, switch_state);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800127
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700128 switch(switch_state) {
129 case EXTERNAL_OFFLINE:
130 {
131 /* Display not connected */
132 if(!ctx->dpyAttr[dpy].connected){
133 ALOGE_IF(UEVENT_DEBUG,"%s: Ignoring EXTERNAL_OFFLINE event"
134 "for display: %d", __FUNCTION__, dpy);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800135 break;
Saurabh Shahcf053c62012-12-13 12:32:55 -0800136 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700137
Saurabh Shahb39f8152013-08-22 10:21:44 -0700138 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700139 clear(ctx, dpy);
140 ctx->dpyAttr[dpy].connected = false;
141 ctx->dpyAttr[dpy].isActive = false;
142
143 if(dpy == HWC_DISPLAY_EXTERNAL) {
144 ctx->mExtDisplay->teardown();
145 } else {
146 ctx->mVirtualDisplay->teardown();
147 }
148
149 /* We need to send hotplug to SF only when we are disconnecting
150 * (1) HDMI OR (2) proprietary WFD session */
151 if(dpy == HWC_DISPLAY_EXTERNAL ||
152 ctx->mVirtualonExtActive) {
153 ALOGE_IF(UEVENT_DEBUG,"%s:Sending EXTERNAL OFFLINE hotplug"
154 "event", __FUNCTION__);
155 ctx->proc->hotplug(ctx->proc, HWC_DISPLAY_EXTERNAL,
156 EXTERNAL_OFFLINE);
Manoj Kumar AVM7afbcf92013-10-18 09:07:41 -0700157 ctx->mVirtualonExtActive = false;
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700158 }
159 break;
160 }
161 case EXTERNAL_ONLINE:
162 {
163 /* Display already connected */
164 if(ctx->dpyAttr[dpy].connected) {
165 ALOGE_IF(UEVENT_DEBUG,"%s: Ignoring EXTERNAL_ONLINE event"
166 "for display: %d", __FUNCTION__, dpy);
167 break;
168 }
169 {
170 //Force composition to give up resources like pipes and
171 //close fb. For example if assertive display is going on,
172 //fb2 could be open, thus connecting Layer Mixer#0 to
173 //WriteBack module. If HDMI attempts to open fb1, the driver
174 //will try to attach Layer Mixer#0 to HDMI INT, which will
175 //fail, since Layer Mixer#0 is still connected to WriteBack.
176 //This block will force composition to close fb2 in above
177 //example.
Saurabh Shahb39f8152013-08-22 10:21:44 -0700178 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700179 ctx->dpyAttr[dpy].isConfiguring = true;
180 ctx->proc->invalidate(ctx->proc);
181 }
182 //2 cycles for slower content
183 usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period
184 * 2 / 1000);
185
186 if(dpy == HWC_DISPLAY_EXTERNAL) {
187 if(ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].connected) {
188 ALOGD_IF(UEVENT_DEBUG,"Received HDMI connection request"
189 "when WFD is active");
190 {
Saurabh Shahb39f8152013-08-22 10:21:44 -0700191 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700192 clear(ctx, HWC_DISPLAY_VIRTUAL);
193 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].connected = false;
194 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isActive = false;
195 }
196
197 ctx->mVirtualDisplay->teardown();
198
199 /* Need to send hotplug only when connected WFD in
200 * proprietary path */
201 if(ctx->mVirtualonExtActive) {
202 ALOGE_IF(UEVENT_DEBUG,"%s: Sending EXTERNAL OFFLINE"
203 "hotplug event", __FUNCTION__);
204 ctx->proc->hotplug(ctx->proc, HWC_DISPLAY_EXTERNAL,
205 EXTERNAL_OFFLINE);
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800206 {
Saurabh Shahb39f8152013-08-22 10:21:44 -0700207 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700208 ctx->mVirtualonExtActive = false;
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800209 }
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800210 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700211 /* Wait for few frames for SF to tear down
212 * the WFD session. */
213 usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period
214 * 2 / 1000);
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800215 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700216 ctx->mExtDisplay->configure();
217 } else {
Saurabh Shah188e4332013-07-18 10:33:22 -0700218 {
Saurabh Shahb39f8152013-08-22 10:21:44 -0700219 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700220 /* TRUE only when we are on proprietary WFD session */
221 ctx->mVirtualonExtActive = true;
222 char property[PROPERTY_VALUE_MAX];
223 if((property_get("persist.sys.wfd.virtual",
224 property, NULL) > 0) &&
225 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
226 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
227 // This means we are on Google's WFD session
228 ctx->mVirtualonExtActive = false;
229 }
Saurabh Shah188e4332013-07-18 10:33:22 -0700230 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700231 ctx->mVirtualDisplay->configure();
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800232 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700233
Saurabh Shahb39f8152013-08-22 10:21:44 -0700234 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700235 setup(ctx, dpy);
236 ctx->dpyAttr[dpy].isPause = false;
237 ctx->dpyAttr[dpy].connected = true;
238 ctx->dpyAttr[dpy].isConfiguring = true;
239
Raj Kamal2464cce2013-09-06 16:20:12 +0530240 if(dpy == HWC_DISPLAY_EXTERNAL ||
241 ctx->mVirtualonExtActive) {
242 /* External display is HDMI or non-hybrid WFD solution */
243 ALOGE_IF(UEVENT_DEBUG, "%s: Sending EXTERNAL_OFFLINE ONLINE"
244 "hotplug event", __FUNCTION__);
245 ctx->proc->hotplug(ctx->proc,HWC_DISPLAY_EXTERNAL,
246 EXTERNAL_ONLINE);
247 } else {
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700248 /* We wont be getting unblank for VIRTUAL DISPLAY and its
249 * always guaranteed from WFD stack that CONNECT uevent for
250 * VIRTUAL DISPLAY will be triggered before creating
251 * surface for the same. */
252 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isActive = true;
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800253 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700254 break;
255 }
Amara Venkata Mastan Manoj Kumar5cbac942013-08-13 19:00:14 -0700256 case EXTERNAL_PAUSE:
257 { // pause case
258 ALOGD("%s Received Pause event",__FUNCTION__);
259 {
260 Locker::Autolock _l(ctx->mDrawLock);
261 ctx->dpyAttr[dpy].isActive = true;
262 ctx->dpyAttr[dpy].isPause = true;
263 ctx->proc->invalidate(ctx->proc);
264 }
265 usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period
266 * 2 / 1000);
267 // At this point all the pipes used by External have been
268 // marked as UNSET.
269 {
270 Locker::Autolock _l(ctx->mDrawLock);
271 // Perform commit to unstage the pipes.
272 if (!Overlay::displayCommit(ctx->dpyAttr[dpy].fd)) {
273 ALOGE("%s: display commit fail! for %d dpy",
274 __FUNCTION__, dpy);
275 }
276 }
277 break;
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800278 }
Amara Venkata Mastan Manoj Kumar5cbac942013-08-13 19:00:14 -0700279 case EXTERNAL_RESUME:
280 { // resume case
281 ALOGD("%s Received resume event",__FUNCTION__);
282 //Treat Resume as Online event
283 //Since external didnt have any pipes, force primary to give up
284 //its pipes; we don't allow inter-mixer pipe transfers.
285 {
286 Locker::Autolock _l(ctx->mDrawLock);
Tatenda Chipeperekwa3368d082013-08-30 14:03:15 -0700287
288 // A dynamic resolution change (DRC) can be made for a WiFi
289 // display. In order to support the resolution change, we
290 // need to reconfigure the corresponding display attributes.
291 // Since DRC is only on WiFi display, we only need to call
292 // configure() on the VirtualDisplay device.
293 if(dpy == HWC_DISPLAY_VIRTUAL)
294 ctx->mVirtualDisplay->configure();
295
Amara Venkata Mastan Manoj Kumar5cbac942013-08-13 19:00:14 -0700296 ctx->dpyAttr[dpy].isConfiguring = true;
297 ctx->dpyAttr[dpy].isActive = true;
298 ctx->proc->invalidate(ctx->proc);
299 }
300 usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period
301 * 2 / 1000);
302 //At this point external has all the pipes it would need.
303 {
304 Locker::Autolock _l(ctx->mDrawLock);
305 ctx->dpyAttr[dpy].isPause = false;
306 ctx->proc->invalidate(ctx->proc);
307 }
308 break;
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700309 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700310 default:
311 {
312 ALOGE("%s: Invalid state to swtich:%d", __FUNCTION__, switch_state);
313 break;
314 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700315 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700316}
317
318static void *uevent_loop(void *param)
319{
320 int len = 0;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400321 static char udata[PAGE_SIZE];
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700322 hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700323 char thread_name[64] = HWC_UEVENT_THREAD_NAME;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400324 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700325 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
Sravan Kumar D.V.N04ce9ad2013-10-23 15:57:57 +0530326 if(!uevent_init()) {
327 ALOGE("%s: failed to init uevent ",__FUNCTION__);
328 return NULL;
329 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700330
331 while(1) {
332 len = uevent_next_event(udata, sizeof(udata) - 2);
333 handle_uevent(ctx, udata, len);
334 }
335
336 return NULL;
337}
338
339void init_uevent_thread(hwc_context_t* ctx)
340{
341 pthread_t uevent_thread;
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700342 int ret;
343
344 ALOGI("Initializing UEVENT Thread");
345 ret = pthread_create(&uevent_thread, NULL, uevent_loop, (void*) ctx);
346 if (ret) {
347 ALOGE("%s: failed to create %s: %s", __FUNCTION__,
348 HWC_UEVENT_THREAD_NAME, strerror(ret));
349 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700350}
351
352}; //namespace