summaryrefslogtreecommitdiff
path: root/src/screen/drm/frame-buffer/root.zig
blob: 0083711b01de76a78ca7fb67c9ae01b1c14fbd89 (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
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
const std = @import("std");
const os = std.os.linux;
const Card = @import("../card.zig").Card;
const Drm = @import("../request.zig").Drm;
const PixelFormat = @import("pixelformat.zig").PixelFormat;
const cerror = @import("../../cerror.zig");

const CreateDumb = extern struct {
    height: u32,
    width: u32,
    bpp: u32,
    flags: u32,
    handle: u32,
    pitch: u32,
    size: u32,
};

const FrameBufferCmd2 = extern struct {
    id: u32,
    width: u32,
    height: u32,
    pixel_format: PixelFormat,
    flags: u32,
    handles: [4]u32,
    pitches: [4]u32,
    offsets: [4]u32,
    modifier: [4]u32,
};

const MapDumb = extern struct {
    handle: u32,
    __padding: u32,
    offset: i64,
};

pub const DoubleBuffer = struct {
    const Self = @This();

    back: FrameBuffer,
    front: FrameBuffer,

    pub fn init(card: *Card, width: u32, height: u32, bpp: u32) !Self {
        return .{
            .back = try FrameBuffer.init(card, width, height, bpp),
            .front = try FrameBuffer.init(card, width, height, bpp),
        };
    }

    pub fn swap(self: *Self) void {
        const back_data = self.back.data;
        self.back.data = self.front.data;
        self.front.data = back_data;

        const back_id = self.back.id;
        self.back.id = self.front.id;
        self.front.id = back_id;
    }

    pub fn deinit(self: *Self) void {
        self.back.deinit();
        self.front.deinit();
    }

    pub fn buffer(self: *Self) *FrameBuffer {
        return &self.back;
    }

    pub fn crtc_buffer(self: *Self) *FrameBuffer {
        return &self.front;
    }
};

pub const Pixel = packed struct(u32) {
    blue: u8,
    green: u8,
    red: u8,
    _padding: u8 = 0x0,
};

pub const FrameBuffer = struct {
    const Self = @This();

    card: *Card,
    id: u32,
    width: u32,
    height: u32,
    bpp: u32,
    stride: u32,
    data_size: u64,
    canvas_size: u64,
    handle: u32,
    data: [*]volatile Pixel,

    pub fn init(card: *Card, width: u32, height: u32, bpp: u32) !Self {
        var create_dumb = std.mem.zeroInit(CreateDumb, .{
            .width = width,
            .height = height,
            .bpp = bpp,
        });
        try Drm.create_dumb.request(
            card.file.handle,
            CreateDumb,
            &create_dumb
        );

        var fb = Self {
            .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,
            .card = card,
        };

        var buffer_cmd = FrameBufferCmd2 {
            .id = 0,
            .width = fb.width,
            .height = fb.height,
            .pixel_format = PixelFormat.xrgb8888,
            .flags = 0,
            .handles = [_] u32 { fb.handle, 0, 0, 0 },
            .pitches = [_] u32 { fb.stride, 0, 0, 0 },
            .offsets = [_] u32 { 0, 0, 0, 0 },
            .modifier = [_] u32 { 0, 0, 0, 0 },
        };

        try Drm.add_frame_buffer2.request(
            card.file.handle,
            FrameBufferCmd2,
            &buffer_cmd
        );

        fb.id = buffer_cmd.id;

        var map_dumb = std.mem.zeroInit(MapDumb, .{ .handle = fb.handle });

        try Drm.map_dumb.request(
            card.file.handle,
            MapDumb,
            &map_dumb
        );

        const address = os.mmap(
            null,
            fb.data_size,
            os.PROT.READ | os.PROT.WRITE,
            .{ .TYPE = os.MAP_TYPE.SHARED },
            card.file.handle,
            map_dumb.offset
        );

        try cerror.from_usize(address);

        fb.data = @ptrFromInt(address);

        return fb;
    }

    pub fn set(self: *Self, x: u32, y: u32, pixel: Pixel) void {
        self.data[x + self.width * y] = pixel;
    }

    pub fn fill(self: *Self, pixel: Pixel) void {
        @memset(self.data[0..self.canvas_size], pixel);
    }

    pub fn deinit(self: *Self) void {
        cerror.from_usize(
            os.munmap(@ptrCast(@volatileCast(self.data)), self.data_size)
        ) catch |err| std.debug.panic("munmap failed in frame-buffer: {}", .{err});

        Drm.remove_frame_buffer.request(
            self.card.file.handle,
            u32,
            &self.id
        ) catch @panic("failed to remove frame-buffer");

        Drm.destroy_dumb.request(
            self.card.file.handle,
            u32,
            &self.handle
        ) catch @panic("failed to destroy dumb-buffer");
    }
};