Home | History | Annotate | Download | only in liboverlay
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  * Copyright (c) 2010-2013, 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.
     18 */
     19 
     20 #include "overlayUtils.h"
     21 #include "overlayRotator.h"
     22 
     23 namespace ovutils = overlay::utils;
     24 
     25 namespace overlay {
     26 
     27 MdpRot::MdpRot() {
     28     reset();
     29     init();
     30 }
     31 
     32 MdpRot::~MdpRot() { close(); }
     33 
     34 bool MdpRot::enabled() const { return mRotImgInfo.enable; }
     35 
     36 void MdpRot::setRotations(uint32_t r) { mRotImgInfo.rotations = r; }
     37 
     38 int MdpRot::getDstMemId() const {
     39     return mRotDataInfo.dst.memory_id;
     40 }
     41 
     42 uint32_t MdpRot::getDstOffset() const {
     43     return mRotDataInfo.dst.offset;
     44 }
     45 
     46 uint32_t MdpRot::getDstFormat() const {
     47     return mRotImgInfo.dst.format;
     48 }
     49 
     50 uint32_t MdpRot::getSessId() const { return mRotImgInfo.session_id; }
     51 
     52 void MdpRot::setDownscale(int ds) {
     53     if(mRotImgInfo.src.format == MDP_Y_CR_CB_GH2V2){
     54         if((utils::ROT_DS_HALF == ds) && (mRotImgInfo.src_rect.h &0x3))
     55             mRotImgInfo.src_rect.h = utils::aligndown(mRotImgInfo.src_rect.h, 4);
     56         else if(((utils::ROT_DS_FOURTH == ds) && (mRotImgInfo.src_rect.h &0x7)))
     57             mRotImgInfo.src_rect.h = utils::aligndown(mRotImgInfo.src_rect.h, 8);
     58         else if(((utils::ROT_DS_EIGHTH == ds) && (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 
     70 void MdpRot::save() {
     71     mLSRotImgInfo = mRotImgInfo;
     72 }
     73 
     74 bool 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 
     83 bool 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 
     92 void MdpRot::setSource(const overlay::utils::Whf& awhf) {
     93     utils::Whf whf(awhf);
     94     mRotImgInfo.src.format = whf.format;
     95 
     96     mRotImgInfo.src.width = whf.w;
     97     mRotImgInfo.src.height = whf.h;
     98 
     99     mRotImgInfo.src_rect.w = whf.w;
    100     mRotImgInfo.src_rect.h = whf.h;
    101 
    102     mRotImgInfo.dst.width = whf.w;
    103     mRotImgInfo.dst.height = whf.h;
    104 }
    105 
    106 void MdpRot::setSource(const utils::Whf& awhf, const utils::Whf& owhf) {
    107     mOrigWhf = owhf;
    108     setSource(awhf);
    109 }
    110 
    111 void MdpRot::setFlags(const utils::eMdpFlags& flags) {
    112     mRotImgInfo.secure = 0;
    113     if(flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
    114         mRotImgInfo.secure = 1;
    115 }
    116 
    117 void MdpRot::setTransform(const utils::eTransform& rot)
    118 {
    119     int r = utils::getMdpOrient(rot);
    120     setRotations(r);
    121     //getMdpOrient will switch the flips if the source is 90 rotated.
    122     //Clients in Android dont factor in 90 rotation while deciding the flip.
    123     mOrientation = static_cast<utils::eTransform>(r);
    124     ALOGE_IF(DEBUG_OVERLAY, "%s: r=%d", __FUNCTION__, r);
    125 }
    126 
    127 void MdpRot::doTransform() {
    128     if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
    129         utils::swap(mRotImgInfo.dst.width, mRotImgInfo.dst.height);
    130 }
    131 
    132 bool MdpRot::commit() {
    133     doTransform();
    134     if(rotConfChanged()) {
    135         mRotImgInfo.enable = 1;
    136         if(!overlay::mdp_wrapper::startRotator(mFd.getFD(), mRotImgInfo)) {
    137             ALOGE("MdpRot commit failed");
    138             dump();
    139             mRotImgInfo.enable = 0;
    140             return false;
    141         }
    142         save();
    143         mRotDataInfo.session_id = mRotImgInfo.session_id;
    144     }
    145     return true;
    146 }
    147 
    148 uint32_t MdpRot::calcOutputBufSize() {
    149     if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
    150         utils::swap(mOrigWhf.w, mOrigWhf.h);
    151 
    152     ovutils::Whf destWhf(mOrigWhf.w, mOrigWhf.h,
    153                          mRotImgInfo.dst.format);
    154     return Rotator::calcOutputBufSize(destWhf);
    155 }
    156 
    157 bool MdpRot::open_i(uint32_t numbufs, uint32_t bufsz)
    158 {
    159     OvMem mem;
    160 
    161     OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
    162 
    163     if(!mem.open(numbufs, bufsz, mRotImgInfo.secure)){
    164         ALOGE("%s: Failed to open", __func__);
    165         mem.close();
    166         return false;
    167     }
    168 
    169     OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
    170     OVASSERT(mem.getFD() != -1, "getFd is -1");
    171 
    172     mRotDataInfo.dst.memory_id = mem.getFD();
    173     mRotDataInfo.dst.offset = 0;
    174     mMem.curr().m = mem;
    175     return true;
    176 }
    177 
    178 bool MdpRot::close() {
    179     bool success = true;
    180     if(mFd.valid() && (getSessId() != 0)) {
    181         if(!mdp_wrapper::endRotator(mFd.getFD(), getSessId())) {
    182             ALOGE("Mdp Rot error endRotator, fd=%d sessId=%u",
    183                     mFd.getFD(), getSessId());
    184             success = false;
    185         }
    186     }
    187     if (!mFd.close()) {
    188         ALOGE("Mdp Rot error closing fd");
    189         success = false;
    190     }
    191     if (!mMem.close()) {
    192         ALOGE("Mdp Rot error closing mem");
    193         success = false;
    194     }
    195     reset();
    196     return success;
    197 }
    198 
    199 bool MdpRot::remap(uint32_t numbufs) {
    200     // if current size changed, remap
    201     uint32_t opBufSize = calcOutputBufSize();
    202     if(opBufSize == mMem.curr().size()) {
    203         ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, opBufSize);
    204         return true;
    205     }
    206 
    207     ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
    208     OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
    209 
    210     // ++mMem will make curr to be prev, and prev will be curr
    211     ++mMem;
    212     if(!open_i(numbufs, opBufSize)) {
    213         ALOGE("%s Error could not open", __FUNCTION__);
    214         return false;
    215     }
    216     for (uint32_t i = 0; i < numbufs; ++i) {
    217         mMem.curr().mRotOffset[i] = i * opBufSize;
    218     }
    219     return true;
    220 }
    221 
    222 void MdpRot::reset() {
    223     ovutils::memset0(mRotImgInfo);
    224     ovutils::memset0(mLSRotImgInfo);
    225     ovutils::memset0(mRotDataInfo);
    226     ovutils::memset0(mMem.curr().mRotOffset);
    227     ovutils::memset0(mMem.prev().mRotOffset);
    228     mMem.curr().mCurrOffset = 0;
    229     mMem.prev().mCurrOffset = 0;
    230     mOrientation = utils::OVERLAY_TRANSFORM_0;
    231 }
    232 
    233 bool MdpRot::queueBuffer(int fd, uint32_t offset) {
    234     if(enabled()) {
    235         mRotDataInfo.src.memory_id = fd;
    236         mRotDataInfo.src.offset = offset;
    237 
    238         remap(RotMem::Mem::ROT_NUM_BUFS);
    239         OVASSERT(mMem.curr().m.numBufs(),
    240                 "queueBuffer numbufs is 0");
    241         mRotDataInfo.dst.offset =
    242                 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
    243         mMem.curr().mCurrOffset =
    244                 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
    245 
    246         if(!overlay::mdp_wrapper::rotate(mFd.getFD(), mRotDataInfo)) {
    247             ALOGE("MdpRot failed rotate");
    248             dump();
    249             return false;
    250         }
    251 
    252         // if the prev mem is valid, we need to close
    253         if(mMem.prev().valid()) {
    254             // FIXME if no wait for vsync the above
    255             // play will return immediatly and might cause
    256             // tearing when prev.close is called.
    257             if(!mMem.prev().close()) {
    258                 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
    259                 return false;
    260             }
    261         }
    262     }
    263     return true;
    264 }
    265 
    266 void MdpRot::dump() const {
    267     ALOGE("== Dump MdpRot start ==");
    268     mFd.dump();
    269     mMem.curr().m.dump();
    270     mdp_wrapper::dump("mRotImgInfo", mRotImgInfo);
    271     mdp_wrapper::dump("mRotDataInfo", mRotDataInfo);
    272     ALOGE("== Dump MdpRot end ==");
    273 }
    274 
    275 void MdpRot::getDump(char *buf, size_t len) const {
    276     ovutils::getDump(buf, len, "MdpRotCtrl(msm_rotator_img_info)", mRotImgInfo);
    277     ovutils::getDump(buf, len, "MdpRotData(msm_rotator_data_info)", mRotDataInfo);
    278 }
    279 
    280 } // namespace overlay
    281