1/*
2 Copyright (C) 2010 codeaurora.org
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#include "config.h"
29#include "Tile.h"
30
31#if ENABLE(TILED_BACKING_STORE)
32
33#include "GraphicsContext.h"
34#include "GtkVersioning.h"
35#include "TiledBackingStore.h"
36#include "TiledBackingStoreClient.h"
37#include <GOwnPtr.h>
38#include <GRefPtr.h>
39#include <RefPtrCairo.h>
40#include <OwnPtrCairo.h>
41#include <gtk/gtk.h>
42
43namespace WebCore {
44
45static const unsigned checkerSize = 16;
46static const unsigned checkerColor1 = 0xff555555;
47static const unsigned checkerColor2 = 0xffaaaaaa;
48
49static GdkPixmap* checkeredPixmap()
50{
51 static GdkPixmap* pixmap = 0;
52
53 if (pixmap)
54 return pixmap;
55 pixmap = gdk_pixmap_new(0, checkerSize, checkerSize, gdk_visual_get_depth(gdk_visual_get_system()));
56 cairo_t* cr = gdk_cairo_create(pixmap);
57 GraphicsContext context(cr);
58 for (unsigned y = 0; y < checkerSize; y += checkerSize / 2) {
59 bool alternate = y % checkerSize;
60 for (unsigned x = 0; x < checkerSize; x += checkerSize / 2) {
61 context.fillRect(FloatRect(x, y, checkerSize / 2, checkerSize / 2), alternate ? checkerColor1 : checkerColor2, ColorSpaceDeviceRGB);
62 alternate = !alternate;
63 }
64 }
65
66 return pixmap;
67}
68
69Tile::Tile(TiledBackingStore* backingStore, const Coordinate& tileCoordinate)
70 : m_backingStore(backingStore)
71 , m_coordinate(tileCoordinate)
72 , m_rect(m_backingStore->tileRectForCoordinate(tileCoordinate))
73{
74#ifdef GTK_API_VERSION_2
75 GdkRectangle rect = m_rect;
76 m_dirtyRegion = gdk_region_rectangle(&rect);
77#else
78 cairo_rectangle_int_t rect = m_rect;
79 m_dirtyRegion = cairo_region_create_rectangle(&rect);
80#endif
81}
82
83Tile::~Tile()
84{
85#ifdef GTK_API_VERSION_2
86 gdk_region_destroy(m_dirtyRegion);
87#else
88 cairo_region_destroy(m_dirtyRegion);
89#endif
90}
91
92bool Tile::isDirty() const
93{
94#ifdef GTK_API_VERSION_2
95 return !gdk_region_empty(m_dirtyRegion);
96#else
97 return !cairo_region_is_empty(m_dirtyRegion);
98#endif
99}
100
101bool Tile::isReadyToPaint() const
102{
103 return m_buffer;
104}
105
106void Tile::invalidate(const IntRect& dirtyRect)
107{
108 IntRect tileDirtyRect = intersection(dirtyRect, m_rect);
109 if (tileDirtyRect.isEmpty())
110 return;
111
112#ifdef GTK_API_VERSION_2
113 GdkRectangle rect = tileDirtyRect;
114 gdk_region_union_with_rect(m_dirtyRegion, &rect);
115#else
116 cairo_rectangle_int_t rect = tileDirtyRect;
117 cairo_region_union_rectangle(m_dirtyRegion, &rect);
118#endif
119}
120
121void Tile::updateBackBuffer()
122{
123 if (m_buffer && !isDirty())
124 return;
125
126 if (!m_buffer)
127 m_buffer = adoptRef(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, m_backingStore->m_tileSize.width(), m_backingStore->m_tileSize.height()));
128
129 int rectCount;
130#ifdef GTK_API_VERSION_2
131 GOwnPtr<GdkRectangle> rects;
132 gdk_region_get_rectangles(m_dirtyRegion, &rects.outPtr(), &rectCount);
133 gdk_region_destroy(m_dirtyRegion);
134 m_dirtyRegion = gdk_region_new();
135#else
136 int rectCount = cairo_region_num_rectangles(m_dirtyRegion);
137 GOwnPtr<GdkRectangle> rects(g_new(GdkRectangle, rectCount));
138 for (int i = 0; i < rectCount; i++)
139 cairo_region_get_rectangle(event->region, i, rects.get()+i);
140 cairo_region_destroy(m_dirtyRegion);
141 m_dirtyRegion = cairo_region_create();
142#endif
143
144 RefPtr<cairo_t> cr = adoptRef(cairo_create(m_buffer.get()));
145 GraphicsContext context(cr.get());
146 context.translate(-m_rect.x(), -m_rect.y());
147
148 for (int i = 0; i < rectCount ; ++i) {
149 context.save();
150 IntRect rect = rects.get()[i];
151 context.clip(FloatRect(rect));
152 context.scale(FloatSize(m_backingStore->m_contentsScale, m_backingStore->m_contentsScale));
153 m_backingStore->m_client->tiledBackingStorePaint(&context, m_backingStore->mapToContents(rect));
154 context.restore();
155 }
156}
157
158void Tile::swapBackBufferToFront()
159{
160}
161
162void Tile::paint(GraphicsContext* context, const IntRect& rect)
163{
164 if (!m_buffer)
165 return;
166
167 IntRect target = intersection(rect, m_rect);
168 IntRect source(target.x() - m_rect.x(), target.y() - m_rect.y(), target.width(), target.height());
169
170 cairo_t* cr = context->platformContext();
171 cairo_set_source_surface(cr, m_buffer.get(), target.x() - source.x(), target.y() - source.y());
172 cairo_rectangle(cr, target.x(), target.y(), target.width(), target.height());
173 cairo_fill(cr);
174}
175
176void Tile::paintCheckerPattern(GraphicsContext* context, const FloatRect& target)
177{
178 cairo_t* cr = context->platformContext();
179 gdk_cairo_set_source_pixmap(cr, checkeredPixmap(), 0, 0);
180 cairo_pattern_set_extend(cairo_get_source(cr), CAIRO_EXTEND_REPEAT);
181 cairo_rectangle(cr, target.x(), target.y(), target.width(), target.height());
182 cairo_fill(cr);
183}
184
185}
186#endif // ENABLE(TILED_BACKING_STORE)