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