Load image data in a different thread
This commit is contained in:
parent
d633e549bc
commit
1175424c23
6 changed files with 93 additions and 40 deletions
|
|
@ -81,7 +81,7 @@ void DrawImage::draw(SkCanvas* canvas, const DrawData& drawData) {
|
|||
if(drawSetupData.shouldDraw) {
|
||||
canvas->save();
|
||||
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)
|
||||
display->draw(canvas, drawData, imRect);
|
||||
else
|
||||
|
|
@ -91,7 +91,7 @@ void DrawImage::draw(SkCanvas* canvas, const DrawData& drawData) {
|
|||
}
|
||||
|
||||
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->update_draw() && collabListInfo.lock())
|
||||
drawP.invalidate_cache_at_component(collabListInfo.lock());
|
||||
|
|
|
|||
|
|
@ -463,7 +463,7 @@ void DrawingProgram::update_downloading_dropped_files() {
|
|||
ResourceData newResource;
|
||||
newResource.data = std::make_shared<std::string>(downFile.downData->str);
|
||||
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) {
|
||||
Logger::get().log("WORLDFATAL", "Failed to parse image from URL");
|
||||
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) {
|
||||
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();
|
||||
auto img(std::make_shared<DrawImage>());
|
||||
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;
|
||||
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();
|
||||
auto img(std::make_shared<DrawImage>());
|
||||
img->coords = world.drawData.cam.c;
|
||||
|
|
|
|||
|
|
@ -21,21 +21,52 @@ std::array<const SkCodecs::Decoder, 7> decoders = {
|
|||
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 {
|
||||
return mustUpdateDraw;
|
||||
}
|
||||
|
||||
void ImageResourceDisplay::update(World& w) {
|
||||
mustUpdateDraw = false;
|
||||
if(frames.size() <= 1)
|
||||
if(!loadedFirstFrame && loadedFrames >= 1) {
|
||||
mustUpdateDraw = true;
|
||||
loadedFirstFrame = true;
|
||||
}
|
||||
if(loadedFrames <= 1)
|
||||
return;
|
||||
currentTime += w.main.deltaTime * 1000.0;
|
||||
for(;;) {
|
||||
if(currentTime >= frames[frameIndex].duration) {
|
||||
currentTime -= frames[frameIndex].duration;
|
||||
frameIndex++;
|
||||
if(frameIndex >= frames.size())
|
||||
frameIndex = 0;
|
||||
if(frameIndex >= loadedFrames) {
|
||||
if(loadedFrames != totalFramesToLoad) // Dont loop the GIF if it's not completely loaded yet
|
||||
frameIndex--;
|
||||
else
|
||||
frameIndex = 0;
|
||||
}
|
||||
mustUpdateDraw = true;
|
||||
}
|
||||
else
|
||||
|
|
@ -46,30 +77,26 @@ void ImageResourceDisplay::update(World& w) {
|
|||
}
|
||||
|
||||
bool ImageResourceDisplay::load(ResourceManager& rMan, const std::string& fileName, const std::string& fileData) {
|
||||
sk_sp<SkData> newData = SkData::MakeWithoutCopy(fileData.c_str(), fileData.size());
|
||||
std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(newData, decoders);
|
||||
imageRawData = SkData::MakeWithoutCopy(fileData.c_str(), fileData.size());
|
||||
codec = SkCodec::MakeFromData(imageRawData, decoders);
|
||||
mustUpdateDraw = true;
|
||||
if(codec) {
|
||||
auto origin = codec->getOrigin();
|
||||
bool rotate = (origin == kLeftTop_SkEncodedOrigin || origin == kRightTop_SkEncodedOrigin || origin == kRightBottom_SkEncodedOrigin || origin == kLeftBottom_SkEncodedOrigin);
|
||||
int frameCount = codec->getFrameCount();
|
||||
SkImageInfo imageInfo = codec->getInfo();
|
||||
totalFramesToLoad = codec->getFrameCount();
|
||||
imageInfo = codec->getInfo();
|
||||
frames.resize(totalFramesToLoad);
|
||||
if(rotate) {
|
||||
Logger::get().log("INFO", "Image loaded is rotated, so must rotate dimensions");
|
||||
imageInfo = imageInfo.makeDimensions(SkISize(imageInfo.height(), imageInfo.width()));
|
||||
}
|
||||
for(int i = 0; i < frameCount; i++) {
|
||||
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.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()));
|
||||
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();
|
||||
}
|
||||
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) {
|
||||
canvas->drawImageRect(frames[frameIndex].data, imRect, {SkFilterMode::kLinear, SkMipmapMode::kLinear});
|
||||
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});
|
||||
}
|
||||
|
||||
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 {
|
||||
|
|
@ -91,3 +121,10 @@ float ImageResourceDisplay::get_dimension_scale() const {
|
|||
ResourceDisplay::Type ImageResourceDisplay::get_type() const {
|
||||
return ResourceDisplay::Type::IMAGE;
|
||||
}
|
||||
|
||||
ImageResourceDisplay::~ImageResourceDisplay() {
|
||||
if(loadThread) {
|
||||
shutdownLoadThread = true;
|
||||
loadThread->join();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
#include "ResourceDisplay.hpp"
|
||||
#include <include/core/SkImage.h>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <include/codec/SkCodec.h>
|
||||
|
||||
class ImageResourceDisplay : public ResourceDisplay {
|
||||
public:
|
||||
|
|
@ -12,13 +14,27 @@ class ImageResourceDisplay : public ResourceDisplay {
|
|||
virtual Vector2f get_dimensions() const override;
|
||||
virtual float get_dimension_scale() const override;
|
||||
virtual Type get_type() const override;
|
||||
virtual ~ImageResourceDisplay() override;
|
||||
private:
|
||||
struct FrameData {
|
||||
sk_sp<SkImage> data;
|
||||
float duration;
|
||||
};
|
||||
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;
|
||||
size_t frameIndex = 0;
|
||||
int frameIndex = 0;
|
||||
bool mustUpdateDraw = false;
|
||||
bool loadedFirstFrame = false;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -141,31 +141,31 @@ std::optional<ResourceData> ResourceManager::get_resource(const ServerClientID&
|
|||
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);
|
||||
if(loadedDisplayIt != displays.end())
|
||||
return loadedDisplayIt->second;
|
||||
return loadedDisplayIt->second.get();
|
||||
|
||||
auto resourceIt = resources.find(fileID);
|
||||
if(resourceIt == resources.end())
|
||||
return nullptr;
|
||||
|
||||
auto imgResource(std::make_shared<ImageResourceDisplay>());
|
||||
auto imgResource(std::make_unique<ImageResourceDisplay>());
|
||||
if(imgResource->load(*this, resourceIt->second.name, *resourceIt->second.data)) {
|
||||
displays[fileID] = imgResource;
|
||||
return imgResource;
|
||||
displays.emplace(fileID, std::move(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)) {
|
||||
displays[fileID] = svgResource;
|
||||
return svgResource;
|
||||
displays.emplace(fileID, std::move(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);
|
||||
displays[fileID] = fileResource;
|
||||
return fileResource;
|
||||
displays.emplace(fileID, std::move(fileResource));
|
||||
return displays[fileID].get();
|
||||
}
|
||||
|
||||
const std::unordered_map<ServerClientID, ResourceData>& ResourceManager::resource_list() {
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ class ResourceManager {
|
|||
void init_client_callbacks();
|
||||
ServerClientID add_resource_file(std::string_view fileName);
|
||||
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;
|
||||
ClientPortionID get_max_id(ServerPortionID serverID) const;
|
||||
const std::unordered_map<ServerClientID, ResourceData>& resource_list();
|
||||
|
|
@ -53,7 +53,7 @@ class ResourceManager {
|
|||
ServerClientID resourceBeingRetrievedClient = {0, 0};
|
||||
|
||||
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;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue