1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
/*
* Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this program; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "qquickwebpage_p.h"
#include "LayerTreeHostProxy.h"
#include "QtWebPageEventHandler.h"
#include "TransformationMatrix.h"
#include "qquickwebpage_p_p.h"
#include "qquickwebview_p.h"
#include <QtQuick/QQuickCanvas>
#include <QtQuick/QSGGeometryNode>
#include <QtQuick/QSGMaterial>
#include <private/qsgrendernode_p.h>
QQuickWebPage::QQuickWebPage(QQuickWebView* viewportItem)
: QQuickItem(viewportItem)
, d(new QQuickWebPagePrivate(this, viewportItem))
{
setFlag(ItemHasContents);
// We do the transform from the top left so the viewport can assume the position 0, 0
// is always where rendering starts.
setTransformOrigin(TopLeft);
}
QQuickWebPage::~QQuickWebPage()
{
delete d;
}
QQuickWebPagePrivate::QQuickWebPagePrivate(QQuickWebPage* q, QQuickWebView* viewportItem)
: q(q)
, viewportItem(viewportItem)
, webPageProxy(0)
, paintingIsInitialized(false)
, m_paintNode(0)
, contentsScale(1)
{
}
void QQuickWebPagePrivate::initialize(WebKit::WebPageProxy* webPageProxy)
{
this->webPageProxy = webPageProxy;
eventHandler.reset(new QtWebPageEventHandler(toAPI(webPageProxy), q, viewportItem));
}
static float computeEffectiveOpacity(const QQuickItem* item)
{
if (!item)
return 1;
float opacity = item->opacity();
if (opacity < 0.01)
return 0;
return opacity * computeEffectiveOpacity(item->parentItem());
}
void QQuickWebPagePrivate::setDrawingAreaSize(const QSize& size)
{
DrawingAreaProxy* drawingArea = webPageProxy->drawingArea();
if (!drawingArea)
return;
drawingArea->setSize(WebCore::IntSize(size), WebCore::IntSize());
}
void QQuickWebPagePrivate::paint(QPainter* painter)
{
if (webPageProxy->drawingArea())
webPageProxy->drawingArea()->paintLayerTree(painter);
}
void QQuickWebPagePrivate::paintToCurrentGLContext()
{
if (!q->isVisible())
return;
QTransform transform = q->itemTransform(0, 0);
transform.scale(contentsScale, contentsScale);
float opacity = computeEffectiveOpacity(q);
QRectF clipRect = viewportItem->mapRectToScene(viewportItem->boundingRect());
if (!clipRect.isValid())
return;
DrawingAreaProxy* drawingArea = webPageProxy->drawingArea();
if (!drawingArea)
return;
drawingArea->paintToCurrentGLContext(transform, opacity, clipRect);
}
struct PageProxyNode : public QSGRenderNode {
PageProxyNode(QQuickWebPagePrivate* page)
: m_pagePrivate(page)
{
}
virtual StateFlags changedStates()
{
return StateFlags(DepthState) | StencilState | ScissorState | ColorState | BlendState
| CullState | ViewportState;
}
virtual void render(const RenderState &)
{
if (m_pagePrivate)
m_pagePrivate->paintToCurrentGLContext();
}
~PageProxyNode()
{
if (m_pagePrivate)
m_pagePrivate->resetPaintNode();
}
QQuickWebPagePrivate* m_pagePrivate;
};
QSGNode* QQuickWebPage::updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData*)
{
if (!(flags() & ItemHasContents)) {
if (oldNode)
delete oldNode;
return 0;
}
PageProxyNode* proxyNode = static_cast<PageProxyNode*>(oldNode);
if (!proxyNode) {
proxyNode = new PageProxyNode(d);
d->m_paintNode = proxyNode;
}
return proxyNode;
}
QtWebPageEventHandler* QQuickWebPage::eventHandler() const
{
return d->eventHandler.data();
}
void QQuickWebPage::setContentsSize(const QSizeF& size)
{
if (size.isEmpty() || d->contentsSize == size)
return;
d->contentsSize = size;
d->updateSize();
d->setDrawingAreaSize(d->contentsSize.toSize());
}
const QSizeF& QQuickWebPage::contentsSize() const
{
return d->contentsSize;
}
void QQuickWebPage::setContentsScale(qreal scale)
{
ASSERT(scale > 0);
d->contentsScale = scale;
d->updateSize();
}
qreal QQuickWebPage::contentsScale() const
{
ASSERT(d->contentsScale > 0);
return d->contentsScale;
}
QTransform QQuickWebPage::transformFromItem() const
{
return transformToItem().inverted();
}
QTransform QQuickWebPage::transformToItem() const
{
QPointF pos = d->viewportItem->pageItemPos();
return QTransform(d->contentsScale, 0, 0, 0, d->contentsScale, 0, pos.x(), pos.y(), 1);
}
void QQuickWebPagePrivate::updateSize()
{
QSizeF scaledSize = contentsSize * contentsScale;
q->setSize(scaledSize);
viewportItem->updateContentsSize(scaledSize);
}
void QQuickWebPagePrivate::resetPaintNode()
{
m_paintNode = 0;
DrawingAreaProxy* drawingArea = webPageProxy->drawingArea();
if (drawingArea && drawingArea->layerTreeHostProxy())
drawingArea->layerTreeHostProxy()->purgeGLResources();
}
QQuickWebPagePrivate::~QQuickWebPagePrivate()
{
if (m_paintNode)
static_cast<PageProxyNode*>(m_paintNode)->m_pagePrivate = 0;
}
#include "moc_qquickwebpage_p.cpp"
|