blob: bb985d780e6a727d8071873161aa97590a2b42c9 [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
Arun Kumar K.R7e5a1f82014-01-22 10:36:16 -08003 * Copyright (c) 2010-2014, 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"
Saurabh Shah8ec9b5e2014-06-30 14:37:17 -070022#include "gr.h"
Saurabh Shahe012f7a2012-08-18 15:11:57 -070023
24namespace ovutils = overlay::utils;
25
26namespace overlay {
27
Naseer Ahmed758bfc52012-11-28 17:02:08 -050028MdpRot::MdpRot() {
29 reset();
30 init();
31}
32
33MdpRot::~MdpRot() { close(); }
34
Saurabh Shahacf10202013-02-26 10:15:15 -080035bool MdpRot::enabled() const { return mRotImgInfo.enable; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050036
Shalaj Jaina70b4352014-06-15 13:47:47 -070037void MdpRot::setRotations(uint32_t r) { mRotImgInfo.rotations = (uint8_t)r; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050038
Saurabh Shahacf10202013-02-26 10:15:15 -080039int MdpRot::getDstMemId() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050040 return mRotDataInfo.dst.memory_id;
41}
42
Saurabh Shahacf10202013-02-26 10:15:15 -080043uint32_t MdpRot::getDstOffset() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050044 return mRotDataInfo.dst.offset;
45}
46
Saurabh Shahacf10202013-02-26 10:15:15 -080047uint32_t MdpRot::getDstFormat() const {
Raj kamal23f69b22012-11-17 00:20:55 +053048 return mRotImgInfo.dst.format;
49}
Naseer Ahmed758bfc52012-11-28 17:02:08 -050050
Saurabh Shah8ec9b5e2014-06-30 14:37:17 -070051//Added for completeness. Not expected to be called.
52utils::Whf MdpRot::getDstWhf() const {
53 int alW = 0, alH = 0;
54 int halFormat = ovutils::getHALFormat(mRotImgInfo.dst.format);
55 getBufferSizeAndDimensions(mRotImgInfo.dst.width, mRotImgInfo.dst.height,
56 halFormat, alW, alH);
57 return utils::Whf(alW, alH, mRotImgInfo.dst.format);
58}
59
60//Added for completeness. Not expected to be called.
61utils::Dim MdpRot::getDstDimensions() const {
62 int alW = 0, alH = 0;
63 int halFormat = ovutils::getHALFormat(mRotImgInfo.dst.format);
64 getBufferSizeAndDimensions(mRotImgInfo.dst.width, mRotImgInfo.dst.height,
65 halFormat, alW, alH);
66 return utils::Dim(0, 0, alW, alH);
67}
68
Saurabh Shahacf10202013-02-26 10:15:15 -080069uint32_t MdpRot::getSessId() const { return mRotImgInfo.session_id; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050070
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080071void MdpRot::setDownscale(int ds) {
Ramkumar Radhakrishnan4ca8f3a2013-02-14 18:56:50 -080072 if ((utils::ROT_DS_EIGHTH == ds) && (mRotImgInfo.src_rect.h & 0xF)) {
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080073 // Ensure src_rect.h is a multiple of 16 for 1/8 downscaling.
74 // This is an undocumented MDP Rotator constraint.
Ramkumar Radhakrishnan4ca8f3a2013-02-14 18:56:50 -080075 mRotImgInfo.src_rect.h = utils::aligndown(mRotImgInfo.src_rect.h, 16);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080076 }
77 mRotImgInfo.downscale_ratio = ds;
78}
79
Saurabh Shahacf10202013-02-26 10:15:15 -080080void MdpRot::save() {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050081 mLSRotImgInfo = mRotImgInfo;
82}
83
Saurabh Shahacf10202013-02-26 10:15:15 -080084bool MdpRot::rotConfChanged() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050085 // 0 means same
86 if(0 == ::memcmp(&mRotImgInfo, &mLSRotImgInfo,
87 sizeof (msm_rotator_img_info))) {
88 return false;
89 }
90 return true;
91}
92
Saurabh Shahe012f7a2012-08-18 15:11:57 -070093bool MdpRot::init()
94{
95 if(!mFd.open(Res::rotPath, O_RDWR)){
96 ALOGE("MdpRot failed to init %s", Res::rotPath);
97 return false;
98 }
99 return true;
100}
101
102void MdpRot::setSource(const overlay::utils::Whf& awhf) {
103 utils::Whf whf(awhf);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700104 mRotImgInfo.src.format = whf.format;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700105
106 mRotImgInfo.src.width = whf.w;
107 mRotImgInfo.src.height = whf.h;
108
109 mRotImgInfo.src_rect.w = whf.w;
110 mRotImgInfo.src_rect.h = whf.h;
111
112 mRotImgInfo.dst.width = whf.w;
113 mRotImgInfo.dst.height = whf.h;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700114}
115
Arun Kumar K.R7e5a1f82014-01-22 10:36:16 -0800116void MdpRot::setCrop(const utils::Dim& /*crop*/) {
Sushil Chauhan80fc1f92013-04-23 17:30:05 -0700117 // NO-OP for non-mdss rotator due to possible h/w limitations
118}
119
Saurabh Shahacf10202013-02-26 10:15:15 -0800120void MdpRot::setFlags(const utils::eMdpFlags& flags) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700121 mRotImgInfo.secure = 0;
122 if(flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
123 mRotImgInfo.secure = 1;
124}
125
Saurabh Shahacf10202013-02-26 10:15:15 -0800126void MdpRot::setTransform(const utils::eTransform& rot)
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700127{
128 int r = utils::getMdpOrient(rot);
129 setRotations(r);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700130 mOrientation = static_cast<utils::eTransform>(r);
131 ALOGE_IF(DEBUG_OVERLAY, "%s: r=%d", __FUNCTION__, r);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800132}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700133
Saurabh Shahacf10202013-02-26 10:15:15 -0800134void MdpRot::doTransform() {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700135 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
136 utils::swap(mRotImgInfo.dst.width, mRotImgInfo.dst.height);
137}
138
139bool MdpRot::commit() {
140 doTransform();
Saurabh Shahc23b3802012-08-31 11:11:30 -0700141 if(rotConfChanged()) {
Saurabh Shahacf10202013-02-26 10:15:15 -0800142 mRotImgInfo.enable = 1;
Saurabh Shahc23b3802012-08-31 11:11:30 -0700143 if(!overlay::mdp_wrapper::startRotator(mFd.getFD(), mRotImgInfo)) {
144 ALOGE("MdpRot commit failed");
145 dump();
Saurabh Shahacf10202013-02-26 10:15:15 -0800146 mRotImgInfo.enable = 0;
Saurabh Shahc23b3802012-08-31 11:11:30 -0700147 return false;
148 }
149 save();
150 mRotDataInfo.session_id = mRotImgInfo.session_id;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700151 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700152 return true;
153}
154
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800155uint32_t MdpRot::calcOutputBufSize() {
156 ovutils::Whf destWhf(mRotImgInfo.dst.width,
157 mRotImgInfo.dst.height, mRotImgInfo.dst.format);
158 return Rotator::calcOutputBufSize(destWhf);
159}
160
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700161bool MdpRot::open_i(uint32_t numbufs, uint32_t bufsz)
162{
163 OvMem mem;
164
165 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
166
167 if(!mem.open(numbufs, bufsz, mRotImgInfo.secure)){
168 ALOGE("%s: Failed to open", __func__);
169 mem.close();
170 return false;
171 }
172
173 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
174 OVASSERT(mem.getFD() != -1, "getFd is -1");
175
176 mRotDataInfo.dst.memory_id = mem.getFD();
177 mRotDataInfo.dst.offset = 0;
Saurabh Shah912c9482014-02-07 14:01:04 -0800178 mMem.mem = mem;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700179 return true;
180}
181
182bool MdpRot::close() {
183 bool success = true;
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500184 if(mFd.valid() && (getSessId() != 0)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700185 if(!mdp_wrapper::endRotator(mFd.getFD(), getSessId())) {
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500186 ALOGE("Mdp Rot error endRotator, fd=%d sessId=%u",
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700187 mFd.getFD(), getSessId());
188 success = false;
189 }
190 }
191 if (!mFd.close()) {
192 ALOGE("Mdp Rot error closing fd");
193 success = false;
194 }
195 if (!mMem.close()) {
196 ALOGE("Mdp Rot error closing mem");
197 success = false;
198 }
199 reset();
200 return success;
201}
202
203bool MdpRot::remap(uint32_t numbufs) {
204 // if current size changed, remap
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800205 uint32_t opBufSize = calcOutputBufSize();
Saurabh Shah912c9482014-02-07 14:01:04 -0800206 if(opBufSize == mMem.size()) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800207 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, opBufSize);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700208 return true;
209 }
210
Saurabh Shah912c9482014-02-07 14:01:04 -0800211 if(!mMem.close()) {
212 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
213 return false;
214 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700215
Saurabh Shah912c9482014-02-07 14:01:04 -0800216 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
217
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800218 if(!open_i(numbufs, opBufSize)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700219 ALOGE("%s Error could not open", __FUNCTION__);
220 return false;
221 }
Saurabh Shah912c9482014-02-07 14:01:04 -0800222
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700223 for (uint32_t i = 0; i < numbufs; ++i) {
Saurabh Shah912c9482014-02-07 14:01:04 -0800224 mMem.mRotOffset[i] = i * opBufSize;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700225 }
Saurabh Shah912c9482014-02-07 14:01:04 -0800226
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700227 return true;
228}
229
230void MdpRot::reset() {
231 ovutils::memset0(mRotImgInfo);
Saurabh Shahc23b3802012-08-31 11:11:30 -0700232 ovutils::memset0(mLSRotImgInfo);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700233 ovutils::memset0(mRotDataInfo);
Saurabh Shah912c9482014-02-07 14:01:04 -0800234 ovutils::memset0(mMem.mRotOffset);
235 mMem.mCurrIndex = 0;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700236 mOrientation = utils::OVERLAY_TRANSFORM_0;
237}
238
239bool MdpRot::queueBuffer(int fd, uint32_t offset) {
240 if(enabled()) {
241 mRotDataInfo.src.memory_id = fd;
242 mRotDataInfo.src.offset = offset;
243
Saurabh Shah912c9482014-02-07 14:01:04 -0800244 if(false == remap(RotMem::ROT_NUM_BUFS)) {
245 ALOGE("%s Remap failed, not queueing", __FUNCTION__);
246 return false;
247 }
248
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700249 mRotDataInfo.dst.offset =
Saurabh Shah912c9482014-02-07 14:01:04 -0800250 mMem.mRotOffset[mMem.mCurrIndex];
251 mMem.mCurrIndex =
252 (mMem.mCurrIndex + 1) % mMem.mem.numBufs();
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700253
254 if(!overlay::mdp_wrapper::rotate(mFd.getFD(), mRotDataInfo)) {
255 ALOGE("MdpRot failed rotate");
256 dump();
257 return false;
258 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700259 }
260 return true;
261}
262
263void MdpRot::dump() const {
264 ALOGE("== Dump MdpRot start ==");
265 mFd.dump();
Saurabh Shah912c9482014-02-07 14:01:04 -0800266 mMem.mem.dump();
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700267 mdp_wrapper::dump("mRotImgInfo", mRotImgInfo);
268 mdp_wrapper::dump("mRotDataInfo", mRotDataInfo);
269 ALOGE("== Dump MdpRot end ==");
270}
271
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800272void MdpRot::getDump(char *buf, size_t len) const {
Saurabh Shahae61b2b2013-04-10 16:37:25 -0700273 ovutils::getDump(buf, len, "MdpRotCtrl", mRotImgInfo);
274 ovutils::getDump(buf, len, "MdpRotData", mRotDataInfo);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800275}
276
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700277} // namespace overlay