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_H
31#define OVERLAY_H
32
33#include "overlayUtils.h"
34#include "mdp_version.h"
35#include "utils/threads.h"
36
37struct MetaData_t;
38
39namespace overlay {
40class GenericPipe;
41
42class Overlay : utils::NoCopy {
43public:
44    enum { DMA_BLOCK_MODE, DMA_LINE_MODE };
45    //Abstract Display types. Each backed by a LayerMixer,
46    //represented by a fb node.
47    //High res panels can be backed by 2 layer mixers and a single fb node.
48    enum { DPY_PRIMARY, DPY_EXTERNAL, DPY_WRITEBACK, DPY_UNUSED };
49    enum { DPY_MAX = DPY_UNUSED };
50    enum { MIXER_LEFT, MIXER_RIGHT, MIXER_UNUSED };
51    enum { MIXER_DEFAULT = MIXER_LEFT, MIXER_MAX = MIXER_UNUSED };
52    enum { MAX_FB_DEVICES = DPY_MAX };
53    enum { FORMAT_YUV, FORMAT_RGB };
54
55    struct PipeSpecs {
56        PipeSpecs() : formatClass(FORMAT_RGB), needsScaling(false), fb(false),
57                dpy(DPY_PRIMARY), mixer(MIXER_DEFAULT) {}
58        int formatClass;
59        bool needsScaling;
60        bool fb;
61        int dpy;
62        int mixer;
63    };
64
65    /* dtor close */
66    ~Overlay();
67
68    /* Marks the beginning of a drawing round, resets usage bits on pipes
69     * Should be called when drawing begins before any pipe config is done.
70     */
71    void configBegin();
72
73    /* Marks the end of config for this drawing round
74     * Will do garbage collection of pipe objects and thus calling UNSETs,
75     * closing FDs, removing rotator objects and memory, if allocated.
76     * Should be called after all pipe configs are done.
77     */
78    void configDone();
79
80    /* Get a pipe that supported the specified format class (yuv, rgb) and has
81     * scaling capabilities.
82     */
83    utils::eDest getPipe(const PipeSpecs& pipeSpecs);
84    /* Returns the eDest corresponding to an already allocated pipeid.
85     * Useful for the reservation case, when libvpu reserves the pipe at its
86     * end, and expect the overlay to allocate a given pipe for a layer.
87     */
88    utils::eDest reservePipe(int pipeid);
89    /* getting dest for the given pipeid */
90    utils::eDest getDest(int pipeid);
91    /* getting overlay.pipeid for the given dest */
92    int getPipeId(utils::eDest dest);
93
94    void setSource(const utils::PipeArgs args, utils::eDest dest);
95    void setCrop(const utils::Dim& d, utils::eDest dest);
96    void setColor(const uint32_t color, utils::eDest dest);
97    void setTransform(const int orientation, utils::eDest dest);
98    void setPosition(const utils::Dim& dim, utils::eDest dest);
99    void setVisualParams(const MetaData_t& data, utils::eDest dest);
100    bool commit(utils::eDest dest);
101    bool queueBuffer(int fd, uint32_t offset, utils::eDest dest);
102
103    /* pipe reservation session is running */
104    bool sessionInProgress(utils::eDest dest);
105    /* pipe reservation session has ended*/
106    bool isSessionEnded(utils::eDest dest);
107    /* start session for the pipe reservation */
108    void startSession(utils::eDest dest);
109    /* end all started sesisons */
110    void endAllSessions();
111    /* Returns available ("unallocated") pipes for a display's mixer */
112    int availablePipes(int dpy, int mixer);
113    /* Returns available ("unallocated") pipes for a display */
114    int availablePipes(int dpy);
115    /* Returns available ("unallocated") pipe of given type for a display */
116    int availablePipes(int dpy, utils::eMdpPipeType type);
117    /* Returns if any of the requested pipe type is attached to any of the
118     * displays
119     */
120    bool isPipeTypeAttached(utils::eMdpPipeType type);
121    /* Compare pipe priorities and return
122     * 1 if 1st pipe has a higher priority
123     * 0 if both have the same priority
124     *-1 if 2nd pipe has a higher priority
125     */
126    int comparePipePriority(utils::eDest pipe1Index, utils::eDest pipe2Index);
127    /* Returns pipe dump. Expects a NULL terminated buffer of big enough size
128     * to populate.
129     */
130    void getDump(char *buf, size_t len);
131    /* Reset usage and allocation bits on all pipes for given display */
132    void clear(int dpy);
133    /* Validate the set of pipes for a display and set them in driver */
134    bool validateAndSet(const int& dpy, const int& fbFd);
135
136    /* Closes open pipes, called during startup */
137    static int initOverlay();
138    /* Returns the singleton instance of overlay */
139    static Overlay* getInstance();
140    static void setDMAMode(const int& mode);
141    static int getDMAMode();
142    /* Returns the framebuffer node backing up the display */
143    static int getFbForDpy(const int& dpy);
144
145    static bool displayCommit(const int& fd);
146    /* Overloads display commit with ROI's of each halves.
147     * Single interface panels will only update left ROI. */
148    static bool displayCommit(const int& fd, const utils::Dim& lRoi,
149                              const utils::Dim& rRoi);
150
151private:
152    /* Ctor setup */
153    explicit Overlay();
154    /*Validate index range, abort if invalid */
155    void validate(int index);
156    static void setDMAMultiplexingSupported();
157    void dump() const;
158    /* Returns an available pipe based on the type of pipe requested. When ANY
159     * is requested, the first available VG or RGB is returned. If no pipe is
160     * available for the display "dpy" then INV is returned. Note: If a pipe is
161     * assigned to a certain display, then it cannot be assigned to another
162     * display without being garbage-collected once. To add if a pipe is
163     * asisgned to a mixer within a display it cannot be reused for another
164     * mixer without being UNSET once*/
165    utils::eDest nextPipe(utils::eMdpPipeType, int dpy, int mixer);
166    /* Helpers that enfore target specific policies while returning pipes */
167    utils::eDest getPipe_8x26(const PipeSpecs& pipeSpecs);
168    utils::eDest getPipe_8x16(const PipeSpecs& pipeSpecs);
169
170    /* Returns the handle to libscale.so's programScale function */
171    static int (*getFnProgramScale())(struct mdp_overlay_list *);
172    /* Creates a scalar object using libscale.so */
173    static void initScalar();
174    /* Destroys the scalar object using libscale.so */
175    static void destroyScalar();
176
177    /* Just like a Facebook for pipes, but much less profile info */
178    struct PipeBook {
179        void init();
180        void destroy();
181        /* Check if pipe exists and return true, false otherwise */
182        bool valid();
183
184        /* Hardware pipe wrapper */
185        GenericPipe *mPipe;
186        /* Display using this pipe. Refer to enums above */
187        int mDisplay;
188        /* Mixer within a split display this pipe is attached to */
189        int mMixer;
190
191        /* operations on bitmap */
192        static bool pipeUsageUnchanged();
193        static void setUse(int index);
194        static void resetUse(int index);
195        static bool isUsed(int index);
196        static bool isNotUsed(int index);
197        static void save();
198
199        static void setAllocation(int index);
200        static void resetAllocation(int index);
201        static bool isAllocated(int index);
202        static bool isNotAllocated(int index);
203
204        static utils::eMdpPipeType getPipeType(utils::eDest dest);
205        static const char* getDestStr(utils::eDest dest);
206
207        static int NUM_PIPES;
208        static utils::eMdpPipeType pipeTypeLUT[utils::OV_MAX];
209        /* Session for reserved pipes */
210        enum Session {
211            NONE,
212            START,
213            END
214        };
215        Session mSession;
216
217    private:
218        //usage tracks if a successful commit happened. So a pipe could be
219        //allocated to a display, but it may not end up using it for various
220        //reasons. If one display actually uses a pipe then it amy not be
221        //used by another display, without an UNSET in between.
222        static int sPipeUsageBitmap;
223        static int sLastUsageBitmap;
224        //Tracks which pipe objects are allocated. This does not imply that they
225        //will actually be used. For example, a display might choose to acquire
226        //3 pipe objects in one shot and proceed with config only if it gets all
227        //3. The bitmap helps allocate different pipe objects on each request.
228        static int sAllocatedBitmap;
229    };
230
231    PipeBook mPipeBook[utils::OV_INVALID]; //Used as max
232
233    /* Dump string */
234    char mDumpStr[1024];
235
236    /* Singleton Instance*/
237    static Overlay *sInstance;
238    static int sDpyFbMap[DPY_MAX];
239    static int sDMAMode;
240    static bool sDMAMultiplexingSupported;
241    static void *sLibScaleHandle;
242    static int (*sFnProgramScale)(struct mdp_overlay_list *);
243
244    friend class MdpCtrl;
245};
246
247inline void Overlay::validate(int index) {
248    OVASSERT(index >=0 && index < PipeBook::NUM_PIPES, \
249        "%s, Index out of bounds: %d", __FUNCTION__, index);
250    OVASSERT(mPipeBook[index].valid(), "Pipe does not exist %s",
251            PipeBook::getDestStr((utils::eDest)index));
252}
253
254inline int Overlay::availablePipes(int dpy, int mixer) {
255    int avail = 0;
256    for(int i = 0; i < PipeBook::NUM_PIPES; i++) {
257        if( (mPipeBook[i].mDisplay == DPY_UNUSED ||
258             mPipeBook[i].mDisplay == dpy) &&
259            (mPipeBook[i].mMixer == MIXER_UNUSED ||
260             mPipeBook[i].mMixer == mixer) &&
261            PipeBook::isNotAllocated(i) &&
262            !(Overlay::getDMAMode() == Overlay::DMA_BLOCK_MODE &&
263              PipeBook::getPipeType((utils::eDest)i) ==
264              utils::OV_MDP_PIPE_DMA)) {
265            avail++;
266        }
267    }
268    return avail;
269}
270
271inline int Overlay::availablePipes(int dpy) {
272    int avail = 0;
273    for(int i = 0; i < PipeBook::NUM_PIPES; i++) {
274        if( (mPipeBook[i].mDisplay == DPY_UNUSED ||
275             mPipeBook[i].mDisplay == dpy) &&
276            PipeBook::isNotAllocated(i) &&
277            !(Overlay::getDMAMode() == Overlay::DMA_BLOCK_MODE &&
278              PipeBook::getPipeType((utils::eDest)i) ==
279              utils::OV_MDP_PIPE_DMA)) {
280            avail++;
281        }
282    }
283    return avail;
284}
285
286inline int Overlay::availablePipes(int dpy, utils::eMdpPipeType type) {
287    int avail = 0;
288    for(int i = 0; i < PipeBook::NUM_PIPES; i++) {
289        if((mPipeBook[i].mDisplay == DPY_UNUSED ||
290            mPipeBook[i].mDisplay == dpy) &&
291            PipeBook::isNotAllocated(i) &&
292            type == PipeBook::getPipeType((utils::eDest)i)) {
293            avail++;
294        }
295    }
296    return avail;
297}
298
299inline void Overlay::setDMAMode(const int& mode) {
300    if(mode == DMA_LINE_MODE || mode == DMA_BLOCK_MODE)
301        sDMAMode = mode;
302}
303
304inline void Overlay::setDMAMultiplexingSupported() {
305    sDMAMultiplexingSupported = false;
306    if(qdutils::MDPVersion::getInstance().is8x26())
307        sDMAMultiplexingSupported = true;
308}
309
310inline int Overlay::getDMAMode() {
311    return sDMAMode;
312}
313
314inline int Overlay::getFbForDpy(const int& dpy) {
315    OVASSERT(dpy >= 0 && dpy < DPY_MAX, "Invalid dpy %d", dpy);
316    return sDpyFbMap[dpy];
317}
318
319inline int (*Overlay::getFnProgramScale())(struct mdp_overlay_list *) {
320    return sFnProgramScale;
321}
322
323inline bool Overlay::PipeBook::valid() {
324    return (mPipe != NULL);
325}
326
327inline bool Overlay::PipeBook::pipeUsageUnchanged() {
328    return (sPipeUsageBitmap == sLastUsageBitmap);
329}
330
331inline void Overlay::PipeBook::setUse(int index) {
332    sPipeUsageBitmap |= (1 << index);
333}
334
335inline void Overlay::PipeBook::resetUse(int index) {
336    sPipeUsageBitmap &= ~(1 << index);
337}
338
339inline bool Overlay::PipeBook::isUsed(int index) {
340    return sPipeUsageBitmap & (1 << index);
341}
342
343inline bool Overlay::PipeBook::isNotUsed(int index) {
344    return !isUsed(index);
345}
346
347inline void Overlay::PipeBook::save() {
348    sLastUsageBitmap = sPipeUsageBitmap;
349}
350
351inline void Overlay::PipeBook::setAllocation(int index) {
352    sAllocatedBitmap |= (1 << index);
353}
354
355inline void Overlay::PipeBook::resetAllocation(int index) {
356    sAllocatedBitmap &= ~(1 << index);
357}
358
359inline bool Overlay::PipeBook::isAllocated(int index) {
360    return sAllocatedBitmap & (1 << index);
361}
362
363inline bool Overlay::PipeBook::isNotAllocated(int index) {
364    return !isAllocated(index);
365}
366
367inline utils::eMdpPipeType Overlay::PipeBook::getPipeType(utils::eDest dest) {
368    return pipeTypeLUT[(int)dest];
369}
370
371inline void Overlay::startSession(utils::eDest dest) {
372    mPipeBook[(int)dest].mSession = PipeBook::START;
373}
374
375inline bool Overlay::sessionInProgress(utils::eDest dest) {
376    return (mPipeBook[(int)dest].mSession == PipeBook::START);
377}
378
379inline bool Overlay::isSessionEnded(utils::eDest dest) {
380    return (mPipeBook[(int)dest].mSession == PipeBook::END);
381}
382
383inline const char* Overlay::PipeBook::getDestStr(utils::eDest dest) {
384    switch(getPipeType(dest)) {
385        case utils::OV_MDP_PIPE_RGB: return "RGB";
386        case utils::OV_MDP_PIPE_VG: return "VG";
387        case utils::OV_MDP_PIPE_DMA: return "DMA";
388        default: return "Invalid";
389    }
390    return "Invalid";
391}
392
393}; // overlay
394
395#endif // OVERLAY_H
396