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 "config.h"
|
|
|
|
|
#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>
|
2022-02-11 22:19:05 +00:00
|
|
|
#include "buffer.h"
|
|
|
|
|
|
|
|
|
|
static const struct wlr_buffer_impl data_buffer_impl;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
data_buffer_destroy(struct wlr_buffer *wlr_buffer)
|
|
|
|
|
{
|
|
|
|
|
struct lab_data_buffer *buffer = data_buffer_from_buffer(wlr_buffer);
|
2022-02-17 01:46:32 +01:00
|
|
|
if (!buffer->free_on_destroy) {
|
|
|
|
|
free(buffer);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-02-13 13:00:26 +00:00
|
|
|
if (buffer->cairo) {
|
2022-02-13 21:59:14 +00:00
|
|
|
cairo_surface_t *surf = cairo_get_target(buffer->cairo);
|
2022-02-13 13:00:26 +00:00
|
|
|
cairo_destroy(buffer->cairo);
|
2022-02-13 21:59:14 +00:00
|
|
|
cairo_surface_destroy(surf);
|
2022-02-17 01:46:32 +01:00
|
|
|
} else if (buffer->data) {
|
|
|
|
|
free(buffer->data);
|
|
|
|
|
buffer->data = NULL;
|
2022-02-13 13:00:26 +00:00
|
|
|
}
|
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,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct lab_data_buffer *
|
2022-02-17 01:46:32 +01:00
|
|
|
buffer_create_cairo(uint32_t width, uint32_t height, float scale,
|
|
|
|
|
bool free_on_destroy)
|
2022-02-11 22:19:05 +00:00
|
|
|
{
|
|
|
|
|
struct lab_data_buffer *buffer = calloc(1, sizeof(*buffer));
|
2022-02-13 11:47:03 +00:00
|
|
|
if (!buffer) {
|
2022-02-11 22:19:05 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2022-06-12 21:11:25 +02:00
|
|
|
buffer->unscaled_width = width;
|
|
|
|
|
buffer->unscaled_height = height;
|
|
|
|
|
width *= scale;
|
|
|
|
|
height *= scale;
|
2022-02-13 11:47:03 +00:00
|
|
|
|
2022-06-12 21:11:25 +02:00
|
|
|
/* Allocate the buffer with the scaled size */
|
|
|
|
|
wlr_buffer_init(&buffer->base, &data_buffer_impl, width, height);
|
2022-02-13 13:00:26 +00:00
|
|
|
cairo_surface_t *surf =
|
|
|
|
|
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
|
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
|
|
|
|
|
*/
|
2022-02-13 13:00:26 +00:00
|
|
|
cairo_surface_set_device_scale(surf, scale, scale);
|
|
|
|
|
|
|
|
|
|
buffer->cairo = cairo_create(surf);
|
2022-02-13 11:47:03 +00:00
|
|
|
buffer->data = cairo_image_surface_get_data(surf);
|
|
|
|
|
buffer->format = DRM_FORMAT_ARGB8888;
|
|
|
|
|
buffer->stride = cairo_image_surface_get_stride(surf);
|
2022-02-17 01:46:32 +01:00
|
|
|
buffer->free_on_destroy = free_on_destroy;
|
2022-02-13 11:47:03 +00:00
|
|
|
|
|
|
|
|
if (!buffer->data) {
|
2022-02-13 13:00:26 +00:00
|
|
|
cairo_destroy(buffer->cairo);
|
2022-02-13 21:59:14 +00:00
|
|
|
cairo_surface_destroy(surf);
|
2022-02-13 11:47:03 +00:00
|
|
|
free(buffer);
|
2022-02-13 21:59:14 +00:00
|
|
|
buffer = NULL;
|
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 *
|
|
|
|
|
buffer_create_wrap(void *pixel_data, uint32_t width, uint32_t height,
|
|
|
|
|
uint32_t stride, bool free_on_destroy)
|
|
|
|
|
{
|
|
|
|
|
struct lab_data_buffer *buffer = calloc(1, sizeof(*buffer));
|
|
|
|
|
if (!buffer) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
wlr_buffer_init(&buffer->base, &data_buffer_impl, width, height);
|
|
|
|
|
buffer->data = pixel_data;
|
|
|
|
|
buffer->format = DRM_FORMAT_ARGB8888;
|
|
|
|
|
buffer->stride = stride;
|
|
|
|
|
buffer->free_on_destroy = free_on_destroy;
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|