~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/platform/graphics/qt/TileQt.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
 
3
 
 
4
 This library is free software; you can redistribute it and/or
 
5
 modify it under the terms of the GNU Library 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
 Library General Public License for more details.
 
13
 
 
14
 You should have received a copy of the GNU Library General Public License
 
15
 along with this library; see the file COPYING.LIB.  If not, write to
 
16
 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
 Boston, MA 02110-1301, USA.
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
#include "TileQt.h"
 
22
 
 
23
#if USE(TILED_BACKING_STORE)
 
24
 
 
25
#include "GraphicsContext.h"
 
26
#include "TiledBackingStore.h"
 
27
#include "TiledBackingStoreClient.h"
 
28
#include <QObject>
 
29
#include <QPainter>
 
30
#include <QRegion>
 
31
 
 
32
namespace WebCore {
 
33
    
 
34
static const unsigned checkerSize = 16;
 
35
static const unsigned checkerColor1 = 0xff555555;
 
36
static const unsigned checkerColor2 = 0xffaaaaaa;
 
37
    
 
38
static QPixmap& checkeredPixmap()
 
39
{
 
40
    static QPixmap* pixmap;
 
41
    if (!pixmap) {
 
42
        pixmap = new QPixmap(checkerSize, checkerSize);
 
43
        QPainter painter(pixmap);
 
44
        QColor color1(checkerColor1);
 
45
        QColor color2(checkerColor2);
 
46
        for (unsigned y = 0; y < checkerSize; y += checkerSize / 2) {
 
47
            bool alternate = y % checkerSize;
 
48
            for (unsigned x = 0; x < checkerSize; x += checkerSize / 2) {
 
49
                painter.fillRect(x, y, checkerSize / 2, checkerSize / 2, alternate ? color1 : color2);
 
50
                alternate = !alternate;
 
51
            }
 
52
        }
 
53
    }
 
54
    return *pixmap;
 
55
}
 
56
    
 
57
TileQt::TileQt(TiledBackingStore* backingStore, const Coordinate& tileCoordinate)
 
58
    : m_backingStore(backingStore)
 
59
    , m_coordinate(tileCoordinate)
 
60
    , m_rect(m_backingStore->tileRectForCoordinate(tileCoordinate))
 
61
    , m_buffer(0)
 
62
    , m_backBuffer(0)
 
63
    , m_dirtyRegion(new QRegion(m_rect))
 
64
{
 
65
}
 
66
 
 
67
TileQt::~TileQt()
 
68
{
 
69
    delete m_buffer;
 
70
    delete m_backBuffer;
 
71
    delete m_dirtyRegion;
 
72
}
 
73
 
 
74
bool TileQt::isDirty() const
 
75
 
76
    return !m_dirtyRegion->isEmpty(); 
 
77
}
 
78
 
 
79
bool TileQt::isReadyToPaint() const
 
80
 
81
    return m_buffer; 
 
82
}
 
83
 
 
84
void TileQt::invalidate(const IntRect& dirtyRect)
 
85
{
 
86
    IntRect tileDirtyRect = intersection(dirtyRect, m_rect);
 
87
    if (tileDirtyRect.isEmpty())
 
88
        return;
 
89
 
 
90
    *m_dirtyRegion += tileDirtyRect;
 
91
}
 
92
    
 
93
Vector<IntRect> TileQt::updateBackBuffer()
 
