overlayUtils.h revision ae1044e0cbab8d26851782e85525ee9e0e6127e6
1a3ab4494b72d9daf66f45db4d7c43677d1968a55Eric Fischer/*
21866d9ec83dcbc69a2c5ba2b6129ed4ce7f9f665Kenny Root* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
31866d9ec83dcbc69a2c5ba2b6129ed4ce7f9f665Kenny Root*
41866d9ec83dcbc69a2c5ba2b6129ed4ce7f9f665Kenny Root* Redistribution and use in source and binary forms, with or without
51866d9ec83dcbc69a2c5ba2b6129ed4ce7f9f665Kenny Root* modification, are permitted provided that the following conditions are
61866d9ec83dcbc69a2c5ba2b6129ed4ce7f9f665Kenny Root* met:
71866d9ec83dcbc69a2c5ba2b6129ed4ce7f9f665Kenny Root*    * Redistributions of source code must retain the above copyright
81866d9ec83dcbc69a2c5ba2b6129ed4ce7f9f665Kenny Root*      notice, this list of conditions and the following disclaimer.
91866d9ec83dcbc69a2c5ba2b6129ed4ce7f9f665Kenny Root*    * Redistributions in binary form must reproduce the above
101866d9ec83dcbc69a2c5ba2b6129ed4ce7f9f665Kenny Root*      copyright notice, this list of conditions and the following
111866d9ec83dcbc69a2c5ba2b6129ed4ce7f9f665Kenny Root*      disclaimer in the documentation and/or other materials provided
121866d9ec83dcbc69a2c5ba2b6129ed4ce7f9f665Kenny Root*      with the distribution.
131866d9ec83dcbc69a2c5ba2b6129ed4ce7f9f665Kenny Root*    * Neither the name of Code Aurora Forum, Inc. nor the names of its
141866d9ec83dcbc69a2c5ba2b6129ed4ce7f9f665Kenny Root*      contributors may be used to endorse or promote products derived
151866d9ec83dcbc69a2c5ba2b6129ed4ce7f9f665Kenny Root*      from this software without specific prior written permission.
161866d9ec83dcbc69a2c5ba2b6129ed4ce7f9f665Kenny Root*
171866d9ec83dcbc69a2c5ba2b6129ed4ce7f9f665Kenny Root* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
181866d9ec83dcbc69a2c5ba2b6129ed4ce7f9f665Kenny Root* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
191866d9ec83dcbc69a2c5ba2b6129ed4ce7f9f665Kenny Root* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20a3ab4494b72d9daf66f45db4d7c43677d1968a55Eric Fischer* ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21a3ab4494b72d9daf66f45db4d7c43677d1968a55Eric Fischer* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22a3ab4494b72d9daf66f45db4d7c43677d1968a55Eric Fischer* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23e04cc8b826a8d77c62b4377beaa7ba8879f9551esatok* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24e04cc8b826a8d77c62b4377beaa7ba8879f9551esatok* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25566c45eddfaff94f82008fa767d262ce57885a7dEric Fischer* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
265a323e65d06575ccf948a868d18578d8d749e738Eric Fischer* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
275a323e65d06575ccf948a868d18578d8d749e738Eric Fischer* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28566c45eddfaff94f82008fa767d262ce57885a7dEric Fischer*/
292ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer
3080bcf30dd6dabb56076c7dc246eaa54fd43d783cEric Fischer#ifndef OVERLAY_UTILS_H
3180bcf30dd6dabb56076c7dc246eaa54fd43d783cEric Fischer#define OVERLAY_UTILS_H
3280bcf30dd6dabb56076c7dc246eaa54fd43d783cEric Fischer
33144e97f154ef27c48ac9030980ebb6f3ae959369Eric Fischer#include <cutils/log.h> // ALOGE, etc
34144e97f154ef27c48ac9030980ebb6f3ae959369Eric Fischer#include <errno.h>
35144e97f154ef27c48ac9030980ebb6f3ae959369Eric Fischer#include <fcntl.h> // open, O_RDWR, etc
36144e97f154ef27c48ac9030980ebb6f3ae959369Eric Fischer#include <hardware/hardware.h>
37144e97f154ef27c48ac9030980ebb6f3ae959369Eric Fischer#include <hardware/gralloc.h> // buffer_handle_t
38ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer#include <linux/msm_mdp.h> // flags
39ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer#include <stdio.h>
40fc80c3151d629ea38b65565b94e8d8b7cec4f781Eric Fischer#include <stdlib.h>
41ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer#include <string.h>
425a323e65d06575ccf948a868d18578d8d749e738Eric Fischer#include <sys/stat.h>
435a323e65d06575ccf948a868d18578d8d749e738Eric Fischer#include <sys/types.h>
442ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer#include <utils/Log.h>
452ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer#include "gralloc_priv.h" //for interlace
462ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer/*
472ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer*
482ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer* Collection of utilities functions/structs/enums etc...
49a205d23ffdec7e89933c5052ddf325af344f4af0Eric Fischer*
50566c45eddfaff94f82008fa767d262ce57885a7dEric Fischer* */
51566c45eddfaff94f82008fa767d262ce57885a7dEric Fischer
52566c45eddfaff94f82008fa767d262ce57885a7dEric Fischer// comment that out if you want to remove asserts
53566c45eddfaff94f82008fa767d262ce57885a7dEric Fischer// or put it as -D in Android.mk. your choice.
542ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer#define OVERLAY_HAS_ASSERT
552ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer
562ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer#ifdef OVERLAY_HAS_ASSERT
572ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer# define OVASSERT(x, ...) if(!(x)) { ALOGE(__VA_ARGS__); abort(); }
582ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer#else
59ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer# define OVASSERT(x, ...) ALOGE_IF(!(x), __VA_ARGS__)
6080bcf30dd6dabb56076c7dc246eaa54fd43d783cEric Fischer#endif // OVERLAY_HAS_ASSERT
6115c37520c9f914697ec6ca2c26587f0be96007f4Eric Fischer
6280bcf30dd6dabb56076c7dc246eaa54fd43d783cEric Fischer#define DEBUG_OVERLAY 0
6380bcf30dd6dabb56076c7dc246eaa54fd43d783cEric Fischer#define PROFILE_OVERLAY 0
645a323e65d06575ccf948a868d18578d8d749e738Eric Fischer
655a323e65d06575ccf948a868d18578d8d749e738Eric Fischernamespace overlay {
665a323e65d06575ccf948a868d18578d8d749e738Eric Fischer
675a323e65d06575ccf948a868d18578d8d749e738Eric Fischer// fwd
685a323e65d06575ccf948a868d18578d8d749e738Eric Fischerclass Overlay;
692ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischerclass OvFD;
70566c45eddfaff94f82008fa767d262ce57885a7dEric Fischer
71af63f0663ac699926f33ea70e12c9dc72656109fEric Fischer/* helper function to open by using fbnum */
72af63f0663ac699926f33ea70e12c9dc72656109fEric Fischerbool open(OvFD& fd, uint32_t fbnum, const char* const dev,
73ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer    int flags = O_RDWR);
74ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer
75ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischernamespace utils {
76ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischerstruct Whf;
77ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischerstruct Dim;
78ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer
79ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischerinline uint32_t setBit(uint32_t x, uint32_t mask) {
80ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer    return (x | mask);
81ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer}
82ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer
83ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischerinline uint32_t clrBit(uint32_t x, uint32_t mask) {
84ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer    return (x & ~mask);
85ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer}
86ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer
87ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer/* Utility class to help avoid copying instances by making the copy ctor
88ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer* and assignment operator private
89ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer*
90ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer* Usage:
91ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer*    class SomeClass : utils::NoCopy {...};
92ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer*/
93ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischerclass NoCopy {
94ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischerprotected:
95ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer    NoCopy(){}
96ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer    ~NoCopy() {}
97ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischerprivate:
98ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer    NoCopy(const NoCopy&);
99ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer    const NoCopy& operator=(const NoCopy&);
100ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer};
101ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer
102ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer/*
103ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer* Utility class to query the framebuffer info for primary display
104ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer*
105ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer* Usage:
106ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer*    Outside of functions:
107ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer*       utils::FrameBufferInfo* utils::FrameBufferInfo::sFBInfoInstance = 0;
108ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer*    Inside functions:
109466741d8a78965b8509bf527344f289e50873092Mike LeBeau*       utils::FrameBufferInfo::getInstance()->supportTrueMirroring()
110466741d8a78965b8509bf527344f289e50873092Mike LeBeau*/
1112ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischerclass FrameBufferInfo {
1122ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer
1132ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischerpublic:
114466741d8a78965b8509bf527344f289e50873092Mike LeBeau    /* ctor init */
115466741d8a78965b8509bf527344f289e50873092Mike LeBeau    explicit FrameBufferInfo();
11615c37520c9f914697ec6ca2c26587f0be96007f4Eric Fischer
117466741d8a78965b8509bf527344f289e50873092Mike LeBeau    /* Gets an instance if one does not already exist */
118466741d8a78965b8509bf527344f289e50873092Mike LeBeau    static FrameBufferInfo* getInstance();
119466741d8a78965b8509bf527344f289e50873092Mike LeBeau
120466741d8a78965b8509bf527344f289e50873092Mike LeBeau    /* Gets width of primary framebuffer */
121466741d8a78965b8509bf527344f289e50873092Mike LeBeau    int getWidth() const;
122466741d8a78965b8509bf527344f289e50873092Mike LeBeau
123466741d8a78965b8509bf527344f289e50873092Mike LeBeau    /* Gets height of primary framebuffer */
124466741d8a78965b8509bf527344f289e50873092Mike LeBeau    int getHeight() const;
125466741d8a78965b8509bf527344f289e50873092Mike LeBeau
126466741d8a78965b8509bf527344f289e50873092Mike LeBeau    /* Indicates whether true mirroring is supported */
127466741d8a78965b8509bf527344f289e50873092Mike LeBeau    bool supportTrueMirroring() const;
128466741d8a78965b8509bf527344f289e50873092Mike LeBeau
1296aaafeed9682863157deb0506ee36a5177bbcd68Eric Fischerprivate:
1302ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer    int mFBWidth;
1312ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer    int mFBHeight;
1322ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer    bool mBorderFillSupported;
1332ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer    static FrameBufferInfo *sFBInfoInstance;
1342ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer};
13585427b3d8e25b36f8c0575d292348c861de40ed9Eric Fischer
136566c45eddfaff94f82008fa767d262ce57885a7dEric Fischer/* 3D related utils, defines etc...
1376aaafeed9682863157deb0506ee36a5177bbcd68Eric Fischer * The compound format passed to the overlay is
138303077bcb05b3ac4a373e41c3f7cf9b1c3d4bbb8Eric Fischer * ABCCC where A is the input 3D format
139303077bcb05b3ac4a373e41c3f7cf9b1c3d4bbb8Eric Fischer * B is the output 3D format
140566c45eddfaff94f82008fa767d262ce57885a7dEric Fischer * CCC is the color format e.g YCbCr420SP YCrCb420SP etc */
1411866d9ec83dcbc69a2c5ba2b6129ed4ce7f9f665Kenny Rootenum { SHIFT_OUT_3D = 12,
142566c45eddfaff94f82008fa767d262ce57885a7dEric Fischer    SHIFT_TOT_3D = 16 };
143566c45eddfaff94f82008fa767d262ce57885a7dEric Fischerenum { INPUT_3D_MASK = 0xFFFF0000,
1442ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer    OUTPUT_3D_MASK = 0x0000FFFF };
1452ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischerenum { BARRIER_LAND = 1,
146ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer    BARRIER_PORT = 2 };
1472ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer
14885427b3d8e25b36f8c0575d292348c861de40ed9Eric Fischerinline uint32_t format3D(uint32_t x) { return x & 0xFF000; }
1492ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischerinline uint32_t colorFormat(uint32_t fmt) {
1502ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer    /*TODO enable this block only if format has interlace / 3D info in top bits.
151ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer    if(fmt & INTERLACE_MASK) {
1522ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer        fmt = fmt ^ HAL_PIXEL_FORMAT_INTERLACE;
1532ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer    }
1542ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer    fmt = fmt & 0xFFF;*/
155ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer    return fmt;
1562ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer}
1572ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischerinline uint32_t format3DOutput(uint32_t x) {
1582ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer    return (x & 0xF000) >> SHIFT_OUT_3D; }
159ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischerinline uint32_t format3DInput(uint32_t x) { return x & 0xF0000; }
160ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischeruint32_t getColorFormat(uint32_t format);
16185427b3d8e25b36f8c0575d292348c861de40ed9Eric Fischer
1622ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischerbool isHDMIConnected ();
1632ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischerbool is3DTV();
1642ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischerbool isPanel3D();
165a205d23ffdec7e89933c5052ddf325af344f4af0Eric Fischerbool usePanel3D();
166144e97f154ef27c48ac9030980ebb6f3ae959369Eric Fischerbool send3DInfoPacket (uint32_t fmt);
1672ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischerbool enableBarrier (uint32_t orientation);
1682ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischeruint32_t getS3DFormat(uint32_t fmt);
1692ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer
170ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischertemplate <int CHAN>
1712ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischerbool getPositionS3D(const Whf& whf, Dim& out);
1722ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer
1732ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischertemplate <int CHAN>
1742ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischerbool getCropS3D(const Dim& in, Dim& out, uint32_t fmt);
1752ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer
1762ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischertemplate <class Type>
1772ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischervoid swapWidthHeight(Type& width, Type& height);
1782ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer
1792ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischerstruct Dim {
1802ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer    Dim () : x(0), y(0),
1812ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer    w(0), h(0),
1822ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer    o(0) {}
1832ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer    Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h) :
1842ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer        x(_x), y(_y),
1852ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer        w(_w), h(_h) {}
1862ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer    Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h, uint32_t _o) :
1872ca4e9ae8aaae5f30147f1b96fa27ae690cb4637Eric Fischer        x(_x), y(_y),
188ff53f0fd9788c6e57f6ba658cfc3908619592b9cEric Fischer        w(_w), h(_h),
189a3ab4494b72d9daf66f45db4d7c43677d1968a55Eric Fischer        o(_o) {}
190    bool check(uint32_t _w, uint32_t _h) const {
191        return (x+w <= _w && y+h <= _h);
192
193    }
194
195    bool operator==(const Dim& d) const {
196        return d.x == x && d.y == y &&
197                d.w == w && d.h == h &&
198                d.o == o;
199    }
200
201    bool operator!=(const Dim& d) const {
202        return !operator==(d);
203    }
204
205    void dump() const;
206    uint32_t x;
207    uint32_t y;
208    uint32_t w;
209    uint32_t h;
210    uint32_t o;
211};
212
213// TODO have Whfz
214
215struct Whf {
216    Whf() : w(0), h(0), format(0), size(0) {}
217    Whf(uint32_t wi, uint32_t he, uint32_t f) :
218        w(wi), h(he), format(f), size(0) {}
219    Whf(uint32_t wi, uint32_t he, uint32_t f, uint32_t s) :
220        w(wi), h(he), format(f), size(s) {}
221    // FIXME not comparing size at the moment
222    bool operator==(const Whf& whf) const {
223        return whf.w == w && whf.h == h &&
224                whf.format == format;
225    }
226    bool operator!=(const Whf& whf) const {
227        return !operator==(whf);
228    }
229    void dump() const;
230    uint32_t w;
231    uint32_t h;
232    uint32_t format;
233    uint32_t size;
234};
235
236enum { MAX_PATH_LEN = 256 };
237
238/**
239 * Rotator flags: not to be confused with orientation flags.
240 * Ususally, you want to open the rotator to make sure it is
241 * ready for business.
242 * ROT_FLAG_DISABLED: Rotator would not kick in. (ioctl will emit errors).
243 * ROT_FLAG_ENABLED: and when rotation is needed.
244 *                   (prim video playback)
245 *                   (UI mirroring on HDMI w/ 0 degree rotator. - just memcpy)
246 * In HDMI UI mirroring, rotator is always used.
247 * Even when w/o orienation change on primary,
248 * we do 0 rotation on HDMI and using rotator buffers.
249 * That is because we might see tearing otherwise. so
250 * we use another buffer (rotator).
251 * When a simple video playback on HDMI, no rotator is being used.(null r).
252 * */
253enum eRotFlags {
254    ROT_FLAG_DISABLED = 0,
255    ROT_FLAG_ENABLED = 1 // needed in rot
256};
257
258/* The values for is_fg flag for control alpha and transp
259 * IS_FG_OFF means is_fg = 0
260 * IS_FG_SET means is_fg = 1
261 */
262enum eIsFg {
263    IS_FG_OFF = 0,
264    IS_FG_SET = 1
265};
266
267/*
268 * Various mdp flags like PIPE SHARE, DEINTERLACE etc...
269 * kernel/common/linux/msm_mdp.h
270 * INTERLACE_MASK: hardware/qcom/display/libgralloc/badger/fb_priv.h
271 * */
272enum eMdpFlags {
273    OV_MDP_FLAGS_NONE = 0,
274    OV_MDP_PIPE_SHARE =  MDP_OV_PIPE_SHARE,
275    OV_MDP_DEINTERLACE = MDP_DEINTERLACE,
276    OV_MDP_SECURE_OVERLAY_SESSION = MDP_SECURE_OVERLAY_SESSION,
277    OV_MDP_SOURCE_ROTATED_90 = MDP_SOURCE_ROTATED_90,
278    OV_MDP_MEMORY_ID_TYPE_FB = MDP_MEMORY_ID_TYPE_FB,
279};
280
281enum eZorder {
282    ZORDER_0,
283    ZORDER_1,
284    ZORDER_2,
285    Z_SYSTEM_ALLOC = 0xFFFF
286};
287
288enum eMdpPipeType {
289    OV_MDP_PIPE_RGB,
290    OV_MDP_PIPE_VG
291};
292
293// Max pipes via overlay (VG0, VG1, RGB1)
294enum { MAX_PIPES = 3 };
295
296/* Used to identify destination channels and
297 * also 3D channels e.g. when in 3D mode with 2
298 * pipes opened and it is used in get crop/pos 3D
299 * */
300enum eDest {
301    OV_PIPE0 = 1 << 0,
302    OV_PIPE1 = 1 << 1,
303    OV_PIPE2 = 1 << 2,
304    OV_PIPE_ALL  = (OV_PIPE0 | OV_PIPE1 | OV_PIPE2)
305};
306
307/* values for copybit_set_parameter(OVERLAY_TRANSFORM) */
308enum eTransform {
309    /* No rot */
310    OVERLAY_TRANSFORM_0 = 0x0,
311    /* flip source image horizontally 0x1 */
312    OVERLAY_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H,
313    /* flip source image vertically 0x2 */
314    OVERLAY_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
315    /* rotate source image 180 degrees
316     * It is basically bit-or-ed  H | V == 0x3 */
317    OVERLAY_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
318    /* rotate source image 90 degrees 0x4 */
319    OVERLAY_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
320    /* rotate source image 90 degrees and flip horizontally 0x5 */
321    OVERLAY_TRANSFORM_ROT_90_FLIP_H = HAL_TRANSFORM_ROT_90 |
322                                      HAL_TRANSFORM_FLIP_H,
323    /* rotate source image 90 degrees and flip vertically 0x6 */
324    OVERLAY_TRANSFORM_ROT_90_FLIP_V = HAL_TRANSFORM_ROT_90 |
325                                      HAL_TRANSFORM_FLIP_V,
326    /* rotate source image 270 degrees
327     * Basically 180 | 90 == 0x7 */
328    OVERLAY_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
329    /* rotate invalid like in Transform.h */
330    OVERLAY_TRANSFORM_INV = 0x80
331};
332
333// Used to consolidate pipe params
334struct PipeArgs {
335    PipeArgs() : mdpFlags(OV_MDP_FLAGS_NONE),
336        zorder(Z_SYSTEM_ALLOC),
337        isFg(IS_FG_OFF),
338        rotFlags(ROT_FLAG_DISABLED){
339    }
340
341    PipeArgs(eMdpFlags f, Whf _whf,
342            eZorder z, eIsFg fg, eRotFlags r) :
343        mdpFlags(f),
344        whf(_whf),
345        zorder(z),
346        isFg(fg),
347        rotFlags(r) {
348    }
349
350    eMdpFlags mdpFlags; // for mdp_overlay flags
351    Whf whf;
352    eZorder zorder; // stage number
353    eIsFg isFg; // control alpha & transp
354    eRotFlags rotFlags;
355};
356
357enum eOverlayState{
358    /* No pipes from overlay open */
359    OV_CLOSED = 0,
360
361    /* 2D Video */
362    OV_2D_VIDEO_ON_PANEL,
363    OV_2D_VIDEO_ON_PANEL_TV,
364    OV_2D_VIDEO_ON_TV,
365
366    /* 3D Video on one display (panel or TV) */
367    OV_3D_VIDEO_ON_2D_PANEL,
368    OV_3D_VIDEO_ON_3D_PANEL,
369    OV_3D_VIDEO_ON_3D_TV,
370
371    /* 3D Video on two displays (panel and TV) */
372    OV_3D_VIDEO_ON_2D_PANEL_2D_TV,
373
374    /* UI Mirroring */
375    OV_UI_MIRROR,
376    OV_2D_TRUE_UI_MIRROR,
377
378    /* Composition Bypass */
379    OV_BYPASS_1_LAYER,
380    OV_BYPASS_2_LAYER,
381    OV_BYPASS_3_LAYER,
382
383    /* External only for dual-disp */
384    OV_DUAL_DISP,
385};
386
387inline void setMdpFlags(eMdpFlags& f, eMdpFlags v) {
388    f = static_cast<eMdpFlags>(setBit(f, v));
389}
390
391inline void clearMdpFlags(eMdpFlags& f, eMdpFlags v) {
392    f = static_cast<eMdpFlags>(clrBit(f, v));
393}
394
395// fb 0/1/2
396enum { FB0, FB1, FB2 };
397
398//Panels could be categorized as primary and external
399enum { PRIMARY, EXTERNAL };
400
401//External Panels could use HDMI or WFD
402enum {
403    HDMI = 1,
404    WFD = 2
405};
406
407//TODO Make this a part of some appropriate class
408static int sExtType = HDMI; //HDMI or WFD
409//Set by client as HDMI/WFD
410void setExtType(const int& type);
411//Return External panel type set by client.
412int getExtType();
413
414
415//Gets the FB number for the external type.
416//As of now, HDMI always has fb1, WFD could use fb1 or fb2
417//Assumes Ext type set by setExtType() from client.
418static int getFBForPanel(int panel) { // PRIMARY OR EXTERNAL
419    switch(panel) {
420        case PRIMARY: return FB0;
421            break;
422        case EXTERNAL:
423            switch(getExtType()) {
424                case HDMI: return FB1;
425                    break;
426                case WFD: return FB2;//Hardcoding fb2 for wfd. Will change.
427                    break;
428            }
429            break;
430        default:
431            ALOGE("%s: Unrecognized PANEL category %d", __func__, panel);
432            break;
433    }
434    return -1;
435}
436
437// number of rgb pipes bufs (max)
438
439// 2 for rgb0/1 double bufs
440enum { RGB_PIPE_NUM_BUFS = 2 };
441
442struct ScreenInfo {
443    ScreenInfo() : mFBWidth(0),
444    mFBHeight(0),
445    mFBbpp(0),
446    mFBystride(0) {}
447    void dump(const char* const s) const;
448    uint32_t mFBWidth;
449    uint32_t mFBHeight;
450    uint32_t mFBbpp;
451    uint32_t mFBystride;
452};
453
454int getMdpFormat(int format);
455int getRotOutFmt(uint32_t format);
456/* flip is upside down and such. V, H flip
457 * rotation is 90, 180 etc
458 * It returns MDP related enum/define that match rot+flip*/
459int getMdpOrient(eTransform rotation);
460const char* getFormatString(int format);
461const char* getStateString(eOverlayState state);
462
463// Cannot use HW_OVERLAY_MAGNIFICATION_LIMIT, since at the time
464// of integration, HW_OVERLAY_MAGNIFICATION_LIMIT was a define
465enum { HW_OV_MAGNIFICATION_LIMIT = 20,
466    HW_OV_MINIFICATION_LIMIT  = 8
467};
468
469template <class T>
470inline void memset0(T& t) { ::memset(&t, 0, sizeof(T)); }
471
472template <class T> inline void swap ( T& a, T& b )
473{
474    T c(a); a=b; b=c;
475}
476
477inline int alignup(int value, int a) {
478    //if align = 0, return the value. Else, do alignment.
479    return a ? ((((value - 1) / a) + 1) * a) : value;
480}
481
482// FIXME that align should replace the upper one.
483inline int align(int value, int a) {
484    //if align = 0, return the value. Else, do alignment.
485    return a ? ((value + (a-1)) & ~(a-1)) : value;
486}
487
488enum eRotOutFmt {
489    ROT_OUT_FMT_DEFAULT,
490    ROT_OUT_FMT_Y_CRCB_H2V2
491};
492
493template <int ROT_OUT_FMT> struct RotOutFmt;
494
495// FIXME, taken from gralloc_priv.h. Need to
496// put it back as soon as overlay takes place of the old one
497/* possible formats for 3D content*/
498enum {
499    HAL_NO_3D                         = 0x0000,
500    HAL_3D_IN_SIDE_BY_SIDE_L_R        = 0x10000,
501    HAL_3D_IN_TOP_BOTTOM              = 0x20000,
502    HAL_3D_IN_INTERLEAVE              = 0x40000,
503    HAL_3D_IN_SIDE_BY_SIDE_R_L        = 0x80000,
504    HAL_3D_OUT_SIDE_BY_SIDE           = 0x1000,
505    HAL_3D_OUT_TOP_BOTTOM             = 0x2000,
506    HAL_3D_OUT_INTERLEAVE             = 0x4000,
507    HAL_3D_OUT_MONOSCOPIC             = 0x8000
508};
509
510enum { HAL_3D_OUT_SBS_MASK =
511    HAL_3D_OUT_SIDE_BY_SIDE >> overlay::utils::SHIFT_OUT_3D,
512    HAL_3D_OUT_TOP_BOT_MASK =
513            HAL_3D_OUT_TOP_BOTTOM >> overlay::utils::SHIFT_OUT_3D,
514    HAL_3D_OUT_INTERL_MASK =
515            HAL_3D_OUT_INTERLEAVE >> overlay::utils::SHIFT_OUT_3D,
516    HAL_3D_OUT_MONOS_MASK =
517            HAL_3D_OUT_MONOSCOPIC >> overlay::utils::SHIFT_OUT_3D
518};
519
520
521inline bool isYuv(uint32_t format) {
522    switch(format){
523        case MDP_Y_CBCR_H2V1:
524        case MDP_Y_CBCR_H2V2:
525        case MDP_Y_CRCB_H2V2:
526        case MDP_Y_CRCB_H1V1:
527        case MDP_Y_CRCB_H2V1:
528        case MDP_Y_CRCB_H2V2_TILE:
529        case MDP_Y_CBCR_H2V2_TILE:
530        case MDP_Y_CR_CB_H2V2:
531        case MDP_Y_CR_CB_GH2V2:
532            return true;
533        default:
534            return false;
535    }
536    return false;
537}
538
539inline bool isRgb(uint32_t format) {
540    switch(format) {
541        case MDP_RGBA_8888:
542        case MDP_BGRA_8888:
543        case MDP_RGBX_8888:
544        case MDP_RGB_565:
545            return true;
546        default:
547            return false;
548    }
549    return false;
550}
551
552inline bool isValidDest(eDest dest)
553{
554    if ((OV_PIPE0 & dest) ||
555            (OV_PIPE1 & dest) ||
556            (OV_PIPE2 & dest)) {
557        return true;
558    }
559    return false;
560}
561
562inline const char* getFormatString(int format){
563    static const char* const formats[] = {
564        "MDP_RGB_565",
565        "MDP_XRGB_8888",
566        "MDP_Y_CBCR_H2V2",
567        "MDP_Y_CBCR_H2V2_ADRENO",
568        "MDP_ARGB_8888",
569        "MDP_RGB_888",
570        "MDP_Y_CRCB_H2V2",
571        "MDP_YCRYCB_H2V1",
572        "MDP_Y_CRCB_H2V1",
573        "MDP_Y_CBCR_H2V1",
574        "MDP_Y_CRCB_H1V2",
575        "MDP_Y_CBCR_H1V2",
576        "MDP_RGBA_8888",
577        "MDP_BGRA_8888",
578        "MDP_RGBX_8888",
579        "MDP_Y_CRCB_H2V2_TILE",
580        "MDP_Y_CBCR_H2V2_TILE",
581        "MDP_Y_CR_CB_H2V2",
582        "MDP_Y_CR_CB_GH2V2",
583        "MDP_Y_CB_CR_H2V2",
584        "MDP_Y_CRCB_H1V1",
585        "MDP_Y_CBCR_H1V1",
586        "MDP_YCRCB_H1V1",
587        "MDP_YCBCR_H1V1",
588        "MDP_BGR_565",
589        "MDP_IMGTYPE_LIMIT",
590        "MDP_RGB_BORDERFILL",
591        "MDP_FB_FORMAT",
592        "MDP_IMGTYPE_LIMIT2"
593    };
594    if(format < 0 || format >= (int)(sizeof(formats) / sizeof(formats[0]))) {
595        ALOGE("%s wrong fmt %d", __FUNCTION__, format);
596        return "Unsupported format";
597    }
598    return formats[format];
599}
600
601inline const char* getStateString(eOverlayState state){
602    switch (state) {
603        case OV_CLOSED:
604            return "OV_CLOSED";
605        case OV_2D_VIDEO_ON_PANEL:
606            return "OV_2D_VIDEO_ON_PANEL";
607        case OV_2D_VIDEO_ON_PANEL_TV:
608            return "OV_2D_VIDEO_ON_PANEL_TV";
609        case OV_2D_VIDEO_ON_TV:
610            return "OV_2D_VIDEO_ON_TV";
611        case OV_3D_VIDEO_ON_2D_PANEL:
612            return "OV_3D_VIDEO_ON_2D_PANEL";
613        case OV_3D_VIDEO_ON_3D_PANEL:
614            return "OV_3D_VIDEO_ON_3D_PANEL";
615        case OV_3D_VIDEO_ON_3D_TV:
616            return "OV_3D_VIDEO_ON_3D_TV";
617        case OV_3D_VIDEO_ON_2D_PANEL_2D_TV:
618            return "OV_3D_VIDEO_ON_2D_PANEL_2D_TV";
619        case OV_UI_MIRROR:
620            return "OV_UI_MIRROR";
621        case OV_2D_TRUE_UI_MIRROR:
622            return "OV_2D_TRUE_UI_MIRROR";
623        case OV_BYPASS_1_LAYER:
624            return "OV_BYPASS_1_LAYER";
625        case OV_BYPASS_2_LAYER:
626            return "OV_BYPASS_2_LAYER";
627        case OV_BYPASS_3_LAYER:
628            return "OV_BYPASS_3_LAYER";
629        case OV_DUAL_DISP:
630            return "OV_DUAL_DISP";
631        default:
632            return "UNKNOWN_STATE";
633    }
634    return "BAD_STATE";
635}
636
637inline void Whf::dump() const {
638    ALOGE("== Dump WHF w=%d h=%d f=%d s=%d start/end ==",
639            w, h, format, size);
640}
641
642inline void Dim::dump() const {
643    ALOGE("== Dump Dim x=%d y=%d w=%d h=%d start/end ==", x, y, w, h);
644}
645
646inline int getMdpOrient(eTransform rotation) {
647    ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, rotation);
648    switch(rotation)
649    {
650        case OVERLAY_TRANSFORM_0 : return 0;
651        case OVERLAY_TRANSFORM_FLIP_V:  return MDP_FLIP_UD;
652        case OVERLAY_TRANSFORM_FLIP_H:  return MDP_FLIP_LR;
653        case OVERLAY_TRANSFORM_ROT_90:  return MDP_ROT_90;
654        //getMdpOrient will switch the flips if the source is 90 rotated.
655        //Clients in Android dont factor in 90 rotation while deciding flip.
656        case OVERLAY_TRANSFORM_ROT_90_FLIP_V:
657                return MDP_ROT_90 | MDP_FLIP_LR;
658        case OVERLAY_TRANSFORM_ROT_90_FLIP_H:
659                return MDP_ROT_90 | MDP_FLIP_UD;
660        case OVERLAY_TRANSFORM_ROT_180: return MDP_ROT_180;
661        case OVERLAY_TRANSFORM_ROT_270: return MDP_ROT_270;
662        default:
663            ALOGE("%s: invalid rotation value (value = 0x%x",
664                    __FUNCTION__, rotation);
665    }
666    return -1;
667}
668
669inline int getRotOutFmt(uint32_t format) {
670    switch (format) {
671        case MDP_Y_CRCB_H2V2_TILE:
672            return MDP_Y_CRCB_H2V2;
673        case MDP_Y_CBCR_H2V2_TILE:
674            return MDP_Y_CBCR_H2V2;
675        case MDP_Y_CB_CR_H2V2:
676            return MDP_Y_CBCR_H2V2;
677        case MDP_Y_CR_CB_GH2V2:
678            return MDP_Y_CRCB_H2V2;
679        default:
680            return format;
681    }
682    // not reached
683    OVASSERT(false, "%s not reached", __FUNCTION__);
684    return -1;
685}
686
687
688inline uint32_t getColorFormat(uint32_t format)
689{
690    return (format == HAL_PIXEL_FORMAT_YV12) ?
691            format : colorFormat(format);
692}
693
694// FB0
695template <int CHAN>
696inline Dim getPositionS3DImpl(const Whf& whf)
697{
698    switch (whf.format & OUTPUT_3D_MASK)
699    {
700        case HAL_3D_OUT_SBS_MASK:
701            // x, y, w, h
702            return Dim(0, 0, whf.w/2, whf.h);
703        case HAL_3D_OUT_TOP_BOT_MASK:
704            return Dim(0, 0, whf.w, whf.h/2);
705        case HAL_3D_OUT_MONOS_MASK:
706            return Dim();
707        case HAL_3D_OUT_INTERL_MASK:
708            // FIXME error?
709            ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
710            return Dim();
711        default:
712            ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
713                    whf.format);
714    }
715    return Dim();
716}
717
718template <>
719inline Dim getPositionS3DImpl<utils::OV_PIPE1>(const Whf& whf)
720{
721    switch (whf.format & OUTPUT_3D_MASK)
722    {
723        case HAL_3D_OUT_SBS_MASK:
724            return Dim(whf.w/2, 0, whf.w/2, whf.h);
725        case HAL_3D_OUT_TOP_BOT_MASK:
726            return Dim(0, whf.h/2, whf.w, whf.h/2);
727        case HAL_3D_OUT_MONOS_MASK:
728            return Dim(0, 0, whf.w, whf.h);
729        case HAL_3D_OUT_INTERL_MASK:
730            // FIXME error?
731            ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
732            return Dim();
733        default:
734            ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
735                    whf.format);
736    }
737    return Dim();
738}
739
740template <int CHAN>
741inline bool getPositionS3D(const Whf& whf, Dim& out) {
742    out = getPositionS3DImpl<CHAN>(whf);
743    return (out != Dim());
744}
745
746template <int CHAN>
747inline Dim getCropS3DImpl(const Dim& in, uint32_t fmt) {
748    switch (fmt & INPUT_3D_MASK)
749    {
750        case HAL_3D_IN_SIDE_BY_SIDE_L_R:
751            return Dim(0, 0, in.w/2, in.h);
752        case HAL_3D_IN_SIDE_BY_SIDE_R_L:
753            return Dim(in.w/2, 0, in.w/2, in.h);
754        case HAL_3D_IN_TOP_BOTTOM:
755            return Dim(0, 0, in.w, in.h/2);
756        case HAL_3D_IN_INTERLEAVE:
757            ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
758            break;
759        default:
760            ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
761            break;
762    }
763    return Dim();
764}
765
766template <>
767inline Dim getCropS3DImpl<utils::OV_PIPE1>(const Dim& in, uint32_t fmt) {
768    switch (fmt & INPUT_3D_MASK)
769    {
770        case HAL_3D_IN_SIDE_BY_SIDE_L_R:
771            return Dim(in.w/2, 0, in.w/2, in.h);
772        case HAL_3D_IN_SIDE_BY_SIDE_R_L:
773            return Dim(0, 0, in.w/2, in.h);
774        case HAL_3D_IN_TOP_BOTTOM:
775            return Dim(0, in.h/2, in.w, in.h/2);
776        case HAL_3D_IN_INTERLEAVE:
777            ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
778            break;
779        default:
780            ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
781            break;
782    }
783    return Dim();
784}
785
786template <int CHAN>
787inline bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt)
788{
789    out = getCropS3DImpl<CHAN>(in, fmt);
790    return (out != Dim());
791}
792
793template <class Type>
794void swapWidthHeight(Type& width, Type& height) {
795    Type tmp = width;
796    width = height;
797    height = tmp;
798}
799
800inline void ScreenInfo::dump(const char* const s) const {
801    ALOGE("== Dump %s ScreenInfo w=%d h=%d"
802            " bpp=%d stride=%d start/end ==",
803            s, mFBWidth, mFBHeight, mFBbpp, mFBystride);
804}
805
806inline bool openDev(OvFD& fd, int fbnum,
807    const char* const devpath, int flags) {
808    return overlay::open(fd, fbnum, devpath, flags);
809}
810
811template <class T>
812inline void even_ceil(T& value) {
813    if(value & 1)
814        value++;
815}
816
817template <class T>
818inline void even_floor(T& value) {
819    if(value & 1)
820        value--;
821}
822
823} // namespace utils ends
824
825//--------------------Class Res stuff (namespace overlay only) -----------
826
827class Res {
828public:
829    // /dev/graphics/fb%u
830    static const char* const fbPath;
831    // /dev/msm_rotator
832    static const char* const rotPath;
833    // /sys/class/graphics/fb1/format_3d
834    static const char* const format3DFile;
835    // /sys/class/graphics/fb1/3d_present
836    static const char* const edid3dInfoFile;
837    // /sys/devices/platform/mipi_novatek.0/enable_3d_barrier
838    static const char* const barrierFile;
839};
840
841
842//--------------------Class OvFD stuff (namespace overlay only) -----------
843
844/*
845* Holds one FD
846* Dtor will NOT close the underlying FD.
847* That enables us to copy that object around
848* */
849class OvFD {
850public:
851    /* Ctor */
852    explicit OvFD();
853
854    /* dtor will NOT close the underlying FD */
855    ~OvFD();
856
857    /* Open fd using the path given by dev.
858     * return false in failure */
859    bool open(const char* const dev,
860            int flags = O_RDWR);
861
862    /* populate path */
863    void setPath(const char* const dev);
864
865    /* Close fd if we have a valid fd. */
866    bool close();
867
868    /* returns underlying fd.*/
869    int getFD() const;
870
871    /* returns true if fd is valid */
872    bool valid() const;
873
874    /* like operator= */
875    void copy(int fd);
876
877    /* dump the state of the instance */
878    void dump() const;
879private:
880    /* helper enum for determine valid/invalid fd */
881    enum { INVAL = -1 };
882
883    /* actual os fd */
884    int mFD;
885
886    /* path, for debugging */
887    char mPath[utils::MAX_PATH_LEN];
888};
889
890//-------------------Inlines--------------------------
891
892inline bool open(OvFD& fd, uint32_t fbnum, const char* const dev, int flags)
893{
894    char dev_name[64] = {0};
895    snprintf(dev_name, sizeof(dev_name), dev, fbnum);
896    return fd.open(dev_name, flags);
897}
898
899inline OvFD::OvFD() : mFD (INVAL) {
900    mPath[0] = 0;
901}
902
903inline OvFD::~OvFD() {
904    //no op since copy() can be used to share fd, in 3d cases.
905}
906
907inline bool OvFD::open(const char* const dev, int flags)
908{
909    mFD = ::open(dev, flags, 0);
910    if (mFD < 0) {
911        // FIXME errno, strerror in bionic?
912        ALOGE("Cant open device %s err=%d", dev, errno);
913        return false;
914    }
915    setPath(dev);
916    return true;
917}
918
919inline void OvFD::setPath(const char* const dev)
920{
921    ::strncpy(mPath, dev, utils::MAX_PATH_LEN);
922}
923
924inline bool OvFD::close()
925{
926    int ret = 0;
927    if(valid()) {
928        ret = ::close(mFD);
929        mFD = INVAL;
930    }
931    return (ret == 0);
932}
933
934inline bool OvFD::valid() const
935{
936    return (mFD != INVAL);
937}
938
939inline int OvFD::getFD() const { return mFD; }
940
941inline void OvFD::copy(int fd) {
942    mFD = fd;
943}
944
945inline void OvFD::dump() const
946{
947    ALOGE("== Dump OvFD fd=%d path=%s start/end ==",
948            mFD, mPath);
949}
950
951//--------------- class OvFD stuff ends ---------------------
952
953} // overlay
954
955
956#endif // OVERLAY_UTILS_H
957