blob: 6f2b5642c912045896c86e0e1e3e4c7e75f3dd8d [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
Saurabh Shah08c65b32012-12-18 14:23:43 -08003 * 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
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 Chauhan6e3fab82012-11-19 15:30:33 -080035#define SIZE_1M 0x00100000
Sushil Chauhan95e4c9f2013-01-14 18:44:14 -080036#define MDSS_ROT_MASK (MDP_ROT_90 | MDP_FLIP_UD | MDP_FLIP_LR)
Sushil Chauhan6e3fab82012-11-19 15:30:33 -080037
Saurabh Shahe012f7a2012-08-18 15:11:57 -070038namespace ovutils = overlay::utils;
39
40namespace overlay {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050041MdssRot::MdssRot() {
42 reset();
43 init();
44}
45
46MdssRot::~MdssRot() { close(); }
47
Saurabh Shahacf10202013-02-26 10:15:15 -080048bool MdssRot::enabled() const { return mEnabled; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050049
Saurabh Shahacf10202013-02-26 10:15:15 -080050void MdssRot::setRotations(uint32_t flags) { mRotInfo.flags |= flags; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050051
Saurabh Shahacf10202013-02-26 10:15:15 -080052int MdssRot::getDstMemId() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050053 return mRotData.dst_data.memory_id;
54}
55
Saurabh Shahacf10202013-02-26 10:15:15 -080056uint32_t MdssRot::getDstOffset() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050057 return mRotData.dst_data.offset;
58}
59
Saurabh Shahacf10202013-02-26 10:15:15 -080060uint32_t MdssRot::getDstFormat() const {
Raj kamal23f69b22012-11-17 00:20:55 +053061 //For mdss src and dst formats are same
62 return mRotInfo.src.format;
63}
Naseer Ahmed758bfc52012-11-28 17:02:08 -050064
Saurabh Shahacf10202013-02-26 10:15:15 -080065uint32_t MdssRot::getSessId() const { return mRotInfo.id; }
Raj kamal23f69b22012-11-17 00:20:55 +053066
Saurabh Shahacf10202013-02-26 10:15:15 -080067bool MdssRot::init() {
Saurabh Shahe012f7a2012-08-18 15:11:57 -070068 if(!utils::openDev(mFd, 0, Res::fbPath, O_RDWR)) {
69 ALOGE("MdssRot failed to init fb0");
70 return false;
71 }
72 return true;
73}
74
75void MdssRot::setSource(const overlay::utils::Whf& awhf) {
76 utils::Whf whf(awhf);
77
78 mRotInfo.src.format = whf.format;
Saurabh Shahe012f7a2012-08-18 15:11:57 -070079 mRotInfo.src.width = whf.w;
80 mRotInfo.src.height = whf.h;
Sushil Chauhan80fc1f92013-04-23 17:30:05 -070081}
Saurabh Shahe012f7a2012-08-18 15:11:57 -070082
Sushil Chauhan80fc1f92013-04-23 17:30:05 -070083void MdssRot::setCrop(const utils::Dim& crop) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -070084
Sushil Chauhan80fc1f92013-04-23 17:30:05 -070085 mRotInfo.src_rect.x = crop.x;
86 mRotInfo.src_rect.y = crop.y;
87 mRotInfo.src_rect.w = crop.w;
88 mRotInfo.src_rect.h = crop.h;
89
90 mRotInfo.dst_rect.x = 0;
91 mRotInfo.dst_rect.y = 0;
92 mRotInfo.dst_rect.w = crop.w;
93 mRotInfo.dst_rect.h = crop.h;
Saurabh Shahe012f7a2012-08-18 15:11:57 -070094}
95
Saurabh Shahacf10202013-02-26 10:15:15 -080096void MdssRot::setDownscale(int ds) {}
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080097
Saurabh Shahacf10202013-02-26 10:15:15 -080098void MdssRot::setFlags(const utils::eMdpFlags& flags) {
Sushil Chauhanbbca6292013-01-07 18:07:16 -080099 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() {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700114 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
115 utils::swap(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h);
116}
117
118bool MdssRot::commit() {
119 doTransform();
120 mRotInfo.flags |= MDSS_MDP_ROT_ONLY;
Saurabh Shahacf10202013-02-26 10:15:15 -0800121 mEnabled = true;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700122 if(!overlay::mdp_wrapper::setOverlay(mFd.getFD(), mRotInfo)) {
123 ALOGE("MdssRot commit failed!");
124 dump();
Saurabh Shahacf10202013-02-26 10:15:15 -0800125 return (mEnabled = false);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700126 }
127 mRotData.id = mRotInfo.id;
128 return true;
129}
130
131bool MdssRot::queueBuffer(int fd, uint32_t offset) {
132 if(enabled()) {
133 mRotData.data.memory_id = fd;
134 mRotData.data.offset = offset;
135
136 remap(RotMem::Mem::ROT_NUM_BUFS);
137 OVASSERT(mMem.curr().m.numBufs(), "queueBuffer numbufs is 0");
138
139 mRotData.dst_data.offset =
140 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
141 mMem.curr().mCurrOffset =
142 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
143
144 if(!overlay::mdp_wrapper::play(mFd.getFD(), mRotData)) {
145 ALOGE("MdssRot play failed!");
146 dump();
147 return false;
148 }
149
150 // if the prev mem is valid, we need to close
151 if(mMem.prev().valid()) {
152 // FIXME if no wait for vsync the above
153 // play will return immediatly and might cause
154 // tearing when prev.close is called.
155 if(!mMem.prev().close()) {
156 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
157 return false;
158 }
159 }
160 }
161 return true;
162}
163
164bool MdssRot::open_i(uint32_t numbufs, uint32_t bufsz)
165{
166 OvMem mem;
167 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700168 bool isSecure = mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700169
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700170 if(!mem.open(numbufs, bufsz, isSecure)){
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700171 ALOGE("%s: Failed to open", __func__);
172 mem.close();
173 return false;
174 }
175
176 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
177 OVASSERT(mem.getFD() != -1, "getFd is -1");
178
179 mRotData.dst_data.memory_id = mem.getFD();
180 mRotData.dst_data.offset = 0;
181 mMem.curr().m = mem;
182 return true;
183}
184
185bool MdssRot::remap(uint32_t numbufs) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800186 // Calculate the size based on rotator's dst format, w and h.
187 uint32_t opBufSize = calcOutputBufSize();
188 // If current size changed, remap
189 if(opBufSize == mMem.curr().size()) {
190 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, opBufSize);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700191 return true;
192 }
193
194 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
195 OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
196
197 // ++mMem will make curr to be prev, and prev will be curr
198 ++mMem;
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 }
203 for (uint32_t i = 0; i < numbufs; ++i) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800204 mMem.curr().mRotOffset[i] = i * opBufSize;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700205 }
206 return true;
207}
208
209bool MdssRot::close() {
210 bool success = true;
Ken Zhangba48b012012-12-11 20:33:59 -0500211 if(mFd.valid() && (getSessId() != (uint32_t) MSMFB_NEW_REQUEST)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700212 if(!mdp_wrapper::unsetOverlay(mFd.getFD(), getSessId())) {
213 ALOGE("MdssRot::close unsetOverlay failed, fd=%d sessId=%d",
214 mFd.getFD(), getSessId());
Ken Zhangba48b012012-12-11 20:33:59 -0500215 success = false;
216 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700217 }
218
219 if (!mFd.close()) {
220 ALOGE("Mdss Rot error closing fd");
221 success = false;
222 }
223 if (!mMem.close()) {
224 ALOGE("Mdss Rot error closing mem");
225 success = false;
226 }
227 reset();
228 return success;
229}
230
231void MdssRot::reset() {
232 ovutils::memset0(mRotInfo);
233 ovutils::memset0(mRotData);
234 mRotData.data.memory_id = -1;
235 mRotInfo.id = MSMFB_NEW_REQUEST;
236 ovutils::memset0(mMem.curr().mRotOffset);
237 ovutils::memset0(mMem.prev().mRotOffset);
238 mMem.curr().mCurrOffset = 0;
239 mMem.prev().mCurrOffset = 0;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700240 mOrientation = utils::OVERLAY_TRANSFORM_0;
241}
242
243void MdssRot::dump() const {
244 ALOGE("== Dump MdssRot start ==");
245 mFd.dump();
246 mMem.curr().m.dump();
247 mdp_wrapper::dump("mRotInfo", mRotInfo);
248 mdp_wrapper::dump("mRotData", mRotData);
249 ALOGE("== Dump MdssRot end ==");
250}
251
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800252uint32_t MdssRot::calcOutputBufSize() {
253 uint32_t opBufSize = 0;
254 ovutils::Whf destWhf(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h,
255 mRotInfo.src.format); //mdss src and dst formats are same.
Sushil Chauhanb4d184f2013-02-11 16:13:10 -0800256
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800257 if (mRotInfo.flags & ovutils::OV_MDSS_MDP_BWC_EN) {
258 opBufSize = calcCompressedBufSize();
259 } else {
260 opBufSize = Rotator::calcOutputBufSize(destWhf);
261 }
Sushil Chauhanb4d184f2013-02-11 16:13:10 -0800262
Sushil Chauhan6e3fab82012-11-19 15:30:33 -0800263 if (mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800264 opBufSize = utils::align(opBufSize, SIZE_1M);
265
266 return opBufSize;
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800267}
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800268
269void MdssRot::getDump(char *buf, size_t len) const {
Saurabh Shahae61b2b2013-04-10 16:37:25 -0700270 ovutils::getDump(buf, len, "MdssRotCtrl", mRotInfo);
271 ovutils::getDump(buf, len, "MdssRotData", mRotData);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800272}
273
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800274// Calculate the compressed o/p buffer size for BWC
275uint32_t MdssRot::calcCompressedBufSize() {
276 uint32_t bufSize = 0;
277 int aWidth = ovutils::align(mRotInfo.src_rect.w, 64);
278 int aHeight = ovutils::align(mRotInfo.src_rect.h, 4);
279 int rau_cnt = aWidth/64;
280 int stride0 = (64 * 4 * rau_cnt) + rau_cnt/8;
281 int stride1 = (64 * 2 * rau_cnt) + rau_cnt/8;
282 int stride0_off = (aHeight/4);
283 int stride1_off = (aHeight/2);
284
285 //New o/p size for BWC
286 bufSize = (stride0 * stride0_off + stride1 * stride1_off) +
287 (rau_cnt * 2 * (stride0_off + stride1_off));
288 ALOGD_IF(DEBUG_MDSS_ROT, "%s: width = %d, height = %d raucount = %d"
289 "opBufSize = %d ", __FUNCTION__, aWidth, aHeight, rau_cnt, bufSize);
290 return bufSize;
291}
292
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700293} // namespace overlay