libweston: introduce CMS component architecture

See: https://gitlab.freedesktop.org/wayland/weston/-/issues/467#note_814985

This starts building the framework required for implementing color
management.

The main new interface is struct weston_color_manager. This commit also
adds a no-op color manager implementation, which is used if no other
color manager is loaded. This no-op color manager simply provides
identity color transforms for everything, so that Weston keeps running
exactly like before.

weston_color_manager interface is incomplete and will be extended later.

Colorspace objects are not introduced in this commit. However, when
client content colorspace and output colorspace definitions are
combined, they will produce color transformations from client content to
output blending space and from output blending space to output space.

This commit introduces a placeholder struct for color transforms,
weston_color_transform. Objects of this type are expected to be heavy to
create and store, which is why they are designed to be shared as much as
possible, ideally making their instances unique. As color transform
description is intended to be generic in libweston core, renderers and
backends are expected to derive their own state for each transform
object as necessary. Creating and storing the derived state maybe be
expensive as well, more the reason to re-use these objects as much as
possible. E.g. GL-renderer might upload a 3D LUT into a texture and keep
the texture around. DRM-backend might create a KMS blob for a LUT and
keep that around.

As a color transform depends on both the surface and the output, a
transform object may need to be created for each unique pair of them.
Therefore color transforms are referenced from weston_paint_node. As
paint nodes exist for not just surface+output but surface+view+output
triplets, the code ensures that all paint nodes (having different view)
for the same surface+output have the same color transform state.

As a special case, if weston_color_transform is NULL, it means identity
transform. This short-circuits some checks and memory allocations, but
it does mean we use a separate member on weston_paint_node to know if
the color transform has been initialized or not.

Color transformations are pre-created at the weston_output
paint_node_z_order_list creation step. Currently the z order lists
contain all views globally, which means we populate color transforms we
may never need, e.g. a view is never shown on a particular output.
This problem should get fixed naturally when z order lists are
constructed "pruned" in the future: to contain only those paint nodes
that actually contribute to the output's image.

As nothing actually supports color transforms yet, both renderers and
the DRM-backend assert that they only get identity transforms. This
check has the side-effect that all surface-output pairs actually get a
weston_surface_color_transform_ref even though it points to NULL
weston_color_transform.

This design is inspired by Sebastian Wick's Weston color management
work.

Co-authored-by: Sebastian Wick <sebastian@sebastianwick.net>
Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.com>
This commit is contained in:
Pekka Paalanen 2021-02-25 12:03:28 +02:00 committed by Pekka Paalanen
parent da0f7ea4a7
commit 90a5ffa097
10 changed files with 519 additions and 3 deletions

View File

@ -1046,6 +1046,7 @@ struct weston_touch_calibrator;
struct weston_desktop_xwayland;
struct weston_desktop_xwayland_interface;
struct weston_debug_compositor;
struct weston_color_manager;
/** Main object, container-like structure which aggregates all other objects.
*
@ -1114,12 +1115,11 @@ struct weston_compositor {
struct weston_plane primary_plane;
uint32_t capabilities; /* combination of enum weston_capability */
struct weston_color_manager *color_manager;
struct weston_renderer *renderer;
pixman_format_code_t read_format;
struct weston_backend *backend;
struct weston_launcher *launcher;
struct wl_list plugin_api_list; /* struct weston_plugin_api::link */

View File

