blob: 5052bc84a6e47beca6e9868f50062f1b255ddc6e [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
23namespace ovutils = overlay::utils;
24
25namespace overlay {
26
Naseer Ahmed758bfc52012-11-28 17:02:08 -050027MdpRot::MdpRot() {
28 reset();
29 init();
30}
31
32MdpRot::~MdpRot() { close(); }
33
34void MdpRot::setEnable() { mRotImgInfo.enable = 1; }
35
36void MdpRot::setDisable() { mRotImgInfo.enable = 0; }
37
38bool MdpRot::enabled() const { return mRotImgInfo.enable; }
39
40void MdpRot::setRotations(uint32_t r) { mRotImgInfo.rotations = r; }
41
42int MdpRot::getDstMemId() const {
43 return mRotDataInfo.dst.memory_id;
44}
45
46uint32_t MdpRot::getDstOffset() const {
47 return mRotDataInfo.dst.offset;
48}
49
50uint32_t MdpRot::getSessId() const { return mRotImgInfo.session_id; }
51
52void MdpRot::setSrcFB() {
53 mRotDataInfo.src.flags |= MDP_MEMORY_ID_TYPE_FB;
54}
55
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080056void MdpRot::setDownscale(int ds) {
57 if (mRotImgInfo.src.format == MDP_Y_CR_CB_GH2V2 &&
58 (mRotImgInfo.src_rect.h & 0xF)) {
59 mRotImgInfo.src_rect.h = utils::aligndown(mRotImgInfo.src_rect.h, 16);
60 } else if ((utils::ROT_DS_EIGHTH == ds) && (mRotImgInfo.src_rect.h & 0xF)) {
61 // Ensure src_rect.h is a multiple of 16 for 1/8 downscaling.
62 // This is an undocumented MDP Rotator constraint.
63 // Note that src_rect.h is already ensured to be 32 pixel height aligned
64 // for MDP_Y_CRCB_H2V2_TILE and MDP_Y_CBCR_H2V2_TILE formats.
65 mRotImgInfo.src_rect.h = utils::alignup(mRotImgInfo.src_rect.h, 16);
66 }
67 mRotImgInfo.downscale_ratio = ds;
68}
69
Naseer Ahmed758bfc52012-11-28 17:02:08 -050070void MdpRot::save() {
71 mLSRotImgInfo = mRotImgInfo;
72}
73
74bool MdpRot::rotConfChanged() const {
75 // 0 means same
76 if(0 == ::memcmp(&mRotImgInfo, &mLSRotImgInfo,
77 sizeof (msm_rotator_img_info))) {
78 return false;
79 }
80 return true;
81}
82
Saurabh Shahe012f7a2012-08-18 15:11:57 -070083bool MdpRot::init()
84{
85 if(!mFd.open(Res::rotPath, O_RDWR)){
86 ALOGE("MdpRot failed to init %s", Res::rotPath);
87 return false;
88 }
89 return true;
90}
91
92void MdpRot::setSource(const overlay::utils::Whf& awhf) {
93 utils::Whf whf(awhf);
94
95 mRotImgInfo.src.format = whf.format;
96 if(whf.format == MDP_Y_CRCB_H2V2_TILE ||
97 whf.format == MDP_Y_CBCR_H2V2_TILE) {
98 whf.w = utils::alignup(awhf.w, 64);
99 whf.h = utils::alignup(awhf.h, 32);
100 }
101
102 mRotImgInfo.src.width = whf.w;
103 mRotImgInfo.src.height = whf.h;
104
105 mRotImgInfo.src_rect.w = whf.w;
106 mRotImgInfo.src_rect.h = whf.h;
107
108 mRotImgInfo.dst.width = whf.w;
109 mRotImgInfo.dst.height = whf.h;
110
111 mBufSize = awhf.size;
112}
113
114void MdpRot::setFlags(const utils::eMdpFlags& flags) {
115 mRotImgInfo.secure = 0;
116 if(flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
117 mRotImgInfo.secure = 1;
118}
119
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800120void MdpRot::setTransform(const utils::eTransform& rot)
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700121{
122 int r = utils::getMdpOrient(rot);
123 setRotations(r);
124 //getMdpOrient will switch the flips if the source is 90 rotated.
125 //Clients in Android dont factor in 90 rotation while deciding the flip.
126 mOrientation = static_cast<utils::eTransform>(r);
127 ALOGE_IF(DEBUG_OVERLAY, "%s: r=%d", __FUNCTION__, r);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800128}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700129
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800130void MdpRot::setRotatorUsed(const bool& rotUsed) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700131 setDisable();
132 if(rotUsed) {
133 setEnable();
134 }
135}
136
137void MdpRot::doTransform() {
138 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
139 utils::swap(mRotImgInfo.dst.width, mRotImgInfo.dst.height);
140}
141
142bool MdpRot::commit() {
143 doTransform();
Saurabh Shahc23b3802012-08-31 11:11:30 -0700144 if(rotConfChanged()) {
145 if(!overlay::mdp_wrapper::startRotator(mFd.getFD(), mRotImgInfo)) {
146 ALOGE("MdpRot commit failed");
147 dump();
148 return false;
149 }
150 save();
151 mRotDataInfo.session_id = mRotImgInfo.session_id;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700152 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700153 return true;
154}
155
156bool MdpRot::open_i(uint32_t numbufs, uint32_t bufsz)
157{
158 OvMem mem;
159
160 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
161
162 if(!mem.open(numbufs, bufsz, mRotImgInfo.secure)){
163 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 mRotDataInfo.dst.memory_id = mem.getFD();
172 mRotDataInfo.dst.offset = 0;
173 mMem.curr().m = mem;
174 return true;
175}
176
177bool MdpRot::close() {
178 bool success = true;
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500179 if(mFd.valid() && (getSessId() != 0)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700180 if(!mdp_wrapper::endRotator(mFd.getFD(), getSessId())) {
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500181 ALOGE("Mdp Rot error endRotator, fd=%d sessId=%u",
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700182 mFd.getFD(), getSessId());
183 success = false;
184 }
185 }
186 if (!mFd.close()) {
187 ALOGE("Mdp Rot error closing fd");
188 success = false;
189 }
190 if (!mMem.close()) {
191 ALOGE("Mdp Rot error closing mem");
192 success = false;
193 }
194 reset();
195 return success;
196}
197
198bool MdpRot::remap(uint32_t numbufs) {
199 // if current size changed, remap
200 if(mBufSize == mMem.curr().size()) {
201 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, mBufSize);
202 return true;
203 }
204
205 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
206 OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
207
208 // ++mMem will make curr to be prev, and prev will be curr
209 ++mMem;
210 if(!open_i(numbufs, mBufSize)) {
211 ALOGE("%s Error could not open", __FUNCTION__);
212 return false;
213 }
214 for (uint32_t i = 0; i < numbufs; ++i) {
215 mMem.curr().mRotOffset[i] = i * mBufSize;
216 }
217 return true;
218}
219
220void MdpRot::reset() {
221 ovutils::memset0(mRotImgInfo);
Saurabh Shahc23b3802012-08-31 11:11:30 -0700222 ovutils::memset0(mLSRotImgInfo);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700223 ovutils::memset0(mRotDataInfo);
224 ovutils::memset0(mMem.curr().mRotOffset);
225 ovutils::memset0(mMem.prev().mRotOffset);
226 mMem.curr().mCurrOffset = 0;
227 mMem.prev().mCurrOffset = 0;
228 mBufSize = 0;
229 mOrientation = utils::OVERLAY_TRANSFORM_0;
230}
231
232bool MdpRot::queueBuffer(int fd, uint32_t offset) {
233 if(enabled()) {
234 mRotDataInfo.src.memory_id = fd;
235 mRotDataInfo.src.offset = offset;
236
237 remap(RotMem::Mem::ROT_NUM_BUFS);
238 OVASSERT(mMem.curr().m.numBufs(),
239 "queueBuffer numbufs is 0");
240 mRotDataInfo.dst.offset =
241 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
242 mMem.curr().mCurrOffset =
243 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
244
245 if(!overlay::mdp_wrapper::rotate(mFd.getFD(), mRotDataInfo)) {
246 ALOGE("MdpRot failed rotate");
247 dump();
248 return false;
249 }
250
251 // if the prev mem is valid, we need to close
252 if(mMem.prev().valid()) {
253 // FIXME if no wait for vsync the above
254 // play will return immediatly and might cause
255 // tearing when prev.close is called.
256 if(!mMem.prev().close()) {
257 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
258 return false;
259 }
260 }
261 }
262 return true;
263}
264
265void MdpRot::dump() const {
266 ALOGE("== Dump MdpRot start ==");
267 mFd.dump();
268 mMem.curr().m.dump();
269 mdp_wrapper::dump("mRotImgInfo", mRotImgInfo);
270 mdp_wrapper::dump("mRotDataInfo", mRotDataInfo);
271 ALOGE("== Dump MdpRot end ==");
272}
273
274} // namespace overlay