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 DEBUG_MDPDOWNSCALE 0
25 #define HWC_REMOVE_DEPRECATED_VERSIONS 1
26
27 #include <fcntl.h>
28 #include <math.h>
29 #include <hardware/hwcomposer.h>
30 #include <gr.h>
31 #include <gralloc_priv.h>
32 #include <utils/String8.h>
33 #include "qdMetaData.h"
34 #include "mdp_version.h"
35 #include <overlayUtils.h>
36 #include <overlayRotator.h>
37 #include <EGL/egl.h>
38
39
40 #define ALIGN_TO(x, align) (((x) + ((align)-1)) & ~((align)-1))
41 #define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
42 #define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
43 #define MAX_NUM_APP_LAYERS 32
44 #define MAX_NUM_BLEND_STAGES 16
45 #define MIN_DISPLAY_XRES 200
46 #define MIN_DISPLAY_YRES 200
47 #define HWC_WFDDISPSYNC_LOG 0
48 #define STR(f) #f;
49 // Max number of PTOR layers handled
50 #define MAX_PTOR_LAYERS 2
51 #define MAX_NUM_COLOR_MODES 32
52
53 //Fwrd decls
54 struct hwc_context_t;
55
56 namespace ovutils = overlay::utils;
57
58 namespace overlay {
59 class Overlay;
60 class Rotator;
61 class RotMgr;
62 }
63
64 namespace qhwc {
65 //fwrd decl
66 class QueuedBufferStore;
67 class HDMIDisplay;
68 class VirtualDisplay;
69 class IFBUpdate;
70 class IVideoOverlay;
71 class MDPComp;
72 class CopyBit;
73 class HwcDebug;
74 class AssertiveDisplay;
75 class HWCVirtualVDS;
76
77
78 struct MDPInfo {
79 int version;
80 char panel;
81 bool hasOverlay;
82 };
83
84 struct DisplayAttributes {
85 uint32_t refreshRate;
86 uint32_t dynRefreshRate;
87 uint32_t vsync_period; //nanos
88 uint32_t xres;
89 uint32_t yres;
90 uint32_t stride;
91 float xdpi;
92 float ydpi;
93 bool secure;
94 int fd;
95 bool connected; //Applies only to pluggable disp.
96 //Connected does not mean it ready to use.
97 //It should be active also. (UNBLANKED)
98 bool isActive;
99 // In pause state, composition is bypassed
100 // used for WFD displays and in QDCM calibration mode
101 bool isPause;
102 // To trigger padding round to clean up mdp
103 // pipes
104 bool isConfiguring;
105 // Indicates whether external/virtual display is in MDP scaling mode
106 bool mMDPScalingMode;
107 // Ext dst Rect
108 hwc_rect_t mDstRect;
109 //Action safe attributes
110 // Flag to indicate the presence of action safe dimensions for external
111 bool mActionSafePresent;
112 int mAsWidthRatio;
113 int mAsHeightRatio;
114
115 //If property fbsize set via adb shell debug.hwc.fbsize = XRESxYRES
116 //following fields are used.
117 bool customFBSize;
118 uint32_t xres_new;
119 uint32_t yres_new;
120
121 };
122
123 struct ListStats {
124 int numAppLayers; //Total - 1, excluding FB layer.
125 int skipCount;
126 int fbLayerIndex; //Always last for now. = numAppLayers
127 //Video specific
128 int yuvCount;
129 int yuvIndices[MAX_NUM_APP_LAYERS];
130 bool preMultipliedAlpha;
131 int yuv4k2kIndices[MAX_NUM_APP_LAYERS];
132 int yuv4k2kCount;
133 // Notifies hwcomposer about the start and end of animation
134 // This will be set to true during animation, otherwise false.
135 bool isDisplayAnimating;
136 bool secureUI; // Secure display layer
137 bool isSecurePresent;
138 hwc_rect_t lRoi; //left ROI
139 hwc_rect_t rRoi; //right ROI. Unused in single DSI panels.
140 //App Buffer Composition index
141 int renderBufIndexforABC;
142 // Secure RGB specific
143 int secureRGBCount;
144 int secureRGBIndices[MAX_NUM_APP_LAYERS];
145 //dyn refresh rate-Client requested refreshrate
146 uint32_t refreshRateRequest;
147 // Flag related to windowboxing feature
148 bool mAIVVideoMode;
149 };
150
151 //PTOR Comp info
152 struct PtorInfo {
153 int count;
154 int layerIndex[MAX_PTOR_LAYERS];
155 hwc_rect_t displayFrame[MAX_PTOR_LAYERS];
isActivePtorInfo156 bool isActive() { return (count>0); }
getPTORArrayIndexPtorInfo157 int getPTORArrayIndex(int index) {
158 int idx = -1;
159 for(int i = 0; i < count; i++) {
160 if(index == layerIndex[i])
161 idx = i;
162 }
163 return idx;
164 }
165 };
166
167 struct LayerProp {
168 uint32_t mFlags; //qcom specific layer flags
LayerPropLayerProp169 LayerProp():mFlags(0){};
170 };
171
172 struct VsyncState {
173 bool enable;
174 bool fakevsync;
175 bool debug;
176 };
177
178 struct BwcPM {
179 static void setBwc(const hwc_context_t *ctx, const int& dpy,
180 const private_handle_t *hnd,
181 const hwc_rect_t& crop, const hwc_rect_t& dst,
182 const int& transform, const int& downscale,
183 ovutils::eMdpFlags& mdpFlags);
184 };
185
186 // LayerProp::flag values
187 enum {
188 HWC_MDPCOMP = 0x00000001,
189 HWC_COPYBIT = 0x00000002,
190 };
191
192 // AIV specific flags
193 enum {
194 HWC_AIV_VIDEO = 0x80000000,
195 HWC_AIV_CC = 0x40000000,
196 };
197
198 // HAL specific features
199 enum {
200 HWC_COLOR_FILL = 0x00000008,
201 HWC_FORMAT_RB_SWAP = 0x00000040,
202 };
203
204 /* External Display states */
205 enum {
206 EXTERNAL_OFFLINE = 0,
207 EXTERNAL_ONLINE,
208 EXTERNAL_PAUSE,
209 EXTERNAL_RESUME,
210 EXTERNAL_MAXSTATES
211 };
212
213 class LayerRotMap {
214 public:
LayerRotMap()215 LayerRotMap() { reset(); }
216 void add(hwc_layer_1_t* layer, overlay::Rotator *rot);
217 //Resets the mapping of layer to rotator
218 void reset();
219 //Clears mappings and existing rotator fences
220 //Intended to be used during errors
221 void clear();
222 uint32_t getCount() const;
223 hwc_layer_1_t* getLayer(uint32_t index) const;
224 overlay::Rotator* getRot(uint32_t index) const;
225 bool isRotCached(uint32_t index) const;
226 void setReleaseFd(const int& fence);
227 private:
228 hwc_layer_1_t* mLayer[overlay::RotMgr::MAX_ROT_SESS];
229 overlay::Rotator* mRot[overlay::RotMgr::MAX_ROT_SESS];
230 uint32_t mCount;
231 };
232
233 //ColorModes for primary displays
234 class ColorMode {
235 public:
236 void init();
237 void destroy();
getNumModes()238 int32_t getNumModes() { return mNumModes; }
getModeList()239 const int32_t* getModeList() { return mModeList; }
240 int32_t getModeForIndex(int32_t index);
241 int32_t getIndexForMode(int32_t mode);
242 int applyDefaultMode();
243 int applyModeByID(int modeID);
244 int applyModeByIndex(int index);
245 int setDefaultMode(int modeID);
getActiveModeIndex()246 int getActiveModeIndex() { return mCurModeIndex; }
247 private:
248 int32_t (*fnGetNumModes)(int /*dispID*/);
249 int32_t (*fnGetModeList)(int32_t* /*mModeList*/, int32_t* /*current default*/,
250 int32_t /*dispID*/);
251 int (*fnApplyDefaultMode)(int /*dispID*/);
252 int (*fnApplyModeById)(int /*modeID*/, int /*dispID*/);
253 int (*fnSetDefaultMode)(int /*modeID*/, int /*dispID*/);
254
255 void* mModeHandle = NULL;
256 int32_t mModeList[MAX_NUM_COLOR_MODES];
257 int32_t mNumModes = 0;
258 int32_t mCurModeIndex = 0;
259 int32_t mCurMode = 0;
260
261 };
262
getCount()263 inline uint32_t LayerRotMap::getCount() const {
264 return mCount;
265 }
266
getLayer(uint32_t index)267 inline hwc_layer_1_t* LayerRotMap::getLayer(uint32_t index) const {
268 if(index >= mCount) return NULL;
269 return mLayer[index];
270 }
271
getRot(uint32_t index)272 inline overlay::Rotator* LayerRotMap::getRot(uint32_t index) const {
273 if(index >= mCount) return NULL;
274 return mRot[index];
275 }
276
integerizeSourceCrop(const hwc_frect_t & cropF)277 inline hwc_rect_t integerizeSourceCrop(const hwc_frect_t& cropF) {
278 hwc_rect_t cropI = {0,0,0,0};
279 cropI.left = int(ceilf(cropF.left));
280 cropI.top = int(ceilf(cropF.top));
281 cropI.right = int(floorf(cropF.right));
282 cropI.bottom = int(floorf(cropF.bottom));
283 return cropI;
284 }
285
isNonIntegralSourceCrop(const hwc_frect_t & cropF)286 inline bool isNonIntegralSourceCrop(const hwc_frect_t& cropF) {
287 if(cropF.left - roundf(cropF.left) ||
288 cropF.top - roundf(cropF.top) ||
289 cropF.right - roundf(cropF.right) ||
290 cropF.bottom - roundf(cropF.bottom))
291 return true;
292 else
293 return false;
294 }
295
296 // -----------------------------------------------------------------------------
297 // Utility functions - implemented in hwc_utils.cpp
298 void dumpLayer(hwc_layer_1_t const* l);
299 void setListStats(hwc_context_t *ctx, hwc_display_contents_1_t *list,
300 int dpy);
301 void initContext(hwc_context_t *ctx);
302 void closeContext(hwc_context_t *ctx);
303 //Crops source buffer against destination and FB boundaries
304 void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
305 const hwc_rect_t& scissor, int orient);
306 void getNonWormholeRegion(hwc_display_contents_1_t* list,
307 hwc_rect_t& nwr);
308 bool isSecuring(hwc_context_t* ctx, hwc_layer_1_t const* layer);
309 bool isSecureModePolicy(int mdpVersion);
310 // Returns true, if the input layer format is supported by rotator
311 bool isRotatorSupportedFormat(private_handle_t *hnd);
312 //Returns true, if the layer is YUV or the layer has been rendered by CPU
313 bool isRotationDoable(hwc_context_t *ctx, private_handle_t *hnd);
314 bool isExternalActive(hwc_context_t* ctx);
315 bool isAlphaScaled(hwc_layer_1_t const* layer);
316 bool needsScaling(hwc_layer_1_t const* layer);
317 bool isDownscaleRequired(hwc_layer_1_t const* layer);
318 bool needsScalingWithSplit(hwc_context_t* ctx, hwc_layer_1_t const* layer,
319 const int& dpy);
320 void sanitizeSourceCrop(hwc_rect_t& cropL, hwc_rect_t& cropR,
321 private_handle_t *hnd);
322 bool isAlphaPresent(hwc_layer_1_t const* layer);
323 int hwc_vsync_control(hwc_context_t* ctx, int dpy, int enable);
324 int getBlending(int blending);
325 bool isGLESOnlyComp(hwc_context_t *ctx, const int& dpy);
326 void reset_layer_prop(hwc_context_t* ctx, int dpy, int numAppLayers);
327 bool isAbcInUse(hwc_context_t *ctx);
328
329 void dumpBuffer(private_handle_t *ohnd, char *bufferName);
330 void updateDisplayInfo(hwc_context_t* ctx, int dpy);
331 void resetDisplayInfo(hwc_context_t* ctx, int dpy);
332 void initCompositionResources(hwc_context_t* ctx, int dpy);
333 void destroyCompositionResources(hwc_context_t* ctx, int dpy);
334 void clearPipeResources(hwc_context_t* ctx, int dpy);
335
336 //Helper function to dump logs
337 void dumpsys_log(android::String8& buf, const char* fmt, ...);
338
339 int getExtOrientation(hwc_context_t* ctx);
340 bool isValidRect(const hwc_rect_t& rect);
341 hwc_rect_t deductRect(const hwc_rect_t& rect1, const hwc_rect_t& rect2);
342 bool isSameRect(const hwc_rect& rect1, const hwc_rect& rect2);
343 hwc_rect_t moveRect(const hwc_rect_t& rect, const int& x_off, const int& y_off);
344 hwc_rect_t getIntersection(const hwc_rect_t& rect1, const hwc_rect_t& rect2);
345 hwc_rect_t getUnion(const hwc_rect_t& rect1, const hwc_rect_t& rect2);
346 void optimizeLayerRects(const hwc_display_contents_1_t *list);
347 bool areLayersIntersecting(const hwc_layer_1_t* layer1,
348 const hwc_layer_1_t* layer2);
349 bool operator ==(const hwc_rect_t& lhs, const hwc_rect_t& rhs);
350 bool layerUpdating(const hwc_layer_1_t* layer);
351
352 // returns true if Action safe dimensions are set and target supports Actionsafe
353 bool isActionSafePresent(hwc_context_t *ctx, int dpy);
354
355 /* Calculates the destination position based on the action safe rectangle */
356 void getActionSafePosition(hwc_context_t *ctx, int dpy, hwc_rect_t& dst);
357
358 void getAspectRatioPosition(hwc_context_t* ctx, int dpy, int extOrientation,
359 hwc_rect_t& inRect, hwc_rect_t& outRect);
360
361 uint32_t roundOff(uint32_t refreshRate);
362
363 void setRefreshRate(hwc_context_t *ctx, int dpy, uint32_t refreshRate);
364
365 bool isPrimaryPortrait(hwc_context_t *ctx);
366
367 bool isOrientationPortrait(hwc_context_t *ctx);
368
369 void calcExtDisplayPosition(hwc_context_t *ctx,
370 private_handle_t *hnd,
371 int dpy,
372 hwc_rect_t& sourceCrop,
373 hwc_rect_t& displayFrame,
374 int& transform,
375 ovutils::eTransform& orient);
376
377 // Returns the orientation that needs to be set on external for
378 // BufferMirrirMode(Sidesync)
379 int getMirrorModeOrientation(hwc_context_t *ctx);
380
381 /* Get External State names */
382 const char* getExternalDisplayState(uint32_t external_state);
383
384 // Resets display ROI to full panel resoluion
385 void resetROI(hwc_context_t *ctx, const int dpy);
386
387 // Modifies ROI even from middle of the screen
388 hwc_rect expandROIFromMidPoint(hwc_rect roi, hwc_rect fullFrame);
389
390 // Aligns updating ROI to panel restrictions
391 hwc_rect_t getSanitizeROI(struct hwc_rect roi, hwc_rect boundary);
392
393 // Handles wfd Pause and resume events
394 void handle_pause(hwc_context_t *ctx, int dpy);
395 void handle_resume(hwc_context_t *ctx, int dpy);
396
397 // Handle ONLINE/OFFLINE for HDMI display
398 void handle_online(hwc_context_t* ctx, int dpy);
399 void handle_offline(hwc_context_t* ctx, int dpy);
400
401 //Close acquireFenceFds of all layers of incoming list
402 void closeAcquireFds(hwc_display_contents_1_t* list);
403
404 //Sync point impl.
405 int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy,
406 int fd);
407
408 //Sets appropriate mdp flags for a layer.
409 void setMdpFlags(hwc_context_t *ctx, hwc_layer_1_t *layer,
410 ovutils::eMdpFlags &mdpFlags,
411 int rotDownscale, int transform);
412
413 int configRotator(overlay::Rotator *rot, ovutils::Whf& whf,
414 hwc_rect_t& crop, const ovutils::eMdpFlags& mdpFlags,
415 const ovutils::eTransform& orient, const int& downscale);
416
417 int configMdp(overlay::Overlay *ov, const ovutils::PipeArgs& parg,
418 const ovutils::eTransform& orient, const hwc_rect_t& crop,
419 const hwc_rect_t& pos, const MetaData_t *metadata,
420 const ovutils::eDest& dest);
421
422 int configColorLayer(hwc_context_t *ctx, hwc_layer_1_t *layer, const int& dpy,
423 ovutils::eMdpFlags& mdpFlags, ovutils::eZorder& z,
424 const ovutils::eDest& dest);
425
426 void updateSource(ovutils::eTransform& orient, ovutils::Whf& whf,
427 hwc_rect_t& crop, overlay::Rotator *rot);
428
429 bool isZoomModeEnabled(hwc_rect_t crop);
430 void updateCropAIVVideoMode(hwc_context_t *ctx, hwc_rect_t& crop, int dpy);
431 void updateDestAIVVideoMode(hwc_context_t *ctx, hwc_rect_t& dst, int dpy);
432 void updateCoordinates(hwc_context_t *ctx, hwc_rect_t& crop,
433 hwc_rect_t& dst, int dpy);
434
435 //Routine to configure low resolution panels (<= 2048 width)
436 int configureNonSplit(hwc_context_t *ctx, hwc_layer_1_t *layer, const int& dpy,
437 ovutils::eMdpFlags& mdpFlags, ovutils::eZorder& z,
438 const ovutils::eDest& dest,
439 overlay::Rotator **rot);
440
441 //Routine to configure high resolution panels (> 2048 width)
442 int configureSplit(hwc_context_t *ctx, hwc_layer_1_t *layer, const int& dpy,
443 ovutils::eMdpFlags& mdpFlags, ovutils::eZorder& z,
444 const ovutils::eDest& lDest,
445 const ovutils::eDest& rDest, overlay::Rotator **rot);
446
447 //Routine to split and configure high resolution YUV layer (> 2048 width)
448 int configureSourceSplit(hwc_context_t *ctx, hwc_layer_1_t *layer,
449 const int& dpy,
450 ovutils::eMdpFlags& mdpFlags, ovutils::eZorder& z,
451 const ovutils::eDest& lDest,
452 const ovutils::eDest& rDest, overlay::Rotator **rot);
453
454 //On certain targets DMA pipes are used for rotation and they won't be available
455 //for line operations. On a per-target basis we can restrict certain use cases
456 //from using rotator, since we know before-hand that such scenarios can lead to
457 //extreme unavailability of pipes. This can also be done via hybrid calculations
458 //also involving many more variables like number of write-back interfaces etc,
459 //but the variety of scenarios is too high to warrant that.
460 bool canUseRotator(hwc_context_t *ctx, int dpy);
461
462 int getLeftSplit(hwc_context_t *ctx, const int& dpy);
463
464 bool isDisplaySplit(hwc_context_t* ctx, int dpy);
465
466 int getRotDownscale(hwc_context_t *ctx, const hwc_layer_1_t *layer);
467
468 // Set the GPU hint flag to high for MIXED/GPU composition only for
469 // first frame after MDP to GPU/MIXED mode transition.
470 // Set the GPU hint to default if the current composition type is GPU
471 // due to idle fallback or MDP composition.
472 void setGPUHint(hwc_context_t* ctx, hwc_display_contents_1_t* list);
473
474 // Returns true if rect1 is peripheral to rect2, false otherwise.
475 bool isPeripheral(const hwc_rect_t& rect1, const hwc_rect_t& rect2);
476
477 // Applies default mode at boot
478 void applyDefaultMode(hwc_context_t *ctx);
479
480 // Inline utility functions
isSkipLayer(const hwc_layer_1_t * l)481 static inline bool isSkipLayer(const hwc_layer_1_t* l) {
482 return (UNLIKELY(l && (l->flags & HWC_SKIP_LAYER)));
483 }
484
isAIVVideoLayer(const hwc_layer_1_t * l)485 static inline bool isAIVVideoLayer(const hwc_layer_1_t* l) {
486 return (UNLIKELY(l && (l->flags & HWC_AIV_VIDEO)));
487 }
488
isAIVCCLayer(const hwc_layer_1_t * l)489 static inline bool isAIVCCLayer(const hwc_layer_1_t* l) {
490 return (UNLIKELY(l && (l->flags & HWC_AIV_CC)));
491 }
492
493 // Returns true if the buffer is yuv
isYuvBuffer(const private_handle_t * hnd)494 static inline bool isYuvBuffer(const private_handle_t* hnd) {
495 return (hnd && (hnd->bufferType == BUFFER_TYPE_VIDEO));
496 }
497
498 // Returns true if the buffer is yuv and exceeds the mixer width
isYUVSplitNeeded(const private_handle_t * hnd)499 static inline bool isYUVSplitNeeded(const private_handle_t* hnd) {
500 int maxPipeWidth = qdutils::MDPVersion::getInstance().getMaxPipeWidth();
501 return (hnd && (hnd->bufferType == BUFFER_TYPE_VIDEO) &&
502 (hnd->width > maxPipeWidth));
503 }
504
505 // Returns true if the buffer is secure
isSecureBuffer(const private_handle_t * hnd)506 static inline bool isSecureBuffer(const private_handle_t* hnd) {
507 return (hnd && (private_handle_t::PRIV_FLAGS_SECURE_BUFFER & hnd->flags));
508 }
509
510 // Returns true if the buffer is protected
isProtectedBuffer(const private_handle_t * hnd)511 static inline bool isProtectedBuffer(const private_handle_t* hnd) {
512 return (hnd && (private_handle_t::PRIV_FLAGS_PROTECTED_BUFFER & hnd->flags));
513 }
514
515
isTileRendered(const private_handle_t * hnd)516 static inline bool isTileRendered(const private_handle_t* hnd) {
517 return (hnd && (private_handle_t::PRIV_FLAGS_TILE_RENDERED & hnd->flags));
518 }
519
520 //Return true if the buffer is intended for Secure Display
isSecureDisplayBuffer(const private_handle_t * hnd)521 static inline bool isSecureDisplayBuffer(const private_handle_t* hnd) {
522 return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_DISPLAY));
523 }
524
getWidth(const private_handle_t * hnd)525 static inline int getWidth(const private_handle_t* hnd) {
526 MetaData_t *metadata = reinterpret_cast<MetaData_t*>(hnd->base_metadata);
527 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
528 return metadata->bufferDim.sliceWidth;
529 }
530 return hnd->width;
531 }
532
getHeight(const private_handle_t * hnd)533 static inline int getHeight(const private_handle_t* hnd) {
534 MetaData_t *metadata = reinterpret_cast<MetaData_t*>(hnd->base_metadata);
535 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
536 return metadata->bufferDim.sliceHeight;
537 }
538 return hnd->height;
539 }
540
max(T a,T b)541 template<typename T> inline T max(T a, T b) { return (a > b) ? a : b; }
min(T a,T b)542 template<typename T> inline T min(T a, T b) { return (a < b) ? a : b; }
543
544 // Initialize uevent thread
545 void init_uevent_thread(hwc_context_t* ctx);
546 // Initialize vsync thread
547 void init_vsync_thread(hwc_context_t* ctx);
548
getLayerResolution(const hwc_layer_1_t * layer,int & width,int & height)549 inline void getLayerResolution(const hwc_layer_1_t* layer,
550 int& width, int& height) {
551 hwc_rect_t displayFrame = layer->displayFrame;
552 width = displayFrame.right - displayFrame.left;
553 height = displayFrame.bottom - displayFrame.top;
554 }
555
openFb(int dpy)556 static inline int openFb(int dpy) {
557 int fd = -1;
558 const char *devtmpl = "/dev/graphics/fb%u";
559 char name[64] = {0};
560 snprintf(name, 64, devtmpl, dpy);
561 fd = open(name, O_RDWR);
562 return fd;
563 }
564
565 template <class T>
swap(T & a,T & b)566 inline void swap(T& a, T& b) {
567 T tmp = a;
568 a = b;
569 b = tmp;
570 }
571
572 }; //qhwc namespace
573
574 enum eAnimationState{
575 ANIMATION_STOPPED,
576 ANIMATION_STARTED,
577 };
578
579 enum eCompositionState {
580 COMPOSITION_STATE_MDP = 0, // Set if composition type is MDP
581 COMPOSITION_STATE_GPU, // Set if composition type is GPU or MIXED
582 COMPOSITION_STATE_IDLE_FALLBACK, // Set if it is idlefallback
583 };
584
585 // Structure holds the information about the GPU hint.
586 struct gpu_hint_info {
587 // system level flag to enable gpu_perf_mode
588 bool mGpuPerfModeEnable;
589 // Stores the current GPU performance mode DEFAULT/HIGH
590 bool mCurrGPUPerfMode;
591 // Stores the compositon state GPU, MDP or IDLE_FALLBACK
592 bool mCompositionState;
593 // Stores the EGLContext of current process
594 EGLContext mEGLContext;
595 // Stores the EGLDisplay of current process
596 EGLDisplay mEGLDisplay;
597 };
598
599 // -----------------------------------------------------------------------------
600 // HWC context
601 // This structure contains overall state
602 struct hwc_context_t {
603 hwc_composer_device_1_t device;
604 const hwc_procs_t* proc;
605
606 //CopyBit objects
607 qhwc::CopyBit *mCopyBit[HWC_NUM_DISPLAY_TYPES];
608
609 //Overlay object - NULL for non overlay devices
610 overlay::Overlay *mOverlay;
611 //Holds a few rot objects
612 overlay::RotMgr *mRotMgr;
613
614 //Primary and external FB updater
615 qhwc::IFBUpdate *mFBUpdate[HWC_NUM_DISPLAY_TYPES];
616 // HDMI display related object. Used to configure/teardown
617 // HDMI when it is connected as primary or external.
618 qhwc::HDMIDisplay *mHDMIDisplay;
619 qhwc::MDPInfo mMDP;
620 qhwc::VsyncState vstate;
621 qhwc::DisplayAttributes dpyAttr[HWC_NUM_DISPLAY_TYPES];
622 qhwc::ListStats listStats[HWC_NUM_DISPLAY_TYPES];
623 qhwc::LayerProp *layerProp[HWC_NUM_DISPLAY_TYPES];
624 qhwc::MDPComp *mMDPComp[HWC_NUM_DISPLAY_TYPES];
625 qhwc::HwcDebug *mHwcDebug[HWC_NUM_DISPLAY_TYPES];
626 hwc_rect_t mViewFrame[HWC_NUM_DISPLAY_TYPES];
627 qhwc::AssertiveDisplay *mAD;
628 eAnimationState mAnimationState[HWC_NUM_DISPLAY_TYPES];
629 qhwc::HWCVirtualVDS *mHWCVirtual;
630
631 // stores the #numHwLayers of the previous frame
632 // for each display device
633 int mPrevHwLayerCount[HWC_NUM_DISPLAY_TYPES];
634
635 // stores the primary device orientation
636 int deviceOrientation;
637 //Securing in progress indicator
638 bool mSecuring;
639 //Display in secure mode indicator
640 bool mSecureMode;
641 //Lock to protect drawing data structures
642 mutable Locker mDrawLock;
643 //Drawing round when we use GPU
644 bool isPaddingRound;
645 // Used to mark composition cycle when DMA state change is required
646 bool isDMAStateChanging;
647 // External Orientation
648 int mExtOrientation;
649 //Flags the transition of a video session
650 bool mVideoTransFlag;
651 //Used for SideSync feature
652 //which overrides the mExtOrientation
653 bool mBufferMirrorMode;
654 // Used to synchronize between WFD and Display modules
655 mutable Locker mWfdSyncLock;
656
657 qhwc::LayerRotMap *mLayerRotMap[HWC_NUM_DISPLAY_TYPES];
658 // Panel reset flag will be set if BTA check fails
659 bool mPanelResetStatus;
660 // number of active Displays
661 int numActiveDisplays;
662 struct gpu_hint_info mGPUHintInfo;
663 //App Buffer Composition
664 bool enableABC;
665 // PTOR Info
666 qhwc::PtorInfo mPtorInfo;
667 //Running in Thermal burst mode
668 bool mThermalBurstMode;
669 //Layers out of ROI
670 bool copybitDrop[MAX_NUM_APP_LAYERS];
671 // Flag related to windowboxing feature
672 bool mWindowboxFeature;
673 // This denotes the tolerance between video layer and external display
674 // aspect ratio
675 float mAspectRatioToleranceLevel;
676 // Runtime switch for BWC for targets that support it
677 bool mBWCEnabled;
678 // Provides a way for OEM's to disable setting dynfps via metadata.
679 bool mUseMetaDataRefreshRate;
680 // Stores the hpd enabled status- avoids re-enabling HDP on suspend resume.
681 bool mHPDEnabled;
682 //Used to notify that default mode has been applied
683 bool mDefaultModeApplied;
684 //Manages color modes
685 qhwc::ColorMode *mColorMode;
686 };
687
688 namespace qhwc {
isSkipPresent(hwc_context_t * ctx,int dpy)689 static inline bool isSkipPresent (hwc_context_t *ctx, int dpy) {
690 return ctx->listStats[dpy].skipCount;
691 }
692
isYuvPresent(hwc_context_t * ctx,int dpy)693 static inline bool isYuvPresent (hwc_context_t *ctx, int dpy) {
694 return ctx->listStats[dpy].yuvCount;
695 }
696
has90Transform(hwc_layer_1_t const * layer)697 static inline bool has90Transform(hwc_layer_1_t const* layer) {
698 return ((layer->transform & HWC_TRANSFORM_ROT_90) &&
699 !(layer->flags & HWC_COLOR_FILL));
700 }
701
isSecurePresent(hwc_context_t * ctx,int dpy)702 inline bool isSecurePresent(hwc_context_t *ctx, int dpy) {
703 return ctx->listStats[dpy].isSecurePresent;
704 }
705
isSecondaryConfiguring(hwc_context_t * ctx)706 static inline bool isSecondaryConfiguring(hwc_context_t* ctx) {
707 return (ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isConfiguring ||
708 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isConfiguring);
709 }
710
isSecondaryConnected(hwc_context_t * ctx)711 static inline bool isSecondaryConnected(hwc_context_t* ctx) {
712 return (ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].connected ||
713 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].connected);
714 }
715
716 /* Return Virtual Display connection status */
isVDConnected(hwc_context_t * ctx)717 static inline bool isVDConnected(hwc_context_t* ctx) {
718 return ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].connected;
719 }
720
getLayerClock(const uint32_t & dstW,const uint32_t & dstH,const uint32_t & srcH)721 inline uint32_t getLayerClock(const uint32_t& dstW, const uint32_t& dstH,
722 const uint32_t& srcH) {
723 return max(dstW, (srcH * dstW) / dstH);
724 }
725
726 };
727
728 #endif //HWC_UTILS_H
729