blob: 9111826dba85e384362fb82381e91a41a401a86c [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
Arun Kumar K.Rc62935a2013-12-03 16:47:47 -08004 * Copyright (C) 2012-14, 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);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070053 int compositionType =
54 qdutils::QCCompositionType::getInstance().getCompositionType();
55 if (compositionType & (qdutils::COMPOSITION_TYPE_DYN |
56 qdutils::COMPOSITION_TYPE_MDP |
57 qdutils::COMPOSITION_TYPE_C2D)) {
Saurabh Shah220a30c2013-10-29 16:12:12 -070058 ctx->mCopyBit[dpy] = new CopyBit(ctx, dpy);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070059 }
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -080060}
61
62static void clear(hwc_context_t* ctx, int dpy)
63{
64 if(ctx->mFBUpdate[dpy]) {
65 delete ctx->mFBUpdate[dpy];
66 ctx->mFBUpdate[dpy] = NULL;
67 }
68 if(ctx->mCopyBit[dpy]){
69 delete ctx->mCopyBit[dpy];
70 ctx->mCopyBit[dpy] = NULL;
71 }
Jeykumar Sankaran85977e32013-02-25 17:06:08 -080072 if(ctx->mMDPComp[dpy]) {
73 delete ctx->mMDPComp[dpy];
74 ctx->mMDPComp[dpy] = NULL;
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -080075 }
76}
77
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070078/* Parse uevent data for devices which we are interested */
79static int getConnectedDisplay(const char* strUdata)
80{
81 if(strcasestr("change@/devices/virtual/switch/hdmi", strUdata))
82 return HWC_DISPLAY_EXTERNAL;
83 if(strcasestr("change@/devices/virtual/switch/wfd", strUdata))
84 return HWC_DISPLAY_VIRTUAL;
85 return -1;
86}
87
Veera Sankaran0dadfb32013-07-22 16:07:48 -070088static bool getPanelResetStatus(hwc_context_t* ctx, const char* strUdata, int len)
89{
90 const char* iter_str = strUdata;
91 if (strcasestr("change@/devices/virtual/graphics/fb0", strUdata)) {
92 while(((iter_str - strUdata) <= len) && (*iter_str)) {
93 char* pstr = strstr(iter_str, "PANEL_ALIVE=0");
94 if (pstr != NULL) {
Arpita Banerjeed8965982013-11-08 17:27:33 -080095 ALOGI("%s: got change event in fb0 with PANEL_ALIVE=0",
Veera Sankaran0dadfb32013-07-22 16:07:48 -070096 __FUNCTION__);
97 ctx->mPanelResetStatus = true;
98 return true;
99 }
100 iter_str += strlen(iter_str)+1;
101 }
102 }
103 return false;
104}
105
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700106/* Parse uevent data for action requested for the display */
107static int getConnectedState(const char* strUdata, int len)
108{
109 const char* iter_str = strUdata;
110 while(((iter_str - strUdata) <= len) && (*iter_str)) {
111 char* pstr = strstr(iter_str, "SWITCH_STATE=");
112 if (pstr != NULL) {
113 return (atoi(pstr + strlen("SWITCH_STATE=")));
114 }
115 iter_str += strlen(iter_str)+1;
116 }
117 return -1;
118}
119
Arun Kumar K.Rc62935a2013-12-03 16:47:47 -0800120void handle_pause(hwc_context_t* ctx, int dpy) {
121 {
122 Locker::Autolock _l(ctx->mDrawLock);
123 ctx->dpyAttr[dpy].isActive = true;
124 ctx->dpyAttr[dpy].isPause = true;
125 ctx->proc->invalidate(ctx->proc);
126 }
127 usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period
128 * 2 / 1000);
129 // At this point all the pipes used by External have been
130 // marked as UNSET.
131 {
132 Locker::Autolock _l(ctx->mDrawLock);
133 // Perform commit to unstage the pipes.
134 if (!Overlay::displayCommit(ctx->dpyAttr[dpy].fd)) {
135 ALOGE("%s: display commit fail! for %d dpy",
136 __FUNCTION__, dpy);
137 }
138 }
139 return;
140}
141
142void handle_resume(hwc_context_t* ctx, int dpy) {
143 //Treat Resume as Online event
144 //Since external didnt have any pipes, force primary to give up
145 //its pipes; we don't allow inter-mixer pipe transfers.
146 {
147 Locker::Autolock _l(ctx->mDrawLock);
148
149 // A dynamic resolution change (DRC) can be made for a WiFi
150 // display. In order to support the resolution change, we
151 // need to reconfigure the corresponding display attributes.
152 // Since DRC is only on WiFi display, we only need to call
153 // configure() on the VirtualDisplay device.
154 if(dpy == HWC_DISPLAY_VIRTUAL)
155 ctx->mVirtualDisplay->configure();
156
157 ctx->dpyAttr[dpy].isConfiguring = true;
158 ctx->dpyAttr[dpy].isActive = true;
159 ctx->proc->invalidate(ctx->proc);
160 }
161 usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period
162 * 2 / 1000);
163 //At this point external has all the pipes it would need.
164 {
165 Locker::Autolock _l(ctx->mDrawLock);
166 ctx->dpyAttr[dpy].isPause = false;
167 ctx->proc->invalidate(ctx->proc);
168 }
169 return;
170}
171
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700172static void handle_uevent(hwc_context_t* ctx, const char* udata, int len)
173{
Veera Sankaran0dadfb32013-07-22 16:07:48 -0700174 bool bpanelReset = getPanelResetStatus(ctx, udata, len);
175 if (bpanelReset) {
radhakrishna5d24c252013-12-19 18:03:56 +0530176 ctx->proc->invalidate(ctx->proc);
Veera Sankaran0dadfb32013-07-22 16:07:48 -0700177 return;
178 }
179
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700180 int dpy = getConnectedDisplay(udata);
181 if(dpy < 0) {
182 ALOGD_IF(UEVENT_DEBUG, "%s: Not disp Event ", __FUNCTION__);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700183 return;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700184 }
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800185
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700186 int switch_state = getConnectedState(udata, len);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800187
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700188 ALOGE_IF(UEVENT_DEBUG,"%s: uevent recieved: %s switch state: %d",
189 __FUNCTION__,udata, switch_state);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800190
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700191 switch(switch_state) {
192 case EXTERNAL_OFFLINE:
193 {
194 /* Display not connected */
195 if(!ctx->dpyAttr[dpy].connected){
196 ALOGE_IF(UEVENT_DEBUG,"%s: Ignoring EXTERNAL_OFFLINE event"
197 "for display: %d", __FUNCTION__, dpy);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800198 break;
Saurabh Shahcf053c62012-12-13 12:32:55 -0800199 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700200
Saurabh Shahb39f8152013-08-22 10:21:44 -0700201 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700202 clear(ctx, dpy);
203 ctx->dpyAttr[dpy].connected = false;
204 ctx->dpyAttr[dpy].isActive = false;
205
206 if(dpy == HWC_DISPLAY_EXTERNAL) {
207 ctx->mExtDisplay->teardown();
208 } else {
209 ctx->mVirtualDisplay->teardown();
210 }
211
212 /* We need to send hotplug to SF only when we are disconnecting
213 * (1) HDMI OR (2) proprietary WFD session */
214 if(dpy == HWC_DISPLAY_EXTERNAL ||
215 ctx->mVirtualonExtActive) {
216 ALOGE_IF(UEVENT_DEBUG,"%s:Sending EXTERNAL OFFLINE hotplug"
217 "event", __FUNCTION__);
218 ctx->proc->hotplug(ctx->proc, HWC_DISPLAY_EXTERNAL,
219 EXTERNAL_OFFLINE);
Manoj Kumar AVM7afbcf92013-10-18 09:07:41 -0700220 ctx->mVirtualonExtActive = false;
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700221 }
222 break;
223 }
224 case EXTERNAL_ONLINE:
225 {
226 /* Display already connected */
227 if(ctx->dpyAttr[dpy].connected) {
228 ALOGE_IF(UEVENT_DEBUG,"%s: Ignoring EXTERNAL_ONLINE event"
229 "for display: %d", __FUNCTION__, dpy);
230 break;
231 }
232 {
233 //Force composition to give up resources like pipes and
234 //close fb. For example if assertive display is going on,
235 //fb2 could be open, thus connecting Layer Mixer#0 to
236 //WriteBack module. If HDMI attempts to open fb1, the driver
237 //will try to attach Layer Mixer#0 to HDMI INT, which will
238 //fail, since Layer Mixer#0 is still connected to WriteBack.
239 //This block will force composition to close fb2 in above
240 //example.
Saurabh Shahb39f8152013-08-22 10:21:44 -0700241 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700242 ctx->dpyAttr[dpy].isConfiguring = true;
243 ctx->proc->invalidate(ctx->proc);
244 }
245 //2 cycles for slower content
246 usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period
247 * 2 / 1000);
248
249 if(dpy == HWC_DISPLAY_EXTERNAL) {
250 if(ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].connected) {
251 ALOGD_IF(UEVENT_DEBUG,"Received HDMI connection request"
252 "when WFD is active");
253 {
Saurabh Shahb39f8152013-08-22 10:21:44 -0700254 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700255 clear(ctx, HWC_DISPLAY_VIRTUAL);
256 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].connected = false;
257 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isActive = false;
258 }
259
260 ctx->mVirtualDisplay->teardown();
261
262 /* Need to send hotplug only when connected WFD in
263 * proprietary path */
264 if(ctx->mVirtualonExtActive) {
265 ALOGE_IF(UEVENT_DEBUG,"%s: Sending EXTERNAL OFFLINE"
266 "hotplug event", __FUNCTION__);
267 ctx->proc->hotplug(ctx->proc, HWC_DISPLAY_EXTERNAL,
268 EXTERNAL_OFFLINE);
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800269 {
Saurabh Shahb39f8152013-08-22 10:21:44 -0700270 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700271 ctx->mVirtualonExtActive = false;
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800272 }
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800273 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700274 /* Wait for few frames for SF to tear down
275 * the WFD session. */
276 usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period
277 * 2 / 1000);
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800278 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700279 ctx->mExtDisplay->configure();
280 } else {
Saurabh Shah188e4332013-07-18 10:33:22 -0700281 {
Saurabh Shahb39f8152013-08-22 10:21:44 -0700282 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700283 /* TRUE only when we are on proprietary WFD session */
284 ctx->mVirtualonExtActive = true;
285 char property[PROPERTY_VALUE_MAX];
286 if((property_get("persist.sys.wfd.virtual",
287 property, NULL) > 0) &&
288 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
289 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
290 // This means we are on Google's WFD session
291 ctx->mVirtualonExtActive = false;
292 }
Saurabh Shah188e4332013-07-18 10:33:22 -0700293 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700294 ctx->mVirtualDisplay->configure();
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800295 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700296
Saurabh Shahb39f8152013-08-22 10:21:44 -0700297 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700298 setup(ctx, dpy);
299 ctx->dpyAttr[dpy].isPause = false;
300 ctx->dpyAttr[dpy].connected = true;
301 ctx->dpyAttr[dpy].isConfiguring = true;
302
Raj Kamal2464cce2013-09-06 16:20:12 +0530303 if(dpy == HWC_DISPLAY_EXTERNAL ||
304 ctx->mVirtualonExtActive) {
305 /* External display is HDMI or non-hybrid WFD solution */
306 ALOGE_IF(UEVENT_DEBUG, "%s: Sending EXTERNAL_OFFLINE ONLINE"
307 "hotplug event", __FUNCTION__);
308 ctx->proc->hotplug(ctx->proc,HWC_DISPLAY_EXTERNAL,
309 EXTERNAL_ONLINE);
310 } else {
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700311 /* We wont be getting unblank for VIRTUAL DISPLAY and its
312 * always guaranteed from WFD stack that CONNECT uevent for
313 * VIRTUAL DISPLAY will be triggered before creating
314 * surface for the same. */
315 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isActive = true;
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800316 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700317 break;
318 }
Amara Venkata Mastan Manoj Kumar5cbac942013-08-13 19:00:14 -0700319 case EXTERNAL_PAUSE:
320 { // pause case
321 ALOGD("%s Received Pause event",__FUNCTION__);
Arun Kumar K.Rc62935a2013-12-03 16:47:47 -0800322 handle_pause(ctx, dpy);
323 break;
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800324 }
Amara Venkata Mastan Manoj Kumar5cbac942013-08-13 19:00:14 -0700325 case EXTERNAL_RESUME:
326 { // resume case
327 ALOGD("%s Received resume event",__FUNCTION__);
328 //Treat Resume as Online event
329 //Since external didnt have any pipes, force primary to give up
330 //its pipes; we don't allow inter-mixer pipe transfers.
Arun Kumar K.Rc62935a2013-12-03 16:47:47 -0800331 handle_resume(ctx, dpy);
Amara Venkata Mastan Manoj Kumar5cbac942013-08-13 19:00:14 -0700332 break;
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700333 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700334 default:
335 {
336 ALOGE("%s: Invalid state to swtich:%d", __FUNCTION__, switch_state);
337 break;
338 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700339 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700340}
341
342static void *uevent_loop(void *param)
343{
344 int len = 0;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400345 static char udata[PAGE_SIZE];
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700346 hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700347 char thread_name[64] = HWC_UEVENT_THREAD_NAME;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400348 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700349 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
Sravan Kumar D.V.N04ce9ad2013-10-23 15:57:57 +0530350 if(!uevent_init()) {
351 ALOGE("%s: failed to init uevent ",__FUNCTION__);
352 return NULL;
353 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700354
355 while(1) {
356 len = uevent_next_event(udata, sizeof(udata) - 2);
357 handle_uevent(ctx, udata, len);
358 }
359
360 return NULL;
361}
362
363void init_uevent_thread(hwc_context_t* ctx)
364{
365 pthread_t uevent_thread;
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700366 int ret;
367
368 ALOGI("Initializing UEVENT Thread");
369 ret = pthread_create(&uevent_thread, NULL, uevent_loop, (void*) ctx);
370 if (ret) {
371 ALOGE("%s: failed to create %s: %s", __FUNCTION__,
372 HWC_UEVENT_THREAD_NAME, strerror(ret));
373 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700374}
375
376}; //namespace