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 "rs_base_render_util.h"
24 #include "screen_manager/rs_screen_manager.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 
29 using FallbackCallback = std::function<void(const sptr<Surface>& surface, const std::vector<LayerInfoPtr>& layers)>;
30 class RSComposerAdapter {
31 public:
32     RSComposerAdapter() = default;
33     ~RSComposerAdapter() noexcept = default;
34 
35     // noncopyable
36     RSComposerAdapter(const RSComposerAdapter&) = delete;
37     void operator=(const RSComposerAdapter&) = delete;
38 
39     bool Init(const ScreenInfo& screenInfo, int32_t offsetX, int32_t offsetY, float mirrorAdaptiveCoefficient,
40         const FallbackCallback& cb);
41 
42     LayerInfoPtr CreateLayer(RSSurfaceRenderNode& node) const;
43     LayerInfoPtr CreateLayer(RSDisplayRenderNode& node) const;
44     void CommitLayers(const std::vector<LayerInfoPtr>& layers);
45     /* only used for mock tests */
46     void SetHdiBackendDevice(HdiDevice* device);
47     void SetColorFilterMode(ColorFilterMode colorFilterMode);
48 
49 private:
50     // check if the node is out of the screen region.
51     bool IsOutOfScreenRegion(const ComposeInfo& info) const;
52     LayerInfoPtr CreateBufferLayer(RSSurfaceRenderNode& node) const;
53     LayerInfoPtr CreateTunnelLayer(RSSurfaceRenderNode& node) const;
54     ComposeInfo BuildComposeInfo(RSSurfaceRenderNode& node, bool isTunnelCheck = false) const;
55     ComposeInfo BuildComposeInfo(RSDisplayRenderNode& node) const;
56     void SetComposeInfoToLayer(
57         const LayerInfoPtr& layer,
58         const ComposeInfo& info,
59         const sptr<IConsumerSurface>& surface,
60         RSBaseRenderNode* node) const;
61     void SetMetaDataInfoToLayer(const LayerInfoPtr& layer, const ComposeInfo& info,
62                                 const sptr<IConsumerSurface>& surface) const;
63     void DealWithNodeGravity(const RSSurfaceRenderNode& node, ComposeInfo& info) const;
64     void DumpLayersToFile(const std::vector<LayerInfoPtr>& layers);
65 
66     void LayerRotate(const LayerInfoPtr& layer, RSBaseRenderNode& node) const;
67     void LayerCrop(const LayerInfoPtr& layer) const;
68     static void LayerScaleDown(const LayerInfoPtr& layer);
69     static void LayerPresentTimestamp(const LayerInfoPtr& layer, const sptr<IConsumerSurface>& surface);
70 
71     void OnPrepareComplete(sptr<Surface>& surface, const PrepareCompleteParam& param, void* data);
72     static void GetComposerInfoSrcRect(ComposeInfo& info, const RSSurfaceRenderNode& node);
73     bool GetComposerInfoNeedClient(const ComposeInfo& info, RSSurfaceRenderNode& node) const;
74     bool CheckStatusBeforeCreateLayer(RSSurfaceRenderNode& node, bool isTunnelCheck = false) const;
75     HdiBackend *hdiBackend_ = nullptr;
76     std::shared_ptr<HdiOutput> output_;
77     ScreenInfo screenInfo_;
78     ColorFilterMode colorFilterMode_ = ColorFilterMode::COLOR_FILTER_END;
79 
80     // The offset on dst screen for all layers.
81     int32_t offsetX_ = 0;
82     int32_t offsetY_ = 0;
83 
84     float mirrorAdaptiveCoefficient_ = 1.0f;
85     FallbackCallback fallbackCb_;
86 };
87 } // namespace Rosen
88 } // namespace OHOS
89 #endif // RS_CORE_PIPELINE_RS_COMPOSER_ADAPTER_H
90