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