hwc_utils.h revision a9776b10e38f4ebdc7466955e933217fd15ae180
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
34#define ALIGN_TO(x, align)     (((x) + ((align)-1)) & ~((align)-1))
35#define LIKELY( exp )       (__builtin_expect( (exp) != 0, true  ))
36#define UNLIKELY( exp )     (__builtin_expect( (exp) != 0, false ))
37#define MAX_NUM_APP_LAYERS 32
38
39//Fwrd decls
40struct hwc_context_t;
41
42namespace ovutils = overlay::utils;
43
44namespace overlay {
45class Overlay;
46class Rotator;
47class RotMgr;
48}
49
50namespace qhwc {
51//fwrd decl
52class QueuedBufferStore;
53class ExternalDisplay;
54class VirtualDisplay;
55class IFBUpdate;
56class IVideoOverlay;
57class MDPComp;
58class CopyBit;
59class HwcDebug;
60class AssertiveDisplay;
61class VPUClient;
62class HWCVirtualBase;
63
64
65struct MDPInfo {
66    int version;
67    char panel;
68    bool hasOverlay;
69};
70
71struct DisplayAttributes {
72    uint32_t vsync_period; //nanos
73    uint32_t xres;
74    uint32_t yres;
75    uint32_t stride;
76    float xdpi;
77    float ydpi;
78    int fd;
79    bool connected; //Applies only to pluggable disp.
80    //Connected does not mean it ready to use.
81    //It should be active also. (UNBLANKED)
82    bool isActive;
83    // In pause state, composition is bypassed
84    // used for WFD displays only
85    bool isPause;
86    // To trigger padding round to clean up mdp
87    // pipes
88    bool isConfiguring;
89    // External Display is in MDP Downscale mode indicator
90    bool mDownScaleMode;
91    // Ext dst Rect
92    hwc_rect_t mDstRect;
93    //Action safe attributes
94    // Flag to indicate the presence of action safe dimensions for external
95    bool mActionSafePresent;
96    int mAsWidthRatio;
97    int mAsHeightRatio;
98};
99
100struct ListStats {
101    int numAppLayers; //Total - 1, excluding FB layer.
102    int skipCount;
103    int fbLayerIndex; //Always last for now. = numAppLayers
104    //Video specific
105    int yuvCount;
106    int yuvIndices[MAX_NUM_APP_LAYERS];
107    int extOnlyLayerIndex;
108    bool preMultipliedAlpha;
109    int yuv4k2kIndices[MAX_NUM_APP_LAYERS];
110    int yuv4k2kCount;
111    // Notifies hwcomposer about the start and end of animation
112    // This will be set to true during animation, otherwise false.
113    bool isDisplayAnimating;
114    ovutils::Dim roi;
115    bool secureUI; // Secure display layer
116    bool isSecurePresent;
117};
118
119struct LayerProp {
120    uint32_t mFlags; //qcom specific layer flags
121    LayerProp():mFlags(0){};
122};
123
124struct VsyncState {
125    bool enable;
126    bool fakevsync;
127};
128
129struct BwcPM {
130    static void setBwc(hwc_context_t *ctx, const hwc_rect_t& crop,
131            const hwc_rect_t& dst, const int& transform,
132            ovutils::eMdpFlags& mdpFlags);
133};
134
135// LayerProp::flag values
136enum {
137    HWC_MDPCOMP = 0x00000001,
138    HWC_COPYBIT = 0x00000002,
139};
140
141// HAL specific features
142enum {
143    HWC_COLOR_FILL = 0x00000008,
144    HWC_FORMAT_RB_SWAP = 0x00000040,
145    HWC_VPU_PIPE = 0x00000200,
146};
147
148class LayerRotMap {
149public:
150    LayerRotMap() { reset(); }
151    enum { MAX_SESS = 3 };
152    void add(hwc_layer_1_t* layer, overlay::Rotator *rot);
153    //Resets the mapping of layer to rotator
154    void reset();
155    //Clears mappings and existing rotator fences
156    //Intended to be used during errors
157    void clear();
158    uint32_t getCount() const;
159    hwc_layer_1_t* getLayer(uint32_t index) const;
160    overlay::Rotator* getRot(uint32_t index) const;
161    void setReleaseFd(const int& fence);
162private:
163    hwc_layer_1_t* mLayer[MAX_SESS];
164    overlay::Rotator* mRot[MAX_SESS];
165    uint32_t mCount;
166};
167
168inline uint32_t LayerRotMap::getCount() const {
169    return mCount;
170}
171
172inline hwc_layer_1_t* LayerRotMap::getLayer(uint32_t index) const {
173    if(index >= mCount) return NULL;
174    return mLayer[index];
175}
176
177inline overlay::Rotator* LayerRotMap::getRot(uint32_t index) const {
178    if(index >= mCount) return NULL;
179    return mRot[index];
180}
181
182inline hwc_rect_t integerizeSourceCrop(const hwc_frect_t& cropF) {
183    hwc_rect_t cropI = {0};
184    cropI.left = int(ceilf(cropF.left));
185    cropI.top = int(ceilf(cropF.top));
186    cropI.right = int(floorf(cropF.right));
187    cropI.bottom = int(floorf(cropF.bottom));
188    return cropI;
189}
190
191inline bool isNonIntegralSourceCrop(const hwc_frect_t& cropF) {
192    if(cropF.left - roundf(cropF.left)     ||
193       cropF.top - roundf(cropF.top)       ||
194       cropF.right - roundf(cropF.right)   ||
195       cropF.bottom - roundf(cropF.bottom))
196        return true;
197    else
198        return false;
199}
200
201// -----------------------------------------------------------------------------
202// Utility functions - implemented in hwc_utils.cpp
203void dumpLayer(hwc_layer_1_t const* l);
204void setListStats(hwc_context_t *ctx, hwc_display_contents_1_t *list,
205        int dpy);
206void initContext(hwc_context_t *ctx);
207void closeContext(hwc_context_t *ctx);
208//Crops source buffer against destination and FB boundaries
209void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
210                         const hwc_rect_t& scissor, int orient);
211void getNonWormholeRegion(hwc_display_contents_1_t* list,
212                              hwc_rect_t& nwr);
213bool isSecuring(hwc_context_t* ctx, hwc_layer_1_t const* layer);
214bool isSecureModePolicy(int mdpVersion);
215bool isExternalActive(hwc_context_t* ctx);
216bool isAlphaScaled(hwc_layer_1_t const* layer);
217bool needsScaling(hwc_layer_1_t const* layer);
218bool isDownscaleRequired(hwc_layer_1_t const* layer);
219bool needsScalingWithSplit(hwc_context_t* ctx, hwc_layer_1_t const* layer,
220                           const int& dpy);
221void sanitizeSourceCrop(hwc_rect_t& cropL, hwc_rect_t& cropR,
222                        private_handle_t *hnd);
223bool isAlphaPresent(hwc_layer_1_t const* layer);
224int hwc_vsync_control(hwc_context_t* ctx, int dpy, int enable);
225int getBlending(int blending);
226bool isGLESOnlyComp(hwc_context_t *ctx, const int& dpy);
227void reset_layer_prop(hwc_context_t* ctx, int dpy, int numAppLayers);
228
229//Helper function to dump logs
230void dumpsys_log(android::String8& buf, const char* fmt, ...);
231
232int getExtOrientation(hwc_context_t* ctx);
233bool isValidRect(const hwc_rect_t& rect);
234hwc_rect_t deductRect(const hwc_rect_t& rect1, const hwc_rect_t& rect2);
235hwc_rect_t getIntersection(const hwc_rect_t& rect1, const hwc_rect_t& rect2);
236hwc_rect_t getUnion(const hwc_rect_t& rect1, const hwc_rect_t& rect2);
237void optimizeLayerRects(hwc_context_t *ctx,
238        const hwc_display_contents_1_t *list, const int& dpy);
239bool areLayersIntersecting(const hwc_layer_1_t* layer1,
240        const hwc_layer_1_t* layer2);
241
242// returns true if Action safe dimensions are set and target supports Actionsafe
243bool isActionSafePresent(hwc_context_t *ctx, int dpy);
244
245/* Calculates the destination position based on the action safe rectangle */
246void getActionSafePosition(hwc_context_t *ctx, int dpy, hwc_rect_t& dst);
247
248void getAspectRatioPosition(int destWidth, int destHeight, int srcWidth,
249                                int srcHeight, hwc_rect_t& rect);
250
251void getAspectRatioPosition(hwc_context_t* ctx, int dpy, int extOrientation,
252                                hwc_rect_t& inRect, hwc_rect_t& outRect);
253
254bool isPrimaryPortrait(hwc_context_t *ctx);
255
256bool isOrientationPortrait(hwc_context_t *ctx);
257
258void calcExtDisplayPosition(hwc_context_t *ctx,
259                               private_handle_t *hnd,
260                               int dpy,
261                               hwc_rect_t& sourceCrop,
262                               hwc_rect_t& displayFrame,
263                               int& transform,
264                               ovutils::eTransform& orient);
265
266// Returns the orientation that needs to be set on external for
267// BufferMirrirMode(Sidesync)
268int getMirrorModeOrientation(hwc_context_t *ctx);
269
270// Handles wfd Pause and resume events
271void handle_pause(hwc_context_t *ctx, int dpy);
272void handle_resume(hwc_context_t *ctx, int dpy);
273
274//Close acquireFenceFds of all layers of incoming list
275void closeAcquireFds(hwc_display_contents_1_t* list);
276
277//Sync point impl.
278int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy,
279        int fd);
280
281//Sets appropriate mdp flags for a layer.
282void setMdpFlags(hwc_layer_1_t *layer,
283        ovutils::eMdpFlags &mdpFlags,
284        int rotDownscale, int transform);
285
286int configRotator(overlay::Rotator *rot, ovutils::Whf& whf,
287        hwc_rect_t& crop, const ovutils::eMdpFlags& mdpFlags,
288        const ovutils::eTransform& orient, const int& downscale);
289
290int configMdp(overlay::Overlay *ov, const ovutils::PipeArgs& parg,
291        const ovutils::eTransform& orient, const hwc_rect_t& crop,
292        const hwc_rect_t& pos, const MetaData_t *metadata,
293        const ovutils::eDest& dest);
294
295int configColorLayer(hwc_context_t *ctx, hwc_layer_1_t *layer, const int& dpy,
296        ovutils::eMdpFlags& mdpFlags, ovutils::eZorder& z,
297        ovutils::eIsFg& isFg, const ovutils::eDest& dest);
298
299void updateSource(ovutils::eTransform& orient, ovutils::Whf& whf,
300        hwc_rect_t& crop);
301
302//Routine to configure low resolution panels (<= 2048 width)
303int configureNonSplit(hwc_context_t *ctx, hwc_layer_1_t *layer, const int& dpy,
304        ovutils::eMdpFlags& mdpFlags, ovutils::eZorder& z,
305        ovutils::eIsFg& isFg, const ovutils::eDest& dest,
306        overlay::Rotator **rot);
307
308//Routine to configure high resolution panels (> 2048 width)
309int configureSplit(hwc_context_t *ctx, hwc_layer_1_t *layer, const int& dpy,
310        ovutils::eMdpFlags& mdpFlags, ovutils::eZorder& z,
311        ovutils::eIsFg& isFg, const ovutils::eDest& lDest,
312        const ovutils::eDest& rDest, overlay::Rotator **rot);
313
314//Routine to split and configure high resolution YUV layer (> 2048 width)
315int configureSourceSplit(hwc_context_t *ctx, hwc_layer_1_t *layer,
316        const int& dpy,
317        ovutils::eMdpFlags& mdpFlags, ovutils::eZorder& z,
318        ovutils::eIsFg& isFg, const ovutils::eDest& lDest,
319        const ovutils::eDest& rDest, overlay::Rotator **rot);
320
321//On certain targets DMA pipes are used for rotation and they won't be available
322//for line operations. On a per-target basis we can restrict certain use cases
323//from using rotator, since we know before-hand that such scenarios can lead to
324//extreme unavailability of pipes. This can also be done via hybrid calculations
325//also involving many more variables like number of write-back interfaces etc,
326//but the variety of scenarios is too high to warrant that.
327bool canUseRotator(hwc_context_t *ctx, int dpy);
328
329int getLeftSplit(hwc_context_t *ctx, const int& dpy);
330
331bool isDisplaySplit(hwc_context_t* ctx, int dpy);
332
333// Inline utility functions
334static inline bool isSkipLayer(const hwc_layer_1_t* l) {
335    return (UNLIKELY(l && (l->flags & HWC_SKIP_LAYER)));
336}
337
338// Returns true if the buffer is yuv
339static inline bool isYuvBuffer(const private_handle_t* hnd) {
340    return (hnd && (hnd->bufferType == BUFFER_TYPE_VIDEO));
341}
342
343// Returns true if the buffer is yuv
344static inline bool is4kx2kYuvBuffer(const private_handle_t* hnd) {
345    return (hnd && (hnd->bufferType == BUFFER_TYPE_VIDEO) &&
346            (hnd->width > 2048));
347}
348
349// Returns true if the buffer is secure
350static inline bool isSecureBuffer(const private_handle_t* hnd) {
351    return (hnd && (private_handle_t::PRIV_FLAGS_SECURE_BUFFER & hnd->flags));
352}
353
354static inline bool isTileRendered(const private_handle_t* hnd) {
355    return (hnd && (private_handle_t::PRIV_FLAGS_TILE_RENDERED & hnd->flags));
356}
357
358//Return true if buffer is marked locked
359static inline bool isBufferLocked(const private_handle_t* hnd) {
360    return (hnd && (private_handle_t::PRIV_FLAGS_HWC_LOCK & hnd->flags));
361}
362
363//Return true if buffer is for external display only
364static inline bool isExtOnly(const private_handle_t* hnd) {
365    return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY));
366}
367
368//Return true if buffer is for external display only with a BLOCK flag.
369static inline bool isExtBlock(const private_handle_t* hnd) {
370    return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_BLOCK));
371}
372
373//Return true if buffer is for external display only with a Close Caption flag.
374static inline bool isExtCC(const private_handle_t* hnd) {
375    return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_CC));
376}
377
378//Return true if the buffer is intended for Secure Display
379static inline bool isSecureDisplayBuffer(const private_handle_t* hnd) {
380    return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_DISPLAY));
381}
382
383static inline int getWidth(const private_handle_t* hnd) {
384    if(isYuvBuffer(hnd)) {
385        MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
386        if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
387            return metadata->bufferDim.sliceWidth;
388        }
389    }
390    return hnd->width;
391}
392
393static inline int getHeight(const private_handle_t* hnd) {
394    if(isYuvBuffer(hnd)) {
395        MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
396        if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
397            return metadata->bufferDim.sliceHeight;
398        }
399    }
400    return hnd->height;
401}
402
403template<typename T> inline T max(T a, T b) { return (a > b) ? a : b; }
404template<typename T> inline T min(T a, T b) { return (a < b) ? a : b; }
405
406// Initialize uevent thread
407void init_uevent_thread(hwc_context_t* ctx);
408// Initialize vsync thread
409void init_vsync_thread(hwc_context_t* ctx);
410
411inline void getLayerResolution(const hwc_layer_1_t* layer,
412                               int& width, int& height) {
413    hwc_rect_t displayFrame  = layer->displayFrame;
414    width = displayFrame.right - displayFrame.left;
415    height = displayFrame.bottom - displayFrame.top;
416}
417
418static inline int openFb(int dpy) {
419    int fd = -1;
420    const char *devtmpl = "/dev/graphics/fb%u";
421    char name[64] = {0};
422    snprintf(name, 64, devtmpl, dpy);
423    fd = open(name, O_RDWR);
424    return fd;
425}
426
427template <class T>
428inline void swap(T& a, T& b) {
429    T tmp = a;
430    a = b;
431    b = tmp;
432}
433
434}; //qhwc namespace
435
436enum eAnimationState{
437    ANIMATION_STOPPED,
438    ANIMATION_STARTED,
439};
440
441// -----------------------------------------------------------------------------
442// HWC context
443// This structure contains overall state
444struct hwc_context_t {
445    hwc_composer_device_1_t device;
446    const hwc_procs_t* proc;
447
448    //CopyBit objects
449    qhwc::CopyBit *mCopyBit[HWC_NUM_DISPLAY_TYPES];
450
451    //Overlay object - NULL for non overlay devices
452    overlay::Overlay *mOverlay;
453    //Holds a few rot objects
454    overlay::RotMgr *mRotMgr;
455
456    //Primary and external FB updater
457    qhwc::IFBUpdate *mFBUpdate[HWC_NUM_DISPLAY_TYPES];
458    // External display related information
459    qhwc::ExternalDisplay *mExtDisplay;
460    qhwc::VirtualDisplay *mVirtualDisplay;
461    qhwc::MDPInfo mMDP;
462    qhwc::VsyncState vstate;
463    qhwc::DisplayAttributes dpyAttr[HWC_NUM_DISPLAY_TYPES];
464    qhwc::ListStats listStats[HWC_NUM_DISPLAY_TYPES];
465    qhwc::LayerProp *layerProp[HWC_NUM_DISPLAY_TYPES];
466    qhwc::MDPComp *mMDPComp[HWC_NUM_DISPLAY_TYPES];
467    qhwc::HwcDebug *mHwcDebug[HWC_NUM_DISPLAY_TYPES];
468    hwc_rect_t mViewFrame[HWC_NUM_DISPLAY_TYPES];
469    qhwc::AssertiveDisplay *mAD;
470    qhwc::VPUClient *mVPUClient;
471    eAnimationState mAnimationState[HWC_NUM_DISPLAY_TYPES];
472    qhwc::HWCVirtualBase *mHWCVirtual;
473
474    // stores the primary device orientation
475    int deviceOrientation;
476    //Securing in progress indicator
477    bool mSecuring;
478    //WFD on proprietary stack
479    bool mVirtualonExtActive;
480    //Display in secure mode indicator
481    bool mSecureMode;
482    //Lock to protect drawing data structures
483    mutable Locker mDrawLock;
484    //Drawing round when we use GPU
485    bool isPaddingRound;
486    // External Orientation
487    int mExtOrientation;
488    //Flags the transition of a video session
489    bool mVideoTransFlag;
490
491    //Used for SideSync feature
492    //which overrides the mExtOrientation
493    bool mBufferMirrorMode;
494
495    qhwc::LayerRotMap *mLayerRotMap[HWC_NUM_DISPLAY_TYPES];
496
497    // Panel reset flag will be set if BTA check fails
498    bool mPanelResetStatus;
499};
500
501namespace qhwc {
502static inline bool isSkipPresent (hwc_context_t *ctx, int dpy) {
503    return  ctx->listStats[dpy].skipCount;
504}
505
506static inline bool isYuvPresent (hwc_context_t *ctx, int dpy) {
507    return  ctx->listStats[dpy].yuvCount;
508}
509
510static inline bool has90Transform(hwc_layer_1_t *layer) {
511    return ((layer->transform & HWC_TRANSFORM_ROT_90) &&
512            !(layer->flags & HWC_COLOR_FILL));
513}
514
515inline bool isSecurePresent(hwc_context_t *ctx, int dpy) {
516    return ctx->listStats[dpy].isSecurePresent;
517}
518
519static inline bool isSecondaryConfiguring(hwc_context_t* ctx) {
520    return (ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isConfiguring ||
521            ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isConfiguring);
522}
523
524static inline bool isSecondaryConnected(hwc_context_t* ctx) {
525    return (ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].connected ||
526            ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].connected);
527}
528
529};
530
531#endif //HWC_UTILS_H
532