1/*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <mutex>
17#include "gpu_data_plugin.h"
18
19namespace {
20constexpr uint32_t MAX_BUFFER_SIZE = 4 * 1024 * 1024;
21std::unique_ptr<GpuDataPlugin> g_plugin = nullptr;
22std::mutex g_taskMutex;
23} // namespace
24
25static int GpuDataPluginSessionStart(const uint8_t* configData, uint32_t configSize)
26{
27    std::lock_guard<std::mutex> guard(g_taskMutex);
28    g_plugin = std::make_unique<GpuDataPlugin>();
29    return g_plugin->Start(configData, configSize);
30}
31
32static int GpuPluginReportResult(uint8_t* bufferData, uint32_t bufferSize)
33{
34    std::lock_guard<std::mutex> guard(g_taskMutex);
35    CHECK_NOTNULL(g_plugin, -1, "g_plugin is nullptr");
36    return g_plugin->Report(bufferData, bufferSize);
37}
38
39static int GpuPluginReportResultOptimize(RandomWriteCtx* randomWrite)
40{
41    std::lock_guard<std::mutex> guard(g_taskMutex);
42    return g_plugin->ReportOptimize(randomWrite);
43}
44
45static int GpuPluginSessionStop()
46{
47    std::lock_guard<std::mutex> guard(g_taskMutex);
48    g_plugin->Stop();
49    return 0;
50}
51
52static PluginModuleCallbacks g_callbacks = {
53    .onPluginSessionStart = GpuDataPluginSessionStart,
54    .onPluginReportResult = GpuPluginReportResult,
55    .onPluginSessionStop = GpuPluginSessionStop,
56    .onPluginReportResultOptimize = GpuPluginReportResultOptimize,
57};
58
59EXPORT_API PluginModuleStruct g_pluginModule = {
60    .callbacks = &g_callbacks,
61    .name = "gpu-plugin",
62    .version = "1.02",
63    .resultBufferSizeHint = MAX_BUFFER_SIZE,
64};
65