summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsframebuffer.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsframebuffer.c')
-rw-r--r--contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsframebuffer.c161
1 files changed, 161 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsframebuffer.c b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsframebuffer.c
new file mode 100644
index 0000000..d632545
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/video/n3ds/SDL_n3dsframebuffer.c
@@ -0,0 +1,161 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 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#ifdef SDL_VIDEO_DRIVER_N3DS
24
25#include "../SDL_sysvideo.h"
26#include "../../SDL_properties_c.h"
27#include "SDL_n3dsframebuffer_c.h"
28#include "SDL_n3dsvideo.h"
29
30#define N3DS_SURFACE "SDL.internal.window.surface"
31
32typedef struct
33{
34 int width, height;
35} Dimensions;
36
37static void CopyFramebuffertoN3DS_16(u16 *dest, const Dimensions dest_dim, const u16 *source, const Dimensions source_dim);
38static void CopyFramebuffertoN3DS_24(u8 *dest, const Dimensions dest_dim, const u8 *source, const Dimensions source_dim);
39static void CopyFramebuffertoN3DS_32(u32 *dest, const Dimensions dest_dim, const u32 *source, const Dimensions source_dim);
40static int GetDestOffset(int x, int y, int dest_width);
41static int GetSourceOffset(int x, int y, int source_width);
42static void FlushN3DSBuffer(const void *buffer, u32 bufsize, gfxScreen_t screen);
43
44
45bool SDL_N3DS_CreateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, SDL_PixelFormat *format, void **pixels, int *pitch)
46{
47 SDL_Surface *framebuffer;
48 const SDL_DisplayMode *mode;
49 int w, h;
50
51 SDL_N3DS_DestroyWindowFramebuffer(_this, window);
52
53 mode = SDL_GetCurrentDisplayMode(SDL_GetDisplayForWindow(window));
54 SDL_GetWindowSizeInPixels(window, &w, &h);
55 framebuffer = SDL_CreateSurface(w, h, mode->format);
56
57 if (!framebuffer) {
58 return false;
59 }
60
61 SDL_SetSurfaceProperty(SDL_GetWindowProperties(window), N3DS_SURFACE, framebuffer);
62 *format = mode->format;
63 *pixels = framebuffer->pixels;
64 *pitch = framebuffer->pitch;
65 return true;
66}
67
68bool SDL_N3DS_UpdateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, const SDL_Rect *rects, int numrects)
69{
70 SDL_WindowData *drv_data = window->internal;
71 SDL_Surface *surface;
72 u16 width, height;
73 void *framebuffer;
74 u32 bufsize;
75
76 surface = (SDL_Surface *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), N3DS_SURFACE, NULL);
77 if (!surface) {
78 return SDL_SetError("%s: Unable to get the window surface.", __func__);
79 }
80
81 // Get the N3DS internal framebuffer and its size
82 framebuffer = gfxGetFramebuffer(drv_data->screen, GFX_LEFT, &width, &height);
83 bufsize = width * height * 4;
84
85 if (SDL_BYTESPERPIXEL(surface->format) == 2)
86 CopyFramebuffertoN3DS_16(framebuffer, (Dimensions){ width, height },
87 surface->pixels, (Dimensions){ surface->w, surface->h });
88 else if (SDL_BYTESPERPIXEL(surface->format) == 3)
89 CopyFramebuffertoN3DS_24(framebuffer, (Dimensions){ width, height },
90 surface->pixels, (Dimensions){ surface->w, surface->h });
91 else
92 CopyFramebuffertoN3DS_32(framebuffer, (Dimensions){ width, height },
93 surface->pixels, (Dimensions){ surface->w, surface->h });
94 FlushN3DSBuffer(framebuffer, bufsize, drv_data->screen);
95
96 return true;
97}
98
99static void CopyFramebuffertoN3DS_16(u16 *dest, const Dimensions dest_dim, const u16 *source, const Dimensions source_dim)
100{
101 int rows = SDL_min(dest_dim.width, source_dim.height);
102 int cols = SDL_min(dest_dim.height, source_dim.width);
103 for (int y = 0; y < rows; ++y) {
104 for (int x = 0; x < cols; ++x) {
105 const u16 *s = source + GetSourceOffset(x, y, source_dim.width);
106 u16 *d = dest + GetDestOffset(x, y, dest_dim.width);
107 *d = *s;
108 }
109 }
110}
111
112static void CopyFramebuffertoN3DS_24(u8 *dest, const Dimensions dest_dim, const u8 *source, const Dimensions source_dim)
113{
114 int rows = SDL_min(dest_dim.width, source_dim.height);
115 int cols = SDL_min(dest_dim.height, source_dim.width);
116 for (int y = 0; y < rows; ++y) {
117 for (int x = 0; x < cols; ++x) {
118 const u8 *s = source + GetSourceOffset(x, y, source_dim.width) * 3;
119 u8 *d = dest + GetDestOffset(x, y, dest_dim.width) * 3;
120 d[0] = s[0];
121 d[1] = s[1];
122 d[2] = s[2];
123 }
124 }
125}
126
127static void CopyFramebuffertoN3DS_32(u32 *dest, const Dimensions dest_dim, const u32 *source, const Dimensions source_dim)
128{
129 int rows = SDL_min(dest_dim.width, source_dim.height);
130 int cols = SDL_min(dest_dim.height, source_dim.width);
131 for (int y = 0; y < rows; ++y) {
132 for (int x = 0; x < cols; ++x) {
133 const u32 *s = source + GetSourceOffset(x, y, source_dim.width);
134 u32 *d = dest + GetDestOffset(x, y, dest_dim.width);
135 *d = *s;
136 }
137 }
138}
139
140static int GetDestOffset(int x, int y, int dest_width)
141{
142 return dest_width - y - 1 + dest_width * x;
143}
144
145static int GetSourceOffset(int x, int y, int source_width)
146{
147 return x + y * source_width;
148}
149
150static void FlushN3DSBuffer(const void *buffer, u32 bufsize, gfxScreen_t screen)
151{
152 GSPGPU_FlushDataCache(buffer, bufsize);
153 gfxScreenSwapBuffers(screen, false);
154}
155
156void SDL_N3DS_DestroyWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window)
157{
158 SDL_ClearProperty(SDL_GetWindowProperties(window), N3DS_SURFACE);
159}
160
161#endif // SDL_VIDEO_DRIVER_N3DS