1 /*
2  * Copyright (c) 2021 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_TEXTURE_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_VIDEO_TEXTURE_COMPONENT_H
18 
19 #include <string>
20 
21 #include "base/utils/utils.h"
22 #include "core/components/common/layout/constants.h"
23 #include "core/pipeline/base/element.h"
24 #include "core/pipeline/base/render_component.h"
25 #include "frameworks/core/components/common/properties/decoration.h"
26 
27 namespace OHOS::Ace {
28 
29 inline constexpr int64_t INVALID_TEXTURE = -1;
30 inline constexpr double IMAGE_POSITION_DEFAULT_SIZE = 50.0;
31 
32 // A component can show Texture.
33 class TextureComponent : public RenderComponent {
34     DECLARE_ACE_TYPE(TextureComponent, RenderComponent);
35 
36 public:
TextureComponent()37     TextureComponent()
38     {
39         imagePosition_.SetSizeValueX(IMAGE_POSITION_DEFAULT_SIZE);
40         imagePosition_.SetSizeValueY(IMAGE_POSITION_DEFAULT_SIZE);
41         imagePosition_.SetSizeTypeX(BackgroundImagePositionType::PERCENT);
42         imagePosition_.SetSizeTypeY(BackgroundImagePositionType::PERCENT);
43     }
44     ~TextureComponent() override = default;
45 
46     RefPtr<RenderNode> CreateRenderNode() override;
47     RefPtr<Element> CreateElement() override;
48 
GetTextureId()49     int64_t GetTextureId() const
50     {
51         return textureId_;
52     }
53 
SetTextureId(int64_t textureId)54     void SetTextureId(int64_t textureId)
55     {
56         textureId_ = textureId;
57     }
58 
GetSrcWidth()59     uint32_t GetSrcWidth() const
60     {
61         return srcWidth_;
62     }
63 
GetSrcHeight()64     uint32_t GetSrcHeight() const
65     {
66         return srcHeight_;
67     }
68 
SetSrcWidth(uint32_t width)69     void SetSrcWidth(uint32_t width)
70     {
71         srcWidth_ = width;
72     }
73 
SetSrcHeight(uint32_t height)74     void SetSrcHeight(uint32_t height)
75     {
76         srcHeight_ = height;
77     }
78 
SetFit(ImageFit fit)79     void SetFit(ImageFit fit)
80     {
81         imageFit_ = fit;
82     }
83 
GetFit()84     ImageFit GetFit() const
85     {
86         return imageFit_;
87     }
88 
GetChild()89     RefPtr<Component> GetChild() const
90     {
91         return child_;
92     }
93 
SetChild(const RefPtr<Component> & child)94     void SetChild(const RefPtr<Component>& child)
95     {
96         child_ = child;
97     }
98 
SetImagePosition(ImageObjectPosition imagePosition)99     void SetImagePosition(ImageObjectPosition imagePosition)
100     {
101         imagePosition_ = imagePosition;
102     }
103 
GetImagePosition()104     ImageObjectPosition GetImagePosition() const
105     {
106         return imagePosition_;
107     }
108 
109 private:
110     int64_t textureId_ = INVALID_TEXTURE;
111     uint32_t srcWidth_ = 0;
112     uint32_t srcHeight_ = 0;
113     ImageFit imageFit_ { ImageFit::CONTAIN };
114     ImageObjectPosition imagePosition_;
115 
116     RefPtr<Component> child_;
117 };
118 
119 } // namespace OHOS::Ace
120 
121 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_VIDEO_TEXTURE_COMPONENT_H
122