1 
2 /*
3  * Copyright (C) 2010 The Android Open Source Project
4  * Copyright (C) 2012, The Linux Foundation. All rights reserved.
5  *
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  */
21 #define UEVENT_DEBUG 0
22 #include <hardware_legacy/uevent.h>
23 #include <utils/Log.h>
24 #include <sys/resource.h>
25 #include <sys/prctl.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include "hwc_utils.h"
29 #include "hwc_fbupdate.h"
30 #include "hwc_mdpcomp.h"
31 #include "hwc_copybit.h"
32 #include "comptype.h"
33 #include "external.h"
34 
35 namespace qhwc {
36 
37 #define HWC_UEVENT_THREAD_NAME "hwcUeventThread"
38 
39 /* External Display states */
40 enum {
41     EXTERNAL_OFFLINE = 0,
42     EXTERNAL_ONLINE,
43     EXTERNAL_PAUSE,
44     EXTERNAL_RESUME
45 };
46 
isHDMI(const char * str)47 static bool isHDMI(const char* str)
48 {
49     if(strcasestr("change@/devices/virtual/switch/hdmi", str))
50         return true;
51     return false;
52 }
53 
handle_uevent(hwc_context_t * ctx,const char * udata,int len)54 static void handle_uevent(hwc_context_t* ctx, const char* udata, int len)
55 {
56     int vsync = 0;
57     int64_t timestamp = 0;
58     const char *str = udata;
59     bool usecopybit = false;
60     int compositionType =
61         qdutils::QCCompositionType::getInstance().getCompositionType();
62 
63     if (compositionType & (qdutils::COMPOSITION_TYPE_DYN |
64                            qdutils::COMPOSITION_TYPE_MDP |
65                            qdutils::COMPOSITION_TYPE_C2D)) {
66         usecopybit = true;
67     }
68 
69     if(!strcasestr("change@/devices/virtual/switch/hdmi", str) &&
70        !strcasestr("change@/devices/virtual/switch/wfd", str)) {
71         ALOGD_IF(UEVENT_DEBUG, "%s: Not Ext Disp Event ", __FUNCTION__);
72         return;
73     }
74     int connected = -1; // initial value - will be set to  1/0 based on hotplug
75     int extDpyNum = HWC_DISPLAY_EXTERNAL;
76     char property[PROPERTY_VALUE_MAX];
77     if((property_get("persist.sys.wfd.virtual", property, NULL) > 0) &&
78             (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
79              (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
80         // This means we are using Google API to trigger WFD Display
81         extDpyNum = HWC_DISPLAY_VIRTUAL;
82 
83     }
84 
85     int dpy = isHDMI(str) ? HWC_DISPLAY_EXTERNAL : extDpyNum;
86 
87     // update extDpyNum
88     ctx->mExtDisplay->setExtDpyNum(dpy);
89 
90     // parse HDMI/WFD switch state for connect/disconnect
91     // for HDMI:
92     // The event will be of the form:
93     // change@/devices/virtual/switch/hdmi ACTION=change
94     // SWITCH_STATE=1 or SWITCH_STATE=0
95     while(*str) {
96         if (!strncmp(str, "SWITCH_STATE=", strlen("SWITCH_STATE="))) {
97             connected = atoi(str + strlen("SWITCH_STATE="));
98             //Disabled until SF calls unblank
99             ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive = false;
100             //Ignored for Virtual Displays
101             //ToDo: we can do this in a much better way
102             ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isActive = true;
103             break;
104         }
105         str += strlen(str) + 1;
106         if (str - udata >= len)
107             break;
108     }
109 
110     switch(connected) {
111         case EXTERNAL_OFFLINE:
112             {   // disconnect event
113                 ctx->mExtDisplay->processUEventOffline(udata);
114                 if(ctx->mFBUpdate[dpy]) {
115                     Locker::Autolock _l(ctx->mExtSetLock);
116                     delete ctx->mFBUpdate[dpy];
117                     ctx->mFBUpdate[dpy] = NULL;
118                 }
119                 if(ctx->mCopyBit[dpy]){
120                     Locker::Autolock _l(ctx->mExtSetLock);
121                     delete ctx->mCopyBit[dpy];
122                     ctx->mCopyBit[dpy] = NULL;
123                 }
124                 if(ctx->mMDPComp[dpy]) {
125                     delete ctx->mMDPComp[dpy];
126                     ctx->mMDPComp[dpy] = NULL;
127                 }
128                 ALOGD("%s sending hotplug: connected = %d and dpy:%d",
129                       __FUNCTION__, connected, dpy);
130                 ctx->dpyAttr[dpy].connected = false;
131                 Locker::Autolock _l(ctx->mExtSetLock);
132                 //hwc comp could be on
133                 ctx->proc->hotplug(ctx->proc, dpy, connected);
134                 break;
135             }
136         case EXTERNAL_ONLINE:
137             {   // connect case
138                 ctx->mExtDispConfiguring = true;
139                 ctx->mExtDisplay->processUEventOnline(udata);
140                 ctx->mFBUpdate[dpy] =
141                         IFBUpdate::getObject(ctx->dpyAttr[dpy].xres, dpy);
142                 ctx->dpyAttr[dpy].isPause = false;
143                 if(usecopybit)
144                     ctx->mCopyBit[dpy] = new CopyBit();
145                 ctx->mMDPComp[dpy] =  MDPComp::getObject(
146                         ctx->dpyAttr[dpy].xres, dpy);
147                 ALOGD("%s sending hotplug: connected = %d", __FUNCTION__,
148                         connected);
149                 ctx->dpyAttr[dpy].connected = true;
150                 Locker::Autolock _l(ctx->mExtSetLock); //hwc comp could be on
151                 ctx->proc->hotplug(ctx->proc, dpy, connected);
152                 break;
153             }
154         case EXTERNAL_PAUSE:
155             {   // pause case
156                 ALOGD("%s Received Pause event",__FUNCTION__);
157                 // This is required to ensure that composition
158                 // fall back to FB, closing all MDP pipes.
159                 ctx->mExtDispConfiguring = true;
160                 ctx->dpyAttr[dpy].isActive = true;
161                 ctx->dpyAttr[dpy].isPause = true;
162                 break;
163             }
164         case EXTERNAL_RESUME:
165             {  // resume case
166                 ALOGD("%s Received resume event",__FUNCTION__);
167                 ctx->dpyAttr[dpy].isActive = true;
168                 ctx->dpyAttr[dpy].isPause = false;
169                 break;
170             }
171         default:
172             {
173                 ALOGE("ignore event and connected:%d",connected);
174                 break;
175             }
176     }
177 }
178 
uevent_loop(void * param)179 static void *uevent_loop(void *param)
180 {
181     int len = 0;
182     static char udata[PAGE_SIZE];
183     hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
184     char thread_name[64] = HWC_UEVENT_THREAD_NAME;
185     prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
186     setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
187     uevent_init();
188 
189     while(1) {
190         len = uevent_next_event(udata, sizeof(udata) - 2);
191         handle_uevent(ctx, udata, len);
192     }
193 
194     return NULL;
195 }
196 
init_uevent_thread(hwc_context_t * ctx)197 void init_uevent_thread(hwc_context_t* ctx)
198 {
199     pthread_t uevent_thread;
200     int ret;
201 
202     ALOGI("Initializing UEVENT Thread");
203     ret = pthread_create(&uevent_thread, NULL, uevent_loop, (void*) ctx);
204     if (ret) {
205         ALOGE("%s: failed to create %s: %s", __FUNCTION__,
206             HWC_UEVENT_THREAD_NAME, strerror(ret));
207     }
208 }
209 
210 }; //namespace
211