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#ifndef SDL_sysrender_h_
24#define SDL_sysrender_h_
25
26#include "SDL_render.h"
27#include "SDL_events.h"
28#include "SDL_mutex.h"
29#include "SDL_yuv_sw_c.h"
30
31/* The SDL 2D rendering system */
32
33typedef struct SDL_RenderDriver SDL_RenderDriver;
34
35/* Define the SDL texture structure */
36struct SDL_Texture
37{
38 const void *magic;
39 Uint32 format; /**< The pixel format of the texture */
40 int access; /**< SDL_TextureAccess */
41 int w; /**< The width of the texture */
42 int h; /**< The height of the texture */
43 int modMode; /**< The texture modulation mode */
44 SDL_BlendMode blendMode; /**< The texture blend mode */
45 SDL_ScaleMode scaleMode; /**< The texture scale mode */
46 Uint8 r, g, b, a; /**< Texture modulation values */
47
48 SDL_Renderer *renderer;
49
50 /* Support for formats not supported directly by the renderer */
51 SDL_Texture *native;
52 SDL_SW_YUVTexture *yuv;
53 void *pixels;
54 int pitch;
55 SDL_Rect locked_rect;
56 SDL_Surface *locked_surface; /**< Locked region exposed as a SDL surface */
57
58 Uint32 last_command_generation; /* last command queue generation this texture was in. */
59
60 void *driverdata; /**< Driver specific texture representation */
61
62 SDL_Texture *prev;
63 SDL_Texture *next;
64};
65
66typedef enum
67{
68 SDL_RENDERCMD_NO_OP,
69 SDL_RENDERCMD_SETVIEWPORT,
70 SDL_RENDERCMD_SETCLIPRECT,
71 SDL_RENDERCMD_SETDRAWCOLOR,
72 SDL_RENDERCMD_CLEAR,
73 SDL_RENDERCMD_DRAW_POINTS,
74 SDL_RENDERCMD_DRAW_LINES,
75 SDL_RENDERCMD_FILL_RECTS,
76 SDL_RENDERCMD_COPY,
77 SDL_RENDERCMD_COPY_EX
78} SDL_RenderCommandType;
79
80typedef struct SDL_RenderCommand
81{
82 SDL_RenderCommandType command;
83 union {
84 struct {
85 size_t first;
86 SDL_Rect rect;
87 } viewport;
88 struct {
89 SDL_bool enabled;
90 SDL_Rect rect;
91 } cliprect;
92 struct {
93 size_t first;
94 size_t count;
95 Uint8 r, g, b, a;
96 SDL_BlendMode blend;
97 SDL_Texture *texture;
98 } draw;
99 struct {
100 size_t first;
101 Uint8 r, g, b, a;
102 } color;
103 } data;
104 struct SDL_RenderCommand *next;
105} SDL_RenderCommand;
106
107
108/* Define the SDL renderer structure */
109struct SDL_Renderer
110{
111 const void *magic;
112
113 void (*WindowEvent) (SDL_Renderer * renderer, const SDL_WindowEvent *event);
114 int (*GetOutputSize) (SDL_Renderer * renderer, int *w, int *h);
115 SDL_bool (*SupportsBlendMode)(SDL_Renderer * renderer, SDL_BlendMode blendMode);
116 int (*CreateTexture) (SDL_Renderer * renderer, SDL_Texture * texture);
117 int (*QueueSetViewport) (SDL_Renderer * renderer, SDL_RenderCommand *cmd);
118 int (*QueueSetDrawColor) (SDL_Renderer * renderer, SDL_RenderCommand *cmd);
119 int (*QueueDrawPoints) (SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points,
120 int count);
121 int (*QueueDrawLines) (SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points,
122 int count);
123 int (*QueueFillRects) (SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRect * rects,
124 int count);
125 int (*QueueCopy) (SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
126 const SDL_Rect * srcrect, const SDL_FRect * dstrect);
127 int (*QueueCopyEx) (SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
128 const SDL_Rect * srcquad, const SDL_FRect * dstrect,
129 const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip);
130 int (*RunCommandQueue) (SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize);
131 int (*UpdateTexture) (SDL_Renderer * renderer, SDL_Texture * texture,
132 const SDL_Rect * rect, const void *pixels,
133 int pitch);
134#if SDL_HAVE_YUV
135 int (*UpdateTextureYUV) (SDL_Renderer * renderer, SDL_Texture * texture,
136 const SDL_Rect * rect,
137 const Uint8 *Yplane, int Ypitch,
138 const Uint8 *Uplane, int Upitch,
139 const Uint8 *Vplane, int Vpitch);
140 int (*UpdateTextureNV) (SDL_Renderer * renderer, SDL_Texture * texture,
141 const SDL_Rect * rect,
142 const Uint8 *Yplane, int Ypitch,
143 const Uint8 *UVplane, int UVpitch);
144#endif
145 int (*LockTexture) (SDL_Renderer * renderer, SDL_Texture * texture,
146 const SDL_Rect * rect, void **pixels, int *pitch);
147 void (*UnlockTexture) (SDL_Renderer * renderer, SDL_Texture * texture);
148 void (*SetTextureScaleMode) (SDL_Renderer * renderer, SDL_Texture * texture, SDL_ScaleMode scaleMode);
149 int (*SetRenderTarget) (SDL_Renderer * renderer, SDL_Texture * texture);
150 int (*RenderReadPixels) (SDL_Renderer * renderer, const SDL_Rect * rect,
151 Uint32 format, void * pixels, int pitch);
152 void (*RenderPresent) (SDL_Renderer * renderer);
153 void (*DestroyTexture) (SDL_Renderer * renderer, SDL_Texture * texture);
154
155 void (*DestroyRenderer) (SDL_Renderer * renderer);
156
157 int (*GL_BindTexture) (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh);
158 int (*GL_UnbindTexture) (SDL_Renderer * renderer, SDL_Texture *texture);
159
160 void *(*GetMetalLayer) (SDL_Renderer * renderer);
161 void *(*GetMetalCommandEncoder) (SDL_Renderer * renderer);
162
163 /* The current renderer info */
164 SDL_RendererInfo info;
165
166 /* The window associated with the renderer */
167 SDL_Window *window;
168 SDL_bool hidden;
169
170 /* The logical resolution for rendering */
171 int logical_w;
172 int logical_h;
173 int logical_w_backup;
174 int logical_h_backup;
175
176 /* Whether or not to force the viewport to even integer intervals */
177 SDL_bool integer_scale;
178
179 /* The drawable area within the window */
180 SDL_Rect viewport;
181 SDL_Rect viewport_backup;
182
183 /* The clip rectangle within the window */
184 SDL_Rect clip_rect;
185 SDL_Rect clip_rect_backup;
186
187 /* Wether or not the clipping rectangle is used. */
188 SDL_bool clipping_enabled;
189 SDL_bool clipping_enabled_backup;
190
191 /* The render output coordinate scale */
192 SDL_FPoint scale;
193 SDL_FPoint scale_backup;
194
195 /* The pixel to point coordinate scale */
196 SDL_FPoint dpi_scale;
197
198 /* Whether or not to scale relative mouse motion */
199 SDL_bool relative_scaling;
200
201 /* Remainder from scaled relative motion */
202 float xrel;
203 float yrel;
204
205 /* The list of textures */
206 SDL_Texture *textures;
207 SDL_Texture *target;
208 SDL_mutex *target_mutex;
209
210 Uint8 r, g, b, a; /**< Color for drawing operations values */
211 SDL_BlendMode blendMode; /**< The drawing blend mode */
212
213 SDL_bool always_batch;
214 SDL_bool batching;
215 SDL_RenderCommand *render_commands;
216 SDL_RenderCommand *render_commands_tail;
217 SDL_RenderCommand *render_commands_pool;
218 Uint32 render_command_generation;
219 Uint32 last_queued_color;
220 SDL_Rect last_queued_viewport;
221 SDL_Rect last_queued_cliprect;
222 SDL_bool last_queued_cliprect_enabled;
223 SDL_bool color_queued;
224 SDL_bool viewport_queued;
225 SDL_bool cliprect_queued;
226
227 void *vertex_data;
228 size_t vertex_data_used;
229 size_t vertex_data_allocation;
230
231 void *driverdata;
232};
233
234/* Define the SDL render driver structure */
235struct SDL_RenderDriver
236{
237 SDL_Renderer *(*CreateRenderer) (SDL_Window * window, Uint32 flags);
238
239 /* Info about the renderer capabilities */
240 SDL_RendererInfo info;
241};
242
243/* Not all of these are available in a given build. Use #ifdefs, etc. */
244extern SDL_RenderDriver D3D_RenderDriver;
245extern SDL_RenderDriver D3D11_RenderDriver;
246extern SDL_RenderDriver GL_RenderDriver;
247extern SDL_RenderDriver GLES2_RenderDriver;
248extern SDL_RenderDriver GLES_RenderDriver;
249extern SDL_RenderDriver DirectFB_RenderDriver;
250extern SDL_RenderDriver METAL_RenderDriver;
251extern SDL_RenderDriver PSP_RenderDriver;
252extern SDL_RenderDriver SW_RenderDriver;
253extern SDL_RenderDriver VITA_GLES2_RenderDriver;
254extern SDL_RenderDriver VITA_GXM_RenderDriver;
255
256/* Blend mode functions */
257extern SDL_BlendFactor SDL_GetBlendModeSrcColorFactor(SDL_BlendMode blendMode);
258extern SDL_BlendFactor SDL_GetBlendModeDstColorFactor(SDL_BlendMode blendMode);
259extern SDL_BlendOperation SDL_GetBlendModeColorOperation(SDL_BlendMode blendMode);
260extern SDL_BlendFactor SDL_GetBlendModeSrcAlphaFactor(SDL_BlendMode blendMode);
261extern SDL_BlendFactor SDL_GetBlendModeDstAlphaFactor(SDL_BlendMode blendMode);
262extern SDL_BlendOperation SDL_GetBlendModeAlphaOperation(SDL_BlendMode blendMode);
263
264/* drivers call this during their Queue*() methods to make space in a array that are used
265 for a vertex buffer during RunCommandQueue(). Pointers returned here are only valid until
266 the next call, because it might be in an array that gets realloc()'d. */
267extern void *SDL_AllocateRenderVertices(SDL_Renderer *renderer, const size_t numbytes, const size_t alignment, size_t *offset);
268
269extern int SDL_PrivateLowerBlitScaled(SDL_Surface * src, SDL_Rect * srcrect, SDL_Surface * dst, SDL_Rect * dstrect, SDL_ScaleMode scaleMode);
270extern int SDL_PrivateUpperBlitScaled(SDL_Surface * src, const SDL_Rect * srcrect, SDL_Surface * dst, SDL_Rect * dstrect, SDL_ScaleMode scaleMode);
271
272#endif /* SDL_sysrender_h_ */
273
274/* vi: set ts=4 sw=4 expandtab: */
275