exynos_fimg2d_event.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Copyright (C) 2015 - Tobias Jakobi
  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 (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  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.
  22. */
  23. #include <unistd.h>
  24. #include <poll.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <time.h>
  28. #include <getopt.h>
  29. #include <pthread.h>
  30. #include <xf86drm.h>
  31. #include "exynos_drm.h"
  32. #include "exynos_drmif.h"
  33. #include "exynos_fimg2d.h"
  34. struct g2d_job {
  35. unsigned int id;
  36. unsigned int busy;
  37. };
  38. struct exynos_evhandler {
  39. struct pollfd fds;
  40. struct exynos_event_context evctx;
  41. };
  42. struct threaddata {
  43. unsigned int stop;
  44. struct exynos_device *dev;
  45. struct exynos_evhandler evhandler;
  46. };
  47. static void g2d_event_handler(int fd, unsigned int cmdlist_no, unsigned int tv_sec,
  48. unsigned int tv_usec, void *user_data)
  49. {
  50. struct g2d_job *job = user_data;
  51. fprintf(stderr, "info: g2d job (id = %u, cmdlist number = %u) finished!\n",
  52. job->id, cmdlist_no);
  53. job->busy = 0;
  54. }
  55. static void setup_g2d_event_handler(struct exynos_evhandler *evhandler, int fd)
  56. {
  57. evhandler->fds.fd = fd;
  58. evhandler->fds.events = POLLIN;
  59. evhandler->evctx.base.version = 2;
  60. evhandler->evctx.version = 1;
  61. evhandler->evctx.g2d_event_handler = g2d_event_handler;
  62. }
  63. static void* threadfunc(void *arg) {
  64. const int timeout = 0;
  65. struct threaddata *data;
  66. data = arg;
  67. while (1) {
  68. if (data->stop) break;
  69. usleep(500);
  70. data->evhandler.fds.revents = 0;
  71. if (poll(&data->evhandler.fds, 1, timeout) < 0)
  72. continue;
  73. if (data->evhandler.fds.revents & (POLLHUP | POLLERR))
  74. continue;
  75. if (data->evhandler.fds.revents & POLLIN)
  76. exynos_handle_event(data->dev, &data->evhandler.evctx);
  77. }
  78. pthread_exit(0);
  79. }
  80. /*
  81. * We need to wait until all G2D jobs are finished, otherwise we
  82. * potentially remove a BO which the engine still operates on.
  83. * This results in the following kernel message:
  84. * [drm:exynos_drm_gem_put_dma_addr] *ERROR* failed to lookup gem object.
  85. * Also any subsequent BO allocations fail then with:
  86. * [drm:exynos_drm_alloc_buf] *ERROR* failed to allocate buffer.
  87. */
  88. static void wait_all_jobs(struct g2d_job* jobs, unsigned num_jobs)
  89. {
  90. unsigned i;
  91. for (i = 0; i < num_jobs; ++i) {
  92. while (jobs[i].busy)
  93. usleep(500);
  94. }
  95. }
  96. static struct g2d_job* free_job(struct g2d_job* jobs, unsigned num_jobs)
  97. {
  98. unsigned i;
  99. for (i = 0; i < num_jobs; ++i) {
  100. if (jobs[i].busy == 0)
  101. return &jobs[i];
  102. }
  103. return NULL;
  104. }
  105. static int g2d_work(struct g2d_context *ctx, struct g2d_image *img,
  106. unsigned num_jobs, unsigned iterations)
  107. {
  108. struct g2d_job *jobs = calloc(num_jobs, sizeof(struct g2d_job));
  109. int ret;
  110. unsigned i;
  111. /* setup job ids */
  112. for (i = 0; i < num_jobs; ++i)
  113. jobs[i].id = i;
  114. for (i = 0; i < iterations; ++i) {
  115. unsigned x, y, w, h;
  116. struct g2d_job *j = NULL;
  117. while (1) {
  118. j = free_job(jobs, num_jobs);
  119. if (j)
  120. break;
  121. else
  122. usleep(500);
  123. }
  124. x = rand() % img->width;
  125. y = rand() % img->height;
  126. if (x == (img->width - 1))
  127. x -= 1;
  128. if (y == (img->height - 1))
  129. y -= 1;
  130. w = rand() % (img->width - x);
  131. h = rand() % (img->height - y);
  132. if (w == 0) w = 1;
  133. if (h == 0) h = 1;
  134. img->color = rand();
  135. j->busy = 1;
  136. g2d_config_event(ctx, j);
  137. ret = g2d_solid_fill(ctx, img, x, y, w, h);
  138. if (ret == 0)
  139. g2d_exec(ctx);
  140. if (ret != 0) {
  141. fprintf(stderr, "error: iteration %u (x = %u, x = %u, x = %u, x = %u) failed\n",
  142. i, x, y, w, h);
  143. break;
  144. }
  145. }
  146. wait_all_jobs(jobs, num_jobs);
  147. free(jobs);
  148. return 0;
  149. }
  150. static void usage(const char *name)
  151. {
  152. fprintf(stderr, "usage: %s [-ijwh]\n\n", name);
  153. fprintf(stderr, "\t-i <number of iterations>\n");
  154. fprintf(stderr, "\t-j <number of G2D jobs> (default = 4)\n\n");
  155. fprintf(stderr, "\t-w <buffer width> (default = 4096)\n");
  156. fprintf(stderr, "\t-h <buffer height> (default = 4096)\n");
  157. exit(0);
  158. }
  159. int main(int argc, char **argv)
  160. {
  161. int fd, ret, c, parsefail;
  162. pthread_t event_thread;
  163. struct threaddata event_data = {0};
  164. struct exynos_device *dev;
  165. struct g2d_context *ctx;
  166. struct exynos_bo *bo;
  167. struct g2d_image img = {0};
  168. unsigned int iters = 0, njobs = 4;
  169. unsigned int bufw = 4096, bufh = 4096;
  170. ret = 0;
  171. parsefail = 0;
  172. while ((c = getopt(argc, argv, "i:j:w:h:")) != -1) {
  173. switch (c) {
  174. case 'i':
  175. if (sscanf(optarg, "%u", &iters) != 1)
  176. parsefail = 1;
  177. break;
  178. case 'j':
  179. if (sscanf(optarg, "%u", &njobs) != 1)
  180. parsefail = 1;
  181. break;
  182. case 'w':
  183. if (sscanf(optarg, "%u", &bufw) != 1)
  184. parsefail = 1;
  185. break;
  186. case 'h':
  187. if (sscanf(optarg, "%u", &bufh) != 1)
  188. parsefail = 1;
  189. break;
  190. default:
  191. parsefail = 1;
  192. break;
  193. }
  194. }
  195. if (parsefail || (argc == 1) || (iters == 0))
  196. usage(argv[0]);
  197. if (bufw > 4096 || bufh > 4096) {
  198. fprintf(stderr, "error: buffer width/height should be less than 4096.\n");
  199. ret = -1;
  200. goto out;
  201. }
  202. if (bufw == 0 || bufh == 0) {
  203. fprintf(stderr, "error: buffer width/height should be non-zero.\n");
  204. ret = -1;
  205. goto out;
  206. }
  207. fd = drmOpen("exynos", NULL);
  208. if (fd < 0) {
  209. fprintf(stderr, "error: failed to open drm\n");
  210. ret = -1;
  211. goto out;
  212. }
  213. dev = exynos_device_create(fd);
  214. if (dev == NULL) {
  215. fprintf(stderr, "error: failed to create device\n");
  216. ret = -2;
  217. goto fail;
  218. }
  219. ctx = g2d_init(fd);
  220. if (ctx == NULL) {
  221. fprintf(stderr, "error: failed to init G2D\n");
  222. ret = -3;
  223. goto g2d_fail;
  224. }
  225. bo = exynos_bo_create(dev, bufw * bufh * 4, 0);
  226. if (bo == NULL) {
  227. fprintf(stderr, "error: failed to create bo\n");
  228. ret = -4;
  229. goto bo_fail;
  230. }
  231. /* setup g2d image object */
  232. img.width = bufw;
  233. img.height = bufh;
  234. img.stride = bufw * 4;
  235. img.color_mode = G2D_COLOR_FMT_ARGB8888 | G2D_ORDER_AXRGB;
  236. img.buf_type = G2D_IMGBUF_GEM;
  237. img.bo[0] = bo->handle;
  238. event_data.dev = dev;
  239. setup_g2d_event_handler(&event_data.evhandler, fd);
  240. pthread_create(&event_thread, NULL, threadfunc, &event_data);
  241. ret = g2d_work(ctx, &img, njobs, iters);
  242. if (ret != 0)
  243. fprintf(stderr, "error: g2d_work failed\n");
  244. event_data.stop = 1;
  245. pthread_join(event_thread, NULL);
  246. exynos_bo_destroy(bo);
  247. bo_fail:
  248. g2d_fini(ctx);
  249. g2d_fail:
  250. exynos_device_destroy(dev);
  251. fail:
  252. drmClose(fd);
  253. out:
  254. return ret;
  255. }