Skip to content

Commit aab65cb

Browse files
committed
[FREELDR:XBOX/UEFI] vidfb: stub-plement pixel bitmasks support (reactos#8531)
This is the starting point where both support for generic framebuffer video driver support (in bootvid and win32k) for XBOX, UEFI etc., and in FreeLoader itself also for PC VESA, begins.
1 parent 60a0d02 commit aab65cb

5 files changed

Lines changed: 208 additions & 4 deletions

File tree

‎boot/freeldr/freeldr/arch/i386/xbox/xboxvideo.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ XboxVideoInit(VOID)
134134
ScreenWidth,
135135
ScreenHeight,
136136
ScreenWidth, // PixelsPerScanLine
137-
BytesPerPixel * 8);
137+
BytesPerPixel * 8,
138+
NULL);
138139

139140
VidFbClearScreenColor(MAKE_COLOR(0, 0, 0), TRUE);
140141
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* PROJECT: FreeLoader
3+
* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4+
* or MIT (https://spdx.org/licenses/MIT)
5+
* PURPOSE: Bit twiddling helpers
6+
* COPYRIGHT: Copyright 2025 Hermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
7+
*
8+
* Based on http://www.graphics.stanford.edu/~seander/bithacks.html
9+
* and other sources.
10+
*/
11+
12+
// HAVE___BUILTIN_POPCOUNT
13+
14+
#pragma once
15+
16+
/**
17+
* @brief Return the number of bits set in a 32-bit integer.
18+
* @note Equivalent to __popcnt().
19+
**/
20+
FORCEINLINE
21+
ULONG
22+
CountNumberOfBits(
23+
_In_ UINT32 n)
24+
{
25+
#ifdef HAVE___BUILTIN_POPCOUNT
26+
return __popcnt(n);
27+
#else
28+
n -= ((n >> 1) & 0x55555555);
29+
n = (((n >> 2) & 0x33333333) + (n & 0x33333333));
30+
#if 0
31+
n = (((n >> 4) + n) & 0x0f0f0f0f);
32+
n += (n >> 8);
33+
n += (n >> 16);
34+
return (n & 0x3f);
35+
#else
36+
return (((n >> 4) + n) & 0x0f0f0f0f) * 0x01010101 >> 24;
37+
#endif
38+
#endif /* HAVE___BUILTIN_POPCOUNT */
39+
}

‎boot/freeldr/freeldr/arch/uefi/uefivid.c‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,38 @@ EFI_GUID EfiGraphicsOutputProtocol = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
2121

2222
/* FUNCTIONS ******************************************************************/
2323

24+
/* EFI 1.x */
25+
#ifdef EFI_UGA_DRAW_PROTOCOL_GUID
26+
27+
/* NOTE: EFI UGA does not support any other format than 32-bit xRGB, and
28+
* no direct access to the underlying hardware framebuffer is offered */
29+
C_ASSERT(sizeof(EFI_UGA_PIXEL) == sizeof(ULONG));
30+
31+
#endif /* EFI */
32+
33+
/* UEFI support, see efi/GraphicsOutput.h */
34+
#ifdef EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID // __GRAPHICS_OUTPUT_H__
35+
36+
C_ASSERT(sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL) == sizeof(ULONG));
37+
38+
/**
39+
* @brief Maps UEFI GOP pixel format to pixel masks.
40+
* @see EFI_PIXEL_BITMASK
41+
**/
42+
static EFI_PIXEL_BITMASK EfiPixelMasks[] =
43+
{ /* Red, Green, Blue, Reserved */
44+
{0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000}, // PixelRedGreenBlueReserved8BitPerColor
45+
{0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000}, // PixelBlueGreenRedReserved8BitPerColor
46+
{0, 0, 0, 0} // PixelBitMask, PixelBltOnly, ...
47+
};
48+
49+
#endif /* UEFI */
50+
2451
EFI_STATUS
2552
UefiInitializeVideo(VOID)
2653
{
2754
EFI_GRAPHICS_PIXEL_FORMAT PixelFormat;
55+
EFI_PIXEL_BITMASK* pPixelBitmask;
2856
ULONG BitsPerPixel;
2957

3058
EFI_STATUS Status;
@@ -47,15 +75,33 @@ UefiInitializeVideo(VOID)
4775
case PixelRedGreenBlueReserved8BitPerColor:
4876
case PixelBlueGreenRedReserved8BitPerColor:
4977
{
78+
pPixelBitmask = &EfiPixelMasks[PixelFormat];
5079
BitsPerPixel = RTL_BITS_OF(EFI_GRAPHICS_OUTPUT_BLT_PIXEL);
5180
break;
5281
}
5382

5483
case PixelBitMask:
84+
{
85+
/*
86+
* When the GOP pixel format is given by PixelBitMask, the pixel
87+
* element size _may be_ different from 4 bytes.
88+
* See UEFI Spec Rev.2.10 Section 12.9 "Graphics Output Protocol":
89+
* example code "GetPixelElementSize()" function.
90+
*/
91+
pPixelBitmask = &gop->Mode->Info->PixelInformation;
92+
BitsPerPixel =
93+
PixelBitmasksToBpp(pPixelBitmask->RedMask,
94+
pPixelBitmask->GreenMask,
95+
pPixelBitmask->BlueMask,
96+
pPixelBitmask->ReservedMask);
97+
break;
98+
}
99+
55100
case PixelBltOnly:
56101
default:
57102
{
58103
ERR("Unsupported UFEI GOP format %lu\n", PixelFormat);
104+
pPixelBitmask = NULL;
59105
BitsPerPixel = 0;
60106
break;
61107
}
@@ -66,7 +112,8 @@ UefiInitializeVideo(VOID)
66112
gop->Mode->Info->HorizontalResolution,
67113
gop->Mode->Info->VerticalResolution,
68114
gop->Mode->Info->PixelsPerScanLine,
69-
BitsPerPixel);
115+
BitsPerPixel,
116+
(PPIXEL_BITMASK)pPixelBitmask);
70117
return Status;
71118
}
72119

