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