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 |
| 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 Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "overlayUtils.h" |
| 21 | #include "overlayRotator.h" |
| 22 | |
Sushil Chauhan | c6bd6d9 | 2012-12-12 12:33:01 -0800 | [diff] [blame] | 23 | #ifdef VENUS_COLOR_FORMAT |
| 24 | #include <media/msm_media_info.h> |
| 25 | #else |
| 26 | #define VENUS_BUFFER_SIZE(args...) 0 |
| 27 | #endif |
| 28 | |
Naseer Ahmed | c21013b | 2012-11-19 12:44:41 -0500 | [diff] [blame] | 29 | #ifndef MDSS_MDP_ROT_ONLY |
| 30 | #define MDSS_MDP_ROT_ONLY 0x80 |
| 31 | #endif |
| 32 | |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 33 | namespace ovutils = overlay::utils; |
| 34 | |
| 35 | namespace overlay { |
Naseer Ahmed | 758bfc5 | 2012-11-28 17:02:08 -0500 | [diff] [blame] | 36 | MdssRot::MdssRot() { |
| 37 | reset(); |
| 38 | init(); |
| 39 | } |
| 40 | |
| 41 | MdssRot::~MdssRot() { close(); } |
| 42 | |
| 43 | void MdssRot::setEnable() { mEnabled = true; } |
| 44 | |
| 45 | void MdssRot::setDisable() { mEnabled = false; } |
| 46 | |
| 47 | bool MdssRot::enabled() const { return mEnabled; } |
| 48 | |
| 49 | void MdssRot::setRotations(uint32_t flags) { mRotInfo.flags |= flags; } |
| 50 | |
| 51 | int MdssRot::getDstMemId() const { |
| 52 | return mRotData.dst_data.memory_id; |
| 53 | } |
| 54 | |
| 55 | uint32_t MdssRot::getDstOffset() const { |
| 56 | return mRotData.dst_data.offset; |
| 57 | } |
| 58 | |
| 59 | uint32_t MdssRot::getSessId() const { return mRotInfo.id; } |
| 60 | |
| 61 | void MdssRot::setSrcFB() { |
| 62 | mRotData.data.flags |= MDP_MEMORY_ID_TYPE_FB; |
| 63 | } |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 64 | |
| 65 | bool MdssRot::init() { |
| 66 | if(!utils::openDev(mFd, 0, Res::fbPath, O_RDWR)) { |
| 67 | ALOGE("MdssRot failed to init fb0"); |
| 68 | return false; |
| 69 | } |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | void MdssRot::setSource(const overlay::utils::Whf& awhf) { |
| 74 | utils::Whf whf(awhf); |
| 75 | |
| 76 | mRotInfo.src.format = whf.format; |
| 77 | if(whf.format == MDP_Y_CRCB_H2V2_TILE || |
| 78 | whf.format == MDP_Y_CBCR_H2V2_TILE) { |
| 79 | whf.w = utils::alignup(awhf.w, 64); |
| 80 | whf.h = utils::alignup(awhf.h, 32); |
| 81 | } |
| 82 | |
| 83 | mRotInfo.src.width = whf.w; |
| 84 | mRotInfo.src.height = whf.h; |
| 85 | |
| 86 | mRotInfo.src_rect.w = whf.w; |
| 87 | mRotInfo.src_rect.h = whf.h; |
| 88 | |
| 89 | mRotInfo.dst_rect.w = whf.w; |
| 90 | mRotInfo.dst_rect.h = whf.h; |
| 91 | |
| 92 | mBufSize = awhf.size; |
| 93 | } |
| 94 | |
| 95 | void MdssRot::setFlags(const utils::eMdpFlags& flags) { |
| 96 | // TODO |
| 97 | } |
| 98 | |
| 99 | void MdssRot::setTransform(const utils::eTransform& rot, const bool& rotUsed) |
| 100 | { |
| 101 | int flags = utils::getMdpOrient(rot); |
| 102 | if (flags != -1) |
| 103 | setRotations(flags); |
| 104 | //getMdpOrient will switch the flips if the source is 90 rotated. |
| 105 | //Clients in Android dont factor in 90 rotation while deciding the flip. |
| 106 | mOrientation = static_cast<utils::eTransform>(flags); |
| 107 | ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, flags); |
| 108 | |
| 109 | setDisable(); |
| 110 | if(rotUsed) { |
| 111 | setEnable(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void MdssRot::doTransform() { |
| 116 | if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90) |
| 117 | utils::swap(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h); |
| 118 | } |
| 119 | |
| 120 | bool MdssRot::commit() { |
| 121 | doTransform(); |
Sushil Chauhan | fbffad7 | 2012-11-06 13:05:42 -0800 | [diff] [blame^] | 122 | setBufSize(mRotInfo.src.format); |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 123 | mRotInfo.flags |= MDSS_MDP_ROT_ONLY; |
| 124 | if(!overlay::mdp_wrapper::setOverlay(mFd.getFD(), mRotInfo)) { |
| 125 | ALOGE("MdssRot commit failed!"); |
| 126 | dump(); |
| 127 | return false; |
| 128 | } |
| 129 | mRotData.id = mRotInfo.id; |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | bool MdssRot::queueBuffer(int fd, uint32_t offset) { |
| 134 | if(enabled()) { |
| 135 | mRotData.data.memory_id = fd; |
| 136 | mRotData.data.offset = offset; |
| 137 | |
| 138 | remap(RotMem::Mem::ROT_NUM_BUFS); |
| 139 | OVASSERT(mMem.curr().m.numBufs(), "queueBuffer numbufs is 0"); |
| 140 | |
| 141 | mRotData.dst_data.offset = |
| 142 | mMem.curr().mRotOffset[mMem.curr().mCurrOffset]; |
| 143 | mMem.curr().mCurrOffset = |
| 144 | (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs(); |
| 145 | |
| 146 | if(!overlay::mdp_wrapper::play(mFd.getFD(), mRotData)) { |
| 147 | ALOGE("MdssRot play failed!"); |
| 148 | dump(); |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | // if the prev mem is valid, we need to close |
| 153 | if(mMem.prev().valid()) { |
| 154 | // FIXME if no wait for vsync the above |
| 155 | // play will return immediatly and might cause |
| 156 | // tearing when prev.close is called. |
| 157 | if(!mMem.prev().close()) { |
| 158 | ALOGE("%s error in closing prev rot mem", __FUNCTION__); |
| 159 | return false; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | bool MdssRot::open_i(uint32_t numbufs, uint32_t bufsz) |
| 167 | { |
| 168 | OvMem mem; |
| 169 | OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i"); |
| 170 | |
| 171 | if(!mem.open(numbufs, bufsz, false)){ // TODO: secure for badger |
| 172 | ALOGE("%s: Failed to open", __func__); |
| 173 | mem.close(); |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | OVASSERT(MAP_FAILED != mem.addr(), "MAP failed"); |
| 178 | OVASSERT(mem.getFD() != -1, "getFd is -1"); |
| 179 | |
| 180 | mRotData.dst_data.memory_id = mem.getFD(); |
| 181 | mRotData.dst_data.offset = 0; |
| 182 | mMem.curr().m = mem; |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | bool MdssRot::remap(uint32_t numbufs) { |
| 187 | // if current size changed, remap |
| 188 | if(mBufSize == mMem.curr().size()) { |
| 189 | ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, mBufSize); |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__); |
| 194 | OVASSERT(!mMem.prev().valid(), "Prev should not be valid"); |
| 195 | |
| 196 | // ++mMem will make curr to be prev, and prev will be curr |
| 197 | ++mMem; |
| 198 | if(!open_i(numbufs, mBufSize)) { |
| 199 | ALOGE("%s Error could not open", __FUNCTION__); |
| 200 | return false; |
| 201 | } |
| 202 | for (uint32_t i = 0; i < numbufs; ++i) { |
| 203 | mMem.curr().mRotOffset[i] = i * mBufSize; |
| 204 | } |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | bool MdssRot::close() { |
| 209 | bool success = true; |
Ken Zhang | ba48b01 | 2012-12-11 20:33:59 -0500 | [diff] [blame] | 210 | if(mFd.valid() && (getSessId() != (uint32_t) MSMFB_NEW_REQUEST)) { |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 211 | if(!mdp_wrapper::unsetOverlay(mFd.getFD(), getSessId())) { |
| 212 | ALOGE("MdssRot::close unsetOverlay failed, fd=%d sessId=%d", |
| 213 | mFd.getFD(), getSessId()); |
Ken Zhang | ba48b01 | 2012-12-11 20:33:59 -0500 | [diff] [blame] | 214 | success = false; |
| 215 | } |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | if (!mFd.close()) { |
| 219 | ALOGE("Mdss Rot error closing fd"); |
| 220 | success = false; |
| 221 | } |
| 222 | if (!mMem.close()) { |
| 223 | ALOGE("Mdss Rot error closing mem"); |
| 224 | success = false; |
| 225 | } |
| 226 | reset(); |
| 227 | return success; |
| 228 | } |
| 229 | |
| 230 | void MdssRot::reset() { |
| 231 | ovutils::memset0(mRotInfo); |
| 232 | ovutils::memset0(mRotData); |
| 233 | mRotData.data.memory_id = -1; |
| 234 | mRotInfo.id = MSMFB_NEW_REQUEST; |
| 235 | ovutils::memset0(mMem.curr().mRotOffset); |
| 236 | ovutils::memset0(mMem.prev().mRotOffset); |
| 237 | mMem.curr().mCurrOffset = 0; |
| 238 | mMem.prev().mCurrOffset = 0; |
| 239 | mBufSize = 0; |
| 240 | mOrientation = utils::OVERLAY_TRANSFORM_0; |
| 241 | } |
| 242 | |
| 243 | void MdssRot::dump() const { |
| 244 | ALOGE("== Dump MdssRot start =="); |
| 245 | mFd.dump(); |
| 246 | mMem.curr().m.dump(); |
| 247 | mdp_wrapper::dump("mRotInfo", mRotInfo); |
| 248 | mdp_wrapper::dump("mRotData", mRotData); |
| 249 | ALOGE("== Dump MdssRot end =="); |
| 250 | } |
| 251 | |
Sushil Chauhan | fbffad7 | 2012-11-06 13:05:42 -0800 | [diff] [blame^] | 252 | void MdssRot::setBufSize(int format) { |
| 253 | if (format == MDP_Y_CBCR_H2V2_VENUS) { |
| 254 | mBufSize = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, mRotInfo.dst_rect.w, |
| 255 | mRotInfo.dst_rect.h); |
| 256 | } |
| 257 | } |
Saurabh Shah | e012f7a | 2012-08-18 15:11:57 -0700 | [diff] [blame] | 258 | } // namespace overlay |