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