hwc_utils.h revision a5b5aebd86ed595990435692e7e7ede31aeb63dc
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 <linux/fb.h>
32#include "qdMetaData.h"
33#include <overlayUtils.h>
34
35#define ALIGN_TO(x, align)     (((x) + ((align)-1)) & ~((align)-1))
36#define LIKELY( exp )       (__builtin_expect( (exp) != 0, true  ))
37#define UNLIKELY( exp )     (__builtin_expect( (exp) != 0, false ))
38#define MAX_NUM_APP_LAYERS 32
39
40// For support of virtual displays
41#define MAX_DISPLAYS            (HWC_NUM_DISPLAY_TYPES)
42
43//Fwrd decls
44struct hwc_context_t;
45
46namespace ovutils = overlay::utils;
47
48namespace overlay {
49class Overlay;
50class Rotator;
51class RotMgr;
52}
53
54namespace qhwc {
55//fwrd decl
56class QueuedBufferStore;
57class ExternalDisplay;
58class IFBUpdate;
59class IVideoOverlay;
60class MDPComp;
61class CopyBit;
62class AssertiveDisplay;
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 secondary displays
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    //Secondary displays will have this set until they are able to acquire
87    //pipes.
88    bool isConfiguring;
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    bool needsAlphaScale;
99    bool preMultipliedAlpha;
100    bool planeAlpha;
101};
102
103struct LayerProp {
104    uint32_t mFlags; //qcom specific layer flags
105    LayerProp():mFlags(0) {};
106};
107
108struct VsyncState {
109    bool enable;
110    bool fakevsync;
111};
112
113struct BwcPM {
114    static void setBwc(hwc_context_t *ctx, const hwc_rect_t& crop,
115            const hwc_rect_t& dst, const int& transform,
116            ovutils::eMdpFlags& mdpFlags);
117};
118
119// LayerProp::flag values
120enum {
121    HWC_MDPCOMP = 0x00000001,
122    HWC_COPYBIT = 0x00000002,
123};
124
125class LayerRotMap {
126public:
127    LayerRotMap() { reset(); }
128    enum { MAX_SESS = 3 };
129    void add(hwc_layer_1_t* layer, overlay::Rotator *rot);
130    void reset();
131    uint32_t getCount() const;
132    hwc_layer_1_t* getLayer(uint32_t index) const;
133    overlay::Rotator* getRot(uint32_t index) const;
134    void setReleaseFd(const int& fence);
135private:
136    hwc_layer_1_t* mLayer[MAX_SESS];
137    overlay::Rotator* mRot[MAX_SESS];
138    uint32_t mCount;
139};
140
141inline uint32_t LayerRotMap::getCount() const {
142    return mCount;
143}
144
145inline hwc_layer_1_t* LayerRotMap::getLayer(uint32_t index) const {
146    if(index >= mCount) return NULL;
147    return mLayer[index];
148}
149
150inline overlay::Rotator* LayerRotMap::getRot(uint32_t index) const {
151    if(index >= mCount) return NULL;
152    return mRot[index];
153}
154
155inline hwc_rect_t integerizeSourceCrop(const hwc_frect_t& cropF) {
156    hwc_rect_t cropI = {0};
157    cropI.left = int(ceilf(cropF.left));
158    cropI.top = int(ceilf(cropF.top));
159    cropI.right = int(floorf(cropF.right));
160    cropI.bottom = int(floorf(cropF.bottom));
161    return cropI;
162}
163
164// -----------------------------------------------------------------------------
165// Utility functions - implemented in hwc_utils.cpp
166void dumpLayer(hwc_layer_1_t const* l);
167void setListStats(hwc_context_t *ctx, const hwc_display_contents_1_t *list,
168        int dpy);
169void initContext(hwc_context_t *ctx);
170void closeContext(hwc_context_t *ctx);
171//Crops source buffer against destination and FB boundaries
172void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
173                         const hwc_rect_t& scissor, int orient);
174void getNonWormholeRegion(hwc_display_contents_1_t* list,
175                              hwc_rect_t& nwr);
176bool isSecuring(hwc_context_t* ctx, hwc_layer_1_t const* layer);
177bool isSecureModePolicy(int mdpVersion);
178//Secondary display hasnt acquired any pipes yet.
179//Secondary stands for external as well as virtual
180bool isSecondaryConfiguring(hwc_context_t* ctx);
181bool needsScaling(hwc_context_t* ctx, hwc_layer_1_t const* layer, const int& dpy);
182bool isAlphaPresent(hwc_layer_1_t const* layer);
183bool setupBasePipe(hwc_context_t *ctx);
184int hwc_vsync_control(hwc_context_t* ctx, int dpy, int enable);
185int getBlending(int blending);
186
187//Helper function to dump logs
188void dumpsys_log(android::String8& buf, const char* fmt, ...);
189
190/* Calculates the destination position based on the action safe rectangle */
191void getActionSafePosition(hwc_context_t *ctx, int dpy, uint32_t& x,
192                                        uint32_t& y, uint32_t& w, uint32_t& h);
193
194//Close acquireFenceFds of all layers of incoming list
195void closeAcquireFds(hwc_display_contents_1_t* list);
196
197//Sync point impl.
198int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy,
199        int fd);
200
201//Trims a layer's source crop which is outside of screen boundary.
202void trimLayer(hwc_context_t *ctx, const int& dpy, const int& transform,
203        hwc_rect_t& crop, hwc_rect_t& dst);
204
205//Sets appropriate mdp flags for a layer.
206void setMdpFlags(hwc_layer_1_t *layer,
207        ovutils::eMdpFlags &mdpFlags,
208        int rotDownscale);
209
210int configRotator(overlay::Rotator *rot, const ovutils::Whf& whf,
211        hwc_rect_t& crop, const ovutils::eMdpFlags& mdpFlags,
212        const ovutils::eTransform& orient, const int& downscale);
213
214int configMdp(overlay::Overlay *ov, const ovutils::PipeArgs& parg,
215        const ovutils::eTransform& orient, const hwc_rect_t& crop,
216        const hwc_rect_t& pos, const MetaData_t *metadata,
217        const ovutils::eDest& dest);
218
219void updateSource(ovutils::eTransform& orient, ovutils::Whf& whf,
220        hwc_rect_t& crop);
221
222//Routine to configure low resolution panels (<= 2048 width)
223int configureLowRes(hwc_context_t *ctx, hwc_layer_1_t *layer, const int& dpy,
224        ovutils::eMdpFlags& mdpFlags, const ovutils::eZorder& z,
225        const ovutils::eIsFg& isFg, const ovutils::eDest& dest,
226        overlay::Rotator **rot);
227
228//Routine to configure high resolution panels (> 2048 width)
229int configureHighRes(hwc_context_t *ctx, hwc_layer_1_t *layer, const int& dpy,
230        ovutils::eMdpFlags& mdpFlags, const ovutils::eZorder& z,
231        const ovutils::eIsFg& isFg, const ovutils::eDest& lDest,
232        const ovutils::eDest& rDest, overlay::Rotator **rot);
233
234//On certain targets DMA pipes are used for rotation and they won't be available
235//for line operations. On a per-target basis we can restrict certain use cases
236//from using rotator, since we know before-hand that such scenarios can lead to
237//extreme unavailability of pipes. This can also be done via hybrid calculations
238//also involving many more variables like number of write-back interfaces etc,
239//but the variety of scenarios is too high to warrant that.
240bool canUseRotator(hwc_context_t *ctx);
241
242int getLeftSplit(hwc_context_t *ctx, const int& dpy);
243
244//Sets up composition objects for secondary displays when they are added.
245//Should be called with extlock held.
246void setupSecondaryObjs(hwc_context_t *ctx, const int& dpy);
247void clearSecondaryObjs(hwc_context_t *ctx, const int& dpy);
248
249// Inline utility functions
250static inline bool isSkipLayer(const hwc_layer_1_t* l) {
251    return (UNLIKELY(l && (l->flags & HWC_SKIP_LAYER)));
252}
253
254// Returns true if the buffer is yuv
255static inline bool isYuvBuffer(const private_handle_t* hnd) {
256    return (hnd && (hnd->bufferType == BUFFER_TYPE_VIDEO));
257}
258
259// Returns true if the buffer is secure
260static inline bool isSecureBuffer(const private_handle_t* hnd) {
261    return (hnd && (private_handle_t::PRIV_FLAGS_SECURE_BUFFER & hnd->flags));
262}
263//Return true if buffer is marked locked
264static inline bool isBufferLocked(const private_handle_t* hnd) {
265    return (hnd && (private_handle_t::PRIV_FLAGS_HWC_LOCK & hnd->flags));
266}
267
268//Return true if buffer is for external display only
269static inline bool isExtOnly(const private_handle_t* hnd) {
270    return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY));
271}
272
273//Return true if buffer is for external display only with a BLOCK flag.
274static inline bool isExtBlock(const private_handle_t* hnd) {
275    return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_BLOCK));
276}
277
278//Return true if buffer is for external display only with a Close Caption flag.
279static inline bool isExtCC(const private_handle_t* hnd) {
280    return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_CC));
281}
282
283template<typename T> inline T max(T a, T b) { return (a > b) ? a : b; }
284template<typename T> inline T min(T a, T b) { return (a < b) ? a : b; }
285
286// Initialize uevent thread
287void init_uevent_thread(hwc_context_t* ctx);
288// Initialize vsync thread
289void init_vsync_thread(hwc_context_t* ctx);
290
291inline void getLayerResolution(const hwc_layer_1_t* layer,
292                               int& width, int& height) {
293    hwc_rect_t displayFrame  = layer->displayFrame;
294    width = displayFrame.right - displayFrame.left;
295    height = displayFrame.bottom - displayFrame.top;
296}
297
298static inline int openFb(int dpy) {
299    int fd = -1;
300    const char *devtmpl = "/dev/graphics/fb%u";
301    char name[64] = {0};
302    snprintf(name, 64, devtmpl, dpy);
303    fd = open(name, O_RDWR);
304    return fd;
305}
306
307template <class T>
308inline void swap(T& a, T& b) {
309    T tmp = a;
310    a = b;
311    b = tmp;
312}
313
314}; //qhwc namespace
315
316// -----------------------------------------------------------------------------
317// HWC context
318// This structure contains overall state
319struct hwc_context_t {
320    hwc_composer_device_1_t device;
321    const hwc_procs_t* proc;
322
323    //CopyBit objects
324    qhwc::CopyBit *mCopyBit[MAX_DISPLAYS];
325
326    //Overlay object - NULL for non overlay devices
327    overlay::Overlay *mOverlay;
328    //Holds a few rot objects
329    overlay::RotMgr *mRotMgr;
330
331    //Primary and external FB updater
332    qhwc::IFBUpdate *mFBUpdate[MAX_DISPLAYS];
333    // External display related information
334    qhwc::ExternalDisplay *mExtDisplay;
335    qhwc::MDPInfo mMDP;
336    qhwc::VsyncState vstate;
337    qhwc::DisplayAttributes dpyAttr[MAX_DISPLAYS];
338    qhwc::ListStats listStats[MAX_DISPLAYS];
339    qhwc::LayerProp *layerProp[MAX_DISPLAYS];
340    qhwc::LayerRotMap *mLayerRotMap[MAX_DISPLAYS];
341    qhwc::MDPComp *mMDPComp[MAX_DISPLAYS];
342    qhwc::AssertiveDisplay *mAD;
343
344    //Securing in progress indicator
345    bool mSecuring;
346    //Display in secure mode indicator
347    bool mSecureMode;
348    //Lock to prevent set from being called while blanking
349    mutable Locker mBlankLock;
350    //Lock to protect prepare & set when detaching external disp
351    mutable Locker mExtLock;
352    //Check if base pipe is set up
353    bool mBasePipeSetup;
354    //Drawing round when we use GPU
355    bool isPaddingRound;
356    //Flags the transition of a video session
357    bool mVideoTransFlag;
358};
359
360namespace qhwc {
361static inline bool isSkipPresent (hwc_context_t *ctx, int dpy) {
362    return  ctx->listStats[dpy].skipCount;
363}
364
365static inline bool isYuvPresent (hwc_context_t *ctx, int dpy) {
366    return  ctx->listStats[dpy].yuvCount;
367}
368};
369
370#endif //HWC_UTILS_H
371