Add tdm backend server and client as an example
[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 static pixman_color_t *color_rgb888(pixman_color_t *tmp,
9         uint8_t r, uint8_t g, uint8_t b);
10
11 pixman_image_t *
12 pixman_image_from_buffer(struct ds_buffer *buffer,
13         enum ds_buffer_data_ptr_access_flag access_flag)
14 {
15     pixman_image_t *image;
16     void *data;
17     uint32_t format;
18     size_t stride;
19     int width, height;
20
21     ds_buffer_get_size(buffer, &width, &height);
22
23     if (!ds_buffer_begin_data_ptr_access(buffer,
24                 access_flag, &data, &format, &stride))
25         return NULL;
26
27     format = convert_drm_format_to_pixman(format);
28     image = pixman_image_create_bits(format, width, height, data, stride);
29     if (!image) {
30         ds_buffer_end_data_ptr_access(buffer);
31         return NULL;
32     }
33
34     pixman_image_set_destroy_function(image,
35             destroy_pixman_image, ds_buffer_lock(buffer));
36
37     return image;
38 }
39
40
41 void
42 pixman_image_fill_color(pixman_image_t *image, uint8_t r, uint8_t g, uint8_t b)
43 {
44     pixman_image_t *color_image;
45     pixman_color_t color;
46
47     color_rgb888(&color, r, g, b);
48     color_image = pixman_image_create_solid_fill(&color);
49     pixman_image_composite32(PIXMAN_OP_SRC,
50             color_image,
51             NULL,
52             image,
53             0, 0, 0, 0, 0, 0,
54             pixman_image_get_width(image),
55             pixman_image_get_height(image));
56     pixman_image_unref(color_image);
57 }
58
59 static pixman_color_t *
60 color_rgb888(pixman_color_t *tmp, uint8_t r, uint8_t g, uint8_t b)
61 {
62     tmp->alpha = 65535;
63     tmp->red = (r << 8) + r;
64     tmp->green = (g << 8) + g;
65     tmp->blue = (b << 8) +b;
66
67     return tmp;
68 }
69
70 static uint32_t
71 convert_drm_format_to_pixman(uint32_t fmt)
72 {
73     switch (fmt) {
74         case DRM_FORMAT_XRGB8888:
75             return PIXMAN_x8r8g8b8;
76         case DRM_FORMAT_ARGB8888:
77             return PIXMAN_a8r8g8b8;
78         default:
79             assert(0 && "not reached");
80     }
81 }
82
83 static void                                             
84 destroy_pixman_image(pixman_image_t *image, void *data)
85 {
86     struct ds_buffer *buffer = data;
87     ds_buffer_end_data_ptr_access(buffer);
88     ds_buffer_unlock(buffer);
89 }