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 18 #pragma once 19 20 #include <optional> 21 #include <stdint.h> 22 #include <thread> 23 #include <future> 24 25 #include <wayland-server-core.h> 26 27 namespace wayland { 28 29 // Tracks the buffer associated with a Wayland surface. 30 class Surface { 31 public: 32 Surface() = default; 33 virtual ~Surface() = default; 34 35 Surface(const Surface& rhs) = delete; 36 Surface& operator=(const Surface& rhs) = delete; 37 38 Surface(Surface&& rhs) = delete; 39 Surface& operator=(Surface&& rhs) = delete; 40 41 struct Region { 42 int32_t x; 43 int32_t y; 44 int32_t w; 45 int32_t h; 46 }; 47 48 void SetRegion(const Region& region); 49 50 // Sets the buffer of the pending frame. 51 void Attach(struct wl_resource* buffer); 52 53 // Commits the pending frame state. 54 void Commit(); 55 56 using FrameCallbackPackaged = 57 std::packaged_task<void(std::uint32_t /*frame_number*/, 58 std::uint8_t* /*frame_pixels*/)>; 59 60 // Registers a callback that should be invoked on the first frame after 61 // the given frame number. 62 void OnFrameAfter(uint32_t frame_number, 63 FrameCallbackPackaged frame_callback); 64 65 private: 66 struct State { 67 uint32_t current_frame_number = 0; 68 69 // The buffer for the current committed frame. 70 struct wl_resource* current_buffer = nullptr; 71 72 // The buffer for the next frame. 73 struct wl_resource* pending_buffer = nullptr; 74 75 // The buffers expected dimensions. 76 Region region; 77 }; 78 79 std::mutex state_mutex_; 80 State state_; 81 82 struct PendingFrameCallback { 83 uint32_t frame_number; 84 85 FrameCallbackPackaged frame_callback; 86 }; 87 88 std::mutex callback_mutex_; 89 std::optional<PendingFrameCallback> callback_; 90 }; 91 92 } // namespace wayland