all repos — mgba @ 8e096916b1dc1aacc17236e8d2bf6eda4edb5edf

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