summaryrefslogtreecommitdiffstats
path: root/src/Runtime/ogl-runtime/src/runtimerender/resourcemanager/Qt3DSRenderBufferLoader.cpp
blob: 963186ff1aff328443041831967bf1e3f4dadb64 (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/****************************************************************************
**
** Copyright (C) 2008-2012 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "Qt3DSRenderBufferLoader.h"
#include "foundation/Qt3DSInvasiveLinkedList.h"
#include "foundation/Qt3DSMutex.h"
#include "foundation/Qt3DSFoundation.h"
#include "foundation/Qt3DSBroadcastingAllocator.h"
#include "foundation/Qt3DSAtomic.h"
#include "foundation/Qt3DSSync.h"
#include "Qt3DSRenderInputStreamFactory.h"
#include "Qt3DSRenderThreadPool.h"

using namespace qt3ds::render;

namespace {
struct SBufferLoader;
struct SBufferLoadResult : public ILoadedBuffer
{
    NVFoundationBase &m_Foundation;
    CRegisteredString m_Path;
    IBufferLoaderCallback *m_UserData;
    NVDataRef<QT3DSU8> m_Data;
    QT3DSI32 mRefCount;

    SBufferLoadResult(NVFoundationBase &fnd, CRegisteredString p, IBufferLoaderCallback *ud,
                      NVDataRef<QT3DSU8> data)
        : m_Foundation(fnd)
        , m_Path(p)
        , m_UserData(ud)
        , m_Data(data)
        , mRefCount(0)
    {
    }

    QT3DS_IMPLEMENT_REF_COUNT_ADDREF_RELEASE_OVERRIDE(m_Foundation.getAllocator())

    CRegisteredString Path() override { return m_Path; }
    // Data is released when the buffer itself is released.
    NVDataRef<QT3DSU8> Data() override { return m_Data; }
    IBufferLoaderCallback *UserData() override { return m_UserData; }

    static SBufferLoadResult *Allocate(QT3DSU32 inBufferSize, NVFoundationBase &fnd,
                                       CRegisteredString p,
                                       NVScopedRefCounted<IBufferLoaderCallback> ud)
    {
        size_t allocSize = sizeof(SBufferLoadResult) + inBufferSize;
        QT3DSU8 *allocMem =
            (QT3DSU8 *)fnd.getAllocator().allocate(allocSize, "ILoadedBuffer", __FILE__, __LINE__);
        if (allocMem == NULL)
            return NULL;
        QT3DSU8 *bufferStart = allocMem + sizeof(SBufferLoadResult);
        NVDataRef<QT3DSU8> dataBuffer = toDataRef(bufferStart, inBufferSize);
        return new (allocMem) SBufferLoadResult(fnd, p, ud, dataBuffer);
    }
};
struct SLoadedBufferImpl
{
    SBufferLoader &m_Loader;
    QT3DSU64 m_JobId;
    QT3DSU64 m_LoadId;
    CRegisteredString m_Path;
    NVScopedRefCounted<IBufferLoaderCallback> m_UserData;
    bool m_Quiet;
    volatile bool m_Cancel;
    NVScopedRefCounted<SBufferLoadResult> m_Result;
    SLoadedBufferImpl *m_NextBuffer;
    SLoadedBufferImpl *m_PreviousBuffer;

    SLoadedBufferImpl(SBufferLoader &l, CRegisteredString inPath,
                      NVScopedRefCounted<IBufferLoaderCallback> ud, bool inQuiet, QT3DSU64 loadId)
        : m_Loader(l)
        , m_JobId(0)
        , m_LoadId(loadId)
        , m_Path(inPath)
        , m_UserData(ud)
        , m_Quiet(inQuiet)
        , m_Cancel(false)
        , m_NextBuffer(NULL)
        , m_PreviousBuffer(NULL)
    {
    }
};

DEFINE_INVASIVE_LIST(LoadedBufferImpl);
IMPLEMENT_INVASIVE_LIST(LoadedBufferImpl, m_PreviousBuffer, m_NextBuffer);

struct SBufferLoader : public IBufferLoader
{
    NVFoundationBase &m_Foundation;
    NVScopedRefCounted<IInputStreamFactory> m_Factory;
    NVScopedRefCounted<IThreadPool> m_ThreadPool;

    Mutex m_BuffersToLoadMutex;
    TLoadedBufferImplList m_BuffersToLoad;

    Mutex m_BuffersLoadingMutex;
    TLoadedBufferImplList m_BuffersLoading;

    Mutex m_LoadedBuffersMutex;
    TLoadedBufferImplList m_LoadedBuffers;

    Sync m_BufferLoadedEvent;

    QT3DSU64 m_NextBufferId;

    QT3DSI32 mRefCount;

    SBufferLoader(NVFoundationBase &fnd, IInputStreamFactory &fac, IThreadPool &tp)
        : m_Foundation(fnd)
        , m_Factory(fac)
        , m_ThreadPool(tp)
        , m_BuffersToLoadMutex(fnd.getAllocator())
        , m_BuffersLoadingMutex(fnd.getAllocator())
        , m_LoadedBuffersMutex(fnd.getAllocator())
        , m_BufferLoadedEvent(fnd.getAllocator())
        , m_NextBufferId(1)
        , mRefCount(0)
    {
        m_BufferLoadedEvent.reset();
    }

    virtual ~SBufferLoader()
    {
        {
            Mutex::ScopedLock __locker(m_BuffersToLoadMutex);
            for (TLoadedBufferImplList::iterator iter = m_BuffersToLoad.begin(),
                                                 end = m_BuffersToLoad.end();
                 iter != end; ++iter) {
                m_ThreadPool->CancelTask(iter->m_JobId);
            }
        }

        // Pull any remaining buffers out of the thread system.
        while (WillLoadedBuffersBeAvailable()) {
            NextLoadedBuffer();
        }
    }

    QT3DS_IMPLEMENT_REF_COUNT_ADDREF_RELEASE(m_Foundation.getAllocator())

    static void InitializeActiveLoadingBuffer(SLoadedBufferImpl &theBuffer)
    {
        Mutex::ScopedLock theLocker(theBuffer.m_Loader.m_BuffersToLoadMutex);
        Mutex::ScopedLock theSecondLocker(theBuffer.m_Loader.m_BuffersLoadingMutex);
        theBuffer.m_Loader.m_BuffersToLoad.remove(theBuffer);
        theBuffer.m_Loader.m_BuffersLoading.push_back(theBuffer);
    }

    static void SetBufferAsLoaded(SLoadedBufferImpl &theBuffer)
    {
        Mutex::ScopedLock theSecondLocker(theBuffer.m_Loader.m_BuffersLoadingMutex);
        Mutex::ScopedLock theLocker(theBuffer.m_Loader.m_LoadedBuffersMutex);
        theBuffer.m_Loader.m_BuffersLoading.remove(theBuffer);
        theBuffer.m_Loader.m_LoadedBuffers.push_back(theBuffer);
        theBuffer.m_Loader.m_BufferLoadedEvent.set();
        theBuffer.m_Loader.m_BufferLoadedEvent.reset();
    }

    static void LoadNextBuffer(void *loader)
    {
        SLoadedBufferImpl &theBuffer = *reinterpret_cast<SLoadedBufferImpl *>(loader);

        InitializeActiveLoadingBuffer(theBuffer);
        NVScopedRefCounted<IRefCountedInputStream> theStream =
            theBuffer.m_Loader.m_Factory->GetStreamForFile(theBuffer.m_Path.c_str(),
                                                           theBuffer.m_Quiet);
        if (theStream && theBuffer.m_Cancel == false) {
            theStream->SetPosition(0, SeekPosition::End);
            QT3DSI64 theFileLen = theStream->GetPosition();
            if (theFileLen > 0 && theFileLen < (QT3DSU32)QT3DS_MAX_U32) {
                QT3DSU32 required = (QT3DSU32)theFileLen;
                theBuffer.m_Result =
                    SBufferLoadResult::Allocate(required, theBuffer.m_Loader.m_Foundation,
                                                theBuffer.m_Path, theBuffer.m_UserData);
                QT3DSU32 amountRead = 0;
                QT3DSU32 total = amountRead;
                if (theBuffer.m_Result && theBuffer.m_Cancel == false) {
                    NVDataRef<QT3DSU8> theDataBuffer(theBuffer.m_Result->m_Data);
                    theStream->SetPosition(0, SeekPosition::Begin);
                    amountRead = theStream->Read(theDataBuffer);
                    total += amountRead;
                    // Ensure we keep trying, not all file systems allow huge reads.
                    while (total < required && amountRead > 0 && theBuffer.m_Cancel == false) {
                        NVDataRef<QT3DSU8> newBuffer(theDataBuffer.mData + total, required - total);
                        amountRead = theStream->Read(newBuffer);
                        total += amountRead;
                    }
                }
                if (theBuffer.m_Cancel || total != required) {
                    theBuffer.m_Result->m_Data = NVDataRef<QT3DSU8>();
                }
            }
        }

        // only callback if the file was successfully loaded.
        if (theBuffer.m_UserData) {
            if (theBuffer.m_Cancel == false && theBuffer.m_Result.mPtr
                && theBuffer.m_Result->m_Data.size()) {
                theBuffer.m_UserData->OnBufferLoaded(*theBuffer.m_Result.mPtr);
            } else {
                if (theBuffer.m_Cancel)
                    theBuffer.m_UserData->OnBufferLoadCancelled(theBuffer.m_Path);
                else
                    theBuffer.m_UserData->OnBufferLoadFailed(theBuffer.m_Path);
            }
        }

        SetBufferAsLoaded(theBuffer);
    }
    static void CancelNextBuffer(void *loader)
    {
        SLoadedBufferImpl &theBuffer = *reinterpret_cast<SLoadedBufferImpl *>(loader);
        theBuffer.m_Cancel = true;
        InitializeActiveLoadingBuffer(theBuffer);

        if (theBuffer.m_UserData)
            theBuffer.m_UserData->OnBufferLoadCancelled(theBuffer.m_Path);

        SetBufferAsLoaded(theBuffer);
    }

    // nonblocking.  Quiet failure is passed to the input stream factory.
    QT3DSU64 QueueForLoading(CRegisteredString inPath,
                                  IBufferLoaderCallback *inUserData = NULL,
                                  bool inQuietFailure = false) override
    {
        SLoadedBufferImpl &theBuffer = *QT3DS_NEW(m_Foundation.getAllocator(), SLoadedBufferImpl)(
            *this, inPath, inUserData, inQuietFailure, m_NextBufferId);
        ++m_NextBufferId;
        {
            Mutex::ScopedLock theLocker(m_BuffersToLoadMutex);
            m_BuffersToLoad.push_back(theBuffer);
        }
        theBuffer.m_JobId = m_ThreadPool->AddTask(&theBuffer, LoadNextBuffer, CancelNextBuffer);
        return theBuffer.m_LoadId;
    }

    void CancelBufferLoad(QT3DSU64 inBufferId) override
    {
        {
            Mutex::ScopedLock theLocker(m_BuffersToLoadMutex);
            SLoadedBufferImpl *theLoadedBuffer = NULL;
            for (TLoadedBufferImplList::iterator iter = m_BuffersToLoad.begin(),
                                                 end = m_BuffersToLoad.end();
                 iter != end && theLoadedBuffer == NULL; ++iter) {
                if (iter->m_LoadId == inBufferId) {
                    theLoadedBuffer = &(*iter);
                    // both cancellation attempts are necessary.  The user will still get
                    // a load result, it will just have no data.
                    theLoadedBuffer->m_Cancel = true;
                    m_ThreadPool->CancelTask(theLoadedBuffer->m_JobId);
                }
            }
        }
    }

    // If we were will to wait, will we ever get another buffer
    bool WillLoadedBuffersBeAvailable() override
    {
        Mutex::ScopedLock theLocker(m_BuffersToLoadMutex);
        Mutex::ScopedLock theSecondLocker(m_BuffersLoadingMutex);
        return AreLoadedBuffersAvailable() || m_BuffersToLoad.empty() == false
            || m_BuffersLoading.empty() == false;
    }
    // Will nextLoadedBuffer block or not?
    bool AreLoadedBuffersAvailable() override
    {
        Mutex::ScopedLock theLocker(m_LoadedBuffersMutex);
        return m_LoadedBuffers.empty() == false;
    }

    // blocking, be careful with this.  No order guarantees here.
    NVScopedRefCounted<ILoadedBuffer> NextLoadedBuffer() override
    {
        while (!AreLoadedBuffersAvailable()) {
            m_BufferLoadedEvent.wait();
        }
        SLoadedBufferImpl *theBuffer;
        {
            Mutex::ScopedLock theLocker(m_LoadedBuffersMutex);
            theBuffer = m_LoadedBuffers.back_ptr();
            m_LoadedBuffers.remove(*theBuffer);
        }
        NVScopedRefCounted<ILoadedBuffer> retval(theBuffer->m_Result);
        if (retval.mPtr == NULL) {
            retval = SBufferLoadResult::Allocate(0, m_Foundation, theBuffer->m_Path,
                                                 theBuffer->m_UserData);
        }
        NVDelete(m_Foundation.getAllocator(), theBuffer);
        return retval;
    }
};
}

IBufferLoader &IBufferLoader::Create(NVFoundationBase &fnd, IInputStreamFactory &inFactory,
                                     IThreadPool &inThreadPool)
{
    return *QT3DS_NEW(fnd.getAllocator(), SBufferLoader)(fnd, inFactory, inThreadPool);
}