aboutsummaryrefslogtreecommitdiff
path: root/src/wl/shm/pool.zig
blob: 16ba936a28b294d9e7a75f807a8a0add30a9ad42 (plain)
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
const std = @import("std");
const wayland = @import("../../root.zig");
const wl = wayland.wl;
const Format = wl.Shm.Format;
const AnonymousFile = wl.Shm.AnonymousFile;

const Self = @This();

const log = std.log.scoped(.@"wl.shm.pool");

pub const Events = wayland.EventSet(@This(), .{});
pub const Request = union(enum) {
    create_buffer: struct { wayland.Object.Ref, u32, u32, u32, u32, Format },
    destroy: void,
    resize: struct { u32 },
};

handle: wayland.Object.Ref = .null,
init: wayland.Object.interface.Init(Self) = .{},
file: AnonymousFile,
buffer: []u8,

pub fn deinit(self: *Self, ctx: wayland.Context) void {
    self.file.close();
    std.posix.munmap(@alignCast(self.buffer));
    ctx.display.registry.disable_object(self.handle);
    ctx.display.request(self, .{ .destroy = {} }) catch |e| {
        log.err("error on deinit: {}", .{e});
    };
    self.* = undefined;
}

pub fn create_buffer(
    self: *Self,
    ctx: wayland.Context,
    offset: u32,
    width: u32,
    height: u32,
    comptime format: Format,
) !*wl.Buffer(format) {
    const stride = width * format.bytes_per_pixel();
    const data = self.buffer[offset..offset + stride * height];

    const buffer = try ctx.allocator.create(wl.Buffer(format));
    errdefer ctx.allocator.destroy(buffer);
    try buffer.init.with(ctx, .{
        .width = width,
        .height = height,
        .data = @alignCast(@ptrCast(data)),
    });

    try ctx.display.request(
        self,
        .{
            .create_buffer = .{
                buffer.handle,
                @truncate(offset),
                @truncate(width),
                @truncate(height),
                @truncate(stride),
                format,
            }
        }
    );

    return buffer;
}