File size: 12,609 Bytes
dcbeeb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
extern crate alloc;

use alloc::vec;
use alloc::vec::Vec;
use bootloader_api::info::PixelFormat;
use crate::drivers::framebuffer::FramebufferConfig;
use crate::graphics::color::Color;
use crate::graphics::font::{get_glyph, FONT_HEIGHT, FONT_WIDTH};
use crate::graphics::icons::{get_icon_16, IconType, MOUSE_CURSOR, MOUSE_CURSOR_HEIGHT, MOUSE_CURSOR_WIDTH};

pub struct Surface {
    pub width: usize,
    pub height: usize,
    pub buffer: Vec<u32>,
}

impl Surface {
    pub fn new(width: usize, height: usize) -> Self {
        Self {
            width,
            height,
            buffer: vec![0; width * height],
        }
    }

    pub fn clear(&mut self, color: Color) {
        let val = color.to_u32_argb();
        self.buffer.fill(val);
    }

    #[inline(always)]
    pub fn set_pixel(&mut self, x: i32, y: i32, color: Color) {
        if x < 0 || y < 0 || (x as usize) >= self.width || (y as usize) >= self.height {
            return;
        }
        let idx = (y as usize) * self.width + (x as usize);
        if color.a == 255 {
            self.buffer[idx] = color.to_u32_argb();
        } else if color.a > 0 {
            let bg_val = self.buffer[idx];
            let bg = Color::rgba(
                ((bg_val >> 16) & 0xFF) as u8,
                ((bg_val >> 8) & 0xFF) as u8,
                (bg_val & 0xFF) as u8,
                ((bg_val >> 24) & 0xFF) as u8,
            );
            let blended = color.blend(bg);
            self.buffer[idx] = blended.to_u32_argb();
        }
    }

    pub fn draw_rect(&mut self, x: i32, y: i32, w: usize, h: usize, color: Color) {
        for dy in 0..h {
            for dx in 0..w {
                self.set_pixel(x + dx as i32, y + dy as i32, color);
            }
        }
    }

    pub fn draw_rect_outline(&mut self, x: i32, y: i32, w: usize, h: usize, color: Color, thickness: usize) {
        for t in 0..thickness {
            let offset = t as i32;
            let current_w = if w > 2 * t { w - 2 * t } else { 0 };
            let current_h = if h > 2 * t { h - 2 * t } else { 0 };

            for dx in 0..current_w {
                self.set_pixel(x + offset + dx as i32, y + offset, color);
                self.set_pixel(x + offset + dx as i32, y + offset + current_h as i32 - 1, color);
            }
            for dy in 0..current_h {
                self.set_pixel(x + offset, y + offset + dy as i32, color);
                self.set_pixel(x + offset + current_w as i32 - 1, y + offset + dy as i32, color);
            }
        }
    }

    pub fn draw_rounded_rect(&mut self, x: i32, y: i32, w: usize, h: usize, radius: i32, color: Color) {
        let r = radius.min((w / 2) as i32).min((h / 2) as i32);
        let r_sq = r * r;

        for dy in 0..h as i32 {
            for dx in 0..w as i32 {
                let in_corner_tl = dx < r && dy < r;
                let in_corner_tr = dx >= (w as i32 - r) && dy < r;
                let in_corner_bl = dx < r && dy >= (h as i32 - r);
                let in_corner_br = dx >= (w as i32 - r) && dy >= (h as i32 - r);

                let draw = if in_corner_tl {
                    let cx = r - dx;
                    let cy = r - dy;
                    cx * cx + cy * cy <= r_sq
                } else if in_corner_tr {
                    let cx = dx - (w as i32 - r - 1);
                    let cy = r - dy;
                    cx * cx + cy * cy <= r_sq
                } else if in_corner_bl {
                    let cx = r - dx;
                    let cy = dy - (h as i32 - r - 1);
                    cx * cx + cy * cy <= r_sq
                } else if in_corner_br {
                    let cx = dx - (w as i32 - r - 1);
                    let cy = dy - (h as i32 - r - 1);
                    cx * cx + cy * cy <= r_sq
                } else {
                    true
                };

                if draw {
                    self.set_pixel(x + dx, y + dy, color);
                }
            }
        }
    }

