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