Overte C++ Documentation
SharedObject.h
1 //
2 // Created by Bradley Austin Davis on 2018-01-04
3 // Copyright 2013-2018 High Fidelity, Inc.
4 //
5 // Distributed under the Apache License, Version 2.0.
6 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
7 //
8 #pragma once
9 
10 #include <QtCore/QObject>
11 #include <QtCore/QThread>
12 #include <QtCore/QWaitCondition>
13 #include <QtCore/QMutex>
14 #include <QtCore/QSize>
15 
16 #include "TextureCache.h"
17 
18 class QWindow;
19 class QTimer;
20 class QQuickWindow;
21 class QQuickItem;
22 class QOpenGLContext;
23 class QQmlEngine;
24 class QQmlContext;
25 
26 namespace hifi { namespace qml {
27 
28 class OffscreenSurface;
29 
30 namespace impl {
31 
32 class RenderControl;
33 class RenderEventHandler;
34 
35 class SharedObject : public QObject {
36  Q_OBJECT
37 
38  friend class RenderEventHandler;
39 
40 public:
41  static void setSharedContext(QOpenGLContext* context);
42  static QOpenGLContext* getSharedContext();
43  static TextureCache& getTextureCache();
44 
45  SharedObject();
46  virtual ~SharedObject();
47 
48  void create(OffscreenSurface* surface);
49  void setRootItem(QQuickItem* rootItem);
50  void destroy();
51  bool isQuit() const;
52 
53  QSize getSize() const;
54  void setSize(const QSize& size);
55  void setMaxFps(uint8_t maxFps);
56 
57  QQuickWindow* getWindow() { return _quickWindow; }
58  QQuickItem* getRootItem() { return _rootItem; }
59  QQmlContext* getContext() { return _qmlContext; }
60  void setProxyWindow(QWindow* window);
61 
62  void pause();
63  void resume();
64  bool isPaused() const;
65  bool fetchTexture(TextureAndFence& textureAndFence);
66  void addToDeletionList(QObject* object);
67 
68 private:
69  bool event(QEvent* e) override;
70 
71  bool preRender(bool sceneGraphSync);
72  void shutdownRendering(const QSize& size);
73  // Called by the render event handler, from the render thread
74  void initializeRenderControl(QOpenGLContext* context);
75  void releaseTextureAndFence();
76  void setRenderTarget(uint32_t fbo, const QSize& size);
77 
78  QQmlEngine* acquireEngine(OffscreenSurface* surface);
79  void releaseEngine(QQmlEngine* engine);
80 
81  void requestRender();
82  void requestRenderSync();
83  void wait();
84  void wake();
85  void onInitialize();
86  void onRender();
87  void onTimer();
88  void onAboutToQuit();
89  void updateTextureAndFence(const TextureAndFence& newTextureAndFence);
90 
91  QList<QPointer<QObject>> _deletionList;
92 
93  // Texture management
94  TextureAndFence _latestTextureAndFence { 0, 0 };
95  QQuickItem* _rootItem { nullptr };
96  QQuickWindow* _quickWindow { nullptr };
97  QQmlContext* _qmlContext { nullptr };
98  mutable QMutex _mutex;
99  QWaitCondition _cond;
100 
101 #ifndef DISABLE_QML
102  QWindow* _proxyWindow { nullptr };
103  RenderControl* _renderControl { nullptr };
104  RenderEventHandler* _renderObject { nullptr };
105 
106  QTimer* _renderTimer { nullptr };
107  QThread* _renderThread { nullptr };
108 #endif
109 
110  uint64_t _lastRenderTime { 0 };
111  QSize _size { 100, 100 };
112  uint8_t _maxFps { 60 };
113 
114  bool _renderRequested { false };
115  bool _syncRequested { false };
116  bool _quit { false };
117  bool _paused { false };
118 };
119 
120 } // namespace impl
121 }} // namespace hifi::qml