libweston: Move apply_color_effect() to common code

Signed-off-by: Robert Mader <robert.mader@collabora.com>
This commit is contained in:
Robert Mader 2026-03-19 18:16:14 +01:00
parent d5569d27a3
commit e52d58cd4b
3 changed files with 51 additions and 51 deletions

View file

@ -234,3 +234,48 @@ weston_color_curve_sample(struct weston_compositor *compositor,
weston_assert_not_reached(compositor, "unknown color curve");
}
WL_EXPORT void
weston_apply_color_effect(struct weston_output *output, float *r, float *g,
float *b, const float a)
{
struct weston_compositor *compositor = output->compositor;
struct weston_output_color_effect *effect = output->color_effect;
struct weston_vec3f input = WESTON_VEC3F(*r, *g, *b);
struct weston_vec3f res;
/*
* Caller guarantees alpha is always 0.0f (fully transparent) or 1.0f
* (fully opaque). If alpha is 0.0f, no color effect needed. Otherwise
* assert that alpha is 1.0f, and in such case we don't have to worry
* if values are alpha premultiplied or not.
*/
if (!output->color_effect || a == 0.0f) {
return;
}
weston_assert_f32_eq(compositor, a, 1.0f);
switch (effect->type) {
case WESTON_OUTPUT_COLOR_EFFECT_TYPE_INVERSION:
*r = 1.0f - *r;
*g = 1.0f - *g;
*b = 1.0f - *b;
return;
case WESTON_OUTPUT_COLOR_EFFECT_TYPE_GRAYSCALE:
*r = 0.2126f * (*r) + 0.7152f * (*g) + 0.0722f * (*b);
*g = *r;
*b = *r;
return;
case WESTON_OUTPUT_COLOR_EFFECT_TYPE_CVD_CORRECTION:
/**
* See weston_output_color_effect_cvd_correction() for more details.
*/
res = weston_m3f_mul_v3f(effect->u.cvd.correction, input);
res = weston_v3f_clamp(res, 0.0f, 1.0f);
*r = res.el[0];
*g = res.el[1];
*b = res.el[2];
return;
};
weston_assert_not_reached(compositor, "unknown color effect type");
}

View file

@ -38,4 +38,8 @@ weston_color_curve_sample(struct weston_compositor *compositor,
struct weston_vec3f *out,
size_t len);
void
weston_apply_color_effect(struct weston_output *output, float *r, float *g,
float *b, const float a);
#endif /* WESTON_COLOR_OPERATIONS_H */

View file

@ -49,6 +49,7 @@
#include "timeline.h"
#include "color.h"
#include "color-operations.h"
#include "color-representation.h"
#include "gl-renderer.h"
#include "gl-renderer-internal.h"
@ -2313,56 +2314,6 @@ weston_output_cvd_type_to_str(struct weston_cvd_correction cvd)
return "invalid cvd";
}
static void
apply_color_effect(struct gl_renderer *gr, struct weston_output *output,
float *r, float *g, float *b, const float a)
{
struct weston_compositor *compositor = output->compositor;
struct weston_output_color_effect *effect = output->color_effect;
struct weston_vec3f input = WESTON_VEC3F(*r, *g, *b);
struct weston_vec3f res;
/*
* Caller guarantees alpha is always 0.0f (fully transparent) or 1.0f
* (fully opaque). If alpha is 0.0f, no color effect needed. Otherwise
* assert that alpha is 1.0f, and in such case we don't have to worry
* if values are alpha premultiplied or not.
*/
if (!output->color_effect || a == 0.0f) {
return;
}
weston_assert_f32_eq(compositor, a, 1.0f);
switch (effect->type) {
case WESTON_OUTPUT_COLOR_EFFECT_TYPE_INVERSION:
*r = 1.0f - *r;
*g = 1.0f - *g;
*b = 1.0f - *b;
gl_log_paint_node(gr, "\t\tcolor effect: inversion\n");
return;
case WESTON_OUTPUT_COLOR_EFFECT_TYPE_GRAYSCALE:
*r = 0.2126f * (*r) + 0.7152f * (*g) + 0.0722f * (*b);
*g = *r;
*b = *r;
gl_log_paint_node(gr, "\t\tcolor effect: grayscale\n");
return;
case WESTON_OUTPUT_COLOR_EFFECT_TYPE_CVD_CORRECTION:
/**
* See weston_output_color_effect_cvd_correction() for more details.
*/
res = weston_m3f_mul_v3f(effect->u.cvd.correction, input);
res = weston_v3f_clamp(res, 0.0f, 1.0f);
*r = res.el[0];
*g = res.el[1];
*b = res.el[2];
weston_log_scope_printf(gr->paint_node_scope,
"\t\tcolor effect: cvd - %s\n",
weston_output_cvd_type_to_str(effect->u.cvd));
return;
};
weston_assert_not_reached(compositor, "unknown color effect type");
}
static void
clear_region(struct gl_renderer *gr, struct weston_paint_node *pnode,
pixman_region32_t *repaint)
@ -2387,7 +2338,7 @@ clear_region(struct gl_renderer *gr, struct weston_paint_node *pnode,
g = pnode->solid.g;
b = pnode->solid.b;
a = pnode->solid.a;
apply_color_effect(gr, output, &r, &g, &b, a);
weston_apply_color_effect(output, &r, &g, &b, a);
glClearColor(r, g, b, a);
glEnable(GL_SCISSOR_TEST);