10ed5401bSmrg/*
20ed5401bSmrg * Copyright © 2018 NVIDIA Corporation
30ed5401bSmrg *
40ed5401bSmrg * Permission is hereby granted, free of charge, to any person obtaining a
50ed5401bSmrg * copy of this software and associated documentation files (the "Software"),
60ed5401bSmrg * to deal in the Software without restriction, including without limitation
70ed5401bSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
80ed5401bSmrg * and/or sell copies of the Software, and to permit persons to whom the
90ed5401bSmrg * Software is furnished to do so, subject to the following conditions:
100ed5401bSmrg *
110ed5401bSmrg * The above copyright notice and this permission notice shall be included in
120ed5401bSmrg * all copies or substantial portions of the Software.
130ed5401bSmrg *
140ed5401bSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
150ed5401bSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
160ed5401bSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
170ed5401bSmrg * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
180ed5401bSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
190ed5401bSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
200ed5401bSmrg * OTHER DEALINGS IN THE SOFTWARE.
210ed5401bSmrg */
220ed5401bSmrg
230ed5401bSmrg#include <errno.h>
240ed5401bSmrg#include <fcntl.h>
250ed5401bSmrg#include <stdio.h>
260ed5401bSmrg#include <string.h>
270ed5401bSmrg#include <unistd.h>
280ed5401bSmrg
290ed5401bSmrg#include "util_math.h"
300ed5401bSmrg
310ed5401bSmrg#include "tegra.h"
320ed5401bSmrg
330ed5401bSmrg#include "host1x.h"
340ed5401bSmrg#include "vic.h"
350ed5401bSmrg
360ed5401bSmrg#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
370ed5401bSmrg
380ed5401bSmrgint main(int argc, char *argv[])
390ed5401bSmrg{
400ed5401bSmrg    const unsigned int format = VIC_PIXEL_FORMAT_A8R8G8B8;
410ed5401bSmrg    const unsigned int kind = VIC_BLK_KIND_PITCH;
420ed5401bSmrg    const unsigned int width = 16, height = 16;
430ed5401bSmrg    const char *device = "/dev/dri/renderD128";
440ed5401bSmrg    struct drm_tegra_channel *channel;
450ed5401bSmrg    struct drm_tegra_pushbuf *pushbuf;
460ed5401bSmrg    struct drm_tegra_job *job;
470ed5401bSmrg    struct vic_image *output;
480ed5401bSmrg    struct drm_tegra *drm;
490ed5401bSmrg    unsigned int version;
500ed5401bSmrg    struct vic *vic;
510ed5401bSmrg    uint32_t *pb;
520ed5401bSmrg    int fd, err;
530ed5401bSmrg    void *ptr;
540ed5401bSmrg
550ed5401bSmrg    if (argc > 1)
560ed5401bSmrg        device = argv[1];
570ed5401bSmrg
580ed5401bSmrg    fd = open(device, O_RDWR);
590ed5401bSmrg    if (fd < 0) {
600ed5401bSmrg        fprintf(stderr, "open() failed: %s\n", strerror(errno));
610ed5401bSmrg        return 1;
620ed5401bSmrg    }
630ed5401bSmrg
640ed5401bSmrg    err = drm_tegra_new(fd, &drm);
650ed5401bSmrg    if (err < 0) {
660ed5401bSmrg        fprintf(stderr, "failed to open Tegra device: %s\n", strerror(-err));
670ed5401bSmrg        close(fd);
680ed5401bSmrg        return 1;
690ed5401bSmrg    }
700ed5401bSmrg
710ed5401bSmrg    err = drm_tegra_channel_open(drm, DRM_TEGRA_VIC, &channel);
720ed5401bSmrg    if (err < 0) {
730ed5401bSmrg        fprintf(stderr, "failed to open channel to VIC: %s\n", strerror(-err));
740ed5401bSmrg        return 1;
750ed5401bSmrg    }
760ed5401bSmrg
770ed5401bSmrg    version = drm_tegra_channel_get_version(channel);
780ed5401bSmrg    printf("version: %08x\n", version);
790ed5401bSmrg
800ed5401bSmrg    err = vic_new(drm, channel, &vic);
810ed5401bSmrg    if (err < 0) {
820ed5401bSmrg        fprintf(stderr, "failed to create VIC: %s\n", strerror(-err));
830ed5401bSmrg        return 1;
840ed5401bSmrg    }
850ed5401bSmrg
860ed5401bSmrg    err = vic_image_new(vic, width, height, format, kind, DRM_TEGRA_CHANNEL_MAP_READ_WRITE,
870ed5401bSmrg                        &output);
880ed5401bSmrg    if (err < 0) {
890ed5401bSmrg        fprintf(stderr, "failed to create output image: %d\n", err);
900ed5401bSmrg        return 1;
910ed5401bSmrg    }
920ed5401bSmrg
930ed5401bSmrg    printf("image: %zu bytes\n", output->size);
940ed5401bSmrg
950ed5401bSmrg    err = drm_tegra_bo_map(output->bo, &ptr);
960ed5401bSmrg    if (err < 0) {
970ed5401bSmrg        fprintf(stderr, "failed to map output image: %d\n", err);
980ed5401bSmrg        return 1;
990ed5401bSmrg    }
1000ed5401bSmrg
1010ed5401bSmrg    memset(ptr, 0xff, output->size);
1020ed5401bSmrg    drm_tegra_bo_unmap(output->bo);
1030ed5401bSmrg
1040ed5401bSmrg    printf("output: %ux%u\n", output->width, output->height);
1050ed5401bSmrg    vic_image_dump(output, stdout);
1060ed5401bSmrg
1070ed5401bSmrg    err = drm_tegra_job_new(channel, &job);
1080ed5401bSmrg    if (err < 0) {
1090ed5401bSmrg        fprintf(stderr, "failed to create job: %s\n", strerror(-err));
1100ed5401bSmrg        return 1;
1110ed5401bSmrg    }
1120ed5401bSmrg
1130ed5401bSmrg    err = drm_tegra_job_get_pushbuf(job, &pushbuf);
1140ed5401bSmrg    if (err < 0) {
1150ed5401bSmrg        fprintf(stderr, "failed to create push buffer: %s\n", strerror(-err));
1160ed5401bSmrg        return 1;
1170ed5401bSmrg    }
1180ed5401bSmrg
1190ed5401bSmrg    err = drm_tegra_pushbuf_begin(pushbuf, 32, &pb);
1200ed5401bSmrg    if (err < 0) {
1210ed5401bSmrg        fprintf(stderr, "failed to prepare push buffer: %s\n", strerror(-err));
1220ed5401bSmrg        return 1;
1230ed5401bSmrg    }
1240ed5401bSmrg
1250ed5401bSmrg    err = vic_clear(vic, output, 1023, 0, 0, 1023);
1260ed5401bSmrg    if (err < 0) {
1270ed5401bSmrg        fprintf(stderr, "failed to clear surface: %s\n", strerror(-err));
1280ed5401bSmrg        return err;
1290ed5401bSmrg    }
1300ed5401bSmrg
1310ed5401bSmrg    err = vic->ops->execute(vic, pushbuf, &pb, output, NULL, 0);
1320ed5401bSmrg    if (err < 0) {
1330ed5401bSmrg        fprintf(stderr, "failed to execute operation: %s\n", strerror(-err));
1340ed5401bSmrg        return 1;
1350ed5401bSmrg    }
1360ed5401bSmrg
1370ed5401bSmrg    err = drm_tegra_pushbuf_sync_cond(pushbuf, &pb, vic->syncpt,
1380ed5401bSmrg                                      DRM_TEGRA_SYNC_COND_OP_DONE);
1390ed5401bSmrg    if (err < 0) {
1400ed5401bSmrg        fprintf(stderr, "failed to push syncpoint: %s\n", strerror(-err));
1410ed5401bSmrg        return 1;
1420ed5401bSmrg    }
1430ed5401bSmrg
1440ed5401bSmrg    err = drm_tegra_pushbuf_end(pushbuf, pb);
1450ed5401bSmrg    if (err < 0) {
1460ed5401bSmrg        fprintf(stderr, "failed to update push buffer: %s\n", strerror(-err));
1470ed5401bSmrg        return 1;
1480ed5401bSmrg    }
1490ed5401bSmrg
1500ed5401bSmrg    err = drm_tegra_job_submit(job, NULL);
1510ed5401bSmrg    if (err < 0) {
1520ed5401bSmrg        fprintf(stderr, "failed to submit job: %s\n", strerror(-err));
1530ed5401bSmrg        return 1;
1540ed5401bSmrg    }
1550ed5401bSmrg
1560ed5401bSmrg    err = drm_tegra_job_wait(job, 1000000000);
1570ed5401bSmrg    if (err < 0) {
1580ed5401bSmrg        fprintf(stderr, "failed to wait for job: %s\n", strerror(-err));
1590ed5401bSmrg        return 1;
1600ed5401bSmrg    }
1610ed5401bSmrg
1620ed5401bSmrg    printf("output: %ux%u\n", output->width, output->height);
1630ed5401bSmrg    vic_image_dump(output, stdout);
1640ed5401bSmrg
1650ed5401bSmrg    drm_tegra_job_free(job);
1660ed5401bSmrg    vic_image_free(output);
1670ed5401bSmrg    vic_free(vic);
1680ed5401bSmrg    drm_tegra_channel_close(channel);
1690ed5401bSmrg    drm_tegra_close(drm);
1700ed5401bSmrg    close(fd);
1710ed5401bSmrg
1720ed5401bSmrg    return 0;
1730ed5401bSmrg}
174