blob: d0242d6d8f15a3c61a22576d42ee2c22b4a7f9e3 [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;
114
115 mBufSize = awhf.size;
116}
117
Raj kamal23f69b22012-11-17 00:20:55 +0530118inline void MdpRot::setFlags(const utils::eMdpFlags& flags) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700119 mRotImgInfo.secure = 0;
120 if(flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
121 mRotImgInfo.secure = 1;
122}
123
Raj kamal23f69b22012-11-17 00:20:55 +0530124inline void MdpRot::setTransform(const utils::eTransform& rot)
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700125{
126 int r = utils::getMdpOrient(rot);
127 setRotations(r);
128 //getMdpOrient will switch the flips if the source is 90 rotated.
129 //Clients in Android dont factor in 90 rotation while deciding the flip.
130 mOrientation = static_cast<utils::eTransform>(r);
131 ALOGE_IF(DEBUG_OVERLAY, "%s: r=%d", __FUNCTION__, r);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800132}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700133
Raj kamal23f69b22012-11-17 00:20:55 +0530134inline void MdpRot::setRotatorUsed(const bool& rotUsed) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700135 setDisable();
136 if(rotUsed) {
137 setEnable();
138 }
139}
140
Raj kamal23f69b22012-11-17 00:20:55 +0530141inline void MdpRot::doTransform() {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700142 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
143 utils::swap(mRotImgInfo.dst.width, mRotImgInfo.dst.height);
144}
145
146bool MdpRot::commit() {
147 doTransform();
Saurabh Shahc23b3802012-08-31 11:11:30 -0700148 if(rotConfChanged()) {
149 if(!overlay::mdp_wrapper::startRotator(mFd.getFD(), mRotImgInfo)) {
150 ALOGE("MdpRot commit failed");
151 dump();
152 return false;
153 }
154 save();
155 mRotDataInfo.session_id = mRotImgInfo.session_id;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700156 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700157 return true;
158}
159
160bool MdpRot::open_i(uint32_t numbufs, uint32_t bufsz)
161{
162 OvMem mem;
163
164 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
165
166 if(!mem.open(numbufs, bufsz, mRotImgInfo.secure)){
167 ALOGE("%s: Failed to open", __func__);
168 mem.close();
169 return false;
170 }
171
172 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
173 OVASSERT(mem.getFD() != -1, "getFd is -1");
174
175 mRotDataInfo.dst.memory_id = mem.getFD();
176 mRotDataInfo.dst.offset = 0;
177 mMem.curr().m = mem;
178 return true;
179}
180
181bool MdpRot::close() {
182 bool success = true;
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500183 if(mFd.valid() && (getSessId() != 0)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700184 if(!mdp_wrapper::endRotator(mFd.getFD(), getSessId())) {
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500185 ALOGE("Mdp Rot error endRotator, fd=%d sessId=%u",
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700186 mFd.getFD(), getSessId());
187 success = false;
188 }
189 }
190 if (!mFd.close()) {
191 ALOGE("Mdp Rot error closing fd");
192 success = false;
193 }
194 if (!mMem.close()) {
195 ALOGE("Mdp Rot error closing mem");
196 success = false;
197 }
198 reset();
199 return success;
200}
201
202bool MdpRot::remap(uint32_t numbufs) {
203 // if current size changed, remap
204 if(mBufSize == mMem.curr().size()) {
205 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, mBufSize);
206 return true;
207 }
208
209 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
210 OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
211
212 // ++mMem will make curr to be prev, and prev will be curr
213 ++mMem;
214 if(!open_i(numbufs, mBufSize)) {
215 ALOGE("%s Error could not open", __FUNCTION__);
216 return false;
217 }
218 for (uint32_t i = 0; i < numbufs; ++i) {
219 mMem.curr().mRotOffset[i] = i * mBufSize;
220 }
221 return true;
222}
223
224void MdpRot::reset() {
225 ovutils::memset0(mRotImgInfo);
Saurabh Shahc23b3802012-08-31 11:11:30 -0700226 ovutils::memset0(mLSRotImgInfo);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700227 ovutils::memset0(mRotDataInfo);
228 ovutils::memset0(mMem.curr().mRotOffset);
229 ovutils::memset0(mMem.prev().mRotOffset);
230 mMem.curr().mCurrOffset = 0;
231 mMem.prev().mCurrOffset = 0;
232 mBufSize = 0;
233 mOrientation = utils::OVERLAY_TRANSFORM_0;
234}
235
236bool MdpRot::queueBuffer(int fd, uint32_t offset) {
237 if(enabled()) {
238 mRotDataInfo.src.memory_id = fd;
239 mRotDataInfo.src.offset = offset;
240
241 remap(RotMem::Mem::ROT_NUM_BUFS);
242 OVASSERT(mMem.curr().m.numBufs(),
243 "queueBuffer numbufs is 0");
244 mRotDataInfo.dst.offset =
245 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
246 mMem.curr().mCurrOffset =
247 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
248
249 if(!overlay::mdp_wrapper::rotate(mFd.getFD(), mRotDataInfo)) {
250 ALOGE("MdpRot failed rotate");
251 dump();
252 return false;
253 }
254
255 // if the prev mem is valid, we need to close
256 if(mMem.prev().valid()) {
257 // FIXME if no wait for vsync the above
258 // play will return immediatly and might cause
259 // tearing when prev.close is called.
260 if(!mMem.prev().close()) {
261 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
262 return false;
263 }
264 }
265 }
266 return true;
267}
268
269void MdpRot::dump() const {
270 ALOGE("== Dump MdpRot start ==");
271 mFd.dump();
272 mMem.curr().m.dump();
273 mdp_wrapper::dump("mRotImgInfo", mRotImgInfo);
274 mdp_wrapper::dump("mRotDataInfo", mRotDataInfo);
275 ALOGE("== Dump MdpRot end ==");
276}
277
278} // namespace overlay