blob: 70bf52bcbd50638c2a02619c7070d8dbfd3c8860 [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
Saurabh Shahacf10202013-02-26 10:15:15 -080046bool MdssRot::enabled() const { return mEnabled; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050047
Saurabh Shahacf10202013-02-26 10:15:15 -080048void MdssRot::setRotations(uint32_t flags) { mRotInfo.flags |= flags; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050049
Saurabh Shahacf10202013-02-26 10:15:15 -080050int MdssRot::getDstMemId() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050051 return mRotData.dst_data.memory_id;
52}
53
Saurabh Shahacf10202013-02-26 10:15:15 -080054uint32_t MdssRot::getDstOffset() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050055 return mRotData.dst_data.offset;
56}
57
Saurabh Shahacf10202013-02-26 10:15:15 -080058uint32_t MdssRot::getDstFormat() const {
Raj kamal23f69b22012-11-17 00:20:55 +053059 //For mdss src and dst formats are same
60 return mRotInfo.src.format;
61}
Naseer Ahmed758bfc52012-11-28 17:02:08 -050062
Saurabh Shahacf10202013-02-26 10:15:15 -080063uint32_t MdssRot::getSessId() const { return mRotInfo.id; }
Raj kamal23f69b22012-11-17 00:20:55 +053064
Saurabh Shahacf10202013-02-26 10:15:15 -080065bool MdssRot::init() {
Saurabh Shahe012f7a2012-08-18 15:11:57 -070066 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;
Saurabh Shahe012f7a2012-08-18 15:11:57 -070077
78 mRotInfo.src.width = whf.w;
79 mRotInfo.src.height = whf.h;
80
81 mRotInfo.src_rect.w = whf.w;
82 mRotInfo.src_rect.h = whf.h;
83
84 mRotInfo.dst_rect.w = whf.w;
85 mRotInfo.dst_rect.h = whf.h;
Saurabh Shahe012f7a2012-08-18 15:11:57 -070086}
87
Saurabh Shahacf10202013-02-26 10:15:15 -080088void MdssRot::setDownscale(int ds) {}
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080089
Saurabh Shahacf10202013-02-26 10:15:15 -080090void MdssRot::setFlags(const utils::eMdpFlags& flags) {
Sushil Chauhanbbca6292013-01-07 18:07:16 -080091 mRotInfo.flags |= flags;
Saurabh Shahe012f7a2012-08-18 15:11:57 -070092}
93
Saurabh Shahacf10202013-02-26 10:15:15 -080094void MdssRot::setTransform(const utils::eTransform& rot)
Saurabh Shahe012f7a2012-08-18 15:11:57 -070095{
96 int flags = utils::getMdpOrient(rot);
97 if (flags != -1)
98 setRotations(flags);
99 //getMdpOrient will switch the flips if the source is 90 rotated.
100 //Clients in Android dont factor in 90 rotation while deciding the flip.
101 mOrientation = static_cast<utils::eTransform>(flags);
102 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, flags);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800103}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700104
Saurabh Shahacf10202013-02-26 10:15:15 -0800105void MdssRot::doTransform() {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700106 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
107 utils::swap(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h);
108}
109
110bool MdssRot::commit() {
111 doTransform();
112 mRotInfo.flags |= MDSS_MDP_ROT_ONLY;
Saurabh Shahacf10202013-02-26 10:15:15 -0800113 mEnabled = true;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700114 if(!overlay::mdp_wrapper::setOverlay(mFd.getFD(), mRotInfo)) {
115 ALOGE("MdssRot commit failed!");
116 dump();
Saurabh Shahacf10202013-02-26 10:15:15 -0800117 return (mEnabled = false);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700118 }
119 mRotData.id = mRotInfo.id;
Sushil Chauhan95e4c9f2013-01-14 18:44:14 -0800120 // reset rotation flags to avoid stale orientation values
121 mRotInfo.flags &= ~MDSS_ROT_MASK;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700122 return true;
123}
124
125bool MdssRot::queueBuffer(int fd, uint32_t offset) {
126 if(enabled()) {
127 mRotData.data.memory_id = fd;
128 mRotData.data.offset = offset;
129
130 remap(RotMem::Mem::ROT_NUM_BUFS);
131 OVASSERT(mMem.curr().m.numBufs(), "queueBuffer numbufs is 0");
132
133 mRotData.dst_data.offset =
134 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
135 mMem.curr().mCurrOffset =
136 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
137
138 if(!overlay::mdp_wrapper::play(mFd.getFD(), mRotData)) {
139 ALOGE("MdssRot play failed!");
140 dump();
141 return false;
142 }
143
144 // if the prev mem is valid, we need to close
145 if(mMem.prev().valid()) {
146 // FIXME if no wait for vsync the above
147 // play will return immediatly and might cause
148 // tearing when prev.close is called.
149 if(!mMem.prev().close()) {
150 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
151 return false;
152 }
153 }
154 }
155 return true;
156}
157
158bool MdssRot::open_i(uint32_t numbufs, uint32_t bufsz)
159{
160 OvMem mem;
161 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700162 bool isSecure = mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700163
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700164 if(!mem.open(numbufs, bufsz, isSecure)){
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700165 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) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800180 // Calculate the size based on rotator's dst format, w and h.
181 uint32_t opBufSize = calcOutputBufSize();
182 // If current size changed, remap
183 if(opBufSize == mMem.curr().size()) {
184 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, opBufSize);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700185 return true;
186 }
187
188 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
189 OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
190
191 // ++mMem will make curr to be prev, and prev will be curr
192 ++mMem;
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800193 if(!open_i(numbufs, opBufSize)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700194 ALOGE("%s Error could not open", __FUNCTION__);
195 return false;
196 }
197 for (uint32_t i = 0; i < numbufs; ++i) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800198 mMem.curr().mRotOffset[i] = i * opBufSize;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700199 }
200 return true;
201}
202
203bool MdssRot::close() {
204 bool success = true;
Ken Zhangba48b012012-12-11 20:33:59 -0500205 if(mFd.valid() && (getSessId() != (uint32_t) MSMFB_NEW_REQUEST)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700206 if(!mdp_wrapper::unsetOverlay(mFd.getFD(), getSessId())) {
207 ALOGE("MdssRot::close unsetOverlay failed, fd=%d sessId=%d",
208 mFd.getFD(), getSessId());
Ken Zhangba48b012012-12-11 20:33:59 -0500209 success = false;
210 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700211 }
212
213 if (!mFd.close()) {
214 ALOGE("Mdss Rot error closing fd");
215 success = false;
216 }
217 if (!mMem.close()) {
218 ALOGE("Mdss Rot error closing mem");
219 success = false;
220 }
221 reset();
222 return success;
223}
224
225void MdssRot::reset() {
226 ovutils::memset0(mRotInfo);
227 ovutils::memset0(mRotData);
228 mRotData.data.memory_id = -1;
229 mRotInfo.id = MSMFB_NEW_REQUEST;
230 ovutils::memset0(mMem.curr().mRotOffset);
231 ovutils::memset0(mMem.prev().mRotOffset);
232 mMem.curr().mCurrOffset = 0;
233 mMem.prev().mCurrOffset = 0;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700234 mOrientation = utils::OVERLAY_TRANSFORM_0;
235}
236
237void MdssRot::dump() const {
238 ALOGE("== Dump MdssRot start ==");
239 mFd.dump();
240 mMem.curr().m.dump();
241 mdp_wrapper::dump("mRotInfo", mRotInfo);
242 mdp_wrapper::dump("mRotData", mRotData);
243 ALOGE("== Dump MdssRot end ==");
244}
245
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800246uint32_t MdssRot::calcOutputBufSize() {
247 uint32_t opBufSize = 0;
248 ovutils::Whf destWhf(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h,
249 mRotInfo.src.format); //mdss src and dst formats are same.
Sushil Chauhanb4d184f2013-02-11 16:13:10 -0800250
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800251 opBufSize = Rotator::calcOutputBufSize(destWhf);
Sushil Chauhanb4d184f2013-02-11 16:13:10 -0800252
Sushil Chauhan6e3fab82012-11-19 15:30:33 -0800253 if (mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800254 opBufSize = utils::align(opBufSize, SIZE_1M);
255
256 return opBufSize;
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800257}
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800258
259void MdssRot::getDump(char *buf, size_t len) const {
260 ovutils::getDump(buf, len, "MdssRotCtrl(mdp_overlay)", mRotInfo);
261 ovutils::getDump(buf, len, "MdssRotData(msmfb_overlay_data)", mRotData);
262}
263
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700264} // namespace overlay