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 <SettingHandle.h>
22 #include <SimpleMovingAverage.h>
23 #include <shared/RateCounter.h>
25 #include <gpu/Batch.h>
27 namespace gpu {
namespace gl {
31 class RefreshRateController;
33 class OpenGLDisplayPlugin :
public DisplayPlugin {
35 Q_PROPERTY(
float hudAlpha MEMBER _hudAlpha)
36 using Parent = DisplayPlugin;
39 using Mutex = std::mutex;
40 using Lock = std::unique_lock<Mutex>;
41 using Condition = std::condition_variable;
44 ~OpenGLDisplayPlugin();
48 static std::function<void(
int)> getRefreshRateOperator();
50 bool activate() override final;
51 void deactivate() override final;
52 bool startStandBySession() override final;
53 void endSession() override final;
54 bool eventFilter(QObject* receiver, QEvent* event) override;
55 bool isDisplayVisible()
const override {
return true; }
56 bool isSupported()
const override;
57 void captureFrame(
const std::string& outputName)
const override;
58 void submitFrame(
const gpu::FramePointer& newFrame)
override;
60 glm::uvec2 getRecommendedRenderSize()
const override {
return getSurfacePixels(); }
62 glm::uvec2 getRecommendedUiSize()
const override {
return getSurfaceSize(); }
64 virtual bool setDisplayTexture(
const QString& name)
override;
65 virtual bool onDisplayTextureReset() {
return false; };
67 float presentRate()
const override;
69 void resetPresentRate()
override;
71 float newFramePresentRate()
const override;
73 float droppedFrameRate()
const override;
75 float renderRate()
const override;
77 bool beginFrameRender(uint32_t frameIndex)
override;
79 virtual bool wantVsync()
const {
return true; }
80 void setVsyncEnabled(
bool vsyncEnabled) { _vsyncEnabled = vsyncEnabled; }
81 bool isVsyncEnabled()
const {
return _vsyncEnabled; }
84 int getRequiredThreadCount()
const override {
return 1; }
86 virtual std::function<void(gpu::Batch&,
const gpu::TexturePointer&)> getHUDOperator()
override;
87 void copyTextureToQuickFramebuffer(NetworkTexturePointer source,
88 QOpenGLFramebufferObject* target,
89 GLsync* fenceSync)
override;
91 static void setExtraLinearToSRGBConversion(
bool value) { _extraLinearToSRGBConversionSetting.set(value); }
92 static bool getExtraLinearToSRGBConversion() {
return _extraLinearToSRGBConversionSetting.get(); }
95 friend class OpenGLPresentThread;
97 glm::uvec2 getSurfaceSize()
const;
98 glm::uvec2 getSurfacePixels()
const;
103 virtual bool alwaysPresent()
const {
return false; }
105 void updateCompositeFramebuffer();
107 virtual QThread::Priority getPresentPriority() {
return QThread::HighPriority; }
108 virtual void compositeLayers();
109 virtual void setupCompositeScenePipeline(gpu::Batch& batch);
110 virtual void compositeScene();
111 virtual void compositePointer();
112 virtual void compositeExtra(){};
115 virtual void customizeContext();
116 virtual void uncustomizeContext();
119 virtual bool internalActivate() {
return true; }
120 virtual void internalDeactivate() {}
123 virtual bool activateStandBySession() {
return true; }
124 virtual void deactivateSession() {}
127 virtual void internalPresent();
129 void renderFromTexture(gpu::Batch& batch,
130 const gpu::TexturePointer& texture,
131 const glm::ivec4& viewport,
132 const glm::ivec4& scissor,
133 const gpu::FramebufferPointer& fbo);
134 void renderFromTexture(gpu::Batch& batch,
135 const gpu::TexturePointer& texture,
136 const glm::ivec4& viewport,
137 const glm::ivec4& scissor);
138 virtual void updateFrameData();
139 virtual glm::mat4 getViewCorrection() {
return glm::mat4(); }
141 void withOtherThreadContext(std::function<
void()> f)
const;
143 void present(
const std::shared_ptr<RefreshRateController>& refreshRateController);
144 virtual void swapBuffers();
146 void render(std::function<
void(gpu::Batch& batch)> f);
148 bool _vsyncEnabled{
true };
149 QThread* _presentThread{
nullptr };
150 std::queue<gpu::FramePointer> _newFrameQueue;
151 RateCounter<200> _droppedFrameRate;
152 RateCounter<200> _newFrameRate;
153 RateCounter<200> _presentRate;
154 RateCounter<200> _renderRate;
156 gpu::FramePointer _currentFrame;
157 gpu::Frame* _lastFrame{
nullptr };
158 gpu::FramebufferPointer _compositeFramebuffer;
159 gpu::PipelinePointer _hudPipeline;
160 gpu::PipelinePointer _mirrorHUDPipeline;
161 gpu::ShaderPointer _mirrorHUDPS;
162 gpu::PipelinePointer _drawTexturePipeline;
163 gpu::PipelinePointer _drawTextureSqueezePipeline;
164 gpu::PipelinePointer _linearToSRGBPipeline;
165 gpu::PipelinePointer _SRGBToLinearPipeline;
166 gpu::PipelinePointer _cursorPipeline;
167 gpu::TexturePointer _displayTexture{};
168 float _compositeHUDAlpha{ 1.0f };
170 virtual gpu::PipelinePointer getRenderTexturePipeline();
176 gpu::TexturePointer texture;
179 std::map<uint16_t, CursorData> _cursorsData;
180 bool _lockCurrentTexture{
false };
182 void assertNotPresentThread()
const;
183 void assertIsPresentThread()
const;
185 template <
typename F>
186 void withPresentThreadLock(F f)
const {
187 assertIsPresentThread();
188 Lock lock(_presentMutex);
192 template <
typename F>
193 void withNonPresentThreadLock(F f)
const {
194 assertNotPresentThread();
195 Lock lock(_presentMutex);
199 const gpu::BackendPointer& getBackend()
const;
203 mutable Mutex _presentMutex;
204 float _hudAlpha{ 1.0f };
206 QImage getScreenshot(
float aspectRatio);
207 QImage getSecondaryCameraScreenshot();
211 static bool _hasSetSRGBConversion;