all repos — mgba @ 2f7232292c79ddaefaef9db3b950b40720b3d6e2

mGBA Game Boy Advance Emulator

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 <QAtomicInt>
 20#include <QElapsedTimer>
 21#include <QHash>
 22#include <QList>
 23#include <QMouseEvent>
 24#include <QOpenGLContext>
 25#include <QPainter>
 26#include <QQueue>
 27#include <QThread>
 28#include <QTimer>
 29
 30#include <array>
 31
 32#include "VideoProxy.h"
 33
 34#include "platform/video-backend.h"
 35
 36class QOpenGLPaintDevice;
 37
 38uint qHash(const QSurfaceFormat&, uint seed = 0);
 39
 40namespace QGBA {
 41
 42class PainterGL;
 43class DisplayGL : public Display {
 44Q_OBJECT
 45
 46public:
 47	DisplayGL(const QSurfaceFormat& format, QWidget* parent = nullptr);
 48	~DisplayGL();
 49
 50	void startDrawing(std::shared_ptr<CoreController>) override;
 51	bool isDrawing() const override { return m_isDrawing; }
 52	bool supportsShaders() const override;
 53	VideoShader* shaders() override;
 54	void setVideoProxy(std::shared_ptr<VideoProxy>) override;
 55	int framebufferHandle() override;
 56
 57	static bool supportsFormat(const QSurfaceFormat&);
 58
 59public slots:
 60	void stopDrawing() override;
 61	void pauseDrawing() override;
 62	void unpauseDrawing() override;
 63	void forceDraw() override;
 64	void lockAspectRatio(bool lock) override;
 65	void lockIntegerScaling(bool lock) override;
 66	void interframeBlending(bool enable) override;
 67	void showOSDMessages(bool enable) override;
 68	void filter(bool filter) override;
 69	void framePosted() override;
 70	void setShaders(struct VDir*) override;
 71	void clearShaders() override;
 72	void resizeContext() override;
 73	void setVideoScale(int scale) override;
 74
 75protected:
 76	virtual void paintEvent(QPaintEvent*) override { forceDraw(); }
 77	virtual void resizeEvent(QResizeEvent*) override;
 78
 79private:
 80	void resizePainter();
 81
 82	static QHash<QSurfaceFormat, bool> s_supports;
 83
 84	bool m_isDrawing = false;
 85	bool m_hasStarted = false;
 86	std::unique_ptr<PainterGL> m_painter;
 87	QThread m_drawThread;
 88	std::shared_ptr<CoreController> m_context;
 89};
 90
 91class PainterGL : public QObject {
 92Q_OBJECT
 93
 94public:
 95	PainterGL(QWindow* surface, const QSurfaceFormat& format);
 96	~PainterGL();
 97
 98	void setContext(std::shared_ptr<CoreController>);
 99	void setMessagePainter(MessagePainter*);
100	void enqueue(const uint32_t* backing);
101
102	bool supportsShaders() const { return m_supportsShaders; }
103
104	void setVideoProxy(std::shared_ptr<VideoProxy>);
105
106public slots:
107	void create();
108	void destroy();
109
110	void forceDraw();
111	void draw();
112	void start();
113	void stop();
114	void pause();
115	void unpause();
116	void resize(const QSize& size);
117	void lockAspectRatio(bool lock);
118	void lockIntegerScaling(bool lock);
119	void interframeBlending(bool enable);
120	void showOSD(bool enable);
121	void filter(bool filter);
122	void resizeContext();
123
124	void setShaders(struct VDir*);
125	void clearShaders();
126	VideoShader* shaders();
127
128	int glTex();
129
130signals:
131	void started();
132
133private:
134	void makeCurrent();
135	void performDraw();
136	void dequeue();
137	void dequeueAll();
138
139	std::array<std::array<uint32_t, 0x100000>, 3> m_buffers;
140	QList<uint32_t*> m_free;
141	QQueue<uint32_t*> m_queue;
142	QAtomicInt m_lagging = 0;
143	uint32_t* m_buffer;
144	QPainter m_painter;
145	QMutex m_mutex;
146	QWindow* m_surface;
147	QSurfaceFormat m_format;
148	std::unique_ptr<QOpenGLPaintDevice> m_window;
149	std::unique_ptr<QOpenGLContext> m_gl;
150	bool m_active = false;
151	bool m_started = false;
152	std::shared_ptr<CoreController> m_context = nullptr;
153	bool m_supportsShaders;
154	bool m_showOSD;
155	VideoShader m_shader{};
156	VideoBackend* m_backend = nullptr;
157	QSize m_size;
158	MessagePainter* m_messagePainter = nullptr;
159	QElapsedTimer m_delayTimer;
160	std::shared_ptr<VideoProxy> m_videoProxy;
161};
162
163}
164
165#endif