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