From a149c2370afa995968fde78721c36bb59e8e4b59 Mon Sep 17 00:00:00 2001 From: emersion Date: Sun, 22 Jul 2018 13:23:32 +0100 Subject: [PATCH 01/13] Implement wlr-gamma-control-unstable-v1 --- backend/drm/atomic.c | 12 +- backend/drm/drm.c | 6 +- include/wlr/interfaces/wlr_output.h | 2 +- include/wlr/types/wlr_gamma_control_v1.h | 31 +++ include/wlr/types/wlr_output.h | 2 +- protocol/meson.build | 1 + protocol/wlr-gamma-control-unstable-v1.xml | 126 +++++++++++ types/meson.build | 1 + types/wlr_gamma_control.c | 4 +- types/wlr_gamma_control_v1.c | 244 +++++++++++++++++++++ types/wlr_output.c | 9 +- 11 files changed, 424 insertions(+), 14 deletions(-) create mode 100644 include/wlr/types/wlr_gamma_control_v1.h create mode 100644 protocol/wlr-gamma-control-unstable-v1.xml create mode 100644 types/wlr_gamma_control_v1.c diff --git a/backend/drm/atomic.c b/backend/drm/atomic.c index 61d2f6e1..40a272a1 100644 --- a/backend/drm/atomic.c +++ b/backend/drm/atomic.c @@ -199,14 +199,18 @@ static bool atomic_crtc_move_cursor(struct wlr_drm_backend *drm, static bool atomic_crtc_set_gamma(struct wlr_drm_backend *drm, struct wlr_drm_crtc *crtc, uint16_t *r, uint16_t *g, uint16_t *b, uint32_t size) { - struct drm_color_lut gamma[size]; - // Fallback to legacy gamma interface when gamma properties are not available // (can happen on older intel gpu's that support gamma but not degamma) if (crtc->props.gamma_lut == 0) { return legacy_iface.crtc_set_gamma(drm, crtc, r, g, b, size); } + struct drm_color_lut *gamma = malloc(size * sizeof(struct drm_color_lut)); + if (gamma == NULL) { + wlr_log(WLR_ERROR, "Failed to allocate gamma table"); + return false; + } + for (uint32_t i = 0; i < size; i++) { gamma[i].red = r[i]; gamma[i].green = g[i]; @@ -218,10 +222,12 @@ static bool atomic_crtc_set_gamma(struct wlr_drm_backend *drm, } if (drmModeCreatePropertyBlob(drm->fd, gamma, - sizeof(struct drm_color_lut) * size, &crtc->gamma_lut)) { + size * sizeof(struct drm_color_lut), &crtc->gamma_lut)) { + free(gamma); wlr_log_errno(WLR_ERROR, "Unable to create property blob"); return false; } + free(gamma); struct atomic atom; atomic_begin(crtc, &atom); diff --git a/backend/drm/drm.c b/backend/drm/drm.c index c4674235..3e6659f1 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -228,19 +228,19 @@ static bool drm_connector_swap_buffers(struct wlr_output *output, return true; } -static void drm_connector_set_gamma(struct wlr_output *output, +static bool drm_connector_set_gamma(struct wlr_output *output, uint32_t size, uint16_t *r, uint16_t *g, uint16_t *b) { struct wlr_drm_connector *conn = (struct wlr_drm_connector *)output; struct wlr_drm_backend *drm = (struct wlr_drm_backend *)output->backend; - bool ok; + bool ok = false; if (conn->crtc) { ok = drm->iface->crtc_set_gamma(drm, conn->crtc, r, g, b, size); if (ok) { wlr_output_update_needs_swap(output); } } - + return ok; } static uint32_t drm_connector_get_gamma_size(struct wlr_output *output) { diff --git a/include/wlr/interfaces/wlr_output.h b/include/wlr/interfaces/wlr_output.h index 2224932f..4860a5b6 100644 --- a/include/wlr/interfaces/wlr_output.h +++ b/include/wlr/interfaces/wlr_output.h @@ -28,7 +28,7 @@ struct wlr_output_impl { void (*destroy)(struct wlr_output *output); bool (*make_current)(struct wlr_output *output, int *buffer_age); bool (*swap_buffers)(struct wlr_output *output, pixman_region32_t *damage); - void (*set_gamma)(struct wlr_output *output, + bool (*set_gamma)(struct wlr_output *output, uint32_t size, uint16_t *r, uint16_t *g, uint16_t *b); uint32_t (*get_gamma_size)(struct wlr_output *output); bool (*export_dmabuf)(struct wlr_output *output, diff --git a/include/wlr/types/wlr_gamma_control_v1.h b/include/wlr/types/wlr_gamma_control_v1.h new file mode 100644 index 00000000..5a173323 --- /dev/null +++ b/include/wlr/types/wlr_gamma_control_v1.h @@ -0,0 +1,31 @@ +#ifndef WLR_TYPES_WLR_GAMMA_CONTROL_V1_H +#define WLR_TYPES_WLR_GAMMA_CONTROL_V1_H + +#include + +struct wlr_gamma_control_manager_v1 { + struct wl_global *global; + struct wl_list resources; + struct wl_list controls; // wlr_gamma_control_v1::link + + struct wl_listener display_destroy; + + void *data; +}; + +struct wlr_gamma_control_v1 { + struct wl_resource *resource; + struct wlr_output *output; + struct wl_list link; + + struct wl_listener output_destroy_listener; + + void *data; +}; + +struct wlr_gamma_control_manager_v1 *wlr_gamma_control_manager_v1_create( + struct wl_display *display); +void wlr_gamma_control_manager_v1_destroy( + struct wlr_gamma_control_manager_v1 *manager); + +#endif diff --git a/include/wlr/types/wlr_output.h b/include/wlr/types/wlr_output.h index f8452d12..0cf3e9f6 100644 --- a/include/wlr/types/wlr_output.h +++ b/include/wlr/types/wlr_output.h @@ -174,7 +174,7 @@ bool wlr_output_swap_buffers(struct wlr_output *output, struct timespec *when, * it is a no-op. */ void wlr_output_schedule_frame(struct wlr_output *output); -void wlr_output_set_gamma(struct wlr_output *output, +bool wlr_output_set_gamma(struct wlr_output *output, uint32_t size, uint16_t *r, uint16_t *g, uint16_t *b); uint32_t wlr_output_get_gamma_size(struct wlr_output *output); bool wlr_output_export_dmabuf(struct wlr_output *output, diff --git a/protocol/meson.build b/protocol/meson.build index ffdf01a8..23da1afc 100644 --- a/protocol/meson.build +++ b/protocol/meson.build @@ -42,6 +42,7 @@ protocols = [ 'server-decoration.xml', 'virtual-keyboard-unstable-v1.xml', 'wlr-export-dmabuf-unstable-v1.xml', + 'wlr-gamma-control-unstable-v1.xml', 'wlr-input-inhibitor-unstable-v1.xml', 'wlr-layer-shell-unstable-v1.xml', 'wlr-screencopy-unstable-v1.xml', diff --git a/protocol/wlr-gamma-control-unstable-v1.xml b/protocol/wlr-gamma-control-unstable-v1.xml new file mode 100644 index 00000000..f55f3fe5 --- /dev/null +++ b/protocol/wlr-gamma-control-unstable-v1.xml @@ -0,0 +1,126 @@ + + + + Copyright © 2015 Giulio camuffo + Copyright © 2018 Simon Ser + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that copyright notice and this permission + notice appear in supporting documentation, and that the name of + the copyright holders not be used in advertising or publicity + pertaining to distribution of the software without specific, + written prior permission. The copyright holders make no + representations about the suitability of this software for any + purpose. It is provided "as is" without express or implied + warranty. + + THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN + AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, + ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF + THIS SOFTWARE. + + + + This protocol allows a privileged client to set the gamma tables for + outputs. + + Warning! The protocol described in this file is experimental and + backward incompatible changes may be made. Backward compatible changes + may be added together with the corresponding interface version bump. + Backward incompatible changes are done by bumping the version number in + the protocol and interface names and resetting the interface version. + Once the protocol is to be declared stable, the 'z' prefix and the + version number in the protocol and interface names are removed and the + interface version number is reset. + + + + + This interface is a manager that allows creating per-output gamma + controls. + + + + + Create a gamma control that can be used to adjust gamma tables for the + provided output. + + + + + + + + All objects created by the manager will still remain valid, until their + appropriate destroy request has been called. + + + + + + + This interface allows a client to adjust gamma tables for a particular + output. + + The client will receive the gamma size, and will then be able to set gamma + tables. At any time the compositor can send a failed event indicating that + this object is no longer valid. + + There must always be at most one gamma control object per output, which + has exclusive access to this particular output. When the gamma control + object is destroyed, the gamma table is restored to its original value. + + + + + Advertise the size of each gamma ramp. + + This event is sent immediately when the gamma control object is created. + + + + + + + + + + + Set the gamma table. The file descriptor can be memory-mapped to provide + the raw gamma table, which contains successive gamma ramps for the red, + green and blue channels. Each gamma ramp is an array of 16-byte unsigned + integers which has the same length as the gamma size. + + The file descriptor data must have the same length as three times the + gamma size. + + + + + + + This event indicates that the gamma control is no longer valid. This + can happen for a number of reasons, including: + - The output doesn't support gamma tables + - Setting the gamma tables failed + - Another client already has exclusive gamma control for this output + - The compositor has transfered gamma control to another client + + Upon receiving this event, the client should destroy this object. + + + + + + Destroys the gamma control object. If the object is still valid, this + restores the original gamma tables. + + + + diff --git a/types/meson.build b/types/meson.build index 64188a39..b37d551b 100644 --- a/types/meson.build +++ b/types/meson.build @@ -25,6 +25,7 @@ lib_wlr_types = static_library( 'wlr_cursor.c', 'wlr_export_dmabuf_v1.c', 'wlr_gamma_control.c', + 'wlr_gamma_control_v1.c', 'wlr_idle_inhibit_v1.c', 'wlr_idle.c', 'wlr_input_device.c', diff --git a/types/wlr_gamma_control.c b/types/wlr_gamma_control.c index 5b871335..8dbe1c22 100644 --- a/types/wlr_gamma_control.c +++ b/types/wlr_gamma_control.c @@ -25,7 +25,7 @@ static void gamma_control_destroy(struct wlr_gamma_control *gamma_control) { static const struct gamma_control_interface gamma_control_impl; -struct wlr_gamma_control *gamma_control_from_resource( +static struct wlr_gamma_control *gamma_control_from_resource( struct wl_resource *resource) { assert(wl_resource_instance_of(resource, &gamma_control_interface, &gamma_control_impl)); @@ -83,7 +83,7 @@ static const struct gamma_control_interface gamma_control_impl = { static const struct gamma_control_manager_interface gamma_control_manager_impl; -struct wlr_gamma_control_manager *gamma_control_manager_from_resource( +static struct wlr_gamma_control_manager *gamma_control_manager_from_resource( struct wl_resource *resource) { assert(wl_resource_instance_of(resource, &gamma_control_manager_interface, &gamma_control_manager_impl)); diff --git a/types/wlr_gamma_control_v1.c b/types/wlr_gamma_control_v1.c new file mode 100644 index 00000000..36d9296b --- /dev/null +++ b/types/wlr_gamma_control_v1.c @@ -0,0 +1,244 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "wlr-gamma-control-unstable-v1-protocol.h" +#include "util/signal.h" + +#define GAMMA_CONTROL_MANAGER_V1_VERSION 1 + +static void gamma_control_handle_destroy(struct wl_client *client, + struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static void gamma_control_destroy(struct wlr_gamma_control_v1 *gamma_control) { + if (gamma_control == NULL) { + return; + } + // TODO: reset gamma table + wl_resource_set_user_data(gamma_control->resource, NULL); + wl_list_remove(&gamma_control->output_destroy_listener.link); + wl_list_remove(&gamma_control->link); + free(gamma_control); +} + +static const struct zwlr_gamma_control_v1_interface gamma_control_impl; + +static struct wlr_gamma_control_v1 *gamma_control_from_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, &zwlr_gamma_control_v1_interface, + &gamma_control_impl)); + return wl_resource_get_user_data(resource); +} + +static void gamma_control_handle_resource_destroy(struct wl_resource *resource) { + struct wlr_gamma_control_v1 *gamma_control = + gamma_control_from_resource(resource); + gamma_control_destroy(gamma_control); +} + +static void gamma_control_handle_output_destroy(struct wl_listener *listener, + void *data) { + struct wlr_gamma_control_v1 *gamma_control = + wl_container_of(listener, gamma_control, output_destroy_listener); + gamma_control_destroy(gamma_control); +} + +static void gamma_control_handle_set_gamma(struct wl_client *client, + struct wl_resource *gamma_control_resource, int fd) { + struct wlr_gamma_control_v1 *gamma_control = + gamma_control_from_resource(gamma_control_resource); + if (gamma_control == NULL) { + close(fd); + return; + } + + uint32_t ramp_size = wlr_output_get_gamma_size(gamma_control->output); + size_t table_size = ramp_size * 3 * sizeof(uint16_t); + + off_t fd_size = lseek(fd, 0, SEEK_END); + // Skip checks if kernel does no support seek on buffer + if (fd_size != -1 && (size_t)fd_size != table_size) { + close(fd); + wl_resource_post_error(gamma_control_resource, + ZWLR_GAMMA_CONTROL_V1_ERROR_INVALID_GAMMA, + "The gamma ramps don't have the correct size"); + return; + } + lseek(fd, 0, SEEK_SET); + + // Use the heap since gamma tables can be large + uint16_t *table = malloc(table_size); + if (table == NULL) { + close(fd); + wl_resource_post_no_memory(gamma_control_resource); + return; + } + + ssize_t n_read = read(fd, table, table_size); + close(fd); + if (n_read == -1 || (size_t)n_read != table_size) { + free(table); + zwlr_gamma_control_v1_send_failed(gamma_control->resource); + gamma_control_destroy(gamma_control); + return; + } + + uint16_t *r = table; + uint16_t *g = table + ramp_size; + uint16_t *b = table + 2 * ramp_size; + + bool ok = wlr_output_set_gamma(gamma_control->output, ramp_size, r, g, b); + free(table); + if (!ok) { + zwlr_gamma_control_v1_send_failed(gamma_control->resource); + gamma_control_destroy(gamma_control); + return; + } +} + +static const struct zwlr_gamma_control_v1_interface gamma_control_impl = { + .destroy = gamma_control_handle_destroy, + .set_gamma = gamma_control_handle_set_gamma, +}; + +static const struct zwlr_gamma_control_manager_v1_interface + gamma_control_manager_impl; + +static struct wlr_gamma_control_manager_v1 *gamma_control_manager_from_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, + &zwlr_gamma_control_manager_v1_interface, &gamma_control_manager_impl)); + return wl_resource_get_user_data(resource); +} + +static void gamma_control_manager_get_gamma_control(struct wl_client *client, + struct wl_resource *manager_resource, uint32_t id, + struct wl_resource *output_resource) { + struct wlr_gamma_control_manager_v1 *manager = + gamma_control_manager_from_resource(manager_resource); + struct wlr_output *output = wlr_output_from_resource(output_resource); + + struct wlr_gamma_control_v1 *gamma_control = + calloc(1, sizeof(struct wlr_gamma_control_v1)); + if (gamma_control == NULL) { + wl_client_post_no_memory(client); + return; + } + gamma_control->output = output; + + uint32_t version = wl_resource_get_version(manager_resource); + gamma_control->resource = wl_resource_create(client, + &zwlr_gamma_control_v1_interface, version, id); + if (gamma_control->resource == NULL) { + free(gamma_control); + wl_client_post_no_memory(client); + return; + } + wl_resource_set_implementation(gamma_control->resource, &gamma_control_impl, + gamma_control, gamma_control_handle_resource_destroy); + + wl_signal_add(&output->events.destroy, + &gamma_control->output_destroy_listener); + gamma_control->output_destroy_listener.notify = + gamma_control_handle_output_destroy; + + wl_list_insert(&manager->controls, &gamma_control->link); + + if (!output->impl->set_gamma) { + zwlr_gamma_control_v1_send_failed(gamma_control->resource); + gamma_control_destroy(gamma_control); + return; + } + + struct wlr_gamma_control_v1 *gc; + wl_list_for_each(gc, &manager->controls, link) { + if (gc->output == output) { + zwlr_gamma_control_v1_send_failed(gc->resource); + gamma_control_destroy(gc); + return; + } + } + + zwlr_gamma_control_v1_send_gamma_size(gamma_control->resource, + wlr_output_get_gamma_size(output)); +} + +static const struct zwlr_gamma_control_manager_v1_interface + gamma_control_manager_impl = { + .get_gamma_control = gamma_control_manager_get_gamma_control, +}; + +static void gamma_control_manager_handle_resource_destroy( + struct wl_resource *resource) { + wl_list_remove(wl_resource_get_link(resource)); +} + +static void gamma_control_manager_bind(struct wl_client *client, void *data, + uint32_t version, uint32_t id) { + struct wlr_gamma_control_manager_v1 *manager = data; + + struct wl_resource *resource = wl_resource_create(client, + &zwlr_gamma_control_manager_v1_interface, version, id); + if (resource == NULL) { + wl_client_post_no_memory(client); + return; + } + wl_resource_set_implementation(resource, &gamma_control_manager_impl, + manager, gamma_control_manager_handle_resource_destroy); + wl_list_insert(&manager->resources, wl_resource_get_link(resource)); +} + +void wlr_gamma_control_manager_v1_destroy( + struct wlr_gamma_control_manager_v1 *manager) { + if (!manager) { + return; + } + wl_list_remove(&manager->display_destroy.link); + struct wlr_gamma_control_v1 *gamma_control, *tmp; + wl_list_for_each_safe(gamma_control, tmp, &manager->controls, link) { + wl_resource_destroy(gamma_control->resource); + } + struct wl_resource *resource, *resource_tmp; + wl_resource_for_each_safe(resource, resource_tmp, &manager->resources) { + wl_resource_destroy(resource); + } + wl_global_destroy(manager->global); + free(manager); +} + +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_gamma_control_manager_v1 *manager = + wl_container_of(listener, manager, display_destroy); + wlr_gamma_control_manager_v1_destroy(manager); +} + +struct wlr_gamma_control_manager_v1 *wlr_gamma_control_manager_v1_create( + struct wl_display *display) { + struct wlr_gamma_control_manager_v1 *manager = + calloc(1, sizeof(struct wlr_gamma_control_manager_v1)); + if (!manager) { + return NULL; + } + + manager->global = wl_global_create(display, + &zwlr_gamma_control_manager_v1_interface, + GAMMA_CONTROL_MANAGER_V1_VERSION, manager, gamma_control_manager_bind); + if (manager->global == NULL) { + free(manager); + return NULL; + } + + wl_list_init(&manager->resources); + wl_list_init(&manager->controls); + + manager->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &manager->display_destroy); + + return manager; +} diff --git a/types/wlr_output.c b/types/wlr_output.c index a1d00c0c..2ebf2acd 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -552,11 +552,12 @@ void wlr_output_schedule_frame(struct wlr_output *output) { wl_event_loop_add_idle(ev, schedule_frame_handle_idle_timer, output); } -void wlr_output_set_gamma(struct wlr_output *output, - uint32_t size, uint16_t *r, uint16_t *g, uint16_t *b) { - if (output->impl->set_gamma) { - output->impl->set_gamma(output, size, r, g, b); +bool wlr_output_set_gamma(struct wlr_output *output, uint32_t size, + uint16_t *r, uint16_t *g, uint16_t *b) { + if (!output->impl->set_gamma) { + return false; } + return output->impl->set_gamma(output, size, r, g, b); } uint32_t wlr_output_get_gamma_size(struct wlr_output *output) { From a5cab82a0f1a766e25a017036a8f7f39db0c889a Mon Sep 17 00:00:00 2001 From: emersion Date: Sun, 22 Jul 2018 16:22:34 +0100 Subject: [PATCH 02/13] rootston: fire up wlr-gamma-control-unstable-v1 --- include/rootston/desktop.h | 2 ++ rootston/desktop.c | 3 +++ 2 files changed, 5 insertions(+) diff --git a/include/rootston/desktop.h b/include/rootston/desktop.h index 51cf68db..363a16f0 100644 --- a/include/rootston/desktop.h +++ b/include/rootston/desktop.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -43,6 +44,7 @@ struct roots_desktop { struct wlr_xdg_shell_v6 *xdg_shell_v6; struct wlr_xdg_shell *xdg_shell; struct wlr_gamma_control_manager *gamma_control_manager; + struct wlr_gamma_control_manager_v1 *gamma_control_manager_v1; struct wlr_screenshooter *screenshooter; struct wlr_export_dmabuf_manager_v1 *export_dmabuf_manager_v1; struct wlr_server_decoration_manager *server_decoration_manager; diff --git a/rootston/desktop.c b/rootston/desktop.c index 786ac99e..658611e3 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -868,6 +869,8 @@ struct roots_desktop *desktop_create(struct roots_server *server, desktop->gamma_control_manager = wlr_gamma_control_manager_create( server->wl_display); + desktop->gamma_control_manager_v1 = wlr_gamma_control_manager_v1_create( + server->wl_display); desktop->screenshooter = wlr_screenshooter_create(server->wl_display); desktop->export_dmabuf_manager_v1 = wlr_export_dmabuf_manager_v1_create(server->wl_display); From 6e8892d4d71c231120ddcfb15e1a86c30cf9e497 Mon Sep 17 00:00:00 2001 From: emersion Date: Sun, 22 Jul 2018 17:06:30 +0100 Subject: [PATCH 03/13] gamma-control-v1: don't insert control in the list before checking unicity --- types/wlr_gamma_control_v1.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/types/wlr_gamma_control_v1.c b/types/wlr_gamma_control_v1.c index 36d9296b..063837fc 100644 --- a/types/wlr_gamma_control_v1.c +++ b/types/wlr_gamma_control_v1.c @@ -148,7 +148,7 @@ static void gamma_control_manager_get_gamma_control(struct wl_client *client, gamma_control->output_destroy_listener.notify = gamma_control_handle_output_destroy; - wl_list_insert(&manager->controls, &gamma_control->link); + wl_list_init(&gamma_control->link); if (!output->impl->set_gamma) { zwlr_gamma_control_v1_send_failed(gamma_control->resource); @@ -165,6 +165,8 @@ static void gamma_control_manager_get_gamma_control(struct wl_client *client, } } + wl_list_remove(&gamma_control->link); + wl_list_insert(&manager->controls, &gamma_control->link); zwlr_gamma_control_v1_send_gamma_size(gamma_control->resource, wlr_output_get_gamma_size(output)); } From e21563ec76b5f1f500b530cd2a5787123b26d627 Mon Sep 17 00:00:00 2001 From: emersion Date: Sun, 22 Jul 2018 17:23:34 +0100 Subject: [PATCH 04/13] output: document gamma functions --- include/wlr/types/wlr_output.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/include/wlr/types/wlr_output.h b/include/wlr/types/wlr_output.h index 0cf3e9f6..e9f3ae3b 100644 --- a/include/wlr/types/wlr_output.h +++ b/include/wlr/types/wlr_output.h @@ -174,9 +174,17 @@ bool wlr_output_swap_buffers(struct wlr_output *output, struct timespec *when, * it is a no-op. */ void wlr_output_schedule_frame(struct wlr_output *output); +/** + * Returns the maximum length of each gamma ramp, or 0 if unsupported. + */ +uint32_t wlr_output_get_gamma_size(struct wlr_output *output); +/** + * Sets the gamma table for this output. `r`, `g` and `b` are gamma ramps for + * red, green and blue. `size` is the length of the ramps and must not exceed + * the value returned by `wlr_output_get_gamma_size`. + */ bool wlr_output_set_gamma(struct wlr_output *output, uint32_t size, uint16_t *r, uint16_t *g, uint16_t *b); -uint32_t wlr_output_get_gamma_size(struct wlr_output *output); bool wlr_output_export_dmabuf(struct wlr_output *output, struct wlr_dmabuf_attributes *attribs); void wlr_output_set_fullscreen_surface(struct wlr_output *output, From 2ebecb6727b55dbdfe067d27bdb1679014e66879 Mon Sep 17 00:00:00 2001 From: emersion Date: Sun, 22 Jul 2018 17:37:01 +0100 Subject: [PATCH 05/13] backend/drm: allow to pass empty gamma ramp to reset it --- backend/drm/drm.c | 48 +++++++++++++++++++++++++--------- include/wlr/types/wlr_output.h | 2 ++ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/backend/drm/drm.c b/backend/drm/drm.c index 3e6659f1..ce5a1640 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -228,19 +228,12 @@ static bool drm_connector_swap_buffers(struct wlr_output *output, return true; } -static bool drm_connector_set_gamma(struct wlr_output *output, - uint32_t size, uint16_t *r, uint16_t *g, uint16_t *b) { - struct wlr_drm_connector *conn = (struct wlr_drm_connector *)output; - struct wlr_drm_backend *drm = (struct wlr_drm_backend *)output->backend; - - bool ok = false; - if (conn->crtc) { - ok = drm->iface->crtc_set_gamma(drm, conn->crtc, r, g, b, size); - if (ok) { - wlr_output_update_needs_swap(output); - } +static void fill_empty_gamma_table(uint32_t size, + uint16_t *r, uint16_t *g, uint16_t *b) { + for (uint32_t i = 0; i < size; ++i) { + uint16_t val = (uint32_t)0xffff * (uint32_t)i / (uint32_t)(size - 1); + r[i] = g[i] = b[i] = val; } - return ok; } static uint32_t drm_connector_get_gamma_size(struct wlr_output *output) { @@ -254,6 +247,37 @@ static uint32_t drm_connector_get_gamma_size(struct wlr_output *output) { return 0; } +static bool drm_connector_set_gamma(struct wlr_output *output, + uint32_t size, uint16_t *r, uint16_t *g, uint16_t *b) { + struct wlr_drm_connector *conn = (struct wlr_drm_connector *)output; + struct wlr_drm_backend *drm = (struct wlr_drm_backend *)output->backend; + + if (!conn->crtc) { + return false; + } + + uint16_t *reset_table = NULL; + if (size == 0) { + size = drm_connector_get_gamma_size(output); + reset_table = malloc(3 * size * sizeof(uint16_t)); + if (reset_table == NULL) { + wlr_log(WLR_ERROR, "Failed to allocate gamma table"); + return false; + } + r = reset_table; + g = reset_table + size; + b = reset_table + 2 * size; + fill_empty_gamma_table(size, r, g, b); + } + + bool ok = drm->iface->crtc_set_gamma(drm, conn->crtc, r, g, b, size); + if (ok) { + wlr_output_update_needs_swap(output); + } + free(reset_table); + return ok; +} + static bool drm_connector_export_dmabuf(struct wlr_output *output, struct wlr_dmabuf_attributes *attribs) { struct wlr_drm_connector *conn = (struct wlr_drm_connector *)output; diff --git a/include/wlr/types/wlr_output.h b/include/wlr/types/wlr_output.h index e9f3ae3b..3a9f3c41 100644 --- a/include/wlr/types/wlr_output.h +++ b/include/wlr/types/wlr_output.h @@ -182,6 +182,8 @@ uint32_t wlr_output_get_gamma_size(struct wlr_output *output); * Sets the gamma table for this output. `r`, `g` and `b` are gamma ramps for * red, green and blue. `size` is the length of the ramps and must not exceed * the value returned by `wlr_output_get_gamma_size`. + * + * Providing zero-sized ramps resets the gamma table. */ bool wlr_output_set_gamma(struct wlr_output *output, uint32_t size, uint16_t *r, uint16_t *g, uint16_t *b); From 73b71118463d67581c2e2c38d263742ce35ec7d8 Mon Sep 17 00:00:00 2001 From: emersion Date: Sun, 22 Jul 2018 17:37:18 +0100 Subject: [PATCH 06/13] gamma-control-v1: reset gamma table when control is destroyed --- types/wlr_gamma_control_v1.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/wlr_gamma_control_v1.c b/types/wlr_gamma_control_v1.c index 063837fc..c347a13d 100644 --- a/types/wlr_gamma_control_v1.c +++ b/types/wlr_gamma_control_v1.c @@ -20,7 +20,7 @@ static void gamma_control_destroy(struct wlr_gamma_control_v1 *gamma_control) { if (gamma_control == NULL) { return; } - // TODO: reset gamma table + wlr_output_set_gamma(gamma_control->output, 0, NULL, NULL, NULL); wl_resource_set_user_data(gamma_control->resource, NULL); wl_list_remove(&gamma_control->output_destroy_listener.link); wl_list_remove(&gamma_control->link); From 1705316fa759d528b0b19b1a13819d459da20a6a Mon Sep 17 00:00:00 2001 From: emersion Date: Sun, 22 Jul 2018 20:17:51 +0100 Subject: [PATCH 07/13] gamma-control-v1: set O_NONBLOCK, improve error handling style --- types/wlr_gamma_control_v1.c | 51 ++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/types/wlr_gamma_control_v1.c b/types/wlr_gamma_control_v1.c index c347a13d..dfa52115 100644 --- a/types/wlr_gamma_control_v1.c +++ b/types/wlr_gamma_control_v1.c @@ -1,13 +1,14 @@ #include +#include #include #include #include +#include #include #include -#include #include -#include "wlr-gamma-control-unstable-v1-protocol.h" #include "util/signal.h" +#include "wlr-gamma-control-unstable-v1-protocol.h" #define GAMMA_CONTROL_MANAGER_V1_VERSION 1 @@ -27,6 +28,12 @@ static void gamma_control_destroy(struct wlr_gamma_control_v1 *gamma_control) { free(gamma_control); } +static void gamma_control_send_failed( + struct wlr_gamma_control_v1 *gamma_control) { + zwlr_gamma_control_v1_send_failed(gamma_control->resource); + gamma_control_destroy(gamma_control); +} + static const struct zwlr_gamma_control_v1_interface gamma_control_impl; static struct wlr_gamma_control_v1 *gamma_control_from_resource( @@ -54,8 +61,7 @@ static void gamma_control_handle_set_gamma(struct wl_client *client, struct wlr_gamma_control_v1 *gamma_control = gamma_control_from_resource(gamma_control_resource); if (gamma_control == NULL) { - close(fd); - return; + goto error_fd; } uint32_t ramp_size = wlr_output_get_gamma_size(gamma_control->output); @@ -64,29 +70,34 @@ static void gamma_control_handle_set_gamma(struct wl_client *client, off_t fd_size = lseek(fd, 0, SEEK_END); // Skip checks if kernel does no support seek on buffer if (fd_size != -1 && (size_t)fd_size != table_size) { - close(fd); wl_resource_post_error(gamma_control_resource, ZWLR_GAMMA_CONTROL_V1_ERROR_INVALID_GAMMA, "The gamma ramps don't have the correct size"); - return; + goto error_fd; } lseek(fd, 0, SEEK_SET); + int fd_flags = fcntl(fd, F_GETFL, 0); + if (fd_flags == -1) { + gamma_control_send_failed(gamma_control); + goto error_fd; + } + if (fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK) == -1) { + gamma_control_send_failed(gamma_control); + goto error_fd; + } + // Use the heap since gamma tables can be large uint16_t *table = malloc(table_size); if (table == NULL) { - close(fd); wl_resource_post_no_memory(gamma_control_resource); - return; + goto error_fd; } ssize_t n_read = read(fd, table, table_size); - close(fd); if (n_read == -1 || (size_t)n_read != table_size) { - free(table); - zwlr_gamma_control_v1_send_failed(gamma_control->resource); - gamma_control_destroy(gamma_control); - return; + gamma_control_send_failed(gamma_control); + goto error_table; } uint16_t *r = table; @@ -94,12 +105,18 @@ static void gamma_control_handle_set_gamma(struct wl_client *client, uint16_t *b = table + 2 * ramp_size; bool ok = wlr_output_set_gamma(gamma_control->output, ramp_size, r, g, b); - free(table); if (!ok) { - zwlr_gamma_control_v1_send_failed(gamma_control->resource); - gamma_control_destroy(gamma_control); - return; + gamma_control_send_failed(gamma_control); + goto error_table; } + + free(table); + return; + +error_table: + free(table); +error_fd: + close(fd); } static const struct zwlr_gamma_control_v1_interface gamma_control_impl = { From 364afced3e944db97b1f4534f161541478319d69 Mon Sep 17 00:00:00 2001 From: emersion Date: Sun, 22 Jul 2018 22:57:22 +0100 Subject: [PATCH 08/13] backend/drm: remove unnecessary casts --- backend/drm/drm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/drm/drm.c b/backend/drm/drm.c index ce5a1640..9d030dff 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -231,7 +231,7 @@ static bool drm_connector_swap_buffers(struct wlr_output *output, static void fill_empty_gamma_table(uint32_t size, uint16_t *r, uint16_t *g, uint16_t *b) { for (uint32_t i = 0; i < size; ++i) { - uint16_t val = (uint32_t)0xffff * (uint32_t)i / (uint32_t)(size - 1); + uint16_t val = (uint32_t)0xffff * i / (size - 1); r[i] = g[i] = b[i] = val; } } From 1d97202ec2b9ca3c4b77e0cc5ab2a574c3938447 Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 23 Jul 2018 22:48:00 +0100 Subject: [PATCH 09/13] examples: add gamma-control --- examples/gamma-control.c | 190 +++++++++++++++++++++++++++++++++++++++ examples/meson.build | 6 ++ protocol/meson.build | 1 + 3 files changed, 197 insertions(+) create mode 100644 examples/gamma-control.c diff --git a/examples/gamma-control.c b/examples/gamma-control.c new file mode 100644 index 00000000..161c6fd0 --- /dev/null +++ b/examples/gamma-control.c @@ -0,0 +1,190 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "wlr-gamma-control-unstable-v1-client-protocol.h" + +struct output { + struct wl_output *wl_output; + struct zwlr_gamma_control_v1 *gamma_control; + uint32_t ramp_size; + int table_fd; + uint16_t *table; + struct wl_list link; +}; + +static struct wl_list outputs; +static struct zwlr_gamma_control_manager_v1 *gamma_control_manager = NULL; + +static int create_anonymous_file(off_t size) { + char template[] = "/tmp/wlroots-shared-XXXXXX"; + int fd = mkstemp(template); + if (fd < 0) { + return -1; + } + + int ret; + do { + errno = 0; + ret = ftruncate(fd, size); + } while (errno == EINTR); + if (ret < 0) { + close(fd); + return -1; + } + + unlink(template); + return fd; +} + +static int create_gamma_table(uint32_t ramp_size, uint16_t **table) { + size_t table_size = ramp_size * 3 * sizeof(uint16_t); + int fd = create_anonymous_file(table_size); + if (fd < 0) { + fprintf(stderr, "failed to create anonymous file\n"); + return -1; + } + + void *data = + mmap(NULL, table_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (data == MAP_FAILED) { + fprintf(stderr, "failed to mmap()\n"); + close(fd); + return -1; + } + + *table = data; + return fd; +} + +static void gamma_control_handle_gamma_size(void *data, + struct zwlr_gamma_control_v1 *gamma_control, uint32_t ramp_size) { + struct output *output = data; + output->ramp_size = ramp_size; + output->table_fd = create_gamma_table(ramp_size, &output->table); + if (output->table_fd < 0) { + exit(EXIT_FAILURE); + } +} + +static void gamma_control_handle_failed(void *data, + struct zwlr_gamma_control_v1 *gamma_control) { + fprintf(stderr, "failed to set gamma table\n"); + exit(EXIT_FAILURE); +} + +static const struct zwlr_gamma_control_v1_listener gamma_control_listener = { + .gamma_size = gamma_control_handle_gamma_size, + .failed = gamma_control_handle_failed, +}; + +static void registry_handle_global(void *data, struct wl_registry *registry, + uint32_t name, const char *interface, uint32_t version) { + if (strcmp(interface, wl_output_interface.name) == 0) { + struct output *output = calloc(1, sizeof(struct output)); + output->wl_output = wl_registry_bind(registry, name, + &wl_output_interface, 1); + wl_list_insert(&outputs, &output->link); + } else if (strcmp(interface, + zwlr_gamma_control_manager_v1_interface.name) == 0) { + gamma_control_manager = wl_registry_bind(registry, name, + &zwlr_gamma_control_manager_v1_interface, 1); + } +} + +static void registry_handle_global_remove(void *data, + struct wl_registry *registry, uint32_t name) { + // Who cares? +} + +static const struct wl_registry_listener registry_listener = { + .global = registry_handle_global, + .global_remove = registry_handle_global_remove, +}; + +static void fill_gamma_table(uint16_t *table, uint32_t ramp_size, + double contrast, double brightness, double gamma) { + uint16_t *r = table; + uint16_t *g = table + ramp_size; + uint16_t *b = table + 2 * ramp_size; + for (uint32_t i = 0; i < ramp_size; ++i) { + double val = (double)i / (ramp_size - 1); + val = contrast * pow(val, 1.0 / gamma) + brightness; + r[i] = g[i] = b[i] = (uint16_t)(UINT16_MAX * val); + } +} + +static const char usage[] = "usage: gamma-control [options]\n" + " -h show this help message\n" + " -c set contrast (default: 1)\n" + " -b set brightness (default: 0)\n" + " -g set gamma (default: 1)\n"; + +int main(int argc, char *argv[]) { + wl_list_init(&outputs); + + double contrast = 1, brightness = 0, gamma = 1; + int opt; + while ((opt = getopt(argc, argv, "hc:b:g:")) != -1) { + switch (opt) { + case 'c': + contrast = strtod(optarg, NULL); + break; + case 'b': + brightness = strtod(optarg, NULL); + break; + case 'g': + gamma = strtod(optarg, NULL); + break; + case 'h': + default: + fprintf(stderr, usage); + return opt == 'h' ? EXIT_SUCCESS : EXIT_FAILURE; + } + } + + struct wl_display *display = wl_display_connect(NULL); + if (display == NULL) { + fprintf(stderr, "failed to create display\n"); + return -1; + } + + struct wl_registry *registry = wl_display_get_registry(display); + wl_registry_add_listener(registry, ®istry_listener, NULL); + wl_display_dispatch(display); + wl_display_roundtrip(display); + + if (gamma_control_manager == NULL) { + fprintf(stderr, + "compositor doesn't support wlr-gamma-control-unstable-v1\n"); + return EXIT_FAILURE; + } + + struct output *output; + wl_list_for_each(output, &outputs, link) { + output->gamma_control = zwlr_gamma_control_manager_v1_get_gamma_control( + gamma_control_manager, output->wl_output); + zwlr_gamma_control_v1_add_listener(output->gamma_control, + &gamma_control_listener, output); + } + wl_display_roundtrip(display); + + wl_list_for_each(output, &outputs, link) { + fill_gamma_table(output->table, output->ramp_size, + contrast, brightness, gamma); + zwlr_gamma_control_v1_set_gamma(output->gamma_control, + output->table_fd); + } + + while (wl_display_dispatch(display) != -1) { + // This space is intentionnally left blank + } + + return EXIT_SUCCESS; +} diff --git a/examples/meson.build b/examples/meson.build index 26d20850..25ad7566 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -50,6 +50,12 @@ executable( dependencies: [wayland_cursor, wayland_client, wlr_protos, wlroots] ) +executable( + 'gamma-control', + 'gamma-control.c', + dependencies: [wayland_cursor, wayland_client, wlr_protos, wlroots] +) + if libavutil.found() and libavcodec.found() and libavformat.found() executable( 'dmabuf-capture', diff --git a/protocol/meson.build b/protocol/meson.build index 23da1afc..73a3156a 100644 --- a/protocol/meson.build +++ b/protocol/meson.build @@ -56,6 +56,7 @@ client_protocols = [ 'idle.xml', 'screenshooter.xml', 'wlr-export-dmabuf-unstable-v1.xml', + 'wlr-gamma-control-unstable-v1.xml', 'wlr-input-inhibitor-unstable-v1.xml', 'wlr-layer-shell-unstable-v1.xml', 'wlr-screencopy-unstable-v1.xml', From b0c32019c384b51fdd53e573c5e52fdf082a888d Mon Sep 17 00:00:00 2001 From: emersion Date: Wed, 25 Jul 2018 14:37:10 +0100 Subject: [PATCH 10/13] examples/gamma-control: clamp values, default brightness to 1 --- examples/gamma-control.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/examples/gamma-control.c b/examples/gamma-control.c index 161c6fd0..e2af9cdd 100644 --- a/examples/gamma-control.c +++ b/examples/gamma-control.c @@ -115,7 +115,12 @@ static void fill_gamma_table(uint16_t *table, uint32_t ramp_size, uint16_t *b = table + 2 * ramp_size; for (uint32_t i = 0; i < ramp_size; ++i) { double val = (double)i / (ramp_size - 1); - val = contrast * pow(val, 1.0 / gamma) + brightness; + val = contrast * pow(val, 1.0 / gamma) + (1 - brightness); + if (val > 1.0) { + val = 1.0; + } else if (val < 0.0) { + val = 0.0; + } r[i] = g[i] = b[i] = (uint16_t)(UINT16_MAX * val); } } @@ -123,13 +128,13 @@ static void fill_gamma_table(uint16_t *table, uint32_t ramp_size, static const char usage[] = "usage: gamma-control [options]\n" " -h show this help message\n" " -c set contrast (default: 1)\n" - " -b set brightness (default: 0)\n" + " -b set brightness (default: 1)\n" " -g set gamma (default: 1)\n"; int main(int argc, char *argv[]) { wl_list_init(&outputs); - double contrast = 1, brightness = 0, gamma = 1; + double contrast = 1, brightness = 1, gamma = 1; int opt; while ((opt = getopt(argc, argv, "hc:b:g:")) != -1) { switch (opt) { From 2ae05756319206a9fbfde29e38bdcb04743486e4 Mon Sep 17 00:00:00 2001 From: emersion Date: Wed, 25 Jul 2018 14:46:50 +0100 Subject: [PATCH 11/13] examples/gamma-control: fix brightness formula --- examples/gamma-control.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gamma-control.c b/examples/gamma-control.c index e2af9cdd..a060b883 100644 --- a/examples/gamma-control.c +++ b/examples/gamma-control.c @@ -115,7 +115,7 @@ static void fill_gamma_table(uint16_t *table, uint32_t ramp_size, uint16_t *b = table + 2 * ramp_size; for (uint32_t i = 0; i < ramp_size; ++i) { double val = (double)i / (ramp_size - 1); - val = contrast * pow(val, 1.0 / gamma) + (1 - brightness); + val = contrast * pow(val, 1.0 / gamma) + (brightness - 1); if (val > 1.0) { val = 1.0; } else if (val < 0.0) { From bbd0fbe573bee16975eb30e7e680e50251a1b47b Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 27 Jul 2018 21:23:50 +0100 Subject: [PATCH 12/13] gamma-control-v1: fix fds not closed --- types/wlr_gamma_control_v1.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/types/wlr_gamma_control_v1.c b/types/wlr_gamma_control_v1.c index dfa52115..2163d356 100644 --- a/types/wlr_gamma_control_v1.c +++ b/types/wlr_gamma_control_v1.c @@ -99,6 +99,8 @@ static void gamma_control_handle_set_gamma(struct wl_client *client, gamma_control_send_failed(gamma_control); goto error_table; } + close(fd); + fd = -1; uint16_t *r = table; uint16_t *g = table + ramp_size; @@ -109,8 +111,8 @@ static void gamma_control_handle_set_gamma(struct wl_client *client, gamma_control_send_failed(gamma_control); goto error_table; } - free(table); + return; error_table: From c3afe4f42e3ddfbd51500d99416b51aee552b689 Mon Sep 17 00:00:00 2001 From: emersion Date: Sun, 29 Jul 2018 17:51:27 +0100 Subject: [PATCH 13/13] gamma-control-v1: improve error handling --- types/wlr_gamma_control_v1.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/types/wlr_gamma_control_v1.c b/types/wlr_gamma_control_v1.c index 2163d356..54ab7e9d 100644 --- a/types/wlr_gamma_control_v1.c +++ b/types/wlr_gamma_control_v1.c @@ -67,22 +67,15 @@ static void gamma_control_handle_set_gamma(struct wl_client *client, uint32_t ramp_size = wlr_output_get_gamma_size(gamma_control->output); size_t table_size = ramp_size * 3 * sizeof(uint16_t); - off_t fd_size = lseek(fd, 0, SEEK_END); - // Skip checks if kernel does no support seek on buffer - if (fd_size != -1 && (size_t)fd_size != table_size) { - wl_resource_post_error(gamma_control_resource, - ZWLR_GAMMA_CONTROL_V1_ERROR_INVALID_GAMMA, - "The gamma ramps don't have the correct size"); - goto error_fd; - } - lseek(fd, 0, SEEK_SET); - + // Refuse to block when reading int fd_flags = fcntl(fd, F_GETFL, 0); if (fd_flags == -1) { + wlr_log_errno(WLR_ERROR, "failed to get FD flags"); gamma_control_send_failed(gamma_control); goto error_fd; } if (fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK) == -1) { + wlr_log_errno(WLR_ERROR, "failed to set FD flags"); gamma_control_send_failed(gamma_control); goto error_fd; } @@ -95,9 +88,15 @@ static void gamma_control_handle_set_gamma(struct wl_client *client, } ssize_t n_read = read(fd, table, table_size); - if (n_read == -1 || (size_t)n_read != table_size) { + if (n_read < 0) { + wlr_log_errno(WLR_ERROR, "failed to read gamma table"); gamma_control_send_failed(gamma_control); goto error_table; + } else if ((size_t)n_read != table_size) { + wl_resource_post_error(gamma_control_resource, + ZWLR_GAMMA_CONTROL_V1_ERROR_INVALID_GAMMA, + "The gamma ramps don't have the correct size"); + goto error_table; } close(fd); fd = -1;