A second take on HDR support with an SDR white point and HDR headroom
This better reflects how HDR content is actually used, e.g. most content is in the SDR range, with specular highlights and bright details beyond the SDR range, in the HDR headroom. This more closely matches how HDR is handled on Apple platforms, as EDR. This also greatly simplifies application code which no longer has to think about color scaling. SDR content is rendered at the appropriate brightness automatically, and HDR content is scaled to the correct range for the display HDR headroom.
This commit is contained in:
@@ -370,7 +370,7 @@ static Uint16 float_to_half(float a)
|
||||
return ir;
|
||||
}
|
||||
|
||||
static void ReadFloatPixel(Uint8 *pixels, SlowBlitPixelAccess access, SDL_PixelFormat *fmt, SDL_Colorspace colorspace,
|
||||
static void ReadFloatPixel(Uint8 *pixels, SlowBlitPixelAccess access, SDL_PixelFormat *fmt, SDL_Colorspace colorspace, float SDR_white_point,
|
||||
float *outR, float *outG, float *outB, float *outA)
|
||||
{
|
||||
Uint32 pixel;
|
||||
@@ -500,20 +500,19 @@ static void ReadFloatPixel(Uint8 *pixels, SlowBlitPixelAccess access, SDL_PixelF
|
||||
/* Convert to nits so src and dst are guaranteed to be linear and in the same units */
|
||||
switch (SDL_COLORSPACETRANSFER(colorspace)) {
|
||||
case SDL_TRANSFER_CHARACTERISTICS_SRGB:
|
||||
fR = SDL_sRGBtoNits(fR);
|
||||
fG = SDL_sRGBtoNits(fG);
|
||||
fB = SDL_sRGBtoNits(fB);
|
||||
fR = SDL_sRGBtoLinear(fR);
|
||||
fG = SDL_sRGBtoLinear(fG);
|
||||
fB = SDL_sRGBtoLinear(fB);
|
||||
break;
|
||||
case SDL_TRANSFER_CHARACTERISTICS_PQ:
|
||||
fR = SDL_PQtoNits(fR);
|
||||
fG = SDL_PQtoNits(fG);
|
||||
fB = SDL_PQtoNits(fB);
|
||||
fR = SDL_PQtoNits(fR) / SDR_white_point;
|
||||
fG = SDL_PQtoNits(fG) / SDR_white_point;
|
||||
fB = SDL_PQtoNits(fB) / SDR_white_point;
|
||||
break;
|
||||
case SDL_TRANSFER_CHARACTERISTICS_LINEAR:
|
||||
/* Assuming scRGB for now */
|
||||
fR = SDL_scRGBtoNits(fR);
|
||||
fG = SDL_scRGBtoNits(fG);
|
||||
fB = SDL_scRGBtoNits(fB);
|
||||
fR /= SDR_white_point;
|
||||
fG /= SDR_white_point;
|
||||
fB /= SDR_white_point;
|
||||
break;
|
||||
default:
|
||||
/* Unknown, leave it alone */
|
||||
@@ -526,7 +525,7 @@ static void ReadFloatPixel(Uint8 *pixels, SlowBlitPixelAccess access, SDL_PixelF
|
||||
*outA = fA;
|
||||
}
|
||||
|
||||
static void WriteFloatPixel(Uint8 *pixels, SlowBlitPixelAccess access, SDL_PixelFormat *fmt, SDL_Colorspace colorspace,
|
||||
static void WriteFloatPixel(Uint8 *pixels, SlowBlitPixelAccess access, SDL_PixelFormat *fmt, SDL_Colorspace colorspace, float SDR_white_point,
|
||||
float fR, float fG, float fB, float fA)
|
||||
{
|
||||
Uint32 R, G, B, A;
|
||||
@@ -535,20 +534,19 @@ static void WriteFloatPixel(Uint8 *pixels, SlowBlitPixelAccess access, SDL_Pixel
|
||||
/* We converted to nits so src and dst are guaranteed to be linear and in the same units */
|
||||
switch (SDL_COLORSPACETRANSFER(colorspace)) {
|
||||
case SDL_TRANSFER_CHARACTERISTICS_SRGB:
|
||||
fR = SDL_sRGBfromNits(fR);
|
||||
fG = SDL_sRGBfromNits(fG);
|
||||
fB = SDL_sRGBfromNits(fB);
|
||||
fR = SDL_sRGBfromLinear(fR);
|
||||
fG = SDL_sRGBfromLinear(fG);
|
||||
fB = SDL_sRGBfromLinear(fB);
|
||||
break;
|
||||
case SDL_TRANSFER_CHARACTERISTICS_PQ:
|
||||
fR = SDL_PQfromNits(fR);
|
||||
fG = SDL_PQfromNits(fG);
|
||||
fB = SDL_PQfromNits(fB);
|
||||
fR = SDL_PQfromNits(fR * SDR_white_point);
|
||||
fG = SDL_PQfromNits(fG * SDR_white_point);
|
||||
fB = SDL_PQfromNits(fB * SDR_white_point);
|
||||
break;
|
||||
case SDL_TRANSFER_CHARACTERISTICS_LINEAR:
|
||||
/* Assuming scRGB for now */
|
||||
fR = SDL_scRGBfromNits(fR);
|
||||
fG = SDL_scRGBfromNits(fG);
|
||||
fB = SDL_scRGBfromNits(fB);
|
||||
fR *= SDR_white_point;
|
||||
fG *= SDR_white_point;
|
||||
fB *= SDR_white_point;
|
||||
break;
|
||||
default:
|
||||
/* Unknown, leave it alone */
|
||||
@@ -672,6 +670,7 @@ typedef enum
|
||||
{
|
||||
SDL_TONEMAP_NONE,
|
||||
SDL_TONEMAP_LINEAR,
|
||||
SDL_TONEMAP_PIECEWISE_HDR,
|
||||
} SDL_TonemapOperator;
|
||||
|
||||
typedef struct
|
||||
@@ -682,6 +681,11 @@ typedef struct
|
||||
struct {
|
||||
float scale;
|
||||
} linear;
|
||||
|
||||
struct
|
||||
{
|
||||
float scale;
|
||||
} piecewise_HDR;
|
||||
} data;
|
||||
|
||||
} SDL_TonemapContext;
|
||||
@@ -694,6 +698,11 @@ static void ApplyTonemap(SDL_TonemapContext *ctx, float *r, float *g, float *b)
|
||||
*g *= ctx->data.linear.scale;
|
||||
*b *= ctx->data.linear.scale;
|
||||
break;
|
||||
case SDL_TONEMAP_PIECEWISE_HDR:
|
||||
*r = (*r <= 1.0f) ? *r : (1.0f + (*r - 1.0f) * ctx->data.piecewise_HDR.scale);
|
||||
*g = (*g <= 1.0f) ? *g : (1.0f + (*g - 1.0f) * ctx->data.piecewise_HDR.scale);
|
||||
*b = (*r <= 1.0f) ? *b : (1.0f + (*b - 1.0f) * ctx->data.piecewise_HDR.scale);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -701,7 +710,7 @@ static void ApplyTonemap(SDL_TonemapContext *ctx, float *r, float *g, float *b)
|
||||
|
||||
static SDL_bool IsHDRColorspace(SDL_Colorspace colorspace)
|
||||
{
|
||||
if (colorspace == SDL_COLORSPACE_SCRGB ||
|
||||
if (colorspace == SDL_COLORSPACE_SRGB_LINEAR ||
|
||||
SDL_COLORSPACETRANSFER(colorspace) == SDL_TRANSFER_CHARACTERISTICS_PQ) {
|
||||
return SDL_TRUE;
|
||||
}
|
||||
@@ -732,6 +741,8 @@ void SDL_Blit_Slow_Float(SDL_BlitInfo *info)
|
||||
SDL_Colorspace src_colorspace;
|
||||
SDL_Colorspace dst_colorspace;
|
||||
const float *color_primaries_matrix = NULL;
|
||||
float SDR_white_point_src = 1.0f;
|
||||
float SDR_white_point_dst = 1.0f;
|
||||
SDL_TonemapContext tonemap;
|
||||
|
||||
if (SDL_GetSurfaceColorspace(info->src_surface, &src_colorspace) < 0 ||
|
||||
@@ -756,6 +767,9 @@ void SDL_Blit_Slow_Float(SDL_BlitInfo *info)
|
||||
}
|
||||
}
|
||||
|
||||
SDR_white_point_src = SDL_GetSurfaceSDRWhitePoint(info->src_surface, src_colorspace);
|
||||
SDR_white_point_dst = SDL_GetSurfaceSDRWhitePoint(info->dst_surface, dst_colorspace);
|
||||
|
||||
src_access = GetPixelAccessMethod(src_fmt);
|
||||
dst_access = GetPixelAccessMethod(dst_fmt);
|
||||
|
||||
@@ -773,7 +787,7 @@ void SDL_Blit_Slow_Float(SDL_BlitInfo *info)
|
||||
srcx = posx >> 16;
|
||||
src = (info->src + (srcy * info->src_pitch) + (srcx * srcbpp));
|
||||
|
||||
ReadFloatPixel(src, src_access, src_fmt, src_colorspace, &srcR, &srcG, &srcB, &srcA);
|
||||
ReadFloatPixel(src, src_access, src_fmt, src_colorspace, SDR_white_point_src, &srcR, &srcG, &srcB, &srcA);
|
||||
|
||||
if (color_primaries_matrix) {
|
||||
SDL_ConvertColorPrimaries(&srcR, &srcG, &srcB, color_primaries_matrix);
|
||||
@@ -787,7 +801,7 @@ void SDL_Blit_Slow_Float(SDL_BlitInfo *info)
|
||||
/* colorkey isn't supported */
|
||||
}
|
||||
if ((flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD | SDL_COPY_MUL))) {
|
||||
ReadFloatPixel(dst, dst_access, dst_fmt, dst_colorspace, &dstR, &dstG, &dstB, &dstA);
|
||||
ReadFloatPixel(dst, dst_access, dst_fmt, dst_colorspace, SDR_white_point_dst, &dstR, &dstG, &dstB, &dstA);
|
||||
} else {
|
||||
/* don't care */
|
||||
dstR = dstG = dstB = dstA = 0.0f;
|
||||
@@ -839,7 +853,7 @@ void SDL_Blit_Slow_Float(SDL_BlitInfo *info)
|
||||
break;
|
||||
}
|
||||
|
||||
WriteFloatPixel(dst, dst_access, dst_fmt, dst_colorspace, dstR, dstG, dstB, dstA);
|
||||
WriteFloatPixel(dst, dst_access, dst_fmt, dst_colorspace, SDR_white_point_dst, dstR, dstG, dstB, dstA);
|
||||
|
||||
posx += incx;
|
||||
dst += dstbpp;
|
||||
|
||||
@@ -706,7 +706,7 @@ SDL_Colorspace SDL_GetDefaultColorspaceForFormat(Uint32 format)
|
||||
if (SDL_ISPIXELFORMAT_FOURCC(format)) {
|
||||
return SDL_COLORSPACE_YUV_DEFAULT;
|
||||
} else if (SDL_ISPIXELFORMAT_FLOAT(format)) {
|
||||
return SDL_COLORSPACE_SCRGB;
|
||||
return SDL_COLORSPACE_SRGB_LINEAR;
|
||||
} else if (SDL_ISPIXELFORMAT_10BIT(format)) {
|
||||
return SDL_COLORSPACE_HDR10;
|
||||
} else {
|
||||
@@ -714,30 +714,18 @@ SDL_Colorspace SDL_GetDefaultColorspaceForFormat(Uint32 format)
|
||||
}
|
||||
}
|
||||
|
||||
float SDL_scRGBtoNits(float v)
|
||||
{
|
||||
return v * 80.0f;
|
||||
}
|
||||
|
||||
float SDL_scRGBfromNits(float v)
|
||||
{
|
||||
return v / 80.0f;
|
||||
}
|
||||
|
||||
float SDL_sRGBtoNits(float v)
|
||||
float SDL_sRGBtoLinear(float v)
|
||||
{
|
||||
if (v <= 0.04045f) {
|
||||
v = (v / 12.92f);
|
||||
} else {
|
||||
v = SDL_powf((v + 0.055f) / 1.055f, 2.4f);
|
||||
}
|
||||
return SDL_scRGBtoNits(v);
|
||||
return v;
|
||||
}
|
||||
|
||||
float SDL_sRGBfromNits(float v)
|
||||
float SDL_sRGBfromLinear(float v)
|
||||
{
|
||||
v = SDL_scRGBfromNits(v);
|
||||
|
||||
if (v <= 0.0031308f) {
|
||||
v = (v * 12.92f);
|
||||
} else {
|
||||
|
||||
@@ -34,10 +34,8 @@ extern int SDL_CalculateSize(Uint32 format, int width, int height, size_t *size,
|
||||
extern SDL_Colorspace SDL_GetDefaultColorspaceForFormat(Uint32 pixel_format);
|
||||
|
||||
/* Colorspace conversion functions */
|
||||
extern float SDL_scRGBtoNits(float v);
|
||||
extern float SDL_scRGBfromNits(float v);
|
||||
extern float SDL_sRGBtoNits(float v);
|
||||
extern float SDL_sRGBfromNits(float v);
|
||||
extern float SDL_sRGBtoLinear(float v);
|
||||
extern float SDL_sRGBfromLinear(float v);
|
||||
extern float SDL_PQtoNits(float v);
|
||||
extern float SDL_PQfromNits(float v);
|
||||
extern const float *SDL_GetYCbCRtoRGBConversionMatrix(SDL_Colorspace colorspace, int w, int h, int bits_per_pixel);
|
||||
@@ -52,6 +50,10 @@ extern void SDL_FreeBlitMap(SDL_BlitMap *map);
|
||||
|
||||
extern void SDL_InvalidateAllBlitMap(SDL_Surface *surface);
|
||||
|
||||
/* Surface functions */
|
||||
extern float SDL_GetSurfaceSDRWhitePoint(SDL_Surface *surface, SDL_Colorspace colorspace);
|
||||
extern float SDL_GetSurfaceHDRHeadroom(SDL_Surface *surface, SDL_Colorspace colorspace);
|
||||
|
||||
/* Miscellaneous functions */
|
||||
extern void SDL_DitherColors(SDL_Color *colors, int bpp);
|
||||
extern Uint8 SDL_FindColor(SDL_Palette *pal, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
|
||||
|
||||
@@ -303,6 +303,52 @@ int SDL_GetSurfaceColorspace(SDL_Surface *surface, SDL_Colorspace *colorspace)
|
||||
return 0;
|
||||
}
|
||||
|
||||
float SDL_GetSurfaceSDRWhitePoint(SDL_Surface *surface, SDL_Colorspace colorspace)
|
||||
{
|
||||
SDL_TransferCharacteristics transfer = SDL_COLORSPACETRANSFER(colorspace);
|
||||
|
||||
if (transfer == SDL_TRANSFER_CHARACTERISTICS_LINEAR ||
|
||||
transfer == SDL_TRANSFER_CHARACTERISTICS_PQ) {
|
||||
SDL_PropertiesID props;
|
||||
float default_value = 1.0f;
|
||||
|
||||
if (surface->flags & SDL_SURFACE_USES_PROPERTIES) {
|
||||
props = SDL_GetSurfaceProperties(surface);
|
||||
} else {
|
||||
props = 0;
|
||||
}
|
||||
if (transfer == SDL_TRANSFER_CHARACTERISTICS_PQ) {
|
||||
const float DEFAULT_PQ_SDR_WHITE_POINT = 100.0f;
|
||||
default_value = DEFAULT_PQ_SDR_WHITE_POINT;
|
||||
}
|
||||
return SDL_GetFloatProperty(props, SDL_PROP_SURFACE_SDR_WHITE_POINT_FLOAT, default_value);
|
||||
}
|
||||
return 1.0f;
|
||||
}
|
||||
|
||||
float SDL_GetSurfaceHDRHeadroom(SDL_Surface *surface, SDL_Colorspace colorspace)
|
||||
{
|
||||
SDL_TransferCharacteristics transfer = SDL_COLORSPACETRANSFER(colorspace);
|
||||
|
||||
if (transfer == SDL_TRANSFER_CHARACTERISTICS_LINEAR ||
|
||||
transfer == SDL_TRANSFER_CHARACTERISTICS_PQ) {
|
||||
SDL_PropertiesID props;
|
||||
float default_value = 0.0f;
|
||||
|
||||
if (surface->flags & SDL_SURFACE_USES_PROPERTIES) {
|
||||
props = SDL_GetSurfaceProperties(surface);
|
||||
} else {
|
||||
props = 0;
|
||||
}
|
||||
if (transfer == SDL_TRANSFER_CHARACTERISTICS_PQ &&
|
||||
SDL_HasProperty(props, SDL_PROP_SURFACE_MAXCLL_NUMBER)) {
|
||||
default_value = (float)SDL_GetNumberProperty(props, SDL_PROP_SURFACE_MAXCLL_NUMBER, 0) / SDL_GetSurfaceSDRWhitePoint(surface, colorspace);
|
||||
}
|
||||
return SDL_GetFloatProperty(props, SDL_PROP_SURFACE_HDR_HEADROOM_FLOAT, default_value);
|
||||
}
|
||||
return 1.0f;
|
||||
}
|
||||
|
||||
int SDL_SetSurfacePalette(SDL_Surface *surface, SDL_Palette *palette)
|
||||
{
|
||||
if (!surface) {
|
||||
@@ -1193,10 +1239,11 @@ int SDL_FlipSurface(SDL_Surface *surface, SDL_FlipMode flip)
|
||||
}
|
||||
}
|
||||
|
||||
static SDL_Surface *SDL_ConvertSurfaceWithPixelFormatAndColorspace(SDL_Surface *surface, const SDL_PixelFormat *format, Uint32 colorspace)
|
||||
static SDL_Surface *SDL_ConvertSurfaceWithPixelFormatAndColorspace(SDL_Surface *surface, const SDL_PixelFormat *format, Uint32 colorspace, SDL_PropertiesID props)
|
||||
{
|
||||
SDL_Surface *convert;
|
||||
SDL_Colorspace src_colorspace;
|
||||
SDL_PropertiesID src_properties;
|
||||
Uint32 copy_flags;
|
||||
SDL_Color copy_color;
|
||||
SDL_Rect bounds;
|
||||
@@ -1234,6 +1281,12 @@ static SDL_Surface *SDL_ConvertSurfaceWithPixelFormatAndColorspace(SDL_Surface *
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (surface->flags & SDL_SURFACE_USES_PROPERTIES) {
|
||||
src_properties = SDL_GetSurfaceProperties(surface);
|
||||
} else {
|
||||
src_properties = 0;
|
||||
}
|
||||
|
||||
/* Create a new surface with the desired format */
|
||||
convert = SDL_CreateSurface(surface->w, surface->h, format->format);
|
||||
if (!convert) {
|
||||
@@ -1246,7 +1299,7 @@ static SDL_Surface *SDL_ConvertSurfaceWithPixelFormatAndColorspace(SDL_Surface *
|
||||
SDL_SetSurfaceColorspace(convert, colorspace);
|
||||
|
||||
if (SDL_ISPIXELFORMAT_FOURCC(format->format) || SDL_ISPIXELFORMAT_FOURCC(surface->format->format)) {
|
||||
if (SDL_ConvertPixelsAndColorspace(surface->w, surface->h, surface->format->format, src_colorspace, surface->pixels, surface->pitch, convert->format->format, colorspace, convert->pixels, convert->pitch) < 0) {
|
||||
if (SDL_ConvertPixelsAndColorspace(surface->w, surface->h, surface->format->format, src_colorspace, src_properties, surface->pixels, surface->pitch, convert->format->format, colorspace, props, convert->pixels, convert->pitch) < 0) {
|
||||
SDL_DestroySurface(convert);
|
||||
return NULL;
|
||||
}
|
||||
@@ -1411,7 +1464,7 @@ static SDL_Surface *SDL_ConvertSurfaceWithPixelFormatAndColorspace(SDL_Surface *
|
||||
tmp->map->info.flags &= ~SDL_COPY_COLORKEY;
|
||||
|
||||
/* Conversion of the colorkey */
|
||||
tmp2 = SDL_ConvertSurfaceWithPixelFormatAndColorspace(tmp, format, colorspace);
|
||||
tmp2 = SDL_ConvertSurfaceWithPixelFormatAndColorspace(tmp, format, colorspace, props);
|
||||
if (!tmp2) {
|
||||
SDL_DestroySurface(tmp);
|
||||
SDL_DestroySurface(convert);
|
||||
@@ -1460,7 +1513,7 @@ SDL_Surface *SDL_DuplicateSurface(SDL_Surface *surface)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return SDL_ConvertSurfaceWithPixelFormatAndColorspace(surface, surface->format, SDL_COLORSPACE_UNKNOWN);
|
||||
return SDL_ConvertSurfaceWithPixelFormatAndColorspace(surface, surface->format, SDL_COLORSPACE_UNKNOWN, 0);
|
||||
}
|
||||
|
||||
SDL_Surface *SDL_ConvertSurface(SDL_Surface *surface, const SDL_PixelFormat *format)
|
||||
@@ -1479,12 +1532,13 @@ SDL_Surface *SDL_ConvertSurface(SDL_Surface *surface, const SDL_PixelFormat *for
|
||||
|
||||
colorspace = SDL_GetDefaultColorspaceForFormat(format->format);
|
||||
|
||||
return SDL_ConvertSurfaceWithPixelFormatAndColorspace(surface, format, colorspace);
|
||||
return SDL_ConvertSurfaceWithPixelFormatAndColorspace(surface, format, colorspace, 0);
|
||||
}
|
||||
|
||||
SDL_Surface *SDL_ConvertSurfaceFormat(SDL_Surface *surface, Uint32 pixel_format)
|
||||
{
|
||||
SDL_Colorspace colorspace;
|
||||
SDL_PropertiesID props;
|
||||
|
||||
if (!surface) {
|
||||
SDL_InvalidParamError("surface");
|
||||
@@ -1493,17 +1547,23 @@ SDL_Surface *SDL_ConvertSurfaceFormat(SDL_Surface *surface, Uint32 pixel_format)
|
||||
|
||||
colorspace = SDL_GetDefaultColorspaceForFormat(pixel_format);
|
||||
|
||||
return SDL_ConvertSurfaceFormatAndColorspace(surface, pixel_format, colorspace);
|
||||
if (surface->flags & SDL_SURFACE_USES_PROPERTIES) {
|
||||
props = SDL_GetSurfaceProperties(surface);
|
||||
} else {
|
||||
props = 0;
|
||||
}
|
||||
|
||||
return SDL_ConvertSurfaceFormatAndColorspace(surface, pixel_format, colorspace, props);
|
||||
}
|
||||
|
||||
SDL_Surface *SDL_ConvertSurfaceFormatAndColorspace(SDL_Surface *surface, Uint32 pixel_format, SDL_Colorspace colorspace)
|
||||
SDL_Surface *SDL_ConvertSurfaceFormatAndColorspace(SDL_Surface *surface, Uint32 pixel_format, SDL_Colorspace colorspace, SDL_PropertiesID props)
|
||||
{
|
||||
SDL_PixelFormat *format;
|
||||
SDL_Surface *convert = NULL;
|
||||
|
||||
format = SDL_CreatePixelFormat(pixel_format);
|
||||
if (format) {
|
||||
convert = SDL_ConvertSurfaceWithPixelFormatAndColorspace(surface, format, colorspace);
|
||||
convert = SDL_ConvertSurfaceWithPixelFormatAndColorspace(surface, format, colorspace, props);
|
||||
SDL_DestroyPixelFormat(format);
|
||||
}
|
||||
return convert;
|
||||
@@ -1512,7 +1572,7 @@ SDL_Surface *SDL_ConvertSurfaceFormatAndColorspace(SDL_Surface *surface, Uint32
|
||||
/*
|
||||
* Create a surface on the stack for quick blit operations
|
||||
*/
|
||||
static SDL_bool SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format, SDL_Colorspace colorspace, void *pixels, int pitch, SDL_Surface *surface, SDL_PixelFormat *format, SDL_BlitMap *blitmap)
|
||||
static SDL_bool SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format, SDL_Colorspace colorspace, SDL_PropertiesID props, void *pixels, int pitch, SDL_Surface *surface, SDL_PixelFormat *format, SDL_BlitMap *blitmap)
|
||||
{
|
||||
if (SDL_ISPIXELFORMAT_INDEXED(pixel_format)) {
|
||||
SDL_SetError("Indexed pixel formats not supported");
|
||||
@@ -1542,6 +1602,13 @@ static SDL_bool SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_for
|
||||
|
||||
SDL_SetSurfaceColorspace(surface, colorspace);
|
||||
|
||||
if (props) {
|
||||
SDL_PropertiesID surface_props = SDL_GetSurfaceProperties(surface);
|
||||
if (SDL_CopyProperties(props, surface_props) < 0) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/* The surface is ready to go */
|
||||
surface->refcount = 1;
|
||||
return SDL_TRUE;
|
||||
@@ -1577,8 +1644,8 @@ SDL_Surface *SDL_DuplicatePixels(int width, int height, Uint32 format, SDL_Color
|
||||
}
|
||||
|
||||
int SDL_ConvertPixelsAndColorspace(int width, int height,
|
||||
Uint32 src_format, SDL_Colorspace src_colorspace, const void *src, int src_pitch,
|
||||
Uint32 dst_format, SDL_Colorspace dst_colorspace, void *dst, int dst_pitch)
|
||||
Uint32 src_format, SDL_Colorspace src_colorspace, SDL_PropertiesID src_properties, const void *src, int src_pitch,
|
||||
Uint32 dst_format, SDL_Colorspace dst_colorspace, SDL_PropertiesID dst_properties, void *dst, int dst_pitch)
|
||||
{
|
||||
SDL_Surface src_surface, dst_surface;
|
||||
SDL_PixelFormat src_fmt, dst_fmt;
|
||||
@@ -1609,11 +1676,11 @@ int SDL_ConvertPixelsAndColorspace(int width, int height,
|
||||
|
||||
#if SDL_HAVE_YUV
|
||||
if (SDL_ISPIXELFORMAT_FOURCC(src_format) && SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
|
||||
return SDL_ConvertPixels_YUV_to_YUV(width, height, src_format, src_colorspace, src, src_pitch, dst_format, dst_colorspace, dst, dst_pitch);
|
||||
return SDL_ConvertPixels_YUV_to_YUV(width, height, src_format, src_colorspace, src_properties, src, src_pitch, dst_format, dst_colorspace, dst_properties, dst, dst_pitch);
|
||||
} else if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
|
||||
return SDL_ConvertPixels_YUV_to_RGB(width, height, src_format, src_colorspace, src, src_pitch, dst_format, dst_colorspace, dst, dst_pitch);
|
||||
return SDL_ConvertPixels_YUV_to_RGB(width, height, src_format, src_colorspace, src_properties, src, src_pitch, dst_format, dst_colorspace, dst_properties, dst, dst_pitch);
|
||||
} else if (SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
|
||||
return SDL_ConvertPixels_RGB_to_YUV(width, height, src_format, src_colorspace, src, src_pitch, dst_format, dst_colorspace, dst, dst_pitch);
|
||||
return SDL_ConvertPixels_RGB_to_YUV(width, height, src_format, src_colorspace, src_properties, src, src_pitch, dst_format, dst_colorspace, dst_properties, dst, dst_pitch);
|
||||
}
|
||||
#else
|
||||
if (SDL_ISPIXELFORMAT_FOURCC(src_format) || SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
|
||||
@@ -1634,11 +1701,11 @@ int SDL_ConvertPixelsAndColorspace(int width, int height,
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!SDL_CreateSurfaceOnStack(width, height, src_format, src_colorspace, nonconst_src, src_pitch, &src_surface, &src_fmt, &src_blitmap)) {
|
||||
if (!SDL_CreateSurfaceOnStack(width, height, src_format, src_colorspace, src_properties, nonconst_src, src_pitch, &src_surface, &src_fmt, &src_blitmap)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!SDL_CreateSurfaceOnStack(width, height, dst_format, dst_colorspace, dst, dst_pitch, &dst_surface, &dst_fmt, &dst_blitmap)) {
|
||||
if (!SDL_CreateSurfaceOnStack(width, height, dst_format, dst_colorspace, dst_properties, dst, dst_pitch, &dst_surface, &dst_fmt, &dst_blitmap)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -1660,8 +1727,8 @@ int SDL_ConvertPixels(int width, int height,
|
||||
Uint32 dst_format, void *dst, int dst_pitch)
|
||||
{
|
||||
return SDL_ConvertPixelsAndColorspace(width, height,
|
||||
src_format, SDL_COLORSPACE_UNKNOWN, src, src_pitch,
|
||||
dst_format, SDL_COLORSPACE_UNKNOWN, dst, dst_pitch);
|
||||
src_format, SDL_COLORSPACE_UNKNOWN, 0, src, src_pitch,
|
||||
dst_format, SDL_COLORSPACE_UNKNOWN, 0, dst, dst_pitch);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1777,7 +1844,7 @@ int SDL_ReadSurfacePixel(SDL_Surface *surface, int x, int y, Uint8 *r, Uint8 *g,
|
||||
SDL_Colorspace colorspace;
|
||||
|
||||
if (SDL_GetSurfaceColorspace(surface, &colorspace) == 0 &&
|
||||
SDL_ConvertPixelsAndColorspace(1, 1, surface->format->format, colorspace, p, surface->pitch, SDL_PIXELFORMAT_RGBA32, SDL_COLORSPACE_SRGB, rgba, sizeof(rgba)) == 0) {
|
||||
SDL_ConvertPixelsAndColorspace(1, 1, surface->format->format, colorspace, SDL_GetSurfaceProperties(surface), p, surface->pitch, SDL_PIXELFORMAT_RGBA32, SDL_COLORSPACE_SRGB, 0, rgba, sizeof(rgba)) == 0) {
|
||||
*r = rgba[0];
|
||||
*g = rgba[1];
|
||||
*b = rgba[2];
|
||||
|
||||
@@ -120,9 +120,8 @@ struct SDL_Window
|
||||
|
||||
typedef struct
|
||||
{
|
||||
SDL_bool enabled;
|
||||
float SDR_whitelevel;
|
||||
float HDR_whitelevel;
|
||||
float SDR_white_point;
|
||||
float HDR_headroom;
|
||||
} SDL_HDRDisplayProperties;
|
||||
|
||||
/*
|
||||
|
||||
@@ -701,14 +701,20 @@ SDL_DisplayID SDL_AddVideoDisplay(const SDL_VideoDisplay *display, SDL_bool send
|
||||
|
||||
props = SDL_GetDisplayProperties(id);
|
||||
|
||||
if (display->HDR.enabled) {
|
||||
if (display->HDR.HDR_headroom > 1.0f) {
|
||||
SDL_SetBooleanProperty(props, SDL_PROP_DISPLAY_HDR_ENABLED_BOOLEAN, SDL_TRUE);
|
||||
} else {
|
||||
SDL_SetBooleanProperty(props, SDL_PROP_DISPLAY_HDR_ENABLED_BOOLEAN, SDL_FALSE);
|
||||
}
|
||||
if (display->HDR.SDR_whitelevel != 0.0f) {
|
||||
SDL_SetFloatProperty(props, SDL_PROP_DISPLAY_SDR_WHITE_LEVEL_FLOAT, display->HDR.SDR_whitelevel);
|
||||
if (display->HDR.SDR_white_point <= 1.0f) {
|
||||
SDL_SetFloatProperty(props, SDL_PROP_DISPLAY_SDR_WHITE_POINT_FLOAT, 1.0f);
|
||||
} else {
|
||||
SDL_SetFloatProperty(props, SDL_PROP_DISPLAY_SDR_WHITE_POINT_FLOAT, display->HDR.SDR_white_point);
|
||||
}
|
||||
if (display->HDR.HDR_whitelevel != 0.0f) {
|
||||
SDL_SetFloatProperty(props, SDL_PROP_DISPLAY_HDR_WHITE_LEVEL_FLOAT, display->HDR.HDR_whitelevel);
|
||||
if (display->HDR.HDR_headroom <= 1.0f) {
|
||||
SDL_SetFloatProperty(props, SDL_PROP_DISPLAY_HDR_HEADROOM_FLOAT, 1.0f);
|
||||
} else {
|
||||
SDL_SetFloatProperty(props, SDL_PROP_DISPLAY_HDR_HEADROOM_FLOAT, display->HDR.HDR_headroom);
|
||||
}
|
||||
|
||||
return id;
|
||||
@@ -979,22 +985,32 @@ void SDL_SetDisplayHDRProperties(SDL_VideoDisplay *display, const SDL_HDRDisplay
|
||||
SDL_PropertiesID props = SDL_GetDisplayProperties(display->id);
|
||||
SDL_bool changed = SDL_FALSE;
|
||||
|
||||
if (HDR->enabled != display->HDR.enabled) {
|
||||
SDL_SetBooleanProperty(props, SDL_PROP_DISPLAY_HDR_ENABLED_BOOLEAN, HDR->enabled);
|
||||
if (HDR->SDR_white_point != display->HDR.SDR_white_point) {
|
||||
if (HDR->SDR_white_point <= 1.0f) {
|
||||
SDL_SetFloatProperty(props, SDL_PROP_DISPLAY_SDR_WHITE_POINT_FLOAT, 1.0f);
|
||||
} else {
|
||||
SDL_SetFloatProperty(props, SDL_PROP_DISPLAY_SDR_WHITE_POINT_FLOAT, HDR->SDR_white_point);
|
||||
}
|
||||
changed = SDL_TRUE;
|
||||
}
|
||||
if (HDR->SDR_whitelevel != display->HDR.SDR_whitelevel) {
|
||||
SDL_SetFloatProperty(props, SDL_PROP_DISPLAY_SDR_WHITE_LEVEL_FLOAT, HDR->SDR_whitelevel);
|
||||
changed = SDL_TRUE;
|
||||
}
|
||||
if (HDR->HDR_whitelevel != display->HDR.HDR_whitelevel) {
|
||||
SDL_SetFloatProperty(props, SDL_PROP_DISPLAY_HDR_WHITE_LEVEL_FLOAT, HDR->HDR_whitelevel);
|
||||
if (HDR->HDR_headroom != display->HDR.HDR_headroom) {
|
||||
if (HDR->HDR_headroom > 1.0f) {
|
||||
SDL_SetBooleanProperty(props, SDL_PROP_DISPLAY_HDR_ENABLED_BOOLEAN, SDL_TRUE);
|
||||
} else {
|
||||
SDL_SetBooleanProperty(props, SDL_PROP_DISPLAY_HDR_ENABLED_BOOLEAN, SDL_FALSE);
|
||||
}
|
||||
if (HDR->HDR_headroom <= 1.0f) {
|
||||
SDL_SetFloatProperty(props, SDL_PROP_DISPLAY_HDR_HEADROOM_FLOAT, 1.0f);
|
||||
} else {
|
||||
SDL_SetFloatProperty(props, SDL_PROP_DISPLAY_HDR_HEADROOM_FLOAT, HDR->HDR_headroom);
|
||||
}
|
||||
changed = SDL_TRUE;
|
||||
}
|
||||
SDL_copyp(&display->HDR, HDR);
|
||||
|
||||
if (changed) {
|
||||
SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_HDR_STATE_CHANGED, HDR->enabled);
|
||||
SDL_bool enabled = SDL_GetBooleanProperty(props, SDL_PROP_DISPLAY_HDR_ENABLED_BOOLEAN, SDL_FALSE);
|
||||
SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_HDR_STATE_CHANGED, enabled);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -555,8 +555,8 @@ static SDL_bool yuv_rgb_std(
|
||||
}
|
||||
|
||||
int SDL_ConvertPixels_YUV_to_RGB(int width, int height,
|
||||
Uint32 src_format, SDL_Colorspace src_colorspace, const void *src, int src_pitch,
|
||||
Uint32 dst_format, SDL_Colorspace dst_colorspace, void *dst, int dst_pitch)
|
||||
Uint32 src_format, SDL_Colorspace src_colorspace, SDL_PropertiesID src_properties, const void *src, int src_pitch,
|
||||
Uint32 dst_format, SDL_Colorspace dst_colorspace, SDL_PropertiesID dst_properties, void *dst, int dst_pitch)
|
||||
{
|
||||
const Uint8 *y = NULL;
|
||||
const Uint8 *u = NULL;
|
||||
@@ -597,14 +597,14 @@ int SDL_ConvertPixels_YUV_to_RGB(int width, int height,
|
||||
}
|
||||
|
||||
/* convert src/src_format to tmp/ARGB8888 */
|
||||
ret = SDL_ConvertPixels_YUV_to_RGB(width, height, src_format, src_colorspace, src, src_pitch, SDL_PIXELFORMAT_ARGB8888, SDL_COLORSPACE_SRGB, tmp, tmp_pitch);
|
||||
ret = SDL_ConvertPixels_YUV_to_RGB(width, height, src_format, src_colorspace, src_properties, src, src_pitch, SDL_PIXELFORMAT_ARGB8888, SDL_COLORSPACE_SRGB, 0, tmp, tmp_pitch);
|
||||
if (ret < 0) {
|
||||
SDL_free(tmp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* convert tmp/ARGB8888 to dst/RGB */
|
||||
ret = SDL_ConvertPixelsAndColorspace(width, height, SDL_PIXELFORMAT_ARGB8888, SDL_COLORSPACE_SRGB, tmp, tmp_pitch, dst_format, dst_colorspace, dst, dst_pitch);
|
||||
ret = SDL_ConvertPixelsAndColorspace(width, height, SDL_PIXELFORMAT_ARGB8888, SDL_COLORSPACE_SRGB, 0, tmp, tmp_pitch, dst_format, dst_colorspace, dst_properties, dst, dst_pitch);
|
||||
SDL_free(tmp);
|
||||
return ret;
|
||||
}
|
||||
@@ -935,8 +935,8 @@ static int SDL_ConvertPixels_ARGB8888_to_YUV(int width, int height, const void *
|
||||
}
|
||||
|
||||
int SDL_ConvertPixels_RGB_to_YUV(int width, int height,
|
||||
Uint32 src_format, SDL_Colorspace src_colorspace, const void *src, int src_pitch,
|
||||
Uint32 dst_format, SDL_Colorspace dst_colorspace, void *dst, int dst_pitch)
|
||||
Uint32 src_format, SDL_Colorspace src_colorspace, SDL_PropertiesID src_properties, const void *src, int src_pitch,
|
||||
Uint32 dst_format, SDL_Colorspace dst_colorspace, SDL_PropertiesID dst_properties, void *dst, int dst_pitch)
|
||||
{
|
||||
YCbCrType yuv_type = YCBCR_601;
|
||||
|
||||
@@ -2323,8 +2323,8 @@ static int SDL_ConvertPixels_Packed4_to_Planar2x2(int width, int height,
|
||||
#endif /* SDL_HAVE_YUV */
|
||||
|
||||
int SDL_ConvertPixels_YUV_to_YUV(int width, int height,
|
||||
Uint32 src_format, SDL_Colorspace src_colorspace, const void *src, int src_pitch,
|
||||
Uint32 dst_format, SDL_Colorspace dst_colorspace, void *dst, int dst_pitch)
|
||||
Uint32 src_format, SDL_Colorspace src_colorspace, SDL_PropertiesID src_properties, const void *src, int src_pitch,
|
||||
Uint32 dst_format, SDL_Colorspace dst_colorspace, SDL_PropertiesID dst_properties, void *dst, int dst_pitch)
|
||||
{
|
||||
#if SDL_HAVE_YUV
|
||||
if (src_colorspace != dst_colorspace) {
|
||||
|
||||
@@ -26,9 +26,9 @@
|
||||
|
||||
/* YUV conversion functions */
|
||||
|
||||
extern int SDL_ConvertPixels_YUV_to_RGB(int width, int height, Uint32 src_format, SDL_Colorspace src_colorspace, const void *src, int src_pitch, Uint32 dst_format, SDL_Colorspace dst_colorspace, void *dst, int dst_pitch);
|
||||
extern int SDL_ConvertPixels_RGB_to_YUV(int width, int height, Uint32 src_format, SDL_Colorspace src_colorspace, const void *src, int src_pitch, Uint32 dst_format, SDL_Colorspace dst_colorspace, void *dst, int dst_pitch);
|
||||
extern int SDL_ConvertPixels_YUV_to_YUV(int width, int height, Uint32 src_format, SDL_Colorspace src_colorspace, const void *src, int src_pitch, Uint32 dst_format, SDL_Colorspace dst_colorspace, void *dst, int dst_pitch);
|
||||
extern int SDL_ConvertPixels_YUV_to_RGB(int width, int height, Uint32 src_format, SDL_Colorspace src_colorspace, SDL_PropertiesID src_properties, const void *src, int src_pitch, Uint32 dst_format, SDL_Colorspace dst_colorspace, SDL_PropertiesID dst_properties, void *dst, int dst_pitch);
|
||||
extern int SDL_ConvertPixels_RGB_to_YUV(int width, int height, Uint32 src_format, SDL_Colorspace src_colorspace, SDL_PropertiesID src_properties, const void *src, int src_pitch, Uint32 dst_format, SDL_Colorspace dst_colorspace, SDL_PropertiesID dst_properties, void *dst, int dst_pitch);
|
||||
extern int SDL_ConvertPixels_YUV_to_YUV(int width, int height, Uint32 src_format, SDL_Colorspace src_colorspace, SDL_PropertiesID src_properties, const void *src, int src_pitch, Uint32 dst_format, SDL_Colorspace dst_colorspace, SDL_PropertiesID dst_properties, void *dst, int dst_pitch);
|
||||
|
||||
|
||||
extern int SDL_CalculateYUVSize(Uint32 format, int w, int h, size_t *size, size_t *pitch);
|
||||
|
||||
@@ -292,17 +292,14 @@ static char *Cocoa_GetDisplayName(CGDirectDisplayID displayID)
|
||||
|
||||
static void Cocoa_GetHDRProperties(CGDirectDisplayID displayID, SDL_HDRDisplayProperties *HDR)
|
||||
{
|
||||
HDR->enabled = SDL_FALSE;
|
||||
HDR->SDR_whitelevel = 0.0f;
|
||||
HDR->SDR_white_point = 1.0f;
|
||||
HDR->HDR_headroom = 1.0f;
|
||||
|
||||
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101500 /* Added in the 10.15 SDK */
|
||||
if (@available(macOS 10.15, *)) {
|
||||
NSScreen *screen = GetNSScreenForDisplayID(displayID);
|
||||
|
||||
if (screen && screen.maximumPotentialExtendedDynamicRangeColorComponentValue > 1.0f) {
|
||||
HDR->enabled = SDL_TRUE;
|
||||
HDR->SDR_whitelevel = 80.0f; /* SDR content is always at scRGB 1.0 */
|
||||
HDR->HDR_whitelevel = HDR->SDR_whitelevel * screen.maximumExtendedDynamicRangeColorComponentValue;
|
||||
if (screen) {
|
||||
HDR->HDR_headroom = screen.maximumExtendedDynamicRangeColorComponentValue;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -242,13 +242,12 @@ int UIKit_AddDisplay(UIScreen *uiscreen, SDL_bool send_event)
|
||||
}
|
||||
display.desktop_mode = mode;
|
||||
|
||||
display.HDR.SDR_white_point = 1.0f;
|
||||
display.HDR.HDR_headroom = 1.0f;
|
||||
|
||||
#ifndef SDL_PLATFORM_TVOS
|
||||
if (@available(iOS 16.0, *)) {
|
||||
if (uiscreen.potentialEDRHeadroom > 1.0f) {
|
||||
display.HDR.enabled = SDL_TRUE;
|
||||
display.HDR.SDR_whitelevel = 80.0f; /* SDR content is always at scRGB 1.0 */
|
||||
display.HDR.HDR_whitelevel = display.HDR.SDR_whitelevel * uiscreen.currentEDRHeadroom;
|
||||
}
|
||||
display.HDR.HDR_headroom = uiscreen.currentEDRHeadroom;
|
||||
}
|
||||
#endif /* !SDL_PLATFORM_TVOS */
|
||||
|
||||
|
||||
@@ -452,10 +452,10 @@ done:
|
||||
return found;
|
||||
}
|
||||
|
||||
static float WIN_GetSDRWhiteLevel(HMONITOR hMonitor)
|
||||
static float WIN_GetSDRWhitePoint(HMONITOR hMonitor)
|
||||
{
|
||||
DISPLAYCONFIG_PATH_INFO path_info;
|
||||
float SDR_whitelevel = 200.0f;
|
||||
float SDR_white_point = 1.0f;
|
||||
|
||||
if (WIN_GetMonitorPathInfo(hMonitor, &path_info)) {
|
||||
DISPLAYCONFIG_SDR_WHITE_LEVEL white_level;
|
||||
@@ -465,11 +465,12 @@ static float WIN_GetSDRWhiteLevel(HMONITOR hMonitor)
|
||||
white_level.header.size = sizeof(white_level);
|
||||
white_level.header.adapterId = path_info.targetInfo.adapterId;
|
||||
white_level.header.id = path_info.targetInfo.id;
|
||||
if (DisplayConfigGetDeviceInfo(&white_level.header) == ERROR_SUCCESS) {
|
||||
SDR_whitelevel = (white_level.SDRWhiteLevel / 1000.0f) * 80.0f;
|
||||
if (DisplayConfigGetDeviceInfo(&white_level.header) == ERROR_SUCCESS &&
|
||||
white_level.SDRWhiteLevel > 0) {
|
||||
SDR_white_point = (white_level.SDRWhiteLevel / 1000.0f);
|
||||
}
|
||||
}
|
||||
return SDR_whitelevel;
|
||||
return SDR_white_point;
|
||||
}
|
||||
|
||||
static void WIN_GetHDRProperties(SDL_VideoDevice *_this, HMONITOR hMonitor, SDL_HDRDisplayProperties *HDR)
|
||||
@@ -480,9 +481,8 @@ static void WIN_GetHDRProperties(SDL_VideoDevice *_this, HMONITOR hMonitor, SDL_
|
||||
|
||||
if (WIN_GetMonitorDESC1(hMonitor, &desc)) {
|
||||
if (desc.ColorSpace == DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020) {
|
||||
HDR->enabled = SDL_TRUE;
|
||||
HDR->SDR_whitelevel = WIN_GetSDRWhiteLevel(hMonitor);
|
||||
HDR->HDR_whitelevel = desc.MaxLuminance;
|
||||
HDR->SDR_white_point = WIN_GetSDRWhitePoint(hMonitor);
|
||||
HDR->HDR_headroom = (desc.MaxLuminance / 80.0f) / HDR->SDR_white_point;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user