2025-07-06 20:43:53 +03:00
|
|
|
#include "ImageResourceDisplay.hpp"
|
|
|
|
|
#include <include/codec/SkPngDecoder.h>
|
|
|
|
|
#include <include/codec/SkJpegDecoder.h>
|
|
|
|
|
#include <include/codec/SkBmpDecoder.h>
|
|
|
|
|
#include <include/codec/SkIcoDecoder.h>
|
|
|
|
|
#include <include/codec/SkGifDecoder.h>
|
2025-08-13 19:23:14 +03:00
|
|
|
//#include <include/codec/SkRawDecoder.h>
|
2025-08-11 21:34:01 +03:00
|
|
|
#include <include/codec/SkWbmpDecoder.h>
|
|
|
|
|
#include <include/codec/SkWebpDecoder.h>
|
2025-07-06 20:43:53 +03:00
|
|
|
#include "../MainProgram.hpp"
|
|
|
|
|
#include <Helpers/Logger.hpp>
|
|
|
|
|
|
2025-08-13 19:23:14 +03:00
|
|
|
std::array<const SkCodecs::Decoder, 7> decoders = {
|
2025-07-06 20:43:53 +03:00
|
|
|
SkBmpDecoder::Decoder(),
|
2025-08-11 21:34:01 +03:00
|
|
|
SkGifDecoder::Decoder(),
|
2025-07-06 20:43:53 +03:00
|
|
|
SkIcoDecoder::Decoder(),
|
2025-08-11 21:34:01 +03:00
|
|
|
SkJpegDecoder::Decoder(),
|
|
|
|
|
SkPngDecoder::Decoder(),
|
2025-08-13 19:23:14 +03:00
|
|
|
// SkRawDecoder::Decoder(),
|
2025-08-11 21:34:01 +03:00
|
|
|
SkWbmpDecoder::Decoder(),
|
|
|
|
|
SkWebpDecoder::Decoder()
|
2025-07-06 20:43:53 +03:00
|
|
|
};
|
|
|
|
|
|
2025-10-10 18:41:08 +03:00
|
|
|
void ImageResourceDisplay::load_thread() {
|
|
|
|
|
for(int i = 0; i < totalFramesToLoad; i++) {
|
|
|
|
|
if(shutdownLoadThread)
|
|
|
|
|
break;
|
|
|
|
|
SkCodec::Options imageOptions;
|
|
|
|
|
imageOptions.fFrameIndex = i;
|
|
|
|
|
SkCodec::FrameInfo frameInfo;
|
|
|
|
|
float frameTime = -1.0f;
|
|
|
|
|
if(codec->getFrameInfo(i, &frameInfo))
|
|
|
|
|
frameTime = frameInfo.fDuration;
|
|
|
|
|
auto decodeResult = codec->getImage(imageInfo, &imageOptions);
|
|
|
|
|
if(std::get<1>(decodeResult) == SkCodec::Result::kSuccess)
|
|
|
|
|
frames[i] = {std::get<0>(decodeResult)->withDefaultMipmaps(), frameTime};
|
|
|
|
|
else {
|
|
|
|
|
Logger::get().log("WORLDFATAL", "Could not decode image, got error code " + std::to_string(std::get<1>(decodeResult)) + ". Image has dimensions " + std::to_string(imageInfo.width()) + " " + std::to_string(imageInfo.height()));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
loadedFrames++;
|
|
|
|
|
}
|
|
|
|
|
imageRawData = nullptr;
|
|
|
|
|
codec = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
bool ImageResourceDisplay::update_draw() const {
|
|
|
|
|
return mustUpdateDraw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageResourceDisplay::update(World& w) {
|
2025-08-07 22:27:54 +03:00
|
|
|
mustUpdateDraw = false;
|
2025-10-10 18:41:08 +03:00
|
|
|
if(!loadedFirstFrame && loadedFrames >= 1) {
|
|
|
|
|
mustUpdateDraw = true;
|
|
|
|
|
loadedFirstFrame = true;
|
|
|
|
|
}
|
|
|
|
|
if(loadedFrames <= 1)
|
2025-07-06 20:43:53 +03:00
|
|
|
return;
|
|
|
|
|
currentTime += w.main.deltaTime * 1000.0;
|
|
|
|
|
for(;;) {
|
|
|
|
|
if(currentTime >= frames[frameIndex].duration) {
|
|
|
|
|
currentTime -= frames[frameIndex].duration;
|
|
|
|
|
frameIndex++;
|
2025-10-10 18:41:08 +03:00
|
|
|
if(frameIndex >= loadedFrames) {
|
|
|
|
|
if(loadedFrames != totalFramesToLoad) // Dont loop the GIF if it's not completely loaded yet
|
|
|
|
|
frameIndex--;
|
|
|
|
|
else
|
|
|
|
|
frameIndex = 0;
|
|
|
|
|
}
|
2025-07-06 20:43:53 +03:00
|
|
|
mustUpdateDraw = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return;
|
|
|
|
|
if(frames[frameIndex].duration <= 0.0f)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-07 17:45:10 +03:00
|
|
|
bool ImageResourceDisplay::load(ResourceManager& rMan, const std::string& fileName, const std::string& fileData) {
|
2025-10-10 18:41:08 +03:00
|
|
|
imageRawData = SkData::MakeWithoutCopy(fileData.c_str(), fileData.size());
|
|
|
|
|
codec = SkCodec::MakeFromData(imageRawData, decoders);
|
2025-08-01 20:07:47 +03:00
|
|
|
mustUpdateDraw = true;
|
2025-07-06 20:43:53 +03:00
|
|
|
if(codec) {
|
|
|
|
|
auto origin = codec->getOrigin();
|
|
|
|
|
bool rotate = (origin == kLeftTop_SkEncodedOrigin || origin == kRightTop_SkEncodedOrigin || origin == kRightBottom_SkEncodedOrigin || origin == kLeftBottom_SkEncodedOrigin);
|
2025-10-10 18:41:08 +03:00
|
|
|
totalFramesToLoad = codec->getFrameCount();
|
|
|
|
|
imageInfo = codec->getInfo();
|
|
|
|
|
frames.resize(totalFramesToLoad);
|
2025-07-06 20:43:53 +03:00
|
|
|
if(rotate) {
|
|
|
|
|
Logger::get().log("INFO", "Image loaded is rotated, so must rotate dimensions");
|
|
|
|
|
imageInfo = imageInfo.makeDimensions(SkISize(imageInfo.height(), imageInfo.width()));
|
|
|
|
|
}
|
2025-10-10 18:41:08 +03:00
|
|
|
try{
|
|
|
|
|
Logger::get().log("INFO", "[ImageResourceDisplay::load] Failed to create load thread, loading on main thread");
|
|
|
|
|
loadedFrames = 0;
|
|
|
|
|
loadThread = std::make_unique<std::thread>(&ImageResourceDisplay::load_thread, this);
|
|
|
|
|
}
|
|
|
|
|
catch(...) {
|
|
|
|
|
load_thread();
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageResourceDisplay::draw(SkCanvas* canvas, const DrawData& drawData, const SkRect& imRect) {
|
2025-10-10 18:41:08 +03:00
|
|
|
if(loadedFrames == 0)
|
|
|
|
|
canvas->drawRect(imRect, SkPaint({0.5f, 0.5f, 0.5f, 0.5f}));
|
|
|
|
|
else
|
|
|
|
|
canvas->drawImageRect(frames[frameIndex].data, imRect, {SkFilterMode::kLinear, SkMipmapMode::kLinear});
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector2f ImageResourceDisplay::get_dimensions() const {
|
2025-10-10 18:41:08 +03:00
|
|
|
return {imageInfo.width(), imageInfo.height()};
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float ImageResourceDisplay::get_dimension_scale() const {
|
|
|
|
|
return 0.3f;
|
|
|
|
|
}
|
2025-10-09 16:56:05 +03:00
|
|
|
|
|
|
|
|
ResourceDisplay::Type ImageResourceDisplay::get_type() const {
|
|
|
|
|
return ResourceDisplay::Type::IMAGE;
|
|
|
|
|
}
|
2025-10-10 18:41:08 +03:00
|
|
|
|
|
|
|
|
ImageResourceDisplay::~ImageResourceDisplay() {
|
|
|
|
|
if(loadThread) {
|
|
|
|
|
shutdownLoadThread = true;
|
|
|
|
|
loadThread->join();
|
|
|
|
|
}
|
|
|
|
|
}
|