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 
SetScene(const RenderScene & renderScene)42 void RenderDataStoreDefaultScene::SetScene(const RenderScene& renderScene)
43 {
44     const size_t arrIdx = scenes_.size();
45     scenes_.push_back(renderScene);
46     if (renderScene.name.empty()) {
47         nameToScene_[to_string(nextId++)] = arrIdx;
48     } else {
49         nameToScene_[renderScene.name] = arrIdx;
50     }
51 }
52 
GetScene(const string_view name) const53 RenderScene RenderDataStoreDefaultScene::GetScene(const string_view name) const
54 {
55     if (const auto iter = nameToScene_.find(name); iter != nameToScene_.cend()) {
56         CORE_ASSERT(iter->second < scenes_.size());
57         return scenes_[iter->second];
58     } else {
59         return {};
60     }
61 }
62 
GetScene() const63 RenderScene RenderDataStoreDefaultScene::GetScene() const
64 {
65     if (!scenes_.empty()) {
66         return scenes_[0];
67     } else {
68         return {};
69     }
70 }
71 
72 // for plugin / factory interface
Create(RENDER_NS::IRenderContext &,char const * name)73 RENDER_NS::IRenderDataStore* RenderDataStoreDefaultScene::Create(RENDER_NS::IRenderContext&, char const* name)
74 {
75     // device not needed
76     return new RenderDataStoreDefaultScene(name);
77 }
78 
Destroy(IRenderDataStore * instance)79 void RenderDataStoreDefaultScene::Destroy(IRenderDataStore* instance)
80 {
81     delete static_cast<RenderDataStoreDefaultScene*>(instance);
82 }
83 CORE3D_END_NAMESPACE()
84