enable write buffering on VRAM, optimize some UI drawing algorithms

This commit is contained in:
Wolfvak 2019-05-24 18:09:50 -03:00 committed by d0k3
parent bb5182cae3
commit 6823e15584
4 changed files with 62 additions and 65 deletions

View File

@ -25,6 +25,8 @@ static u32 font_height = 0;
static u32 line_height = 0;
static u8 font_bin[FONT_MAX_HEIGHT * 256];
#define PIXEL_OFFSET(x, y) (((x) * SCREEN_HEIGHT) + (SCREEN_HEIGHT - (y) - 1))
u8* GetFontFromPbm(const void* pbm, const u32 pbm_size, u32* w, u32* h) {
char* hdr = (char*) pbm;
@ -151,32 +153,25 @@ void ClearScreenF(bool clear_main, bool clear_alt, u32 color)
u16 GetColor(u16 *screen, int x, int y)
{
int xDisplacement = x * SCREEN_HEIGHT;
int yDisplacement = SCREEN_HEIGHT - y - 1;
return screen[xDisplacement + yDisplacement];
return screen[PIXEL_OFFSET(x, y)];
}
void DrawPixel(u16 *screen, int x, int y, int color)
void DrawPixel(u16 *screen, int x, int y, u32 color)
{
int xDisplacement = x * SCREEN_HEIGHT;
int yDisplacement = SCREEN_HEIGHT - y - 1;
screen[xDisplacement + yDisplacement] = color;
screen[PIXEL_OFFSET(x, y)] = color;
}
void DrawRectangle(u16 *screen, int x, int y, int width, int height, int color)
void DrawRectangle(u16 *screen, int x, int y, u32 width, u32 height, u32 color)
{
for (int yy = 0; yy < height; yy++) {
int xDisplacement = x * SCREEN_HEIGHT;
int yDisplacement = (SCREEN_HEIGHT - (y + yy) - 1);
u16* screenPos = screen + xDisplacement + yDisplacement;
for (int xx = width - 1; xx >= 0; xx--) {
*screenPos = color;
screenPos += SCREEN_HEIGHT;
}
screen += PIXEL_OFFSET(x, y) - height;
while(width--) {
for (u32 h = 0; h < height; h++)
screen[h] = color;
screen += SCREEN_HEIGHT;
}
}
void DrawBitmap(u16 *screen, int x, int y, int w, int h, const u8* bitmap)
void DrawBitmap(u16 *screen, int x, int y, u32 w, u32 h, const u8* bitmap)
{
// on negative values: center the bitmap
if (x < 0) x = (SCREEN_WIDTH(screen) - w) >> 1;
@ -186,15 +181,13 @@ void DrawBitmap(u16 *screen, int x, int y, int w, int h, const u8* bitmap)
if ((x < 0) || (y < 0) || (w > SCREEN_WIDTH(screen)) || (h > SCREEN_HEIGHT))
return;
for (int yy = 0; yy < h; yy++) {
int xDisplacement = x * SCREEN_HEIGHT;
int yDisplacement = SCREEN_HEIGHT - (y + yy) - 1;
u16 *screenPos = screen + xDisplacement + yDisplacement;
for (int xx = 0; xx < w; xx++) {
*(screenPos) = RGB(bitmap[2], bitmap[1], bitmap[0]);
screen += PIXEL_OFFSET(x, y);
while(h--) {
for (u32 i = 0; i < w; i++) {
screen[i * SCREEN_HEIGHT] = RGB(bitmap[2], bitmap[1], bitmap[0]);
bitmap += 3;
screenPos += SCREEN_HEIGHT;
}
screen--;
}
}
@ -231,7 +224,7 @@ void DrawQrCode(u16 *screen, const u8* qrcode)
}
}
void DrawCharacter(u16 *screen, int character, int x, int y, int color, int bgcolor)
void DrawCharacter(u16 *screen, int character, int x, int y, u32 color, u32 bgcolor)
{
for (int yy = 0; yy < (int) font_height; yy++) {
int xDisplacement = x * SCREEN_HEIGHT;
@ -250,17 +243,18 @@ void DrawCharacter(u16 *screen, int character, int x, int y, int color, int bgco
}
}
void DrawString(u16 *screen, const char *str, int x, int y, int color, int bgcolor, bool fix_utf8)
void DrawString(u16 *screen, const char *str, int x, int y, u32 color, u32 bgcolor, bool fix_utf8)
{
size_t max_len = (((screen == TOP_SCREEN) ? SCREEN_WIDTH_TOP : SCREEN_WIDTH_BOT) - x) / font_width;
size_t len = (strlen(str) > max_len) ? max_len : strlen(str);
for (size_t i = 0; i < len; i++) {
char c = (char) (fix_utf8 && str[i] >= 0x80) ? '?' : str[i];
DrawCharacter(screen, c, x + i * font_width, y, color, bgcolor);
}
}
void DrawStringF(u16 *screen, int x, int y, int color, int bgcolor, const char *format, ...)
void DrawStringF(u16 *screen, int x, int y, u32 color, u32 bgcolor, const char *format, ...)
{
char str[STRBUF_SIZE] = { 0 };
va_list va;
@ -272,7 +266,7 @@ void DrawStringF(u16 *screen, int x, int y, int color, int bgcolor, const char *
DrawString(screen, text, x, y, color, bgcolor, true);
}
void DrawStringCenter(u16 *screen, int color, int bgcolor, const char *format, ...)
void DrawStringCenter(u16 *screen, u32 color, u32 bgcolor, const char *format, ...)
{
char str[STRBUF_SIZE] = { 0 };
va_list va;

View File

@ -52,15 +52,15 @@ u16 GetColor(u16 *screen, int x, int y);
void ClearScreen(u16 *screen, u32 color);
void ClearScreenF(bool clear_main, bool clear_alt, u32 color);
void DrawPixel(u16 *screen, int x, int y, int color);
void DrawRectangle(u16 *screen, int x, int y, int width, int height, int color);
void DrawBitmap(u16 *screen, int x, int y, int w, int h, const u8* bitmap);
void DrawPixel(u16 *screen, int x, int y, u32 color);
void DrawRectangle(u16 *screen, int x, int y, u32 width, u32 height, u32 color);
void DrawBitmap(u16 *screen, int x, int y, u32 w, u32 h, const u8* bitmap);
void DrawQrCode(u16 *screen, const u8* qrcode);
void DrawCharacter(u16 *screen, int character, int x, int y, int color, int bgcolor);
void DrawString(u16 *screen, const char *str, int x, int y, int color, int bgcolor, bool fix_utf8);
void DrawStringF(u16 *screen, int x, int y, int color, int bgcolor, const char *format, ...);
void DrawStringCenter(u16 *screen, int color, int bgcolor, const char *format, ...);
void DrawCharacter(u16 *screen, int character, int x, int y, u32 color, u32 bgcolor);
void DrawString(u16 *screen, const char *str, int x, int y, u32 color, u32 bgcolor, bool fix_utf8);
void DrawStringF(u16 *screen, int x, int y, u32 color, u32 bgcolor, const char *format, ...);
void DrawStringCenter(u16 *screen, u32 color, u32 bgcolor, const char *format, ...);
u32 GetDrawStringHeight(const char* str);
u32 GetDrawStringWidth(const char* str);

View File

@ -46,9 +46,8 @@ _start:
mcr p15, 0, r0, c5, c0, 3 @ write instruction access
@ Set MPU regions and cache settings
ldr lr, =__mpu_regions
ldmia lr, {r0-r7}
mov lr, #0b00101000
ldr r0, =__mpu_regions
ldmia r0, {r0-r7}
mcr p15, 0, r0, c6, c0, 0
mcr p15, 0, r1, c6, c1, 0
mcr p15, 0, r2, c6, c2, 0
@ -57,9 +56,13 @@ _start:
mcr p15, 0, r5, c6, c5, 0
mcr p15, 0, r6, c6, c6, 0
mcr p15, 0, r7, c6, c7, 0
mcr p15, 0, lr, c3, c0, 0 @ Write bufferable
mcr p15, 0, lr, c2, c0, 0 @ Data cacheable
mcr p15, 0, lr, c2, c0, 1 @ Inst cacheable
mov r0, #0b10101000 @ enable write buffer for VRAM
mcr p15, 0, r0, c3, c0, 0 @ Write bufferable
mov r0, #0b00101000
mcr p15, 0, r0, c2, c0, 0 @ Data cacheable
mcr p15, 0, r0, c2, c0, 1 @ Inst cacheable
@ Enable DTCM
ldr r0, =0x3000800A