blob: 7bc29bcf6a12517bd7e39b0b8913d0553c7ca2cc [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"
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -080035#include "hwc_virtual.h"
Saurabh Shah67a38c32013-06-10 16:23:15 -070036#include "mdp_version.h"
Amara Venkata Mastan Manoj Kumar5cbac942013-08-13 19:00:14 -070037using namespace overlay;
Naseer Ahmed72cf9762012-07-21 12:17:13 -070038namespace qhwc {
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -080039#define HWC_UEVENT_SWITCH_STR "change@/devices/virtual/switch/"
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070040#define HWC_UEVENT_THREAD_NAME "hwcUeventThread"
41
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -080042/* External Display states */
43enum {
44 EXTERNAL_OFFLINE = 0,
45 EXTERNAL_ONLINE,
46 EXTERNAL_PAUSE,
47 EXTERNAL_RESUME
48};
49
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070050static void setup(hwc_context_t* ctx, int dpy)
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -080051{
Saurabh Shah88e4d272013-09-03 13:31:29 -070052 ctx->mFBUpdate[dpy] = IFBUpdate::getObject(ctx, dpy);
53 ctx->mMDPComp[dpy] = MDPComp::getObject(ctx, dpy);
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -080054}
55
56static void clear(hwc_context_t* ctx, int dpy)
57{
58 if(ctx->mFBUpdate[dpy]) {
59 delete ctx->mFBUpdate[dpy];
60 ctx->mFBUpdate[dpy] = NULL;
61 }
Jeykumar Sankaran85977e32013-02-25 17:06:08 -080062 if(ctx->mMDPComp[dpy]) {
63 delete ctx->mMDPComp[dpy];
64 ctx->mMDPComp[dpy] = NULL;
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -080065 }
66}
67
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070068/* Parse uevent data for devices which we are interested */
69static int getConnectedDisplay(const char* strUdata)
70{
71 if(strcasestr("change@/devices/virtual/switch/hdmi", strUdata))
72 return HWC_DISPLAY_EXTERNAL;
73 if(strcasestr("change@/devices/virtual/switch/wfd", strUdata))
74 return HWC_DISPLAY_VIRTUAL;
75 return -1;
76}
77
Veera Sankaran0dadfb32013-07-22 16:07:48 -070078static bool getPanelResetStatus(hwc_context_t* ctx, const char* strUdata, int len)
79{
80 const char* iter_str = strUdata;
81 if (strcasestr("change@/devices/virtual/graphics/fb0", strUdata)) {
82 while(((iter_str - strUdata) <= len) && (*iter_str)) {
83 char* pstr = strstr(iter_str, "PANEL_ALIVE=0");
84 if (pstr != NULL) {
Arpita Banerjeed8965982013-11-08 17:27:33 -080085 ALOGI("%s: got change event in fb0 with PANEL_ALIVE=0",
Veera Sankaran0dadfb32013-07-22 16:07:48 -070086 __FUNCTION__);
87 ctx->mPanelResetStatus = true;
88 return true;
89 }
90 iter_str += strlen(iter_str)+1;
91 }
92 }
93 return false;
94}
95
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070096/* Parse uevent data for action requested for the display */
97static int getConnectedState(const char* strUdata, int len)
98{
99 const char* iter_str = strUdata;
100 while(((iter_str - strUdata) <= len) && (*iter_str)) {
101 char* pstr = strstr(iter_str, "SWITCH_STATE=");
102 if (pstr != NULL) {
103 return (atoi(pstr + strlen("SWITCH_STATE=")));
104 }
105 iter_str += strlen(iter_str)+1;
106 }
107 return -1;
108}
109
Arun Kumar K.Rc62935a2013-12-03 16:47:47 -0800110void handle_pause(hwc_context_t* ctx, int dpy) {
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -0800111 if(ctx->mHWCVirtual) {
112 ctx->mHWCVirtual->pause(ctx, dpy);
Arun Kumar K.Rc62935a2013-12-03 16:47:47 -0800113 }
114 return;
115}
116
117void handle_resume(hwc_context_t* ctx, int dpy) {
Tatenda Chipeperekwa8f1b9d72014-02-11 16:20:50 -0800118 if(ctx->mHWCVirtual) {
119 ctx->mHWCVirtual->resume(ctx, dpy);
Arun Kumar K.Rc62935a2013-12-03 16:47:47 -0800120 }
121 return;
122}
123
Manoj Kumar AVM1e8b3fe2014-01-07 15:31:51 -0800124static void teardownWfd(hwc_context_t* ctx) {
125 // Teardown WFD display
126 ALOGD_IF(UEVENT_DEBUG,"Received HDMI connection request when WFD is "
127 "active");
128 {
129 Locker::Autolock _l(ctx->mDrawLock);
130 clear(ctx, HWC_DISPLAY_VIRTUAL);
131 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].connected = false;
132 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isActive = false;
133 }
134
135 ctx->mVirtualDisplay->teardown();
136
137 /* Need to send hotplug only when connected WFD in proprietary path */
138 if(ctx->mVirtualonExtActive) {
139 ALOGE_IF(UEVENT_DEBUG,"%s: Sending EXTERNAL OFFLINE"
140 "hotplug event for wfd display", __FUNCTION__);
141 ctx->proc->hotplug(ctx->proc, HWC_DISPLAY_EXTERNAL,
142 EXTERNAL_OFFLINE);
143 {
144 Locker::Autolock _l(ctx->mDrawLock);
145 ctx->mVirtualonExtActive = false;
146 }
147 }
148 /* Wait for few frames for SF to tear down the WFD session. */
149 usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period
150 * 2 / 1000);
151}
152
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700153static void handle_uevent(hwc_context_t* ctx, const char* udata, int len)
154{
Veera Sankaran0dadfb32013-07-22 16:07:48 -0700155 bool bpanelReset = getPanelResetStatus(ctx, udata, len);
156 if (bpanelReset) {
radhakrishna5d24c252013-12-19 18:03:56 +0530157 ctx->proc->invalidate(ctx->proc);
Veera Sankaran0dadfb32013-07-22 16:07:48 -0700158 return;
159 }
160
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700161 int dpy = getConnectedDisplay(udata);
162 if(dpy < 0) {
163 ALOGD_IF(UEVENT_DEBUG, "%s: Not disp Event ", __FUNCTION__);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700164 return;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700165 }
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800166
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700167 int switch_state = getConnectedState(udata, len);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800168
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700169 ALOGE_IF(UEVENT_DEBUG,"%s: uevent recieved: %s switch state: %d",
170 __FUNCTION__,udata, switch_state);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800171
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700172 switch(switch_state) {
173 case EXTERNAL_OFFLINE:
174 {
175 /* Display not connected */
176 if(!ctx->dpyAttr[dpy].connected){
177 ALOGE_IF(UEVENT_DEBUG,"%s: Ignoring EXTERNAL_OFFLINE event"
178 "for display: %d", __FUNCTION__, dpy);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800179 break;
Saurabh Shahcf053c62012-12-13 12:32:55 -0800180 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700181
Saurabh Shahb39f8152013-08-22 10:21:44 -0700182 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700183 clear(ctx, dpy);
184 ctx->dpyAttr[dpy].connected = false;
185 ctx->dpyAttr[dpy].isActive = false;
186
187 if(dpy == HWC_DISPLAY_EXTERNAL) {
188 ctx->mExtDisplay->teardown();
189 } else {
190 ctx->mVirtualDisplay->teardown();
191 }
192
193 /* We need to send hotplug to SF only when we are disconnecting
194 * (1) HDMI OR (2) proprietary WFD session */
195 if(dpy == HWC_DISPLAY_EXTERNAL ||
196 ctx->mVirtualonExtActive) {
197 ALOGE_IF(UEVENT_DEBUG,"%s:Sending EXTERNAL OFFLINE hotplug"
198 "event", __FUNCTION__);
199 ctx->proc->hotplug(ctx->proc, HWC_DISPLAY_EXTERNAL,
200 EXTERNAL_OFFLINE);
Manoj Kumar AVM7afbcf92013-10-18 09:07:41 -0700201 ctx->mVirtualonExtActive = false;
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700202 }
203 break;
204 }
205 case EXTERNAL_ONLINE:
206 {
207 /* Display already connected */
208 if(ctx->dpyAttr[dpy].connected) {
209 ALOGE_IF(UEVENT_DEBUG,"%s: Ignoring EXTERNAL_ONLINE event"
210 "for display: %d", __FUNCTION__, dpy);
211 break;
212 }
213 {
214 //Force composition to give up resources like pipes and
215 //close fb. For example if assertive display is going on,
216 //fb2 could be open, thus connecting Layer Mixer#0 to
217 //WriteBack module. If HDMI attempts to open fb1, the driver
218 //will try to attach Layer Mixer#0 to HDMI INT, which will
219 //fail, since Layer Mixer#0 is still connected to WriteBack.
220 //This block will force composition to close fb2 in above
221 //example.
Saurabh Shahb39f8152013-08-22 10:21:44 -0700222 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700223 ctx->dpyAttr[dpy].isConfiguring = true;
224 ctx->proc->invalidate(ctx->proc);
225 }
226 //2 cycles for slower content
227 usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period
228 * 2 / 1000);
229
230 if(dpy == HWC_DISPLAY_EXTERNAL) {
231 if(ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].connected) {
Manoj Kumar AVM1e8b3fe2014-01-07 15:31:51 -0800232 // Triple Display is supported on 8084 target
233 // WFD can be initiated by Wfd-client or Settings app
234 // 1. wfd-client use hdmi hotplug mechanism.
235 // If wfd is connected via wfd-client and if HDMI is
236 // connected, we have to teardown wfd session.
237 // (as SF support only one active External display
238 // at a given time).
239 // (ToDo: Once wfd-client migrates using virtual display
240 // apis, second condition is redundant).
241 // 2. Settings app use virtual display mechanism.
242 // In this approach, there is no limitation of supporting
243 // triple display.
244 if(!(qdutils::MDPVersion::getInstance().is8084() &&
245 !ctx->mVirtualonExtActive)) {
246 teardownWfd(ctx);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700247 }
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800248 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700249 ctx->mExtDisplay->configure();
250 } else {
Saurabh Shah188e4332013-07-18 10:33:22 -0700251 {
Saurabh Shahb39f8152013-08-22 10:21:44 -0700252 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700253 /* TRUE only when we are on proprietary WFD session */
254 ctx->mVirtualonExtActive = true;
255 char property[PROPERTY_VALUE_MAX];
256 if((property_get("persist.sys.wfd.virtual",
257 property, NULL) > 0) &&
258 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
259 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
260 // This means we are on Google's WFD session
261 ctx->mVirtualonExtActive = false;
262 }
Saurabh Shah188e4332013-07-18 10:33:22 -0700263 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700264 ctx->mVirtualDisplay->configure();
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800265 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700266
Saurabh Shahb39f8152013-08-22 10:21:44 -0700267 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700268 setup(ctx, dpy);
269 ctx->dpyAttr[dpy].isPause = false;
270 ctx->dpyAttr[dpy].connected = true;
271 ctx->dpyAttr[dpy].isConfiguring = true;
272
Raj Kamal2464cce2013-09-06 16:20:12 +0530273 if(dpy == HWC_DISPLAY_EXTERNAL ||
274 ctx->mVirtualonExtActive) {
275 /* External display is HDMI or non-hybrid WFD solution */
276 ALOGE_IF(UEVENT_DEBUG, "%s: Sending EXTERNAL_OFFLINE ONLINE"
277 "hotplug event", __FUNCTION__);
278 ctx->proc->hotplug(ctx->proc,HWC_DISPLAY_EXTERNAL,
279 EXTERNAL_ONLINE);
280 } else {
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700281 /* We wont be getting unblank for VIRTUAL DISPLAY and its
282 * always guaranteed from WFD stack that CONNECT uevent for
283 * VIRTUAL DISPLAY will be triggered before creating
284 * surface for the same. */
285 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isActive = true;
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800286 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700287 break;
288 }
Amara Venkata Mastan Manoj Kumar5cbac942013-08-13 19:00:14 -0700289 case EXTERNAL_PAUSE:
290 { // pause case
291 ALOGD("%s Received Pause event",__FUNCTION__);
Arun Kumar K.Rc62935a2013-12-03 16:47:47 -0800292 handle_pause(ctx, dpy);
293 break;
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800294 }
Amara Venkata Mastan Manoj Kumar5cbac942013-08-13 19:00:14 -0700295 case EXTERNAL_RESUME:
296 { // resume case
297 ALOGD("%s Received resume event",__FUNCTION__);
298 //Treat Resume as Online event
299 //Since external didnt have any pipes, force primary to give up
300 //its pipes; we don't allow inter-mixer pipe transfers.
Arun Kumar K.Rc62935a2013-12-03 16:47:47 -0800301 handle_resume(ctx, dpy);
Amara Venkata Mastan Manoj Kumar5cbac942013-08-13 19:00:14 -0700302 break;
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700303 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700304 default:
305 {
306 ALOGE("%s: Invalid state to swtich:%d", __FUNCTION__, switch_state);
307 break;
308 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700309 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700310}
311
312static void *uevent_loop(void *param)
313{
314 int len = 0;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400315 static char udata[PAGE_SIZE];
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700316 hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700317 char thread_name[64] = HWC_UEVENT_THREAD_NAME;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400318 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700319 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
Sravan Kumar D.V.N04ce9ad2013-10-23 15:57:57 +0530320 if(!uevent_init()) {
321 ALOGE("%s: failed to init uevent ",__FUNCTION__);
322 return NULL;
323 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700324
325 while(1) {
326 len = uevent_next_event(udata, sizeof(udata) - 2);
327 handle_uevent(ctx, udata, len);
328 }
329
330 return NULL;
331}
332
333void init_uevent_thread(hwc_context_t* ctx)
334{
335 pthread_t uevent_thread;
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700336 int ret;
337
338 ALOGI("Initializing UEVENT Thread");
339 ret = pthread_create(&uevent_thread, NULL, uevent_loop, (void*) ctx);
340 if (ret) {
341 ALOGE("%s: failed to create %s: %s", __FUNCTION__,
342 HWC_UEVENT_THREAD_NAME, strerror(ret));
343 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700344}
345
346}; //namespace