qxl_image.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 2013 Red Hat Inc.
  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. * Authors: Dave Airlie
  23. * Alon Levy
  24. */
  25. #include <linux/gfp.h>
  26. #include <linux/slab.h>
  27. #include "qxl_drv.h"
  28. #include "qxl_object.h"
  29. static int
  30. qxl_image_create_helper(struct qxl_device *qdev,
  31. struct qxl_release *release,
  32. struct qxl_bo **image_bo,
  33. const uint8_t *data,
  34. int width, int height,
  35. int depth, unsigned int hash,
  36. int stride)
  37. {
  38. struct qxl_image *image;
  39. struct qxl_data_chunk *chunk;
  40. int i;
  41. int chunk_stride;
  42. int linesize = width * depth / 8;
  43. struct qxl_bo *chunk_bo;
  44. int ret;
  45. void *ptr;
  46. /* Chunk */
  47. /* FIXME: Check integer overflow */
  48. /* TODO: variable number of chunks */
  49. chunk_stride = stride; /* TODO: should use linesize, but it renders
  50. wrong (check the bitmaps are sent correctly
  51. first) */
  52. ret = qxl_alloc_bo_reserved(qdev, sizeof(*chunk) + height * chunk_stride,
  53. &chunk_bo);
  54. ptr = qxl_bo_kmap_atomic_page(qdev, chunk_bo, 0);
  55. chunk = ptr;
  56. chunk->data_size = height * chunk_stride;
  57. chunk->prev_chunk = 0;
  58. chunk->next_chunk = 0;
  59. qxl_bo_kunmap_atomic_page(qdev, chunk_bo, ptr);
  60. {
  61. void *k_data, *i_data;
  62. int remain;
  63. int page;
  64. int size;
  65. if (stride == linesize && chunk_stride == stride) {
  66. remain = linesize * height;
  67. page = 0;
  68. i_data = (void *)data;
  69. while (remain > 0) {
  70. ptr = qxl_bo_kmap_atomic_page(qdev, chunk_bo, page << PAGE_SHIFT);
  71. if (page == 0) {
  72. chunk = ptr;
  73. k_data = chunk->data;
  74. size = PAGE_SIZE - offsetof(struct qxl_data_chunk, data);
  75. } else {
  76. k_data = ptr;
  77. size = PAGE_SIZE;
  78. }
  79. size = min(size, remain);
  80. memcpy(k_data, i_data, size);
  81. qxl_bo_kunmap_atomic_page(qdev, chunk_bo, ptr);
  82. i_data += size;
  83. remain -= size;
  84. page++;
  85. }
  86. } else {
  87. unsigned page_base, page_offset, out_offset;
  88. for (i = 0 ; i < height ; ++i) {
  89. i_data = (void *)data + i * stride;
  90. remain = linesize;
  91. out_offset = offsetof(struct qxl_data_chunk, data) + i * chunk_stride;
  92. while (remain > 0) {
  93. page_base = out_offset & PAGE_MASK;
  94. page_offset = offset_in_page(out_offset);
  95. size = min((int)(PAGE_SIZE - page_offset), remain);
  96. ptr = qxl_bo_kmap_atomic_page(qdev, chunk_bo, page_base);
  97. k_data = ptr + page_offset;
  98. memcpy(k_data, i_data, size);
  99. qxl_bo_kunmap_atomic_page(qdev, chunk_bo, ptr);
  100. remain -= size;
  101. i_data += size;
  102. out_offset += size;
  103. }
  104. }
  105. }
  106. }
  107. qxl_bo_kunmap(chunk_bo);
  108. /* Image */
  109. ret = qxl_alloc_bo_reserved(qdev, sizeof(*image), image_bo);
  110. ptr = qxl_bo_kmap_atomic_page(qdev, *image_bo, 0);
  111. image = ptr;
  112. image->descriptor.id = 0;
  113. image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
  114. image->descriptor.flags = 0;
  115. image->descriptor.width = width;
  116. image->descriptor.height = height;
  117. switch (depth) {
  118. case 1:
  119. /* TODO: BE? check by arch? */
  120. image->u.bitmap.format = SPICE_BITMAP_FMT_1BIT_BE;
  121. break;
  122. case 24:
  123. image->u.bitmap.format = SPICE_BITMAP_FMT_24BIT;
  124. break;
  125. case 32:
  126. image->u.bitmap.format = SPICE_BITMAP_FMT_32BIT;
  127. break;
  128. default:
  129. DRM_ERROR("unsupported image bit depth\n");
  130. return -EINVAL; /* TODO: cleanup */
  131. }
  132. image->u.bitmap.flags = QXL_BITMAP_TOP_DOWN;
  133. image->u.bitmap.x = width;
  134. image->u.bitmap.y = height;
  135. image->u.bitmap.stride = chunk_stride;
  136. image->u.bitmap.palette = 0;
  137. image->u.bitmap.data = qxl_bo_physical_address(qdev, chunk_bo, 0);
  138. qxl_release_add_res(qdev, release, chunk_bo);
  139. qxl_bo_unreserve(chunk_bo);
  140. qxl_bo_unref(&chunk_bo);
  141. qxl_bo_kunmap_atomic_page(qdev, *image_bo, ptr);
  142. return 0;
  143. }
  144. int qxl_image_create(struct qxl_device *qdev,
  145. struct qxl_release *release,
  146. struct qxl_bo **image_bo,
  147. const uint8_t *data,
  148. int x, int y, int width, int height,
  149. int depth, int stride)
  150. {
  151. data += y * stride + x * (depth / 8);
  152. return qxl_image_create_helper(qdev, release, image_bo, data,
  153. width, height, depth, 0, stride);
  154. }