Xyris  0.5
graphics.cpp
Go to the documentation of this file.
1 /**
2  * @file graphics.cpp
3  * @author Keeton Feavel ([email protected])
4  * @author Michel (JMallone) Gomes ([email protected])
5  * @brief Graphics management and control
6  * @version 0.2
7  * @date 2021-07-24
8  *
9  * @copyright Copyright the Xyris Contributors (c) 2021
10  *
11  * References:
12  * https://wiki.osdev.org/Double_Buffering
13  * https://github.com/skiftOS/skift/blob/main/kernel/system/Graphics/Graphics.cpp
14  *
15  */
17 #include <stddef.h>
18 #include <stdint.h>
19 #include <Memory/heap.hpp>
20 #include <Memory/paging.hpp>
21 #include <Bootloader/Handoff.hpp>
22 #include <Library/stdio.hpp>
23 #include <Library/string.hpp>
24 #include <Logger.hpp>
25 
26 namespace Graphics {
27 
28 static Framebuffer* info = NULL;
29 static void* backbuffer = NULL;
30 static bool initialized = false;
31 
32 void init(Framebuffer* fb)
33 {
34  // Get the framebuffer info
35  if (!(info = fb))
36  return;
37  // Ensure valid info is provided
38  if (!info->getAddress())
39  return;
40  // Map in the framebuffer
41  Logger::Debug(__func__, "==== MAP FRAMEBUFFER ====");
43  (uintptr_t)info->getAddress(),
44  (uintptr_t)info->getAddress() + (info->getPitch() * info->getHeight())
45  ));
46  // Alloc the backbuffer
47  backbuffer = malloc(info->getPitch() * info->getHeight());
48  memcpy(backbuffer, info->getAddress(), info->getPitch() * info->getHeight());
49 
50  initialized = true;
51 }
52 
53 void pixel(uint32_t x, uint32_t y, uint32_t color)
54 {
55  // Ensure framebuffer information exists
56  if (!initialized)
57  return;
58  if ((x <= info->getWidth()) && (y <= info->getHeight())) {
59  // Special thanks to the SkiftOS contributors.
60  uint8_t* pixel = (uint8_t*)backbuffer + (y * info->getPitch()) + (x * info->getPixelWidth());
61  // Pixel information
62  pixel[0] = (color >> info->getBlueMaskShift()) & 0xff; // B
63  pixel[1] = (color >> info->getGreenMaskShift()) & 0xff; // G
64  pixel[2] = (color >> info->getRedMaskShift()) & 0xff; // R
65  // Additional pixel information
66  if (info->getPixelWidth() == 4)
67  pixel[3] = 0x00;
68  }
69 }
70 
71 void putrect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t color)
72 {
73  // Ensure framebuffer information exists
74  if (!initialized)
75  return;
76  for (uint32_t curr_x = x; curr_x <= x + w; curr_x++) {
77  for (uint32_t curr_y = y; curr_y <= y + h; curr_y++) {
78  // Extremely slow but good for debugging
79  pixel(curr_x, curr_y, color);
80  }
81  }
82  // Swap after doing a large operation
83  swap();
84 }
85 
87 {
88  if (!initialized)
89  return;
90  memset(backbuffer, 0, (info->getPitch() * info->getHeight()));
91 }
92 
93 void swap()
94 {
95  if (!initialized)
96  return;
97  memcpy(info->getAddress(), backbuffer, (info->getPitch() * info->getHeight()));
98 }
99 
100 } // !namespace graphics
stdio.hpp
graphics.hpp
Graphics::Framebuffer::getGreenMaskShift
uint8_t getGreenMaskShift()
Definition: framebuffer.hpp:41
Graphics::Framebuffer::getPitch
uint32_t getPitch()
Definition: framebuffer.hpp:37
Graphics::Framebuffer::getAddress
void * getAddress()
Definition: framebuffer.hpp:33
Graphics::pixel
void pixel(uint32_t x, uint32_t y, uint32_t color)
Draws a pixel at a given coordinate.
Definition: graphics.cpp:53
Graphics::Framebuffer::getPixelWidth
uint8_t getPixelWidth()
Definition: framebuffer.hpp:44
Graphics::swap
void swap()
Swap the data on backbuffer to memory video buffer and show in the screen.
Definition: graphics.cpp:93
string.hpp
Standard string and memory utility library.
Graphics::resetDoubleBuffer
void resetDoubleBuffer()
Fill the backbuffer with '0'.
Definition: graphics.cpp:86
Graphics::Framebuffer::getBlueMaskShift
uint8_t getBlueMaskShift()
Definition: framebuffer.hpp:43
Graphics
Definition: font.cpp:17
Memory::Section
Definition: MemorySection.hpp:29
Graphics::Framebuffer
Definition: framebuffer.hpp:21
Graphics::Framebuffer::getRedMaskShift
uint8_t getRedMaskShift()
Definition: framebuffer.hpp:39
Handoff.hpp
memcpy
void * memcpy(void *dstptr, const void *srcptr, size_t size)
Definition: string.cpp:141
paging.hpp
Logger::Debug
static void Debug(const char *tag, const char *fmt,...)
Definition: Logger.cpp:71
malloc
void * malloc(size_t)
heap.hpp
Liballoc heap implementation.
Graphics::Framebuffer::getHeight
uint32_t getHeight()
Definition: framebuffer.hpp:35
Memory::mapKernelRangeVirtual
void mapKernelRangeVirtual(Section sect)
Map an address range into the kernel virtual address space.
Definition: paging.cpp:192
memset
void * memset(void *bufptr, int value, size_t size)
Sets the number of bytes in memory at ptr to the value.
Definition: string.cpp:106
Graphics::putrect
void putrect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t color)
Draws and fills a rectangle of a given width and height, and color at the provided coordinates.
Definition: graphics.cpp:71
Graphics::init
void init(Framebuffer *fb)
Initializes the framebuffer (if available)
Definition: graphics.cpp:32
Logger.hpp