1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 * Copyright (c) 2010-2014, 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 <math.h>
21 #include "overlayUtils.h"
22 #include "overlayRotator.h"
23
24 #define DEBUG_MDSS_ROT 0
25
26 #ifdef VENUS_COLOR_FORMAT
27 #include <media/msm_media_info.h>
28 #else
29 #define VENUS_BUFFER_SIZE(args...) 0
30 #endif
31
32 #ifndef MDSS_MDP_ROT_ONLY
33 #define MDSS_MDP_ROT_ONLY 0x80
34 #endif
35
36 #define MDSS_ROT_MASK (MDP_ROT_90 | MDP_FLIP_UD | MDP_FLIP_LR)
37
38 namespace ovutils = overlay::utils;
39
40 namespace overlay {
41 using namespace utils;
42
MdssRot()43 MdssRot::MdssRot() {
44 reset();
45 init();
46 }
47
~MdssRot()48 MdssRot::~MdssRot() { close(); }
49
enabled() const50 bool MdssRot::enabled() const { return mEnabled; }
51
setRotations(uint32_t flags)52 void MdssRot::setRotations(uint32_t flags) { mRotInfo.flags |= flags; }
53
getSrcMemId() const54 int MdssRot::getSrcMemId() const {
55 return mRotData.data.memory_id;
56 }
57
getDstMemId() const58 int MdssRot::getDstMemId() const {
59 return mRotData.dst_data.memory_id;
60 }
61
getSrcOffset() const62 uint32_t MdssRot::getSrcOffset() const {
63 return mRotData.data.offset;
64 }
65
getDstOffset() const66 uint32_t MdssRot::getDstOffset() const {
67 return mRotData.dst_data.offset;
68 }
69
getDstFormat() const70 uint32_t MdssRot::getDstFormat() const {
71 //For mdss src and dst formats are same
72 return mRotInfo.src.format;
73 }
74
getDstWhf() const75 utils::Whf MdssRot::getDstWhf() const {
76 //For Mdss dst_rect itself represents buffer dimensions. We ignore actual
77 //aligned values during buffer allocation. Also the driver overwrites the
78 //src.format field if destination format is different.
79 //This implementation detail makes it possible to retrieve w,h even before
80 //buffer allocation, which happens in queueBuffer.
81 return utils::Whf(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h,
82 mRotInfo.src.format);
83 }
84
getDstDimensions() const85 utils::Dim MdssRot::getDstDimensions() const {
86 return utils::Dim(mRotInfo.dst_rect.x, mRotInfo.dst_rect.y,
87 mRotInfo.dst_rect.w, mRotInfo.dst_rect.h);
88 }
89
getSessId() const90 uint32_t MdssRot::getSessId() const { return mRotInfo.id; }
91
save()92 void MdssRot::save() {
93 mLSRotInfo = mRotInfo;
94 }
95
rotConfChanged() const96 bool MdssRot::rotConfChanged() const {
97 // 0 means same
98 if(0 == ::memcmp(&mRotInfo, &mLSRotInfo,
99 sizeof (mdp_overlay))) {
100 return false;
101 }
102 return true;
103 }
104
init()105 bool MdssRot::init() {
106 if(!utils::openDev(mFd, 0, Res::fbPath, O_RDWR)) {
107 ALOGE("MdssRot failed to init fb0");
108 return false;
109 }
110 return true;
111 }
112
setSource(const overlay::utils::Whf & awhf)113 void MdssRot::setSource(const overlay::utils::Whf& awhf) {
114 utils::Whf whf(awhf);
115
116 mRotInfo.src.format = whf.format;
117 mRotInfo.src.width = whf.w;
118 mRotInfo.src.height = whf.h;
119 }
120
setCrop(const utils::Dim & crop)121 void MdssRot::setCrop(const utils::Dim& crop) {
122 mRotInfo.src_rect.x = crop.x;
123 mRotInfo.src_rect.y = crop.y;
124 mRotInfo.src_rect.w = crop.w;
125 mRotInfo.src_rect.h = crop.h;
126 }
127
setDownscale(int downscale)128 void MdssRot::setDownscale(int downscale) {
129 mDownscale = downscale;
130 }
131
setFlags(const utils::eMdpFlags & flags)132 void MdssRot::setFlags(const utils::eMdpFlags& flags) {
133 mRotInfo.flags = flags;
134 }
135
setTransform(const utils::eTransform & rot)136 void MdssRot::setTransform(const utils::eTransform& rot)
137 {
138 // reset rotation flags to avoid stale orientation values
139 mRotInfo.flags &= ~MDSS_ROT_MASK;
140 int flags = utils::getMdpOrient(rot);
141 if (flags != -1)
142 setRotations(flags);
143 mOrientation = static_cast<utils::eTransform>(flags);
144 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, flags);
145 }
146
doTransform()147 void MdssRot::doTransform() {
148 mRotInfo.flags |= mOrientation;
149 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
150 utils::swap(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h);
151 }
152
commit()153 bool MdssRot::commit() {
154 Dim adjCrop(mRotInfo.src_rect.x,mRotInfo.src_rect.y,
155 mRotInfo.src_rect.w,mRotInfo.src_rect.h);
156 adjCrop = getFormatAdjustedCrop(adjCrop, mRotInfo.src.format,
157 mRotInfo.flags & utils::OV_MDP_DEINTERLACE);
158 adjCrop = getDownscaleAdjustedCrop(adjCrop, mDownscale);
159
160 mRotInfo.src_rect.x = adjCrop.x;
161 mRotInfo.src_rect.y = adjCrop.y;
162 mRotInfo.src_rect.w = adjCrop.w;
163 mRotInfo.src_rect.h = adjCrop.h;
164
165 mRotInfo.dst_rect.x = 0;
166 mRotInfo.dst_rect.y = 0;
167 mRotInfo.dst_rect.w = mDownscale ?
168 mRotInfo.src_rect.w / mDownscale : mRotInfo.src_rect.w;
169 mRotInfo.dst_rect.h = mDownscale ?
170 mRotInfo.src_rect.h / mDownscale : mRotInfo.src_rect.h;
171 //Clear for next round
172 mDownscale = 0;
173
174 doTransform();
175
176 mRotInfo.flags |= MDSS_MDP_ROT_ONLY;
177 mEnabled = true;
178 if(!overlay::mdp_wrapper::setOverlay(mFd.getFD(), mRotInfo)) {
179 ALOGE("MdssRot commit failed!");
180 dump();
181 return (mEnabled = false);
182 }
183 mRotData.id = mRotInfo.id;
184 return true;
185 }
186
queueBuffer(int fd,uint32_t offset)187 bool MdssRot::queueBuffer(int fd, uint32_t offset) {
188 if(enabled() and (not isRotCached(fd,offset))) {
189 int prev_fd = getSrcMemId();
190 uint32_t prev_offset = getSrcOffset();
191
192 mRotData.data.memory_id = fd;
193 mRotData.data.offset = offset;
194
195 if(false == remap(RotMem::ROT_NUM_BUFS)) {
196 ALOGE("%s Remap failed, not queuing", __FUNCTION__);
197 return false;
198 }
199
200 mRotData.dst_data.offset =
201 mMem.mRotOffset[mMem.mCurrIndex];
202
203 if(!overlay::mdp_wrapper::play(mFd.getFD(), mRotData)) {
204 ALOGE("MdssRot play failed!");
205 dump();
206 mRotData.data.memory_id = prev_fd;
207 mRotData.data.offset = prev_offset;
208 return false;
209 }
210 save();
211 mMem.mCurrIndex =
212 (mMem.mCurrIndex + 1) % mMem.mem.numBufs();
213 }
214 return true;
215 }
216
open_i(uint32_t numbufs,uint32_t bufsz)217 bool MdssRot::open_i(uint32_t numbufs, uint32_t bufsz)
218 {
219 OvMem mem;
220 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
221 bool isSecure = mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION;
222
223 if(!mem.open(numbufs, bufsz, isSecure)){
224 ALOGE("%s: Failed to open", __func__);
225 mem.close();
226 return false;
227 }
228
229 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
230 OVASSERT(mem.getFD() != -1, "getFd is -1");
231
232 mRotData.dst_data.memory_id = mem.getFD();
233 mRotData.dst_data.offset = 0;
234 mMem.mem = mem;
235 return true;
236 }
237
remap(uint32_t numbufs)238 bool MdssRot::remap(uint32_t numbufs) {
239 // Calculate the size based on rotator's dst format, w and h.
240 uint32_t opBufSize = calcOutputBufSize();
241 // If current size changed, remap
242 if(opBufSize == mMem.size()) {
243 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, opBufSize);
244 return true;
245 }
246
247 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
248
249 if(!mMem.close()) {
250 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
251 return false;
252 }
253
254 if(!open_i(numbufs, opBufSize)) {
255 ALOGE("%s Error could not open", __FUNCTION__);
256 return false;
257 }
258
259 for (uint32_t i = 0; i < numbufs; ++i) {
260 mMem.mRotOffset[i] = i * opBufSize;
261 }
262
263 return true;
264 }
265
close()266 bool MdssRot::close() {
267 bool success = true;
268 if(mFd.valid() && (getSessId() != (uint32_t) MSMFB_NEW_REQUEST)) {
269 if(!mdp_wrapper::unsetOverlay(mFd.getFD(), getSessId())) {
270 ALOGE("MdssRot::close unsetOverlay failed, fd=%d sessId=%d",
271 mFd.getFD(), getSessId());
272 success = false;
273 }
274 }
275
276 if (!mFd.close()) {
277 ALOGE("Mdss Rot error closing fd");
278 success = false;
279 }
280 if (!mMem.close()) {
281 ALOGE("Mdss Rot error closing mem");
282 success = false;
283 }
284 reset();
285 return success;
286 }
287
reset()288 void MdssRot::reset() {
289 ovutils::memset0(mRotInfo);
290 ovutils::memset0(mLSRotInfo);
291 ovutils::memset0(mRotData);
292 mRotData.data.memory_id = -1;
293 mRotInfo.id = MSMFB_NEW_REQUEST;
294 ovutils::memset0(mMem.mRotOffset);
295 mMem.mCurrIndex = 0;
296 mOrientation = utils::OVERLAY_TRANSFORM_0;
297 mDownscale = 0;
298 }
299
dump() const300 void MdssRot::dump() const {
301 ALOGE("== Dump MdssRot start ==");
302 mFd.dump();
303 mMem.mem.dump();
304 mdp_wrapper::dump("mRotInfo", mRotInfo);
305 mdp_wrapper::dump("mRotData", mRotData);
306 ALOGE("== Dump MdssRot end ==");
307 }
308
calcOutputBufSize()309 uint32_t MdssRot::calcOutputBufSize() {
310 uint32_t opBufSize = 0;
311 ovutils::Whf destWhf(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h,
312 mRotInfo.src.format); //mdss src and dst formats are same.
313
314 if (mRotInfo.flags & ovutils::OV_MDSS_MDP_BWC_EN) {
315 opBufSize = calcCompressedBufSize(destWhf);
316 } else {
317 opBufSize = Rotator::calcOutputBufSize(destWhf);
318 }
319
320 return opBufSize;
321 }
322
getDump(char * buf,size_t len) const323 void MdssRot::getDump(char *buf, size_t len) const {
324 ovutils::getDump(buf, len, "MdssRotCtrl", mRotInfo);
325 ovutils::getDump(buf, len, "MdssRotData", mRotData);
326 }
327
328 // Calculate the compressed o/p buffer size for BWC
calcCompressedBufSize(const ovutils::Whf & destWhf)329 uint32_t MdssRot::calcCompressedBufSize(const ovutils::Whf& destWhf) {
330 uint32_t bufSize = 0;
331 //Worst case alignments
332 int aWidth = ovutils::align(destWhf.w, 64);
333 int aHeight = ovutils::align(destWhf.h, 4);
334 /*
335 Format | RAU size (width x height)
336 ----------------------------------------------
337 ARGB | 32 pixel x 4 line
338 RGB888 | 32 pixel x 4 line
339 Y (Luma) | 64 pixel x 4 line
340 CRCB 420 | 32 pixel x 2 line
341 CRCB 422 H2V1 | 32 pixel x 4 line
342 CRCB 422 H1V2 | 64 pixel x 2 line
343
344 Metadata requirements:-
345 1 byte meta data for every 8 RAUs
346 2 byte meta data per RAU
347 */
348
349 //These blocks attempt to allocate for the worst case in each of the
350 //respective format classes, yuv/rgb. The table above is for reference
351 if(utils::isYuv(destWhf.format)) {
352 int yRauCount = aWidth / 64; //Y
353 int cRauCount = aWidth / 32; //C
354 int yStride = (64 * 4 * yRauCount) + alignup(yRauCount, 8) / 8;
355 int cStride = ((32 * 2 * cRauCount) + alignup(cRauCount, 8) / 8) * 2;
356 int yStrideOffset = (aHeight / 4);
357 int cStrideOffset = (aHeight / 2);
358 bufSize = (yStride * yStrideOffset + cStride * cStrideOffset) +
359 (yRauCount * yStrideOffset * 2) +
360 (cRauCount * cStrideOffset * 2) * 2;
361 ALOGD_IF(DEBUG_MDSS_ROT, "%s:YUV Y RAU Count = %d C RAU Count = %d",
362 __FUNCTION__, yRauCount, cRauCount);
363 } else {
364 int rauCount = aWidth / 32;
365 //Single plane
366 int stride = (32 * 4 * rauCount) + alignup(rauCount, 8) / 8;
367 int strideOffset = (aHeight / 4);
368 bufSize = (stride * strideOffset * 4 /*bpp*/) +
369 (rauCount * strideOffset * 2);
370 ALOGD_IF(DEBUG_MDSS_ROT, "%s:RGB RAU count = %d", __FUNCTION__,
371 rauCount);
372 }
373
374 ALOGD_IF(DEBUG_MDSS_ROT, "%s: aligned width = %d, aligned height = %d "
375 "Buf Size = %d", __FUNCTION__, aWidth, aHeight, bufSize);
376
377 return bufSize;
378 }
379
getDownscaleFactor(const int & srcW,const int & srcH,const int & dstW,const int & dstH,const uint32_t & mdpFormat,const bool & isInterlaced)380 int MdssRot::getDownscaleFactor(const int& srcW, const int& srcH,
381 const int& dstW, const int& dstH, const uint32_t& mdpFormat,
382 const bool& isInterlaced) {
383 if(not srcW or not srcH or not dstW or not dstH or isInterlaced) return 0;
384
385 Dim crop(0, 0, srcW, srcH);
386 Dim adjCrop = getFormatAdjustedCrop(crop, mdpFormat,
387 false /*isInterlaced */);
388
389 uint32_t downscale = min((adjCrop.w / dstW), (adjCrop.h / dstH));
390 //Reduced to a power of 2
391 downscale = (uint32_t) powf(2.0f, floorf(log2f((float)downscale)));
392
393 if(downscale < 2 or downscale > 32) return 0;
394
395 //Allow only 1 line or pixel to be chopped off since the source needs to
396 //be aligned to downscale. Progressively try with smaller downscale to see
397 //if we can satisfy the threshold
398 //For YUV the loop shouldnt be needed, unless in exceptional cases
399 Dim dsAdjCrop = getDownscaleAdjustedCrop(adjCrop, downscale);
400 while(downscale > 2 and (adjCrop.w > dsAdjCrop.w or
401 adjCrop.h > dsAdjCrop.h)) {
402 downscale /= 2;
403 dsAdjCrop = getDownscaleAdjustedCrop(adjCrop, downscale);
404 }
405
406 if(not dsAdjCrop.w or not dsAdjCrop.h) return 0;
407 return downscale;
408 }
409
getFormatAdjustedCrop(const Dim & crop,const uint32_t & mdpFormat,const bool & isInterlaced)410 Dim MdssRot::getFormatAdjustedCrop(const Dim& crop,
411 const uint32_t& mdpFormat, const bool& isInterlaced) {
412 Dim adjCrop = crop;
413 if (isYuv(mdpFormat)) {
414 normalizeCrop(adjCrop.x, adjCrop.w);
415 normalizeCrop(adjCrop.y, adjCrop.h);
416 // For interlaced, crop.h should be 4-aligned
417 if (isInterlaced and (adjCrop.h % 4))
418 adjCrop.h = aligndown(adjCrop.h, 4);
419 }
420 return adjCrop;
421 }
422
getDownscaleAdjustedCrop(const Dim & crop,const uint32_t & downscale)423 Dim MdssRot::getDownscaleAdjustedCrop(const Dim& crop,
424 const uint32_t& downscale) {
425 uint32_t alignedSrcW = aligndown(crop.w, downscale * 2);
426 uint32_t alignedSrcH = aligndown(crop.h, downscale * 2);
427 return Dim(crop.x, crop.y, alignedSrcW, alignedSrcH);
428 }
429
430 } // namespace overlay
431