blob: 58790fc14f2f3a9c1ea4fe233a16bad4d56cefb3 [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{
Saurabh Shah78ad4952013-04-05 09:38:01 -070096 // reset rotation flags to avoid stale orientation values
97 mRotInfo.flags &= ~MDSS_ROT_MASK;
Saurabh Shahe012f7a2012-08-18 15:11:57 -070098 int flags = utils::getMdpOrient(rot);
99 if (flags != -1)
100 setRotations(flags);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700101 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;
120 return true;
121}
122
123bool MdssRot::queueBuffer(int fd, uint32_t offset) {
124 if(enabled()) {
125 mRotData.data.memory_id = fd;
126 mRotData.data.offset = offset;
127
128 remap(RotMem::Mem::ROT_NUM_BUFS);
129 OVASSERT(mMem.curr().m.numBufs(), "queueBuffer numbufs is 0");
130
131 mRotData.dst_data.offset =
132 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
133 mMem.curr().mCurrOffset =
134 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
135
136 if(!overlay::mdp_wrapper::play(mFd.getFD(), mRotData)) {
137 ALOGE("MdssRot play failed!");
138 dump();
139 return false;
140 }
141
142 // if the prev mem is valid, we need to close
143 if(mMem.prev().valid()) {
144 // FIXME if no wait for vsync the above
145 // play will return immediatly and might cause
146 // tearing when prev.close is called.
147 if(!mMem.prev().close()) {
148 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
149 return false;
150 }
151 }
152 }
153 return true;
154}
155
156bool MdssRot::open_i(uint32_t numbufs, uint32_t bufsz)
157{
158 OvMem mem;
159 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700160 bool isSecure = mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700161
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700162 if(!mem.open(numbufs, bufsz, isSecure)){
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700163 ALOGE("%s: Failed to open", __func__);
164 mem.close();
165 return false;
166 }
167
168 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
169 OVASSERT(mem.getFD() != -1, "getFd is -1");
170
171 mRotData.dst_data.memory_id = mem.getFD();
172 mRotData.dst_data.offset = 0;
173 mMem.curr().m = mem;
174 return true;
175}
176
177bool MdssRot::remap(uint32_t numbufs) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800178 // Calculate the size based on rotator's dst format, w and h.
179 uint32_t opBufSize = calcOutputBufSize();
180 // If current size changed, remap
181 if(opBufSize == mMem.curr().size()) {
182 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, opBufSize);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700183 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;
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800191 if(!open_i(numbufs, opBufSize)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700192 ALOGE("%s Error could not open", __FUNCTION__);
193 return false;
194 }
195 for (uint32_t i = 0; i < numbufs; ++i) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800196 mMem.curr().mRotOffset[i] = i * opBufSize;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700197 }
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;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700232 mOrientation = utils::OVERLAY_TRANSFORM_0;
233}
234
235void MdssRot::dump() const {
236 ALOGE("== Dump MdssRot start ==");
237 mFd.dump();
238 mMem.curr().m.dump();
239 mdp_wrapper::dump("mRotInfo", mRotInfo);
240 mdp_wrapper::dump("mRotData", mRotData);
241 ALOGE("== Dump MdssRot end ==");
242}
243
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800244uint32_t MdssRot::calcOutputBufSize() {
245 uint32_t opBufSize = 0;
246 ovutils::Whf destWhf(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h,
247 mRotInfo.src.format); //mdss src and dst formats are same.
Sushil Chauhanb4d184f2013-02-11 16:13:10 -0800248
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800249 opBufSize = Rotator::calcOutputBufSize(destWhf);
Sushil Chauhanb4d184f2013-02-11 16:13:10 -0800250
Sushil Chauhan6e3fab82012-11-19 15:30:33 -0800251 if (mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800252 opBufSize = utils::align(opBufSize, SIZE_1M);
253
254 return opBufSize;
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800255}
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800256
257void MdssRot::getDump(char *buf, size_t len) const {
Saurabh Shahae61b2b2013-04-10 16:37:25 -0700258 ovutils::getDump(buf, len, "MdssRotCtrl", mRotInfo);
259 ovutils::getDump(buf, len, "MdssRotData", mRotData);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800260}
261
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700262} // namespace overlay