glamor_transfer.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright © 2014 Keith Packard
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #include "glamor_priv.h"
  23. #include "glamor_transfer.h"
  24. /* XXX a kludge for now */
  25. void
  26. glamor_format_for_pixmap(PixmapPtr pixmap, GLenum *format, GLenum *type)
  27. {
  28. switch (pixmap->drawable.depth) {
  29. case 24:
  30. case 32:
  31. *format = GL_BGRA;
  32. *type = GL_UNSIGNED_INT_8_8_8_8_REV;
  33. break;
  34. case 16:
  35. *format = GL_RGB;
  36. *type = GL_UNSIGNED_SHORT_5_6_5;
  37. break;
  38. case 15:
  39. *format = GL_BGRA;
  40. *type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
  41. break;
  42. case 8:
  43. *format = GL_ALPHA;
  44. *type = GL_UNSIGNED_BYTE;
  45. break;
  46. default:
  47. FatalError("Invalid pixmap depth %d\n", pixmap->drawable.depth);
  48. break;
  49. }
  50. }
  51. /*
  52. * Write a region of bits into a pixmap
  53. */
  54. void
  55. glamor_upload_boxes(PixmapPtr pixmap, BoxPtr in_boxes, int in_nbox,
  56. int dx_src, int dy_src,
  57. int dx_dst, int dy_dst,
  58. uint8_t *bits, uint32_t byte_stride)
  59. {
  60. ScreenPtr screen = pixmap->drawable.pScreen;
  61. glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
  62. glamor_pixmap_private *priv = glamor_get_pixmap_private(pixmap);
  63. int box_x, box_y;
  64. int bytes_per_pixel = pixmap->drawable.bitsPerPixel >> 3;
  65. GLenum type;
  66. GLenum format;
  67. glamor_format_for_pixmap(pixmap, &format, &type);
  68. glamor_make_current(glamor_priv);
  69. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  70. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  71. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  72. glPixelStorei(GL_UNPACK_ROW_LENGTH, byte_stride / bytes_per_pixel);
  73. glamor_pixmap_loop(priv, box_x, box_y) {
  74. BoxPtr box = glamor_pixmap_box_at(priv, box_x, box_y);
  75. glamor_pixmap_fbo *fbo = glamor_pixmap_fbo_at(priv, box_x, box_y);
  76. BoxPtr boxes = in_boxes;
  77. int nbox = in_nbox;
  78. glActiveTexture(GL_TEXTURE0);
  79. glBindTexture(GL_TEXTURE_2D, fbo->tex);
  80. while (nbox--) {
  81. /* compute drawable coordinates */
  82. int x1 = boxes->x1 + dx_dst;
  83. int x2 = boxes->x2 + dx_dst;
  84. int y1 = boxes->y1 + dy_dst;
  85. int y2 = boxes->y2 + dy_dst;
  86. boxes++;
  87. if (x1 < box->x1)
  88. x1 = box->x1;
  89. if (box->x2 < x2)
  90. x2 = box->x2;
  91. if (x2 <= x1)
  92. continue;
  93. if (y1 < box->y1)
  94. y1 = box->y1;
  95. if (box->y2 < y2)
  96. y2 = box->y2;
  97. if (y2 <= y1)
  98. continue;
  99. glPixelStorei(GL_UNPACK_SKIP_ROWS, y1 - dy_dst + dy_src);
  100. glPixelStorei(GL_UNPACK_SKIP_PIXELS, x1 - dx_dst + dx_src);
  101. glTexSubImage2D(GL_TEXTURE_2D, 0,
  102. x1 - box->x1, y1 - box->y1,
  103. x2 - x1, y2 - y1,
  104. format, type,
  105. bits);
  106. }
  107. }
  108. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  109. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  110. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  111. }
  112. /*
  113. * Upload a region of data
  114. */
  115. void
  116. glamor_upload_region(PixmapPtr pixmap, RegionPtr region,
  117. int region_x, int region_y,
  118. uint8_t *bits, uint32_t byte_stride)
  119. {
  120. glamor_upload_boxes(pixmap, RegionRects(region), RegionNumRects(region),
  121. -region_x, -region_y,
  122. 0, 0,
  123. bits, byte_stride);
  124. }
  125. /*
  126. * Take the data in the pixmap and stuff it back into the FBO
  127. */
  128. void
  129. glamor_upload_pixmap(PixmapPtr pixmap)
  130. {
  131. BoxRec box;
  132. box.x1 = 0;
  133. box.x2 = pixmap->drawable.width;
  134. box.y1 = 0;
  135. box.y2 = pixmap->drawable.height;
  136. glamor_upload_boxes(pixmap, &box, 1, 0, 0, 0, 0,
  137. pixmap->devPrivate.ptr, pixmap->devKind);
  138. }
  139. /*
  140. * Read stuff from the pixmap FBOs and write to memory
  141. */
  142. void
  143. glamor_download_boxes(PixmapPtr pixmap, BoxPtr in_boxes, int in_nbox,
  144. int dx_src, int dy_src,
  145. int dx_dst, int dy_dst,
  146. uint8_t *bits, uint32_t byte_stride)
  147. {
  148. ScreenPtr screen = pixmap->drawable.pScreen;
  149. glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
  150. glamor_pixmap_private *priv = glamor_get_pixmap_private(pixmap);
  151. int box_x, box_y;
  152. int bytes_per_pixel = pixmap->drawable.bitsPerPixel >> 3;
  153. GLenum type;
  154. GLenum format;
  155. glamor_format_for_pixmap(pixmap, &format, &type);
  156. glamor_make_current(glamor_priv);
  157. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  158. glPixelStorei(GL_PACK_ROW_LENGTH, byte_stride / bytes_per_pixel);
  159. glamor_pixmap_loop(priv, box_x, box_y) {
  160. BoxPtr box = glamor_pixmap_box_at(priv, box_x, box_y);
  161. glamor_pixmap_fbo *fbo = glamor_pixmap_fbo_at(priv, box_x, box_y);
  162. BoxPtr boxes = in_boxes;
  163. int nbox = in_nbox;
  164. glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo->fb);
  165. while (nbox--) {
  166. /* compute drawable coordinates */
  167. int x1 = boxes->x1 + dx_src;
  168. int x2 = boxes->x2 + dx_src;
  169. int y1 = boxes->y1 + dy_src;
  170. int y2 = boxes->y2 + dy_src;
  171. boxes++;
  172. if (x1 < box->x1)
  173. x1 = box->x1;
  174. if (box->x2 < x2)
  175. x2 = box->x2;
  176. if (y1 < box->y1)
  177. y1 = box->y1;
  178. if (box->y2 < y2)
  179. y2 = box->y2;
  180. if (x2 <= x1)
  181. continue;
  182. if (y2 <= y1)
  183. continue;
  184. glPixelStorei(GL_PACK_SKIP_PIXELS, x1 - dx_src + dx_dst);
  185. glPixelStorei(GL_PACK_SKIP_ROWS, y1 - dy_src + dy_dst);
  186. glReadPixels(x1 - box->x1, y1 - box->y1, x2 - x1, y2 - y1, format, type, bits);
  187. }
  188. }
  189. glPixelStorei(GL_PACK_ROW_LENGTH, 0);
  190. glPixelStorei(GL_PACK_SKIP_ROWS, 0);
  191. glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
  192. }
  193. /*
  194. * Read data from the pixmap FBO
  195. */
  196. void
  197. glamor_download_rect(PixmapPtr pixmap, int x, int y, int w, int h, uint8_t *bits)
  198. {
  199. BoxRec box;
  200. box.x1 = x;
  201. box.x2 = x + w;
  202. box.y1 = y;
  203. box.y2 = y + h;
  204. glamor_download_boxes(pixmap, &box, 1, 0, 0, -x, -y,
  205. bits, PixmapBytePad(w, pixmap->drawable.depth));
  206. }
  207. /*
  208. * Pull the data from the FBO down to the pixmap
  209. */
  210. void
  211. glamor_download_pixmap(PixmapPtr pixmap)
  212. {
  213. BoxRec box;
  214. box.x1 = 0;
  215. box.x2 = pixmap->drawable.width;
  216. box.y1 = 0;
  217. box.y2 = pixmap->drawable.height;
  218. glamor_download_boxes(pixmap, &box, 1, 0, 0, 0, 0,
  219. pixmap->devPrivate.ptr, pixmap->devKind);
  220. }