hwc_utils.h revision 87838778cdb83d1f8d5c708865714268bc8ea167
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);
183int hwc_vsync_control(hwc_context_t* ctx, int dpy, int enable);
184int getBlending(int blending);
185
186//Helper function to dump logs
187void dumpsys_log(android::String8& buf, const char* fmt, ...);
188
189/* Calculates the destination position based on the action safe rectangle */
190void getActionSafePosition(hwc_context_t *ctx, int dpy, uint32_t& x,
191                                        uint32_t& y, uint32_t& w, uint32_t& h);
192
193//Close acquireFenceFds of all layers of incoming list
194void closeAcquireFds(hwc_display_contents_1_t* list);
195
196//Sync point impl.
197int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy,
198        int fd);
199
200//Trims a layer's source crop which is outside of screen boundary.
201void trimLayer(hwc_context_t *ctx, const int& dpy, const int& transform,
202        hwc_rect_t& crop, hwc_rect_t& dst);
203
204//Sets appropriate mdp flags for a layer.
205void setMdpFlags(hwc_layer_1_t *layer,
206        ovutils::eMdpFlags &mdpFlags,
207        int rotDownscale);
208
209int configRotator(overlay::Rotator *rot, const ovutils::Whf& whf,
210        hwc_rect_t& crop, const ovutils::eMdpFlags& mdpFlags,
211        const ovutils::eTransform& orient, const int& downscale);
212
213int configMdp(overlay::Overlay *ov, const ovutils::PipeArgs& parg,
214        const ovutils::eTransform& orient, const hwc_rect_t& crop,
215        const hwc_rect_t& pos, const MetaData_t *metadata,
216        const ovutils::eDest& dest);
217
218void updateSource(ovutils::eTransform& orient, ovutils::Whf& whf,
219        hwc_rect_t& crop);
220
221//Routine to configure low resolution panels (<= 2048 width)
222int configureLowRes(hwc_context_t *ctx, hwc_layer_1_t *layer, const int& dpy,
223        ovutils::eMdpFlags& mdpFlags, const ovutils::eZorder& z,
224        const ovutils::eIsFg& isFg, const ovutils::eDest& dest,
225        overlay::Rotator **rot);
226
227//Routine to configure high resolution panels (> 2048 width)
228int configureHighRes(hwc_context_t *ctx, hwc_layer_1_t *layer, const int& dpy,
229        ovutils::eMdpFlags& mdpFlags, const ovutils::eZorder& z,
230        const ovutils::eIsFg& isFg, const ovutils::eDest& lDest,
231        const ovutils::eDest& rDest, overlay::Rotator **rot);
232
233//On certain targets DMA pipes are used for rotation and they won't be available
234//for line operations. On a per-target basis we can restrict certain use cases
235//from using rotator, since we know before-hand that such scenarios can lead to
236//extreme unavailability of pipes. This can also be done via hybrid calculations
237//also involving many more variables like number of write-back interfaces etc,
238//but the variety of scenarios is too high to warrant that.
239bool canUseRotator(hwc_context_t *ctx);
240
241int getLeftSplit(hwc_context_t *ctx, const int& dpy);
242
243//Sets up composition objects for secondary displays when they are added.
244//Should be called with extlock held.
245void setupSecondaryObjs(hwc_context_t *ctx, const int& dpy);
246void clearSecondaryObjs(hwc_context_t *ctx, const int& dpy);
247
248// Inline utility functions
249static inline bool isSkipLayer(const hwc_layer_1_t* l) {
250    return (UNLIKELY(l && (l->flags & HWC_SKIP_LAYER)));
251}
252
253// Returns true if the buffer is yuv
254static inline bool isYuvBuffer(const private_handle_t* hnd) {
255    return (hnd && (hnd->bufferType == BUFFER_TYPE_VIDEO));
256}
257
258// Returns true if the buffer is secure
259static inline bool isSecureBuffer(const private_handle_t* hnd) {
260    return (hnd && (private_handle_t::PRIV_FLAGS_SECURE_BUFFER & hnd->flags));
261}
262//Return true if buffer is marked locked
263static inline bool isBufferLocked(const private_handle_t* hnd) {
264    return (hnd && (private_handle_t::PRIV_FLAGS_HWC_LOCK & hnd->flags));
265}
266
267//Return true if buffer is for external display only
268static inline bool isExtOnly(const private_handle_t* hnd) {
269    return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY));
270}
271
272//Return true if buffer is for external display only with a BLOCK flag.
273static inline bool isExtBlock(const private_handle_t* hnd) {
274    return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_BLOCK));
275}
276
277//Return true if buffer is for external display only with a Close Caption flag.
278static inline bool isExtCC(const private_handle_t* hnd) {
279    return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_CC));
280}
281
282static inline int getWidth(const private_handle_t* hnd) {
283    if(isYuvBuffer(hnd)) {
284        MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
285        if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
286            return metadata->bufferDim.sliceWidth;
287        }
288    }
289    return hnd->width;
290}
291
292static inline int getHeight(const private_handle_t* hnd) {
293    if(isYuvBuffer(hnd)) {
294        MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
295        if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
296            return metadata->bufferDim.sliceHeight;
297        }
298    }
299    return hnd->height;
300}
301
302template<typename T> inline T max(T a, T b) { return (a > b) ? a : b; }
303template<typename T> inline T min(T a, T b) { return (a < b) ? a : b; }
304
305// Initialize uevent thread
306void init_uevent_thread(hwc_context_t* ctx);
307// Initialize vsync thread
308void init_vsync_thread(hwc_context_t* ctx);
309
310inline void getLayerResolution(const hwc_layer_1_t* layer,
311                               int& width, int& height) {
312    hwc_rect_t displayFrame  = layer->displayFrame;
313    width = displayFrame.right - displayFrame.left;
314    height = displayFrame.bottom - displayFrame.top;
315}
316
317static inline int openFb(int dpy) {
318    int fd = -1;
319    const char *devtmpl = "/dev/graphics/fb%u";
320    char name[64] = {0};
321    snprintf(name, 64, devtmpl, dpy);
322    fd = open(name, O_RDWR);
323    return fd;
324}
325
326template <class T>
327inline void swap(T& a, T& b) {
328    T tmp = a;
329    a = b;
330    b = tmp;
331}
332
333}; //qhwc namespace
334
335// -----------------------------------------------------------------------------
336// HWC context
337// This structure contains overall state
338struct hwc_context_t {
339    hwc_composer_device_1_t device;
340    const hwc_procs_t* proc;
341
342    //CopyBit objects
343    qhwc::CopyBit *mCopyBit[MAX_DISPLAYS];
344
345    //Overlay object - NULL for non overlay devices
346    overlay::Overlay *mOverlay;
347    //Holds a few rot objects
348    overlay::RotMgr *mRotMgr;
349
350    //Primary and external FB updater
351    qhwc::IFBUpdate *mFBUpdate[MAX_DISPLAYS];
352    // External display related information
353    qhwc::ExternalDisplay *mExtDisplay;
354    qhwc::MDPInfo mMDP;
355    qhwc::VsyncState vstate;
356    qhwc::DisplayAttributes dpyAttr[MAX_DISPLAYS];
357    qhwc::ListStats listStats[MAX_DISPLAYS];
358    qhwc::LayerProp *layerProp[MAX_DISPLAYS];
359    qhwc::LayerRotMap *mLayerRotMap[MAX_DISPLAYS];
360    qhwc::MDPComp *mMDPComp[MAX_DISPLAYS];
361    qhwc::AssertiveDisplay *mAD;
362
363    //Securing in progress indicator
364    bool mSecuring;
365    //Display in secure mode indicator
366    bool mSecureMode;
367    //Lock to prevent set from being called while blanking
368    mutable Locker mBlankLock;
369    //Lock to protect prepare & set when detaching external disp
370    mutable Locker mExtLock;
371    //Drawing round when we use GPU
372    bool isPaddingRound;
373    //Flags the transition of a video session
374    bool mVideoTransFlag;
375};
376
377namespace qhwc {
378static inline bool isSkipPresent (hwc_context_t *ctx, int dpy) {
379    return  ctx->listStats[dpy].skipCount;
380}
381
382static inline bool isYuvPresent (hwc_context_t *ctx, int dpy) {
383    return  ctx->listStats[dpy].yuvCount;
384}
385};
386
387#endif //HWC_UTILS_H
388