Back to index

libdrm  2.4.37
gem_readwrite.c
Go to the documentation of this file.
00001 /*
00002  * Copyright © 2008 Intel Corporation
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice (including the next
00012  * paragraph) shall be included in all copies or substantial portions of the
00013  * Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
00018  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00020  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
00021  * IN THE SOFTWARE.
00022  *
00023  * Authors:
00024  *    Eric Anholt <eric@anholt.net>
00025  *
00026  */
00027 
00028 #include <stdlib.h>
00029 #include <stdio.h>
00030 #include <string.h>
00031 #include <assert.h>
00032 #include <fcntl.h>
00033 #include <inttypes.h>
00034 #include <errno.h>
00035 #include <sys/stat.h>
00036 #include "drm.h"
00037 #include "i915_drm.h"
00038 
00039 #define OBJECT_SIZE 16384
00040 
00041 int do_read(int fd, int handle, void *buf, int offset, int size)
00042 {
00043        struct drm_i915_gem_pread read;
00044 
00045        /* Ensure that we don't have any convenient data in buf in case
00046         * we fail.
00047         */
00048        memset(buf, 0xd0, size);
00049 
00050        memset(&read, 0, sizeof(read));
00051        read.handle = handle;
00052        read.data_ptr = (uintptr_t)buf;
00053        read.size = size;
00054        read.offset = offset;
00055 
00056        return ioctl(fd, DRM_IOCTL_I915_GEM_PREAD, &read);
00057 }
00058 
00059 int do_write(int fd, int handle, void *buf, int offset, int size)
00060 {
00061        struct drm_i915_gem_pwrite write;
00062 
00063        memset(&write, 0, sizeof(write));
00064        write.handle = handle;
00065        write.data_ptr = (uintptr_t)buf;
00066        write.size = size;
00067        write.offset = offset;
00068 
00069        return ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &write);
00070 }
00071 
00072 int main(int argc, char **argv)
00073 {
00074        int fd;
00075        struct drm_i915_gem_create create;
00076        uint8_t expected[OBJECT_SIZE];
00077        uint8_t buf[OBJECT_SIZE];
00078        int ret;
00079        int handle;
00080 
00081        fd = drm_open_matching("8086:*", 0);
00082        if (fd < 0) {
00083               fprintf(stderr, "failed to open intel drm device, skipping\n");
00084               return 0;
00085        }
00086 
00087        memset(&create, 0, sizeof(create));
00088        create.size = OBJECT_SIZE;
00089        ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
00090        assert(ret == 0);
00091        handle = create.handle;
00092 
00093        printf("Testing contents of newly created object.\n");
00094        ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
00095        assert(ret == 0);
00096        memset(&expected, 0, sizeof(expected));
00097        assert(memcmp(expected, buf, sizeof(expected)) == 0);
00098 
00099        printf("Testing read beyond end of buffer.\n");
00100        ret = do_read(fd, handle, buf, OBJECT_SIZE / 2, OBJECT_SIZE);
00101        printf("%d %d\n", ret, errno);
00102        assert(ret == -1 && errno == EINVAL);
00103 
00104        printf("Testing full write of buffer\n");
00105        memset(buf, 0, sizeof(buf));
00106        memset(buf + 1024, 0x01, 1024);
00107        memset(expected + 1024, 0x01, 1024);
00108        ret = do_write(fd, handle, buf, 0, OBJECT_SIZE);
00109        assert(ret == 0);
00110        ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
00111        assert(ret == 0);
00112        assert(memcmp(buf, expected, sizeof(buf)) == 0);
00113 
00114        printf("Testing partial write of buffer\n");
00115        memset(buf + 4096, 0x02, 1024);
00116        memset(expected + 4096, 0x02, 1024);
00117        ret = do_write(fd, handle, buf + 4096, 4096, 1024);
00118        assert(ret == 0);
00119        ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
00120        assert(ret == 0);
00121        assert(memcmp(buf, expected, sizeof(buf)) == 0);
00122 
00123        printf("Testing partial read of buffer\n");
00124        ret = do_read(fd, handle, buf, 512, 1024);
00125        assert(ret == 0);
00126        assert(memcmp(buf, expected + 512, 1024) == 0);
00127 
00128        printf("Testing read of bad buffer handle\n");
00129        ret = do_read(fd, 1234, buf, 0, 1024);
00130        assert(ret == -1 && errno == ENOENT);
00131 
00132        printf("Testing write of bad buffer handle\n");
00133        ret = do_write(fd, 1234, buf, 0, 1024);
00134        assert(ret == -1 && errno == ENOENT);
00135 
00136        close(fd);
00137 
00138        return 0;
00139 }