1 /* 2 * Copyright (C) 2019 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 #include "host/libs/wayland/wayland_server.h" 18 19 #include <android-base/logging.h> 20 21 #include <wayland-server-core.h> 22 #include <wayland-server-protocol.h> 23 24 #include "host/libs/wayland/wayland_compositor.h" 25 #include "host/libs/wayland/wayland_dmabuf.h" 26 #include "host/libs/wayland/wayland_seat.h" 27 #include "host/libs/wayland/wayland_shell.h" 28 #include "host/libs/wayland/wayland_subcompositor.h" 29 #include "host/libs/wayland/wayland_surface.h" 30 #include "host/libs/wayland/wayland_utils.h" 31 #include "host/libs/wayland/wayland_virtio_gpu_metadata.h" 32 33 namespace wayland { 34 namespace internal { 35 36 struct WaylandServerState { 37 struct wl_display* display_ = nullptr; 38 39 Surfaces surfaces_; 40 }; 41 42 } // namespace internal 43 WaylandServer(int wayland_socket_fd)44WaylandServer::WaylandServer(int wayland_socket_fd) { 45 server_thread_ = 46 std::thread( 47 [this, wayland_socket_fd]() { 48 ServerLoop(wayland_socket_fd); 49 }); 50 51 std::unique_lock<std::mutex> lock(server_ready_mutex_); 52 server_ready_cv_.wait(lock, [&]{return server_ready_; }); 53 } 54 ~WaylandServer()55WaylandServer::~WaylandServer() { 56 wl_display_terminate(server_state_->display_); 57 server_thread_.join(); 58 } 59 ServerLoop(int fd)60void WaylandServer::ServerLoop(int fd) { 61 server_state_.reset(new internal::WaylandServerState()); 62 63 server_state_->display_ = wl_display_create(); 64 CHECK(server_state_->display_ != nullptr) 65 << "Failed to start WaylandServer: failed to create display"; 66 67 if (fd < 0) { 68 const char* socket = wl_display_add_socket_auto(server_state_->display_); 69 CHECK(socket != nullptr) 70 << "Failed to start WaylandServer: failed to create socket"; 71 72 LOG(INFO) << "WaylandServer running on socket " << socket; 73 } else { 74 CHECK(wl_display_add_socket_fd(server_state_->display_, fd) == 0) 75 << "Failed to start WaylandServer: failed to use fd " << fd; 76 77 LOG(INFO) << "WaylandServer running on socket " << fd; 78 } 79 80 wl_display_init_shm(server_state_->display_); 81 82 BindCompositorInterface(server_state_->display_, &server_state_->surfaces_); 83 BindVirtioGpuMetadataInterface(server_state_->display_, 84 &server_state_->surfaces_); 85 BindDmabufInterface(server_state_->display_); 86 BindSubcompositorInterface(server_state_->display_); 87 BindSeatInterface(server_state_->display_); 88 BindShellInterface(server_state_->display_); 89 90 { 91 std::lock_guard<std::mutex> lock(server_ready_mutex_); 92 server_ready_ = true; 93 } 94 server_ready_cv_.notify_one(); 95 96 wl_display_run(server_state_->display_); 97 wl_display_destroy(server_state_->display_); 98 } 99 SetFrameCallback(Surfaces::FrameCallback callback)100void WaylandServer::SetFrameCallback(Surfaces::FrameCallback callback) { 101 server_state_->surfaces_.SetFrameCallback(std::move(callback)); 102 } 103 104 } // namespace wayland 105