50 lines
1.1 KiB
C
50 lines
1.1 KiB
C
#include "fb.h"
|
|
|
|
#include <assert.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/mman.h>
|
|
|
|
fb_info_t fb_init(int drm_fd, drmModeModeInfo mode)
|
|
{
|
|
int err;
|
|
|
|
struct drm_mode_create_dumb create = {
|
|
.width = mode.hdisplay,
|
|
.height = mode.vdisplay,
|
|
.bpp = 32,
|
|
};
|
|
err = ioctl(drm_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
|
|
assert(err != -1);
|
|
|
|
uint32_t id;
|
|
err = drmModeAddFB(
|
|
drm_fd, create.width, create.height, 24, 32, create.pitch,
|
|
create.handle, &id);
|
|
assert(err == 0);
|
|
|
|
struct drm_mode_map_dumb map = { .handle = create.handle };
|
|
err = ioctl(drm_fd, DRM_IOCTL_MODE_MAP_DUMB, &map);
|
|
assert(err != -1);
|
|
|
|
uint32_t *buf = mmap(
|
|
0, create.size, PROT_READ | PROT_WRITE, MAP_SHARED, drm_fd,
|
|
map.offset);
|
|
assert(buf != MAP_FAILED);
|
|
|
|
return (fb_info_t) {
|
|
.buf = buf,
|
|
.handle = create.handle,
|
|
.id = id,
|
|
.pitch = create.pitch,
|
|
.size = create.size,
|
|
};
|
|
}
|
|
|
|
void fb_cleanup(int drm_fd, fb_info_t fb)
|
|
{
|
|
munmap(fb.buf, fb.size);
|
|
drmModeRmFB(drm_fd, fb.id);
|
|
struct drm_mode_destroy_dumb delete = { .handle = fb.handle };
|
|
ioctl(drm_fd, DRM_IOCTL_MODE_DESTROY_DUMB, &delete);
|
|
}
|