blob: 6ee9e4b7d796633a2bbbc9ea6955e4c3a1d0971a [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 Chauhanc6bd6d92012-12-12 12:33:01 -080023#ifdef VENUS_COLOR_FORMAT
24#include <media/msm_media_info.h>
25#else
26#define VENUS_BUFFER_SIZE(args...) 0
27#endif
28
Naseer Ahmedc21013b2012-11-19 12:44:41 -050029#ifndef MDSS_MDP_ROT_ONLY
30#define MDSS_MDP_ROT_ONLY 0x80
31#endif
32
Sushil Chauhan6e3fab82012-11-19 15:30:33 -080033#define SIZE_1M 0x00100000
Sushil Chauhan95e4c9f2013-01-14 18:44:14 -080034#define MDSS_ROT_MASK (MDP_ROT_90 | MDP_FLIP_UD | MDP_FLIP_LR)
Sushil Chauhan6e3fab82012-11-19 15:30:33 -080035
Saurabh Shahe012f7a2012-08-18 15:11:57 -070036namespace ovutils = overlay::utils;
37
38namespace overlay {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050039MdssRot::MdssRot() {
40 reset();
41 init();
42}
43
44MdssRot::~MdssRot() { close(); }
45
Raj kamal23f69b22012-11-17 00:20:55 +053046inline void MdssRot::setEnable() { mEnabled = true; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050047
Raj kamal23f69b22012-11-17 00:20:55 +053048inline void MdssRot::setDisable() { mEnabled = false; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050049
Raj kamal23f69b22012-11-17 00:20:55 +053050inline bool MdssRot::enabled() const { return mEnabled; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050051
Raj kamal23f69b22012-11-17 00:20:55 +053052inline void MdssRot::setRotations(uint32_t flags) { mRotInfo.flags |= flags; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050053
Raj kamal23f69b22012-11-17 00:20:55 +053054inline int MdssRot::getDstMemId() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050055 return mRotData.dst_data.memory_id;
56}
57
Raj kamal23f69b22012-11-17 00:20:55 +053058inline uint32_t MdssRot::getDstOffset() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050059 return mRotData.dst_data.offset;
60}
61
Raj kamal23f69b22012-11-17 00:20:55 +053062inline uint32_t MdssRot::getDstFormat() const {
63 //For mdss src and dst formats are same
64 return mRotInfo.src.format;
65}
Naseer Ahmed758bfc52012-11-28 17:02:08 -050066
Raj kamal23f69b22012-11-17 00:20:55 +053067inline uint32_t MdssRot::getSessId() const { return mRotInfo.id; }
68
69inline void MdssRot::setSrcFB() {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050070 mRotData.data.flags |= MDP_MEMORY_ID_TYPE_FB;
71}
Saurabh Shahe012f7a2012-08-18 15:11:57 -070072
Raj kamal23f69b22012-11-17 00:20:55 +053073inline bool MdssRot::init() {
Saurabh Shahe012f7a2012-08-18 15:11:57 -070074 if(!utils::openDev(mFd, 0, Res::fbPath, O_RDWR)) {
75 ALOGE("MdssRot failed to init fb0");
76 return false;
77 }
78 return true;
79}
80
81void MdssRot::setSource(const overlay::utils::Whf& awhf) {
82 utils::Whf whf(awhf);
83
84 mRotInfo.src.format = whf.format;
85 if(whf.format == MDP_Y_CRCB_H2V2_TILE ||
86 whf.format == MDP_Y_CBCR_H2V2_TILE) {
87 whf.w = utils::alignup(awhf.w, 64);
88 whf.h = utils::alignup(awhf.h, 32);
89 }
90
91 mRotInfo.src.width = whf.w;
92 mRotInfo.src.height = whf.h;
93
94 mRotInfo.src_rect.w = whf.w;
95 mRotInfo.src_rect.h = whf.h;
96
97 mRotInfo.dst_rect.w = whf.w;
98 mRotInfo.dst_rect.h = whf.h;
99
100 mBufSize = awhf.size;
101}
102
Raj kamal23f69b22012-11-17 00:20:55 +0530103inline void MdssRot::setDownscale(int ds) {}
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800104
Raj kamal23f69b22012-11-17 00:20:55 +0530105inline void MdssRot::setFlags(const utils::eMdpFlags& flags) {
Sushil Chauhanbbca6292013-01-07 18:07:16 -0800106 mRotInfo.flags |= flags;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700107}
108
Raj kamal23f69b22012-11-17 00:20:55 +0530109inline void MdssRot::setTransform(const utils::eTransform& rot)
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700110{
111 int flags = utils::getMdpOrient(rot);
112 if (flags != -1)
113 setRotations(flags);
114 //getMdpOrient will switch the flips if the source is 90 rotated.
115 //Clients in Android dont factor in 90 rotation while deciding the flip.
116 mOrientation = static_cast<utils::eTransform>(flags);
117 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, flags);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800118}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700119
Raj kamal23f69b22012-11-17 00:20:55 +0530120inline void MdssRot::setRotatorUsed(const bool& rotUsed) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700121 setDisable();
122 if(rotUsed) {
123 setEnable();
124 }
125}
126
Raj kamal23f69b22012-11-17 00:20:55 +0530127inline void MdssRot::doTransform() {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700128 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
129 utils::swap(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h);
130}
131
132bool MdssRot::commit() {
133 doTransform();
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800134 setBufSize(mRotInfo.src.format);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700135 mRotInfo.flags |= MDSS_MDP_ROT_ONLY;
136 if(!overlay::mdp_wrapper::setOverlay(mFd.getFD(), mRotInfo)) {
137 ALOGE("MdssRot commit failed!");
138 dump();
139 return false;
140 }
141 mRotData.id = mRotInfo.id;
Sushil Chauhan95e4c9f2013-01-14 18:44:14 -0800142 // reset rotation flags to avoid stale orientation values
143 mRotInfo.flags &= ~MDSS_ROT_MASK;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700144 return true;
145}
146
147bool MdssRot::queueBuffer(int fd, uint32_t offset) {
148 if(enabled()) {
149 mRotData.data.memory_id = fd;
150 mRotData.data.offset = offset;
151
152 remap(RotMem::Mem::ROT_NUM_BUFS);
153 OVASSERT(mMem.curr().m.numBufs(), "queueBuffer numbufs is 0");
154
155 mRotData.dst_data.offset =
156 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
157 mMem.curr().mCurrOffset =
158 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
159
160 if(!overlay::mdp_wrapper::play(mFd.getFD(), mRotData)) {
161 ALOGE("MdssRot play failed!");
162 dump();
163 return false;
164 }
165
166 // if the prev mem is valid, we need to close
167 if(mMem.prev().valid()) {
168 // FIXME if no wait for vsync the above
169 // play will return immediatly and might cause
170 // tearing when prev.close is called.
171 if(!mMem.prev().close()) {
172 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
173 return false;
174 }
175 }
176 }
177 return true;
178}
179
180bool MdssRot::open_i(uint32_t numbufs, uint32_t bufsz)
181{
182 OvMem mem;
183 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700184 bool isSecure = mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700185
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700186 if(!mem.open(numbufs, bufsz, isSecure)){
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700187 ALOGE("%s: Failed to open", __func__);
188 mem.close();
189 return false;
190 }
191
192 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
193 OVASSERT(mem.getFD() != -1, "getFd is -1");
194
195 mRotData.dst_data.memory_id = mem.getFD();
196 mRotData.dst_data.offset = 0;
197 mMem.curr().m = mem;
198 return true;
199}
200
201bool MdssRot::remap(uint32_t numbufs) {
202 // if current size changed, remap
203 if(mBufSize == mMem.curr().size()) {
204 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, mBufSize);
205 return true;
206 }
207
208 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
209 OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
210
211 // ++mMem will make curr to be prev, and prev will be curr
212 ++mMem;
213 if(!open_i(numbufs, mBufSize)) {
214 ALOGE("%s Error could not open", __FUNCTION__);
215 return false;
216 }
217 for (uint32_t i = 0; i < numbufs; ++i) {
218 mMem.curr().mRotOffset[i] = i * mBufSize;
219 }
220 return true;
221}
222
223bool MdssRot::close() {
224 bool success = true;
Ken Zhangba48b012012-12-11 20:33:59 -0500225 if(mFd.valid() && (getSessId() != (uint32_t) MSMFB_NEW_REQUEST)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700226 if(!mdp_wrapper::unsetOverlay(mFd.getFD(), getSessId())) {
227 ALOGE("MdssRot::close unsetOverlay failed, fd=%d sessId=%d",
228 mFd.getFD(), getSessId());
Ken Zhangba48b012012-12-11 20:33:59 -0500229 success = false;
230 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700231 }
232
233 if (!mFd.close()) {
234 ALOGE("Mdss Rot error closing fd");
235 success = false;
236 }
237 if (!mMem.close()) {
238 ALOGE("Mdss Rot error closing mem");
239 success = false;
240 }
241 reset();
242 return success;
243}
244
245void MdssRot::reset() {
246 ovutils::memset0(mRotInfo);
247 ovutils::memset0(mRotData);
248 mRotData.data.memory_id = -1;
249 mRotInfo.id = MSMFB_NEW_REQUEST;
250 ovutils::memset0(mMem.curr().mRotOffset);
251 ovutils::memset0(mMem.prev().mRotOffset);
252 mMem.curr().mCurrOffset = 0;
253 mMem.prev().mCurrOffset = 0;
254 mBufSize = 0;
255 mOrientation = utils::OVERLAY_TRANSFORM_0;
256}
257
258void MdssRot::dump() const {
259 ALOGE("== Dump MdssRot start ==");
260 mFd.dump();
261 mMem.curr().m.dump();
262 mdp_wrapper::dump("mRotInfo", mRotInfo);
263 mdp_wrapper::dump("mRotData", mRotData);
264 ALOGE("== Dump MdssRot end ==");
265}
266
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800267void MdssRot::setBufSize(int format) {
268 if (format == MDP_Y_CBCR_H2V2_VENUS) {
269 mBufSize = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, mRotInfo.dst_rect.w,
270 mRotInfo.dst_rect.h);
271 }
Sushil Chauhan6e3fab82012-11-19 15:30:33 -0800272 if (mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
273 mBufSize = utils::align(mBufSize, SIZE_1M);
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800274}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700275} // namespace overlay