OSDN Git Service

4142a045151b6f98567d81e24b9567f6e483de9b
[android-x86/external-IA-Hardware-Composer.git] / public / hwcutils.h
1 /*
2 // Copyright (c) 2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #ifndef COMMON_UTILS_HWCUTILS_H_
18 #define COMMON_UTILS_HWCUTILS_H_
19
20 #include <hwcdefs.h>
21 #include <sstream>
22
23 namespace hwcomposer {
24
25 // Helper functions.
26
27 // Call poll() on fd.
28 //  - timeout: time in miliseconds to stay blocked before returning if fd
29 //  is not ready.
30 int HWCPoll(int fd, int timeout);
31
32 // Reset's rect to include region hwc_region.
33 void ResetRectToRegion(const HwcRegion& hwc_region, HwcRect<int>& rect);
34
35 // Calculate new_rect to include target_rect. This is used to calculate
36 // Display Frame and Surface Damage rect of layers.
37 void CalculateRect(const HwcRect<int>& target_rect, HwcRect<int>& new_rect);
38
39 // Calculate new_rect to include target_rect. This is used to calculate
40 // Source Rect of layers.
41 void CalculateSourceRect(const HwcRect<float>& target_rect,
42                          HwcRect<float>& new_rect);
43
44 // Returns true if format is a Media format.
45 bool IsSupportedMediaFormat(uint32_t format);
46
47 // Returns total planes for a given format.
48 uint32_t GetTotalPlanesForFormat(uint32_t format);
49
50 template <class T>
51 inline bool IsOverlapping(T l1, T t1, T r1, T b1, T l2, T t2, T r2, T b2)
52 // Do two rectangles overlap?
53 // Top-left is inclusive; bottom-right is exclusive
54 {
55   return ((l1 < r2 && r1 > l2) && (t1 < b2 && b1 > t2));
56 }
57
58 inline bool IsOverlapping(const hwcomposer::HwcRect<int>& rect1,
59                           const hwcomposer::HwcRect<int>& rect2) {
60   return IsOverlapping(rect1.left, rect1.top, rect1.right, rect1.bottom,
61                        rect2.left, rect2.top, rect2.right, rect2.bottom);
62 }
63
64 template <class T>
65 inline bool IsEnclosedBy(T l1, T t1, T r1, T b1, T l2, T t2, T r2, T b2)
66 // Do two rectangles overlap?
67 // Top-left is inclusive; bottom-right is exclusive
68 {
69   return ((l1 >= l2 && t1 >= t2) && (r1 <= r2 && b1 <= b2));
70 }
71
72 inline bool IsEnclosedBy(const hwcomposer::HwcRect<int>& rect1,
73                          const hwcomposer::HwcRect<int>& rect2) {
74   return IsEnclosedBy(rect1.left, rect1.top, rect1.right, rect1.bottom,
75                       rect2.left, rect2.top, rect2.right, rect2.bottom);
76 }
77
78 enum OverlapType { kEnclosed = 0, kOverlapping, kOutside };
79
80 inline OverlapType AnalyseOverlap(const hwcomposer::HwcRect<int>& rect,
81                                   const hwcomposer::HwcRect<int>& bounds) {
82   if (IsEnclosedBy(rect, bounds)) {
83     return kEnclosed;
84   } else if (IsOverlapping(rect, bounds)) {
85     return kOverlapping;
86   } else {
87     return kOutside;
88   }
89 }
90
91 /**
92  * Pretty-print HwcRect for debugging.
93  */
94 std::string StringifyRect(HwcRect<int> rect);
95
96 /**
97  * Pretty-print HwcRegion for debugging.
98  */
99 std::string StringifyRegion(HwcRegion region);
100
101 }  // namespace hwcomposer
102
103 #endif  // COMMON_UTILS_HWCUTILS_H_