axle OS
x86_32 UNIX-like hobby OS
gfx.h
1 #ifndef GFX_H
2 #define GFX_H
3 
4 #include <std/common.h>
5 #include <std/timer.h>
6 
7 typedef void (*event_handler)(void* obj, void* context);
8 
9 #include "rect.h"
10 #include "view.h"
11 #include "button.h"
12 #include <gfx/font/font.h>
13 #include <std/math.h>
14 #include <kernel/drivers/vbe/vbe.h>
15 
16 typedef struct __attribute__((packed)) {
17  unsigned short di, si, bp, sp, bx, dx, cx, ax;
18  unsigned short gs, fs, es, ds, eflags;
19 } regs16_t;
20 
21 typedef struct window Window;
22 typedef struct screen_t {
23  Window* window; //root window
24  uint16_t depth; //bits per pixel
25  uint8_t bpp; //bytes per pixel
26  Size resolution;
27  uint32_t* physbase; //address of beginning of framebuffer
28  volatile int finished_drawing; //are we currently rendering a frame?
29  ca_layer* vmem; //raw framebuffer pushed to screen
30  Size default_font_size; //recommended font size for screen resolution
31 } Screen;
32 
33 typedef struct Vec2d {
34  double x;
35  double y;
36 } Vec2d;
37 
38 extern void int32(unsigned char intnum, regs16_t* regs);
39 
40 Screen* screen_create(Size dimensions, uint32_t* physbase, uint8_t depth);
41 
42 void gfx_teardown(Screen* screen);
43 void vga_boot_screen(Screen* screen);
44 
45 //fill double buffer with a given Color
46 void fill_screen(Screen* screen, Color color);
47 //copy all double buffer data to real screen
48 void write_screen(Screen* screen);
49 //copy 'region' from double buffer to real screen
50 void write_screen_region(Rect region);
51 
52 void draw_boot_background();
53 void display_boot_screen();
54 
55 void gfx_init(void* mboot_ptr);
56 void process_gfx_switch(Screen* screen, int new_depth);
57 void set_gfx_depth(uint32_t depth);
58 int gfx_depth();
59 int gfx_bpp();
60 Screen* gfx_screen();
61 
62 Vec2d vec2d(double x, float y);
63 
64 __attribute__((always_inline))
65 inline void putpixel(ca_layer* layer, int x, int y, Color color) {
66  //don't attempt writing a pixel outside of screen bounds
67  //if (x < 0 || y < 0 || x >= layer->size.width || y >= layer->size.height) return;
68 
69  int depth = gfx_depth();
70  int bpp = gfx_bpp();
71  bool write_directly = !(layer);
72  int offset = (x * bpp) + (y * layer->size.width * bpp);
73 
74 
75  if (depth == 4 || write_directly) {
76  offset = (x * bpp) + (y * gfx_screen()->resolution.width * bpp);
77 
78  int bank = offset / BANK_SIZE;
79  int bank_offset = offset % BANK_SIZE;
80  vbe_set_bank(bank);
81 
82  uint8_t* vmem = (uint8_t*)VBE_DISPI_LFB_PHYSICAL_ADDRESS;
83 
84  if (depth == 4) {
85  vmem[bank_offset] = 3;
86  }
87  else if (write_directly) {
88  vmem[bank_offset + 0] = color.val[0];
89  vmem[bank_offset + 1] = color.val[1];
90  vmem[bank_offset + 2] = color.val[2];
91  }
92  return;
93  }
94 
95  for (int i = 0; i < MIN(3, bpp); i++) {
96  //we have to write the pixels in BGR, not RGB
97  //layer->raw[offset + i] = color.val[bpp - 1 - i];
98  layer->raw[offset + i] = color.val[bpp - 1 - i];
99  }
100 }
101 __attribute__((always_inline))
102 inline void addpixel(ca_layer* layer, int x, int y, Color color) {
103  //don't attempt writing a pixel outside of screen bounds
104  if (x < 0 || y < 0 || x >= layer->size.width || y >= layer->size.height) return;
105 
106  int bpp = gfx_bpp();
107  int offset = (x * bpp) + (y * layer->size.width * bpp);
108  for (int i = 0; i < bpp; i++) {
109  //we have to write the pixels in BGR, not RGB
110  layer->raw[offset + i] += color.val[bpp - 1 - i];
111  }
112 }
113 
114 #endif
Definition: gfx.h:33
Definition: color.h:7
Definition: elf.h:10
Definition: size.h:4
Definition: rect.h:14
Definition: window.h:16
Definition: gfx.h:22