blob: 18d1bec5f9a2f0b9f5cba3c49ca54def21f95489 [file] [log] [blame]
Saurabh Shahe012f7a2012-08-18 15:11:57 -07001/*
2* Copyright (C) 2008 The Android Open Source Project
3* Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved.
4*
5* Licensed under the Apache License, Version 2.0 (the "License");
6* you may not use this file except in compliance with the License.
7* You may obtain a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS,
13* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14* See the License for the specific language governing permissions and
15* limitations under the License.
16*/
17
18#include "overlayUtils.h"
19#include "overlayRotator.h"
20
21namespace ovutils = overlay::utils;
22
23namespace overlay {
24
25bool MdpRot::init()
26{
27 if(!mFd.open(Res::rotPath, O_RDWR)){
28 ALOGE("MdpRot failed to init %s", Res::rotPath);
29 return false;
30 }
31 return true;
32}
33
34void MdpRot::setSource(const overlay::utils::Whf& awhf) {
35 utils::Whf whf(awhf);
36
37 mRotImgInfo.src.format = whf.format;
38 if(whf.format == MDP_Y_CRCB_H2V2_TILE ||
39 whf.format == MDP_Y_CBCR_H2V2_TILE) {
40 whf.w = utils::alignup(awhf.w, 64);
41 whf.h = utils::alignup(awhf.h, 32);
42 }
43
44 mRotImgInfo.src.width = whf.w;
45 mRotImgInfo.src.height = whf.h;
46
47 mRotImgInfo.src_rect.w = whf.w;
48 mRotImgInfo.src_rect.h = whf.h;
49
50 mRotImgInfo.dst.width = whf.w;
51 mRotImgInfo.dst.height = whf.h;
52
53 mBufSize = awhf.size;
54}
55
56void MdpRot::setFlags(const utils::eMdpFlags& flags) {
57 mRotImgInfo.secure = 0;
58 if(flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
59 mRotImgInfo.secure = 1;
60}
61
62void MdpRot::setTransform(const utils::eTransform& rot, const bool& rotUsed)
63{
64 int r = utils::getMdpOrient(rot);
65 setRotations(r);
66 //getMdpOrient will switch the flips if the source is 90 rotated.
67 //Clients in Android dont factor in 90 rotation while deciding the flip.
68 mOrientation = static_cast<utils::eTransform>(r);
69 ALOGE_IF(DEBUG_OVERLAY, "%s: r=%d", __FUNCTION__, r);
70
71 setDisable();
72 if(rotUsed) {
73 setEnable();
74 }
75}
76
77void MdpRot::doTransform() {
78 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
79 utils::swap(mRotImgInfo.dst.width, mRotImgInfo.dst.height);
80}
81
82bool MdpRot::commit() {
83 doTransform();
84 if(!overlay::mdp_wrapper::startRotator(mFd.getFD(), mRotImgInfo)) {
85 ALOGE("MdpRot commit failed");
86 dump();
87 return false;
88 }
89 mRotDataInfo.session_id = mRotImgInfo.session_id;
90 return true;
91}
92
93bool MdpRot::open_i(uint32_t numbufs, uint32_t bufsz)
94{
95 OvMem mem;
96
97 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
98
99 if(!mem.open(numbufs, bufsz, mRotImgInfo.secure)){
100 ALOGE("%s: Failed to open", __func__);
101 mem.close();
102 return false;
103 }
104
105 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
106 OVASSERT(mem.getFD() != -1, "getFd is -1");
107
108 mRotDataInfo.dst.memory_id = mem.getFD();
109 mRotDataInfo.dst.offset = 0;
110 mMem.curr().m = mem;
111 return true;
112}
113
114bool MdpRot::close() {
115 bool success = true;
116 if(mFd.valid() && (getSessId() > 0)) {
117 if(!mdp_wrapper::endRotator(mFd.getFD(), getSessId())) {
118 ALOGE("Mdp Rot error endRotator, fd=%d sessId=%d",
119 mFd.getFD(), getSessId());
120 success = false;
121 }
122 }
123 if (!mFd.close()) {
124 ALOGE("Mdp Rot error closing fd");
125 success = false;
126 }
127 if (!mMem.close()) {
128 ALOGE("Mdp Rot error closing mem");
129 success = false;
130 }
131 reset();
132 return success;
133}
134
135bool MdpRot::remap(uint32_t numbufs) {
136 // if current size changed, remap
137 if(mBufSize == mMem.curr().size()) {
138 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, mBufSize);
139 return true;
140 }
141
142 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
143 OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
144
145 // ++mMem will make curr to be prev, and prev will be curr
146 ++mMem;
147 if(!open_i(numbufs, mBufSize)) {
148 ALOGE("%s Error could not open", __FUNCTION__);
149 return false;
150 }
151 for (uint32_t i = 0; i < numbufs; ++i) {
152 mMem.curr().mRotOffset[i] = i * mBufSize;
153 }
154 return true;
155}
156
157void MdpRot::reset() {
158 ovutils::memset0(mRotImgInfo);
159 ovutils::memset0(mRotDataInfo);
160 ovutils::memset0(mMem.curr().mRotOffset);
161 ovutils::memset0(mMem.prev().mRotOffset);
162 mMem.curr().mCurrOffset = 0;
163 mMem.prev().mCurrOffset = 0;
164 mBufSize = 0;
165 mOrientation = utils::OVERLAY_TRANSFORM_0;
166}
167
168bool MdpRot::queueBuffer(int fd, uint32_t offset) {
169 if(enabled()) {
170 mRotDataInfo.src.memory_id = fd;
171 mRotDataInfo.src.offset = offset;
172
173 remap(RotMem::Mem::ROT_NUM_BUFS);
174 OVASSERT(mMem.curr().m.numBufs(),
175 "queueBuffer numbufs is 0");
176 mRotDataInfo.dst.offset =
177 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
178 mMem.curr().mCurrOffset =
179 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
180
181 if(!overlay::mdp_wrapper::rotate(mFd.getFD(), mRotDataInfo)) {
182 ALOGE("MdpRot failed rotate");
183 dump();
184 return false;
185 }
186
187 // if the prev mem is valid, we need to close
188 if(mMem.prev().valid()) {
189 // FIXME if no wait for vsync the above
190 // play will return immediatly and might cause
191 // tearing when prev.close is called.
192 if(!mMem.prev().close()) {
193 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
194 return false;
195 }
196 }
197 }
198 return true;
199}
200
201void MdpRot::dump() const {
202 ALOGE("== Dump MdpRot start ==");
203 mFd.dump();
204 mMem.curr().m.dump();
205 mdp_wrapper::dump("mRotImgInfo", mRotImgInfo);
206 mdp_wrapper::dump("mRotDataInfo", mRotDataInfo);
207 ALOGE("== Dump MdpRot end ==");
208}
209
210} // namespace overlay