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_SERVICE_DRAWABLE_RS_CANVAS_DRAWING_RENDER_NODE_DRAWABLE_H
17 #define RENDER_SERVICE_DRAWABLE_RS_CANVAS_DRAWING_RENDER_NODE_DRAWABLE_H
18 
19 #include "drawable/rs_render_node_drawable.h"
20 #include "pipeline/rs_canvas_drawing_render_node.h"
21 #include "pipeline/rs_paint_filter_canvas.h"
22 #include "pipeline/rs_uni_render_thread.h"
23 
24 namespace OHOS::Rosen::DrawableV2 {
25 using ThreadInfo = std::pair<uint32_t, std::function<void(std::shared_ptr<Drawing::Surface>)>>;
26 class RSCanvasDrawingRenderNodeDrawable : public RSRenderNodeDrawable {
27 public:
28     ~RSCanvasDrawingRenderNodeDrawable() override;
29 
30     static RSRenderNodeDrawable::Ptr OnGenerate(std::shared_ptr<const RSRenderNode> node);
31     void OnDraw(Drawing::Canvas& canvas) override;
32     void OnCapture(Drawing::Canvas& canvas) override;
33 
34     void PlaybackInCorrespondThread();
35     void Purge() override;
36     void SetSurfaceClearFunc(ThreadInfo threadInfo, pid_t threadId = 0)
37     {
38         curThreadInfo_ = threadInfo;
39         threadId_ = threadId;
40     }
41     bool InitSurface(int width, int height, RSPaintFilterCanvas& canvas);
42     bool InitSurfaceForVK(int width, int height, RSPaintFilterCanvas& canvas);
43     bool InitSurfaceForGL(int width, int height, RSPaintFilterCanvas& canvas);
44     std::shared_ptr<RSPaintFilterCanvas> GetCanvas();
45     void Flush(float width, float height, std::shared_ptr<RSContext> context,
46         NodeId nodeId, RSPaintFilterCanvas& rscanvas);
47     Drawing::Bitmap GetBitmap(Drawing::GPUContext* grContext);
48     bool GetPixelmap(const std::shared_ptr<Media::PixelMap> pixelmap, const Drawing::Rect* rect,
49         const uint64_t tid = UINT32_MAX, std::shared_ptr<Drawing::DrawCmdList> drawCmdList = nullptr);
50     void DrawCaptureImage(RSPaintFilterCanvas& canvas);
51     void ReleaseCaptureImage();
52 
GetTid()53     uint32_t GetTid() const
54     {
55         return curThreadInfo_.first;
56     }
57     void ResetSurface();
IsDrawCmdListsVisited()58     bool IsDrawCmdListsVisited() const override
59     {
60         return drawCmdListsVisited_;
61     }
SetDrawCmdListsVisited(bool flag)62     void SetDrawCmdListsVisited(bool flag) override
63     {
64         drawCmdListsVisited_ = flag;
65     }
66 
67 private:
68     explicit RSCanvasDrawingRenderNodeDrawable(std::shared_ptr<const RSRenderNode>&& node);
69     using Registrar = RenderNodeDrawableRegistrar<RSRenderNodeType::CANVAS_DRAWING_NODE, OnGenerate>;
70     void ProcessCPURenderInBackgroundThread(std::shared_ptr<Drawing::DrawCmdList> cmds,
71         std::shared_ptr<RSContext> ctx, NodeId nodeId);
72     void DrawRenderContent(Drawing::Canvas& canvas, const Drawing::Rect& rect);
73     bool ResetSurfaceForGL(int width, int height, RSPaintFilterCanvas& canvas);
74     bool ResetSurfaceForVK(int width, int height, RSPaintFilterCanvas& canvas);
75     bool IsNeedResetSurface() const;
76     void FlushForGL(float width, float height, std::shared_ptr<RSContext> context,
77         NodeId nodeId, RSPaintFilterCanvas& rscanvas);
78     void FlushForVK(float width, float height, std::shared_ptr<RSContext> context,
79         NodeId nodeId, RSPaintFilterCanvas& rscanvas);
80 #if (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK))
81     bool ResetSurfaceWithTexture(int width, int height, RSPaintFilterCanvas& canvas);
82     bool ReuseBackendTexture(int width, int height, RSPaintFilterCanvas& canvas);
83     void ClearPreSurface(std::shared_ptr<Drawing::Surface>& surface);
84     bool GetCurrentContextAndImage(std::shared_ptr<Drawing::GPUContext>& grContext,
85         std::shared_ptr<Drawing::Image>& image, const uint64_t tid);
86 #endif
87     static Registrar instance_;
88     std::recursive_mutex drawableMutex_;
89     std::shared_ptr<Drawing::Surface> surface_;
90     std::shared_ptr<Drawing::Image> image_;
91     std::shared_ptr<Drawing::Image> captureImage_;
92     std::shared_ptr<ExtendRecordingCanvas> recordingCanvas_;
93 #if (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK))
94     bool isGpuSurface_ = true;
95     bool isPurge_ = false;
96     Drawing::BackendTexture backendTexture_;
97     NativeBufferUtils::VulkanCleanupHelper* vulkanCleanupHelper_ = nullptr;
98 #endif
99     std::shared_ptr<RSPaintFilterCanvas> canvas_;
100     std::atomic<pid_t> threadId_ = RSUniRenderThread::Instance().GetTid();
101 
102     ThreadInfo curThreadInfo_ = { UNI_RENDER_THREAD_INDEX, std::function<void(std::shared_ptr<Drawing::Surface>)>() };
103     ThreadInfo preThreadInfo_ = { UNI_RENDER_THREAD_INDEX, std::function<void(std::shared_ptr<Drawing::Surface>)>() };
104 
105     // setted in render thread, used and resetted in main thread
106     std::atomic<bool> drawCmdListsVisited_ = false;
107 };
108 
109 } // namespace OHOS::Rosen::DrawableV2
110 #endif // RENDER_SERVICE_DRAWABLE_RS_CANVAS_DRAWING_RENDER_NODE_DRAWABLE_H
111