1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
4 
5   This software is provided 'as-is', without any express or implied
6   warranty.  In no event will the authors be held liable for any damages
7   arising from the use of this software.
8 
9   Permission is granted to anyone to use this software for any purpose,
10   including commercial applications, and to alter it and redistribute it
11   freely, subject to the following restrictions:
12 
13   1. The origin of this software must not be misrepresented; you must not
14      claim that you wrote the original software. If you use this software
15      in a product, an acknowledgment in the product documentation would be
16      appreciated but is not required.
17   2. Altered source versions must be plainly marked as such, and must not be
18      misrepresented as being the original software.
19   3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22 
23 #if SDL_VIDEO_DRIVER_VITA
24 
25 #include "SDL_vitavideo.h"
26 
27 #include <psp2/kernel/sysmem.h>
28 
29 #define SCREEN_W 960
30 #define SCREEN_H 544
31 #define ALIGN(x, a)     (((x) + ((a) - 1)) & ~((a) - 1))
32 #define DISPLAY_PIXEL_FORMAT SCE_DISPLAY_PIXELFORMAT_A8B8G8R8
33 
vita_gpu_alloc(SceKernelMemBlockType type,unsigned int size,SceUID * uid)34 void *vita_gpu_alloc(SceKernelMemBlockType type, unsigned int size, SceUID *uid)
35 {
36     void *mem;
37 
38     if (type == SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW) {
39         size = ALIGN(size, 256*1024);
40     } else {
41         size = ALIGN(size, 4*1024);
42     }
43 
44     *uid = sceKernelAllocMemBlock("gpu_mem", type, size, NULL);
45 
46     if (*uid < 0)
47         return NULL;
48 
49     if (sceKernelGetMemBlockBase(*uid, &mem) < 0)
50         return NULL;
51 
52     return mem;
53 }
54 
vita_gpu_free(SceUID uid)55 void vita_gpu_free(SceUID uid)
56 {
57     void *mem = NULL;
58     if (sceKernelGetMemBlockBase(uid, &mem) < 0)
59         return;
60     sceKernelFreeMemBlock(uid);
61 }
62 
VITA_CreateWindowFramebuffer(_THIS,SDL_Window * window,Uint32 * format,void ** pixels,int * pitch)63 int VITA_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
64 {
65     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
66     SceDisplayFrameBuf framebuf;
67 
68     *format = SDL_PIXELFORMAT_ABGR8888;
69     *pitch = SCREEN_W * 4;
70 
71     data->buffer = vita_gpu_alloc(
72         SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW,
73         4 * SCREEN_W * SCREEN_H,
74         &data->buffer_uid
75     );
76 
77     // SDL_memset the buffer to black
78     SDL_memset(data->buffer, 0x0, SCREEN_W*SCREEN_H*4);
79 
80     SDL_memset(&framebuf, 0x00, sizeof(SceDisplayFrameBuf));
81     framebuf.size        = sizeof(SceDisplayFrameBuf);
82     framebuf.base        = data->buffer;
83     framebuf.pitch       = SCREEN_W;
84     framebuf.pixelformat = DISPLAY_PIXEL_FORMAT;
85     framebuf.width       = SCREEN_W;
86     framebuf.height      = SCREEN_H;
87     sceDisplaySetFrameBuf(&framebuf, SCE_DISPLAY_SETBUF_NEXTFRAME);
88 
89     *pixels = data->buffer;
90 
91     return 0;
92 }
93 
VITA_UpdateWindowFramebuffer(_THIS,SDL_Window * window,const SDL_Rect * rects,int numrects)94 int VITA_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rect * rects, int numrects)
95 {
96     // do nothing
97     return 0;
98 }
99 
VITA_DestroyWindowFramebuffer(_THIS,SDL_Window * window)100 void VITA_DestroyWindowFramebuffer(_THIS, SDL_Window * window)
101 {
102     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
103 
104     if (!data) {
105         /* The window wasn't fully initialized */
106         return;
107     }
108 
109     vita_gpu_free(data->buffer_uid);
110     data->buffer = NULL;
111     return;
112 }
113 
114 #endif /* SDL_VIDEO_DRIVER_VITA */
115 
116 /* vi: set ts=4 sw=4 expandtab: */
117