2
0

bo.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright 2016 Rockchip Electronics S.LSI Co. LTD
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "bo.h"
  17. void fill_bo(struct sp_bo* bo, uint8_t a, uint8_t r, uint8_t g, uint8_t b)
  18. {
  19. draw_rect(bo, 0, 0, bo->width, bo->height, a, r, g, b);
  20. }
  21. void draw_rect(struct sp_bo* bo, uint32_t x, uint32_t y, uint32_t width,
  22. uint32_t height, uint8_t a, uint8_t r, uint8_t g, uint8_t b)
  23. {
  24. uint32_t i, j, xmax = x + width, ymax = y + height;
  25. if (xmax > bo->width)
  26. xmax = bo->width;
  27. if (ymax > bo->height)
  28. ymax = bo->height;
  29. for (i = y; i < ymax; i++) {
  30. uint8_t* row = (uint8_t*)bo->map_addr + i * bo->pitch;
  31. for (j = x; j < xmax; j++) {
  32. uint8_t* pixel = row + j * 4;
  33. if (bo->format == DRM_FORMAT_ARGB8888 || bo->format == DRM_FORMAT_XRGB8888) {
  34. pixel[0] = b;
  35. pixel[1] = g;
  36. pixel[2] = r;
  37. pixel[3] = a;
  38. } else if (bo->format == DRM_FORMAT_RGBA8888) {
  39. pixel[0] = r;
  40. pixel[1] = g;
  41. pixel[2] = b;
  42. pixel[3] = a;
  43. }
  44. }
  45. }
  46. }
  47. int add_fb_sp_bo(struct sp_bo* bo, uint32_t format)
  48. {
  49. int ret;
  50. uint32_t handles[4], pitches[4], offsets[4];
  51. handles[0] = bo->handle;
  52. pitches[0] = bo->pitch;
  53. offsets[0] = 0;
  54. if(format == DRM_FORMAT_NV12 || format == DRM_FORMAT_NV16) {
  55. handles[1] = bo->handle;
  56. pitches[0] = bo->width;
  57. pitches[1] = bo->width;
  58. offsets[1] = bo->width * bo->height;
  59. }
  60. ret = drmModeAddFB2(bo->dev->fd, bo->width, bo->height,
  61. format, handles, pitches, offsets,
  62. &bo->fb_id, bo->flags);
  63. if (ret) {
  64. printf("failed to create fb ret=%d\n", ret);
  65. return ret;
  66. }
  67. return 0;
  68. }
  69. static int map_sp_bo(struct sp_bo* bo)
  70. {
  71. int ret;
  72. struct drm_mode_map_dumb md;
  73. if (bo->map_addr)
  74. return 0;
  75. md.handle = bo->handle;
  76. ret = drmIoctl(bo->dev->fd, DRM_IOCTL_MODE_MAP_DUMB, &md);
  77. if (ret) {
  78. printf("failed to map sp_bo ret=%d\n", ret);
  79. return ret;
  80. }
  81. bo->map_addr = mmap(NULL, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
  82. bo->dev->fd, md.offset);
  83. if (bo->map_addr == MAP_FAILED) {
  84. printf("failed to map bo ret=%d\n", -errno);
  85. return -errno;
  86. }
  87. return 0;
  88. }
  89. struct sp_bo* create_sp_bo(struct sp_dev* dev, uint32_t width, uint32_t height,
  90. uint32_t depth, uint32_t bpp, uint32_t format, uint32_t flags)
  91. {
  92. int ret;
  93. struct drm_mode_create_dumb cd;
  94. struct sp_bo* bo;
  95. memset(&cd, 0, sizeof(cd));
  96. bo = (struct sp_bo *) calloc(1, sizeof(*bo));
  97. if (!bo)
  98. return NULL;
  99. cd.height = height;
  100. cd.width = width;
  101. cd.bpp = bpp;
  102. cd.flags = flags;
  103. ret = drmIoctl(dev->fd, DRM_IOCTL_MODE_CREATE_DUMB, &cd);
  104. if (ret) {
  105. printf("failed to create sp_bo %d\n", ret);
  106. goto err;
  107. }
  108. bo->dev = dev;
  109. bo->width = width;
  110. bo->height = height;
  111. bo->depth = depth;
  112. bo->bpp = bpp;
  113. bo->format = format;
  114. bo->flags = flags;
  115. bo->handle = cd.handle;
  116. bo->pitch = cd.pitch;
  117. bo->size = cd.size;
  118. ret = add_fb_sp_bo(bo, format);
  119. if (ret) {
  120. printf("failed to add fb ret=%d\n", ret);
  121. goto err;
  122. }
  123. ret = map_sp_bo(bo);
  124. if (ret) {
  125. printf("failed to map bo ret=%d\n", ret);
  126. goto err;
  127. }
  128. return bo;
  129. err:
  130. free_sp_bo(bo);
  131. return NULL;
  132. }
  133. void free_sp_bo(struct sp_bo* bo)
  134. {
  135. int ret;
  136. struct drm_mode_destroy_dumb dd;
  137. if (!bo)
  138. return;
  139. if (bo->map_addr)
  140. munmap(bo->map_addr, bo->size);
  141. if (bo->fb_id) {
  142. ret = drmModeRmFB(bo->dev->fd, bo->fb_id);
  143. if (ret)
  144. printf("Failed to rmfb ret=%d!\n", ret);
  145. }
  146. if (bo->handle) {
  147. dd.handle = bo->handle;
  148. ret = drmIoctl(bo->dev->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &dd);
  149. if (ret)
  150. printf("Failed to destroy buffer ret=%d\n", ret);
  151. }
  152. free(bo);
  153. }