2022-08-20 00:01:06 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
|
|
|
|
|
#include <assert.h>
|
2022-08-20 20:11:58 +02:00
|
|
|
#include <cairo.h>
|
2022-08-20 00:01:06 +02:00
|
|
|
#include <stdlib.h>
|
2023-12-17 02:16:49 +01:00
|
|
|
#include <string.h>
|
2022-08-20 00:01:06 +02:00
|
|
|
#include <wlr/types/wlr_scene.h>
|
2023-06-29 17:45:33 +01:00
|
|
|
#include <wlr/util/box.h>
|
2023-12-17 02:16:49 +01:00
|
|
|
#include "buffer.h"
|
2022-08-20 00:01:06 +02:00
|
|
|
#include "common/graphic-helpers.h"
|
2022-09-16 18:41:02 -04:00
|
|
|
#include "common/mem.h"
|
2022-08-20 00:01:06 +02:00
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
multi_rect_destroy_notify(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
|
|
|
|
struct multi_rect *rect = wl_container_of(listener, rect, destroy);
|
|
|
|
|
free(rect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct multi_rect *
|
|
|
|
|
multi_rect_create(struct wlr_scene_tree *parent, float *colors[3], int line_width)
|
|
|
|
|
{
|
2022-09-18 15:22:26 -04:00
|
|
|
struct multi_rect *rect = znew(*rect);
|
2022-08-20 00:01:06 +02:00
|
|
|
rect->line_width = line_width;
|
|
|
|
|
rect->tree = wlr_scene_tree_create(parent);
|
|
|
|
|
rect->destroy.notify = multi_rect_destroy_notify;
|
|
|
|
|
wl_signal_add(&rect->tree->node.events.destroy, &rect->destroy);
|
|
|
|
|
for (size_t i = 0; i < 3; i++) {
|
|
|
|
|
rect->top[i] = wlr_scene_rect_create(rect->tree, 0, 0, colors[i]);
|
|
|
|
|
rect->right[i] = wlr_scene_rect_create(rect->tree, 0, 0, colors[i]);
|
|
|
|
|
rect->bottom[i] = wlr_scene_rect_create(rect->tree, 0, 0, colors[i]);
|
|
|
|
|
rect->left[i] = wlr_scene_rect_create(rect->tree, 0, 0, colors[i]);
|
|
|
|
|
wlr_scene_node_set_position(&rect->top[i]->node,
|
|
|
|
|
i * line_width, i * line_width);
|
|
|
|
|
wlr_scene_node_set_position(&rect->left[i]->node,
|
2024-04-13 22:32:06 +09:00
|
|
|
i * line_width, (i + 1) * line_width);
|
2022-08-20 00:01:06 +02:00
|
|
|
}
|
|
|
|
|
return rect;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
multi_rect_set_size(struct multi_rect *rect, int width, int height)
|
|
|
|
|
{
|
|
|
|
|
assert(rect);
|
|
|
|
|
int line_width = rect->line_width;
|
|
|
|
|
|
2024-04-13 22:32:06 +09:00
|
|
|
/*
|
|
|
|
|
* The outmost outline is drawn like below:
|
|
|
|
|
*
|
|
|
|
|
* |--width--|
|
|
|
|
|
*
|
|
|
|
|
* +---------+ ---
|
|
|
|
|
* +-+-----+-+ |
|
|
|
|
|
* | | | | height
|
|
|
|
|
* | | | | |
|
|
|
|
|
* +-+-----+-+ |
|
|
|
|
|
* +---------+ ---
|
|
|
|
|
*/
|
2022-08-20 00:01:06 +02:00
|
|
|
for (size_t i = 0; i < 3; i++) {
|
|
|
|
|
/* Reposition, top and left don't ever change */
|
|
|
|
|
wlr_scene_node_set_position(&rect->right[i]->node,
|
2024-04-13 22:32:06 +09:00
|
|
|
width - (i + 1) * line_width, (i + 1) * line_width);
|
2022-08-20 00:01:06 +02:00
|
|
|
wlr_scene_node_set_position(&rect->bottom[i]->node,
|
|
|
|
|
i * line_width, height - (i + 1) * line_width);
|
|
|
|
|
|
|
|
|
|
/* Update sizes */
|
|
|
|
|
wlr_scene_rect_set_size(rect->top[i],
|
|
|
|
|
width - i * line_width * 2, line_width);
|
|
|
|
|
wlr_scene_rect_set_size(rect->bottom[i],
|
|
|
|
|
width - i * line_width * 2, line_width);
|
|
|
|
|
wlr_scene_rect_set_size(rect->left[i],
|
2024-04-13 22:32:06 +09:00
|
|
|
line_width, height - (i + 1) * line_width * 2);
|
2022-08-20 00:01:06 +02:00
|
|
|
wlr_scene_rect_set_size(rect->right[i],
|
2024-04-13 22:32:06 +09:00
|
|
|
line_width, height - (i + 1) * line_width * 2);
|
2022-08-20 00:01:06 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-08-20 20:11:58 +02:00
|
|
|
|
|
|
|
|
/* Draws a border with a specified line width */
|
|
|
|
|
void
|
2023-06-29 17:45:33 +01:00
|
|
|
draw_cairo_border(cairo_t *cairo, struct wlr_fbox fbox, double line_width)
|
2022-08-20 20:11:58 +02:00
|
|
|
{
|
|
|
|
|
cairo_save(cairo);
|
|
|
|
|
|
|
|
|
|
/* The anchor point of a line is in the center */
|
2023-06-29 17:45:33 +01:00
|
|
|
fbox.x += line_width / 2.0;
|
|
|
|
|
fbox.y += line_width / 2.0;
|
|
|
|
|
fbox.width -= line_width;
|
|
|
|
|
fbox.height -= line_width;
|
2022-08-20 20:11:58 +02:00
|
|
|
cairo_set_line_width(cairo, line_width);
|
2023-06-29 17:45:33 +01:00
|
|
|
cairo_rectangle(cairo, fbox.x, fbox.y, fbox.width, fbox.height);
|
2022-08-20 20:11:58 +02:00
|
|
|
cairo_stroke(cairo);
|
|
|
|
|
|
|
|
|
|
cairo_restore(cairo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Sets the cairo color. Splits the single color channels */
|
|
|
|
|
void
|
2024-03-16 22:20:40 -04:00
|
|
|
set_cairo_color(cairo_t *cairo, const float *c)
|
2022-08-20 20:11:58 +02:00
|
|
|
{
|
2024-04-06 17:46:30 +02:00
|
|
|
/*
|
|
|
|
|
* We are dealing with pre-multiplied colors
|
|
|
|
|
* but cairo expects unmultiplied colors here
|
|
|
|
|
*/
|
|
|
|
|
float alpha = c[3];
|
|
|
|
|
|
|
|
|
|
if (alpha == 0.0f) {
|
|
|
|
|
cairo_set_source_rgba(cairo, 0, 0, 0, 0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cairo_set_source_rgba(cairo, c[0] / alpha, c[1] / alpha,
|
|
|
|
|
c[2] / alpha, alpha);
|
2022-08-20 20:11:58 +02:00
|
|
|
}
|
2023-12-17 02:16:49 +01:00
|
|
|
|
|
|
|
|
struct surface_context
|
|
|
|
|
get_cairo_surface_from_lab_data_buffer(struct lab_data_buffer *buffer)
|
|
|
|
|
{
|
|
|
|
|
/* Handle CAIRO_FORMAT_ARGB32 buffers */
|
|
|
|
|
if (buffer->cairo) {
|
|
|
|
|
return (struct surface_context){
|
|
|
|
|
.is_duplicate = false,
|
|
|
|
|
.surface = cairo_get_target(buffer->cairo),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Handle DRM_FORMAT_ARGB8888 buffers */
|
|
|
|
|
int w = buffer->unscaled_width;
|
|
|
|
|
int h = buffer->unscaled_height;
|
|
|
|
|
cairo_surface_t *surface =
|
|
|
|
|
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
|
|
|
|
|
if (!surface) {
|
|
|
|
|
return (struct surface_context){0};
|
|
|
|
|
}
|
|
|
|
|
unsigned char *data = cairo_image_surface_get_data(surface);
|
|
|
|
|
cairo_surface_flush(surface);
|
|
|
|
|
memcpy(data, buffer->data, h * buffer->stride);
|
|
|
|
|
cairo_surface_mark_dirty(surface);
|
|
|
|
|
return (struct surface_context){
|
|
|
|
|
.is_duplicate = true,
|
|
|
|
|
.surface = surface,
|
|
|
|
|
};
|
|
|
|
|
}
|