hwc_utils.h revision c8d563c4df56d25bf6ac6f38fb6703150458737a
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
31#define ALIGN_TO(x, align)     (((x) + ((align)-1)) & ~((align)-1))
32#define LIKELY( exp )       (__builtin_expect( (exp) != 0, true  ))
33#define UNLIKELY( exp )     (__builtin_expect( (exp) != 0, false ))
34#define FINAL_TRANSFORM_MASK 0x000F
35#define MAX_NUM_DISPLAYS 4 //Yes, this is ambitious
36#define MAX_NUM_LAYERS 32
37#define MAX_DISPLAY_DIM 2048
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;
45struct framebuffer_device_t;
46
47namespace overlay {
48class Overlay;
49}
50
51namespace qhwc {
52//fwrd decl
53class QueuedBufferStore;
54class ExternalDisplay;
55class IFBUpdate;
56class MDPComp;
57class CopyBit;
58
59struct MDPInfo {
60    int version;
61    char panel;
62    bool hasOverlay;
63};
64
65struct DisplayAttributes {
66    uint32_t vsync_period; //nanos
67    uint32_t xres;
68    uint32_t yres;
69    uint32_t stride;
70    float xdpi;
71    float ydpi;
72    int fd;
73    bool connected; //Applies only to pluggable disp.
74    //Connected does not mean it ready to use.
75    //It should be active also. (UNBLANKED)
76    bool isActive;
77    // In pause state, composition is bypassed
78    // used for WFD displays only
79    bool isPause;
80};
81
82struct ListStats {
83    int numAppLayers; //Total - 1, excluding FB layer.
84    int skipCount;
85    int fbLayerIndex; //Always last for now. = numAppLayers
86    //Video specific
87    int yuvCount;
88    int yuvIndices[MAX_NUM_LAYERS];
89    bool needsAlphaScale;
90};
91
92struct LayerProp {
93    uint32_t mFlags; //qcom specific layer flags
94    LayerProp():mFlags(0) {};
95};
96
97// LayerProp::flag values
98enum {
99    HWC_MDPCOMP = 0x00000001,
100};
101
102class LayerCache {
103    public:
104    LayerCache() {
105        canUseLayerCache = false;
106        numHwLayers = 0;
107        for(uint32_t i = 0; i < MAX_NUM_LAYERS; i++) {
108            hnd[i] = NULL;
109        }
110    }
111    //LayerCache optimization
112    void updateLayerCache(hwc_display_contents_1_t* list);
113    void resetLayerCache(int num);
114    void markCachedLayersAsOverlay(hwc_display_contents_1_t* list);
115    private:
116    uint32_t numHwLayers;
117    bool canUseLayerCache;
118    buffer_handle_t hnd[MAX_NUM_LAYERS];
119
120};
121
122
123
124
125// -----------------------------------------------------------------------------
126// Utility functions - implemented in hwc_utils.cpp
127void dumpLayer(hwc_layer_1_t const* l);
128void setListStats(hwc_context_t *ctx, const hwc_display_contents_1_t *list,
129        int dpy);
130void initContext(hwc_context_t *ctx);
131void closeContext(hwc_context_t *ctx);
132//Crops source buffer against destination and FB boundaries
133void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
134                         const hwc_rect_t& scissor, int orient);
135bool isSecuring(hwc_context_t* ctx);
136bool isSecureModePolicy(int mdpVersion);
137bool isExternalActive(hwc_context_t* ctx);
138bool needsScaling(hwc_layer_1_t const* layer);
139
140//Helper function to dump logs
141void dumpsys_log(android::String8& buf, const char* fmt, ...);
142
143/* Calculates the destination position based on the action safe rectangle */
144void getActionSafePosition(hwc_context_t *ctx, int dpy, uint32_t& x,
145                                        uint32_t& y, uint32_t& w, uint32_t& h);
146
147//Sync point impl.
148int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy,
149                                                    int fd);
150
151// Inline utility functions
152static inline bool isSkipLayer(const hwc_layer_1_t* l) {
153    return (UNLIKELY(l && (l->flags & HWC_SKIP_LAYER)));
154}
155
156// Returns true if the buffer is yuv
157static inline bool isYuvBuffer(const private_handle_t* hnd) {
158    return (hnd && (hnd->bufferType == BUFFER_TYPE_VIDEO));
159}
160
161// Returns true if the buffer is secure
162static inline bool isSecureBuffer(const private_handle_t* hnd) {
163    return (hnd && (private_handle_t::PRIV_FLAGS_SECURE_BUFFER & hnd->flags));
164}
165//Return true if buffer is marked locked
166static inline bool isBufferLocked(const private_handle_t* hnd) {
167    return (hnd && (private_handle_t::PRIV_FLAGS_HWC_LOCK & hnd->flags));
168}
169
170//Return true if buffer is for external display only
171static inline bool isExtOnly(const private_handle_t* hnd) {
172    return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY));
173}
174
175//Return true if buffer is for external display only with a BLOCK flag.
176static inline bool isExtBlock(const private_handle_t* hnd) {
177    return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_BLOCK));
178}
179
180//Return true if buffer is for external display only with a Close Caption flag.
181static inline bool isExtCC(const private_handle_t* hnd) {
182    return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_CC));
183}
184
185// Initialize uevent thread
186void init_uevent_thread(hwc_context_t* ctx);
187// Initialize vsync thread
188void init_vsync_thread(hwc_context_t* ctx);
189
190inline void getLayerResolution(const hwc_layer_1_t* layer,
191                               int& width, int& height)
192{
193    hwc_rect_t displayFrame  = layer->displayFrame;
194    width = displayFrame.right - displayFrame.left;
195    height = displayFrame.bottom - displayFrame.top;
196}
197
198static inline int openFb(int dpy) {
199    int fd = -1;
200    const char *devtmpl = "/dev/graphics/fb%u";
201    char name[64] = {0};
202    snprintf(name, 64, devtmpl, dpy);
203    fd = open(name, O_RDWR);
204    return fd;
205}
206
207template <class T>
208inline void swap(T& a, T& b) {
209    T tmp = a;
210    a = b;
211    b = tmp;
212}
213
214}; //qhwc namespace
215
216struct vsync_state {
217    pthread_mutex_t lock;
218    pthread_cond_t  cond;
219    bool enable;
220};
221
222// -----------------------------------------------------------------------------
223// HWC context
224// This structure contains overall state
225struct hwc_context_t {
226    hwc_composer_device_1_t device;
227    const hwc_procs_t* proc;
228    //Framebuffer device
229    framebuffer_device_t *mFbDev;
230
231    //CopyBit objects
232    qhwc::CopyBit *mCopyBit[MAX_DISPLAYS];
233
234    //Overlay object - NULL for non overlay devices
235    overlay::Overlay *mOverlay;
236
237    //Primary and external FB updater
238    qhwc::IFBUpdate *mFBUpdate[MAX_DISPLAYS];
239    // External display related information
240    qhwc::ExternalDisplay *mExtDisplay;
241    qhwc::MDPInfo mMDP;
242    qhwc::DisplayAttributes dpyAttr[MAX_DISPLAYS];
243    qhwc::ListStats listStats[MAX_DISPLAYS];
244    qhwc::LayerCache *mLayerCache[MAX_DISPLAYS];
245    qhwc::LayerProp *layerProp[MAX_DISPLAYS];
246    qhwc::MDPComp *mMDPComp;
247
248    //Securing in progress indicator
249    bool mSecuring;
250    //External Display configuring progress indicator
251    bool mExtDispConfiguring;
252    //Display in secure mode indicator
253    bool mSecureMode;
254    //Lock to prevent set from being called while blanking
255    mutable Locker mBlankLock;
256    //Lock to protect set when detaching external disp
257    mutable Locker mExtSetLock;
258    //Vsync
259    struct vsync_state vstate;
260    //DMA used for rotator
261    bool mDMAInUse;
262};
263
264static inline bool isSkipPresent (hwc_context_t *ctx, int dpy) {
265    return  ctx->listStats[dpy].skipCount;
266}
267
268static inline bool isYuvPresent (hwc_context_t *ctx, int dpy) {
269    return  ctx->listStats[dpy].yuvCount;
270}
271
272#endif //HWC_UTILS_H
273