blob: 6105736b0f66c325ba81c48519016f862df74ade [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
Saurabh Shahacf10202013-02-26 10:15:15 -080040int MdpRot::getDstMemId() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050041 return mRotDataInfo.dst.memory_id;
42}
43
Saurabh Shahacf10202013-02-26 10:15:15 -080044uint32_t MdpRot::getDstOffset() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050045 return mRotDataInfo.dst.offset;
46}
47
Saurabh Shahacf10202013-02-26 10:15:15 -080048uint32_t MdpRot::getDstFormat() const {
Raj kamal23f69b22012-11-17 00:20:55 +053049 return mRotImgInfo.dst.format;
50}
Naseer Ahmed758bfc52012-11-28 17:02:08 -050051
Saurabh Shah8ec9b5e2014-06-30 14:37:17 -070052//Added for completeness. Not expected to be called.
53utils::Whf MdpRot::getDstWhf() const {
54 int alW = 0, alH = 0;
55 int halFormat = ovutils::getHALFormat(mRotImgInfo.dst.format);
56 getBufferSizeAndDimensions(mRotImgInfo.dst.width, mRotImgInfo.dst.height,
57 halFormat, alW, alH);
58 return utils::Whf(alW, alH, mRotImgInfo.dst.format);
59}
60
61//Added for completeness. Not expected to be called.
62utils::Dim MdpRot::getDstDimensions() const {
63 int alW = 0, alH = 0;
64 int halFormat = ovutils::getHALFormat(mRotImgInfo.dst.format);
65 getBufferSizeAndDimensions(mRotImgInfo.dst.width, mRotImgInfo.dst.height,
66 halFormat, alW, alH);
67 return utils::Dim(0, 0, alW, alH);
68}
69
Saurabh Shahacf10202013-02-26 10:15:15 -080070uint32_t MdpRot::getSessId() const { return mRotImgInfo.session_id; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050071
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080072void MdpRot::setDownscale(int ds) {
Ramkumar Radhakrishnan4ca8f3a2013-02-14 18:56:50 -080073 if ((utils::ROT_DS_EIGHTH == ds) && (mRotImgInfo.src_rect.h & 0xF)) {
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080074 // Ensure src_rect.h is a multiple of 16 for 1/8 downscaling.
75 // This is an undocumented MDP Rotator constraint.
Ramkumar Radhakrishnan4ca8f3a2013-02-14 18:56:50 -080076 mRotImgInfo.src_rect.h = utils::aligndown(mRotImgInfo.src_rect.h, 16);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080077 }
78 mRotImgInfo.downscale_ratio = ds;
79}
80
Saurabh Shahacf10202013-02-26 10:15:15 -080081void MdpRot::save() {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050082 mLSRotImgInfo = mRotImgInfo;
83}
84
Saurabh Shahacf10202013-02-26 10:15:15 -080085bool MdpRot::rotConfChanged() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050086 // 0 means same
87 if(0 == ::memcmp(&mRotImgInfo, &mLSRotImgInfo,
88 sizeof (msm_rotator_img_info))) {
89 return false;
90 }
91 return true;
92}
93
Saurabh Shahe012f7a2012-08-18 15:11:57 -070094bool MdpRot::init()
95{
96 if(!mFd.open(Res::rotPath, O_RDWR)){
97 ALOGE("MdpRot failed to init %s", Res::rotPath);
98 return false;
99 }
100 return true;
101}
102
103void MdpRot::setSource(const overlay::utils::Whf& awhf) {
104 utils::Whf whf(awhf);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700105 mRotImgInfo.src.format = whf.format;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700106
107 mRotImgInfo.src.width = whf.w;
108 mRotImgInfo.src.height = whf.h;
109
110 mRotImgInfo.src_rect.w = whf.w;
111 mRotImgInfo.src_rect.h = whf.h;
112
113 mRotImgInfo.dst.width = whf.w;
114 mRotImgInfo.dst.height = whf.h;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700115}
116
Arun Kumar K.R7e5a1f82014-01-22 10:36:16 -0800117void MdpRot::setCrop(const utils::Dim& /*crop*/) {
Sushil Chauhan80fc1f92013-04-23 17:30:05 -0700118 // NO-OP for non-mdss rotator due to possible h/w limitations
119}
120
Saurabh Shahacf10202013-02-26 10:15:15 -0800121void MdpRot::setFlags(const utils::eMdpFlags& flags) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700122 mRotImgInfo.secure = 0;
123 if(flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
124 mRotImgInfo.secure = 1;
125}
126
Saurabh Shahacf10202013-02-26 10:15:15 -0800127void MdpRot::setTransform(const utils::eTransform& rot)
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700128{
129 int r = utils::getMdpOrient(rot);
130 setRotations(r);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700131 mOrientation = static_cast<utils::eTransform>(r);
132 ALOGE_IF(DEBUG_OVERLAY, "%s: r=%d", __FUNCTION__, r);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800133}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700134
Saurabh Shahacf10202013-02-26 10:15:15 -0800135void MdpRot::doTransform() {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700136 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
137 utils::swap(mRotImgInfo.dst.width, mRotImgInfo.dst.height);
138}
139
140bool MdpRot::commit() {
141 doTransform();
Saurabh Shahc23b3802012-08-31 11:11:30 -0700142 if(rotConfChanged()) {
Saurabh Shahacf10202013-02-26 10:15:15 -0800143 mRotImgInfo.enable = 1;
Saurabh Shahc23b3802012-08-31 11:11:30 -0700144 if(!overlay::mdp_wrapper::startRotator(mFd.getFD(), mRotImgInfo)) {
145 ALOGE("MdpRot commit failed");
146 dump();
Saurabh Shahacf10202013-02-26 10:15:15 -0800147 mRotImgInfo.enable = 0;
Saurabh Shahc23b3802012-08-31 11:11:30 -0700148 return false;
149 }
150 save();
151 mRotDataInfo.session_id = mRotImgInfo.session_id;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700152 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700153 return true;
154}
155
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800156uint32_t MdpRot::calcOutputBufSize() {
157 ovutils::Whf destWhf(mRotImgInfo.dst.width,
158 mRotImgInfo.dst.height, mRotImgInfo.dst.format);
159 return Rotator::calcOutputBufSize(destWhf);
160}
161
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700162bool MdpRot::open_i(uint32_t numbufs, uint32_t bufsz)
163{
164 OvMem mem;
165
166 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
167
168 if(!mem.open(numbufs, bufsz, mRotImgInfo.secure)){
169 ALOGE("%s: Failed to open", __func__);
170 mem.close();
171 return false;
172 }
173
174 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
175 OVASSERT(mem.getFD() != -1, "getFd is -1");
176
177 mRotDataInfo.dst.memory_id = mem.getFD();
178 mRotDataInfo.dst.offset = 0;
Saurabh Shah912c9482014-02-07 14:01:04 -0800179 mMem.mem = mem;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700180 return true;
181}
182
183bool MdpRot::close() {
184 bool success = true;
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500185 if(mFd.valid() && (getSessId() != 0)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700186 if(!mdp_wrapper::endRotator(mFd.getFD(), getSessId())) {
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500187 ALOGE("Mdp Rot error endRotator, fd=%d sessId=%u",
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700188 mFd.getFD(), getSessId());
189 success = false;
190 }
191 }
192 if (!mFd.close()) {
193 ALOGE("Mdp Rot error closing fd");
194 success = false;
195 }
196 if (!mMem.close()) {
197 ALOGE("Mdp Rot error closing mem");
198 success = false;
199 }
200 reset();
201 return success;
202}
203
204bool MdpRot::remap(uint32_t numbufs) {
205 // if current size changed, remap
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800206 uint32_t opBufSize = calcOutputBufSize();
Saurabh Shah912c9482014-02-07 14:01:04 -0800207 if(opBufSize == mMem.size()) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800208 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, opBufSize);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700209 return true;
210 }
211
Saurabh Shah912c9482014-02-07 14:01:04 -0800212 if(!mMem.close()) {
213 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
214 return false;
215 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700216
Saurabh Shah912c9482014-02-07 14:01:04 -0800217 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
218
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800219 if(!open_i(numbufs, opBufSize)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700220 ALOGE("%s Error could not open", __FUNCTION__);
221 return false;
222 }
Saurabh Shah912c9482014-02-07 14:01:04 -0800223
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700224 for (uint32_t i = 0; i < numbufs; ++i) {
Saurabh Shah912c9482014-02-07 14:01:04 -0800225 mMem.mRotOffset[i] = i * opBufSize;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700226 }
Saurabh Shah912c9482014-02-07 14:01:04 -0800227
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700228 return true;
229}
230
231void MdpRot::reset() {
232 ovutils::memset0(mRotImgInfo);
Saurabh Shahc23b3802012-08-31 11:11:30 -0700233 ovutils::memset0(mLSRotImgInfo);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700234 ovutils::memset0(mRotDataInfo);
Saurabh Shah912c9482014-02-07 14:01:04 -0800235 ovutils::memset0(mMem.mRotOffset);
236 mMem.mCurrIndex = 0;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700237 mOrientation = utils::OVERLAY_TRANSFORM_0;
238}
239
240bool MdpRot::queueBuffer(int fd, uint32_t offset) {
241 if(enabled()) {
242 mRotDataInfo.src.memory_id = fd;
243 mRotDataInfo.src.offset = offset;
244
Saurabh Shah912c9482014-02-07 14:01:04 -0800245 if(false == remap(RotMem::ROT_NUM_BUFS)) {
246 ALOGE("%s Remap failed, not queueing", __FUNCTION__);
247 return false;
248 }
249
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700250 mRotDataInfo.dst.offset =
Saurabh Shah912c9482014-02-07 14:01:04 -0800251 mMem.mRotOffset[mMem.mCurrIndex];
252 mMem.mCurrIndex =
253 (mMem.mCurrIndex + 1) % mMem.mem.numBufs();
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700254
255 if(!overlay::mdp_wrapper::rotate(mFd.getFD(), mRotDataInfo)) {
256 ALOGE("MdpRot failed rotate");
257 dump();
258 return false;
259 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700260 }
261 return true;
262}
263
264void MdpRot::dump() const {
265 ALOGE("== Dump MdpRot start ==");
266 mFd.dump();
Saurabh Shah912c9482014-02-07 14:01:04 -0800267 mMem.mem.dump();
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700268 mdp_wrapper::dump("mRotImgInfo", mRotImgInfo);
269 mdp_wrapper::dump("mRotDataInfo", mRotDataInfo);
270 ALOGE("== Dump MdpRot end ==");
271}
272
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800273void MdpRot::getDump(char *buf, size_t len) const {
Saurabh Shahae61b2b2013-04-10 16:37:25 -0700274 ovutils::getDump(buf, len, "MdpRotCtrl", mRotImgInfo);
275 ovutils::getDump(buf, len, "MdpRotData", mRotDataInfo);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800276}
277
Saurabh Shahc46cf9d2014-07-02 15:22:34 -0700278int MdpRot::getDownscaleFactor(const int& src_w, const int& src_h,
279 const int& dst_w, const int& dst_h, const uint32_t& /*mdpFormat*/,
280 const bool& /*isInterlaced*/) {
281 int dscale_factor = utils::ROT_DS_NONE;
282 // We need this check to engage the rotator whenever possible to assist MDP
283 // in performing video downscale.
284 // This saves bandwidth and avoids causing the driver to make too many panel
285 // -mode switches between BLT (writeback) and non-BLT (Direct) modes.
286 // Use-case: Video playback [with downscaling and rotation].
287 if (dst_w && dst_h)
288 {
289 float fDscale = (float)(src_w * src_h) / (float)(dst_w * dst_h);
290 uint32_t dscale = (int)sqrtf(fDscale);
291
292 if(dscale < 2) {
293 // Down-scale to > 50% of orig.
294 dscale_factor = utils::ROT_DS_NONE;
295 } else if(dscale < 4) {
296 // Down-scale to between > 25% to <= 50% of orig.
297 dscale_factor = utils::ROT_DS_HALF;
298 } else if(dscale < 8) {
299 // Down-scale to between > 12.5% to <= 25% of orig.
300 dscale_factor = utils::ROT_DS_FOURTH;
301 } else {
302 // Down-scale to <= 12.5% of orig.
303 dscale_factor = utils::ROT_DS_EIGHTH;
304 }
305 }
306 return dscale_factor;
307}
308
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700309} // namespace overlay