@ -38,6 +38,7 @@
#include "drm-internal.h"
#include "color.h"
#include "linux-dmabuf.h"
#include "presentation-time-server-protocol.h"
@ -882,6 +883,13 @@ drm_output_propose_state(struct weston_output *output_base,
continue;
}
/* Cannot show anything without a color transform. */
if (!pnode->surf_xform_valid) {
drm_debug(b, "\t\t\t\t[view] ignoring view %p "
"(color transform failed)\n", ev);
continue;
}
/* Ignore views we know to be totally occluded. */
pixman_region32_init(&clipped_view);
pixman_region32_intersect(&clipped_view,
@ -917,6 +925,13 @@ drm_output_propose_state(struct weston_output *output_base,
force_renderer = true;
}
if (pnode->surf_xform.transform != NULL ||
!pnode->surf_xform.identity_pipeline) {
drm_debug(b, "\t\t\t\t[view] not assigning view %p to plane "
"(requires color transform)\n", ev);
force_renderer = true;
}
/* Since we process views from top to bottom, we know that if
* the view intersects the calculated renderer region, it must
* be part of, or occluded by, it, and cannot go on a plane. */

100
libweston/color-noop.c Normal file
View File

@ -0,0 +1,100 @@
/*
* Copyright 2021 Collabora, Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "config.h"
#include <libweston/libweston.h>
#include "color.h"
#include "shared/helpers.h"
struct weston_color_manager_noop {
struct weston_color_manager base;
};
static struct weston_color_manager_noop *
get_cmnoop(struct weston_color_manager *cm_base)
{
return container_of(cm_base, struct weston_color_manager_noop, base);
}
static void
cmnoop_destroy_color_transform(struct weston_color_transform *xform)
{
/* Never called, as never creates an actual color transform. */
}
static bool
cmnoop_get_surface_color_transform(struct weston_color_manager *cm_base,
struct weston_surface *surface,
struct weston_output *output,
struct weston_surface_color_transform *surf_xform)
{
/* TODO: Assert surface has no colorspace set */
/* TODO: Assert output has no colorspace set */
/* Identity transform */
surf_xform->transform = NULL;
surf_xform->identity_pipeline = true;
return true;
}
static bool
cmnoop_init(struct weston_color_manager *cm_base)
{
/* No renderer requirements to check. */
/* Nothing to initialize. */
return true;
}
static void
cmnoop_destroy(struct weston_color_manager *cm_base)
{
struct weston_color_manager_noop *cmnoop = get_cmnoop(cm_base);
free(cmnoop);
}
struct weston_color_manager *
weston_color_manager_noop_create(struct weston_compositor *compositor)
{
struct weston_color_manager_noop *cm;
cm = zalloc(sizeof *cm);
if (!cm)
return NULL;
cm->base.name = "no-op";
cm->base.compositor = compositor;
cm->base.supports_client_protocol = false;
cm->base.init = cmnoop_init;
cm->base.destroy = cmnoop_destroy;
cm->base.destroy_color_transform = cmnoop_destroy_color_transform;
cm->base.get_surface_color_transform =
cmnoop_get_surface_color_transform;
return &cm->base;
}

151
libweston/color.c Normal file
View File

