1/*
2* Copyright (c) 2011-2013, 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#define FB_DEVICE_TEMPLATE "/dev/graphics/fb%u"
81
82namespace overlay {
83
84// fwd
85class Overlay;
86class OvFD;
87
88/* helper function to open by using fbnum */
89bool open(OvFD& fd, uint32_t fbnum, const char* const dev,
90    int flags = O_RDWR);
91
92namespace utils {
93struct Whf;
94struct Dim;
95
96inline uint32_t setBit(uint32_t x, uint32_t mask) {
97    return (x | mask);
98}
99
100inline uint32_t clrBit(uint32_t x, uint32_t mask) {
101    return (x & ~mask);
102}
103
104/* Utility class to help avoid copying instances by making the copy ctor
105* and assignment operator private
106*
107* Usage:
108*    class SomeClass : utils::NoCopy {...};
109*/
110class NoCopy {
111protected:
112    NoCopy(){}
113    ~NoCopy() {}
114private:
115    NoCopy(const NoCopy&);
116    const NoCopy& operator=(const NoCopy&);
117};
118
119
120/* 3D related utils, defines etc...
121 * The compound format passed to the overlay is
122 * ABCCC where A is the input 3D format
123 * B is the output 3D format
124 * CCC is the color format e.g YCbCr420SP YCrCb420SP etc */
125enum { SHIFT_OUT_3D = 12,
126    SHIFT_TOT_3D = 16 };
127enum { INPUT_3D_MASK = 0xFFFF0000,
128    OUTPUT_3D_MASK = 0x0000FFFF };
129enum { BARRIER_LAND = 1,
130    BARRIER_PORT = 2 };
131
132inline uint32_t format3D(uint32_t x) { return x & 0xFF000; }
133inline uint32_t format3DOutput(uint32_t x) {
134    return (x & 0xF000) >> SHIFT_OUT_3D; }
135inline uint32_t format3DInput(uint32_t x) { return x & 0xF0000; }
136
137bool isHDMIConnected ();
138bool is3DTV();
139bool isPanel3D();
140bool usePanel3D();
141bool send3DInfoPacket (uint32_t fmt);
142bool enableBarrier (uint32_t orientation);
143uint32_t getS3DFormat(uint32_t fmt);
144bool isMdssRotator();
145void normalizeCrop(uint32_t& xy, uint32_t& wh);
146
147template <int CHAN>
148bool getPositionS3D(const Whf& whf, Dim& out);
149
150template <int CHAN>
151bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt);
152
153template <class Type>
154void swapWidthHeight(Type& width, Type& height);
155
156struct Dim {
157    Dim () : x(0), y(0),
158    w(0), h(0),
159    o(0) {}
160    Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h) :
161        x(_x), y(_y),
162        w(_w), h(_h) {}
163    Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h, uint32_t _o) :
164        x(_x), y(_y),
165        w(_w), h(_h),
166        o(_o) {}
167    bool check(uint32_t _w, uint32_t _h) const {
168        return (x+w <= _w && y+h <= _h);
169
170    }
171
172    bool operator==(const Dim& d) const {
173        return d.x == x && d.y == y &&
174                d.w == w && d.h == h &&
175                d.o == o;
176    }
177
178    bool operator!=(const Dim& d) const {
179        return !operator==(d);
180    }
181
182    void dump() const;
183    uint32_t x;
184    uint32_t y;
185    uint32_t w;
186    uint32_t h;
187    uint32_t o;
188};
189
190// TODO have Whfz
191
192struct Whf {
193    Whf() : w(0), h(0), format(0), size(0) {}
194    Whf(uint32_t wi, uint32_t he, uint32_t f) :
195        w(wi), h(he), format(f), size(0) {}
196    Whf(uint32_t wi, uint32_t he, uint32_t f, uint32_t s) :
197        w(wi), h(he), format(f), size(s) {}
198    // FIXME not comparing size at the moment
199    bool operator==(const Whf& whf) const {
200        return whf.w == w && whf.h == h &&
201                whf.format == format;
202    }
203    bool operator!=(const Whf& whf) const {
204        return !operator==(whf);
205    }
206    void dump() const;
207    uint32_t w;
208    uint32_t h;
209    uint32_t format;
210    uint32_t size;
211};
212
213enum { MAX_PATH_LEN = 256 };
214
215enum { DEFAULT_PLANE_ALPHA = 0xFF };
216
217/**
218 * Rotator flags: not to be confused with orientation flags.
219 * Usually, you want to open the rotator to make sure it is
220 * ready for business.
221 * */
222 enum eRotFlags {
223    ROT_FLAGS_NONE = 0,
224    //Use rotator for 0 rotation. It is used anyway for others.
225    ROT_0_ENABLED = 1 << 0,
226    //Enable rotator downscale optimization for hardware bugs not handled in
227    //driver. If downscale optimizatation is required,
228    //then rotator will be used even if its 0 rotation case.
229    ROT_DOWNSCALE_ENABLED = 1 << 1,
230    ROT_PREROTATED = 1 << 2,
231};
232
233enum eRotDownscale {
234    ROT_DS_NONE = 0,
235    ROT_DS_HALF = 1,
236    ROT_DS_FOURTH = 2,
237    ROT_DS_EIGHTH = 3,
238};
239
240/* The values for is_fg flag for control alpha and transp
241 * IS_FG_OFF means is_fg = 0
242 * IS_FG_SET means is_fg = 1
243 */
244enum eIsFg {
245    IS_FG_OFF = 0,
246    IS_FG_SET = 1
247};
248
249/*
250 * Various mdp flags like PIPE SHARE, DEINTERLACE etc...
251 * kernel/common/linux/msm_mdp.h
252 * INTERLACE_MASK: hardware/qcom/display/libgralloc/badger/fb_priv.h
253 * */
254enum eMdpFlags {
255    OV_MDP_FLAGS_NONE = 0,
256    OV_MDP_PIPE_SHARE =  MDP_OV_PIPE_SHARE,
257    OV_MDP_PIPE_FORCE_DMA = MDP_OV_PIPE_FORCE_DMA,
258    OV_MDP_DEINTERLACE = MDP_DEINTERLACE,
259    OV_MDP_SECURE_OVERLAY_SESSION = MDP_SECURE_OVERLAY_SESSION,
260    OV_MDP_SOURCE_ROTATED_90 = MDP_SOURCE_ROTATED_90,
261    OV_MDP_BACKEND_COMPOSITION = MDP_BACKEND_COMPOSITION,
262    OV_MDP_BLEND_FG_PREMULT = MDP_BLEND_FG_PREMULT,
263    OV_MDP_FLIP_H = MDP_FLIP_LR,
264    OV_MDP_FLIP_V = MDP_FLIP_UD,
265    OV_MDSS_MDP_RIGHT_MIXER = MDSS_MDP_RIGHT_MIXER,
266    OV_MDP_PP_EN = MDP_OVERLAY_PP_CFG_EN,
267    OV_MDSS_MDP_BWC_EN = MDP_BWC_EN,
268};
269
270enum eZorder {
271    ZORDER_0 = 0,
272    ZORDER_1,
273    ZORDER_2,
274    ZORDER_3,
275    Z_SYSTEM_ALLOC = 0xFFFF
276};
277
278enum eMdpPipeType {
279    OV_MDP_PIPE_RGB = 0,
280    OV_MDP_PIPE_VG,
281    OV_MDP_PIPE_DMA,
282    OV_MDP_PIPE_ANY, //Any
283};
284
285// Identify destination pipes
286// TODO Names useless, replace with int and change all interfaces
287enum eDest {
288    OV_P0 = 0,
289    OV_P1,
290    OV_P2,
291    OV_P3,
292    OV_P4,
293    OV_P5,
294    OV_P6,
295    OV_P7,
296    OV_P8,
297    OV_P9,
298    OV_INVALID,
299    OV_MAX = OV_INVALID,
300};
301
302/* Used when a buffer is split over 2 pipes and sent to display */
303enum {
304    OV_LEFT_SPLIT = 0,
305    OV_RIGHT_SPLIT,
306};
307
308/* values for copybit_set_parameter(OVERLAY_TRANSFORM) */
309enum eTransform {
310    /* No rot */
311    OVERLAY_TRANSFORM_0 = 0x0,
312    /* flip source image horizontally 0x1 */
313    OVERLAY_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H,
314    /* flip source image vertically 0x2 */
315    OVERLAY_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
316    /* rotate source image 180 degrees
317     * It is basically bit-or-ed  H | V == 0x3 */
318    OVERLAY_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
319    /* rotate source image 90 degrees 0x4 */
320    OVERLAY_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
321    /* rotate source image 90 degrees and flip horizontally 0x5 */
322    OVERLAY_TRANSFORM_ROT_90_FLIP_H = HAL_TRANSFORM_ROT_90 |
323                                      HAL_TRANSFORM_FLIP_H,
324    /* rotate source image 90 degrees and flip vertically 0x6 */
325    OVERLAY_TRANSFORM_ROT_90_FLIP_V = HAL_TRANSFORM_ROT_90 |
326                                      HAL_TRANSFORM_FLIP_V,
327    /* rotate source image 270 degrees
328     * Basically 180 | 90 == 0x7 */
329    OVERLAY_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
330    /* rotate invalid like in Transform.h */
331    OVERLAY_TRANSFORM_INV = 0x80
332};
333
334enum eBlending {
335    OVERLAY_BLENDING_UNDEFINED = 0x0,
336    /* No blending */
337    OVERLAY_BLENDING_OPAQUE,
338    /* src.rgb + dst.rgb*(1-src_alpha) */
339    OVERLAY_BLENDING_PREMULT,
340    /* src.rgb * src_alpha + dst.rgb (1 - src_alpha) */
341    OVERLAY_BLENDING_COVERAGE,
342};
343
344// Used to consolidate pipe params
345struct PipeArgs {
346    PipeArgs() : mdpFlags(OV_MDP_FLAGS_NONE),
347        zorder(Z_SYSTEM_ALLOC),
348        isFg(IS_FG_OFF),
349        rotFlags(ROT_FLAGS_NONE),
350        planeAlpha(DEFAULT_PLANE_ALPHA),
351        blending(OVERLAY_BLENDING_COVERAGE){
352    }
353
354    PipeArgs(eMdpFlags f, Whf _whf,
355            eZorder z, eIsFg fg, eRotFlags r,
356            int pA, eBlending b) :
357        mdpFlags(f),
358        whf(_whf),
359        zorder(z),
360        isFg(fg),
361        rotFlags(r),
362        planeAlpha(pA),
363        blending(b){
364    }
365
366    eMdpFlags mdpFlags; // for mdp_overlay flags
367    Whf whf;
368    eZorder zorder; // stage number
369    eIsFg isFg; // control alpha & transp
370    eRotFlags rotFlags;
371    int planeAlpha;
372    eBlending blending;
373};
374
375// Cannot use HW_OVERLAY_MAGNIFICATION_LIMIT, since at the time
376// of integration, HW_OVERLAY_MAGNIFICATION_LIMIT was a define
377enum { HW_OV_MAGNIFICATION_LIMIT = 20,
378    HW_OV_MINIFICATION_LIMIT  = 8
379};
380
381inline void setMdpFlags(eMdpFlags& f, eMdpFlags v) {
382    f = static_cast<eMdpFlags>(setBit(f, v));
383}
384
385inline void clearMdpFlags(eMdpFlags& f, eMdpFlags v) {
386    f = static_cast<eMdpFlags>(clrBit(f, v));
387}
388
389enum { FB0, FB1, FB2 };
390
391struct ScreenInfo {
392    ScreenInfo() : mFBWidth(0),
393    mFBHeight(0),
394    mFBbpp(0),
395    mFBystride(0) {}
396    void dump(const char* const s) const;
397    uint32_t mFBWidth;
398    uint32_t mFBHeight;
399    uint32_t mFBbpp;
400    uint32_t mFBystride;
401};
402
403int getMdpFormat(int format);
404int getHALFormat(int mdpFormat);
405int getDownscaleFactor(const int& src_w, const int& src_h,
406        const int& dst_w, const int& dst_h);
407void getDecimationFactor(const int& src_w, const int& src_h,
408        const int& dst_w, const int& dst_h, float& horDscale,
409        float& verDscale);
410
411/* flip is upside down and such. V, H flip
412 * rotation is 90, 180 etc
413 * It returns MDP related enum/define that match rot+flip*/
414int getMdpOrient(eTransform rotation);
415const char* getFormatString(int format);
416
417template <class T>
418inline void memset0(T& t) { ::memset(&t, 0, sizeof(T)); }
419
420template <class T> inline void swap ( T& a, T& b )
421{
422    T c(a); a=b; b=c;
423}
424
425inline int alignup(int value, int a) {
426    //if align = 0, return the value. Else, do alignment.
427    return a ? ((((value - 1) / a) + 1) * a) : value;
428}
429
430inline int aligndown(int value, int a) {
431    //if align = 0, return the value. Else, do alignment.
432    return a ? ((value) & ~(a-1)) : value;
433}
434
435// FIXME that align should replace the upper one.
436inline int align(int value, int a) {
437    //if align = 0, return the value. Else, do alignment.
438    return a ? ((value + (a-1)) & ~(a-1)) : value;
439}
440
441enum eRotOutFmt {
442    ROT_OUT_FMT_DEFAULT,
443    ROT_OUT_FMT_Y_CRCB_H2V2
444};
445
446template <int ROT_OUT_FMT> struct RotOutFmt;
447
448// FIXME, taken from gralloc_priv.h. Need to
449// put it back as soon as overlay takes place of the old one
450/* possible formats for 3D content*/
451enum {
452    HAL_NO_3D                         = 0x0000,
453    HAL_3D_IN_SIDE_BY_SIDE_L_R        = 0x10000,
454    HAL_3D_IN_TOP_BOTTOM              = 0x20000,
455    HAL_3D_IN_INTERLEAVE              = 0x40000,
456    HAL_3D_IN_SIDE_BY_SIDE_R_L        = 0x80000,
457    HAL_3D_OUT_SIDE_BY_SIDE           = 0x1000,
458    HAL_3D_OUT_TOP_BOTTOM             = 0x2000,
459    HAL_3D_OUT_INTERLEAVE             = 0x4000,
460    HAL_3D_OUT_MONOSCOPIC             = 0x8000
461};
462
463enum { HAL_3D_OUT_SBS_MASK =
464    HAL_3D_OUT_SIDE_BY_SIDE >> overlay::utils::SHIFT_OUT_3D,
465    HAL_3D_OUT_TOP_BOT_MASK =
466            HAL_3D_OUT_TOP_BOTTOM >> overlay::utils::SHIFT_OUT_3D,
467    HAL_3D_OUT_INTERL_MASK =
468            HAL_3D_OUT_INTERLEAVE >> overlay::utils::SHIFT_OUT_3D,
469    HAL_3D_OUT_MONOS_MASK =
470            HAL_3D_OUT_MONOSCOPIC >> overlay::utils::SHIFT_OUT_3D
471};
472
473
474inline bool isYuv(uint32_t format) {
475    switch(format){
476        case MDP_Y_CBCR_H2V1:
477        case MDP_Y_CBCR_H2V2:
478        case MDP_Y_CRCB_H2V2:
479        case MDP_Y_CRCB_H1V1:
480        case MDP_Y_CRCB_H2V1:
481        case MDP_Y_CRCB_H2V2_TILE:
482        case MDP_Y_CBCR_H2V2_TILE:
483        case MDP_Y_CR_CB_H2V2:
484        case MDP_Y_CR_CB_GH2V2:
485        case MDP_Y_CBCR_H2V2_VENUS:
486            return true;
487        default:
488            return false;
489    }
490    return false;
491}
492
493inline bool isRgb(uint32_t format) {
494    switch(format) {
495        case MDP_RGBA_8888:
496        case MDP_BGRA_8888:
497        case MDP_RGBX_8888:
498        case MDP_RGB_565:
499            return true;
500        default:
501            return false;
502    }
503    return false;
504}
505
506inline const char* getFormatString(int format){
507    #define STR(f) #f;
508    static const char* formats[MDP_IMGTYPE_LIMIT + 1] = {0};
509    formats[MDP_RGB_565] = STR(MDP_RGB_565);
510    formats[MDP_XRGB_8888] = STR(MDP_XRGB_8888);
511    formats[MDP_Y_CBCR_H2V2] = STR(MDP_Y_CBCR_H2V2);
512    formats[MDP_Y_CBCR_H2V2_ADRENO] = STR(MDP_Y_CBCR_H2V2_ADRENO);
513    formats[MDP_ARGB_8888] = STR(MDP_ARGB_8888);
514    formats[MDP_RGB_888] = STR(MDP_RGB_888);
515    formats[MDP_Y_CRCB_H2V2] = STR(MDP_Y_CRCB_H2V2);
516    formats[MDP_YCRYCB_H2V1] = STR(MDP_YCRYCB_H2V1);
517    formats[MDP_CBYCRY_H2V1] = STR(MDP_CBYCRY_H2V1);
518    formats[MDP_Y_CRCB_H2V1] = STR(MDP_Y_CRCB_H2V1);
519    formats[MDP_Y_CBCR_H2V1] = STR(MDP_Y_CBCR_H2V1);
520    formats[MDP_Y_CRCB_H1V2] = STR(MDP_Y_CRCB_H1V2);
521    formats[MDP_Y_CBCR_H1V2] = STR(MDP_Y_CBCR_H1V2);
522    formats[MDP_RGBA_8888] = STR(MDP_RGBA_8888);
523    formats[MDP_BGRA_8888] = STR(MDP_BGRA_8888);
524    formats[MDP_RGBX_8888] = STR(MDP_RGBX_8888);
525    formats[MDP_Y_CRCB_H2V2_TILE] = STR(MDP_Y_CRCB_H2V2_TILE);
526    formats[MDP_Y_CBCR_H2V2_TILE] = STR(MDP_Y_CBCR_H2V2_TILE);
527    formats[MDP_Y_CR_CB_H2V2] = STR(MDP_Y_CR_CB_H2V2);
528    formats[MDP_Y_CR_CB_GH2V2] = STR(MDP_Y_CR_CB_GH2V2);
529    formats[MDP_Y_CB_CR_H2V2] = STR(MDP_Y_CB_CR_H2V2);
530    formats[MDP_Y_CRCB_H1V1] = STR(MDP_Y_CRCB_H1V1);
531    formats[MDP_Y_CBCR_H1V1] = STR(MDP_Y_CBCR_H1V1);
532    formats[MDP_YCRCB_H1V1] = STR(MDP_YCRCB_H1V1);
533    formats[MDP_YCBCR_H1V1] = STR(MDP_YCBCR_H1V1);
534    formats[MDP_BGR_565] = STR(MDP_BGR_565);
535    formats[MDP_BGR_888] = STR(MDP_BGR_888);
536    formats[MDP_Y_CBCR_H2V2_VENUS] = STR(MDP_Y_CBCR_H2V2_VENUS);
537    formats[MDP_BGRX_8888] = STR(MDP_BGRX_8888);
538    formats[MDP_IMGTYPE_LIMIT] = STR(MDP_IMGTYPE_LIMIT);
539
540    if(format < 0 || format >= MDP_IMGTYPE_LIMIT) {
541        ALOGE("%s wrong fmt %d", __FUNCTION__, format);
542        return "Unsupported format";
543    }
544    if(formats[format] == 0) {
545        ALOGE("%s: table missing format %d from header", __FUNCTION__, format);
546        return "";
547    }
548    return formats[format];
549}
550
551inline void Whf::dump() const {
552    ALOGE("== Dump WHF w=%d h=%d f=%d s=%d start/end ==",
553            w, h, format, size);
554}
555
556inline void Dim::dump() const {
557    ALOGE("== Dump Dim x=%d y=%d w=%d h=%d start/end ==", x, y, w, h);
558}
559
560// FB0
561template <int CHAN>
562inline Dim getPositionS3DImpl(const Whf& whf)
563{
564    switch (whf.format & OUTPUT_3D_MASK)
565    {
566        case HAL_3D_OUT_SBS_MASK:
567            // x, y, w, h
568            return Dim(0, 0, whf.w/2, whf.h);
569        case HAL_3D_OUT_TOP_BOT_MASK:
570            return Dim(0, 0, whf.w, whf.h/2);
571        case HAL_3D_OUT_MONOS_MASK:
572            return Dim();
573        case HAL_3D_OUT_INTERL_MASK:
574            // FIXME error?
575            ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
576            return Dim();
577        default:
578            ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
579                    whf.format);
580    }
581    return Dim();
582}
583
584template <>
585inline Dim getPositionS3DImpl<utils::OV_RIGHT_SPLIT>(const Whf& whf)
586{
587    switch (whf.format & OUTPUT_3D_MASK)
588    {
589        case HAL_3D_OUT_SBS_MASK:
590            return Dim(whf.w/2, 0, whf.w/2, whf.h);
591        case HAL_3D_OUT_TOP_BOT_MASK:
592            return Dim(0, whf.h/2, whf.w, whf.h/2);
593        case HAL_3D_OUT_MONOS_MASK:
594            return Dim(0, 0, whf.w, whf.h);
595        case HAL_3D_OUT_INTERL_MASK:
596            // FIXME error?
597            ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
598            return Dim();
599        default:
600            ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
601                    whf.format);
602    }
603    return Dim();
604}
605
606template <int CHAN>
607inline bool getPositionS3D(const Whf& whf, Dim& out) {
608    out = getPositionS3DImpl<CHAN>(whf);
609    return (out != Dim());
610}
611
612template <int CHAN>
613inline Dim getCropS3DImpl(const Dim& in, uint32_t fmt) {
614    switch (fmt & INPUT_3D_MASK)
615    {
616        case HAL_3D_IN_SIDE_BY_SIDE_L_R:
617            return Dim(0, 0, in.w/2, in.h);
618        case HAL_3D_IN_SIDE_BY_SIDE_R_L:
619            return Dim(in.w/2, 0, in.w/2, in.h);
620        case HAL_3D_IN_TOP_BOTTOM:
621            return Dim(0, 0, in.w, in.h/2);
622        case HAL_3D_IN_INTERLEAVE:
623            ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
624            break;
625        default:
626            ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
627            break;
628    }
629    return Dim();
630}
631
632template <>
633inline Dim getCropS3DImpl<utils::OV_RIGHT_SPLIT>(const Dim& in, uint32_t fmt) {
634    switch (fmt & INPUT_3D_MASK)
635    {
636        case HAL_3D_IN_SIDE_BY_SIDE_L_R:
637            return Dim(in.w/2, 0, in.w/2, in.h);
638        case HAL_3D_IN_SIDE_BY_SIDE_R_L:
639            return Dim(0, 0, in.w/2, in.h);
640        case HAL_3D_IN_TOP_BOTTOM:
641            return Dim(0, in.h/2, in.w, in.h/2);
642        case HAL_3D_IN_INTERLEAVE:
643            ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
644            break;
645        default:
646            ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
647            break;
648    }
649    return Dim();
650}
651
652template <int CHAN>
653inline bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt)
654{
655    out = getCropS3DImpl<CHAN>(in, fmt);
656    return (out != Dim());
657}
658
659template <class Type>
660void swapWidthHeight(Type& width, Type& height) {
661    Type tmp = width;
662    width = height;
663    height = tmp;
664}
665
666inline void ScreenInfo::dump(const char* const s) const {
667    ALOGE("== Dump %s ScreenInfo w=%d h=%d"
668            " bpp=%d stride=%d start/end ==",
669            s, mFBWidth, mFBHeight, mFBbpp, mFBystride);
670}
671
672inline bool openDev(OvFD& fd, int fbnum,
673    const char* const devpath, int flags) {
674    return overlay::open(fd, fbnum, devpath, flags);
675}
676
677template <class T>
678inline void even_ceil(T& value) {
679    if(value & 1)
680        value++;
681}
682
683template <class T>
684inline void even_floor(T& value) {
685    if(value & 1)
686        value--;
687}
688
689void preRotateSource(const eTransform& tr, Whf& whf, Dim& srcCrop);
690void getDump(char *buf, size_t len, const char *prefix, const mdp_overlay& ov);
691void getDump(char *buf, size_t len, const char *prefix, const msmfb_img& ov);
692void getDump(char *buf, size_t len, const char *prefix, const mdp_rect& ov);
693void getDump(char *buf, size_t len, const char *prefix,
694        const msmfb_overlay_data& ov);
695void getDump(char *buf, size_t len, const char *prefix, const msmfb_data& ov);
696void getDump(char *buf, size_t len, const char *prefix,
697        const msm_rotator_img_info& ov);
698void getDump(char *buf, size_t len, const char *prefix,
699        const msm_rotator_data_info& ov);
700
701} // namespace utils ends
702
703//--------------------Class Res stuff (namespace overlay only) -----------
704
705class Res {
706public:
707    // /dev/graphics/fb%u
708    static const char* const fbPath;
709    // /dev/msm_rotator
710    static const char* const rotPath;
711    // /sys/class/graphics/fb1/format_3d
712    static const char* const format3DFile;
713    // /sys/class/graphics/fb1/3d_present
714    static const char* const edid3dInfoFile;
715    // /sys/devices/platform/mipi_novatek.0/enable_3d_barrier
716    static const char* const barrierFile;
717};
718
719
720//--------------------Class OvFD stuff (namespace overlay only) -----------
721
722/*
723* Holds one FD
724* Dtor will NOT close the underlying FD.
725* That enables us to copy that object around
726* */
727class OvFD {
728public:
729    /* Ctor */
730    explicit OvFD();
731
732    /* dtor will NOT close the underlying FD */
733    ~OvFD();
734
735    /* Open fd using the path given by dev.
736     * return false in failure */
737    bool open(const char* const dev,
738            int flags = O_RDWR);
739
740    /* populate path */
741    void setPath(const char* const dev);
742
743    /* Close fd if we have a valid fd. */
744    bool close();
745
746    /* returns underlying fd.*/
747    int getFD() const;
748
749    /* returns true if fd is valid */
750    bool valid() const;
751
752    /* like operator= */
753    void copy(int fd);
754
755    /* dump the state of the instance */
756    void dump() const;
757private:
758    /* helper enum for determine valid/invalid fd */
759    enum { INVAL = -1 };
760
761    /* actual os fd */
762    int mFD;
763
764    /* path, for debugging */
765    char mPath[utils::MAX_PATH_LEN];
766};
767
768//-------------------Inlines--------------------------
769
770inline bool open(OvFD& fd, uint32_t fbnum, const char* const dev, int flags)
771{
772    char dev_name[64] = {0};
773    snprintf(dev_name, sizeof(dev_name), dev, fbnum);
774    return fd.open(dev_name, flags);
775}
776
777inline OvFD::OvFD() : mFD (INVAL) {
778    mPath[0] = 0;
779}
780
781inline OvFD::~OvFD() {
782    //no op since copy() can be used to share fd, in 3d cases.
783}
784
785inline bool OvFD::open(const char* const dev, int flags)
786{
787    mFD = ::open(dev, flags, 0);
788    if (mFD < 0) {
789        // FIXME errno, strerror in bionic?
790        ALOGE("Cant open device %s err=%d", dev, errno);
791        return false;
792    }
793    setPath(dev);
794    return true;
795}
796
797inline void OvFD::setPath(const char* const dev)
798{
799    ::strncpy(mPath, dev, utils::MAX_PATH_LEN);
800}
801
802inline bool OvFD::close()
803{
804    int ret = 0;
805    if(valid()) {
806        ret = ::close(mFD);
807        mFD = INVAL;
808    }
809    return (ret == 0);
810}
811
812inline bool OvFD::valid() const
813{
814    return (mFD != INVAL);
815}
816
817inline int OvFD::getFD() const { return mFD; }
818
819inline void OvFD::copy(int fd) {
820    mFD = fd;
821}
822
823inline void OvFD::dump() const
824{
825    ALOGE("== Dump OvFD fd=%d path=%s start/end ==",
826            mFD, mPath);
827}
828
829//--------------- class OvFD stuff ends ---------------------
830
831} // overlay
832
833
834#endif // OVERLAY_UTILS_H
835