From 208e23de77ad6d104f13a0bb591ae16c4a805fe9 Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 26 Oct 2021 23:20:45 +0800 Subject: *load image async --- Runtime/FileSystem/ImageJobs.cpp | 54 ++++++++++++++++++++++++++++++++++++++++ Runtime/FileSystem/ImageJobs.h | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 Runtime/FileSystem/ImageJobs.cpp create mode 100644 Runtime/FileSystem/ImageJobs.h (limited to 'Runtime/FileSystem') diff --git a/Runtime/FileSystem/ImageJobs.cpp b/Runtime/FileSystem/ImageJobs.cpp new file mode 100644 index 0000000..84e4ff3 --- /dev/null +++ b/Runtime/FileSystem/ImageJobs.cpp @@ -0,0 +1,54 @@ +#include "ImageJobs.h" +#include "ThirdParty/stb/stb_image.h" +#include "Runtime/Graphics/ImageData.h" + +//-------------------------------------------------------------------------- + +ReadImageFileJob::ReadImageFileJob(LuaBind::VM* vm, int indexOfCallback, const char* path) + : isFinished(false) + , callback(vm) +{ + this->path = path; + LuaBind::State state = vm->GetCurThread(); + callback.SetRef(state, indexOfCallback); +} + +ReadImageFileJob::~ReadImageFileJob() +{ + callback.UnRef(); + delete bridge.pixels; //for safe +} + +void ReadImageFileJob::Process() +{ + stbi_set_flip_vertically_on_load(true); + bridge.pixels = stbi_load(path.c_str(), &bridge.width, &bridge.height, &bridge.channels, 0); + isFinished = true; +} + +bool ReadImageFileJob::IsFinished() +{ + return isFinished; +} + +// callback(imageData) +void ReadImageFileJob::Dispacth(void* param) +{ + LUA_BIND_STATE((lua_State*)param); + + ImageData* imgData = new ImageData(state.GetVM()); + imgData->pixels = bridge.pixels; + bridge.pixels = NULL; + imgData->width = bridge.width; + imgData->height = bridge.height; + imgData->format = ImageData::EPixelFormat::RGB; + imgData->type = ImageData::EPixelElementType::UNSIGNED_BYTE; + + callback.PushRef(state); + imgData->PushUserdata(state); + state.Call(1, 0); +} + +//-------------------------------------------------------------------------- + + diff --git a/Runtime/FileSystem/ImageJobs.h b/Runtime/FileSystem/ImageJobs.h new file mode 100644 index 0000000..77e48e0 --- /dev/null +++ b/Runtime/FileSystem/ImageJobs.h @@ -0,0 +1,46 @@ +#pragma once +#include +#include "Runtime/Threading/Job.h" +#include "Runtime/Lua/LuaHelper.h" +#include "Runtime/Threading/JobSystem.h" + +struct ImageDataBridge +{ + void* pixels; + int width; + int height; + int channels; +}; + +// in: string +// out: ImageData +class ReadImageFileJob : public Job +{ +public: + ReadImageFileJob(LuaBind::VM* vm, int indexOfCallback, const char* path); + ~ReadImageFileJob(); + + void Dispacth(void* param) override; + void Process() override; + bool IsFinished() override; + +private: + std::string path; + bool isFinished; + ImageDataBridge bridge; + LuaBind::StrongRef callback; // 完成后的回调函数 +}; + +// in: string[] +// out: ImageData[] +class ReadImageFilesJob : public Job +{ + +}; + +// in: DataBuffer[] +// out: ImageData[] +class DecodeImageFilesJob : public Job +{ + +}; \ No newline at end of file -- cgit v1.1-26-g67d0