1 /*
2 * Copyright (c) 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 #ifndef RS_CORE_PIPELINE_RS_COMPOSER_ADAPTER_H
17 #define RS_CORE_PIPELINE_RS_COMPOSER_ADAPTER_H
18
19 #include "hdi_backend.h"
20
21 #include "pipeline/rs_display_render_node.h"
22 #include "pipeline/rs_surface_render_node.h"
23 #include "screen_manager/rs_screen_manager.h"
24
25 namespace OHOS {
26 namespace Rosen {
27 struct ComposeInfo {
28 GraphicIRect srcRect;
29 GraphicIRect dstRect;
30 GraphicIRect boundRect;
31 GraphicIRect visibleRect;
32 GraphicMatrix matrix;
33 int32_t gravity;
34 int32_t zOrder { 0 };
35 GraphicLayerAlpha alpha;
36 sptr<SurfaceBuffer> buffer;
37 sptr<SurfaceBuffer> preBuffer;
38 sptr<SyncFence> fence = SyncFence::INVALID_FENCE;
39 GraphicBlendType blendType;
40 bool needClient;
41 };
42
RotateEnumToInt(ScreenRotation rotation)43 static inline int RotateEnumToInt(ScreenRotation rotation)
44 {
45 static const std::map<ScreenRotation, int> screenRotationEnumToIntMap = {
46 {ScreenRotation::ROTATION_0, 0}, {ScreenRotation::ROTATION_90, 90},
47 {ScreenRotation::ROTATION_180, 180}, {ScreenRotation::ROTATION_270, 270}};
48 auto iter = screenRotationEnumToIntMap.find(rotation);
49 return iter != screenRotationEnumToIntMap.end() ? iter->second : 0;
50 }
51
52 using FallbackCallback = std::function<void(const sptr<Surface>& surface, const std::vector<LayerInfoPtr>& layers)>;
53 class RSComposerAdapter {
54 public:
55 RSComposerAdapter() = default;
56 ~RSComposerAdapter() noexcept = default;
57
58 // noncopyable
59 RSComposerAdapter(const RSComposerAdapter&) = delete;
60 void operator=(const RSComposerAdapter&) = delete;
61
62 bool Init(const ScreenInfo& screenInfo, int32_t offsetX, int32_t offsetY, float mirrorAdaptiveCoefficient,
63 const FallbackCallback& cb);
64
65 LayerInfoPtr CreateLayer(RSSurfaceRenderNode& node) const;
66 LayerInfoPtr CreateLayer(RSDisplayRenderNode& node) const;
67 void CommitLayers(const std::vector<LayerInfoPtr>& layers);
68 /* only used for mock tests */
69 void SetHdiBackendDevice(HdiDevice* device);
70
71 private:
72 // check if the node is out of the screen region.
73 bool IsOutOfScreenRegion(const ComposeInfo& info) const;
74 LayerInfoPtr CreateBufferLayer(RSSurfaceRenderNode& node) const;
75 LayerInfoPtr CreateTunnelLayer(RSSurfaceRenderNode& node) const;
76 ComposeInfo BuildComposeInfo(RSSurfaceRenderNode& node, bool isTunnelCheck = false) const;
77 ComposeInfo BuildComposeInfo(RSDisplayRenderNode& node) const;
78 void SetComposeInfoToLayer(
79 const LayerInfoPtr& layer,
80 const ComposeInfo& info,
81 const sptr<IConsumerSurface>& surface,
82 RSBaseRenderNode* node) const;
83 void SetMetaDataInfoToLayer(const LayerInfoPtr& layer, const ComposeInfo& info,
84 const sptr<IConsumerSurface>& surface) const;
85 void DealWithNodeGravity(const RSSurfaceRenderNode& node, ComposeInfo& info) const;
86 void DumpLayersToFile(const std::vector<LayerInfoPtr>& layers);
87
88 void LayerRotate(const LayerInfoPtr& layer, RSBaseRenderNode& node) const;
89 void LayerCrop(const LayerInfoPtr& layer) const;
90 static void LayerScaleDown(const LayerInfoPtr& layer);
91 static void LayerPresentTimestamp(const LayerInfoPtr& layer, const sptr<IConsumerSurface>& surface);
92
93 void OnPrepareComplete(sptr<Surface>& surface, const PrepareCompleteParam& param, void* data);
94 static void GetComposerInfoSrcRect(ComposeInfo &info, const RSSurfaceRenderNode& node);
95 bool GetComposerInfoNeedClient(const ComposeInfo &info, RSSurfaceRenderNode& node) const;
96 bool CheckStatusBeforeCreateLayer(RSSurfaceRenderNode& node, bool isTunnelCheck = false) const;
97 HdiBackend *hdiBackend_ = nullptr;
98 std::shared_ptr<HdiOutput> output_;
99 ScreenInfo screenInfo_;
100
101 // The offset on dst screen for all layers.
102 int32_t offsetX_ = 0;
103 int32_t offsetY_ = 0;
104
105 float mirrorAdaptiveCoefficient_ = 1.0f;
106 FallbackCallback fallbackCb_;
107 };
108 } // namespace Rosen
109 } // namespace OHOS
110 #endif // RS_CORE_PIPELINE_RS_COMPOSER_ADAPTER_H
111