blob: 071e9f27e8b9f4e8c211191eadb4673a2a3b7f68 [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 Chauhanbbca6292013-01-07 18:07:16 -080098 mRotInfo.flags |= flags;
Saurabh Shahe012f7a2012-08-18 15:11:57 -070099}
100
101void MdssRot::setTransform(const utils::eTransform& rot, const bool& rotUsed)
102{
103 int flags = utils::getMdpOrient(rot);
104 if (flags != -1)
105 setRotations(flags);
106 //getMdpOrient will switch the flips if the source is 90 rotated.
107 //Clients in Android dont factor in 90 rotation while deciding the flip.
108 mOrientation = static_cast<utils::eTransform>(flags);
109 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, flags);
110
111 setDisable();
112 if(rotUsed) {
113 setEnable();
114 }
115}
116
117void MdssRot::doTransform() {
118 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
119 utils::swap(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h);
120}
121
122bool MdssRot::commit() {
123 doTransform();
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800124 setBufSize(mRotInfo.src.format);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700125 mRotInfo.flags |= MDSS_MDP_ROT_ONLY;
126 if(!overlay::mdp_wrapper::setOverlay(mFd.getFD(), mRotInfo)) {
127 ALOGE("MdssRot commit failed!");
128 dump();
129 return false;
130 }
131 mRotData.id = mRotInfo.id;
Saurabh Shah08c65b32012-12-18 14:23:43 -0800132 //reset flags to avoid stale orientation values
133 mRotInfo.flags = 0;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700134 return true;
135}
136
137bool MdssRot::queueBuffer(int fd, uint32_t offset) {
138 if(enabled()) {
139 mRotData.data.memory_id = fd;
140 mRotData.data.offset = offset;
141
142 remap(RotMem::Mem::ROT_NUM_BUFS);
143 OVASSERT(mMem.curr().m.numBufs(), "queueBuffer numbufs is 0");
144
145 mRotData.dst_data.offset =
146 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
147 mMem.curr().mCurrOffset =
148 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
149
150 if(!overlay::mdp_wrapper::play(mFd.getFD(), mRotData)) {
151 ALOGE("MdssRot play failed!");
152 dump();
153 return false;
154 }
155
156 // if the prev mem is valid, we need to close
157 if(mMem.prev().valid()) {
158 // FIXME if no wait for vsync the above
159 // play will return immediatly and might cause
160 // tearing when prev.close is called.
161 if(!mMem.prev().close()) {
162 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
163 return false;
164 }
165 }
166 }
167 return true;
168}
169
170bool MdssRot::open_i(uint32_t numbufs, uint32_t bufsz)
171{
172 OvMem mem;
173 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700174 bool isSecure = mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700175
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700176 if(!mem.open(numbufs, bufsz, isSecure)){
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700177 ALOGE("%s: Failed to open", __func__);
178 mem.close();
179 return false;
180 }
181
182 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
183 OVASSERT(mem.getFD() != -1, "getFd is -1");
184
185 mRotData.dst_data.memory_id = mem.getFD();
186 mRotData.dst_data.offset = 0;
187 mMem.curr().m = mem;
188 return true;
189}
190
191bool MdssRot::remap(uint32_t numbufs) {
192 // if current size changed, remap
193 if(mBufSize == mMem.curr().size()) {
194 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, mBufSize);
195 return true;
196 }
197
198 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
199 OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
200
201 // ++mMem will make curr to be prev, and prev will be curr
202 ++mMem;
203 if(!open_i(numbufs, mBufSize)) {
204 ALOGE("%s Error could not open", __FUNCTION__);
205 return false;
206 }
207 for (uint32_t i = 0; i < numbufs; ++i) {
208 mMem.curr().mRotOffset[i] = i * mBufSize;
209 }
210 return true;
211}
212
213bool MdssRot::close() {
214 bool success = true;
Ken Zhangba48b012012-12-11 20:33:59 -0500215 if(mFd.valid() && (getSessId() != (uint32_t) MSMFB_NEW_REQUEST)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700216 if(!mdp_wrapper::unsetOverlay(mFd.getFD(), getSessId())) {
217 ALOGE("MdssRot::close unsetOverlay failed, fd=%d sessId=%d",
218 mFd.getFD(), getSessId());
Ken Zhangba48b012012-12-11 20:33:59 -0500219 success = false;
220 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700221 }
222
223 if (!mFd.close()) {
224 ALOGE("Mdss Rot error closing fd");
225 success = false;
226 }
227 if (!mMem.close()) {
228 ALOGE("Mdss Rot error closing mem");
229 success = false;
230 }
231 reset();
232 return success;
233}
234
235void MdssRot::reset() {
236 ovutils::memset0(mRotInfo);
237 ovutils::memset0(mRotData);
238 mRotData.data.memory_id = -1;
239 mRotInfo.id = MSMFB_NEW_REQUEST;
240 ovutils::memset0(mMem.curr().mRotOffset);
241 ovutils::memset0(mMem.prev().mRotOffset);
242 mMem.curr().mCurrOffset = 0;
243 mMem.prev().mCurrOffset = 0;
244 mBufSize = 0;
245 mOrientation = utils::OVERLAY_TRANSFORM_0;
246}
247
248void MdssRot::dump() const {
249 ALOGE("== Dump MdssRot start ==");
250 mFd.dump();
251 mMem.curr().m.dump();
252 mdp_wrapper::dump("mRotInfo", mRotInfo);
253 mdp_wrapper::dump("mRotData", mRotData);
254 ALOGE("== Dump MdssRot end ==");
255}
256
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800257void MdssRot::setBufSize(int format) {
258 if (format == MDP_Y_CBCR_H2V2_VENUS) {
259 mBufSize = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, mRotInfo.dst_rect.w,
260 mRotInfo.dst_rect.h);
261 }
Sushil Chauhan6e3fab82012-11-19 15:30:33 -0800262 if (mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
263 mBufSize = utils::align(mBufSize, SIZE_1M);
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800264}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700265} // namespace overlay