@ -0,0 +1,151 @@
/*
* Copyright 2021 Collabora, Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "config.h"
#include <libweston/libweston.h>
#include <assert.h>
#include "color.h"
#include "libweston-internal.h"
/**
* Increase reference count of the color transform object
*
* \param xform The color transform. NULL is accepted too.
* \return xform.
*/
WL_EXPORT struct weston_color_transform *
weston_color_transform_ref(struct weston_color_transform *xform)
{
/* NULL is a valid color transform: identity */
if (!xform)
return NULL;
assert(xform->ref_count > 0);
xform->ref_count++;
return xform;
}
/**
* Decrease and potentially destroy the color transform object
*
* \param xform The color transform. NULL is accepted too.
*/
WL_EXPORT void
weston_color_transform_unref(struct weston_color_transform *xform)
{
if (!xform)
return;
assert(xform->ref_count > 0);
if (--xform->ref_count > 0)
return;
wl_signal_emit(&xform->destroy_signal, xform);
xform->cm->destroy_color_transform(xform);
}
/**
* Initializes a newly allocated color transform object
*
* This is used only by color managers. They sub-class weston_color_transform.
*
* The reference count starts at 1.
*
* To destroy a weston_color_transform, use weston_color_transfor_unref().
*/
WL_EXPORT void
weston_color_transform_init(struct weston_color_transform *xform,
struct weston_color_manager *cm)
{
xform->cm = cm;
xform->ref_count = 1;
wl_signal_init(&xform->destroy_signal);
}
/** Deep copy */
void
weston_surface_color_transform_copy(struct weston_surface_color_transform *dst,
const struct weston_surface_color_transform *src)
{
*dst = *src;
dst->transform = weston_color_transform_ref(src->transform);
}
/** Unref contents */
void
weston_surface_color_transform_fini(struct weston_surface_color_transform *surf_xform)
{
weston_color_transform_unref(surf_xform->transform);
}
/**
* Ensure that the surface's color transformation for the given output is
* populated in the paint nodes for all the views.
*
* Creates the color transformation description if necessary by calling
* into the color manager.
*
* \param pnode Paint node defining the surface and the output. All
* paint nodes with the same surface and output will be ensured.
*/
void
weston_paint_node_ensure_color_transform(struct weston_paint_node *pnode)
{
struct weston_surface *surface = pnode->surface;
struct weston_output *output = pnode->output;
struct weston_color_manager *cm = surface->compositor->color_manager;
struct weston_surface_color_transform surf_xform = {};
struct weston_paint_node *it;
bool ok;
/*
* Invariant: all paint nodes with the same surface+output have the
* same surf_xform state.
*/
if (pnode->surf_xform_valid)
return;
ok = cm->get_surface_color_transform(cm, surface, output, &surf_xform);
wl_list_for_each(it, &surface->paint_node_list, surface_link) {
if (it->output == output) {
assert(it->surf_xform_valid == false);
assert(it->surf_xform.transform == NULL);
weston_surface_color_transform_copy(&it->surf_xform,
&surf_xform);
it->surf_xform_valid = ok;
}
}
weston_surface_color_transform_fini(&surf_xform);
if (!ok) {
if (surface->resource)
wl_resource_post_no_memory(surface->resource);
weston_log("Failed to create color transformation for a surface.\n");
}
}

190
libweston/color.h Normal file
View File

