1 /*
2 * Copyright (c) 2021-2022 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 "rs_uni_render_virtual_processor.h"
17
18 #include <ctime>
19
20 #include "platform/common/rs_log.h"
21 #ifndef NEW_RENDER_CONTEXT
22 #include "platform/ohos/backend/rs_surface_frame_ohos_raster.h"
23 #endif
24 #include "pipeline/rs_uni_render_util.h"
25 #include "rs_trace.h"
26 #include "string_utils.h"
27
28 namespace OHOS {
29 namespace Rosen {
Init(RSDisplayRenderNode & node,int32_t offsetX,int32_t offsetY,ScreenId mirroredId,std::shared_ptr<RSBaseRenderEngine> renderEngine)30 bool RSUniRenderVirtualProcessor::Init(RSDisplayRenderNode& node, int32_t offsetX, int32_t offsetY, ScreenId mirroredId,
31 std::shared_ptr<RSBaseRenderEngine> renderEngine)
32 {
33 if (!RSProcessor::Init(node, offsetX, offsetY, mirroredId, renderEngine)) {
34 return false;
35 }
36
37 // Do expand screen if the mirror id is invalid.
38 if (mirroredId == INVALID_SCREEN_ID) {
39 isExpand_ = true;
40 } else {
41 isExpand_ = false;
42 }
43
44 renderFrameConfig_.usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_MEM_DMA;
45
46 auto screenManager = CreateOrGetScreenManager();
47 producerSurface_ = screenManager->GetProducerSurface(node.GetScreenId());
48 if (producerSurface_ == nullptr) {
49 RS_LOGE("RSUniRenderVirtualProcessor::Init for Screen(id %" PRIu64 "): ProducerSurface is null!",
50 node.GetScreenId());
51 return false;
52 }
53 renderFrame_ = renderEngine_->RequestFrame(producerSurface_, renderFrameConfig_, forceCPU_, false);
54 if (renderFrame_ == nullptr) {
55 return false;
56 }
57 canvas_ = renderFrame_->GetCanvas();
58 if (canvas_ == nullptr) {
59 return false;
60 }
61 auto mirrorNode = node.GetMirrorSource().lock();
62 if (mirrorNode && node.IsFirstTimeToProcessor()) {
63 auto boundsGeoPtr = (
64 mirrorNode->GetRenderProperties().GetBoundsGeometry());
65 if (boundsGeoPtr != nullptr) {
66 boundsGeoPtr->UpdateByMatrixFromSelf();
67 node.SetInitMatrix(boundsGeoPtr->GetMatrix());
68 }
69 }
70 #ifndef USE_ROSEN_DRAWING
71 SkMatrix invertMatrix;
72 if (node.GetInitMatrix().invert(&invertMatrix)) {
73 screenTransformMatrix_.postConcat(invertMatrix);
74 }
75 canvas_->concat(screenTransformMatrix_);
76 #else
77 Drawing::Matrix invertMatrix;
78 if (node.GetInitMatrix().Invert(invertMatrix)) {
79 screenTransformMatrix_ = screenTransformMatrix_ * invertMatrix;
80 }
81 canvas_->ConcatMatrix(screenTransformMatrix_);
82 #endif
83 return true;
84 }
85
PostProcess()86 void RSUniRenderVirtualProcessor::PostProcess()
87 {
88 if (producerSurface_ == nullptr) {
89 RS_LOGE("RSUniRenderVirtualProcessor::PostProcess surface is null!");
90 return;
91 }
92 auto surfaceOhos = renderFrame_->GetSurface();
93 renderEngine_->SetUiTimeStamp(renderFrame_, surfaceOhos);
94 if (renderFrame_ == nullptr) {
95 RS_LOGE("RSUniRenderVirtualProcessor::PostProcess renderFrame_ is null.");
96 return;
97 }
98 renderFrame_->Flush();
99 }
100
ProcessSurface(RSSurfaceRenderNode & node)101 void RSUniRenderVirtualProcessor::ProcessSurface(RSSurfaceRenderNode& node)
102 {
103 RS_LOGI("RSUniRenderVirtualProcessor::ProcessSurface() is not supported.");
104 }
105
ProcessDisplaySurface(RSDisplayRenderNode & node)106 void RSUniRenderVirtualProcessor::ProcessDisplaySurface(RSDisplayRenderNode& node)
107 {
108 if (!isExpand_) {
109 RS_TRACE_NAME("RSUniRenderVirtualProcessor::ProcessDisplaySurface");
110 if (canvas_ == nullptr || node.GetBuffer() == nullptr) {
111 RS_LOGE("RSUniRenderVirtualProcessor::ProcessDisplaySurface: Canvas or buffer is null!");
112 return;
113 }
114 #ifndef USE_ROSEN_DRAWING
115 SkMatrix invertMatrix;
116 if (screenTransformMatrix_.invert(&invertMatrix)) {
117 canvas_->concat(invertMatrix);
118 }
119 #else
120 Drawing::Matrix invertMatrix;
121 if (screenTransformMatrix_.Invert(invertMatrix)) {
122 canvas_->ConcatMatrix(invertMatrix);
123 }
124 #endif
125 auto params = RSUniRenderUtil::CreateBufferDrawParam(node, forceCPU_);
126 renderEngine_->DrawDisplayNodeWithParams(*canvas_, node, params);
127 }
128 }
129
ProcessDrivenSurface(RSDrivenSurfaceRenderNode & node)130 void RSUniRenderVirtualProcessor::ProcessDrivenSurface(RSDrivenSurfaceRenderNode& node)
131 {
132 RS_LOGI("RSUniRenderVirtualProcessor::ProcessDrivenSurface() is not supported.");
133 }
134 } // namespace Rosen
135 } // namespace OHOS