summaryrefslogtreecommitdiff
path: root/src/screen/drm/buffer/root.zig
blob: f14ed66a47cab4089adf29fce314743760858d5c (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
const drm = @import("../root.zig");
const graphics = @import("estd").graphics;

pub const Buffer = struct {
	const Self = @This();
	pub const Raw = @import("raw.zig").Raw;

	current: Raw,
	crtc: Raw,

	pub fn init(card: drm.Card, width: u32, height: u32) !Self {
		return .{
			.current = try Raw.init(card, width, height),
			.crtc = try Raw.init(card, width, height),
		};
	}

	pub fn swap(self: *Self) void {
		const bytes = self.current.bytes;
		self.current.bytes = self.crtc.bytes;
		self.crtc.bytes = bytes;

		const id = self.current.id;
		self.current.id = self.crtc.id;
		self.crtc.id = id;
	}

	pub fn deinit(self: *Self) void {
		self.current.deinit();
		self.crtc.deinit();
	}

	pub fn canvas(self: *Self) graphics.Canvas {
		return .{
			.width = self.current.width,
			.height = self.current.height,
			.buffer = @as(
				[*]volatile graphics.Pixel,
				@ptrCast(@alignCast(self.current.bytes))
			)[0..self.current.bytes.len / 4],
		};
	}
};