‎boot/freeldr/freeldr/arch/vidfb.c‎

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ typedef struct _FRAMEBUFFER_INFO
3131
ULONG PixelsPerScanLine; // aka. "Pitch" or "ScreenStride", but Stride is in bytes or bits...
3232
ULONG BitsPerPixel; // aka. "PixelStride".
3333

34+
/* Physical format of the pixel for BPP > 8, specified by bit-mask */
35+
PIXEL_BITMASK PixelMasks;
36+
3437
/** Calculated values */
3538

3639
ULONG BytesPerPixel;
@@ -55,6 +58,11 @@ VidFbPrintFramebufferInfo(VOID)
5558
TRACE(" BitsPerPixel : %lu\n", framebufInfo.BitsPerPixel);
5659
TRACE(" BytesPerPixel : %lu\n", framebufInfo.BytesPerPixel);
5760
TRACE(" Delta : %lu\n", framebufInfo.Delta);
61+
TRACE(" ARGB masks: : %08x/%08x/%08x/%08x\n",
62+
framebufInfo.PixelMasks.ReservedMask,
63+
framebufInfo.PixelMasks.RedMask,
64+
framebufInfo.PixelMasks.GreenMask,
65+
framebufInfo.PixelMasks.BlueMask);
5866
}
5967
#endif
6068

