mirror of
https://gitlab.freedesktop.org/cairo/cairo.git
synced 2026-05-30 13:08:15 +02:00
Add a new surface backend that allocates pixel buffers through GBM (Generic Buffer Manager), allowing Cairo-rendered content to be imported directly by GPU compositors via DMA-BUF file descriptors without an intermediate memory copy. The backend renders on the CPU via pixman (like the image backend) but allocates its buffer through GBM with a linear modifier, making it suitable for zero-copy handoff to GPU-based compositors such as Mutter/Clutter (GNOME) or GSK (GTK 4). New public API (cairo-gbm.h): - cairo_gbm_surface_create() — allocate a new GBM-backed surface - cairo_gbm_surface_create_for_bo() — wrap an existing gbm_bo - cairo_gbm_surface_get_bo() — retrieve the underlying gbm_bo - cairo_gbm_surface_get_dma_buf_fd() — export a DMA-BUF fd for the buffer Build integration: - New 'gbm' meson option (auto-detected via libgbm + libdrm) - CAIRO_HAS_GBM_SURFACE feature flag Test coverage: - Dedicated API + error-path tests (test/gbm-surface.c) - Surface-as-source pattern test (test/gbm-surface-source.c) - GBM boilerplate target for the full test suite - Entries in api-special-cases.c and error-setters.c
438 lines
13 KiB
C
438 lines
13 KiB
C
/*
|
|
* Copyright © 2026 Red Hat, Inc.
|
|
*
|
|
* 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
|
|
* Red Hat, Inc. not be used in advertising or publicity pertaining to
|
|
* distribution of the software without specific, written prior
|
|
* permission. Red Hat, Inc. makes no representations about the
|
|
* suitability of this software for any purpose. It is provided "as
|
|
* is" without express or implied warranty.
|
|
*
|
|
* RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
|
|
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
* FITNESS, IN NO EVENT SHALL RED HAT, INC. 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.
|
|
*
|
|
* Author: Alberto Ruiz <aruiz@redhat.com>
|
|
*/
|
|
|
|
#include "cairo-test.h"
|
|
|
|
#include <cairo-gbm.h>
|
|
|
|
#include <gbm.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
|
|
static struct gbm_device *
|
|
open_gbm_device (int *fd_out)
|
|
{
|
|
int fd;
|
|
struct gbm_device *dev;
|
|
|
|
fd = open ("/dev/dri/renderD128", O_RDWR);
|
|
if (fd < 0)
|
|
return NULL;
|
|
|
|
dev = gbm_create_device (fd);
|
|
if (dev == NULL) {
|
|
close (fd);
|
|
return NULL;
|
|
}
|
|
|
|
*fd_out = fd;
|
|
return dev;
|
|
}
|
|
|
|
static cairo_test_status_t
|
|
test_create_valid (struct gbm_device *dev, cairo_test_context_t *ctx)
|
|
{
|
|
cairo_surface_t *surface;
|
|
|
|
surface = cairo_gbm_surface_create (dev, CAIRO_FORMAT_ARGB32, 100, 100);
|
|
if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS) {
|
|
cairo_test_log (ctx, "Failed to create ARGB32 GBM surface\n");
|
|
cairo_surface_destroy (surface);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
if (cairo_surface_get_type (surface) != CAIRO_SURFACE_TYPE_GBM) {
|
|
cairo_test_log (ctx, "Surface type is not CAIRO_SURFACE_TYPE_GBM\n");
|
|
cairo_surface_destroy (surface);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
cairo_surface_destroy (surface);
|
|
|
|
surface = cairo_gbm_surface_create (dev, CAIRO_FORMAT_RGB24, 100, 100);
|
|
if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS) {
|
|
cairo_test_log (ctx, "Failed to create RGB24 GBM surface\n");
|
|
cairo_surface_destroy (surface);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
cairo_surface_destroy (surface);
|
|
|
|
return CAIRO_TEST_SUCCESS;
|
|
}
|
|
|
|
static cairo_test_status_t
|
|
test_create_error_paths (cairo_test_context_t *ctx)
|
|
{
|
|
cairo_surface_t *surface;
|
|
|
|
/* _cairo_surface_create_in_error() has no dedicated nil surface for
|
|
* CAIRO_STATUS_NULL_POINTER, so it falls through to NO_MEMORY. */
|
|
surface = cairo_gbm_surface_create (NULL, CAIRO_FORMAT_ARGB32, 100, 100);
|
|
if (cairo_surface_status (surface) != CAIRO_STATUS_NO_MEMORY) {
|
|
cairo_test_log (ctx, "NULL device: expected NO_MEMORY, got %d\n",
|
|
cairo_surface_status (surface));
|
|
cairo_surface_destroy (surface);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
cairo_surface_destroy (surface);
|
|
|
|
surface = cairo_gbm_surface_create (NULL, CAIRO_FORMAT_INVALID, 100, 100);
|
|
if (cairo_surface_status (surface) != CAIRO_STATUS_NO_MEMORY) {
|
|
cairo_test_log (ctx, "NULL device + invalid format: expected NO_MEMORY, got %d\n",
|
|
cairo_surface_status (surface));
|
|
cairo_surface_destroy (surface);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
cairo_surface_destroy (surface);
|
|
|
|
return CAIRO_TEST_SUCCESS;
|
|
}
|
|
|
|
static cairo_test_status_t
|
|
test_create_invalid_format (struct gbm_device *dev, cairo_test_context_t *ctx)
|
|
{
|
|
cairo_surface_t *surface;
|
|
|
|
surface = cairo_gbm_surface_create (dev, CAIRO_FORMAT_INVALID, 100, 100);
|
|
if (cairo_surface_status (surface) != CAIRO_STATUS_INVALID_FORMAT) {
|
|
cairo_test_log (ctx, "INVALID format: expected INVALID_FORMAT, got %d\n",
|
|
cairo_surface_status (surface));
|
|
cairo_surface_destroy (surface);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
cairo_surface_destroy (surface);
|
|
|
|
surface = cairo_gbm_surface_create (dev, CAIRO_FORMAT_A1, 100, 100);
|
|
if (cairo_surface_status (surface) != CAIRO_STATUS_INVALID_FORMAT) {
|
|
cairo_test_log (ctx, "A1 format (no DRM equivalent): expected INVALID_FORMAT, got %d\n",
|
|
cairo_surface_status (surface));
|
|
cairo_surface_destroy (surface);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
cairo_surface_destroy (surface);
|
|
|
|
surface = cairo_gbm_surface_create (dev, CAIRO_FORMAT_RGB96F, 100, 100);
|
|
if (cairo_surface_status (surface) != CAIRO_STATUS_INVALID_FORMAT) {
|
|
cairo_test_log (ctx, "RGB96F format (no DRM equivalent): expected INVALID_FORMAT, got %d\n",
|
|
cairo_surface_status (surface));
|
|
cairo_surface_destroy (surface);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
cairo_surface_destroy (surface);
|
|
|
|
return CAIRO_TEST_SUCCESS;
|
|
}
|
|
|
|
static cairo_test_status_t
|
|
test_create_invalid_size (struct gbm_device *dev, cairo_test_context_t *ctx)
|
|
{
|
|
cairo_surface_t *surface;
|
|
|
|
surface = cairo_gbm_surface_create (dev, CAIRO_FORMAT_ARGB32, 0, 100);
|
|
if (cairo_surface_status (surface) != CAIRO_STATUS_INVALID_SIZE) {
|
|
cairo_test_log (ctx, "Zero width: expected INVALID_SIZE, got %d\n",
|
|
cairo_surface_status (surface));
|
|
cairo_surface_destroy (surface);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
cairo_surface_destroy (surface);
|
|
|
|
surface = cairo_gbm_surface_create (dev, CAIRO_FORMAT_ARGB32, 100, 0);
|
|
if (cairo_surface_status (surface) != CAIRO_STATUS_INVALID_SIZE) {
|
|
cairo_test_log (ctx, "Zero height: expected INVALID_SIZE, got %d\n",
|
|
cairo_surface_status (surface));
|
|
cairo_surface_destroy (surface);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
cairo_surface_destroy (surface);
|
|
|
|
surface = cairo_gbm_surface_create (dev, CAIRO_FORMAT_ARGB32, -1, 100);
|
|
if (cairo_surface_status (surface) != CAIRO_STATUS_INVALID_SIZE) {
|
|
cairo_test_log (ctx, "Negative width: expected INVALID_SIZE, got %d\n",
|
|
cairo_surface_status (surface));
|
|
cairo_surface_destroy (surface);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
cairo_surface_destroy (surface);
|
|
|
|
return CAIRO_TEST_SUCCESS;
|
|
}
|
|
|
|
static cairo_test_status_t
|
|
test_create_for_bo (struct gbm_device *dev, cairo_test_context_t *ctx)
|
|
{
|
|
cairo_surface_t *surface;
|
|
struct gbm_bo *bo;
|
|
|
|
bo = gbm_bo_create (dev, 64, 64, GBM_FORMAT_ARGB8888,
|
|
GBM_BO_USE_LINEAR | GBM_BO_USE_RENDERING);
|
|
if (bo == NULL) {
|
|
cairo_test_log (ctx, "Failed to create gbm_bo for test\n");
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
|
|
surface = cairo_gbm_surface_create_for_bo (bo, CAIRO_FORMAT_ARGB32);
|
|
if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS) {
|
|
cairo_test_log (ctx, "Failed to create surface for bo\n");
|
|
cairo_surface_destroy (surface);
|
|
gbm_bo_destroy (bo);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
if (cairo_surface_get_type (surface) != CAIRO_SURFACE_TYPE_GBM) {
|
|
cairo_test_log (ctx, "create_for_bo: type is not GBM\n");
|
|
cairo_surface_destroy (surface);
|
|
gbm_bo_destroy (bo);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
|
|
/* Verify get_bo returns the same bo */
|
|
if (cairo_gbm_surface_get_bo (surface) != bo) {
|
|
cairo_test_log (ctx, "create_for_bo: get_bo returned wrong pointer\n");
|
|
cairo_surface_destroy (surface);
|
|
gbm_bo_destroy (bo);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
|
|
/* Destroy surface — bo should survive (caller ownership) */
|
|
cairo_surface_destroy (surface);
|
|
gbm_bo_destroy (bo);
|
|
|
|
return CAIRO_TEST_SUCCESS;
|
|
}
|
|
|
|
static cairo_test_status_t
|
|
test_create_for_bo_error_paths (struct gbm_device *dev, cairo_test_context_t *ctx)
|
|
{
|
|
cairo_surface_t *surface;
|
|
struct gbm_bo *bo;
|
|
|
|
surface = cairo_gbm_surface_create_for_bo (NULL, CAIRO_FORMAT_ARGB32);
|
|
if (cairo_surface_status (surface) != CAIRO_STATUS_NO_MEMORY) {
|
|
cairo_test_log (ctx, "NULL bo: expected NO_MEMORY, got %d\n",
|
|
cairo_surface_status (surface));
|
|
cairo_surface_destroy (surface);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
cairo_surface_destroy (surface);
|
|
|
|
bo = gbm_bo_create (dev, 64, 64, GBM_FORMAT_ARGB8888,
|
|
GBM_BO_USE_LINEAR | GBM_BO_USE_RENDERING);
|
|
if (bo == NULL) {
|
|
cairo_test_log (ctx, "Failed to create gbm_bo for error test\n");
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
|
|
surface = cairo_gbm_surface_create_for_bo (bo, CAIRO_FORMAT_INVALID);
|
|
if (cairo_surface_status (surface) != CAIRO_STATUS_INVALID_FORMAT) {
|
|
cairo_test_log (ctx, "Invalid format with valid bo: expected INVALID_FORMAT, got %d\n",
|
|
cairo_surface_status (surface));
|
|
cairo_surface_destroy (surface);
|
|
gbm_bo_destroy (bo);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
cairo_surface_destroy (surface);
|
|
gbm_bo_destroy (bo);
|
|
|
|
return CAIRO_TEST_SUCCESS;
|
|
}
|
|
|
|
static cairo_test_status_t
|
|
test_getters_wrong_type (cairo_test_context_t *ctx)
|
|
{
|
|
cairo_surface_t *image;
|
|
|
|
image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 10, 10);
|
|
|
|
if (cairo_gbm_surface_get_bo (image) != NULL) {
|
|
cairo_test_log (ctx, "get_bo on image surface should return NULL\n");
|
|
cairo_surface_destroy (image);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
if (cairo_gbm_surface_get_dma_buf_fd (image) != -1) {
|
|
cairo_test_log (ctx, "get_dma_buf_fd on image surface should return -1\n");
|
|
cairo_surface_destroy (image);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
|
|
cairo_surface_destroy (image);
|
|
return CAIRO_TEST_SUCCESS;
|
|
}
|
|
|
|
static cairo_test_status_t
|
|
test_dma_buf_fd (struct gbm_device *dev, cairo_test_context_t *ctx)
|
|
{
|
|
cairo_surface_t *surface;
|
|
int fd;
|
|
|
|
surface = cairo_gbm_surface_create (dev, CAIRO_FORMAT_ARGB32, 64, 64);
|
|
if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS) {
|
|
cairo_test_log (ctx, "Failed to create surface for fd test\n");
|
|
cairo_surface_destroy (surface);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
|
|
fd = cairo_gbm_surface_get_dma_buf_fd (surface);
|
|
if (fd < 0) {
|
|
cairo_test_log (ctx, "get_dma_buf_fd returned %d\n", fd);
|
|
cairo_surface_destroy (surface);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
close (fd);
|
|
|
|
cairo_surface_destroy (surface);
|
|
return CAIRO_TEST_SUCCESS;
|
|
}
|
|
|
|
static cairo_test_status_t
|
|
test_draw_and_flush (struct gbm_device *dev, cairo_test_context_t *ctx)
|
|
{
|
|
cairo_surface_t *surface;
|
|
cairo_t *cr;
|
|
int fd;
|
|
|
|
surface = cairo_gbm_surface_create (dev, CAIRO_FORMAT_ARGB32, 64, 64);
|
|
if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS) {
|
|
cairo_test_log (ctx, "Failed to create surface for draw test\n");
|
|
cairo_surface_destroy (surface);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
|
|
cr = cairo_create (surface);
|
|
cairo_set_source_rgb (cr, 1, 0, 0);
|
|
cairo_rectangle (cr, 0, 0, 32, 32);
|
|
cairo_fill (cr);
|
|
if (cairo_status (cr) != CAIRO_STATUS_SUCCESS) {
|
|
cairo_test_log (ctx, "Drawing failed: %d\n", cairo_status (cr));
|
|
cairo_destroy (cr);
|
|
cairo_surface_destroy (surface);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
cairo_destroy (cr);
|
|
|
|
cairo_surface_flush (surface);
|
|
|
|
fd = cairo_gbm_surface_get_dma_buf_fd (surface);
|
|
if (fd < 0) {
|
|
cairo_test_log (ctx, "get_dma_buf_fd after flush returned %d\n", fd);
|
|
cairo_surface_destroy (surface);
|
|
return CAIRO_TEST_FAILURE;
|
|
}
|
|
close (fd);
|
|
|
|
cairo_surface_destroy (surface);
|
|
return CAIRO_TEST_SUCCESS;
|
|
}
|
|
|
|
static cairo_test_status_t
|
|
test_all_supported_formats (struct gbm_device *dev, cairo_test_context_t *ctx)
|
|
{
|
|
static const cairo_format_t formats[] = {
|
|
CAIRO_FORMAT_ARGB32,
|
|
CAIRO_FORMAT_RGB24,
|
|
CAIRO_FORMAT_RGB30,
|
|
CAIRO_FORMAT_RGB16_565,
|
|
CAIRO_FORMAT_A8,
|
|
};
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < sizeof (formats) / sizeof (formats[0]); i++) {
|
|
cairo_surface_t *surface;
|
|
|
|
surface = cairo_gbm_surface_create (dev, formats[i], 64, 64);
|
|
if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS) {
|
|
cairo_test_log (ctx,
|
|
"Format %d: create failed with status %d "
|
|
"(GPU may not support this DRM format, skipping)\n",
|
|
formats[i], cairo_surface_status (surface));
|
|
cairo_surface_destroy (surface);
|
|
continue;
|
|
}
|
|
cairo_surface_destroy (surface);
|
|
}
|
|
|
|
return CAIRO_TEST_SUCCESS;
|
|
}
|
|
|
|
static cairo_test_status_t
|
|
preamble (cairo_test_context_t *ctx)
|
|
{
|
|
struct gbm_device *dev;
|
|
int drm_fd;
|
|
cairo_test_status_t status;
|
|
|
|
status = test_create_error_paths (ctx);
|
|
if (status != CAIRO_TEST_SUCCESS)
|
|
return status;
|
|
|
|
status = test_getters_wrong_type (ctx);
|
|
if (status != CAIRO_TEST_SUCCESS)
|
|
return status;
|
|
|
|
dev = open_gbm_device (&drm_fd);
|
|
if (dev == NULL) {
|
|
cairo_test_log (ctx, "No DRM render node available, skipping\n");
|
|
return CAIRO_TEST_UNTESTED;
|
|
}
|
|
|
|
status = test_create_valid (dev, ctx);
|
|
if (status != CAIRO_TEST_SUCCESS)
|
|
goto CLEANUP;
|
|
|
|
status = test_create_invalid_format (dev, ctx);
|
|
if (status != CAIRO_TEST_SUCCESS)
|
|
goto CLEANUP;
|
|
|
|
status = test_create_invalid_size (dev, ctx);
|
|
if (status != CAIRO_TEST_SUCCESS)
|
|
goto CLEANUP;
|
|
|
|
status = test_create_for_bo (dev, ctx);
|
|
if (status != CAIRO_TEST_SUCCESS)
|
|
goto CLEANUP;
|
|
|
|
status = test_create_for_bo_error_paths (dev, ctx);
|
|
if (status != CAIRO_TEST_SUCCESS)
|
|
goto CLEANUP;
|
|
|
|
status = test_dma_buf_fd (dev, ctx);
|
|
if (status != CAIRO_TEST_SUCCESS)
|
|
goto CLEANUP;
|
|
|
|
status = test_draw_and_flush (dev, ctx);
|
|
if (status != CAIRO_TEST_SUCCESS)
|
|
goto CLEANUP;
|
|
|
|
status = test_all_supported_formats (dev, ctx);
|
|
|
|
CLEANUP:
|
|
gbm_device_destroy (dev);
|
|
close (drm_fd);
|
|
return status;
|
|
}
|
|
|
|
CAIRO_TEST (gbm_surface,
|
|
"Test GBM surface API: creation, error handling, getters, draw, flush, DMA-BUF fd",
|
|
"gbm, api", /* keywords */
|
|
NULL, /* requirements */
|
|
0, 0,
|
|
preamble, NULL)
|