summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common.c55
-rw-r--r--common.h4
-rw-r--r--cube-tex.c120
-rw-r--r--gst-decoder.c30
4 files changed, 69 insertions, 140 deletions
diff --git a/common.c b/common.c
index b67c94b..6238cd6 100644
--- a/common.c
+++ b/common.c
@@ -38,6 +38,9 @@ static struct gbm gbm;
static struct egl _egl;
static struct egl *egl = &_egl;
+WEAK uint64_t
+gbm_bo_get_modifier(struct gbm_bo *bo);
+
WEAK struct gbm_surface *
gbm_surface_create_with_modifiers(struct gbm_device *gbm,
uint32_t width, uint32_t height,
@@ -536,3 +539,55 @@ int64_t get_time_ns(void)
clock_gettime(CLOCK_MONOTONIC, &tv);
return tv.tv_nsec + tv.tv_sec * NSEC_PER_SEC;
}
+
+int buf_to_fd(const struct gbm *gbm,
+ uint32_t width, uint32_t height, uint32_t bpp, const void *ptr,
+ uint32_t *pstride, uint64_t *modifier)
+{
+ const uint8_t *src = (const uint8_t *)ptr;
+ uint32_t format;
+ struct gbm_bo *bo;
+ void *map_data = NULL;
+ uint32_t stride;
+ uint8_t *map;
+ int fd;
+
+ switch (bpp) {
+ case 1:
+ format = GBM_FORMAT_R8;
+ break;
+ case 2:
+ format = GBM_FORMAT_GR88;
+ break;
+ case 4:
+ format = GBM_FORMAT_ABGR8888;
+ break;
+ default:
+ assert(!"unreachable");
+ return -1;
+ }
+
+ bo = gbm_bo_create(gbm->dev, width, height, format, GBM_BO_USE_LINEAR);
+
+ map = gbm_bo_map(bo, 0, 0, width, height, GBM_BO_TRANSFER_WRITE, &stride, &map_data);
+
+ for (uint32_t i = 0; i < height; i++) {
+ memcpy(&map[stride * i], &src[width * bpp * i], width * bpp);
+ }
+
+ gbm_bo_unmap(bo, map_data);
+
+ fd = gbm_bo_get_fd(bo);
+
+ if (gbm_bo_get_modifier)
+ *modifier = gbm_bo_get_modifier(bo);
+ else
+ *modifier = DRM_FORMAT_MOD_LINEAR;
+
+ /* we have the fd now, no longer need the bo: */
+ gbm_bo_destroy(bo);
+
+ *pstride = stride;
+
+ return fd;
+}
diff --git a/common.h b/common.h
index 3b63473..980f031 100644
--- a/common.h
+++ b/common.h
@@ -223,4 +223,8 @@ void finish_fpscntrs(void);
int64_t get_time_ns(void);
+int buf_to_fd(const struct gbm *gbm,
+ uint32_t width, uint32_t height, uint32_t bpp, const void *ptr,
+ uint32_t *pstride, uint64_t *modifier);
+
#endif /* _COMMON_H */
diff --git a/cube-tex.c b/cube-tex.c
index 71111d9..91436a5 100644
--- a/cube-tex.c
+++ b/cube-tex.c
@@ -212,120 +212,14 @@ static const char *fragment_shader_source_2img =
"} \n";
static const uint32_t texw = 512, texh = 512;
-
-WEAK uint64_t
-gbm_bo_get_modifier(struct gbm_bo *bo);
-
-static int get_fd_rgba(uint32_t *pstride, uint64_t *modifier)
-{
- struct gbm_bo *bo;
- void *map_data = NULL;
- uint32_t stride;
- extern const uint32_t raw_512x512_rgba[];
- uint8_t *map, *src = (uint8_t *)raw_512x512_rgba;
- int fd;
-
- /* NOTE: do not actually use GBM_BO_USE_WRITE since that gets us a dumb buffer: */
- bo = gbm_bo_create(gl.gbm->dev, texw, texh, GBM_FORMAT_ABGR8888, GBM_BO_USE_LINEAR);
-
- map = gbm_bo_map(bo, 0, 0, texw, texh, GBM_BO_TRANSFER_WRITE, &stride, &map_data);
-
- for (uint32_t i = 0; i < texh; i++) {
- memcpy(&map[stride * i], &src[texw * 4 * i], texw * 4);
- }
-
- gbm_bo_unmap(bo, map_data);
-
- fd = gbm_bo_get_fd(bo);
-
- if (gbm_bo_get_modifier)
- *modifier = gbm_bo_get_modifier(bo);
- else
- *modifier = DRM_FORMAT_MOD_LINEAR;
-
- /* we have the fd now, no longer need the bo: */
- gbm_bo_destroy(bo);
-
- *pstride = stride;
-
- return fd;
-}
-
-static int get_fd_y(uint32_t *pstride, uint64_t *modifier)
-{
- struct gbm_bo *bo;
- void *map_data = NULL;
- uint32_t stride;
- extern const uint32_t raw_512x512_nv12[];
- uint8_t *map, *src = (uint8_t *)raw_512x512_nv12;
- int fd;
-
- /* NOTE: do not actually use GBM_BO_USE_WRITE since that gets us a dumb buffer: */
- bo = gbm_bo_create(gl.gbm->dev, texw, texh, GBM_FORMAT_R8, GBM_BO_USE_LINEAR);
-
- map = gbm_bo_map(bo, 0, 0, texw, texh, GBM_BO_TRANSFER_WRITE, &stride, &map_data);
-
- for (uint32_t i = 0; i < texh; i++) {
- memcpy(&map[stride * i], &src[texw * i], texw);
- }
-
- gbm_bo_unmap(bo, map_data);
-
- fd = gbm_bo_get_fd(bo);
-
- if (gbm_bo_get_modifier)
- *modifier = gbm_bo_get_modifier(bo);
- else
- *modifier = DRM_FORMAT_MOD_LINEAR;
-
- /* we have the fd now, no longer need the bo: */
- gbm_bo_destroy(bo);
-
- *pstride = stride;
-
- return fd;
-}
-
-static int get_fd_uv(uint32_t *pstride, uint64_t *modifier)
-{
- struct gbm_bo *bo;
- void *map_data = NULL;
- uint32_t stride;
- extern const uint32_t raw_512x512_nv12[];
- uint8_t *map, *src = &((uint8_t *)raw_512x512_nv12)[texw * texh];
- int fd;
-
- /* NOTE: do not actually use GBM_BO_USE_WRITE since that gets us a dumb buffer: */
- bo = gbm_bo_create(gl.gbm->dev, texw/2, texh/2, GBM_FORMAT_GR88, GBM_BO_USE_LINEAR);
-
- map = gbm_bo_map(bo, 0, 0, texw/2, texh/2, GBM_BO_TRANSFER_WRITE, &stride, &map_data);
-
- for (uint32_t i = 0; i < texh/2; i++) {
- memcpy(&map[stride * i], &src[texw * i], texw);
- }
-
- gbm_bo_unmap(bo, map_data);
-
- fd = gbm_bo_get_fd(bo);
-
- if (gbm_bo_get_modifier)
- *modifier = gbm_bo_get_modifier(bo);
- else
- *modifier = DRM_FORMAT_MOD_LINEAR;
-
- /* we have the fd now, no longer need the bo: */
- gbm_bo_destroy(bo);
-
- *pstride = stride;
-
- return fd;
-}
+extern const uint32_t raw_512x512_rgba[];
+extern const uint32_t raw_512x512_nv12[];
static int init_tex_rgba(void)
{
uint32_t stride;
uint64_t modifier;
- int fd = get_fd_rgba(&stride, &modifier);
+ int fd = buf_to_fd(gl.gbm, texw, texh, 4, raw_512x512_rgba, &stride, &modifier);
EGLint attr[] = {
EGL_WIDTH, texw,
EGL_HEIGHT, texh,
@@ -371,8 +265,8 @@ static int init_tex_nv12_2img(void)
{
uint32_t stride_y, stride_uv;
uint64_t modifier_y, modifier_uv;
- int fd_y = get_fd_y(&stride_y, &modifier_y);
- int fd_uv = get_fd_uv(&stride_uv, &modifier_uv);
+ int fd_y = buf_to_fd(gl.gbm, texw, texh, 1, raw_512x512_nv12, &stride_y, &modifier_y);
+ int fd_uv = buf_to_fd(gl.gbm, texw/2, texh/2, 2, &((uint8_t *)raw_512x512_nv12)[texw * texh], &stride_uv, &modifier_uv);
EGLint attr_y[] = {
EGL_WIDTH, texw,
EGL_HEIGHT, texh,
@@ -453,8 +347,8 @@ static int init_tex_nv12_1img(void)
{
uint32_t stride_y, stride_uv;
uint64_t modifier_y, modifier_uv;
- int fd_y = get_fd_y(&stride_y, &modifier_y);
- int fd_uv = get_fd_uv(&stride_uv, &modifier_uv);
+ int fd_y = buf_to_fd(gl.gbm, texw, texh, 1, raw_512x512_nv12, &stride_y, &modifier_y);
+ int fd_uv = buf_to_fd(gl.gbm, texw/2, texh/2, 2, &((uint8_t *)raw_512x512_nv12)[texw * texh], &stride_uv, &modifier_uv);
EGLint attr[] = {
EGL_WIDTH, texw,
EGL_HEIGHT, texh,
diff --git a/gst-decoder.c b/gst-decoder.c
index 91bfe93..8d1f764 100644
--- a/gst-decoder.c
+++ b/gst-decoder.c
@@ -332,32 +332,6 @@ set_last_frame(struct decoder *dec, EGLImage frame, GstSample *samp)
dec->last_samp = samp;
}
-// TODO this could probably be a helper re-used by cube-tex:
-static int
-buf_to_fd(const struct gbm *gbm, int size, void *ptr)
-{
- struct gbm_bo *bo;
- void *map, *map_data = NULL;
- uint32_t stride;
- int fd;
-
- /* NOTE: do not actually use GBM_BO_USE_WRITE since that gets us a dumb buffer: */
- bo = gbm_bo_create(gbm->dev, size, 1, GBM_FORMAT_R8, GBM_BO_USE_LINEAR);
-
- map = gbm_bo_map(bo, 0, 0, size, 1, GBM_BO_TRANSFER_WRITE, &stride, &map_data);
-
- memcpy(map, ptr, size);
-
- gbm_bo_unmap(bo, map_data);
-
- fd = gbm_bo_get_fd(bo);
-
- /* we have the fd now, no longer need the bo: */
- gbm_bo_destroy(bo);
-
- return fd;
-}
-
static EGLImage
buffer_to_image(struct decoder *dec, GstBuffer *buf)
{
@@ -413,7 +387,9 @@ buffer_to_image(struct decoder *dec, GstBuffer *buf)
} else {
GstMapInfo map_info;
gst_buffer_map(buf, &map_info, GST_MAP_READ);
- dmabuf_fd = buf_to_fd(dec->gbm, map_info.size, map_info.data);
+ uint32_t stride;
+ uint64_t modifier;
+ dmabuf_fd = buf_to_fd(dec->gbm, map_info.size, 1, 1, map_info.data, &stride, &modifier);
gst_buffer_unmap(buf, &map_info);
}