@@ -78,6 +86,10 @@ VidFbPrintFramebufferInfo(VOID)
7886
* @param[in] BitsPerPixel
7987
* The number of usable bits (not counting the reserved ones) per pixel.
8088
*
89+
* @param[in] PixelMasks
90+
* Optional pointer to a PIXEL_BITMASK structure describing the pixel
91+
* format used by the framebuffer.
92+
*
8193
* @return
8294
* TRUE if initialization is successful; FALSE if not.
8395
**/
@@ -88,8 +100,11 @@ VidFbInitializeVideo(
88100
_In_ UINT32 ScreenWidth,
89101
_In_ UINT32 ScreenHeight,
90102
_In_ UINT32 PixelsPerScanLine,
91-
_In_ UINT32 BitsPerPixel)
103+
_In_ UINT32 BitsPerPixel,
104+
_In_opt_ PPIXEL_BITMASK PixelMasks)
92105
{
106+
PPIXEL_BITMASK BitMasks = &framebufInfo.PixelMasks;
107+
93108
RtlZeroMemory(&framebufInfo, sizeof(framebufInfo));
94109

95110
framebufInfo.BaseAddress = BaseAddress;
@@ -110,8 +125,66 @@ VidFbInitializeVideo(
110125
return FALSE;
111126
}
112127

128+
//ASSERT((BitsPerPixel <= 8 && !PixelMasks) || (BitsPerPixel > 8));
129+
if (BitsPerPixel > 8)
130+
{
131+
if (!PixelMasks ||
132+
(PixelMasks->RedMask == 0 &&
133+
PixelMasks->GreenMask == 0 &&
134+
PixelMasks->BlueMask == 0 /* &&
135+
PixelMasks->ReservedMask == 0 */))
136+
{
137+
/* Determine pixel mask given color depth and color channel */
138+
switch (BitsPerPixel)
139+
{
140+
case 32:
141+
case 24: /* 8:8:8 */
142+
BitMasks->RedMask = 0x00FF0000; // 0x00FF0000;
143+
BitMasks->GreenMask = 0x0000FF00; // 0x00FF0000 >> 8;
144+
BitMasks->BlueMask = 0x000000FF; // 0x00FF0000 >> 16;
145+
BitMasks->ReservedMask = ((1 << (BitsPerPixel - 24)) - 1) << 24;
146+
break;
147+
case 16: /* 5:6:5 */
148+
BitMasks->RedMask = 0xF800; // 0xF800;
149+
BitMasks->GreenMask = 0x07E0; // (0xF800 >> 5) | 0x20;
150+
BitMasks->BlueMask = 0x001F; // 0xF800 >> 11;
151+
BitMasks->ReservedMask = 0;
152+
break;
153+
case 15: /* 5:5:5 */
154+
BitMasks->RedMask = 0x7C00; // 0x7C00;
155+
BitMasks->GreenMask = 0x03E0; // 0x7C00 >> 5;
156+
BitMasks->BlueMask = 0x001F; // 0x7C00 >> 10;
157+
BitMasks->ReservedMask = 0x8000;
158+
break;
159+
default:
160+
/* Unsupported BPP */
161+
UNIMPLEMENTED;
162+
RtlZeroMemory(BitMasks, sizeof(*BitMasks));
163+
}
164+
}
165+
else
166+
{
167+
/* Copy the pixel masks */
168+
RtlCopyMemory(BitMasks, PixelMasks, sizeof(*BitMasks));
169+
}
170+
}
171+
else
172+
{
173+
/* Palettized modes don't use masks */
174+
RtlZeroMemory(BitMasks, sizeof(*BitMasks));
175+
}
176+
113177
#if DBG
114178
VidFbPrintFramebufferInfo();
179+
{
180+
ULONG BppFromMasks =
181+
PixelBitmasksToBpp(BitMasks->RedMask,
182+
BitMasks->GreenMask,
183+
BitMasks->BlueMask,
184+
BitMasks->ReservedMask);
185+
TRACE("BitsPerPixel = %lu , BppFromMasks = %lu\n", BitsPerPixel, BppFromMasks);
186+
//ASSERT(BitsPerPixel == BppFromMasks);
187+
}
115188
#endif
116189

117190
return TRUE;

‎boot/freeldr/freeldr/arch/vidfb.h‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,58 @@
77

88
#pragma once
99

10+
#include "twidbits.h"
11+
12+
/**
13+
* @brief
14+
* Physical format of an RGB pixel, specified with per-component bit-masks.
15+
* A bit being set defines those used for the given color component, such
16+
* as Red, Green, Blue, or Reserved.
17+
*
18+
* @note
19+
* Supports up to 32 bits-per-pixel deep pixels.
20+
**/
21+
typedef struct _PIXEL_BITMASK
22+
{
23+
ULONG RedMask;
24+
ULONG GreenMask;
25+
ULONG BlueMask;
26+
ULONG ReservedMask;
27+
} PIXEL_BITMASK, *PPIXEL_BITMASK;
28+
29+
30+
/**
31+
* @brief
32+
* Calculates the number of bits per pixel ("PixelDepth") for
33+
* the given pixel format, given by the pixel color masks.
34+
*
35+
* @remark
36+
* See UEFI Spec Rev.2.10 Section 12.9 "Graphics Output Protocol":
37+
* example code "GetPixelElementSize()" function.
38+
**/
39+
FORCEINLINE
40+
ULONG
41+
PixelBitmasksToBpp(
42+
_In_ ULONG RedMask,
43+
_In_ ULONG GreenMask,
44+
_In_ ULONG BlueMask,
45+
_In_ ULONG ReservedMask)
46+
{
47+
ULONG CompoundMask = (RedMask | GreenMask | BlueMask | ReservedMask);
48+
/* Alternatively, the calculation could be done by finding the highest
49+
* bit set in the combined pixel color masks, if they are packed together. */
50+
return CountNumberOfBits(CompoundMask); // FindHighestSetBit(CompoundMask);
51+
}
52+
1053
BOOLEAN
1154
VidFbInitializeVideo(
1255
_In_ ULONG_PTR BaseAddress,
1356
_In_ ULONG BufferSize,
1457
_In_ UINT32 ScreenWidth,
1558
_In_ UINT32 ScreenHeight,
1659
_In_ UINT32 PixelsPerScanLine,
17-
_In_ UINT32 BitsPerPixel);
60+
_In_ UINT32 BitsPerPixel,
61+
_In_opt_ PPIXEL_BITMASK PixelMasks);
1862

1963
VOID
2064
VidFbClearScreenColor(

0 commit comments

Comments
 (0)