@ -0,0 +1,190 @@
/*
* Copyright 2021 Collabora, Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef WESTON_COLOR_H
#define WESTON_COLOR_H
#include <stdbool.h>
#include <libweston/libweston.h>
/** Type or formula for a curve */
enum weston_color_curve_type {
/** Identity function, no-op */
WESTON_COLOR_CURVE_TYPE_IDENTITY = 0,
/** Three-channel, one-dimensional look-up table */
WESTON_COLOR_CURVE_TYPE_LUT_3x1D,
};
/** LUT_3x1D parameters */
struct weston_color_curve_lut_3x1d {
/* To be defined */
};
/**
* A scalar function for color encoding and decoding
*
* This object can represent a one-dimensional function that is applied
* independently to each of the color channels. Depending on the type and
* parameterization of the curve, all color channels may use the
* same function or each may have separate parameters.
*
* This is usually used for EOTF or EOTF^-1 and to optimize a 3D LUT size
* without sacrificing precision, both in one step.
*/
struct weston_color_curve {
/** Which member of 'u' defines the curve. */
enum weston_color_curve_type type;
/** Parameters for the curve. */
union {
/* identity: no parameters */
struct weston_color_curve_lut_3x1d lut_3x1d;
} u;
};
/**
* Describes a color transformation formula
*
* Guaranteed unique, de-duplicated.
*
* Sub-classed by the color manager that created this.
*
* For a renderer to support WESTON_CAP_COLOR_OPS it must implement everything
* that this structure can represent.
*/
struct weston_color_transform {
struct weston_color_manager *cm;
int ref_count;
/* for renderer or backend to attach their own cached objects */
struct wl_signal destroy_signal;
/* Color transform is the series of steps: */
/** Step 1: color model change */
/* YCbCr→RGB conversion, but that is done elsewhere */
/** Step 2: color curve before color mapping */
struct weston_color_curve pre_curve;
/** Step 3: color mapping */
/* TBD: e.g. a 3D LUT or a matrix */
/** Step 4: color curve after color mapping */
/* struct weston_color_curve post_curve; */
};
/**
* How content color needs to be transformed
*
* This object is specific to the color properties of the weston_surface and
* weston_output it was created for. It is automatically destroyed if any
* relevant color properties change.
*
* Fundamentally this contains the color transformation from content color
* space to an output's blending color space. This is stored in field
* 'transform' with NULL value corresponding to identity transformation.
*
* For graphics pipeline optimization purposes, the field 'identity_pipeline'
* indicates whether the combination of 'transform' here and the output's
* blending color space to monitor color space transformation total to
* identity transformation. This helps detecting cases where renderer bypass
* (direct scanout) is possible.
*/
struct weston_surface_color_transform {
/** Transformation from source to blending space */
struct weston_color_transform *transform;
/** True, if source colorspace is identical to monitor color space */
bool identity_pipeline;
};
struct weston_color_manager {
/** Identifies this CMS component */
const char *name;
/** This compositor instance */
struct weston_compositor *compositor;
/** Supports the Wayland CM&HDR protocol extension? */
bool supports_client_protocol;
/** Initialize color manager */
bool
(*init)(struct weston_color_manager *cm);
/** Destroy color manager */
void
(*destroy)(struct weston_color_manager *cm);
/** Destroy a color transform after refcount fell to zero */
void
(*destroy_color_transform)(struct weston_color_transform *xform);
/** Get surface to output's blending space transformation
*
* \param cm The color manager.
* \param surface The surface for the source color space.
* \param output The output for the destination blending color space.
* \param surf_xform For storing the color transformation and
* additional information.
*
* The callee is responsible for increasing the reference count on the
* weston_color_transform it stores into surf_xform.
*/
bool
(*get_surface_color_transform)(struct weston_color_manager *cm,
struct weston_surface *surface,
struct weston_output *output,
struct weston_surface_color_transform *surf_xform);
};
struct weston_color_transform *
weston_color_transform_ref(struct weston_color_transform *xform);
void
weston_color_transform_unref(struct weston_color_transform *xform);
void
weston_color_transform_init(struct weston_color_transform *xform,
struct weston_color_manager *cm);
void
weston_surface_color_transform_copy(struct weston_surface_color_transform *dst,
const struct weston_surface_color_transform *src);
void
weston_surface_color_transform_fini(struct weston_surface_color_transform *surf_xform);
struct weston_paint_node;
void
weston_paint_node_ensure_color_transform(struct weston_paint_node *pnode);
struct weston_color_manager *
weston_color_manager_noop_create(struct weston_compositor *compositor);
#endif /* WESTON_COLOR_H */

View File

