2025-07-06 20:43:53 +03:00
|
|
|
#include "ImageResourceDisplay.hpp"
|
2026-02-17 14:41:08 +03:00
|
|
|
#include <chrono>
|
2025-07-06 20:43:53 +03:00
|
|
|
#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>
|
2026-01-06 12:36:15 +03:00
|
|
|
#include <include/core/SkCanvas.h>
|
2025-07-06 20:43:53 +03:00
|
|
|
#include "../MainProgram.hpp"
|
|
|
|
|
#include <Helpers/Logger.hpp>
|
|
|
|
|
|
2026-02-16 18:19:17 +03:00
|
|
|
#ifdef USE_SKIA_BACKEND_GRAPHITE
|
|
|
|
|
#include <include/gpu/graphite/Surface.h>
|
|
|
|
|
#elif USE_SKIA_BACKEND_GANESH
|
|
|
|
|
#include <include/gpu/ganesh/GrDirectContext.h>
|
|
|
|
|
#include <include/gpu/ganesh/SkSurfaceGanesh.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
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)
|
2026-02-16 18:19:17 +03:00
|
|
|
frames[i] = {std::get<0>(decodeResult), frameTime};
|
2025-10-10 18:41:08 +03:00
|
|
|
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) {
|
2026-02-17 14:41:08 +03:00
|
|
|
for(auto& frame : frames) {
|
|
|
|
|
std::erase_if(frame.mipmapLevels, [](const auto& mipmapPair) {
|
|
|
|
|
static constexpr std::chrono::duration TIME_TO_REMOVE_MIPMAP_CACHE = std::chrono::seconds(40);
|
|
|
|
|
return std::chrono::steady_clock::now() - mipmapPair.second.timeLastUsed > TIME_TO_REMOVE_MIPMAP_CACHE;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
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-11-17 16:08:42 +03:00
|
|
|
loadedFrames = 0;
|
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{
|
|
|
|
|
loadThread = std::make_unique<std::thread>(&ImageResourceDisplay::load_thread, this);
|
|
|
|
|
}
|
|
|
|
|
catch(...) {
|
2025-11-17 16:08:42 +03:00
|
|
|
Logger::get().log("INFO", "[ImageResourceDisplay::load] Failed to create load thread, loading on main thread");
|
2025-10-10 18:41:08 +03:00
|
|
|
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}));
|
2026-02-16 18:19:17 +03:00
|
|
|
else {
|
|
|
|
|
SkRect imRectPixelSize = canvas->getLocalToDeviceAs3x3().mapRect(imRect);
|
|
|
|
|
float imWidth = std::max(imRectPixelSize.width(), 32.0f);
|
|
|
|
|
unsigned divisionLevel = imageInfo.width() / imWidth;
|
|
|
|
|
auto& frame = frames[frameIndex];
|
|
|
|
|
if(divisionLevel <= 1)
|
|
|
|
|
canvas->drawImageRect(frame.data, imRect, {SkFilterMode::kLinear, SkMipmapMode::kNone});
|
|
|
|
|
else {
|
|
|
|
|
unsigned mipmapLevel = std::log2(divisionLevel);
|
|
|
|
|
auto mipmapIt = frame.mipmapLevels.find(mipmapLevel);
|
|
|
|
|
|
|
|
|
|
if(mipmapIt == frame.mipmapLevels.end()) {
|
|
|
|
|
unsigned divideResolutionBy = std::exp2(mipmapLevel);
|
|
|
|
|
SkISize mipmapResolution(imageInfo.width() / divideResolutionBy, imageInfo.height() / divideResolutionBy);
|
|
|
|
|
|
|
|
|
|
SkImageInfo mipmapImgInfo = imageInfo.makeDimensions(mipmapResolution);
|
|
|
|
|
sk_sp<SkImage> mipmapImage = frame.data->makeScaled(mipmapImgInfo, SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kNone));
|
2026-02-17 14:41:08 +03:00
|
|
|
frame.mipmapLevels.emplace(mipmapLevel, MipmapLevelData{mipmapImage, std::chrono::steady_clock::now()});
|
2026-02-16 18:19:17 +03:00
|
|
|
canvas->drawImageRect(mipmapImage, imRect, {SkFilterMode::kLinear, SkMipmapMode::kNone});
|
|
|
|
|
}
|
2026-02-17 14:41:08 +03:00
|
|
|
else {
|
|
|
|
|
mipmapIt->second.timeLastUsed = std::chrono::steady_clock::now();
|
|
|
|
|
canvas->drawImageRect(mipmapIt->second.imageData, imRect, {SkFilterMode::kLinear, SkMipmapMode::kNone});
|
|
|
|
|
}
|
2026-02-16 18:19:17 +03:00
|
|
|
}
|
|
|
|
|
}
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|