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