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 "platform/common/rs_log.h"
22 #ifndef NEW_RENDER_CONTEXT
23 #include "platform/ohos/backend/rs_surface_frame_ohos_raster.h"
24 #endif
25 #include "rs_base_render_util.h"
26 #include "rs_divided_render_util.h"
27 #include "rs_trace.h"
28 #include "string_utils.h"
29
30 namespace OHOS {
31 namespace Rosen {
RSVirtualScreenProcessor()32 RSVirtualScreenProcessor::RSVirtualScreenProcessor()
33 {
34 }
35
~RSVirtualScreenProcessor()36 RSVirtualScreenProcessor::~RSVirtualScreenProcessor() noexcept
37 {
38 }
39
Init(RSDisplayRenderNode & node,int32_t offsetX,int32_t offsetY,ScreenId mirroredId,std::shared_ptr<RSBaseRenderEngine> renderEngine)40 bool RSVirtualScreenProcessor::Init(RSDisplayRenderNode& node, int32_t offsetX, int32_t offsetY, ScreenId mirroredId,
41 std::shared_ptr<RSBaseRenderEngine> renderEngine)
42 {
43 if (!RSProcessor::Init(node, offsetX, offsetY, mirroredId, renderEngine)) {
44 return false;
45 }
46
47 if (mirroredId != INVALID_SCREEN_ID) {
48 SetMirrorScreenSwap(node);
49 }
50
51 renderFrameConfig_.usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_MEM_DMA;
52
53 auto screenManager = CreateOrGetScreenManager();
54 producerSurface_ = screenManager->GetProducerSurface(node.GetScreenId());
55 if (producerSurface_ == nullptr) {
56 RS_LOGE(
57 "RSVirtualScreenProcessor::Init for Screen(id %" PRIu64 "): ProducerSurface is null!", node.GetScreenId());
58 return false;
59 }
60
61 bool forceCPU = false;
62 renderFrame_ = renderEngine_->RequestFrame(producerSurface_, renderFrameConfig_, forceCPU, false);
63 if (renderFrame_ == nullptr) {
64 RS_LOGE("RSVirtualScreenProcessor::Init: renderFrame_ is null!");
65 return false;
66 }
67 canvas_ = renderFrame_->GetCanvas();
68 if (canvas_ == nullptr) {
69 return false;
70 }
71 #ifndef USE_ROSEN_DRAWING
72 canvas_->concat(screenTransformMatrix_);
73 #else
74 canvas_->ConcatMatrix(screenTransformMatrix_);
75 #endif
76
77 return true;
78 }
79
PostProcess()80 void RSVirtualScreenProcessor::PostProcess()
81 {
82 if (producerSurface_ == nullptr) {
83 RS_LOGE("RSVirtualScreenProcessor::PostProcess surface is null!");
84 return;
85 }
86 auto surfaceOhos = renderFrame_->GetSurface();
87 renderEngine_->SetUiTimeStamp(renderFrame_, surfaceOhos);
88
89 if (renderFrame_ == nullptr) {
90 RS_LOGE("RSVirtualScreenProcessor::PostProcess renderFrame_ is null.");
91 return;
92 }
93 renderFrame_->Flush();
94 }
95
ProcessSurface(RSSurfaceRenderNode & node)96 void RSVirtualScreenProcessor::ProcessSurface(RSSurfaceRenderNode& node)
97 {
98 if (canvas_ == nullptr) {
99 RS_LOGE("RSVirtualScreenProcessor::ProcessSurface: Canvas is null!");
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 #ifndef USE_ROSEN_DRAWING
113 const float adaptiveDstWidth = params.dstRect.width() * mirrorAdaptiveCoefficient_;
114 const float adaptiveDstHeight = params.dstRect.height() * mirrorAdaptiveCoefficient_;
115 params.dstRect.setWH(adaptiveDstWidth, adaptiveDstHeight);
116 #else
117 const float adaptiveDstWidth = params.dstRect.GetWidth() * mirrorAdaptiveCoefficient_;
118 const float adaptiveDstHeight = params.dstRect.GetHeight() * mirrorAdaptiveCoefficient_;
119 params.dstRect.SetLeft(0);
120 params.dstRect.SetTop(0);
121 params.dstRect.SetRight(adaptiveDstWidth);
122 params.dstRect.SetBottom(adaptiveDstHeight);
123 #endif
124 renderEngine_->DrawSurfaceNodeWithParams(*canvas_, node, params);
125 }
126
ProcessDisplaySurface(RSDisplayRenderNode & node)127 void RSVirtualScreenProcessor::ProcessDisplaySurface(RSDisplayRenderNode& node)
128 {
129 RS_LOGI("RSVirtualScreenProcessor::ProcessDisplaySurface() is not supported.");
130 }
131
ProcessDrivenSurface(RSDrivenSurfaceRenderNode & node)132 void RSVirtualScreenProcessor::ProcessDrivenSurface(RSDrivenSurfaceRenderNode& node)
133 {
134 RS_LOGI("RSVirtualScreenProcessor::ProcessDrivenSurface() is not supported.");
135 }
136 } // namespace Rosen
137 } // namespace OHOS
138