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 
17 #ifndef _EXYNOSLAYER_H
18 #define _EXYNOSLAYER_H
19 
20 #include <hardware/hwcomposer2.h>
21 #include <log/log.h>
22 #include <system/graphics.h>
23 #include <utils/Timers.h>
24 
25 #include <array>
26 #include <unordered_map>
27 
28 #include "ExynosDisplay.h"
29 #include "ExynosHWC.h"
30 #include "ExynosHWCHelper.h"
31 #include "VendorGraphicBuffer.h"
32 #include "VendorVideoAPI.h"
33 
34 #ifndef HWC2_HDR10_PLUS_SEI
35 /* based on android.hardware.composer.2_3 */
36 #define HWC2_HDR10_PLUS_SEI 12
37 #endif
38 
39 using namespace android;
40 using namespace vendor::graphics;
41 
42 class ExynosMPP;
43 
44 enum overlay_priority {
45     ePriorityNone,
46     /* Normal layer */
47     ePriorityLow,
48     /* Assign resource before normal layers */
49     ePriorityMid,
50     /*
51      * Overlay is better than client composition,
52      * Displayed screen can be abnormal if the layer is composited by client
53      */
54     ePriorityHigh,
55     /*
56      * Overlay is mandatory,
57      * Black screen will be displayed if the layer is composited by client
58      */
59     ePriorityMax
60 };
61 
62 typedef struct pre_processed_layer_info
63 {
64     bool preProcessed;
65     hwc_frect_t sourceCrop;
66     hwc_rect_t displayFrame;
67     int interlacedType;
68     /* SBWC exception */
69     bool mUsePrivateFormat = false;
70     uint32_t mPrivateFormat = 0;
71 } pre_processed_layer_info_t;
72 
73 enum {
74     /*add after hwc2_composition_t, margin number here*/
75     HWC2_COMPOSITION_EXYNOS = 32,
76 };
77 
78 class ExynosLayer : public ExynosMPPSource {
79     public:
80 
81         ExynosLayer(ExynosDisplay* display);
82         virtual ~ExynosLayer();
83 
84         ExynosDisplay* mDisplay;
85 
86         /**
87          * Layer's compositionType
88          */
89         int32_t mCompositionType;
90 
91         /**
92          * Composition type that is used by HAL
93          * (ex: COMPOSITION_G2D)
94          */
95         int32_t mExynosCompositionType;
96 
97         /**
98          * Validated compositionType
99          */
100         int32_t mValidateCompositionType;
101 
102         /**
103          * Validated ExynosCompositionType
104          */
105         int32_t mValidateExynosCompositionType;
106 
107         uint32_t mOverlayInfo;
108 
109         /**
110          * Layer supported information for each MPP type (bit setting)
111          * (= Restriction check, out format will be set as RGB for temporary
112          * If mGeometryChanged is true, It will be rearranged in ExynosDisplay::validateDisplay()
113          * This infor will be used for supported infomation at resource arranging time
114          */
115         uint32_t mSupportedMPPFlag;
116 
117         /**
118          * TODO : Should be defined..
119          */
120         /* Key is logical type of MPP */
121         std::unordered_map<uint32_t, uint64_t> mCheckMPPFlag;
122 
123         /**
124          * Update rate for using client composition.
125          */
126         uint32_t mFps;
127 
128         /**
129          * Assign priority, when priority changing is needded by order infomation in mGeometryChanged
130          */
131         overlay_priority mOverlayPriority;
132 
133         /**
134          * This will be set when property changed except buffer update case.
135          */
136         uint64_t mGeometryChanged;
137 
138         /**
139          * Layer's window index
140          */
141         uint32_t mWindowIndex;
142 
143         /**
144          * Source buffer's compression information
145          */
146         bool mCompressed;
147 
148         /**
149          * Acquire fence
150          */
151         int32_t mAcquireFence;
152         int32_t mPrevAcquireFence;
153 
154         /**
155          * Release fence
156          */
157         int32_t mReleaseFence;
158 
159         uint32_t mFrameCount;
160         uint32_t mLastFrameCount;
161         nsecs_t mLastFpsTime;
162 
163         /**
164          * Previous buffer's handle
165          */
166         buffer_handle_t mLastLayerBuffer;
167 
168         /**
169          * Display buffer handle
170          */
171         buffer_handle_t mLayerBuffer;
172 
173         /**
174          * Surface Damage
175          */
176         size_t mDamageNum;
177         android::Vector <hwc_rect_t> mDamageRects;
178 
179         /**
180          * Blending type
181          */
182         int32_t mBlending; /* hwc2_blend_mode_t */
183 
184         /**
185          * Display Frame
186          */
187         hwc_rect_t mDisplayFrame;
188 
189         /**
190          * Pland alpha
191          */
192         float mPlaneAlpha;
193 
194         /**
195          * Source Crop
196          */
197         hwc_frect_t mSourceCrop;
198 
199         /**
200          * Transform
201          */
202         int32_t mTransform; /*hwc_transform_t*/
203 
204         /**
205          * Visible region
206          */
207         hwc_region_t mVisibleRegionScreen;
208 
209         /**
210          * Z-Order
211          */
212         uint32_t mZOrder;
213 
214         /**
215          * Color
216          */
217         hwc_color_t mColor;
218 
219         /** Data Space
220          */
221         android_dataspace mDataSpace; // android_dataspace_t
222 
223         pre_processed_layer_info mPreprocessedInfo;
224 
225         /**
226          * user defined flag
227          */
228         int32_t mLayerFlag;
229 
230         /**
231          * HDR flags
232          */
233         bool mIsHdrLayer;
234         bool mBufferHasMetaParcel;
235         int mMetaParcelFd;
236 
237         /**
238          * color transform info
239          */
240         struct LayerColorTransform {
241             bool enable = false;
242             std::array<float, TRANSFORM_MAT_SIZE> mat;
243         } mLayerColorTransform;
244 
245         /**
246          * @param type
247          */
248         int32_t setCompositionType(int32_t type);
249 
250         uint32_t checkFps();
251 
252         uint32_t getFps();
253 
254         int32_t doPreProcess();
255 
256         /* setCursorPosition(..., x, y)
257          * Descriptor: HWC2_FUNCTION_SET_CURSOR_POSITION
258          * HWC2_PFN_SET_CURSOR_POSITION
259          */
260         virtual int32_t setCursorPosition(int32_t x, int32_t y);
261 
262         /* setLayerBuffer(..., buffer, acquireFence)
263          * Descriptor: HWC2_FUNCTION_SET_LAYER_BUFFER
264          * HWC2_PFN_SET_LAYER_BUFFER
265          */
266         virtual int32_t setLayerBuffer(buffer_handle_t buffer, int32_t acquireFence);
267 
268         /* setLayerSurfaceDamage(..., damage)
269          * Descriptor: HWC2_FUNCTION_SET_LAYER_SURFACE_DAMAGE
270          * HWC2_PFN_SET_LAYER_SURFACE_DAMAGE
271          */
272         virtual int32_t setLayerSurfaceDamage(hwc_region_t damage);
273 
274         /*
275          * Layer State Functions
276          *
277          * These functions modify the state of a given layer. They do not take effect
278          * until the display configuration is successfully validated with
279          * validateDisplay and the display contents are presented with presentDisplay.
280          *
281          * All of these functions take as their first three parameters a device pointer,
282          * a display handle for the display which contains the layer, and a layer
283          * handle, so these parameters are omitted from the described parameter lists.
284          */
285 
286         /* setLayerBlendMode(..., mode)
287          * Descriptor: HWC2_FUNCTION_SET_LAYER_BLEND_MODE
288          * HWC2_PFN_SET_LAYER_BLEND_MODE
289          */
290         virtual int32_t setLayerBlendMode(int32_t /*hwc2_blend_mode_t*/ mode);
291 
292         /* setLayerColor(..., color)
293          * Descriptor: HWC2_FUNCTION_SET_LAYER_COLOR
294          * HWC2_PFN_SET_LAYER_COLOR
295          */
296         virtual int32_t setLayerColor(hwc_color_t color);
297 
298         /* setLayerCompositionType(..., type)
299          * Descriptor: HWC2_FUNCTION_SET_LAYER_COMPOSITION_TYPE
300          * HWC2_PFN_SET_LAYER_COMPOSITION_TYPE
301          */
302         virtual int32_t setLayerCompositionType(
303                 int32_t /*hwc2_composition_t*/ type);
304 
305         /* setLayerDataspace(..., dataspace)
306          * Descriptor: HWC2_FUNCTION_SET_LAYER_DATASPACE
307          * HWC2_PFN_SET_LAYER_DATASPACE
308          */
309         virtual int32_t setLayerDataspace(int32_t /*android_dataspace_t*/ dataspace);
310 
311         /* setLayerDisplayFrame(..., frame)
312          * Descriptor: HWC2_FUNCTION_SET_LAYER_DISPLAY_FRAME
313          * HWC2_PFN_SET_LAYER_DISPLAY_FRAME
314          */
315         virtual int32_t setLayerDisplayFrame(hwc_rect_t frame);
316 
317         /* setLayerPlaneAlpha(..., alpha)
318          * Descriptor: HWC2_FUNCTION_SET_LAYER_PLANE_ALPHA
319          * HWC2_PFN_SET_LAYER_PLANE_ALPHA
320          */
321         virtual int32_t setLayerPlaneAlpha(float alpha);
322 
323         /* setLayerSidebandStream(..., stream)
324          * Descriptor: HWC2_FUNCTION_SET_LAYER_SIDEBAND_STREAM
325          * HWC2_PFN_SET_LAYER_SIDEBAND_STREAM
326          */
327         virtual int32_t setLayerSidebandStream(const native_handle_t* stream);
328 
329         /* setLayerSourceCrop(..., crop)
330          * Descriptor: HWC2_FUNCTION_SET_LAYER_SOURCE_CROP
331          * HWC2_PFN_SET_LAYER_SOURCE_CROP
332          */
333         virtual int32_t setLayerSourceCrop(hwc_frect_t crop);
334 
335         /* setLayerTransform(..., transform)
336          * Descriptor: HWC2_FUNCTION_SET_LAYER_TRANSFORM
337          * HWC2_PFN_SET_LAYER_TRANSFORM
338          */
339         virtual int32_t setLayerTransform(int32_t /*hwc_transform_t*/ transform);
340 
341         /* setLayerVisibleRegion(..., visible)
342          * Descriptor: HWC2_FUNCTION_SET_LAYER_VISIBLE_REGION
343          * HWC2_PFN_SET_LAYER_VISIBLE_REGION
344          */
345         virtual int32_t setLayerVisibleRegion(hwc_region_t visible);
346 
347         /* setLayerZOrder(..., z)
348          * Descriptor: HWC2_FUNCTION_SET_LAYER_Z_ORDER
349          * HWC2_PFN_SET_LAYER_Z_ORDER
350          */
351         virtual int32_t setLayerZOrder(uint32_t z);
352 
353         virtual int32_t setLayerPerFrameMetadata(uint32_t numElements,
354                 const int32_t* /*hw2_per_frame_metadata_key_t*/ keys, const float* metadata);
355 
356         /* setLayerPerFrameMetadataBlobs(...,numElements, keys, sizes, blobs)
357          * Descriptor: HWC2_FUNCTION_SET_LAYER_PER_FRAME_METADATA_BLOBS
358          * Parameters:
359          *   numElements is the number of elements in each of the keys, sizes, and
360          *   metadata arrays
361          *   keys is a pointer to an array of keys.  Current valid keys are those listed
362          *   above as valid blob type keys.
363          *   sizes is a pointer to an array of unsigned ints specifying the sizes of
364          *   each metadata blob
365          *   metadata is a pointer to a blob of data holding all blobs contiguously in
366          *   memory
367          *
368          *   Returns HWC2_ERROR_NONE or one of the following erros:
369          *     HWC2_ERROR_BAD_DISPLAY - an invalid display handle was passed in
370          *     HWC2_ERROR_BAD_PARAMETER - sizes of keys and metadata parameters does
371          *     not match numElements, numElements < 0, or keys contains a
372          *     non-valid key (see above for current valid blob type keys).
373          *     HWC2_ERROR_UNSUPPORTED - metadata is not supported on this display
374          */
375         int32_t setLayerPerFrameMetadataBlobs(uint32_t numElements, const int32_t* keys, const uint32_t* sizes,
376                 const uint8_t* metadata);
377 
378         int32_t setLayerColorTransform(const float* matrix);
379         /* setLayerGenericMetadata(..., keyLength, key, mandatory, valueLength, value)
380          * Descriptor: HWC2_FUNCTION_SET_LAYER_GENERIC_METADATA
381          * Optional for HWC2 devices for composer 2.4+
382          *
383          * setLayerGenericMetadata sets a piece of generic metadata for the given layer.
384          * If this function is called twice with the same key but different values, the
385          * newer value must override the older one. Calling this function with
386          * valueLength == 0 must reset that key's metadata as if it had not been set.
387          *
388          * A given piece of metadata may either be mandatory or a hint (non-mandatory)
389          * as indicated by the `mandatory` parameter. Mandatory metadata may affect the
390          * composition result, which is to say that it may cause a visible change in the
391          * final image. By contrast, hints may only affect the composition strategy,
392          * such as which layers are composited by the client, but must not cause a
393          * visible change in the final image.
394          *
395          * This implies that if the device does not understand a given key:
396          * - If the key is marked as mandatory, it must mark this layer for client
397          *   composition in order to ensure the correct composition result
398          * - If the key is a hint, the metadata provided may be ignored
399          *
400          * Parameters:
401          *   keyLength - the length of the key parameter
402          *   key - the metadata key
403          *   mandatory - indicates whether this particular key represents mandatory
404          *       metadata or a hint, as described above
405          *   valueLength - the length of the value parameter
406          *   value - the metadata value
407          *
408          * Returns HWC2_ERROR_NONE or one of the following errors:
409          *   HWC2_ERROR_BAD_DISPLAY - an invalid display handle was passed in
410          *   HWC2_ERROR_BAD_LAYER - an invalid layer handle was passed in
411          *   HWC2_ERROR_BAD_PARAMETER - an unsupported key was passed in, or the value
412          *       does not conform to the expected format for the key
413          */
414         int32_t setLayerGenericMetadata(hwc2_layer_t __unused layer,
415                 uint32_t __unused keyLength, const char* __unused key,
416                 bool __unused mandatory, uint32_t __unused valueLength, const uint8_t* __unused value);
417 
418         void resetValidateData();
419         virtual void dump(String8& result);
420         void printLayer();
421         int32_t setSrcExynosImage(exynos_image *src_img);
422         int32_t setDstExynosImage(exynos_image *dst_img);
423         int32_t resetAssignedResource();
424         bool checkDownscaleCap(uint32_t btsRefreshRate);
425 
426         void setSrcAcquireFence();
427 
isDrm()428         bool isDrm() {return ((mLayerBuffer != NULL) && (getDrmMode(mLayerBuffer) != NO_DRM));};
isLayerFormatRgb()429         bool isLayerFormatRgb() {
430             return ((mLayerBuffer != NULL) &&
431                     isFormatRgb(VendorGraphicBufferMeta::get_internal_format(mLayerBuffer)));
432         }
isLayerFormatYuv()433         bool isLayerFormatYuv() {
434             return ((mLayerBuffer != NULL) &&
435                     isFormatYUV(VendorGraphicBufferMeta::get_internal_format(mLayerBuffer)));
436         }
getDisplayFrameArea()437         size_t getDisplayFrameArea() { return HEIGHT(mDisplayFrame) * WIDTH(mDisplayFrame); }
438         void setGeometryChanged(uint64_t changedBit);
clearGeometryChanged()439         void clearGeometryChanged() {mGeometryChanged = 0;};
440         bool isDimLayer();
getMetaParcel()441         const ExynosVideoMeta* getMetaParcel() { return mMetaParcel; };
442 
443     private:
444         ExynosVideoMeta *mMetaParcel;
445         int allocMetaParcel();
446 };
447 
448 #endif //_EXYNOSLAYER_H
449