This repository has been archived by the owner on Aug 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
buffers.c
159 lines (136 loc) · 3.94 KB
/
buffers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// Shared memory buffers
//
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include "oguri.h"
#include "buffers.h"
static int pid_shm_open(const char * prefix, int oflag, mode_t mode) {
static const char format[] = "%s-%d";
pid_t pid = getpid();
int length = snprintf(NULL, 0, format, prefix, pid);
if (length < 0) {
return -1;
}
char * name = calloc(1, length);
length = snprintf(name, length, format, prefix, pid);
if (length < 0) {
return -1;
}
int fd = shm_open(name, oflag, mode);
if (fd < 0) {
return -1;
}
shm_unlink(name);
free(name);
return fd;
}
static void buffer_handle_release(
void *data,
struct wl_buffer *wl_buffer __attribute__((unused))) {
((struct oguri_buffer *) data)->busy = false;
}
static const struct wl_buffer_listener buffer_listener = {
.release = buffer_handle_release,
};
struct oguri_buffer * oguri_allocate_buffer(struct oguri_output * output) {
uint32_t stride = cairo_format_stride_for_width(
CAIRO_FMT,
output->width * output->scale);
size_t size = stride * output->height * output->scale;
if (size < 1) {
fprintf(stderr, "Tiny buffer\n");
return NULL;
}
// O_EXCL shouldn't be necessary here, but I would rather have it fail if
// something weird happens.
errno = 0;
int fd = pid_shm_open("/oguri-buffer", O_RDWR | O_CREAT | O_EXCL, 0600);
if (fd < 0) {
fprintf(stderr, "Failed to create buffer backing memory: %s\n",
strerror(errno));
return NULL;
}
shm_unlink("/oguri-buffer");
errno = 0;
if (ftruncate(fd, size) < 0) {
fprintf(stderr, "Failed to resize buffer memory: %s\n",
strerror(errno));
close(fd);
return NULL;
}
errno = 0;
void * data = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (!data) {
fprintf(stderr, "Failed to map backing memory: %s\n", strerror(errno));
close(fd);
return NULL;
}
struct oguri_buffer * buffer = calloc(1, sizeof(struct oguri_buffer));
wl_list_init(&buffer->link);
struct wl_shm_pool * pool = wl_shm_create_pool(
output->oguri->shm, fd, size);
buffer->backing = wl_shm_pool_create_buffer(
pool, 0,
output->width * output->scale,
output->height * output->scale,
stride,
WL_SHM_FORMAT_ARGB8888);
wl_buffer_add_listener(buffer->backing, &buffer_listener, buffer);
wl_shm_pool_destroy(pool);
close(fd);
buffer->data = data;
buffer->size = size;
buffer->cairo_surface = cairo_image_surface_create_for_data(
data,
CAIRO_FMT,
output->width * output->scale,
output->height * output->scale,
stride);
buffer->cairo = cairo_create(buffer->cairo_surface);
wl_list_insert(output->buffer_ring.prev, &buffer->link);
return buffer;
}
bool oguri_allocate_buffers(struct oguri_output * output, unsigned int count) {
struct oguri_buffer * buffer;
// If we have too many buffers, shrink the pool instead to recover memory
// and prevent getting the animation out of sync.
if (output->buffer_count >= count) {
for (; output->buffer_count > count; --output->buffer_count) {
buffer = wl_container_of(output->buffer_ring.prev, buffer, link);
oguri_buffer_destroy(buffer);
}
}
else {
for (; output->buffer_count < count; ++output->buffer_count) {
buffer = oguri_allocate_buffer(output);
if (!buffer) {
return false;
}
}
}
return true;
}
struct oguri_buffer * oguri_next_buffer(struct oguri_output * output) {
struct oguri_buffer * current = wl_container_of(
output->buffer_ring.next, current, link);
wl_list_remove(¤t->link);
wl_list_insert(output->buffer_ring.prev, ¤t->link);
return wl_container_of(output->buffer_ring.next, current, link);
}
void oguri_buffer_destroy(struct oguri_buffer * buffer) {
wl_list_remove(&buffer->link);
cairo_destroy(buffer->cairo);
cairo_surface_destroy(buffer->cairo_surface);
wl_buffer_destroy(buffer->backing);
munmap(buffer->data, buffer->size);
free(buffer);
}