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 "CairoUtilitiesEfl.h"
23
24#include "RefPtrCairo.h"
25
26namespace WebCore {
27
28EflUniquePtr<Evas_Object> evasObjectFromCairoImageSurface(Evas* canvas, cairo_surface_t* surface)
29{
30    EINA_SAFETY_ON_NULL_RETURN_VAL(canvas, 0);
31    EINA_SAFETY_ON_NULL_RETURN_VAL(surface, 0);
32
33    cairo_status_t status = cairo_surface_status(surface);
34    if (status != CAIRO_STATUS_SUCCESS) {
35        EINA_LOG_ERR("cairo surface is invalid: %s", cairo_status_to_string(status));
36        return nullptr;
37    }
38
39    cairo_surface_type_t type = cairo_surface_get_type(surface);
40    if (type != CAIRO_SURFACE_TYPE_IMAGE) {
41        EINA_LOG_ERR("unknown surface type %d, required %d (CAIRO_SURFACE_TYPE_IMAGE).",
42            type, CAIRO_SURFACE_TYPE_IMAGE);
43        return nullptr;
44    }
45
46    cairo_format_t format = cairo_image_surface_get_format(surface);
47    if (format != CAIRO_FORMAT_ARGB32 && format != CAIRO_FORMAT_RGB24) {
48        EINA_LOG_ERR("unknown surface format %d, expected %d or %d.",
49            format, CAIRO_FORMAT_ARGB32, CAIRO_FORMAT_RGB24);
50        return nullptr;
51    }
52
53    int width = cairo_image_surface_get_width(surface);
54    int height = cairo_image_surface_get_height(surface);
55    int stride = cairo_image_surface_get_stride(surface);
56    if (width <= 0 || height <= 0 || stride <= 0) {
57        EINA_LOG_ERR("invalid image size %dx%d, stride=%d", width, height, stride);
58        return nullptr;
59    }
60
61    void* data = cairo_image_surface_get_data(surface);
62    if (!data) {
63        EINA_LOG_ERR("could not get source data.");
64        return nullptr;
65    }
66
67    EflUniquePtr<Evas_Object> image = EflUniquePtr<Evas_Object>(evas_object_image_filled_add(canvas));
68    if (!image) {
69        EINA_LOG_ERR("could not add image to canvas.");
70        return nullptr;
71    }
72
73    evas_object_image_colorspace_set(image.get(), EVAS_COLORSPACE_ARGB8888);
74    evas_object_image_size_set(image.get(), width, height);
75    evas_object_image_alpha_set(image.get(), format == CAIRO_FORMAT_ARGB32);
76
77    if (evas_object_image_stride_get(image.get()) != stride) {
78        EINA_LOG_ERR("evas' stride %d diverges from cairo's %d.",
79            evas_object_image_stride_get(image.get()), stride);
80        return nullptr;
81    }
82
83    evas_object_image_data_copy_set(image.get(), data);
84
85    return WTF::move(image);
86}
87
88PassRefPtr<cairo_surface_t> createSurfaceForBackingStore(Ecore_Evas* ee)
89{
90    ASSERT(ee);
91
92    int width;
93    int height;
94    ecore_evas_geometry_get(ee, 0, 0, &width, &height);
95    ASSERT(width > 0 && height > 0);
96
97    unsigned char* buffer = static_cast<unsigned char*>(const_cast<void*>(ecore_evas_buffer_pixels_get(ee)));
98    RefPtr<cairo_surface_t> surface = adoptRef(cairo_image_surface_create_for_data(buffer, CAIRO_FORMAT_ARGB32, width, height, width * 4));
99
100    cairo_status_t status = cairo_surface_status(surface.get());
101    if (status != CAIRO_STATUS_SUCCESS) {
102        EINA_LOG_ERR("Could not create cairo surface: %s", cairo_status_to_string(status));
103        return 0;
104    }
105
106    return surface.release();
107}
108
109PassRefPtr<cairo_surface_t> createSurfaceForImage(Evas_Object* image)
110{
111    ASSERT(image);
112
113    Evas_Coord width;
114    Evas_Coord height;
115    evas_object_image_size_get(image, &width, &height);
116    ASSERT(width > 0 && height > 0);
117
118    unsigned char* buffer = static_cast<unsigned char*>(const_cast<void*>(evas_object_image_data_get(image, true)));
119    RefPtr<cairo_surface_t> surface = adoptRef(cairo_image_surface_create_for_data(buffer, CAIRO_FORMAT_ARGB32, width, height, width * 4));
120
121    cairo_status_t status = cairo_surface_status(surface.get());
122    if (status != CAIRO_STATUS_SUCCESS) {
123        EINA_LOG_ERR("Could not create cairo surface: %s", cairo_status_to_string(status));
124        return 0;
125    }
126
127    return surface.release();
128}
129
130}
131