2022-02-11 22:19:05 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
/*
|
|
|
|
|
* Based on wlroots/types/wlr_buffer.c
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2017, 2018 Drew DeVault
|
|
|
|
|
* Copyright (c) 2018-2021 Simon Ser, Simon Zeni
|
|
|
|
|
|
|
|
|
|
* 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 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 <assert.h>
|
2022-02-21 03:18:38 +01:00
|
|
|
#include <stdlib.h>
|
2022-02-11 22:19:05 +00:00
|
|
|
#include <drm_fourcc.h>
|
2022-03-04 20:25:04 +00:00
|
|
|
#include <wlr/interfaces/wlr_buffer.h>
|
2025-05-30 20:58:15 +09:00
|
|
|
#include <wlr/util/log.h>
|
2022-02-11 22:19:05 +00:00
|
|
|
#include "buffer.h"
|
2025-05-30 20:55:31 +09:00
|
|
|
#include "common/box.h"
|
2022-09-16 18:41:02 -04:00
|
|
|
#include "common/mem.h"
|
2022-02-11 22:19:05 +00:00
|
|
|
|
2025-07-04 00:12:21 -04:00
|
|
|
static struct lab_data_buffer *data_buffer_from_buffer(
|
|
|
|
|
struct wlr_buffer *buffer);
|
2022-02-11 22:19:05 +00:00
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
data_buffer_destroy(struct wlr_buffer *wlr_buffer)
|
|
|
|
|
{
|
|
|
|
|
struct lab_data_buffer *buffer = data_buffer_from_buffer(wlr_buffer);
|
2024-11-27 14:17:28 +09:00
|
|
|
/* this also frees buffer->data if surface_owns_data == true */
|
|
|
|
|
cairo_surface_destroy(buffer->surface);
|
|
|
|
|
if (!buffer->surface_owns_data) {
|
2022-02-17 01:46:32 +01:00
|
|
|
free(buffer->data);
|
2022-02-13 13:00:26 +00:00
|
|
|
}
|
2025-04-15 06:40:40 +02:00
|
|
|
wlr_buffer_finish(wlr_buffer);
|
2022-02-11 22:19:05 +00:00
|
|
|
free(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
data_buffer_begin_data_ptr_access(struct wlr_buffer *wlr_buffer, uint32_t flags,
|
|
|
|
|
void **data, uint32_t *format, size_t *stride)
|
|
|
|
|
{
|
2022-02-13 11:47:03 +00:00
|
|
|
struct lab_data_buffer *buffer =
|
|
|
|
|
wl_container_of(wlr_buffer, buffer, base);
|
|
|
|
|
assert(buffer->data);
|
2022-02-11 22:19:05 +00:00
|
|
|
*data = (void *)buffer->data;
|
|
|
|
|
*format = buffer->format;
|
|
|
|
|
*stride = buffer->stride;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
data_buffer_end_data_ptr_access(struct wlr_buffer *wlr_buffer)
|
|
|
|
|
{
|
|
|
|
|
/* noop */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct wlr_buffer_impl data_buffer_impl = {
|
|
|
|
|
.destroy = data_buffer_destroy,
|
|
|
|
|
.begin_data_ptr_access = data_buffer_begin_data_ptr_access,
|
|
|
|
|
.end_data_ptr_access = data_buffer_end_data_ptr_access,
|
|
|
|
|
};
|
|
|
|
|
|
2025-07-04 00:12:21 -04:00
|
|
|
static struct lab_data_buffer *
|
|
|
|
|
data_buffer_from_buffer(struct wlr_buffer *buffer)
|
|
|
|
|
{
|
|
|
|
|
assert(buffer->impl == &data_buffer_impl);
|
|
|
|
|
return (struct lab_data_buffer *)buffer;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 22:19:05 +00:00
|
|
|
struct lab_data_buffer *
|
2024-10-06 01:42:54 -04:00
|
|
|
buffer_adopt_cairo_surface(cairo_surface_t *surface)
|
2022-02-11 22:19:05 +00:00
|
|
|
{
|
2024-10-06 01:42:54 -04:00
|
|
|
assert(cairo_surface_get_type(surface) == CAIRO_SURFACE_TYPE_IMAGE);
|
|
|
|
|
assert(cairo_image_surface_get_format(surface) == CAIRO_FORMAT_ARGB32);
|
2022-02-13 11:47:03 +00:00
|
|
|
|
2024-10-06 01:42:54 -04:00
|
|
|
int width = cairo_image_surface_get_width(surface);
|
|
|
|
|
int height = cairo_image_surface_get_height(surface);
|
|
|
|
|
|
|
|
|
|
struct lab_data_buffer *buffer = znew(*buffer);
|
2022-06-12 21:11:25 +02:00
|
|
|
wlr_buffer_init(&buffer->base, &data_buffer_impl, width, height);
|
2024-10-06 01:42:54 -04:00
|
|
|
|
|
|
|
|
buffer->surface = surface;
|
|
|
|
|
buffer->data = cairo_image_surface_get_data(buffer->surface);
|
|
|
|
|
buffer->format = DRM_FORMAT_ARGB8888;
|
|
|
|
|
buffer->stride = cairo_image_surface_get_stride(buffer->surface);
|
|
|
|
|
buffer->logical_width = width;
|
|
|
|
|
buffer->logical_height = height;
|
2024-11-27 14:17:28 +09:00
|
|
|
buffer->surface_owns_data = true;
|
2024-10-06 01:42:54 -04:00
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct lab_data_buffer *
|
|
|
|
|
buffer_create_cairo(uint32_t logical_width, uint32_t logical_height, float scale)
|
|
|
|
|
{
|
|
|
|
|
/* Create an image surface with the scaled size */
|
|
|
|
|
cairo_surface_t *surface =
|
|
|
|
|
cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
|
|
|
|
|
lroundf(logical_width * scale),
|
|
|
|
|
lroundf(logical_height * scale));
|
2022-06-12 21:11:25 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tell cairo about the device scale so we can keep drawing in unscaled
|
|
|
|
|
* coordinate space. Pango will automatically use the cairo scale attribute
|
|
|
|
|
* as well when creating text on this surface.
|
|
|
|
|
*
|
|
|
|
|
* For a more complete explanation see PR #389
|
|
|
|
|
*/
|
2024-10-06 01:42:54 -04:00
|
|
|
cairo_surface_set_device_scale(surface, scale, scale);
|
2022-02-13 13:00:26 +00:00
|
|
|
|
2024-10-06 01:42:54 -04:00
|
|
|
/*
|
|
|
|
|
* Adopt the image surface into a buffer, set the correct
|
|
|
|
|
* logical size, and create a cairo context for drawing
|
|
|
|
|
*/
|
|
|
|
|
struct lab_data_buffer *buffer = buffer_adopt_cairo_surface(surface);
|
|
|
|
|
buffer->logical_width = logical_width;
|
|
|
|
|
buffer->logical_height = logical_height;
|
2022-02-13 11:47:03 +00:00
|
|
|
|
2022-02-11 22:19:05 +00:00
|
|
|
return buffer;
|
|
|
|
|
}
|
2022-02-17 01:46:32 +01:00
|
|
|
|
|
|
|
|
struct lab_data_buffer *
|
2024-10-06 01:42:54 -04:00
|
|
|
buffer_create_from_data(void *pixel_data, uint32_t width, uint32_t height,
|
|
|
|
|
uint32_t stride)
|
2022-02-17 01:46:32 +01:00
|
|
|
{
|
2022-09-18 15:22:26 -04:00
|
|
|
struct lab_data_buffer *buffer = znew(*buffer);
|
2022-02-17 01:46:32 +01:00
|
|
|
wlr_buffer_init(&buffer->base, &data_buffer_impl, width, height);
|
2024-10-06 01:42:54 -04:00
|
|
|
buffer->logical_width = width;
|
|
|
|
|
buffer->logical_height = height;
|
2022-02-17 01:46:32 +01:00
|
|
|
buffer->data = pixel_data;
|
|
|
|
|
buffer->format = DRM_FORMAT_ARGB8888;
|
|
|
|
|
buffer->stride = stride;
|
2024-11-27 14:17:28 +09:00
|
|
|
buffer->surface = cairo_image_surface_create_for_data(
|
|
|
|
|
pixel_data, CAIRO_FORMAT_ARGB32, width, height, stride);
|
|
|
|
|
buffer->surface_owns_data = false;
|
2022-02-17 01:46:32 +01:00
|
|
|
return buffer;
|
|
|
|
|
}
|
2025-05-30 20:55:31 +09:00
|
|
|
|
2025-05-30 20:58:15 +09:00
|
|
|
struct lab_data_buffer *
|
|
|
|
|
buffer_create_from_wlr_buffer(struct wlr_buffer *wlr_buffer)
|
|
|
|
|
{
|
|
|
|
|
void *data;
|
|
|
|
|
uint32_t format;
|
|
|
|
|
size_t stride;
|
|
|
|
|
if (!wlr_buffer_begin_data_ptr_access(wlr_buffer,
|
|
|
|
|
WLR_BUFFER_DATA_PTR_ACCESS_READ, &data, &format, &stride)) {
|
|
|
|
|
wlr_log(WLR_ERROR, "failed to access wlr_buffer");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (format != DRM_FORMAT_ARGB8888) {
|
|
|
|
|
/* TODO: support other formats */
|
|
|
|
|
wlr_buffer_end_data_ptr_access(wlr_buffer);
|
|
|
|
|
wlr_log(WLR_ERROR, "cannot create buffer: format=%d", format);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
size_t buffer_size = stride * wlr_buffer->height;
|
|
|
|
|
void *copied_data = xmalloc(buffer_size);
|
|
|
|
|
memcpy(copied_data, data, buffer_size);
|
|
|
|
|
wlr_buffer_end_data_ptr_access(wlr_buffer);
|
|
|
|
|
|
|
|
|
|
return buffer_create_from_data(copied_data,
|
|
|
|
|
wlr_buffer->width, wlr_buffer->height, stride);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-30 20:55:31 +09:00
|
|
|
struct lab_data_buffer *
|
|
|
|
|
buffer_resize(struct lab_data_buffer *src_buffer, int width, int height,
|
|
|
|
|
double scale)
|
|
|
|
|
{
|
|
|
|
|
assert(src_buffer);
|
|
|
|
|
cairo_surface_t *surface = src_buffer->surface;
|
|
|
|
|
|
|
|
|
|
int src_w = cairo_image_surface_get_width(surface);
|
|
|
|
|
int src_h = cairo_image_surface_get_height(surface);
|
|
|
|
|
|
|
|
|
|
struct lab_data_buffer *buffer =
|
|
|
|
|
buffer_create_cairo(width, height, scale);
|
|
|
|
|
cairo_t *cairo = cairo_create(buffer->surface);
|
|
|
|
|
|
|
|
|
|
struct wlr_box container = {
|
|
|
|
|
.width = width,
|
|
|
|
|
.height = height,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct wlr_box dst_box = box_fit_within(src_w, src_h, &container);
|
|
|
|
|
double scene_scale = (double)dst_box.width / (double)src_w;
|
|
|
|
|
cairo_translate(cairo, dst_box.x, dst_box.y);
|
|
|
|
|
cairo_scale(cairo, scene_scale, scene_scale);
|
|
|
|
|
cairo_set_source_surface(cairo, surface, 0, 0);
|
|
|
|
|
cairo_pattern_set_filter(cairo_get_source(cairo), CAIRO_FILTER_GOOD);
|
|
|
|
|
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
|
|
|
|
|
cairo_paint(cairo);
|
|
|
|
|
|
|
|
|
|
cairo_surface_flush(buffer->surface);
|
|
|
|
|
cairo_destroy(cairo);
|
|
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|