blob: 339aae7206636687df465fbc6cf1a70374cc0068 [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
Naseer Ahmedc21013b2012-11-19 12:44:41 -050023#ifndef MDSS_MDP_ROT_ONLY
24#define MDSS_MDP_ROT_ONLY 0x80
25#endif
26
Saurabh Shahe012f7a2012-08-18 15:11:57 -070027namespace ovutils = overlay::utils;
28
29namespace overlay {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050030MdssRot::MdssRot() {
31 reset();
32 init();
33}
34
35MdssRot::~MdssRot() { close(); }
36
37void MdssRot::setEnable() { mEnabled = true; }
38
39void MdssRot::setDisable() { mEnabled = false; }
40
41bool MdssRot::enabled() const { return mEnabled; }
42
43void MdssRot::setRotations(uint32_t flags) { mRotInfo.flags |= flags; }
44
45int MdssRot::getDstMemId() const {
46 return mRotData.dst_data.memory_id;
47}
48
49uint32_t MdssRot::getDstOffset() const {
50 return mRotData.dst_data.offset;
51}
52
53uint32_t MdssRot::getSessId() const { return mRotInfo.id; }
54
55void MdssRot::setSrcFB() {
56 mRotData.data.flags |= MDP_MEMORY_ID_TYPE_FB;
57}
Saurabh Shahe012f7a2012-08-18 15:11:57 -070058
59bool MdssRot::init() {
60 if(!utils::openDev(mFd, 0, Res::fbPath, O_RDWR)) {
61 ALOGE("MdssRot failed to init fb0");
62 return false;
63 }
64 return true;
65}
66
67void MdssRot::setSource(const overlay::utils::Whf& awhf) {
68 utils::Whf whf(awhf);
69
70 mRotInfo.src.format = whf.format;
71 if(whf.format == MDP_Y_CRCB_H2V2_TILE ||
72 whf.format == MDP_Y_CBCR_H2V2_TILE) {
73 whf.w = utils::alignup(awhf.w, 64);
74 whf.h = utils::alignup(awhf.h, 32);
75 }
76
77 mRotInfo.src.width = whf.w;
78 mRotInfo.src.height = whf.h;
79
80 mRotInfo.src_rect.w = whf.w;
81 mRotInfo.src_rect.h = whf.h;
82
83 mRotInfo.dst_rect.w = whf.w;
84 mRotInfo.dst_rect.h = whf.h;
85
86 mBufSize = awhf.size;
87}
88
89void MdssRot::setFlags(const utils::eMdpFlags& flags) {
90 // TODO
91}
92
93void MdssRot::setTransform(const utils::eTransform& rot, const bool& rotUsed)
94{
95 int flags = utils::getMdpOrient(rot);
96 if (flags != -1)
97 setRotations(flags);
98 //getMdpOrient will switch the flips if the source is 90 rotated.
99 //Clients in Android dont factor in 90 rotation while deciding the flip.
100 mOrientation = static_cast<utils::eTransform>(flags);
101 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, flags);
102
103 setDisable();
104 if(rotUsed) {
105 setEnable();
106 }
107}
108
109void MdssRot::doTransform() {
110 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
111 utils::swap(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h);
112}
113
114bool MdssRot::commit() {
115 doTransform();
116 mRotInfo.flags |= MDSS_MDP_ROT_ONLY;
117 if(!overlay::mdp_wrapper::setOverlay(mFd.getFD(), mRotInfo)) {
118 ALOGE("MdssRot commit failed!");
119 dump();
120 return false;
121 }
122 mRotData.id = mRotInfo.id;
123 return true;
124}
125
126bool MdssRot::queueBuffer(int fd, uint32_t offset) {
127 if(enabled()) {
128 mRotData.data.memory_id = fd;
129 mRotData.data.offset = offset;
130
131 remap(RotMem::Mem::ROT_NUM_BUFS);
132 OVASSERT(mMem.curr().m.numBufs(), "queueBuffer numbufs is 0");
133
134 mRotData.dst_data.offset =
135 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
136 mMem.curr().mCurrOffset =
137 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
138
139 if(!overlay::mdp_wrapper::play(mFd.getFD(), mRotData)) {
140 ALOGE("MdssRot play failed!");
141 dump();
142 return false;
143 }
144
145 // if the prev mem is valid, we need to close
146 if(mMem.prev().valid()) {
147 // FIXME if no wait for vsync the above
148 // play will return immediatly and might cause
149 // tearing when prev.close is called.
150 if(!mMem.prev().close()) {
151 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
152 return false;
153 }
154 }
155 }
156 return true;
157}
158
159bool MdssRot::open_i(uint32_t numbufs, uint32_t bufsz)
160{
161 OvMem mem;
162 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
163
164 if(!mem.open(numbufs, bufsz, false)){ // TODO: secure for badger
165 ALOGE("%s: Failed to open", __func__);
166 mem.close();
167 return false;
168 }
169
170 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
171 OVASSERT(mem.getFD() != -1, "getFd is -1");
172
173 mRotData.dst_data.memory_id = mem.getFD();
174 mRotData.dst_data.offset = 0;
175 mMem.curr().m = mem;
176 return true;
177}
178
179bool MdssRot::remap(uint32_t numbufs) {
180 // if current size changed, remap
181 if(mBufSize == mMem.curr().size()) {
182 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, mBufSize);
183 return true;
184 }
185
186 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
187 OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
188
189 // ++mMem will make curr to be prev, and prev will be curr
190 ++mMem;
191 if(!open_i(numbufs, mBufSize)) {
192 ALOGE("%s Error could not open", __FUNCTION__);
193 return false;
194 }
195 for (uint32_t i = 0; i < numbufs; ++i) {
196 mMem.curr().mRotOffset[i] = i * mBufSize;
197 }
198 return true;
199}
200
201bool MdssRot::close() {
202 bool success = true;
Ken Zhangba48b012012-12-11 20:33:59 -0500203 if(mFd.valid() && (getSessId() != (uint32_t) MSMFB_NEW_REQUEST)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700204 if(!mdp_wrapper::unsetOverlay(mFd.getFD(), getSessId())) {
205 ALOGE("MdssRot::close unsetOverlay failed, fd=%d sessId=%d",
206 mFd.getFD(), getSessId());
Ken Zhangba48b012012-12-11 20:33:59 -0500207 success = false;
208 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700209 }
210
211 if (!mFd.close()) {
212 ALOGE("Mdss Rot error closing fd");
213 success = false;
214 }
215 if (!mMem.close()) {
216 ALOGE("Mdss Rot error closing mem");
217 success = false;
218 }
219 reset();
220 return success;
221}
222
223void MdssRot::reset() {
224 ovutils::memset0(mRotInfo);
225 ovutils::memset0(mRotData);
226 mRotData.data.memory_id = -1;
227 mRotInfo.id = MSMFB_NEW_REQUEST;
228 ovutils::memset0(mMem.curr().mRotOffset);
229 ovutils::memset0(mMem.prev().mRotOffset);
230 mMem.curr().mCurrOffset = 0;
231 mMem.prev().mCurrOffset = 0;
232 mBufSize = 0;
233 mOrientation = utils::OVERLAY_TRANSFORM_0;
234}
235
236void MdssRot::dump() const {
237 ALOGE("== Dump MdssRot start ==");
238 mFd.dump();
239 mMem.curr().m.dump();
240 mdp_wrapper::dump("mRotInfo", mRotInfo);
241 mdp_wrapper::dump("mRotData", mRotData);
242 ALOGE("== Dump MdssRot end ==");
243}
244
245} // namespace overlay