You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
weston/libweston/color.c

352 lines
8.9 KiB

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>
4 years ago
/*
* Copyright 2019 Sebastian Wick
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>
4 years ago
* 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 <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <errno.h>
#include <string.h>
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>
4 years ago
#include "color.h"
#include "libweston-internal.h"
color: introduce weston_color_profile Roughly speaking, a color profile describes the color space of content or an output. Under the hood, the description includes one or more ways to map colors between the profile space and some standard profile connecting space (PCS). This object is not called a color space. A color space has a unique definition, while a color profile may contain multiple different mappings depending on render intent. Some of these mappings may be subjective, with an artistic touch. When a source color profile and a destination color profile are combined under a specific render intent, they produce a color transformation. Color transformations are already preresented by weston_color_transform. This patch adds the basic API for color profile objects. Everything worthwhile of these objects is implemented in the color managers: color-noop never creates these, and in color-lcms they are basically a container for cmsHPROFILE, the Little CMS object for color profiles. Color profile objects will not be interpreted outside of the color managers, unlike color transformations. For a start, the color manager API has one function to create color profiles: from ICC profile data. More creation functions for other sources will be added later. The API has errmsg return parameter for error messages. These are not simply weston_log()'d, because CM&HDR protocol will allow clients to trigger errors and the protocol handles that gracefully. Therefore instead of flooding the compositor logs, the error messages will probably need to be relayed back to clients. Color-lcms is expected to create a cmsHPROFILE for all kinds of color profiles, not just for those created from ICC profile data. Hence, color-lcms will fingerprint color profiles by the MD5 hash which Little CMS computes for us. The fingerprint is used for de-duplication: instead of creating copies, reference existing color profiles. This code is very much based on Sebastian Wick's earlier work on Weston color management, but structured and named differently. Co-authored-by: Sebastian Wick <sebastian@sebastianwick.net> Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.com>
4 years ago
/**
* Increase reference count of the color profile object
*
* \param cprof The color profile. NULL is accepted too.
* \return cprof.
*/
WL_EXPORT struct weston_color_profile *
weston_color_profile_ref(struct weston_color_profile *cprof)
{
/* NULL is a valid color space: sRGB */
if (!cprof)
return NULL;
assert(cprof->ref_count > 0);
cprof->ref_count++;
return cprof;
}
/**
* Decrease reference count and potentially destroy the color profile object
*
* \param cprof The color profile. NULL is accepted too.
*/
WL_EXPORT void
weston_color_profile_unref(struct weston_color_profile *cprof)
{
if (!cprof)
return;
assert(cprof->ref_count > 0);
if (--cprof->ref_count > 0)
return;
cprof->cm->destroy_color_profile(cprof);
}
/**
* Get color profile description
*
* A description of the profile is meant for human readable logs.
*
* \param cprof The color profile, NULL is accepted too.
* \returns The color profile description, valid as long as the
* color profile itself is.
*/
WL_EXPORT const char *
weston_color_profile_get_description(struct weston_color_profile *cprof)
{
if (cprof)
return cprof->description;
else
return "built-in default sRGB SDR profile";
}
/**
* Initializes a newly allocated color profile object
*
* This is used only by color managers. They sub-class weston_color_profile.
*
* The reference count starts at 1.
*
* To destroy a weston_color_profile, use weston_color_profile_unref().
*/
WL_EXPORT void
weston_color_profile_init(struct weston_color_profile *cprof,
struct weston_color_manager *cm)
{
cprof->cm = cm;
cprof->ref_count = 1;
}
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>
4 years ago
/**
* 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);
surf_xform->transform = NULL;
surf_xform->identity_pipeline = false;
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>
4 years ago
}
/**
* 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");
}
}
/**
* Load ICC profile file
*
* Loads an ICC profile file, ensures it is fit for use, and returns a
* new reference to the weston_color_profile. Use weston_color_profile_unref()
* to free it.
*
* \param compositor The compositor instance, identifies the color manager.
* \param path Path to the ICC file to be open()'d.
* \return A color profile reference, or NULL on failure.
*
* Error messages are printed to libweston log.
*
* This function is not meant for loading profiles on behalf of Wayland
* clients.
*/
WL_EXPORT struct weston_color_profile *
weston_compositor_load_icc_file(struct weston_compositor *compositor,
const char *path)
{
struct weston_color_manager *cm = compositor->color_manager;
struct weston_color_profile *cprof = NULL;
int fd;
struct stat icc_stat;
void *icc_data;
size_t len;
char *errmsg = NULL;
fd = open(path, O_RDONLY);
if (fd == -1) {
weston_log("Error: Cannot open ICC profile \"%s\" for reading: %s\n",
path, strerror(errno));
return NULL;
}
if (fstat(fd, &icc_stat) != 0) {
weston_log("Error: Cannot fstat ICC profile \"%s\": %s\n",
path, strerror(errno));
goto out_close;
}
len = icc_stat.st_size;
if (len < 1) {
weston_log("Error: ICC profile \"%s\" has no size.\n", path);
goto out_close;
}
icc_data = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
if (icc_data == MAP_FAILED) {
weston_log("Error: Cannot mmap ICC profile \"%s\": %s\n",
path, strerror(errno));
goto out_close;
}
if (!cm->get_color_profile_from_icc(cm, icc_data, len,
path, &cprof, &errmsg)) {
weston_log("Error: loading ICC profile \"%s\" failed: %s\n",
path, errmsg);
free(errmsg);
}
munmap(icc_data, len);
out_close:
close(fd);
return cprof;
}
/** Get a string naming the EOTF mode
*
* \internal
*/
WL_EXPORT const char *
weston_eotf_mode_to_str(enum weston_eotf_mode e)
{
switch (e) {
case WESTON_EOTF_MODE_NONE: return "(none)";
case WESTON_EOTF_MODE_SDR: return "SDR";
case WESTON_EOTF_MODE_TRADITIONAL_HDR: return "traditional gamma HDR";
case WESTON_EOTF_MODE_ST2084: return "ST2084";
case WESTON_EOTF_MODE_HLG: return "HLG";
}
return "???";
}
/** A list of EOTF modes as a string
*
* \param eotf_mask Bitwise-or'd enum weston_eotf_mode values.
* \return Comma separated names of the listed EOTF modes. Must be free()'d by
* the caller.
*/
WL_EXPORT char *
weston_eotf_mask_to_str(uint32_t eotf_mask)
{
FILE *fp;
char *str = NULL;
size_t size = 0;
unsigned i;
const char *sep = "";
fp = open_memstream(&str, &size);
if (!fp)
return NULL;
for (i = 0; eotf_mask; i++) {
uint32_t bitmask = 1u << i;
if (eotf_mask & bitmask) {
fprintf(fp, "%s%s", sep,
weston_eotf_mode_to_str(bitmask));
sep = ", ";
}
eotf_mask &= ~bitmask;
}
fclose(fp);
return str;
}