@ -74,6 +74,7 @@
#include "pixel-formats.h"
#include "backend.h"
#include "libweston-internal.h"
#include "color.h"
#include "weston-log-internal.h"
@ -105,6 +106,7 @@ weston_paint_node_create(struct weston_surface *surface,
struct weston_output *output)
{
struct weston_paint_node *pnode;
struct weston_paint_node *existing_node;
assert(view->surface == surface);
@ -112,6 +114,21 @@ weston_paint_node_create(struct weston_surface *surface,
if (!pnode)
return NULL;
/*
* Invariant: all paint nodes with the same surface+output have the
* same surf_xform state.
*/
wl_list_for_each(existing_node, &view->paint_node_list, view_link) {
assert(existing_node->surface == surface);
if (existing_node->output != output)
continue;
weston_surface_color_transform_copy(&pnode->surf_xform,
&existing_node->surf_xform);
pnode->surf_xform_valid = existing_node->surf_xform_valid;
break;
}
pnode->surface = surface;
wl_list_insert(&surface->paint_node_list, &pnode->surface_link);
@ -134,6 +151,8 @@ weston_paint_node_destroy(struct weston_paint_node *pnode)
wl_list_remove(&pnode->view_link);
wl_list_remove(&pnode->output_link);
wl_list_remove(&pnode->z_order_link);
assert(pnode->surf_xform_valid || !pnode->surf_xform.transform);
weston_surface_color_transform_fini(&pnode->surf_xform);
free(pnode);
}
@ -2695,6 +2714,12 @@ add_to_z_order_list(struct weston_output *output,
wl_list_remove(&pnode->z_order_link);
wl_list_insert(output->paint_node_z_order_list.prev,
&pnode->z_order_link);
/*
* Building weston_output::paint_node_z_order_list ensures all
* necessary color transform objects are installed.
*/
weston_paint_node_ensure_color_transform(pnode);
}
static void
@ -7743,6 +7768,12 @@ weston_compositor_shutdown(struct weston_compositor *ec)
wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
output->destroy(output);
/* Color manager objects may have renderer hooks */
if (ec->color_manager) {
ec->color_manager->destroy(ec->color_manager);
ec->color_manager = NULL;
}
if (ec->renderer)
ec->renderer->destroy(ec);
@ -8172,6 +8203,19 @@ weston_compositor_load_backend(struct weston_compositor *compositor,
return -1;
}
if (!compositor->color_manager) {
compositor->color_manager =
weston_color_manager_noop_create(compositor);
}
if (!compositor->color_manager)
return -1;
if (!compositor->color_manager->init(compositor->color_manager))
return -1;
weston_log("Color manager: %s\n", compositor->color_manager->name);
return 0;
}

View File

@ -41,6 +41,7 @@
*/
#include <libweston/libweston.h>
#include "color.h"
/* weston_buffer */
@ -260,7 +261,6 @@ weston_surface_to_buffer_float(struct weston_surface *surface,
pixman_box32_t
weston_surface_to_buffer_rect(struct weston_surface *surface,
pixman_box32_t rect);
void
weston_surface_to_buffer_region(struct weston_surface *surface,
pixman_region32_t *surface_region,
@ -416,6 +416,9 @@ struct weston_paint_node {
/* struct weston_output::paint_node_z_order_list */
struct wl_list z_order_link;
struct weston_surface_color_transform surf_xform;
bool surf_xform_valid;
};
struct weston_paint_node *

View File

@ -12,6 +12,8 @@ srcs_libweston = [
'animation.c',
'bindings.c',
'clipboard.c',
'color.c',
'color-noop.c',
'compositor.c',
'content-protection.c',
'data-device.c',

View File

@ -33,6 +33,7 @@
#include <assert.h>
#include "pixman-renderer.h"
#include "color.h"
#include "shared/helpers.h"
#include <linux/input.h>
@ -478,6 +479,11 @@ draw_paint_node(struct weston_paint_node *pnode,
/* repaint bounding region in global coordinates: */
pixman_region32_t repaint;
if (!pnode->surf_xform_valid)
return;
assert(pnode->surf_xform.transform == NULL);
/* No buffer attached */
if (!ps->image)
return;

View File

@ -44,6 +44,7 @@
#include "linux-sync-file.h"
#include "timeline.h"
#include "color.h"
#include "gl-renderer.h"
#include "gl-renderer-internal.h"
#include "vertex-clipping.h"
@ -978,6 +979,10 @@ gl_shader_config_init_for_paint_node(struct gl_shader_config *sconf,
struct gl_surface_state *gs = get_surface_state(pnode->surface);
struct gl_output_state *go = get_output_state(pnode->output);
if (!pnode->surf_xform_valid)
return false;
assert(pnode->surf_xform.transform == NULL);
*sconf = (struct gl_shader_config) {
.projection = go->output_matrix,
.view_alpha = pnode->view->alpha,