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