hwc_utils.h revision ff4f0254be575a264504687c407e0db2fd5573d7
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 * Copyright (C) 2012, The Linux Foundation. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef HWC_UTILS_H
19#define HWC_UTILS_H
20
21#define HWC_REMOVE_DEPRECATED_VERSIONS 1
22#include <fcntl.h>
23#include <hardware/hwcomposer.h>
24#include <gr.h>
25#include <gralloc_priv.h>
26
27#define ALIGN_TO(x, align)     (((x) + ((align)-1)) & ~((align)-1))
28#define LIKELY( exp )       (__builtin_expect( (exp) != 0, true  ))
29#define UNLIKELY( exp )     (__builtin_expect( (exp) != 0, false ))
30#define FINAL_TRANSFORM_MASK 0x000F
31#define MAX_NUM_DISPLAYS 4 //Yes, this is ambitious
32
33//Fwrd decls
34struct hwc_context_t;
35struct framebuffer_device_t;
36
37namespace qService {
38class QService;
39}
40
41namespace overlay {
42class Overlay;
43}
44
45namespace qhwc {
46//fwrd decl
47class QueuedBufferStore;
48class ExternalDisplay;
49
50struct MDPInfo {
51    int version;
52    char panel;
53    bool hasOverlay;
54};
55
56struct DisplayAttributes {
57    uint32_t vsync_period; //nanos
58    uint32_t xres;
59    uint32_t yres;
60    float xdpi;
61    float ydpi;
62    int fd;
63    bool isActive;
64};
65
66struct ListStats {
67    int numAppLayers; //Total - 1, excluding FB layer.
68    int skipCount;
69    int fbLayerIndex; //Always last for now. = numAppLayers
70    //Video specific
71    int yuvCount;
72    int yuvIndex;
73};
74
75enum {
76    HWC_MDPCOMP = 0x00000002,
77    HWC_LAYER_RESERVED_0 = 0x00000004,
78    HWC_LAYER_RESERVED_1 = 0x00000008
79};
80
81
82// -----------------------------------------------------------------------------
83// Utility functions - implemented in hwc_utils.cpp
84void dumpLayer(hwc_layer_1_t const* l);
85void setListStats(hwc_context_t *ctx, const hwc_display_contents_1_t *list,
86        int dpy);
87void initContext(hwc_context_t *ctx);
88void closeContext(hwc_context_t *ctx);
89//Crops source buffer against destination and FB boundaries
90void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
91        const int fbWidth, const int fbHeight);
92
93bool isExternalActive(hwc_context_t* ctx);
94
95//Sync point impl.
96int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy);
97
98// Inline utility functions
99static inline bool isSkipLayer(const hwc_layer_1_t* l) {
100    return (UNLIKELY(l && (l->flags & HWC_SKIP_LAYER)));
101}
102
103// Returns true if the buffer is yuv
104static inline bool isYuvBuffer(const private_handle_t* hnd) {
105    return (hnd && (hnd->bufferType == BUFFER_TYPE_VIDEO));
106}
107
108// Returns true if the buffer is secure
109static inline bool isSecureBuffer(const private_handle_t* hnd) {
110    return (hnd && (private_handle_t::PRIV_FLAGS_SECURE_BUFFER & hnd->flags));
111}
112//Return true if buffer is marked locked
113static inline bool isBufferLocked(const private_handle_t* hnd) {
114    return (hnd && (private_handle_t::PRIV_FLAGS_HWC_LOCK & hnd->flags));
115}
116
117//Return true if buffer is for external display only
118static inline bool isExtOnly(const private_handle_t* hnd) {
119    return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY));
120}
121
122//Return true if buffer is for external display only with a BLOCK flag.
123static inline bool isExtBlock(const private_handle_t* hnd) {
124    return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_BLOCK));
125}
126
127//Return true if buffer is for external display only with a Close Caption flag.
128static inline bool isExtCC(const private_handle_t* hnd) {
129    return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_CC));
130}
131
132// Initialize uevent thread
133void init_uevent_thread(hwc_context_t* ctx);
134// Initialize vsync thread
135void init_vsync_thread(hwc_context_t* ctx);
136
137inline void getLayerResolution(const hwc_layer_1_t* layer,
138                               int& width, int& height)
139{
140    hwc_rect_t displayFrame  = layer->displayFrame;
141    width = displayFrame.right - displayFrame.left;
142    height = displayFrame.bottom - displayFrame.top;
143}
144
145static inline int openFb(int dpy) {
146    int fd = -1;
147    const char *devtmpl = "/dev/graphics/fb%u";
148    char name[64] = {0};
149    snprintf(name, 64, devtmpl, dpy);
150    fd = open(name, O_RDWR);
151    return fd;
152}
153
154}; //qhwc namespace
155
156struct vsync_state {
157    pthread_mutex_t lock;
158    pthread_cond_t  cond;
159    bool enable;
160};
161
162// -----------------------------------------------------------------------------
163// HWC context
164// This structure contains overall state
165struct hwc_context_t {
166    hwc_composer_device_1_t device;
167    const hwc_procs_t* proc;
168    int numHwLayers;
169    int overlayInUse;
170
171    //Framebuffer device
172    framebuffer_device_t *mFbDev;
173
174    //Overlay object - NULL for non overlay devices
175    overlay::Overlay *mOverlay;
176
177    //QService object
178    qService::QService *mQService;
179
180    // External display related information
181    qhwc::ExternalDisplay *mExtDisplay;
182
183    qhwc::MDPInfo mMDP;
184
185    qhwc::DisplayAttributes dpyAttr[HWC_NUM_DISPLAY_TYPES];
186
187    qhwc::ListStats listStats[HWC_NUM_DISPLAY_TYPES];
188
189    //Securing in progress indicator
190    bool mSecuring;
191
192    //Display in secure mode indicator
193    bool mSecureMode;
194
195    //Lock to prevent set from being called while blanking
196    mutable Locker mBlankLock;
197    //Vsync
198    struct vsync_state vstate;
199
200};
201
202#endif //HWC_UTILS_H
203