10 #include "DisplayPlugin.h"
12 #include <condition_variable>
16 #include <QtCore/QThread>
17 #include <QtCore/QTimer>
18 #include <QtGui/QImage>
20 #include <GLMHelpers.h>
21 #include <SimpleMovingAverage.h>
22 #include <shared/RateCounter.h>
24 #include <gpu/Batch.h>
26 namespace gpu {
namespace gl {
30 class RefreshRateController;
32 class OpenGLDisplayPlugin :
public DisplayPlugin {
34 Q_PROPERTY(
float hudAlpha MEMBER _hudAlpha)
35 using Parent = DisplayPlugin;
38 using Mutex = std::mutex;
39 using Lock = std::unique_lock<Mutex>;
40 using Condition = std::condition_variable;
43 ~OpenGLDisplayPlugin();
47 static std::function<void(
int)> getRefreshRateOperator();
49 bool activate() override final;
50 void deactivate() override final;
51 bool startStandBySession() override final;
52 void endSession() override final;
53 bool eventFilter(QObject* receiver, QEvent* event) override;
54 bool isDisplayVisible()
const override {
return true; }
55 void captureFrame(
const std::string& outputName)
const override;
56 void submitFrame(
const gpu::FramePointer& newFrame)
override;
58 glm::uvec2 getRecommendedRenderSize()
const override {
return getSurfacePixels(); }
60 glm::uvec2 getRecommendedUiSize()
const override {
return getSurfaceSize(); }
62 virtual bool setDisplayTexture(
const QString& name)
override;
63 virtual bool onDisplayTextureReset() {
return false; };
65 float presentRate()
const override;
67 void resetPresentRate()
override;
69 float newFramePresentRate()
const override;
71 float droppedFrameRate()
const override;
73 float renderRate()
const override;
75 bool beginFrameRender(uint32_t frameIndex)
override;
77 virtual bool wantVsync()
const {
return true; }
78 void setVsyncEnabled(
bool vsyncEnabled) { _vsyncEnabled = vsyncEnabled; }
79 bool isVsyncEnabled()
const {
return _vsyncEnabled; }
82 int getRequiredThreadCount()
const override {
return 1; }
84 virtual std::function<void(gpu::Batch&,
const gpu::TexturePointer&)> getHUDOperator()
override;
85 void copyTextureToQuickFramebuffer(NetworkTexturePointer source,
86 QOpenGLFramebufferObject* target,
87 GLsync* fenceSync)
override;
90 friend class PresentThread;
92 glm::uvec2 getSurfaceSize()
const;
93 glm::uvec2 getSurfacePixels()
const;
98 virtual bool alwaysPresent()
const {
return false; }
100 void updateCompositeFramebuffer();
102 virtual QThread::Priority getPresentPriority() {
return QThread::HighPriority; }
103 virtual void compositeLayers();
104 virtual void setupCompositeScenePipeline(gpu::Batch& batch);
105 virtual void compositeScene();
106 virtual void compositePointer();
107 virtual void compositeExtra(){};
110 virtual void customizeContext();
111 virtual void uncustomizeContext();
114 virtual bool internalActivate() {
return true; }
115 virtual void internalDeactivate() {}
118 virtual bool activateStandBySession() {
return true; }
119 virtual void deactivateSession() {}
122 virtual void internalPresent();
124 void renderFromTexture(gpu::Batch& batch,
125 const gpu::TexturePointer& texture,
126 const glm::ivec4& viewport,
127 const glm::ivec4& scissor,
128 const gpu::FramebufferPointer& fbo);
129 void renderFromTexture(gpu::Batch& batch,
130 const gpu::TexturePointer& texture,
131 const glm::ivec4& viewport,
132 const glm::ivec4& scissor);
133 virtual void updateFrameData();
134 virtual glm::mat4 getViewCorrection() {
return glm::mat4(); }
136 void withOtherThreadContext(std::function<
void()> f)
const;
138 void present(
const std::shared_ptr<RefreshRateController>& refreshRateController);
139 virtual void swapBuffers();
141 void render(std::function<
void(gpu::Batch& batch)> f);
143 bool _vsyncEnabled{
true };
144 QThread* _presentThread{
nullptr };
145 std::queue<gpu::FramePointer> _newFrameQueue;
146 RateCounter<200> _droppedFrameRate;
147 RateCounter<200> _newFrameRate;
148 RateCounter<200> _presentRate;
149 RateCounter<200> _renderRate;
151 gpu::FramePointer _currentFrame;
152 gpu::Frame* _lastFrame{
nullptr };
153 mat4 _prevRenderView;
154 gpu::FramebufferPointer _compositeFramebuffer;
155 gpu::PipelinePointer _hudPipeline;
156 gpu::PipelinePointer _mirrorHUDPipeline;
157 gpu::ShaderPointer _mirrorHUDPS;
158 gpu::PipelinePointer _drawTexturePipeline;
159 gpu::PipelinePointer _drawTextureSqueezePipeline;
160 gpu::PipelinePointer _linearToSRGBPipeline;
161 gpu::PipelinePointer _SRGBToLinearPipeline;
162 gpu::PipelinePointer _cursorPipeline;
163 gpu::TexturePointer _displayTexture{};
164 float _compositeHUDAlpha{ 1.0f };
166 virtual gpu::PipelinePointer getRenderTexturePipeline();
172 gpu::TexturePointer texture;
175 std::map<uint16_t, CursorData> _cursorsData;
176 bool _lockCurrentTexture{
false };
178 void assertNotPresentThread()
const;
179 void assertIsPresentThread()
const;
181 template <
typename F>
182 void withPresentThreadLock(F f)
const {
183 assertIsPresentThread();
184 Lock lock(_presentMutex);
188 template <
typename F>
189 void withNonPresentThreadLock(F f)
const {
190 assertNotPresentThread();
191 Lock lock(_presentMutex);
195 gpu::gl::GLBackend* getGLBackend();
199 mutable Mutex _presentMutex;
200 float _hudAlpha{ 1.0f };
202 QImage getScreenshot(
float aspectRatio);
203 QImage getSecondaryCameraScreenshot();