• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_SERVICE_BASE_PIPELINE_RS_RENDER_NODE_ALLOCATOR_H
17 #define RENDER_SERVICE_BASE_PIPELINE_RS_RENDER_NODE_ALLOCATOR_H
18 
19 #include <atomic>
20 #include <queue>
21 
22 #include "pipeline/rs_canvas_render_node.h"
23 
24 namespace OHOS {
25 namespace Rosen {
26 class RSB_EXPORT RSRenderNodeAllocator {
27 public:
28     using DrawablePtr = DrawableV2::RSRenderNodeDrawableAdapter::Ptr;
29     static RSRenderNodeAllocator& Instance();
30 
31     bool AddNodeToAllocator(RSRenderNode* ptr);
32     bool AddDrawableToAllocator(DrawablePtr ptr);
33 
34     std::shared_ptr<RSCanvasRenderNode> CreateRSCanvasRenderNode(NodeId id,
35         const std::weak_ptr<RSContext>& context = {}, bool isTextureExportNode = false);
36     DrawablePtr CreateRSRenderNodeDrawable(std::shared_ptr<const RSRenderNode> node,
37         std::function<DrawablePtr(std::shared_ptr<const RSRenderNode> node, DrawablePtr front)> generator);
38 
39 private:
40     class Spinlock {
41     public:
Spinlock()42         Spinlock() : flag(false) {}
43 
lock()44         void lock() {
45             while (flag.exchange(true, std::memory_order_acquire)) {}
46         }
47 
unlock()48         void unlock() {
49             flag.store(false, std::memory_order_release);
50         }
51 
52     private:
53         std::atomic<bool> flag;
54     };
55 
56     std::queue<RSRenderNode*> nodeAllocator_;
57     Spinlock nodeAllocatorSpinlock_;
58 
59     std::queue<DrawablePtr> drawableAllocator_;
60     Spinlock drawableAllocatorSpinlock_;
61 };
62 
63 } // namespace Rosen
64 } // namespace OHOS
65 
66 #endif // RENDER_SERVICE_BASE_PIPELINE_RS_RENDER_NODE_ALLOCATOR_H