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