blob: a8cc2e69fb489d05a0e40c1eb750ccb95c9ba871 [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
Saurabh Shahacf10202013-02-26 10:15:15 -080034bool MdpRot::enabled() const { return mRotImgInfo.enable; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050035
Saurabh Shahacf10202013-02-26 10:15:15 -080036void MdpRot::setRotations(uint32_t r) { mRotImgInfo.rotations = r; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050037
Saurabh Shahacf10202013-02-26 10:15:15 -080038int MdpRot::getDstMemId() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050039 return mRotDataInfo.dst.memory_id;
40}
41
Saurabh Shahacf10202013-02-26 10:15:15 -080042uint32_t MdpRot::getDstOffset() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050043 return mRotDataInfo.dst.offset;
44}
45
Saurabh Shahacf10202013-02-26 10:15:15 -080046uint32_t MdpRot::getDstFormat() const {
Raj kamal23f69b22012-11-17 00:20:55 +053047 return mRotImgInfo.dst.format;
48}
Naseer Ahmed758bfc52012-11-28 17:02:08 -050049
Saurabh Shahacf10202013-02-26 10:15:15 -080050uint32_t MdpRot::getSessId() const { return mRotImgInfo.session_id; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050051
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080052void MdpRot::setDownscale(int ds) {
Ramkumar Radhakrishnan4ca8f3a2013-02-14 18:56:50 -080053 if ((utils::ROT_DS_EIGHTH == ds) && (mRotImgInfo.src_rect.h & 0xF)) {
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080054 // Ensure src_rect.h is a multiple of 16 for 1/8 downscaling.
55 // This is an undocumented MDP Rotator constraint.
Ramkumar Radhakrishnan4ca8f3a2013-02-14 18:56:50 -080056 mRotImgInfo.src_rect.h = utils::aligndown(mRotImgInfo.src_rect.h, 16);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080057 }
58 mRotImgInfo.downscale_ratio = ds;
59}
60
Saurabh Shahacf10202013-02-26 10:15:15 -080061void MdpRot::save() {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050062 mLSRotImgInfo = mRotImgInfo;
63}
64
Saurabh Shahacf10202013-02-26 10:15:15 -080065bool MdpRot::rotConfChanged() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050066 // 0 means same
67 if(0 == ::memcmp(&mRotImgInfo, &mLSRotImgInfo,
68 sizeof (msm_rotator_img_info))) {
69 return false;
70 }
71 return true;
72}
73
Saurabh Shahe012f7a2012-08-18 15:11:57 -070074bool MdpRot::init()
75{
76 if(!mFd.open(Res::rotPath, O_RDWR)){
77 ALOGE("MdpRot failed to init %s", Res::rotPath);
78 return false;
79 }
80 return true;
81}
82
83void MdpRot::setSource(const overlay::utils::Whf& awhf) {
84 utils::Whf whf(awhf);
Saurabh Shahe012f7a2012-08-18 15:11:57 -070085 mRotImgInfo.src.format = whf.format;
Saurabh Shahe012f7a2012-08-18 15:11:57 -070086
87 mRotImgInfo.src.width = whf.w;
88 mRotImgInfo.src.height = whf.h;
89
90 mRotImgInfo.src_rect.w = whf.w;
91 mRotImgInfo.src_rect.h = whf.h;
92
93 mRotImgInfo.dst.width = whf.w;
94 mRotImgInfo.dst.height = whf.h;
Saurabh Shahe012f7a2012-08-18 15:11:57 -070095}
96
Sushil Chauhan80fc1f92013-04-23 17:30:05 -070097void MdpRot::setCrop(const utils::Dim& crop) {
98 // NO-OP for non-mdss rotator due to possible h/w limitations
99}
100
Saurabh Shahacf10202013-02-26 10:15:15 -0800101void MdpRot::setFlags(const utils::eMdpFlags& flags) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700102 mRotImgInfo.secure = 0;
103 if(flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
104 mRotImgInfo.secure = 1;
105}
106
Saurabh Shahacf10202013-02-26 10:15:15 -0800107void MdpRot::setTransform(const utils::eTransform& rot)
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700108{
109 int r = utils::getMdpOrient(rot);
110 setRotations(r);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700111 mOrientation = static_cast<utils::eTransform>(r);
112 ALOGE_IF(DEBUG_OVERLAY, "%s: r=%d", __FUNCTION__, r);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800113}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700114
Saurabh Shahacf10202013-02-26 10:15:15 -0800115void MdpRot::doTransform() {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700116 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
117 utils::swap(mRotImgInfo.dst.width, mRotImgInfo.dst.height);
118}
119
120bool MdpRot::commit() {
121 doTransform();
Saurabh Shahc23b3802012-08-31 11:11:30 -0700122 if(rotConfChanged()) {
Saurabh Shahacf10202013-02-26 10:15:15 -0800123 mRotImgInfo.enable = 1;
Saurabh Shahc23b3802012-08-31 11:11:30 -0700124 if(!overlay::mdp_wrapper::startRotator(mFd.getFD(), mRotImgInfo)) {
125 ALOGE("MdpRot commit failed");
126 dump();
Saurabh Shahacf10202013-02-26 10:15:15 -0800127 mRotImgInfo.enable = 0;
Saurabh Shahc23b3802012-08-31 11:11:30 -0700128 return false;
129 }
130 save();
131 mRotDataInfo.session_id = mRotImgInfo.session_id;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700132 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700133 return true;
134}
135
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800136uint32_t MdpRot::calcOutputBufSize() {
137 ovutils::Whf destWhf(mRotImgInfo.dst.width,
138 mRotImgInfo.dst.height, mRotImgInfo.dst.format);
139 return Rotator::calcOutputBufSize(destWhf);
140}
141
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700142bool MdpRot::open_i(uint32_t numbufs, uint32_t bufsz)
143{
144 OvMem mem;
145
146 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
147
148 if(!mem.open(numbufs, bufsz, mRotImgInfo.secure)){
149 ALOGE("%s: Failed to open", __func__);
150 mem.close();
151 return false;
152 }
153
154 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
155 OVASSERT(mem.getFD() != -1, "getFd is -1");
156
157 mRotDataInfo.dst.memory_id = mem.getFD();
158 mRotDataInfo.dst.offset = 0;
Saurabh Shah9acd8f72014-02-07 14:01:04 -0800159 mMem.mem = mem;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700160 return true;
161}
162
163bool MdpRot::close() {
164 bool success = true;
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500165 if(mFd.valid() && (getSessId() != 0)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700166 if(!mdp_wrapper::endRotator(mFd.getFD(), getSessId())) {
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500167 ALOGE("Mdp Rot error endRotator, fd=%d sessId=%u",
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700168 mFd.getFD(), getSessId());
169 success = false;
170 }
171 }
172 if (!mFd.close()) {
173 ALOGE("Mdp Rot error closing fd");
174 success = false;
175 }
176 if (!mMem.close()) {
177 ALOGE("Mdp Rot error closing mem");
178 success = false;
179 }
180 reset();
181 return success;
182}
183
184bool MdpRot::remap(uint32_t numbufs) {
185 // if current size changed, remap
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800186 uint32_t opBufSize = calcOutputBufSize();
Saurabh Shah9acd8f72014-02-07 14:01:04 -0800187 if(opBufSize == mMem.size()) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800188 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, opBufSize);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700189 return true;
190 }
191
Saurabh Shah9acd8f72014-02-07 14:01:04 -0800192 if(!mMem.close()) {
193 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
194 return false;
195 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700196
Saurabh Shah9acd8f72014-02-07 14:01:04 -0800197 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
198
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800199 if(!open_i(numbufs, opBufSize)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700200 ALOGE("%s Error could not open", __FUNCTION__);
201 return false;
202 }
Saurabh Shah9acd8f72014-02-07 14:01:04 -0800203
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700204 for (uint32_t i = 0; i < numbufs; ++i) {
Saurabh Shah9acd8f72014-02-07 14:01:04 -0800205 mMem.mRotOffset[i] = i * opBufSize;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700206 }
Saurabh Shah9acd8f72014-02-07 14:01:04 -0800207
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700208 return true;
209}
210
211void MdpRot::reset() {
212 ovutils::memset0(mRotImgInfo);
Saurabh Shahc23b3802012-08-31 11:11:30 -0700213 ovutils::memset0(mLSRotImgInfo);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700214 ovutils::memset0(mRotDataInfo);
Saurabh Shah9acd8f72014-02-07 14:01:04 -0800215 ovutils::memset0(mMem.mRotOffset);
216 mMem.mCurrIndex = 0;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700217 mOrientation = utils::OVERLAY_TRANSFORM_0;
218}
219
220bool MdpRot::queueBuffer(int fd, uint32_t offset) {
221 if(enabled()) {
222 mRotDataInfo.src.memory_id = fd;
223 mRotDataInfo.src.offset = offset;
224
Saurabh Shah9acd8f72014-02-07 14:01:04 -0800225 if(false == remap(RotMem::ROT_NUM_BUFS)) {
226 ALOGE("%s Remap failed, not queueing", __FUNCTION__);
227 return false;
228 }
229
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700230 mRotDataInfo.dst.offset =
Saurabh Shah9acd8f72014-02-07 14:01:04 -0800231 mMem.mRotOffset[mMem.mCurrIndex];
232 mMem.mCurrIndex =
233 (mMem.mCurrIndex + 1) % mMem.mem.numBufs();
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700234
235 if(!overlay::mdp_wrapper::rotate(mFd.getFD(), mRotDataInfo)) {
236 ALOGE("MdpRot failed rotate");
237 dump();
238 return false;
239 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700240 }
241 return true;
242}
243
244void MdpRot::dump() const {
245 ALOGE("== Dump MdpRot start ==");
246 mFd.dump();
Saurabh Shah9acd8f72014-02-07 14:01:04 -0800247 mMem.mem.dump();
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700248 mdp_wrapper::dump("mRotImgInfo", mRotImgInfo);
249 mdp_wrapper::dump("mRotDataInfo", mRotDataInfo);
250 ALOGE("== Dump MdpRot end ==");
251}
252
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800253void MdpRot::getDump(char *buf, size_t len) const {
Saurabh Shahae61b2b2013-04-10 16:37:25 -0700254 ovutils::getDump(buf, len, "MdpRotCtrl", mRotImgInfo);
255 ovutils::getDump(buf, len, "MdpRotData", mRotDataInfo);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800256}
257
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700258} // namespace overlay