1/*
2 * Copyright (C) 2011, Igalia S.L.
3 *
4 *  This library is free software; you can redistribute it and/or
5 *  modify it under the terms of the GNU Lesser General Public
6 *  License as published by the Free Software Foundation; either
7 *  version 2 of the License, or (at your option) any later version.
8 *
9 *  This library is distributed in the hope that it will be useful,
10 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 *  Lesser General Public License for more details.
13 *
14 *  You should have received a copy of the GNU Lesser General Public
15 *  License along with this library; if not, write to the Free Software
16 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19#include "config.h"
20#include "GtkWidgetBackingStoreX11.h"
21
22#include "GtkVersioning.h"
23#include "RefPtrCairo.h"
24#include <cairo-xlib.h>
25#include <cairo.h>
26#include <gdk/gdkx.h>
27
28namespace WebCore {
29
30PassOwnPtr<WidgetBackingStore> WidgetBackingStoreGtkX11::create(GtkWidget* widget, const IntSize& size)
31{
32    return adoptPtr(new WidgetBackingStoreGtkX11(widget, size));
33}
34
35// We keep two copies of the surface here, which will double the memory usage, but increase
36// scrolling performance since we do not have to keep reallocating a memory region during
37// quick scrolling requests.
38WidgetBackingStoreGtkX11::WidgetBackingStoreGtkX11(GtkWidget* widget, const IntSize& size)
39    : WidgetBackingStore(size)
40{
41    GdkVisual* visual = gtk_widget_get_visual(widget);
42    GdkScreen* screen = gdk_visual_get_screen(visual);
43    m_display = GDK_SCREEN_XDISPLAY(screen);
44    m_pixmap = XCreatePixmap(m_display, GDK_WINDOW_XID(gdk_screen_get_root_window(screen)),
45        size.width(), size.height(), gdk_visual_get_depth(visual));
46    m_gc = XCreateGC(m_display, m_pixmap, 0, 0);
47
48    m_surface = adoptRef(cairo_xlib_surface_create(m_display, m_pixmap,
49        GDK_VISUAL_XVISUAL(visual), size.width(), size.height()));
50}
51
52WidgetBackingStoreGtkX11::~WidgetBackingStoreGtkX11()
53{
54    XFreePixmap(m_display, m_pixmap);
55    XFreeGC(m_display, m_gc);
56}
57
58cairo_surface_t* WidgetBackingStoreGtkX11::cairoSurface()
59{
60    return m_surface.get();
61}
62
63void WidgetBackingStoreGtkX11::scroll(const IntRect& scrollRect, const IntSize& scrollOffset)
64{
65    IntRect targetRect(scrollRect);
66    targetRect.move(scrollOffset);
67    targetRect.intersect(scrollRect);
68    if (targetRect.isEmpty())
69        return;
70
71    cairo_surface_flush(m_surface.get());
72    XCopyArea(m_display, m_pixmap, m_pixmap, m_gc,
73              targetRect.x() - scrollOffset.width(), targetRect.y() - scrollOffset.height(),
74              targetRect.width(), targetRect.height(),
75              targetRect.x(), targetRect.y());
76    cairo_surface_mark_dirty_rectangle(m_surface.get(),
77                                       targetRect.x(), targetRect.y(),
78                                       targetRect.width(), targetRect.height());
79}
80
81} // namespace WebCore
82