blob: d32289769ced6449c20eba42c55ce62c2ce4c602 [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
Saurabh Shahacf10202013-02-26 10:15:15 -0800129void MdpRot::setFlags(const utils::eMdpFlags& flags) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700130 mRotImgInfo.secure = 0;
131 if(flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
132 mRotImgInfo.secure = 1;
133}
134
Saurabh Shahacf10202013-02-26 10:15:15 -0800135void MdpRot::setTransform(const utils::eTransform& rot)
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700136{
137 int r = utils::getMdpOrient(rot);
138 setRotations(r);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700139 mOrientation = static_cast<utils::eTransform>(r);
140 ALOGE_IF(DEBUG_OVERLAY, "%s: r=%d", __FUNCTION__, r);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800141}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700142
Saurabh Shahacf10202013-02-26 10:15:15 -0800143void MdpRot::doTransform() {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700144 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
145 utils::swap(mRotImgInfo.dst.width, mRotImgInfo.dst.height);
146}
147
148bool MdpRot::commit() {
149 doTransform();
Saurabh Shahc23b3802012-08-31 11:11:30 -0700150 if(rotConfChanged()) {
Saurabh Shahacf10202013-02-26 10:15:15 -0800151 mRotImgInfo.enable = 1;
Saurabh Shahc23b3802012-08-31 11:11:30 -0700152 if(!overlay::mdp_wrapper::startRotator(mFd.getFD(), mRotImgInfo)) {
153 ALOGE("MdpRot commit failed");
154 dump();
Saurabh Shahacf10202013-02-26 10:15:15 -0800155 mRotImgInfo.enable = 0;
Saurabh Shahc23b3802012-08-31 11:11:30 -0700156 return false;
157 }
Saurabh Shahc23b3802012-08-31 11:11:30 -0700158 mRotDataInfo.session_id = mRotImgInfo.session_id;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700159 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700160 return true;
161}
162
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800163uint32_t MdpRot::calcOutputBufSize() {
164 ovutils::Whf destWhf(mRotImgInfo.dst.width,
165 mRotImgInfo.dst.height, mRotImgInfo.dst.format);
166 return Rotator::calcOutputBufSize(destWhf);
167}
168
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700169bool MdpRot::open_i(uint32_t numbufs, uint32_t bufsz)
170{
171 OvMem mem;
172
173 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
174
175 if(!mem.open(numbufs, bufsz, mRotImgInfo.secure)){
176 ALOGE("%s: Failed to open", __func__);
177 mem.close();
178 return false;
179 }
180
181 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
182 OVASSERT(mem.getFD() != -1, "getFd is -1");
183
184 mRotDataInfo.dst.memory_id = mem.getFD();
185 mRotDataInfo.dst.offset = 0;
Saurabh Shah912c9482014-02-07 14:01:04 -0800186 mMem.mem = mem;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700187 return true;
188}
189
190bool MdpRot::close() {
191 bool success = true;
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500192 if(mFd.valid() && (getSessId() != 0)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700193 if(!mdp_wrapper::endRotator(mFd.getFD(), getSessId())) {
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500194 ALOGE("Mdp Rot error endRotator, fd=%d sessId=%u",
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700195 mFd.getFD(), getSessId());
196 success = false;
197 }
198 }
199 if (!mFd.close()) {
200 ALOGE("Mdp Rot error closing fd");
201 success = false;
202 }
203 if (!mMem.close()) {
204 ALOGE("Mdp Rot error closing mem");
205 success = false;
206 }
207 reset();
208 return success;
209}
210
211bool MdpRot::remap(uint32_t numbufs) {
212 // if current size changed, remap
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800213 uint32_t opBufSize = calcOutputBufSize();
Saurabh Shah912c9482014-02-07 14:01:04 -0800214 if(opBufSize == mMem.size()) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800215 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, opBufSize);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700216 return true;
217 }
218
Saurabh Shah912c9482014-02-07 14:01:04 -0800219 if(!mMem.close()) {
220 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
221 return false;
222 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700223
Saurabh Shah912c9482014-02-07 14:01:04 -0800224 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
225
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800226 if(!open_i(numbufs, opBufSize)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700227 ALOGE("%s Error could not open", __FUNCTION__);
228 return false;
229 }
Saurabh Shah912c9482014-02-07 14:01:04 -0800230
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700231 for (uint32_t i = 0; i < numbufs; ++i) {
Saurabh Shah912c9482014-02-07 14:01:04 -0800232 mMem.mRotOffset[i] = i * opBufSize;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700233 }
Saurabh Shah912c9482014-02-07 14:01:04 -0800234
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700235 return true;
236}
237
238void MdpRot::reset() {
239 ovutils::memset0(mRotImgInfo);
Saurabh Shahc23b3802012-08-31 11:11:30 -0700240 ovutils::memset0(mLSRotImgInfo);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700241 ovutils::memset0(mRotDataInfo);
Saurabh Shah912c9482014-02-07 14:01:04 -0800242 ovutils::memset0(mMem.mRotOffset);
243 mMem.mCurrIndex = 0;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700244 mOrientation = utils::OVERLAY_TRANSFORM_0;
245}
246
247bool MdpRot::queueBuffer(int fd, uint32_t offset) {
Raj Kamalbd3bdc62014-08-05 18:52:49 +0530248 if(enabled() and (not isRotCached(fd,offset))) {
249 int prev_fd = getSrcMemId();
250 uint32_t prev_offset = getSrcOffset();
251
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700252 mRotDataInfo.src.memory_id = fd;
253 mRotDataInfo.src.offset = offset;
254
Saurabh Shah912c9482014-02-07 14:01:04 -0800255 if(false == remap(RotMem::ROT_NUM_BUFS)) {
256 ALOGE("%s Remap failed, not queueing", __FUNCTION__);
257 return false;
258 }
259
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700260 mRotDataInfo.dst.offset =
Saurabh Shah912c9482014-02-07 14:01:04 -0800261 mMem.mRotOffset[mMem.mCurrIndex];
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700262
263 if(!overlay::mdp_wrapper::rotate(mFd.getFD(), mRotDataInfo)) {
264 ALOGE("MdpRot failed rotate");
265 dump();
Raj Kamalbd3bdc62014-08-05 18:52:49 +0530266 mRotDataInfo.src.memory_id = prev_fd;
267 mRotDataInfo.src.offset = prev_offset;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700268 return false;
269 }
Raj Kamalbd3bdc62014-08-05 18:52:49 +0530270 save();
271 mMem.mCurrIndex =
272 (mMem.mCurrIndex + 1) % mMem.mem.numBufs();
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700273 }
274 return true;
275}
276
277void MdpRot::dump() const {
278 ALOGE("== Dump MdpRot start ==");
279 mFd.dump();
Saurabh Shah912c9482014-02-07 14:01:04 -0800280 mMem.mem.dump();
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700281 mdp_wrapper::dump("mRotImgInfo", mRotImgInfo);
282 mdp_wrapper::dump("mRotDataInfo", mRotDataInfo);
283 ALOGE("== Dump MdpRot end ==");
284}
285
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800286void MdpRot::getDump(char *buf, size_t len) const {
Saurabh Shahae61b2b2013-04-10 16:37:25 -0700287 ovutils::getDump(buf, len, "MdpRotCtrl", mRotImgInfo);
288 ovutils::getDump(buf, len, "MdpRotData", mRotDataInfo);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800289}
290
Saurabh Shahc46cf9d2014-07-02 15:22:34 -0700291int MdpRot::getDownscaleFactor(const int& src_w, const int& src_h,
292 const int& dst_w, const int& dst_h, const uint32_t& /*mdpFormat*/,
293 const bool& /*isInterlaced*/) {
294 int dscale_factor = utils::ROT_DS_NONE;
295 // We need this check to engage the rotator whenever possible to assist MDP
296 // in performing video downscale.
297 // This saves bandwidth and avoids causing the driver to make too many panel
298 // -mode switches between BLT (writeback) and non-BLT (Direct) modes.
299 // Use-case: Video playback [with downscaling and rotation].
300 if (dst_w && dst_h)
301 {
302 float fDscale = (float)(src_w * src_h) / (float)(dst_w * dst_h);
303 uint32_t dscale = (int)sqrtf(fDscale);
304
305 if(dscale < 2) {
306 // Down-scale to > 50% of orig.
307 dscale_factor = utils::ROT_DS_NONE;
308 } else if(dscale < 4) {
309 // Down-scale to between > 25% to <= 50% of orig.
310 dscale_factor = utils::ROT_DS_HALF;
311 } else if(dscale < 8) {
312 // Down-scale to between > 12.5% to <= 25% of orig.
313 dscale_factor = utils::ROT_DS_FOURTH;
314 } else {
315 // Down-scale to <= 12.5% of orig.
316 dscale_factor = utils::ROT_DS_EIGHTH;
317 }
318 }
319 return dscale_factor;
320}
321
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700322} // namespace overlay