• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef RENDER_SERVICE_BASE_CORE_COMMON_RS_RECTANGLES_MERGER_H
16 #define RENDER_SERVICE_BASE_CORE_COMMON_RS_RECTANGLES_MERGER_H
17 
18 #include <algorithm>
19 #include <utility>
20 #include "common/rs_occlusion_region.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 
25 class RSB_EXPORT RectsMerger {
26 public:
27     RectsMerger() = default;
28     ~RectsMerger() = default;
29     // set parameters
RectsMerger(int thresholdMergingAll,int thresholdMergingByLevel)30     RectsMerger(int thresholdMergingAll, int thresholdMergingByLevel)
31     {
32         thresholdMergingAll_ = std::max(thresholdMergingAll, 1);
33         thresholdMergingByLevel_ = std::max(thresholdMergingByLevel, 1);
34     }
35 
36     std::vector<Occlusion::Rect> MergeAllRects(const std::vector<Occlusion::Rect>& rectangles,
37         int expectedOutputNum, int maxTolerableCost);
38 
39 private:
40     Occlusion::Rect MergeSomeRects(const std::vector<Occlusion::Rect>& rectangles,
41         int idx1, int idx2);
42 
43     // return the rectangle with minimal area, which encloses two rectangles
44     Occlusion::Rect MergeTwoRects(const Occlusion::Rect& a, const Occlusion::Rect& b);
45 
46 
47     // check if one rectangles is enclosed by another rectangle
48     bool IsEnclosedBy(const Occlusion::Rect& a, const Occlusion::Rect& b) const;
49 
50     // calculate the explicit rectangles
51     // sort by area, and then check
52     void CalcExplicitRects(std::vector<Occlusion::Rect> rectangles);
53 
54     // all rectangles are merged into one
55     std::vector<Occlusion::Rect> NaiveMerging(const std::vector<Occlusion::Rect>& rects);
56 
57     // sort the rectangles by height, equally divide them into some groups and then merge
58     std::vector<Occlusion::Rect> MergeRectsByLevel(int inputNumOfRects, int outputNumOfRects);
59     // two thresholds for different cases
60     int thresholdMergingAll_;
61     int thresholdMergingByLevel_;
62     std::vector<Occlusion::Rect> explicitRects;
63 };
64 
65 } // namespace Rosen
66 } // namespace OHOS
67 #endif //RENDER_SERVICE_BASE_CORE_COMMON_RS_RECTANGLES_MERGER_H