diff --git a/example/common.c b/example/common.c new file mode 100644 index 0000000..00e07ea --- /dev/null +++ b/example/common.c @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include "common.h" + +drmModeConnector *pick_connector(int drm_fd, drmModeRes *drm_res) +{ + int i; + drmModeConnector *connector; + + for (i = 0; i < drm_res->count_connectors; i++) { + connector = drmModeGetConnector(drm_fd, drm_res->connectors[i]); + if (connector->connection == DRM_MODE_CONNECTED) { + return connector; + } + drmModeFreeConnector(connector); + } + + return NULL; +} + +drmModeCrtc *pick_crtc(int drm_fd, drmModeRes *drm_res, + drmModeConnector *connector) +{ + drmModeEncoder *enc; + uint32_t crtc_id; + + /* TODO: don't blindly use current CRTC */ + enc = drmModeGetEncoder(drm_fd, connector->encoder_id); + crtc_id = enc->crtc_id; + drmModeFreeEncoder(enc); + + return drmModeGetCrtc(drm_fd, crtc_id); +} + +void disable_all_crtcs_except(int drm_fd, drmModeRes *drm_res, uint32_t crtc_id) +{ + int i; + + for (i = 0; i < drm_res->count_crtcs; i++) { + if (drm_res->crtcs[i] == crtc_id) { + continue; + } + drmModeSetCrtc(drm_fd, drm_res->crtcs[i], + 0, 0, 0, NULL, 0, NULL); + } +} + +uint32_t create_argb_fb(int drm_fd, uint32_t width, uint32_t height, + uint32_t color, bool with_alpha) +{ + int ret; + uint32_t fb_id; + uint32_t *data; + size_t i; + + struct drm_mode_create_dumb create = { + .width = width, + .height = height, + .bpp = 32, + .flags = 0, + }; + ret = drmIoctl(drm_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create); + if (ret < 0) { + return 0; + } + + uint32_t fmt = with_alpha ? DRM_FORMAT_ARGB8888 : DRM_FORMAT_XRGB8888; + uint32_t handles[4] = { create.handle }; + uint32_t strides[4] = { create.pitch }; + uint32_t offsets[4] = { 0 }; + ret = drmModeAddFB2(drm_fd, width, height, fmt, handles, strides, + offsets, &fb_id, 0); + if (ret < 0) { + return 0; + } + + struct drm_mode_map_dumb map = { .handle = create.handle }; + ret = drmIoctl(drm_fd, DRM_IOCTL_MODE_MAP_DUMB, &map); + if (ret < 0) { + return 0; + } + + data = mmap(0, create.size, PROT_READ | PROT_WRITE, MAP_SHARED, drm_fd, + map.offset); + if (data == MAP_FAILED) { + return 0; + } + + for (i = 0; i < create.size / sizeof(uint32_t); i++) { + data[i] = color; + } + + munmap(data, create.size); + return fb_id; +} + diff --git a/example/common.h b/example/common.h new file mode 100644 index 0000000..c264d36 --- /dev/null +++ b/example/common.h @@ -0,0 +1,15 @@ +#ifndef EXAMPLE_COMMON_H +#define EXAMPLE_COMMON_H + +#include +#include +#include + +drmModeConnector *pick_connector(int drm_fd, drmModeRes *drm_res); +drmModeCrtc *pick_crtc(int drm_fd, drmModeRes *drm_res, + drmModeConnector *connector); +void disable_all_crtcs_except(int drm_fd, drmModeRes *drm_res, uint32_t crtc_id); +uint32_t create_argb_fb(int drm_fd, uint32_t width, uint32_t height, + uint32_t color, bool with_alpha); + +#endif diff --git a/example/meson.build b/example/meson.build index 9204293..81be0a1 100644 --- a/example/meson.build +++ b/example/meson.build @@ -1,5 +1,5 @@ executable( 'simple', - files('simple.c'), + files('common.c', 'simple.c'), dependencies: [liftoff], ) diff --git a/example/simple.c b/example/simple.c index 3665386..fded603 100644 --- a/example/simple.c +++ b/example/simple.c @@ -6,102 +6,7 @@ #include #include #include - -#include -#include - -static drmModeConnector *pick_connector(int drm_fd, drmModeRes *drm_res) -{ - int i; - drmModeConnector *connector; - - for (i = 0; i < drm_res->count_connectors; i++) { - connector = drmModeGetConnector(drm_fd, drm_res->connectors[i]); - if (connector->connection == DRM_MODE_CONNECTED) { - return connector; - } - drmModeFreeConnector(connector); - } - - return NULL; -} - -static drmModeCrtc *pick_crtc(int drm_fd, drmModeRes *drm_res, - drmModeConnector *connector) -{ - drmModeEncoder *enc; - uint32_t crtc_id; - - /* TODO: don't blindly use current CRTC */ - enc = drmModeGetEncoder(drm_fd, connector->encoder_id); - crtc_id = enc->crtc_id; - drmModeFreeEncoder(enc); - - return drmModeGetCrtc(drm_fd, crtc_id); -} - -static void disable_all_crtcs_except(int drm_fd, drmModeRes *drm_res, - uint32_t crtc_id) -{ - int i; - - for (i = 0; i < drm_res->count_crtcs; i++) { - if (drm_res->crtcs[i] == crtc_id) { - continue; - } - drmModeSetCrtc(drm_fd, drm_res->crtcs[i], - 0, 0, 0, NULL, 0, NULL); - } -} - -static uint32_t create_argb_fb(int drm_fd, uint32_t width, uint32_t height, - uint32_t color, bool with_alpha) -{ - int ret; - uint32_t fb_id; - uint32_t *data; - size_t i; - - struct drm_mode_create_dumb create = { - .width = width, - .height = height, - .bpp = 32, - .flags = 0, - }; - ret = drmIoctl(drm_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create); - if (ret < 0) { - return 0; - } - - uint32_t fmt = with_alpha ? DRM_FORMAT_ARGB8888 : DRM_FORMAT_XRGB8888; - uint32_t handles[4] = { create.handle }; - uint32_t strides[4] = { create.pitch }; - uint32_t offsets[4] = { 0 }; - ret = drmModeAddFB2(drm_fd, width, height, fmt, handles, strides, - offsets, &fb_id, 0); - if (ret < 0) { - return 0; - } - - struct drm_mode_map_dumb map = { .handle = create.handle }; - ret = drmIoctl(drm_fd, DRM_IOCTL_MODE_MAP_DUMB, &map); - if (ret < 0) { - return 0; - } - - data = mmap(0, create.size, PROT_READ | PROT_WRITE, MAP_SHARED, drm_fd, - map.offset); - if (data == MAP_FAILED) { - return 0; - } - - for (i = 0; i < create.size / sizeof(uint32_t); i++) { - data[i] = color; - } - - munmap(data, create.size); - return fb_id; -} +#include "common.h" /* ARGB 8:8:8:8 */ static const uint32_t colors[] = {