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