Home | History | Annotate | Download | only in liboverlay
      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 "utils/threads.h"
     35 
     36 struct MetaData_t;
     37 
     38 namespace overlay {
     39 class GenericPipe;
     40 
     41 class Overlay : utils::NoCopy {
     42 public:
     43     enum { DMA_BLOCK_MODE, DMA_LINE_MODE };
     44     //Abstract Display types. Each backed by a LayerMixer,
     45     //represented by a fb node.
     46     //High res panels can be backed by 2 layer mixers and a single fb node.
     47     enum { DPY_PRIMARY, DPY_EXTERNAL, DPY_WRITEBACK, DPY_UNUSED };
     48     enum { DPY_MAX = DPY_UNUSED };
     49     enum { MIXER_LEFT, MIXER_RIGHT, MIXER_UNUSED };
     50     enum { MIXER_DEFAULT = MIXER_LEFT, MIXER_MAX = MIXER_UNUSED };
     51     enum { MAX_FB_DEVICES = DPY_MAX };
     52 
     53     /* dtor close */
     54     ~Overlay();
     55 
     56     /* Marks the beginning of a drawing round, resets usage bits on pipes
     57      * Should be called when drawing begins before any pipe config is done.
     58      */
     59     void configBegin();
     60 
     61     /* Marks the end of config for this drawing round
     62      * Will do garbage collection of pipe objects and thus calling UNSETs,
     63      * closing FDs, removing rotator objects and memory, if allocated.
     64      * Should be called after all pipe configs are done.
     65      */
     66     void configDone();
     67 
     68     /* Returns an available pipe based on the type of pipe requested. When ANY
     69      * is requested, the first available VG or RGB is returned. If no pipe is
     70      * available for the display "dpy" then INV is returned. Note: If a pipe is
     71      * assigned to a certain display, then it cannot be assigned to another
     72      * display without being garbage-collected once. To add if a pipe is
     73      * asisgned to a mixer within a display it cannot be reused for another
     74      * mixer without being UNSET once*/
     75     utils::eDest nextPipe(utils::eMdpPipeType, int dpy, int mixer);
     76 
     77     void setSource(const utils::PipeArgs args, utils::eDest dest);
     78     void setCrop(const utils::Dim& d, utils::eDest dest);
     79     void setTransform(const int orientation, utils::eDest dest);
     80     void setPosition(const utils::Dim& dim, utils::eDest dest);
     81     void setVisualParams(const MetaData_t& data, utils::eDest dest);
     82     bool commit(utils::eDest dest);
     83     bool queueBuffer(int fd, uint32_t offset, utils::eDest dest);
     84 
     85     /* Returns available ("unallocated") pipes for a display's mixer */
     86     int availablePipes(int dpy, int mixer);
     87     /* Returns if any of the requested pipe type is attached to any of the
     88      * displays
     89      */
     90     bool isPipeTypeAttached(utils::eMdpPipeType type);
     91     /* Returns pipe dump. Expects a NULL terminated buffer of big enough size
     92      * to populate.
     93      */
     94     void getDump(char *buf, size_t len);
     95     /* Reset usage and allocation bits on all pipes for given display */
     96     void clear(int dpy);
     97     /* Marks the display, whose pipes need to be forcibaly configured */
     98     void forceSet(const int& dpy);
     99 
    100     /* Closes open pipes, called during startup */
    101     static int initOverlay();
    102     /* Returns the singleton instance of overlay */
    103     static Overlay* getInstance();
    104     static void setDMAMode(const int& mode);
    105     static int getDMAMode();
    106     /* Returns the framebuffer node backing up the display */
    107     static int getFbForDpy(const int& dpy);
    108     static bool displayCommit(const int& fd);
    109 
    110 private:
    111     /* Ctor setup */
    112     explicit Overlay();
    113     /*Validate index range, abort if invalid */
    114     void validate(int index);
    115     void dump() const;
    116 
    117     /* Just like a Facebook for pipes, but much less profile info */
    118     struct PipeBook {
    119         void init();
    120         void destroy();
    121         /* Check if pipe exists and return true, false otherwise */
    122         bool valid();
    123 
    124         /* Hardware pipe wrapper */
    125         GenericPipe *mPipe;
    126         /* Display using this pipe. Refer to enums above */
    127         int mDisplay;
    128         /* Mixer within a split display this pipe is attached to */
    129         int mMixer;
    130 
    131         /* operations on bitmap */
    132         static bool pipeUsageUnchanged();
    133         static void setUse(int index);
    134         static void resetUse(int index);
    135         static bool isUsed(int index);
    136         static bool isNotUsed(int index);
    137         static void save();
    138 
    139         static void setAllocation(int index);
    140         static void resetAllocation(int index);
    141         static bool isAllocated(int index);
    142         static bool isNotAllocated(int index);
    143 
    144         static utils::eMdpPipeType getPipeType(utils::eDest dest);
    145         static const char* getDestStr(utils::eDest dest);
    146 
    147         static int NUM_PIPES;
    148         static utils::eMdpPipeType pipeTypeLUT[utils::OV_MAX];
    149 
    150 
    151     private:
    152         //usage tracks if a successful commit happened. So a pipe could be
    153         //allocated to a display, but it may not end up using it for various
    154         //reasons. If one display actually uses a pipe then it amy not be
    155         //used by another display, without an UNSET in between.
    156         static int sPipeUsageBitmap;
    157         static int sLastUsageBitmap;
    158         //Tracks which pipe objects are allocated. This does not imply that they
    159         //will actually be used. For example, a display might choose to acquire
    160         //3 pipe objects in one shot and proceed with config only if it gets all
    161         //3. The bitmap helps allocate different pipe objects on each request.
    162         static int sAllocatedBitmap;
    163     };
    164 
    165     PipeBook mPipeBook[utils::OV_INVALID]; //Used as max
    166 
    167     /* Dump string */
    168     char mDumpStr[1024];
    169 
    170     /* Singleton Instance*/
    171     static Overlay *sInstance;
    172     static int sDpyFbMap[DPY_MAX];
    173     static int sDMAMode;
    174     static int sForceSetBitmap;
    175 };
    176 
    177 inline void Overlay::validate(int index) {
    178     OVASSERT(index >=0 && index < PipeBook::NUM_PIPES, \
    179         "%s, Index out of bounds: %d", __FUNCTION__, index);
    180     OVASSERT(mPipeBook[index].valid(), "Pipe does not exist %s",
    181             PipeBook::getDestStr((utils::eDest)index));
    182 }
    183 
    184 inline int Overlay::availablePipes(int dpy, int mixer) {
    185     int avail = 0;
    186     for(int i = 0; i < PipeBook::NUM_PIPES; i++) {
    187         if( (mPipeBook[i].mDisplay == DPY_UNUSED ||
    188              mPipeBook[i].mDisplay == dpy) &&
    189             (mPipeBook[i].mMixer == MIXER_UNUSED ||
    190              mPipeBook[i].mMixer == mixer) &&
    191             PipeBook::isNotAllocated(i) &&
    192             !(Overlay::getDMAMode() == Overlay::DMA_BLOCK_MODE &&
    193               PipeBook::getPipeType((utils::eDest)i) ==
    194               utils::OV_MDP_PIPE_DMA)) {
    195             avail++;
    196         }
    197     }
    198     return avail;
    199 }
    200 
    201 inline void Overlay::setDMAMode(const int& mode) {
    202     if(mode == DMA_LINE_MODE || mode == DMA_BLOCK_MODE)
    203         sDMAMode = mode;
    204 }
    205 
    206 inline int Overlay::getDMAMode() {
    207     return sDMAMode;
    208 }
    209 
    210 inline int Overlay::getFbForDpy(const int& dpy) {
    211     OVASSERT(dpy >= 0 && dpy < DPY_MAX, "Invalid dpy %d", dpy);
    212     return sDpyFbMap[dpy];
    213 }
    214 
    215 inline void Overlay::forceSet(const int& dpy) {
    216     sForceSetBitmap |= (1 << dpy);
    217 }
    218 
    219 inline bool Overlay::PipeBook::valid() {
    220     return (mPipe != NULL);
    221 }
    222 
    223 inline bool Overlay::PipeBook::pipeUsageUnchanged() {
    224     return (sPipeUsageBitmap == sLastUsageBitmap);
    225 }
    226 
    227 inline void Overlay::PipeBook::setUse(int index) {
    228     sPipeUsageBitmap |= (1 << index);
    229 }
    230 
    231 inline void Overlay::PipeBook::resetUse(int index) {
    232     sPipeUsageBitmap &= ~(1 << index);
    233 }
    234 
    235 inline bool Overlay::PipeBook::isUsed(int index) {
    236     return sPipeUsageBitmap & (1 << index);
    237 }
    238 
    239 inline bool Overlay::PipeBook::isNotUsed(int index) {
    240     return !isUsed(index);
    241 }
    242 
    243 inline void Overlay::PipeBook::save() {
    244     sLastUsageBitmap = sPipeUsageBitmap;
    245 }
    246 
    247 inline void Overlay::PipeBook::setAllocation(int index) {
    248     sAllocatedBitmap |= (1 << index);
    249 }
    250 
    251 inline void Overlay::PipeBook::resetAllocation(int index) {
    252     sAllocatedBitmap &= ~(1 << index);
    253 }
    254 
    255 inline bool Overlay::PipeBook::isAllocated(int index) {
    256     return sAllocatedBitmap & (1 << index);
    257 }
    258 
    259 inline bool Overlay::PipeBook::isNotAllocated(int index) {
    260     return !isAllocated(index);
    261 }
    262 
    263 inline utils::eMdpPipeType Overlay::PipeBook::getPipeType(utils::eDest dest) {
    264     return pipeTypeLUT[(int)dest];
    265 }
    266 
    267 inline const char* Overlay::PipeBook::getDestStr(utils::eDest dest) {
    268     switch(getPipeType(dest)) {
    269         case utils::OV_MDP_PIPE_RGB: return "RGB";
    270         case utils::OV_MDP_PIPE_VG: return "VG";
    271         case utils::OV_MDP_PIPE_DMA: return "DMA";
    272         default: return "Invalid";
    273     }
    274     return "Invalid";
    275 }
    276 
    277 }; // overlay
    278 
    279 #endif // OVERLAY_H
    280