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