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