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