• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "mdp_version.h"
35 
36 namespace qhwc {
37 
38 #define HWC_UEVENT_THREAD_NAME "hwcUeventThread"
39 
40 /* External Display states */
41 enum {
42     EXTERNAL_OFFLINE = 0,
43     EXTERNAL_ONLINE,
44     EXTERNAL_PAUSE,
45     EXTERNAL_RESUME
46 };
47 
isHDMI(const char * str)48 static bool isHDMI(const char* str)
49 {
50     if(strcasestr("change@/devices/virtual/switch/hdmi", str))
51         return true;
52     return false;
53 }
54 
handle_uevent(hwc_context_t * ctx,const char * udata,int len)55 static void handle_uevent(hwc_context_t* ctx, const char* udata, int len)
56 {
57     const char *str = udata;
58 
59     if(!strcasestr("change@/devices/virtual/switch/hdmi", str) &&
60        !strcasestr("change@/devices/virtual/switch/wfd", str)) {
61         ALOGD_IF(UEVENT_DEBUG, "%s: Not Ext Disp Event ", __FUNCTION__);
62         return;
63     }
64     int connected = -1; // initial value - will be set to  1/0 based on hotplug
65     int extDpyNum = HWC_DISPLAY_EXTERNAL;
66     char property[PROPERTY_VALUE_MAX];
67     if((property_get("persist.sys.wfd.virtual", property, NULL) > 0) &&
68             (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
69              (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
70         // This means we are using Google API to trigger WFD Display
71         extDpyNum = HWC_DISPLAY_VIRTUAL;
72 
73     }
74 
75     int dpy = isHDMI(str) ? HWC_DISPLAY_EXTERNAL : extDpyNum;
76 
77     // update extDpyNum
78     ctx->mExtDisplay->setExtDpyNum(dpy);
79 
80     // parse HDMI/WFD switch state for connect/disconnect
81     // for HDMI:
82     // The event will be of the form:
83     // change@/devices/virtual/switch/hdmi ACTION=change
84     // SWITCH_STATE=1 or SWITCH_STATE=0
85     while(*str) {
86         if (!strncmp(str, "SWITCH_STATE=", strlen("SWITCH_STATE="))) {
87             connected = atoi(str + strlen("SWITCH_STATE="));
88             //Disabled until SF calls unblank
89             ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive = false;
90             //Ignored for Virtual Displays
91             //ToDo: we can do this in a much better way
92             ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isActive = true;
93             break;
94         }
95         str += strlen(str) + 1;
96         if (str - udata >= len)
97             break;
98     }
99 
100     switch(connected) {
101         case EXTERNAL_OFFLINE:
102             {   // disconnect event
103                 ctx->mExtDisplay->processUEventOffline(udata);
104                 Locker::Autolock _l(ctx->mDrawLock);
105                 clearSecondaryObjs(ctx, dpy);
106                 ALOGD("%s sending hotplug: connected = %d and dpy:%d",
107                       __FUNCTION__, connected, dpy);
108                 ctx->dpyAttr[dpy].connected = false;
109                 //hwc comp could be on
110                 ctx->proc->hotplug(ctx->proc, dpy, connected);
111                 break;
112             }
113         case EXTERNAL_ONLINE:
114             {   // connect case
115                 {
116                     //Force composition to give up resources like pipes and
117                     //close fb. For example if assertive display is going on,
118                     //fb2 could be open, thus connecting Layer Mixer#0 to
119                     //WriteBack module. If HDMI attempts to open fb1, the driver
120                     //will try to attach Layer Mixer#0 to HDMI INT, which will
121                     //fail, since Layer Mixer#0 is still connected to WriteBack.
122                     //This block will force composition to close fb2 in above
123                     //example.
124                     Locker::Autolock _l(ctx->mDrawLock);
125                     ctx->dpyAttr[dpy].isConfiguring = true;
126                     ctx->dpyAttr[dpy].connected = false;
127                     ctx->proc->invalidate(ctx->proc);
128                 }
129                 //2 cycles for slower content
130                 usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period
131                         * 2 / 1000);
132                 ctx->mExtDisplay->processUEventOnline(udata);
133                 {
134                     Locker::Autolock _l(ctx->mDrawLock);
135                     ctx->dpyAttr[dpy].isPause = false;
136                     setupSecondaryObjs(ctx, dpy);
137                     ALOGD("%s sending hotplug: connected = %d", __FUNCTION__,
138                             connected);
139                     ctx->dpyAttr[dpy].connected = true;
140                     ctx->proc->hotplug(ctx->proc, dpy, connected);
141                 }
142                 break;
143             }
144         case EXTERNAL_PAUSE:
145             {   // pause case
146                 ALOGD("%s Received Pause event",__FUNCTION__);
147                 Locker::Autolock _l(ctx->mDrawLock);
148                 ctx->dpyAttr[dpy].isActive = true;
149                 ctx->dpyAttr[dpy].isPause = true;
150                 break;
151             }
152         case EXTERNAL_RESUME:
153             {  // resume case
154                 ALOGD("%s Received resume event",__FUNCTION__);
155                 //Treat Resume as Online event
156                 //Since external didnt have any pipes, force primary to give up
157                 //its pipes; we don't allow inter-mixer pipe transfers.
158                 {
159                     Locker::Autolock _l(ctx->mDrawLock);
160                     ctx->dpyAttr[dpy].isConfiguring = true;
161                     ctx->dpyAttr[dpy].isActive = true;
162                     ctx->proc->invalidate(ctx->proc);
163                 }
164                 usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period
165                         * 2 / 1000);
166                 //At this point external has all the pipes it would need.
167                 {
168                     Locker::Autolock _l(ctx->mDrawLock);
169                     ctx->dpyAttr[dpy].isPause = false;
170                     ctx->proc->invalidate(ctx->proc);
171                 }
172                 break;
173             }
174         default:
175             {
176                 ALOGE("ignore event and connected:%d",connected);
177                 break;
178             }
179     }
180 }
181 
uevent_loop(void * param)182 static void *uevent_loop(void *param)
183 {
184     int len = 0;
185     static char udata[PAGE_SIZE];
186     hwc_context_t * ctx = reinterpret_cast<hwc_context_t *>(param);
187     char thread_name[64] = HWC_UEVENT_THREAD_NAME;
188     prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
189     setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
190     uevent_init();
191 
192     while(1) {
193         len = uevent_next_event(udata, sizeof(udata) - 2);
194         handle_uevent(ctx, udata, len);
195     }
196 
197     return NULL;
198 }
199 
init_uevent_thread(hwc_context_t * ctx)200 void init_uevent_thread(hwc_context_t* ctx)
201 {
202     pthread_t uevent_thread;
203     int ret;
204 
205     ALOGI("Initializing UEVENT Thread");
206     ret = pthread_create(&uevent_thread, NULL, uevent_loop, (void*) ctx);
207     if (ret) {
208         ALOGE("%s: failed to create %s: %s", __FUNCTION__,
209             HWC_UEVENT_THREAD_NAME, strerror(ret));
210     }
211 }
212 
213 }; //namespace
214