rearrange the directories of git repository
[platform/core/uifw/libds-tizen.git] / examples / pixman-helper.c
1 #include "pixman-helper.h"
2
3 #include <assert.h>
4 #include <drm_fourcc.h>
5
6 static void destroy_pixman_image(pixman_image_t *image, void *data);
7 static uint32_t convert_drm_format_to_pixman(uint32_t fmt);
8
9 pixman_image_t *
10 pixman_image_from_buffer(struct ds_buffer *buffer,
11         enum ds_buffer_data_ptr_access_flag access_flag)
12 {
13     pixman_image_t *image;
14     void *data;
15     uint32_t format;
16     size_t stride;
17     int width, height;
18
19     ds_buffer_get_size(buffer, &width, &height);
20
21     if (!ds_buffer_begin_data_ptr_access(buffer,
22                 access_flag, &data, &format, &stride))
23         return NULL;
24
25     format = convert_drm_format_to_pixman(format);
26     image = pixman_image_create_bits(format, width, height, data, stride);
27     if (!image) {
28         ds_buffer_end_data_ptr_access(buffer);
29         return NULL;
30     }
31
32     pixman_image_set_destroy_function(image,
33             destroy_pixman_image, ds_buffer_lock(buffer));
34
35     return image;
36 }
37
38 void
39 pixman_image_fill_color(pixman_image_t *image, uint8_t r, uint8_t g, uint8_t b)
40 {
41     pixman_image_t *color_image;
42     pixman_color_t color;
43
44     color_rgb888(&color, r, g, b);
45     color_image = pixman_image_create_solid_fill(&color);
46     pixman_image_composite32(PIXMAN_OP_SRC,
47             color_image,
48             NULL,
49             image,
50             0, 0, 0, 0, 0, 0,
51             pixman_image_get_width(image),
52             pixman_image_get_height(image));
53     pixman_image_unref(color_image);
54 }
55
56 pixman_color_t *
57 color_rgb888(pixman_color_t *tmp, uint8_t r, uint8_t g, uint8_t b)
58 {
59     tmp->alpha = 65535;
60     tmp->red = (r << 8) + r;
61     tmp->green = (g << 8) + g;
62     tmp->blue = (b << 8) +b;
63
64     return tmp;
65 }
66
67 static uint32_t
68 convert_drm_format_to_pixman(uint32_t fmt)
69 {
70     switch (fmt) {
71         case DRM_FORMAT_XRGB8888:
72             return PIXMAN_x8r8g8b8;
73         case DRM_FORMAT_ARGB8888:
74             return PIXMAN_a8r8g8b8;
75         default:
76             assert(0 && "not reached");
77     }
78 }
79
80 static void                                             
81 destroy_pixman_image(pixman_image_t *image, void *data)
82 {
83     struct ds_buffer *buffer = data;
84     ds_buffer_end_data_ptr_access(buffer);
85     ds_buffer_unlock(buffer);
86 }