src/platform/qt/DisplayGL.h (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#pragma once
7
8#if defined(BUILD_GL) || defined(BUILD_GLES2)
9
10#include "Display.h"
11
12#ifdef USE_EPOXY
13#include <epoxy/gl.h>
14#ifndef GLdouble
15#define GLdouble GLdouble
16#endif
17#endif
18
19#include <QElapsedTimer>
20#include <QOpenGLContext>
21#include <QList>
22#include <QMouseEvent>
23#include <QPainter>
24#include <QQueue>
25#include <QThread>
26#include <QTimer>
27
28#include <array>
29
30#include "VideoProxy.h"
31
32#include "platform/video-backend.h"
33
34class QOpenGLPaintDevice;
35
36namespace QGBA {
37
38class PainterGL;
39class DisplayGL : public Display {
40Q_OBJECT
41
42public:
43 DisplayGL(const QSurfaceFormat& format, QWidget* parent = nullptr);
44 ~DisplayGL();
45
46 void startDrawing(std::shared_ptr<CoreController>) override;
47 bool isDrawing() const override { return m_isDrawing; }
48 bool supportsShaders() const override;
49 VideoShader* shaders() override;
50 void setVideoProxy(std::shared_ptr<VideoProxy>) override;
51 int framebufferHandle() override;
52
53public slots:
54 void stopDrawing() override;
55 void pauseDrawing() override;
56 void unpauseDrawing() override;
57 void forceDraw() override;
58 void lockAspectRatio(bool lock) override;
59 void lockIntegerScaling(bool lock) override;
60 void interframeBlending(bool enable) override;
61 void showOSDMessages(bool enable) override;
62 void filter(bool filter) override;
63 void framePosted() override;
64 void setShaders(struct VDir*) override;
65 void clearShaders() override;
66 void resizeContext() override;
67 void setVideoScale(int scale) override;
68
69protected:
70 virtual void paintEvent(QPaintEvent*) override { forceDraw(); }
71 virtual void resizeEvent(QResizeEvent*) override;
72
73private:
74 void resizePainter();
75
76 bool m_isDrawing = false;
77 bool m_hasStarted = false;
78 std::unique_ptr<PainterGL> m_painter;
79 QThread* m_drawThread = nullptr;
80 std::shared_ptr<CoreController> m_context;
81};
82
83class PainterGL : public QObject {
84Q_OBJECT
85
86public:
87 PainterGL(QWindow* surface, const QSurfaceFormat& format);
88 ~PainterGL();
89
90 void setContext(std::shared_ptr<CoreController>);
91 void setMessagePainter(MessagePainter*);
92 void enqueue(const uint32_t* backing);
93
94 bool supportsShaders() const { return m_supportsShaders; }
95
96 void setVideoProxy(std::shared_ptr<VideoProxy>);
97
98public slots:
99 void forceDraw();
100 void draw();
101 void start();
102 void stop();
103 void pause();
104 void unpause();
105 void resize(const QSize& size);
106 void lockAspectRatio(bool lock);
107 void lockIntegerScaling(bool lock);
108 void interframeBlending(bool enable);
109 void showOSD(bool enable);
110 void filter(bool filter);
111 void resizeContext();
112
113 void setShaders(struct VDir*);
114 void clearShaders();
115 VideoShader* shaders();
116
117 int glTex();
118
119signals:
120 void started();
121
122private:
123 void makeCurrent();
124 void performDraw();
125 void dequeue();
126 void dequeueAll();
127 void create();
128 void destroy();
129
130 std::array<std::array<uint32_t, 0x100000>, 3> m_buffers;
131 QList<uint32_t*> m_free;
132 QQueue<uint32_t*> m_queue;
133 uint32_t* m_buffer;
134 QPainter m_painter;
135 QMutex m_mutex;
136 QWindow* m_surface;
137 QSurfaceFormat m_format;
138 std::unique_ptr<QOpenGLPaintDevice> m_window;
139 std::unique_ptr<QOpenGLContext> m_gl;
140 bool m_active = false;
141 bool m_started = false;
142 std::shared_ptr<CoreController> m_context = nullptr;
143 bool m_supportsShaders;
144 bool m_showOSD;
145 VideoShader m_shader{};
146 VideoBackend* m_backend = nullptr;
147 QSize m_size;
148 MessagePainter* m_messagePainter = nullptr;
149 QElapsedTimer m_delayTimer;
150 std::shared_ptr<VideoProxy> m_videoProxy;
151};
152
153}
154
155#endif