1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtWebEngine module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "display_gl_output_surface.h"
41 
42 #include "compositor_resource_fence.h"
43 #include "render_widget_host_view_qt_delegate.h"
44 #include "type_conversion.h"
45 
46 #include <QOpenGLFunctions>
47 #include <QSGImageNode>
48 #include <QSGTexture>
49 
50 namespace QtWebEngineCore {
51 
52 class DisplayGLOutputSurface::Texture final : public QSGTexture
53 {
54 public:
Texture(uint32_t id,QSize sizeInPixels,bool hasAlphaChannel,scoped_refptr<CompositorResourceFence> fence)55     Texture(uint32_t id, QSize sizeInPixels, bool hasAlphaChannel, scoped_refptr<CompositorResourceFence> fence)
56         : m_id(id)
57         , m_sizeInPixels(sizeInPixels)
58         , m_hasAlphaChannel(hasAlphaChannel)
59         , m_fence(std::move(fence))
60     {
61     }
62 
63     // QSGTexture:
textureId() const64     int textureId() const override { return m_id; }
textureSize() const65     QSize textureSize() const override { return m_sizeInPixels; }
hasAlphaChannel() const66     bool hasAlphaChannel() const override { return m_hasAlphaChannel; }
hasMipmaps() const67     bool hasMipmaps() const override { return false; }
bind()68     void bind() override
69     {
70         if (m_fence) {
71             m_fence->wait();
72             m_fence.reset();
73         }
74 
75         QOpenGLContext *context = QOpenGLContext::currentContext();
76         QOpenGLFunctions *funcs = context->functions();
77         funcs->glBindTexture(GL_TEXTURE_2D, m_id);
78     }
79 
80 private:
81     uint32_t m_id;
82     QSize m_sizeInPixels;
83     bool m_hasAlphaChannel;
84     scoped_refptr<CompositorResourceFence> m_fence;
85 };
86 
updatePaintNode(QSGNode * oldNode,RenderWidgetHostViewQtDelegate * delegate)87 QSGNode *DisplayGLOutputSurface::updatePaintNode(QSGNode *oldNode, RenderWidgetHostViewQtDelegate *delegate)
88 {
89     {
90         QMutexLocker locker(&m_mutex);
91         if (m_readyToUpdate) {
92             std::swap(m_middleBuffer, m_frontBuffer);
93             m_taskRunner->PostTask(
94                     FROM_HERE,
95                     base::BindOnce(&DisplayGLOutputSurface::swapBuffersOnVizThread, base::Unretained(this)));
96             m_taskRunner.reset();
97             m_readyToUpdate = false;
98         }
99     }
100 
101     if (!m_frontBuffer)
102         return oldNode;
103 
104     auto node = static_cast<QSGImageNode *>(oldNode);
105     if (!node)
106         node = delegate->createImageNode();
107 
108     QSize sizeInPixels = toQt(m_frontBuffer->shape.sizeInPixels);
109     QSizeF sizeInDips = QSizeF(sizeInPixels) / m_frontBuffer->shape.devicePixelRatio;
110     QRectF rectInDips(QPointF(0, 0), sizeInDips);
111     node->setRect(rectInDips);
112     node->setOwnsTexture(true);
113     node->setTexture(new Texture(m_frontBuffer->serviceId,
114                                  sizeInPixels,
115                                  m_frontBuffer->shape.hasAlpha,
116                                  m_frontBuffer->fence));
117     node->setTextureCoordinatesTransform(QSGImageNode::MirrorVertically);
118 
119     return node;
120 }
121 
122 } // namespace QtWebEngineCore
123