blob: c6582245ef1b0bfd2a780e05cc5390058e8d9236 [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"
Saurabh Shah67a38c32013-06-10 16:23:15 -070034#include "mdp_version.h"
Naseer Ahmed72cf9762012-07-21 12:17:13 -070035
36namespace qhwc {
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -080037#define HWC_UEVENT_SWITCH_STR "change@/devices/virtual/switch/"
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070038#define HWC_UEVENT_THREAD_NAME "hwcUeventThread"
39
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -080040/* External Display states */
41enum {
42 EXTERNAL_OFFLINE = 0,
43 EXTERNAL_ONLINE,
44 EXTERNAL_PAUSE,
45 EXTERNAL_RESUME
46};
47
48static bool isHDMI(const char* str)
49{
50 if(strcasestr("change@/devices/virtual/switch/hdmi", str))
51 return true;
52 return false;
53}
54
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -080055static void setup(hwc_context_t* ctx, int dpy, bool usecopybit)
56{
Saurabh Shah67a38c32013-06-10 16:23:15 -070057 const int rSplit = 0; //Even split for external if at all
58 ctx->mFBUpdate[dpy] = IFBUpdate::getObject(ctx->dpyAttr[dpy].xres,
59 rSplit, dpy);
60 ctx->mMDPComp[dpy] = MDPComp::getObject(ctx->dpyAttr[dpy].xres,
61 rSplit, dpy);
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -080062 if(usecopybit)
63 ctx->mCopyBit[dpy] = new CopyBit();
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -080064}
65
66static void clear(hwc_context_t* ctx, int dpy)
67{
68 if(ctx->mFBUpdate[dpy]) {
69 delete ctx->mFBUpdate[dpy];
70 ctx->mFBUpdate[dpy] = NULL;
71 }
72 if(ctx->mCopyBit[dpy]){
73 delete ctx->mCopyBit[dpy];
74 ctx->mCopyBit[dpy] = NULL;
75 }
Jeykumar Sankaran85977e32013-02-25 17:06:08 -080076 if(ctx->mMDPComp[dpy]) {
77 delete ctx->mMDPComp[dpy];
78 ctx->mMDPComp[dpy] = NULL;
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -080079 }
80}
81
Naseer Ahmed72cf9762012-07-21 12:17:13 -070082static void handle_uevent(hwc_context_t* ctx, const char* udata, int len)
83{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -070084 int vsync = 0;
Naseer Ahmed72cf9762012-07-21 12:17:13 -070085 int64_t timestamp = 0;
86 const char *str = udata;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080087 bool usecopybit = false;
88 int compositionType =
89 qdutils::QCCompositionType::getInstance().getCompositionType();
90
91 if (compositionType & (qdutils::COMPOSITION_TYPE_DYN |
92 qdutils::COMPOSITION_TYPE_MDP |
93 qdutils::COMPOSITION_TYPE_C2D)) {
94 usecopybit = true;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080095 }
Amara Venkata Mastan Manoj Kumare37e5712012-12-17 17:36:02 -080096 if(!strcasestr("change@/devices/virtual/switch/hdmi", str) &&
97 !strcasestr("change@/devices/virtual/switch/wfd", str)) {
Saurabh Shah649cda62012-09-16 16:05:58 -070098 ALOGD_IF(UEVENT_DEBUG, "%s: Not Ext Disp Event ", __FUNCTION__);
Naseer Ahmed72cf9762012-07-21 12:17:13 -070099 return;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700100 }
Arun Kumar K.Rf6f49a12012-11-27 11:46:50 -0800101 int connected = -1; // initial value - will be set to 1/0 based on hotplug
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800102 int extDpyNum = HWC_DISPLAY_EXTERNAL;
103 char property[PROPERTY_VALUE_MAX];
104 if((property_get("persist.sys.wfd.virtual", property, NULL) > 0) &&
105 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
106 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
107 // This means we are using Google API to trigger WFD Display
108 extDpyNum = HWC_DISPLAY_VIRTUAL;
109
110 }
111
112 int dpy = isHDMI(str) ? HWC_DISPLAY_EXTERNAL : extDpyNum;
113
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800114 // parse HDMI/WFD switch state for connect/disconnect
115 // for HDMI:
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400116 // The event will be of the form:
Arun Kumar K.Rf6f49a12012-11-27 11:46:50 -0800117 // change@/devices/virtual/switch/hdmi ACTION=change
118 // SWITCH_STATE=1 or SWITCH_STATE=0
119 while(*str) {
120 if (!strncmp(str, "SWITCH_STATE=", strlen("SWITCH_STATE="))) {
121 connected = atoi(str + strlen("SWITCH_STATE="));
Saurabh Shah1a8cda02012-10-12 17:00:39 -0700122 //Disabled until SF calls unblank
123 ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive = false;
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800124 //Ignored for Virtual Displays
125 //ToDo: we can do this in a much better way
126 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isActive = true;
Arun Kumar K.Rf6f49a12012-11-27 11:46:50 -0800127 break;
Saurabh Shah1a8cda02012-10-12 17:00:39 -0700128 }
Arun Kumar K.Rf6f49a12012-11-27 11:46:50 -0800129 str += strlen(str) + 1;
130 if (str - udata >= len)
131 break;
132 }
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800133 ALOGD_IF(UEVENT_DEBUG, "Received str:%s",udata);
Arun Kumar K.Rf6f49a12012-11-27 11:46:50 -0800134
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800135 if(connected != EXTERNAL_ONLINE) {
136 if(ctx->mExtDisplay->ignoreRequest(udata)) {
137 ALOGD_IF(UEVENT_DEBUG,"No need to process this connection request"
138 "str:%s",udata);
139 ctx->dpyAttr[dpy].isActive = true;
140 return;
141 }
142 }
143
144 // update extDpyNum
145 ctx->mExtDisplay->setExtDpyNum(dpy);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800146 switch(connected) {
147 case EXTERNAL_OFFLINE:
148 { // disconnect event
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800149 const char *s1 = udata + strlen(HWC_UEVENT_SWITCH_STR);
150 if(!strncmp(s1,"hdmi",strlen(s1))) {
151 ctx->mExtDisplay->teardownHDMIDisplay();
152 }else if(!strncmp(s1,"wfd",strlen(s1))) {
153 ctx->mExtDisplay->teardownWFDDisplay();
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800154 }
Raj kamale3e68ae2013-05-02 17:28:23 +0530155 Locker::Autolock _l(ctx->mExtLock);
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800156 clear(ctx, dpy);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800157 ALOGD("%s sending hotplug: connected = %d and dpy:%d",
158 __FUNCTION__, connected, dpy);
159 ctx->dpyAttr[dpy].connected = false;
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800160 //hwc comp could be on
161 ctx->proc->hotplug(ctx->proc, dpy, connected);
162 break;
Saurabh Shahcf053c62012-12-13 12:32:55 -0800163 }
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800164 case EXTERNAL_ONLINE:
165 { // connect case
Saurabh Shah188e4332013-07-18 10:33:22 -0700166 {
167 //Force composition to give up resources like pipes and
168 //close fb. For example if assertive display is going on,
169 //fb2 could be open, thus connecting Layer Mixer#0 to
170 //WriteBack module. If HDMI attempts to open fb1, the driver
171 //will try to attach Layer Mixer#0 to HDMI INT, which will
172 //fail, since Layer Mixer#0 is still connected to WriteBack.
173 //This block will force composition to close fb2 in above
174 //example.
175 Locker::Autolock _l(ctx->mExtLock);
176 ctx->mExtDispConfiguring = true;
177 ctx->dpyAttr[dpy].connected = false;
178 ctx->proc->invalidate(ctx->proc);
179 }
180 //2 cycles for slower content
181 usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period
182 * 2 / 1000);
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800183 const char *s1 = udata + strlen(HWC_UEVENT_SWITCH_STR);
184 if(!strncmp(s1,"hdmi",strlen(s1))) {
185 // hdmi online event..!
186 // check if WFD is configured
187 if(ctx->mExtDisplay->isWFDActive()) {
188 ALOGD_IF(UEVENT_DEBUG,"Received HDMI connection request"
189 "when WFD is active");
190 // teardown Active WFD Display
191 ctx->mExtDisplay->teardownWFDDisplay();
192 {
Raj kamale3e68ae2013-05-02 17:28:23 +0530193 Locker::Autolock _l(ctx->mExtLock);
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800194 clear(ctx, dpy);
195 //send hotplug disconnect event
196 ALOGD_IF(UEVENT_DEBUG, "sending hotplug: disconnect"
197 "for WFD");
198 // hwc comp could be on
199 ctx->proc->hotplug(ctx->proc, dpy, EXTERNAL_OFFLINE);
200 }
201 //Invalidate
202 ctx->proc->invalidate(ctx->proc);
203 //wait for 1 second
204 ALOGE_IF(UEVENT_DEBUG, "wait for 1 second -- padding"
205 "round");
206 sleep(1);
207 }
208 ctx->mExtDisplay->configureHDMIDisplay();
209 } else if(!strncmp(s1,"wfd",strlen(s1))) {
210 // wfd online event..!
211 ctx->mExtDisplay->configureWFDDisplay();
212 }
Saurabh Shah188e4332013-07-18 10:33:22 -0700213 {
214 Locker::Autolock _l(ctx->mExtLock);
215 ctx->dpyAttr[dpy].isPause = false;
216 setup(ctx, dpy, usecopybit);
217 ALOGD("%s sending hotplug: connected = %d", __FUNCTION__,
218 connected);
219 ctx->dpyAttr[dpy].connected = true;
220 ctx->proc->hotplug(ctx->proc, dpy, connected);
221 }
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800222 break;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800223 }
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800224 case EXTERNAL_PAUSE:
225 { // pause case
226 ALOGD("%s Received Pause event",__FUNCTION__);
Saurabh Shah567788b2013-07-11 10:19:52 -0700227 Locker::Autolock _l(ctx->mExtLock);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800228 ctx->dpyAttr[dpy].isActive = true;
229 ctx->dpyAttr[dpy].isPause = true;
230 break;
231 }
232 case EXTERNAL_RESUME:
233 { // resume case
234 ALOGD("%s Received resume event",__FUNCTION__);
Saurabh Shah188e4332013-07-18 10:33:22 -0700235 //Treat Resume as Online event
236 //Since external didnt have any pipes, force primary to give up
237 //its pipes; we don't allow inter-mixer pipe transfers.
238 {
239 Locker::Autolock _l(ctx->mExtLock);
240 ctx->mExtDispConfiguring = true;
241 ctx->dpyAttr[dpy].isActive = true;
242 ctx->proc->invalidate(ctx->proc);
243 }
244 usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period
245 * 2 / 1000);
246 //At this point external has all the pipes it would need.
247 {
248 Locker::Autolock _l(ctx->mExtLock);
249 ctx->dpyAttr[dpy].isPause = false;
250 ctx->proc->invalidate(ctx->proc);
251 }
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800252 break;
253 }
254 default:
255 {
256 ALOGE("ignore event and connected:%d",connected);
257 break;
258 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700259 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700260}
261
262static void *uevent_loop(void *param)
263{
264 int len = 0;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400265 static char udata[PAGE_SIZE];
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700266 hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700267 char thread_name[64] = HWC_UEVENT_THREAD_NAME;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400268 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700269 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
270 uevent_init();
271
272 while(1) {
273 len = uevent_next_event(udata, sizeof(udata) - 2);
274 handle_uevent(ctx, udata, len);
275 }
276
277 return NULL;
278}
279
280void init_uevent_thread(hwc_context_t* ctx)
281{
282 pthread_t uevent_thread;
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700283 int ret;
284
285 ALOGI("Initializing UEVENT Thread");
286 ret = pthread_create(&uevent_thread, NULL, uevent_loop, (void*) ctx);
287 if (ret) {
288 ALOGE("%s: failed to create %s: %s", __FUNCTION__,
289 HWC_UEVENT_THREAD_NAME, strerror(ret));
290 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700291}
292
293}; //namespace