    pub fn draw_gradient_v(&mut self, x: i32, y: i32, w: usize, h: usize, top: Color, bottom: Color) {
        if h == 0 || w == 0 {
            return;
        }
        let total = (h - 1).max(1) as u32;

        for dy in 0..h {
            let py = y + dy as i32;
            if py < 0 || py as usize >= self.height {
                continue;
            }
            let factor = dy as u32;
            let inv_factor = (h - 1 - dy) as u32;

            let r = ((top.r as u32 * inv_factor + bottom.r as u32 * factor) / total) as u8;
            let g = ((top.g as u32 * inv_factor + bottom.g as u32 * factor) / total) as u8;
            let b = ((top.b as u32 * inv_factor + bottom.b as u32 * factor) / total) as u8;
            let a = ((top.a as u32 * inv_factor + bottom.a as u32 * factor) / total) as u8;

            let val = Color::rgba(r, g, b, a).to_u32_argb();

            let x0 = x.max(0) as usize;
            let x1 = ((x + w as i32).max(0) as usize).min(self.width);
            if x1 > x0 {
                let row_offset = py as usize * self.width;
                self.buffer[row_offset + x0..row_offset + x1].fill(val);
            }
        }
    }

    pub fn draw_drop_shadow(&mut self, x: i32, y: i32, w: usize, h: usize, radius: i32, blur: usize) {
        let max_blur = blur.min(4);
        for b in (1..=max_blur).rev() {
            let alpha = ((20 * (max_blur - b + 1)) / max_blur) as u8;
            let shadow_color = Color::rgba(0, 0, 0, alpha);
            let offset = b as i32;
            self.draw_rounded_rect(
                x - offset,
                y - offset + 2,
                w + (b * 2),
                h + (b * 2),
                radius + offset,
                shadow_color,
            );
        }
    }

    pub fn draw_text(&mut self, text: &str, start_x: i32, start_y: i32, color: Color) {
        let mut cur_x = start_x;
        let mut cur_y = start_y;

        for c in text.chars() {
            if c == '\n' {
                cur_x = start_x;
                cur_y += FONT_HEIGHT as i32;
                continue;
            }
            let glyph = get_glyph(c);
            for (row_idx, &row_byte) in glyph.iter().enumerate() {
                for bit in 0..8 {
                    if (row_byte & (0x80 >> bit)) != 0 {
                        self.set_pixel(cur_x + bit, cur_y + row_idx as i32, color);
                    }
                }
            }
            cur_x += FONT_WIDTH as i32;
        }
    }

    pub fn draw_text_with_shadow(&mut self, text: &str, x: i32, y: i32, color: Color, shadow_color: Color) {
        self.draw_text(text, x + 1, y + 1, shadow_color);
        self.draw_text(text, x, y, color);
    }

    pub fn draw_icon_16(&mut self, icon: IconType, x: i32, y: i32, color: Color) {
        let bitmap = get_icon_16(icon);
        for row in 0..16 {
            let row_val = bitmap[row];
            for col in 0..16 {
                if (row_val & (0x8000 >> col)) != 0 {
                    self.set_pixel(x + col, y + row as i32, color);
                }
            }
        }
    }

    pub fn draw_close_cross(&mut self, cx: i32, cy: i32, size: i32, thickness: i32, color: Color) {
        for d in -size..=size {
            for t in 0..thickness {
                self.set_pixel(cx + d + t, cy + d, color);
                self.set_pixel(cx + d + t, cy - d, color);
            }
        }
    }

    pub fn draw_mouse_cursor(&mut self, x: i32, y: i32) {
        for dy in 0..MOUSE_CURSOR_HEIGHT {
            for dx in 0..MOUSE_CURSOR_WIDTH {
                let pixel = MOUSE_CURSOR[dy][dx];
                if pixel == 1 {
                    self.set_pixel(x + dx as i32, y + dy as i32, Color::BLACK);
                } else if pixel == 2 {
                    self.set_pixel(x + dx as i32, y + dy as i32, Color::WHITE);
                }
            }
        }
    }

    pub fn copy_from(&mut self, src: &Surface) {
        let copy_len = self.buffer.len().min(src.buffer.len());
        self.buffer[..copy_len].copy_from_slice(&src.buffer[..copy_len]);
    }

