1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * Copyright (C)2012-2014, The Linux Foundation. All rights reserved. 4 * 5 * Not a Contribution, Apache license notifications and license are retained 6 * for attribution purposes only. 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 #ifndef HWC_UTILS_H 22 #define HWC_UTILS_H 23 24 #define HWC_REMOVE_DEPRECATED_VERSIONS 1 25 #include <fcntl.h> 26 #include <math.h> 27 #include <hardware/hwcomposer.h> 28 #include <gr.h> 29 #include <gralloc_priv.h> 30 #include <utils/String8.h> 31 #include "qdMetaData.h" 32 #include <overlayUtils.h> 33 #include <EGL/egl.h> 34 #include <QService.h> 35 36 37 #define ALIGN_TO(x, align) (((x) + ((align)-1)) & ~((align)-1)) 38 #define LIKELY( exp ) (__builtin_expect( (exp) != 0, true )) 39 #define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false )) 40 #define MAX_NUM_APP_LAYERS 32 41 #define MIN_DISPLAY_XRES 200 42 #define MIN_DISPLAY_YRES 200 43 #define HWC_WFDDISPSYNC_LOG 0 44 #define STR(f) #f; 45 46 //Fwrd decls 47 struct hwc_context_t; 48 49 namespace ovutils = overlay::utils; 50 51 namespace overlay { 52 class Overlay; 53 class Rotator; 54 class RotMgr; 55 } 56 57 namespace qhwc { 58 //fwrd decl 59 class QueuedBufferStore; 60 class ExternalDisplay; 61 class VirtualDisplay; 62 class IFBUpdate; 63 class IVideoOverlay; 64 class MDPComp; 65 class CopyBit; 66 class HwcDebug; 67 class AssertiveDisplay; 68 class HWCVirtualBase; 69 70 71 struct MDPInfo { 72 int version; 73 char panel; 74 bool hasOverlay; 75 }; 76 77 struct DisplayAttributes { 78 uint32_t vsync_period; //nanos 79 uint32_t xres; 80 uint32_t yres; 81 uint32_t stride; 82 float xdpi; 83 float ydpi; 84 int fd; 85 bool connected; //Applies only to pluggable disp. 86 //Connected does not mean it ready to use. 87 //It should be active also. (UNBLANKED) 88 bool isActive; 89 // In pause state, composition is bypassed 90 // used for WFD displays only 91 bool isPause; 92 // To trigger padding round to clean up mdp 93 // pipes 94 bool isConfiguring; 95 // External Display is in MDP Downscale mode indicator 96 bool mDownScaleMode; 97 // Ext dst Rect 98 hwc_rect_t mDstRect; 99 //Action safe attributes 100 // Flag to indicate the presence of action safe dimensions for external 101 bool mActionSafePresent; 102 int mAsWidthRatio; 103 int mAsHeightRatio; 104 105 //If property fbsize set via adb shell debug.hwc.fbsize = XRESxYRES 106 //following fields are used. 107 bool customFBSize; 108 uint32_t xres_orig; 109 uint32_t yres_orig; 110 111 }; 112 113 struct ListStats { 114 int numAppLayers; //Total - 1, excluding FB layer. 115 int skipCount; 116 int fbLayerIndex; //Always last for now. = numAppLayers 117 //Video specific 118 int yuvCount; 119 int yuvIndices[MAX_NUM_APP_LAYERS]; 120 int extOnlyLayerIndex; 121 bool preMultipliedAlpha; 122 int yuv4k2kIndices[MAX_NUM_APP_LAYERS]; 123 int yuv4k2kCount; 124 // Notifies hwcomposer about the start and end of animation 125 // This will be set to true during animation, otherwise false. 126 bool isDisplayAnimating; 127 bool secureUI; // Secure display layer 128 bool isSecurePresent; 129 hwc_rect_t lRoi; //left ROI 130 hwc_rect_t rRoi; //right ROI. Unused in single DSI panels. 131 }; 132 133 struct LayerProp { 134 uint32_t mFlags; //qcom specific layer flags 135 LayerProp():mFlags(0){}; 136 }; 137 138 struct VsyncState { 139 bool enable; 140 bool fakevsync; 141 }; 142 143 struct BwcPM { 144 static void setBwc(const hwc_rect_t& crop, 145 const hwc_rect_t& dst, const int& transform, 146 ovutils::eMdpFlags& mdpFlags); 147 }; 148 149 // LayerProp::flag values 150 enum { 151 HWC_MDPCOMP = 0x00000001, 152 HWC_COPYBIT = 0x00000002, 153 }; 154 155 // HAL specific features 156 enum { 157 HWC_COLOR_FILL = 0x00000008, 158 HWC_FORMAT_RB_SWAP = 0x00000040, 159 }; 160 161 /* External Display states */ 162 enum { 163 EXTERNAL_OFFLINE = 0, 164 EXTERNAL_ONLINE, 165 EXTERNAL_PAUSE, 166 EXTERNAL_RESUME, 167 EXTERNAL_MAXSTATES 168 }; 169 170 class LayerRotMap { 171 public: 172 LayerRotMap() { reset(); } 173 enum { MAX_SESS = 3 }; 174 void add(hwc_layer_1_t* layer, overlay::Rotator *rot); 175 //Resets the mapping of layer to rotator 176 void reset(); 177 //Clears mappings and existing rotator fences 178 //Intended to be used during errors 179 void clear(); 180 uint32_t getCount() const; 181 hwc_layer_1_t* getLayer(uint32_t index) const; 182 overlay::Rotator* getRot(uint32_t index) const; 183 void setReleaseFd(const int& fence); 184 private: 185 hwc_layer_1_t* mLayer[MAX_SESS]; 186 overlay::Rotator* mRot[MAX_SESS]; 187 uint32_t mCount; 188 }; 189 190 inline uint32_t LayerRotMap::getCount() const { 191 return mCount; 192 } 193 194 inline hwc_layer_1_t* LayerRotMap::getLayer(uint32_t index) const { 195 if(index >= mCount) return NULL; 196 return mLayer[index]; 197 } 198 199 inline overlay::Rotator* LayerRotMap::getRot(uint32_t index) const { 200 if(index >= mCount) return NULL; 201 return mRot[index]; 202 } 203 204 inline hwc_rect_t integerizeSourceCrop(const hwc_frect_t& cropF) { 205 hwc_rect_t cropI = {0,0,0,0}; 206 cropI.left = int(ceilf(cropF.left)); 207 cropI.top = int(ceilf(cropF.top)); 208 cropI.right = int(floorf(cropF.right)); 209 cropI.bottom = int(floorf(cropF.bottom)); 210 return cropI; 211 } 212 213 inline bool isNonIntegralSourceCrop(const hwc_frect_t& cropF) { 214 if(cropF.left - roundf(cropF.left) || 215 cropF.top - roundf(cropF.top) || 216 cropF.right - roundf(cropF.right) || 217 cropF.bottom - roundf(cropF.bottom)) 218 return true; 219 else 220 return false; 221 } 222 223 // ----------------------------------------------------------------------------- 224 // Utility functions - implemented in hwc_utils.cpp 225 void dumpLayer(hwc_layer_1_t const* l); 226 227 // Calculate viewframe for external/primary display from primary resolution and 228 // primary device orientation 229 hwc_rect_t calculateDisplayViewFrame(hwc_context_t *ctx, int dpy); 230 231 void setListStats(hwc_context_t *ctx, hwc_display_contents_1_t *list, 232 int dpy); 233 void initContext(hwc_context_t *ctx); 234 void closeContext(hwc_context_t *ctx); 235 //Crops source buffer against destination and FB boundaries 236 void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst, 237 const hwc_rect_t& scissor, int orient); 238 void getNonWormholeRegion(hwc_display_contents_1_t* list, 239 hwc_rect_t& nwr); 240 bool isSecuring(hwc_context_t* ctx, hwc_layer_1_t const* layer); 241 bool isSecureModePolicy(int mdpVersion); 242 bool isExternalActive(hwc_context_t* ctx); 243 bool isAlphaScaled(hwc_layer_1_t const* layer); 244 bool needsScaling(hwc_layer_1_t const* layer); 245 bool isDownscaleRequired(hwc_layer_1_t const* layer); 246 bool needsScalingWithSplit(hwc_context_t* ctx, hwc_layer_1_t const* layer, 247 const int& dpy); 248 void sanitizeSourceCrop(hwc_rect_t& cropL, hwc_rect_t& cropR, 249 private_handle_t *hnd); 250 bool isAlphaPresent(hwc_layer_1_t const* layer); 251 int hwc_vsync_control(hwc_context_t* ctx, int dpy, int enable); 252 int getBlending(int blending); 253 bool isGLESOnlyComp(hwc_context_t *ctx, const int& dpy); 254 void reset_layer_prop(hwc_context_t* ctx, int dpy, int numAppLayers); 255 256 bool canUseMDPforVirtualDisplay(hwc_context_t* ctx, 257 const hwc_display_contents_1_t *list); 258 259 //Helper function to dump logs 260 void dumpsys_log(android::String8& buf, const char* fmt, ...); 261 262 int getExtOrientation(hwc_context_t* ctx); 263 bool isValidRect(const hwc_rect_t& rect); 264 hwc_rect_t deductRect(const hwc_rect_t& rect1, const hwc_rect_t& rect2); 265 bool isSameRect(const hwc_rect& rect1, const hwc_rect& rect2); 266 hwc_rect_t moveRect(const hwc_rect_t& rect, const int& x_off, const int& y_off); 267 hwc_rect_t getIntersection(const hwc_rect_t& rect1, const hwc_rect_t& rect2); 268 hwc_rect_t getUnion(const hwc_rect_t& rect1, const hwc_rect_t& rect2); 269 void optimizeLayerRects(const hwc_display_contents_1_t *list); 270 bool areLayersIntersecting(const hwc_layer_1_t* layer1, 271 const hwc_layer_1_t* layer2); 272 bool layerUpdating(const hwc_layer_1_t* layer); 273 274 275 // returns true if Action safe dimensions are set and target supports Actionsafe 276 bool isActionSafePresent(hwc_context_t *ctx, int dpy); 277 278 /* Calculates the destination position based on the action safe rectangle */ 279 void getActionSafePosition(hwc_context_t *ctx, int dpy, hwc_rect_t& dst); 280 281 void getAspectRatioPosition(int destWidth, int destHeight, int srcWidth, 282 int srcHeight, hwc_rect_t& rect); 283 284 void getAspectRatioPosition(hwc_context_t* ctx, int dpy, int extOrientation, 285 hwc_rect_t& inRect, hwc_rect_t& outRect); 286 287 bool isPrimaryPortrait(hwc_context_t *ctx); 288 289 bool isOrientationPortrait(hwc_context_t *ctx); 290 291 void calcExtDisplayPosition(hwc_context_t *ctx, 292 private_handle_t *hnd, 293 int dpy, 294 hwc_rect_t& sourceCrop, 295 hwc_rect_t& displayFrame, 296 int& transform, 297 ovutils::eTransform& orient); 298 299 // Returns the orientation that needs to be set on external for 300 // BufferMirrirMode(Sidesync) 301 int getMirrorModeOrientation(hwc_context_t *ctx); 302 303 /* Get External State names */ 304 const char* getExternalDisplayState(uint32_t external_state); 305 306 // Resets display ROI to full panel resoluion 307 void resetROI(hwc_context_t *ctx, const int dpy); 308 309 // Aligns updating ROI to panel restrictions 310 hwc_rect_t getSanitizeROI(struct hwc_rect roi, hwc_rect boundary); 311 312 // Handles wfd Pause and resume events 313 void handle_pause(hwc_context_t *ctx, int dpy); 314 void handle_resume(hwc_context_t *ctx, int dpy); 315 316 //Close acquireFenceFds of all layers of incoming list 317 void closeAcquireFds(hwc_display_contents_1_t* list); 318 319 //Sync point impl. 320 int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy, 321 int fd); 322 323 //Sets appropriate mdp flags for a layer. 324 void setMdpFlags(hwc_layer_1_t *layer, 325 ovutils::eMdpFlags &mdpFlags, 326 int rotDownscale, int transform); 327 328 int configRotator(overlay::Rotator *rot, ovutils::Whf& whf, 329 hwc_rect_t& crop, const ovutils::eMdpFlags& mdpFlags, 330 const ovutils::eTransform& orient, const int& downscale); 331 332 int configMdp(overlay::Overlay *ov, const ovutils::PipeArgs& parg, 333 const ovutils::eTransform& orient, const hwc_rect_t& crop, 334 const hwc_rect_t& pos, const MetaData_t *metadata, 335 const ovutils::eDest& dest); 336 337 int configColorLayer(hwc_context_t *ctx, hwc_layer_1_t *layer, const int& dpy, 338 ovutils::eMdpFlags& mdpFlags, ovutils::eZorder& z, 339 ovutils::eIsFg& isFg, const ovutils::eDest& dest); 340 341 void updateSource(ovutils::eTransform& orient, ovutils::Whf& whf, 342 hwc_rect_t& crop); 343 344 //Routine to configure low resolution panels (<= 2048 width) 345 int configureNonSplit(hwc_context_t *ctx, hwc_layer_1_t *layer, const int& dpy, 346 ovutils::eMdpFlags& mdpFlags, ovutils::eZorder& z, 347 ovutils::eIsFg& isFg, const ovutils::eDest& dest, 348 overlay::Rotator **rot); 349 350 //Routine to configure high resolution panels (> 2048 width) 351 int configureSplit(hwc_context_t *ctx, hwc_layer_1_t *layer, const int& dpy, 352 ovutils::eMdpFlags& mdpFlags, ovutils::eZorder& z, 353 ovutils::eIsFg& isFg, const ovutils::eDest& lDest, 354 const ovutils::eDest& rDest, overlay::Rotator **rot); 355 356 //Routine to split and configure high resolution YUV layer (> 2048 width) 357 int configureSourceSplit(hwc_context_t *ctx, hwc_layer_1_t *layer, 358 const int& dpy, 359 ovutils::eMdpFlags& mdpFlags, ovutils::eZorder& z, 360 ovutils::eIsFg& isFg, const ovutils::eDest& lDest, 361 const ovutils::eDest& rDest, overlay::Rotator **rot); 362 363 //On certain targets DMA pipes are used for rotation and they won't be available 364 //for line operations. On a per-target basis we can restrict certain use cases 365 //from using rotator, since we know before-hand that such scenarios can lead to 366 //extreme unavailability of pipes. This can also be done via hybrid calculations 367 //also involving many more variables like number of write-back interfaces etc, 368 //but the variety of scenarios is too high to warrant that. 369 bool canUseRotator(hwc_context_t *ctx, int dpy); 370 371 int getLeftSplit(hwc_context_t *ctx, const int& dpy); 372 373 bool isDisplaySplit(hwc_context_t* ctx, int dpy); 374 375 // Set the GPU hint flag to high for MIXED/GPU composition only for 376 // first frame after MDP to GPU/MIXED mode transition. 377 // Set the GPU hint to default if the current composition type is GPU 378 // due to idle fallback or MDP composition. 379 void setGPUHint(hwc_context_t* ctx, hwc_display_contents_1_t* list); 380 381 // Inline utility functions 382 static inline bool isSkipLayer(const hwc_layer_1_t* l) { 383 return (UNLIKELY(l && (l->flags & HWC_SKIP_LAYER))); 384 } 385 386 // Returns true if the buffer is yuv 387 static inline bool isYuvBuffer(const private_handle_t* hnd) { 388 return (hnd && (hnd->bufferType == BUFFER_TYPE_VIDEO)); 389 } 390 391 // Returns true if the buffer is yuv 392 static inline bool is4kx2kYuvBuffer(const private_handle_t* hnd) { 393 return (hnd && (hnd->bufferType == BUFFER_TYPE_VIDEO) && 394 (hnd->width > 2048)); 395 } 396 397 // Returns true if the buffer is secure 398 static inline bool isSecureBuffer(const private_handle_t* hnd) { 399 return (hnd && (private_handle_t::PRIV_FLAGS_SECURE_BUFFER & hnd->flags)); 400 } 401 402 static inline bool isTileRendered(const private_handle_t* hnd) { 403 return (hnd && (private_handle_t::PRIV_FLAGS_TILE_RENDERED & hnd->flags)); 404 } 405 406 //Return true if buffer is marked locked 407 static inline bool isBufferLocked(const private_handle_t* hnd) { 408 return (hnd && (private_handle_t::PRIV_FLAGS_HWC_LOCK & hnd->flags)); 409 } 410 411 //Return true if buffer is for external display only 412 static inline bool isExtOnly(const private_handle_t* hnd) { 413 return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY)); 414 } 415 416 //Return true if the buffer is intended for Secure Display 417 static inline bool isSecureDisplayBuffer(const private_handle_t* hnd) { 418 return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_DISPLAY)); 419 } 420 421 static inline int getWidth(const private_handle_t* hnd) { 422 if(isYuvBuffer(hnd)) { 423 MetaData_t *metadata = reinterpret_cast<MetaData_t*>(hnd->base_metadata); 424 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) { 425 return metadata->bufferDim.sliceWidth; 426 } 427 } 428 return hnd->width; 429 } 430 431 static inline int getHeight(const private_handle_t* hnd) { 432 if(isYuvBuffer(hnd)) { 433 MetaData_t *metadata = reinterpret_cast<MetaData_t*>(hnd->base_metadata); 434 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) { 435 return metadata->bufferDim.sliceHeight; 436 } 437 } 438 return hnd->height; 439 } 440 441 template<typename T> inline T max(T a, T b) { return (a > b) ? a : b; } 442 template<typename T> inline T min(T a, T b) { return (a < b) ? a : b; } 443 444 // Initialize uevent thread 445 void init_uevent_thread(hwc_context_t* ctx); 446 // Initialize vsync thread 447 void init_vsync_thread(hwc_context_t* ctx); 448 449 inline void getLayerResolution(const hwc_layer_1_t* layer, 450 int& width, int& height) { 451 hwc_rect_t displayFrame = layer->displayFrame; 452 width = displayFrame.right - displayFrame.left; 453 height = displayFrame.bottom - displayFrame.top; 454 } 455 456 static inline int openFb(int dpy) { 457 int fd = -1; 458 const char *devtmpl = "/dev/graphics/fb%u"; 459 char name[64] = {0}; 460 snprintf(name, 64, devtmpl, dpy); 461 fd = open(name, O_RDWR); 462 return fd; 463 } 464 465 template <class T> 466 inline void swap(T& a, T& b) { 467 T tmp = a; 468 a = b; 469 b = tmp; 470 } 471 472 }; //qhwc namespace 473 474 enum eAnimationState{ 475 ANIMATION_STOPPED, 476 ANIMATION_STARTED, 477 }; 478 479 // Structure holds the information about the GPU hint. 480 struct gpu_hint_info { 481 // system level flag to enable gpu_perf_mode 482 bool mGpuPerfModeEnable; 483 // Stores the current GPU performance mode DEFAULT/HIGH 484 bool mCurrGPUPerfMode; 485 // true if previous composition used GPU 486 bool mPrevCompositionGLES; 487 // Stores the EGLContext of current process 488 EGLContext mEGLContext; 489 // Stores the EGLDisplay of current process 490 EGLDisplay mEGLDisplay; 491 }; 492 493 // ----------------------------------------------------------------------------- 494 // HWC context 495 // This structure contains overall state 496 struct hwc_context_t { 497 hwc_composer_device_1_t device; 498 const hwc_procs_t* proc; 499 500 //CopyBit objects 501 qhwc::CopyBit *mCopyBit[HWC_NUM_DISPLAY_TYPES]; 502 503 //Overlay object - NULL for non overlay devices 504 overlay::Overlay *mOverlay; 505 //Holds a few rot objects 506 overlay::RotMgr *mRotMgr; 507 508 //Primary and external FB updater 509 qhwc::IFBUpdate *mFBUpdate[HWC_NUM_DISPLAY_TYPES]; 510 // External display related information 511 qhwc::ExternalDisplay *mExtDisplay; 512 qhwc::VirtualDisplay *mVirtualDisplay; 513 qhwc::MDPInfo mMDP; 514 qhwc::VsyncState vstate; 515 qhwc::DisplayAttributes dpyAttr[HWC_NUM_DISPLAY_TYPES]; 516 qhwc::ListStats listStats[HWC_NUM_DISPLAY_TYPES]; 517 qhwc::LayerProp *layerProp[HWC_NUM_DISPLAY_TYPES]; 518 qhwc::MDPComp *mMDPComp[HWC_NUM_DISPLAY_TYPES]; 519 qhwc::HwcDebug *mHwcDebug[HWC_NUM_DISPLAY_TYPES]; 520 hwc_rect_t mViewFrame[HWC_NUM_DISPLAY_TYPES]; 521 qhwc::AssertiveDisplay *mAD; 522 eAnimationState mAnimationState[HWC_NUM_DISPLAY_TYPES]; 523 qhwc::HWCVirtualBase *mHWCVirtual; 524 525 // stores the #numHwLayers of the previous frame 526 // for each display device 527 int mPrevHwLayerCount[HWC_NUM_DISPLAY_TYPES]; 528 529 // stores the primary device orientation 530 int deviceOrientation; 531 //Securing in progress indicator 532 bool mSecuring; 533 //WFD on proprietary stack 534 bool mVirtualonExtActive; 535 //Display in secure mode indicator 536 bool mSecureMode; 537 //Lock to protect drawing data structures 538 mutable Locker mDrawLock; 539 //Drawing round when we use GPU 540 bool isPaddingRound; 541 // External Orientation 542 int mExtOrientation; 543 //Flags the transition of a video session 544 bool mVideoTransFlag; 545 //Used for SideSync feature 546 //which overrides the mExtOrientation 547 bool mBufferMirrorMode; 548 // Used to synchronize between WFD and Display modules 549 mutable Locker mWfdSyncLock; 550 551 qhwc::LayerRotMap *mLayerRotMap[HWC_NUM_DISPLAY_TYPES]; 552 // Panel reset flag will be set if BTA check fails 553 bool mPanelResetStatus; 554 // number of active Displays 555 int numActiveDisplays; 556 // Downscale feature switch, set via system property 557 // sys.hwc.mdp_downscale_enabled 558 bool mMDPDownscaleEnabled; 559 struct gpu_hint_info mGPUHintInfo; 560 // Display binder service 561 qService::QService* mQService; 562 }; 563 564 namespace qhwc { 565 static inline bool isSkipPresent (hwc_context_t *ctx, int dpy) { 566 return ctx->listStats[dpy].skipCount; 567 } 568 569 static inline bool isYuvPresent (hwc_context_t *ctx, int dpy) { 570 return ctx->listStats[dpy].yuvCount; 571 } 572 573 static inline bool has90Transform(hwc_layer_1_t *layer) { 574 return ((layer->transform & HWC_TRANSFORM_ROT_90) && 575 !(layer->flags & HWC_COLOR_FILL)); 576 } 577 578 inline bool isSecurePresent(hwc_context_t *ctx, int dpy) { 579 return ctx->listStats[dpy].isSecurePresent; 580 } 581 582 static inline bool isSecondaryConfiguring(hwc_context_t* ctx) { 583 return (ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isConfiguring || 584 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isConfiguring); 585 } 586 587 static inline bool isSecondaryConnected(hwc_context_t* ctx) { 588 return (ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].connected || 589 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].connected); 590 } 591 592 }; 593 594 #endif //HWC_UTILS_H 595