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 #define MAX_NUM_LAYERS 32
33
34 //Fwrd decls
35 struct hwc_context_t;
36 struct framebuffer_device_t;
37
38 namespace qService {
39 class QService;
40 }
41
42 namespace overlay {
43 class Overlay;
44 }
45
46 namespace qhwc {
47 //fwrd decl
48 class QueuedBufferStore;
49 class ExternalDisplay;
50
51 struct MDPInfo {
52 int version;
53 char panel;
54 bool hasOverlay;
55 };
56
57 struct DisplayAttributes {
58 uint32_t vsync_period; //nanos
59 uint32_t xres;
60 uint32_t yres;
61 uint32_t stride;
62 float xdpi;
63 float ydpi;
64 int fd;
65 bool connected; //Applies only to pluggable disp.
66 //Connected does not mean it ready to use.
67 //It should be active also. (UNBLANKED)
68 bool isActive;
69 };
70
71 struct ListStats {
72 int numAppLayers; //Total - 1, excluding FB layer.
73 int skipCount;
74 int fbLayerIndex; //Always last for now. = numAppLayers
75 //Video specific
76 int yuvCount;
77 int yuvIndex;
78 bool needsAlphaScale;
79 };
80
81
82 struct LayerProp {
83 uint32_t mFlags; //qcom specific layer flags
LayerPropLayerProp84 LayerProp():mFlags(0) {};
85 };
86
87 // LayerProp::flag values
88 enum {
89 HWC_MDPCOMP = 0x00000001,
90 };
91
92 class LayerCache {
93 public:
LayerCache()94 LayerCache() {
95 canUseLayerCache = false;
96 numHwLayers = 0;
97 for(uint32_t i = 0; i < MAX_NUM_LAYERS; i++) {
98 hnd[i] = NULL;
99 }
100 }
101 //LayerCache optimization
102 void updateLayerCache(hwc_display_contents_1_t* list);
103 void resetLayerCache(int num);
104 void markCachedLayersAsOverlay(hwc_display_contents_1_t* list);
105 private:
106 uint32_t numHwLayers;
107 bool canUseLayerCache;
108 buffer_handle_t hnd[MAX_NUM_LAYERS];
109
110 };
111
112
113
114
115 // -----------------------------------------------------------------------------
116 // Utility functions - implemented in hwc_utils.cpp
117 void dumpLayer(hwc_layer_1_t const* l);
118 void setListStats(hwc_context_t *ctx, const hwc_display_contents_1_t *list,
119 int dpy);
120 void initContext(hwc_context_t *ctx);
121 void closeContext(hwc_context_t *ctx);
122 //Crops source buffer against destination and FB boundaries
123 void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
124 const int fbWidth, const int fbHeight, int orient);
125 bool isSecuring(hwc_context_t* ctx);
126 bool isExternalActive(hwc_context_t* ctx);
127
128 //Sync point impl.
129 int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy);
130
131 // Inline utility functions
isSkipLayer(const hwc_layer_1_t * l)132 static inline bool isSkipLayer(const hwc_layer_1_t* l) {
133 return (UNLIKELY(l && (l->flags & HWC_SKIP_LAYER)));
134 }
135
136 // Returns true if the buffer is yuv
isYuvBuffer(const private_handle_t * hnd)137 static inline bool isYuvBuffer(const private_handle_t* hnd) {
138 return (hnd && (hnd->bufferType == BUFFER_TYPE_VIDEO));
139 }
140
141 // Returns true if the buffer is secure
isSecureBuffer(const private_handle_t * hnd)142 static inline bool isSecureBuffer(const private_handle_t* hnd) {
143 return (hnd && (private_handle_t::PRIV_FLAGS_SECURE_BUFFER & hnd->flags));
144 }
145 //Return true if buffer is marked locked
isBufferLocked(const private_handle_t * hnd)146 static inline bool isBufferLocked(const private_handle_t* hnd) {
147 return (hnd && (private_handle_t::PRIV_FLAGS_HWC_LOCK & hnd->flags));
148 }
149
150 //Return true if buffer is for external display only
isExtOnly(const private_handle_t * hnd)151 static inline bool isExtOnly(const private_handle_t* hnd) {
152 return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY));
153 }
154
155 //Return true if buffer is for external display only with a BLOCK flag.
isExtBlock(const private_handle_t * hnd)156 static inline bool isExtBlock(const private_handle_t* hnd) {
157 return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_BLOCK));
158 }
159
160 //Return true if buffer is for external display only with a Close Caption flag.
isExtCC(const private_handle_t * hnd)161 static inline bool isExtCC(const private_handle_t* hnd) {
162 return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_CC));
163 }
164
165 // Initialize uevent thread
166 void init_uevent_thread(hwc_context_t* ctx);
167 // Initialize vsync thread
168 void init_vsync_thread(hwc_context_t* ctx);
169
getLayerResolution(const hwc_layer_1_t * layer,int & width,int & height)170 inline void getLayerResolution(const hwc_layer_1_t* layer,
171 int& width, int& height)
172 {
173 hwc_rect_t displayFrame = layer->displayFrame;
174 width = displayFrame.right - displayFrame.left;
175 height = displayFrame.bottom - displayFrame.top;
176 }
177
openFb(int dpy)178 static inline int openFb(int dpy) {
179 int fd = -1;
180 const char *devtmpl = "/dev/graphics/fb%u";
181 char name[64] = {0};
182 snprintf(name, 64, devtmpl, dpy);
183 fd = open(name, O_RDWR);
184 return fd;
185 }
186
187 template <class T>
swap(T & a,T & b)188 inline void swap(T& a, T& b) {
189 T tmp = a;
190 a = b;
191 b = tmp;
192 }
193
194 }; //qhwc namespace
195
196 struct vsync_state {
197 pthread_mutex_t lock;
198 pthread_cond_t cond;
199 bool enable;
200 };
201
202 // -----------------------------------------------------------------------------
203 // HWC context
204 // This structure contains overall state
205 struct hwc_context_t {
206 hwc_composer_device_1_t device;
207 const hwc_procs_t* proc;
208 //Framebuffer device
209 framebuffer_device_t *mFbDev;
210 //Overlay object - NULL for non overlay devices
211 overlay::Overlay *mOverlay;
212 //QService object
213 qService::QService *mQService;
214 // External display related information
215 qhwc::ExternalDisplay *mExtDisplay;
216 qhwc::MDPInfo mMDP;
217 qhwc::DisplayAttributes dpyAttr[HWC_NUM_DISPLAY_TYPES];
218 qhwc::ListStats listStats[HWC_NUM_DISPLAY_TYPES];
219 qhwc::LayerCache *mLayerCache[HWC_NUM_DISPLAY_TYPES];
220 qhwc::LayerProp *layerProp[HWC_NUM_DISPLAY_TYPES];
221
222 //Securing in progress indicator
223 bool mSecuring;
224 //Display in secure mode indicator
225 bool mSecureMode;
226 //Lock to prevent set from being called while blanking
227 mutable Locker mBlankLock;
228 //Lock to protect set when detaching external disp
229 mutable Locker mExtSetLock;
230 //Vsync
231 struct vsync_state vstate;
232 };
233
234 #endif //HWC_UTILS_H
235