1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
4  * Not a Contribution, Apache license notifications and license are retained
5  * for attribution purposes only.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 #include <sync/sync.h>
21 #include "overlayRotator.h"
22 #include "overlayUtils.h"
23 #include "mdp_version.h"
24 #include "gr.h"
25 
26 namespace ovutils = overlay::utils;
27 
28 namespace overlay {
29 
30 //============Rotator=========================
31 
~Rotator()32 Rotator::~Rotator() {}
33 
getRotator()34 Rotator* Rotator::getRotator() {
35     int type = getRotatorHwType();
36     if(type == TYPE_MDP) {
37         return new MdpRot(); //will do reset
38     } else if(type == TYPE_MDSS) {
39         return new MdssRot();
40     } else {
41         ALOGE("%s Unknown h/w type %d", __FUNCTION__, type);
42         return NULL;
43     }
44 }
45 
calcOutputBufSize(const utils::Whf & destWhf)46 uint32_t Rotator::calcOutputBufSize(const utils::Whf& destWhf) {
47     //dummy aligned w & h.
48     int alW = 0, alH = 0;
49     int halFormat = ovutils::getHALFormat(destWhf.format);
50     //A call into gralloc/memalloc
51     return getBufferSizeAndDimensions(
52             destWhf.w, destWhf.h, halFormat, alW, alH);
53 }
54 
getRotatorHwType()55 int Rotator::getRotatorHwType() {
56     int mdpVersion = qdutils::MDPVersion::getInstance().getMDPVersion();
57     if (mdpVersion == qdutils::MDSS_V5)
58         return TYPE_MDSS;
59     return TYPE_MDP;
60 }
61 
62 
63 //============RotMem=========================
64 
close()65 bool RotMem::close() {
66     bool ret = true;
67     for(uint32_t i=0; i < RotMem::MAX_ROT_MEM; ++i) {
68         // skip current, and if valid, close
69         if(m[i].valid()) {
70             if(m[i].close() == false) {
71                 ALOGE("%s error in closing rot mem %d", __FUNCTION__, i);
72                 ret = false;
73             }
74         }
75     }
76     return ret;
77 }
78 
Mem()79 RotMem::Mem::Mem() : mCurrOffset(0) {
80     utils::memset0(mRotOffset);
81     for(int i = 0; i < ROT_NUM_BUFS; i++) {
82         mRelFence[i] = -1;
83     }
84 }
85 
~Mem()86 RotMem::Mem::~Mem() {
87     for(int i = 0; i < ROT_NUM_BUFS; i++) {
88         ::close(mRelFence[i]);
89         mRelFence[i] = -1;
90     }
91 }
92 
setReleaseFd(const int & fence)93 void RotMem::Mem::setReleaseFd(const int& fence) {
94     int ret = 0;
95 
96     if(mRelFence[mCurrOffset] >= 0) {
97         //Wait for previous usage of this buffer to be over.
98         //Can happen if rotation takes > vsync and a fast producer. i.e queue
99         //happens in subsequent vsyncs either because content is 60fps or
100         //because the producer is hasty sometimes.
101         ret = sync_wait(mRelFence[mCurrOffset], 1000);
102         if(ret < 0) {
103             ALOGE("%s: sync_wait error!! error no = %d err str = %s",
104                 __FUNCTION__, errno, strerror(errno));
105         }
106         ::close(mRelFence[mCurrOffset]);
107     }
108     mRelFence[mCurrOffset] = fence;
109 }
110 
111 //============RotMgr=========================
112 
RotMgr()113 RotMgr::RotMgr() {
114     for(int i = 0; i < MAX_ROT_SESS; i++) {
115         mRot[i] = 0;
116     }
117     mUseCount = 0;
118     mRotDevFd = -1;
119 }
120 
~RotMgr()121 RotMgr::~RotMgr() {
122     clear();
123 }
124 
configBegin()125 void RotMgr::configBegin() {
126     //Reset the number of objects used
127     mUseCount = 0;
128 }
129 
configDone()130 void RotMgr::configDone() {
131     //Remove the top most unused objects. Videos come and go.
132     for(int i = mUseCount; i < MAX_ROT_SESS; i++) {
133         if(mRot[i]) {
134             delete mRot[i];
135             mRot[i] = 0;
136         }
137     }
138 }
139 
getNext()140 Rotator* RotMgr::getNext() {
141     //Return a rot object, creating one if necessary
142     overlay::Rotator *rot = NULL;
143     if(mUseCount >= MAX_ROT_SESS) {
144         ALOGE("%s, MAX rotator sessions reached", __func__);
145     } else {
146         if(mRot[mUseCount] == NULL)
147             mRot[mUseCount] = overlay::Rotator::getRotator();
148         rot = mRot[mUseCount++];
149     }
150     return rot;
151 }
152 
clear()153 void RotMgr::clear() {
154     //Brute force obj destruction, helpful in suspend.
155     for(int i = 0; i < MAX_ROT_SESS; i++) {
156         if(mRot[i]) {
157             delete mRot[i];
158             mRot[i] = 0;
159         }
160     }
161     mUseCount = 0;
162     ::close(mRotDevFd);
163     mRotDevFd = -1;
164 }
165 
getDump(char * buf,size_t len)166 void RotMgr::getDump(char *buf, size_t len) {
167     for(int i = 0; i < MAX_ROT_SESS; i++) {
168         if(mRot[i]) {
169             mRot[i]->getDump(buf, len);
170         }
171     }
172     char str[32] = {'\0'};
173     snprintf(str, 32, "\n================\n");
174     strncat(buf, str, strlen(str));
175 }
176 
getRotDevFd()177 int RotMgr::getRotDevFd() {
178     //2nd check just in case
179     if(mRotDevFd < 0 && Rotator::getRotatorHwType() == Rotator::TYPE_MDP) {
180         mRotDevFd = ::open("/dev/msm_rotator", O_RDWR, 0);
181         if(mRotDevFd < 0) {
182             ALOGE("%s failed to open rotator device", __FUNCTION__);
183         }
184     }
185     return mRotDevFd;
186 }
187 
188 }
189