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