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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_VIDEO_RENDER_TEXTURE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_VIDEO_RENDER_TEXTURE_H
18 
19 #include "core/components/common/layout/constants.h"
20 #include "core/components/video/texture_component.h"
21 #include "core/pipeline/base/render_node.h"
22 
23 namespace OHOS::Ace {
24 
25 class RenderTexture : public RenderNode {
26     DECLARE_ACE_TYPE(RenderTexture, RenderNode);
27 
28 public:
29     using HiddenChangeEvent = std::function<void(bool)>;
30     using TextureSizeChangeEvent = std::function<void(int64_t, int32_t, int32_t)>;
31     using TextureOffsetChangeEvent = std::function<void(int64_t, int32_t, int32_t)>;
32 
33     ~RenderTexture() override = default;
34 
35     static RefPtr<RenderNode> Create();
36 
37     void Update(const RefPtr<Component>& component) override;
38     void PerformLayout() override;
39     void SetHidden(bool hidden, bool inRecursion) override;
40 
IsRepaintBoundary()41     bool IsRepaintBoundary() const override
42     {
43         return true;
44     }
45 
SetHiddenChangeEvent(HiddenChangeEvent && hiddenChangeEvent)46     void SetHiddenChangeEvent(HiddenChangeEvent&& hiddenChangeEvent)
47     {
48         hiddenChangeEvent_ = std::move(hiddenChangeEvent);
49     }
50 
SetTextureSizeChange(TextureSizeChangeEvent && textureSizeChangeEvent)51     void SetTextureSizeChange(TextureSizeChangeEvent&& textureSizeChangeEvent)
52     {
53         textureSizeChangeEvent_ = std::move(textureSizeChangeEvent);
54     }
55 
SetTextureOffsetChange(TextureOffsetChangeEvent && textureOffsetChangeEvent)56     void SetTextureOffsetChange(TextureOffsetChangeEvent&& textureOffsetChangeEvent)
57     {
58         textureOffsetChangeEvent_ = std::move(textureOffsetChangeEvent);
59     }
60 
SupportOpacity()61     bool SupportOpacity() override
62     {
63         return true;
64     }
65 
GetFit()66     ImageFit GetFit() const
67     {
68         return imageFit_;
69     }
70 
71 #ifdef OHOS_STANDARD_SYSTEM
OnAppShow()72     void OnAppShow() override
73     {
74         LOGI("RenderTexture: Camera OnAppShow.");
75         RenderNode::OnAppShow();
76         if (hiddenChangeEvent_) {
77             hiddenChangeEvent_(false);
78         }
79     }
80 
OnAppHide()81     void OnAppHide() override
82     {
83         LOGI("RenderTexture: Camera OnAppHidden.");
84         RenderNode::OnAppHide();
85         if (hiddenChangeEvent_) {
86             hiddenChangeEvent_(true);
87         }
88     }
89 #endif
90 
SetIsComponentSize(bool isComponentSize)91     void SetIsComponentSize(bool isComponentSize)
92     {
93         isComponentSize_ = isComponentSize;
94     }
95 
IsComponentSize()96     bool IsComponentSize() const
97     {
98         return isComponentSize_;
99     }
100 
SetIsAddGaussianFuzzy(bool isAddGaussianFuzzy)101     virtual void SetIsAddGaussianFuzzy(bool isAddGaussianFuzzy)
102     {
103         isAddGaussianFuzzy_ = isAddGaussianFuzzy;
104     }
105 
IsAddGaussianFuzzy()106     bool IsAddGaussianFuzzy() const
107     {
108         return isAddGaussianFuzzy_;
109     }
110 
OnGlobalPositionChanged()111     void OnGlobalPositionChanged() override
112     {
113         if (textureOffsetChangeEvent_) {
114             textureOffsetChangeEvent_(textureId_,
115                 (int32_t)(alignmentX_ + GetGlobalOffset().GetX()), (int32_t)(alignmentY_ + GetGlobalOffset().GetY()));
116         }
117     }
118 
119 protected:
120     RenderTexture();
121     void Measure();
122 
123     int64_t textureId_ = INVALID_TEXTURE;
124     Size drawSize_;   // size of draw area
125     Size sourceSize_; // size of source
126 #if (defined OHOS_STANDARD_SYSTEM) && (!defined ENABLE_ROSEN_BACKEND)
127     ImageFit imageFit_ = ImageFit::FILL;
128 #else
129     ImageFit imageFit_ = ImageFit::CONTAIN;
130 #endif
131     ImageObjectPosition imagePosition_;
132     double alignmentX_ = 0.0;
133     double alignmentY_ = 0.0;
134     double controlsHeight_ = 0.0;
135 
136 private:
137     void CalculateFitContain();
138     void CalculateFitCover();
139     void CalculateFitFill();
140     void CalculateFitNone();
141     void CalculateFitScaleDown();
142     void ApplyObjectPosition();
143     void InitControlsHeight(const RefPtr<Component>& component);
144     HiddenChangeEvent hiddenChangeEvent_;
145     TextureSizeChangeEvent textureSizeChangeEvent_;
146     TextureOffsetChangeEvent textureOffsetChangeEvent_;
147     bool isComponentSize_ = false;
148     bool isAddGaussianFuzzy_ = false;
149 };
150 
151 } // namespace OHOS::Ace
152 
153 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_VIDEO_RENDER_TEXTURE_H
154