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 namespace wayland { 28 29 namespace internal { 30 struct WaylandServerState; 31 } // namespace internal 32 33 using FrameCallback = std::function<void(std::uint32_t /*frame_number*/, 34 std::uint8_t* /*frame_pixels*/)>; 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 virtual ~WaylandServer(); 45 46 WaylandServer(const WaylandServer& rhs) = delete; 47 WaylandServer& operator=(const WaylandServer& rhs) = delete; 48 49 WaylandServer(WaylandServer&& rhs) = delete; 50 WaylandServer& operator=(WaylandServer&& rhs) = delete; 51 52 // Registers a callback to run on the next frame available after the given 53 // frame number. 54 std::future<void> OnFrameAfter(std::uint32_t frame_number, 55 const FrameCallback& frame_callback); 56 57 private: 58 void ServerLoop(int wayland_socket_fd); 59 60 bool server_ready_ = false; 61 std::mutex server_ready_mutex_; 62 std::condition_variable server_ready_cv_; 63 64 std::thread server_thread_; 65 std::unique_ptr<internal::WaylandServerState> server_state_; 66 }; 67 68 } // namespace wayland