Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 1 | /* |
Naseer Ahmed | 758bfc5 | 2012-11-28 17:02:08 -0500 | [diff] [blame] | 2 | * Copyright (C) 2008 The Android Open Source Project |
Raj kamal | 23f69b2 | 2012-11-17 00:20:55 +0530 | [diff] [blame] | 3 | * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved. |
Naseer Ahmed | 758bfc5 | 2012-11-28 17:02:08 -0500 | [diff] [blame] | 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 Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "overlayUtils.h" |
| 21 | #include "overlayRotator.h" |
| 22 | |
| 23 | namespace ovutils = overlay::utils; |
| 24 | |
| 25 | namespace overlay { |
| 26 | |
Naseer Ahmed | 758bfc5 | 2012-11-28 17:02:08 -0500 | [diff] [blame] | 27 | MdpRot::MdpRot() { |
| 28 | reset(); |
| 29 | init(); |
| 30 | } |
| 31 | |
| 32 | MdpRot::~MdpRot() { close(); } |
| 33 | |
Saurabh Shah | acf1020 | 2013-02-26 10:15:15 -0800 | [diff] [blame] | 34 | bool MdpRot::enabled() const { return mRotImgInfo.enable; } |
Naseer Ahmed | 758bfc5 | 2012-11-28 17:02:08 -0500 | [diff] [blame] | 35 | |
Saurabh Shah | acf1020 | 2013-02-26 10:15:15 -0800 | [diff] [blame] | 36 | void MdpRot::setRotations(uint32_t r) { mRotImgInfo.rotations = r; } |
Naseer Ahmed | 758bfc5 | 2012-11-28 17:02:08 -0500 | [diff] [blame] | 37 | |
Saurabh Shah | acf1020 | 2013-02-26 10:15:15 -0800 | [diff] [blame] | 38 | int MdpRot::getDstMemId() const { |
Naseer Ahmed | 758bfc5 | 2012-11-28 17:02:08 -0500 | [diff] [blame] | 39 | return mRotDataInfo.dst.memory_id; |
| 40 | } |
| 41 | |
Saurabh Shah | acf1020 | 2013-02-26 10:15:15 -0800 | [diff] [blame] | 42 | uint32_t MdpRot::getDstOffset() const { |
Naseer Ahmed | 758bfc5 | 2012-11-28 17:02:08 -0500 | [diff] [blame] | 43 | return mRotDataInfo.dst.offset; |
| 44 | } |
| 45 | |
Saurabh Shah | acf1020 | 2013-02-26 10:15:15 -0800 | [diff] [blame] | 46 | uint32_t MdpRot::getDstFormat() const { |
Raj kamal | 23f69b2 | 2012-11-17 00:20:55 +0530 | [diff] [blame] | 47 | return mRotImgInfo.dst.format; |
| 48 | } |
Naseer Ahmed | 758bfc5 | 2012-11-28 17:02:08 -0500 | [diff] [blame] | 49 | |
Saurabh Shah | acf1020 | 2013-02-26 10:15:15 -0800 | [diff] [blame] | 50 | uint32_t MdpRot::getSessId() const { return mRotImgInfo.session_id; } |
Naseer Ahmed | 758bfc5 | 2012-11-28 17:02:08 -0500 | [diff] [blame] | 51 | |
Ramkumar Radhakrishnan | 288f8c7 | 2013-01-15 11:37:54 -0800 | [diff] [blame] | 52 | void MdpRot::setDownscale(int ds) { |
Ramkumar Radhakrishnan | 4ca8f3a | 2013-02-14 18:56:50 -0800 | [diff] [blame] | 53 | if ((utils::ROT_DS_EIGHTH == ds) && (mRotImgInfo.src_rect.h & 0xF)) { |
Ramkumar Radhakrishnan | 288f8c7 | 2013-01-15 11:37:54 -0800 | [diff] [blame] | 54 | // Ensure src_rect.h is a multiple of 16 for 1/8 downscaling. |
| 55 | // This is an undocumented MDP Rotator constraint. |
Ramkumar Radhakrishnan | 4ca8f3a | 2013-02-14 18:56:50 -0800 | [diff] [blame] | 56 | mRotImgInfo.src_rect.h = utils::aligndown(mRotImgInfo.src_rect.h, 16); |
Ramkumar Radhakrishnan | 288f8c7 | 2013-01-15 11:37:54 -0800 | [diff] [blame] | 57 | } |
| 58 | mRotImgInfo.downscale_ratio = ds; |
| 59 | } |
| 60 | |
Saurabh Shah | acf1020 | 2013-02-26 10:15:15 -0800 | [diff] [blame] | 61 | void MdpRot::save() { |
Naseer Ahmed | 758bfc5 | 2012-11-28 17:02:08 -0500 | [diff] [blame] | 62 | mLSRotImgInfo = mRotImgInfo; |
| 63 | } |
| 64 | |
Saurabh Shah | acf1020 | 2013-02-26 10:15:15 -0800 | [diff] [blame] | 65 | bool MdpRot::rotConfChanged() const { |
Naseer Ahmed | 758bfc5 | 2012-11-28 17:02:08 -0500 | [diff] [blame] | 66 | // 0 means same |
| 67 | if(0 == ::memcmp(&mRotImgInfo, &mLSRotImgInfo, |
| 68 | sizeof (msm_rotator_img_info))) { |
| 69 | return false; |
| 70 | } |
| 71 | return true; |
| 72 | } |
| 73 | |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 74 | bool MdpRot::init() |
| 75 | { |
| 76 | if(!mFd.open(Res::rotPath, O_RDWR)){ |
| 77 | ALOGE("MdpRot failed to init %s", Res::rotPath); |
| 78 | return false; |
| 79 | } |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | void MdpRot::setSource(const overlay::utils::Whf& awhf) { |
| 84 | utils::Whf whf(awhf); |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 85 | mRotImgInfo.src.format = whf.format; |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 86 | |
| 87 | mRotImgInfo.src.width = whf.w; |
| 88 | mRotImgInfo.src.height = whf.h; |
| 89 | |
| 90 | mRotImgInfo.src_rect.w = whf.w; |
| 91 | mRotImgInfo.src_rect.h = whf.h; |
| 92 | |
| 93 | mRotImgInfo.dst.width = whf.w; |
| 94 | mRotImgInfo.dst.height = whf.h; |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Sushil Chauhan | 80fc1f9 | 2013-04-23 17:30:05 -0700 | [diff] [blame] | 97 | void MdpRot::setCrop(const utils::Dim& crop) { |
| 98 | // NO-OP for non-mdss rotator due to possible h/w limitations |
| 99 | } |
| 100 | |
Saurabh Shah | acf1020 | 2013-02-26 10:15:15 -0800 | [diff] [blame] | 101 | void MdpRot::setFlags(const utils::eMdpFlags& flags) { |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 102 | mRotImgInfo.secure = 0; |
| 103 | if(flags & utils::OV_MDP_SECURE_OVERLAY_SESSION) |
| 104 | mRotImgInfo.secure = 1; |
| 105 | } |
| 106 | |
Saurabh Shah | acf1020 | 2013-02-26 10:15:15 -0800 | [diff] [blame] | 107 | void MdpRot::setTransform(const utils::eTransform& rot) |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 108 | { |
| 109 | int r = utils::getMdpOrient(rot); |
| 110 | setRotations(r); |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 111 | mOrientation = static_cast<utils::eTransform>(r); |
| 112 | ALOGE_IF(DEBUG_OVERLAY, "%s: r=%d", __FUNCTION__, r); |
Ramkumar Radhakrishnan | 288f8c7 | 2013-01-15 11:37:54 -0800 | [diff] [blame] | 113 | } |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 114 | |
Saurabh Shah | acf1020 | 2013-02-26 10:15:15 -0800 | [diff] [blame] | 115 | void MdpRot::doTransform() { |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 116 | if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90) |
| 117 | utils::swap(mRotImgInfo.dst.width, mRotImgInfo.dst.height); |
| 118 | } |
| 119 | |
| 120 | bool MdpRot::commit() { |
| 121 | doTransform(); |
Saurabh Shah | c23b380 | 2012-08-31 11:11:30 -0700 | [diff] [blame] | 122 | if(rotConfChanged()) { |
Saurabh Shah | acf1020 | 2013-02-26 10:15:15 -0800 | [diff] [blame] | 123 | mRotImgInfo.enable = 1; |
Saurabh Shah | c23b380 | 2012-08-31 11:11:30 -0700 | [diff] [blame] | 124 | if(!overlay::mdp_wrapper::startRotator(mFd.getFD(), mRotImgInfo)) { |
| 125 | ALOGE("MdpRot commit failed"); |
| 126 | dump(); |
Saurabh Shah | acf1020 | 2013-02-26 10:15:15 -0800 | [diff] [blame] | 127 | mRotImgInfo.enable = 0; |
Saurabh Shah | c23b380 | 2012-08-31 11:11:30 -0700 | [diff] [blame] | 128 | return false; |
| 129 | } |
| 130 | save(); |
| 131 | mRotDataInfo.session_id = mRotImgInfo.session_id; |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 132 | } |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | |
Saurabh Shah | fc3652f | 2013-02-15 13:15:45 -0800 | [diff] [blame] | 136 | uint32_t MdpRot::calcOutputBufSize() { |
| 137 | ovutils::Whf destWhf(mRotImgInfo.dst.width, |
| 138 | mRotImgInfo.dst.height, mRotImgInfo.dst.format); |
| 139 | return Rotator::calcOutputBufSize(destWhf); |
| 140 | } |
| 141 | |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 142 | bool MdpRot::open_i(uint32_t numbufs, uint32_t bufsz) |
| 143 | { |
| 144 | OvMem mem; |
| 145 | |
| 146 | OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i"); |
| 147 | |
| 148 | if(!mem.open(numbufs, bufsz, mRotImgInfo.secure)){ |
| 149 | ALOGE("%s: Failed to open", __func__); |
| 150 | mem.close(); |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | OVASSERT(MAP_FAILED != mem.addr(), "MAP failed"); |
| 155 | OVASSERT(mem.getFD() != -1, "getFd is -1"); |
| 156 | |
| 157 | mRotDataInfo.dst.memory_id = mem.getFD(); |
| 158 | mRotDataInfo.dst.offset = 0; |
Saurabh Shah | 9acd8f7 | 2014-02-07 14:01:04 -0800 | [diff] [blame] | 159 | mMem.mem = mem; |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 160 | return true; |
| 161 | } |
| 162 | |
| 163 | bool MdpRot::close() { |
| 164 | bool success = true; |
Naseer Ahmed | 758bfc5 | 2012-11-28 17:02:08 -0500 | [diff] [blame] | 165 | if(mFd.valid() && (getSessId() != 0)) { |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 166 | if(!mdp_wrapper::endRotator(mFd.getFD(), getSessId())) { |
Naseer Ahmed | 758bfc5 | 2012-11-28 17:02:08 -0500 | [diff] [blame] | 167 | ALOGE("Mdp Rot error endRotator, fd=%d sessId=%u", |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 168 | mFd.getFD(), getSessId()); |
| 169 | success = false; |
| 170 | } |
| 171 | } |
| 172 | if (!mFd.close()) { |
| 173 | ALOGE("Mdp Rot error closing fd"); |
| 174 | success = false; |
| 175 | } |
| 176 | if (!mMem.close()) { |
| 177 | ALOGE("Mdp Rot error closing mem"); |
| 178 | success = false; |
| 179 | } |
| 180 | reset(); |
| 181 | return success; |
| 182 | } |
| 183 | |
| 184 | bool MdpRot::remap(uint32_t numbufs) { |
| 185 | // if current size changed, remap |
Saurabh Shah | fc3652f | 2013-02-15 13:15:45 -0800 | [diff] [blame] | 186 | uint32_t opBufSize = calcOutputBufSize(); |
Saurabh Shah | 9acd8f7 | 2014-02-07 14:01:04 -0800 | [diff] [blame] | 187 | if(opBufSize == mMem.size()) { |
Saurabh Shah | fc3652f | 2013-02-15 13:15:45 -0800 | [diff] [blame] | 188 | ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, opBufSize); |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 189 | return true; |
| 190 | } |
| 191 | |
Saurabh Shah | 9acd8f7 | 2014-02-07 14:01:04 -0800 | [diff] [blame] | 192 | if(!mMem.close()) { |
| 193 | ALOGE("%s error in closing prev rot mem", __FUNCTION__); |
| 194 | return false; |
| 195 | } |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 196 | |
Saurabh Shah | 9acd8f7 | 2014-02-07 14:01:04 -0800 | [diff] [blame] | 197 | ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__); |
| 198 | |
Saurabh Shah | fc3652f | 2013-02-15 13:15:45 -0800 | [diff] [blame] | 199 | if(!open_i(numbufs, opBufSize)) { |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 200 | ALOGE("%s Error could not open", __FUNCTION__); |
| 201 | return false; |
| 202 | } |
Saurabh Shah | 9acd8f7 | 2014-02-07 14:01:04 -0800 | [diff] [blame] | 203 | |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 204 | for (uint32_t i = 0; i < numbufs; ++i) { |
Saurabh Shah | 9acd8f7 | 2014-02-07 14:01:04 -0800 | [diff] [blame] | 205 | mMem.mRotOffset[i] = i * opBufSize; |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 206 | } |
Saurabh Shah | 9acd8f7 | 2014-02-07 14:01:04 -0800 | [diff] [blame] | 207 | |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 208 | return true; |
| 209 | } |
| 210 | |
| 211 | void MdpRot::reset() { |
| 212 | ovutils::memset0(mRotImgInfo); |
Saurabh Shah | c23b380 | 2012-08-31 11:11:30 -0700 | [diff] [blame] | 213 | ovutils::memset0(mLSRotImgInfo); |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 214 | ovutils::memset0(mRotDataInfo); |
Saurabh Shah | 9acd8f7 | 2014-02-07 14:01:04 -0800 | [diff] [blame] | 215 | ovutils::memset0(mMem.mRotOffset); |
| 216 | mMem.mCurrIndex = 0; |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 217 | mOrientation = utils::OVERLAY_TRANSFORM_0; |
| 218 | } |
| 219 | |
| 220 | bool MdpRot::queueBuffer(int fd, uint32_t offset) { |
| 221 | if(enabled()) { |
| 222 | mRotDataInfo.src.memory_id = fd; |
| 223 | mRotDataInfo.src.offset = offset; |
| 224 | |
Saurabh Shah | 9acd8f7 | 2014-02-07 14:01:04 -0800 | [diff] [blame] | 225 | if(false == remap(RotMem::ROT_NUM_BUFS)) { |
| 226 | ALOGE("%s Remap failed, not queueing", __FUNCTION__); |
| 227 | return false; |
| 228 | } |
| 229 | |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 230 | mRotDataInfo.dst.offset = |
Saurabh Shah | 9acd8f7 | 2014-02-07 14:01:04 -0800 | [diff] [blame] | 231 | mMem.mRotOffset[mMem.mCurrIndex]; |
| 232 | mMem.mCurrIndex = |
| 233 | (mMem.mCurrIndex + 1) % mMem.mem.numBufs(); |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 234 | |
| 235 | if(!overlay::mdp_wrapper::rotate(mFd.getFD(), mRotDataInfo)) { |
| 236 | ALOGE("MdpRot failed rotate"); |
| 237 | dump(); |
| 238 | return false; |
| 239 | } |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 240 | } |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | void MdpRot::dump() const { |
| 245 | ALOGE("== Dump MdpRot start =="); |
| 246 | mFd.dump(); |
Saurabh Shah | 9acd8f7 | 2014-02-07 14:01:04 -0800 | [diff] [blame] | 247 | mMem.mem.dump(); |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 248 | mdp_wrapper::dump("mRotImgInfo", mRotImgInfo); |
| 249 | mdp_wrapper::dump("mRotDataInfo", mRotDataInfo); |
| 250 | ALOGE("== Dump MdpRot end =="); |
| 251 | } |
| 252 | |
Saurabh Shah | 0d0a7cb | 2013-02-12 17:58:19 -0800 | [diff] [blame] | 253 | void MdpRot::getDump(char *buf, size_t len) const { |
Saurabh Shah | ae61b2b | 2013-04-10 16:37:25 -0700 | [diff] [blame] | 254 | ovutils::getDump(buf, len, "MdpRotCtrl", mRotImgInfo); |
| 255 | ovutils::getDump(buf, len, "MdpRotData", mRotDataInfo); |
Saurabh Shah | 0d0a7cb | 2013-02-12 17:58:19 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 258 | } // namespace overlay |