1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 Juan José Casafranca |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt3D 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 "sendbuffercapturejob_p.h" |
41 | |
42 | #include <Qt3DRender/private/nodemanagers_p.h> |
43 | #include <Qt3DRender/private/job_common_p.h> |
44 | #include <Qt3DRender/private/buffer_p.h> |
45 | #include <Qt3DRender/private/buffermanager_p.h> |
46 | #include <Qt3DCore/private/qaspectmanager_p.h> |
47 | #include <Qt3DRender/private/qbuffer_p.h> |
48 | |
49 | QT_BEGIN_NAMESPACE |
50 | |
51 | namespace Qt3DRender { |
52 | |
53 | namespace Render { |
54 | |
55 | class SendBufferCaptureJobPrivate : public Qt3DCore::QAspectJobPrivate |
56 | { |
57 | public: |
58 | SendBufferCaptureJobPrivate() {} |
59 | ~SendBufferCaptureJobPrivate() {} |
60 | |
61 | void postFrame(Qt3DCore::QAspectManager *aspectManager) override; |
62 | |
63 | mutable QMutex m_mutex; |
64 | QVector<QPair<Qt3DCore::QNodeId, QByteArray>> m_buffersToCapture; |
65 | QVector<QPair<Qt3DCore::QNodeId, QByteArray>> m_buffersToNotify; |
66 | }; |
67 | |
68 | SendBufferCaptureJob::SendBufferCaptureJob() |
69 | : Qt3DCore::QAspectJob(*new SendBufferCaptureJobPrivate) |
70 | , m_nodeManagers(nullptr) |
71 | { |
72 | SET_JOB_RUN_STAT_TYPE(this, JobTypes::SendBufferCapture, 0) |
73 | } |
74 | |
75 | SendBufferCaptureJob::~SendBufferCaptureJob() |
76 | { |
77 | } |
78 | |
79 | // Called from SubmitRenderView while rendering |
80 | void SendBufferCaptureJob::addRequest(QPair<Qt3DCore::QNodeId, QByteArray> request) |
81 | { |
82 | Q_D(SendBufferCaptureJob); |
83 | QMutexLocker locker(&d->m_mutex); |
84 | d->m_buffersToCapture.push_back(t: request); |
85 | } |
86 | |
87 | // Called by aspect thread jobs to execute (we may still be rendering at this point) |
88 | bool SendBufferCaptureJob::hasRequests() const |
89 | { |
90 | Q_D(const SendBufferCaptureJob); |
91 | QMutexLocker locker(&d->m_mutex); |
92 | return d->m_buffersToCapture.size() > 0; |
93 | } |
94 | |
95 | void SendBufferCaptureJob::run() |
96 | { |
97 | Q_ASSERT(m_nodeManagers); |
98 | Q_D(SendBufferCaptureJob); |
99 | QMutexLocker locker(&d->m_mutex); |
100 | for (const QPair<Qt3DCore::QNodeId, QByteArray> &pendingCapture : qAsConst(t&: d->m_buffersToCapture)) { |
101 | Buffer *buffer = m_nodeManagers->bufferManager()->lookupResource(id: pendingCapture.first); |
102 | // Buffer might have been destroyed between the time addRequest is made and this job gets run |
103 | // If it exists however, it cannot be destroyed before this job is done running |
104 | if (buffer != nullptr) |
105 | buffer->updateDataFromGPUToCPU(data: pendingCapture.second); |
106 | } |
107 | d->m_buffersToNotify = std::move(d->m_buffersToCapture); |
108 | } |
109 | |
110 | void SendBufferCaptureJobPrivate::postFrame(Qt3DCore::QAspectManager *aspectManager) |
111 | { |
112 | QMutexLocker locker(&m_mutex); |
113 | const QVector<QPair<Qt3DCore::QNodeId, QByteArray>> pendingSendBufferCaptures = std::move(m_buffersToNotify); |
114 | for (const auto &bufferDataPair : pendingSendBufferCaptures) { |
115 | QBuffer *frontendBuffer = static_cast<decltype(frontendBuffer)>(aspectManager->lookupNode(id: bufferDataPair.first)); |
116 | if (!frontendBuffer) |
117 | continue; |
118 | QBufferPrivate *dFrontend = static_cast<decltype(dFrontend)>(Qt3DCore::QNodePrivate::get(q: frontendBuffer)); |
119 | // Calling frontendBuffer->setData would result in forcing a sync against the backend |
120 | // which isn't necessary |
121 | dFrontend->setData(bufferDataPair.second); |
122 | Q_EMIT frontendBuffer->dataAvailable(); |
123 | } |
124 | } |
125 | |
126 | } // Render |
127 | |
128 | } // Qt3DRender |
129 | |
130 | QT_END_NAMESPACE |
131 | |