blob: cb3c48f5557a01988c1ddfe547672f22e5fa59a4 [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) {
96 // TODO
97}
98
99void MdssRot::setTransform(const utils::eTransform& rot, const bool& rotUsed)
100{
101 int flags = utils::getMdpOrient(rot);
102 if (flags != -1)
103 setRotations(flags);
104 //getMdpOrient will switch the flips if the source is 90 rotated.
105 //Clients in Android dont factor in 90 rotation while deciding the flip.
106 mOrientation = static_cast<utils::eTransform>(flags);
107 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, flags);
108
109 setDisable();
110 if(rotUsed) {
111 setEnable();
112 }
113}
114
115void MdssRot::doTransform() {
116 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
117 utils::swap(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h);
118}
119
120bool MdssRot::commit() {
121 doTransform();
122 mRotInfo.flags |= MDSS_MDP_ROT_ONLY;
123 if(!overlay::mdp_wrapper::setOverlay(mFd.getFD(), mRotInfo)) {
124 ALOGE("MdssRot commit failed!");
125 dump();
126 return false;
127 }
128 mRotData.id = mRotInfo.id;
129 return true;
130}
131
132bool MdssRot::queueBuffer(int fd, uint32_t offset) {
133 if(enabled()) {
134 mRotData.data.memory_id = fd;
135 mRotData.data.offset = offset;
136
137 remap(RotMem::Mem::ROT_NUM_BUFS);
138 OVASSERT(mMem.curr().m.numBufs(), "queueBuffer numbufs is 0");
139
140 mRotData.dst_data.offset =
141 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
142 mMem.curr().mCurrOffset =
143 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
144
145 if(!overlay::mdp_wrapper::play(mFd.getFD(), mRotData)) {
146 ALOGE("MdssRot play failed!");
147 dump();
148 return false;
149 }
150
151 // if the prev mem is valid, we need to close
152 if(mMem.prev().valid()) {
153 // FIXME if no wait for vsync the above
154 // play will return immediatly and might cause
155 // tearing when prev.close is called.
156 if(!mMem.prev().close()) {
157 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
158 return false;
159 }
160 }
161 }
162 return true;
163}
164
165bool MdssRot::open_i(uint32_t numbufs, uint32_t bufsz)
166{
167 OvMem mem;
168 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
169
170 if(!mem.open(numbufs, bufsz, false)){ // TODO: secure for badger
171 ALOGE("%s: Failed to open", __func__);
172 mem.close();
173 return false;
174 }
175
176 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
177 OVASSERT(mem.getFD() != -1, "getFd is -1");
178
179 mRotData.dst_data.memory_id = mem.getFD();
180 mRotData.dst_data.offset = 0;
181 mMem.curr().m = mem;
182 return true;
183}
184
185bool MdssRot::remap(uint32_t numbufs) {
186 // if current size changed, remap
187 if(mBufSize == mMem.curr().size()) {
188 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, mBufSize);
189 return true;
190 }
191
192 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
193 OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
194
195 // ++mMem will make curr to be prev, and prev will be curr
196 ++mMem;
197 if(!open_i(numbufs, mBufSize)) {
198 ALOGE("%s Error could not open", __FUNCTION__);
199 return false;
200 }
201 for (uint32_t i = 0; i < numbufs; ++i) {
202 mMem.curr().mRotOffset[i] = i * mBufSize;
203 }
204 return true;
205}
206
207bool MdssRot::close() {
208 bool success = true;
Ken Zhangba48b012012-12-11 20:33:59 -0500209 if(mFd.valid() && (getSessId() != (uint32_t) MSMFB_NEW_REQUEST)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700210 if(!mdp_wrapper::unsetOverlay(mFd.getFD(), getSessId())) {
211 ALOGE("MdssRot::close unsetOverlay failed, fd=%d sessId=%d",
212 mFd.getFD(), getSessId());
Ken Zhangba48b012012-12-11 20:33:59 -0500213 success = false;
214 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700215 }
216
217 if (!mFd.close()) {
218 ALOGE("Mdss Rot error closing fd");
219 success = false;
220 }
221 if (!mMem.close()) {
222 ALOGE("Mdss Rot error closing mem");
223 success = false;
224 }
225 reset();
226 return success;
227}
228
229void MdssRot::reset() {
230 ovutils::memset0(mRotInfo);
231 ovutils::memset0(mRotData);
232 mRotData.data.memory_id = -1;
233 mRotInfo.id = MSMFB_NEW_REQUEST;
234 ovutils::memset0(mMem.curr().mRotOffset);
235 ovutils::memset0(mMem.prev().mRotOffset);
236 mMem.curr().mCurrOffset = 0;
237 mMem.prev().mCurrOffset = 0;
238 mBufSize = 0;
239 mOrientation = utils::OVERLAY_TRANSFORM_0;
240}
241
242void MdssRot::dump() const {
243 ALOGE("== Dump MdssRot start ==");
244 mFd.dump();
245 mMem.curr().m.dump();
246 mdp_wrapper::dump("mRotInfo", mRotInfo);
247 mdp_wrapper::dump("mRotData", mRotData);
248 ALOGE("== Dump MdssRot end ==");
249}
250
251} // namespace overlay