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