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

« back to all changes in this revision

Viewing changes to Source/WebKit/qt/WebCoreSupport/PageClientQt.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
 
 
21
#include "config.h"
 
22
#include "PageClientQt.h"
 
23
 
 
24
#include <QGraphicsScene>
 
25
#include <QGraphicsView>
 
26
 
 
27
#if defined(Q_WS_X11)
 
28
#include <QX11Info>
 
29
#endif
 
30
 
 
31
#ifdef QT_OPENGL_LIB
 
32
#include <QGLWidget>
 
33
#endif
 
34
 
 
35
#if USE(3D_GRAPHICS)
 
36
#if HAVE(QT5)
 
37
#include <QWindow>
 
38
#endif
 
39
 
 
40
static void createPlatformGraphicsContext3DFromWidget(QWidget* widget, PlatformGraphicsContext3D* context,
 
41
                                                      PlatformGraphicsSurface3D* surface, QObject** surfaceOwner)
 
42
{
 
43
#ifdef QT_OPENGL_LIB
 
44
    *context = 0;
 
45
    *surface = 0;
 
46
    if (surfaceOwner)
 
47
        *surfaceOwner = 0;
 
48
 
 
49
    QGLWidget* glViewport = 0;
 
50
 
 
51
    QAbstractScrollArea* scrollArea = qobject_cast<QAbstractScrollArea*>(widget);
 
52
 
 
53
    if (scrollArea)
 
54
        glViewport = qobject_cast<QGLWidget*>(scrollArea->viewport());
 
55
 
 
56
    QGLWidget* glWidget = 0;
 
57
    if (glViewport)
 
58
        glWidget = new QGLWidget(0, glViewport);
 
59
    else
 
60
        glWidget = new QGLWidget();
 
61
 
 
62
    if (glWidget->isValid()) {
 
63
        // Geometry can be set to zero because m_glWidget is used only for its QGLContext.
 
64
        glWidget->setGeometry(0, 0, 0, 0);
 
65
        if (surfaceOwner)
 
66
            *surfaceOwner = glWidget;
 
67
#if HAVE(QT5)
 
68
        *surface = glWidget->windowHandle();
 
69
        *context = glWidget->context()->contextHandle();
 
70
#else
 
71
        *surface = glWidget;
 
72
        *context = const_cast<QGLContext*>(glWidget->context());
 
73
#endif
 
74
    } else {
 
75
        delete glWidget;
 
76
        glWidget = 0;
 
77
    }
 
78
#endif
 
79
}
 
80
#endif
 
81
 
 
82
#if USE(ACCELERATED_COMPOSITING)
 
83
#include "TextureMapper.h"
 
84
#include "texmap/TextureMapperLayer.h"
 
85
#endif
 
86
 
 
87
#if HAVE(QT5)
 
88
QWindow* QWebPageClient::ownerWindow() const
 
89
{
 
90
    QWidget* widget = ownerWidget();
 
91
    if (!widget)
 
92
        return 0;
 
93
    if (QWindow *window = widget->windowHandle())
 
94
        return window;
 
95
    if (const QWidget *nativeParent = widget->nativeParentWidget())
 
96
        return nativeParent->windowHandle();
 
97
    return 0;
 
98
}
 
99
#endif
 
