1 /*
2 * Copyright (c) 2023 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 #include "core/components_ng/pattern/web/web_layout_algorithm.h"
17
18 #include "core/components_ng/base/frame_node.h"
19
20 #if !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM)
21 #include "core/components_ng/pattern/web/web_pattern.h"
22 #else
23 #include "core/components_ng/pattern/web/cross_platform/web_pattern.h"
24 #endif
25 #include "core/components_ng/property/measure_utils.h"
26 #include "core/pipeline_ng/pipeline_context.h"
27
28 constexpr int32_t MAX_TEXTURE_SIZE = 500000;
29 constexpr int32_t MAX_SURFACE_SIZE = 8000;
30
31 namespace OHOS::Ace::NG {
32 WebLayoutAlgorithm::WebLayoutAlgorithm() = default;
33
Measure(LayoutWrapper * layoutWrapper)34 void WebLayoutAlgorithm::Measure(LayoutWrapper* layoutWrapper)
35 {
36 CHECK_NULL_VOID(layoutWrapper);
37 auto host = layoutWrapper->GetHostNode();
38 CHECK_NULL_VOID(host);
39 auto pattern = DynamicCast<WebPattern>(host->GetPattern());
40 CHECK_NULL_VOID(pattern);
41 auto geometryNode = layoutWrapper->GetGeometryNode();
42 CHECK_NULL_VOID(geometryNode);
43 BoxLayoutAlgorithm::Measure(layoutWrapper);
44 int frameWidth = geometryNode->GetFrameSize().Width();
45 int rootLayerHeight = pattern->GetRootLayerHeight();
46 auto renderMode = pattern->GetRenderMode();
47 if (pattern->GetLayoutMode() == WebLayoutMode::FIT_CONTENT && IsValidRootLayer(frameWidth, renderMode) &&
48 IsValidRootLayer(rootLayerHeight, renderMode)) {
49 auto drawSize = SizeF(frameWidth, rootLayerHeight);
50 TAG_LOGD(AceLogTag::ACE_WEB, "WebLayoutAlgorithm::Measure,drawSize : %{public}s", drawSize.ToString().c_str());
51 layoutWrapper->GetGeometryNode()->SetFrameSize(drawSize);
52 }
53 }
54
IsValidRootLayer(int32_t x,RenderMode renderMode)55 bool WebLayoutAlgorithm::IsValidRootLayer(int32_t x, RenderMode renderMode)
56 {
57 int32_t maxSize = 0;
58 if (renderMode == RenderMode::SYNC_RENDER) {
59 maxSize = MAX_TEXTURE_SIZE;
60 } else {
61 maxSize = MAX_SURFACE_SIZE;
62 }
63 return x > 0 && x <= maxSize;
64 }
65 } // namespace OHOS::Ace::NG
66