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