blob: d0a9ee5a3e9534e27df2b87b8c8c1665ce7c662f [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) {
61 if (mRotImgInfo.src.format == MDP_Y_CR_CB_GH2V2 &&
62 (mRotImgInfo.src_rect.h & 0xF)) {
63 mRotImgInfo.src_rect.h = utils::aligndown(mRotImgInfo.src_rect.h, 16);
64 } else if ((utils::ROT_DS_EIGHTH == ds) && (mRotImgInfo.src_rect.h & 0xF)) {
65 // Ensure src_rect.h is a multiple of 16 for 1/8 downscaling.
66 // This is an undocumented MDP Rotator constraint.
67 // Note that src_rect.h is already ensured to be 32 pixel height aligned
68 // for MDP_Y_CRCB_H2V2_TILE and MDP_Y_CBCR_H2V2_TILE formats.
69 mRotImgInfo.src_rect.h = utils::alignup(mRotImgInfo.src_rect.h, 16);
70 }
71 mRotImgInfo.downscale_ratio = ds;
72}
73
Raj kamal23f69b22012-11-17 00:20:55 +053074inline void MdpRot::save() {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050075 mLSRotImgInfo = mRotImgInfo;
76}
77
Raj kamal23f69b22012-11-17 00:20:55 +053078inline bool MdpRot::rotConfChanged() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050079 // 0 means same
80 if(0 == ::memcmp(&mRotImgInfo, &mLSRotImgInfo,
81 sizeof (msm_rotator_img_info))) {
82 return false;
83 }
84 return true;
85}
86
Saurabh Shahe012f7a2012-08-18 15:11:57 -070087bool MdpRot::init()
88{
89 if(!mFd.open(Res::rotPath, O_RDWR)){
90 ALOGE("MdpRot failed to init %s", Res::rotPath);
91 return false;
92 }
93 return true;
94}
95
96void MdpRot::setSource(const overlay::utils::Whf& awhf) {
97 utils::Whf whf(awhf);
98
99 mRotImgInfo.src.format = whf.format;
100 if(whf.format == MDP_Y_CRCB_H2V2_TILE ||
101 whf.format == MDP_Y_CBCR_H2V2_TILE) {
102 whf.w = utils::alignup(awhf.w, 64);
103 whf.h = utils::alignup(awhf.h, 32);
104 }
105
106 mRotImgInfo.src.width = whf.w;
107 mRotImgInfo.src.height = whf.h;
108
109 mRotImgInfo.src_rect.w = whf.w;
110 mRotImgInfo.src_rect.h = whf.h;
111
112 mRotImgInfo.dst.width = whf.w;
113 mRotImgInfo.dst.height = whf.h;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700114}
115
Raj kamal23f69b22012-11-17 00:20:55 +0530116inline void MdpRot::setFlags(const utils::eMdpFlags& flags) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700117 mRotImgInfo.secure = 0;
118 if(flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
119 mRotImgInfo.secure = 1;
120}
121
Raj kamal23f69b22012-11-17 00:20:55 +0530122inline void MdpRot::setTransform(const utils::eTransform& rot)
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700123{
124 int r = utils::getMdpOrient(rot);
125 setRotations(r);
126 //getMdpOrient will switch the flips if the source is 90 rotated.
127 //Clients in Android dont factor in 90 rotation while deciding the flip.
128 mOrientation = static_cast<utils::eTransform>(r);
129 ALOGE_IF(DEBUG_OVERLAY, "%s: r=%d", __FUNCTION__, r);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800130}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700131
Raj kamal23f69b22012-11-17 00:20:55 +0530132inline void MdpRot::setRotatorUsed(const bool& rotUsed) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700133 setDisable();
134 if(rotUsed) {
135 setEnable();
136 }
137}
138
Raj kamal23f69b22012-11-17 00:20:55 +0530139inline void MdpRot::doTransform() {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700140 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
141 utils::swap(mRotImgInfo.dst.width, mRotImgInfo.dst.height);
142}
143
144bool MdpRot::commit() {
145 doTransform();
Saurabh Shahc23b3802012-08-31 11:11:30 -0700146 if(rotConfChanged()) {
147 if(!overlay::mdp_wrapper::startRotator(mFd.getFD(), mRotImgInfo)) {
148 ALOGE("MdpRot commit failed");
149 dump();
150 return false;
151 }
152 save();
153 mRotDataInfo.session_id = mRotImgInfo.session_id;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700154 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700155 return true;
156}
157
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800158uint32_t MdpRot::calcOutputBufSize() {
159 ovutils::Whf destWhf(mRotImgInfo.dst.width,
160 mRotImgInfo.dst.height, mRotImgInfo.dst.format);
161 return Rotator::calcOutputBufSize(destWhf);
162}
163
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700164bool MdpRot::open_i(uint32_t numbufs, uint32_t bufsz)
165{
166 OvMem mem;
167
168 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
169
170 if(!mem.open(numbufs, bufsz, mRotImgInfo.secure)){
171 ALOGE("%s: Failed to open", __func__);
172 mem.close();
173 return false;
174 }
175
176 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
177 OVASSERT(mem.getFD() != -1, "getFd is -1");
178
179 mRotDataInfo.dst.memory_id = mem.getFD();
180 mRotDataInfo.dst.offset = 0;
181 mMem.curr().m = mem;
182 return true;
183}
184
185bool MdpRot::close() {
186 bool success = true;
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500187 if(mFd.valid() && (getSessId() != 0)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700188 if(!mdp_wrapper::endRotator(mFd.getFD(), getSessId())) {
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500189 ALOGE("Mdp Rot error endRotator, fd=%d sessId=%u",
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700190 mFd.getFD(), getSessId());
191 success = false;
192 }
193 }
194 if (!mFd.close()) {
195 ALOGE("Mdp Rot error closing fd");
196 success = false;
197 }
198 if (!mMem.close()) {
199 ALOGE("Mdp Rot error closing mem");
200 success = false;
201 }
202 reset();
203 return success;
204}
205
206bool MdpRot::remap(uint32_t numbufs) {
207 // if current size changed, remap
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800208 uint32_t opBufSize = calcOutputBufSize();
209 if(opBufSize == mMem.curr().size()) {
210 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, opBufSize);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700211 return true;
212 }
213
214 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
215 OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
216
217 // ++mMem will make curr to be prev, and prev will be curr
218 ++mMem;
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800219 if(!open_i(numbufs, opBufSize)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700220 ALOGE("%s Error could not open", __FUNCTION__);
221 return false;
222 }
223 for (uint32_t i = 0; i < numbufs; ++i) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800224 mMem.curr().mRotOffset[i] = i * opBufSize;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700225 }
226 return true;
227}
228
229void MdpRot::reset() {
230 ovutils::memset0(mRotImgInfo);
Saurabh Shahc23b3802012-08-31 11:11:30 -0700231 ovutils::memset0(mLSRotImgInfo);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700232 ovutils::memset0(mRotDataInfo);
233 ovutils::memset0(mMem.curr().mRotOffset);
234 ovutils::memset0(mMem.prev().mRotOffset);
235 mMem.curr().mCurrOffset = 0;
236 mMem.prev().mCurrOffset = 0;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700237 mOrientation = utils::OVERLAY_TRANSFORM_0;
238}
239
240bool MdpRot::queueBuffer(int fd, uint32_t offset) {
241 if(enabled()) {
242 mRotDataInfo.src.memory_id = fd;
243 mRotDataInfo.src.offset = offset;
244
245 remap(RotMem::Mem::ROT_NUM_BUFS);
246 OVASSERT(mMem.curr().m.numBufs(),
247 "queueBuffer numbufs is 0");
248 mRotDataInfo.dst.offset =
249 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
250 mMem.curr().mCurrOffset =
251 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
252
253 if(!overlay::mdp_wrapper::rotate(mFd.getFD(), mRotDataInfo)) {
254 ALOGE("MdpRot failed rotate");
255 dump();
256 return false;
257 }
258
259 // if the prev mem is valid, we need to close
260 if(mMem.prev().valid()) {
261 // FIXME if no wait for vsync the above
262 // play will return immediatly and might cause
263 // tearing when prev.close is called.
264 if(!mMem.prev().close()) {
265 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
266 return false;
267 }
268 }
269 }
270 return true;
271}
272
273void MdpRot::dump() const {
274 ALOGE("== Dump MdpRot start ==");
275 mFd.dump();
276 mMem.curr().m.dump();
277 mdp_wrapper::dump("mRotImgInfo", mRotImgInfo);
278 mdp_wrapper::dump("mRotDataInfo", mRotDataInfo);
279 ALOGE("== Dump MdpRot end ==");
280}
281
282} // namespace overlay