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 && SDL_VIDEO_VITA_PVR
24 #include <stdlib.h>
25 #include <string.h>
26 #include <psp2/kernel/modulemgr.h>
27 #include <gpu_es4/psp2_pvr_hint.h>
28 
29 #include "SDL_error.h"
30 #include "SDL_log.h"
31 #include "SDL_vitavideo.h"
32 #include "../SDL_egl_c.h"
33 #include "SDL_vitagl_pvr_c.h"
34 
35 #define MAX_PATH 256 // vita limits are somehow wrong
36 
37 int
VITA_GL_LoadLibrary(_THIS,const char * path)38 VITA_GL_LoadLibrary(_THIS, const char *path)
39 {
40     PVRSRV_PSP2_APPHINT hint;
41     char* override = SDL_getenv("VITA_MODULE_PATH");
42     char* skip_init = SDL_getenv("VITA_PVR_SKIP_INIT");
43     char* default_path = "app0:module";
44     char target_path[MAX_PATH];
45 
46     if (skip_init == NULL) // we don't care about actual value
47     {
48         if (override != NULL)
49         {
50           default_path = override;
51         }
52 
53         sceKernelLoadStartModule("vs0:sys/external/libfios2.suprx", 0, NULL, 0, NULL, NULL);
54         sceKernelLoadStartModule("vs0:sys/external/libc.suprx", 0, NULL, 0, NULL, NULL);
55 
56         SDL_snprintf(target_path, MAX_PATH, "%s/%s", default_path, "libgpu_es4_ext.suprx");
57         sceKernelLoadStartModule(target_path, 0, NULL, 0, NULL, NULL);
58 
59         SDL_snprintf(target_path, MAX_PATH, "%s/%s", default_path, "libIMGEGL.suprx");
60         sceKernelLoadStartModule(target_path, 0, NULL, 0, NULL, NULL);
61 
62         PVRSRVInitializeAppHint(&hint);
63 
64         SDL_snprintf(hint.szGLES1, MAX_PATH, "%s/%s", default_path, "libGLESv1_CM.suprx");
65         SDL_snprintf(hint.szGLES2, MAX_PATH, "%s/%s", default_path, "libGLESv2.suprx");
66         SDL_snprintf(hint.szWindowSystem, MAX_PATH, "%s/%s", default_path, "libpvrPSP2_WSEGL.suprx");
67 
68         PVRSRVCreateVirtualAppHint(&hint);
69     }
70 
71     return SDL_EGL_LoadLibrary(_this, path, (NativeDisplayType) 0, 0);
72 }
73 
74 SDL_GLContext
VITA_GL_CreateContext(_THIS,SDL_Window * window)75 VITA_GL_CreateContext(_THIS, SDL_Window * window)
76 {
77     return SDL_EGL_CreateContext(_this, ((SDL_WindowData *) window->driverdata)->egl_surface);
78 }
79 
80 int
VITA_GL_MakeCurrent(_THIS,SDL_Window * window,SDL_GLContext context)81 VITA_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
82 {
83     if (window && context) {
84         return SDL_EGL_MakeCurrent(_this, ((SDL_WindowData *) window->driverdata)->egl_surface, context);
85     } else {
86         return SDL_EGL_MakeCurrent(_this, NULL, NULL);
87     }
88 }
89 
90 int
VITA_GL_SwapWindow(_THIS,SDL_Window * window)91 VITA_GL_SwapWindow(_THIS, SDL_Window * window)
92 {
93     SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
94     if (videodata->ime_active) {
95         sceImeUpdate();
96     }
97     return SDL_EGL_SwapBuffers(_this, ((SDL_WindowData *) window->driverdata)->egl_surface);
98 }
99 
100 
101 #endif /* SDL_VIDEO_DRIVER_VITA && SDL_VIDEO_VITA_PVR */
102 
103 /* vi: set ts=4 sw=4 expandtab: */
104