xref: /third_party/libdrm/tests/tegra/vic-clear.c (revision d722e3fb)
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 <fcntl.h>
25#include <stdio.h>
26#include <string.h>
27#include <unistd.h>
28
29#include "util_math.h"
30
31#include "tegra.h"
32
33#include "host1x.h"
34#include "vic.h"
35
36#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
37
38int main(int argc, char *argv[])
39{
40    const unsigned int format = VIC_PIXEL_FORMAT_A8R8G8B8;
41    const unsigned int kind = VIC_BLK_KIND_PITCH;
42    const unsigned int width = 16, height = 16;
43    const char *device = "/dev/dri/renderD128";
44    struct drm_tegra_channel *channel;
45    struct drm_tegra_pushbuf *pushbuf;
46    struct drm_tegra_job *job;
47    struct vic_image *output;
48    struct drm_tegra *drm;
49    unsigned int version;
50    struct vic *vic;
51    uint32_t *pb;
52    int fd, err;
53    void *ptr;
54
55    if (argc > 1)
56        device = argv[1];
57
58    fd = open(device, O_RDWR);
59    if (fd < 0) {
60        fprintf(stderr, "open() failed: %s\n", strerror(errno));
61        return 1;
62    }
63
64    err = drm_tegra_new(fd, &drm);
65    if (err < 0) {
66        fprintf(stderr, "failed to open Tegra device: %s\n", strerror(-err));
67        close(fd);
68        return 1;
69    }
70
71    err = drm_tegra_channel_open(drm, DRM_TEGRA_VIC, &channel);
72    if (err < 0) {
73        fprintf(stderr, "failed to open channel to VIC: %s\n", strerror(-err));
74        return 1;
75    }
76
77    version = drm_tegra_channel_get_version(channel);
78    printf("version: %08x\n", version);
79
80    err = vic_new(drm, channel, &vic);
81    if (err < 0) {
82        fprintf(stderr, "failed to create VIC: %s\n", strerror(-err));
83        return 1;
84    }
85
86    err = vic_image_new(vic, width, height, format, kind, DRM_TEGRA_CHANNEL_MAP_READ_WRITE,
87                        &output);
88    if (err < 0) {
89        fprintf(stderr, "failed to create output image: %d\n", err);
90        return 1;
91    }
92
93    printf("image: %zu bytes\n", output->size);
94
95    err = drm_tegra_bo_map(output->bo, &ptr);
96    if (err < 0) {
97        fprintf(stderr, "failed to map output image: %d\n", err);
98        return 1;
99    }
100
101    memset(ptr, 0xff, output->size);
102    drm_tegra_bo_unmap(output->bo);
103
104    printf("output: %ux%u\n", output->width, output->height);
105    vic_image_dump(output, stdout);
106
107    err = drm_tegra_job_new(channel, &job);
108    if (err < 0) {
109        fprintf(stderr, "failed to create job: %s\n", strerror(-err));
110        return 1;
111    }
112
113    err = drm_tegra_job_get_pushbuf(job, &pushbuf);
114    if (err < 0) {
115        fprintf(stderr, "failed to create push buffer: %s\n", strerror(-err));
116        return 1;
117    }
118
119    err = drm_tegra_pushbuf_begin(pushbuf, 32, &pb);
120    if (err < 0) {
121        fprintf(stderr, "failed to prepare push buffer: %s\n", strerror(-err));
122        return 1;
123    }
124
125    err = vic_clear(vic, output, 1023, 0, 0, 1023);
126    if (err < 0) {
127        fprintf(stderr, "failed to clear surface: %s\n", strerror(-err));
128        return err;
129    }
130
131    err = vic->ops->execute(vic, pushbuf, &pb, output, NULL, 0);
132    if (err < 0) {
133        fprintf(stderr, "failed to execute operation: %s\n", strerror(-err));
134        return 1;
135    }
136
137    err = drm_tegra_pushbuf_sync_cond(pushbuf, &pb, vic->syncpt,
138                                      DRM_TEGRA_SYNC_COND_OP_DONE);
139    if (err < 0) {
140        fprintf(stderr, "failed to push syncpoint: %s\n", strerror(-err));
141        return 1;
142    }
143
144    err = drm_tegra_pushbuf_end(pushbuf, pb);
145    if (err < 0) {
146        fprintf(stderr, "failed to update push buffer: %s\n", strerror(-err));
147        return 1;
148    }
149
150    err = drm_tegra_job_submit(job, NULL);
151    if (err < 0) {
152        fprintf(stderr, "failed to submit job: %s\n", strerror(-err));
153        return 1;
154    }
155
156    err = drm_tegra_job_wait(job, 1000000000);
157    if (err < 0) {
158        fprintf(stderr, "failed to wait for job: %s\n", strerror(-err));
159        return 1;
160    }
161
162    printf("output: %ux%u\n", output->width, output->height);
163    vic_image_dump(output, stdout);
164
165    drm_tegra_job_free(job);
166    vic_image_free(output);
167    vic_free(vic);
168    drm_tegra_channel_close(channel);
169    drm_tegra_close(drm);
170    close(fd);
171
172    return 0;
173}
174