Advertisement
LittleFox94

LF OS framebuffer test - also on linux :3

Aug 27th, 2020
4,547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <unistd.h>
  3. #include <cstdint>
  4. #include <cmath>
  5.  
  6. #if defined(__LF_OS__)
  7. #define _POSIX_THREADS
  8. #include <pthread.h>
  9. #include <kernel/syscalls.h>
  10. #elif  defined(__linux)
  11. #include <sys/mman.h>
  12. #include <sys/stat.h>
  13. #include <sys/types.h>
  14. #include <fcntl.h>
  15.  
  16. void sc_do_hardware_framebuffer(uint32_t** fb, uint16_t* width, uint16_t* height, uint16_t* stride, uint16_t* color) {
  17.     *width = 800;
  18.     *height = 600;
  19.     *stride = 800;
  20.  
  21.     int fd = open("framebuffer.bmp", O_CREAT|O_RDWR, 0644);
  22.  
  23.     // BMP file header
  24.     write(fd, "BM", 2); // magic
  25.     uint32_t i = (*width * *height * 4) + 4096;
  26.     write(fd, &i, 4); // file size
  27.     i = 0;
  28.     write(fd, &i, 4); // reserved
  29.     i = 4096;
  30.     write(fd, &i, 4); // image data offset
  31.  
  32.     // BMP info header
  33.     i = 40;
  34.     write(fd, &i, 4);  // info header size
  35.     i = *width;
  36.     write(fd, &i, 4);
  37.     i = *height;
  38.     write(fd, &i, 4);
  39.     i = (32 << 16) | 1;
  40.     write(fd, &i, 4);  // planes & bpp
  41.     i = 0;
  42.     write(fd, &i, 4); // compression
  43.     i = *width * *height * 4;
  44.     write(fd, &i, 4); // image size
  45.     i = 0;
  46.     write(fd, &i, 4); // pixels per meter x
  47.     write(fd, &i, 4); // pixels per meter y
  48.     write(fd, &i, 4); // color table size
  49.     write(fd, &i, 4); // color table important
  50.  
  51.     lseek(fd, (*width * *height * 4) + 4095, SEEK_SET);
  52.     write(fd, "", 1);
  53.  
  54.     *fb = (uint32_t*)mmap(0, *width * *height * 4, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 4096);
  55.  
  56.     close(fd);
  57. }
  58. #endif
  59.  
  60. #include <pthread.h>
  61.  
  62. void hsv_to_rgb (float h, float s, float v, uint8_t *r_r, uint8_t *r_g, uint8_t *r_b) {
  63.     double      hh, p, q, t, ff;
  64.     long        i;
  65.  
  66.     if(s <= 0.0) {       // < is bogus, just shuts up warnings
  67.         *r_r = v * 255.f;
  68.         *r_g = v * 255.f;
  69.         *r_b = v * 255.f;
  70.     }
  71.     hh = h;
  72.     if(hh >= 360.0) hh = 0.0;
  73.     hh /= 60.0;
  74.     i = (long)hh;
  75.     ff = hh - i;
  76.     p = v * (1.0 -  s);
  77.     q = v * (1.0 - (s * ff));
  78.     t = v * (1.0 - (s * (1.0 - ff)));
  79.  
  80.     switch(i) {
  81.     case 0:
  82.         *r_r = 255.0 * v;
  83.         *r_g = 255.0 * t;
  84.         *r_b = 255.0 * p;
  85.         break;
  86.     case 1:
  87.         *r_r = 255.0 * q;
  88.         *r_g = 255.0 * v;
  89.         *r_b = 255.0 * p;
  90.         break;
  91.     case 2:
  92.         *r_r = 255.0 * p;
  93.         *r_g = 255.0 * v;
  94.         *r_b = 255.0 * t;
  95.         break;
  96.     case 3:
  97.         *r_r = 255.0 * p;
  98.         *r_g = 255.0 * q;
  99.         *r_b = 255.0 * v;
  100.         break;
  101.     case 4:
  102.         *r_r = 255.0 * t;
  103.         *r_g = 255.0 * p;
  104.         *r_b = 255.0 * v;
  105.         break;
  106.     case 5:
  107.     default:
  108.         *r_r = 255.0 * v;
  109.         *r_g = 255.0 * p;
  110.         *r_b = 255.0 * q;
  111.         break;
  112.     }
  113. }
  114.  
  115. int main(int argc, char* argv[]) {
  116.     volatile uint32_t* fb;
  117.     uint16_t width, height, stride, colorFormat;
  118.     sc_do_hardware_framebuffer((uint32_t**)&fb, &width, &height, &stride, &colorFormat);
  119.  
  120.     int error;
  121.     pthread_mutex_t fb_mutex;
  122.     if((error = pthread_mutex_init(&fb_mutex, 0)) != 0) {
  123.         printf("mutex_init error: %d\n", error);
  124.         return EXIT_FAILURE;
  125.     }
  126.  
  127.     if((error = pthread_mutex_lock(&fb_mutex)) != 0) {
  128.         printf("main process lock error: %d\n", error);
  129.     }
  130.  
  131.     const uint16_t forks = 64;
  132.     const uint16_t grid  = std::sqrt(forks);
  133.  
  134.     for(int i = 0; i < forks; ++i) {
  135.         volatile int pid = fork();
  136.  
  137.         if(pid == 0) {
  138.             const uint16_t row = i % grid;
  139.             const uint16_t col = i / grid;
  140.  
  141.             const uint16_t xstart = col * (width  / grid);
  142.             const uint16_t ystart = row * (height / grid);
  143.  
  144.             float h = (row / (float)grid) * 120;
  145.             float s = 0.75;
  146.             float v = 0.5 + (col / ((float)grid) * 0.5);
  147.  
  148.             uint8_t color[4];
  149.             color[3] = 0;
  150.  
  151.             while(true) {
  152.                 if((error = pthread_mutex_lock(&fb_mutex)) != 0) {
  153.                     printf("fb process %d lock error: %d\n", i, error);
  154.                 }
  155.  
  156.                 hsv_to_rgb(h, s, v, &color[2], &color[1], &color[0]);
  157.  
  158.                 for(size_t y = 0; y < height / grid; ++y) {
  159.                     for(size_t x = 0; x < width / grid; ++x) {
  160.                         fb[(ystart + y) * width + xstart + x] = *(uint32_t*)color;
  161.                     }
  162.                 }
  163.  
  164.                 h += 5;
  165.                 while(h > 360) {
  166.                     h -= 360;
  167.                 }
  168.  
  169.                 if((error = pthread_mutex_unlock(&fb_mutex)) != 0) {
  170.                     printf("fb process %d unlock error: %d\n", i, error);
  171.                 }
  172.  
  173. #if defined(__linux)
  174.                 return EXIT_SUCCESS;
  175. #endif
  176.             }
  177.         }
  178.     }
  179.  
  180.     if((error = pthread_mutex_unlock(&fb_mutex)) != 0) {
  181.         printf("main process unlock error: %d\n", error);
  182.     }
  183.  
  184.     return EXIT_SUCCESS;
  185. }
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement