diff --git a/include/SDL3/SDL_surface.h b/include/SDL3/SDL_surface.h index 96eb558baf..8074e2e1b6 100644 --- a/include/SDL3/SDL_surface.h +++ b/include/SDL3/SDL_surface.h @@ -186,11 +186,6 @@ extern SDL_DECLSPEC void SDLCALL SDL_DestroySurface(SDL_Surface *surface); * * The following properties are understood by SDL: * - * - `SDL_PROP_SURFACE_COLORSPACE_NUMBER`: an SDL_ColorSpace value describing - * the surface colorspace, defaults to SDL_COLORSPACE_SRGB_LINEAR for - * floating point formats, SDL_COLORSPACE_HDR10 for 10-bit formats, - * SDL_COLORSPACE_SRGB for other RGB surfaces and SDL_COLORSPACE_BT709_FULL - * for YUV surfaces. * - `SDL_PROP_SURFACE_SDR_WHITE_POINT_FLOAT`: for HDR10 and floating point * surfaces, this defines the value of 100% diffuse white, with higher * values being displayed in the High Dynamic Range headroom. This defaults @@ -214,7 +209,6 @@ extern SDL_DECLSPEC void SDLCALL SDL_DestroySurface(SDL_Surface *surface); */ extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_GetSurfaceProperties(SDL_Surface *surface); -#define SDL_PROP_SURFACE_COLORSPACE_NUMBER "SDL.surface.colorspace" #define SDL_PROP_SURFACE_SDR_WHITE_POINT_FLOAT "SDL.surface.SDR_white_point" #define SDL_PROP_SURFACE_HDR_HEADROOM_FLOAT "SDL.surface.HDR_headroom" #define SDL_PROP_SURFACE_TONEMAP_OPERATOR_STRING "SDL.surface.tonemap" diff --git a/src/video/SDL_blit.c b/src/video/SDL_blit.c index 5965bf3483..2972bc823b 100644 --- a/src/video/SDL_blit.c +++ b/src/video/SDL_blit.c @@ -181,15 +181,8 @@ int SDL_CalculateBlit(SDL_Surface *surface) SDL_BlitFunc blit = NULL; SDL_BlitMap *map = &surface->internal->map; SDL_Surface *dst = map->dst; - SDL_Colorspace src_colorspace = SDL_GetSurfaceColorspace(surface); - SDL_Colorspace dst_colorspace = SDL_GetSurfaceColorspace(dst); - - if (src_colorspace == SDL_COLORSPACE_UNKNOWN) { - return -1; - } - if (dst_colorspace == SDL_COLORSPACE_UNKNOWN) { - return -1; - } + SDL_Colorspace src_colorspace = surface->internal->colorspace; + SDL_Colorspace dst_colorspace = dst->internal->colorspace; /* We don't currently support blitting to < 8 bpp surfaces */ if (SDL_BITSPERPIXEL(dst->format) < 8) { diff --git a/src/video/SDL_blit_slow.c b/src/video/SDL_blit_slow.c index 365b4cd52b..68b693cd8b 100644 --- a/src/video/SDL_blit_slow.c +++ b/src/video/SDL_blit_slow.c @@ -837,12 +837,8 @@ void SDL_Blit_Slow_Float(SDL_BlitInfo *info) float src_headroom; SDL_TonemapContext tonemap; - src_colorspace = SDL_GetSurfaceColorspace(info->src_surface); - dst_colorspace = SDL_GetSurfaceColorspace(info->dst_surface); - if (src_colorspace == SDL_COLORSPACE_UNKNOWN || - dst_colorspace == SDL_COLORSPACE_UNKNOWN) { - return; - } + src_colorspace = info->src_surface->internal->colorspace; + dst_colorspace = info->dst_surface->internal->colorspace; src_primaries = SDL_COLORSPACEPRIMARIES(src_colorspace); dst_primaries = SDL_COLORSPACEPRIMARIES(dst_colorspace); diff --git a/src/video/SDL_stretch.c b/src/video/SDL_stretch.c index 88e0f49e40..6c37f064f1 100644 --- a/src/video/SDL_stretch.c +++ b/src/video/SDL_stretch.c @@ -44,7 +44,7 @@ int SDL_SoftStretch(SDL_Surface *src, const SDL_Rect *srcrect, if (src->format != dst->format) { // Slow! - SDL_Surface *src_tmp = SDL_ConvertSurfaceAndColorspace(src, dst->format, dst->internal->palette, SDL_GetSurfaceColorspace(dst), dst->internal->props); + SDL_Surface *src_tmp = SDL_ConvertSurfaceAndColorspace(src, dst->format, dst->internal->palette, dst->internal->colorspace, dst->internal->props); if (!src_tmp) { return -1; } @@ -68,11 +68,10 @@ int SDL_SoftStretch(SDL_Surface *src, const SDL_Rect *srcrect, if (src_tmp && dst_tmp) { ret = SDL_SoftStretch(src_tmp, srcrect, dst_tmp, NULL, scaleMode); if (ret == 0) { - SDL_Colorspace dst_colorspace = SDL_GetSurfaceColorspace(dst); SDL_ConvertPixelsAndColorspace(dstrect->w, dstrect->h, dst_tmp->format, SDL_COLORSPACE_SRGB, 0, dst_tmp->pixels, dst_tmp->pitch, - dst->format, dst_colorspace, SDL_GetSurfaceProperties(dst), + dst->format, dst->internal->colorspace, SDL_GetSurfaceProperties(dst), (Uint8 *)dst->pixels + dstrect->y * dst->pitch + dstrect->x * SDL_BYTESPERPIXEL(dst->format), dst->pitch); } } else { diff --git a/src/video/SDL_surface.c b/src/video/SDL_surface.c index 4e6996ecf3..f56242cec7 100644 --- a/src/video/SDL_surface.c +++ b/src/video/SDL_surface.c @@ -158,9 +158,10 @@ static SDL_Surface *SDL_InitializeSurface(SDL_InternalSurface *mem, int width, i surface->internal->map.info.b = 0xFF; surface->internal->map.info.a = 0xFF; - if (colorspace != SDL_COLORSPACE_UNKNOWN && - colorspace != SDL_GetDefaultColorspaceForFormat(format)) { - SDL_SetSurfaceColorspace(surface, colorspace); + if (colorspace == SDL_COLORSPACE_UNKNOWN) { + surface->internal->colorspace = SDL_GetDefaultColorspaceForFormat(format); + } else { + surface->internal->colorspace = colorspace; } if (props) { @@ -289,25 +290,17 @@ int SDL_SetSurfaceColorspace(SDL_Surface *surface, SDL_Colorspace colorspace) return SDL_InvalidParamError("surface"); } - if (colorspace == SDL_GetDefaultColorspaceForFormat(surface->format)) { - return 0; - } - return SDL_SetNumberProperty(SDL_GetSurfaceProperties(surface), SDL_PROP_SURFACE_COLORSPACE_NUMBER, colorspace); + surface->internal->colorspace = colorspace; + return 0; } SDL_Colorspace SDL_GetSurfaceColorspace(SDL_Surface *surface) { - SDL_Colorspace colorspace; - if (!SDL_SurfaceValid(surface)) { return SDL_COLORSPACE_UNKNOWN; } - colorspace = (SDL_Colorspace)SDL_GetNumberProperty(surface->internal->props, SDL_PROP_SURFACE_COLORSPACE_NUMBER, SDL_COLORSPACE_UNKNOWN); - if (colorspace == SDL_COLORSPACE_UNKNOWN) { - colorspace = SDL_GetDefaultColorspaceForFormat(surface->format); - } - return colorspace; + return surface->internal->colorspace; } float SDL_GetDefaultSDRWhitePoint(SDL_Colorspace colorspace) @@ -1740,7 +1733,7 @@ SDL_Surface *SDL_ConvertSurfaceAndColorspace(SDL_Surface *surface, SDL_PixelForm } } - src_colorspace = SDL_GetSurfaceColorspace(surface); + src_colorspace = surface->internal->colorspace; src_properties = surface->internal->props; /* Create a new surface with the desired format */ @@ -1968,7 +1961,7 @@ SDL_Surface *SDL_DuplicateSurface(SDL_Surface *surface) return NULL; } - return SDL_ConvertSurfaceAndColorspace(surface, surface->format, surface->internal->palette, SDL_GetSurfaceColorspace(surface), surface->internal->props); + return SDL_ConvertSurfaceAndColorspace(surface, surface->format, surface->internal->palette, surface->internal->colorspace, surface->internal->props); } SDL_Surface *SDL_ConvertSurface(SDL_Surface *surface, SDL_PixelFormat format) @@ -2311,7 +2304,7 @@ int SDL_PremultiplySurfaceAlpha(SDL_Surface *surface, SDL_bool linear) return SDL_InvalidParamError("surface"); } - colorspace = SDL_GetSurfaceColorspace(surface); + colorspace = surface->internal->colorspace; return SDL_PremultiplyAlphaPixelsAndColorspace(surface->w, surface->h, surface->format, colorspace, surface->internal->props, surface->pixels, surface->pitch, surface->format, colorspace, surface->internal->props, surface->pixels, surface->pitch, linear); } @@ -2346,7 +2339,7 @@ int SDL_ClearSurface(SDL_Surface *surface, float r, float g, float b, float a) } if (SDL_ClearSurface(tmp, r, g, b, a) == 0) { - result = SDL_ConvertPixelsAndColorspace(surface->w, surface->h, tmp->format, SDL_GetSurfaceColorspace(tmp), tmp->internal->props, tmp->pixels, tmp->pitch, surface->format, SDL_GetSurfaceColorspace(surface), surface->internal->props, surface->pixels, surface->pitch); + result = SDL_ConvertPixelsAndColorspace(surface->w, surface->h, tmp->format, tmp->internal->colorspace, tmp->internal->props, tmp->pixels, tmp->pitch, surface->format, surface->internal->colorspace, surface->internal->props, surface->pixels, surface->pitch); } SDL_DestroySurface(tmp); } else { @@ -2355,7 +2348,7 @@ int SDL_ClearSurface(SDL_Surface *surface, float r, float g, float b, float a) if (!tmp) { goto done; } - SDL_SetSurfaceColorspace(tmp, SDL_GetSurfaceColorspace(surface)); + SDL_SetSurfaceColorspace(tmp, surface->internal->colorspace); float *pixels = (float *)tmp->pixels; pixels[0] = r; @@ -2462,9 +2455,8 @@ int SDL_ReadSurfacePixel(SDL_Surface *surface, int x, int y, Uint8 *r, Uint8 *g, } else { /* This is really slow, but it gets the job done */ Uint8 rgba[4]; - SDL_Colorspace colorspace = SDL_GetSurfaceColorspace(surface); - if (SDL_ConvertPixelsAndColorspace(1, 1, surface->format, colorspace, surface->internal->props, p, surface->pitch, SDL_PIXELFORMAT_RGBA32, SDL_COLORSPACE_SRGB, 0, rgba, sizeof(rgba)) == 0) { + if (SDL_ConvertPixelsAndColorspace(1, 1, surface->format, surface->internal->colorspace, surface->internal->props, p, surface->pitch, SDL_PIXELFORMAT_RGBA32, SDL_COLORSPACE_SRGB, 0, rgba, sizeof(rgba)) == 0) { *r = rgba[0]; *g = rgba[1]; *b = rgba[2]; @@ -2554,7 +2546,7 @@ int SDL_ReadSurfacePixelFloat(SDL_Surface *surface, int x, int y, float *r, floa SDL_memcpy(rgba, p, sizeof(rgba)); result = 0; } else { - SDL_Colorspace src_colorspace = SDL_GetSurfaceColorspace(surface); + SDL_Colorspace src_colorspace = surface->internal->colorspace; SDL_Colorspace dst_colorspace = (src_colorspace == SDL_COLORSPACE_SRGB_LINEAR ? SDL_COLORSPACE_SRGB_LINEAR : SDL_COLORSPACE_SRGB); if (SDL_ConvertPixelsAndColorspace(1, 1, surface->format, src_colorspace, surface->internal->props, p, surface->pitch, SDL_PIXELFORMAT_RGBA128_FLOAT, dst_colorspace, 0, rgba, sizeof(rgba)) == 0) { @@ -2618,13 +2610,12 @@ int SDL_WriteSurfacePixel(SDL_Surface *surface, int x, int y, Uint8 r, Uint8 g, } else { /* This is really slow, but it gets the job done */ Uint8 rgba[4]; - SDL_Colorspace colorspace = SDL_GetSurfaceColorspace(surface); rgba[0] = r; rgba[1] = g; rgba[2] = b; rgba[3] = a; - result = SDL_ConvertPixelsAndColorspace(1, 1, SDL_PIXELFORMAT_RGBA32, SDL_COLORSPACE_SRGB, 0, rgba, sizeof(rgba), surface->format, colorspace, surface->internal->props, p, surface->pitch); + result = SDL_ConvertPixelsAndColorspace(1, 1, SDL_PIXELFORMAT_RGBA32, SDL_COLORSPACE_SRGB, 0, rgba, sizeof(rgba), surface->format, surface->internal->colorspace, surface->internal->props, p, surface->pitch); } if (SDL_MUSTLOCK(surface)) { @@ -2683,7 +2674,7 @@ int SDL_WriteSurfacePixelFloat(SDL_Surface *surface, int x, int y, float r, floa SDL_memcpy(p, rgba, sizeof(rgba)); result = 0; } else { - SDL_Colorspace dst_colorspace = SDL_GetSurfaceColorspace(surface); + SDL_Colorspace dst_colorspace = surface->internal->colorspace; SDL_Colorspace src_colorspace = (dst_colorspace == SDL_COLORSPACE_SRGB_LINEAR ? SDL_COLORSPACE_SRGB_LINEAR : SDL_COLORSPACE_SRGB); result = SDL_ConvertPixelsAndColorspace(1, 1, SDL_PIXELFORMAT_RGBA128_FLOAT, src_colorspace, 0, rgba, sizeof(rgba), surface->format, dst_colorspace, surface->internal->props, p, surface->pitch); diff --git a/src/video/SDL_surface_c.h b/src/video/SDL_surface_c.h index cd8420e78d..70c24c3b24 100644 --- a/src/video/SDL_surface_c.h +++ b/src/video/SDL_surface_c.h @@ -46,6 +46,9 @@ struct SDL_SurfaceData /** detailed format for this surface */ const SDL_PixelFormatDetails *format; + /** Pixel colorspace */ + SDL_Colorspace colorspace; + /** palette for indexed surfaces */ SDL_Palette *palette;