vic.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <stdio.h> /* XXX remove */
  24. #include <stdlib.h>
  25. #include "util_math.h"
  26. #include "tegra.h"
  27. #include "host1x.h"
  28. #include "vic.h"
  29. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  30. const struct vic_format_info *vic_format_get_info(unsigned int format)
  31. {
  32. static const struct vic_format_info formats[] = {
  33. { .format = VIC_PIXEL_FORMAT_A8R8G8B8, .cpp = 4 },
  34. };
  35. unsigned int i;
  36. for (i = 0; i < ARRAY_SIZE(formats); i++) {
  37. if (formats[i].format == format)
  38. return &formats[i];
  39. }
  40. return 0;
  41. }
  42. int vic_image_new(struct vic *vic, unsigned int width, unsigned int height,
  43. unsigned int format, unsigned int kind, uint32_t flags,
  44. struct vic_image **imagep)
  45. {
  46. const struct vic_format_info *info = vic_format_get_info(format);
  47. struct vic_image *image;
  48. int err;
  49. if (!info)
  50. return -EINVAL;
  51. image = calloc(1, sizeof(*image));
  52. if (!image)
  53. return -ENOMEM;
  54. if (kind == VIC_BLK_KIND_PITCH)
  55. image->align = 256;
  56. else
  57. image->align = 256; /* XXX */
  58. image->width = width;
  59. image->stride = ALIGN(width, image->align);
  60. image->pitch = image->stride * info->cpp;
  61. image->height = height;
  62. image->format = format;
  63. image->kind = kind;
  64. image->size = image->pitch * image->height;
  65. printf("image: %ux%u align: %zu stride: %u pitch: %u size: %zu\n",
  66. image->width, image->height, image->align, image->stride,
  67. image->pitch, image->size);
  68. err = drm_tegra_bo_new(vic->drm, 0, image->size, &image->bo);
  69. if (err < 0) {
  70. free(image);
  71. return err;
  72. }
  73. err = drm_tegra_channel_map(vic->channel, image->bo, flags, &image->map);
  74. if (err < 0) {
  75. drm_tegra_bo_unref(image->bo);
  76. free(image);
  77. return err;
  78. }
  79. *imagep = image;
  80. return 0;
  81. }
  82. void vic_image_free(struct vic_image *image)
  83. {
  84. if (image) {
  85. drm_tegra_channel_unmap(image->map);
  86. drm_tegra_bo_unref(image->bo);
  87. free(image);
  88. }
  89. }
  90. void vic_image_dump(struct vic_image *image, FILE *fp)
  91. {
  92. unsigned int i, j;
  93. void *ptr;
  94. int err;
  95. err = drm_tegra_bo_map(image->bo, &ptr);
  96. if (err < 0)
  97. return;
  98. for (j = 0; j < image->height; j++) {
  99. uint32_t *pixels = (uint32_t *)((unsigned long)ptr + j * image->pitch);
  100. printf(" ");
  101. for (i = 0; i < image->width; i++)
  102. printf(" %08x", pixels[i]);
  103. printf("\n");
  104. }
  105. drm_tegra_bo_unmap(image->bo);
  106. }
  107. /* from vic30.c */
  108. int vic30_new(struct drm_tegra *drm, struct drm_tegra_channel *channel,
  109. struct vic **vicp);
  110. /* from vic40.c */
  111. int vic40_new(struct drm_tegra *drm, struct drm_tegra_channel *channel,
  112. struct vic **vicp);
  113. /* from vic41.c */
  114. int vic41_new(struct drm_tegra *drm, struct drm_tegra_channel *channel,
  115. struct vic **vicp);
  116. /* from vic42.c */
  117. int vic42_new(struct drm_tegra *drm, struct drm_tegra_channel *channel,
  118. struct vic **vicp);
  119. int vic_new(struct drm_tegra *drm, struct drm_tegra_channel *channel,
  120. struct vic **vicp)
  121. {
  122. unsigned int version;
  123. version = drm_tegra_channel_get_version(channel);
  124. switch (version) {
  125. case 0x40:
  126. return vic30_new(drm, channel, vicp);
  127. case 0x21:
  128. return vic40_new(drm, channel, vicp);
  129. case 0x18:
  130. return vic41_new(drm, channel, vicp);
  131. case 0x19:
  132. return vic42_new(drm, channel, vicp);
  133. }
  134. return -ENOTSUP;
  135. }
  136. void vic_free(struct vic *vic)
  137. {
  138. if (vic)
  139. vic->ops->free(vic);
  140. }
  141. int vic_clear(struct vic *vic, struct vic_image *output, unsigned int alpha,
  142. unsigned int red, unsigned int green, unsigned int blue)
  143. {
  144. return vic->ops->fill(vic, output, 0, 0, output->width - 1,
  145. output->height - 1, alpha, red, green, blue);
  146. }