vic-blit.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Copyright © 2018 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. #include <errno.h>
  23. #include <fcntl.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include "tegra.h"
  28. #include "host1x.h"
  29. #include "vic.h"
  30. /* clear output image to red */
  31. static int clear(struct vic *vic, struct drm_tegra_channel *channel,
  32. struct vic_image *output)
  33. {
  34. struct drm_tegra_pushbuf *pushbuf;
  35. struct drm_tegra_job *job;
  36. uint32_t *ptr;
  37. int err;
  38. err = drm_tegra_job_new(channel, &job);
  39. if (err < 0) {
  40. fprintf(stderr, "failed to create job: %s\n", strerror(-err));
  41. return 1;
  42. }
  43. err = drm_tegra_job_get_pushbuf(job, &pushbuf);
  44. if (err < 0) {
  45. fprintf(stderr, "failed to create push buffer: %s\n", strerror(-err));
  46. return 1;
  47. }
  48. err = vic_clear(vic, output, 1023, 1023, 0, 0);
  49. if (err < 0) {
  50. fprintf(stderr, "failed to clear surface: %s\n", strerror(-err));
  51. return err;
  52. }
  53. err = drm_tegra_pushbuf_begin(pushbuf, 32, &ptr);
  54. if (err < 0) {
  55. fprintf(stderr, "failed to prepare push buffer: %s\n", strerror(-err));
  56. return err;
  57. }
  58. err = vic->ops->execute(vic, pushbuf, &ptr, output, NULL, 0);
  59. if (err < 0) {
  60. fprintf(stderr, "failed to execute operation: %s\n", strerror(-err));
  61. return err;
  62. }
  63. err = drm_tegra_pushbuf_sync_cond(pushbuf, &ptr, vic->syncpt,
  64. DRM_TEGRA_SYNC_COND_OP_DONE);
  65. if (err < 0) {
  66. fprintf(stderr, "failed to push syncpoint: %s\n", strerror(-err));
  67. return err;
  68. }
  69. err = drm_tegra_pushbuf_end(pushbuf, ptr);
  70. if (err < 0) {
  71. fprintf(stderr, "failed to update push buffer: %s\n", strerror(-err));
  72. return err;
  73. }
  74. err = drm_tegra_job_submit(job, NULL);
  75. if (err < 0) {
  76. fprintf(stderr, "failed to submit job: %s\n", strerror(-err));
  77. return err;
  78. }
  79. err = drm_tegra_job_wait(job, 1000000000);
  80. if (err < 0) {
  81. fprintf(stderr, "failed to wait for job: %s\n", strerror(-err));
  82. return err;
  83. }
  84. drm_tegra_job_free(job);
  85. return 0;
  86. }
  87. /* fill bottom half of image to blue */
  88. static int fill(struct vic *vic, struct drm_tegra_channel *channel,
  89. struct vic_image *output)
  90. {
  91. struct drm_tegra_pushbuf *pushbuf;
  92. struct drm_tegra_job *job;
  93. uint32_t *ptr;
  94. int err;
  95. err = drm_tegra_job_new(channel, &job);
  96. if (err < 0) {
  97. fprintf(stderr, "failed to create job: %s\n", strerror(-err));
  98. return 1;
  99. }
  100. err = drm_tegra_job_get_pushbuf(job, &pushbuf);
  101. if (err < 0) {
  102. fprintf(stderr, "failed to create push buffer: %s\n", strerror(-err));
  103. return 1;
  104. }
  105. err = drm_tegra_pushbuf_begin(pushbuf, 32, &ptr);
  106. if (err < 0) {
  107. fprintf(stderr, "failed to prepare push buffer: %s\n", strerror(-err));
  108. return err;
  109. }
  110. err = vic->ops->fill(vic, output, 0, output->height / 2, output->width - 1,
  111. output->height -1, 1023, 0, 0, 1023);
  112. if (err < 0) {
  113. fprintf(stderr, "failed to fill surface: %s\n", strerror(-err));
  114. return err;
  115. }
  116. err = vic->ops->execute(vic, pushbuf, &ptr, output, NULL, 0);
  117. if (err < 0) {
  118. fprintf(stderr, "failed to execute operation: %s\n", strerror(-err));
  119. return err;
  120. }
  121. err = drm_tegra_pushbuf_sync_cond(pushbuf, &ptr, vic->syncpt,
  122. DRM_TEGRA_SYNC_COND_OP_DONE);
  123. if (err < 0) {
  124. fprintf(stderr, "failed to push syncpoint: %s\n", strerror(-err));
  125. return err;
  126. }
  127. err = drm_tegra_pushbuf_end(pushbuf, ptr);
  128. if (err < 0) {
  129. fprintf(stderr, "failed to update push buffer: %s\n", strerror(-err));
  130. return err;
  131. }
  132. err = drm_tegra_job_submit(job, NULL);
  133. if (err < 0) {
  134. fprintf(stderr, "failed to submit job: %s\n", strerror(-err));
  135. return err;
  136. }
  137. err = drm_tegra_job_wait(job, 1000000000);
  138. if (err < 0) {
  139. fprintf(stderr, "failed to wait for job: %s\n", strerror(-err));
  140. return err;
  141. }
  142. drm_tegra_job_free(job);
  143. return 0;
  144. }
  145. /* blit image */
  146. static int blit(struct vic *vic, struct drm_tegra_channel *channel,
  147. struct vic_image *output, struct vic_image *input)
  148. {
  149. struct drm_tegra_pushbuf *pushbuf;
  150. struct drm_tegra_job *job;
  151. uint32_t *ptr;
  152. int err;
  153. err = drm_tegra_job_new(channel, &job);
  154. if (err < 0) {
  155. fprintf(stderr, "failed to create job: %s\n", strerror(-err));
  156. return 1;
  157. }
  158. err = drm_tegra_job_get_pushbuf(job, &pushbuf);
  159. if (err < 0) {
  160. fprintf(stderr, "failed to create push buffer: %s\n", strerror(-err));
  161. return 1;
  162. }
  163. err = drm_tegra_pushbuf_begin(pushbuf, 32, &ptr);
  164. if (err < 0) {
  165. fprintf(stderr, "failed to prepare push buffer: %s\n", strerror(-err));
  166. return err;
  167. }
  168. err = vic->ops->blit(vic, output, input);
  169. if (err < 0) {
  170. fprintf(stderr, "failed to blit surface: %s\n", strerror(-err));
  171. return err;
  172. }
  173. err = vic->ops->execute(vic, pushbuf, &ptr, output, &input, 1);
  174. if (err < 0) {
  175. fprintf(stderr, "failed to execute operation: %s\n", strerror(-err));
  176. return err;
  177. }
  178. err = drm_tegra_pushbuf_sync_cond(pushbuf, &ptr, vic->syncpt,
  179. DRM_TEGRA_SYNC_COND_OP_DONE);
  180. if (err < 0) {
  181. fprintf(stderr, "failed to push syncpoint: %s\n", strerror(-err));
  182. return err;
  183. }
  184. err = drm_tegra_pushbuf_end(pushbuf, ptr);
  185. if (err < 0) {
  186. fprintf(stderr, "failed to update push buffer: %s\n", strerror(-err));
  187. return err;
  188. }
  189. err = drm_tegra_job_submit(job, NULL);
  190. if (err < 0) {
  191. fprintf(stderr, "failed to submit job: %s\n", strerror(-err));
  192. return err;
  193. }
  194. err = drm_tegra_job_wait(job, 1000000000);
  195. if (err < 0) {
  196. fprintf(stderr, "failed to wait for job: %s\n", strerror(-err));
  197. return err;
  198. }
  199. drm_tegra_job_free(job);
  200. return 0;
  201. }
  202. int main(int argc, char *argv[])
  203. {
  204. const unsigned int format = VIC_PIXEL_FORMAT_A8R8G8B8;
  205. const unsigned int kind = VIC_BLK_KIND_PITCH;
  206. const unsigned int width = 16, height = 16;
  207. const char *device = "/dev/dri/renderD128";
  208. struct drm_tegra_channel *channel;
  209. struct vic_image *input, *output;
  210. struct drm_tegra *drm;
  211. unsigned int version;
  212. struct vic *vic;
  213. int fd, err;
  214. if (argc > 1)
  215. device = argv[1];
  216. fd = open(device, O_RDWR);
  217. if (fd < 0) {
  218. fprintf(stderr, "open() failed: %s\n", strerror(errno));
  219. return 1;
  220. }
  221. err = drm_tegra_new(fd, &drm);
  222. if (err < 0) {
  223. fprintf(stderr, "failed to open Tegra device: %s\n", strerror(-err));
  224. close(fd);
  225. return 1;
  226. }
  227. err = drm_tegra_channel_open(drm, DRM_TEGRA_VIC, &channel);
  228. if (err < 0) {
  229. fprintf(stderr, "failed to open channel to VIC: %s\n", strerror(-err));
  230. return 1;
  231. }
  232. version = drm_tegra_channel_get_version(channel);
  233. printf("version: %08x\n", version);
  234. err = vic_new(drm, channel, &vic);
  235. if (err < 0) {
  236. fprintf(stderr, "failed to create VIC: %s\n", strerror(-err));
  237. return 1;
  238. }
  239. err = vic_image_new(vic, width, height, format, kind, DRM_TEGRA_CHANNEL_MAP_READ_WRITE,
  240. &input);
  241. if (err < 0) {
  242. fprintf(stderr, "failed to create input image: %d\n", err);
  243. return 1;
  244. }
  245. err = vic_image_new(vic, width, height, format, kind, DRM_TEGRA_CHANNEL_MAP_READ_WRITE,
  246. &output);
  247. if (err < 0) {
  248. fprintf(stderr, "failed to create output image: %d\n", err);
  249. return 1;
  250. }
  251. err = clear(vic, channel, input);
  252. if (err < 0) {
  253. fprintf(stderr, "failed to clear image: %s\n", strerror(-err));
  254. return 1;
  255. }
  256. err = fill(vic, channel, input);
  257. if (err < 0) {
  258. fprintf(stderr, "failed to fill rectangle: %s\n", strerror(-err));
  259. return 1;
  260. }
  261. err = blit(vic, channel, output, input);
  262. if (err < 0) {
  263. fprintf(stderr, "failed to blit image: %s\n", strerror(-err));
  264. return 1;
  265. }
  266. printf("input: %ux%u\n", input->width, input->height);
  267. vic_image_dump(input, stdout);
  268. printf("output: %ux%u\n", output->width, output->height);
  269. vic_image_dump(output, stdout);
  270. vic_image_free(output);
  271. vic_image_free(input);
  272. vic_free(vic);
  273. drm_tegra_channel_close(channel);
  274. drm_tegra_close(drm);
  275. close(fd);
  276. return 0;
  277. }