Create string tightly when retrive string from cbhm callback event
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / API / efl / ewk_util.cpp
1 /*
2     Copyright (C) 2009-2010 ProFUSION embedded systems
3     Copyright (C) 2009-2010 Samsung Electronics
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Library General Public
7     License as published by the Free Software Foundation; either
8     version 2 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     Library General Public License for more details.
14
15     You should have received a copy of the GNU Library General Public License
16     along with this library; see the file COPYING.LIB.  If not, write to
17     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18     Boston, MA 02110-1301, USA.
19 */
20
21 #include "config.h"
22 #include "ewk_util.h"
23
24 #include <Evas.h>
25 #include <cairo.h>
26 #include <eina_safety_checks.h>
27
28 /**
29  * Converts an image from cairo_surface to the Evas_Object.
30  *
31  * @param canvas display canvas
32  * @param surface cairo representation of an image
33  * @return converted cairo_surface object to the Evas_Object
34  */
35 Evas_Object* ewk_util_image_from_cairo_surface_add(Evas* canvas, cairo_surface_t* surface)
36 {
37 #if USE(CAIRO)
38     cairo_status_t status;
39     cairo_surface_type_t type;
40     cairo_format_t format;
41     int w, h, stride;
42     Evas_Object* image;
43     const void* src;
44     void* dst;
45
46     EINA_SAFETY_ON_NULL_RETURN_VAL(canvas, 0);
47     EINA_SAFETY_ON_NULL_RETURN_VAL(surface, 0);
48
49     status = cairo_surface_status(surface);
50     if (status != CAIRO_STATUS_SUCCESS)
51         return 0;
52
53     type = cairo_surface_get_type(surface);
54     if (type != CAIRO_SURFACE_TYPE_IMAGE)
55         return 0;
56
57     format = cairo_image_surface_get_format(surface);
58     if (format != CAIRO_FORMAT_ARGB32 && format != CAIRO_FORMAT_RGB24)
59         return 0;
60
61     w = cairo_image_surface_get_width(surface);
62     h = cairo_image_surface_get_height(surface);
63     stride = cairo_image_surface_get_stride(surface);
64     if (w <= 0 || h <= 0 || stride <= 0)
65         return 0;
66
67     src = cairo_image_surface_get_data(surface);
68     if (!src)
69         return 0;
70
71     image = evas_object_image_filled_add(canvas);
72     if (!image)
73         return 0;
74
75     evas_object_image_colorspace_set(image, EVAS_COLORSPACE_ARGB8888);
76     evas_object_image_size_set(image, w, h);
77     evas_object_image_alpha_set(image, format == CAIRO_FORMAT_ARGB32);
78
79     if (evas_object_image_stride_get(image) != stride) {
80         evas_object_del(image);
81         return 0;
82     }
83
84     dst = evas_object_image_data_get(image, EINA_TRUE);
85     memcpy(dst, src, h * stride);
86     evas_object_image_data_set(image, dst);
87
88     evas_object_resize(image, w, h); // helpful but not really required
89
90     return image;
91 #else
92     return 0;
93 #endif
94 }
95