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 "overlayUtils.h"
21 #include "overlayRotator.h"
22
23 namespace ovutils = overlay::utils;
24
25 namespace overlay {
26
MdpRot()27 MdpRot::MdpRot() {
28 reset();
29 init();
30 }
31
~MdpRot()32 MdpRot::~MdpRot() { close(); }
33
setEnable()34 void MdpRot::setEnable() { mRotImgInfo.enable = 1; }
35
setDisable()36 void MdpRot::setDisable() { mRotImgInfo.enable = 0; }
37
enabled() const38 bool MdpRot::enabled() const { return mRotImgInfo.enable; }
39
setRotations(uint32_t r)40 void MdpRot::setRotations(uint32_t r) { mRotImgInfo.rotations = r; }
41
getDstMemId() const42 int MdpRot::getDstMemId() const {
43 return mRotDataInfo.dst.memory_id;
44 }
45
getDstOffset() const46 uint32_t MdpRot::getDstOffset() const {
47 return mRotDataInfo.dst.offset;
48 }
49
getSessId() const50 uint32_t MdpRot::getSessId() const { return mRotImgInfo.session_id; }
51
setSrcFB()52 void MdpRot::setSrcFB() {
53 mRotDataInfo.src.flags |= MDP_MEMORY_ID_TYPE_FB;
54 }
55
save()56 void MdpRot::save() {
57 mLSRotImgInfo = mRotImgInfo;
58 }
59
rotConfChanged() const60 bool MdpRot::rotConfChanged() const {
61 // 0 means same
62 if(0 == ::memcmp(&mRotImgInfo, &mLSRotImgInfo,
63 sizeof (msm_rotator_img_info))) {
64 return false;
65 }
66 return true;
67 }
68
init()69 bool MdpRot::init()
70 {
71 if(!mFd.open(Res::rotPath, O_RDWR)){
72 ALOGE("MdpRot failed to init %s", Res::rotPath);
73 return false;
74 }
75 return true;
76 }
77
setSource(const overlay::utils::Whf & awhf)78 void MdpRot::setSource(const overlay::utils::Whf& awhf) {
79 utils::Whf whf(awhf);
80
81 mRotImgInfo.src.format = whf.format;
82 if(whf.format == MDP_Y_CRCB_H2V2_TILE ||
83 whf.format == MDP_Y_CBCR_H2V2_TILE) {
84 whf.w = utils::alignup(awhf.w, 64);
85 whf.h = utils::alignup(awhf.h, 32);
86 }
87
88 mRotImgInfo.src.width = whf.w;
89 mRotImgInfo.src.height = whf.h;
90
91 mRotImgInfo.src_rect.w = whf.w;
92 mRotImgInfo.src_rect.h = whf.h;
93
94 mRotImgInfo.dst.width = whf.w;
95 mRotImgInfo.dst.height = whf.h;
96
97 mBufSize = awhf.size;
98 }
99
setFlags(const utils::eMdpFlags & flags)100 void MdpRot::setFlags(const utils::eMdpFlags& flags) {
101 mRotImgInfo.secure = 0;
102 if(flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
103 mRotImgInfo.secure = 1;
104 }
105
setTransform(const utils::eTransform & rot,const bool & rotUsed)106 void MdpRot::setTransform(const utils::eTransform& rot, const bool& rotUsed)
107 {
108 int r = utils::getMdpOrient(rot);
109 setRotations(r);
110 //getMdpOrient will switch the flips if the source is 90 rotated.
111 //Clients in Android dont factor in 90 rotation while deciding the flip.
112 mOrientation = static_cast<utils::eTransform>(r);
113 ALOGE_IF(DEBUG_OVERLAY, "%s: r=%d", __FUNCTION__, r);
114
115 setDisable();
116 if(rotUsed) {
117 setEnable();
118 }
119 }
120
doTransform()121 void MdpRot::doTransform() {
122 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
123 utils::swap(mRotImgInfo.dst.width, mRotImgInfo.dst.height);
124 }
125
commit()126 bool MdpRot::commit() {
127 doTransform();
128 if(rotConfChanged()) {
129 if(!overlay::mdp_wrapper::startRotator(mFd.getFD(), mRotImgInfo)) {
130 ALOGE("MdpRot commit failed");
131 dump();
132 return false;
133 }
134 save();
135 mRotDataInfo.session_id = mRotImgInfo.session_id;
136 }
137 return true;
138 }
139
open_i(uint32_t numbufs,uint32_t bufsz)140 bool MdpRot::open_i(uint32_t numbufs, uint32_t bufsz)
141 {
142 OvMem mem;
143
144 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
145
146 if(!mem.open(numbufs, bufsz, mRotImgInfo.secure)){
147 ALOGE("%s: Failed to open", __func__);
148 mem.close();
149 return false;
150 }
151
152 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
153 OVASSERT(mem.getFD() != -1, "getFd is -1");
154
155 mRotDataInfo.dst.memory_id = mem.getFD();
156 mRotDataInfo.dst.offset = 0;
157 mMem.curr().m = mem;
158 return true;
159 }
160
close()161 bool MdpRot::close() {
162 bool success = true;
163 if(mFd.valid() && (getSessId() != 0)) {
164 if(!mdp_wrapper::endRotator(mFd.getFD(), getSessId())) {
165 ALOGE("Mdp Rot error endRotator, fd=%d sessId=%u",
166 mFd.getFD(), getSessId());
167 success = false;
168 }
169 }
170 if (!mFd.close()) {
171 ALOGE("Mdp Rot error closing fd");
172 success = false;
173 }
174 if (!mMem.close()) {
175 ALOGE("Mdp Rot error closing mem");
176 success = false;
177 }
178 reset();
179 return success;
180 }
181
remap(uint32_t numbufs)182 bool MdpRot::remap(uint32_t numbufs) {
183 // if current size changed, remap
184 if(mBufSize == mMem.curr().size()) {
185 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, mBufSize);
186 return true;
187 }
188
189 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
190 OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
191
192 // ++mMem will make curr to be prev, and prev will be curr
193 ++mMem;
194 if(!open_i(numbufs, mBufSize)) {
195 ALOGE("%s Error could not open", __FUNCTION__);
196 return false;
197 }
198 for (uint32_t i = 0; i < numbufs; ++i) {
199 mMem.curr().mRotOffset[i] = i * mBufSize;
200 }
201 return true;
202 }
203
reset()204 void MdpRot::reset() {
205 ovutils::memset0(mRotImgInfo);
206 ovutils::memset0(mLSRotImgInfo);
207 ovutils::memset0(mRotDataInfo);
208 ovutils::memset0(mMem.curr().mRotOffset);
209 ovutils::memset0(mMem.prev().mRotOffset);
210 mMem.curr().mCurrOffset = 0;
211 mMem.prev().mCurrOffset = 0;
212 mBufSize = 0;
213 mOrientation = utils::OVERLAY_TRANSFORM_0;
214 }
215
queueBuffer(int fd,uint32_t offset)216 bool MdpRot::queueBuffer(int fd, uint32_t offset) {
217 if(enabled()) {
218 mRotDataInfo.src.memory_id = fd;
219 mRotDataInfo.src.offset = offset;
220
221 remap(RotMem::Mem::ROT_NUM_BUFS);
222 OVASSERT(mMem.curr().m.numBufs(),
223 "queueBuffer numbufs is 0");
224 mRotDataInfo.dst.offset =
225 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
226 mMem.curr().mCurrOffset =
227 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
228
229 if(!overlay::mdp_wrapper::rotate(mFd.getFD(), mRotDataInfo)) {
230 ALOGE("MdpRot failed rotate");
231 dump();
232 return false;
233 }
234
235 // if the prev mem is valid, we need to close
236 if(mMem.prev().valid()) {
237 // FIXME if no wait for vsync the above
238 // play will return immediatly and might cause
239 // tearing when prev.close is called.
240 if(!mMem.prev().close()) {
241 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
242 return false;
243 }
244 }
245 }
246 return true;
247 }
248
dump() const249 void MdpRot::dump() const {
250 ALOGE("== Dump MdpRot start ==");
251 mFd.dump();
252 mMem.curr().m.dump();
253 mdp_wrapper::dump("mRotImgInfo", mRotImgInfo);
254 mdp_wrapper::dump("mRotDataInfo", mRotDataInfo);
255 ALOGE("== Dump MdpRot end ==");
256 }
257
258 } // namespace overlay
259