Core: Refactor GBASync into mCoreSync

This commit is contained in:
Jeffrey Pfau 2016-01-29 00:19:54 -08:00
parent 6ec99ce4e4
commit 234ecd9619
18 changed files with 84 additions and 84 deletions

View File

@ -5,7 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "sync.h"
static void _changeVideoSync(struct GBASync* sync, bool frameOn) {
static void _changeVideoSync(struct mCoreSync* sync, bool frameOn) {
// Make sure the video thread can process events while the GBA thread is paused
MutexLock(&sync->videoFrameMutex);
if (frameOn != sync->videoFrameOn) {
@ -15,7 +15,7 @@ static void _changeVideoSync(struct GBASync* sync, bool frameOn) {
MutexUnlock(&sync->videoFrameMutex);
}
void GBASyncPostFrame(struct GBASync* sync) {
void mCoreSyncPostFrame(struct mCoreSync* sync) {
if (!sync) {
return;
}
@ -31,7 +31,7 @@ void GBASyncPostFrame(struct GBASync* sync) {
MutexUnlock(&sync->videoFrameMutex);
}
void GBASyncForceFrame(struct GBASync* sync) {
void mCoreSyncForceFrame(struct mCoreSync* sync) {
if (!sync) {
return;
}
@ -41,7 +41,7 @@ void GBASyncForceFrame(struct GBASync* sync) {
MutexUnlock(&sync->videoFrameMutex);
}
bool GBASyncWaitFrameStart(struct GBASync* sync) {
bool mCoreSyncWaitFrameStart(struct mCoreSync* sync) {
if (!sync) {
return true;
}
@ -60,7 +60,7 @@ bool GBASyncWaitFrameStart(struct GBASync* sync) {
return true;
}
void GBASyncWaitFrameEnd(struct GBASync* sync) {
void mCoreSyncWaitFrameEnd(struct mCoreSync* sync) {
if (!sync) {
return;
}
@ -68,7 +68,7 @@ void GBASyncWaitFrameEnd(struct GBASync* sync) {
MutexUnlock(&sync->videoFrameMutex);
}
void GBASyncSetVideoSync(struct GBASync* sync, bool wait) {
void mCoreSyncSetVideoSync(struct mCoreSync* sync, bool wait) {
if (!sync) {
return;
}
@ -76,7 +76,7 @@ void GBASyncSetVideoSync(struct GBASync* sync, bool wait) {
_changeVideoSync(sync, wait);
}
void GBASyncProduceAudio(struct GBASync* sync, bool wait) {
void mCoreSyncProduceAudio(struct mCoreSync* sync, bool wait) {
if (!sync) {
return;
}
@ -88,7 +88,7 @@ void GBASyncProduceAudio(struct GBASync* sync, bool wait) {
MutexUnlock(&sync->audioBufferMutex);
}
void GBASyncLockAudio(struct GBASync* sync) {
void mCoreSyncLockAudio(struct mCoreSync* sync) {
if (!sync) {
return;
}
@ -96,7 +96,7 @@ void GBASyncLockAudio(struct GBASync* sync) {
MutexLock(&sync->audioBufferMutex);
}
void GBASyncUnlockAudio(struct GBASync* sync) {
void mCoreSyncUnlockAudio(struct mCoreSync* sync) {
if (!sync) {
return;
}
@ -104,7 +104,7 @@ void GBASyncUnlockAudio(struct GBASync* sync) {
MutexUnlock(&sync->audioBufferMutex);
}
void GBASyncConsumeAudio(struct GBASync* sync) {
void mCoreSyncConsumeAudio(struct mCoreSync* sync) {
if (!sync) {
return;
}

37
src/core/sync.h Normal file
View File

@ -0,0 +1,37 @@
/* Copyright (c) 2013-2016 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef M_CORE_SYNC_H
#define M_CORE_SYNC_H
#include "util/common.h"
#include "util/threading.h"
struct mCoreSync {
int videoFramePending;
bool videoFrameWait;
bool videoFrameOn;
Mutex videoFrameMutex;
Condition videoFrameAvailableCond;
Condition videoFrameRequiredCond;
bool audioWait;
Condition audioRequiredCond;
Mutex audioBufferMutex;
};
void mCoreSyncPostFrame(struct mCoreSync* sync);
void mCoreSyncForceFrame(struct mCoreSync* sync);
bool mCoreSyncWaitFrameStart(struct mCoreSync* sync);
void mCoreSyncWaitFrameEnd(struct mCoreSync* sync);
void mCoreSyncSetVideoSync(struct mCoreSync* sync, bool wait);
void mCoreSyncProduceAudio(struct mCoreSync* sync, bool wait);
void mCoreSyncLockAudio(struct mCoreSync* sync);
void mCoreSyncUnlockAudio(struct mCoreSync* sync);
void mCoreSyncConsumeAudio(struct mCoreSync* sync);
#endif

View File

@ -125,7 +125,7 @@ void GBAAudioDeinit(struct GBAAudio* audio) {
}
void GBAAudioResizeBuffer(struct GBAAudio* audio, size_t samples) {
GBASyncLockAudio(audio->p->sync);
mCoreSyncLockAudio(audio->p->sync);
audio->samples = samples;
#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
size_t oldCapacity = audio->left.capacity;
@ -161,7 +161,7 @@ void GBAAudioResizeBuffer(struct GBAAudio* audio, size_t samples) {
audio->clock = 0;
#endif
GBASyncConsumeAudio(audio->p->sync);
mCoreSyncConsumeAudio(audio->p->sync);
}
int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles) {
@ -534,7 +534,7 @@ void GBAAudioSampleFIFO(struct GBAAudio* audio, int fifoId, int32_t cycles) {
#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
unsigned GBAAudioCopy(struct GBAAudio* audio, void* left, void* right, unsigned nSamples) {
GBASyncLockAudio(audio->p->sync);
mCoreSyncLockAudio(audio->p->sync);
unsigned read = 0;
if (left) {
unsigned readL = CircleBufferRead(&audio->left, left, nSamples * sizeof(int16_t)) >> 1;
@ -550,7 +550,7 @@ unsigned GBAAudioCopy(struct GBAAudio* audio, void* left, void* right, unsigned
}
read = read >= readR ? read : readR;
}
GBASyncConsumeAudio(audio->p->sync);
mCoreSyncConsumeAudio(audio->p->sync);
return read;
}
@ -821,7 +821,7 @@ static void _sample(struct GBAAudio* audio) {
sampleLeft = _applyBias(audio, sampleLeft);
sampleRight = _applyBias(audio, sampleRight);
GBASyncLockAudio(audio->p->sync);
mCoreSyncLockAudio(audio->p->sync);
unsigned produced;
#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
CircleBufferWrite16(&audio->left, sampleLeft);
@ -846,7 +846,7 @@ static void _sample(struct GBAAudio* audio) {
audio->p->stream->postAudioFrame(audio->p->stream, sampleLeft, sampleRight);
}
bool wait = produced >= audio->samples;
GBASyncProduceAudio(audio->p->sync, wait);
mCoreSyncProduceAudio(audio->p->sync, wait);
if (wait && audio->p->stream && audio->p->stream->postAudioBuffer) {
audio->p->stream->postAudioBuffer(audio->p->stream, audio);

View File

@ -9,8 +9,8 @@
#include "util/common.h"
#include "core/directories.h"
#include "core/sync.h"
#include "gba/context/config.h"
#include "gba/context/sync.h"
#include "gba/input.h"
struct GBAContext {

View File

@ -1,37 +0,0 @@
/* Copyright (c) 2013-2015 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef GBA_SYNC_H
#define GBA_SYNC_H
#include "util/common.h"
#include "util/threading.h"
struct GBASync {
int videoFramePending;
bool videoFrameWait;
bool videoFrameOn;
Mutex videoFrameMutex;
Condition videoFrameAvailableCond;
Condition videoFrameRequiredCond;
bool audioWait;
Condition audioRequiredCond;
Mutex audioBufferMutex;
};
void GBASyncPostFrame(struct GBASync* sync);
void GBASyncForceFrame(struct GBASync* sync);
bool GBASyncWaitFrameStart(struct GBASync* sync);
void GBASyncWaitFrameEnd(struct GBASync* sync);
void GBASyncSetVideoSync(struct GBASync* sync, bool wait);
void GBASyncProduceAudio(struct GBASync* sync, bool wait);
void GBASyncLockAudio(struct GBASync* sync);
void GBASyncUnlockAudio(struct GBASync* sync);
void GBASyncConsumeAudio(struct GBASync* sync);
#endif

View File

@ -83,7 +83,7 @@ struct GBA {
struct GBAAudio audio;
struct GBASIO sio;
struct GBASync* sync;
struct mCoreSync* sync;
struct ARMDebugger* debugger;

View File

@ -519,7 +519,7 @@ bool GBALoadStateNamed(struct GBA* gba, struct VFile* vf, int flags) {
if (flags & SAVESTATE_SCREENSHOT && GBAExtdataGet(&extdata, EXTDATA_SCREENSHOT, &item)) {
if (item.size >= VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4) {
gba->video.renderer->putPixels(gba->video.renderer, VIDEO_HORIZONTAL_PIXELS, item.data);
GBASyncForceFrame(gba->sync);
mCoreSyncForceFrame(gba->sync);
} else {
GBALog(gba, GBA_LOG_WARN, "Savestate includes invalid screenshot");
}

View File

@ -627,7 +627,7 @@ void GBAThreadPause(struct GBAThread* threadContext) {
}
MutexUnlock(&threadContext->stateMutex);
GBASyncSetVideoSync(&threadContext->sync, frameOn);
mCoreSyncSetVideoSync(&threadContext->sync, frameOn);
}
void GBAThreadUnpause(struct GBAThread* threadContext) {
@ -641,7 +641,7 @@ void GBAThreadUnpause(struct GBAThread* threadContext) {
}
MutexUnlock(&threadContext->stateMutex);
GBASyncSetVideoSync(&threadContext->sync, frameOn);
mCoreSyncSetVideoSync(&threadContext->sync, frameOn);
}
bool GBAThreadIsPaused(struct GBAThread* threadContext) {
@ -668,7 +668,7 @@ void GBAThreadTogglePause(struct GBAThread* threadContext) {
}
MutexUnlock(&threadContext->stateMutex);
GBASyncSetVideoSync(&threadContext->sync, frameOn);
mCoreSyncSetVideoSync(&threadContext->sync, frameOn);
}
void GBAThreadPauseFromThread(struct GBAThread* threadContext) {
@ -681,7 +681,7 @@ void GBAThreadPauseFromThread(struct GBAThread* threadContext) {
}
MutexUnlock(&threadContext->stateMutex);
GBASyncSetVideoSync(&threadContext->sync, frameOn);
mCoreSyncSetVideoSync(&threadContext->sync, frameOn);
}
void GBAThreadLoadROM(struct GBAThread* threadContext, const char* fname) {

View File

@ -9,10 +9,10 @@
#include "util/common.h"
#include "core/directories.h"
#include "core/sync.h"
#include "gba/gba.h"
#include "gba/input.h"
#include "gba/context/overrides.h"
#include "gba/context/sync.h"
#include "util/threading.h"
@ -93,7 +93,7 @@ struct GBAThread {
void* userData;
void (*run)(struct GBAThread*);
struct GBASync sync;
struct mCoreSync sync;
int rewindBufferSize;
int rewindBufferCapacity;

View File

@ -5,7 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "video.h"
#include "gba/context/sync.h"
#include "core/sync.h"
#include "gba/gba.h"
#include "gba/io.h"
#include "gba/rr/rr.h"
@ -160,7 +160,7 @@ int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) {
GBAFrameEnded(video->p);
--video->frameskipCounter;
if (video->frameskipCounter < 0) {
GBASyncPostFrame(video->p->sync);
mCoreSyncPostFrame(video->p->sync);
video->frameskipCounter = video->frameskip;
}
++video->frameCounter;

View File

@ -35,10 +35,10 @@ void AudioDevice::setFormat(const QAudioFormat& format) {
GBAThreadContinue(m_context);
#elif RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
double fauxClock = GBAAudioCalculateRatio(1, m_context->fpsTarget, 1);
GBASyncLockAudio(&m_context->sync);
mCoreSyncLockAudio(&m_context->sync);
blip_set_rates(m_context->gba->audio.left, GBA_ARM7TDMI_FREQUENCY, format.sampleRate() * fauxClock);
blip_set_rates(m_context->gba->audio.right, GBA_ARM7TDMI_FREQUENCY, format.sampleRate() * fauxClock);
GBASyncUnlockAudio(&m_context->sync);
mCoreSyncUnlockAudio(&m_context->sync);
#endif
}
@ -59,14 +59,14 @@ qint64 AudioDevice::readData(char* data, qint64 maxSize) {
#if RESAMPLE_LIBRARY == RESAMPLE_NN
return GBAAudioResampleNN(&m_context->gba->audio, m_ratio, &m_drift, reinterpret_cast<GBAStereoSample*>(data), maxSize / sizeof(GBAStereoSample)) * sizeof(GBAStereoSample);
#elif RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
GBASyncLockAudio(&m_context->sync);
mCoreSyncLockAudio(&m_context->sync);
int available = blip_samples_avail(m_context->gba->audio.left);
if (available > maxSize / sizeof(GBAStereoSample)) {
available = maxSize / sizeof(GBAStereoSample);
}
blip_read_samples(m_context->gba->audio.left, &reinterpret_cast<GBAStereoSample*>(data)->left, available, true);
blip_read_samples(m_context->gba->audio.right, &reinterpret_cast<GBAStereoSample*>(data)->right, available, true);
GBASyncConsumeAudio(&m_context->sync);
mCoreSyncConsumeAudio(&m_context->sync);
return available * sizeof(GBAStereoSample);
#endif
}

View File

@ -79,7 +79,7 @@ void DisplayGL::startDrawing(GBAThread* thread) {
m_painter->moveToThread(m_drawThread);
connect(m_drawThread, SIGNAL(started()), m_painter, SLOT(start()));
m_drawThread->start();
GBASyncSetVideoSync(&m_context->sync, false);
mCoreSyncSetVideoSync(&m_context->sync, false);
lockAspectRatio(isAspectRatioLocked());
filter(isFiltered());
@ -310,15 +310,15 @@ void PainterGL::draw() {
if (m_queue.isEmpty() || !GBAThreadIsActive(m_context)) {
return;
}
if (GBASyncWaitFrameStart(&m_context->sync) || !m_queue.isEmpty()) {
if (mCoreSyncWaitFrameStart(&m_context->sync) || !m_queue.isEmpty()) {
dequeue();
GBASyncWaitFrameEnd(&m_context->sync);
mCoreSyncWaitFrameEnd(&m_context->sync);
m_painter.begin(m_gl->context()->device());
performDraw();
m_painter.end();
m_backend->swap(m_backend);
} else {
GBASyncWaitFrameEnd(&m_context->sync);
mCoreSyncWaitFrameEnd(&m_context->sync);
}
if (!m_queue.isEmpty()) {
QMetaObject::invokeMethod(this, "draw", Qt::QueuedConnection);

View File

@ -79,10 +79,10 @@ void mSDLGLRunloopGBA(struct mSDLRenderer* renderer, void* user) {
#endif
}
if (GBASyncWaitFrameStart(&context->sync)) {
if (mCoreSyncWaitFrameStart(&context->sync)) {
v->postFrame(v, renderer->d.outputBuffer);
}
GBASyncWaitFrameEnd(&context->sync);
mCoreSyncWaitFrameEnd(&context->sync);
v->drawFrame(v);
v->swap(v);
}

View File

@ -115,10 +115,10 @@ void mSDLGLES2Runloop(struct mSDLRenderer* renderer, void* user) {
GBASDLHandleEvent(context, &renderer->player, &event);
}
if (GBASyncWaitFrameStart(&context->sync)) {
if (mCoreSyncWaitFrameStart(&context->sync)) {
v->postFrame(v, renderer->d.outputBuffer);
}
GBASyncWaitFrameEnd(&context->sync);
mCoreSyncWaitFrameEnd(&context->sync);
v->drawFrame(v);
#ifdef BUILD_RASPI
eglSwapBuffers(renderer->display, renderer->surface);

View File

@ -91,7 +91,7 @@ void GBASDLRunloop(struct GBAThread* context, struct SDLSoftwareRenderer* render
GBASDLHandleEvent(context, &renderer->player, &event);
}
if (GBASyncWaitFrameStart(&context->sync)) {
if (mCoreSyncWaitFrameStart(&context->sync)) {
struct fb_var_screeninfo info;
ioctl(renderer->fb, FBIOGET_VSCREENINFO, &info);
info.yoffset = VIDEO_VERTICAL_PIXELS * renderer->odd;
@ -103,7 +103,7 @@ void GBASDLRunloop(struct GBAThread* context, struct SDLSoftwareRenderer* render
renderer->odd = !renderer->odd;
renderer->d.outputBuffer = renderer->base[renderer->odd];
}
GBASyncWaitFrameEnd(&context->sync);
mCoreSyncWaitFrameEnd(&context->sync);
}
}

View File

@ -122,7 +122,7 @@ static void _GBASDLAudioCallback(void* context, Uint8* data, int len) {
double fauxClock = 1;
if (audioContext->thread) {
fauxClock = GBAAudioCalculateRatio(1, audioContext->thread->fpsTarget, 1);
GBASyncLockAudio(&audioContext->thread->sync);
mCoreSyncLockAudio(&audioContext->thread->sync);
}
blip_set_rates(gba->audio.left, GBA_ARM7TDMI_FREQUENCY, audioContext->obtainedSpec.freq * fauxClock);
blip_set_rates(gba->audio.right, GBA_ARM7TDMI_FREQUENCY, audioContext->obtainedSpec.freq * fauxClock);
@ -137,7 +137,7 @@ static void _GBASDLAudioCallback(void* context, Uint8* data, int len) {
}
if (audioContext->thread) {
GBASyncConsumeAudio(&audioContext->thread->sync);
mCoreSyncConsumeAudio(&audioContext->thread->sync);
}
if (available < len) {
memset(((short*) data) + audioContext->obtainedSpec.channels * available, 0, (len - available) * audioContext->obtainedSpec.channels * sizeof(short));

View File

@ -94,7 +94,7 @@ void mSDLSWRunloopGBA(struct mSDLRenderer* renderer, void* user) {
GBASDLHandleEvent(context, &renderer->player, &event);
}
if (GBASyncWaitFrameStart(&context->sync)) {
if (mCoreSyncWaitFrameStart(&context->sync)) {
#if SDL_VERSION_ATLEAST(2, 0, 0)
SDL_UnlockTexture(renderer->sdlTex);
SDL_RenderCopy(renderer->sdlRenderer, renderer->sdlTex, 0, 0);
@ -129,7 +129,7 @@ void mSDLSWRunloopGBA(struct mSDLRenderer* renderer, void* user) {
SDL_LockSurface(surface);
#endif
}
GBASyncWaitFrameEnd(&context->sync);
mCoreSyncWaitFrameEnd(&context->sync);
}
}

View File

@ -174,7 +174,7 @@ static void _GBAPerfRunloop(struct GBAThread* context, int* frames, bool quiet)
*frames = 0;
int lastFrames = 0;
while (context->state < THREAD_EXITING) {
if (GBASyncWaitFrameStart(&context->sync)) {
if (mCoreSyncWaitFrameStart(&context->sync)) {
++*frames;
++lastFrames;
if (!quiet) {
@ -192,7 +192,7 @@ static void _GBAPerfRunloop(struct GBAThread* context, int* frames, bool quiet)
}
}
}
GBASyncWaitFrameEnd(&context->sync);
mCoreSyncWaitFrameEnd(&context->sync);
if (duration > 0 && *frames == duration) {
_GBAPerfShutdown(0);
}