资源编译优化

Signed-off-by: liduo <liduo29@huawei.com>
This commit is contained in:
liduo
2024-08-26 21:59:52 +08:00
parent 1ee4042244
commit 6c5c2e9ae3
13 changed files with 416 additions and 86 deletions
+3
View File
@@ -17,6 +17,7 @@
#define OHOS_RESTOOL_GENERIC_COMPILER_H
#include "i_resource_compiler.h"
#include <mutex>
namespace OHOS {
namespace Global {
@@ -27,11 +28,13 @@ public:
virtual ~GenericCompiler();
protected:
uint32_t CompileSingleFile(const FileInfo &fileInfo) override;
uint32_t CompileFiles(const std::vector<FileInfo> &fileInfos) override;
bool PostMediaFile(const FileInfo &fileInfo, const std::string &output);
private:
std::string GetOutputFilePath(const FileInfo &fileInfo) const;
bool IsIgnore(const FileInfo &fileInfo);
bool CopyMediaFile(const FileInfo &fileInfo, std::string &output);
std::mutex mutex_;
};
}
}
+1
View File
@@ -36,6 +36,7 @@ public:
protected:
virtual uint32_t CompileSingleFile(const FileInfo &fileInfo);
virtual uint32_t CompileFiles(const std::vector<FileInfo> &fileInfos);
bool MergeResourceItem(const ResourceItem &resourceItem);
std::string GetOutputFolder(const DirectoryInfo &directoryInfo) const;
ResType type_;
+45
View File
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2021-2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_RESTOOL_RAWFILE_RESFILE_PACKER_H
#define OHOS_RESTOOL_RAWFILE_RESFILE_PACKER_H
#include "cmd_parser.h"
#include "resource_util.h"
#include "thread_pool.h"
namespace OHOS {
namespace Global {
namespace Restool {
class RawFileResFilePacker {
public:
explicit RawFileResFilePacker(const PackageParser &packageParser, const std::string &moduleName);
~RawFileResFilePacker();
std::future<uint32_t> CopyRawFileOrResFileAsync(const std::vector<std::string> &inputs);
private:
uint32_t CopyRawFileOrResFile(const std::vector<std::string> &inputs);
uint32_t CopyRawFileOrResFile(const std::string &filePath, const std::string &fileType);
uint32_t CopyRawFileOrResFileImpl(const std::string &src, const std::string &dst);
uint32_t CopySingleFile(const std::string &path, std::string &subPath);
PackageParser packageParser_;
std::string moduleName_;
ThreadPool threadPool_;
std::vector<std::future<uint32_t>> copyResults_;
};
} // namespace Restool
} // namespace Global
} // namespace OHOS
#endif
+3 -1
View File
@@ -16,6 +16,7 @@
#ifndef OHOS_RESTOOL_RESOURCE_DATA_H
#define OHOS_RESTOOL_RESOURCE_DATA_H
#include <cstdint>
#include <map>
#include <set>
#include <stdint.h>
@@ -48,9 +49,10 @@ const static std::string SOLUTIONS_ARROW = "> ";
const static std::string LONG_PATH_HEAD = "\\\\?\\";
const static int32_t VERSION_MAX_LEN = 128;
const static int32_t INT_TO_BYTES = sizeof(uint32_t);
static const int8_t RESTOOL_VERSION[VERSION_MAX_LEN] = { "Restool 5.010" };
static const int8_t RESTOOL_VERSION[VERSION_MAX_LEN] = { "Restool 5.011" };
const static int32_t TAG_LEN = 4;
static std::set<std::string> g_resourceSet;
const static int8_t THREAD_POOL_SIZE = 2;
enum class KeyType {
LANGUAGE = 0,
-3
View File
@@ -43,9 +43,6 @@ private:
uint32_t GenerateTextHeader(const std::string &headerPath) const;
uint32_t GenerateCplusHeader(const std::string &headerPath) const;
uint32_t GenerateJsHeader(const std::string &headerPath) const;
uint32_t CopyRawFileOrResFile(const std::string &filePath, const std::string &fileType);
uint32_t CopyRawFileOrResFile(const std::vector<std::string> &inputs);
uint32_t CopyRawFileOrResFileImpl(const std::string &src, const std::string &dst);
uint32_t GenerateConfigJson();
uint32_t ScanResources(const std::vector<std::string> &inputs, const std::string &output);
uint32_t PackNormal();
+94
View File
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2021-2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_RESTOOL_THREAD_POOL_H
#define OHOS_RESTOOL_THREAD_POOL_H
#include "no_copy_able.h"
#include <condition_variable>
#include <functional>
#include <future>
#include <mutex>
#include <queue>
#include <string>
#include <thread>
#include <vector>
namespace OHOS {
namespace Global {
namespace Restool {
class ThreadPool : public NoCopyable {
public:
/**
* @brief Creates a thread pool with specify thread count
* @param threadCount the count of threads to be created
*/
explicit ThreadPool(const size_t threadCount);
~ThreadPool() override;
/**
* @brief Start the thread pool
*/
uint32_t Start();
/**
* @brief Stop the thread pool
*/
void Stop();
/**
* @brief Enqueue a task to queue of thread pool
* @param f the function to execute
* @param args the args of the function
*/
template <class F, class... Args>
auto Enqueue(F &&f, Args &&...args) -> std::future<typename std::result_of<F(Args...)>::type>;
private:
void WorkInThread();
std::vector<std::thread> workerThreads_;
std::queue<std::function<void()>> tasks_;
std::mutex queueMutex_;
std::condition_variable condition_;
bool running_;
size_t threadCount_;
};
template <typename F, typename... Args>
auto ThreadPool::Enqueue(F &&f, Args &&...args) -> std::future<typename std::result_of<F(Args...)>::type>
{
using return_type = typename std::result_of<F(Args...)>::type;
using p_task = std::packaged_task<return_type()>;
auto task = std::make_shared<p_task>(std::bind(std::forward<F>(f), std::forward<Args>(args)...));
std::future<return_type> res = task->get_future();
if (!running_ || workerThreads_.empty()) {
// If the pool is not running or there are no worker threads, execute the task immediately
(*task)();
return res;
}
{
std::unique_lock<std::mutex> lock(queueMutex_);
tasks_.emplace([task]() { (*task)(); });
}
condition_.notify_one();
return res;
}
} // namespace Restool
} // namespace Global
} // namespace OHOS
#endif