clutter/color-state-params: Add new_from_primitives

When creating a new color state from the primitives Colorimetry, EOTF
and Luminance; it is needed to previously check their tags to properly get
their values and avoid UB.

This check is duplicated and is a bit unreadable.

Using this new function helps keeping readability.

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/4144>
This commit is contained in:
Joan Torres 2024-10-25 14:41:55 +02:00 committed by Marge Bot
parent 2e2a91a14b
commit 7f8df9d9a1
4 changed files with 98 additions and 98 deletions

View File

@ -1153,9 +1153,9 @@ clutter_color_state_params_get_blending (ClutterColorState *color_state,
ClutterColorStateParams *color_state_params =
CLUTTER_COLOR_STATE_PARAMS (color_state);
ClutterContext *context;
ClutterTransferFunction blending_tf;
ClutterColorspace colorspace;
ClutterPrimaries *primaries;
ClutterEOTF blending_eotf;
blending_eotf.type = CLUTTER_EOTF_TYPE_NAMED;
switch (color_state_params->eotf.type)
{
@ -1164,51 +1164,35 @@ clutter_color_state_params_get_blending (ClutterColorState *color_state,
{
/* effectively this means we will blend sRGB content in sRGB, not linear */
case CLUTTER_TRANSFER_FUNCTION_SRGB:
blending_tf = color_state_params->eotf.tf_name;
blending_eotf.tf_name = CLUTTER_TRANSFER_FUNCTION_SRGB;
break;
case CLUTTER_TRANSFER_FUNCTION_PQ:
case CLUTTER_TRANSFER_FUNCTION_BT709:
case CLUTTER_TRANSFER_FUNCTION_LINEAR:
blending_tf = CLUTTER_TRANSFER_FUNCTION_LINEAR;
blending_eotf.tf_name = CLUTTER_TRANSFER_FUNCTION_LINEAR;
break;
default:
g_assert_not_reached ();
}
break;
case CLUTTER_EOTF_TYPE_GAMMA:
blending_tf = CLUTTER_TRANSFER_FUNCTION_LINEAR;
blending_eotf.tf_name = CLUTTER_TRANSFER_FUNCTION_LINEAR;
break;
}
if (force)
blending_tf = CLUTTER_TRANSFER_FUNCTION_LINEAR;
blending_eotf.tf_name = CLUTTER_TRANSFER_FUNCTION_LINEAR;
if (color_state_params->eotf.type == CLUTTER_EOTF_TYPE_NAMED &&
color_state_params->eotf.tf_name == blending_tf)
color_state_params->eotf.tf_name == blending_eotf.tf_name)
return g_object_ref (color_state);
switch (color_state_params->colorimetry.type)
{
case CLUTTER_COLORIMETRY_TYPE_COLORSPACE:
colorspace = color_state_params->colorimetry.colorspace;
primaries = NULL;
break;
case CLUTTER_COLORIMETRY_TYPE_PRIMARIES:
colorspace = CLUTTER_COLORSPACE_SRGB;
primaries = color_state_params->colorimetry.primaries;
break;
}
g_object_get (G_OBJECT (color_state), "context", &context, NULL);
return clutter_color_state_params_new_full (context,
colorspace,
blending_tf,
primaries,
-1.0f,
color_state_params->luminance.min,
color_state_params->luminance.max,
color_state_params->luminance.ref);
return clutter_color_state_params_new_from_primitives (context,
color_state_params->colorimetry,
blending_eotf,
color_state_params->luminance);
}
static void
@ -1311,3 +1295,55 @@ clutter_color_state_params_new_full (ClutterContext *context,
return CLUTTER_COLOR_STATE (color_state_params);
}
/**
* clutter_color_state_params_new_from_primitives:
*
* Create a new ClutterColorState object using the color primitives.
*
* Return value: A new ClutterColorState object.
**/
ClutterColorState *
clutter_color_state_params_new_from_primitives (ClutterContext *context,
ClutterColorimetry colorimetry,
ClutterEOTF eotf,
ClutterLuminance luminance)
{
ClutterColorspace colorspace;
ClutterPrimaries *primaries;
ClutterTransferFunction tf_name;
float gamma_exp;
switch (colorimetry.type)
{
case CLUTTER_COLORIMETRY_TYPE_COLORSPACE:
colorspace = colorimetry.colorspace;
primaries = NULL;
break;
case CLUTTER_COLORIMETRY_TYPE_PRIMARIES:
colorspace = CLUTTER_COLORSPACE_SRGB;
primaries = colorimetry.primaries;
break;
}
switch (eotf.type)
{
case CLUTTER_EOTF_TYPE_NAMED:
tf_name = eotf.tf_name;
gamma_exp = -1.0f;
break;
case CLUTTER_EOTF_TYPE_GAMMA:
tf_name = CLUTTER_TRANSFER_FUNCTION_SRGB;
gamma_exp = eotf.gamma_exp;
break;
}
return clutter_color_state_params_new_full (context,
colorspace,
tf_name,
primaries,
gamma_exp,
luminance.min,
luminance.max,
luminance.ref);
}

View File

@ -117,6 +117,12 @@ ClutterColorState * clutter_color_state_params_new_full (ClutterContext
float max_lum,
float ref_lum);
CLUTTER_EXPORT
ClutterColorState * clutter_color_state_params_new_from_primitives (ClutterContext *context,
ClutterColorimetry colorimetry,
ClutterEOTF eotf,
ClutterLuminance luminance);
CLUTTER_EXPORT
const ClutterColorimetry * clutter_color_state_params_get_colorimetry (ClutterColorStateParams *color_state_params);

