File: wayland_shm.h

package info (click to toggle)
mir 2.25.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 22,080 kB
  • sloc: cpp: 192,777; xml: 13,784; ansic: 8,207; python: 1,304; sh: 794; makefile: 258
file content (82 lines) | stat: -rw-r--r-- 2,936 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * Copyright © Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 or 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef MIRAL_WAYLAND_SHM_H
#define MIRAL_WAYLAND_SHM_H

#include <mir/geometry/size.h>

#include <wayland-client.h>
#include <memory>
#include <vector>

class WaylandShm;
struct WaylandShmPool;

class WaylandShmBuffer : public std::enable_shared_from_this<WaylandShmBuffer>
{
public:
    WaylandShmBuffer(
        std::shared_ptr<WaylandShmPool> pool,
        void* data,
        mir::geometry::Size size,
        mir::geometry::Stride stride,
        wl_buffer* buffer);
    ~WaylandShmBuffer();

    auto data() const -> void* { return data_; }
    auto size() const -> mir::geometry::Size { return size_; }
    auto stride() const -> mir::geometry::Stride { return stride_; }
    /// Returns if this buffer is currently being used by the compositor. In-use buffers keep themselves alive.
    auto is_in_use() const -> bool { return self_ptr != nullptr; }
    /// Marks this buffer as in-use and assumes the resulting wl_buffer is sent to the compositor. Keeps this buffer
    /// alive until the compositor releases it (if it's not sent to the compositor or the compositor never releases it
    /// this buffer is leaked). Should only be called if the buffer is not already in-use.
    auto use() -> wl_buffer*;

private:
    friend WaylandShm;

    static wl_buffer_listener const buffer_listener;

    static void handle_release(void *data, wl_buffer*);

    std::shared_ptr<WaylandShmPool> const pool;
    void* const data_;
    mir::geometry::Size const size_;
    mir::geometry::Stride const stride_;
    wl_buffer* const buffer;
    std::shared_ptr<WaylandShmBuffer> self_ptr; ///< Is set on use and cleared on release
};

/// A single WaylandShm does not efficiently provision multiple buffers for multiple window sizes. Please use one
/// WaylandShm per window (if windows may have distinct sizes)
class WaylandShm
{
public:
    /// Does not take ownership of the wl_shm
    WaylandShm(wl_shm* shm);

    /// Always returns a buffer of the correct size that is not in-use
    auto get_buffer(mir::geometry::Size size, mir::geometry::Stride stride) -> std::shared_ptr<WaylandShmBuffer>;

private:
    wl_shm* const shm;
    /// get_buffer() assumes all buffers in the list have the same size and stride
    std::vector<std::shared_ptr<WaylandShmBuffer>> buffers;
};

#endif // MIRAL_WAYLAND_SHM_H