Mirror of Awesome WM window manager
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

draw.c 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * draw.c - draw functions
  3. *
  4. * Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. */
  21. #include "config.h"
  22. #include "draw.h"
  23. #include "globalconf.h"
  24. #include <langinfo.h>
  25. #include <errno.h>
  26. #include <ctype.h>
  27. #include <math.h>
  28. #include <gdk-pixbuf/gdk-pixbuf.h>
  29. #include <cairo-xcb.h>
  30. #include <lauxlib.h>
  31. static cairo_user_data_key_t data_key;
  32. static inline void
  33. free_data(void *data)
  34. {
  35. p_delete(&data);
  36. }
  37. /** Create a surface object from this image data.
  38. * \param width The width of the image.
  39. * \param height The height of the image
  40. * \param data The image's data in ARGB format, will be copied by this function.
  41. * \return Number of items pushed on the lua stack.
  42. */
  43. cairo_surface_t *
  44. draw_surface_from_data(int width, int height, uint32_t *data)
  45. {
  46. unsigned long int len = width * height;
  47. unsigned long int i;
  48. uint32_t *buffer = p_new(uint32_t, len);
  49. cairo_surface_t *surface;
  50. /* Cairo wants premultiplied alpha, meh :( */
  51. for(i = 0; i < len; i++)
  52. {
  53. uint8_t a = (data[i] >> 24) & 0xff;
  54. double alpha = a / 255.0;
  55. uint8_t r = ((data[i] >> 16) & 0xff) * alpha;
  56. uint8_t g = ((data[i] >> 8) & 0xff) * alpha;
  57. uint8_t b = ((data[i] >> 0) & 0xff) * alpha;
  58. buffer[i] = (a << 24) | (r << 16) | (g << 8) | b;
  59. }
  60. surface =
  61. cairo_image_surface_create_for_data((unsigned char *) buffer,
  62. CAIRO_FORMAT_ARGB32,
  63. width,
  64. height,
  65. width*4);
  66. /* This makes sure that buffer will be freed */
  67. cairo_surface_set_user_data(surface, &data_key, buffer, &free_data);
  68. return surface;
  69. }
  70. /** Create a surface object from this pixbuf
  71. * \param buf The pixbuf
  72. * \return Number of items pushed on the lua stack.
  73. */
  74. cairo_surface_t *
  75. draw_surface_from_pixbuf(GdkPixbuf *buf)
  76. {
  77. int width = gdk_pixbuf_get_width(buf);
  78. int height = gdk_pixbuf_get_height(buf);
  79. int pix_stride = gdk_pixbuf_get_rowstride(buf);
  80. guchar *pixels = gdk_pixbuf_get_pixels(buf);
  81. int channels = gdk_pixbuf_get_n_channels(buf);
  82. cairo_surface_t *surface;
  83. int cairo_stride;
  84. unsigned char *cairo_pixels;
  85. cairo_format_t format = CAIRO_FORMAT_ARGB32;
  86. if (channels == 3)
  87. format = CAIRO_FORMAT_RGB24;
  88. surface = cairo_image_surface_create(format, width, height);
  89. cairo_surface_flush(surface);
  90. cairo_stride = cairo_image_surface_get_stride(surface);
  91. cairo_pixels = cairo_image_surface_get_data(surface);
  92. for (int y = 0; y < height; y++)
  93. {
  94. guchar *row = pixels;
  95. uint32_t *cairo = (uint32_t *) cairo_pixels;
  96. for (int x = 0; x < width; x++) {
  97. if (channels == 3)
  98. {
  99. uint8_t r = *row++;
  100. uint8_t g = *row++;
  101. uint8_t b = *row++;
  102. *cairo++ = (r << 16) | (g << 8) | b;
  103. } else {
  104. uint8_t r = *row++;
  105. uint8_t g = *row++;
  106. uint8_t b = *row++;
  107. uint8_t a = *row++;
  108. double alpha = a / 255.0;
  109. r = r * alpha;
  110. g = g * alpha;
  111. b = b * alpha;
  112. *cairo++ = (a << 24) | (r << 16) | (g << 8) | b;
  113. }
  114. }
  115. pixels += pix_stride;
  116. cairo_pixels += cairo_stride;
  117. }
  118. cairo_surface_mark_dirty(surface);
  119. return surface;
  120. }
  121. static void
  122. get_surface_size(cairo_surface_t *surface, int *width, int *height)
  123. {
  124. double x1, y1, x2, y2;
  125. cairo_t *cr = cairo_create(surface);
  126. cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
  127. cairo_destroy(cr);
  128. *width = x2 - x1;
  129. *height = y2 - y1;
  130. }
  131. /** Duplicate the specified image surface.
  132. * \param surface The surface to copy
  133. * \return A pointer to a new cairo image surface.
  134. */
  135. cairo_surface_t *
  136. draw_dup_image_surface(cairo_surface_t *surface)
  137. {
  138. cairo_surface_t *res;
  139. int width, height;
  140. get_surface_size(surface, &width, &height);
  141. #if CAIRO_VERSION_MAJOR == 1 && CAIRO_VERSION_MINOR > 12
  142. res = cairo_surface_create_similar_image(surface, CAIRO_FORMAT_ARGB32, width, height);
  143. #else
  144. res = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
  145. #endif
  146. cairo_t *cr = cairo_create(res);
  147. cairo_set_source_surface(cr, surface, 0, 0);
  148. cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
  149. cairo_paint(cr);
  150. cairo_destroy(cr);
  151. return res;
  152. }
  153. /** Load the specified path into a cairo surface
  154. * \param L Lua state
  155. * \param path file to load
  156. * \param error A place to store an error message, if needed
  157. * \return A cairo image surface or NULL on error.
  158. */
  159. cairo_surface_t *
  160. draw_load_image(lua_State *L, const char *path, GError **error)
  161. {
  162. cairo_surface_t *ret;
  163. GdkPixbuf *buf = gdk_pixbuf_new_from_file(path, error);
  164. if (!buf)
  165. /* error was set above */
  166. return NULL;
  167. ret = draw_surface_from_pixbuf(buf);
  168. g_object_unref(buf);
  169. return ret;
  170. }
  171. xcb_visualtype_t *draw_find_visual(const xcb_screen_t *s, xcb_visualid_t visual)
  172. {
  173. xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(s);
  174. if(depth_iter.data)
  175. for(; depth_iter.rem; xcb_depth_next (&depth_iter))
  176. for(xcb_visualtype_iterator_t visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
  177. visual_iter.rem; xcb_visualtype_next (&visual_iter))
  178. if(visual == visual_iter.data->visual_id)
  179. return visual_iter.data;
  180. return NULL;
  181. }
  182. xcb_visualtype_t *draw_default_visual(const xcb_screen_t *s)
  183. {
  184. return draw_find_visual(s, s->root_visual);
  185. }
  186. xcb_visualtype_t *draw_argb_visual(const xcb_screen_t *s)
  187. {
  188. xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(s);
  189. if(depth_iter.data)
  190. for(; depth_iter.rem; xcb_depth_next (&depth_iter))
  191. if(depth_iter.data->depth == 32)
  192. for(xcb_visualtype_iterator_t visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
  193. visual_iter.rem; xcb_visualtype_next (&visual_iter))
  194. return visual_iter.data;
  195. return NULL;
  196. }
  197. uint8_t draw_visual_depth(const xcb_screen_t *s, xcb_visualid_t vis)
  198. {
  199. xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(s);
  200. if(depth_iter.data)
  201. for(; depth_iter.rem; xcb_depth_next (&depth_iter))
  202. for(xcb_visualtype_iterator_t visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
  203. visual_iter.rem; xcb_visualtype_next (&visual_iter))
  204. if(vis == visual_iter.data->visual_id)
  205. return depth_iter.data->depth;
  206. fatal("Could not find a visual's depth");
  207. }
  208. void draw_test_cairo_xcb(void)
  209. {
  210. xcb_pixmap_t pixmap = xcb_generate_id(globalconf.connection);
  211. xcb_create_pixmap(globalconf.connection, globalconf.default_depth, pixmap,
  212. globalconf.screen->root, 1, 1);
  213. cairo_surface_t *surface = cairo_xcb_surface_create(globalconf.connection,
  214. pixmap, globalconf.visual, 1, 1);
  215. if(cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS)
  216. fatal("Could not set up display: got cairo surface with status %s",
  217. cairo_status_to_string(cairo_surface_status(surface)));
  218. cairo_surface_finish(surface);
  219. cairo_surface_destroy(surface);
  220. xcb_free_pixmap(globalconf.connection, pixmap);
  221. }
  222. // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80