overlay.cpp revision 5ceb9c6a763418d5e0cf5da4e74b7a7c733fb4b1
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#include "overlay.h"
31#include "pipes/overlayGenPipe.h"
32#include "mdp_version.h"
33#include "qdMetaData.h"
34
35#define PIPE_DEBUG 0
36
37namespace overlay {
38using namespace utils;
39
40Overlay::Overlay() {
41    PipeBook::NUM_PIPES = qdutils::MDPVersion::getInstance().getTotalPipes();
42    for(int i = 0; i < PipeBook::NUM_PIPES; i++) {
43        mPipeBook[i].init();
44    }
45
46    mDumpStr[0] = '\0';
47}
48
49Overlay::~Overlay() {
50    for(int i = 0; i < PipeBook::NUM_PIPES; i++) {
51        mPipeBook[i].destroy();
52    }
53}
54
55void Overlay::configBegin() {
56    for(int i = 0; i < PipeBook::NUM_PIPES; i++) {
57        //Mark as available for this round.
58        PipeBook::resetUse(i);
59        PipeBook::resetAllocation(i);
60    }
61    mDumpStr[0] = '\0';
62}
63
64void Overlay::configDone() {
65    if(PipeBook::pipeUsageUnchanged()) return;
66
67    for(int i = 0; i < PipeBook::NUM_PIPES; i++) {
68        if(PipeBook::isNotUsed(i)) {
69            //Forces UNSET on pipes, flushes rotator memory and session, closes
70            //fds
71            if(mPipeBook[i].valid()) {
72                char str[32];
73                sprintf(str, "Unset pipe=%s dpy=%d; ",
74                        PipeBook::getDestStr((eDest)i), mPipeBook[i].mDisplay);
75                strncat(mDumpStr, str, strlen(str));
76            }
77            mPipeBook[i].destroy();
78        }
79    }
80    dump();
81    PipeBook::save();
82}
83
84eDest Overlay::nextPipe(eMdpPipeType type, int dpy) {
85    eDest dest = OV_INVALID;
86
87    for(int i = 0; i < PipeBook::NUM_PIPES; i++) {
88        //Match requested pipe type
89        if(type == OV_MDP_PIPE_ANY || type == PipeBook::getPipeType((eDest)i)) {
90            //If the pipe is not allocated to any display or used by the
91            //requesting display already in previous round.
92            if((mPipeBook[i].mDisplay == DPY_UNUSED ||
93                    mPipeBook[i].mDisplay == dpy) &&
94                    PipeBook::isNotAllocated(i)) {
95                //In block mode we don't allow line operations
96                if(sDMAMode == DMA_BLOCK_MODE &&
97                    PipeBook::getPipeType((eDest)i) == OV_MDP_PIPE_DMA)
98                    continue;
99
100                dest = (eDest)i;
101                PipeBook::setAllocation(i);
102                break;
103            }
104        }
105    }
106
107    if(dest != OV_INVALID) {
108        int index = (int)dest;
109        //If the pipe is not registered with any display OR if the pipe is
110        //requested again by the same display using it, then go ahead.
111        mPipeBook[index].mDisplay = dpy;
112        if(not mPipeBook[index].valid()) {
113            mPipeBook[index].mPipe = new GenericPipe(dpy);
114            char str[32];
115            snprintf(str, 32, "Set pipe=%s dpy=%d; ",
116                     PipeBook::getDestStr(dest), dpy);
117            strncat(mDumpStr, str, strlen(str));
118        }
119    } else {
120        ALOGD_IF(PIPE_DEBUG, "Pipe unavailable type=%d display=%d",
121                (int)type, dpy);
122    }
123
124    return dest;
125}
126
127bool Overlay::isPipeTypeAttached(eMdpPipeType type) {
128    for(int i = 0; i < PipeBook::NUM_PIPES; i++) {
129        if(type == PipeBook::getPipeType((eDest)i) &&
130                mPipeBook[i].mDisplay != DPY_UNUSED) {
131            return true;
132        }
133    }
134    return false;
135}
136
137bool Overlay::commit(utils::eDest dest) {
138    bool ret = false;
139    int index = (int)dest;
140    validate(index);
141
142    if(mPipeBook[index].mPipe->commit()) {
143        ret = true;
144        PipeBook::setUse((int)dest);
145    } else {
146        int dpy = mPipeBook[index].mDisplay;
147        for(int i = 0; i < PipeBook::NUM_PIPES; i++)
148            if (mPipeBook[i].mDisplay == dpy) {
149                PipeBook::resetAllocation(i);
150                PipeBook::resetUse(i);
151                if(mPipeBook[i].valid()) {
152                    mPipeBook[i].mPipe->forceSet();
153                }
154            }
155    }
156    return ret;
157}
158
159bool Overlay::queueBuffer(int fd, uint32_t offset,
160        utils::eDest dest) {
161    int index = (int)dest;
162    bool ret = false;
163    validate(index);
164    //Queue only if commit() has succeeded (and the bit set)
165    if(PipeBook::isUsed((int)dest)) {
166        ret = mPipeBook[index].mPipe->queueBuffer(fd, offset);
167    }
168    return ret;
169}
170
171void Overlay::setCrop(const utils::Dim& d,
172        utils::eDest dest) {
173    int index = (int)dest;
174    validate(index);
175    mPipeBook[index].mPipe->setCrop(d);
176}
177
178void Overlay::setPosition(const utils::Dim& d,
179        utils::eDest dest) {
180    int index = (int)dest;
181    validate(index);
182    mPipeBook[index].mPipe->setPosition(d);
183}
184
185void Overlay::setTransform(const int orient,
186        utils::eDest dest) {
187    int index = (int)dest;
188    validate(index);
189
190    utils::eTransform transform =
191            static_cast<utils::eTransform>(orient);
192    mPipeBook[index].mPipe->setTransform(transform);
193
194}
195
196void Overlay::setSource(const utils::PipeArgs args,
197        utils::eDest dest) {
198    int index = (int)dest;
199    validate(index);
200
201    PipeArgs newArgs(args);
202    if(PipeBook::getPipeType(dest) == OV_MDP_PIPE_VG) {
203        setMdpFlags(newArgs.mdpFlags, OV_MDP_PIPE_SHARE);
204    } else {
205        clearMdpFlags(newArgs.mdpFlags, OV_MDP_PIPE_SHARE);
206    }
207
208    if(PipeBook::getPipeType(dest) == OV_MDP_PIPE_DMA) {
209        setMdpFlags(newArgs.mdpFlags, OV_MDP_PIPE_FORCE_DMA);
210    } else {
211        clearMdpFlags(newArgs.mdpFlags, OV_MDP_PIPE_FORCE_DMA);
212    }
213
214    mPipeBook[index].mPipe->setSource(newArgs);
215}
216
217void Overlay::setVisualParams(const MetaData_t& metadata, utils::eDest dest) {
218    int index = (int)dest;
219    validate(index);
220    mPipeBook[index].mPipe->setVisualParams(metadata);
221}
222
223Overlay* Overlay::getInstance() {
224    if(sInstance == NULL) {
225        sInstance = new Overlay();
226    }
227    return sInstance;
228}
229
230// Clears any VG pipes allocated to the fb devices
231// Generates a LUT for pipe types.
232int Overlay::initOverlay() {
233    int mdpVersion = qdutils::MDPVersion::getInstance().getMDPVersion();
234    int numPipesXType[OV_MDP_PIPE_ANY] = {0};
235    numPipesXType[OV_MDP_PIPE_RGB] =
236            qdutils::MDPVersion::getInstance().getRGBPipes();
237    numPipesXType[OV_MDP_PIPE_VG] =
238            qdutils::MDPVersion::getInstance().getVGPipes();
239    numPipesXType[OV_MDP_PIPE_DMA] =
240            qdutils::MDPVersion::getInstance().getDMAPipes();
241
242    int index = 0;
243    for(int X = 0; X < (int)OV_MDP_PIPE_ANY; X++) { //iterate over types
244        for(int j = 0; j < numPipesXType[X]; j++) { //iterate over num
245            PipeBook::pipeTypeLUT[index] = (utils::eMdpPipeType)X;
246            index++;
247        }
248    }
249
250    if (mdpVersion < qdutils::MDSS_V5 && mdpVersion != qdutils::MDP_V3_0_4) {
251        msmfb_mixer_info_req  req;
252        mdp_mixer_info *minfo = NULL;
253        char name[64];
254        int fd = -1;
255        for(int i = 0; i < MAX_FB_DEVICES; i++) {
256            snprintf(name, 64, FB_DEVICE_TEMPLATE, i);
257            ALOGD("initoverlay:: opening the device:: %s", name);
258            fd = ::open(name, O_RDWR, 0);
259            if(fd < 0) {
260                ALOGE("cannot open framebuffer(%d)", i);
261                return -1;
262            }
263            //Get the mixer configuration */
264            req.mixer_num = i;
265            if (ioctl(fd, MSMFB_MIXER_INFO, &req) == -1) {
266                ALOGE("ERROR: MSMFB_MIXER_INFO ioctl failed");
267                close(fd);
268                return -1;
269            }
270            minfo = req.info;
271            for (int j = 0; j < req.cnt; j++) {
272                ALOGD("ndx=%d num=%d z_order=%d", minfo->pndx, minfo->pnum,
273                      minfo->z_order);
274                // clear any pipe connected to mixer including base pipe.
275                int index = minfo->pndx;
276                ALOGD("Unset overlay with index: %d at mixer %d", index, i);
277                if(ioctl(fd, MSMFB_OVERLAY_UNSET, &index) == -1) {
278                    ALOGE("ERROR: MSMFB_OVERLAY_UNSET failed");
279                    close(fd);
280                    return -1;
281                }
282                minfo++;
283            }
284            close(fd);
285            fd = -1;
286        }
287    }
288
289    FILE *displayDeviceFP = NULL;
290    const int MAX_FRAME_BUFFER_NAME_SIZE = 128;
291    char fbType[MAX_FRAME_BUFFER_NAME_SIZE];
292    char msmFbTypePath[MAX_FRAME_BUFFER_NAME_SIZE];
293    const char *strDtvPanel = "dtv panel";
294    const char *strWbPanel = "writeback panel";
295
296    for(int num = 1; num < MAX_FB_DEVICES; num++) {
297        snprintf (msmFbTypePath, sizeof(msmFbTypePath),
298                "/sys/class/graphics/fb%d/msm_fb_type", num);
299        displayDeviceFP = fopen(msmFbTypePath, "r");
300
301        if(displayDeviceFP){
302            fread(fbType, sizeof(char), MAX_FRAME_BUFFER_NAME_SIZE,
303                    displayDeviceFP);
304
305            if(strncmp(fbType, strDtvPanel, strlen(strDtvPanel)) == 0) {
306                sDpyFbMap[DPY_EXTERNAL] = num;
307            } else if(strncmp(fbType, strWbPanel, strlen(strWbPanel)) == 0) {
308                sDpyFbMap[DPY_WRITEBACK] = num;
309            }
310
311            fclose(displayDeviceFP);
312        }
313    }
314
315    return 0;
316}
317
318bool Overlay::displayCommit(const int& fd) {
319    //Commit
320    struct mdp_display_commit info;
321    memset(&info, 0, sizeof(struct mdp_display_commit));
322    info.flags = MDP_DISPLAY_COMMIT_OVERLAY;
323    if(!mdp_wrapper::displayCommit(fd, info)) {
324       ALOGE("%s: commit failed", __func__);
325       return false;
326    }
327    return true;
328}
329
330void Overlay::dump() const {
331    if(strlen(mDumpStr)) { //dump only on state change
332        ALOGD_IF(PIPE_DEBUG, "%s\n", mDumpStr);
333    }
334}
335
336void Overlay::getDump(char *buf, size_t len) {
337    int totalPipes = 0;
338    const char *str = "\nOverlay State\n\n";
339    strncat(buf, str, strlen(str));
340    for(int i = 0; i < PipeBook::NUM_PIPES; i++) {
341        if(mPipeBook[i].valid()) {
342            mPipeBook[i].mPipe->getDump(buf, len);
343            char str[64] = {'\0'};
344            snprintf(str, 64, "Display=%d\n\n", mPipeBook[i].mDisplay);
345            strncat(buf, str, strlen(str));
346            totalPipes++;
347        }
348    }
349    char str_pipes[64] = {'\0'};
350    snprintf(str_pipes, 64, "Pipes=%d\n\n", totalPipes);
351    strncat(buf, str_pipes, strlen(str_pipes));
352}
353
354void Overlay::clear(int dpy) {
355    for(int i = 0; i < PipeBook::NUM_PIPES; i++) {
356        if (mPipeBook[i].mDisplay == dpy) {
357            // Mark as available for this round
358            PipeBook::resetUse(i);
359            PipeBook::resetAllocation(i);
360            if(mPipeBook[i].valid()) {
361                mPipeBook[i].mPipe->forceSet();
362            }
363        }
364    }
365}
366
367void Overlay::PipeBook::init() {
368    mPipe = NULL;
369    mDisplay = DPY_UNUSED;
370}
371
372void Overlay::PipeBook::destroy() {
373    if(mPipe) {
374        delete mPipe;
375        mPipe = NULL;
376    }
377    mDisplay = DPY_UNUSED;
378}
379
380Overlay* Overlay::sInstance = 0;
381int Overlay::sExtFbIndex = 1;
382int Overlay::sDpyFbMap[DPY_MAX] = {0, -1, -1};
383int Overlay::sDMAMode = DMA_LINE_MODE;
384int Overlay::PipeBook::NUM_PIPES = 0;
385int Overlay::PipeBook::sPipeUsageBitmap = 0;
386int Overlay::PipeBook::sLastUsageBitmap = 0;
387int Overlay::PipeBook::sAllocatedBitmap = 0;
388utils::eMdpPipeType Overlay::PipeBook::pipeTypeLUT[utils::OV_MAX] =
389    {utils::OV_MDP_PIPE_ANY};
390
391}; // namespace overlay
392