From fef523a8d7c87f272de18c8abd57e0cc53e2ef40 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Sun, 2 Feb 2025 22:24:00 +0100 Subject: screen: drm change raw buffer interface --- src/screen/drm/buffer/raw.zig | 31 +++++++++----------------- src/screen/drm/buffer/root.zig | 16 +++++++++----- src/screen/drm/card.zig | 4 ++-- src/screen/main.zig | 50 ++++++++++++++++++------------------------ 4 files changed, 43 insertions(+), 58 deletions(-) diff --git a/src/screen/drm/buffer/raw.zig b/src/screen/drm/buffer/raw.zig index e4674eb..a79c6a7 100644 --- a/src/screen/drm/buffer/raw.zig +++ b/src/screen/drm/buffer/raw.zig @@ -10,18 +10,16 @@ pub const Raw = struct { id: u32, width: u32, height: u32, - bpp: u32, stride: u32, - data_size: u64, - canvas_size: u64, handle: u32, - data: [*]volatile drm.Pixel, + pixels: []volatile drm.Pixel, + bytes: []volatile u8, - pub fn init(card: *drm.Card, width: u32, height: u32, bpp: u32) !Self { + pub fn init(card: *drm.Card, width: u32, height: u32) !Self { var create_dumb = std.mem.zeroInit(drm.request.CreateDumb, .{ .width = width, .height = height, - .bpp = bpp, + .bpp = 32, }); try card.request( .create_dumb, @@ -32,12 +30,10 @@ pub const Raw = struct { .id = 0, .width = create_dumb.width, .height = create_dumb.height, - .bpp = create_dumb.bpp, .stride = create_dumb.pitch, - .data_size = create_dumb.size, - .canvas_size = create_dumb.size / (bpp / 8), .handle = create_dumb.handle, - .data = undefined, + .pixels = undefined, + .bytes = undefined, .card = card, }; @@ -69,7 +65,7 @@ pub const Raw = struct { const address = os.mmap( null, - fb.data_size, + create_dumb.size, os.PROT.READ | os.PROT.WRITE, .{ .TYPE = os.MAP_TYPE.SHARED }, card.file.handle, @@ -78,22 +74,15 @@ pub const Raw = struct { try cerror.from_usize(address); - fb.data = @ptrFromInt(address); + fb.pixels = @as([*]volatile drm.Pixel, @ptrFromInt(address))[0..create_dumb.size / 4]; + fb.bytes = @as([*]volatile u8, @ptrFromInt(address))[0..create_dumb.size]; return fb; } - pub fn set(self: *Self, x: u32, y: u32, p: drm.Pixel) void { - self.data[x + self.width * y] = p; - } - - pub fn fill(self: *Self, p: drm.Pixel) void { - @memset(self.data[0..self.canvas_size], p); - } - pub fn deinit(self: *Self) void { cerror.from_usize( - os.munmap(@ptrCast(@volatileCast(self.data)), self.data_size) + os.munmap(@ptrCast(@volatileCast(self.bytes)), self.bytes.len) ) catch |err| std.debug.panic("munmap failed in frame-buffer: {}", .{err}); self.card.request( diff --git a/src/screen/drm/buffer/root.zig b/src/screen/drm/buffer/root.zig index e967d51..d40fd8d 100644 --- a/src/screen/drm/buffer/root.zig +++ b/src/screen/drm/buffer/root.zig @@ -7,17 +7,21 @@ pub const Buffer = struct { current: Raw, crtc: Raw, - pub fn init(card: *drm.Card, width: u32, height: u32, bpp: u32) !Self { + pub fn init(card: *drm.Card, width: u32, height: u32) !Self { return .{ - .current = try Raw.init(card, width, height, bpp), - .crtc = try Raw.init(card, width, height, bpp), + .current = try Raw.init(card, width, height), + .crtc = try Raw.init(card, width, height), }; } pub fn swap(self: *Self) void { - const data = self.current.data; - self.current.data = self.crtc.data; - self.crtc.data = data; + const pixels = self.current.pixels; + self.current.pixels = self.crtc.pixels; + self.crtc.pixels = pixels; + + 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; diff --git a/src/screen/drm/card.zig b/src/screen/drm/card.zig index cff8edf..ef56142 100644 --- a/src/screen/drm/card.zig +++ b/src/screen/drm/card.zig @@ -45,8 +45,8 @@ pub const Card = struct { return drm.Crtc.init(self, id); } - pub fn create_double_buffer(self: *Card, width: u32, height: u32, bpp: u32) !drm.Buffer { - return drm.Buffer.init(self, width, height, bpp); + pub fn create_buffer(self: *Card, width: u32, height: u32) !drm.Buffer { + return drm.Buffer.init(self, width, height); } pub fn request( diff --git a/src/screen/main.zig b/src/screen/main.zig index 673be0e..0514210 100644 --- a/src/screen/main.zig +++ b/src/screen/main.zig @@ -14,30 +14,17 @@ pub fn main() !void { var connector = try card.connector(resources.connector_ids[0]); defer connector.deinit(); - std.debug.print("connector = {}\n", .{ connector.id }); - const mode = connector.modes[0]; - std.debug.print("mode = {s}@{d}Hz\n", .{ mode.name, mode.frame_rate() }); - var crtc = try card.crtc(resources.crtc_ids[ connector.compatible_crtc() orelse @panic("no crtc found") ]); defer crtc.detach(); - std.debug.print("crtc = {}\n", .{ crtc.id }); - - var double_buffer = try card.create_double_buffer(mode.horizontal.size, mode.vertical.size, 32); - defer double_buffer.deinit(); + var buffer = try card.create_buffer(mode.horizontal.size, mode.vertical.size); + defer buffer.deinit(); - std.debug.print("buffer = {}, {}x{}, stride = {}\n", .{ - double_buffer.current.id, - double_buffer.current.width, - double_buffer.current.height, - double_buffer.current.stride, - }); - - try crtc.attach(&double_buffer, &connector, mode); + try crtc.attach(&buffer, &connector, mode); const Pos = struct { x: f32, y: f32 }; @@ -46,13 +33,20 @@ pub fn main() !void { var delta: f32 = 0; const size = 100; - const width: f32 = @floatFromInt(double_buffer.current.width); - const height: f32 = @floatFromInt(double_buffer.current.height); + const width: f32 = @floatFromInt(buffer.current.width); + const height: f32 = @floatFromInt(buffer.current.height); while (true) { const start = try std.time.Instant.now(); - double_buffer.current.fill(.{ .red = 0x25, .green = 0x25, .blue = 0x25 }); + @memset( + buffer.current.pixels, + .{ + .red = 0x25, + .green = 0x25, + .blue = 0x25 + } + ); pos.x += vec.x * delta; pos.y += vec.y * delta; @@ -75,23 +69,21 @@ pub fn main() !void { for (0..size) |w| { for (0..size) |h| { - double_buffer.current.set( - @as(u32, @intFromFloat(pos.x)) + @as(u32, @intCast(w)), - @as(u32, @intFromFloat(pos.y)) + @as(u32, @intCast(h)), - .{ - .red = 0xff, - .green = 0, - .blue = 0, - }); + const x = @as(usize, @intFromFloat(pos.x)) + w; + const y = @as(usize, @intFromFloat(pos.y)) + h; + buffer.current.pixels[x + buffer.current.width * y] = .{ + .red = 0xff, + .green = 0, + .blue = 0, + }; } } const end = try std.time.Instant.now(); const elapsed = (@as(f32, @floatFromInt(end.since(start))) / std.time.ns_per_s); - std.debug.print("FPS: {d:.2}\r", .{ 1 / elapsed }); delta = elapsed * 60; - try crtc.page_flip(&double_buffer); + try crtc.page_flip(&buffer); while (true) { var event: ?drm.Event = null; -- cgit v1.2.3-70-g09d2