blob: 916b3460c0769e9fa42054b4da991bcff2bca8c1 [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"
Naseer Ahmed72cf9762012-07-21 12:17:13 -070036
37namespace 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 Shah67a38c32013-06-10 16:23:15 -070051 const int rSplit = 0; //Even split for external if at all
52 ctx->mFBUpdate[dpy] = IFBUpdate::getObject(ctx->dpyAttr[dpy].xres,
53 rSplit, dpy);
54 ctx->mMDPComp[dpy] = MDPComp::getObject(ctx->dpyAttr[dpy].xres,
55 rSplit, dpy);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070056 int compositionType =
57 qdutils::QCCompositionType::getInstance().getCompositionType();
58 if (compositionType & (qdutils::COMPOSITION_TYPE_DYN |
59 qdutils::COMPOSITION_TYPE_MDP |
60 qdutils::COMPOSITION_TYPE_C2D)) {
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -080061 ctx->mCopyBit[dpy] = new CopyBit();
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070062 }
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -080063}
64
65static void clear(hwc_context_t* ctx, int dpy)
66{
67 if(ctx->mFBUpdate[dpy]) {
68 delete ctx->mFBUpdate[dpy];
69 ctx->mFBUpdate[dpy] = NULL;
70 }
71 if(ctx->mCopyBit[dpy]){
72 delete ctx->mCopyBit[dpy];
73 ctx->mCopyBit[dpy] = NULL;
74 }
Jeykumar Sankaran85977e32013-02-25 17:06:08 -080075 if(ctx->mMDPComp[dpy]) {
76 delete ctx->mMDPComp[dpy];
77 ctx->mMDPComp[dpy] = NULL;
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -080078 }
79}
80
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070081/* Parse uevent data for devices which we are interested */
82static int getConnectedDisplay(const char* strUdata)
83{
84 if(strcasestr("change@/devices/virtual/switch/hdmi", strUdata))
85 return HWC_DISPLAY_EXTERNAL;
86 if(strcasestr("change@/devices/virtual/switch/wfd", strUdata))
87 return HWC_DISPLAY_VIRTUAL;
88 return -1;
89}
90
91/* Parse uevent data for action requested for the display */
92static int getConnectedState(const char* strUdata, int len)
93{
94 const char* iter_str = strUdata;
95 while(((iter_str - strUdata) <= len) && (*iter_str)) {
96 char* pstr = strstr(iter_str, "SWITCH_STATE=");
97 if (pstr != NULL) {
98 return (atoi(pstr + strlen("SWITCH_STATE=")));
99 }
100 iter_str += strlen(iter_str)+1;
101 }
102 return -1;
103}
104
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700105static void handle_uevent(hwc_context_t* ctx, const char* udata, int len)
106{
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700107 int dpy = getConnectedDisplay(udata);
108 if(dpy < 0) {
109 ALOGD_IF(UEVENT_DEBUG, "%s: Not disp Event ", __FUNCTION__);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700110 return;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700111 }
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800112
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700113 int switch_state = getConnectedState(udata, len);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800114
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700115 ALOGE_IF(UEVENT_DEBUG,"%s: uevent recieved: %s switch state: %d",
116 __FUNCTION__,udata, switch_state);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800117
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700118 switch(switch_state) {
119 case EXTERNAL_OFFLINE:
120 {
121 /* Display not connected */
122 if(!ctx->dpyAttr[dpy].connected){
123 ALOGE_IF(UEVENT_DEBUG,"%s: Ignoring EXTERNAL_OFFLINE event"
124 "for display: %d", __FUNCTION__, dpy);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800125 break;
Saurabh Shahcf053c62012-12-13 12:32:55 -0800126 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700127
Saurabh Shahb39f8152013-08-22 10:21:44 -0700128 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700129 clear(ctx, dpy);
130 ctx->dpyAttr[dpy].connected = false;
131 ctx->dpyAttr[dpy].isActive = false;
132
133 if(dpy == HWC_DISPLAY_EXTERNAL) {
134 ctx->mExtDisplay->teardown();
135 } else {
136 ctx->mVirtualDisplay->teardown();
137 }
138
139 /* We need to send hotplug to SF only when we are disconnecting
140 * (1) HDMI OR (2) proprietary WFD session */
141 if(dpy == HWC_DISPLAY_EXTERNAL ||
142 ctx->mVirtualonExtActive) {
143 ALOGE_IF(UEVENT_DEBUG,"%s:Sending EXTERNAL OFFLINE hotplug"
144 "event", __FUNCTION__);
145 ctx->proc->hotplug(ctx->proc, HWC_DISPLAY_EXTERNAL,
146 EXTERNAL_OFFLINE);
147 }
148 break;
149 }
150 case EXTERNAL_ONLINE:
151 {
152 /* Display already connected */
153 if(ctx->dpyAttr[dpy].connected) {
154 ALOGE_IF(UEVENT_DEBUG,"%s: Ignoring EXTERNAL_ONLINE event"
155 "for display: %d", __FUNCTION__, dpy);
156 break;
157 }
158 {
159 //Force composition to give up resources like pipes and
160 //close fb. For example if assertive display is going on,
161 //fb2 could be open, thus connecting Layer Mixer#0 to
162 //WriteBack module. If HDMI attempts to open fb1, the driver
163 //will try to attach Layer Mixer#0 to HDMI INT, which will
164 //fail, since Layer Mixer#0 is still connected to WriteBack.
165 //This block will force composition to close fb2 in above
166 //example.
Saurabh Shahb39f8152013-08-22 10:21:44 -0700167 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700168 ctx->dpyAttr[dpy].isConfiguring = true;
169 ctx->proc->invalidate(ctx->proc);
170 }
171 //2 cycles for slower content
172 usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period
173 * 2 / 1000);
174
175 if(dpy == HWC_DISPLAY_EXTERNAL) {
176 if(ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].connected) {
177 ALOGD_IF(UEVENT_DEBUG,"Received HDMI connection request"
178 "when WFD is active");
179 {
Saurabh Shahb39f8152013-08-22 10:21:44 -0700180 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700181 clear(ctx, HWC_DISPLAY_VIRTUAL);
182 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].connected = false;
183 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isActive = false;
184 }
185
186 ctx->mVirtualDisplay->teardown();
187
188 /* Need to send hotplug only when connected WFD in
189 * proprietary path */
190 if(ctx->mVirtualonExtActive) {
191 ALOGE_IF(UEVENT_DEBUG,"%s: Sending EXTERNAL OFFLINE"
192 "hotplug event", __FUNCTION__);
193 ctx->proc->hotplug(ctx->proc, HWC_DISPLAY_EXTERNAL,
194 EXTERNAL_OFFLINE);
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800195 {
Saurabh Shahb39f8152013-08-22 10:21:44 -0700196 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700197 ctx->mVirtualonExtActive = false;
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800198 }
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800199 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700200 /* Wait for few frames for SF to tear down
201 * the WFD session. */
202 usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period
203 * 2 / 1000);
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800204 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700205 ctx->mExtDisplay->configure();
206 } else {
Saurabh Shah188e4332013-07-18 10:33:22 -0700207 {
Saurabh Shahb39f8152013-08-22 10:21:44 -0700208 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700209 /* TRUE only when we are on proprietary WFD session */
210 ctx->mVirtualonExtActive = true;
211 char property[PROPERTY_VALUE_MAX];
212 if((property_get("persist.sys.wfd.virtual",
213 property, NULL) > 0) &&
214 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
215 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
216 // This means we are on Google's WFD session
217 ctx->mVirtualonExtActive = false;
218 }
Saurabh Shah188e4332013-07-18 10:33:22 -0700219 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700220 ctx->mVirtualDisplay->configure();
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800221 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700222
Saurabh Shahb39f8152013-08-22 10:21:44 -0700223 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700224 setup(ctx, dpy);
225 ctx->dpyAttr[dpy].isPause = false;
226 ctx->dpyAttr[dpy].connected = true;
227 ctx->dpyAttr[dpy].isConfiguring = true;
228
229 if(dpy == HWC_DISPLAY_VIRTUAL) {
230 /* We wont be getting unblank for VIRTUAL DISPLAY and its
231 * always guaranteed from WFD stack that CONNECT uevent for
232 * VIRTUAL DISPLAY will be triggered before creating
233 * surface for the same. */
234 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isActive = true;
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800235 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700236
237 if(dpy == HWC_DISPLAY_EXTERNAL ||
238 ctx->mVirtualonExtActive) {
239 ALOGE_IF(UEVENT_DEBUG, "%s: Sending EXTERNAL_OFFLINE ONLINE"
240 "hotplug event", __FUNCTION__);
241 ctx->proc->hotplug(ctx->proc,HWC_DISPLAY_EXTERNAL,
242 EXTERNAL_ONLINE);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800243 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700244 break;
245 }
246 case EXTERNAL_PAUSE:
247 {
248 ctx->dpyAttr[dpy].isActive = true;
249 ctx->dpyAttr[dpy].isPause = true;
250 break;
251 }
252 case EXTERNAL_RESUME:
253 {
254 //Treat Resume as Online event
255 //Since external didnt have any pipes, force primary to give up
256 //its pipes; we don't allow inter-mixer pipe transfers.
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800257 {
Saurabh Shahb39f8152013-08-22 10:21:44 -0700258 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700259 ctx->dpyAttr[dpy].isConfiguring = true;
260 ctx->dpyAttr[dpy].isActive = true;
261 ctx->proc->invalidate(ctx->proc);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800262 }
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700263 usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period
264 * 2 / 1000);
265 {
266 //At this point external has all the pipes it would need.
Saurabh Shahb39f8152013-08-22 10:21:44 -0700267 Locker::Autolock _l(ctx->mDrawLock);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700268 ctx->dpyAttr[dpy].isPause = false;
269 ctx->proc->invalidate(ctx->proc);
270 }
271 break;
272 }
273 default:
274 {
275 ALOGE("%s: Invalid state to swtich:%d", __FUNCTION__, switch_state);
276 break;
277 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700278 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700279}
280
281static void *uevent_loop(void *param)
282{
283 int len = 0;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400284 static char udata[PAGE_SIZE];
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700285 hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700286 char thread_name[64] = HWC_UEVENT_THREAD_NAME;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400287 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700288 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
289 uevent_init();
290
291 while(1) {
292 len = uevent_next_event(udata, sizeof(udata) - 2);
293 handle_uevent(ctx, udata, len);
294 }
295
296 return NULL;
297}
298
299void init_uevent_thread(hwc_context_t* ctx)
300{
301 pthread_t uevent_thread;
Iliyan Malchev0f9c3972012-10-11 15:16:15 -0700302 int ret;
303
304 ALOGI("Initializing UEVENT Thread");
305 ret = pthread_create(&uevent_thread, NULL, uevent_loop, (void*) ctx);
306 if (ret) {
307 ALOGE("%s: failed to create %s: %s", __FUNCTION__,
308 HWC_UEVENT_THREAD_NAME, strerror(ret));
309 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700310}
311
312}; //namespace