2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "hwc-drm-event-listener"
19 #include "drmeventlistener.h"
20 #include "drmresources.h"
24 #include <linux/netlink.h>
25 #include <sys/socket.h>
28 #include <hardware/hardware.h>
29 #include <hardware/hwcomposer.h>
34 DrmEventListener::DrmEventListener(DrmResources *drm)
35 : Worker("drm-event-listener", HAL_PRIORITY_URGENT_DISPLAY),
39 int DrmEventListener::Init() {
40 uevent_fd_.Set(socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT));
41 if (uevent_fd_.get() < 0) {
42 ALOGE("Failed to open uevent socket %d", uevent_fd_.get());
43 return uevent_fd_.get();
46 struct sockaddr_nl addr;
47 memset(&addr, 0, sizeof(addr));
48 addr.nl_family = AF_NETLINK;
50 addr.nl_groups = 0xFFFFFFFF;
52 int ret = bind(uevent_fd_.get(), (struct sockaddr *)&addr, sizeof(addr));
54 ALOGE("Failed to bind uevent socket %d", -errno);
59 FD_SET(drm_->fd(), &fds_);
60 FD_SET(uevent_fd_.get(), &fds_);
61 max_fd_ = std::max(drm_->fd(), uevent_fd_.get());
66 void DrmEventListener::RegisterHotplugHandler(DrmEventHandler *handler) {
67 assert(!hotplug_handler_);
68 hotplug_handler_ = handler;
71 void DrmEventListener::FlipHandler(int /* fd */, unsigned int /* sequence */,
72 unsigned int tv_sec, unsigned int tv_usec,
74 DrmEventHandler *handler = (DrmEventHandler *)user_data;
78 handler->HandleEvent((uint64_t)tv_sec * 1000 * 1000 + tv_usec);
82 void DrmEventListener::UEventHandler() {
87 uint64_t timestamp = 0;
88 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
90 timestamp = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
92 ALOGE("Failed to get monotonic clock on hotplug %d", ret);
95 ret = read(uevent_fd_.get(), &buffer, sizeof(buffer));
99 ALOGE("Got error reading uevent %d", ret);
103 if (!hotplug_handler_)
106 bool drm_event = false, hotplug_event = false;
107 for (int i = 0; i < ret;) {
108 char *event = buffer + i;
109 if (strcmp(event, "DEVTYPE=drm_minor"))
111 else if (strcmp(event, "HOTPLUG=1"))
112 hotplug_event = true;
114 i += strlen(event) + 1;
117 if (drm_event && hotplug_event)
118 hotplug_handler_->HandleEvent(timestamp);
122 void DrmEventListener::Routine() {
125 ret = select(max_fd_ + 1, &fds_, NULL, NULL, NULL);
126 } while (ret == -1 && errno == EINTR);
128 if (FD_ISSET(drm_->fd(), &fds_)) {
129 drmEventContext event_context = {
131 .vblank_handler = NULL,
132 .page_flip_handler = DrmEventListener::FlipHandler};
133 drmHandleEvent(drm_->fd(), &event_context);
136 if (FD_ISSET(uevent_fd_.get(), &fds_))