blob: cb329adf41d2785eff952a7106e0677450c8db8d [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
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080098void MdssRot::setDownscale(int ds) {
99}
100
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700101void MdssRot::setFlags(const utils::eMdpFlags& flags) {
Sushil Chauhanbbca6292013-01-07 18:07:16 -0800102 mRotInfo.flags |= flags;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700103}
104
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800105void MdssRot::setTransform(const utils::eTransform& rot)
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700106{
107 int flags = utils::getMdpOrient(rot);
108 if (flags != -1)
109 setRotations(flags);
110 //getMdpOrient will switch the flips if the source is 90 rotated.
111 //Clients in Android dont factor in 90 rotation while deciding the flip.
112 mOrientation = static_cast<utils::eTransform>(flags);
113 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, flags);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800114}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700115
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800116void MdssRot::setRotatorUsed(const bool& rotUsed) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700117 setDisable();
118 if(rotUsed) {
119 setEnable();
120 }
121}
122
123void MdssRot::doTransform() {
124 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
125 utils::swap(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h);
126}
127
128bool MdssRot::commit() {
129 doTransform();
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800130 setBufSize(mRotInfo.src.format);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700131 mRotInfo.flags |= MDSS_MDP_ROT_ONLY;
132 if(!overlay::mdp_wrapper::setOverlay(mFd.getFD(), mRotInfo)) {
133 ALOGE("MdssRot commit failed!");
134 dump();
135 return false;
136 }
137 mRotData.id = mRotInfo.id;
Sushil Chauhan95e4c9f2013-01-14 18:44:14 -0800138 // reset rotation flags to avoid stale orientation values
139 mRotInfo.flags &= ~MDSS_ROT_MASK;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700140 return true;
141}
142
143bool MdssRot::queueBuffer(int fd, uint32_t offset) {
144 if(enabled()) {
145 mRotData.data.memory_id = fd;
146 mRotData.data.offset = offset;
147
148 remap(RotMem::Mem::ROT_NUM_BUFS);
149 OVASSERT(mMem.curr().m.numBufs(), "queueBuffer numbufs is 0");
150
151 mRotData.dst_data.offset =
152 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
153 mMem.curr().mCurrOffset =
154 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
155
156 if(!overlay::mdp_wrapper::play(mFd.getFD(), mRotData)) {
157 ALOGE("MdssRot play failed!");
158 dump();
159 return false;
160 }
161
162 // if the prev mem is valid, we need to close
163 if(mMem.prev().valid()) {
164 // FIXME if no wait for vsync the above
165 // play will return immediatly and might cause
166 // tearing when prev.close is called.
167 if(!mMem.prev().close()) {
168 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
169 return false;
170 }
171 }
172 }
173 return true;
174}
175
176bool MdssRot::open_i(uint32_t numbufs, uint32_t bufsz)
177{
178 OvMem mem;
179 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700180 bool isSecure = mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700181
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700182 if(!mem.open(numbufs, bufsz, isSecure)){
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700183 ALOGE("%s: Failed to open", __func__);
184 mem.close();
185 return false;
186 }
187
188 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
189 OVASSERT(mem.getFD() != -1, "getFd is -1");
190
191 mRotData.dst_data.memory_id = mem.getFD();
192 mRotData.dst_data.offset = 0;
193 mMem.curr().m = mem;
194 return true;
195}
196
197bool MdssRot::remap(uint32_t numbufs) {
198 // if current size changed, remap
199 if(mBufSize == mMem.curr().size()) {
200 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, mBufSize);
201 return true;
202 }
203
204 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
205 OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
206
207 // ++mMem will make curr to be prev, and prev will be curr
208 ++mMem;
209 if(!open_i(numbufs, mBufSize)) {
210 ALOGE("%s Error could not open", __FUNCTION__);
211 return false;
212 }
213 for (uint32_t i = 0; i < numbufs; ++i) {
214 mMem.curr().mRotOffset[i] = i * mBufSize;
215 }
216 return true;
217}
218
219bool MdssRot::close() {
220 bool success = true;
Ken Zhangba48b012012-12-11 20:33:59 -0500221 if(mFd.valid() && (getSessId() != (uint32_t) MSMFB_NEW_REQUEST)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700222 if(!mdp_wrapper::unsetOverlay(mFd.getFD(), getSessId())) {
223 ALOGE("MdssRot::close unsetOverlay failed, fd=%d sessId=%d",
224 mFd.getFD(), getSessId());
Ken Zhangba48b012012-12-11 20:33:59 -0500225 success = false;
226 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700227 }
228
229 if (!mFd.close()) {
230 ALOGE("Mdss Rot error closing fd");
231 success = false;
232 }
233 if (!mMem.close()) {
234 ALOGE("Mdss Rot error closing mem");
235 success = false;
236 }
237 reset();
238 return success;
239}
240
241void MdssRot::reset() {
242 ovutils::memset0(mRotInfo);
243 ovutils::memset0(mRotData);
244 mRotData.data.memory_id = -1;
245 mRotInfo.id = MSMFB_NEW_REQUEST;
246 ovutils::memset0(mMem.curr().mRotOffset);
247 ovutils::memset0(mMem.prev().mRotOffset);
248 mMem.curr().mCurrOffset = 0;
249 mMem.prev().mCurrOffset = 0;
250 mBufSize = 0;
251 mOrientation = utils::OVERLAY_TRANSFORM_0;
252}
253
254void MdssRot::dump() const {
255 ALOGE("== Dump MdssRot start ==");
256 mFd.dump();
257 mMem.curr().m.dump();
258 mdp_wrapper::dump("mRotInfo", mRotInfo);
259 mdp_wrapper::dump("mRotData", mRotData);
260 ALOGE("== Dump MdssRot end ==");
261}
262
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800263void MdssRot::setBufSize(int format) {
Sushil Chauhanb4d184f2013-02-11 16:13:10 -0800264
265 switch (format) {
266 case MDP_Y_CBCR_H2V2_VENUS:
267 mBufSize = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, mRotInfo.dst_rect.w,
268 mRotInfo.dst_rect.h);
269 break;
270
271 case MDP_Y_CR_CB_GH2V2:
272 int alignedw = utils::align(mRotInfo.dst_rect.w, 16);
273 int alignedh = mRotInfo.dst_rect.h;
274 mBufSize = (alignedw*alignedh) +
275 (utils::align(alignedw/2, 16) * (alignedh/2))*2;
276 mBufSize = utils::align(mBufSize, 4096);
277 break;
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800278 }
Sushil Chauhanb4d184f2013-02-11 16:13:10 -0800279
Sushil Chauhan6e3fab82012-11-19 15:30:33 -0800280 if (mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
281 mBufSize = utils::align(mBufSize, SIZE_1M);
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800282}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700283} // namespace overlay