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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
/*
* 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/>.
*/
#include "wayland_shm.h"
#include "wayland_app.h"
#include <mir/fd.h>
#include <mir/fatal.h>
#include <boost/throw_exception.hpp>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <system_error>
namespace geom = mir::geometry;
namespace
{
static wl_shm_pool* make_shm_pool(wl_shm* shm, int size, void **data)
{
static auto (*open_shm_file)() -> mir::Fd = []
{
static char const* shm_dir;
open_shm_file = []{ return mir::Fd{open(shm_dir, O_TMPFILE | O_RDWR | O_EXCL, S_IRWXU)}; };
// Wayland based toolkits typically use $XDG_RUNTIME_DIR to open shm pools
// so we try that before "/dev/shm". But confined snaps can't access "/dev/shm"
// so we try "/tmp" if both of the above fail.
for (auto dir : {const_cast<const char*>(getenv("XDG_RUNTIME_DIR")), "/dev/shm", "/tmp" })
{
if (dir)
{
shm_dir = dir;
auto fd = open_shm_file();
if (fd >= 0)
return fd;
}
}
// Workaround for filesystems that don't support O_TMPFILE (E.g. phones based on 3.4 or 3.10
open_shm_file = []
{
char template_filename[] = "/dev/shm/mir-buffer-XXXXXX";
mir::Fd fd{mkostemp(template_filename, O_CLOEXEC)};
if (fd != -1)
{
if (unlink(template_filename) < 0)
{
return mir::Fd{};
}
}
return fd;
};
return open_shm_file();
};
auto fd = open_shm_file();
if (fd < 0) {
BOOST_THROW_EXCEPTION((std::system_error{errno, std::system_category(), "Failed to open shm buffer"}));
}
if (auto error = posix_fallocate(fd, 0, size))
{
BOOST_THROW_EXCEPTION((std::system_error{error, std::system_category(), "Failed to allocate shm buffer"}));
}
if ((*data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
{
BOOST_THROW_EXCEPTION((std::system_error{errno, std::system_category(), "Failed to mmap buffer"}));
}
return wl_shm_create_pool(shm, fd, size);
}
}
struct WaylandShmPool
{
public:
WaylandShmPool(wl_shm_pool* shm_pool, void* data, size_t size)
: shm_pool{shm_pool},
data{data},
size{size}
{
}
~WaylandShmPool()
{
munmap(data, size);
wl_shm_pool_destroy(shm_pool);
}
wl_shm_pool* const shm_pool;
void* const data;
size_t const size;
};
wl_buffer_listener const WaylandShmBuffer::buffer_listener {handle_release};
WaylandShmBuffer::WaylandShmBuffer(
std::shared_ptr<WaylandShmPool> pool,
void* data,
geom::Size size,
geom::Stride stride,
wl_buffer* buffer)
: pool{pool},
data_{data},
size_{size},
stride_{stride},
buffer{buffer}
{
wl_buffer_add_listener(buffer, &buffer_listener, this);
}
WaylandShmBuffer::~WaylandShmBuffer()
{
wl_buffer_destroy(buffer);
}
auto WaylandShmBuffer::use() -> wl_buffer*
{
if (self_ptr)
{
mir::fatal_error("WaylandShmBuffer used multiple times");
}
self_ptr = shared_from_this();
return buffer;
}
void WaylandShmBuffer::handle_release(void *data, wl_buffer*)
{
auto const self = static_cast<WaylandShmBuffer*>(data);
self->self_ptr.reset();
}
WaylandShm::WaylandShm(wl_shm* shm)
: shm{shm}
{
}
auto WaylandShm::get_buffer(geom::Size size, geom::Stride stride) -> std::shared_ptr<WaylandShmBuffer>
{
// If the old buffers have a different size, clear them all
if (!buffers.empty() && (buffers[0]->size() != size || buffers[0]->stride() != stride))
{
buffers.clear();
}
std::shared_ptr<WaylandShmBuffer> free_buffer;
// We can now assume all buffers in the list are the correct size, and look for a free one
for (auto const& buffer : buffers)
{
if (!buffer->is_in_use())
{
free_buffer = buffer;
break;
}
}
// If we don't find one, we create a new buffer with a single-use pool
if (!free_buffer)
{
void* data;
size_t const data_size = size.height.as_int() * stride.as_int();
auto const pool_resource = make_shm_pool(shm, data_size, &data);
auto const pool = std::make_shared<WaylandShmPool>(pool_resource, data, data_size);
auto const buffer = wl_shm_pool_create_buffer(
pool_resource,
0,
size.width.as_int(),
size.height.as_int(),
stride.as_int(),
WL_SHM_FORMAT_ARGB8888);
free_buffer = std::make_shared<WaylandShmBuffer>(pool, data, size, stride, std::move(buffer));
buffers.push_back(free_buffer);
}
return free_buffer;
}
|