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