1
0

vic-clear.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "util_math.h"
  28. #include "tegra.h"
  29. #include "host1x.h"
  30. #include "vic.h"
  31. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  32. int main(int argc, char *argv[])
  33. {
  34. const unsigned int format = VIC_PIXEL_FORMAT_A8R8G8B8;
  35. const unsigned int kind = VIC_BLK_KIND_PITCH;
  36. const unsigned int width = 16, height = 16;
  37. const char *device = "/dev/dri/renderD128";
  38. struct drm_tegra_channel *channel;
  39. struct drm_tegra_pushbuf *pushbuf;
  40. struct drm_tegra_job *job;
  41. struct vic_image *output;
  42. struct drm_tegra *drm;
  43. unsigned int version;
  44. struct vic *vic;
  45. uint32_t *pb;
  46. int fd, err;
  47. void *ptr;
  48. if (argc > 1)
  49. device = argv[1];
  50. fd = open(device, O_RDWR);
  51. if (fd < 0) {
  52. fprintf(stderr, "open() failed: %s\n", strerror(errno));
  53. return 1;
  54. }
  55. err = drm_tegra_new(fd, &drm);
  56. if (err < 0) {
  57. fprintf(stderr, "failed to open Tegra device: %s\n", strerror(-err));
  58. close(fd);
  59. return 1;
  60. }
  61. err = drm_tegra_channel_open(drm, DRM_TEGRA_VIC, &channel);
  62. if (err < 0) {
  63. fprintf(stderr, "failed to open channel to VIC: %s\n", strerror(-err));
  64. return 1;
  65. }
  66. version = drm_tegra_channel_get_version(channel);
  67. printf("version: %08x\n", version);
  68. err = vic_new(drm, channel, &vic);
  69. if (err < 0) {
  70. fprintf(stderr, "failed to create VIC: %s\n", strerror(-err));
  71. return 1;
  72. }
  73. err = vic_image_new(vic, width, height, format, kind, DRM_TEGRA_CHANNEL_MAP_READ_WRITE,
  74. &output);
  75. if (err < 0) {
  76. fprintf(stderr, "failed to create output image: %d\n", err);
  77. return 1;
  78. }
  79. printf("image: %zu bytes\n", output->size);
  80. err = drm_tegra_bo_map(output->bo, &ptr);
  81. if (err < 0) {
  82. fprintf(stderr, "failed to map output image: %d\n", err);
  83. return 1;
  84. }
  85. memset(ptr, 0xff, output->size);
  86. drm_tegra_bo_unmap(output->bo);
  87. printf("output: %ux%u\n", output->width, output->height);
  88. vic_image_dump(output, stdout);
  89. err = drm_tegra_job_new(channel, &job);
  90. if (err < 0) {
  91. fprintf(stderr, "failed to create job: %s\n", strerror(-err));
  92. return 1;
  93. }
  94. err = drm_tegra_job_get_pushbuf(job, &pushbuf);
  95. if (err < 0) {
  96. fprintf(stderr, "failed to create push buffer: %s\n", strerror(-err));
  97. return 1;
  98. }
  99. err = drm_tegra_pushbuf_begin(pushbuf, 32, &pb);
  100. if (err < 0) {
  101. fprintf(stderr, "failed to prepare push buffer: %s\n", strerror(-err));
  102. return 1;
  103. }
  104. err = vic_clear(vic, output, 1023, 0, 0, 1023);
  105. if (err < 0) {
  106. fprintf(stderr, "failed to clear surface: %s\n", strerror(-err));
  107. return err;
  108. }
  109. err = vic->ops->execute(vic, pushbuf, &pb, output, NULL, 0);
  110. if (err < 0) {
  111. fprintf(stderr, "failed to execute operation: %s\n", strerror(-err));
  112. return 1;
  113. }
  114. err = drm_tegra_pushbuf_sync_cond(pushbuf, &pb, vic->syncpt,
  115. DRM_TEGRA_SYNC_COND_OP_DONE);
  116. if (err < 0) {
  117. fprintf(stderr, "failed to push syncpoint: %s\n", strerror(-err));
  118. return 1;
  119. }
  120. err = drm_tegra_pushbuf_end(pushbuf, pb);
  121. if (err < 0) {
  122. fprintf(stderr, "failed to update push buffer: %s\n", strerror(-err));
  123. return 1;
  124. }
  125. err = drm_tegra_job_submit(job, NULL);
  126. if (err < 0) {
  127. fprintf(stderr, "failed to submit job: %s\n", strerror(-err));
  128. return 1;
  129. }
  130. err = drm_tegra_job_wait(job, 1000000000);
  131. if (err < 0) {
  132. fprintf(stderr, "failed to wait for job: %s\n", strerror(-err));
  133. return 1;
  134. }
  135. printf("output: %ux%u\n", output->width, output->height);
  136. vic_image_dump(output, stdout);
  137. drm_tegra_job_free(job);
  138. vic_image_free(output);
  139. vic_free(vic);
  140. drm_tegra_channel_close(channel);
  141. drm_tegra_close(drm);
  142. close(fd);
  143. return 0;
  144. }