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
|
#include <sys/mman.h>
#include <unistd.h>
#include <math.h>
#include <stdlib.h>
#include "drw.h"
#include "util.h"
/* static function declarations */
static Color to_color(uint32_t color);
static uint32_t to_uint32_t(Color color);
Canvas*
create_drw(struct wl_shm *shm, unsigned width, unsigned height)
{
struct wl_shm_pool *pool;
Canvas *canvas;
int stride;
int fd;
canvas = malloc(sizeof(Canvas));
canvas->width = width;
canvas->height = height;
stride = width * sizeof(uint32_t);
canvas->size = stride * height;
fd = allocate_shm_file(canvas->size);
if (fd == -1) {
return NULL;
}
canvas->data = mmap(
NULL,
canvas->size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fd,
0
);
if (canvas->data == MAP_FAILED) {
close(fd);
return NULL;
}
pool = wl_shm_create_pool(
shm,
fd,
canvas->size
);
canvas->buffer = wl_shm_pool_create_buffer(
pool,
0,
width,
height,
stride,
WL_SHM_FORMAT_XRGB8888
);
wl_shm_pool_destroy(pool);
close(fd);
return canvas;
}
void
free_drw(Canvas *canvas)
{
munmap(canvas->data, canvas->size);
free(canvas);
}
void
draw_point(Canvas *canvas, unsigned x, unsigned y, uint32_t color)
{
if (!(x < canvas->width && y < canvas->height))
return;
canvas->data[x + canvas->width * y] = color;
}
void
draw_rect(Canvas *canvas, unsigned x, unsigned y, unsigned width, unsigned height, uint32_t color) {
int py;
int px;
for (py = y; py < y + height; ++py) {
for (px = x; px < x + width; ++px) {
draw_point(canvas, px, py, color);
}
}
}
|