all repos — mgba @ d33f1d13926f4aa2afebad31775222cdd278411c

mGBA Game Boy Advance Emulator

src/platform/qt/VideoProxy.cpp (view raw)

  1/* Copyright (c) 2013-2018 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#include "VideoProxy.h"
  7
  8#include "CoreController.h"
  9
 10#include <QThread>
 11
 12using namespace QGBA;
 13
 14VideoProxy::VideoProxy() {
 15	mVideoLoggerRendererCreate(&m_logger.d, false);
 16	m_logger.d.block = true;
 17
 18	m_logger.d.init = &cbind<&VideoProxy::init>;
 19	m_logger.d.reset = &cbind<&VideoProxy::reset>;
 20	m_logger.d.deinit = &cbind<&VideoProxy::deinit>;
 21	m_logger.d.lock = &cbind<&VideoProxy::lock>;
 22	m_logger.d.unlock = &cbind<&VideoProxy::unlock>;
 23	m_logger.d.wait = &cbind<&VideoProxy::wait>;
 24	m_logger.d.wake = &callback<void, int>::func<&VideoProxy::wake>;
 25
 26	m_logger.d.writeData = &callback<bool, const void*, size_t>::func<&VideoProxy::writeData>;
 27	m_logger.d.readData = &callback<bool, void*, size_t, bool>::func<&VideoProxy::readData>;
 28	m_logger.d.postEvent = &callback<void, enum mVideoLoggerEvent>::func<&VideoProxy::postEvent>;
 29
 30	connect(this, &VideoProxy::dataAvailable, this, &VideoProxy::processData);
 31	connect(this, &VideoProxy::eventPosted, this, &VideoProxy::handleEvent);
 32}
 33
 34void VideoProxy::attach(CoreController* controller) {
 35	CoreController::Interrupter interrupter(controller);
 36	controller->thread()->core->videoLogger = &m_logger.d;
 37}
 38
 39void VideoProxy::processData() {
 40	mVideoLoggerRendererRun(&m_logger.d, false);
 41	m_fromThreadCond.wakeAll();
 42}
 43
 44void VideoProxy::init() {
 45	RingFIFOInit(&m_dirtyQueue, 0x80000);
 46}
 47
 48void VideoProxy::reset() {
 49	m_mutex.lock();
 50	RingFIFOClear(&m_dirtyQueue);
 51	m_toThreadCond.wakeAll();
 52	m_mutex.unlock();
 53}
 54
 55void VideoProxy::deinit() {
 56	RingFIFODeinit(&m_dirtyQueue);
 57}
 58
 59bool VideoProxy::writeData(const void* data, size_t length) {
 60	while (!RingFIFOWrite(&m_dirtyQueue, data, length)) {
 61		emit dataAvailable();
 62		m_mutex.lock();
 63		m_toThreadCond.wakeAll();
 64		m_fromThreadCond.wait(&m_mutex);
 65		m_mutex.unlock();
 66	}
 67	return true;
 68}
 69
 70bool VideoProxy::readData(void* data, size_t length, bool block) {
 71	bool read = false;
 72	while (true) {
 73		read = RingFIFORead(&m_dirtyQueue, data, length);
 74		if (!block || read) {
 75			break;
 76		}
 77		m_mutex.lock();
 78		m_fromThreadCond.wakeAll();
 79		m_toThreadCond.wait(&m_mutex);
 80		m_mutex.unlock();
 81	}
 82	return read;
 83}
 84
 85void VideoProxy::postEvent(enum mVideoLoggerEvent event) {
 86	if (QThread::currentThread() == thread()) {
 87		// We're on the main thread
 88		emit eventPosted(event);
 89	} else {
 90		m_mutex.lock();
 91		emit eventPosted(event);
 92		m_fromThreadCond.wait(&m_mutex, 1);
 93		m_mutex.unlock();
 94	}
 95}
 96
 97void VideoProxy::handleEvent(int event) {
 98	m_mutex.lock();
 99	m_logger.d.handleEvent(&m_logger.d, static_cast<enum mVideoLoggerEvent>(event));
100	m_fromThreadCond.wakeAll();
101	m_mutex.unlock();
102}
103
104void VideoProxy::lock() {
105	m_mutex.lock();
106}
107
108void VideoProxy::unlock() {
109	m_mutex.unlock();
110}
111
112void VideoProxy::wait() {
113	m_mutex.lock();
114	while (RingFIFOSize(&m_dirtyQueue)) {
115		emit dataAvailable();
116		m_toThreadCond.wakeAll();
117		m_fromThreadCond.wait(&m_mutex, 1);
118	}
119	m_mutex.unlock();
120}
121
122void VideoProxy::wake(int y) {
123	if ((y & 15) == 15) {
124		emit dataAvailable();
125		m_toThreadCond.wakeAll();
126	}
127}