blob: b5a90ad1b5af6311d7b00e1022156462f7f37544 [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
Arun Kumar K.R7e5a1f82014-01-22 10:36:16 -08003 * Copyright (c) 2010-2014, 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
Saurabh Shahc46cf9d2014-07-02 15:22:34 -070020#include <math.h>
Saurabh Shahe012f7a2012-08-18 15:11:57 -070021#include "overlayUtils.h"
22#include "overlayRotator.h"
Saurabh Shah8ec9b5e2014-06-30 14:37:17 -070023#include "gr.h"
Saurabh Shahe012f7a2012-08-18 15:11:57 -070024
25namespace ovutils = overlay::utils;
26
27namespace overlay {
28
Naseer Ahmed758bfc52012-11-28 17:02:08 -050029MdpRot::MdpRot() {
30 reset();
31 init();
32}
33
34MdpRot::~MdpRot() { close(); }
35
Saurabh Shahacf10202013-02-26 10:15:15 -080036bool MdpRot::enabled() const { return mRotImgInfo.enable; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050037
Shalaj Jaina70b4352014-06-15 13:47:47 -070038void MdpRot::setRotations(uint32_t r) { mRotImgInfo.rotations = (uint8_t)r; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050039
Raj Kamalbd3bdc62014-08-05 18:52:49 +053040int MdpRot::getSrcMemId() const {
41 return mRotDataInfo.src.memory_id;
42}
43
Saurabh Shahacf10202013-02-26 10:15:15 -080044int MdpRot::getDstMemId() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050045 return mRotDataInfo.dst.memory_id;
46}
47
Raj Kamalbd3bdc62014-08-05 18:52:49 +053048uint32_t MdpRot::getSrcOffset() const {
49 return mRotDataInfo.src.offset;
50}
51
Saurabh Shahacf10202013-02-26 10:15:15 -080052uint32_t MdpRot::getDstOffset() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050053 return mRotDataInfo.dst.offset;
54}
55
Saurabh Shahacf10202013-02-26 10:15:15 -080056uint32_t MdpRot::getDstFormat() const {
Raj kamal23f69b22012-11-17 00:20:55 +053057 return mRotImgInfo.dst.format;
58}
Naseer Ahmed758bfc52012-11-28 17:02:08 -050059
Saurabh Shah8ec9b5e2014-06-30 14:37:17 -070060//Added for completeness. Not expected to be called.
61utils::Whf MdpRot::getDstWhf() const {
62 int alW = 0, alH = 0;
63 int halFormat = ovutils::getHALFormat(mRotImgInfo.dst.format);
64 getBufferSizeAndDimensions(mRotImgInfo.dst.width, mRotImgInfo.dst.height,
65 halFormat, alW, alH);
66 return utils::Whf(alW, alH, mRotImgInfo.dst.format);
67}
68
69//Added for completeness. Not expected to be called.
70utils::Dim MdpRot::getDstDimensions() const {
71 int alW = 0, alH = 0;
72 int halFormat = ovutils::getHALFormat(mRotImgInfo.dst.format);
73 getBufferSizeAndDimensions(mRotImgInfo.dst.width, mRotImgInfo.dst.height,
74 halFormat, alW, alH);
75 return utils::Dim(0, 0, alW, alH);
76}
77
Saurabh Shahacf10202013-02-26 10:15:15 -080078uint32_t MdpRot::getSessId() const { return mRotImgInfo.session_id; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050079
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080080void MdpRot::setDownscale(int ds) {
Ramkumar Radhakrishnan4ca8f3a2013-02-14 18:56:50 -080081 if ((utils::ROT_DS_EIGHTH == ds) && (mRotImgInfo.src_rect.h & 0xF)) {
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080082 // Ensure src_rect.h is a multiple of 16 for 1/8 downscaling.
83 // This is an undocumented MDP Rotator constraint.
Ramkumar Radhakrishnan4ca8f3a2013-02-14 18:56:50 -080084 mRotImgInfo.src_rect.h = utils::aligndown(mRotImgInfo.src_rect.h, 16);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080085 }
86 mRotImgInfo.downscale_ratio = ds;
87}
88
Saurabh Shahacf10202013-02-26 10:15:15 -080089void MdpRot::save() {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050090 mLSRotImgInfo = mRotImgInfo;
91}
92
Saurabh Shahacf10202013-02-26 10:15:15 -080093bool MdpRot::rotConfChanged() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050094 // 0 means same
95 if(0 == ::memcmp(&mRotImgInfo, &mLSRotImgInfo,
96 sizeof (msm_rotator_img_info))) {
97 return false;
98 }
99 return true;
100}
101
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700102bool MdpRot::init()
103{
104 if(!mFd.open(Res::rotPath, O_RDWR)){
105 ALOGE("MdpRot failed to init %s", Res::rotPath);
106 return false;
107 }
108 return true;
109}
110
111void MdpRot::setSource(const overlay::utils::Whf& awhf) {
112 utils::Whf whf(awhf);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700113 mRotImgInfo.src.format = whf.format;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700114
115 mRotImgInfo.src.width = whf.w;
116 mRotImgInfo.src.height = whf.h;
117
118 mRotImgInfo.src_rect.w = whf.w;
119 mRotImgInfo.src_rect.h = whf.h;
120
121 mRotImgInfo.dst.width = whf.w;
122 mRotImgInfo.dst.height = whf.h;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700123}
124
Arun Kumar K.R7e5a1f82014-01-22 10:36:16 -0800125void MdpRot::setCrop(const utils::Dim& /*crop*/) {
Sushil Chauhan80fc1f92013-04-23 17:30:05 -0700126 // NO-OP for non-mdss rotator due to possible h/w limitations
127}
128
radhakrishnab8e2c952015-06-04 16:09:16 +0530129void MdpRot::setFrameRate(uint32_t /*frame_rate*/) {
130 // NO-OP for non-mdss rotator
131}
132
Saurabh Shahacf10202013-02-26 10:15:15 -0800133void MdpRot::setFlags(const utils::eMdpFlags& flags) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700134 mRotImgInfo.secure = 0;
135 if(flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
136 mRotImgInfo.secure = 1;
137}
138
Saurabh Shahacf10202013-02-26 10:15:15 -0800139void MdpRot::setTransform(const utils::eTransform& rot)
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700140{
141 int r = utils::getMdpOrient(rot);
142 setRotations(r);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700143 mOrientation = static_cast<utils::eTransform>(r);
144 ALOGE_IF(DEBUG_OVERLAY, "%s: r=%d", __FUNCTION__, r);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800145}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700146
Saurabh Shahacf10202013-02-26 10:15:15 -0800147void MdpRot::doTransform() {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700148 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
149 utils::swap(mRotImgInfo.dst.width, mRotImgInfo.dst.height);
150}
151
152bool MdpRot::commit() {
153 doTransform();
Saurabh Shahc23b3802012-08-31 11:11:30 -0700154 if(rotConfChanged()) {
Saurabh Shahacf10202013-02-26 10:15:15 -0800155 mRotImgInfo.enable = 1;
Saurabh Shahc23b3802012-08-31 11:11:30 -0700156 if(!overlay::mdp_wrapper::startRotator(mFd.getFD(), mRotImgInfo)) {
157 ALOGE("MdpRot commit failed");
158 dump();
Saurabh Shahacf10202013-02-26 10:15:15 -0800159 mRotImgInfo.enable = 0;
Saurabh Shahc23b3802012-08-31 11:11:30 -0700160 return false;
161 }
Saurabh Shahc23b3802012-08-31 11:11:30 -0700162 mRotDataInfo.session_id = mRotImgInfo.session_id;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700163 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700164 return true;
165}
166
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800167uint32_t MdpRot::calcOutputBufSize() {
168 ovutils::Whf destWhf(mRotImgInfo.dst.width,
169 mRotImgInfo.dst.height, mRotImgInfo.dst.format);
170 return Rotator::calcOutputBufSize(destWhf);
171}
172
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700173bool MdpRot::open_i(uint32_t numbufs, uint32_t bufsz)
174{
175 OvMem mem;
176
177 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
178
179 if(!mem.open(numbufs, bufsz, mRotImgInfo.secure)){
180 ALOGE("%s: Failed to open", __func__);
181 mem.close();
182 return false;
183 }
184
185 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
186 OVASSERT(mem.getFD() != -1, "getFd is -1");
187
188 mRotDataInfo.dst.memory_id = mem.getFD();
189 mRotDataInfo.dst.offset = 0;
Saurabh Shah912c9482014-02-07 14:01:04 -0800190 mMem.mem = mem;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700191 return true;
192}
193
194bool MdpRot::close() {
195 bool success = true;
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500196 if(mFd.valid() && (getSessId() != 0)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700197 if(!mdp_wrapper::endRotator(mFd.getFD(), getSessId())) {
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500198 ALOGE("Mdp Rot error endRotator, fd=%d sessId=%u",
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700199 mFd.getFD(), getSessId());
200 success = false;
201 }
202 }
203 if (!mFd.close()) {
204 ALOGE("Mdp Rot error closing fd");
205 success = false;
206 }
207 if (!mMem.close()) {
208 ALOGE("Mdp Rot error closing mem");
209 success = false;
210 }
211 reset();
212 return success;
213}
214
215bool MdpRot::remap(uint32_t numbufs) {
216 // if current size changed, remap
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800217 uint32_t opBufSize = calcOutputBufSize();
Saurabh Shah912c9482014-02-07 14:01:04 -0800218 if(opBufSize == mMem.size()) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800219 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, opBufSize);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700220 return true;
221 }
222
Saurabh Shah912c9482014-02-07 14:01:04 -0800223 if(!mMem.close()) {
224 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
225 return false;
226 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700227
Saurabh Shah912c9482014-02-07 14:01:04 -0800228 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
229
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800230 if(!open_i(numbufs, opBufSize)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700231 ALOGE("%s Error could not open", __FUNCTION__);
232 return false;
233 }
Saurabh Shah912c9482014-02-07 14:01:04 -0800234
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700235 for (uint32_t i = 0; i < numbufs; ++i) {
Saurabh Shah912c9482014-02-07 14:01:04 -0800236 mMem.mRotOffset[i] = i * opBufSize;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700237 }
Saurabh Shah912c9482014-02-07 14:01:04 -0800238
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700239 return true;
240}
241
242void MdpRot::reset() {
243 ovutils::memset0(mRotImgInfo);
Saurabh Shahc23b3802012-08-31 11:11:30 -0700244 ovutils::memset0(mLSRotImgInfo);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700245 ovutils::memset0(mRotDataInfo);
Saurabh Shah912c9482014-02-07 14:01:04 -0800246 ovutils::memset0(mMem.mRotOffset);
247 mMem.mCurrIndex = 0;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700248 mOrientation = utils::OVERLAY_TRANSFORM_0;
249}
250
251bool MdpRot::queueBuffer(int fd, uint32_t offset) {
Raj Kamalbd3bdc62014-08-05 18:52:49 +0530252 if(enabled() and (not isRotCached(fd,offset))) {
253 int prev_fd = getSrcMemId();
254 uint32_t prev_offset = getSrcOffset();
255
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700256 mRotDataInfo.src.memory_id = fd;
257 mRotDataInfo.src.offset = offset;
258
Saurabh Shah912c9482014-02-07 14:01:04 -0800259 if(false == remap(RotMem::ROT_NUM_BUFS)) {
260 ALOGE("%s Remap failed, not queueing", __FUNCTION__);
261 return false;
262 }
263
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700264 mRotDataInfo.dst.offset =
Saurabh Shah912c9482014-02-07 14:01:04 -0800265 mMem.mRotOffset[mMem.mCurrIndex];
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700266
267 if(!overlay::mdp_wrapper::rotate(mFd.getFD(), mRotDataInfo)) {
268 ALOGE("MdpRot failed rotate");
269 dump();
Raj Kamalbd3bdc62014-08-05 18:52:49 +0530270 mRotDataInfo.src.memory_id = prev_fd;
271 mRotDataInfo.src.offset = prev_offset;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700272 return false;
273 }
Raj Kamalbd3bdc62014-08-05 18:52:49 +0530274 save();
275 mMem.mCurrIndex =
276 (mMem.mCurrIndex + 1) % mMem.mem.numBufs();
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700277 }
278 return true;
279}
280
281void MdpRot::dump() const {
282 ALOGE("== Dump MdpRot start ==");
283 mFd.dump();
Saurabh Shah912c9482014-02-07 14:01:04 -0800284 mMem.mem.dump();
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700285 mdp_wrapper::dump("mRotImgInfo", mRotImgInfo);
286 mdp_wrapper::dump("mRotDataInfo", mRotDataInfo);
287 ALOGE("== Dump MdpRot end ==");
288}
289
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800290void MdpRot::getDump(char *buf, size_t len) const {
Saurabh Shahae61b2b2013-04-10 16:37:25 -0700291 ovutils::getDump(buf, len, "MdpRotCtrl", mRotImgInfo);
292 ovutils::getDump(buf, len, "MdpRotData", mRotDataInfo);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800293}
294
Saurabh Shahc46cf9d2014-07-02 15:22:34 -0700295int MdpRot::getDownscaleFactor(const int& src_w, const int& src_h,
296 const int& dst_w, const int& dst_h, const uint32_t& /*mdpFormat*/,
297 const bool& /*isInterlaced*/) {
298 int dscale_factor = utils::ROT_DS_NONE;
299 // We need this check to engage the rotator whenever possible to assist MDP
300 // in performing video downscale.
301 // This saves bandwidth and avoids causing the driver to make too many panel
302 // -mode switches between BLT (writeback) and non-BLT (Direct) modes.
303 // Use-case: Video playback [with downscaling and rotation].
304 if (dst_w && dst_h)
305 {
306 float fDscale = (float)(src_w * src_h) / (float)(dst_w * dst_h);
307 uint32_t dscale = (int)sqrtf(fDscale);
308
309 if(dscale < 2) {
310 // Down-scale to > 50% of orig.
311 dscale_factor = utils::ROT_DS_NONE;
312 } else if(dscale < 4) {
313 // Down-scale to between > 25% to <= 50% of orig.
314 dscale_factor = utils::ROT_DS_HALF;
315 } else if(dscale < 8) {
316 // Down-scale to between > 12.5% to <= 25% of orig.
317 dscale_factor = utils::ROT_DS_FOURTH;
318 } else {
319 // Down-scale to <= 12.5% of orig.
320 dscale_factor = utils::ROT_DS_EIGHTH;
321 }
322 }
323 return dscale_factor;
324}
325
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700326} // namespace overlay