#include "SDL_internal.h"
#ifdef SDL_VIDEO_DRIVER_VITA
#include "SDL_vitavideo.h"
#include <psp2/kernel/sysmem.h>
#define SCREEN_W 960
#define SCREEN_H 544
#define ALIGN(x, a) (((x) + ((a)-1)) & ~((a)-1))
#define DISPLAY_PIXEL_FORMAT SCE_DISPLAY_PIXELFORMAT_A8B8G8R8
void *vita_gpu_alloc(unsigned int type, unsigned int size, SceUID *uid)
{
void *mem;
if (type == SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW) {
size = ALIGN(size, 256 * 1024);
} else {
size = ALIGN(size, 4 * 1024);
}
*uid = sceKernelAllocMemBlock("gpu_mem", type, size, NULL);
if (*uid < 0) {
return NULL;
}
if (sceKernelGetMemBlockBase(*uid, &mem) < 0) {
return NULL;
}
return mem;
}
void vita_gpu_free(SceUID uid)
{
void *mem = NULL;
if (sceKernelGetMemBlockBase(uid, &mem) < 0) {
return;
}
sceKernelFreeMemBlock(uid);
}
bool VITA_CreateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, SDL_PixelFormat *format, void **pixels, int *pitch)
{
SDL_WindowData *data = window->internal;
SceDisplayFrameBuf framebuf;
*format = SDL_PIXELFORMAT_ABGR8888;
*pitch = SCREEN_W * 4;
data->buffer = vita_gpu_alloc(
SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW,
4 * SCREEN_W * SCREEN_H,
&data->buffer_uid);
SDL_memset(data->buffer, 0x0, SCREEN_W * SCREEN_H * 4);
SDL_zero(framebuf);
framebuf.size = sizeof(SceDisplayFrameBuf);
framebuf.base = data->buffer;
framebuf.pitch = SCREEN_W;
framebuf.pixelformat = DISPLAY_PIXEL_FORMAT;
framebuf.width = SCREEN_W;
framebuf.height = SCREEN_H;
sceDisplaySetFrameBuf(&framebuf, SCE_DISPLAY_SETBUF_NEXTFRAME);
*pixels = data->buffer;
return true;
}
bool VITA_UpdateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, const SDL_Rect *rects, int numrects)
{
return true;
}
void VITA_DestroyWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window)
{
SDL_WindowData *data = window->internal;
if (!data) {
return;
}
vita_gpu_free(data->buffer_uid);
data->buffer = NULL;
return;
}
#endif