• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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/intf_render_context.h>
24 #include <render/namespace.h>
25 #include <render/resource_handle.h>
26 
27 #include "device/gpu_resource_manager.h"
28 #include "device/gpu_resource_util.h"
29 #include "util/log.h"
30 
31 using namespace BASE_NS;
32 
RENDER_BEGIN_NAMESPACE()33 RENDER_BEGIN_NAMESPACE()
34 RenderDataStoreDefaultGpuResourceDataCopy::RenderDataStoreDefaultGpuResourceDataCopy(
35     IRenderContext& renderContext, const string_view name)
36     : device_(renderContext.GetDevice()), gpuResourceMgr_((GpuResourceManager&)device_.GetGpuResourceManager()),
37       name_(name)
38 {}
39 
PostRenderBackend()40 void RenderDataStoreDefaultGpuResourceDataCopy::PostRenderBackend()
41 {
42     std::lock_guard<std::mutex> lock(mutex_);
43 
44     if (!copyData_.empty() && waitForIdle_) {
45         PLUGIN_LOG_W("RENDER_PERFORMANCE_WARNING: wait for idle called for device");
46         device_.WaitForIdle();
47     }
48     for (const auto& ref : copyData_) {
49         if (ref.gpuHandle && ref.byteArray) {
50             GpuResourceUtil::CopyGpuResource(device_, gpuResourceMgr_, ref.gpuHandle.GetHandle(), *ref.byteArray);
51         }
52     }
53 
54     copyData_.clear();
55     waitForIdle_ = false;
56 }
57 
Clear()58 void RenderDataStoreDefaultGpuResourceDataCopy::Clear()
59 {
60     // data cannot be cleared
61 }
62 
Ref()63 void RenderDataStoreDefaultGpuResourceDataCopy::Ref()
64 {
65     refcnt_.fetch_add(1, std::memory_order_relaxed);
66 }
67 
Unref()68 void RenderDataStoreDefaultGpuResourceDataCopy::Unref()
69 {
70     if (std::atomic_fetch_sub_explicit(&refcnt_, 1, std::memory_order_release) == 1) {
71         std::atomic_thread_fence(std::memory_order_acquire);
72         delete this;
73     }
74 }
75 
GetRefCount()76 int32_t RenderDataStoreDefaultGpuResourceDataCopy::GetRefCount()
77 {
78     return refcnt_;
79 }
80 
AddCopyOperation(const GpuResourceDataCopy & copyOp)81 void RenderDataStoreDefaultGpuResourceDataCopy::AddCopyOperation(const GpuResourceDataCopy& copyOp)
82 {
83     std::lock_guard<std::mutex> lock(mutex_);
84 
85     // NOTE: one could add support for GPU image copies
86     // process the image -> buffer in render node staging
87 
88     if (gpuResourceMgr_.IsGpuBuffer(copyOp.gpuHandle)) {
89         if (copyOp.copyType == CopyType::WAIT_FOR_IDLE) {
90             waitForIdle_ = true;
91         }
92         copyData_.push_back(copyOp);
93     } else {
94         PLUGIN_LOG_E("Copy operation only supported for GPU buffers");
95     }
96 }
97 
98 // for plugin / factory interface
Create(IRenderContext & renderContext,const char * name)99 refcnt_ptr<IRenderDataStore> RenderDataStoreDefaultGpuResourceDataCopy::Create(
100     IRenderContext& renderContext, const char* name)
101 {
102     return refcnt_ptr<IRenderDataStore>(new RenderDataStoreDefaultGpuResourceDataCopy(renderContext, name));
103 }
104 RENDER_END_NAMESPACE()
105