blob: 8e55362f0024e0b51eac3923f6bc009684cc97ca [file] [log] [blame]
Saurabh Shahe012f7a2012-08-18 15:11:57 -07001/*
Naseer Ahmed758bfc52012-11-28 17:02:08 -05002 * Copyright (C) 2008 The Android Open Source Project
Arun Kumar K.R7e5a1f82014-01-22 10:36:16 -08003 * Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
Naseer Ahmed758bfc52012-11-28 17:02:08 -05004 * 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.
Saurabh Shahe012f7a2012-08-18 15:11:57 -070018*/
19
Saurabh Shahc46cf9d2014-07-02 15:22:34 -070020#include <math.h>
Saurabh Shahe012f7a2012-08-18 15:11:57 -070021#include "overlayUtils.h"
22#include "overlayRotator.h"
23
Sushil Chauhanbab187a2013-01-30 17:44:15 -080024#define DEBUG_MDSS_ROT 0
25
Sushil Chauhanc6bd6d92012-12-12 12:33:01 -080026#ifdef VENUS_COLOR_FORMAT
27#include <media/msm_media_info.h>
28#else
29#define VENUS_BUFFER_SIZE(args...) 0
30#endif
31
Naseer Ahmedc21013b2012-11-19 12:44:41 -050032#ifndef MDSS_MDP_ROT_ONLY
33#define MDSS_MDP_ROT_ONLY 0x80
34#endif
35
Sushil Chauhan95e4c9f2013-01-14 18:44:14 -080036#define MDSS_ROT_MASK (MDP_ROT_90 | MDP_FLIP_UD | MDP_FLIP_LR)
Sushil Chauhan6e3fab82012-11-19 15:30:33 -080037
Saurabh Shahe012f7a2012-08-18 15:11:57 -070038namespace ovutils = overlay::utils;
39
40namespace overlay {
Saurabh Shah4b54c5b2014-05-01 18:36:51 -070041using namespace utils;
42
Naseer Ahmed758bfc52012-11-28 17:02:08 -050043MdssRot::MdssRot() {
44 reset();
45 init();
46}
47
48MdssRot::~MdssRot() { close(); }
49
Saurabh Shahacf10202013-02-26 10:15:15 -080050bool MdssRot::enabled() const { return mEnabled; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050051
Saurabh Shahacf10202013-02-26 10:15:15 -080052void MdssRot::setRotations(uint32_t flags) { mRotInfo.flags |= flags; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050053
Saurabh Shahacf10202013-02-26 10:15:15 -080054int MdssRot::getDstMemId() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050055 return mRotData.dst_data.memory_id;
56}
57
Saurabh Shahacf10202013-02-26 10:15:15 -080058uint32_t MdssRot::getDstOffset() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050059 return mRotData.dst_data.offset;
60}
61
Saurabh Shahacf10202013-02-26 10:15:15 -080062uint32_t MdssRot::getDstFormat() const {
Raj kamal23f69b22012-11-17 00:20:55 +053063 //For mdss src and dst formats are same
64 return mRotInfo.src.format;
65}
Naseer Ahmed758bfc52012-11-28 17:02:08 -050066
Saurabh Shah8ec9b5e2014-06-30 14:37:17 -070067utils::Whf MdssRot::getDstWhf() const {
68 //For Mdss dst_rect itself represents buffer dimensions. We ignore actual
69 //aligned values during buffer allocation. Also the driver overwrites the
70 //src.format field if destination format is different.
71 //This implementation detail makes it possible to retrieve w,h even before
72 //buffer allocation, which happens in queueBuffer.
73 return utils::Whf(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h,
74 mRotInfo.src.format);
75}
76
77utils::Dim MdssRot::getDstDimensions() const {
78 return utils::Dim(mRotInfo.dst_rect.x, mRotInfo.dst_rect.y,
79 mRotInfo.dst_rect.w, mRotInfo.dst_rect.h);
80}
81
Saurabh Shahacf10202013-02-26 10:15:15 -080082uint32_t MdssRot::getSessId() const { return mRotInfo.id; }
Raj kamal23f69b22012-11-17 00:20:55 +053083
Saurabh Shahacf10202013-02-26 10:15:15 -080084bool MdssRot::init() {
Saurabh Shahe012f7a2012-08-18 15:11:57 -070085 if(!utils::openDev(mFd, 0, Res::fbPath, O_RDWR)) {
86 ALOGE("MdssRot failed to init fb0");
87 return false;
88 }
89 return true;
90}
91
92void MdssRot::setSource(const overlay::utils::Whf& awhf) {
93 utils::Whf whf(awhf);
94
95 mRotInfo.src.format = whf.format;
Saurabh Shahe012f7a2012-08-18 15:11:57 -070096 mRotInfo.src.width = whf.w;
97 mRotInfo.src.height = whf.h;
Sushil Chauhan80fc1f92013-04-23 17:30:05 -070098}
Saurabh Shahe012f7a2012-08-18 15:11:57 -070099
Sushil Chauhan80fc1f92013-04-23 17:30:05 -0700100void MdssRot::setCrop(const utils::Dim& crop) {
Sushil Chauhan80fc1f92013-04-23 17:30:05 -0700101 mRotInfo.src_rect.x = crop.x;
102 mRotInfo.src_rect.y = crop.y;
103 mRotInfo.src_rect.w = crop.w;
104 mRotInfo.src_rect.h = crop.h;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700105}
106
Saurabh Shahc46cf9d2014-07-02 15:22:34 -0700107void MdssRot::setDownscale(int downscale) {
108 mDownscale = downscale;
Arun Kumar K.R7e5a1f82014-01-22 10:36:16 -0800109}
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800110
Saurabh Shahacf10202013-02-26 10:15:15 -0800111void MdssRot::setFlags(const utils::eMdpFlags& flags) {
Sushil Chauhan6dcffbf2013-05-13 19:11:11 -0700112 mRotInfo.flags = flags;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700113}
114
Saurabh Shahacf10202013-02-26 10:15:15 -0800115void MdssRot::setTransform(const utils::eTransform& rot)
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700116{
Saurabh Shah78ad4952013-04-05 09:38:01 -0700117 // reset rotation flags to avoid stale orientation values
118 mRotInfo.flags &= ~MDSS_ROT_MASK;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700119 int flags = utils::getMdpOrient(rot);
120 if (flags != -1)
121 setRotations(flags);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700122 mOrientation = static_cast<utils::eTransform>(flags);
123 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, flags);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800124}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700125
Saurabh Shahacf10202013-02-26 10:15:15 -0800126void MdssRot::doTransform() {
Sushil Chauhan6dcffbf2013-05-13 19:11:11 -0700127 mRotInfo.flags |= mOrientation;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700128 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
129 utils::swap(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h);
130}
131
132bool MdssRot::commit() {
Saurabh Shahc46cf9d2014-07-02 15:22:34 -0700133 Dim adjCrop(mRotInfo.src_rect.x,mRotInfo.src_rect.y,
134 mRotInfo.src_rect.w,mRotInfo.src_rect.h);
135 adjCrop = getFormatAdjustedCrop(adjCrop, mRotInfo.src.format,
136 mRotInfo.flags & utils::OV_MDP_DEINTERLACE);
137 adjCrop = getDownscaleAdjustedCrop(adjCrop, mDownscale);
138
139 mRotInfo.src_rect.x = adjCrop.x;
140 mRotInfo.src_rect.y = adjCrop.y;
141 mRotInfo.src_rect.w = adjCrop.w;
142 mRotInfo.src_rect.h = adjCrop.h;
Saurabh Shah8ec9b5e2014-06-30 14:37:17 -0700143
144 mRotInfo.dst_rect.x = 0;
145 mRotInfo.dst_rect.y = 0;
Saurabh Shahc46cf9d2014-07-02 15:22:34 -0700146 mRotInfo.dst_rect.w = mDownscale ?
147 mRotInfo.src_rect.w / mDownscale : mRotInfo.src_rect.w;
148 mRotInfo.dst_rect.h = mDownscale ?
149 mRotInfo.src_rect.h / mDownscale : mRotInfo.src_rect.h;
150 //Clear for next round
151 mDownscale = 0;
Saurabh Shah8ec9b5e2014-06-30 14:37:17 -0700152
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700153 doTransform();
Saurabh Shah8ec9b5e2014-06-30 14:37:17 -0700154
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700155 mRotInfo.flags |= MDSS_MDP_ROT_ONLY;
Saurabh Shahacf10202013-02-26 10:15:15 -0800156 mEnabled = true;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700157 if(!overlay::mdp_wrapper::setOverlay(mFd.getFD(), mRotInfo)) {
158 ALOGE("MdssRot commit failed!");
159 dump();
Saurabh Shahacf10202013-02-26 10:15:15 -0800160 return (mEnabled = false);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700161 }
162 mRotData.id = mRotInfo.id;
163 return true;
164}
165
166bool MdssRot::queueBuffer(int fd, uint32_t offset) {
167 if(enabled()) {
168 mRotData.data.memory_id = fd;
169 mRotData.data.offset = offset;
170
Saurabh Shah912c9482014-02-07 14:01:04 -0800171 if(false == remap(RotMem::ROT_NUM_BUFS)) {
172 ALOGE("%s Remap failed, not queuing", __FUNCTION__);
173 return false;
174 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700175
176 mRotData.dst_data.offset =
Saurabh Shah912c9482014-02-07 14:01:04 -0800177 mMem.mRotOffset[mMem.mCurrIndex];
178 mMem.mCurrIndex =
179 (mMem.mCurrIndex + 1) % mMem.mem.numBufs();
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700180
181 if(!overlay::mdp_wrapper::play(mFd.getFD(), mRotData)) {
182 ALOGE("MdssRot play failed!");
183 dump();
184 return false;
185 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700186 }
187 return true;
188}
189
190bool MdssRot::open_i(uint32_t numbufs, uint32_t bufsz)
191{
192 OvMem mem;
193 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700194 bool isSecure = mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700195
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700196 if(!mem.open(numbufs, bufsz, isSecure)){
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700197 ALOGE("%s: Failed to open", __func__);
198 mem.close();
199 return false;
200 }
201
202 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
203 OVASSERT(mem.getFD() != -1, "getFd is -1");
204
205 mRotData.dst_data.memory_id = mem.getFD();
206 mRotData.dst_data.offset = 0;
Saurabh Shah912c9482014-02-07 14:01:04 -0800207 mMem.mem = mem;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700208 return true;
209}
210
211bool MdssRot::remap(uint32_t numbufs) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800212 // Calculate the size based on rotator's dst format, w and h.
213 uint32_t opBufSize = calcOutputBufSize();
214 // If current size changed, remap
Saurabh Shah912c9482014-02-07 14:01:04 -0800215 if(opBufSize == mMem.size()) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800216 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, opBufSize);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700217 return true;
218 }
219
220 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700221
Saurabh Shah912c9482014-02-07 14:01:04 -0800222 if(!mMem.close()) {
223 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
224 return false;
225 }
226
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800227 if(!open_i(numbufs, opBufSize)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700228 ALOGE("%s Error could not open", __FUNCTION__);
229 return false;
230 }
Saurabh Shah912c9482014-02-07 14:01:04 -0800231
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700232 for (uint32_t i = 0; i < numbufs; ++i) {
Saurabh Shah912c9482014-02-07 14:01:04 -0800233 mMem.mRotOffset[i] = i * opBufSize;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700234 }
Saurabh Shah912c9482014-02-07 14:01:04 -0800235
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700236 return true;
237}
238
239bool MdssRot::close() {
240 bool success = true;
Ken Zhangba48b012012-12-11 20:33:59 -0500241 if(mFd.valid() && (getSessId() != (uint32_t) MSMFB_NEW_REQUEST)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700242 if(!mdp_wrapper::unsetOverlay(mFd.getFD(), getSessId())) {
243 ALOGE("MdssRot::close unsetOverlay failed, fd=%d sessId=%d",
244 mFd.getFD(), getSessId());
Ken Zhangba48b012012-12-11 20:33:59 -0500245 success = false;
246 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700247 }
248
249 if (!mFd.close()) {
250 ALOGE("Mdss Rot error closing fd");
251 success = false;
252 }
253 if (!mMem.close()) {
254 ALOGE("Mdss Rot error closing mem");
255 success = false;
256 }
257 reset();
258 return success;
259}
260
261void MdssRot::reset() {
262 ovutils::memset0(mRotInfo);
263 ovutils::memset0(mRotData);
264 mRotData.data.memory_id = -1;
265 mRotInfo.id = MSMFB_NEW_REQUEST;
Saurabh Shah912c9482014-02-07 14:01:04 -0800266 ovutils::memset0(mMem.mRotOffset);
267 mMem.mCurrIndex = 0;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700268 mOrientation = utils::OVERLAY_TRANSFORM_0;
Saurabh Shahc46cf9d2014-07-02 15:22:34 -0700269 mDownscale = 0;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700270}
271
272void MdssRot::dump() const {
273 ALOGE("== Dump MdssRot start ==");
274 mFd.dump();
Saurabh Shah912c9482014-02-07 14:01:04 -0800275 mMem.mem.dump();
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700276 mdp_wrapper::dump("mRotInfo", mRotInfo);
277 mdp_wrapper::dump("mRotData", mRotData);
278 ALOGE("== Dump MdssRot end ==");
279}
280
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800281uint32_t MdssRot::calcOutputBufSize() {
282 uint32_t opBufSize = 0;
283 ovutils::Whf destWhf(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h,
284 mRotInfo.src.format); //mdss src and dst formats are same.
Sushil Chauhanb4d184f2013-02-11 16:13:10 -0800285
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800286 if (mRotInfo.flags & ovutils::OV_MDSS_MDP_BWC_EN) {
Sushil Chauhan466766f2013-07-31 11:21:07 -0700287 opBufSize = calcCompressedBufSize(destWhf);
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800288 } else {
289 opBufSize = Rotator::calcOutputBufSize(destWhf);
290 }
Sushil Chauhanb4d184f2013-02-11 16:13:10 -0800291
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800292 return opBufSize;
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800293}
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800294
295void MdssRot::getDump(char *buf, size_t len) const {
Saurabh Shahae61b2b2013-04-10 16:37:25 -0700296 ovutils::getDump(buf, len, "MdssRotCtrl", mRotInfo);
297 ovutils::getDump(buf, len, "MdssRotData", mRotData);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800298}
299
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800300// Calculate the compressed o/p buffer size for BWC
Sushil Chauhan466766f2013-07-31 11:21:07 -0700301uint32_t MdssRot::calcCompressedBufSize(const ovutils::Whf& destWhf) {
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800302 uint32_t bufSize = 0;
Saurabh Shah4b54c5b2014-05-01 18:36:51 -0700303 //Worst case alignments
Sushil Chauhan466766f2013-07-31 11:21:07 -0700304 int aWidth = ovutils::align(destWhf.w, 64);
305 int aHeight = ovutils::align(destWhf.h, 4);
Saurabh Shah4b54c5b2014-05-01 18:36:51 -0700306 /*
307 Format | RAU size (width x height)
308 ----------------------------------------------
309 ARGB | 32 pixel x 4 line
310 RGB888 | 32 pixel x 4 line
311 Y (Luma) | 64 pixel x 4 line
312 CRCB 420 | 32 pixel x 2 line
313 CRCB 422 H2V1 | 32 pixel x 4 line
314 CRCB 422 H1V2 | 64 pixel x 2 line
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800315
Saurabh Shah4b54c5b2014-05-01 18:36:51 -0700316 Metadata requirements:-
317 1 byte meta data for every 8 RAUs
318 2 byte meta data per RAU
319 */
320
321 //These blocks attempt to allocate for the worst case in each of the
322 //respective format classes, yuv/rgb. The table above is for reference
323 if(utils::isYuv(destWhf.format)) {
324 int yRauCount = aWidth / 64; //Y
325 int cRauCount = aWidth / 32; //C
326 int yStride = (64 * 4 * yRauCount) + alignup(yRauCount, 8) / 8;
327 int cStride = ((32 * 2 * cRauCount) + alignup(cRauCount, 8) / 8) * 2;
328 int yStrideOffset = (aHeight / 4);
329 int cStrideOffset = (aHeight / 2);
330 bufSize = (yStride * yStrideOffset + cStride * cStrideOffset) +
331 (yRauCount * yStrideOffset * 2) +
332 (cRauCount * cStrideOffset * 2) * 2;
333 ALOGD_IF(DEBUG_MDSS_ROT, "%s:YUV Y RAU Count = %d C RAU Count = %d",
334 __FUNCTION__, yRauCount, cRauCount);
335 } else {
336 int rauCount = aWidth / 32;
337 //Single plane
338 int stride = (32 * 4 * rauCount) + alignup(rauCount, 8) / 8;
339 int strideOffset = (aHeight / 4);
340 bufSize = (stride * strideOffset * 4 /*bpp*/) +
341 (rauCount * strideOffset * 2);
342 ALOGD_IF(DEBUG_MDSS_ROT, "%s:RGB RAU count = %d", __FUNCTION__,
343 rauCount);
344 }
345
346 ALOGD_IF(DEBUG_MDSS_ROT, "%s: aligned width = %d, aligned height = %d "
347 "Buf Size = %d", __FUNCTION__, aWidth, aHeight, bufSize);
348
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800349 return bufSize;
350}
351
Saurabh Shahc46cf9d2014-07-02 15:22:34 -0700352int MdssRot::getDownscaleFactor(const int& srcW, const int& srcH,
353 const int& dstW, const int& dstH, const uint32_t& mdpFormat,
354 const bool& isInterlaced) {
355 if(not srcW or not srcH or not dstW or not dstH or isInterlaced) return 0;
356
357 Dim crop(0, 0, srcW, srcH);
358 Dim adjCrop = getFormatAdjustedCrop(crop, mdpFormat,
359 false /*isInterlaced */);
360
361 uint32_t downscale = min((adjCrop.w / dstW), (adjCrop.h / dstH));
362 //Reduced to a power of 2
363 downscale = (uint32_t) powf(2.0f, floorf(log2f((float)downscale)));
364
365 if(downscale < 2 or downscale > 32) return 0;
366
367 //Allow only 1 line or pixel to be chopped off since the source needs to
368 //be aligned to downscale. Progressively try with smaller downscale to see
369 //if we can satisfy the threshold
370 //For YUV the loop shouldnt be needed, unless in exceptional cases
371 Dim dsAdjCrop = getDownscaleAdjustedCrop(adjCrop, downscale);
372 while(downscale > 2 and (adjCrop.w > dsAdjCrop.w or
373 adjCrop.h > dsAdjCrop.h)) {
374 downscale /= 2;
375 dsAdjCrop = getDownscaleAdjustedCrop(adjCrop, downscale);
376 }
377
378 if(not dsAdjCrop.w or not dsAdjCrop.h) return 0;
379 return downscale;
380}
381
382Dim MdssRot::getFormatAdjustedCrop(const Dim& crop,
383 const uint32_t& mdpFormat, const bool& isInterlaced) {
384 Dim adjCrop = crop;
385 if (isYuv(mdpFormat)) {
386 normalizeCrop(adjCrop.x, adjCrop.w);
387 normalizeCrop(adjCrop.y, adjCrop.h);
388 // For interlaced, crop.h should be 4-aligned
389 if (isInterlaced and (adjCrop.h % 4))
390 adjCrop.h = aligndown(adjCrop.h, 4);
391 }
392 return adjCrop;
393}
394
395Dim MdssRot::getDownscaleAdjustedCrop(const Dim& crop,
396 const uint32_t& downscale) {
397 uint32_t alignedSrcW = aligndown(crop.w, downscale * 2);
398 uint32_t alignedSrcH = aligndown(crop.h, downscale * 2);
399 return Dim(crop.x, crop.y, alignedSrcW, alignedSrcH);
400}
401
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700402} // namespace overlay