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 
17 #include "rs_virtual_screen_processor.h"
18 
19 #include <ctime>
20 
21 #include "draw/color.h"
22 #include "platform/common/rs_log.h"
23 #ifndef NEW_RENDER_CONTEXT
24 #include "platform/ohos/backend/rs_surface_frame_ohos_raster.h"
25 #endif
26 #include "rs_base_render_util.h"
27 #include "rs_divided_render_util.h"
28 #include "rs_trace.h"
29 #include "string_utils.h"
30 
31 namespace OHOS {
32 namespace Rosen {
RSVirtualScreenProcessor()33 RSVirtualScreenProcessor::RSVirtualScreenProcessor()
34 {
35 }
36 
~RSVirtualScreenProcessor()37 RSVirtualScreenProcessor::~RSVirtualScreenProcessor() noexcept
38 {
39 }
40 
Init(RSDisplayRenderNode & node,int32_t offsetX,int32_t offsetY,ScreenId mirroredId,std::shared_ptr<RSBaseRenderEngine> renderEngine)41 bool RSVirtualScreenProcessor::Init(RSDisplayRenderNode& node, int32_t offsetX, int32_t offsetY, ScreenId mirroredId,
42                                     std::shared_ptr<RSBaseRenderEngine> renderEngine)
43 {
44     if (!RSProcessor::Init(node, offsetX, offsetY, mirroredId, renderEngine)) {
45         return false;
46     }
47 
48     if (mirroredId != INVALID_SCREEN_ID) {
49         SetMirrorScreenSwap(node);
50     }
51 
52     renderFrameConfig_.usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_MEM_DMA;
53 
54     auto screenManager = CreateOrGetScreenManager();
55     if (screenManager == nullptr) {
56         RS_LOGE("RSVirtualScreenProcessor::Init for Screen(id %{public}" PRIu64 "): screenManager is null!",
57             node.GetScreenId());
58         return false;
59     }
60     producerSurface_ = screenManager->GetProducerSurface(node.GetScreenId());
61     if (producerSurface_ == nullptr) {
62         RS_LOGE("RSVirtualScreenProcessor::Init for Screen(id %{public}" PRIu64 "): ProducerSurface is null!",
63             node.GetScreenId());
64         return false;
65     }
66 
67     bool forceCPU = false;
68     renderFrame_ = renderEngine_->RequestFrame(producerSurface_, renderFrameConfig_, forceCPU, false);
69     if (renderFrame_ == nullptr) {
70         RS_LOGE("RSVirtualScreenProcessor::Init: renderFrame_ is null!");
71         return false;
72     }
73     canvas_ = renderFrame_->GetCanvas();
74     if (canvas_ == nullptr) {
75         return false;
76     }
77     canvas_->ConcatMatrix(screenTransformMatrix_);
78 
79     return true;
80 }
81 
PostProcess()82 void RSVirtualScreenProcessor::PostProcess()
83 {
84     if (renderFrame_ == nullptr || canvas_ == nullptr || renderEngine_ == nullptr) {
85         RS_LOGE("RSVirtualScreenProcessor::PostProcess renderFrame or canvas or renderEngine is nullptr");
86         return;
87     }
88     if (isSecurityDisplay_ && displayHasSecSurface_) {
89         canvas_->Clear(Drawing::Color::COLOR_BLACK);
90     }
91     auto surfaceOhos = renderFrame_->GetSurface();
92     renderEngine_->SetUiTimeStamp(renderFrame_, surfaceOhos);
93     renderFrame_->Flush();
94 }
95 
ProcessSurface(RSSurfaceRenderNode & node)96 void RSVirtualScreenProcessor::ProcessSurface(RSSurfaceRenderNode& node)
97 {
98     if (canvas_ == nullptr || renderEngine_ == nullptr) {
99         RS_LOGE("RSVirtualScreenProcessor::ProcessSurface canvas or renderEngine is nullptr");
100         return;
101     }
102 
103     std::string traceInfo;
104     AppendFormat(traceInfo, "RSVirtualScreenProcessor::ProcessSurface Node:%s ", node.GetName().c_str());
105     RS_TRACE_NAME(traceInfo);
106 
107     // prepare BufferDrawParam
108     // in display's coordinate.
109     // clipHole: false.
110     // forceCPU: true.
111     auto params = RSDividedRenderUtil::CreateBufferDrawParam(node, false, false, false);
112     const float adaptiveDstWidth = params.dstRect.GetWidth() * mirrorAdaptiveCoefficient_;
113     const float adaptiveDstHeight = params.dstRect.GetHeight() * mirrorAdaptiveCoefficient_;
114     params.dstRect.SetLeft(0);
115     params.dstRect.SetTop(0);
116     params.dstRect.SetRight(adaptiveDstWidth);
117     params.dstRect.SetBottom(adaptiveDstHeight);
118     renderEngine_->DrawSurfaceNodeWithParams(*canvas_, node, params);
119 }
120 
ProcessDisplaySurface(RSDisplayRenderNode & node)121 void RSVirtualScreenProcessor::ProcessDisplaySurface(RSDisplayRenderNode& node)
122 {
123     RS_LOGI("RSVirtualScreenProcessor::ProcessDisplaySurface() is not supported.");
124 }
125 
ProcessRcdSurface(RSRcdSurfaceRenderNode & node)126 void RSVirtualScreenProcessor::ProcessRcdSurface(RSRcdSurfaceRenderNode& node)
127 {
128     RS_LOGI("RSVirtualScreenProcessor::ProcessRcdSurface() is not supported.");
129 }
130 } // namespace Rosen
131 } // namespace OHOS
132