SDL: Added SDL_SetRenderDrawColorspace() and SDL_GetRenderDrawColorspace()

From dd28ab048946b37f3ca5ac7290b80e8ecfcc8479 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 31 Jan 2024 21:46:02 -0800
Subject: [PATCH] Added SDL_SetRenderDrawColorspace() and
 SDL_GetRenderDrawColorspace()

---
 include/SDL3/SDL_render.h         | 35 ++++++++++++++++++++++++++++++-
 src/dynapi/SDL_dynapi.sym         |  2 ++
 src/dynapi/SDL_dynapi_overrides.h |  2 ++
 src/dynapi/SDL_dynapi_procs.h     |  2 ++
 src/render/SDL_render.c           | 24 +++++++++++++++++++++
 5 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/include/SDL3/SDL_render.h b/include/SDL3/SDL_render.h
index 87ac213ab8a4..53531cecc56e 100644
--- a/include/SDL3/SDL_render.h
+++ b/include/SDL3/SDL_render.h
@@ -1361,7 +1361,40 @@ extern DECLSPEC int SDLCALL SDL_SetRenderScale(SDL_Renderer *renderer, float sca
 extern DECLSPEC int SDLCALL SDL_GetRenderScale(SDL_Renderer *renderer, float *scaleX, float *scaleY);
 
 /**
- * Set the color used for drawing operations (Rect, Line and Clear).
+ * Set the colorspace used for drawing operations
+ *
+ * The default colorspace for drawing operations is SDL_COLORSPACE_SRGB, but you can change it to other colorspaces such as SDL_COLORSPACE_SCRGB for HDR rendering.
+ *
+ * This does not affect the colorspace of textures, which is specified via properties when the texture is created and does not change.
+ *
+ * \param renderer the rendering context
+ * \param colorspace an SDL_ColorSpace value describing the colorspace for drawing operations
+ * \returns 0 on success or a negative error code on failure; call
+ *          SDL_GetError() for more information.
+ *
+ * \since This function is available since SDL 3.0.0.
+ *
+ * \sa SDL_GetRenderDrawColorspace
+ */
+extern DECLSPEC int SDLCALL SDL_SetRenderDrawColorspace(SDL_Renderer *renderer, SDL_Colorspace colorspace);
+
+/**
+ * Get the colorspace used for drawing operations
+ *
+ * \param renderer the rendering context
+ * \param colorspace a pointer filled in with an SDL_ColorSpace value describing the colorspace for drawing operations
+ * \returns 0 on success or a negative error code on failure; call
+ *          SDL_GetError() for more information.
+ *
+ * \since This function is available since SDL 3.0.0.
+ *
+ * \sa SDL_SetRenderDrawColorspace
+ */
+extern DECLSPEC int SDLCALL SDL_GetRenderDrawColorspace(SDL_Renderer *renderer, SDL_Colorspace *colorspace);
+
+
+/**
+ * Set the color used for drawing operations.
  *
  * Set the color for drawing or filling rectangles, lines, and points, and for
  * SDL_RenderClear().
diff --git a/src/dynapi/SDL_dynapi.sym b/src/dynapi/SDL_dynapi.sym
index 1dc5a5405ef2..c9d6cceebd0e 100644
--- a/src/dynapi/SDL_dynapi.sym
+++ b/src/dynapi/SDL_dynapi.sym
@@ -969,6 +969,8 @@ SDL3_0.0.0 {
     SDL_SetSurfaceColorspace;
     SDL_GetSurfaceColorspace;
     SDL_ConvertSurfaceFormatAndColorspace;
+    SDL_SetRenderDrawColorspace;
+    SDL_GetRenderDrawColorspace;
     # extra symbols go here (don't modify this line)
   local: *;
 };
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index 42a2e4846f92..51530f6701aa 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -994,3 +994,5 @@
 #define SDL_SetSurfaceColorspace SDL_SetSurfaceColorspace_REAL
 #define SDL_GetSurfaceColorspace SDL_GetSurfaceColorspace_REAL
 #define SDL_ConvertSurfaceFormatAndColorspace SDL_ConvertSurfaceFormatAndColorspace_REAL
+#define SDL_SetRenderDrawColorspace SDL_SetRenderDrawColorspace_REAL
+#define SDL_GetRenderDrawColorspace SDL_GetRenderDrawColorspace_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index b6221bb51d90..8eb2f14d9890 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -1019,3 +1019,5 @@ SDL_DYNAPI_PROC(int,SDL_ConvertPixelsAndColorspace,(int a, int b, Uint32 c, SDL_
 SDL_DYNAPI_PROC(int,SDL_SetSurfaceColorspace,(SDL_Surface *a, SDL_Colorspace b),(a,b),return)
 SDL_DYNAPI_PROC(int,SDL_GetSurfaceColorspace,(SDL_Surface *a, SDL_Colorspace *b),(a,b),return)
 SDL_DYNAPI_PROC(SDL_Surface*,SDL_ConvertSurfaceFormatAndColorspace,(SDL_Surface *a, Uint32 b, SDL_Colorspace c),(a,b,c),return)
+SDL_DYNAPI_PROC(int,SDL_SetRenderDrawColorspace,(SDL_Renderer *a, SDL_Colorspace b),(a,b),return)
+SDL_DYNAPI_PROC(int,SDL_GetRenderDrawColorspace,(SDL_Renderer *a, SDL_Colorspace *b),(a,b),return)
diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index a94bf9c5b087..4f6d48d358be 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -2757,6 +2757,30 @@ int SDL_GetRenderScale(SDL_Renderer *renderer, float *scaleX, float *scaleY)
     return 0;
 }
 
+int SDL_SetRenderDrawColorspace(SDL_Renderer *renderer, SDL_Colorspace colorspace)
+{
+    CHECK_RENDERER_MAGIC(renderer, -1);
+
+    if (colorspace != SDL_COLORSPACE_SRGB &&
+        colorspace != SDL_COLORSPACE_SCRGB) {
+        return SDL_SetError("Unsupported colorspace");
+    }
+
+    renderer->input_colorspace = colorspace;
+    return 0;
+}
+
+int SDL_GetRenderDrawColorspace(SDL_Renderer *renderer, SDL_Colorspace *colorspace)
+{
+    CHECK_RENDERER_MAGIC(renderer, -1);
+
+    if (colorspace) {
+        *colorspace = renderer->input_colorspace;
+    }
+    return 0;
+}
+
+
 int SDL_SetRenderDrawColor(SDL_Renderer *renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
 {
     const float fR = (float)r / 255.0f;