• 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 #ifndef RENDER_DATA_STORE_RENDER_DATA_STORE_POD_H
17 #define RENDER_DATA_STORE_RENDER_DATA_STORE_POD_H
18 
19 #include <atomic>
20 #include <cstdint>
21 #include <mutex>
22 
23 #include <base/containers/array_view.h>
24 #include <base/containers/refcnt_ptr.h>
25 #include <base/containers/string.h>
26 #include <base/containers/string_view.h>
27 #include <base/containers/unordered_map.h>
28 #include <base/containers/vector.h>
29 #include <base/util/uid.h>
30 #include <render/datastore/intf_render_data_store_pod.h>
31 #include <render/namespace.h>
32 
33 RENDER_BEGIN_NAMESPACE()
34 class IRenderContext;
35 /**
36 RenderDataStorePod implementation.
37 */
38 class RenderDataStorePod final : public IRenderDataStorePod {
39 public:
40     explicit RenderDataStorePod(BASE_NS::string_view name);
41     ~RenderDataStorePod() override = default;
42 
43     // IRenderDataStore
PreRender()44     void PreRender() override {}
PostRender()45     void PostRender() override {}
PreRenderBackend()46     void PreRenderBackend() override {}
PostRenderBackend()47     void PostRenderBackend() override {}
Clear()48     void Clear() override {}
GetFlags()49     uint32_t GetFlags() const override
50     {
51         return 0;
52     }
GetTypeName()53     BASE_NS::string_view GetTypeName() const override
54     {
55         return TYPE_NAME;
56     }
57 
GetName()58     BASE_NS::string_view GetName() const override
59     {
60         return name_;
61     }
62 
GetUid()63     const BASE_NS::Uid& GetUid() const override
64     {
65         return UID;
66     }
67 
68     void Ref() override;
69     void Unref() override;
70     int32_t GetRefCount() override;
71 
72     // IRenderDataStorePod
73     void CreatePod(
74         BASE_NS::string_view typeName, BASE_NS::string_view name, BASE_NS::array_view<const uint8_t> data) override;
75     void DestroyPod(BASE_NS::string_view typeName, BASE_NS::string_view name) override;
76 
77     void Set(BASE_NS::string_view name, BASE_NS::array_view<const uint8_t> data) override;
78     BASE_NS::array_view<const uint8_t> Get(BASE_NS::string_view name) const override;
79 
80     BASE_NS::array_view<const BASE_NS::string> GetPodNames(BASE_NS::string_view typeName) const override;
81 
82     // for plugin / factory interface
83     static constexpr const char* const TYPE_NAME = "RenderDataStorePod";
84 
85     static BASE_NS::refcnt_ptr<IRenderDataStore> Create(IRenderContext& renderContext, const char* name);
86 
87 private:
88     const BASE_NS::string name_;
89 
90     // NOTE: allocate bigger blocks
91     BASE_NS::vector<uint8_t> dataStore_;
92 
93     struct OffsetToData {
94         uint32_t index { 0 };
95         uint32_t byteSize { 0 };
96     };
97     BASE_NS::unordered_map<BASE_NS::string, OffsetToData> nameToDataOffset_;
98 
99     BASE_NS::unordered_map<BASE_NS::string, BASE_NS::vector<BASE_NS::string>> typeNameToPodNames_;
100 
101     mutable std::mutex mutex_;
102 
103     std::atomic_int32_t refcnt_ { 0 };
104 };
105 RENDER_END_NAMESPACE()
106 
107 #endif // RENDER_DATA_STORE_RENDER_DATA_STORE_POD_H
108