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