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