1d722e3fbSopenharmony_ci/* 2d722e3fbSopenharmony_ci * Copyright © 2018 NVIDIA Corporation 3d722e3fbSopenharmony_ci * 4d722e3fbSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5d722e3fbSopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6d722e3fbSopenharmony_ci * to deal in the Software without restriction, including without limitation 7d722e3fbSopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8d722e3fbSopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9d722e3fbSopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10d722e3fbSopenharmony_ci * 11d722e3fbSopenharmony_ci * The above copyright notice and this permission notice shall be included in 12d722e3fbSopenharmony_ci * all copies or substantial portions of the Software. 13d722e3fbSopenharmony_ci * 14d722e3fbSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15d722e3fbSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16d722e3fbSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17d722e3fbSopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18d722e3fbSopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19d722e3fbSopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20d722e3fbSopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE. 21d722e3fbSopenharmony_ci */ 22d722e3fbSopenharmony_ci 23d722e3fbSopenharmony_ci#include <errno.h> 24d722e3fbSopenharmony_ci#include <fcntl.h> 25d722e3fbSopenharmony_ci#include <stdio.h> 26d722e3fbSopenharmony_ci#include <string.h> 27d722e3fbSopenharmony_ci#include <unistd.h> 28d722e3fbSopenharmony_ci 29d722e3fbSopenharmony_ci#include "util_math.h" 30d722e3fbSopenharmony_ci 31d722e3fbSopenharmony_ci#include "tegra.h" 32d722e3fbSopenharmony_ci 33d722e3fbSopenharmony_ci#include "host1x.h" 34d722e3fbSopenharmony_ci#include "vic.h" 35d722e3fbSopenharmony_ci 36d722e3fbSopenharmony_ci#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) 37d722e3fbSopenharmony_ci 38d722e3fbSopenharmony_ciint main(int argc, char *argv[]) 39d722e3fbSopenharmony_ci{ 40d722e3fbSopenharmony_ci const unsigned int format = VIC_PIXEL_FORMAT_A8R8G8B8; 41d722e3fbSopenharmony_ci const unsigned int kind = VIC_BLK_KIND_PITCH; 42d722e3fbSopenharmony_ci const unsigned int width = 16, height = 16; 43d722e3fbSopenharmony_ci const char *device = "/dev/dri/renderD128"; 44d722e3fbSopenharmony_ci struct drm_tegra_channel *channel; 45d722e3fbSopenharmony_ci struct drm_tegra_pushbuf *pushbuf; 46d722e3fbSopenharmony_ci struct drm_tegra_job *job; 47d722e3fbSopenharmony_ci struct vic_image *output; 48d722e3fbSopenharmony_ci struct drm_tegra *drm; 49d722e3fbSopenharmony_ci unsigned int version; 50d722e3fbSopenharmony_ci struct vic *vic; 51d722e3fbSopenharmony_ci uint32_t *pb; 52d722e3fbSopenharmony_ci int fd, err; 53d722e3fbSopenharmony_ci void *ptr; 54d722e3fbSopenharmony_ci 55d722e3fbSopenharmony_ci if (argc > 1) 56d722e3fbSopenharmony_ci device = argv[1]; 57d722e3fbSopenharmony_ci 58d722e3fbSopenharmony_ci fd = open(device, O_RDWR); 59d722e3fbSopenharmony_ci if (fd < 0) { 60d722e3fbSopenharmony_ci fprintf(stderr, "open() failed: %s\n", strerror(errno)); 61d722e3fbSopenharmony_ci return 1; 62d722e3fbSopenharmony_ci } 63d722e3fbSopenharmony_ci 64d722e3fbSopenharmony_ci err = drm_tegra_new(fd, &drm); 65d722e3fbSopenharmony_ci if (err < 0) { 66d722e3fbSopenharmony_ci fprintf(stderr, "failed to open Tegra device: %s\n", strerror(-err)); 67d722e3fbSopenharmony_ci close(fd); 68d722e3fbSopenharmony_ci return 1; 69d722e3fbSopenharmony_ci } 70d722e3fbSopenharmony_ci 71d722e3fbSopenharmony_ci err = drm_tegra_channel_open(drm, DRM_TEGRA_VIC, &channel); 72d722e3fbSopenharmony_ci if (err < 0) { 73d722e3fbSopenharmony_ci fprintf(stderr, "failed to open channel to VIC: %s\n", strerror(-err)); 74d722e3fbSopenharmony_ci return 1; 75d722e3fbSopenharmony_ci } 76d722e3fbSopenharmony_ci 77d722e3fbSopenharmony_ci version = drm_tegra_channel_get_version(channel); 78d722e3fbSopenharmony_ci printf("version: %08x\n", version); 79d722e3fbSopenharmony_ci 80d722e3fbSopenharmony_ci err = vic_new(drm, channel, &vic); 81d722e3fbSopenharmony_ci if (err < 0) { 82d722e3fbSopenharmony_ci fprintf(stderr, "failed to create VIC: %s\n", strerror(-err)); 83d722e3fbSopenharmony_ci return 1; 84d722e3fbSopenharmony_ci } 85d722e3fbSopenharmony_ci 86d722e3fbSopenharmony_ci err = vic_image_new(vic, width, height, format, kind, DRM_TEGRA_CHANNEL_MAP_READ_WRITE, 87d722e3fbSopenharmony_ci &output); 88d722e3fbSopenharmony_ci if (err < 0) { 89d722e3fbSopenharmony_ci fprintf(stderr, "failed to create output image: %d\n", err); 90d722e3fbSopenharmony_ci return 1; 91d722e3fbSopenharmony_ci } 92d722e3fbSopenharmony_ci 93d722e3fbSopenharmony_ci printf("image: %zu bytes\n", output->size); 94d722e3fbSopenharmony_ci 95d722e3fbSopenharmony_ci err = drm_tegra_bo_map(output->bo, &ptr); 96d722e3fbSopenharmony_ci if (err < 0) { 97d722e3fbSopenharmony_ci fprintf(stderr, "failed to map output image: %d\n", err); 98d722e3fbSopenharmony_ci return 1; 99d722e3fbSopenharmony_ci } 100d722e3fbSopenharmony_ci 101d722e3fbSopenharmony_ci memset(ptr, 0xff, output->size); 102d722e3fbSopenharmony_ci drm_tegra_bo_unmap(output->bo); 103d722e3fbSopenharmony_ci 104d722e3fbSopenharmony_ci printf("output: %ux%u\n", output->width, output->height); 105d722e3fbSopenharmony_ci vic_image_dump(output, stdout); 106d722e3fbSopenharmony_ci 107d722e3fbSopenharmony_ci err = drm_tegra_job_new(channel, &job); 108d722e3fbSopenharmony_ci if (err < 0) { 109d722e3fbSopenharmony_ci fprintf(stderr, "failed to create job: %s\n", strerror(-err)); 110d722e3fbSopenharmony_ci return 1; 111d722e3fbSopenharmony_ci } 112d722e3fbSopenharmony_ci 113d722e3fbSopenharmony_ci err = drm_tegra_job_get_pushbuf(job, &pushbuf); 114d722e3fbSopenharmony_ci if (err < 0) { 115d722e3fbSopenharmony_ci fprintf(stderr, "failed to create push buffer: %s\n", strerror(-err)); 116d722e3fbSopenharmony_ci return 1; 117d722e3fbSopenharmony_ci } 118d722e3fbSopenharmony_ci 119d722e3fbSopenharmony_ci err = drm_tegra_pushbuf_begin(pushbuf, 32, &pb); 120d722e3fbSopenharmony_ci if (err < 0) { 121d722e3fbSopenharmony_ci fprintf(stderr, "failed to prepare push buffer: %s\n", strerror(-err)); 122d722e3fbSopenharmony_ci return 1; 123d722e3fbSopenharmony_ci } 124d722e3fbSopenharmony_ci 125d722e3fbSopenharmony_ci err = vic_clear(vic, output, 1023, 0, 0, 1023); 126d722e3fbSopenharmony_ci if (err < 0) { 127d722e3fbSopenharmony_ci fprintf(stderr, "failed to clear surface: %s\n", strerror(-err)); 128d722e3fbSopenharmony_ci return err; 129d722e3fbSopenharmony_ci } 130d722e3fbSopenharmony_ci 131d722e3fbSopenharmony_ci err = vic->ops->execute(vic, pushbuf, &pb, output, NULL, 0); 132d722e3fbSopenharmony_ci if (err < 0) { 133d722e3fbSopenharmony_ci fprintf(stderr, "failed to execute operation: %s\n", strerror(-err)); 134d722e3fbSopenharmony_ci return 1; 135d722e3fbSopenharmony_ci } 136d722e3fbSopenharmony_ci 137d722e3fbSopenharmony_ci err = drm_tegra_pushbuf_sync_cond(pushbuf, &pb, vic->syncpt, 138d722e3fbSopenharmony_ci DRM_TEGRA_SYNC_COND_OP_DONE); 139d722e3fbSopenharmony_ci if (err < 0) { 140d722e3fbSopenharmony_ci fprintf(stderr, "failed to push syncpoint: %s\n", strerror(-err)); 141d722e3fbSopenharmony_ci return 1; 142d722e3fbSopenharmony_ci } 143d722e3fbSopenharmony_ci 144d722e3fbSopenharmony_ci err = drm_tegra_pushbuf_end(pushbuf, pb); 145d722e3fbSopenharmony_ci if (err < 0) { 146d722e3fbSopenharmony_ci fprintf(stderr, "failed to update push buffer: %s\n", strerror(-err)); 147d722e3fbSopenharmony_ci return 1; 148d722e3fbSopenharmony_ci } 149d722e3fbSopenharmony_ci 150d722e3fbSopenharmony_ci err = drm_tegra_job_submit(job, NULL); 151d722e3fbSopenharmony_ci if (err < 0) { 152d722e3fbSopenharmony_ci fprintf(stderr, "failed to submit job: %s\n", strerror(-err)); 153d722e3fbSopenharmony_ci return 1; 154d722e3fbSopenharmony_ci } 155d722e3fbSopenharmony_ci 156d722e3fbSopenharmony_ci err = drm_tegra_job_wait(job, 1000000000); 157d722e3fbSopenharmony_ci if (err < 0) { 158d722e3fbSopenharmony_ci fprintf(stderr, "failed to wait for job: %s\n", strerror(-err)); 159d722e3fbSopenharmony_ci return 1; 160d722e3fbSopenharmony_ci } 161d722e3fbSopenharmony_ci 162d722e3fbSopenharmony_ci printf("output: %ux%u\n", output->width, output->height); 163d722e3fbSopenharmony_ci vic_image_dump(output, stdout); 164d722e3fbSopenharmony_ci 165d722e3fbSopenharmony_ci drm_tegra_job_free(job); 166d722e3fbSopenharmony_ci vic_image_free(output); 167d722e3fbSopenharmony_ci vic_free(vic); 168d722e3fbSopenharmony_ci drm_tegra_channel_close(channel); 169d722e3fbSopenharmony_ci drm_tegra_close(drm); 170d722e3fbSopenharmony_ci close(fd); 171d722e3fbSopenharmony_ci 172d722e3fbSopenharmony_ci return 0; 173d722e3fbSopenharmony_ci} 174