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 <stdint.h>
21 #include <mutex>
22 #include <optional>
23 
24 #include <wayland-server-core.h>
25 
26 namespace wayland {
27 
28 class Surfaces;
29 
30 // Tracks the buffer associated with a Wayland surface.
31 class Surface {
32  public:
33   Surface(Surfaces& surfaces);
34   virtual ~Surface();
35 
36   Surface(const Surface& rhs) = delete;
37   Surface& operator=(const Surface& rhs) = delete;
38 
39   Surface(Surface&& rhs) = delete;
40   Surface& operator=(Surface&& rhs) = delete;
41 
42   struct Region {
43     int32_t x;
44     int32_t y;
45     int32_t w;
46     int32_t h;
47   };
48 
49   void SetRegion(const Region& region);
50 
51   // Sets the buffer of the pending frame.
52   void Attach(struct wl_resource* buffer);
53 
54   // Commits the pending frame state.
55   void Commit();
56 
57   void SetVirtioGpuScanoutId(uint32_t scanout);
58 
59  private:
60   Surfaces& surfaces_;
61 
62   struct VirtioGpuMetadata {
63     std::optional<uint32_t> scanout_id;
64   };
65 
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     VirtioGpuMetadata virtio_gpu_metadata_;
79 
80     bool has_notified_surface_create = false;
81   };
82 
83   std::mutex state_mutex_;
84   State state_;
85 };
86 
87 }  // namespace wayland
88