cederom commented on PR #17294:
URL: https://github.com/apache/nuttx/pull/17294#issuecomment-3506973383

   Some colors ordering hints here https://www.rpi4os.com/part5-framebuffer/ ?
   
   > The framebuffer
   > 
   > Now take a look at fb.c. The fb_init() routine makes our very first 
mailbox call, using some definitions from mb.h. Remember the email analogy? 
Well, since it’s possible to ask a person to do more than one thing by email, 
we can also ask a few things of the VideoCore at once. This message asks for 
two things:
   > 
   >     A pointer to the framebuffer start (MBOX_TAG_GETFB)
   >     The pitch (MBOX_TAG_GETPITCH)
   > 
   > You can read more about the message structure on the [mailbox property 
interface](https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface)
 page that I shared before.
   > 
   > A framebuffer is simply an area of memory that contains a bitmap which 
drives a video display. In other words, we can manipulate the pixels on the 
screen directly by writing to specific memory addresses. We will first need to 
understand how this memory is organised though.
   > 
   > In this example, we ask the VideoCore for:
   > 
   >     a simple 1920x1080 (1080p) framebuffer
   >     a depth of 32 bits per pixel, with an RGB pixel order
   > 
   > So each pixel is made up of 8-bits for the Red value, 8-bits for Green, 
8-bits for Blue and 8-bits for the Alpha channel (representing 
transparency/opacity). We’re asking that the pixels are ordered in memory with 
the Red byte coming first, then Green, then Blue - RGB. In actual fact, the 
Alpha byte always comes ahead of all of these, so it’s really ARGB.
   > 
   > We then send the message using channel 8 (MBOX_CH_PROP) and check that 
what the VideoCore sends back is what we asked for. It should also tell us the 
missing piece of the framebuffer organisation puzzle - the number of bytes per 
line or pitch.
   > 
   > If everything comes back as expected, then we’re ready to write to the 
screen!
   > Drawing a pixel
   > 
   > To save us remembering RGB colour combinations, let’s set up a simple 
16-colour palette. Anyone remember the old [EGA/VGA 
palette](https://en.wikipedia.org/wiki/Enhanced_Graphics_Adapter)? If you take 
a look in terminal.h, you’ll see the vgapal array sets up that same palette, 
with Black as item 0 and White as item 15, and many shades in between!
   > 
   > Our drawPixel routine can then take an (x, y) coordinate and a colour. We 
use an unsigned char (8 bits) to represent two palette indexes at once, with 
the 4 most significant bits representing the background colour and the 4 least 
significant bits, the foreground colour. You may see why it’s helpful to have 
only a 16-colour palette for now!
   > 
   >      void drawPixel(int x, int y, unsigned char attr)
   >       {
   >           int offs = (y * pitch) + (x * 4);
   >           *((unsigned int*)(fb + offs)) = vgapal[attr & 0x0f];
   >       }
   > 
   > We first calculate the framebuffer offset in bytes. (y * pitch) gets us to 
coordinate (0, y) - pitch is the number of bytes per line. We then add (x * 4) 
to get to (x, y) - there are 4 bytes (or 32 bits!) per pixel (ARGB). We can 
then set that byte in the framebuffer to our foreground colour (we don’t need a 
background colour here).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to