diff options
Diffstat (limited to 'Runtime')
-rw-r--r-- | Runtime/FileSystem/ImageJobs.cpp | 54 | ||||
-rw-r--r-- | Runtime/FileSystem/ImageJobs.h | 46 | ||||
-rw-r--r-- | Runtime/Graphics/ImageData.h | 12 | ||||
-rw-r--r-- | Runtime/Scripting/Resource/Resource.bind.cpp | 41 | ||||
-rw-r--r-- | Runtime/Threading/WorkThread.cpp | 1 |
5 files changed, 142 insertions, 12 deletions
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 <vector> +#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 diff --git a/Runtime/Graphics/ImageData.h b/Runtime/Graphics/ImageData.h index 4663e0e..4525ee3 100644 --- a/Runtime/Graphics/ImageData.h +++ b/Runtime/Graphics/ImageData.h @@ -69,16 +69,4 @@ namespace ImageDataUtil ImageDataRequest* LoadAsync(std::vector<const char*> paths); } -// 在工作线程最多只能走到读取->解码完,提交到GPU还得主线程做 - -class ReadImageFilesJob : public Job -{ - -}; - -class DecodeImageFilesJob : public Job -{ - -}; - #endif
\ No newline at end of file diff --git a/Runtime/Scripting/Resource/Resource.bind.cpp b/Runtime/Scripting/Resource/Resource.bind.cpp new file mode 100644 index 0000000..b4ba025 --- /dev/null +++ b/Runtime/Scripting/Resource/Resource.bind.cpp @@ -0,0 +1,41 @@ +#include "Runtime/Lua/LuaHelper.h" +#include "Runtime/Debug/Log.h" +#include "Runtime/FileSystem/ImageJobs.h" + +using namespace LuaBind; + +// Resource.LoadImageDataAsync(path, callback) +int ReadImageDataAsync(lua_State* L) +{ + LUA_BIND_STATE(L); + LUA_BIND_CHECK(L, "TF"); + + cc8* path = state.GetValue(1, ""); + + ReadImageFileJob* job = new ReadImageFileJob(state.GetVM(), 2, path); + + JobSystem::Instance()->AddJobAtEnd(job); + + return 0; +} + +static luaL_Reg funcs[] = { + {"ReadImageDataAsync", ReadImageDataAsync}, + {0, 0} +}; + +int luaopen_GameLab_Engine_Resource(lua_State* L) +{ + log_info("Scripting", "luaopen_GameLab_Engine_Resource()"); + + LUA_BIND_STATE(L); + + state.PushGlobalNamespace(); + state.PushNamespace("GameLab"); + state.PushNamespace("Engine"); + state.PushNamespace("Resource"); + + state.RegisterMethods(funcs); + + return 1; +}
\ No newline at end of file diff --git a/Runtime/Threading/WorkThread.cpp b/Runtime/Threading/WorkThread.cpp index 31c3ead..5ffa38f 100644 --- a/Runtime/Threading/WorkThread.cpp +++ b/Runtime/Threading/WorkThread.cpp @@ -34,6 +34,7 @@ void WorkThread::Dispatch(void* param) for (int i = 0; i < m_FinishedJobs.size(); ++i) { m_FinishedJobs[i]->Dispacth(param); + delete m_FinishedJobs[i]; } m_FinishedJobs.clear(); } |