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 #include <cutils/sockets.h>
34
35 #define ALIGN_TO(x, align) (((x) + ((align)-1)) & ~((align)-1))
36 #define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
37 #define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
38 #define MAX_NUM_APP_LAYERS 32
39 #define MAX_DISPLAY_DIM 2048
40
41 // For support of virtual displays
42 #define MAX_DISPLAYS (HWC_NUM_DISPLAY_TYPES)
43 #define DAEMON_SOCKET "pps"
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_APP_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 struct CablProp {
111 bool enabled;
112 bool start;
113 bool videoOnly;
114 //daemon_socket for connection to pp-daemon
115 int daemon_socket;
116 };
117
118 // LayerProp::flag values
119 enum {
120 HWC_MDPCOMP = 0x00000001,
121 HWC_COPYBIT = 0x00000002,
122 };
123
124 class LayerRotMap {
125 public:
LayerRotMap()126 LayerRotMap() { reset(); }
127 enum { MAX_SESS = 3 };
128 void add(hwc_layer_1_t* layer, overlay::Rotator *rot);
129 void reset();
130 uint32_t getCount() const;
131 hwc_layer_1_t* getLayer(uint32_t index) const;
132 overlay::Rotator* getRot(uint32_t index) const;
133 void setReleaseFd(const int& fence);
134 private:
135 hwc_layer_1_t* mLayer[MAX_SESS];
136 overlay::Rotator* mRot[MAX_SESS];
137 uint32_t mCount;
138 };
139
getCount()140 inline uint32_t LayerRotMap::getCount() const {
141 return mCount;
142 }
143
getLayer(uint32_t index)144 inline hwc_layer_1_t* LayerRotMap::getLayer(uint32_t index) const {
145 if(index >= mCount) return NULL;
146 return mLayer[index];
147 }
148
getRot(uint32_t index)149 inline overlay::Rotator* LayerRotMap::getRot(uint32_t index) const {
150 if(index >= mCount) return NULL;
151 return mRot[index];
152 }
153
154 // -----------------------------------------------------------------------------
155 // Utility functions - implemented in hwc_utils.cpp
156 void dumpLayer(hwc_layer_1_t const* l);
157 void setListStats(hwc_context_t *ctx, const hwc_display_contents_1_t *list,
158 int dpy);
159 void initContext(hwc_context_t *ctx);
160 void closeContext(hwc_context_t *ctx);
161 //Crops source buffer against destination and FB boundaries
162 void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
163 const hwc_rect_t& scissor, int orient);
164 void getNonWormholeRegion(hwc_display_contents_1_t* list,
165 hwc_rect_t& nwr);
166 bool isSecuring(hwc_context_t* ctx, hwc_layer_1_t const* layer);
167 bool isSecureModePolicy(int mdpVersion);
168 bool isExternalActive(hwc_context_t* ctx);
169 bool needsScaling(hwc_layer_1_t const* layer);
170 bool isAlphaPresent(hwc_layer_1_t const* layer);
171 bool setupBasePipe(hwc_context_t *ctx);
172 int hwc_vsync_control(hwc_context_t* ctx, int dpy, int enable);
173 int getBlending(int blending);
174
175 //Helper function to dump logs
176 void dumpsys_log(android::String8& buf, const char* fmt, ...);
177
178 /* Calculates the destination position based on the action safe rectangle */
179 void getActionSafePosition(hwc_context_t *ctx, int dpy, uint32_t& x,
180 uint32_t& y, uint32_t& w, uint32_t& h);
181
182 //Close acquireFenceFds of all layers of incoming list
183 void closeAcquireFds(hwc_display_contents_1_t* list);
184
185 //Sync point impl.
186 int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy,
187 int fd);
188
189 //Trims a layer's source crop which is outside of screen boundary.
190 void trimLayer(hwc_context_t *ctx, const int& dpy, const int& transform,
191 hwc_rect_t& crop, hwc_rect_t& dst);
192
193 //Sets appropriate mdp flags for a layer.
194 void setMdpFlags(hwc_layer_1_t *layer,
195 ovutils::eMdpFlags &mdpFlags,
196 int rotDownscale = 0);
197
198 //Routine to configure low resolution panels (<= 2048 width)
199 int configureLowRes(hwc_context_t *ctx, hwc_layer_1_t *layer, const int& dpy,
200 ovutils::eMdpFlags& mdpFlags, const ovutils::eZorder& z,
201 const ovutils::eIsFg& isFg, const ovutils::eDest& dest,
202 overlay::Rotator **rot);
203
204 //Routine to configure high resolution panels (> 2048 width)
205 int configureHighRes(hwc_context_t *ctx, hwc_layer_1_t *layer, const int& dpy,
206 ovutils::eMdpFlags& mdpFlags, const ovutils::eZorder& z,
207 const ovutils::eIsFg& isFg, const ovutils::eDest& lDest,
208 const ovutils::eDest& rDest, overlay::Rotator **rot);
209
210 // Inline utility functions
isSkipLayer(const hwc_layer_1_t * l)211 static inline bool isSkipLayer(const hwc_layer_1_t* l) {
212 return (UNLIKELY(l && (l->flags & HWC_SKIP_LAYER)));
213 }
214
215 // Returns true if the buffer is yuv
isYuvBuffer(const private_handle_t * hnd)216 static inline bool isYuvBuffer(const private_handle_t* hnd) {
217 return (hnd && (hnd->bufferType == BUFFER_TYPE_VIDEO));
218 }
219
220 // Returns true if the buffer is secure
isSecureBuffer(const private_handle_t * hnd)221 static inline bool isSecureBuffer(const private_handle_t* hnd) {
222 return (hnd && (private_handle_t::PRIV_FLAGS_SECURE_BUFFER & hnd->flags));
223 }
224 //Return true if buffer is marked locked
isBufferLocked(const private_handle_t * hnd)225 static inline bool isBufferLocked(const private_handle_t* hnd) {
226 return (hnd && (private_handle_t::PRIV_FLAGS_HWC_LOCK & hnd->flags));
227 }
228
229 //Return true if buffer is for external display only
isExtOnly(const private_handle_t * hnd)230 static inline bool isExtOnly(const private_handle_t* hnd) {
231 return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY));
232 }
233
234 //Return true if buffer is for external display only with a BLOCK flag.
isExtBlock(const private_handle_t * hnd)235 static inline bool isExtBlock(const private_handle_t* hnd) {
236 return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_BLOCK));
237 }
238
239 //Return true if buffer is for external display only with a Close Caption flag.
isExtCC(const private_handle_t * hnd)240 static inline bool isExtCC(const private_handle_t* hnd) {
241 return (hnd && (hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_CC));
242 }
243
getWidth(const private_handle_t * hnd)244 static inline int getWidth(const private_handle_t* hnd) {
245 if(isYuvBuffer(hnd)) {
246 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
247 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
248 return metadata->bufferDim.sliceWidth;
249 }
250 }
251 return hnd->width;
252 }
253
getHeight(const private_handle_t * hnd)254 static inline int getHeight(const private_handle_t* hnd) {
255 if(isYuvBuffer(hnd)) {
256 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
257 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
258 return metadata->bufferDim.sliceHeight;
259 }
260 }
261 return hnd->height;
262 }
263
max(T a,T b)264 template<typename T> inline T max(T a, T b) { return (a > b) ? a : b; }
min(T a,T b)265 template<typename T> inline T min(T a, T b) { return (a < b) ? a : b; }
266
267 // Initialize uevent thread
268 void init_uevent_thread(hwc_context_t* ctx);
269 // Initialize vsync thread
270 void init_vsync_thread(hwc_context_t* ctx);
271
getLayerResolution(const hwc_layer_1_t * layer,int & width,int & height)272 inline void getLayerResolution(const hwc_layer_1_t* layer,
273 int& width, int& height) {
274 hwc_rect_t displayFrame = layer->displayFrame;
275 width = displayFrame.right - displayFrame.left;
276 height = displayFrame.bottom - displayFrame.top;
277 }
278
openFb(int dpy)279 static inline int openFb(int dpy) {
280 int fd = -1;
281 const char *devtmpl = "/dev/graphics/fb%u";
282 char name[64] = {0};
283 snprintf(name, 64, devtmpl, dpy);
284 fd = open(name, O_RDWR);
285 return fd;
286 }
287
288 template <class T>
swap(T & a,T & b)289 inline void swap(T& a, T& b) {
290 T tmp = a;
291 a = b;
292 b = tmp;
293 }
294
295 }; //qhwc namespace
296
297 // -----------------------------------------------------------------------------
298 // HWC context
299 // This structure contains overall state
300 struct hwc_context_t {
301 hwc_composer_device_1_t device;
302 const hwc_procs_t* proc;
303
304 //CopyBit objects
305 qhwc::CopyBit *mCopyBit[MAX_DISPLAYS];
306
307 //Overlay object - NULL for non overlay devices
308 overlay::Overlay *mOverlay;
309 //Holds a few rot objects
310 overlay::RotMgr *mRotMgr;
311
312 //Primary and external FB updater
313 qhwc::IFBUpdate *mFBUpdate[MAX_DISPLAYS];
314 // External display related information
315 qhwc::ExternalDisplay *mExtDisplay;
316 qhwc::MDPInfo mMDP;
317 qhwc::VsyncState vstate;
318 qhwc::DisplayAttributes dpyAttr[MAX_DISPLAYS];
319 qhwc::ListStats listStats[MAX_DISPLAYS];
320 qhwc::LayerProp *layerProp[MAX_DISPLAYS];
321 qhwc::LayerRotMap *mLayerRotMap[MAX_DISPLAYS];
322 qhwc::MDPComp *mMDPComp[MAX_DISPLAYS];
323 qhwc::CablProp mCablProp;
324 overlay::utils::Whf mPrevWHF[MAX_DISPLAYS];
325
326 //Securing in progress indicator
327 bool mSecuring;
328 //External Display configuring progress indicator
329 bool mExtDispConfiguring;
330 //Display in secure mode indicator
331 bool mSecureMode;
332 //Lock to prevent set from being called while blanking
333 mutable Locker mBlankLock;
334 //Lock to protect set when detaching external disp
335 mutable Locker mExtSetLock;
336 //DMA used for rotator
337 bool mDMAInUse;
338 //MDP rotater needed
339 bool mNeedsRotator;
340 //Check if base pipe is set up
341 bool mBasePipeSetup;
342 //Flags the transition of a video session
343 bool mVideoTransFlag;
344 };
345
346 namespace qhwc {
isSkipPresent(hwc_context_t * ctx,int dpy)347 static inline bool isSkipPresent (hwc_context_t *ctx, int dpy) {
348 return ctx->listStats[dpy].skipCount;
349 }
350
isYuvPresent(hwc_context_t * ctx,int dpy)351 static inline bool isYuvPresent (hwc_context_t *ctx, int dpy) {
352 return ctx->listStats[dpy].yuvCount;
353 }
354 };
355
356 #endif //HWC_UTILS_H
357