Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 76 additions & 5 deletions render/allocator/allocator.c → allocator/allocator.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <drm_fourcc.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <wlr/allocator/interface.h>
#include <wlr/render/drm_format_set.h>
#include <wlr/util/log.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include "backend/backend.h"
#include "render/allocator/allocator.h"
#include "render/allocator/drm_dumb.h"
#include "render/allocator/gbm.h"
#include "render/allocator/shm.h"
#include "allocator/allocator.h"
#include "allocator/drm_dumb.h"
#include "allocator/gbm.h"
#include "allocator/shm.h"
#include "render/drm_format_set.h"
#include "render/swapchain.h"
#include "render/wlr_renderer.h"

void wlr_allocator_init(struct wlr_allocator *alloc,
Expand Down Expand Up @@ -138,7 +143,18 @@ struct wlr_allocator *wlr_allocator_autocreate(struct wlr_backend *backend,
struct wlr_renderer *renderer) {
// Note, drm_fd may be negative if unavailable
int drm_fd = wlr_backend_get_drm_fd(backend);
return allocator_autocreate_with_drm_fd(backend, renderer, drm_fd);
struct wlr_allocator *alloc = allocator_autocreate_with_drm_fd(backend,
renderer, drm_fd);
if (alloc != NULL) {
alloc->render_formats = wlr_renderer_get_render_formats(renderer);
if (alloc->render_formats == NULL) {
wlr_log(WLR_ERROR, "Failed to get render formats");
free(alloc);
return NULL;
}
}

return alloc;
}

void wlr_allocator_destroy(struct wlr_allocator *alloc) {
Expand All @@ -149,6 +165,61 @@ void wlr_allocator_destroy(struct wlr_allocator *alloc) {
alloc->impl->destroy(alloc);
}

struct wlr_swapchain *wlr_allocator_create_swapchain(struct wlr_allocator *alloc,
int width, int height, const struct wlr_drm_format_set *display_formats,
bool allow_modifiers) {
static const uint32_t candidates[] = {
DRM_FORMAT_ARGB8888, DRM_FORMAT_XRGB8888
};

struct wlr_drm_format *format = NULL;
for (size_t i = 0; i < sizeof(candidates) / sizeof(candidates[0]); i++) {
uint32_t fmt = candidates[i];

const struct wlr_drm_format *render_format =
wlr_drm_format_set_get(alloc->render_formats, fmt);
if (render_format == NULL) {
wlr_log(WLR_DEBUG, "Renderer doesn't support format 0x%"PRIX32, fmt);
continue;
}

if (display_formats != NULL) {
const struct wlr_drm_format *display_format =
wlr_drm_format_set_get(display_formats, fmt);
if (display_format == NULL) {
wlr_log(WLR_DEBUG, "Output doesn't support format 0x%"PRIX32, fmt);
continue;
}
format = wlr_drm_format_intersect(display_format, render_format);
} else {
// The output can display any format
format = wlr_drm_format_dup(render_format);
}

if (format == NULL) {
wlr_log(WLR_DEBUG, "Failed to intersect display and render "
"modifiers for format 0x%"PRIX32, fmt);
} else {
break;
}
}

if (format == NULL) {
wlr_log(WLR_ERROR, "Failed to choose a swapchain format");
return NULL;
}

if (!allow_modifiers
&& (format->len != 1 || format->modifiers[0] != DRM_FORMAT_MOD_LINEAR)) {
format->len = 0;
}

wlr_log(WLR_DEBUG, "Choosing primary buffer format 0x%"PRIX32,
format->format);

return wlr_swapchain_create(alloc, width, height, format);
}

struct wlr_buffer *wlr_allocator_create_buffer(struct wlr_allocator *alloc,
int width, int height, const struct wlr_drm_format *format) {
struct wlr_buffer *buffer =
Expand Down
8 changes: 5 additions & 3 deletions render/allocator/drm_dumb.c → allocator/drm_dumb.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include <wlr/allocator/interface.h>
#include <wlr/backend.h>
#include <wlr/backend/session.h>
#include <wlr/render/drm_format_set.h>
#include <wlr/util/log.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <wlr/backend.h>
#include <wlr/backend/session.h>

#include "render/allocator/drm_dumb.h"
#include "allocator/drm_dumb.h"
#include "render/pixel_format.h"

static const struct wlr_buffer_impl buffer_impl;
Expand Down
5 changes: 4 additions & 1 deletion render/allocator/gbm.c → allocator/gbm.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <wlr/allocator/interface.h>
#include <wlr/render/drm_format_set.h>
#include <wlr/util/log.h>
#include <xf86drm.h>
#include "render/allocator/gbm.h"

#include "allocator/gbm.h"

static const struct wlr_buffer_impl buffer_impl;

Expand Down
File renamed without changes.
5 changes: 4 additions & 1 deletion render/allocator/shm.c → allocator/shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
#include <wlr/allocator/interface.h>
#include <wlr/render/drm_format_set.h>
#include <wlr/util/log.h>

#include "allocator/shm.h"
#include "render/pixel_format.h"
#include "render/allocator/shm.h"
#include "util/shm.h"

static const struct wlr_buffer_impl buffer_impl;
Expand Down
2 changes: 1 addition & 1 deletion backend/backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
#include <wlr/config.h>
#include <wlr/render/wlr_renderer.h>
#include <wlr/util/log.h>
#include "allocator/allocator.h"
#include "backend/backend.h"
#include "backend/multi.h"
#include "render/allocator/allocator.h"
#include "util/signal.h"

#if WLR_HAS_DRM_BACKEND
Expand Down
2 changes: 1 addition & 1 deletion backend/drm/renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
#include <wlr/render/wlr_renderer.h>
#include <wlr/types/wlr_matrix.h>
#include <wlr/util/log.h>
#include "allocator/allocator.h"
#include "backend/drm/drm.h"
#include "backend/drm/util.h"
#include "render/drm_format_set.h"
#include "render/allocator/allocator.h"
#include "render/pixel_format.h"
#include "render/swapchain.h"
#include "render/wlr_renderer.h"
Expand Down
23 changes: 23 additions & 0 deletions include/allocator/allocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef RENDER_ALLOCATOR_ALLOCATOR_H
#define RENDER_ALLOCATOR_ALLOCATOR_H

#include <wlr/allocator/wlr_allocator.h>

struct wlr_allocator;
struct wlr_backend;
struct wlr_drm_format;
struct wlr_renderer;

/**
* Allocate a new buffer.
*
* When the caller is done with it, they must unreference it by calling
* wlr_buffer_drop.
*/
struct wlr_buffer *wlr_allocator_create_buffer(struct wlr_allocator *alloc,
int width, int height, const struct wlr_drm_format *format);

struct wlr_allocator *allocator_autocreate_with_drm_fd(
struct wlr_backend *backend, struct wlr_renderer *renderer, int drm_fd);

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <wlr/render/dmabuf.h>
#include <wlr/types/wlr_buffer.h>
#include "render/allocator/allocator.h"
#include "allocator/allocator.h"

struct wlr_drm_dumb_buffer {
struct wlr_buffer base;
Expand Down
2 changes: 1 addition & 1 deletion include/render/allocator/gbm.h → include/allocator/gbm.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <gbm.h>
#include <wlr/render/dmabuf.h>
#include <wlr/types/wlr_buffer.h>
#include "render/allocator/allocator.h"
#include "allocator/allocator.h"

struct wlr_gbm_buffer {
struct wlr_buffer base;
Expand Down
2 changes: 1 addition & 1 deletion include/render/allocator/shm.h → include/allocator/shm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define RENDER_ALLOCATOR_SHM_H

#include <wlr/types/wlr_buffer.h>
#include "render/allocator/allocator.h"
#include "allocator/allocator.h"

struct wlr_shm_buffer {
struct wlr_buffer base;
Expand Down
54 changes: 0 additions & 54 deletions include/render/allocator/allocator.h

This file was deleted.

2 changes: 0 additions & 2 deletions include/types/wlr_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
void output_pending_resolution(struct wlr_output *output, int *width,
int *height);

struct wlr_drm_format *output_pick_format(struct wlr_output *output,
const struct wlr_drm_format_set *display_formats);
void output_clear_back_buffer(struct wlr_output *output);
bool output_ensure_buffer(struct wlr_output *output);

Expand Down
26 changes: 26 additions & 0 deletions include/wlr/allocator/interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* This an unstable interface of wlroots. No guarantees are made regarding the
* future consistency of this API.
*/
#ifndef WLR_USE_UNSTABLE
#error "Add -DWLR_USE_UNSTABLE to enable unstable wlroots features"
#endif

#ifndef WLR_ALLOCATOR_INTERFACE_H
#define WLR_ALLOCATOR_INTERFACE_H

#include <stdint.h>

struct wlr_allocator;
struct wlr_drm_format;

struct wlr_allocator_interface {
struct wlr_buffer *(*create_buffer)(struct wlr_allocator *alloc,
int width, int height, const struct wlr_drm_format *format);
void (*destroy)(struct wlr_allocator *alloc);
};

void wlr_allocator_init(struct wlr_allocator *alloc,
const struct wlr_allocator_interface *impl, uint32_t buffer_caps);

#endif
42 changes: 42 additions & 0 deletions include/wlr/allocator/wlr_allocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef WLR_ALLOCATOR_H
#define WLR_ALLOCATOR_H

#include <wayland-server-core.h>

struct wlr_allocator_interface;
struct wlr_backend;
struct wlr_drm_format_set;
struct wlr_renderer;

struct wlr_allocator {
const struct wlr_allocator_interface *impl;

// Capabilities of the buffers created with this allocator
uint32_t buffer_caps;

const struct wlr_drm_format_set *render_formats;

struct {
struct wl_signal destroy;
} events;
};

/**
* Creates the adequate wlr_allocator given a backend and a renderer
*/
struct wlr_allocator *wlr_allocator_autocreate(struct wlr_backend *backend,
struct wlr_renderer *renderer);
/**
* Destroy the allocator.
*/
void wlr_allocator_destroy(struct wlr_allocator *alloc);

/**
* Create a new swapchain with the given width and height. The allocator will
* find the best suited format in provided wlr_drm_format_set
*/
struct wlr_swapchain *wlr_allocator_create_swapchain(struct wlr_allocator *alloc,
int width, int height, const struct wlr_drm_format_set *display_formats,
bool allow_modifiers);

#endif
12 changes: 12 additions & 0 deletions include/wlr/types/wlr_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <wlr/types/wlr_buffer.h>
#include <wlr/util/addon.h>

struct wlr_allocator;

struct wlr_output_mode {
int32_t width, height;
int32_t refresh; // mHz
Expand Down Expand Up @@ -112,6 +114,7 @@ struct wlr_output_impl;
struct wlr_output {
const struct wlr_output_impl *impl;
struct wlr_backend *backend;
struct wlr_allocator *allocator;
struct wl_display *display;

struct wl_global *global;
Expand Down Expand Up @@ -304,6 +307,15 @@ void wlr_output_set_scale(struct wlr_output *output, float scale);
void wlr_output_set_subpixel(struct wlr_output *output,
enum wl_output_subpixel subpixel);
void wlr_output_set_description(struct wlr_output *output, const char *desc);
/**
* Sets the output allocator. The allocator and the output backend must have
* matching buffer capabilities. Returns false otherwise.
*
* The allocator is responsible for swapchain managment inside the output,
* the swapchain provides a place for the output to write pixels.
*/
bool wlr_output_set_allocator(struct wlr_output *output,
struct wlr_allocator *allocator);
/**
* Schedule a done event.
*
Expand Down
Loading