1 /*
2  * Copyright (c) 2023 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 ROSEN_RENDER_SERVICE_BASE_PIPELINE_RS_CANVAS_DRAWING_RENDER_NODE_H
17 #define ROSEN_RENDER_SERVICE_BASE_PIPELINE_RS_CANVAS_DRAWING_RENDER_NODE_H
18 
19 #include <atomic>
20 #include <functional>
21 #include <memory>
22 
23 #include "pipeline/rs_canvas_render_node.h"
24 #include "pipeline/rs_recording_canvas.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 
29 using ThreadInfo = std::pair<uint32_t, std::function<void(std::shared_ptr<Drawing::Surface>)>>;
30 class RSRecordingCanvas;
31 
32 class RSB_EXPORT RSCanvasDrawingRenderNode : public RSCanvasRenderNode {
33 public:
34     using WeakPtr = std::weak_ptr<RSCanvasDrawingRenderNode>;
35     using SharedPtr = std::shared_ptr<RSCanvasDrawingRenderNode>;
36     static inline constexpr RSRenderNodeType Type = RSRenderNodeType::CANVAS_DRAWING_NODE;
37 
38     virtual ~RSCanvasDrawingRenderNode();
39 
40     // Used in uni render thread.
41     void InitRenderContent();
42 
43     void ProcessRenderContents(RSPaintFilterCanvas& canvas) override;
44 
GetType()45     RSRenderNodeType GetType() const override
46     {
47         return Type;
48     }
49 
OpincGetNodeSupportFlag()50     bool OpincGetNodeSupportFlag() override
51     {
52         return false;
53     }
54 
55     Drawing::Bitmap GetBitmap();
56     Drawing::Bitmap GetBitmap(const uint64_t tid);
57     std::shared_ptr<Drawing::Image> GetImage(const uint64_t tid);
58     bool GetPixelmap(const std::shared_ptr<Media::PixelMap> pixelmap, const Drawing::Rect* rect,
59         const uint64_t tid = UINT32_MAX, std::shared_ptr<Drawing::DrawCmdList> drawCmdList = nullptr);
60 
61     void SetSurfaceClearFunc(ThreadInfo threadInfo, pid_t threadId = 0)
62     {
63         curThreadInfo_ = threadInfo;
64         threadId_ = threadId;
65     }
66 
67     uint32_t GetTid() const;
68 
69     void AddDirtyType(RSModifierType type) override;
70     void ClearOp();
71     void ResetSurface(int width, int height);
72     bool IsNeedProcess() const;
73     void SetNeedProcess(bool needProcess);
74     void PlaybackInCorrespondThread();
75     const std::map<RSModifierType, std::list<Drawing::DrawCmdListPtr>>& GetDrawCmdLists() const;
76     void ClearResource() override;
77     bool IsDrawCmdListsVisited() const;
78     void SetDrawCmdListsVisited(bool flag);
79 private:
80     explicit RSCanvasDrawingRenderNode(
81         NodeId id, const std::weak_ptr<RSContext>& context = {}, bool isTextureExportNode = false);
82     void ApplyDrawCmdModifier(RSModifierContext& context, RSModifierType type);
83     bool ResetSurface(int width, int height, RSPaintFilterCanvas& canvas);
84     bool GetSizeFromDrawCmdModifiers(int& width, int& height);
85     bool IsNeedResetSurface() const;
86     void InitRenderParams() override;
87 #if (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK))
88     bool ResetSurfaceWithTexture(int width, int height, RSPaintFilterCanvas& canvas);
89 #endif
90 
91     std::mutex imageMutex_;
92     std::shared_ptr<Drawing::Surface> surface_;
93     std::shared_ptr<Drawing::Image> image_;
94     std::shared_ptr<ExtendRecordingCanvas> recordingCanvas_;
95 #if (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK))
96     bool isGpuSurface_ = true;
97 #endif
98     std::unique_ptr<RSPaintFilterCanvas> canvas_;
99     std::mutex drawCmdListsMutex_;
100     ThreadInfo curThreadInfo_ = { UNI_MAIN_THREAD_INDEX, std::function<void(std::shared_ptr<Drawing::Surface>)>() };
101     ThreadInfo preThreadInfo_ = { UNI_MAIN_THREAD_INDEX, std::function<void(std::shared_ptr<Drawing::Surface>)>() };
102     std::mutex taskMutex_;
103     std::atomic<bool> isNeedProcess_ = false;
104     pid_t threadId_ = 0;
105     std::map<RSModifierType, std::list<Drawing::DrawCmdListPtr>> drawCmdLists_;
106     bool lastOverflowStatus_ = false;
107 
108     // Used in uni render thread.
109     uint32_t drawingNodeRenderID = UNI_MAIN_THREAD_INDEX;
110 
111     friend class RSCanvasDrawingNodeCommandHelper;
112 };
113 
114 } // namespace Rosen
115 } // namespace OHOS
116 
117 #endif // ROSEN_RENDER_SERVICE_BASE_PIPELINE_RS_CANVAS_DRAWING_RENDER_NODE_H