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/render_thread/rs_uni_render_thread.h" 21 #include "pipeline/rs_canvas_drawing_render_node.h" 22 #include "pipeline/rs_paint_filter_canvas.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 CheckAndSetThreadIdx(uint32_t& threadIdx); 35 bool CheckPostplaybackParamValid(NodeId, pid_t); 36 void PostPlaybackInCorrespondThread(); 37 void SetSurfaceClearFunc(ThreadInfo threadInfo, pid_t threadId = 0) 38 { 39 curThreadInfo_ = threadInfo; 40 threadId_ = threadId; 41 } 42 bool InitSurface(int width, int height, RSPaintFilterCanvas& canvas); 43 std::shared_ptr<RSPaintFilterCanvas> GetCanvas(); 44 void Flush(float width, float height, std::shared_ptr<RSContext> context, 45 NodeId nodeId, RSPaintFilterCanvas& rscanvas); 46 Drawing::Bitmap GetBitmap(Drawing::GPUContext* grContext); 47 bool GetPixelmap(const std::shared_ptr<Media::PixelMap> pixelmap, const Drawing::Rect* rect, 48 const uint64_t tid = UINT32_MAX, std::shared_ptr<Drawing::DrawCmdList> drawCmdList = nullptr); 49 void DrawCaptureImage(RSPaintFilterCanvas& canvas); 50 void ReleaseCaptureImage(); 51 void DumpCanvasDrawing(); 52 GetTid()53 uint32_t GetTid() const 54 { 55 return curThreadInfo_.first; 56 } 57 void ResetSurface(); IsNeedDraw()58 virtual bool IsNeedDraw() const override 59 { 60 return needDraw_; 61 } SetNeedDraw(bool flag)62 virtual void SetNeedDraw(bool flag) override 63 { 64 needDraw_ = flag; 65 } 66 Snapshot()67 std::shared_ptr<Drawing::Image> Snapshot() const override 68 { 69 return image_; 70 } 71 72 private: 73 explicit RSCanvasDrawingRenderNodeDrawable(std::shared_ptr<const RSRenderNode>&& node); 74 using Registrar = RenderNodeDrawableRegistrar<RSRenderNodeType::CANVAS_DRAWING_NODE, OnGenerate>; 75 void ProcessCPURenderInBackgroundThread(std::shared_ptr<Drawing::DrawCmdList> cmds, 76 std::shared_ptr<RSContext> ctx, NodeId nodeId); 77 void DrawRenderContent(Drawing::Canvas& canvas, const Drawing::Rect& rect); 78 bool ResetSurfaceForGL(int width, int height, RSPaintFilterCanvas& canvas); 79 bool ResetSurfaceForVK(int width, int height, RSPaintFilterCanvas& canvas); 80 bool GpuContextResetGL(int width, int height, std::shared_ptr<Drawing::GPUContext>& gpuContext); 81 bool GpuContextResetVK(int width, int height, std::shared_ptr<Drawing::GPUContext>& gpuContext); 82 Drawing::TextureOrigin GetTextureOrigin(); 83 void DrawRegionForDfx(Drawing::Canvas& canvas, const Drawing::Rect& bounds); 84 #ifdef RS_ENABLE_VK 85 bool ReleaseSurfaceVK(int width, int height); 86 #endif 87 bool ResetSurfaceforPlayback(int width, int height); 88 bool GetCurrentContext(std::shared_ptr<Drawing::GPUContext>& grContext); 89 bool IsNeedResetSurface() const; 90 void FlushForGL(float width, float height, std::shared_ptr<RSContext> context, 91 NodeId nodeId, RSPaintFilterCanvas& rscanvas); 92 void FlushForVK(float width, float height, std::shared_ptr<RSContext> context, 93 NodeId nodeId, RSPaintFilterCanvas& rscanvas); 94 #if (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK)) 95 bool InitSurfaceForVK(int width, int height, RSPaintFilterCanvas& canvas); 96 bool InitSurfaceForGL(int width, int height, RSPaintFilterCanvas& canvas); 97 bool ResetSurfaceWithTexture(int width, int height, RSPaintFilterCanvas& canvas); 98 bool ReuseBackendTexture(int width, int height, RSPaintFilterCanvas& canvas); 99 void ClearPreSurface(std::shared_ptr<Drawing::Surface>& surface); 100 bool GetCurrentContextAndImage(std::shared_ptr<Drawing::GPUContext>& grContext, 101 std::shared_ptr<Drawing::Image>& image, const uint64_t tid); 102 #endif 103 static Registrar instance_; 104 std::recursive_mutex drawableMutex_; 105 std::shared_ptr<Drawing::Surface> surface_; 106 std::shared_ptr<Drawing::Image> image_; 107 std::shared_ptr<Drawing::Image> captureImage_; 108 std::shared_ptr<ExtendRecordingCanvas> recordingCanvas_; 109 #if (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK)) 110 bool isGpuSurface_ = true; 111 Drawing::BackendTexture backendTexture_; 112 NativeBufferUtils::VulkanCleanupHelper* vulkanCleanupHelper_ = nullptr; 113 #endif 114 std::shared_ptr<RSPaintFilterCanvas> canvas_; 115 std::atomic<pid_t> threadId_ = RSUniRenderThread::Instance().GetTid(); 116 117 ThreadInfo curThreadInfo_ = { UNI_RENDER_THREAD_INDEX, std::function<void(std::shared_ptr<Drawing::Surface>)>() }; 118 ThreadInfo preThreadInfo_ = { UNI_RENDER_THREAD_INDEX, std::function<void(std::shared_ptr<Drawing::Surface>)>() }; 119 120 // setted in render thread, used and resetted in main thread 121 std::atomic<bool> needDraw_ = false; 122 }; 123 124 } // namespace OHOS::Rosen::DrawableV2 125 #endif // RENDER_SERVICE_DRAWABLE_RS_CANVAS_DRAWING_RENDER_NODE_DRAWABLE_H 126