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