blob: f1024b4008772136dac368b66516c612a1621028 [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
3 * Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
4 * 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
Saurabh Shahe012f7a2012-08-18 15:11:57 -070033namespace ovutils = overlay::utils;
34
35namespace overlay {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050036MdssRot::MdssRot() {
37 reset();
38 init();
39}
40
41MdssRot::~MdssRot() { close(); }
42
43void MdssRot::setEnable() { mEnabled = true; }
44
45void MdssRot::setDisable() { mEnabled = false; }
46
47bool MdssRot::enabled() const { return mEnabled; }
48
49void MdssRot::setRotations(uint32_t flags) { mRotInfo.flags |= flags; }
50
51int MdssRot::getDstMemId() const {
52 return mRotData.dst_data.memory_id;
53}
54
55uint32_t MdssRot::getDstOffset() const {
56 return mRotData.dst_data.offset;
57}
58
59uint32_t MdssRot::getSessId() const { return mRotInfo.id; }
60
61void MdssRot::setSrcFB() {
62 mRotData.data.flags |= MDP_MEMORY_ID_TYPE_FB;
63}
Saurabh Shahe012f7a2012-08-18 15:11:57 -070064
65bool MdssRot::init() {
66 if(!utils::openDev(mFd, 0, Res::fbPath, O_RDWR)) {
67 ALOGE("MdssRot failed to init fb0");
68 return false;
69 }
70 return true;
71}
72
73void MdssRot::setSource(const overlay::utils::Whf& awhf) {
74 utils::Whf whf(awhf);
75
76 mRotInfo.src.format = whf.format;
77 if(whf.format == MDP_Y_CRCB_H2V2_TILE ||
78 whf.format == MDP_Y_CBCR_H2V2_TILE) {
79 whf.w = utils::alignup(awhf.w, 64);
80 whf.h = utils::alignup(awhf.h, 32);
81 }
82
83 mRotInfo.src.width = whf.w;
84 mRotInfo.src.height = whf.h;
85
86 mRotInfo.src_rect.w = whf.w;
87 mRotInfo.src_rect.h = whf.h;
88
89 mRotInfo.dst_rect.w = whf.w;
90 mRotInfo.dst_rect.h = whf.h;
91
92 mBufSize = awhf.size;
93}
94
95void MdssRot::setFlags(const utils::eMdpFlags& flags) {
Sushil Chauhanfaae0422012-10-23 23:48:04 -070096 mRotInfo.flags &= ~utils::OV_MDP_SECURE_OVERLAY_SESSION;
97 if (flags & utils::OV_MDP_SECURE_OVERLAY_SESSION) {
98 mRotInfo.flags |= utils::OV_MDP_SECURE_OVERLAY_SESSION;
99 }
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;
133 return true;
134}
135
136bool MdssRot::queueBuffer(int fd, uint32_t offset) {
137 if(enabled()) {
138 mRotData.data.memory_id = fd;
139 mRotData.data.offset = offset;
140
141 remap(RotMem::Mem::ROT_NUM_BUFS);
142 OVASSERT(mMem.curr().m.numBufs(), "queueBuffer numbufs is 0");
143
144 mRotData.dst_data.offset =
145 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
146 mMem.curr().mCurrOffset =
147 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
148
149 if(!overlay::mdp_wrapper::play(mFd.getFD(), mRotData)) {
150 ALOGE("MdssRot play failed!");
151 dump();
152 return false;
153 }
154
155 // if the prev mem is valid, we need to close
156 if(mMem.prev().valid()) {
157 // FIXME if no wait for vsync the above
158 // play will return immediatly and might cause
159 // tearing when prev.close is called.
160 if(!mMem.prev().close()) {
161 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
162 return false;
163 }
164 }
165 }
166 return true;
167}
168
169bool MdssRot::open_i(uint32_t numbufs, uint32_t bufsz)
170{
171 OvMem mem;
172 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700173 bool isSecure = mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700174
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700175 if(!mem.open(numbufs, bufsz, isSecure)){
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700176 ALOGE("%s: Failed to open", __func__);
177 mem.close();
178 return false;
179 }
180
181 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
182 OVASSERT(mem.getFD() != -1, "getFd is -1");
183
184 mRotData.dst_data.memory_id = mem.getFD();
185 mRotData.dst_data.offset = 0;
186 mMem.curr().m = mem;
187 return true;
188}
189
190bool MdssRot::remap(uint32_t numbufs) {
191 // if current size changed, remap
192 if(mBufSize == mMem.curr().size()) {
193 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, mBufSize);
194 return true;
195 }
196
197 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
198 OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
199
200 // ++mMem will make curr to be prev, and prev will be curr
201 ++mMem;
202 if(!open_i(numbufs, mBufSize)) {
203 ALOGE("%s Error could not open", __FUNCTION__);
204 return false;
205 }
206 for (uint32_t i = 0; i < numbufs; ++i) {
207 mMem.curr().mRotOffset[i] = i * mBufSize;
208 }
209 return true;
210}
211
212bool MdssRot::close() {
213 bool success = true;
Ken Zhangba48b012012-12-11 20:33:59 -0500214 if(mFd.valid() && (getSessId() != (uint32_t) MSMFB_NEW_REQUEST)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700215 if(!mdp_wrapper::unsetOverlay(mFd.getFD(), getSessId())) {
216 ALOGE("MdssRot::close unsetOverlay failed, fd=%d sessId=%d",
217 mFd.getFD(), getSessId());
Ken Zhangba48b012012-12-11 20:33:59 -0500218 success = false;
219 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700220 }
221
222 if (!mFd.close()) {
223 ALOGE("Mdss Rot error closing fd");
224 success = false;
225 }
226 if (!mMem.close()) {
227 ALOGE("Mdss Rot error closing mem");
228 success = false;
229 }
230 reset();
231 return success;
232}
233
234void MdssRot::reset() {
235 ovutils::memset0(mRotInfo);
236 ovutils::memset0(mRotData);
237 mRotData.data.memory_id = -1;
238 mRotInfo.id = MSMFB_NEW_REQUEST;
239 ovutils::memset0(mMem.curr().mRotOffset);
240 ovutils::memset0(mMem.prev().mRotOffset);
241 mMem.curr().mCurrOffset = 0;
242 mMem.prev().mCurrOffset = 0;
243 mBufSize = 0;
244 mOrientation = utils::OVERLAY_TRANSFORM_0;
245}
246
247void MdssRot::dump() const {
248 ALOGE("== Dump MdssRot start ==");
249 mFd.dump();
250 mMem.curr().m.dump();
251 mdp_wrapper::dump("mRotInfo", mRotInfo);
252 mdp_wrapper::dump("mRotData", mRotData);
253 ALOGE("== Dump MdssRot end ==");
254}
255
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800256void MdssRot::setBufSize(int format) {
257 if (format == MDP_Y_CBCR_H2V2_VENUS) {
258 mBufSize = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, mRotInfo.dst_rect.w,
259 mRotInfo.dst_rect.h);
260 }
261}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700262} // namespace overlay