blob: b0f51c11826682c36be1c2927077a62d4527939e [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
46void MdssRot::setEnable() { mEnabled = true; }
47
48void MdssRot::setDisable() { mEnabled = false; }
49
50bool MdssRot::enabled() const { return mEnabled; }
51
52void MdssRot::setRotations(uint32_t flags) { mRotInfo.flags |= flags; }
53
54int MdssRot::getDstMemId() const {
55 return mRotData.dst_data.memory_id;
56}
57
58uint32_t MdssRot::getDstOffset() const {
59 return mRotData.dst_data.offset;
60}
61
62uint32_t MdssRot::getSessId() const { return mRotInfo.id; }
63
64void MdssRot::setSrcFB() {
65 mRotData.data.flags |= MDP_MEMORY_ID_TYPE_FB;
66}
Saurabh Shahe012f7a2012-08-18 15:11:57 -070067
68bool MdssRot::init() {
69 if(!utils::openDev(mFd, 0, Res::fbPath, O_RDWR)) {
70 ALOGE("MdssRot failed to init fb0");
71 return false;
72 }
73 return true;
74}
75
76void MdssRot::setSource(const overlay::utils::Whf& awhf) {
77 utils::Whf whf(awhf);
78
79 mRotInfo.src.format = whf.format;
80 if(whf.format == MDP_Y_CRCB_H2V2_TILE ||
81 whf.format == MDP_Y_CBCR_H2V2_TILE) {
82 whf.w = utils::alignup(awhf.w, 64);
83 whf.h = utils::alignup(awhf.h, 32);
84 }
85
86 mRotInfo.src.width = whf.w;
87 mRotInfo.src.height = whf.h;
88
89 mRotInfo.src_rect.w = whf.w;
90 mRotInfo.src_rect.h = whf.h;
91
92 mRotInfo.dst_rect.w = whf.w;
93 mRotInfo.dst_rect.h = whf.h;
94
95 mBufSize = awhf.size;
96}
97
98void 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
102void MdssRot::setTransform(const utils::eTransform& rot, const bool& rotUsed)
103{
104 int flags = utils::getMdpOrient(rot);
105 if (flags != -1)
106 setRotations(flags);
107 //getMdpOrient will switch the flips if the source is 90 rotated.
108 //Clients in Android dont factor in 90 rotation while deciding the flip.
109 mOrientation = static_cast<utils::eTransform>(flags);
110 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, flags);
111
112 setDisable();
113 if(rotUsed) {
114 setEnable();
115 }
116}
117
118void MdssRot::doTransform() {
119 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
120 utils::swap(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h);
121}
122
123bool MdssRot::commit() {
124 doTransform();
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800125 setBufSize(mRotInfo.src.format);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700126 mRotInfo.flags |= MDSS_MDP_ROT_ONLY;
127 if(!overlay::mdp_wrapper::setOverlay(mFd.getFD(), mRotInfo)) {
128 ALOGE("MdssRot commit failed!");
129 dump();
130 return false;
131 }
132 mRotData.id = mRotInfo.id;
Sushil Chauhan95e4c9f2013-01-14 18:44:14 -0800133 // reset rotation flags to avoid stale orientation values
134 mRotInfo.flags &= ~MDSS_ROT_MASK;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700135 return true;
136}
137
138bool MdssRot::queueBuffer(int fd, uint32_t offset) {
139 if(enabled()) {
140 mRotData.data.memory_id = fd;
141 mRotData.data.offset = offset;
142
143 remap(RotMem::Mem::ROT_NUM_BUFS);
144 OVASSERT(mMem.curr().m.numBufs(), "queueBuffer numbufs is 0");
145
146 mRotData.dst_data.offset =
147 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
148 mMem.curr().mCurrOffset =
149 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
150
151 if(!overlay::mdp_wrapper::play(mFd.getFD(), mRotData)) {
152 ALOGE("MdssRot play failed!");
153 dump();
154 return false;
155 }
156
157 // if the prev mem is valid, we need to close
158 if(mMem.prev().valid()) {
159 // FIXME if no wait for vsync the above
160 // play will return immediatly and might cause
161 // tearing when prev.close is called.
162 if(!mMem.prev().close()) {
163 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
164 return false;
165 }
166 }
167 }
168 return true;
169}
170
171bool MdssRot::open_i(uint32_t numbufs, uint32_t bufsz)
172{
173 OvMem mem;
174 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700175 bool isSecure = mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700176
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700177 if(!mem.open(numbufs, bufsz, isSecure)){
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700178 ALOGE("%s: Failed to open", __func__);
179 mem.close();
180 return false;
181 }
182
183 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
184 OVASSERT(mem.getFD() != -1, "getFd is -1");
185
186 mRotData.dst_data.memory_id = mem.getFD();
187 mRotData.dst_data.offset = 0;
188 mMem.curr().m = mem;
189 return true;
190}
191
192bool MdssRot::remap(uint32_t numbufs) {
193 // if current size changed, remap
194 if(mBufSize == mMem.curr().size()) {
195 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, mBufSize);
196 return true;
197 }
198
199 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
200 OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
201
202 // ++mMem will make curr to be prev, and prev will be curr
203 ++mMem;
204 if(!open_i(numbufs, mBufSize)) {
205 ALOGE("%s Error could not open", __FUNCTION__);
206 return false;
207 }
208 for (uint32_t i = 0; i < numbufs; ++i) {
209 mMem.curr().mRotOffset[i] = i * mBufSize;
210 }
211 return true;
212}
213
214bool MdssRot::close() {
215 bool success = true;
Ken Zhangba48b012012-12-11 20:33:59 -0500216 if(mFd.valid() && (getSessId() != (uint32_t) MSMFB_NEW_REQUEST)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700217 if(!mdp_wrapper::unsetOverlay(mFd.getFD(), getSessId())) {
218 ALOGE("MdssRot::close unsetOverlay failed, fd=%d sessId=%d",
219 mFd.getFD(), getSessId());
Ken Zhangba48b012012-12-11 20:33:59 -0500220 success = false;
221 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700222 }
223
224 if (!mFd.close()) {
225 ALOGE("Mdss Rot error closing fd");
226 success = false;
227 }
228 if (!mMem.close()) {
229 ALOGE("Mdss Rot error closing mem");
230 success = false;
231 }
232 reset();
233 return success;
234}
235
236void MdssRot::reset() {
237 ovutils::memset0(mRotInfo);
238 ovutils::memset0(mRotData);
239 mRotData.data.memory_id = -1;
240 mRotInfo.id = MSMFB_NEW_REQUEST;
241 ovutils::memset0(mMem.curr().mRotOffset);
242 ovutils::memset0(mMem.prev().mRotOffset);
243 mMem.curr().mCurrOffset = 0;
244 mMem.prev().mCurrOffset = 0;
245 mBufSize = 0;
246 mOrientation = utils::OVERLAY_TRANSFORM_0;
247}
248
249void MdssRot::dump() const {
250 ALOGE("== Dump MdssRot start ==");
251 mFd.dump();
252 mMem.curr().m.dump();
253 mdp_wrapper::dump("mRotInfo", mRotInfo);
254 mdp_wrapper::dump("mRotData", mRotData);
255 ALOGE("== Dump MdssRot end ==");
256}
257
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800258void MdssRot::setBufSize(int format) {
259 if (format == MDP_Y_CBCR_H2V2_VENUS) {
260 mBufSize = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, mRotInfo.dst_rect.w,
261 mRotInfo.dst_rect.h);
262 }
Sushil Chauhan6e3fab82012-11-19 15:30:33 -0800263 if (mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
264 mBufSize = utils::align(mBufSize, SIZE_1M);
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800265}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700266} // namespace overlay