1 /* 2 * Copyright (c) 2021-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 RS_SURFACE_CAPTURE_TASK 17 #define RS_SURFACE_CAPTURE_TASK 18 #define EGL_EGLEXT_PROTOTYPES 19 #define GL_GLEXT_PROTOTYPES 20 21 #include "common/rs_common_def.h" 22 #include "system/rs_system_parameters.h" 23 #include "draw/canvas.h" 24 #include "draw/surface.h" 25 #include "utils/matrix.h" 26 #include "ipc_callbacks/surface_capture_callback.h" 27 #include "pipeline/rs_display_render_node.h" 28 #include "pipeline/rs_effect_render_node.h" 29 #include "pipeline/rs_surface_render_node.h" 30 #include "pixel_map.h" 31 #include "rs_base_render_engine.h" 32 #include "visitor/rs_node_visitor.h" 33 34 namespace OHOS { 35 namespace Rosen { 36 bool CopyDataToPixelMap(std::shared_ptr<Drawing::Image> img, const std::unique_ptr<Media::PixelMap>& pixelmap); 37 class RSSurfaceCaptureVisitor : public RSNodeVisitor { 38 public: 39 RSSurfaceCaptureVisitor(const RSSurfaceCaptureConfig& captureConfig, bool isUniRender); 40 ~RSSurfaceCaptureVisitor() noexcept override = default; PrepareChildren(RSRenderNode & node)41 void PrepareChildren(RSRenderNode& node) override {} PrepareCanvasRenderNode(RSCanvasRenderNode & node)42 void PrepareCanvasRenderNode(RSCanvasRenderNode& node) override {} PrepareDisplayRenderNode(RSDisplayRenderNode & node)43 void PrepareDisplayRenderNode(RSDisplayRenderNode& node) override {} PrepareProxyRenderNode(RSProxyRenderNode & node)44 void PrepareProxyRenderNode(RSProxyRenderNode& node) override {} PrepareRootRenderNode(RSRootRenderNode & node)45 void PrepareRootRenderNode(RSRootRenderNode& node) override {} PrepareSurfaceRenderNode(RSSurfaceRenderNode & node)46 void PrepareSurfaceRenderNode(RSSurfaceRenderNode& node) override {} PrepareEffectRenderNode(RSEffectRenderNode & node)47 void PrepareEffectRenderNode(RSEffectRenderNode& node) override {} 48 49 void ProcessChildren(RSRenderNode& node) override; 50 void ProcessCanvasRenderNode(RSCanvasRenderNode& node) override; 51 void ProcessDisplayRenderNode(RSDisplayRenderNode& node) override; ProcessProxyRenderNode(RSProxyRenderNode & node)52 void ProcessProxyRenderNode(RSProxyRenderNode& node) override {} 53 void ProcessRootRenderNode(RSRootRenderNode& node) override; 54 void ProcessSurfaceRenderNode(RSSurfaceRenderNode& node) override; 55 void ProcessEffectRenderNode(RSEffectRenderNode& node) override; 56 57 void SetSurface(Drawing::Surface* surface); IsDisplayNode(bool isDisplayNode)58 void IsDisplayNode(bool isDisplayNode) 59 { 60 isDisplayNode_ = isDisplayNode; 61 } 62 IsUniRender()63 bool IsUniRender() const 64 { 65 return isUniRender_; 66 } 67 GetHasingSecurityOrSkipOrProtectedLayer()68 bool GetHasingSecurityOrSkipOrProtectedLayer() const 69 { 70 return hasSecurityOrSkipOrProtectedLayer_; 71 } 72 SetHasingSecurityOrSkipOrProtectedLayer(const bool & hasSecurityOrSkipOrProtectedLayer)73 void SetHasingSecurityOrSkipOrProtectedLayer(const bool &hasSecurityOrSkipOrProtectedLayer) 74 { 75 hasSecurityOrSkipOrProtectedLayer_ = hasSecurityOrSkipOrProtectedLayer; 76 } 77 78 private: 79 void ProcessSurfaceRenderNodeWithUni(RSSurfaceRenderNode& node); 80 void CaptureSingleSurfaceNodeWithUni(RSSurfaceRenderNode& node); 81 void CaptureSurfaceInDisplayWithUni(RSSurfaceRenderNode& node); 82 void ProcessSurfaceRenderNodeWithoutUni(RSSurfaceRenderNode& node); 83 void CaptureSingleSurfaceNodeWithoutUni(RSSurfaceRenderNode& node); 84 void CaptureSurfaceInDisplayWithoutUni(RSSurfaceRenderNode& node); 85 void DrawWatermarkIfNeed(RSDisplayRenderNode& node); 86 void FindHardwareEnabledNodes(); 87 void AdjustZOrderAndDrawSurfaceNode(std::vector<std::shared_ptr<RSSurfaceRenderNode>>& nodes); 88 // Reuse DrawSpherize function in RSUniRenderVisitor. 89 // Since the surfaceCache has been updated by the main screen drawing, 90 // the updated surfaceCache can be reused directly without reupdating. 91 void DrawSpherize(RSRenderNode& node); 92 // Reuse DrawChildRenderNode function partially in RSUniRenderVisitor. 93 void DrawChildRenderNode(RSRenderNode& node); 94 // Reuse UpdateCacheRenderNodeMapWithBlur function partially in RSUniRenderVisitor and rename it. 95 void ProcessCacheFilterRects(RSRenderNode& node); 96 // Reuse DrawBlurInCache function partially in RSUniRenderVisitor. 97 bool DrawBlurInCache(RSRenderNode& node); 98 std::unique_ptr<RSPaintFilterCanvas> canvas_ = nullptr; 99 bool isDisplayNode_ = false; 100 RSSurfaceCaptureConfig captureConfig_ = {}; 101 bool isUniRender_ = false; 102 bool hasSecurityOrSkipOrProtectedLayer_ = false; 103 bool isUIFirst_ = false; 104 // Fisrt node don't need to concat matrix in singleSurafce capturing. 105 bool isFirstNode_ = true; 106 std::unordered_set<NodeId> curCacheFilterRects_ = {}; 107 108 Drawing::Matrix captureMatrix_ = Drawing::Matrix(); 109 110 std::shared_ptr<RSBaseRenderEngine> renderEngine_ = nullptr; 111 112 std::vector<std::shared_ptr<RSSurfaceRenderNode>> hardwareEnabledNodes_; 113 // vector of hardwareEnabled nodes above displayNodeSurface like pointer window 114 std::vector<std::shared_ptr<RSSurfaceRenderNode>> hardwareEnabledTopNodes_; 115 }; 116 117 class RSSurfaceCaptureTask { 118 public: 119 explicit RSSurfaceCaptureTask(NodeId nodeId, 120 const RSSurfaceCaptureConfig& captureConfig, bool isProcOnBgThread = false) nodeId_(nodeId)121 : nodeId_(nodeId), captureConfig_(captureConfig), isProcOnBgThread_(isProcOnBgThread), 122 rsParallelType_(RSSystemParameters::GetRsParallelType()) {} 123 ~RSSurfaceCaptureTask() = default; 124 125 bool Run(sptr<RSISurfaceCaptureCallback> callback); 126 127 private: 128 std::shared_ptr<RSSurfaceCaptureVisitor> visitor_ = nullptr; 129 130 std::shared_ptr<Drawing::Surface> CreateSurface(const std::unique_ptr<Media::PixelMap>& pixelmap); 131 132 std::unique_ptr<Media::PixelMap> CreatePixelMapBySurfaceNode(std::shared_ptr<RSSurfaceRenderNode> node, 133 bool isUniRender = false); 134 135 std::unique_ptr<Media::PixelMap> CreatePixelMapByDisplayNode(std::shared_ptr<RSDisplayRenderNode> node, 136 bool isUniRender = false, bool hasSecurityOrSkipOrProtectedLayer = false); 137 138 bool FindSecurityOrSkipOrProtectedLayer(); 139 140 // It is currently only used on folding screen. 141 int32_t ScreenCorrection(ScreenRotation screenRotation); 142 143 NodeId nodeId_ = INVALID_NODEID; 144 145 RSSurfaceCaptureConfig captureConfig_ = {}; 146 147 ScreenRotation screenCorrection_ = ScreenRotation::ROTATION_0; 148 149 // if true, do surfaceCapture on background thread 150 bool isProcOnBgThread_ = false; 151 RsParallelType rsParallelType_; 152 }; 153 } // namespace Rosen 154 } // namespace OHOS 155 156 #endif // RS_SURFACE_CAPTURE_TASK 157