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_render_post_processes.h"
17
18 #include <cstdint>
19
20 #include <base/containers/fixed_string.h>
21 #include <render/intf_render_context.h>
22
23 #include "datastore/render_data_store_manager.h"
24 #include "util/log.h"
25
26 using namespace BASE_NS;
27 using namespace CORE_NS;
28
RENDER_BEGIN_NAMESPACE()29 RENDER_BEGIN_NAMESPACE()
30 RenderDataStoreRenderPostProcesses::RenderDataStoreRenderPostProcesses(
31 const IRenderContext& /*renderContext*/, const string_view name)
32 : name_(name)
33 {}
34
35 RenderDataStoreRenderPostProcesses::~RenderDataStoreRenderPostProcesses() = default;
36
PostRender()37 void RenderDataStoreRenderPostProcesses::PostRender()
38 {
39 Clear();
40 }
41
Clear()42 void RenderDataStoreRenderPostProcesses::Clear()
43 {
44 const auto lock = std::lock_guard(mutex_);
45
46 nameToPipeline_.clear();
47 pipelines_.clear();
48 }
49
AddData(const string_view name,array_view<const IRenderDataStoreRenderPostProcesses::PostProcessData> data)50 void RenderDataStoreRenderPostProcesses::AddData(
51 const string_view name, array_view<const IRenderDataStoreRenderPostProcesses::PostProcessData> data)
52 {
53 if ((!name.empty()) && (!data.empty())) {
54 const auto lock = std::lock_guard(mutex_);
55
56 size_t index = 0;
57 if (auto iter = nameToPipeline_.find(name); iter != nameToPipeline_.end()) {
58 index = static_cast<size_t>(iter->second);
59 } else {
60 index = pipelines_.size();
61 nameToPipeline_.insert_or_assign(name, index);
62 pipelines_.push_back({});
63 }
64 // clear and add
65 if (index < pipelines_.size()) {
66 auto& pp = pipelines_[index].postProcesses;
67 pp.clear();
68 pp.append(data.begin(), data.end());
69 }
70 }
71 }
72
GetData(const string_view name) const73 IRenderDataStoreRenderPostProcesses::PostProcessPipeline RenderDataStoreRenderPostProcesses::GetData(
74 const string_view name) const
75 {
76 if (const auto iter = nameToPipeline_.find(name); iter != nameToPipeline_.cend()) {
77 if (iter->second < pipelines_.size()) {
78 return pipelines_[iter->second];
79 }
80 }
81 return {};
82 }
83
Ref()84 void RenderDataStoreRenderPostProcesses::Ref()
85 {
86 refcnt_.fetch_add(1, std::memory_order_relaxed);
87 }
88
Unref()89 void RenderDataStoreRenderPostProcesses::Unref()
90 {
91 if (std::atomic_fetch_sub_explicit(&refcnt_, 1, std::memory_order_release) == 1) {
92 std::atomic_thread_fence(std::memory_order_acquire);
93 delete this;
94 }
95 }
96
GetRefCount()97 int32_t RenderDataStoreRenderPostProcesses::GetRefCount()
98 {
99 return refcnt_;
100 }
101
102 // for plugin / factory interface
Create(IRenderContext & renderContext,const char * name)103 refcnt_ptr<IRenderDataStore> RenderDataStoreRenderPostProcesses::Create(IRenderContext& renderContext, const char* name)
104 {
105 return refcnt_ptr<IRenderDataStore>(new RenderDataStoreRenderPostProcesses(renderContext, name));
106 }
107 RENDER_END_NAMESPACE()
108