View File

@ -625,18 +625,25 @@ on_manager_ready (MetaColorManager *color_manager,
create_cd_device (color_device);
}
static ClutterColorspace
get_color_space_from_monitor (MetaMonitor *monitor)
static ClutterColorimetry
get_colorimetry_from_monitor (MetaMonitor *monitor)
{
ClutterColorimetry colorimetry;
colorimetry.type = CLUTTER_COLORIMETRY_TYPE_COLORSPACE;
switch (meta_monitor_get_color_space (monitor))
{
case META_OUTPUT_COLORSPACE_DEFAULT:
case META_OUTPUT_COLORSPACE_UNKNOWN:
return CLUTTER_COLORSPACE_SRGB;
colorimetry.colorspace = CLUTTER_COLORSPACE_SRGB;
break;
case META_OUTPUT_COLORSPACE_BT2020:
return CLUTTER_COLORSPACE_BT2020;
colorimetry.colorspace = CLUTTER_COLORSPACE_BT2020;
break;
}
g_assert_not_reached ();
return colorimetry;
}
static ClutterEOTF
@ -685,51 +692,33 @@ update_color_state (MetaColorDevice *color_device)
MetaDebugControl *debug_control = meta_context_get_debug_control (context);
ClutterContext *clutter_context = meta_backend_get_clutter_context (backend);
g_autoptr (ClutterColorState) color_state = NULL;
ClutterColorspace colorspace;
ClutterColorimetry colorimetry;
ClutterEOTF eotf;
ClutterTransferFunction transfer_function;
const ClutterLuminance *luminance;
float gamma_exp;
ClutterLuminance luminance;
float reference_luminance_factor;
float new_ref_luminance;
UpdateResult result = 0;
colorspace = get_color_space_from_monitor (monitor);
colorimetry = get_colorimetry_from_monitor (monitor);
eotf = get_eotf_from_monitor (monitor);
if (meta_debug_control_is_hdr_forced (debug_control))
{
colorspace = CLUTTER_COLORSPACE_BT2020;
colorimetry.type = CLUTTER_COLORIMETRY_TYPE_COLORSPACE;
colorimetry.colorspace = CLUTTER_COLORSPACE_BT2020;
eotf.type = CLUTTER_EOTF_TYPE_NAMED;
eotf.tf_name = CLUTTER_TRANSFER_FUNCTION_PQ;
}
luminance = clutter_eotf_get_default_luminance (eotf);
luminance = *clutter_eotf_get_default_luminance (eotf);
reference_luminance_factor =
meta_debug_control_get_luminance_percentage (debug_control) / 100.0f;
new_ref_luminance = luminance->ref * reference_luminance_factor;
luminance.ref = luminance.ref * reference_luminance_factor;
switch (eotf.type)
{
case CLUTTER_EOTF_TYPE_NAMED:
transfer_function = eotf.tf_name;
gamma_exp = -1.0f;
break;
case CLUTTER_EOTF_TYPE_GAMMA:
transfer_function = CLUTTER_TRANSFER_FUNCTION_SRGB;
gamma_exp = eotf.gamma_exp;
break;
}
color_state = clutter_color_state_params_new_full (clutter_context,
colorspace,
transfer_function,
NULL,
gamma_exp,
luminance->min,
luminance->max,
new_ref_luminance);
color_state = clutter_color_state_params_new_from_primitives (clutter_context,
colorimetry,
eotf,
luminance);
if (!color_device->color_state ||
!clutter_color_state_equals (color_device->color_state, color_state))

View File

@ -911,10 +911,6 @@ creator_params_create (struct wl_client *client,
struct wl_resource *image_desc_resource;
g_autoptr (ClutterColorState) color_state = NULL;
MetaWaylandImageDescription *image_desc;
ClutterColorspace colorspace;
ClutterPrimaries *primaries;
ClutterTransferFunction tf_name;
float gamma_exp;
if (!creator_params->is_colorimetry_set || !creator_params->is_eotf_set)
{
@ -930,38 +926,11 @@ creator_params_create (struct wl_client *client,
wl_resource_get_version (resource),
id);
switch (creator_params->colorimetry.type)
{
case CLUTTER_COLORIMETRY_TYPE_COLORSPACE:
colorspace = creator_params->colorimetry.colorspace;
primaries = NULL;
break;
case CLUTTER_COLORIMETRY_TYPE_PRIMARIES:
colorspace = CLUTTER_COLORSPACE_SRGB;
primaries = creator_params->colorimetry.primaries;
break;
}
switch (creator_params->eotf.type)
{
case CLUTTER_EOTF_TYPE_NAMED:
tf_name = creator_params->eotf.tf_name;
gamma_exp = -1.0f;
break;
case CLUTTER_EOTF_TYPE_GAMMA:
tf_name = CLUTTER_TRANSFER_FUNCTION_SRGB;
gamma_exp = creator_params->eotf.gamma_exp;
break;
}
color_state = clutter_color_state_params_new_full (clutter_context,
colorspace,
tf_name,
primaries,
gamma_exp,
creator_params->lum.min,
creator_params->lum.max,
creator_params->lum.ref);
color_state =
clutter_color_state_params_new_from_primitives (clutter_context,
creator_params->colorimetry,
creator_params->eotf,
creator_params->lum);
image_desc =
meta_wayland_image_description_new_color_state (color_manager,