• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_scene.h"
17 
18 #include <cstdint>
19 
20 #include <3d/render/intf_render_data_store_default_scene.h>
21 #include <base/containers/string.h>
22 #include <base/containers/string_view.h>
23 
24 CORE3D_BEGIN_NAMESPACE()
25 using namespace BASE_NS;
26 using namespace RENDER_NS;
27 
RenderDataStoreDefaultScene(const string_view name)28 RenderDataStoreDefaultScene::RenderDataStoreDefaultScene(const string_view name) : name_(name) {}
29 
PostRender()30 void RenderDataStoreDefaultScene::PostRender()
31 {
32     Clear();
33 }
34 
Clear()35 void RenderDataStoreDefaultScene::Clear()
36 {
37     scenes_.clear();
38     nameToScene_.clear();
39     nextId = 0;
40 }
41 
Ref()42 void RenderDataStoreDefaultScene::Ref()
43 {
44     refcnt_.fetch_add(1, std::memory_order_relaxed);
45 }
46 
Unref()47 void RenderDataStoreDefaultScene::Unref()
48 {
49     if (std::atomic_fetch_sub_explicit(&refcnt_, 1, std::memory_order_release) == 1) {
50         std::atomic_thread_fence(std::memory_order_acquire);
51         delete this;
52     }
53 }
54 
GetRefCount()55 int32_t RenderDataStoreDefaultScene::GetRefCount()
56 {
57     return refcnt_;
58 }
59 
SetScene(const RenderScene & renderScene)60 void RenderDataStoreDefaultScene::SetScene(const RenderScene& renderScene)
61 {
62     const size_t arrIdx = scenes_.size();
63     scenes_.push_back(renderScene);
64     if (renderScene.name.empty()) {
65         nameToScene_[to_string(nextId++)] = arrIdx;
66     } else {
67         nameToScene_[renderScene.name] = arrIdx;
68     }
69 }
70 
GetScene(const string_view name) const71 RenderScene RenderDataStoreDefaultScene::GetScene(const string_view name) const
72 {
73     if (const auto iter = nameToScene_.find(name); iter != nameToScene_.cend()) {
74         CORE_ASSERT(iter->second < scenes_.size());
75         return scenes_[iter->second];
76     } else {
77         return {};
78     }
79 }
80 
GetScene() const81 RenderScene RenderDataStoreDefaultScene::GetScene() const
82 {
83     if (!scenes_.empty()) {
84         return scenes_[0];
85     } else {
86         return {};
87     }
88 }
89 
90 // for plugin / factory interface
Create(IRenderContext &,char const * name)91 refcnt_ptr<IRenderDataStore> RenderDataStoreDefaultScene::Create(IRenderContext&, char const* name)
92 {
93     // device not needed
94     return refcnt_ptr<IRenderDataStore>(new RenderDataStoreDefaultScene(name));
95 }
96 CORE3D_END_NAMESPACE()
97