libgui/source/gui/GuiImageAsync.cpp

174 lines
5.7 KiB
C++
Raw Normal View History

2017-10-29 10:28:14 +01:00
/****************************************************************************
* Copyright (C) 2015 Dimok
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include <unistd.h>
2018-06-21 20:44:58 +02:00
#include <gui/GuiImageAsync.h>
#include "../fs/CFile.hpp"
2017-10-29 10:28:14 +01:00
std::vector<GuiImageAsync *> GuiImageAsync::imageQueue;
2020-08-13 12:38:07 +02:00
CThread *GuiImageAsync::pThread = NULL;
std::recursive_mutex *GuiImageAsync::pMutex = NULL;
2018-06-21 20:44:58 +02:00
uint32_t GuiImageAsync::threadRefCounter = 0;
2017-10-29 10:28:14 +01:00
bool GuiImageAsync::bExitRequested = false;
2020-08-13 12:38:07 +02:00
GuiImageAsync *GuiImageAsync::pInUse = NULL;
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
GuiImageAsync::GuiImageAsync(const uint8_t *imageBuffer, const uint32_t &imageBufferSize, GuiImageData *preloadImg)
: GuiImage(preloadImg), imgData(NULL), imgBuffer(imageBuffer), imgBufferSize(imageBufferSize) {
2018-06-21 20:44:58 +02:00
threadInit();
threadAddImage(this);
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
GuiImageAsync::GuiImageAsync(const std::string &file, GuiImageData *preloadImg)
: GuiImage(preloadImg), imgData(NULL), filename(file), imgBuffer(NULL), imgBufferSize(0) {
2018-06-21 20:44:58 +02:00
threadInit();
threadAddImage(this);
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
GuiImageAsync::~GuiImageAsync() {
threadRemoveImage(this);
2020-08-13 12:38:07 +02:00
while (pInUse == this)
2018-06-21 20:44:58 +02:00
OSSleepTicks(OSMicrosecondsToTicks(1000));
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (imgData) {
2017-10-29 10:28:14 +01:00
delete imgData;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
//threadExit();
}
2018-06-21 20:44:58 +02:00
void GuiImageAsync::threadAddImage(GuiImageAsync *Image) {
2017-10-29 10:28:14 +01:00
pMutex->lock();
2018-06-21 20:44:58 +02:00
imageQueue.push_back(Image);
2017-10-29 10:28:14 +01:00
pMutex->unlock();
2018-06-21 20:44:58 +02:00
pThread->resumeThread();
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void GuiImageAsync::threadRemoveImage(GuiImageAsync *image) {
2017-10-29 10:28:14 +01:00
pMutex->lock();
2020-08-13 12:38:07 +02:00
for (uint32_t i = 0; i < imageQueue.size(); ++i) {
if (imageQueue[i] == image) {
2018-06-21 20:44:58 +02:00
imageQueue.erase(imageQueue.begin() + i);
break;
}
}
2017-10-29 10:28:14 +01:00
pMutex->unlock();
}
2018-06-21 20:44:58 +02:00
void GuiImageAsync::clearQueue() {
2017-10-29 10:28:14 +01:00
pMutex->lock();
2018-06-21 20:44:58 +02:00
imageQueue.clear();
2017-10-29 10:28:14 +01:00
pMutex->unlock();
}
2018-06-21 20:44:58 +02:00
void GuiImageAsync::guiImageAsyncThread(CThread *thread, void *arg) {
2020-08-13 12:38:07 +02:00
while (!bExitRequested) {
2020-08-13 12:58:19 +02:00
if (imageQueue.empty() && !bExitRequested) {
2017-10-29 10:28:14 +01:00
pThread->suspendThread();
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
if (!imageQueue.empty() && !bExitRequested) {
2017-10-29 10:28:14 +01:00
pMutex->lock();
2018-06-21 20:44:58 +02:00
pInUse = imageQueue.front();
imageQueue.erase(imageQueue.begin());
2017-10-29 10:28:14 +01:00
pMutex->unlock();
2020-08-13 12:58:19 +02:00
if (!pInUse) {
2018-06-21 20:44:58 +02:00
continue;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
if (pInUse->imgBuffer && pInUse->imgBufferSize) {
2017-10-29 10:28:14 +01:00
pInUse->imgData = new GuiImageData(pInUse->imgBuffer, pInUse->imgBufferSize);
2018-06-21 20:44:58 +02:00
} else {
uint8_t *buffer = NULL;
uint64_t bufferSize = 0;
CFile file(pInUse->filename, CFile::ReadOnly);
2020-08-13 12:38:07 +02:00
if (file.isOpen()) {
uint64_t filesize = file.size();
buffer = (uint8_t *) malloc(filesize);
if (buffer != NULL) {
uint32_t blocksize = 0x4000;
uint32_t done = 0;
int32_t readBytes = 0;
2020-08-13 12:38:07 +02:00
while (done < filesize) {
if (done + blocksize > filesize) {
blocksize = filesize - done;
}
readBytes = file.read(buffer + done, blocksize);
2020-08-13 12:58:19 +02:00
if (readBytes <= 0) {
break;
2020-08-13 12:58:19 +02:00
}
done += readBytes;
}
2020-08-13 12:38:07 +02:00
if (done == filesize) {
bufferSize = filesize;
2020-08-13 12:38:07 +02:00
} else {
free(buffer);
}
}
file.close();
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
if (buffer != NULL && bufferSize > 0) {
2018-06-21 20:44:58 +02:00
pInUse->imgData = new GuiImageData(buffer, bufferSize, GX2_TEX_CLAMP_MODE_MIRROR);
2017-10-29 10:28:14 +01:00
//! free original image buffer which is converted to texture now and not needed anymore
free(buffer);
}
}
2020-08-13 12:38:07 +02:00
if (pInUse->imgData) {
if (pInUse->imgData->getTexture()) {
2017-10-29 10:28:14 +01:00
pInUse->width = pInUse->imgData->getWidth();
pInUse->height = pInUse->imgData->getHeight();
pInUse->imageData = pInUse->imgData;
2018-06-21 20:44:58 +02:00
} else {
2017-10-29 10:28:14 +01:00
delete pInUse->imgData;
pInUse->imgData = NULL;
}
}
pInUse->imageLoaded(pInUse);
2018-06-21 20:44:58 +02:00
pInUse = NULL;
}
}
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void GuiImageAsync::threadInit() {
if (pThread == NULL) {
2017-10-29 10:28:14 +01:00
bExitRequested = false;
pMutex = new std::recursive_mutex();
2018-06-21 20:44:58 +02:00
pThread = CThread::create(GuiImageAsync::guiImageAsyncThread, NULL, CThread::eAttributeAffCore1 | CThread::eAttributePinnedAff, 10);
pThread->resumeThread();
2017-10-29 10:28:14 +01:00
}
++threadRefCounter;
}
2018-06-21 20:44:58 +02:00
void GuiImageAsync::threadExit() {
2020-08-13 12:38:07 +02:00
if (threadRefCounter) {
2017-10-29 10:28:14 +01:00
--threadRefCounter;
}
2020-08-13 12:38:07 +02:00
if (/*(threadRefCounter == 0) &&*/ (pThread != NULL)) {
2018-06-21 20:44:58 +02:00
bExitRequested = true;
2017-10-29 10:28:14 +01:00
delete pThread;
delete pMutex;
pThread = NULL;
pMutex = NULL;
2018-06-21 20:44:58 +02:00
}
2017-10-29 10:28:14 +01:00
}