1/*
2 * Copyright (C) 2011, Igalia S.L.
3 * Copyright (C) 2011 Samsung Electronics
4 *
5 *  This library is free software; you can redistribute it and/or
6 *  modify it under the terms of the GNU Lesser 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 *  Lesser General Public License for more details.
14 *
15 *  You should have received a copy of the GNU Lesser General Public
16 *  License along with this library; if not, write to the Free Software
17 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20#include "config.h"
21#include "WidgetBackingStoreCairo.h"
22
23#include "CairoUtilities.h"
24#include "RefPtrCairo.h"
25#include <cairo.h>
26
27namespace WebCore {
28
29static PassRefPtr<cairo_surface_t> createSurfaceForBackingStore(PlatformWidget widget, const IntSize& size)
30{
31#if PLATFORM(GTK)
32    return adoptRef(gdk_window_create_similar_surface(gtk_widget_get_window(widget), CAIRO_CONTENT_COLOR_ALPHA, size.width(), size.height()));
33#else
34    UNUSED_PARAM(widget);
35    return adoptRef(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size.width(), size.height()));
36#endif
37}
38
39PassOwnPtr<WidgetBackingStore> WidgetBackingStoreCairo::create(PlatformWidget widget, const IntSize& size)
40{
41    return adoptPtr(new WidgetBackingStoreCairo(widget, size));
42}
43
44// We keep two copies of the surface here, which will double the memory usage, but increase
45// scrolling performance since we do not have to keep reallocating a memory region during
46// quick scrolling requests.
47WidgetBackingStoreCairo::WidgetBackingStoreCairo(PlatformWidget widget, const IntSize& size)
48    : WidgetBackingStore(size)
49    , m_surface(createSurfaceForBackingStore(widget, size))
50    , m_scrollSurface(createSurfaceForBackingStore(widget, size))
51
52{
53}
54
55WidgetBackingStoreCairo::~WidgetBackingStoreCairo()
56{
57}
58
59cairo_surface_t* WidgetBackingStoreCairo::cairoSurface()
60{
61    return m_surface.get();
62}
63
64void WidgetBackingStoreCairo::scroll(const IntRect& scrollRect, const IntSize& scrollOffset)
65{
66    IntRect targetRect(scrollRect);
67    targetRect.move(scrollOffset);
68    targetRect.shiftMaxXEdgeTo(targetRect.maxX() - scrollOffset.width());
69    targetRect.shiftMaxYEdgeTo(targetRect.maxY() - scrollOffset.height());
70    if (targetRect.isEmpty())
71        return;
72
73    copyRectFromOneSurfaceToAnother(m_surface.get(), m_scrollSurface.get(),
74                                    scrollOffset, targetRect);
75    copyRectFromOneSurfaceToAnother(m_scrollSurface.get(), m_surface.get(),
76                                    IntSize(), targetRect);
77}
78
79} // namespace WebCore
80