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 #include "platform/ohos/backend/rs_surface_frame_ohos_raster.h"
23 #include "rs_base_render_util.h"
24 #include "rs_divided_render_util.h"
25 #include "rs_trace.h"
26 #include "string_utils.h"
27
28 namespace OHOS {
29 namespace Rosen {
RSVirtualScreenProcessor()30 RSVirtualScreenProcessor::RSVirtualScreenProcessor()
31 {
32 }
33
~RSVirtualScreenProcessor()34 RSVirtualScreenProcessor::~RSVirtualScreenProcessor() noexcept
35 {
36 }
37
Init(RSDisplayRenderNode & node,int32_t offsetX,int32_t offsetY,ScreenId mirroredId)38 bool RSVirtualScreenProcessor::Init(RSDisplayRenderNode& node, int32_t offsetX, int32_t offsetY, ScreenId mirroredId)
39 {
40 if (!RSProcessor::Init(node, offsetX, offsetY, mirroredId)) {
41 return false;
42 }
43
44 if (mirroredId != INVALID_SCREEN_ID) {
45 SetMirrorScreenSwap(node);
46 }
47
48 renderFrameConfig_.usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_MEM_DMA;
49
50 auto screenManager = CreateOrGetScreenManager();
51 producerSurface_ = screenManager->GetProducerSurface(node.GetScreenId());
52 if (producerSurface_ == nullptr) {
53 RS_LOGE(
54 "RSVirtualScreenProcessor::Init for Screen(id %" PRIu64 "): ProducerSurface is null!", node.GetScreenId());
55 return false;
56 }
57
58 bool forceCPU = false;
59 renderFrame_ = renderEngine_->RequestFrame(producerSurface_, renderFrameConfig_, forceCPU, false);
60 if (renderFrame_ == nullptr) {
61 RS_LOGE("RSVirtualScreenProcessor::Init: renderFrame_ is null!");
62 return false;
63 }
64 canvas_ = renderFrame_->GetCanvas();
65 if (canvas_ == nullptr) {
66 return false;
67 }
68 canvas_->concat(screenTransformMatrix_);
69
70 return true;
71 }
72
PostProcess()73 void RSVirtualScreenProcessor::PostProcess()
74 {
75 if (producerSurface_ == nullptr) {
76 RS_LOGE("RSVirtualScreenProcessor::PostProcess surface is null!");
77 return;
78 }
79 auto surfaceId = producerSurface_->GetUniqueId();
80 renderEngine_->SetUiTimeStamp(renderFrame_, surfaceId);
81
82 if (renderFrame_ == nullptr) {
83 RS_LOGE("RSVirtualScreenProcessor::PostProcess renderFrame_ is null.");
84 return;
85 }
86 renderFrame_->Flush();
87 }
88
ProcessSurface(RSSurfaceRenderNode & node)89 void RSVirtualScreenProcessor::ProcessSurface(RSSurfaceRenderNode& node)
90 {
91 if (canvas_ == nullptr) {
92 RS_LOGE("RSVirtualScreenProcessor::ProcessSurface: Canvas is null!");
93 return;
94 }
95
96 std::string traceInfo;
97 AppendFormat(traceInfo, "RSVirtualScreenProcessor::ProcessSurface Node:%s ", node.GetName().c_str());
98 RS_TRACE_NAME(traceInfo);
99
100 // prepare BufferDrawParam
101 // in display's coordinate.
102 // clipHole: false.
103 // forceCPU: true.
104 auto params = RSDividedRenderUtil::CreateBufferDrawParam(node, false, false, false);
105 const float adaptiveDstWidth = params.dstRect.width() * mirrorAdaptiveCoefficient_;
106 const float adaptiveDstHeight = params.dstRect.height() * mirrorAdaptiveCoefficient_;
107 params.dstRect.setWH(adaptiveDstWidth, adaptiveDstHeight);
108 renderEngine_->DrawSurfaceNodeWithParams(*canvas_, node, params);
109 }
110
ProcessDisplaySurface(RSDisplayRenderNode & node)111 void RSVirtualScreenProcessor::ProcessDisplaySurface(RSDisplayRenderNode& node)
112 {
113 RS_LOGI("RSVirtualScreenProcessor::ProcessDisplaySurface() is not supported.");
114 }
115 } // namespace Rosen
116 } // namespace OHOS
117