AutobootModule/source/DrawUtils.cpp

332 lines
9.7 KiB
C++
Raw Permalink Normal View History

2021-12-28 20:17:21 +01:00
#include "DrawUtils.h"
#include <malloc.h>
2021-12-28 20:17:21 +01:00
#include <cmath>
2022-02-02 19:57:14 +01:00
#include <coreinit/cache.h>
2021-12-28 20:17:21 +01:00
#include <coreinit/memory.h>
#include <coreinit/screen.h>
#include <ft2build.h>
2022-02-02 19:57:14 +01:00
#include <png.h>
2021-12-28 20:17:21 +01:00
#include FT_FREETYPE_H
#include "MenuUtils.h"
2021-12-28 20:17:21 +01:00
// buffer width
#define TV_WIDTH 0x500
#define DRC_WIDTH 0x380
bool DrawUtils::isBackBuffer;
2022-01-22 14:44:39 +01:00
uint8_t *DrawUtils::tvBuffer = nullptr;
2021-12-28 20:17:21 +01:00
uint32_t DrawUtils::tvSize = 0;
2022-01-22 14:44:39 +01:00
uint8_t *DrawUtils::drcBuffer = nullptr;
2021-12-28 20:17:21 +01:00
uint32_t DrawUtils::drcSize = 0;
// Don't put those into the class or we have to include ft everywhere
static FT_Library ft_lib = nullptr;
static FT_Face ft_face = nullptr;
static Color font_col = {0xFFFFFFFF};
2022-01-22 14:44:39 +01:00
void *DrawUtils::InitOSScreen() {
OSScreenInit();
uint32_t tvBufferSize = OSScreenGetBufferSizeEx(SCREEN_TV);
uint32_t drcBufferSize = OSScreenGetBufferSizeEx(SCREEN_DRC);
auto *screenBuffer = (uint8_t *) memalign(0x100, tvBufferSize + drcBufferSize);
if (screenBuffer == nullptr) {
return nullptr;
}
OSScreenSetBufferEx(SCREEN_TV, screenBuffer);
OSScreenSetBufferEx(SCREEN_DRC, screenBuffer + tvBufferSize);
OSScreenEnableEx(SCREEN_TV, TRUE);
OSScreenEnableEx(SCREEN_DRC, TRUE);
return screenBuffer;
}
2022-01-22 14:44:39 +01:00
void DrawUtils::initBuffers(void *tvBuffer, uint32_t tvSize, void *drcBuffer, uint32_t drcSize) {
DrawUtils::tvBuffer = (uint8_t *) tvBuffer;
2021-12-28 20:17:21 +01:00
DrawUtils::tvSize = tvSize;
2022-01-22 14:44:39 +01:00
DrawUtils::drcBuffer = (uint8_t *) drcBuffer;
2021-12-28 20:17:21 +01:00
DrawUtils::drcSize = drcSize;
}
2022-01-22 14:44:39 +01:00
void DrawUtils::beginDraw() {
uint32_t pixel = *(uint32_t *) tvBuffer;
2021-12-28 20:17:21 +01:00
// check which buffer is currently used
OSScreenPutPixelEx(SCREEN_TV, 0, 0, 0xABCDEF90);
2022-01-22 14:44:39 +01:00
if (*(uint32_t *) tvBuffer == 0xABCDEF90) {
2021-12-28 20:17:21 +01:00
isBackBuffer = false;
} else {
isBackBuffer = true;
}
// restore the pixel we used for checking
2022-01-22 14:44:39 +01:00
*(uint32_t *) tvBuffer = pixel;
2021-12-28 20:17:21 +01:00
}
2022-01-22 14:44:39 +01:00
void DrawUtils::endDraw() {
2021-12-28 20:17:21 +01:00
// OSScreenFlipBuffersEx already flushes the cache?
// DCFlushRange(tvBuffer, tvSize);
// DCFlushRange(drcBuffer, drcSize);
OSScreenFlipBuffersEx(SCREEN_DRC);
OSScreenFlipBuffersEx(SCREEN_TV);
}
2022-01-22 14:44:39 +01:00
void DrawUtils::clear(Color col) {
2021-12-28 20:17:21 +01:00
OSScreenClearBufferEx(SCREEN_TV, col.color);
OSScreenClearBufferEx(SCREEN_DRC, col.color);
}
2022-01-22 14:44:39 +01:00
void DrawUtils::drawPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
2021-12-28 20:17:21 +01:00
float opacity = a / 255.0f;
// put pixel in the drc buffer
uint32_t i = (x + y * DRC_WIDTH) * 4;
if (i + 3 < drcSize / 2) {
if (isBackBuffer) {
i += drcSize / 2;
}
if (a == 0xFF) {
2022-01-22 14:44:39 +01:00
drcBuffer[i] = r;
2021-12-28 20:17:21 +01:00
drcBuffer[i + 1] = g;
drcBuffer[i + 2] = b;
2022-01-22 14:44:39 +01:00
} else {
drcBuffer[i] = r * opacity + drcBuffer[i] * (1 - opacity);
2021-12-28 20:17:21 +01:00
drcBuffer[i + 1] = g * opacity + drcBuffer[i + 1] * (1 - opacity);
drcBuffer[i + 2] = b * opacity + drcBuffer[i + 2] * (1 - opacity);
}
}
// scale and put pixel in the tv buffer
for (uint32_t yy = (y * 1.5); yy < ((y * 1.5) + 1); yy++) {
2022-01-22 14:44:39 +01:00
for (uint32_t xx = (x * 1.5); xx < ((x * 1.5) + 1); xx++) {
2021-12-28 20:17:21 +01:00
uint32_t i = (xx + yy * TV_WIDTH) * 4;
if (i + 3 < tvSize / 2) {
if (isBackBuffer) {
i += tvSize / 2;
}
if (a == 0xFF) {
2022-01-22 14:44:39 +01:00
tvBuffer[i] = r;
2021-12-28 20:17:21 +01:00
tvBuffer[i + 1] = g;
tvBuffer[i + 2] = b;
2022-01-22 14:44:39 +01:00
} else {
tvBuffer[i] = r * opacity + tvBuffer[i] * (1 - opacity);
2021-12-28 20:17:21 +01:00
tvBuffer[i + 1] = g * opacity + tvBuffer[i + 1] * (1 - opacity);
tvBuffer[i + 2] = b * opacity + tvBuffer[i + 2] * (1 - opacity);
}
}
2022-01-22 14:44:39 +01:00
}
}
2021-12-28 20:17:21 +01:00
}
2022-01-22 14:44:39 +01:00
void DrawUtils::drawRectFilled(uint32_t x, uint32_t y, uint32_t w, uint32_t h, Color col) {
2021-12-28 20:17:21 +01:00
for (uint32_t yy = y; yy < y + h; yy++) {
for (uint32_t xx = x; xx < x + w; xx++) {
drawPixel(xx, yy, col);
}
}
}
2022-01-22 14:44:39 +01:00
void DrawUtils::drawRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t borderSize, Color col) {
2021-12-28 20:17:21 +01:00
drawRectFilled(x, y, w, borderSize, col);
drawRectFilled(x, y + h - borderSize, w, borderSize, col);
drawRectFilled(x, y, borderSize, h, col);
drawRectFilled(x + w - borderSize, y, borderSize, h, col);
}
2022-01-22 14:44:39 +01:00
void DrawUtils::drawBitmap(uint32_t x, uint32_t y, uint32_t target_width, uint32_t target_height, const uint8_t *data) {
if (data[0] != 'B' || data[1] != 'M') {
// invalid header
return;
}
2021-12-28 20:17:21 +01:00
2022-01-22 14:44:39 +01:00
uint32_t dataPos = __builtin_bswap32(*(uint32_t *) &(data[0x0A]));
uint32_t width = __builtin_bswap32(*(uint32_t *) &(data[0x12]));
uint32_t height = __builtin_bswap32(*(uint32_t *) &(data[0x16]));
2021-12-28 20:17:21 +01:00
2022-01-22 14:44:39 +01:00
if (dataPos == 0) {
dataPos = 54;
}
2021-12-28 20:17:21 +01:00
2022-01-22 14:44:39 +01:00
data += dataPos;
2021-12-28 20:17:21 +01:00
// TODO flip image since bitmaps are stored upside down
for (uint32_t yy = y; yy < y + target_height; yy++) {
for (uint32_t xx = x; xx < x + target_width; xx++) {
uint32_t i = (((xx - x) * width / target_width) + ((yy - y) * height / target_height) * width) * 3;
drawPixel(xx, yy, data[i + 2], data[i + 1], data[i], 0xFF);
}
}
}
2022-01-22 14:44:39 +01:00
static void png_read_data(png_structp png_ptr, png_bytep outBytes, png_size_t byteCountToRead) {
void **data = (void **) png_get_io_ptr(png_ptr);
2021-12-28 20:17:21 +01:00
memcpy(outBytes, *data, byteCountToRead);
2022-01-22 14:44:39 +01:00
*((uint8_t **) data) += byteCountToRead;
2021-12-28 20:17:21 +01:00
}
2022-01-22 14:44:39 +01:00
void DrawUtils::drawPNG(uint32_t x, uint32_t y, const uint8_t *data) {
2021-12-28 20:17:21 +01:00
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2022-01-22 14:44:39 +01:00
if (png_ptr == NULL) {
2021-12-28 20:17:21 +01:00
return;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
2022-01-22 14:44:39 +01:00
if (info_ptr == NULL) {
2021-12-28 20:17:21 +01:00
png_destroy_read_struct(&png_ptr, NULL, NULL);
return;
}
2022-01-22 14:44:39 +01:00
png_set_read_fn(png_ptr, (void *) &data, png_read_data);
2021-12-28 20:17:21 +01:00
png_read_info(png_ptr, info_ptr);
uint32_t width = 0;
uint32_t height = 0;
int bitDepth = 0;
int colorType = -1;
uint32_t retval = png_get_IHDR(png_ptr, info_ptr, &width, &height, &bitDepth, &colorType, NULL, NULL, NULL);
2022-01-22 14:44:39 +01:00
if (retval != 1) {
2021-12-28 20:17:21 +01:00
return;
}
uint32_t bytesPerRow = png_get_rowbytes(png_ptr, info_ptr);
2022-01-22 14:44:39 +01:00
uint8_t *rowData = new uint8_t[bytesPerRow];
2021-12-28 20:17:21 +01:00
2022-01-22 14:44:39 +01:00
for (uint32_t yy = y; yy < y + height; yy++) {
png_read_row(png_ptr, (png_bytep) rowData, NULL);
2021-12-28 20:17:21 +01:00
for (uint32_t xx = x; xx < x + width; xx++) {
if (colorType == PNG_COLOR_TYPE_RGB_ALPHA) {
uint32_t i = (xx - x) * 4;
drawPixel(xx, yy, rowData[i], rowData[i + 1], rowData[i + 2], rowData[i + 3]);
2022-01-22 14:44:39 +01:00
} else if (colorType == PNG_COLOR_TYPE_RGB) {
2021-12-28 20:17:21 +01:00
uint32_t i = (xx - x) * 3;
drawPixel(xx, yy, rowData[i], rowData[i + 1], rowData[i + 2], 0xFF);
}
}
}
delete[] rowData;
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
}
2022-01-22 14:44:39 +01:00
void DrawUtils::initFont() {
void *font = NULL;
2021-12-28 20:17:21 +01:00
uint32_t size = 0;
OSGetSharedData(OS_SHAREDDATATYPE_FONT_STANDARD, 0, &font, &size);
if (font && size) {
FT_Init_FreeType(&ft_lib);
2022-01-22 14:44:39 +01:00
FT_New_Memory_Face(ft_lib, (FT_Byte *) font, size, 0, &ft_face);
2021-12-28 20:17:21 +01:00
}
}
2022-01-22 14:44:39 +01:00
void DrawUtils::deinitFont() {
2021-12-28 20:17:21 +01:00
FT_Done_Face(ft_face);
FT_Done_FreeType(ft_lib);
}
2022-01-22 14:44:39 +01:00
void DrawUtils::setFontSize(uint32_t size) {
2021-12-28 20:17:21 +01:00
FT_Set_Pixel_Sizes(ft_face, 0, size);
}
2022-01-22 14:44:39 +01:00
void DrawUtils::setFontColor(Color col) {
2021-12-28 20:17:21 +01:00
font_col = col;
}
2022-01-22 14:44:39 +01:00
static void draw_freetype_bitmap(FT_Bitmap *bitmap, FT_Int x, FT_Int y) {
FT_Int i, j, p, q;
FT_Int x_max = x + bitmap->width;
FT_Int y_max = y + bitmap->rows;
2021-12-28 20:17:21 +01:00
for (i = x, p = 0; i < x_max; i++, p++) {
for (j = y, q = 0; j < y_max; j++, q++) {
if (i < 0 || j < 0 || i >= SCREEN_WIDTH || j >= SCREEN_HEIGHT) {
continue;
}
float opacity = bitmap->buffer[q * bitmap->pitch + p] / 255.0f;
DrawUtils::drawPixel(i, j, font_col.r, font_col.g, font_col.b, font_col.a * opacity);
}
}
}
2022-01-22 14:44:39 +01:00
void DrawUtils::print(uint32_t x, uint32_t y, const char *string, bool alignRight) {
wchar_t *buffer = new wchar_t[strlen(string) + 1];
2021-12-28 20:17:21 +01:00
size_t num = mbstowcs(buffer, string, strlen(string));
if (num > 0) {
buffer[num] = 0;
2022-01-22 14:44:39 +01:00
} else {
wchar_t *tmp = buffer;
2022-02-02 19:57:14 +01:00
while ((*tmp++ = *string++))
;
2021-12-28 20:17:21 +01:00
}
print(x, y, buffer, alignRight);
delete[] buffer;
}
2022-01-22 14:44:39 +01:00
void DrawUtils::print(uint32_t x, uint32_t y, const wchar_t *string, bool alignRight) {
2021-12-28 20:17:21 +01:00
FT_GlyphSlot slot = ft_face->glyph;
FT_Vector pen = {(int) x, (int) y};
if (alignRight) {
pen.x -= getTextWidth(string);
}
for (; *string; string++) {
uint32_t charcode = *string;
if (charcode == '\n') {
pen.y += ft_face->size->metrics.height >> 6;
pen.x = x;
continue;
}
FT_Load_Glyph(ft_face, FT_Get_Char_Index(ft_face, charcode), FT_LOAD_DEFAULT);
FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
draw_freetype_bitmap(&slot->bitmap, pen.x + slot->bitmap_left, pen.y - slot->bitmap_top);
pen.x += slot->advance.x >> 6;
}
}
2022-01-22 14:44:39 +01:00
uint32_t DrawUtils::getTextWidth(const char *string) {
wchar_t *buffer = new wchar_t[strlen(string) + 1];
2021-12-28 20:17:21 +01:00
size_t num = mbstowcs(buffer, string, strlen(string));
if (num > 0) {
buffer[num] = 0;
2022-01-22 14:44:39 +01:00
} else {
wchar_t *tmp = buffer;
2022-02-02 19:57:14 +01:00
while ((*tmp++ = *string++))
;
2021-12-28 20:17:21 +01:00
}
uint32_t width = getTextWidth(buffer);
delete[] buffer;
return width;
}
2022-01-22 14:44:39 +01:00
uint32_t DrawUtils::getTextWidth(const wchar_t *string) {
2021-12-28 20:17:21 +01:00
FT_GlyphSlot slot = ft_face->glyph;
uint32_t width = 0;
for (; *string; string++) {
FT_Load_Glyph(ft_face, FT_Get_Char_Index(ft_face, *string), FT_LOAD_BITMAP_METRICS_ONLY);
width += slot->advance.x >> 6;
}
return width;
}