1 /*
2 * Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *    * Redistributions of source code must retain the above copyright
8 *      notice, this list of conditions and the following disclaimer.
9 *    * Redistributions in binary form must reproduce the above
10 *      copyright notice, this list of conditions and the following
11 *      disclaimer in the documentation and/or other materials provided
12 *      with the distribution.
13 *    * Neither the name of The Linux Foundation nor the names of its
14 *      contributors may be used to endorse or promote products derived
15 *      from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #ifndef OVERLAY_UTILS_H
31 #define OVERLAY_UTILS_H
32 
33 #include <cutils/log.h> // ALOGE, etc
34 #include <errno.h>
35 #include <fcntl.h> // open, O_RDWR, etc
36 #include <hardware/hardware.h>
37 #include <hardware/gralloc.h> // buffer_handle_t
38 #include <linux/msm_mdp.h> // flags
39 #include <linux/msm_rotator.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <sys/stat.h>
44 #include <sys/types.h>
45 #include <utils/Log.h>
46 #include "gralloc_priv.h" //for interlace
47 
48 // Older platforms do not support Venus
49 #ifndef VENUS_COLOR_FORMAT
50 #define MDP_Y_CBCR_H2V2_VENUS MDP_IMGTYPE_LIMIT
51 #endif
52 
53 /*
54 *
55 * Collection of utilities functions/structs/enums etc...
56 *
57 * */
58 
59 // comment that out if you want to remove asserts
60 // or put it as -D in Android.mk. your choice.
61 #define OVERLAY_HAS_ASSERT
62 
63 #ifdef OVERLAY_HAS_ASSERT
64 # define OVASSERT(x, ...) if(!(x)) { ALOGE(__VA_ARGS__); abort(); }
65 #else
66 # define OVASSERT(x, ...) ALOGE_IF(!(x), __VA_ARGS__)
67 #endif // OVERLAY_HAS_ASSERT
68 
69 #define DEBUG_OVERLAY 0
70 #define PROFILE_OVERLAY 0
71 
72 #ifndef MDSS_MDP_RIGHT_MIXER
73 #define MDSS_MDP_RIGHT_MIXER 0x100
74 #endif
75 
76 #ifndef MDP_OV_PIPE_FORCE_DMA
77 #define MDP_OV_PIPE_FORCE_DMA 0x4000
78 #endif
79 
80 #ifndef MDSS_MDP_DUAL_PIPE
81 #define MDSS_MDP_DUAL_PIPE 0x200
82 #endif
83 
84 #define FB_DEVICE_TEMPLATE "/dev/graphics/fb%u"
85 
86 namespace overlay {
87 
88 // fwd
89 class Overlay;
90 class OvFD;
91 
92 /* helper function to open by using fbnum */
93 bool open(OvFD& fd, uint32_t fbnum, const char* const dev,
94     int flags = O_RDWR);
95 
96 namespace utils {
97 struct Whf;
98 struct Dim;
99 
setBit(uint32_t x,uint32_t mask)100 inline uint32_t setBit(uint32_t x, uint32_t mask) {
101     return (x | mask);
102 }
103 
clrBit(uint32_t x,uint32_t mask)104 inline uint32_t clrBit(uint32_t x, uint32_t mask) {
105     return (x & ~mask);
106 }
107 
108 /* Utility class to help avoid copying instances by making the copy ctor
109 * and assignment operator private
110 *
111 * Usage:
112 *    class SomeClass : utils::NoCopy {...};
113 */
114 class NoCopy {
115 protected:
NoCopy()116     NoCopy(){}
~NoCopy()117     ~NoCopy() {}
118 private:
119     NoCopy(const NoCopy&);
120     const NoCopy& operator=(const NoCopy&);
121 };
122 
123 bool isMdssRotator();
124 void normalizeCrop(uint32_t& xy, uint32_t& wh);
125 
126 template <class Type>
127 void swapWidthHeight(Type& width, Type& height);
128 
129 struct Dim {
DimDim130     Dim () : x(0), y(0),
131     w(0), h(0),
132     o(0) {}
DimDim133     Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h) :
134         x(_x), y(_y),
135         w(_w), h(_h) {}
DimDim136     Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h, uint32_t _o) :
137         x(_x), y(_y),
138         w(_w), h(_h),
139         o(_o) {}
checkDim140     bool check(uint32_t _w, uint32_t _h) const {
141         return (x+w <= _w && y+h <= _h);
142 
143     }
144 
145     bool operator==(const Dim& d) const {
146         return d.x == x && d.y == y &&
147                 d.w == w && d.h == h &&
148                 d.o == o;
149     }
150 
151     bool operator!=(const Dim& d) const {
152         return !operator==(d);
153     }
154 
155     void dump() const;
156     uint32_t x;
157     uint32_t y;
158     uint32_t w;
159     uint32_t h;
160     uint32_t o;
161 };
162 
163 // TODO have Whfz
164 
165 struct Whf {
WhfWhf166     Whf() : w(0), h(0), format(0), size(0) {}
WhfWhf167     Whf(uint32_t wi, uint32_t he, uint32_t f) :
168         w(wi), h(he), format(f), size(0) {}
WhfWhf169     Whf(uint32_t wi, uint32_t he, uint32_t f, uint32_t s) :
170         w(wi), h(he), format(f), size(s) {}
171     // FIXME not comparing size at the moment
172     bool operator==(const Whf& whf) const {
173         return whf.w == w && whf.h == h &&
174                 whf.format == format;
175     }
176     bool operator!=(const Whf& whf) const {
177         return !operator==(whf);
178     }
179     void dump() const;
180     uint32_t w;
181     uint32_t h;
182     uint32_t format;
183     uint32_t size;
184 };
185 
186 enum { MAX_PATH_LEN = 256 };
187 
188 enum { DEFAULT_PLANE_ALPHA = 0xFF };
189 
190 /**
191  * Rotator flags: not to be confused with orientation flags.
192  * Usually, you want to open the rotator to make sure it is
193  * ready for business.
194  * */
195  enum eRotFlags {
196     ROT_FLAGS_NONE = 0,
197     //Use rotator for 0 rotation. It is used anyway for others.
198     ROT_0_ENABLED = 1 << 0,
199     //Enable rotator downscale optimization for hardware bugs not handled in
200     //driver. If downscale optimizatation is required,
201     //then rotator will be used even if its 0 rotation case.
202     ROT_DOWNSCALE_ENABLED = 1 << 1,
203     ROT_PREROTATED = 1 << 2,
204 };
205 
206 enum eRotDownscale {
207     ROT_DS_NONE = 0,
208     ROT_DS_HALF = 1,
209     ROT_DS_FOURTH = 2,
210     ROT_DS_EIGHTH = 3,
211 };
212 
213 /* The values for is_fg flag for control alpha and transp
214  * IS_FG_OFF means is_fg = 0
215  * IS_FG_SET means is_fg = 1
216  */
217 enum eIsFg {
218     IS_FG_OFF = 0,
219     IS_FG_SET = 1
220 };
221 
222 /*
223  * Various mdp flags like PIPE SHARE, DEINTERLACE etc...
224  * kernel/common/linux/msm_mdp.h
225  * INTERLACE_MASK: hardware/qcom/display/libgralloc/badger/fb_priv.h
226  * */
227 enum eMdpFlags {
228     OV_MDP_FLAGS_NONE = 0,
229     OV_MDP_PIPE_SHARE =  MDP_OV_PIPE_SHARE,
230     OV_MDP_PIPE_FORCE_DMA = MDP_OV_PIPE_FORCE_DMA,
231     OV_MDP_DEINTERLACE = MDP_DEINTERLACE,
232     OV_MDP_SECURE_OVERLAY_SESSION = MDP_SECURE_OVERLAY_SESSION,
233     OV_MDP_SECURE_DISPLAY_OVERLAY_SESSION = MDP_SECURE_DISPLAY_OVERLAY_SESSION,
234     OV_MDP_SOURCE_ROTATED_90 = MDP_SOURCE_ROTATED_90,
235     OV_MDP_BACKEND_COMPOSITION = MDP_BACKEND_COMPOSITION,
236     OV_MDP_BLEND_FG_PREMULT = MDP_BLEND_FG_PREMULT,
237     OV_MDP_FLIP_H = MDP_FLIP_LR,
238     OV_MDP_FLIP_V = MDP_FLIP_UD,
239     OV_MDSS_MDP_RIGHT_MIXER = MDSS_MDP_RIGHT_MIXER,
240     OV_MDP_PP_EN = MDP_OVERLAY_PP_CFG_EN,
241     OV_MDSS_MDP_BWC_EN = MDP_BWC_EN,
242     OV_MDSS_MDP_DUAL_PIPE = MDSS_MDP_DUAL_PIPE,
243     OV_MDP_SOLID_FILL = MDP_SOLID_FILL,
244 };
245 
246 enum eZorder {
247     ZORDER_0 = 0,
248     ZORDER_1,
249     ZORDER_2,
250     ZORDER_3,
251     Z_SYSTEM_ALLOC = 0xFFFF
252 };
253 
254 enum eMdpPipeType {
255     OV_MDP_PIPE_RGB = 0,
256     OV_MDP_PIPE_VG,
257     OV_MDP_PIPE_DMA,
258     OV_MDP_PIPE_ANY, //Any
259 };
260 
261 // Identify destination pipes
262 // TODO Names useless, replace with int and change all interfaces
263 enum eDest {
264     OV_P0 = 0,
265     OV_P1,
266     OV_P2,
267     OV_P3,
268     OV_P4,
269     OV_P5,
270     OV_P6,
271     OV_P7,
272     OV_P8,
273     OV_P9,
274     OV_INVALID,
275     OV_MAX = OV_INVALID,
276 };
277 
278 /* Used when a buffer is split over 2 pipes and sent to display */
279 enum {
280     OV_LEFT_SPLIT = 0,
281     OV_RIGHT_SPLIT,
282 };
283 
284 /* values for copybit_set_parameter(OVERLAY_TRANSFORM) */
285 enum eTransform {
286     /* No rot */
287     OVERLAY_TRANSFORM_0 = 0x0,
288     /* flip source image horizontally 0x1 */
289     OVERLAY_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H,
290     /* flip source image vertically 0x2 */
291     OVERLAY_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
292     /* rotate source image 180 degrees
293      * It is basically bit-or-ed  H | V == 0x3 */
294     OVERLAY_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
295     /* rotate source image 90 degrees 0x4 */
296     OVERLAY_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
297     /* rotate source image 90 degrees and flip horizontally 0x5 */
298     OVERLAY_TRANSFORM_ROT_90_FLIP_H = HAL_TRANSFORM_ROT_90 |
299                                       HAL_TRANSFORM_FLIP_H,
300     /* rotate source image 90 degrees and flip vertically 0x6 */
301     OVERLAY_TRANSFORM_ROT_90_FLIP_V = HAL_TRANSFORM_ROT_90 |
302                                       HAL_TRANSFORM_FLIP_V,
303     /* rotate source image 270 degrees
304      * Basically 180 | 90 == 0x7 */
305     OVERLAY_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
306     /* rotate invalid like in Transform.h */
307     OVERLAY_TRANSFORM_INV = 0x80
308 };
309 
310 enum eBlending {
311     OVERLAY_BLENDING_UNDEFINED = 0x0,
312     /* No blending */
313     OVERLAY_BLENDING_OPAQUE,
314     /* src.rgb + dst.rgb*(1-src_alpha) */
315     OVERLAY_BLENDING_PREMULT,
316     /* src.rgb * src_alpha + dst.rgb (1 - src_alpha) */
317     OVERLAY_BLENDING_COVERAGE,
318 };
319 
320 // Used to consolidate pipe params
321 struct PipeArgs {
PipeArgsPipeArgs322     PipeArgs() : mdpFlags(OV_MDP_FLAGS_NONE),
323         zorder(Z_SYSTEM_ALLOC),
324         isFg(IS_FG_OFF),
325         rotFlags(ROT_FLAGS_NONE),
326         planeAlpha(DEFAULT_PLANE_ALPHA),
327         blending(OVERLAY_BLENDING_COVERAGE){
328     }
329 
330     PipeArgs(eMdpFlags f, Whf _whf,
331             eZorder z, eIsFg fg, eRotFlags r,
332             int pA = DEFAULT_PLANE_ALPHA, eBlending b = OVERLAY_BLENDING_COVERAGE) :
mdpFlagsPipeArgs333         mdpFlags(f),
334         whf(_whf),
335         zorder(z),
336         isFg(fg),
337         rotFlags(r),
338         planeAlpha(pA),
339         blending(b){
340     }
341 
342     eMdpFlags mdpFlags; // for mdp_overlay flags
343     Whf whf;
344     eZorder zorder; // stage number
345     eIsFg isFg; // control alpha & transp
346     eRotFlags rotFlags;
347     int planeAlpha;
348     eBlending blending;
349 };
350 
351 // Cannot use HW_OVERLAY_MAGNIFICATION_LIMIT, since at the time
352 // of integration, HW_OVERLAY_MAGNIFICATION_LIMIT was a define
353 enum { HW_OV_MAGNIFICATION_LIMIT = 20,
354     HW_OV_MINIFICATION_LIMIT  = 8
355 };
356 
setMdpFlags(eMdpFlags & f,eMdpFlags v)357 inline void setMdpFlags(eMdpFlags& f, eMdpFlags v) {
358     f = static_cast<eMdpFlags>(setBit(f, v));
359 }
360 
clearMdpFlags(eMdpFlags & f,eMdpFlags v)361 inline void clearMdpFlags(eMdpFlags& f, eMdpFlags v) {
362     f = static_cast<eMdpFlags>(clrBit(f, v));
363 }
364 
365 enum { FB0, FB1, FB2 };
366 
367 struct ScreenInfo {
ScreenInfoScreenInfo368     ScreenInfo() : mFBWidth(0),
369     mFBHeight(0),
370     mFBbpp(0),
371     mFBystride(0) {}
372     void dump(const char* const s) const;
373     uint32_t mFBWidth;
374     uint32_t mFBHeight;
375     uint32_t mFBbpp;
376     uint32_t mFBystride;
377 };
378 
379 int getMdpFormat(int format);
380 int getMdpFormat(int format, bool tileEnabled);
381 int getHALFormat(int mdpFormat);
382 int getDownscaleFactor(const int& src_w, const int& src_h,
383         const int& dst_w, const int& dst_h);
384 void getDecimationFactor(const int& src_w, const int& src_h,
385         const int& dst_w, const int& dst_h, uint8_t& horzDeci,
386         uint8_t& vertDeci);
387 
388 /* flip is upside down and such. V, H flip
389  * rotation is 90, 180 etc
390  * It returns MDP related enum/define that match rot+flip*/
391 int getMdpOrient(eTransform rotation);
392 const char* getFormatString(int format);
393 
394 template <class T>
memset0(T & t)395 inline void memset0(T& t) { ::memset(&t, 0, sizeof(t)); }
396 
swap(T & a,T & b)397 template <class T> inline void swap ( T& a, T& b )
398 {
399     T c(a); a=b; b=c;
400 }
401 
alignup(int value,int a)402 inline int alignup(int value, int a) {
403     //if align = 0, return the value. Else, do alignment.
404     return a ? ((((value - 1) / a) + 1) * a) : value;
405 }
406 
aligndown(int value,int a)407 inline int aligndown(int value, int a) {
408     //if align = 0, return the value. Else, do alignment.
409     return a ? ((value) & ~(a-1)) : value;
410 }
411 
412 // FIXME that align should replace the upper one.
align(int value,int a)413 inline int align(int value, int a) {
414     //if align = 0, return the value. Else, do alignment.
415     return a ? ((value + (a-1)) & ~(a-1)) : value;
416 }
417 
isYuv(uint32_t format)418 inline bool isYuv(uint32_t format) {
419     switch(format){
420         case MDP_Y_CBCR_H2V1:
421         case MDP_Y_CBCR_H2V2:
422         case MDP_Y_CRCB_H2V2:
423         case MDP_Y_CRCB_H1V1:
424         case MDP_Y_CRCB_H2V1:
425         case MDP_Y_CRCB_H2V2_TILE:
426         case MDP_Y_CBCR_H2V2_TILE:
427         case MDP_Y_CR_CB_H2V2:
428         case MDP_Y_CR_CB_GH2V2:
429         case MDP_Y_CBCR_H2V2_VENUS:
430         case MDP_YCBYCR_H2V1:
431         case MDP_YCRYCB_H2V1:
432             return true;
433         default:
434             return false;
435     }
436     return false;
437 }
438 
isRgb(uint32_t format)439 inline bool isRgb(uint32_t format) {
440     switch(format) {
441         case MDP_RGBA_8888:
442         case MDP_BGRA_8888:
443         case MDP_RGBX_8888:
444         case MDP_RGB_565:
445             return true;
446         default:
447             return false;
448     }
449     return false;
450 }
451 
getFormatString(int format)452 inline const char* getFormatString(int format){
453     #define STR(f) #f;
454     static const char* formats[MDP_IMGTYPE_LIMIT + 1] = {0};
455     formats[MDP_RGB_565] = STR(MDP_RGB_565);
456     formats[MDP_XRGB_8888] = STR(MDP_XRGB_8888);
457     formats[MDP_Y_CBCR_H2V2] = STR(MDP_Y_CBCR_H2V2);
458     formats[MDP_Y_CBCR_H2V2_ADRENO] = STR(MDP_Y_CBCR_H2V2_ADRENO);
459     formats[MDP_ARGB_8888] = STR(MDP_ARGB_8888);
460     formats[MDP_RGB_888] = STR(MDP_RGB_888);
461     formats[MDP_Y_CRCB_H2V2] = STR(MDP_Y_CRCB_H2V2);
462     formats[MDP_YCBYCR_H2V1] = STR(MDP_YCBYCR_H2V1);
463     formats[MDP_YCRYCB_H2V1] = STR(MDP_YCRYCB_H2V1);
464     formats[MDP_CBYCRY_H2V1] = STR(MDP_CBYCRY_H2V1);
465     formats[MDP_Y_CRCB_H2V1] = STR(MDP_Y_CRCB_H2V1);
466     formats[MDP_Y_CBCR_H2V1] = STR(MDP_Y_CBCR_H2V1);
467     formats[MDP_Y_CRCB_H1V2] = STR(MDP_Y_CRCB_H1V2);
468     formats[MDP_Y_CBCR_H1V2] = STR(MDP_Y_CBCR_H1V2);
469     formats[MDP_RGBA_8888] = STR(MDP_RGBA_8888);
470     formats[MDP_BGRA_8888] = STR(MDP_BGRA_8888);
471     formats[MDP_RGBX_8888] = STR(MDP_RGBX_8888);
472     formats[MDP_Y_CRCB_H2V2_TILE] = STR(MDP_Y_CRCB_H2V2_TILE);
473     formats[MDP_Y_CBCR_H2V2_TILE] = STR(MDP_Y_CBCR_H2V2_TILE);
474     formats[MDP_Y_CR_CB_H2V2] = STR(MDP_Y_CR_CB_H2V2);
475     formats[MDP_Y_CR_CB_GH2V2] = STR(MDP_Y_CR_CB_GH2V2);
476     formats[MDP_Y_CB_CR_H2V2] = STR(MDP_Y_CB_CR_H2V2);
477     formats[MDP_Y_CRCB_H1V1] = STR(MDP_Y_CRCB_H1V1);
478     formats[MDP_Y_CBCR_H1V1] = STR(MDP_Y_CBCR_H1V1);
479     formats[MDP_YCRCB_H1V1] = STR(MDP_YCRCB_H1V1);
480     formats[MDP_YCBCR_H1V1] = STR(MDP_YCBCR_H1V1);
481     formats[MDP_BGR_565] = STR(MDP_BGR_565);
482     formats[MDP_BGR_888] = STR(MDP_BGR_888);
483     formats[MDP_Y_CBCR_H2V2_VENUS] = STR(MDP_Y_CBCR_H2V2_VENUS);
484     formats[MDP_BGRX_8888] = STR(MDP_BGRX_8888);
485     formats[MDP_RGBA_8888_TILE] = STR(MDP_RGBA_8888_TILE);
486     formats[MDP_ARGB_8888_TILE] = STR(MDP_ARGB_8888_TILE);
487     formats[MDP_ABGR_8888_TILE] = STR(MDP_ABGR_8888_TILE);
488     formats[MDP_BGRA_8888_TILE] = STR(MDP_BGRA_8888_TILE);
489     formats[MDP_RGBX_8888_TILE] = STR(MDP_RGBX_8888_TILE);
490     formats[MDP_XRGB_8888_TILE] = STR(MDP_XRGB_8888_TILE);
491     formats[MDP_XBGR_8888_TILE] = STR(MDP_XBGR_8888_TILE);
492     formats[MDP_BGRX_8888_TILE] = STR(MDP_BGRX_8888_TILE);
493     formats[MDP_RGB_565_TILE] = STR(MDP_RGB_565_TILE);
494     formats[MDP_IMGTYPE_LIMIT] = STR(MDP_IMGTYPE_LIMIT);
495 
496     if(format < 0 || format >= MDP_IMGTYPE_LIMIT) {
497         ALOGE("%s wrong fmt %d", __FUNCTION__, format);
498         return "Unsupported format";
499     }
500     if(formats[format] == 0) {
501         ALOGE("%s: table missing format %d from header", __FUNCTION__, format);
502         return "";
503     }
504     return formats[format];
505 }
506 
dump()507 inline void Whf::dump() const {
508     ALOGE("== Dump WHF w=%d h=%d f=%d s=%d start/end ==",
509             w, h, format, size);
510 }
511 
dump()512 inline void Dim::dump() const {
513     ALOGE("== Dump Dim x=%d y=%d w=%d h=%d start/end ==", x, y, w, h);
514 }
515 
516 template <class Type>
swapWidthHeight(Type & width,Type & height)517 void swapWidthHeight(Type& width, Type& height) {
518     Type tmp = width;
519     width = height;
520     height = tmp;
521 }
522 
dump(const char * const s)523 inline void ScreenInfo::dump(const char* const s) const {
524     ALOGE("== Dump %s ScreenInfo w=%d h=%d"
525             " bpp=%d stride=%d start/end ==",
526             s, mFBWidth, mFBHeight, mFBbpp, mFBystride);
527 }
528 
openDev(OvFD & fd,int fbnum,const char * const devpath,int flags)529 inline bool openDev(OvFD& fd, int fbnum,
530     const char* const devpath, int flags) {
531     return overlay::open(fd, fbnum, devpath, flags);
532 }
533 
534 template <class T>
even_ceil(T & value)535 inline void even_ceil(T& value) {
536     if(value & 1)
537         value++;
538 }
539 
540 template <class T>
even_floor(T & value)541 inline void even_floor(T& value) {
542     if(value & 1)
543         value--;
544 }
545 
546 /* Prerotation adjusts crop co-ordinates to the new transformed values within
547  * destination buffer. This is necessary only when the entire buffer is rotated
548  * irrespective of crop (A-family). If only the crop portion of the buffer is
549  * rotated into a destination buffer matching the size of crop, we don't need to
550  * use this helper (B-family).
551  * @Deprecated as of now, retained for the case where a full buffer needs
552  * transform and also as a reference.
553  */
554 void preRotateSource(const eTransform& tr, Whf& whf, Dim& srcCrop);
555 void getDump(char *buf, size_t len, const char *prefix, const mdp_overlay& ov);
556 void getDump(char *buf, size_t len, const char *prefix, const msmfb_img& ov);
557 void getDump(char *buf, size_t len, const char *prefix, const mdp_rect& ov);
558 void getDump(char *buf, size_t len, const char *prefix,
559         const msmfb_overlay_data& ov);
560 void getDump(char *buf, size_t len, const char *prefix, const msmfb_data& ov);
561 void getDump(char *buf, size_t len, const char *prefix,
562         const msm_rotator_img_info& ov);
563 void getDump(char *buf, size_t len, const char *prefix,
564         const msm_rotator_data_info& ov);
565 
566 } // namespace utils ends
567 
568 //--------------------Class Res stuff (namespace overlay only) -----------
569 
570 class Res {
571 public:
572     // /dev/graphics/fb%u
573     static const char* const fbPath;
574     // /dev/msm_rotator
575     static const char* const rotPath;
576 };
577 
578 
579 //--------------------Class OvFD stuff (namespace overlay only) -----------
580 
581 /*
582 * Holds one FD
583 * Dtor will NOT close the underlying FD.
584 * That enables us to copy that object around
585 * */
586 class OvFD {
587 public:
588     /* Ctor */
589     explicit OvFD();
590 
591     /* dtor will NOT close the underlying FD */
592     ~OvFD();
593 
594     /* Open fd using the path given by dev.
595      * return false in failure */
596     bool open(const char* const dev,
597             int flags = O_RDWR);
598 
599     /* populate path */
600     void setPath(const char* const dev);
601 
602     /* Close fd if we have a valid fd. */
603     bool close();
604 
605     /* returns underlying fd.*/
606     int getFD() const;
607 
608     /* returns true if fd is valid */
609     bool valid() const;
610 
611     /* like operator= */
612     void copy(int fd);
613 
614     /* dump the state of the instance */
615     void dump() const;
616 private:
617     /* helper enum for determine valid/invalid fd */
618     enum { INVAL = -1 };
619 
620     /* actual os fd */
621     int mFD;
622 
623     /* path, for debugging */
624     char mPath[utils::MAX_PATH_LEN];
625 };
626 
627 //-------------------Inlines--------------------------
628 
open(OvFD & fd,uint32_t fbnum,const char * const dev,int flags)629 inline bool open(OvFD& fd, uint32_t fbnum, const char* const dev, int flags)
630 {
631     char dev_name[64] = {0};
632     snprintf(dev_name, sizeof(dev_name), dev, fbnum);
633     return fd.open(dev_name, flags);
634 }
635 
OvFD()636 inline OvFD::OvFD() : mFD (INVAL) {
637     mPath[0] = 0;
638 }
639 
~OvFD()640 inline OvFD::~OvFD() {
641     //no op since copy() can be used to share fd, in 3d cases.
642 }
643 
open(const char * const dev,int flags)644 inline bool OvFD::open(const char* const dev, int flags)
645 {
646     mFD = ::open(dev, flags, 0);
647     if (mFD < 0) {
648         // FIXME errno, strerror in bionic?
649         ALOGE("Cant open device %s err=%d", dev, errno);
650         return false;
651     }
652     setPath(dev);
653     return true;
654 }
655 
setPath(const char * const dev)656 inline void OvFD::setPath(const char* const dev)
657 {
658     ::strlcpy(mPath, dev, sizeof(mPath));
659 }
660 
close()661 inline bool OvFD::close()
662 {
663     int ret = 0;
664     if(valid()) {
665         ret = ::close(mFD);
666         mFD = INVAL;
667     }
668     return (ret == 0);
669 }
670 
valid()671 inline bool OvFD::valid() const
672 {
673     return (mFD != INVAL);
674 }
675 
getFD()676 inline int OvFD::getFD() const { return mFD; }
677 
copy(int fd)678 inline void OvFD::copy(int fd) {
679     mFD = fd;
680 }
681 
dump()682 inline void OvFD::dump() const
683 {
684     ALOGE("== Dump OvFD fd=%d path=%s start/end ==",
685             mFD, mPath);
686 }
687 
688 //--------------- class OvFD stuff ends ---------------------
689 
690 } // overlay
691 
692 
693 #endif // OVERLAY_UTILS_H
694