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