    pub fn copy_rect_from(&mut self, src: &Surface, rx: i32, ry: i32, rw: usize, rh: usize) {
        let x0 = rx.max(0) as usize;
        let y0 = ry.max(0) as usize;
        let x1 = ((rx + rw as i32).max(0) as usize).min(self.width).min(src.width);
        let y1 = ((ry + rh as i32).max(0) as usize).min(self.height).min(src.height);

        if x1 <= x0 || y1 <= y0 {
            return;
        }

        for y in y0..y1 {
            let row_offset = y * self.width;
            let src_offset = y * src.width;
            self.buffer[row_offset + x0..row_offset + x1]
                .copy_from_slice(&src.buffer[src_offset + x0..src_offset + x1]);
        }
    }

    pub fn blit_rect_to_framebuffer(&self, fb: &mut FramebufferConfig, rx: i32, ry: i32, rw: usize, rh: usize) {
        let x0 = rx.max(0) as usize;
        let y0 = ry.max(0) as usize;
        let x1 = ((rx + rw as i32).max(0) as usize).min(self.width).min(fb.width);
        let y1 = ((ry + rh as i32).max(0) as usize).min(self.height).min(fb.height);

        if x1 <= x0 || y1 <= y0 {
            return;
        }

        let bpp = fb.bytes_per_pixel;
        let is_bgr = fb.pixel_format == PixelFormat::Bgr;

        // Ultra-fast 32-bit aligned word blit (avoids 4 separate MMIO traps per pixel in VirtualBox IEM)
        if is_bgr && bpp == 4 {
            for y in y0..y1 {
                let src_row = &self.buffer[y * self.width + x0..y * self.width + x1];
                let dst_ptr = unsafe { (fb.buffer as *mut u32).add(y * fb.stride + x0) };
                for (i, &val) in src_row.iter().enumerate() {
                    unsafe {
                        *dst_ptr.add(i) = val | 0xFF000000;
                    }
                }
            }
            return;
        }

        for y in y0..y1 {
            let src_row_start = y * self.width;
            let dst_row_start = y * fb.stride * bpp;

            for x in x0..x1 {
                let val = self.buffer[src_row_start + x];
                let r = ((val >> 16) & 0xFF) as u8;
                let g = ((val >> 8) & 0xFF) as u8;
                let b = (val & 0xFF) as u8;

                let dst_offset = dst_row_start + x * bpp;
                unsafe {
                    let ptr = fb.buffer.add(dst_offset);
                    if is_bgr {
                        *ptr = b;
                        *ptr.add(1) = g;
                        *ptr.add(2) = r;
                    } else {
                        *ptr = r;
                        *ptr.add(1) = g;
                        *ptr.add(2) = b;
                    }
                    if bpp == 4 {
                        *ptr.add(3) = 0xFF;
                    }
                }
            }
        }
    }

    pub fn blit_to_framebuffer(&self, fb: &mut FramebufferConfig) {
        let copy_width = self.width.min(fb.width);
        let copy_height = self.height.min(fb.height);
        let bpp = fb.bytes_per_pixel;
        let is_bgr = fb.pixel_format == PixelFormat::Bgr;

        // Ultra-fast 32-bit aligned full-screen blit
        if is_bgr && bpp == 4 {
            for y in 0..copy_height {
                let src_row = &self.buffer[y * self.width..y * self.width + copy_width];
                let dst_ptr = unsafe { (fb.buffer as *mut u32).add(y * fb.stride) };
                for (i, &val) in src_row.iter().enumerate() {
                    unsafe {
                        *dst_ptr.add(i) = val | 0xFF000000;
                    }
                }
            }
            return;
        }

        for y in 0..copy_height {
            let src_row_start = y * self.width;
            let dst_row_start = y * fb.stride * bpp;

            for x in 0..copy_width {
                let val = self.buffer[src_row_start + x];
                let r = ((val >> 16) & 0xFF) as u8;
                let g = ((val >> 8) & 0xFF) as u8;
                let b = (val & 0xFF) as u8;

                let dst_offset = dst_row_start + x * bpp;
                unsafe {
                    let ptr = fb.buffer.add(dst_offset);
                    if is_bgr {
                        *ptr = b;
                        *ptr.add(1) = g;
                        *ptr.add(2) = r;
                    } else {
                        *ptr = r;
                        *ptr.add(1) = g;
                        *ptr.add(2) = b;
                    }
                    if bpp == 4 {
                        *ptr.add(3) = 0xFF;
                    }
                }
            }
        }
    }
}