Mir
wayland_shm.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2018 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 or 3 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #ifndef MIRAL_WAYLAND_SHM_H
18 #define MIRAL_WAYLAND_SHM_H
19 
20 #include "mir/geometry/size.h"
21 
22 #include <wayland-client.h>
23 #include <memory>
24 #include <vector>
25 
26 class WaylandShm;
27 struct WaylandShmPool;
28 
30 {
31 public:
33  std::shared_ptr<WaylandShmPool> pool,
34  void* data,
35  mir::geometry::Size size,
36  mir::geometry::Stride stride,
37  wl_buffer* buffer);
39 
40  auto data() const -> void* { return data_; }
41  auto size() const -> mir::geometry::Size { return size_; }
42  auto stride() const -> mir::geometry::Stride { return stride_; }
43  /// Returns if this buffer is currently being used by the compositor. In-use buffers keep themselves alive.
44  auto is_in_use() const -> bool { return self_ptr != nullptr; }
45  /// Marks this buffer as in-use and assumes the resulting wl_buffer is sent to the compositor. Keeps this buffer
46  /// alive until the compositor releases it (if it's not sent to the compositor or the compositor never releases it
47  /// this buffer is leaked). Should only be called if the buffer is not already in-use.
48  auto use() -> wl_buffer*;
49 
50 private:
51  friend WaylandShm;
52 
53  static wl_buffer_listener const buffer_listener;
54 
55  static void handle_release(void *data, wl_buffer*);
56 
57  std::shared_ptr<WaylandShmPool> const pool;
58  void* const data_;
59  mir::geometry::Size const size_;
60  mir::geometry::Stride const stride_;
61  wl_buffer* const buffer;
62  std::shared_ptr<WaylandShmBuffer> self_ptr; ///< Is set on use and cleared on release
63 };
64 
65 /// A single WaylandShm does not efficiently provision multiple buffers for multiple window sizes. Please use one
66 /// WaylandShm per window (if windows may have distinct sizes)
68 {
69 public:
70  /// Does not take ownership of the wl_shm
71  WaylandShm(wl_shm* shm);
72 
73  /// Always returns a buffer of the correct size that is not in-use
74  auto get_buffer(mir::geometry::Size size, mir::geometry::Stride stride) -> std::shared_ptr<WaylandShmBuffer>;
75 
76 private:
77  wl_shm* const shm;
78  /// get_buffer() assumes all buffers in the list have the same size and stride
79  std::vector<std::shared_ptr<WaylandShmBuffer>> buffers;
80 };
81 
82 #endif // MIRAL_WAYLAND_SHM_H

Copyright © 2012-2022 Canonical Ltd.
Generated on Thu Sep 8 12:37:23 UTC 2022
This documentation is licensed under the GPL version 2 or 3.