blob: c6d00cbd55ee4e2bfdccde1eb90dff31fab48d9b [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"
Saurabh Shahacf10202013-02-26 10:15:15 -080030#include "hwc_video.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"
Naseer Ahmed72cf9762012-07-21 12:17:13 -070034
35namespace qhwc {
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -080036#define HWC_UEVENT_SWITCH_STR "change@/devices/virtual/switch/"
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070037#define HWC_UEVENT_THREAD_NAME "hwcUeventThread"
38
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -080039/* External Display states */
40enum {
41 EXTERNAL_OFFLINE = 0,
42 EXTERNAL_ONLINE,
43 EXTERNAL_PAUSE,
44 EXTERNAL_RESUME
45};
46
47static bool isHDMI(const char* str)
48{
49 if(strcasestr("change@/devices/virtual/switch/hdmi", str))
50 return true;
51 return false;
52}
53
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -080054static void setup(hwc_context_t* ctx, int dpy, bool usecopybit)
55{
56 ctx->mFBUpdate[dpy] =
57 IFBUpdate::getObject(ctx->dpyAttr[dpy].xres, dpy);
58 if(usecopybit)
59 ctx->mCopyBit[dpy] = new CopyBit();
60 ctx->mVidOv[dpy] =
61 IVideoOverlay::getObject(ctx->dpyAttr[dpy].xres, dpy);
62}
63
64static void clear(hwc_context_t* ctx, int dpy)
65{
66 if(ctx->mFBUpdate[dpy]) {
67 delete ctx->mFBUpdate[dpy];
68 ctx->mFBUpdate[dpy] = NULL;
69 }
70 if(ctx->mCopyBit[dpy]){
71 delete ctx->mCopyBit[dpy];
72 ctx->mCopyBit[dpy] = NULL;
73 }
74 if(ctx->mVidOv[dpy]) {
75 delete ctx->mVidOv[dpy];
76 ctx->mVidOv[dpy] = NULL;
77 }
78}
79
Naseer Ahmed72cf9762012-07-21 12:17:13 -070080static void handle_uevent(hwc_context_t* ctx, const char* udata, int len)
81{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -070082 int vsync = 0;
Naseer Ahmed72cf9762012-07-21 12:17:13 -070083 int64_t timestamp = 0;
84 const char *str = udata;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080085 bool usecopybit = false;
86 int compositionType =
87 qdutils::QCCompositionType::getInstance().getCompositionType();
88
89 if (compositionType & (qdutils::COMPOSITION_TYPE_DYN |
90 qdutils::COMPOSITION_TYPE_MDP |
91 qdutils::COMPOSITION_TYPE_C2D)) {
92 usecopybit = true;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080093 }
Amara Venkata Mastan Manoj Kumare37e5712012-12-17 17:36:02 -080094 if(!strcasestr("change@/devices/virtual/switch/hdmi", str) &&
95 !strcasestr("change@/devices/virtual/switch/wfd", str)) {
Saurabh Shah649cda62012-09-16 16:05:58 -070096 ALOGD_IF(UEVENT_DEBUG, "%s: Not Ext Disp Event ", __FUNCTION__);
Naseer Ahmed72cf9762012-07-21 12:17:13 -070097 return;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -070098 }
Arun Kumar K.Rf6f49a12012-11-27 11:46:50 -080099 int connected = -1; // initial value - will be set to 1/0 based on hotplug
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800100 int extDpyNum = HWC_DISPLAY_EXTERNAL;
101 char property[PROPERTY_VALUE_MAX];
102 if((property_get("persist.sys.wfd.virtual", property, NULL) > 0) &&
103 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
104 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
105 // This means we are using Google API to trigger WFD Display
106 extDpyNum = HWC_DISPLAY_VIRTUAL;
107
108 }
109
110 int dpy = isHDMI(str) ? HWC_DISPLAY_EXTERNAL : extDpyNum;
111
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800112 // parse HDMI/WFD switch state for connect/disconnect
113 // for HDMI:
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400114 // The event will be of the form:
Arun Kumar K.Rf6f49a12012-11-27 11:46:50 -0800115 // change@/devices/virtual/switch/hdmi ACTION=change
116 // SWITCH_STATE=1 or SWITCH_STATE=0
117 while(*str) {
118 if (!strncmp(str, "SWITCH_STATE=", strlen("SWITCH_STATE="))) {
119 connected = atoi(str + strlen("SWITCH_STATE="));
Saurabh Shah1a8cda02012-10-12 17:00:39 -0700120 //Disabled until SF calls unblank
121 ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive = false;
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800122 //Ignored for Virtual Displays
123 //ToDo: we can do this in a much better way
124 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isActive = true;
Arun Kumar K.Rf6f49a12012-11-27 11:46:50 -0800125 break;
Saurabh Shah1a8cda02012-10-12 17:00:39 -0700126 }
Arun Kumar K.Rf6f49a12012-11-27 11:46:50 -0800127 str += strlen(str) + 1;
128 if (str - udata >= len)
129 break;
130 }
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800131 ALOGD_IF(UEVENT_DEBUG, "Received str:%s",udata);
Arun Kumar K.Rf6f49a12012-11-27 11:46:50 -0800132
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800133 if(connected != EXTERNAL_ONLINE) {
134 if(ctx->mExtDisplay->ignoreRequest(udata)) {
135 ALOGD_IF(UEVENT_DEBUG,"No need to process this connection request"
136 "str:%s",udata);
137 ctx->dpyAttr[dpy].isActive = true;
138 return;
139 }
140 }
141
142 // update extDpyNum
143 ctx->mExtDisplay->setExtDpyNum(dpy);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800144 switch(connected) {
145 case EXTERNAL_OFFLINE:
146 { // disconnect event
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800147 const char *s1 = udata + strlen(HWC_UEVENT_SWITCH_STR);
148 if(!strncmp(s1,"hdmi",strlen(s1))) {
149 ctx->mExtDisplay->teardownHDMIDisplay();
150 }else if(!strncmp(s1,"wfd",strlen(s1))) {
151 ctx->mExtDisplay->teardownWFDDisplay();
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800152 }
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800153 Locker::Autolock _l(ctx->mExtSetLock);
154 clear(ctx, dpy);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800155 ALOGD("%s sending hotplug: connected = %d and dpy:%d",
156 __FUNCTION__, connected, dpy);
157 ctx->dpyAttr[dpy].connected = false;
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800158 //hwc comp could be on
159 ctx->proc->hotplug(ctx->proc, dpy, connected);
160 break;
Saurabh Shahcf053c62012-12-13 12:32:55 -0800161 }
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800162 case EXTERNAL_ONLINE:
163 { // connect case
164 ctx->mExtDispConfiguring = true;
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800165 const char *s1 = udata + strlen(HWC_UEVENT_SWITCH_STR);
166 if(!strncmp(s1,"hdmi",strlen(s1))) {
167 // hdmi online event..!
168 // check if WFD is configured
169 if(ctx->mExtDisplay->isWFDActive()) {
170 ALOGD_IF(UEVENT_DEBUG,"Received HDMI connection request"
171 "when WFD is active");
172 // teardown Active WFD Display
173 ctx->mExtDisplay->teardownWFDDisplay();
174 {
175 Locker::Autolock _l(ctx->mExtSetLock);
176 clear(ctx, dpy);
177 //send hotplug disconnect event
178 ALOGD_IF(UEVENT_DEBUG, "sending hotplug: disconnect"
179 "for WFD");
180 // hwc comp could be on
181 ctx->proc->hotplug(ctx->proc, dpy, EXTERNAL_OFFLINE);
182 }
183 //Invalidate
184 ctx->proc->invalidate(ctx->proc);
185 //wait for 1 second
186 ALOGE_IF(UEVENT_DEBUG, "wait for 1 second -- padding"
187 "round");
188 sleep(1);
189 }
190 ctx->mExtDisplay->configureHDMIDisplay();
191 } else if(!strncmp(s1,"wfd",strlen(s1))) {
192 // wfd online event..!
193 ctx->mExtDisplay->configureWFDDisplay();
194 }
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800195 ctx->dpyAttr[dpy].isPause = false;
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800196 setup(ctx, dpy, usecopybit);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800197 ALOGD("%s sending hotplug: connected = %d", __FUNCTION__,
198 connected);
199 ctx->dpyAttr[dpy].connected = true;
200 Locker::Autolock _l(ctx->mExtSetLock); //hwc comp could be on
201 ctx->proc->hotplug(ctx->proc, dpy, connected);
202 break;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800203 }
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800204 case EXTERNAL_PAUSE:
205 { // pause case
206 ALOGD("%s Received Pause event",__FUNCTION__);
207 ctx->dpyAttr[dpy].isActive = true;
208 ctx->dpyAttr[dpy].isPause = true;
209 break;
210 }
211 case EXTERNAL_RESUME:
212 { // resume case
213 ALOGD("%s Received resume event",__FUNCTION__);
214 ctx->dpyAttr[dpy].isActive = true;
215 ctx->dpyAttr[dpy].isPause = false;
216 break;
217 }
218 default:
219 {
220 ALOGE("ignore event and connected:%d",connected);
221 break;
222 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700223 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700224}
225
226static void *uevent_loop(void *param)
227{
228 int len = 0;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400229 static char udata[PAGE_SIZE];
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700230 hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700231 char thread_name[64] = HWC_UEVENT_THREAD_NAME;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400232 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700233 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
234 uevent_init();
235
236 while(1) {
237 len = uevent_next_event(udata, sizeof(udata) - 2);
238 handle_uevent(ctx, udata, len);
239 }
240
241 return NULL;
242}
243
244void init_uevent_thread(hwc_context_t* ctx)
245{
246 pthread_t uevent_thread;
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700247 int ret;
248
249 ALOGI("Initializing UEVENT Thread");
250 ret = pthread_create(&uevent_thread, NULL, uevent_loop, (void*) ctx);
251 if (ret) {
252 ALOGE("%s: failed to create %s: %s", __FUNCTION__,
253 HWC_UEVENT_THREAD_NAME, strerror(ret));
254 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700255}
256
257}; //namespace