initial import
[vuplus_webkit] / Source / WebKit / efl / ewk / 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 "bindings/js/GCController.h"
25 #include "workers/WorkerThread.h"
26
27 #include "ewk_private.h"
28 #include <eina_safety_checks.h>
29
30 #ifdef HAVE_ECORE_X
31 #include <Ecore_X.h>
32 #endif
33
34 /**
35  * Converts an image from cairo_surface to the Evas_Object.
36  *
37  * @param canvas display canvas
38  * @param surface cairo representation of an image
39  * @return converted cairo_surface object to the Evas_Object
40  */
41 Evas_Object *ewk_util_image_from_cairo_surface_add(Evas *canvas, cairo_surface_t *surface)
42 {
43     cairo_status_t status;
44     cairo_surface_type_t type;
45     cairo_format_t format;
46     int w, h, stride;
47     Evas_Object *image;
48     const void *src;
49     void *dst;
50
51     EINA_SAFETY_ON_NULL_RETURN_VAL(canvas, 0);
52     EINA_SAFETY_ON_NULL_RETURN_VAL(surface, 0);
53
54     status = cairo_surface_status(surface);
55     if (status != CAIRO_STATUS_SUCCESS) {
56         ERR("cairo surface is invalid: %s", cairo_status_to_string(status));
57         return 0;
58     }
59
60     type = cairo_surface_get_type(surface);
61     if (type != CAIRO_SURFACE_TYPE_IMAGE) {
62         ERR("unknown surface type %d, required %d (CAIRO_SURFACE_TYPE_IMAGE).",
63             type, CAIRO_SURFACE_TYPE_IMAGE);
64         return 0;
65     }
66
67     format = cairo_image_surface_get_format(surface);
68     if (format != CAIRO_FORMAT_ARGB32 && format != CAIRO_FORMAT_RGB24) {
69         ERR("unknown surface format %d, expected %d or %d.",
70             format, CAIRO_FORMAT_ARGB32, CAIRO_FORMAT_RGB24);
71         return 0;
72     }
73
74     w = cairo_image_surface_get_width(surface);
75     h = cairo_image_surface_get_height(surface);
76     stride = cairo_image_surface_get_stride(surface);
77     if (w <= 0 || h <= 0 || stride <= 0) {
78         ERR("invalid image size %dx%d, stride=%d", w, h, stride);
79         return 0;
80     }
81
82     src = cairo_image_surface_get_data(surface);
83     if (!src) {
84         ERR("could not get source data.");
85         return 0;
86     }
87
88     image = evas_object_image_filled_add(canvas);
89     if (!image) {
90         ERR("could not add image to canvas.");
91         return 0;
92     }
93
94     evas_object_image_colorspace_set(image, EVAS_COLORSPACE_ARGB8888);
95     evas_object_image_size_set(image, w, h);
96     evas_object_image_alpha_set(image, format == CAIRO_FORMAT_ARGB32);
97
98     if (evas_object_image_stride_get(image) != stride) {
99         ERR("evas' stride %d diverges from cairo's %d.",
100             evas_object_image_stride_get(image), stride);
101         evas_object_del(image);
102         return 0;
103     }
104
105     dst = evas_object_image_data_get(image, EINA_TRUE);
106     memcpy(dst, src, h * stride);
107     evas_object_image_data_set(image, dst);
108
109     evas_object_resize(image, w, h); // helpful but not really required
110
111     return image;
112 }
113
114 /**
115  * @internal
116  *
117  * Performs garbage collection of JavaScript objects.
118  */
119 void ewk_util_javascript_gc_collect()
120 {
121     WebCore::gcController().garbageCollectNow();
122 }
123
124 /**
125  * @internal
126  *
127  * Performs garbage collection of JavaScript objects in a separate thread.
128  *
129  * @param waitUntilDone If @c TRUE, wait the garbage collection thread to finish; if @c FALSE,
130  * return as soon as the thread has been created.
131  */
132 void ewk_util_javascript_gc_alternate_thread_collect(Eina_Bool waitUntilDone)
133 {
134     WebCore::gcController().garbageCollectOnAlternateThreadForDebugging(waitUntilDone);
135 }
136
137 /**
138  * @internal
139  *
140  * Returns the number of current JavaScript objects.
141  */
142 unsigned ewk_util_javascript_gc_object_count_get()
143 {
144     return WebCore::JSDOMWindow::commonJSGlobalData()->heap.objectCount();
145 }
146
147 /**
148  * @internal
149  *
150  * Returns the number of current worked threads.
151  */
152 unsigned ewk_util_worker_thread_count()
153 {
154 #if ENABLE(WORKERS)
155     return WebCore::WorkerThread::workerThreadCount();
156 #else
157     return 0;
158 #endif
159 }
160
161 /**
162  * @internal
163  * Gets dpi value.
164  *
165  * @return device's dpi value.
166  */
167 int ewk_util_dpi_get(void)
168 {
169 #ifdef HAVE_ECORE_X
170      return ecore_x_dpi_get();
171 #else
172      return 160;
173 #endif
174 }