all repos — mgba @ 234ecd961976d7e24609beda22c9db9fafa6901b

mGBA Game Boy Advance Emulator

Core: Refactor GBASync into mCoreSync
Jeffrey Pfau jeffrey@endrift.com
Fri, 29 Jan 2016 00:19:54 -0800
commit

234ecd961976d7e24609beda22c9db9fafa6901b

parent

6ec99ce4e478878130b7a9449253a88c42ae00be

A src/core/sync.h

@@ -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
M src/gba/audio.csrc/gba/audio.c

@@ -125,7 +125,7 @@ CircleBufferDeinit(&audio->chB.fifo);

} 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 @@ blip_clear(audio->right);

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 @@ }

#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 @@ memset((int16_t*) right + readR, 0, nSamples - readR);

} read = read >= readR ? read : readR; } - GBASyncConsumeAudio(audio->p->sync); + mCoreSyncConsumeAudio(audio->p->sync); return read; }

@@ -821,7 +821,7 @@

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 @@ if (audio->p->stream && audio->p->stream->postAudioFrame) {

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);
M src/gba/context/context.hsrc/gba/context/context.h

@@ -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 {
M src/gba/context/sync.csrc/core/sync.c

@@ -5,7 +5,7 @@ * 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/. */ #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 @@ }

MutexUnlock(&sync->videoFrameMutex); } -void GBASyncPostFrame(struct GBASync* sync) { +void mCoreSyncPostFrame(struct mCoreSync* sync) { if (!sync) { return; }

@@ -31,7 +31,7 @@ } while (sync->videoFrameWait && sync->videoFramePending);

MutexUnlock(&sync->videoFrameMutex); } -void GBASyncForceFrame(struct GBASync* sync) { +void mCoreSyncForceFrame(struct mCoreSync* sync) { if (!sync) { return; }

@@ -41,7 +41,7 @@ ConditionWake(&sync->videoFrameAvailableCond);

MutexUnlock(&sync->videoFrameMutex); } -bool GBASyncWaitFrameStart(struct GBASync* sync) { +bool mCoreSyncWaitFrameStart(struct mCoreSync* sync) { if (!sync) { return true; }

@@ -60,7 +60,7 @@ sync->videoFramePending = 0;

return true; } -void GBASyncWaitFrameEnd(struct GBASync* sync) { +void mCoreSyncWaitFrameEnd(struct mCoreSync* sync) { if (!sync) { return; }

@@ -68,7 +68,7 @@

MutexUnlock(&sync->videoFrameMutex); } -void GBASyncSetVideoSync(struct GBASync* sync, bool wait) { +void mCoreSyncSetVideoSync(struct mCoreSync* sync, bool wait) { if (!sync) { return; }

@@ -76,7 +76,7 @@

_changeVideoSync(sync, wait); } -void GBASyncProduceAudio(struct GBASync* sync, bool wait) { +void mCoreSyncProduceAudio(struct mCoreSync* sync, bool wait) { if (!sync) { return; }

@@ -88,7 +88,7 @@ }

MutexUnlock(&sync->audioBufferMutex); } -void GBASyncLockAudio(struct GBASync* sync) { +void mCoreSyncLockAudio(struct mCoreSync* sync) { if (!sync) { return; }

@@ -96,7 +96,7 @@

MutexLock(&sync->audioBufferMutex); } -void GBASyncUnlockAudio(struct GBASync* sync) { +void mCoreSyncUnlockAudio(struct mCoreSync* sync) { if (!sync) { return; }

@@ -104,7 +104,7 @@

MutexUnlock(&sync->audioBufferMutex); } -void GBASyncConsumeAudio(struct GBASync* sync) { +void mCoreSyncConsumeAudio(struct mCoreSync* sync) { if (!sync) { return; }
D src/gba/context/sync.h

@@ -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
M src/gba/gba.hsrc/gba/gba.h

@@ -83,7 +83,7 @@ struct GBAVideo video;

struct GBAAudio audio; struct GBASIO sio; - struct GBASync* sync; + struct mCoreSync* sync; struct ARMDebugger* debugger;
M src/gba/serialize.csrc/gba/serialize.c

@@ -519,7 +519,7 @@ struct GBAExtdataItem item;

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"); }
M src/gba/supervisor/thread.csrc/gba/supervisor/thread.c

@@ -627,7 +627,7 @@ frameOn = false;

} MutexUnlock(&threadContext->stateMutex); - GBASyncSetVideoSync(&threadContext->sync, frameOn); + mCoreSyncSetVideoSync(&threadContext->sync, frameOn); } void GBAThreadUnpause(struct GBAThread* threadContext) {

@@ -641,7 +641,7 @@ frameOn = threadContext->frameWasOn;

} MutexUnlock(&threadContext->stateMutex); - GBASyncSetVideoSync(&threadContext->sync, frameOn); + mCoreSyncSetVideoSync(&threadContext->sync, frameOn); } bool GBAThreadIsPaused(struct GBAThread* threadContext) {

@@ -668,7 +668,7 @@ frameOn = false;

} MutexUnlock(&threadContext->stateMutex); - GBASyncSetVideoSync(&threadContext->sync, frameOn); + mCoreSyncSetVideoSync(&threadContext->sync, frameOn); } void GBAThreadPauseFromThread(struct GBAThread* threadContext) {

@@ -681,7 +681,7 @@ frameOn = false;

} MutexUnlock(&threadContext->stateMutex); - GBASyncSetVideoSync(&threadContext->sync, frameOn); + mCoreSyncSetVideoSync(&threadContext->sync, frameOn); } void GBAThreadLoadROM(struct GBAThread* threadContext, const char* fname) {
M src/gba/supervisor/thread.hsrc/gba/supervisor/thread.h

@@ -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 @@ ThreadStopCallback stopCallback;

void* userData; void (*run)(struct GBAThread*); - struct GBASync sync; + struct mCoreSync sync; int rewindBufferSize; int rewindBufferCapacity;
M src/gba/video.csrc/gba/video.c

@@ -5,7 +5,7 @@ * 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/. */ #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 @@ }

GBAFrameEnded(video->p); --video->frameskipCounter; if (video->frameskipCounter < 0) { - GBASyncPostFrame(video->p->sync); + mCoreSyncPostFrame(video->p->sync); video->frameskipCounter = video->frameskip; } ++video->frameCounter;
M src/platform/qt/AudioDevice.cppsrc/platform/qt/AudioDevice.cpp

@@ -35,10 +35,10 @@ m_ratio = GBAAudioCalculateRatio(m_context->gba->audio.sampleRate, m_context->fpsTarget, format.sampleRate());

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 @@

#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 }
M src/platform/qt/DisplayGL.cppsrc/platform/qt/DisplayGL.cpp

@@ -79,7 +79,7 @@ m_gl->context()->moveToThread(m_drawThread);

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);
M src/platform/sdl/gl-sdl.csrc/platform/sdl/gl-sdl.c

@@ -79,10 +79,10 @@ }

#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); }
M src/platform/sdl/gles2-sdl.csrc/platform/sdl/gles2-sdl.c

@@ -115,10 +115,10 @@ while (SDL_PollEvent(&event)) {

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);
M src/platform/sdl/pandora-sdl.csrc/platform/sdl/pandora-sdl.c

@@ -91,7 +91,7 @@ while (SDL_PollEvent(&event)) {

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 @@

renderer->odd = !renderer->odd; renderer->d.outputBuffer = renderer->base[renderer->odd]; } - GBASyncWaitFrameEnd(&context->sync); + mCoreSyncWaitFrameEnd(&context->sync); } }
M src/platform/sdl/sdl-audio.csrc/platform/sdl/sdl-audio.c

@@ -122,7 +122,7 @@ #elif RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF

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 @@ blip_read_samples(gba->audio.right, ((short*) data) + 1, available, 1);

} 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));
M src/platform/sdl/sw-sdl.csrc/platform/sdl/sw-sdl.c

@@ -94,7 +94,7 @@ while (SDL_PollEvent(&event)) {

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 @@ SDL_Flip(surface);

SDL_LockSurface(surface); #endif } - GBASyncWaitFrameEnd(&context->sync); + mCoreSyncWaitFrameEnd(&context->sync); } }
M src/platform/test/perf-main.csrc/platform/test/perf-main.c

@@ -174,7 +174,7 @@ int duration = *frames;

*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 @@ lastFrames = 0;

} } } - GBASyncWaitFrameEnd(&context->sync); + mCoreSyncWaitFrameEnd(&context->sync); if (duration > 0 && *frames == duration) { _GBAPerfShutdown(0); }