-
Notifications
You must be signed in to change notification settings - Fork 1
/
buffer.c
135 lines (121 loc) · 3.81 KB
/
buffer.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
#include "wlchewing.h"
#include "buffer.h"
#include <fcntl.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
static void handle_release(void *data, struct wl_buffer *wl_buffer) {
struct wlchewing_buffer *buffer = data;
buffer->available = true;
}
static const struct wl_buffer_listener buffer_listener = {
.release = handle_release,
};
static void mktempname(char *template) {
struct timespec ts;
unsigned long r;
clock_gettime(CLOCK_REALTIME, &ts);
r = (ts.tv_nsec * 65537) ^ ((uintptr_t)&ts / 16 + (uintptr_t)template);
for (int i = 0; i < 6; i++, r >>= 5)
template[i] = 'A' + ( r & 15 ) + ( r & 16 ) * 2;
}
struct wlchewing_buffer *buffer_new(struct wl_shm *shm,
uint32_t width, uint32_t height, uint32_t scale) {
struct wlchewing_buffer *buffer = calloc(1, sizeof(struct wlchewing_buffer));
if (buffer == NULL){
wlchewing_err("Failed to calloc for wlchewing_buffer");
return NULL;
}
buffer->width = width;
buffer->height = height;
buffer->scale = scale;
buffer->shm = shm;
char *template = strdup("/wlchewing-XXXXXX");
mktempname(&template[11]);
int fd = shm_open(template, O_RDWR | O_CREAT | O_EXCL, 0600);
if (fd < 0) {
wlchewing_err("Failed to shm_open");
free(buffer);
return NULL;
}
shm_unlink(template);
free(template);
off_t stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32,
width * scale);
buffer->size = (height * scale) * stride;
int ret = ftruncate(fd, buffer->size);
if (ret == -1) {
wlchewing_err("Failed to ftruncate");
free(buffer);
return NULL;
}
buffer->data = mmap(NULL, buffer->size, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
if (buffer->data == MAP_FAILED) {
wlchewing_err("Failed to mmap %ld", buffer->size);
close(fd);
free(buffer);
return NULL;
}
struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, buffer->size);
buffer->wl_buffer = wl_shm_pool_create_buffer(pool, 0, width * scale,
height * scale, stride, WL_SHM_FORMAT_ARGB8888);
wl_buffer_add_listener(buffer->wl_buffer, &buffer_listener, buffer);
wl_shm_pool_destroy(pool);
close(fd);
buffer->cairo_surface = cairo_image_surface_create_for_data(
buffer->data, CAIRO_FORMAT_ARGB32, width * scale,
height * scale, stride);
buffer->cairo = cairo_create(buffer->cairo_surface);
cairo_scale(buffer->cairo, scale, scale);
return buffer;
}
void buffer_destroy(struct wlchewing_buffer *buffer) {
wl_buffer_destroy(buffer->wl_buffer);
cairo_destroy(buffer->cairo);
cairo_surface_destroy(buffer->cairo_surface);
munmap(buffer->data, buffer->size);
free(buffer);
}
struct wl_list *buffer_pool_new(struct wl_shm *shm,
uint32_t width, uint32_t height, uint32_t scale) {
struct wl_list *pool = calloc(1, sizeof(struct wl_list));
if (pool == NULL) {
wlchewing_err("Failed to calloc wl_list for buffer pool");
return NULL;
}
wl_list_init(pool);
struct wlchewing_buffer *buffer = buffer_new(shm, width, height, scale);
buffer->available = true;
if (buffer == NULL) {
free(pool);
wlchewing_err("Failed to create first buffer for buffer pool");
return NULL;
}
wl_list_insert(pool, &buffer->link);
return pool;
}
struct wlchewing_buffer *buffer_pool_get_buffer(struct wl_list *pool) {
struct wlchewing_buffer *cur_buffer, *last_buffer;
wl_list_for_each(cur_buffer, pool, link) {
if (cur_buffer->available) {
cur_buffer->available = false;
return cur_buffer;
}
last_buffer = cur_buffer;
}
printf("new buffer\n");
struct wlchewing_buffer *new_buffer = buffer_new(last_buffer->shm,
last_buffer->width, last_buffer->height, last_buffer->scale);
wl_list_insert(&last_buffer->link, &new_buffer->link);
return new_buffer;
}
void buffer_pool_destroy(struct wl_list *pool) {
struct wlchewing_buffer *cur_buffer, *tmp;
wl_list_for_each_safe(cur_buffer, tmp, pool, link) {
wl_list_remove(&cur_buffer->link);
buffer_destroy(cur_buffer);
}
free(pool);
}