94
{
 
95
    if (m_buffer && !isDirty())
 
96
        return Vector<IntRect>();
 
97
 
 
98
    if (!m_backBuffer) {
 
99
        if (!m_buffer) {
 
100
            m_backBuffer = new QPixmap(m_backingStore->tileSize().width(), m_backingStore->tileSize().height());
 
101
            m_backBuffer->fill(m_backingStore->client()->tiledBackingStoreBackgroundColor());
 
102
        } else {
 
103
            // Currently all buffers are updated synchronously at the same time so there is no real need
 
104
            // to have separate back and front buffers. Just use the existing buffer.
 
105
            m_backBuffer = m_buffer;
 
106
            m_buffer = 0;
 
107
        }
 
108
    }
 
109
 
 
110
    QVector<QRect> dirtyRects = m_dirtyRegion->rects();
 
111
    *m_dirtyRegion = QRegion();
 
112
    
 
113
    QPainter painter(m_backBuffer);
 
114
    GraphicsContext context(&painter);
 
115
    context.translate(-m_rect.x(), -m_rect.y());
 
116
 
 
117
    Vector<IntRect> updatedRects;
 
118
    int size = dirtyRects.size();
 
119
    for (int n = 0; n < size; ++n)  {
 
120
        context.save();
 
121
        IntRect rect = dirtyRects[n];
 
122
        updatedRects.append(rect);
 
123
        context.clip(FloatRect(rect));
 
124
        context.scale(FloatSize(m_backingStore->contentsScale(), m_backingStore->contentsScale()));
 
125
        m_backingStore->client()->tiledBackingStorePaint(&context, m_backingStore->mapToContents(rect));
 
126
        context.restore();
 
127
    }
 
128
 
 
129
    return updatedRects;
 
130
}
 
131
 
 
132
void TileQt::swapBackBufferToFront()
 
133
{
 
134
    if (!m_backBuffer)
 
135
        return;
 
136
    delete m_buffer;
 
137
    m_buffer = m_backBuffer;
 
138
    m_backBuffer = 0;
 
139
}
 
140
 
 
141
void TileQt::paint(GraphicsContext* context, const IntRect& rect)
 
142
{
 
143
    if (!m_buffer)
 
144
        return;
 
145
    
 
146
    IntRect target = intersection(rect, m_rect);
 
147
    IntRect source((target.x() - m_rect.x()),
 
148
                   (target.y() - m_rect.y()),
 
149
                   target.width(),
 
150
                   target.height());
 
151
    
 
152
    context->platformContext()->drawPixmap(target, *m_buffer, source);
 
153
}
 
154
    
 
155
void TileQt::resize(const IntSize& newSize)
 
156
{
 
157
    IntRect oldRect = m_rect;
 
158
    m_rect = IntRect(m_rect.location(), newSize);
 
159
    if (m_rect.maxX() > oldRect.maxX())
 
160
        invalidate(IntRect(oldRect.maxX(), oldRect.y(), m_rect.maxX() - oldRect.maxX(), m_rect.height()));
 
161
    if (m_rect.maxY() > oldRect.maxY())
 
162
        invalidate(IntRect(oldRect.x(), oldRect.maxY(), m_rect.width(), m_rect.maxY() - oldRect.maxY()));
 
163
}
 
164
 
 
165
void TiledBackingStoreBackend::paintCheckerPattern(GraphicsContext* context, const FloatRect& target)
 
166
{
 
167
    QPainter* painter = context->platformContext();
 
168
    QTransform worldTransform = painter->worldTransform();
 
169
    qreal scaleX = worldTransform.m11();
 
170
    qreal scaleY = worldTransform.m22();
 
171
    
 
172
    QRect targetViewRect = QRectF(target.x() * scaleX,
 
173
                                  target.y() * scaleY,
 
174
                                  target.width() * scaleX,
 
175
                                  target.height() * scaleY).toAlignedRect();
 
176
    
 
177
    QTransform adjustedTransform(1., worldTransform.m12(), worldTransform.m13(),
 
178
                  worldTransform.m21(), 1., worldTransform.m23(),
 
179
                  worldTransform.m31(), worldTransform.m32(), worldTransform.m33());
 
180
    painter->setWorldTransform(adjustedTransform);
 
181
    
 
182
    painter->drawTiledPixmap(targetViewRect,
 
183
                             checkeredPixmap(),
 
184
                             QPoint(targetViewRect.left() % checkerSize,
 
185
                                    targetViewRect.top() % checkerSize));
 
186
    
 
187
    painter->setWorldTransform(worldTransform);
 
188
}
 
189
 
 
190
PassRefPtr<Tile> TiledBackingStoreBackend::createTile(TiledBackingStore* backingStore, const Tile::Coordinate& tileCoordinate)
 
191
{
 
192
    return TileQt::create(backingStore, tileCoordinate);
 
193
}
 
194
 
 
195
}
 
196
 
 
197
#endif