Load image data in a different thread

This commit is contained in:
Yousef Khadadeh 2025-10-10 18:41:08 +03:00
parent d633e549bc
commit 1175424c23
6 changed files with 93 additions and 40 deletions

View file

@ -81,7 +81,7 @@ void DrawImage::draw(SkCanvas* canvas, const DrawData& drawData) {
if(drawSetupData.shouldDraw) { if(drawSetupData.shouldDraw) {
canvas->save(); canvas->save();
canvas_do_calculated_transform(canvas); canvas_do_calculated_transform(canvas);
std::shared_ptr<ResourceDisplay> display = drawData.rMan->get_display_data(d.imageID); ResourceDisplay* display = drawData.rMan->get_display_data(d.imageID);
if(display) if(display)
display->draw(canvas, drawData, imRect); display->draw(canvas, drawData, imRect);
else else
@ -91,7 +91,7 @@ void DrawImage::draw(SkCanvas* canvas, const DrawData& drawData) {
} }
void DrawImage::update(DrawingProgram& drawP) { void DrawImage::update(DrawingProgram& drawP) {
std::shared_ptr<ResourceDisplay> display = drawP.world.drawData.rMan->get_display_data(d.imageID); ResourceDisplay* display = drawP.world.drawData.rMan->get_display_data(d.imageID);
if(display) { if(display) {
if(display->update_draw() && collabListInfo.lock()) if(display->update_draw() && collabListInfo.lock())
drawP.invalidate_cache_at_component(collabListInfo.lock()); drawP.invalidate_cache_at_component(collabListInfo.lock());

View file

@ -463,7 +463,7 @@ void DrawingProgram::update_downloading_dropped_files() {
ResourceData newResource; ResourceData newResource;
newResource.data = std::make_shared<std::string>(downFile.downData->str); newResource.data = std::make_shared<std::string>(downFile.downData->str);
ServerClientID imageID = world.rMan.add_resource(newResource); ServerClientID imageID = world.rMan.add_resource(newResource);
std::shared_ptr<ResourceDisplay> display = world.rMan.get_display_data(imageID); ResourceDisplay* display = world.rMan.get_display_data(imageID);
if(display->get_type() == ResourceDisplay::Type::FILE) { if(display->get_type() == ResourceDisplay::Type::FILE) {
Logger::get().log("WORLDFATAL", "Failed to parse image from URL"); Logger::get().log("WORLDFATAL", "Failed to parse image from URL");
client_erase_set({downFile.comp}); client_erase_set({downFile.comp});
@ -504,7 +504,7 @@ void DrawingProgram::add_file_to_canvas_by_path(const std::string& filePath, Vec
void DrawingProgram::add_file_to_canvas_by_path_execute(const std::string& filePath, Vector2f dropPos) { void DrawingProgram::add_file_to_canvas_by_path_execute(const std::string& filePath, Vector2f dropPos) {
ServerClientID imageID = world.rMan.add_resource_file(filePath); ServerClientID imageID = world.rMan.add_resource_file(filePath);
std::shared_ptr<ResourceDisplay> display = world.rMan.get_display_data(imageID); ResourceDisplay* display = world.rMan.get_display_data(imageID);
Vector2f imTrueDim = display->get_dimensions(); Vector2f imTrueDim = display->get_dimensions();
auto img(std::make_shared<DrawImage>()); auto img(std::make_shared<DrawImage>());
img->coords = world.drawData.cam.c; img->coords = world.drawData.cam.c;
@ -527,7 +527,7 @@ void DrawingProgram::add_file_to_canvas_by_data(const std::string& fileName, std
newResource.name = fileName; newResource.name = fileName;
ServerClientID imageID = world.rMan.add_resource(newResource); ServerClientID imageID = world.rMan.add_resource(newResource);
std::shared_ptr<ResourceDisplay> display = world.rMan.get_display_data(imageID); ResourceDisplay* display = world.rMan.get_display_data(imageID);
Vector2f imTrueDim = display->get_dimensions(); Vector2f imTrueDim = display->get_dimensions();
auto img(std::make_shared<DrawImage>()); auto img(std::make_shared<DrawImage>());
img->coords = world.drawData.cam.c; img->coords = world.drawData.cam.c;

View file

@ -21,21 +21,52 @@ std::array<const SkCodecs::Decoder, 7> decoders = {
SkWebpDecoder::Decoder() SkWebpDecoder::Decoder()
}; };
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;
}
bool ImageResourceDisplay::update_draw() const { bool ImageResourceDisplay::update_draw() const {
return mustUpdateDraw; return mustUpdateDraw;
} }
void ImageResourceDisplay::update(World& w) { void ImageResourceDisplay::update(World& w) {
mustUpdateDraw = false; mustUpdateDraw = false;
if(frames.size() <= 1) if(!loadedFirstFrame && loadedFrames >= 1) {
mustUpdateDraw = true;
loadedFirstFrame = true;
}
if(loadedFrames <= 1)
return; return;
currentTime += w.main.deltaTime * 1000.0; currentTime += w.main.deltaTime * 1000.0;
for(;;) { for(;;) {
if(currentTime >= frames[frameIndex].duration) { if(currentTime >= frames[frameIndex].duration) {
currentTime -= frames[frameIndex].duration; currentTime -= frames[frameIndex].duration;
frameIndex++; frameIndex++;
if(frameIndex >= frames.size()) if(frameIndex >= loadedFrames) {
if(loadedFrames != totalFramesToLoad) // Dont loop the GIF if it's not completely loaded yet
frameIndex--;
else
frameIndex = 0; frameIndex = 0;
}
mustUpdateDraw = true; mustUpdateDraw = true;
} }
else else
@ -46,30 +77,26 @@ void ImageResourceDisplay::update(World& w) {
} }
bool ImageResourceDisplay::load(ResourceManager& rMan, const std::string& fileName, const std::string& fileData) { bool ImageResourceDisplay::load(ResourceManager& rMan, const std::string& fileName, const std::string& fileData) {
sk_sp<SkData> newData = SkData::MakeWithoutCopy(fileData.c_str(), fileData.size()); imageRawData = SkData::MakeWithoutCopy(fileData.c_str(), fileData.size());
std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(newData, decoders); codec = SkCodec::MakeFromData(imageRawData, decoders);
mustUpdateDraw = true; mustUpdateDraw = true;
if(codec) { if(codec) {
auto origin = codec->getOrigin(); auto origin = codec->getOrigin();
bool rotate = (origin == kLeftTop_SkEncodedOrigin || origin == kRightTop_SkEncodedOrigin || origin == kRightBottom_SkEncodedOrigin || origin == kLeftBottom_SkEncodedOrigin); bool rotate = (origin == kLeftTop_SkEncodedOrigin || origin == kRightTop_SkEncodedOrigin || origin == kRightBottom_SkEncodedOrigin || origin == kLeftBottom_SkEncodedOrigin);
int frameCount = codec->getFrameCount(); totalFramesToLoad = codec->getFrameCount();
SkImageInfo imageInfo = codec->getInfo(); imageInfo = codec->getInfo();
frames.resize(totalFramesToLoad);
if(rotate) { if(rotate) {
Logger::get().log("INFO", "Image loaded is rotated, so must rotate dimensions"); Logger::get().log("INFO", "Image loaded is rotated, so must rotate dimensions");
imageInfo = imageInfo.makeDimensions(SkISize(imageInfo.height(), imageInfo.width())); imageInfo = imageInfo.makeDimensions(SkISize(imageInfo.height(), imageInfo.width()));
} }
for(int i = 0; i < frameCount; i++) { try{
SkCodec::Options imageOptions; Logger::get().log("INFO", "[ImageResourceDisplay::load] Failed to create load thread, loading on main thread");
imageOptions.fFrameIndex = i; loadedFrames = 0;
SkCodec::FrameInfo frameInfo; loadThread = std::make_unique<std::thread>(&ImageResourceDisplay::load_thread, this);
float frameTime = -1.0f; }
if(codec->getFrameInfo(i, &frameInfo)) catch(...) {
frameTime = frameInfo.fDuration; load_thread();
auto decodeResult = codec->getImage(imageInfo, &imageOptions);
if(std::get<1>(decodeResult) == SkCodec::Result::kSuccess)
frames.emplace_back(std::get<0>(decodeResult)->withDefaultMipmaps(), frameTime);
else
throw std::runtime_error("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()));
} }
return true; return true;
} }
@ -77,11 +104,14 @@ bool ImageResourceDisplay::load(ResourceManager& rMan, const std::string& fileNa
} }
void ImageResourceDisplay::draw(SkCanvas* canvas, const DrawData& drawData, const SkRect& imRect) { void ImageResourceDisplay::draw(SkCanvas* canvas, const DrawData& drawData, const SkRect& imRect) {
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}); canvas->drawImageRect(frames[frameIndex].data, imRect, {SkFilterMode::kLinear, SkMipmapMode::kLinear});
} }
Vector2f ImageResourceDisplay::get_dimensions() const { Vector2f ImageResourceDisplay::get_dimensions() const {
return {frames.front().data->width(), frames.front().data->height()}; return {imageInfo.width(), imageInfo.height()};
} }
float ImageResourceDisplay::get_dimension_scale() const { float ImageResourceDisplay::get_dimension_scale() const {
@ -91,3 +121,10 @@ float ImageResourceDisplay::get_dimension_scale() const {
ResourceDisplay::Type ImageResourceDisplay::get_type() const { ResourceDisplay::Type ImageResourceDisplay::get_type() const {
return ResourceDisplay::Type::IMAGE; return ResourceDisplay::Type::IMAGE;
} }
ImageResourceDisplay::~ImageResourceDisplay() {
if(loadThread) {
shutdownLoadThread = true;
loadThread->join();
}
}

View file

@ -2,6 +2,8 @@
#include "ResourceDisplay.hpp" #include "ResourceDisplay.hpp"
#include <include/core/SkImage.h> #include <include/core/SkImage.h>
#include <vector> #include <vector>
#include <thread>
#include <include/codec/SkCodec.h>
class ImageResourceDisplay : public ResourceDisplay { class ImageResourceDisplay : public ResourceDisplay {
public: public:
@ -12,13 +14,27 @@ class ImageResourceDisplay : public ResourceDisplay {
virtual Vector2f get_dimensions() const override; virtual Vector2f get_dimensions() const override;
virtual float get_dimension_scale() const override; virtual float get_dimension_scale() const override;
virtual Type get_type() const override; virtual Type get_type() const override;
virtual ~ImageResourceDisplay() override;
private: private:
struct FrameData { struct FrameData {
sk_sp<SkImage> data; sk_sp<SkImage> data;
float duration; float duration;
}; };
std::vector<FrameData> frames; std::vector<FrameData> frames;
SkImageInfo imageInfo;
void load_thread();
int totalFramesToLoad;
std::atomic<int> loadedFrames = 0;
sk_sp<SkData> imageRawData;
std::unique_ptr<SkCodec> codec;
std::atomic<bool> shutdownLoadThread = false;
std::unique_ptr<std::thread> loadThread;
float currentTime = 0.0f; float currentTime = 0.0f;
size_t frameIndex = 0; int frameIndex = 0;
bool mustUpdateDraw = false; bool mustUpdateDraw = false;
bool loadedFirstFrame = false;
}; };

View file

@ -141,31 +141,31 @@ std::optional<ResourceData> ResourceManager::get_resource(const ServerClientID&
return it->second; return it->second;
} }
std::shared_ptr<ResourceDisplay> ResourceManager::get_display_data(const ServerClientID& fileID) { ResourceDisplay* ResourceManager::get_display_data(const ServerClientID& fileID) {
auto loadedDisplayIt = displays.find(fileID); auto loadedDisplayIt = displays.find(fileID);
if(loadedDisplayIt != displays.end()) if(loadedDisplayIt != displays.end())
return loadedDisplayIt->second; return loadedDisplayIt->second.get();
auto resourceIt = resources.find(fileID); auto resourceIt = resources.find(fileID);
if(resourceIt == resources.end()) if(resourceIt == resources.end())
return nullptr; return nullptr;
auto imgResource(std::make_shared<ImageResourceDisplay>()); auto imgResource(std::make_unique<ImageResourceDisplay>());
if(imgResource->load(*this, resourceIt->second.name, *resourceIt->second.data)) { if(imgResource->load(*this, resourceIt->second.name, *resourceIt->second.data)) {
displays[fileID] = imgResource; displays.emplace(fileID, std::move(imgResource));
return imgResource; return displays[fileID].get();
} }
auto svgResource(std::make_shared<SvgResourceDisplay>()); auto svgResource(std::make_unique<SvgResourceDisplay>());
if(svgResource->load(*this, resourceIt->second.name, *resourceIt->second.data)) { if(svgResource->load(*this, resourceIt->second.name, *resourceIt->second.data)) {
displays[fileID] = svgResource; displays.emplace(fileID, std::move(svgResource));
return svgResource; return displays[fileID].get();
} }
auto fileResource(std::make_shared<FileResourceDisplay>()); auto fileResource(std::make_unique<FileResourceDisplay>());
fileResource->load(*this, resourceIt->second.name, *resourceIt->second.data); fileResource->load(*this, resourceIt->second.name, *resourceIt->second.data);
displays[fileID] = fileResource; displays.emplace(fileID, std::move(fileResource));
return fileResource; return displays[fileID].get();
} }
const std::unordered_map<ServerClientID, ResourceData>& ResourceManager::resource_list() { const std::unordered_map<ServerClientID, ResourceData>& ResourceManager::resource_list() {

View file

@ -41,7 +41,7 @@ class ResourceManager {
void init_client_callbacks(); void init_client_callbacks();
ServerClientID add_resource_file(std::string_view fileName); ServerClientID add_resource_file(std::string_view fileName);
ServerClientID add_resource(const ResourceData& resource); ServerClientID add_resource(const ResourceData& resource);
std::shared_ptr<ResourceDisplay> get_display_data(const ServerClientID& fileID); ResourceDisplay* get_display_data(const ServerClientID& fileID);
std::optional<ResourceData> get_resource(const ServerClientID& id) const; std::optional<ResourceData> get_resource(const ServerClientID& id) const;
ClientPortionID get_max_id(ServerPortionID serverID) const; ClientPortionID get_max_id(ServerPortionID serverID) const;
const std::unordered_map<ServerClientID, ResourceData>& resource_list(); const std::unordered_map<ServerClientID, ResourceData>& resource_list();
@ -53,7 +53,7 @@ class ResourceManager {
ServerClientID resourceBeingRetrievedClient = {0, 0}; ServerClientID resourceBeingRetrievedClient = {0, 0};
std::unordered_map<ServerClientID, ResourceData> resources; std::unordered_map<ServerClientID, ResourceData> resources;
std::unordered_map<ServerClientID, std::shared_ptr<ResourceDisplay>> displays; std::unordered_map<ServerClientID, std::unique_ptr<ResourceDisplay>> displays;
static std::array<const SkCodecs::Decoder, 5> decoders; static std::array<const SkCodecs::Decoder, 5> decoders;
}; };