diff options
author | Leo (Sunpeng) Li <sunpeng.li@amd.com> | 2018-03-06 09:44:55 -0500 |
---|---|---|
committer | Leo (Sunpeng) Li <sunpeng.li@amd.com> | 2018-05-02 10:13:30 -0400 |
commit | 1c1f870ad224e683a96630266ee701d727212702 (patch) | |
tree | d948d51d2021f97bc83007664362997a27413499 | |
parent | 03a6d3a636c735636890974282157999e610635d (diff) |
Add code to create, get, and destroy property blobs
And fix makefile.
Signed-off-by: Leo (Sunpeng) Li <sunpeng.li@amd.com>
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | dreamcolor-demo.c | 100 |
2 files changed, 95 insertions, 9 deletions
@@ -1,8 +1,8 @@ CFLAGS=-g -Wall CC=gcc -LDFLAGS=$(shell pkg-config --cflags --libs libdrm) -LDLIBS = -lm +LDFLAGS=$(shell pkg-config --cflags libdrm) +LDLIBS = $(shell pkg-config --libs libdrm) -lm SOURCES=$(wildcard *.c) EXECUTABLES=$(patsubst %.c,%,$(SOURCES)) diff --git a/dreamcolor-demo.c b/dreamcolor-demo.c index 094a594..c501e3c 100644 --- a/dreamcolor-demo.c +++ b/dreamcolor-demo.c @@ -23,15 +23,22 @@ * */ -#include <xf86drm.h> -#include <stdio.h> +#include <fcntl.h> #include <math.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <xf86drm.h> +#include <xf86drmMode.h> #define LUT_SIZE 16 -/* Data structures for gamma/degamma ramps & ctm matrix. */ +/** + * The below data structures are identical to the ones used by DRM. They are + * here to help us structure the data being passed to the kernel. + */ struct _drm_color_ctm { /* Transformation matrix in S31.32 format. */ __s64 matrix[9]; @@ -47,6 +54,7 @@ struct _drm_color_lut { __u16 reserved; }; +/* Struct to make constructing luts easier. */ struct color3d { double r; double g; @@ -54,7 +62,7 @@ struct color3d { }; -/** +/******************************************************************************* * Helper functions */ @@ -114,16 +122,94 @@ static void print_lut(const struct _drm_color_lut *lut, int lut_size) } } +static int fd_is_device(int fd, const char *expect) +{ + char name[5] = ""; + drm_version_t version = {0}; + + version.name_len = 4; + version.name = name; + if (drmIoctl(fd, DRM_IOCTL_VERSION, &version)) + return 0; + + return strcmp(expect, version.name) == 0; +} + +static int open_drm_device() +{ + char *base = "/dev/dri/card"; + int i = 0; + for (i = 0; i < 16; i++) { + char name[80]; + int fd; + + sprintf(name, "%s%u", base, i); + printf("Opening %s... ", name); + fd = open(name, O_RDWR); + if (fd == -1) { + printf("Failed.\n"); + continue; + } + + if (fd_is_device(fd, "i915")){ + printf("Success!\n"); + return fd; + } + + printf("Not an amdgpu device.\n"); + close(fd); + } + return -1; +} + +/******************************************************************************* + * main + */ int main(int argc, char const *argv[]) { struct color3d coeffs[LUT_SIZE]; struct _drm_color_lut lut[LUT_SIZE]; - load_table(coeffs, LUT_SIZE, 1); - print_coeffs(coeffs, LUT_SIZE); + int ret; + + int fd = open_drm_device(); + if (fd == -1) { + printf("No valid devices found\n"); + return -1; + } + load_table(coeffs, LUT_SIZE, 1); coeffs_to_lut(coeffs, lut, LUT_SIZE); - print_lut(lut, LUT_SIZE); + + size_t size = sizeof(struct _drm_color_lut) * LUT_SIZE; + uint32_t blob_id = 0; + + ret = drmModeCreatePropertyBlob(fd, lut, size, &blob_id); + if (ret) { + printf("Failed to create blob. %d\n", ret); + return ret; + } + + printf("Successfully created property blob with id %d\n", blob_id); + drmModePropertyBlobPtr blob = drmModeGetPropertyBlob(fd, blob_id); + if (!blob) { + printf("Failed to get blob.\n"); + return -1; + } + + struct _drm_color_lut *ret_lut = (struct _drm_color_lut *)blob->data; + + print_lut(ret_lut, LUT_SIZE); + + ret = drmModeDestroyPropertyBlob(fd, blob_id); + if (ret) { + printf("Failed to destroy blob. %d\n", ret); + return -1; + } + + drmModeFreePropertyBlob(blob); + + printf("Done!\n"); return 0; }
\ No newline at end of file |