blob: 32e0e7720adc1a8a4dfc5ff588bfd5f9866edfbd [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;
Saurabh Shahe012f7a2012-08-18 15:11:57 -070099}
100
Raj kamal23f69b22012-11-17 00:20:55 +0530101inline void MdssRot::setDownscale(int ds) {}
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800102
Raj kamal23f69b22012-11-17 00:20:55 +0530103inline void MdssRot::setFlags(const utils::eMdpFlags& flags) {
Sushil Chauhanbbca6292013-01-07 18:07:16 -0800104 mRotInfo.flags |= flags;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700105}
106
Raj kamal23f69b22012-11-17 00:20:55 +0530107inline void MdssRot::setTransform(const utils::eTransform& rot)
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700108{
109 int flags = utils::getMdpOrient(rot);
110 if (flags != -1)
111 setRotations(flags);
112 //getMdpOrient will switch the flips if the source is 90 rotated.
113 //Clients in Android dont factor in 90 rotation while deciding the flip.
114 mOrientation = static_cast<utils::eTransform>(flags);
115 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, flags);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800116}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700117
Raj kamal23f69b22012-11-17 00:20:55 +0530118inline void MdssRot::setRotatorUsed(const bool& rotUsed) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700119 setDisable();
120 if(rotUsed) {
121 setEnable();
122 }
123}
124
Raj kamal23f69b22012-11-17 00:20:55 +0530125inline void MdssRot::doTransform() {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700126 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
127 utils::swap(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h);
128}
129
130bool MdssRot::commit() {
131 doTransform();
132 mRotInfo.flags |= MDSS_MDP_ROT_ONLY;
133 if(!overlay::mdp_wrapper::setOverlay(mFd.getFD(), mRotInfo)) {
134 ALOGE("MdssRot commit failed!");
135 dump();
136 return false;
137 }
138 mRotData.id = mRotInfo.id;
Sushil Chauhan95e4c9f2013-01-14 18:44:14 -0800139 // reset rotation flags to avoid stale orientation values
140 mRotInfo.flags &= ~MDSS_ROT_MASK;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700141 return true;
142}
143
144bool MdssRot::queueBuffer(int fd, uint32_t offset) {
145 if(enabled()) {
146 mRotData.data.memory_id = fd;
147 mRotData.data.offset = offset;
148
149 remap(RotMem::Mem::ROT_NUM_BUFS);
150 OVASSERT(mMem.curr().m.numBufs(), "queueBuffer numbufs is 0");
151
152 mRotData.dst_data.offset =
153 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
154 mMem.curr().mCurrOffset =
155 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
156
157 if(!overlay::mdp_wrapper::play(mFd.getFD(), mRotData)) {
158 ALOGE("MdssRot play failed!");
159 dump();
160 return false;
161 }
162
163 // if the prev mem is valid, we need to close
164 if(mMem.prev().valid()) {
165 // FIXME if no wait for vsync the above
166 // play will return immediatly and might cause
167 // tearing when prev.close is called.
168 if(!mMem.prev().close()) {
169 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
170 return false;
171 }
172 }
173 }
174 return true;
175}
176
177bool MdssRot::open_i(uint32_t numbufs, uint32_t bufsz)
178{
179 OvMem mem;
180 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700181 bool isSecure = mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700182
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700183 if(!mem.open(numbufs, bufsz, isSecure)){
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700184 ALOGE("%s: Failed to open", __func__);
185 mem.close();
186 return false;
187 }
188
189 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
190 OVASSERT(mem.getFD() != -1, "getFd is -1");
191
192 mRotData.dst_data.memory_id = mem.getFD();
193 mRotData.dst_data.offset = 0;
194 mMem.curr().m = mem;
195 return true;
196}
197
198bool MdssRot::remap(uint32_t numbufs) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800199 // Calculate the size based on rotator's dst format, w and h.
200 uint32_t opBufSize = calcOutputBufSize();
201 // If current size changed, remap
202 if(opBufSize == mMem.curr().size()) {
203 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, opBufSize);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700204 return true;
205 }
206
207 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
208 OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
209
210 // ++mMem will make curr to be prev, and prev will be curr
211 ++mMem;
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800212 if(!open_i(numbufs, opBufSize)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700213 ALOGE("%s Error could not open", __FUNCTION__);
214 return false;
215 }
216 for (uint32_t i = 0; i < numbufs; ++i) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800217 mMem.curr().mRotOffset[i] = i * opBufSize;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700218 }
219 return true;
220}
221
222bool MdssRot::close() {
223 bool success = true;
Ken Zhangba48b012012-12-11 20:33:59 -0500224 if(mFd.valid() && (getSessId() != (uint32_t) MSMFB_NEW_REQUEST)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700225 if(!mdp_wrapper::unsetOverlay(mFd.getFD(), getSessId())) {
226 ALOGE("MdssRot::close unsetOverlay failed, fd=%d sessId=%d",
227 mFd.getFD(), getSessId());
Ken Zhangba48b012012-12-11 20:33:59 -0500228 success = false;
229 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700230 }
231
232 if (!mFd.close()) {
233 ALOGE("Mdss Rot error closing fd");
234 success = false;
235 }
236 if (!mMem.close()) {
237 ALOGE("Mdss Rot error closing mem");
238 success = false;
239 }
240 reset();
241 return success;
242}
243
244void MdssRot::reset() {
245 ovutils::memset0(mRotInfo);
246 ovutils::memset0(mRotData);
247 mRotData.data.memory_id = -1;
248 mRotInfo.id = MSMFB_NEW_REQUEST;
249 ovutils::memset0(mMem.curr().mRotOffset);
250 ovutils::memset0(mMem.prev().mRotOffset);
251 mMem.curr().mCurrOffset = 0;
252 mMem.prev().mCurrOffset = 0;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700253 mOrientation = utils::OVERLAY_TRANSFORM_0;
254}
255
256void MdssRot::dump() const {
257 ALOGE("== Dump MdssRot start ==");
258 mFd.dump();
259 mMem.curr().m.dump();
260 mdp_wrapper::dump("mRotInfo", mRotInfo);
261 mdp_wrapper::dump("mRotData", mRotData);
262 ALOGE("== Dump MdssRot end ==");
263}
264
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800265uint32_t MdssRot::calcOutputBufSize() {
266 uint32_t opBufSize = 0;
267 ovutils::Whf destWhf(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h,
268 mRotInfo.src.format); //mdss src and dst formats are same.
Sushil Chauhanb4d184f2013-02-11 16:13:10 -0800269
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800270 opBufSize = Rotator::calcOutputBufSize(destWhf);
Sushil Chauhanb4d184f2013-02-11 16:13:10 -0800271
Sushil Chauhan6e3fab82012-11-19 15:30:33 -0800272 if (mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800273 opBufSize = utils::align(opBufSize, SIZE_1M);
274
275 return opBufSize;
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800276}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700277} // namespace overlay