blob: c9843f225a2a0f272fb26de607c5e6086f2fdee0 [file] [log] [blame]
Saurabh Shahe012f7a2012-08-18 15:11:57 -07001/*
Naseer Ahmed758bfc52012-11-28 17:02:08 -05002 * Copyright (C) 2008 The Android Open Source Project
Raj kamal23f69b22012-11-17 00:20:55 +05303 * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
Naseer Ahmed758bfc52012-11-28 17:02:08 -05004 * 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.
Saurabh Shahe012f7a2012-08-18 15:11:57 -070018*/
19
20#include "overlayUtils.h"
21#include "overlayRotator.h"
22
23namespace ovutils = overlay::utils;
24
25namespace overlay {
26
Naseer Ahmed758bfc52012-11-28 17:02:08 -050027MdpRot::MdpRot() {
28 reset();
29 init();
30}
31
32MdpRot::~MdpRot() { close(); }
33
Raj kamal23f69b22012-11-17 00:20:55 +053034inline void MdpRot::setEnable() { mRotImgInfo.enable = 1; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050035
Raj kamal23f69b22012-11-17 00:20:55 +053036inline void MdpRot::setDisable() { mRotImgInfo.enable = 0; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050037
Raj kamal23f69b22012-11-17 00:20:55 +053038inline bool MdpRot::enabled() const { return mRotImgInfo.enable; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050039
Raj kamal23f69b22012-11-17 00:20:55 +053040inline void MdpRot::setRotations(uint32_t r) { mRotImgInfo.rotations = r; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050041
Raj kamal23f69b22012-11-17 00:20:55 +053042inline int MdpRot::getDstMemId() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050043 return mRotDataInfo.dst.memory_id;
44}
45
Raj kamal23f69b22012-11-17 00:20:55 +053046inline uint32_t MdpRot::getDstOffset() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050047 return mRotDataInfo.dst.offset;
48}
49
Raj kamal23f69b22012-11-17 00:20:55 +053050inline uint32_t MdpRot::getDstFormat() const {
51 return mRotImgInfo.dst.format;
52}
Naseer Ahmed758bfc52012-11-28 17:02:08 -050053
Raj kamal23f69b22012-11-17 00:20:55 +053054inline uint32_t MdpRot::getSessId() const { return mRotImgInfo.session_id; }
55
56inline void MdpRot::setSrcFB() {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050057 mRotDataInfo.src.flags |= MDP_MEMORY_ID_TYPE_FB;
58}
59
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080060void MdpRot::setDownscale(int ds) {
Ramkumar Radhakrishnan4ca8f3a2013-02-14 18:56:50 -080061 if ((utils::ROT_DS_EIGHTH == ds) && (mRotImgInfo.src_rect.h & 0xF)) {
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080062 // Ensure src_rect.h is a multiple of 16 for 1/8 downscaling.
63 // This is an undocumented MDP Rotator constraint.
64 // Note that src_rect.h is already ensured to be 32 pixel height aligned
65 // for MDP_Y_CRCB_H2V2_TILE and MDP_Y_CBCR_H2V2_TILE formats.
Ramkumar Radhakrishnan4ca8f3a2013-02-14 18:56:50 -080066 mRotImgInfo.src_rect.h = utils::aligndown(mRotImgInfo.src_rect.h, 16);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080067 }
68 mRotImgInfo.downscale_ratio = ds;
69}
70
Raj kamal23f69b22012-11-17 00:20:55 +053071inline void MdpRot::save() {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050072 mLSRotImgInfo = mRotImgInfo;
73}
74
Raj kamal23f69b22012-11-17 00:20:55 +053075inline bool MdpRot::rotConfChanged() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050076 // 0 means same
77 if(0 == ::memcmp(&mRotImgInfo, &mLSRotImgInfo,
78 sizeof (msm_rotator_img_info))) {
79 return false;
80 }
81 return true;
82}
83
Saurabh Shahe012f7a2012-08-18 15:11:57 -070084bool MdpRot::init()
85{
86 if(!mFd.open(Res::rotPath, O_RDWR)){
87 ALOGE("MdpRot failed to init %s", Res::rotPath);
88 return false;
89 }
90 return true;
91}
92
93void MdpRot::setSource(const overlay::utils::Whf& awhf) {
94 utils::Whf whf(awhf);
95
96 mRotImgInfo.src.format = whf.format;
97 if(whf.format == MDP_Y_CRCB_H2V2_TILE ||
98 whf.format == MDP_Y_CBCR_H2V2_TILE) {
99 whf.w = utils::alignup(awhf.w, 64);
100 whf.h = utils::alignup(awhf.h, 32);
101 }
102
103 mRotImgInfo.src.width = whf.w;
104 mRotImgInfo.src.height = whf.h;
105
106 mRotImgInfo.src_rect.w = whf.w;
107 mRotImgInfo.src_rect.h = whf.h;
108
109 mRotImgInfo.dst.width = whf.w;
110 mRotImgInfo.dst.height = whf.h;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700111}
112
Raj kamal23f69b22012-11-17 00:20:55 +0530113inline void MdpRot::setFlags(const utils::eMdpFlags& flags) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700114 mRotImgInfo.secure = 0;
115 if(flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
116 mRotImgInfo.secure = 1;
117}
118
Raj kamal23f69b22012-11-17 00:20:55 +0530119inline void MdpRot::setTransform(const utils::eTransform& rot)
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700120{
121 int r = utils::getMdpOrient(rot);
122 setRotations(r);
123 //getMdpOrient will switch the flips if the source is 90 rotated.
124 //Clients in Android dont factor in 90 rotation while deciding the flip.
125 mOrientation = static_cast<utils::eTransform>(r);
126 ALOGE_IF(DEBUG_OVERLAY, "%s: r=%d", __FUNCTION__, r);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800127}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700128
Raj kamal23f69b22012-11-17 00:20:55 +0530129inline void MdpRot::setRotatorUsed(const bool& rotUsed) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700130 setDisable();
131 if(rotUsed) {
132 setEnable();
133 }
134}
135
Raj kamal23f69b22012-11-17 00:20:55 +0530136inline void MdpRot::doTransform() {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700137 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
138 utils::swap(mRotImgInfo.dst.width, mRotImgInfo.dst.height);
139}
140
141bool MdpRot::commit() {
142 doTransform();
Saurabh Shahc23b3802012-08-31 11:11:30 -0700143 if(rotConfChanged()) {
144 if(!overlay::mdp_wrapper::startRotator(mFd.getFD(), mRotImgInfo)) {
145 ALOGE("MdpRot commit failed");
146 dump();
147 return false;
148 }
149 save();
150 mRotDataInfo.session_id = mRotImgInfo.session_id;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700151 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700152 return true;
153}
154
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800155uint32_t MdpRot::calcOutputBufSize() {
156 ovutils::Whf destWhf(mRotImgInfo.dst.width,
157 mRotImgInfo.dst.height, mRotImgInfo.dst.format);
158 return Rotator::calcOutputBufSize(destWhf);
159}
160
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700161bool MdpRot::open_i(uint32_t numbufs, uint32_t bufsz)
162{
163 OvMem mem;
164
165 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
166
167 if(!mem.open(numbufs, bufsz, mRotImgInfo.secure)){
168 ALOGE("%s: Failed to open", __func__);
169 mem.close();
170 return false;
171 }
172
173 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
174 OVASSERT(mem.getFD() != -1, "getFd is -1");
175
176 mRotDataInfo.dst.memory_id = mem.getFD();
177 mRotDataInfo.dst.offset = 0;
178 mMem.curr().m = mem;
179 return true;
180}
181
182bool MdpRot::close() {
183 bool success = true;
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500184 if(mFd.valid() && (getSessId() != 0)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700185 if(!mdp_wrapper::endRotator(mFd.getFD(), getSessId())) {
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500186 ALOGE("Mdp Rot error endRotator, fd=%d sessId=%u",
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700187 mFd.getFD(), getSessId());
188 success = false;
189 }
190 }
191 if (!mFd.close()) {
192 ALOGE("Mdp Rot error closing fd");
193 success = false;
194 }
195 if (!mMem.close()) {
196 ALOGE("Mdp Rot error closing mem");
197 success = false;
198 }
199 reset();
200 return success;
201}
202
203bool MdpRot::remap(uint32_t numbufs) {
204 // if current size changed, remap
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800205 uint32_t opBufSize = calcOutputBufSize();
206 if(opBufSize == mMem.curr().size()) {
207 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, opBufSize);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700208 return true;
209 }
210
211 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
212 OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
213
214 // ++mMem will make curr to be prev, and prev will be curr
215 ++mMem;
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800216 if(!open_i(numbufs, opBufSize)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700217 ALOGE("%s Error could not open", __FUNCTION__);
218 return false;
219 }
220 for (uint32_t i = 0; i < numbufs; ++i) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800221 mMem.curr().mRotOffset[i] = i * opBufSize;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700222 }
223 return true;
224}
225
226void MdpRot::reset() {
227 ovutils::memset0(mRotImgInfo);
Saurabh Shahc23b3802012-08-31 11:11:30 -0700228 ovutils::memset0(mLSRotImgInfo);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700229 ovutils::memset0(mRotDataInfo);
230 ovutils::memset0(mMem.curr().mRotOffset);
231 ovutils::memset0(mMem.prev().mRotOffset);
232 mMem.curr().mCurrOffset = 0;
233 mMem.prev().mCurrOffset = 0;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700234 mOrientation = utils::OVERLAY_TRANSFORM_0;
235}
236
237bool MdpRot::queueBuffer(int fd, uint32_t offset) {
238 if(enabled()) {
239 mRotDataInfo.src.memory_id = fd;
240 mRotDataInfo.src.offset = offset;
241
242 remap(RotMem::Mem::ROT_NUM_BUFS);
243 OVASSERT(mMem.curr().m.numBufs(),
244 "queueBuffer numbufs is 0");
245 mRotDataInfo.dst.offset =
246 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
247 mMem.curr().mCurrOffset =
248 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
249
250 if(!overlay::mdp_wrapper::rotate(mFd.getFD(), mRotDataInfo)) {
251 ALOGE("MdpRot failed rotate");
252 dump();
253 return false;
254 }
255
256 // if the prev mem is valid, we need to close
257 if(mMem.prev().valid()) {
258 // FIXME if no wait for vsync the above
259 // play will return immediatly and might cause
260 // tearing when prev.close is called.
261 if(!mMem.prev().close()) {
262 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
263 return false;
264 }
265 }
266 }
267 return true;
268}
269
270void MdpRot::dump() const {
271 ALOGE("== Dump MdpRot start ==");
272 mFd.dump();
273 mMem.curr().m.dump();
274 mdp_wrapper::dump("mRotImgInfo", mRotImgInfo);
275 mdp_wrapper::dump("mRotDataInfo", mRotDataInfo);
276 ALOGE("== Dump MdpRot end ==");
277}
278
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800279void MdpRot::getDump(char *buf, size_t len) const {
280 ovutils::getDump(buf, len, "MdpRotCtrl(msm_rotator_img_info)", mRotImgInfo);
281 ovutils::getDump(buf, len, "MdpRotData(msm_rotator_data_info)", mRotDataInfo);
282}
283
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700284} // namespace overlay