1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
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 "render_data_store_default_gpu_resource_data_copy.h"
17 
18 #include <cstdint>
19 #include <mutex>
20 
21 #include <base/containers/vector.h>
22 #include <render/datastore/intf_render_data_store_default_gpu_resource_data_copy.h>
23 #include <render/device/gpu_resource_desc.h>
24 #include <render/device/intf_device.h>
25 #include <render/intf_render_context.h>
26 #include <render/namespace.h>
27 #include <render/resource_handle.h>
28 
29 #include "device/gpu_resource_handle_util.h"
30 #include "device/gpu_resource_manager.h"
31 #include "device/gpu_resource_util.h"
32 #include "util/log.h"
33 
34 using namespace BASE_NS;
35 
RENDER_BEGIN_NAMESPACE()36 RENDER_BEGIN_NAMESPACE()
37 RenderDataStoreDefaultGpuResourceDataCopy::RenderDataStoreDefaultGpuResourceDataCopy(
38     IRenderContext& renderContext, const string_view name)
39     : device_(renderContext.GetDevice()), gpuResourceMgr_((GpuResourceManager&)device_.GetGpuResourceManager()),
40       name_(name)
41 {}
42 
PostRenderBackend()43 void RenderDataStoreDefaultGpuResourceDataCopy::PostRenderBackend()
44 {
45     std::lock_guard<std::mutex> lock(mutex_);
46 
47     if (!copyData_.empty() && waitForIdle_) {
48         PLUGIN_LOG_W("RENDER_PERFORMANCE_WARNING: wait for idle called for device");
49         device_.WaitForIdle();
50     }
51     for (const auto& ref : copyData_) {
52         if (ref.gpuHandle && ref.byteArray) {
53             GpuResourceUtil::CopyGpuResource(device_, gpuResourceMgr_, ref.gpuHandle.GetHandle(), *ref.byteArray);
54         }
55     }
56 
57     copyData_.clear();
58     waitForIdle_ = false;
59 }
60 
Clear()61 void RenderDataStoreDefaultGpuResourceDataCopy::Clear()
62 {
63     // data cannot be cleared
64 }
65 
AddCopyOperation(const GpuResourceDataCopy & copyOp)66 void RenderDataStoreDefaultGpuResourceDataCopy::AddCopyOperation(const GpuResourceDataCopy& copyOp)
67 {
68     std::lock_guard<std::mutex> lock(mutex_);
69 
70     // NOTE: one could add support for GPU image copies
71     // process the image -> buffer in render node staging
72 
73     if (gpuResourceMgr_.IsGpuBuffer(copyOp.gpuHandle)) {
74         if (copyOp.copyType == CopyType::WAIT_FOR_IDLE) {
75             waitForIdle_ = true;
76         }
77         copyData_.push_back(copyOp);
78     } else {
79         PLUGIN_LOG_E("Copy operation only supported for GPU buffers");
80     }
81 }
82 
83 // for plugin / factory interface
Create(IRenderContext & renderContext,char const * name)84 IRenderDataStore* RenderDataStoreDefaultGpuResourceDataCopy::Create(IRenderContext& renderContext, char const* name)
85 {
86     return new RenderDataStoreDefaultGpuResourceDataCopy(renderContext, name);
87 }
88 
Destroy(IRenderDataStore * instance)89 void RenderDataStoreDefaultGpuResourceDataCopy::Destroy(IRenderDataStore* instance)
90 {
91     delete static_cast<RenderDataStoreDefaultGpuResourceDataCopy*>(instance);
92 }
93 RENDER_END_NAMESPACE()
94