drm-test-tegra.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright © 2014 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. #ifdef HAVE_CONFIG_H
  23. # include "config.h"
  24. #endif
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #include "drm-test-tegra.h"
  28. #include "tegra.h"
  29. int drm_tegra_gr2d_open(struct drm_tegra *drm, struct drm_tegra_gr2d **gr2dp)
  30. {
  31. struct drm_tegra_gr2d *gr2d;
  32. int err;
  33. gr2d = calloc(1, sizeof(*gr2d));
  34. if (!gr2d)
  35. return -ENOMEM;
  36. gr2d->drm = drm;
  37. err = drm_tegra_channel_open(drm, DRM_TEGRA_GR2D, &gr2d->channel);
  38. if (err < 0) {
  39. free(gr2d);
  40. return err;
  41. }
  42. *gr2dp = gr2d;
  43. return 0;
  44. }
  45. int drm_tegra_gr2d_close(struct drm_tegra_gr2d *gr2d)
  46. {
  47. if (!gr2d)
  48. return -EINVAL;
  49. drm_tegra_channel_close(gr2d->channel);
  50. free(gr2d);
  51. return 0;
  52. }
  53. int drm_tegra_gr2d_fill(struct drm_tegra_gr2d *gr2d, struct drm_framebuffer *fb,
  54. unsigned int x, unsigned int y, unsigned int width,
  55. unsigned int height, uint32_t color)
  56. {
  57. struct drm_tegra_bo *fbo = fb->data;
  58. struct drm_tegra_pushbuf *pushbuf;
  59. struct drm_tegra_mapping *map;
  60. struct drm_tegra_job *job;
  61. uint32_t *ptr;
  62. int err;
  63. err = drm_tegra_job_new(gr2d->channel, &job);
  64. if (err < 0)
  65. return err;
  66. err = drm_tegra_channel_map(gr2d->channel, fbo, 0, &map);
  67. if (err < 0)
  68. return err;
  69. err = drm_tegra_job_get_pushbuf(job, &pushbuf);
  70. if (err < 0)
  71. return err;
  72. err = drm_tegra_pushbuf_begin(pushbuf, 32, &ptr);
  73. if (err < 0)
  74. return err;
  75. *ptr++ = HOST1X_OPCODE_SETCL(0, HOST1X_CLASS_GR2D, 0);
  76. *ptr++ = HOST1X_OPCODE_MASK(0x9, 0x9);
  77. *ptr++ = 0x0000003a;
  78. *ptr++ = 0x00000000;
  79. *ptr++ = HOST1X_OPCODE_MASK(0x1e, 0x7);
  80. *ptr++ = 0x00000000;
  81. *ptr++ = (2 << 16) | (1 << 6) | (1 << 2);
  82. *ptr++ = 0x000000cc;
  83. *ptr++ = HOST1X_OPCODE_MASK(0x2b, 0x9);
  84. /* relocate destination buffer */
  85. err = drm_tegra_pushbuf_relocate(pushbuf, &ptr, map, 0, 0, 0);
  86. if (err < 0) {
  87. fprintf(stderr, "failed to relocate buffer object: %d\n", err);
  88. return err;
  89. }
  90. *ptr++ = fb->pitch;
  91. *ptr++ = HOST1X_OPCODE_NONINCR(0x35, 1);
  92. *ptr++ = color;
  93. *ptr++ = HOST1X_OPCODE_NONINCR(0x46, 1);
  94. *ptr++ = 0x00000000;
  95. *ptr++ = HOST1X_OPCODE_MASK(0x38, 0x5);
  96. *ptr++ = height << 16 | width;
  97. *ptr++ = y << 16 | x;
  98. err = drm_tegra_pushbuf_end(pushbuf, ptr);
  99. if (err < 0) {
  100. fprintf(stderr, "failed to update push buffer: %d\n", -err);
  101. return err;
  102. }
  103. err = drm_tegra_job_submit(job, NULL);
  104. if (err < 0) {
  105. fprintf(stderr, "failed to submit job: %d\n", err);
  106. return err;
  107. }
  108. err = drm_tegra_job_wait(job, 0);
  109. if (err < 0) {
  110. fprintf(stderr, "failed to wait for fence: %d\n", err);
  111. return err;
  112. }
  113. drm_tegra_channel_unmap(map);
  114. drm_tegra_job_free(job);
  115. return 0;
  116. }