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