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