ipptest: add fimc and rotator test applications
[platform/upstream/libdrm.git] / tests / ipptest / util.c
1 /*
2  * DRM based fimc test program
3  * Copyright 2012 Samsung Electronics
4  *   Eunchul Kim <chulspro.kim@sasmsung.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdint.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <errno.h>
31
32 #include "exynos_drm.h"
33 #include "gem.h"
34
35 int util_gem_create_mmap(int fd, struct drm_exynos_gem_create *gem,
36                                         struct drm_exynos_gem_mmap *mmap,
37                                         unsigned int size)
38 {
39         /* initialize structure for gem create */
40         memset(gem, 0x00, sizeof(struct drm_exynos_gem_create));
41         gem->size = size;
42
43         if (exynos_gem_create(fd, gem) < 0) {
44                 fprintf(stderr, "failed to gem create: %s\n", strerror(errno));
45                 return -1;
46         }
47
48         /* initialize structure for gem mmap */
49         memset(mmap, 0x00, sizeof(struct drm_exynos_gem_mmap));
50         mmap->handle = gem->handle;
51         mmap->size = gem->size;
52
53         if (exynos_gem_mmap(fd, mmap) < 0) {
54                 fprintf(stderr, "failed to gem mmap: %s\n", strerror(errno));
55                 return -2;
56         }
57
58         return 0;
59 }
60
61 void util_draw_buffer(void *addr, unsigned int stripe,
62                                 unsigned int width, unsigned int height,
63                                 unsigned int stride, unsigned int size)
64 {
65         if (stripe == 1) {
66                 int i, j;
67                 unsigned int *fb_ptr;
68                 div_t d;
69
70                 for (j = 0; j < height; j++) {
71                         fb_ptr = (unsigned int *)((char *)addr + j * stride);
72                         for (i = 0; i < width; i++) {
73                                 d = div(i, width);
74                                 fb_ptr[i] = 0x00130502 * (d.quot >> 6)
75                                                 + 0x000a1120 * (d.rem >> 6);
76                         }
77                 }
78         } else
79                 memset(addr, 0x77, size);
80 }
81
82 int util_write_bmp(const char *file, const void *data, unsigned int width,
83                                                         unsigned int height)
84 {
85         int i;
86         unsigned int * blocks;
87         FILE *fp;
88         struct {
89                 unsigned char magic[2];
90         } bmpfile_magic = { {'B', 'M'} };
91         struct {
92                 unsigned int filesz;
93                 unsigned short creator1;
94                 unsigned short creator2;
95                 unsigned int bmp_offset;
96         } bmpfile_header = { 0, 0, 0, 0x36 };
97         struct {
98                 unsigned int header_sz;
99                 unsigned int width;
100                 unsigned int height;
101                 unsigned short nplanes;
102                 unsigned short bitspp;
103                 unsigned int compress_type;
104                 unsigned int bmp_bytesz;
105                 unsigned int hres;
106                 unsigned int vres;
107                 unsigned int ncolors;
108                 unsigned int nimpcolors;
109         } bmp_dib_v3_header_t = { 0x28, 0, 0, 1, 32, 0, 0, 0, 0, 0, 0 };
110
111         fp = fopen(file, "wb");
112         if (fp == NULL) return -1;
113
114         bmpfile_header.filesz = sizeof(bmpfile_magic) + sizeof(bmpfile_header)
115                         + sizeof(bmp_dib_v3_header_t) + width * height * 4;
116         bmp_dib_v3_header_t.header_sz = sizeof(bmp_dib_v3_header_t);
117         bmp_dib_v3_header_t.width = width;
118         bmp_dib_v3_header_t.height = -height;
119         bmp_dib_v3_header_t.nplanes = 1;
120         bmp_dib_v3_header_t.bmp_bytesz = width * height * 4;
121
122         fwrite(&bmpfile_magic, sizeof(bmpfile_magic), 1, fp);
123         fwrite(&bmpfile_header, sizeof(bmpfile_header), 1, fp);
124         fwrite(&bmp_dib_v3_header_t, sizeof(bmp_dib_v3_header_t), 1, fp);
125
126         blocks = (unsigned int*)data;
127         for (i = 0; i < height * width; i++)
128                 fwrite(&blocks[i], 4, 1, fp);
129
130         fclose(fp);
131         return 0;
132 }