• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
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 #ifndef _EXYNOSHWCHELPER_H
17 #define _EXYNOSHWCHELPER_H
18 
19 #include <drm/drm_fourcc.h>
20 #include <hardware/hwcomposer2.h>
21 #include <utils/String8.h>
22 
23 #include <fstream>
24 #include <list>
25 #include <optional>
26 #include <sstream>
27 #include <string>
28 #include <vector>
29 
30 #include "DeconCommonHeader.h"
31 #include "VendorGraphicBuffer.h"
32 #include "VendorVideoAPI.h"
33 #include "exynos_format.h"
34 #include "exynos_sync.h"
35 #include "mali_gralloc_formats.h"
36 
37 #define MAX_FENCE_NAME 64
38 #define MAX_FENCE_THRESHOLD 500
39 #define MAX_FD_NUM      1024
40 
41 #define MAX_USE_FORMAT 27
42 #ifndef P010M_Y_SIZE
43 #define P010M_Y_SIZE(w,h) (__ALIGN_UP((w), 16) * 2 * __ALIGN_UP((h), 16) + 256)
44 #endif
45 #ifndef P010M_CBCR_SIZE
46 #define P010M_CBCR_SIZE(w,h) ((__ALIGN_UP((w), 16) * 2 * __ALIGN_UP((h), 16) / 2) + 256)
47 #endif
48 #ifndef P010_Y_SIZE
49 #define P010_Y_SIZE(w, h) ((w) * (h) * 2)
50 #endif
51 #ifndef P010_CBCR_SIZE
52 #define P010_CBCR_SIZE(w, h) ((w) * (h))
53 #endif
54 #ifndef DRM_FORMAT_YUV420_8BIT
55 #define DRM_FORMAT_YUV420_8BIT fourcc_code('Y', 'U', '0', '8')
56 #endif
57 #ifndef DRM_FORMAT_YUV420_10BIT
58 #define DRM_FORMAT_YUV420_10BIT fourcc_code('Y', 'U', '1', '0')
59 #endif
60 
61 static constexpr uint32_t DISPLAYID_MASK_LEN = 8;
62 
max(T a,T b)63 template<typename T> inline T max(T a, T b) { return (a > b) ? a : b; }
min(T a,T b)64 template<typename T> inline T min(T a, T b) { return (a < b) ? a : b; }
65 
66 class ExynosLayer;
67 class ExynosDisplay;
68 
69 using namespace android;
70 
71 static constexpr uint32_t TRANSFORM_MAT_SIZE = 4*4;
72 
73 enum {
74     EXYNOS_HWC_DIM_LAYER = 1 << 0,
75     EXYNOS_HWC_IGNORE_LAYER = 1 << 1,
76 };
77 
78 enum {
79     INTERFACE_TYPE_NONE = 0,
80     INTERFACE_TYPE_FB   = 1,
81     INTERFACE_TYPE_DRM  = 2,
82 };
83 
84 typedef enum format_type {
85     TYPE_UNDEF      = 0,
86 
87     /* format */
88     FORMAT_SHIFT    = 0,
89     FORMAT_MASK     = 0x00000fff,
90     RGB             = 0x00000001,
91     YUV420          = 0x00000002,
92     YUV422          = 0x00000004,
93     P010            = 0x00000008,
94 
95     /* bit */
96     BIT_SHIFT       = 12,
97     BIT_MASK        = 0x000ff000,
98     BIT8            = 0x00001000,
99     BIT10           = 0x00002000,
100     BIT8_2          = 0x00004000,
101 
102     /* compression */
103     /*
104      * COMP_ANY: Compression type doesn't affect any other
105      * descriptions of format (ex: drmFormat, bufferNum, bpp...)
106      * in format_description
107      */
108     COMP_SHIFT      = 20,
109     COMP_MASK       = 0x0ff00000,
110     COMP_ANY        = 0x08000000, /* the highest bit */
111     AFBC            = 0x00100000,
112     SBWC            = 0x00200000,
113     SBWC_LOSSY      = 0x00400000,
114 
115 } format_type_t;
116 
117 typedef struct format_description {
getFormatformat_description118     inline uint32_t getFormat() const { return type & FORMAT_MASK; }
getBitformat_description119     inline uint32_t getBit() const { return type & BIT_MASK; }
getCompressionformat_description120     inline uint32_t getCompression() const { return type & COMP_MASK; }
121     int halFormat;
122     decon_pixel_format s3cFormat;
123     int drmFormat;
124     uint32_t planeNum;
125     uint32_t bufferNum;
126     uint8_t bpp;
127     uint32_t type;
128     bool hasAlpha;
129     String8 name;
130     uint32_t reserved;
131 } format_description_t;
132 
133 constexpr int HAL_PIXEL_FORMAT_EXYNOS_UNDEFINED = 0;
134 constexpr int DRM_FORMAT_UNDEFINED = 0;
135 
136 // clang-format off
137 const format_description_t exynos_format_desc[] = {
138     /* RGB */
139     {HAL_PIXEL_FORMAT_RGBA_8888, DECON_PIXEL_FORMAT_RGBA_8888, DRM_FORMAT_RGBA8888,
140         1, 1, 32, RGB|BIT8|COMP_ANY, true, String8("RGBA_8888"), 0},
141     {HAL_PIXEL_FORMAT_RGBX_8888, DECON_PIXEL_FORMAT_RGBX_8888, DRM_FORMAT_RGBX8888,
142         1, 1, 32, RGB|BIT8|COMP_ANY, false, String8("RGBx_8888"), 0},
143     {HAL_PIXEL_FORMAT_RGB_888, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_RGB888,
144         1, 1, 24, RGB|BIT8|COMP_ANY, false, String8("RGB_888"), 0},
145     {HAL_PIXEL_FORMAT_RGB_565, DECON_PIXEL_FORMAT_RGB_565, DRM_FORMAT_BGR565,
146         1, 1, 16, RGB, false, String8("RGB_565"), 0},
147     {HAL_PIXEL_FORMAT_RGB_565, DECON_PIXEL_FORMAT_RGB_565, DRM_FORMAT_RGB565,
148         1, 1, 16, RGB|AFBC, false, String8("RGB_565_AFBC"), 0},
149     {HAL_PIXEL_FORMAT_BGRA_8888, DECON_PIXEL_FORMAT_BGRA_8888, DRM_FORMAT_BGRA8888,
150         1, 1, 32, RGB|BIT8|COMP_ANY, true, String8("BGRA_8888"), 0},
151     {HAL_PIXEL_FORMAT_RGBA_1010102, DECON_PIXEL_FORMAT_ABGR_2101010, DRM_FORMAT_RGBA1010102,
152         1, 1, 32, RGB|BIT10|COMP_ANY, true, String8("RGBA_1010102"), 0},
153     {HAL_PIXEL_FORMAT_EXYNOS_ARGB_8888, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_ARGB8888,
154         1, 1, 32, RGB|BIT8|COMP_ANY, true, String8("EXYNOS_ARGB_8888"), 0},
155 
156     /* YUV 420 */
157     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P_M, DECON_PIXEL_FORMAT_YUV420M, DRM_FORMAT_UNDEFINED,
158         3, 3, 12, YUV420|BIT8, false, String8("EXYNOS_YCbCr_420_P_M"), 0},
159     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M, DECON_PIXEL_FORMAT_NV12M, DRM_FORMAT_NV12,
160         2, 2, 12, YUV420|BIT8, false, String8("EXYNOS_YCbCr_420_SP_M"), 0},
161     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_TILED, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
162         2, 2, 12, YUV420|BIT8, false, String8("EXYNOS_YCbCr_420_SP_M_TILED"), 0},
163     {HAL_PIXEL_FORMAT_EXYNOS_YV12_M, DECON_PIXEL_FORMAT_YVU420M, DRM_FORMAT_UNDEFINED,
164         3, 3, 12, YUV420|BIT8, false, String8("EXYNOS_YV12_M"), 0},
165     {HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M, DECON_PIXEL_FORMAT_NV21M, DRM_FORMAT_NV21,
166         2, 2, 12, YUV420|BIT8, false, String8("EXYNOS_YCrCb_420_SP_M"), 0},
167     {HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_FULL, DECON_PIXEL_FORMAT_NV21M, DRM_FORMAT_NV21,
168         2, 2, 12, YUV420|BIT8, false, String8("EXYNOS_YCrCb_420_SP_M_FULL"), 0},
169     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
170         3, 1, 0, YUV420|BIT8, false, String8("EXYNOS_YCbCr_420_P"), 0},
171     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
172         2, 1, 0, YUV420|BIT8, false, String8("EXYNOS_YCbCr_420_SP"), 0},
173     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_PRIV, DECON_PIXEL_FORMAT_NV12M, DRM_FORMAT_NV12,
174         2, 2, 12, YUV420|BIT8, false, String8("EXYNOS_YCbCr_420_SP_M_PRIV"), 0},
175     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_PN, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
176         3, 1, 12, YUV420|BIT8, false, String8("EXYNOS_YCbCr_420_PN"), 0},
177     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN, DECON_PIXEL_FORMAT_NV12N, DRM_FORMAT_NV12,
178         2, 1, 12, YUV420|BIT8, false, String8("EXYNOS_YCbCr_420_SPN"), 0},
179     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_TILED, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
180         2, 1, 12, YUV420|BIT8, false, String8("EXYNOS_YCbCr_420_SPN_TILED"), 0},
181     {HAL_PIXEL_FORMAT_YCrCb_420_SP, DECON_PIXEL_FORMAT_NV21, DRM_FORMAT_NV21,
182         2, 1, 12, YUV420|BIT8, false, String8("YCrCb_420_SP"), 0},
183     {HAL_PIXEL_FORMAT_YV12, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
184         3, 1, 12, YUV420|BIT8, false, String8("YV12"), 0},
185     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B, DECON_PIXEL_FORMAT_NV12M_S10B, DRM_FORMAT_UNDEFINED,
186         2, 2, 12, YUV420|BIT10|BIT8_2, false, String8("EXYNOS_YCbCr_420_SP_M_S10B"), 0},
187     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B, DECON_PIXEL_FORMAT_NV12N_10B, DRM_FORMAT_UNDEFINED,
188         2, 1, 12, YUV420|BIT10|BIT8_2, false, String8("EXYNOS_YCbCr_420_SPN_S10B"), 0},
189     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_M, DECON_PIXEL_FORMAT_NV12M_P010, DRM_FORMAT_P010,
190         2, 2, 24, YUV420|BIT10|P010, false, String8("EXYNOS_YCbCr_P010_M"), 0},
191     {HAL_PIXEL_FORMAT_YCBCR_P010, DECON_PIXEL_FORMAT_NV12_P010, DRM_FORMAT_P010,
192         2, 1, 24, YUV420|BIT10|P010, false, String8("EXYNOS_YCbCr_P010"), 0},
193 
194     {HAL_PIXEL_FORMAT_GOOGLE_NV12_SP, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_NV12,
195         2, 1, 12, YUV420|BIT8, false, String8("GOOGLE_YCbCr_420_SP"), 0},
196     {HAL_PIXEL_FORMAT_GOOGLE_NV12_SP_10B, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_P010,
197         2, 1, 24, YUV420|BIT10, false, String8("GOOGLE_YCbCr_P010"), 0},
198     {MALI_GRALLOC_FORMAT_INTERNAL_YUV420_8BIT_I, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_YUV420_8BIT,
199         1, 1, 12, YUV420|BIT8|AFBC, false, String8("MALI_GRALLOC_FORMAT_INTERNAL_YUV420_8BIT_I"), 0},
200     {MALI_GRALLOC_FORMAT_INTERNAL_YUV420_10BIT_I, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_YUV420_10BIT,
201         1, 1, 15, YUV420|BIT10|AFBC, false, String8("MALI_GRALLOC_FORMAT_INTERNAL_YUV420_10BIT_I"), 0},
202 
203     /* YUV 422 */
204     {HAL_PIXEL_FORMAT_EXYNOS_CbYCrY_422_I, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
205         0, 0, 0, YUV422|BIT8, false, String8("EXYNOS_CbYCrY_422_I"), 0},
206     {HAL_PIXEL_FORMAT_EXYNOS_YCrCb_422_SP, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
207         0, 0, 0, YUV422|BIT8, false, String8("EXYNOS_YCrCb_422_SP"), 0},
208     {HAL_PIXEL_FORMAT_EXYNOS_YCrCb_422_I, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
209         0, 0, 0, YUV422|BIT8, false, String8("EXYNOS_YCrCb_422_I"), 0},
210     {HAL_PIXEL_FORMAT_EXYNOS_CrYCbY_422_I, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
211         0, 0, 0, YUV422|BIT8, false, String8("EXYNOS_CrYCbY_422_I"), 0},
212 
213     /* SBWC formats */
214     /* NV12, YCbCr, Multi */
215     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC, DECON_PIXEL_FORMAT_NV12M_SBWC_8B, DRM_FORMAT_NV12,
216         2, 2, 12, YUV420|BIT8|SBWC, false, String8("EXYNOS_YCbCr_420_SP_M_SBWC"), 0},
217     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L50, DECON_PIXEL_FORMAT_NV12M_SBWC_8B_L50, DRM_FORMAT_NV12,
218         2, 2, 12, YUV420|BIT8|SBWC_LOSSY, false, String8("EXYNOS_YCbCr_420_SP_M_SBWC_L50"), 0},
219     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC_L75, DECON_PIXEL_FORMAT_NV12M_SBWC_8B_L75, DRM_FORMAT_NV12,
220         2, 2, 12, YUV420|BIT8|SBWC_LOSSY, false, String8("EXYNOS_YCbCr_420_SP_M_SBWC_L75"), 0},
221     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC, DECON_PIXEL_FORMAT_NV12M_SBWC_10B, DRM_FORMAT_UNDEFINED,
222         2, 2, 12, YUV420|BIT10|SBWC, false, String8("EXYNOS_YCbCr_420_SP_M_10B_SBWC"), 0},
223     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L40, DECON_PIXEL_FORMAT_NV12M_SBWC_10B_L40, DRM_FORMAT_UNDEFINED,
224         2, 2, 12, YUV420|BIT10|SBWC_LOSSY, false, String8("EXYNOS_YCbCr_420_SP_M_10B_SBWC_L40"), 0},
225     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L60, DECON_PIXEL_FORMAT_NV12M_SBWC_10B_L60, DRM_FORMAT_UNDEFINED,
226         2, 2, 12, YUV420|BIT10|SBWC_LOSSY, false, String8("EXYNOS_YCbCr_420_SP_M_10B_SBWC_L60"), 0},
227     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC_L80, DECON_PIXEL_FORMAT_NV12M_SBWC_10B_L80, DRM_FORMAT_UNDEFINED,
228         2, 2, 12, YUV420|BIT10|SBWC_LOSSY, false, String8("EXYNOS_YCbCr_420_SP_M_10B_SBWC_L80"), 0},
229 
230     /* NV12, YCbCr, Single */
231     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC, DECON_PIXEL_FORMAT_NV12N_SBWC_8B, DRM_FORMAT_NV12,
232         2, 1, 12, YUV420|BIT8|SBWC, false, String8("EXYNOS_YCbCr_420_SPN_SBWC"), 0},
233     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L50, DECON_PIXEL_FORMAT_NV12N_SBWC_8B_L50, DRM_FORMAT_NV12,
234         2, 1, 12, YUV420|BIT8|SBWC_LOSSY, false, String8("EXYNOS_YCbCr_420_SPN_SBWC_L50"), 0},
235     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC_L75, DECON_PIXEL_FORMAT_NV12N_SBWC_8B_L75, DRM_FORMAT_NV12,
236         2, 1, 12, YUV420|BIT8|SBWC_LOSSY, false, String8("EXYNOS_YCbCr_420_SPN_SBWC_75"), 0},
237     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC, DECON_PIXEL_FORMAT_NV12N_SBWC_10B, DRM_FORMAT_UNDEFINED,
238         2, 1, 12, YUV420|BIT10|SBWC, false, String8("EXYNOS_YCbCr_420_SPN_10B_SBWC"), 0},
239     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L40, DECON_PIXEL_FORMAT_NV12N_SBWC_10B_L40, DRM_FORMAT_UNDEFINED,
240         2, 1, 12, YUV420|BIT10|SBWC_LOSSY, false, String8("EXYNOS_YCbCr_420_SPN_10B_SBWC_L40"), 0},
241     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L60, DECON_PIXEL_FORMAT_NV12N_SBWC_10B_L60, DRM_FORMAT_UNDEFINED,
242         2, 1, 12, YUV420|BIT10|SBWC_LOSSY, false, String8("EXYNOS_YCbCr_420_SPN_10B_SBWC_L60"), 0},
243     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC_L80, DECON_PIXEL_FORMAT_NV12N_SBWC_10B_L80, DRM_FORMAT_UNDEFINED,
244         2, 1, 12, YUV420|BIT10|SBWC_LOSSY, false, String8("EXYNOS_YCbCr_420_SPN_10B_SBWC_L80"), 0},
245 
246     /* NV12, YCrCb */
247     {HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_SBWC, DECON_PIXEL_FORMAT_NV21M_SBWC_8B, DRM_FORMAT_UNDEFINED,
248         2, 2, 12, YUV420|BIT8|SBWC, false, String8("EXYNOS_YCrCb_420_SP_M_SBWC"), 0},
249     {HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_10B_SBWC, DECON_PIXEL_FORMAT_NV21M_SBWC_10B, DRM_FORMAT_UNDEFINED,
250         2, 2, 12, YUV420|BIT10|SBWC, false, String8("EXYNOS_YCrbCb_420_SP_M_10B_SBWC"), 0},
251 
252     {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_UNDEFINED,
253         0, 0, 0, TYPE_UNDEF, false, String8("ImplDef"), 0},
254 
255     {HAL_PIXEL_FORMAT_GOOGLE_R_8, DECON_PIXEL_FORMAT_MAX, DRM_FORMAT_C8,
256         1, 1, 8, RGB|BIT8, true, String8("GOOGLE_R_8"), 0},
257 };
258 // clang-format on
259 
260 constexpr size_t FORMAT_MAX_CNT = sizeof(exynos_format_desc) / sizeof(format_description);
261 
262 enum HwcFdebugFenceType {
263     FENCE_TYPE_SRC_RELEASE = 1,
264     FENCE_TYPE_SRC_ACQUIRE = 2,
265     FENCE_TYPE_DST_RELEASE = 3,
266     FENCE_TYPE_DST_ACQUIRE = 4,
267     FENCE_TYPE_FREE_RELEASE = 5,
268     FENCE_TYPE_FREE_ACQUIRE = 6,
269     FENCE_TYPE_HW_STATE = 7,
270     FENCE_TYPE_RETIRE = 8,
271     FENCE_TYPE_READBACK_ACQUIRE = 9,
272     FENCE_TYPE_READBACK_RELEASE = 10,
273     FENCE_TYPE_ALL = 11,
274     FENCE_TYPE_UNDEFINED = 100
275 };
276 
277 enum HwcFdebugIpType {
278     FENCE_IP_DPP = 0,
279     FENCE_IP_MSC = 1,
280     FENCE_IP_G2D = 2,
281     FENCE_IP_FB = 3,
282     FENCE_IP_LAYER = 4,
283     FENCE_IP_ALL = 5,
284     FENCE_IP_UNDEFINED = 100
285 };
286 
287 enum HwcFenceType {
288     FENCE_LAYER_RELEASE_DPP = 0,
289     FENCE_LAYER_RELEASE_MPP = 1,
290     FENCE_LAYER_RELEASE_MSC = 2,
291     FENCE_LAYER_RELEASE_G2D = 3,
292     FENCE_DPP_HW_STATE = 4,
293     FENCE_MSC_HW_STATE = 5,
294     FENCE_G2D_HW_STATE = 6,
295     FENCE_MSC_SRC_LAYER = 7,
296     FENCE_G2D_SRC_LAYER = 8,
297     FENCE_MPP_DST_DPP = 9,
298     FENCE_MSC_DST_DPP = 10,
299     FENCE_G2D_DST_DPP = 11,
300     FENCE_DPP_SRC_MPP = 12,
301     FENCE_DPP_SRC_MSC = 13,
302     FENCE_DPP_SRC_G2D = 14,
303     FENCE_DPP_SRC_LAYER = 15,
304     FENCE_MPP_FREE_BUF_ACQUIRE = 16,
305     FENCE_MPP_FREE_BUF_RELEASE = 17,
306     FENCE_RETIRE = 18,
307     FENCE_MAX
308 };
309 
310 enum {
311     EXYNOS_ERROR_NONE       = 0,
312     EXYNOS_ERROR_CHANGED    = 1
313 };
314 
315 enum {
316     eSkipLayer                    =     0x00000001,
317     eInvalidHandle                =     0x00000002,
318     eHasFloatSrcCrop              =     0x00000004,
319     eUpdateExynosComposition      =     0x00000008,
320     eDynamicRecomposition         =     0x00000010,
321     eForceFbEnabled               =     0x00000020,
322     eSandwitchedBetweenGLES       =     0x00000040,
323     eSandwitchedBetweenEXYNOS     =     0x00000080,
324     eInsufficientWindow           =     0x00000100,
325     eInsufficientMPP              =     0x00000200,
326     eSkipStaticLayer              =     0x00000400,
327     eUnSupportedUseCase           =     0x00000800,
328     eDimLayer                     =     0x00001000,
329     eResourcePendingWork          =     0x00002000,
330     eSkipRotateAnim               =     0x00004000,
331     eUnSupportedColorTransform    =     0x00008000,
332     eLowFpsLayer                  =     0x00010000,
333     eReallocOnGoingForDDI         =     0x00020000,
334     eInvalidDispFrame             =     0x00040000,
335     eExceedMaxLayerNum            =     0x00080000,
336     eExceedSdrDimRatio            =     0x00100000,
337     eResourceAssignFail           =     0x20000000,
338     eMPPUnsupported               =     0x40000000,
339     eUnknown                      =     0x80000000,
340 };
341 
342 enum regionType {
343     eTransparentRegion          =       0,
344     eCoveredOpaqueRegion        =       1,
345     eDamageRegionByDamage       =       2,
346     eDamageRegionByLayer        =       3,
347 };
348 
349 enum {
350     eDamageRegionFull = 0,
351     eDamageRegionPartial,
352     eDamageRegionSkip,
353     eDamageRegionError,
354 };
355 
356 /*
357  * bufferHandle can be NULL if it is not allocated yet
358  * or size or format information can be different between other field values and
359  * member of bufferHandle. This means bufferHandle should be reallocated.
360  * */
361 typedef struct exynos_image {
362     uint32_t fullWidth = 0;
363     uint32_t fullHeight = 0;
364     uint32_t x = 0;
365     uint32_t y = 0;
366     uint32_t w = 0;
367     uint32_t h = 0;
368     uint32_t format= 0;
369     uint64_t usageFlags = 0;
370     uint32_t layerFlags = 0;
371     int acquireFenceFd = -1;
372     int releaseFenceFd = -1;
373     buffer_handle_t bufferHandle = NULL;
374     android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
375     uint32_t blending = 0;
376     uint32_t transform = 0;
377     uint32_t compressed = 0;
378     float planeAlpha = 0;
379     uint32_t zOrder = 0;
380     /* refer
381      * frameworks/native/include/media/hardware/VideoAPI.h
382      * frameworks/native/include/media/hardware/HardwareAPI.h */
383     bool hasMetaParcel = false;
384     ExynosVideoMeta metaParcel;
385     ExynosVideoInfoType metaType = VIDEO_INFO_TYPE_INVALID;
386     bool needColorTransform = false;
387 
isDimLayerexynos_image388     bool isDimLayer()
389     {
390         if (layerFlags & EXYNOS_HWC_DIM_LAYER)
391             return true;
392         return false;
393     };
394 } exynos_image_t;
395 
396 uint32_t getHWC1CompType(int32_t /*hwc2_composition_t*/ type);
397 
398 uint32_t getDrmMode(uint64_t flags);
399 uint32_t getDrmMode(const buffer_handle_t handle);
400 
WIDTH(const hwc_rect & rect)401 inline int WIDTH(const hwc_rect &rect) { return rect.right - rect.left; }
HEIGHT(const hwc_rect & rect)402 inline int HEIGHT(const hwc_rect &rect) { return rect.bottom - rect.top; }
WIDTH(const hwc_frect_t & rect)403 inline int WIDTH(const hwc_frect_t &rect) { return (int)(rect.right - rect.left); }
HEIGHT(const hwc_frect_t & rect)404 inline int HEIGHT(const hwc_frect_t &rect) { return (int)(rect.bottom - rect.top); }
405 
406 const format_description_t *halFormatToExynosFormat(int format, uint32_t compressType);
407 
408 uint32_t halDataSpaceToV4L2ColorSpace(android_dataspace data_space);
409 enum decon_pixel_format halFormatToDpuFormat(int format, uint32_t compressType);
410 uint32_t DpuFormatToHalFormat(int format, uint32_t compressType);
411 int halFormatToDrmFormat(int format, uint32_t compressType);
412 int32_t drmFormatToHalFormats(int format, std::vector<uint32_t> *halFormats);
413 int drmFormatToHalFormat(int format);
414 uint8_t formatToBpp(int format);
415 uint8_t DpuFormatToBpp(decon_pixel_format format);
416 uint64_t halTransformToDrmRot(uint32_t halTransform);
417 uint32_t getCompressionType(const buffer_handle_t handle);
418 
419 bool isFormatRgb(int format);
420 bool isFormatYUV(int format);
421 bool isFormatYUV420(int format);
422 bool isFormatYUV422(int format);
423 bool isFormatYCrCb(int format);
424 bool isFormatYUV8_2(int format);
425 bool isFormat10BitYUV420(int format);
426 bool isFormatLossy(int format);
427 bool isFormatSBWC(int format);
428 bool isFormatP010(int format);
429 bool formatHasAlphaChannel(int format);
430 unsigned int isNarrowRgb(int format, android_dataspace data_space);
431 bool isAFBCCompressed(const buffer_handle_t handle);
432 bool isSrcCropFloat(hwc_frect &frect);
433 bool isScaled(exynos_image &src, exynos_image &dst);
434 bool isScaledDown(exynos_image &src, exynos_image &dst);
435 bool hasHdrInfo(const exynos_image &img);
436 bool hasHdrInfo(android_dataspace dataSpace);
437 bool hasHdr10Plus(exynos_image &img);
438 
439 void dumpExynosImage(uint32_t type, exynos_image &img);
440 void dumpExynosImage(String8& result, exynos_image &img);
441 void dumpHandle(uint32_t type, buffer_handle_t h);
442 void printExynosLayer(const ExynosLayer *layer);
443 String8 getFormatStr(int format, uint32_t compressType);
444 String8 getMPPStr(int typeId);
445 void adjustRect(hwc_rect_t &rect, int32_t width, int32_t height);
446 uint32_t getBufferNumOfFormat(int format, uint32_t compressType);
447 uint32_t getPlaneNumOfFormat(int format, uint32_t compressType);
448 uint32_t getBytePerPixelOfPrimaryPlane(int format);
449 
450 int fence_close(int fence, ExynosDisplay *display, HwcFdebugFenceType type, HwcFdebugIpType ip);
451 bool fence_valid(int fence);
452 
453 int hwcFdClose(int fd);
454 int hwc_dup(int fd, ExynosDisplay *display, HwcFdebugFenceType type, HwcFdebugIpType ip,
455             bool pendingAllowed = false);
456 int hwc_print_stack();
457 
expand(const hwc_rect & r1,const hwc_rect & r2)458 inline hwc_rect expand(const hwc_rect &r1, const hwc_rect &r2)
459 {
460     hwc_rect i;
461     i.top = min(r1.top, r2.top);
462     i.bottom = max(r1.bottom, r2.bottom);
463     i.left = min(r1.left, r2.left);
464     i.right = max(r1.right, r2.right);
465     return i;
466 }
467 
468 template <typename T>
pixel_align_down(const T x,const uint32_t a)469 inline T pixel_align_down(const T x, const uint32_t a) {
470     static_assert(std::numeric_limits<T>::is_integer,
471                   "Integer type is expected as the alignment input");
472     return a ? (x / a) * a : x;
473 }
474 
475 template <typename T>
pixel_align(const T x,const uint32_t a)476 inline T pixel_align(const T x, const uint32_t a) {
477     static_assert(std::numeric_limits<T>::is_integer,
478                   "Integer type is expected as the alignment input");
479     return a ? ((x + a - 1) / a) * a : x;
480 }
481 
482 uint32_t getExynosBufferYLength(uint32_t width, uint32_t height, int format);
483 int getBufLength(buffer_handle_t handle, uint32_t planer_num, size_t *length, int format, uint32_t width, uint32_t height);
484 
485 enum class HwcFenceDirection {
486     FROM = 0,
487     TO,
488     DUP,
489     CLOSE,
490     UPDATE,
491 };
492 
493 struct HwcFenceTrace {
494     HwcFenceDirection direction = HwcFenceDirection::FROM;
495     HwcFdebugFenceType type = FENCE_TYPE_UNDEFINED;
496     HwcFdebugIpType ip = FENCE_IP_UNDEFINED;
497     struct timeval time = {0, 0};
498 };
499 
500 struct HwcFenceInfo {
501     uint32_t displayId = HWC_DISPLAY_PRIMARY;
502     int32_t usage = 0;
503     int32_t dupFrom = -1;
504     bool pendingAllowed = false;
505     bool leaking = false;
506     std::list<HwcFenceTrace> traces = {};
507 };
508 
509 class funcReturnCallback {
510     public:
funcReturnCallback(const std::function<void (void)> cb)511         funcReturnCallback(const std::function<void(void)> cb) : mCb(cb) {}
~funcReturnCallback()512         ~funcReturnCallback() { mCb(); }
513     private:
514         const std::function<void(void)> mCb;
515 };
516 
517 String8 getLocalTimeStr(struct timeval tv);
518 
519 void setFenceName(int fenceFd, HwcFenceType fenceType);
520 void setFenceInfo(uint32_t fd, ExynosDisplay *display, HwcFdebugFenceType type, HwcFdebugIpType ip,
521                   HwcFenceDirection direction, bool pendingAllowed = false, int32_t dupFrom = -1);
522 void printLastFenceInfo(uint32_t fd, ExynosDisplay *display);
523 void dumpFenceInfo(ExynosDisplay *display, int32_t count);
524 bool fenceWarn(ExynosDisplay *display, uint32_t threshold);
525 void printLeakFds(ExynosDisplay *display);
526 bool validateFencePerFrame(ExynosDisplay *display);
527 android_dataspace colorModeToDataspace(android_color_mode_t mode);
528 bool hasPPC(uint32_t physicalType, uint32_t formatIndex, uint32_t rotIndex);
529 
530 class TableBuilder {
531 public:
532     template <typename T>
add(const std::string & key,const T & value)533     TableBuilder& add(const std::string& key, const T& value) {
534         std::stringstream v;
535         v << value;
536         data.emplace_back(std::make_pair(key, v.str()));
537         return *this;
538     }
539 
540     template <typename T>
add(const std::string & key,const std::vector<T> & values)541     TableBuilder& add(const std::string& key, const std::vector<T>& values) {
542         std::stringstream value;
543         for (int i = 0; i < values.size(); i++) {
544             if (i) value << ", ";
545             value << values[i];
546         }
547 
548         data.emplace_back(std::make_pair(key, value.str()));
549         return *this;
550     }
551 
552     // Template overrides for hex integers
553     TableBuilder& add(const std::string& key, const uint64_t& value, bool toHex);
554     TableBuilder& add(const std::string& key, const std::vector<uint64_t>& values, bool toHex);
555 
556     std::string build();
557 
558 private:
559     std::string buildPaddedString(const std::string& str, int size);
560 
561     using StringPairVec = std::vector<std::pair<std::string, std::string>>;
562     StringPairVec data;
563 };
564 
565 void writeFileNode(FILE *fd, int value);
566 int32_t writeIntToFile(const char *file, uint32_t value);
getDisplayId(int32_t displayType,int32_t displayIndex)567 constexpr uint32_t getDisplayId(int32_t displayType, int32_t displayIndex) {
568     return (displayType << DISPLAYID_MASK_LEN) | displayIndex;
569 }
570 
571 int32_t load_png_image(const char *filepath, buffer_handle_t buffer);
572 int readLineFromFile(const std::string &filename, std::string &out, char delim);
573 
574 template <typename T>
575 struct CtrlValue {
576 public:
CtrlValueCtrlValue577     CtrlValue() : value_(), dirty_(false) {}
CtrlValueCtrlValue578     CtrlValue(const T& value) : value_(value), dirty_(false) {}
579 
storeCtrlValue580     void store(T value) {
581         if (value == value_) return;
582         dirty_ = true;
583         value_ = value;
584     };
getCtrlValue585     const T &get() { return value_; };
is_dirtyCtrlValue586     bool is_dirty() { return dirty_; };
clear_dirtyCtrlValue587     void clear_dirty() { dirty_ = false; };
set_dirtyCtrlValue588     void set_dirty() { dirty_ = true; };
resetCtrlValue589     void reset(T value) { value_ = value; dirty_ = false; }
590 private:
591     T value_;
592     bool dirty_;
593 };
594 
595 template <typename T>
toUnderlying(T v)596 constexpr typename std::underlying_type<T>::type toUnderlying(T v) {
597     return static_cast<typename std::underlying_type<T>::type>(v);
598 }
599 
600 template <size_t bufferSize>
601 struct RollingAverage {
602     std::array<int64_t, bufferSize> buffer{0};
603     int64_t total = 0;
604     int64_t average;
605     size_t elems = 0;
606     size_t buffer_index = 0;
insertRollingAverage607     void insert(int64_t newTime) {
608         total += newTime - buffer[buffer_index];
609         buffer[buffer_index] = newTime;
610         buffer_index = (buffer_index + 1) % bufferSize;
611         elems = std::min(elems + 1, bufferSize);
612         average = total / elems;
613     }
614 };
615 
616 // Waits for a given property value, or returns std::nullopt if unavailable
617 std::optional<std::string> waitForPropertyValue(const std::string &property, int64_t timeoutMs);
618 
619 uint32_t rectSize(const hwc_rect_t &rect);
620 void assign(decon_win_rect &win_rect, uint32_t left, uint32_t right, uint32_t width,
621             uint32_t height);
622 #endif
623