100
 
 
101
namespace WebCore {
 
102
 
 
103
#if USE(ACCELERATED_COMPOSITING)
 
104
TextureMapperLayerClientQt::TextureMapperLayerClientQt(QWebFrame* frame, GraphicsLayer* layer)
 
105
    : m_frame(frame)
 
106
    , m_rootGraphicsLayer(GraphicsLayer::create(0))
 
107
{
 
108
    m_frame->d->rootTextureMapperLayer = rootLayer();
 
109
    m_rootGraphicsLayer->addChild(layer);
 
110
    m_rootGraphicsLayer->setDrawsContent(false);
 
111
    m_rootGraphicsLayer->setMasksToBounds(false);
 
112
    m_rootGraphicsLayer->setSize(IntSize(1, 1));
 
113
}
 
114
 
 
115
void TextureMapperLayerClientQt::setTextureMapper(const PassOwnPtr<TextureMapper>& textureMapper)
 
116
{
 
117
    m_frame->d->textureMapper = textureMapper;
 
118
    m_frame->d->rootTextureMapperLayer->setTextureMapper(m_frame->d->textureMapper.get());
 
119
    syncRootLayer();
 
120
}
 
121
 
 
122
TextureMapperLayerClientQt::~TextureMapperLayerClientQt()
 
123
{
 
124
    m_frame->d->rootTextureMapperLayer = 0;
 
125
}
 
126
 
 
127
void TextureMapperLayerClientQt::syncRootLayer()
 
128
{
 
129
    m_rootGraphicsLayer->flushCompositingStateForThisLayerOnly();
 
130
}
 
131
 
 
132
TextureMapperLayer* TextureMapperLayerClientQt::rootLayer()
 
133
{
 
134
    return toTextureMapperLayer(m_rootGraphicsLayer.get());
 
135
}
 
136
 
 
137
 
 
138
void PageClientQWidget::setRootGraphicsLayer(GraphicsLayer* layer)
 
139
{
 
140
    if (layer) {
 
141
        TextureMapperLayerClient = adoptPtr(new TextureMapperLayerClientQt(page->mainFrame(), layer));
 
142
        TextureMapperLayerClient->setTextureMapper(TextureMapper::create());
 
143
        return;
 
144
    }
 
145
    TextureMapperLayerClient.clear();
 
146
}
 
147
 
 
148
void PageClientQWidget::markForSync(bool scheduleSync)
 
149
{
 
150
    if (syncTimer.isActive())
 
151
        return;
 
152
    syncTimer.startOneShot(0);
 
153
}
 
154
 
 
155
void PageClientQWidget::syncLayers(Timer<PageClientQWidget>*)
 
156
{
 
157
    if (TextureMapperLayerClient)
 
158
        TextureMapperLayerClient->syncRootLayer();
 
159
    QWebFramePrivate::core(page->mainFrame())->view()->flushCompositingStateIncludingSubframes();
 
160
    if (!TextureMapperLayerClient)
 
161
        return;
 
162
    if (TextureMapperLayerClient->rootLayer()->descendantsOrSelfHaveRunningAnimations() && !syncTimer.isActive())
 
163
        syncTimer.startOneShot(1.0 / 60.0);
 
164
    update(view->rect());
 
165
}
 
166
#endif
 
167
 
 
168
void PageClientQWidget::scroll(int dx, int dy, const QRect& rectToScroll)
 
169
{
 
170
    view->scroll(qreal(dx), qreal(dy), rectToScroll);
 
171
}
 
172
 
 
173
void PageClientQWidget::update(const QRect & dirtyRect)
 
174
{
 
175
    view->update(dirtyRect);
 
176
}
 
177
 
 
178
void PageClientQWidget::setInputMethodEnabled(bool enable)
 
179
{
 
180
    view->setAttribute(Qt::WA_InputMethodEnabled, enable);
 
181
}
 
182
 
 
183
bool PageClientQWidget::inputMethodEnabled() const
 
184
{
 
185
    return view->testAttribute(Qt::WA_InputMethodEnabled);
 
186
}
 
187
 
 
188
void PageClientQWidget::setInputMethodHints(Qt::InputMethodHints hints)
 
189
{
 
190
    view->setInputMethodHints(hints);
 
191
}
 
192
 
 
193
PageClientQWidget::~PageClientQWidget()
 
194
{
 
195
}
 
196
 
 
197
#ifndef QT_NO_CURSOR
 
198
QCursor PageClientQWidget::cursor() const
 
199
{
 
200
    return view->cursor();
 
201
}
 
202
 
 
203
void PageClientQWidget::updateCursor(const QCursor& cursor)
 
204
{
 
205
    view->setCursor(cursor);
 
206
}
 
207
#endif
 
208
 
 
209
QPalette PageClientQWidget::palette() const
 
210
{
 
211
    return view->palette();
 
212
}
 
213
 
 
214
int PageClientQWidget::screenNumber() const
 
215
{
 
216
#if defined(Q_WS_X11)
 
217
    return view->x11Info().screen();
 
218
#endif
 
219
    return 0;
 
220
}
 
221
 
 
222
QWidget* PageClientQWidget::ownerWidget() const
 
223
{
 
224
    return view;
 
225
}
 
226
 
 
227
QRect PageClientQWidget::geometryRelativeToOwnerWidget() const
 
228
{
 
229
    return view->geometry();
 
230
}
 
231
 
 
232
QObject* PageClientQWidget::pluginParent() const
 
233
{
 
234
    return view;
 
235
}
 
236
 
 
237
QStyle* PageClientQWidget::style() const
 
238
{
 
239
    return view->style();
 
240
}
 
241
 
 
242
QRectF PageClientQWidget::windowRect() const
 
243
{
 
244
    return QRectF(view->window()->geometry());
 
245
}
 
246
 
 
247
void PageClientQWidget::setWidgetVisible(Widget* widget, bool visible)
 
248
{
 
249
    QWidget* qtWidget = qobject_cast<QWidget*>(widget->platformWidget());
 
250
    if (!qtWidget)
 
251
        return;
 
252
    qtWidget->setVisible(visible);
 
253
}
 
254
 
 
255
#if USE(3D_GRAPHICS)
 
256
void PageClientQWidget::createPlatformGraphicsContext3D(PlatformGraphicsContext3D* context,
 
257
                                                        PlatformGraphicsSurface3D* surface,
 
258
                                                        QObject** surfaceOwner)
 
259
{
 
260
    createPlatformGraphicsContext3DFromWidget(view, context, surface, surfaceOwner);
 
261
}
 
262
#endif
 
263
 
 
264
#if !defined(QT_NO_GRAPHICSVIEW)
 
265
PageClientQGraphicsWidget::~PageClientQGraphicsWidget()
 
266
{
 
267
    delete overlay;
 
268
}
 
269
 
 
270
void PageClientQGraphicsWidget::scroll(int dx, int dy, const QRect& rectToScroll)
 
271
{
 
272
    view->scroll(qreal(dx), qreal(dy), rectToScroll);
 
273
}
 
274
 
 
275
void PageClientQGraphicsWidget::update(const QRect& dirtyRect)
 
276
{
 
277
    view->update(dirtyRect);
 
278
 
 
279
    if (overlay)
 
280
        overlay->update(QRectF(dirtyRect));
 
281
}
 
282
 
 
283
#if USE(ACCELERATED_COMPOSITING)
 
284
void PageClientQGraphicsWidget::syncLayers()
 
285
{
 
286
    if (TextureMapperLayerClient)
 
287
        TextureMapperLayerClient->syncRootLayer();
 
288
 
 
289
    QWebFramePrivate::core(page->mainFrame())->view()->flushCompositingStateIncludingSubframes();
 
290
 
 
291
    if (!TextureMapperLayerClient)
 
292
        return;
 
293
 
 
294
    if (TextureMapperLayerClient->rootLayer()->descendantsOrSelfHaveRunningAnimations() && !syncTimer.isActive())
 
295
        syncTimer.startOneShot(1.0 / 60.0);
 
296
    update(view->boundingRect().toAlignedRect());
 
297
}
 
298
 
 
299
void PageClientQGraphicsWidget::setRootGraphicsLayer(GraphicsLayer* layer)
 
300
{
 
301
    if (layer) {
 
302
        TextureMapperLayerClient = adoptPtr(new TextureMapperLayerClientQt(page->mainFrame(), layer));
 
303
#if USE(TEXTURE_MAPPER_GL) && defined(QT_OPENGL_LIB)
 
304
        QGraphicsView* graphicsView = view->scene()->views()[0];
 
305
        if (graphicsView && graphicsView->viewport()) {
 
306
            QGLWidget* glWidget = qobject_cast<QGLWidget*>(graphicsView->viewport());
 
307
            if (glWidget) {
 
308
                // The GL context belonging to the QGLWidget viewport must be current when TextureMapper is being created.
 
309
                glWidget->makeCurrent();
 
310
                TextureMapperLayerClient->setTextureMapper(TextureMapper::create(TextureMapper::OpenGLMode));
 
311
                return;
 
312
            }
 
313
        }
 
314
#endif
 
315
        TextureMapperLayerClient->setTextureMapper(TextureMapper::create());
 
316
        return;
 
317
    }
 
318
    TextureMapperLayerClient.clear();
 
319
}
 
320
 
 
321
void PageClientQGraphicsWidget::markForSync(bool scheduleSync)
 
322
{
 
323
    if (syncTimer.isActive())
 
324
        return;
 
325
    syncTimer.startOneShot(0);
 
326
}
 
327
 
 
328
#endif
 
329
 
 
330
#if USE(TILED_BACKING_STORE)
 
331
void PageClientQGraphicsWidget::updateTiledBackingStoreScale()
 
332
{
 
333
    WebCore::TiledBackingStore* backingStore = QWebFramePrivate::core(page->mainFrame())->tiledBackingStore();
 
334
    if (!backingStore)
 
335
        return;
 
336
    backingStore->setContentsScale(view->scale());
 
337
}
 
338
#endif
 
339
 
 
340
void PageClientQGraphicsWidget::setInputMethodEnabled(bool enable)
 
341
{
 
342
    view->setFlag(QGraphicsItem::ItemAcceptsInputMethod, enable);
 
343
}
 
344
 
 
345
bool PageClientQGraphicsWidget::inputMethodEnabled() const
 
346
{
 
347
    return view->flags() & QGraphicsItem::ItemAcceptsInputMethod;
 
348
}
 
349
 
 
350
void PageClientQGraphicsWidget::setInputMethodHints(Qt::InputMethodHints hints)
 
351
{
 
352
    view->setInputMethodHints(hints);
 
353
}
 
354
 
 
355
#ifndef QT_NO_CURSOR
 
356
QCursor PageClientQGraphicsWidget::cursor() const
 
357
{
 
358
    return view->cursor();
 
359
}
 
360
 
 
361
void PageClientQGraphicsWidget::updateCursor(const QCursor& cursor)
 
362
{
 
363
    view->setCursor(cursor);
 
364
}
 
365
#endif
 
366
 
 
367
QPalette PageClientQGraphicsWidget::palette() const
 
368
{
 
369
    return view->palette();
 
370
}
 
371
 
 
372
int PageClientQGraphicsWidget::screenNumber() const
 
373
{
 
374
#if defined(Q_WS_X11)
 
375
    if (QGraphicsScene* scene = view->scene()) {
 
376
        const QList<QGraphicsView*> views = scene->views();
 
377
 
 
378
        if (!views.isEmpty())
 
379
            return views.at(0)->x11Info().screen();
 
380
    }
 
381
#endif
 
382
 
 
383
    return 0;
 
384
}
 
385
 
 
386
QWidget* PageClientQGraphicsWidget::ownerWidget() const
 
387
{
 
388
    if (QGraphicsScene* scene = view->scene()) {
 
389
        const QList<QGraphicsView*> views = scene->views();
 
390
        return views.value(0);
 
391
    }
 
392
    return 0;
 
393
}
 
394
 
 
395
QRect PageClientQGraphicsWidget::geometryRelativeToOwnerWidget() const
 
396
{
 
397
    if (!view->scene())
 
398
        return QRect();
 
399
 
 
400
    QList<QGraphicsView*> views = view->scene()->views();
 
401
    if (views.isEmpty())
 
402
        return QRect();
 
403
 
 
404
    QGraphicsView* graphicsView = views.at(0);
 
405
    return graphicsView->mapFromScene(view->boundingRect()).boundingRect();
 
406
}
 
407
 
 
408
#if USE(TILED_BACKING_STORE)
 
409
QRectF PageClientQGraphicsWidget::graphicsItemVisibleRect() const
 
410
{
 
411
    if (!view->scene())
 
412
        return QRectF();
 
413
 
 
414
    QList<QGraphicsView*> views = view->scene()->views();
 
415
    if (views.isEmpty())
 
416
        return QRectF();
 
417
 
 
418
    QGraphicsView* graphicsView = views.at(0);
 
419
    int xOffset = graphicsView->horizontalScrollBar()->value();
 
420
    int yOffset = graphicsView->verticalScrollBar()->value();
 
421
    return view->mapRectFromScene(QRectF(QPointF(xOffset, yOffset), graphicsView->viewport()->size()));
 
422
}
 
423
#endif
 
424
 
 
425
QObject* PageClientQGraphicsWidget::pluginParent() const
 
426
{
 
427
    return view;
 
428
}
 
429
 
 
430
QStyle* PageClientQGraphicsWidget::style() const
 
431
{
 
432
    return view->style();
 
433
}
 
434
 
 
435
void PageClientQGraphicsWidget::setWidgetVisible(Widget*, bool)
 
436
{
 
437
    // Doesn't make sense, does it?
 
438
}
 
439
 
 
440
QRectF PageClientQGraphicsWidget::windowRect() const
 
441
{
 
442
    if (!view->scene())
 
443
        return QRectF();
 
444
 
 
445
    // The sceneRect is a good approximation of the size of the application, independent of the view.
 
446
    return view->scene()->sceneRect();
 
447
}
 
448
#endif // QT_NO_GRAPHICSVIEW
 
449
 
 
450
#if USE(3D_GRAPHICS)
 
451
void PageClientQGraphicsWidget::createPlatformGraphicsContext3D(PlatformGraphicsContext3D* context,
 
452
                                                                PlatformGraphicsSurface3D* surface,
 
453
                                                                QObject** surfaceOwner)
 
454
{
 
455
    createPlatformGraphicsContext3DFromWidget(ownerWidget(), context, surface, surfaceOwner);
 
456
}
 
457
#endif
 
458
 
 
459
} // namespace WebCore