1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "hwc-drm-event-listener"
18
19 #include "drmeventlistener.h"
20 #include "drmresources.h"
21
22 #include <linux/netlink.h>
23 #include <sys/socket.h>
24
25 #include <cutils/log.h>
26 #include <xf86drm.h>
27
28 namespace android {
29
DrmEventListener(DrmResources * drm)30 DrmEventListener::DrmEventListener(DrmResources *drm)
31 : Worker("drm-event-listener", HAL_PRIORITY_URGENT_DISPLAY),
32 drm_(drm) {
33 }
34
Init()35 int DrmEventListener::Init() {
36 uevent_fd_.Set(socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT));
37 if (uevent_fd_.get() < 0) {
38 ALOGE("Failed to open uevent socket %d", uevent_fd_.get());
39 return uevent_fd_.get();
40 }
41
42 struct sockaddr_nl addr;
43 memset(&addr, 0, sizeof(addr));
44 addr.nl_family = AF_NETLINK;
45 addr.nl_pid = getpid();
46 addr.nl_groups = 0xFFFFFFFF;
47
48 int ret = bind(uevent_fd_.get(), (struct sockaddr *)&addr, sizeof(addr));
49 if (ret) {
50 ALOGE("Failed to bind uevent socket %d", -errno);
51 return -errno;
52 }
53
54 FD_ZERO(&fds_);
55 FD_SET(drm_->fd(), &fds_);
56 FD_SET(uevent_fd_.get(), &fds_);
57 max_fd_ = std::max(drm_->fd(), uevent_fd_.get());
58
59 return InitWorker();
60 }
61
RegisterHotplugHandler(DrmEventHandler * handler)62 void DrmEventListener::RegisterHotplugHandler(DrmEventHandler *handler) {
63 assert(!hotplug_handler_);
64 hotplug_handler_ = handler;
65 }
66
FlipHandler(int,unsigned int,unsigned int tv_sec,unsigned int tv_usec,void * user_data)67 void DrmEventListener::FlipHandler(int /* fd */, unsigned int /* sequence */,
68 unsigned int tv_sec, unsigned int tv_usec,
69 void *user_data) {
70 DrmEventHandler *handler = (DrmEventHandler *)user_data;
71 if (!handler)
72 return;
73
74 handler->HandleEvent((uint64_t)tv_sec * 1000 * 1000 + tv_usec);
75 delete handler;
76 }
77
UEventHandler()78 void DrmEventListener::UEventHandler() {
79 char buffer[1024];
80 int ret;
81
82 struct timespec ts;
83 uint64_t timestamp = 0;
84 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
85 if (!ret)
86 timestamp = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
87 else
88 ALOGE("Failed to get monotonic clock on hotplug %d", ret);
89
90 while (true) {
91 ret = read(uevent_fd_.get(), &buffer, sizeof(buffer));
92 if (ret == 0) {
93 return;
94 } else if (ret < 0) {
95 ALOGE("Got error reading uevent %d", ret);
96 return;
97 }
98
99 if (!hotplug_handler_)
100 continue;
101
102 bool drm_event = false, hotplug_event = false;
103 for (int i = 0; i < ret;) {
104 char *event = buffer + i;
105 if (strcmp(event, "DEVTYPE=drm_minor"))
106 drm_event = true;
107 else if (strcmp(event, "HOTPLUG=1"))
108 hotplug_event = true;
109
110 i += strlen(event) + 1;
111 }
112
113 if (drm_event && hotplug_event)
114 hotplug_handler_->HandleEvent(timestamp);
115 }
116 }
117
Routine()118 void DrmEventListener::Routine() {
119 int ret;
120 do {
121 ret = select(max_fd_ + 1, &fds_, NULL, NULL, NULL);
122 } while (ret == -1 && errno == EINTR);
123
124 if (FD_ISSET(drm_->fd(), &fds_)) {
125 drmEventContext event_context = {
126 .version = DRM_EVENT_CONTEXT_VERSION,
127 .vblank_handler = NULL,
128 .page_flip_handler = DrmEventListener::FlipHandler};
129 drmHandleEvent(drm_->fd(), &event_context);
130 }
131
132 if (FD_ISSET(uevent_fd_.get(), &fds_))
133 UEventHandler();
134 }
135 }
136