2 * Copyright (C) 2011, Igalia S.L.
3 * Copyright (C) 2011 Samsung Electronics
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.
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.
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
21 #include "WidgetBackingStore.h"
23 #include "CairoUtilities.h"
24 #include "RefPtrCairo.h"
25 #include <cairo/cairo.h>
28 #include "GtkVersioning.h"
33 static PassRefPtr<cairo_surface_t> createSurfaceForBackingStore(PlatformWidget widget, const IntSize& size)
36 return gdk_window_create_similar_surface(gtk_widget_get_window(widget),
37 CAIRO_CONTENT_COLOR_ALPHA,
38 size.width(), size.height());
40 return adoptRef(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size.width(), size.height()));
44 class WidgetBackingStorePrivate {
45 WTF_MAKE_NONCOPYABLE(WidgetBackingStorePrivate);
46 WTF_MAKE_FAST_ALLOCATED;
49 RefPtr<cairo_surface_t> m_surface;
50 RefPtr<cairo_surface_t> m_scrollSurface;
52 static PassOwnPtr<WidgetBackingStorePrivate> create(PlatformWidget widget, const IntSize& size)
54 return adoptPtr(new WidgetBackingStorePrivate(widget, size));
58 // We keep two copies of the surface here, which will double the memory usage, but increase
59 // scrolling performance since we do not have to keep reallocating a memory region during
60 // quick scrolling requests.
61 WidgetBackingStorePrivate(PlatformWidget widget, const IntSize& size)
62 : m_surface(createSurfaceForBackingStore(widget, size))
63 , m_scrollSurface(createSurfaceForBackingStore(widget, size))
68 PassOwnPtr<WidgetBackingStore> WidgetBackingStore::create(PlatformWidget widget, const IntSize& size)
70 return adoptPtr(new WidgetBackingStore(widget, size));
73 WidgetBackingStore::WidgetBackingStore(PlatformWidget widget, const IntSize& size)
74 : m_private(WidgetBackingStorePrivate::create(widget, size))
79 WidgetBackingStore::~WidgetBackingStore()
83 cairo_surface_t* WidgetBackingStore::cairoSurface()
85 return m_private->m_surface.get();
88 void WidgetBackingStore::scroll(const IntRect& scrollRect, const IntSize& scrollOffset)
90 IntRect targetRect(scrollRect);
91 targetRect.move(scrollOffset);
92 targetRect.shiftMaxXEdgeTo(targetRect.maxX() - scrollOffset.width());
93 targetRect.shiftMaxYEdgeTo(targetRect.maxY() - scrollOffset.height());
94 if (targetRect.isEmpty())
97 copyRectFromOneSurfaceToAnother(m_private->m_surface.get(), m_private->m_scrollSurface.get(),
98 scrollOffset, targetRect);
99 copyRectFromOneSurfaceToAnother(m_private->m_scrollSurface.get(), m_private->m_surface.get(),
100 IntSize(), targetRect);
103 } // namespace WebCore