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 #pragma once
18 
19 #include <atomic>
20 #include <cstdint>
21 #include <functional>
22 #include <future>
23 #include <memory>
24 #include <string>
25 #include <thread>
26 
27 #include "host/libs/wayland/wayland_server_callbacks.h"
28 #include "host/libs/wayland/wayland_surfaces.h"
29 
30 namespace wayland {
31 
32 namespace internal {
33 struct WaylandServerState;
34 }  // namespace internal
35 
36 // A Wayland compositing server that provides an interface for receiving frame
37 // updates from a connected client.
38 class WaylandServer {
39   public:
40     // Creates a Wayland compositing server. If specified, uses the given
41     // socket file descriptor to connect with clients. If provided, this
42     // server will close the file descriptor upon exit.
43    WaylandServer(int wayland_socket_fd = -1,
44                  bool wayland_frames_are_rgba = true);
45    virtual ~WaylandServer();
46 
47    WaylandServer(const WaylandServer& rhs) = delete;
48    WaylandServer& operator=(const WaylandServer& rhs) = delete;
49 
50    WaylandServer(WaylandServer&& rhs) = delete;
51    WaylandServer& operator=(WaylandServer&& rhs) = delete;
52 
53    // Registers the callback that will be run whenever a new frame is
54    // available.
55    void SetFrameCallback(Surfaces::FrameCallback callback);
56 
57    void SetDisplayEventCallback(DisplayEventCallback callback);
58 
59   private:
60    void ServerLoop(int wayland_socket_fd, bool wayland_frames_are_rgba);
61 
62    bool server_ready_ = false;
63    std::mutex server_ready_mutex_;
64    std::condition_variable server_ready_cv_;
65 
66    std::thread server_thread_;
67    std::unique_ptr<internal::WaylandServerState> server_state_;
68 };
69 
70 }  // namespace wayland