summaryrefslogtreecommitdiff
path: root/Runtime/FileSystem
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-10-26 23:20:45 +0800
committerchai <chaifix@163.com>2021-10-26 23:20:45 +0800
commit208e23de77ad6d104f13a0bb591ae16c4a805fe9 (patch)
tree29499de463a6d3407d2e555c1ad9b743f07097b1 /Runtime/FileSystem
parent8b419dc90cb4e01fa1810e0f84dcdcfe2bd822ad (diff)
*load image async
Diffstat (limited to 'Runtime/FileSystem')
-rw-r--r--Runtime/FileSystem/ImageJobs.cpp54
-rw-r--r--Runtime/FileSystem/ImageJobs.h46
2 files changed, 100 insertions, 0 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