2 * Copyright (C) 2015 - Tobias Jakobi
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:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
36 #include "exynos_drm.h"
37 #include "exynos_drmif.h"
38 #include "exynos_fimg2d.h"
45 struct exynos_evhandler
{
47 struct exynos_event_context evctx
;
52 struct exynos_device
*dev
;
53 struct exynos_evhandler evhandler
;
56 static void g2d_event_handler(int fd
, unsigned int cmdlist_no
, unsigned int tv_sec
,
57 unsigned int tv_usec
, void *user_data
)
59 struct g2d_job
*job
= user_data
;
61 fprintf(stderr
, "info: g2d job (id = %u, cmdlist number = %u) finished!\n",
67 static void setup_g2d_event_handler(struct exynos_evhandler
*evhandler
, int fd
)
69 evhandler
->fds
.fd
= fd
;
70 evhandler
->fds
.events
= POLLIN
;
71 evhandler
->evctx
.base
.version
= 2;
72 evhandler
->evctx
.version
= 1;
73 evhandler
->evctx
.g2d_event_handler
= g2d_event_handler
;
76 static void* threadfunc(void *arg
) {
77 const int timeout
= 0;
78 struct threaddata
*data
;
83 if (data
->stop
) break;
87 data
->evhandler
.fds
.revents
= 0;
89 if (poll(&data
->evhandler
.fds
, 1, timeout
) < 0)
92 if (data
->evhandler
.fds
.revents
& (POLLHUP
| POLLERR
))
95 if (data
->evhandler
.fds
.revents
& POLLIN
)
96 exynos_handle_event(data
->dev
, &data
->evhandler
.evctx
);
103 * We need to wait until all G2D jobs are finished, otherwise we
104 * potentially remove a BO which the engine still operates on.
105 * This results in the following kernel message:
106 * [drm:exynos_drm_gem_put_dma_addr] *ERROR* failed to lookup gem object.
107 * Also any subsequent BO allocations fail then with:
108 * [drm:exynos_drm_alloc_buf] *ERROR* failed to allocate buffer.
110 static void wait_all_jobs(struct g2d_job
* jobs
, unsigned num_jobs
)
114 for (i
= 0; i
< num_jobs
; ++i
) {
121 static struct g2d_job
* free_job(struct g2d_job
* jobs
, unsigned num_jobs
)
125 for (i
= 0; i
< num_jobs
; ++i
) {
126 if (jobs
[i
].busy
== 0)
133 static int g2d_work(struct g2d_context
*ctx
, struct g2d_image
*img
,
134 unsigned num_jobs
, unsigned iterations
)
136 struct g2d_job
*jobs
= calloc(num_jobs
, sizeof(struct g2d_job
));
141 for (i
= 0; i
< num_jobs
; ++i
)
144 for (i
= 0; i
< iterations
; ++i
) {
147 struct g2d_job
*j
= NULL
;
150 j
= free_job(jobs
, num_jobs
);
158 x
= rand() % img
->width
;
159 y
= rand() % img
->height
;
161 if (x
== (img
->width
- 1))
163 if (y
== (img
->height
- 1))
166 w
= rand() % (img
->width
- x
);
167 h
= rand() % (img
->height
- y
);
175 g2d_config_event(ctx
, j
);
177 ret
= g2d_solid_fill(ctx
, img
, x
, y
, w
, h
);
183 fprintf(stderr
, "error: iteration %u (x = %u, x = %u, x = %u, x = %u) failed\n",
189 wait_all_jobs(jobs
, num_jobs
);
195 static void usage(const char *name
)
197 fprintf(stderr
, "usage: %s [-ijwh]\n\n", name
);
199 fprintf(stderr
, "\t-i <number of iterations>\n");
200 fprintf(stderr
, "\t-j <number of G2D jobs> (default = 4)\n\n");
202 fprintf(stderr
, "\t-w <buffer width> (default = 4096)\n");
203 fprintf(stderr
, "\t-h <buffer height> (default = 4096)\n");
208 int main(int argc
, char **argv
)
210 int fd
, ret
, c
, parsefail
;
212 pthread_t event_thread
;
213 struct threaddata event_data
= {0};
215 struct exynos_device
*dev
;
216 struct g2d_context
*ctx
;
217 struct exynos_bo
*bo
;
219 struct g2d_image img
= {0};
221 unsigned int iters
= 0, njobs
= 4;
222 unsigned int bufw
= 4096, bufh
= 4096;
227 while ((c
= getopt(argc
, argv
, "i:j:w:h:")) != -1) {
230 if (sscanf(optarg
, "%u", &iters
) != 1)
234 if (sscanf(optarg
, "%u", &njobs
) != 1)
238 if (sscanf(optarg
, "%u", &bufw
) != 1)
242 if (sscanf(optarg
, "%u", &bufh
) != 1)
251 if (parsefail
|| (argc
== 1) || (iters
== 0))
254 if (bufw
> 4096 || bufh
> 4096) {
255 fprintf(stderr
, "error: buffer width/height should be less than 4096.\n");
261 if (bufw
== 0 || bufh
== 0) {
262 fprintf(stderr
, "error: buffer width/height should be non-zero.\n");
268 fd
= drmOpen("exynos", NULL
);
270 fprintf(stderr
, "error: failed to open drm\n");
276 dev
= exynos_device_create(fd
);
278 fprintf(stderr
, "error: failed to create device\n");
286 fprintf(stderr
, "error: failed to init G2D\n");
292 bo
= exynos_bo_create(dev
, bufw
* bufh
* 4, 0);
294 fprintf(stderr
, "error: failed to create bo\n");
300 /* setup g2d image object */
303 img
.stride
= bufw
* 4;
304 img
.color_mode
= G2D_COLOR_FMT_ARGB8888
| G2D_ORDER_AXRGB
;
305 img
.buf_type
= G2D_IMGBUF_GEM
;
306 img
.bo
[0] = bo
->handle
;
308 event_data
.dev
= dev
;
309 setup_g2d_event_handler(&event_data
.evhandler
, fd
);
311 pthread_create(&event_thread
, NULL
, threadfunc
, &event_data
);
313 ret
= g2d_work(ctx
, &img
, njobs
, iters
);
315 fprintf(stderr
, "error: g2d_work failed\n");
318 pthread_join(event_thread
, NULL
);
320 exynos_bo_destroy(bo
);
326 exynos_device_destroy(dev
);