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, IntSize size, float deviceScaleFactor)
30{
31    size.scale(deviceScaleFactor);
32
33#if PLATFORM(GTK)
34    return adoptRef(gdk_window_create_similar_surface(gtk_widget_get_window(widget), CAIRO_CONTENT_COLOR_ALPHA, size.width(), size.height()));
35#else
36    UNUSED_PARAM(widget);
37    return adoptRef(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size.width(), size.height()));
38#endif
39}
40
41PassOwnPtr<WidgetBackingStore> WidgetBackingStoreCairo::create(PlatformWidget widget, const IntSize& size, float deviceScaleFactor)
42{
43    return adoptPtr(new WidgetBackingStoreCairo(widget, size, deviceScaleFactor));
44}
45
46// We keep two copies of the surface here, which will double the memory usage, but increase
47// scrolling performance since we do not have to keep reallocating a memory region during
48// quick scrolling requests.
49WidgetBackingStoreCairo::WidgetBackingStoreCairo(PlatformWidget widget, const IntSize& size, float deviceScaleFactor)
50    : WidgetBackingStore(size, deviceScaleFactor)
51    , m_surface(createSurfaceForBackingStore(widget, size, deviceScaleFactor))
52    , m_scrollSurface(createSurfaceForBackingStore(widget, size, deviceScaleFactor))
53{
54    cairoSurfaceSetDeviceScale(m_surface.get(), deviceScaleFactor, deviceScaleFactor);
55}
56
57WidgetBackingStoreCairo::~WidgetBackingStoreCairo()
58{
59}
60
61cairo_surface_t* WidgetBackingStoreCairo::cairoSurface()
62{
63    return m_surface.get();
64}
65
66void WidgetBackingStoreCairo::scroll(const IntRect& scrollRect, const IntSize& scrollOffset)
67{
68    IntRect targetRect(scrollRect);
69    targetRect.move(scrollOffset);
70    targetRect.shiftMaxXEdgeTo(targetRect.maxX() - scrollOffset.width());
71    targetRect.shiftMaxYEdgeTo(targetRect.maxY() - scrollOffset.height());
72    if (targetRect.isEmpty())
73        return;
74
75    copyRectFromOneSurfaceToAnother(m_surface.get(), m_scrollSurface.get(),
76                                    scrollOffset, targetRect);
77    copyRectFromOneSurfaceToAnother(m_scrollSurface.get(), m_surface.get(),
78                                    IntSize(), targetRect);
79}
80
81} // namespace WebCore
82