1/*
2 * Copyright (C) 2010 codeaurora.org All rights reserved.
3 * Copyright (C) 2011 Collabora Ltd.
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 "CairoUtilities.h"
34#include "GraphicsContext.h"
35#include "PlatformContextCairo.h"
36#include "TiledBackingStore.h"
37#include "TiledBackingStoreClient.h"
38#include <OwnPtrCairo.h>
39#include <RefPtrCairo.h>
40
41namespace WebCore {
42
43static const unsigned checkerSize = 16;
44static const unsigned checkerColor1 = 0xff555555;
45static const unsigned checkerColor2 = 0xffaaaaaa;
46
47static cairo_surface_t* checkeredSurface()
48{
49 static cairo_surface_t* surface = 0;
50 if (surface)
51 return surface;
52
53 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, checkerSize, checkerSize);
54 RefPtr<cairo_t> cr = adoptRef(cairo_create(surface));
55
56 for (unsigned y = 0; y < checkerSize; y += checkerSize / 2) {
57 bool alternate = y % checkerSize;
58 for (unsigned x = 0; x < checkerSize; x += checkerSize / 2) {
59 if (alternate)
60 setSourceRGBAFromColor(cr.get(), checkerColor1);
61 else
62 setSourceRGBAFromColor(cr.get(), checkerColor2);
63 cairo_rectangle(cr.get(), x, y, checkerSize / 2, checkerSize / 2);
64 cairo_fill(cr.get());
65 alternate = !alternate;
66 }
67 }
68
69 return surface;
70}
71
72Tile::Tile(TiledBackingStore* backingStore, const Coordinate& tileCoordinate)
73 : m_backingStore(backingStore)
74 , m_coordinate(tileCoordinate)
75 , m_rect(m_backingStore->tileRectForCoordinate(tileCoordinate))
76{
77 cairo_rectangle_int_t rect = m_rect;
78 m_dirtyRegion = cairo_region_create_rectangle(&rect);
79}
80
81Tile::~Tile()
82{
83}
84
85bool Tile::isDirty() const
86{
87 return !cairo_region_is_empty(m_dirtyRegion.get());
88}
89
90bool Tile::isReadyToPaint() const
91{
92 return m_buffer;
93}
94
95void Tile::invalidate(const IntRect& dirtyRect)
96{
97 IntRect tileDirtyRect = intersection(dirtyRect, m_rect);
98 if (tileDirtyRect.isEmpty())
99 return;
100
101 cairo_rectangle_int_t rect = tileDirtyRect;
102 cairo_region_union_rectangle(m_dirtyRegion.get(), &rect);
103
104}
105
106static void regionToRects(cairo_region_t* region, Vector<IntRect>& updateRects)
107{
108 int rectCount = cairo_region_num_rectangles(region);
109 OwnPtr<cairo_rectangle_int_t> rects = adoptPtr(new cairo_rectangle_int_t[rectCount]);
110 for (int i = 0; i < rectCount; i++) {
111 cairo_region_get_rectangle(region, i, rects.get() + i);
112 updateRects.append(IntRect(rects.get()[i]));
113 }
114}
115
116Vector<IntRect> Tile::updateBackBuffer()
117{
118 if (m_buffer && !isDirty())
119 return Vector<IntRect>();
120
121 if (!m_buffer)
122 m_buffer = adoptRef(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, m_backingStore->m_tileSize.width(), m_backingStore->m_tileSize.height()));
123
124 Vector<IntRect> updateRects;
125 regionToRects(m_dirtyRegion.get(), updateRects);
126 m_dirtyRegion.clear();
127 m_dirtyRegion = adoptPtr(cairo_region_create());
128
129 RefPtr<cairo_t> cr = adoptRef(cairo_create(m_buffer.get()));
130 GraphicsContext context(cr.get());
131 context.translate(-m_rect.x(), -m_rect.y());
132
133 for (int i = 0; i < updateRects.size() ; ++i) {
134 context.save();
135 context.clip(FloatRect(updateRects[i]));
136 context.scale(FloatSize(m_backingStore->m_contentsScale, m_backingStore->m_contentsScale));
137 m_backingStore->m_client->tiledBackingStorePaint(&context, m_backingStore->mapToContents(updateRects[i]));
138 context.restore();
139 }
140
141 return updateRects;
142}
143
144void Tile::swapBackBufferToFront()
145{
146}
147
148void Tile::paint(GraphicsContext* context, const IntRect& rect)
149{
150 if (!m_buffer)
151 return;
152
153 IntRect target = intersection(rect, m_rect);
154 IntRect source(target.x() - m_rect.x(), target.y() - m_rect.y(), target.width(), target.height());
155
156 PlatformContextCairo* platformContext = context->platformContext();
157 cairo_t* cr = platformContext->cr();
158 cairo_set_source_surface(cr, m_buffer.get(), target.x() - source.x(), target.y() - source.y());
159 cairo_rectangle(cr, target.x(), target.y(), target.width(), target.height());
160 cairo_fill(cr);
161}
162
163void Tile::paintCheckerPattern(GraphicsContext* context, const FloatRect& target)
164{
165 PlatformContextCairo* platformContext = context->platformContext();
166 cairo_t* cr = platformContext->cr();
167 cairo_set_source_surface(cr, checkeredSurface(), 0, 0);
168 cairo_pattern_set_extend(cairo_get_source(cr), CAIRO_EXTEND_REPEAT);
169 cairo_rectangle(cr, target.x(), target.y(), target.width(), target.height());
170 cairo_fill(cr);
171}
172
173}
174#endif // ENABLE(TILED_BACKING_STORE)