From dae5bc02b1c934075e95694953b4330676e21611 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Fri, 7 Feb 2025 20:39:58 +0100 Subject: estd: add graphics module --- src/screen/drm/buffer/raw.zig | 8 +++----- src/screen/drm/buffer/root.zig | 18 +++++++++++++----- src/screen/drm/card.zig | 20 ++++---------------- src/screen/drm/connector/root.zig | 8 ++++---- src/screen/drm/crtc.zig | 5 +++-- src/screen/drm/encoder.zig | 6 +++--- src/screen/drm/resources.zig | 19 +++++++++++++++---- 7 files changed, 45 insertions(+), 39 deletions(-) (limited to 'src/screen/drm') diff --git a/src/screen/drm/buffer/raw.zig b/src/screen/drm/buffer/raw.zig index a79c6a7..c5a04f3 100644 --- a/src/screen/drm/buffer/raw.zig +++ b/src/screen/drm/buffer/raw.zig @@ -6,16 +6,16 @@ const cerror = @import("../../cerror.zig"); pub const Raw = struct { const Self = @This(); - card: *drm.Card, + card: drm.Card, + id: u32, width: u32, height: u32, stride: u32, handle: u32, - pixels: []volatile drm.Pixel, bytes: []volatile u8, - pub fn init(card: *drm.Card, width: u32, height: 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, @@ -32,7 +32,6 @@ pub const Raw = struct { .height = create_dumb.height, .stride = create_dumb.pitch, .handle = create_dumb.handle, - .pixels = undefined, .bytes = undefined, .card = card, }; @@ -74,7 +73,6 @@ pub const Raw = struct { try cerror.from_usize(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; diff --git a/src/screen/drm/buffer/root.zig b/src/screen/drm/buffer/root.zig index d40fd8d..f14ed66 100644 --- a/src/screen/drm/buffer/root.zig +++ b/src/screen/drm/buffer/root.zig @@ -1,4 +1,5 @@ const drm = @import("../root.zig"); +const graphics = @import("estd").graphics; pub const Buffer = struct { const Self = @This(); @@ -7,7 +8,7 @@ pub const Buffer = struct { current: Raw, crtc: Raw, - pub fn init(card: *drm.Card, width: u32, height: u32) !Self { + 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), @@ -15,10 +16,6 @@ pub const Buffer = struct { } pub fn swap(self: *Self) void { - 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; @@ -32,5 +29,16 @@ pub const Buffer = struct { 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], + }; + } }; diff --git a/src/screen/drm/card.zig b/src/screen/drm/card.zig index ef56142..b5ca893 100644 --- a/src/screen/drm/card.zig +++ b/src/screen/drm/card.zig @@ -25,32 +25,20 @@ pub const Card = struct { }; } - pub fn close(self: *Card) void { + pub fn close(self: Card) void { self.file.close(); } - pub fn resources(self: *Card) !drm.Resources { + pub fn resources(self: Card) !drm.Resources { return drm.Resources.init(self); } - pub fn connector(self: *Card, id: u32) !drm.Connector { - return drm.Connector.init(self, id); - } - - pub fn encoder(self: *Card, id: u32) !drm.Encoder { - return drm.Encoder.init(self, id); - } - - pub fn crtc(self: *Card, id: u32) !drm.Crtc { - return drm.Crtc.init(self, id); - } - - pub fn create_buffer(self: *Card, width: u32, height: u32) !drm.Buffer { + pub fn create_buffer(self: Card, width: u32, height: u32) !drm.Buffer { return drm.Buffer.init(self, width, height); } pub fn request( - self: *Self, + self: *const Self, comptime kind: drm.request.Kind, payload: anytype ) !void { diff --git a/src/screen/drm/connector/root.zig b/src/screen/drm/connector/root.zig index 64686b9..3628817 100644 --- a/src/screen/drm/connector/root.zig +++ b/src/screen/drm/connector/root.zig @@ -9,7 +9,7 @@ pub const Connector = struct { const Self = @This(); - card: *drm.Card, + card: drm.Card, encoder_ids: []u32, modes: []Mode, @@ -25,7 +25,7 @@ pub const Connector = struct { subpixel: u32, pad: u32, - pub fn raw_without_ids(card: *drm.Card, id: u32) !drm.request.Connector { + pub fn raw_without_ids(card: drm.Card, id: u32) !drm.request.Connector { var result = std.mem.zeroInit(drm.request.Connector, .{ .id = id }); try card.request(.get_connector, &result); return result; @@ -34,7 +34,7 @@ pub const Connector = struct { // NOTE: This function does not take in account // that there might be some hot-plugging going // on. This might have to change in the future. - pub fn init(card: *drm.Card, id: u32) !Self { + pub fn init(card: drm.Card, id: u32) !Self { var raw = try Self.raw_without_ids(card, id); const resources = .{ .encoder_ids = try card.allocator.alloc(u32, raw.count_encoders), @@ -76,7 +76,7 @@ pub const Connector = struct { pub fn compatible_crtc(self: *Self) ?usize { for (self.encoder_ids) |encoder_id| { - var encoder = self.card.encoder(encoder_id) catch continue; + var encoder = drm.Encoder.init(self.card, encoder_id) catch continue; defer encoder.deinit(); for (0..32) |index| { diff --git a/src/screen/drm/crtc.zig b/src/screen/drm/crtc.zig index 6130c86..6dc6c3b 100644 --- a/src/screen/drm/crtc.zig +++ b/src/screen/drm/crtc.zig @@ -4,6 +4,8 @@ const drm = @import("root.zig"); pub const Crtc = struct { const Self = @This(); + card: drm.Card, + connector_ids: ?*u32, count_connectors: u32, id: u32, @@ -13,9 +15,8 @@ pub const Crtc = struct { mode: drm.Connector.Mode, mode_valid: u32, gamma_size: u32, - card: *drm.Card, - pub fn init(card: *drm.Card, id: u32) !Self { + pub fn init(card: drm.Card, id: u32) !Self { var raw = std.mem.zeroInit(drm.request.Crtc, .{ .id = id }); try card.request(.get_crtc, &raw); return .{ diff --git a/src/screen/drm/encoder.zig b/src/screen/drm/encoder.zig index 19630ab..2758ee8 100644 --- a/src/screen/drm/encoder.zig +++ b/src/screen/drm/encoder.zig @@ -1,6 +1,5 @@ const std = @import("std"); const drm = @import("root.zig"); -const Card = drm.Card; pub const Encoder = struct { const Self = @This(); @@ -17,13 +16,14 @@ pub const Encoder = struct { dpi = 8, }; + card: drm.Card, + id: u32, type: Kind, possible_crtcs: u32, possible_clones: u32, - card: *Card, - pub fn init(card: *Card, id: u32) !Self { + pub fn init(card: drm.Card, id: u32) !Self { var raw = std.mem.zeroInit(drm.request.Encoder, .{ .id = id }); try card.request(.get_encoder, &raw); diff --git a/src/screen/drm/resources.zig b/src/screen/drm/resources.zig index 9a8566c..8b45fdf 100644 --- a/src/screen/drm/resources.zig +++ b/src/screen/drm/resources.zig @@ -3,13 +3,12 @@ const os = std.os.linux; const cerror = @import("../cerror.zig"); const drm = @import("root.zig"); -const Card = drm.Card; pub const Resources = struct { const Self = @This(); const Range = struct { min: u32, max: u32 }; - card: *Card, + card: drm.Card, fb_ids: []u32, crtc_ids: []u32, @@ -18,7 +17,7 @@ pub const Resources = struct { width: Range, height: Range, - pub fn raw_without_ids(card: *Card) !drm.request.Resources { + pub fn raw_without_ids(card: drm.Card) !drm.request.Resources { var result = std.mem.zeroes(drm.request.Resources); try card.request(.get_resources, &result); return result; @@ -27,7 +26,7 @@ pub const Resources = struct { // NOTE: This function does not take in account // that there might be some hot-plugging going // on. This might have to change in the future. - pub fn init(card: *Card) !Self { + pub fn init(card: drm.Card) !Self { var raw = try Self.raw_without_ids(card); const resources = .{ .fb_ids = try card.allocator.alloc(u32, raw.count_fbs), @@ -59,4 +58,16 @@ pub const Resources = struct { self.card.allocator.free(self.connector_ids); self.card.allocator.free(self.encoder_ids); } + + pub fn connector(self: *Self, index: usize) !drm.Connector { + return drm.Connector.init(self.card, self.connector_ids[index]); + } + + pub fn crtc(self: *Self, index: usize) !drm.Crtc { + return drm.Crtc.init(self.card, self.crtc_ids[index]); + } + + pub fn encoder(self: *Self, index: usize) !drm.Encoder { + return drm.Encoder.init(self.card, self.encoder_ids[index]); + } }; -- cgit v1.2.3-70-g09d2