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,bool wayland_frames_are_rgba)44 WaylandServer::WaylandServer(int wayland_socket_fd,
45                              bool wayland_frames_are_rgba) {
46   server_thread_ =
47       std::thread([this, wayland_socket_fd, wayland_frames_are_rgba]() {
48         ServerLoop(wayland_socket_fd, wayland_frames_are_rgba);
49       });
50 
51   std::unique_lock<std::mutex> lock(server_ready_mutex_);
52   server_ready_cv_.wait(lock, [&]{return server_ready_; });
53 }
54 
~WaylandServer()55 WaylandServer::~WaylandServer() {
56   wl_display_terminate(server_state_->display_);
57   server_thread_.join();
58 }
59 
ServerLoop(int fd,bool frames_are_rgba)60 void WaylandServer::ServerLoop(int fd, bool frames_are_rgba) {
61   server_state_.reset(new internal::WaylandServerState());
62 
63   server_state_->surfaces_.SetFramesAreRGBA(frames_are_rgba);
64 
65   server_state_->display_ = wl_display_create();
66   CHECK(server_state_->display_ != nullptr)
67     << "Failed to start WaylandServer: failed to create display";
68 
69   if (fd < 0) {
70     const char* socket = wl_display_add_socket_auto(server_state_->display_);
71     CHECK(socket != nullptr)
72         << "Failed to start WaylandServer: failed to create socket";
73 
74     LOG(INFO) << "WaylandServer running on socket " << socket;
75   } else {
76     CHECK(wl_display_add_socket_fd(server_state_->display_, fd) == 0)
77         << "Failed to start WaylandServer: failed to use fd " << fd;
78 
79     LOG(INFO) << "WaylandServer running on socket " << fd;
80   }
81 
82   wl_display_init_shm(server_state_->display_);
83 
84   BindCompositorInterface(server_state_->display_, &server_state_->surfaces_);
85   BindVirtioGpuMetadataInterface(server_state_->display_,
86                                  &server_state_->surfaces_);
87   BindDmabufInterface(server_state_->display_);
88   BindSubcompositorInterface(server_state_->display_);
89   BindSeatInterface(server_state_->display_);
90   BindShellInterface(server_state_->display_);
91 
92   {
93     std::lock_guard<std::mutex> lock(server_ready_mutex_);
94     server_ready_ = true;
95   }
96   server_ready_cv_.notify_one();
97 
98   wl_display_run(server_state_->display_);
99   wl_display_destroy(server_state_->display_);
100 }
101 
SetFrameCallback(Surfaces::FrameCallback callback)102 void WaylandServer::SetFrameCallback(Surfaces::FrameCallback callback) {
103   server_state_->surfaces_.SetFrameCallback(std::move(callback));
104 }
105 
SetDisplayEventCallback(DisplayEventCallback callback)106 void WaylandServer::SetDisplayEventCallback(DisplayEventCallback callback) {
107   server_state_->surfaces_.SetDisplayEventCallback(std::move(callback));
108 }
109 
110 }  // namespace wayland
111