From 99044467586a5a37c796a7786834359dfaf30a2c Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Thu, 10 Jun 2021 07:08:30 +0300 Subject: Media: Estimate memory use In-memory images, audio, and downloads are included in RAM usage in Debug Information. --- src/audio/player.c | 7 +++++++ src/audio/player.h | 1 + src/media.c | 25 +++++++++++++++++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/audio/player.c b/src/audio/player.c index 9e026561..a2a3955b 100644 --- a/src/audio/player.c +++ b/src/audio/player.c @@ -724,6 +724,13 @@ void updateSourceData_Player(iPlayer *d, const iString *mimeType, const iBlock * unlock_Mutex(&input->mtx); } +size_t sourceDataSize_Player(const iPlayer *d) { + lock_Mutex(&d->data->mtx); + const size_t size = size_Block(&d->data->data); + unlock_Mutex(&d->data->mtx); + return size; +} + iBool start_Player(iPlayer *d) { if (isStarted_Player(d)) { return iFalse; diff --git a/src/audio/player.h b/src/audio/player.h index 8753d811..b131838d 100644 --- a/src/audio/player.h +++ b/src/audio/player.h @@ -48,6 +48,7 @@ enum iPlayerTag { void updateSourceData_Player (iPlayer *, const iString *mimeType, const iBlock *data, enum iPlayerUpdate update); +size_t sourceDataSize_Player (const iPlayer *); iBool start_Player (iPlayer *); void stop_Player (iPlayer *); diff --git a/src/media.c b/src/media.c index 180c28e6..eb4a8311 100644 --- a/src/media.c +++ b/src/media.c @@ -24,6 +24,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "gmdocument.h" #include "gmrequest.h" #include "ui/window.h" +#include "ui/paint.h" /* size_SDLTexture */ #include "audio/player.h" #include "app.h" #include "stb_image.h" @@ -262,8 +263,28 @@ void clear_Media(iMedia *d) { } size_t memorySize_Media(const iMedia *d) { - /* TODO: Calculate the actual memory use. */ - return 0; + size_t memSize = 0; + iConstForEach(PtrArray, i, &d->images) { + const iGmImage *img = i.ptr; + if (img->texture) { + const iInt2 texSize = size_SDLTexture(img->texture); + memSize += 4 * texSize.x * texSize.y; /* RGBA */ + } + else { + memSize += size_Block(&img->partialData); + } + } + iConstForEach(PtrArray, a, &d->audio) { + const iGmAudio *audio = a.ptr; + if (audio->player) { + memSize += sourceDataSize_Player(audio->player); + } + } + iConstForEach(PtrArray, n, &d->downloads) { + const iGmDownload *down = n.ptr; + memSize += down->numBytes; + } + return memSize; } iBool setDownloadUrl_Media(iMedia *d, iGmLinkId linkId, const iString *url) { -- cgit v1.2.3