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 #include "pipeline/rs_uni_ui_capture.h"
17 
18 #include <functional>
19 #include <memory>
20 #include <sys/mman.h>
21 
22 #include "include/core/SkRect.h"
23 #include "rs_trace.h"
24 
25 #include "common/rs_common_def.h"
26 #include "common/rs_obj_abs_geometry.h"
27 #include "drawable/rs_canvas_drawing_render_node_drawable.h"
28 #include "drawable/rs_render_node_drawable_adapter.h"
29 #include "offscreen_render/rs_offscreen_render_thread.h"
30 #include "pipeline/rs_canvas_drawing_render_node.h"
31 #include "pipeline/rs_dirty_region_manager.h"
32 #include "pipeline/rs_divided_render_util.h"
33 #include "pipeline/rs_main_thread.h"
34 #include "pipeline/rs_uni_render_judgement.h"
35 #include "pipeline/rs_uni_render_thread.h"
36 #include "pipeline/rs_uni_render_util.h"
37 #include "platform/common/rs_log.h"
38 #include "render/rs_drawing_filter.h"
39 #include "render/rs_skia_filter.h"
40 #include "pipeline/rs_render_engine.h"
41 
42 namespace OHOS {
43 namespace Rosen {
44 
45 const int FAKE_WIDTH = 10; // When the width and height of the node are not set, use the fake width
46 const int FAKE_HEIGHT = 10; // When the width and height of the node are not set, use the fake height
47 
RSUniUICapture(NodeId nodeId,const RSSurfaceCaptureConfig & captureConfig)48 RSUniUICapture::RSUniUICapture(NodeId nodeId, const RSSurfaceCaptureConfig& captureConfig)
49     : nodeId_(nodeId), captureConfig_(captureConfig)
50 {
51     isUniRender_ = RSUniRenderJudgement::IsUniRender();
52 }
53 
TakeLocalCapture()54 std::shared_ptr<Media::PixelMap> RSUniUICapture::TakeLocalCapture()
55 {
56     if (ROSEN_EQ(captureConfig_.scaleX, 0.f) || ROSEN_EQ(captureConfig_.scaleY, 0.f) ||
57         captureConfig_.scaleX < 0.f || captureConfig_.scaleY < 0.f) {
58         RS_LOGE("RSUniUICapture::TakeLocalCapture: scale is invalid.");
59         return nullptr;
60     }
61     auto node = RSMainThread::Instance()->GetContext().GetNodeMap().GetRenderNode<RSRenderNode>(nodeId_);
62     if (node == nullptr) {
63         RS_LOGE("RSUniUICapture::TakeLocalCapture node is nullptr return");
64         return nullptr;
65     }
66     std::shared_ptr<RSUniUICaptureVisitor> visitor =
67         std::make_shared<RSUniUICaptureVisitor>(nodeId_, captureConfig_);
68     auto recordingCanvas = std::make_shared<ExtendRecordingCanvas>(FAKE_WIDTH, FAKE_HEIGHT, false);
69     PostTaskToRSRecord(recordingCanvas, node, visitor);
70     auto drawCallList = recordingCanvas->GetDrawCmdList();
71     if (drawCallList == nullptr) {
72         RS_LOGE("RSUniUICapture::TakeLocalCapture: drawCallList == nullptr");
73         return nullptr;
74     }
75     std::shared_ptr<Media::PixelMap> pixelmap = CreatePixelMapByNode(node);
76     if (pixelmap == nullptr) {
77         RS_LOGE("RSUniUICapture::TakeLocalCapture: pixelmap == nullptr!");
78         return nullptr;
79     }
80     RS_LOGD("RSUniUICapture::TakeLocalCapture: PixelMap: (%{public}d, %{public}d)", pixelmap->GetWidth(),
81         pixelmap->GetHeight());
82     auto drSurface = CreateSurface(pixelmap);
83     if (drSurface == nullptr) {
84         return nullptr;
85     }
86     auto canvas = std::make_shared<RSPaintFilterCanvas>(drSurface.get());
87     canvas->SetOffscreen(true);
88     RS_LOGD("RSUniUICapture::TakeLocalCapture: drawCallList size is %{public}zu", drawCallList->GetOpItemSize());
89     const auto& property = node->GetRenderProperties();
90     Drawing::Rect rect = Drawing::Rect(0, 0, property.GetBoundsWidth(), property.GetBoundsHeight());
91     drawCallList->Playback(*canvas, &rect);
92     if (!isUniRender_ || isUseCpuSurface_) {
93         return pixelmap;
94     }
95 #if defined(ROSEN_OHOS) && (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK))
96     auto img = drSurface->GetImageSnapshot();
97     if (!img) {
98         RSOffscreenRenderThread::Instance().CleanGrResource();
99         RS_LOGE("RSUniUICapture::TakeLocalCapture: img is nullptr");
100         return nullptr;
101     }
102     if (!CopyDataToPixelMap(img, pixelmap)) {
103         RSOffscreenRenderThread::Instance().CleanGrResource();
104         RS_LOGE("RSUniUICapture::TakeLocalCapture: CopyDataToPixelMap failed");
105         return nullptr;
106     }
107     RSOffscreenRenderThread::Instance().CleanGrResource();
108 #endif
109     return pixelmap;
110 }
111 
CopyDataToPixelMap(std::shared_ptr<Drawing::Image> img,std::shared_ptr<Media::PixelMap> pixelmap)112 bool RSUniUICapture::CopyDataToPixelMap(std::shared_ptr<Drawing::Image> img,
113     std::shared_ptr<Media::PixelMap> pixelmap)
114 {
115     auto size = pixelmap->GetRowBytes() * pixelmap->GetHeight();
116     Drawing::ImageInfo info = Drawing::ImageInfo(pixelmap->GetWidth(), pixelmap->GetHeight(),
117         Drawing::ColorType::COLORTYPE_RGBA_8888, Drawing::AlphaType::ALPHATYPE_PREMUL);
118     if (size <= 0) {
119         return false;
120     }
121 #ifdef ROSEN_OHOS
122     int fd = AshmemCreate("RSUniUICapture Data", size);
123     if (fd < 0) {
124         RS_LOGE("RSUniUICapture::CopyDataToPixelMap AshmemCreate fd < 0");
125         return false;
126     }
127     int result = AshmemSetProt(fd, PROT_READ | PROT_WRITE);
128     if (result < 0) {
129         RS_LOGE("RSUniUICapture::CopyDataToPixelMap AshmemSetProt error");
130         ::close(fd);
131         return false;
132     }
133     void* ptr = ::mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
134     auto data = static_cast<uint8_t*>(ptr);
135     if (ptr == MAP_FAILED || ptr == nullptr) {
136         RS_LOGE("RSUniUICapture::CopyDataToPixelMap data is nullptr");
137         ::close(fd);
138         return false;
139     }
140     if (!img->ReadPixels(info, data, pixelmap->GetRowBytes(), 0, 0)) {
141         RS_LOGE("RSUniUICapture::CopyDataToPixelMap readPixels failed");
142         ::munmap(ptr, size);
143         ::close(fd);
144         return false;
145     }
146     void* fdPtr = new int32_t();
147     if (fdPtr == nullptr) {
148         ::munmap(ptr, size);
149         ::close(fd);
150         return false;
151     }
152     *static_cast<int32_t*>(fdPtr) = fd;
153     pixelmap->SetPixelsAddr(data, fdPtr, size, Media::AllocatorType::SHARE_MEM_ALLOC, nullptr);
154 #else
155     auto data = static_cast<uint8_t *>(size);
156     if (data == nullptr) {
157         RS_LOGE("RSUniUICapture::CopyDataToPixelMap data is nullptr");
158         return false;
159     }
160     if (!img->ReadPixels(info, data, pixelmap->GetRowBytes(), 0, 0)) {
161         RS_LOGE("RSUniUICapture::CopyDataToPixelMap readPixels failed");
162         free(data);
163         data = nullptr;
164         return false;
165     }
166     pixelmap->SetPixelsAddr(data, nullptr, size, Media::AllocatorType::HEAP_ALLOC, nullptr);
167 #endif
168     return true;
169 }
170 
CreatePixelMapByNode(std::shared_ptr<RSRenderNode> node) const171 std::shared_ptr<Media::PixelMap> RSUniUICapture::CreatePixelMapByNode(std::shared_ptr<RSRenderNode> node) const
172 {
173     float pixmapWidth = node->GetRenderProperties().GetBoundsWidth();
174     float pixmapHeight = node->GetRenderProperties().GetBoundsHeight();
175     Media::InitializationOptions opts;
176     opts.size.width = ceil(pixmapWidth * captureConfig_.scaleX);
177     opts.size.height = ceil(pixmapHeight * captureConfig_.scaleY);
178     return Media::PixelMap::Create(opts);
179 }
180 
CreateSurface(const std::shared_ptr<Media::PixelMap> & pixelmap)181 std::shared_ptr<Drawing::Surface> RSUniUICapture::CreateSurface(
182     const std::shared_ptr<Media::PixelMap>& pixelmap)
183 {
184     if (pixelmap == nullptr) {
185         RS_LOGE("RSUniUICapture::CreateSurface: pixelmap == nullptr");
186         return nullptr;
187     }
188     auto address = const_cast<uint32_t*>(pixelmap->GetPixel32(0, 0));
189     if (address == nullptr) {
190         RS_LOGE("RSUniUICapture::CreateSurface: address == nullptr");
191         return nullptr;
192     }
193 
194     Drawing::ImageInfo info = Drawing::ImageInfo{pixelmap->GetWidth(), pixelmap->GetHeight(),
195         Drawing::COLORTYPE_RGBA_8888, Drawing::ALPHATYPE_PREMUL};
196     if (!isUniRender_) {
197         return Drawing::Surface::MakeRasterDirect(info, address, pixelmap->GetRowBytes());
198     }
199     std::shared_ptr<Drawing::Surface> surface;
200 #if defined(ROSEN_OHOS) && (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK))
201     auto renderContext = RSOffscreenRenderThread::Instance().GetRenderContext();
202     if (renderContext == nullptr) {
203         RS_LOGE("RSUniUICapture::CreateSurface: renderContext is nullptr");
204         return nullptr;
205     }
206     surface = Drawing::Surface::MakeRenderTarget(renderContext->GetDrGPUContext(), false, info);
207 #else
208     surface = Drawing::Surface::MakeRasterDirect(info, address, pixelmap->GetRowBytes());
209     isUseCpuSurface_ = true;
210 #endif
211     if (!surface) {
212         surface = Drawing::Surface::MakeRasterDirect(info, address, pixelmap->GetRowBytes());
213         isUseCpuSurface_ = true;
214     }
215     return surface;
216 }
217 
RSUniUICaptureVisitor(NodeId nodeId,const RSSurfaceCaptureConfig & captureConfig)218 RSUniUICapture::RSUniUICaptureVisitor::RSUniUICaptureVisitor(NodeId nodeId,
219     const RSSurfaceCaptureConfig& captureConfig)
220     : nodeId_(nodeId), captureConfig_(captureConfig)
221 {
222     isUniRender_ = RSUniRenderJudgement::IsUniRender();
223     if (!isUniRender_) {
224         renderEngine_ = RSMainThread::Instance()->GetRenderEngine();
225     }
226 }
227 
PostTaskToRSRecord(std::shared_ptr<ExtendRecordingCanvas> canvas,std::shared_ptr<RSRenderNode> node,std::shared_ptr<RSUniUICaptureVisitor> visitor)228 void RSUniUICapture::PostTaskToRSRecord(std::shared_ptr<ExtendRecordingCanvas> canvas,
229     std::shared_ptr<RSRenderNode> node, std::shared_ptr<RSUniUICaptureVisitor> visitor)
230 {
231     if (canvas == nullptr || node == nullptr || visitor == nullptr) {
232         RS_LOGE("RSUniUICapture::PostTaskToRSRecord has nullptr");
233         return;
234     }
235     std::function<void()> recordingDrawCall = [canvas, node, visitor]() -> void {
236         visitor->SetCanvas(canvas);
237         node->ApplyModifiers();
238         node->PrepareChildrenForApplyModifiers();
239         node->Process(visitor);
240     };
241     RSMainThread::Instance()->PostSyncTask(recordingDrawCall);
242 }
243 
SetPaintFilterCanvas(std::shared_ptr<RSPaintFilterCanvas> canvas)244 void RSUniUICapture::RSUniUICaptureVisitor::SetPaintFilterCanvas(std::shared_ptr<RSPaintFilterCanvas> canvas)
245 {
246     if (canvas == nullptr) {
247         RS_LOGE("RSUniUICaptureVisitor::SetCanvas: canvas == nullptr");
248         return;
249     }
250     canvas_ = canvas;
251     canvas_->Scale(captureConfig_.scaleX, captureConfig_.scaleY);
252     canvas_->SetDisableFilterCache(true);
253 }
254 
SetCanvas(std::shared_ptr<ExtendRecordingCanvas> canvas)255 void RSUniUICapture::RSUniUICaptureVisitor::SetCanvas(std::shared_ptr<ExtendRecordingCanvas> canvas)
256 {
257     if (canvas == nullptr) {
258         RS_LOGE("RSUniUICaptureVisitor::SetCanvas: canvas == nullptr");
259         return;
260     }
261     std::shared_ptr<Drawing::GPUContext> sharedContext = nullptr;
262     if (isUniRender_) {
263         sharedContext = RSOffscreenRenderThread::Instance().GetRenderContext()->GetSharedDrGPUContext();
264     } else {
265         sharedContext = RSMainThread::Instance()->GetRenderEngine()->GetRenderContext()->GetSharedDrGPUContext();
266     }
267     canvas->SetGrRecordingContext(sharedContext);
268     canvas_ = std::make_shared<RSPaintFilterCanvas>(canvas.get());
269     canvas_->Scale(captureConfig_.scaleX, captureConfig_.scaleY);
270     canvas_->SetDisableFilterCache(true);
271     canvas_->SetRecordingState(true);
272     canvas_->SetCacheType(Drawing::CacheType::DISABLED);
273 }
274 
ProcessChildren(RSRenderNode & node)275 void RSUniUICapture::RSUniUICaptureVisitor::ProcessChildren(RSRenderNode& node)
276 {
277     auto sortedChildren = node.GetSortedChildren();
278     if (node.GetRenderProperties().GetUseShadowBatching()) {
279         for (auto& child : *sortedChildren) {
280             if (auto node = child->ReinterpretCastTo<RSCanvasRenderNode>()) {
281                 node->ProcessShadowBatching(*canvas_);
282             }
283         }
284     }
285     for (auto& child : *sortedChildren) {
286         child->Process(shared_from_this());
287     }
288 }
289 
ProcessRootRenderNode(RSRootRenderNode & node)290 void RSUniUICapture::RSUniUICaptureVisitor::ProcessRootRenderNode(RSRootRenderNode& node)
291 {
292     if (!node.ShouldPaint()) {
293         RS_LOGD("RSUniUICaptureVisitor::ProcessRootRenderNode, no need process");
294         return;
295     }
296 
297     if (!canvas_) {
298         RS_LOGE("RSUniUICaptureVisitor::ProcessRootRenderNode, canvas is nullptr");
299         return;
300     }
301 
302     canvas_->Save();
303     ProcessCanvasRenderNode(node);
304     canvas_->Restore();
305 }
306 
ProcessCanvasRenderNode(RSCanvasRenderNode & node)307 void RSUniUICapture::RSUniUICaptureVisitor::ProcessCanvasRenderNode(RSCanvasRenderNode& node)
308 {
309     if (!node.ShouldPaint()) {
310         RS_LOGD("RSUniUICaptureVisitor::ProcessCanvasRenderNode, no need process");
311         return;
312     }
313     if (!canvas_) {
314         RS_LOGE("RSUniUICaptureVisitor::ProcessCanvasRenderNode, canvas is nullptr");
315         return;
316     }
317     if (node.GetId() == nodeId_) {
318         // When drawing nodes, canvas will offset the bounds value, so we will move in reverse here first
319         const auto& property = node.GetRenderProperties();
320         auto& geoPtr = (property.GetBoundsGeometry());
321         Drawing::Matrix relativeMatrix = Drawing::Matrix();
322         relativeMatrix.Set(Drawing::Matrix::Index::SCALE_X, captureConfig_.scaleX);
323         relativeMatrix.Set(Drawing::Matrix::Index::SCALE_Y, captureConfig_.scaleY);
324         Drawing::Matrix invertMatrix;
325         if (geoPtr->GetMatrix().Invert(invertMatrix)) {
326             relativeMatrix.PreConcat(invertMatrix);
327         }
328         canvas_->SetMatrix(relativeMatrix);
329     }
330     node.ProcessRenderBeforeChildren(*canvas_);
331     if (node.GetType() == RSRenderNodeType::CANVAS_DRAWING_NODE) {
332         if (node.GetStagingRenderParams()->NeedSync()) {
333             RSUniRenderThread::Instance().PostSyncTask([&node]() mutable { node.Sync(); });
334         }
335         auto drawable = DrawableV2::RSRenderNodeDrawableAdapter::GetDrawableById(node.GetId());
336         if (!drawable) {
337             return;
338         }
339         auto canvasDrawable = std::static_pointer_cast<DrawableV2::RSCanvasDrawingRenderNodeDrawable>(drawable);
340         canvasDrawable->DrawCaptureImage(*canvas_);
341     } else {
342         node.ProcessRenderContents(*canvas_);
343     }
344     ProcessChildren(node);
345     node.ProcessRenderAfterChildren(*canvas_);
346 }
347 
ProcessEffectRenderNode(RSEffectRenderNode & node)348 void RSUniUICapture::RSUniUICaptureVisitor::ProcessEffectRenderNode(RSEffectRenderNode& node)
349 {
350     if (!node.ShouldPaint()) {
351         RS_LOGD("RSUniUICapture::RSUniUICaptureVisitor::ProcessEffectRenderNode, no need process");
352         return;
353     }
354     if (!canvas_) {
355         RS_LOGE("RSUniUICapture::RSUniUICaptureVisitor::ProcessEffectRenderNode, canvas is nullptr");
356         return;
357     }
358     node.ProcessRenderBeforeChildren(*canvas_);
359     ProcessChildren(node);
360     node.ProcessRenderAfterChildren(*canvas_);
361 }
362 
ProcessSurfaceRenderNode(RSSurfaceRenderNode & node)363 void RSUniUICapture::RSUniUICaptureVisitor::ProcessSurfaceRenderNode(RSSurfaceRenderNode& node)
364 {
365     if (canvas_ == nullptr) {
366         RS_LOGE("RSUniUICaptureVisitor::ProcessSurfaceRenderNode, canvas is nullptr");
367         return;
368     }
369 
370     if (!node.ShouldPaint()) {
371         RS_LOGD("RSUniUICaptureVisitor::ProcessSurfaceRenderNode node: %{public}" PRIu64 " invisible", node.GetId());
372         return;
373     }
374     if (isUniRender_) {
375         ProcessSurfaceRenderNodeWithUni(node);
376     } else {
377         ProcessSurfaceViewWithoutUni(node);
378     }
379 }
380 
ProcessSurfaceRenderNodeWithUni(RSSurfaceRenderNode & node)381 void RSUniUICapture::RSUniUICaptureVisitor::ProcessSurfaceRenderNodeWithUni(RSSurfaceRenderNode& node)
382 {
383     if (canvas_ == nullptr) {
384         RS_LOGE("RSUniUICaptureVisitor::ProcessSurfaceRenderNodeWithUni canvas is nullptr");
385         return;
386     }
387     auto& geoPtr = (node.GetRenderProperties().GetBoundsGeometry());
388     if (geoPtr == nullptr) {
389         RS_LOGI(
390             "RSUniUICaptureVisitor::ProcessSurfaceRenderNode node:%{public}" PRIu64 ", get geoPtr failed",
391                 node.GetId());
392         return;
393     }
394     Drawing::AutoCanvasRestore acr(*canvas_, true);
395     canvas_->MultiplyAlpha(node.GetRenderProperties().GetAlpha());
396     ProcessSurfaceViewWithUni(node);
397 }
398 
ProcessSurfaceViewWithUni(RSSurfaceRenderNode & node)399 void RSUniUICapture::RSUniUICaptureVisitor::ProcessSurfaceViewWithUni(RSSurfaceRenderNode& node)
400 {
401     if (canvas_ == nullptr) {
402         RS_LOGE("RSUniUICaptureVisitor::ProcessSurfaceViewWithUni canvas is nullptr");
403         return;
404     }
405     const auto& property = node.GetRenderProperties();
406     auto& geoPtr = (property.GetBoundsGeometry());
407     if (!geoPtr) {
408         RS_LOGE("RSUniUICaptureVisitor::ProcessSurfaceViewWithUni node:%{public}" PRIu64 ", get geoPtr failed",
409             node.GetId());
410         return;
411     }
412     canvas_->ConcatMatrix(geoPtr->GetMatrix());
413 
414     bool isSelfDrawingSurface = node.GetSurfaceNodeType() == RSSurfaceNodeType::SELF_DRAWING_NODE;
415     if (isSelfDrawingSurface) {
416         canvas_->Save();
417     }
418     auto boundsWidth = std::round(property.GetBoundsWidth());
419     auto boundsHeight = std::round(property.GetBoundsHeight());
420     const RectF absBounds = { 0, 0, boundsWidth, boundsHeight };
421     RRect absClipRRect = RRect(absBounds, property.GetCornerRadius());
422     if (isSelfDrawingSurface) {
423         RSPropertiesPainter::DrawShadow(property, *canvas_, &absClipRRect);
424         RSPropertiesPainter::DrawOutline(property, *canvas_);
425     }
426     if (isSelfDrawingSurface && !property.GetCornerRadius().IsZero()) {
427         canvas_->ClipRoundRect(RSPropertiesPainter::RRect2DrawingRRect(absClipRRect),
428             Drawing::ClipOp::INTERSECT, true);
429     } else {
430         canvas_->ClipRect(Drawing::Rect(0, 0, boundsWidth, boundsHeight), Drawing::ClipOp::INTERSECT, false);
431     }
432     if (isSelfDrawingSurface) {
433         RSPropertiesPainter::DrawBackground(property, *canvas_);
434         RSPropertiesPainter::DrawMask(property, *canvas_);
435         RSPropertiesPainter::DrawFilter(property, *canvas_, FilterType::BACKGROUND_FILTER);
436     } else {
437         auto backgroundColor = static_cast<Drawing::ColorQuad>(property.GetBackgroundColor().AsArgbInt());
438         if (Drawing::Color::ColorQuadGetA(backgroundColor) != Drawing::Color::COLOR_TRANSPARENT) {
439             canvas_->DrawColor(backgroundColor);
440         }
441     }
442     if (isSelfDrawingSurface) {
443         canvas_->Restore();
444     }
445 
446     auto surfaceHandler = node.GetMutableRSSurfaceHandler();
447     if (surfaceHandler->GetBuffer() != nullptr) {
448         if (auto recordingCanvas = static_cast<ExtendRecordingCanvas*>(canvas_->GetRecordingCanvas())) {
449             auto params = RSUniRenderUtil::CreateBufferDrawParam(node, false);
450             auto buffer = surfaceHandler->GetBuffer();
451             DrawingSurfaceBufferInfo rsSurfaceBufferInfo(buffer, params.dstRect.GetLeft(), params.dstRect.GetTop(),
452                 params.dstRect.GetWidth(), params.dstRect.GetHeight());
453             recordingCanvas->ConcatMatrix(params.matrix);
454             recordingCanvas->DrawSurfaceBuffer(rsSurfaceBufferInfo);
455         }
456     }
457     if (isSelfDrawingSurface) {
458         RSPropertiesPainter::DrawFilter(property, *canvas_, FilterType::FOREGROUND_FILTER);
459     }
460     ProcessChildren(node);
461 }
462 
ProcessSurfaceViewWithoutUni(RSSurfaceRenderNode & node)463 void RSUniUICapture::RSUniUICaptureVisitor::ProcessSurfaceViewWithoutUni(RSSurfaceRenderNode& node)
464 {
465     if (canvas_ == nullptr) {
466         RS_LOGE("RSUniUICaptureVisitor::ProcessSurfaceViewWithoutUni canvas is nullptr");
467         return;
468     }
469     Drawing::Matrix translateMatrix;
470     auto parentPtr = node.GetParent().lock();
471     if (parentPtr != nullptr && parentPtr->IsInstanceOf<RSSurfaceRenderNode>()) {
472         // calculate the offset from this node's parent, and perform translate.
473         auto parentNode = std::static_pointer_cast<RSSurfaceRenderNode>(parentPtr);
474         const float parentNodeTranslateX = parentNode->GetTotalMatrix().Get(Drawing::Matrix::Index::TRANS_X);
475         const float parentNodeTranslateY = parentNode->GetTotalMatrix().Get(Drawing::Matrix::Index::TRANS_Y);
476         const float thisNodetranslateX = node.GetTotalMatrix().Get(Drawing::Matrix::Index::TRANS_X);
477         const float thisNodetranslateY = node.GetTotalMatrix().Get(Drawing::Matrix::Index::TRANS_Y);
478         translateMatrix.PreTranslate(
479             thisNodetranslateX - parentNodeTranslateX, thisNodetranslateY - parentNodeTranslateY);
480     }
481 
482     auto surfaceHandler = node.GetMutableRSSurfaceHandler();
483     if (node.GetChildrenCount() > 0) {
484         if (node.GetId() != nodeId_) {
485             canvas_->ConcatMatrix(translateMatrix);
486         }
487         const auto saveCnt = canvas_->Save();
488         ProcessChildren(node);
489         canvas_->RestoreToCount(saveCnt);
490         if (surfaceHandler->GetBuffer() != nullptr) {
491             // in node's local coordinate.
492             auto params = RSDividedRenderUtil::CreateBufferDrawParam(node, true, false, true, false);
493             renderEngine_->DrawSurfaceNodeWithParams(*canvas_, node, params);
494         }
495     } else {
496         canvas_->Save();
497         if (node.GetId() != nodeId_) {
498             canvas_->ConcatMatrix(translateMatrix);
499         }
500         if (surfaceHandler->GetBuffer() != nullptr) {
501             // in node's local coordinate.
502             auto params = RSDividedRenderUtil::CreateBufferDrawParam(node, true, false, true, false);
503             renderEngine_->DrawSurfaceNodeWithParams(*canvas_, node, params);
504         }
505         canvas_->Restore();
506     }
507 }
508 
PrepareChildren(RSRenderNode & node)509 void RSUniUICapture::RSUniUICaptureVisitor::PrepareChildren(RSRenderNode& node)
510 {
511     for (auto& child : *node.GetSortedChildren()) {
512         child->Prepare(shared_from_this());
513     }
514 }
515 
PrepareCanvasRenderNode(RSCanvasRenderNode & node)516 void RSUniUICapture::RSUniUICaptureVisitor::PrepareCanvasRenderNode(RSCanvasRenderNode& node)
517 {
518     auto dirtyManager = std::make_shared<RSDirtyRegionManager>();
519     node.Update(*dirtyManager, nullptr, false);
520     PrepareChildren(node);
521 }
522 
PrepareSurfaceRenderNode(RSSurfaceRenderNode & node)523 void RSUniUICapture::RSUniUICaptureVisitor::PrepareSurfaceRenderNode(RSSurfaceRenderNode& node)
524 {
525     auto dirtyManager = std::make_shared<RSDirtyRegionManager>();
526     node.Update(*dirtyManager, nullptr, false);
527     PrepareChildren(node);
528 }
529 
PrepareRootRenderNode(RSRootRenderNode & node)530 void RSUniUICapture::RSUniUICaptureVisitor::PrepareRootRenderNode(RSRootRenderNode& node)
531 {
532     PrepareCanvasRenderNode(node);
533 }
534 
PrepareEffectRenderNode(RSEffectRenderNode & node)535 void RSUniUICapture::RSUniUICaptureVisitor::PrepareEffectRenderNode(RSEffectRenderNode& node)
536 {
537     auto dirtyManager = std::make_shared<RSDirtyRegionManager>();
538     node.Update(*dirtyManager, nullptr, false);
539     PrepareChildren(node);
540 }
541 } // namespace Rosen
542 } // namespace OHOS
543