qxl_image.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_allocate_chunk(struct qxl_device *qdev,
  31. struct qxl_release *release,
  32. struct qxl_drm_image *image,
  33. unsigned int chunk_size)
  34. {
  35. struct qxl_drm_chunk *chunk;
  36. int ret;
  37. chunk = kmalloc(sizeof(struct qxl_drm_chunk), GFP_KERNEL);
  38. if (!chunk)
  39. return -ENOMEM;
  40. ret = qxl_alloc_bo_reserved(qdev, release, chunk_size, &chunk->bo);
  41. if (ret) {
  42. kfree(chunk);
  43. return ret;
  44. }
  45. list_add_tail(&chunk->head, &image->chunk_list);
  46. return 0;
  47. }
  48. int
  49. qxl_image_alloc_objects(struct qxl_device *qdev,
  50. struct qxl_release *release,
  51. struct qxl_drm_image **image_ptr,
  52. int height, int stride)
  53. {
  54. struct qxl_drm_image *image;
  55. int ret;
  56. image = kmalloc(sizeof(struct qxl_drm_image), GFP_KERNEL);
  57. if (!image)
  58. return -ENOMEM;
  59. INIT_LIST_HEAD(&image->chunk_list);
  60. ret = qxl_alloc_bo_reserved(qdev, release, sizeof(struct qxl_image), &image->bo);
  61. if (ret) {
  62. kfree(image);
  63. return ret;
  64. }
  65. ret = qxl_allocate_chunk(qdev, release, image, sizeof(struct qxl_data_chunk) + stride * height);
  66. if (ret) {
  67. qxl_bo_unref(&image->bo);
  68. kfree(image);
  69. return ret;
  70. }
  71. *image_ptr = image;
  72. return 0;
  73. }
  74. void qxl_image_free_objects(struct qxl_device *qdev, struct qxl_drm_image *dimage)
  75. {
  76. struct qxl_drm_chunk *chunk, *tmp;
  77. list_for_each_entry_safe(chunk, tmp, &dimage->chunk_list, head) {
  78. qxl_bo_unref(&chunk->bo);
  79. kfree(chunk);
  80. }
  81. qxl_bo_unref(&dimage->bo);
  82. kfree(dimage);
  83. }
  84. static int
  85. qxl_image_init_helper(struct qxl_device *qdev,
  86. struct qxl_release *release,
  87. struct qxl_drm_image *dimage,
  88. const uint8_t *data,
  89. int width, int height,
  90. int depth, unsigned int hash,
  91. int stride)
  92. {
  93. struct qxl_drm_chunk *drv_chunk;
  94. struct qxl_image *image;
  95. struct qxl_data_chunk *chunk;
  96. int i;
  97. int chunk_stride;
  98. int linesize = width * depth / 8;
  99. struct qxl_bo *chunk_bo, *image_bo;
  100. void *ptr;
  101. /* Chunk */
  102. /* FIXME: Check integer overflow */
  103. /* TODO: variable number of chunks */
  104. drv_chunk = list_first_entry(&dimage->chunk_list, struct qxl_drm_chunk, head);
  105. chunk_bo = drv_chunk->bo;
  106. chunk_stride = stride; /* TODO: should use linesize, but it renders
  107. wrong (check the bitmaps are sent correctly
  108. first) */
  109. ptr = qxl_bo_kmap_atomic_page(qdev, chunk_bo, 0);
  110. chunk = ptr;
  111. chunk->data_size = height * chunk_stride;
  112. chunk->prev_chunk = 0;
  113. chunk->next_chunk = 0;
  114. qxl_bo_kunmap_atomic_page(qdev, chunk_bo, ptr);
  115. {
  116. void *k_data, *i_data;
  117. int remain;
  118. int page;
  119. int size;
  120. if (stride == linesize && chunk_stride == stride) {
  121. remain = linesize * height;
  122. page = 0;
  123. i_data = (void *)data;
  124. while (remain > 0) {
  125. ptr = qxl_bo_kmap_atomic_page(qdev, chunk_bo, page << PAGE_SHIFT);
  126. if (page == 0) {
  127. chunk = ptr;
  128. k_data = chunk->data;
  129. size = PAGE_SIZE - offsetof(struct qxl_data_chunk, data);
  130. } else {
  131. k_data = ptr;
  132. size = PAGE_SIZE;
  133. }
  134. size = min(size, remain);
  135. memcpy(k_data, i_data, size);
  136. qxl_bo_kunmap_atomic_page(qdev, chunk_bo, ptr);
  137. i_data += size;
  138. remain -= size;
  139. page++;
  140. }
  141. } else {
  142. unsigned page_base, page_offset, out_offset;
  143. for (i = 0 ; i < height ; ++i) {
  144. i_data = (void *)data + i * stride;
  145. remain = linesize;
  146. out_offset = offsetof(struct qxl_data_chunk, data) + i * chunk_stride;
  147. while (remain > 0) {
  148. page_base = out_offset & PAGE_MASK;
  149. page_offset = offset_in_page(out_offset);
  150. size = min((int)(PAGE_SIZE - page_offset), remain);
  151. ptr = qxl_bo_kmap_atomic_page(qdev, chunk_bo, page_base);
  152. k_data = ptr + page_offset;
  153. memcpy(k_data, i_data, size);
  154. qxl_bo_kunmap_atomic_page(qdev, chunk_bo, ptr);
  155. remain -= size;
  156. i_data += size;
  157. out_offset += size;
  158. }
  159. }
  160. }
  161. }
  162. qxl_bo_kunmap(chunk_bo);
  163. image_bo = dimage->bo;
  164. ptr = qxl_bo_kmap_atomic_page(qdev, image_bo, 0);
  165. image = ptr;
  166. image->descriptor.id = 0;
  167. image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
  168. image->descriptor.flags = 0;
  169. image->descriptor.width = width;
  170. image->descriptor.height = height;
  171. switch (depth) {
  172. case 1:
  173. /* TODO: BE? check by arch? */
  174. image->u.bitmap.format = SPICE_BITMAP_FMT_1BIT_BE;
  175. break;
  176. case 24:
  177. image->u.bitmap.format = SPICE_BITMAP_FMT_24BIT;
  178. break;
  179. case 32:
  180. image->u.bitmap.format = SPICE_BITMAP_FMT_32BIT;
  181. break;
  182. default:
  183. DRM_ERROR("unsupported image bit depth\n");
  184. return -EINVAL; /* TODO: cleanup */
  185. }
  186. image->u.bitmap.flags = QXL_BITMAP_TOP_DOWN;
  187. image->u.bitmap.x = width;
  188. image->u.bitmap.y = height;
  189. image->u.bitmap.stride = chunk_stride;
  190. image->u.bitmap.palette = 0;
  191. image->u.bitmap.data = qxl_bo_physical_address(qdev, chunk_bo, 0);
  192. qxl_bo_kunmap_atomic_page(qdev, image_bo, ptr);
  193. return 0;
  194. }
  195. int qxl_image_init(struct qxl_device *qdev,
  196. struct qxl_release *release,
  197. struct qxl_drm_image *dimage,
  198. const uint8_t *data,
  199. int x, int y, int width, int height,
  200. int depth, int stride)
  201. {
  202. data += y * stride + x * (depth / 8);
  203. return qxl_image_init_helper(qdev, release, dimage, data,
  204. width, height, depth, 0, stride);
  205. }