blob: ce9d9d529bd4365b4ff2f33ee8c35779359adcff [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
20#include "overlayUtils.h"
21#include "overlayRotator.h"
22
Sushil Chauhanbab187a2013-01-30 17:44:15 -080023#define DEBUG_MDSS_ROT 0
24
Sushil Chauhanc6bd6d92012-12-12 12:33:01 -080025#ifdef VENUS_COLOR_FORMAT
26#include <media/msm_media_info.h>
27#else
28#define VENUS_BUFFER_SIZE(args...) 0
29#endif
30
Naseer Ahmedc21013b2012-11-19 12:44:41 -050031#ifndef MDSS_MDP_ROT_ONLY
32#define MDSS_MDP_ROT_ONLY 0x80
33#endif
34
Sushil Chauhan95e4c9f2013-01-14 18:44:14 -080035#define MDSS_ROT_MASK (MDP_ROT_90 | MDP_FLIP_UD | MDP_FLIP_LR)
Sushil Chauhan6e3fab82012-11-19 15:30:33 -080036
Saurabh Shahe012f7a2012-08-18 15:11:57 -070037namespace ovutils = overlay::utils;
38
39namespace overlay {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050040MdssRot::MdssRot() {
41 reset();
42 init();
43}
44
45MdssRot::~MdssRot() { close(); }
46
Saurabh Shahacf10202013-02-26 10:15:15 -080047bool MdssRot::enabled() const { return mEnabled; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050048
Saurabh Shahacf10202013-02-26 10:15:15 -080049void MdssRot::setRotations(uint32_t flags) { mRotInfo.flags |= flags; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050050
Saurabh Shahacf10202013-02-26 10:15:15 -080051int MdssRot::getDstMemId() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050052 return mRotData.dst_data.memory_id;
53}
54
Saurabh Shahacf10202013-02-26 10:15:15 -080055uint32_t MdssRot::getDstOffset() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050056 return mRotData.dst_data.offset;
57}
58
Saurabh Shahacf10202013-02-26 10:15:15 -080059uint32_t MdssRot::getDstFormat() const {
Raj kamal23f69b22012-11-17 00:20:55 +053060 //For mdss src and dst formats are same
61 return mRotInfo.src.format;
62}
Naseer Ahmed758bfc52012-11-28 17:02:08 -050063
Saurabh Shahacf10202013-02-26 10:15:15 -080064uint32_t MdssRot::getSessId() const { return mRotInfo.id; }
Raj kamal23f69b22012-11-17 00:20:55 +053065
Saurabh Shahacf10202013-02-26 10:15:15 -080066bool MdssRot::init() {
Saurabh Shahe012f7a2012-08-18 15:11:57 -070067 if(!utils::openDev(mFd, 0, Res::fbPath, O_RDWR)) {
68 ALOGE("MdssRot failed to init fb0");
69 return false;
70 }
71 return true;
72}
73
74void MdssRot::setSource(const overlay::utils::Whf& awhf) {
75 utils::Whf whf(awhf);
76
77 mRotInfo.src.format = whf.format;
Saurabh Shahe012f7a2012-08-18 15:11:57 -070078 mRotInfo.src.width = whf.w;
79 mRotInfo.src.height = whf.h;
Sushil Chauhan80fc1f92013-04-23 17:30:05 -070080}
Saurabh Shahe012f7a2012-08-18 15:11:57 -070081
Sushil Chauhan80fc1f92013-04-23 17:30:05 -070082void MdssRot::setCrop(const utils::Dim& crop) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -070083
Sushil Chauhan80fc1f92013-04-23 17:30:05 -070084 mRotInfo.src_rect.x = crop.x;
85 mRotInfo.src_rect.y = crop.y;
86 mRotInfo.src_rect.w = crop.w;
87 mRotInfo.src_rect.h = crop.h;
88
89 mRotInfo.dst_rect.x = 0;
90 mRotInfo.dst_rect.y = 0;
91 mRotInfo.dst_rect.w = crop.w;
92 mRotInfo.dst_rect.h = crop.h;
Saurabh Shahe012f7a2012-08-18 15:11:57 -070093}
94
Arun Kumar K.R7e5a1f82014-01-22 10:36:16 -080095void MdssRot::setDownscale(int /*ds*/) {
96}
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080097
Saurabh Shahacf10202013-02-26 10:15:15 -080098void MdssRot::setFlags(const utils::eMdpFlags& flags) {
Sushil Chauhan6dcffbf2013-05-13 19:11:11 -070099 mRotInfo.flags = flags;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700100}
101
Saurabh Shahacf10202013-02-26 10:15:15 -0800102void MdssRot::setTransform(const utils::eTransform& rot)
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700103{
Saurabh Shah78ad4952013-04-05 09:38:01 -0700104 // reset rotation flags to avoid stale orientation values
105 mRotInfo.flags &= ~MDSS_ROT_MASK;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700106 int flags = utils::getMdpOrient(rot);
107 if (flags != -1)
108 setRotations(flags);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700109 mOrientation = static_cast<utils::eTransform>(flags);
110 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, flags);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800111}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700112
Saurabh Shahacf10202013-02-26 10:15:15 -0800113void MdssRot::doTransform() {
Sushil Chauhan6dcffbf2013-05-13 19:11:11 -0700114 mRotInfo.flags |= mOrientation;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700115 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
116 utils::swap(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h);
117}
118
119bool MdssRot::commit() {
120 doTransform();
121 mRotInfo.flags |= MDSS_MDP_ROT_ONLY;
Saurabh Shahacf10202013-02-26 10:15:15 -0800122 mEnabled = true;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700123 if(!overlay::mdp_wrapper::setOverlay(mFd.getFD(), mRotInfo)) {
124 ALOGE("MdssRot commit failed!");
125 dump();
Saurabh Shahacf10202013-02-26 10:15:15 -0800126 return (mEnabled = false);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700127 }
128 mRotData.id = mRotInfo.id;
129 return true;
130}
131
132bool MdssRot::queueBuffer(int fd, uint32_t offset) {
133 if(enabled()) {
134 mRotData.data.memory_id = fd;
135 mRotData.data.offset = offset;
136
Saurabh Shah912c9482014-02-07 14:01:04 -0800137 if(false == remap(RotMem::ROT_NUM_BUFS)) {
138 ALOGE("%s Remap failed, not queuing", __FUNCTION__);
139 return false;
140 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700141
142 mRotData.dst_data.offset =
Saurabh Shah912c9482014-02-07 14:01:04 -0800143 mMem.mRotOffset[mMem.mCurrIndex];
144 mMem.mCurrIndex =
145 (mMem.mCurrIndex + 1) % mMem.mem.numBufs();
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700146
147 if(!overlay::mdp_wrapper::play(mFd.getFD(), mRotData)) {
148 ALOGE("MdssRot play failed!");
149 dump();
150 return false;
151 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700152 }
153 return true;
154}
155
156bool MdssRot::open_i(uint32_t numbufs, uint32_t bufsz)
157{
158 OvMem mem;
159 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700160 bool isSecure = mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700161
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700162 if(!mem.open(numbufs, bufsz, isSecure)){
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700163 ALOGE("%s: Failed to open", __func__);
164 mem.close();
165 return false;
166 }
167
168 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
169 OVASSERT(mem.getFD() != -1, "getFd is -1");
170
171 mRotData.dst_data.memory_id = mem.getFD();
172 mRotData.dst_data.offset = 0;
Saurabh Shah912c9482014-02-07 14:01:04 -0800173 mMem.mem = mem;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700174 return true;
175}
176
177bool MdssRot::remap(uint32_t numbufs) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800178 // Calculate the size based on rotator's dst format, w and h.
179 uint32_t opBufSize = calcOutputBufSize();
180 // If current size changed, remap
Saurabh Shah912c9482014-02-07 14:01:04 -0800181 if(opBufSize == mMem.size()) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800182 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, opBufSize);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700183 return true;
184 }
185
186 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700187
Saurabh Shah912c9482014-02-07 14:01:04 -0800188 if(!mMem.close()) {
189 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
190 return false;
191 }
192
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800193 if(!open_i(numbufs, opBufSize)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700194 ALOGE("%s Error could not open", __FUNCTION__);
195 return false;
196 }
Saurabh Shah912c9482014-02-07 14:01:04 -0800197
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700198 for (uint32_t i = 0; i < numbufs; ++i) {
Saurabh Shah912c9482014-02-07 14:01:04 -0800199 mMem.mRotOffset[i] = i * opBufSize;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700200 }
Saurabh Shah912c9482014-02-07 14:01:04 -0800201
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700202 return true;
203}
204
205bool MdssRot::close() {
206 bool success = true;
Ken Zhangba48b012012-12-11 20:33:59 -0500207 if(mFd.valid() && (getSessId() != (uint32_t) MSMFB_NEW_REQUEST)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700208 if(!mdp_wrapper::unsetOverlay(mFd.getFD(), getSessId())) {
209 ALOGE("MdssRot::close unsetOverlay failed, fd=%d sessId=%d",
210 mFd.getFD(), getSessId());
Ken Zhangba48b012012-12-11 20:33:59 -0500211 success = false;
212 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700213 }
214
215 if (!mFd.close()) {
216 ALOGE("Mdss Rot error closing fd");
217 success = false;
218 }
219 if (!mMem.close()) {
220 ALOGE("Mdss Rot error closing mem");
221 success = false;
222 }
223 reset();
224 return success;
225}
226
227void MdssRot::reset() {
228 ovutils::memset0(mRotInfo);
229 ovutils::memset0(mRotData);
230 mRotData.data.memory_id = -1;
231 mRotInfo.id = MSMFB_NEW_REQUEST;
Saurabh Shah912c9482014-02-07 14:01:04 -0800232 ovutils::memset0(mMem.mRotOffset);
233 mMem.mCurrIndex = 0;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700234 mOrientation = utils::OVERLAY_TRANSFORM_0;
235}
236
237void MdssRot::dump() const {
238 ALOGE("== Dump MdssRot start ==");
239 mFd.dump();
Saurabh Shah912c9482014-02-07 14:01:04 -0800240 mMem.mem.dump();
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700241 mdp_wrapper::dump("mRotInfo", mRotInfo);
242 mdp_wrapper::dump("mRotData", mRotData);
243 ALOGE("== Dump MdssRot end ==");
244}
245
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800246uint32_t MdssRot::calcOutputBufSize() {
247 uint32_t opBufSize = 0;
248 ovutils::Whf destWhf(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h,
249 mRotInfo.src.format); //mdss src and dst formats are same.
Sushil Chauhanb4d184f2013-02-11 16:13:10 -0800250
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800251 if (mRotInfo.flags & ovutils::OV_MDSS_MDP_BWC_EN) {
Sushil Chauhan466766f2013-07-31 11:21:07 -0700252 opBufSize = calcCompressedBufSize(destWhf);
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800253 } else {
254 opBufSize = Rotator::calcOutputBufSize(destWhf);
255 }
Sushil Chauhanb4d184f2013-02-11 16:13:10 -0800256
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800257 return opBufSize;
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800258}
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800259
260void MdssRot::getDump(char *buf, size_t len) const {
Saurabh Shahae61b2b2013-04-10 16:37:25 -0700261 ovutils::getDump(buf, len, "MdssRotCtrl", mRotInfo);
262 ovutils::getDump(buf, len, "MdssRotData", mRotData);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800263}
264
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800265// Calculate the compressed o/p buffer size for BWC
Sushil Chauhan466766f2013-07-31 11:21:07 -0700266uint32_t MdssRot::calcCompressedBufSize(const ovutils::Whf& destWhf) {
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800267 uint32_t bufSize = 0;
Sushil Chauhan466766f2013-07-31 11:21:07 -0700268 int aWidth = ovutils::align(destWhf.w, 64);
269 int aHeight = ovutils::align(destWhf.h, 4);
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800270 int rau_cnt = aWidth/64;
271 int stride0 = (64 * 4 * rau_cnt) + rau_cnt/8;
Sushil Chauhan3bdf9e52013-07-08 18:34:35 -0700272 int stride1 = ((64 * 2 * rau_cnt) + rau_cnt/8) * 2;
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800273 int stride0_off = (aHeight/4);
274 int stride1_off = (aHeight/2);
275
276 //New o/p size for BWC
277 bufSize = (stride0 * stride0_off + stride1 * stride1_off) +
278 (rau_cnt * 2 * (stride0_off + stride1_off));
279 ALOGD_IF(DEBUG_MDSS_ROT, "%s: width = %d, height = %d raucount = %d"
280 "opBufSize = %d ", __FUNCTION__, aWidth, aHeight, rau_cnt, bufSize);
281 return bufSize;
282}
283
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700284} // namespace overlay