[TexMap] Remove GraphicsLayer in TextureMapperLayer.
[WebKit-https.git] / Source / WebKit / qt / WebCoreSupport / TextureMapperLayerClientQt.cpp
1 /*
2  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
3  * Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies)
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  */
21
22 #include "config.h"
23 #include "TextureMapperLayerClientQt.h"
24
25 #if USE(ACCELERATED_COMPOSITING)
26
27 #include "FrameView.h"
28 #include "GraphicsLayerTextureMapper.h"
29 #include "QWebFrameAdapter.h"
30 #include "QWebPageAdapter.h"
31 #include "TextureMapperLayer.h"
32
33 using namespace WebCore;
34
35 TextureMapperLayerClientQt::TextureMapperLayerClientQt(QWebFrameAdapter* frame)
36     : m_frame(frame)
37     , m_syncTimer(this, &TextureMapperLayerClientQt::syncLayers)
38     , m_rootTextureMapperLayer(0)
39 {
40 }
41
42 TextureMapperLayerClientQt::~TextureMapperLayerClientQt()
43 {
44     m_rootTextureMapperLayer = 0;
45 }
46
47 void TextureMapperLayerClientQt::syncRootLayer()
48 {
49     m_rootGraphicsLayer->flushCompositingStateForThisLayerOnly();
50 }
51
52 void TextureMapperLayerClientQt::markForSync(bool scheduleSync)
53 {
54     if (m_syncTimer.isActive())
55         return;
56     m_syncTimer.startOneShot(0);
57 }
58
59 TextureMapperLayer* TextureMapperLayerClientQt::rootLayer()
60 {
61     return toTextureMapperLayer(m_rootGraphicsLayer.get());
62 }
63
64 void TextureMapperLayerClientQt::setRootGraphicsLayer(GraphicsLayer* layer)
65 {
66     if (layer) {
67         m_rootGraphicsLayer = GraphicsLayer::create(0);
68         m_rootTextureMapperLayer = toTextureMapperLayer(m_rootGraphicsLayer.get());
69         m_rootGraphicsLayer->addChild(layer);
70         m_rootGraphicsLayer->setDrawsContent(false);
71         m_rootGraphicsLayer->setMasksToBounds(false);
72         m_rootGraphicsLayer->setSize(IntSize(1, 1));
73         TextureMapper::AccelerationMode mode = TextureMapper::SoftwareMode;
74         if (m_frame->pageAdapter->client->makeOpenGLContextCurrentIfAvailable())
75             mode = TextureMapper::OpenGLMode;
76         m_textureMapper = TextureMapper::create(mode);
77         m_rootTextureMapperLayer->setTextureMapper(m_textureMapper.get());
78         syncRootLayer();
79     } else {
80         m_rootGraphicsLayer.clear();
81         m_rootTextureMapperLayer = 0;
82     }
83 }
84
85 void TextureMapperLayerClientQt::syncLayers(Timer<TextureMapperLayerClientQt>*)
86 {
87     if (m_rootGraphicsLayer)
88         syncRootLayer();
89
90     m_frame->frame->view()->flushCompositingStateIncludingSubframes();
91
92     if (!m_rootGraphicsLayer)
93         return;
94
95     if (rootLayer()->descendantsOrSelfHaveRunningAnimations() && !m_syncTimer.isActive())
96         m_syncTimer.startOneShot(1.0 / 60.0);
97
98     m_frame->pageAdapter->client->repaintViewport();
99 }
100
101 void TextureMapperLayerClientQt::renderCompositedLayers(GraphicsContext* context, const IntRect& clip)
102 {
103     if (!m_rootTextureMapperLayer || !m_textureMapper)
104         return;
105
106     m_textureMapper->setGraphicsContext(context);
107     m_textureMapper->setImageInterpolationQuality(context->imageInterpolationQuality());
108     m_textureMapper->setTextDrawingMode(context->textDrawingMode());
109     QPainter* painter = context->platformContext();
110     const QTransform transform = painter->worldTransform();
111     const TransformationMatrix matrix(
112         transform.m11(), transform.m12(), 0, transform.m13(),
113         transform.m21(), transform.m22(), 0, transform.m23(),
114         0, 0, 1, 0,
115         transform.m31(), transform.m32(), 0, transform.m33()
116         );
117     if (m_rootGraphicsLayer->opacity() != painter->opacity() || m_rootGraphicsLayer->transform() != matrix) {
118         m_rootGraphicsLayer->setOpacity(painter->opacity());
119         m_rootGraphicsLayer->setTransform(matrix);
120         m_rootGraphicsLayer->flushCompositingStateForThisLayerOnly();
121     }
122     m_textureMapper->beginPainting();
123     m_textureMapper->beginClip(matrix, clip);
124     m_rootTextureMapperLayer->paint();
125     m_textureMapper->endClip();
126     m_textureMapper->endPainting();
127 }
128
129 #endif