c5745ae58d29f304db2e59f87ec5baebbf53e3b4
[platform/upstream/libdrm.git] / tests / tegra / vic.c
1 /*
2  * Copyright © 2018 NVIDIA Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22
23 #include <errno.h>
24 #include <stdio.h> /* XXX remove */
25 #include <stdlib.h>
26
27 #include "util_math.h"
28
29 #include "tegra.h"
30 #include "host1x.h"
31 #include "vic.h"
32
33 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
34
35 const struct vic_format_info *vic_format_get_info(unsigned int format)
36 {
37     static const struct vic_format_info formats[] = {
38         { .format = VIC_PIXEL_FORMAT_A8R8G8B8, .cpp = 4 },
39     };
40     unsigned int i;
41
42     for (i = 0; i < ARRAY_SIZE(formats); i++) {
43         if (formats[i].format == format)
44             return &formats[i];
45     }
46
47     return 0;
48 }
49
50 int vic_image_new(struct vic *vic, unsigned int width, unsigned int height,
51                   unsigned int format, unsigned int kind, uint32_t flags,
52                   struct vic_image **imagep)
53 {
54     const struct vic_format_info *info = vic_format_get_info(format);
55     struct vic_image *image;
56     int err;
57
58     if (!info)
59         return -EINVAL;
60
61     image = calloc(1, sizeof(*image));
62     if (!image)
63         return -ENOMEM;
64
65     if (kind == VIC_BLK_KIND_PITCH)
66         image->align = 256;
67     else
68         image->align = 256; /* XXX */
69
70     image->width = width;
71     image->stride = ALIGN(width, image->align);
72     image->pitch = image->stride * info->cpp;
73     image->height = height;
74     image->format = format;
75     image->kind = kind;
76
77     image->size = image->pitch * image->height;
78
79     printf("image: %ux%u align: %zu stride: %u pitch: %u size: %zu\n",
80            image->width, image->height, image->align, image->stride,
81            image->pitch, image->size);
82
83     err = drm_tegra_bo_new(vic->drm, 0, image->size, &image->bo);
84     if (err < 0) {
85         free(image);
86         return err;
87     }
88
89     err = drm_tegra_channel_map(vic->channel, image->bo, flags, &image->map);
90     if (err < 0) {
91         drm_tegra_bo_unref(image->bo);
92         free(image);
93         return err;
94     }
95
96     *imagep = image;
97     return 0;
98 }
99
100 void vic_image_free(struct vic_image *image)
101 {
102     if (image) {
103         drm_tegra_channel_unmap(image->map);
104         drm_tegra_bo_unref(image->bo);
105         free(image);
106     }
107 }
108
109 void vic_image_dump(struct vic_image *image, FILE *fp)
110 {
111     unsigned int i, j;
112     void *ptr;
113     int err;
114
115     err = drm_tegra_bo_map(image->bo, &ptr);
116     if (err < 0)
117         return;
118
119     for (j = 0; j < image->height; j++) {
120         uint32_t *pixels = (uint32_t *)((unsigned long)ptr + j * image->pitch);
121
122         printf("   ");
123
124         for (i = 0; i < image->width; i++)
125             printf(" %08x", pixels[i]);
126
127         printf("\n");
128     }
129
130     drm_tegra_bo_unmap(image->bo);
131 }
132
133 /* from vic30.c */
134 int vic30_new(struct drm_tegra *drm, struct drm_tegra_channel *channel,
135               struct vic **vicp);
136
137 /* from vic40.c */
138 int vic40_new(struct drm_tegra *drm, struct drm_tegra_channel *channel,
139               struct vic **vicp);
140
141 /* from vic41.c */
142 int vic41_new(struct drm_tegra *drm, struct drm_tegra_channel *channel,
143               struct vic **vicp);
144
145 int vic_new(struct drm_tegra *drm, struct drm_tegra_channel *channel,
146             struct vic **vicp)
147 {
148     unsigned int version;
149
150     version = drm_tegra_channel_get_version(channel);
151
152     switch (version) {
153     case 0x40:
154         return vic30_new(drm, channel, vicp);
155
156     case 0x21:
157         return vic40_new(drm, channel, vicp);
158
159     case 0x18:
160         return vic41_new(drm, channel, vicp);
161     }
162
163     return -ENOTSUP;
164 }
165
166 void vic_free(struct vic *vic)
167 {
168     if (vic)
169         vic->ops->free(vic);
170 }
171
172 int vic_clear(struct vic *vic, struct vic_image *output, unsigned int alpha,
173               unsigned int red, unsigned int green, unsigned int blue)
174 {
175     return vic->ops->fill(vic, output, 0, 0, output->width - 1,
176                           output->height - 1, alpha, red, green, blue);
177 }