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