1d722e3fbSopenharmony_ci/* 2d722e3fbSopenharmony_ci * Copyright © 2014 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#ifdef HAVE_CONFIG_H 24d722e3fbSopenharmony_ci# include "config.h" 25d722e3fbSopenharmony_ci#endif 26d722e3fbSopenharmony_ci 27d722e3fbSopenharmony_ci#include <errno.h> 28d722e3fbSopenharmony_ci#include <fcntl.h> 29d722e3fbSopenharmony_ci#include <stdbool.h> 30d722e3fbSopenharmony_ci#include <stdint.h> 31d722e3fbSopenharmony_ci#include <stdio.h> 32d722e3fbSopenharmony_ci#include <stdlib.h> 33d722e3fbSopenharmony_ci#include <string.h> 34d722e3fbSopenharmony_ci#include <unistd.h> 35d722e3fbSopenharmony_ci 36d722e3fbSopenharmony_ci#include <sys/ioctl.h> 37d722e3fbSopenharmony_ci 38d722e3fbSopenharmony_ci#include "xf86drm.h" 39d722e3fbSopenharmony_ci#include "xf86drmMode.h" 40d722e3fbSopenharmony_ci#include "drm_fourcc.h" 41d722e3fbSopenharmony_ci 42d722e3fbSopenharmony_ci#include "drm-test-tegra.h" 43d722e3fbSopenharmony_ci#include "tegra.h" 44d722e3fbSopenharmony_ci 45d722e3fbSopenharmony_ciint main(int argc, char *argv[]) 46d722e3fbSopenharmony_ci{ 47d722e3fbSopenharmony_ci uint32_t format = DRM_FORMAT_XRGB8888; 48d722e3fbSopenharmony_ci struct drm_tegra_gr2d *gr2d; 49d722e3fbSopenharmony_ci struct drm_framebuffer *fb; 50d722e3fbSopenharmony_ci struct drm_screen *screen; 51d722e3fbSopenharmony_ci unsigned int pitch, size; 52d722e3fbSopenharmony_ci struct drm_tegra_bo *bo; 53d722e3fbSopenharmony_ci struct drm_tegra *drm; 54d722e3fbSopenharmony_ci uint32_t handle; 55d722e3fbSopenharmony_ci int fd, err; 56d722e3fbSopenharmony_ci void *ptr; 57d722e3fbSopenharmony_ci 58d722e3fbSopenharmony_ci fd = drm_open(argv[1]); 59d722e3fbSopenharmony_ci if (fd < 0) { 60d722e3fbSopenharmony_ci fprintf(stderr, "failed to open DRM device %s: %s\n", argv[1], 61d722e3fbSopenharmony_ci strerror(errno)); 62d722e3fbSopenharmony_ci return 1; 63d722e3fbSopenharmony_ci } 64d722e3fbSopenharmony_ci 65d722e3fbSopenharmony_ci err = drm_screen_open(&screen, fd); 66d722e3fbSopenharmony_ci if (err < 0) { 67d722e3fbSopenharmony_ci fprintf(stderr, "failed to open screen: %s\n", strerror(-err)); 68d722e3fbSopenharmony_ci return 1; 69d722e3fbSopenharmony_ci } 70d722e3fbSopenharmony_ci 71d722e3fbSopenharmony_ci err = drm_tegra_new(fd, &drm); 72d722e3fbSopenharmony_ci if (err < 0) { 73d722e3fbSopenharmony_ci fprintf(stderr, "failed to create Tegra DRM context: %s\n", 74d722e3fbSopenharmony_ci strerror(-err)); 75d722e3fbSopenharmony_ci return 1; 76d722e3fbSopenharmony_ci } 77d722e3fbSopenharmony_ci 78d722e3fbSopenharmony_ci err = drm_tegra_gr2d_open(drm, &gr2d); 79d722e3fbSopenharmony_ci if (err < 0) { 80d722e3fbSopenharmony_ci fprintf(stderr, "failed to open gr2d channel: %s\n", 81d722e3fbSopenharmony_ci strerror(-err)); 82d722e3fbSopenharmony_ci return 1; 83d722e3fbSopenharmony_ci } 84d722e3fbSopenharmony_ci 85d722e3fbSopenharmony_ci pitch = screen->width * screen->bpp / 8; 86d722e3fbSopenharmony_ci size = pitch * screen->height; 87d722e3fbSopenharmony_ci 88d722e3fbSopenharmony_ci err = drm_tegra_bo_new(drm, 0, size, &bo); 89d722e3fbSopenharmony_ci if (err < 0) { 90d722e3fbSopenharmony_ci fprintf(stderr, "failed to create buffer object: %s\n", 91d722e3fbSopenharmony_ci strerror(-err)); 92d722e3fbSopenharmony_ci return 1; 93d722e3fbSopenharmony_ci } 94d722e3fbSopenharmony_ci 95d722e3fbSopenharmony_ci err = drm_tegra_bo_get_handle(bo, &handle); 96d722e3fbSopenharmony_ci if (err < 0) { 97d722e3fbSopenharmony_ci fprintf(stderr, "failed to get handle to buffer object: %s\n", 98d722e3fbSopenharmony_ci strerror(-err)); 99d722e3fbSopenharmony_ci return 1; 100d722e3fbSopenharmony_ci } 101d722e3fbSopenharmony_ci 102d722e3fbSopenharmony_ci err = drm_tegra_bo_map(bo, &ptr); 103d722e3fbSopenharmony_ci if (err < 0) { 104d722e3fbSopenharmony_ci fprintf(stderr, "failed to map buffer object: %s\n", 105d722e3fbSopenharmony_ci strerror(-err)); 106d722e3fbSopenharmony_ci return 1; 107d722e3fbSopenharmony_ci } 108d722e3fbSopenharmony_ci 109d722e3fbSopenharmony_ci memset(ptr, 0xff, size); 110d722e3fbSopenharmony_ci 111d722e3fbSopenharmony_ci err = drm_framebuffer_new(&fb, screen, handle, screen->width, 112d722e3fbSopenharmony_ci screen->height, pitch, format, bo); 113d722e3fbSopenharmony_ci if (err < 0) { 114d722e3fbSopenharmony_ci fprintf(stderr, "failed to create framebuffer: %s\n", 115d722e3fbSopenharmony_ci strerror(-err)); 116d722e3fbSopenharmony_ci return 1; 117d722e3fbSopenharmony_ci } 118d722e3fbSopenharmony_ci 119d722e3fbSopenharmony_ci err = drm_screen_set_framebuffer(screen, fb); 120d722e3fbSopenharmony_ci if (err < 0) { 121d722e3fbSopenharmony_ci fprintf(stderr, "failed to display framebuffer: %s\n", 122d722e3fbSopenharmony_ci strerror(-err)); 123d722e3fbSopenharmony_ci return 1; 124d722e3fbSopenharmony_ci } 125d722e3fbSopenharmony_ci 126d722e3fbSopenharmony_ci sleep(1); 127d722e3fbSopenharmony_ci 128d722e3fbSopenharmony_ci err = drm_tegra_gr2d_fill(gr2d, fb, fb->width / 4, fb->height / 4, 129d722e3fbSopenharmony_ci fb->width / 2, fb->height / 2, 0x00000000); 130d722e3fbSopenharmony_ci if (err < 0) { 131d722e3fbSopenharmony_ci fprintf(stderr, "failed to fill rectangle: %s\n", 132d722e3fbSopenharmony_ci strerror(-err)); 133d722e3fbSopenharmony_ci return 1; 134d722e3fbSopenharmony_ci } 135d722e3fbSopenharmony_ci 136d722e3fbSopenharmony_ci sleep(1); 137d722e3fbSopenharmony_ci 138d722e3fbSopenharmony_ci drm_framebuffer_free(fb); 139d722e3fbSopenharmony_ci drm_tegra_bo_unref(bo); 140d722e3fbSopenharmony_ci drm_tegra_gr2d_close(gr2d); 141d722e3fbSopenharmony_ci drm_tegra_close(drm); 142d722e3fbSopenharmony_ci drm_screen_close(screen); 143d722e3fbSopenharmony_ci drm_close(fd); 144d722e3fbSopenharmony_ci 145d722e3fbSopenharmony_ci return 0; 146d722e3fbSopenharmony_ci} 147