1 /* 2 * Copyright (C) 2020 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 <stdint.h> 20 #include <future> 21 #include <memory> 22 #include <mutex> 23 #include <optional> 24 #include <thread> 25 #include <unordered_map> 26 27 namespace wayland { 28 29 class Surface; 30 31 class Surfaces { 32 public: 33 Surfaces() = default; 34 virtual ~Surfaces() = default; 35 36 Surfaces(const Surfaces& rhs) = delete; 37 Surfaces& operator=(const Surfaces& rhs) = delete; 38 39 Surfaces(Surfaces&& rhs) = delete; 40 Surfaces& operator=(Surfaces&& rhs) = delete; 41 42 Surface* GetOrCreateSurface(std::uint32_t id); 43 44 using FrameCallback = std::function<void(std::uint32_t /*display_number*/, 45 std::uint8_t* /*frame_pixels*/)>; 46 47 // Blocking 48 void OnNextFrame(const FrameCallback& callback); 49 50 private: 51 friend class Surface; 52 void HandleSurfaceFrame(std::uint32_t display_number, 53 std::uint8_t* frame_bytes); 54 55 std::mutex surfaces_mutex_; 56 std::unordered_map<std::uint32_t, std::unique_ptr<Surface>> surfaces_; 57 58 using FrameCallbackPackaged = std::packaged_task<void( 59 std::uint32_t /*display_number*/, std::uint8_t* /*frame_bytes*/)>; 60 61 std::mutex callback_mutex_; 62 std::optional<FrameCallbackPackaged*> callback_; 63 }; 64 65 } // namespace wayland 66