From 5a68fc29c928d4b2cd98c669f03a5018d90a4208 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 25 Aug 2019 21:14:01 +0200 Subject: [PATCH] sceUsbCam: Heap-allocate the buffer. --- Core/HLE/sceKernel.cpp | 4 ++++ Core/HLE/sceUsbCam.cpp | 35 ++++++++++++++++++++++++++--------- Core/HLE/sceUsbCam.h | 3 +++ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/Core/HLE/sceKernel.cpp b/Core/HLE/sceKernel.cpp index bf409a247c5d..c375c59a049a 100644 --- a/Core/HLE/sceKernel.cpp +++ b/Core/HLE/sceKernel.cpp @@ -81,6 +81,7 @@ #include "sceHeap.h" #include "sceDmac.h" #include "sceMp4.h" +#include "sceUsbCam.h" #include "../Util/PPGeDraw.h" @@ -143,6 +144,7 @@ void __KernelInit() __AudioCodecInit(); __VideoPmpInit(); __UsbGpsInit(); + __UsbCamInit(); SaveState::Init(); // Must be after IO, as it may create a directory Reporting::Init(); @@ -166,6 +168,8 @@ void __KernelShutdown() hleCurrentThreadName = NULL; kernelObjects.Clear(); + __UsbCamShutdown(); + __AudioCodecShutdown(); __VideoPmpShutdown(); __AACShutdown(); diff --git a/Core/HLE/sceUsbCam.cpp b/Core/HLE/sceUsbCam.cpp index ae0599c97df1..04e81fd3a7a9 100644 --- a/Core/HLE/sceUsbCam.cpp +++ b/Core/HLE/sceUsbCam.cpp @@ -15,6 +15,7 @@ // Official git repository and contact information can be found at // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. +#include #include #include "base/NativeApp.h" @@ -29,9 +30,25 @@ PspUsbCamSetupVideoParam videoParam; unsigned int videoBufferLength = 0; unsigned int nextVideoFrame = 0; -unsigned char videoBuffer[40 * 1000]; +uint8_t *videoBuffer; std::mutex videoBufferMutex; +enum { + VIDEO_BUFFER_SIZE = 40 * 1000, +}; + +void __UsbCamInit() { + videoBuffer = new uint8_t[VIDEO_BUFFER_SIZE]; +} + +void __UsbCamShutdown() { + delete[] videoBuffer; + videoBuffer = nullptr; +} + +// TODO: Technically, we should store the videoBuffer into the savestate, if this +// module has been initialized. + static int sceUsbCamSetupMic(u32 paramAddr, u32 workareaAddr, int wasize) { INFO_LOG(HLE, "UNIMPL sceUsbCamSetupMic"); if (Memory::IsValidRange(paramAddr, sizeof(micParam))) { @@ -74,6 +91,7 @@ static int sceUsbCamSetupVideo(u32 paramAddr, u32 workareaAddr, int wasize) { INFO_LOG(HLE, "UNIMPL sceUsbCamSetupVideo - size: %d", videoParam.size); INFO_LOG(HLE, "UNIMPL sceUsbCamSetupVideo - resolution: %d", videoParam.resolution); + INFO_LOG(HLE, "UNIMPL sceUsbCamSetupVideo - framesize: %d", videoParam.framesize); return 0; } @@ -96,20 +114,19 @@ static int sceUsbCamAutoImageReverseSW(int rev) { static int sceUsbCamReadVideoFrameBlocking(u32 bufAddr, u32 size) { std::lock_guard lock(videoBufferMutex); - for (unsigned int i = 0; i < videoBufferLength && i < size; i++) { - if (Memory::IsValidAddress(bufAddr + i)) { - Memory::Write_U8(videoBuffer[i], bufAddr + i); - } + + u32 transferSize = std::min(videoBufferLength, size); + if (Memory::IsValidRange(bufAddr, size)) { + Memory::Memcpy(bufAddr, videoBuffer, transferSize); } return videoBufferLength; } static int sceUsbCamReadVideoFrame(u32 bufAddr, u32 size) { std::lock_guard lock(videoBufferMutex); - for (unsigned int i = 0; i < videoBufferLength && i < size; i++) { - if (Memory::IsValidAddress(bufAddr + i)) { - Memory::Write_U8(videoBuffer[i], bufAddr + i); - } + u32 transferSize = std::min(videoBufferLength, size); + if (Memory::IsValidRange(bufAddr, size)) { + Memory::Memcpy(bufAddr, videoBuffer, transferSize); } nextVideoFrame = videoBufferLength; return 0; diff --git a/Core/HLE/sceUsbCam.h b/Core/HLE/sceUsbCam.h index ef088fe91da4..ebb5388db656 100644 --- a/Core/HLE/sceUsbCam.h +++ b/Core/HLE/sceUsbCam.h @@ -19,6 +19,9 @@ void Register_sceUsbCam(); +void __UsbCamInit(); +void __UsbCamShutdown(); + namespace Camera { void pushCameraImage(long long length, unsigned char *image); }