Skip to content
Commits on Source (5)
  • Pekka Paalanen's avatar
    color: introduce weston_color_profile · aa6346f2
    Pekka Paalanen authored and Pekka Paalanen's avatar Pekka Paalanen committed
    
    
    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: default avatarSebastian Wick <sebastian@sebastianwick.net>
    Signed-off-by: default avatarPekka Paalanen <pekka.paalanen@collabora.com>
    aa6346f2
  • Pekka Paalanen's avatar
    color: add weston_compositor_load_icc_file() · f45d5762
    Pekka Paalanen authored and Pekka Paalanen's avatar Pekka Paalanen committed
    
    
    This function will be useful for Weston to load output ICC profiles from
    weston.ini.
    
    Co-authored-by: default avatarSebastian Wick <sebastian@sebastianwick.net>
    Signed-off-by: default avatarPekka Paalanen <pekka.paalanen@collabora.com>
    f45d5762
  • Pekka Paalanen's avatar
    libweston: refactor into weston_output_set_color_transforms() · 9a0aa37e
    Pekka Paalanen authored and Pekka Paalanen's avatar Pekka Paalanen committed
    
    
    Move the code into a new function that either succeeds in setting all
    the color transformations or does not change anything. This will be
    useful when implementing output color profiles changes while the output
    is enabled.
    
    Signed-off-by: default avatarPekka Paalanen <pekka.paalanen@collabora.com>
    9a0aa37e
  • Pekka Paalanen's avatar
    libweston: add weston_output::color_profile · 9a9e6ced
    Pekka Paalanen authored and Pekka Paalanen's avatar Pekka Paalanen committed
    
    
    Add API to set an output's color profile. This new function can also be
    called while the output is enabled. This allows changing the output
    color profile even at runtime if desired.
    
    color-noop has no way of creating weston_color_profile objects, so it
    just asserts that no color profile is set.
    
    color-lcms does not yet implement taking the output color profile into
    account, so for now it just fails everything if a profile is set.
    
    weston_surface_color_transform_fini() was previously used only prior to
    freeing the struct, but now it is used also to just clear the struct,
    hence it needs to reset the fields.
    
    Signed-off-by: default avatarPekka Paalanen <pekka.paalanen@collabora.com>
    9a9e6ced
  • Pekka Paalanen's avatar
    compositor: add icc_profile weston.ini option for outputs · a8827940
    Pekka Paalanen authored and Pekka Paalanen's avatar Pekka Paalanen committed
    
    
    This adds "icc_profile" key support in [output] sections for backends
    headless, x11, wayland, and drm, and also for remoted and pipewire
    outputs FWIW. On the other hand, RDP-backend does not use output
    sections from weston.ini, and fbdev-backend does not deserve anything
    new (it wouldn't support color management anyway due to no GL-renderer).
    
    This allows one to configure an ICC v2 or v4 file to be used as an
    output profile. However, color-lcms does not actually use output
    profiles yet, so trying this will fail until support is implemented.
    
    The parent_winsys_profile argument is reserved for using the color
    profile from a parent window system where applicable, if nothing else is
    set in weston.ini. None of the nested backends provide an output color
    profile yet. It is more of a reminder of a missing feature than a
    serious implementation.
    
    Note: cms-static Weston plugin uses the exact same weston.ini key for
    loading VCGT from ICC profiles. If "color-management" option is set to
    false, this new use of "icc_profile" is disabled and the old behavior
    with cms-static is kept.
    
    Signed-off-by: default avatarPekka Paalanen <pekka.paalanen@collabora.com>
    a8827940
......@@ -126,6 +126,7 @@ struct wet_compositor {
struct wl_list child_process_list;
pid_t autolaunch_pid;
bool autolaunch_watch;
bool use_color_manager;
};
static FILE *weston_logfile = NULL;
......@@ -1095,6 +1096,7 @@ static int
weston_compositor_init_config(struct weston_compositor *ec,
struct weston_config *config)
{
struct wet_compositor *compositor = to_wet_compositor(ec);
struct xkb_rule_names xkb_names;
struct weston_config_section *s;
int repaint_msec;
......@@ -1143,6 +1145,8 @@ weston_compositor_init_config(struct weston_compositor *ec,
if (color_management) {
if (weston_compositor_load_color_manager(ec) < 0)
return -1;
else
compositor->use_color_manager = true;
}
/* weston.ini [libinput] */
......@@ -1302,6 +1306,48 @@ wet_output_set_transform(struct weston_output *output,
return 0;
}
static int
wet_output_set_color_profile(struct weston_output *output,
struct weston_config_section *section,
struct weston_color_profile *parent_winsys_profile)
{
struct wet_compositor *compositor = to_wet_compositor(output->compositor);
struct weston_color_profile *cprof;
char *icc_file = NULL;
bool ok;
if (!compositor->use_color_manager)
return 0;
if (section) {
weston_config_section_get_string(section, "icc_profile",
&icc_file, NULL);
}
if (icc_file) {
cprof = weston_compositor_load_icc_file(output->compositor,
icc_file);
free(icc_file);
} else if (parent_winsys_profile) {
cprof = weston_color_profile_ref(parent_winsys_profile);
} else {
return 0;
}
if (!cprof)
return -1;
ok = weston_output_set_color_profile(output, cprof);
if (!ok) {
weston_log("Error: failed to set color profile '%s' for output %s\n",
weston_color_profile_get_description(cprof),
output->name);
}
weston_color_profile_unref(cprof);
return ok ? 0 : -1;
}
static void
allow_content_protection(struct weston_output *output,
struct weston_config_section *section)
......@@ -1366,6 +1412,9 @@ wet_configure_windowed_output_from_config(struct weston_output *output,
return -1;
}
if (wet_output_set_color_profile(output, section, NULL) < 0)
return -1;
if (api->output_set_size(output, width, height) < 0) {
weston_log("Cannot configure output \"%s\" using weston_windowed_output_api.\n",
output->name);
......@@ -1806,6 +1855,9 @@ drm_backend_output_configure(struct weston_output *output,
return -1;
}
if (wet_output_set_color_profile(output, section, NULL) < 0)
return -1;
weston_config_section_get_string(section,
"gbm-format", &gbm_format, NULL);
......@@ -2305,6 +2357,9 @@ drm_backend_remoted_output_configure(struct weston_output *output,
return -1;
};
if (wet_output_set_color_profile(output, section, NULL) < 0)
return -1;
weston_config_section_get_string(section, "gbm-format", &gbm_format,
NULL);
api->set_gbm_format(output, gbm_format);
......@@ -2456,6 +2511,9 @@ drm_backend_pipewire_output_configure(struct weston_output *output,
return -1;
}
if (wet_output_set_color_profile(output, section, NULL) < 0)
return -1;
weston_config_section_get_string(section, "seat", &seat, "");
api->set_seat(output, seat);
......
......@@ -79,6 +79,7 @@ struct linux_dmabuf_buffer;
struct weston_recorder;
struct weston_pointer_constraint;
struct ro_anonymous_file;
struct weston_color_profile;
struct weston_color_transform;
enum weston_keyboard_modifier {
......@@ -373,6 +374,7 @@ struct weston_output {
bool enabled; /**< is in the output_list, not pending list */
int scale;
struct weston_color_profile *color_profile;
struct weston_color_transform *from_sRGB_to_output;
struct weston_color_transform *from_sRGB_to_blend;
struct weston_color_transform *from_blend_to_output;
......@@ -2082,6 +2084,10 @@ void
weston_output_set_transform(struct weston_output *output,
uint32_t transform);
bool
weston_output_set_color_profile(struct weston_output *output,
struct weston_color_profile *cprof);
void
weston_output_init(struct weston_output *output,
struct weston_compositor *compositor,
......@@ -2126,6 +2132,19 @@ void
weston_timeline_refresh_subscription_objects(struct weston_compositor *wc,
void *object);
struct weston_color_profile *
weston_color_profile_ref(struct weston_color_profile *cprof);
void
weston_color_profile_unref(struct weston_color_profile *cprof);
const char *
weston_color_profile_get_description(struct weston_color_profile *cprof);
struct weston_color_profile *
weston_compositor_load_icc_file(struct weston_compositor *compositor,
const char *path);
#ifdef __cplusplus
}
#endif
......
......@@ -57,6 +57,10 @@ cmlcms_get_surface_color_transform(struct weston_color_manager *cm_base,
};
struct cmlcms_color_transform *xform;
/* TODO: use output color profile */
if (output->color_profile)
return false;
xform = cmlcms_color_transform_get(cm, &param);
if (!xform)
return false;
......@@ -82,6 +86,10 @@ cmlcms_get_output_color_transform(struct weston_color_manager *cm_base,
};
struct cmlcms_color_transform *xform;
/* TODO: use output color profile */
if (output->color_profile)
return false;
xform = cmlcms_color_transform_get(cm, &param);
if (!xform)
return false;
......@@ -96,6 +104,11 @@ cmlcms_get_sRGB_to_output_color_transform(struct weston_color_manager *cm_base,
struct weston_color_transform **xform_out)
{
/* Assumes output color space is sRGB SDR */
/* TODO: use output color profile */
if (output->color_profile)
return false;
/* Identity transform */
*xform_out = NULL;
......@@ -114,6 +127,10 @@ cmlcms_get_sRGB_to_blend_color_transform(struct weston_color_manager *cm_base,
};
struct cmlcms_color_transform *xform;
/* TODO: use output color profile */
if (output->color_profile)
return false;
xform = cmlcms_color_transform_get(cm, &param);
if (!xform)
return false;
......@@ -159,6 +176,7 @@ cmlcms_destroy(struct weston_color_manager *cm_base)
struct weston_color_manager_lcms *cm = get_cmlcms(cm_base);
assert(wl_list_empty(&cm->color_transform_list));
assert(wl_list_empty(&cm->color_profile_list));
cmsDeleteContext(cm->lcms_ctx);
free(cm);
......@@ -178,6 +196,8 @@ weston_color_manager_create(struct weston_compositor *compositor)
cm->base.supports_client_protocol = true;
cm->base.init = cmlcms_init;
cm->base.destroy = cmlcms_destroy;
cm->base.destroy_color_profile = cmlcms_destroy_color_profile;
cm->base.get_color_profile_from_icc = cmlcms_get_color_profile_from_icc;
cm->base.destroy_color_transform = cmlcms_destroy_color_transform;
cm->base.get_surface_color_transform =
cmlcms_get_surface_color_transform;
......@@ -188,6 +208,7 @@ weston_color_manager_create(struct weston_compositor *compositor)
cmlcms_get_sRGB_to_blend_color_transform;
wl_list_init(&cm->color_transform_list);
wl_list_init(&cm->color_profile_list);
return &cm->base;
}
......@@ -38,6 +38,7 @@ struct weston_color_manager_lcms {
cmsContext lcms_ctx;
struct wl_list color_transform_list; /* cmlcms_color_transform::link */
struct wl_list color_profile_list; /* cmlcms_color_profile::link */
};
static inline struct weston_color_manager_lcms *
......@@ -46,6 +47,37 @@ get_cmlcms(struct weston_color_manager *cm_base)
return container_of(cm_base, struct weston_color_manager_lcms, base);
}
struct cmlcms_md5_sum {
uint8_t bytes[16];
};
struct cmlcms_color_profile {
struct weston_color_profile base;
/* struct weston_color_manager_lcms::color_profile_list */
struct wl_list link;
cmsHPROFILE profile;
struct cmlcms_md5_sum md5sum;
};
static inline struct cmlcms_color_profile *
get_cprof(struct weston_color_profile *cprof_base)
{
return container_of(cprof_base, struct cmlcms_color_profile, base);
}
bool
cmlcms_get_color_profile_from_icc(struct weston_color_manager *cm,
const void *icc_data,
size_t icc_len,
const char *name_part,
struct weston_color_profile **cprof_out,
char **errmsg);
void
cmlcms_destroy_color_profile(struct weston_color_profile *cprof_base);
/*
* Perhaps a placeholder, until we get actual color spaces involved and
* see how this would work better.
......
/*
* Copyright 2019 Sebastian Wick
* 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 <assert.h>
#include <stdio.h>
#include <string.h>
#include <libweston/libweston.h>
#include "color.h"
#include "color-lcms.h"
#include "shared/helpers.h"
#include "shared/string-helpers.h"
/* FIXME: sync with spec! */
static bool
validate_icc_profile(cmsHPROFILE profile, char **errmsg)
{
cmsColorSpaceSignature cs = cmsGetColorSpace(profile);
uint32_t nr_channels = cmsChannelsOf(cs);
uint8_t version = cmsGetEncodedICCversion(profile) >> 24;
if (version != 2 && version != 4) {
str_printf(errmsg,
"ICC profile major version %d is unsupported, should be 2 or 4.",
version);
return false;
}
if (nr_channels != 3) {
str_printf(errmsg,
"ICC profile must contain 3 channels for the color space, not %u.",
nr_channels);
return false;
}
if (cmsGetDeviceClass(profile) != cmsSigDisplayClass) {
str_printf(errmsg, "ICC profile is required to be of Display device class, but it is not.");
return false;
}
return true;
}
static struct cmlcms_color_profile *
cmlcms_find_color_profile_by_md5(const struct weston_color_manager_lcms *cm,
const struct cmlcms_md5_sum *md5sum)
{
struct cmlcms_color_profile *cprof;
wl_list_for_each(cprof, &cm->color_profile_list, link) {
if (memcmp(cprof->md5sum.bytes,
md5sum->bytes, sizeof(md5sum->bytes)) == 0)
return cprof;
}
return NULL;
}
static struct cmlcms_color_profile *
cmlcms_color_profile_create(struct weston_color_manager_lcms *cm,
cmsHPROFILE profile,
char *desc,
char **errmsg)
{
struct cmlcms_color_profile *cprof;
cprof = zalloc(sizeof *cprof);
if (!cprof)
return NULL;
weston_color_profile_init(&cprof->base, &cm->base);
cprof->base.description = desc;
cprof->profile = profile;
cmsGetHeaderProfileID(profile, cprof->md5sum.bytes);
wl_list_insert(&cm->color_profile_list, &cprof->link);
return cprof;
}
static void
cmlcms_color_profile_destroy(struct cmlcms_color_profile *cprof)
{
wl_list_remove(&cprof->link);
cmsCloseProfile(cprof->profile);
free(cprof->base.description);
free(cprof);
}
static char *
make_icc_file_description(cmsHPROFILE profile,
const struct cmlcms_md5_sum *md5sum,
const char *name_part)
{
char md5sum_str[sizeof(md5sum->bytes) * 2 + 1];
char *desc;
size_t i;
for (i = 0; i < sizeof(md5sum->bytes); i++) {
snprintf(md5sum_str + 2 * i, sizeof(md5sum_str) - 2 * i,
"%02x", md5sum->bytes[i]);
}
str_printf(&desc, "ICCv%f %s %s", cmsGetProfileVersion(profile),
name_part, md5sum_str);
return desc;
}
bool
cmlcms_get_color_profile_from_icc(struct weston_color_manager *cm_base,
const void *icc_data,
size_t icc_len,
const char *name_part,
struct weston_color_profile **cprof_out,
char **errmsg)
{
struct weston_color_manager_lcms *cm = get_cmlcms(cm_base);
cmsHPROFILE profile;
struct cmlcms_md5_sum md5sum;
struct cmlcms_color_profile *cprof;
char *desc = NULL;
if (!icc_data || icc_len < 1) {
str_printf(errmsg, "No ICC data.");
return false;
}
if (icc_len >= UINT32_MAX) {
str_printf(errmsg, "Too much ICC data.");
return false;
}
profile = cmsOpenProfileFromMemTHR(cm->lcms_ctx, icc_data, icc_len);
if (!profile) {
str_printf(errmsg, "ICC data not understood.");
return false;
}
if (!validate_icc_profile(profile, errmsg))
goto err_close;
if (!cmsMD5computeID(profile)) {
str_printf(errmsg, "Failed to compute MD5 for ICC profile.");
goto err_close;
}
cmsGetHeaderProfileID(profile, md5sum.bytes);
cprof = cmlcms_find_color_profile_by_md5(cm, &md5sum);
if (cprof) {
*cprof_out = weston_color_profile_ref(&cprof->base);
cmsCloseProfile(profile);
return true;
}
desc = make_icc_file_description(profile, &md5sum, name_part);
if (!desc)
goto err_close;
cprof = cmlcms_color_profile_create(cm, profile, desc, errmsg);
if (!cprof)
goto err_close;
*cprof_out = &cprof->base;
return true;
err_close:
free(desc);
cmsCloseProfile(profile);
return false;
}
void
cmlcms_destroy_color_profile(struct weston_color_profile *cprof_base)
{
struct cmlcms_color_profile *cprof = get_cprof(cprof_base);
cmlcms_color_profile_destroy(cprof);
}
......@@ -9,6 +9,7 @@ endif
srcs_color_lcms = [
'color-lcms.c',
'color-profile.c',
'color-transform.c',
]
......
......@@ -29,6 +29,7 @@
#include "color.h"
#include "shared/helpers.h"
#include "shared/string-helpers.h"
struct weston_color_manager_noop {
struct weston_color_manager base;
......@@ -40,6 +41,24 @@ get_cmnoop(struct weston_color_manager *cm_base)
return container_of(cm_base, struct weston_color_manager_noop, base);
}
static void
cmnoop_destroy_color_profile(struct weston_color_profile *cprof)
{
/* Never called, as never creates an actual color profile. */
}
static bool
cmnoop_get_color_profile_from_icc(struct weston_color_manager *cm,
const void *icc_data,
size_t icc_len,
const char *name_part,
struct weston_color_profile **cprof_out,
char **errmsg)
{
str_printf(errmsg, "ICC profiles are unsupported.");
return false;
}
static void
cmnoop_destroy_color_transform(struct weston_color_transform *xform)
{
......@@ -53,7 +72,7 @@ cmnoop_get_surface_color_transform(struct weston_color_manager *cm_base,
struct weston_surface_color_transform *surf_xform)
{
/* TODO: Assert surface has no colorspace set */
/* TODO: Assert output has no colorspace set */
assert(output->color_profile == NULL);
/* Identity transform */
surf_xform->transform = NULL;
......@@ -67,7 +86,7 @@ cmnoop_get_output_color_transform(struct weston_color_manager *cm_base,
struct weston_output *output,
struct weston_color_transform **xform_out)
{
/* TODO: Assert output has no colorspace set */
assert(output->color_profile == NULL);
/* Identity transform */
*xform_out = NULL;
......@@ -80,7 +99,7 @@ cmnoop_get_sRGB_to_output_color_transform(struct weston_color_manager *cm_base,
struct weston_output *output,
struct weston_color_transform **xform_out)
{
/* TODO: Assert output has no colorspace set */
assert(output->color_profile == NULL);
/* Identity transform */
*xform_out = NULL;
......@@ -93,7 +112,7 @@ cmnoop_get_sRGB_to_blend_color_transform(struct weston_color_manager *cm_base,
struct weston_output *output,
struct weston_color_transform **xform_out)
{
/* TODO: Assert output has no colorspace set */
assert(output->color_profile == NULL);
/* Identity transform */
*xform_out = NULL;
......@@ -131,6 +150,8 @@ weston_color_manager_noop_create(struct weston_compositor *compositor)
cm->base.supports_client_protocol = false;
cm->base.init = cmnoop_init;
cm->base.destroy = cmnoop_destroy;
cm->base.destroy_color_profile = cmnoop_destroy_color_profile;
cm->base.get_color_profile_from_icc = cmnoop_get_color_profile_from_icc;
cm->base.destroy_color_transform = cmnoop_destroy_color_transform;
cm->base.get_surface_color_transform =
cmnoop_get_surface_color_transform;
......
/*
* Copyright 2019 Sebastian Wick
* Copyright 2021 Collabora, Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining
......@@ -27,10 +28,88 @@
#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>
#include "color.h"
#include "libweston-internal.h"
/**
* 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;
}
/**
* Increase reference count of the color transform object
*
......@@ -100,6 +179,8 @@ 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;
}
/**
......@@ -149,3 +230,70 @@ weston_paint_node_ensure_color_transform(struct weston_paint_node *pnode)
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;
}
......@@ -27,8 +27,20 @@
#define WESTON_COLOR_H
#include <stdbool.h>
#include <stdint.h>
#include <libweston/libweston.h>
/**
* Represents a color profile description (an ICC color profile)
*
* Sub-classed by the color manager that created this.
*/
struct weston_color_profile {
struct weston_color_manager *cm;
int ref_count;
char *description;
};
/** Type or formula for a curve */
enum weston_color_curve_type {
/** Identity function, no-op */
......@@ -161,6 +173,35 @@ struct weston_color_manager {
void
(*destroy)(struct weston_color_manager *cm);
/** Destroy a color profile after refcount fell to zero */
void
(*destroy_color_profile)(struct weston_color_profile *cprof);
/** Create a color profile from ICC data
*
* \param cm The color manager.
* \param icc_data Pointer to the ICC binary data.
* \param icc_len Length of the ICC data in bytes.
* \param name_part A string to be used in describing the profile.
* \param cprof_out On success, the created object is returned here.
* On failure, untouched.
* \param errmsg On success, untouched. On failure, a pointer to a
* string describing the error is stored here. The string must be
* free()'d.
* \return True on success, false on failure.
*
* This may return a new reference to an existing color profile if
* that profile is identical to the one that would be created, apart
* from name_part.
*/
bool
(*get_color_profile_from_icc)(struct weston_color_manager *cm,
const void *icc_data,
size_t icc_len,
const char *name_part,
struct weston_color_profile **cprof_out,
char **errmsg);
/** Destroy a color transform after refcount fell to zero */
void
(*destroy_color_transform)(struct weston_color_transform *xform);
......@@ -231,6 +272,10 @@ struct weston_color_manager {
struct weston_color_transform **xform_out);
};
void
weston_color_profile_init(struct weston_color_profile *cprof,
struct weston_color_manager *cm);
struct weston_color_transform *
weston_color_transform_ref(struct weston_color_transform *xform);
......
......@@ -6273,6 +6273,42 @@ weston_output_reset_color_transforms(struct weston_output *output)
output->from_blend_to_output = NULL;
}
static bool
weston_output_set_color_transforms(struct weston_output *output)
{
struct weston_color_manager *cm = output->compositor->color_manager;
struct weston_color_transform *blend_to_output = NULL;
struct weston_color_transform *sRGB_to_output = NULL;
struct weston_color_transform *sRGB_to_blend = NULL;
bool ok;
ok = cm->get_output_color_transform(cm, output, &blend_to_output);
ok = ok && cm->get_sRGB_to_output_color_transform(cm, output,
&sRGB_to_output);
ok = ok && cm->get_sRGB_to_blend_color_transform(cm, output,
&sRGB_to_blend);
if (!ok) {
weston_log("Creating color transformation for output \"%s\" failed.\n",
output->name);
weston_color_transform_unref(blend_to_output);
weston_color_transform_unref(sRGB_to_output);
weston_color_transform_unref(sRGB_to_blend);
return false;
}
weston_output_reset_color_transforms(output);
output->from_blend_to_output = blend_to_output;
output->from_blend_to_output_by_backend = false;
output->from_sRGB_to_output = sRGB_to_output;
output->from_sRGB_to_blend = sRGB_to_blend;
weston_log("Output '%s' using color profile: %s\n", output->name,
weston_color_profile_get_description(output->color_profile));
return true;
}
/** Removes output from compositor's list of enabled outputs
*
* \param output The weston_output object that is being removed.
......@@ -6457,6 +6493,52 @@ weston_output_set_transform(struct weston_output *output,
}
}
/** Set output's color profile
*
* \param output The output to change.
* \param cprof The color profile to set. Can be NULL for default sRGB profile.
* \return True on success, or false on failure.
*
* Calling this function changes the color profile of the output. This causes
* all existing weston_color_transform objects related to this output via
* paint nodes to be unreferenced and later re-created on demand.
*
* This function may not be called from within weston_output_repaint().
*
* On failure, nothing is changed.
*
* \ingroup output
*/
WL_EXPORT bool
weston_output_set_color_profile(struct weston_output *output,
struct weston_color_profile *cprof)
{
struct weston_color_profile *old;
struct weston_paint_node *pnode;
old = output->color_profile;
output->color_profile = weston_color_profile_ref(cprof);
if (output->enabled) {
if (!weston_output_set_color_transforms(output)) {
/* Failed, roll back */
weston_color_profile_unref(output->color_profile);
output->color_profile = old;
return false;
}
/* Remove outdated cached color transformations */
wl_list_for_each(pnode, &output->paint_node_list, output_link) {
weston_surface_color_transform_fini(&pnode->surf_xform);
pnode->surf_xform_valid = false;
}
}
weston_color_profile_unref(old);
return true;
}
/** Initializes a weston_output object with enough data so
** an output can be configured.
*
......@@ -6588,12 +6670,10 @@ WL_EXPORT int
weston_output_enable(struct weston_output *output)
{
struct weston_compositor *c = output->compositor;
struct weston_color_manager *cm = c->color_manager;
struct weston_output *iterator;
struct weston_head *head;
char *head_names;
int x = 0, y = 0;
bool ok;
if (output->enabled) {
weston_log("Error: attempt to enable an enabled output '%s'\n",
......@@ -6649,19 +6729,8 @@ weston_output_enable(struct weston_output *output)
wl_list_init(&output->paint_node_list);
wl_list_init(&output->paint_node_z_order_list);
ok = cm->get_output_color_transform(cm, output,
&output->from_blend_to_output);
ok = ok && cm->get_sRGB_to_output_color_transform(cm, output,
&output->from_sRGB_to_output);
ok = ok && cm->get_sRGB_to_blend_color_transform(cm, output,
&output->from_sRGB_to_blend);
if (!ok) {
weston_log("Creating color transformation for output \"%s\" failed.\n",
output->name);
weston_output_reset_color_transforms(output);
if (!weston_output_set_color_transforms(output))
return -1;
}
output->from_blend_to_output_by_backend = false;
/* Enable the output (set up the crtc or create a
* window representing the output, set up the
......@@ -6828,6 +6897,8 @@ weston_output_release(struct weston_output *output)
if (output->enabled)
weston_compositor_remove_output(output);
weston_color_profile_unref(output->color_profile);
pixman_region32_fini(&output->region);
wl_list_remove(&output->link);
......
......@@ -545,6 +545,14 @@ An integer, 1 by default, typically configured as 2 or higher when needed,
denoting the scaling multiplier for the output.
.RE
.TP 7
.BI "icc_profile=" file
If option
.B color-management
is true, load the given ICC file as the output color profile. This works only
on DRM, headless, wayland, and x11 backends, and for remoting and pipewire
outputs.
.RE
.TP 7
.BI "seat=" name
The logical seat name that this output should be associated with. If this
is set then the seat's input will be confined to the output that has the seat
......