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
189
190
191
192
193
194
195
196
197
|
//! Pixel and framebuffer utilities
const boot = @import("../../boot/multiboot/multiboot.zig");
const color = @import("color.zig");
const video = @import("../../common/constants/video.zig");
pub inline fn ptr_32(addr: u64) [*]volatile u32 {
return @as([*]volatile u32, @ptrFromInt(addr));
}
pub inline fn ptr_8(addr: u64) [*]volatile u8 {
return @as([*]volatile u8, @ptrFromInt(addr));
}
pub inline fn offset_32(fb: boot.FramebufferInfo, x: u32, y: u32) usize {
return y * (fb.pitch / video.BYTES_PER_PIXEL_32) + x;
}
pub inline fn offset_24(fb: boot.FramebufferInfo, x: u32, y: u32) usize {
return y * fb.pitch + x * video.BYTES_PER_PIXEL_24;
}
pub inline fn pixels_per_row(fb: boot.FramebufferInfo) u32 {
return fb.pitch / video.BYTES_PER_PIXEL_32;
}
pub fn put(fb: boot.FramebufferInfo, x: u32, y: u32, c: u32) void {
if (x >= fb.width or y >= fb.height) return;
if (fb.bpp == video.BPP_32) {
put_32(fb, x, y, c);
} else if (fb.bpp == video.BPP_24) {
put_24(fb, x, y, c);
}
}
pub fn put_32(fb: boot.FramebufferInfo, x: u32, y: u32, c: u32) void {
const pixels = ptr_32(fb.addr);
pixels[offset_32(fb, x, y)] = c;
}
pub fn put_24(fb: boot.FramebufferInfo, x: u32, y: u32, c: u32) void {
const pixels = ptr_8(fb.addr);
const off = offset_24(fb, x, y);
const rgb = color.extract(c);
pixels[off] = rgb.b;
pixels[off + 1] = rgb.g;
pixels[off + 2] = rgb.r;
}
pub fn fill(fb: boot.FramebufferInfo, c: u32) void {
if (fb.bpp == video.BPP_32) {
fill_32(fb, c);
} else if (fb.bpp == video.BPP_24) {
fill_24(fb, c);
}
}
pub fn fill_32(fb: boot.FramebufferInfo, c: u32) void {
const pixels = ptr_32(fb.addr);
const total = fb.width * fb.height;
var i: usize = 0;
while (i < total) : (i += 1) {
pixels[i] = c;
}
}
pub fn fill_24(fb: boot.FramebufferInfo, c: u32) void {
const pixels = ptr_8(fb.addr);
const rgb = color.extract(c);
var y: u32 = 0;
while (y < fb.height) : (y += 1) {
var x: u32 = 0;
while (x < fb.width) : (x += 1) {
const off = offset_24(fb, x, y);
pixels[off] = rgb.b;
pixels[off + 1] = rgb.g;
pixels[off + 2] = rgb.r;
}
}
}
pub fn fill_rect(fb: boot.FramebufferInfo, x: u32, y: u32, w: u32, h: u32, c: u32) void {
if (fb.bpp == video.BPP_32) {
fill_rect_32(fb, x, y, w, h, c);
} else if (fb.bpp == video.BPP_24) {
fill_rect_24(fb, x, y, w, h, c);
}
}
pub fn fill_rect_32(fb: boot.FramebufferInfo, x: u32, y: u32, w: u32, h: u32, c: u32) void {
const pixels = ptr_32(fb.addr);
var row: u32 = 0;
while (row < h) : (row += 1) {
var col: u32 = 0;
while (col < w) : (col += 1) {
const px = x + col;
const py = y + row;
if (px < fb.width and py < fb.height) {
pixels[offset_32(fb, px, py)] = c;
}
}
}
}
pub fn fill_rect_24(fb: boot.FramebufferInfo, x: u32, y: u32, w: u32, h: u32, c: u32) void {
const pixels = ptr_8(fb.addr);
const rgb = color.extract(c);
var row: u32 = 0;
while (row < h) : (row += 1) {
var col: u32 = 0;
while (col < w) : (col += 1) {
const px = x + col;
const py = y + row;
if (px < fb.width and py < fb.height) {
const off = offset_24(fb, px, py);
pixels[off] = rgb.b;
pixels[off + 1] = rgb.g;
pixels[off + 2] = rgb.r;
}
}
}
}
pub fn copy_row(fb: boot.FramebufferInfo, dst_y: u32, src_y: u32) void {
if (fb.bpp == video.BPP_32) {
copy_row_32(fb, dst_y, src_y);
} else if (fb.bpp == video.BPP_24) {
copy_row_24(fb, dst_y, src_y);
}
}
pub fn copy_row_32(fb: boot.FramebufferInfo, dst_y: u32, src_y: u32) void {
const pixels = ptr_32(fb.addr);
const ppr = pixels_per_row(fb);
var x: u32 = 0;
while (x < fb.width) : (x += 1) {
pixels[dst_y * ppr + x] = pixels[src_y * ppr + x];
}
}
pub fn copy_row_24(fb: boot.FramebufferInfo, dst_y: u32, src_y: u32) void {
const pixels = ptr_8(fb.addr);
var x: u32 = 0;
while (x < fb.width) : (x += 1) {
const src_off = offset_24(fb, x, src_y);
const dst_off = offset_24(fb, x, dst_y);
pixels[dst_off] = pixels[src_off];
pixels[dst_off + 1] = pixels[src_off + 1];
pixels[dst_off + 2] = pixels[src_off + 2];
}
}
pub fn clear_row(fb: boot.FramebufferInfo, y: u32, c: u32) void {
if (fb.bpp == video.BPP_32) {
clear_row_32(fb, y, c);
} else if (fb.bpp == video.BPP_24) {
clear_row_24(fb, y, c);
}
}
pub fn clear_row_32(fb: boot.FramebufferInfo, y: u32, c: u32) void {
const pixels = ptr_32(fb.addr);
const ppr = pixels_per_row(fb);
var x: u32 = 0;
while (x < fb.width) : (x += 1) {
pixels[y * ppr + x] = c;
}
}
pub fn clear_row_24(fb: boot.FramebufferInfo, y: u32, c: u32) void {
const pixels = ptr_8(fb.addr);
const rgb = color.extract(c);
var x: u32 = 0;
while (x < fb.width) : (x += 1) {
const off = offset_24(fb, x, y);
pixels[off] = rgb.b;
pixels[off + 1] = rgb.g;
pixels[off + 2] = rgb.r;
}
}
pub fn line_width(fb: boot.FramebufferInfo) u32 {
if (fb.bpp == video.BPP_32) {
return fb.pitch / video.BYTES_PER_PIXEL_32;
} else if (fb.bpp == video.BPP_24) {
return fb.pitch / video.BYTES_PER_PIXEL_24;
}
return fb.width;
}
|