blob: f7bc87a7313071dbef4af4cf620e9b45f467c128 [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
20#include "overlayUtils.h"
21#include "overlayRotator.h"
22
Sushil Chauhanbab187a2013-01-30 17:44:15 -080023#define DEBUG_MDSS_ROT 0
24
Sushil Chauhanc6bd6d92012-12-12 12:33:01 -080025#ifdef VENUS_COLOR_FORMAT
26#include <media/msm_media_info.h>
27#else
28#define VENUS_BUFFER_SIZE(args...) 0
29#endif
30
Naseer Ahmedc21013b2012-11-19 12:44:41 -050031#ifndef MDSS_MDP_ROT_ONLY
32#define MDSS_MDP_ROT_ONLY 0x80
33#endif
34
Sushil Chauhan95e4c9f2013-01-14 18:44:14 -080035#define MDSS_ROT_MASK (MDP_ROT_90 | MDP_FLIP_UD | MDP_FLIP_LR)
Sushil Chauhan6e3fab82012-11-19 15:30:33 -080036
Saurabh Shahe012f7a2012-08-18 15:11:57 -070037namespace ovutils = overlay::utils;
38
39namespace overlay {
Saurabh Shah4b54c5b2014-05-01 18:36:51 -070040using namespace utils;
41
Naseer Ahmed758bfc52012-11-28 17:02:08 -050042MdssRot::MdssRot() {
43 reset();
44 init();
45}
46
47MdssRot::~MdssRot() { close(); }
48
Saurabh Shahacf10202013-02-26 10:15:15 -080049bool MdssRot::enabled() const { return mEnabled; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050050
Saurabh Shahacf10202013-02-26 10:15:15 -080051void MdssRot::setRotations(uint32_t flags) { mRotInfo.flags |= flags; }
Naseer Ahmed758bfc52012-11-28 17:02:08 -050052
Saurabh Shahacf10202013-02-26 10:15:15 -080053int MdssRot::getDstMemId() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050054 return mRotData.dst_data.memory_id;
55}
56
Saurabh Shahacf10202013-02-26 10:15:15 -080057uint32_t MdssRot::getDstOffset() const {
Naseer Ahmed758bfc52012-11-28 17:02:08 -050058 return mRotData.dst_data.offset;
59}
60
Saurabh Shahacf10202013-02-26 10:15:15 -080061uint32_t MdssRot::getDstFormat() const {
Raj kamal23f69b22012-11-17 00:20:55 +053062 //For mdss src and dst formats are same
63 return mRotInfo.src.format;
64}
Naseer Ahmed758bfc52012-11-28 17:02:08 -050065
Saurabh Shahacf10202013-02-26 10:15:15 -080066uint32_t MdssRot::getSessId() const { return mRotInfo.id; }
Raj kamal23f69b22012-11-17 00:20:55 +053067
Saurabh Shahacf10202013-02-26 10:15:15 -080068bool MdssRot::init() {
Saurabh Shahe012f7a2012-08-18 15:11:57 -070069 if(!utils::openDev(mFd, 0, Res::fbPath, O_RDWR)) {
70 ALOGE("MdssRot failed to init fb0");
71 return false;
72 }
73 return true;
74}
75
76void MdssRot::setSource(const overlay::utils::Whf& awhf) {
77 utils::Whf whf(awhf);
78
79 mRotInfo.src.format = whf.format;
Saurabh Shahe012f7a2012-08-18 15:11:57 -070080 mRotInfo.src.width = whf.w;
81 mRotInfo.src.height = whf.h;
Sushil Chauhan80fc1f92013-04-23 17:30:05 -070082}
Saurabh Shahe012f7a2012-08-18 15:11:57 -070083
Sushil Chauhan80fc1f92013-04-23 17:30:05 -070084void MdssRot::setCrop(const utils::Dim& crop) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -070085
Sushil Chauhan80fc1f92013-04-23 17:30:05 -070086 mRotInfo.src_rect.x = crop.x;
87 mRotInfo.src_rect.y = crop.y;
88 mRotInfo.src_rect.w = crop.w;
89 mRotInfo.src_rect.h = crop.h;
90
91 mRotInfo.dst_rect.x = 0;
92 mRotInfo.dst_rect.y = 0;
93 mRotInfo.dst_rect.w = crop.w;
94 mRotInfo.dst_rect.h = crop.h;
Saurabh Shahe012f7a2012-08-18 15:11:57 -070095}
96
Arun Kumar K.R7e5a1f82014-01-22 10:36:16 -080097void MdssRot::setDownscale(int /*ds*/) {
98}
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -080099
Saurabh Shahacf10202013-02-26 10:15:15 -0800100void MdssRot::setFlags(const utils::eMdpFlags& flags) {
Sushil Chauhan6dcffbf2013-05-13 19:11:11 -0700101 mRotInfo.flags = flags;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700102}
103
Saurabh Shahacf10202013-02-26 10:15:15 -0800104void MdssRot::setTransform(const utils::eTransform& rot)
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700105{
Saurabh Shah78ad4952013-04-05 09:38:01 -0700106 // reset rotation flags to avoid stale orientation values
107 mRotInfo.flags &= ~MDSS_ROT_MASK;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700108 int flags = utils::getMdpOrient(rot);
109 if (flags != -1)
110 setRotations(flags);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700111 mOrientation = static_cast<utils::eTransform>(flags);
112 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, flags);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800113}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700114
Saurabh Shahacf10202013-02-26 10:15:15 -0800115void MdssRot::doTransform() {
Sushil Chauhan6dcffbf2013-05-13 19:11:11 -0700116 mRotInfo.flags |= mOrientation;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700117 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
118 utils::swap(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h);
119}
120
121bool MdssRot::commit() {
122 doTransform();
123 mRotInfo.flags |= MDSS_MDP_ROT_ONLY;
Saurabh Shahacf10202013-02-26 10:15:15 -0800124 mEnabled = true;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700125 if(!overlay::mdp_wrapper::setOverlay(mFd.getFD(), mRotInfo)) {
126 ALOGE("MdssRot commit failed!");
127 dump();
Saurabh Shahacf10202013-02-26 10:15:15 -0800128 return (mEnabled = false);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700129 }
130 mRotData.id = mRotInfo.id;
131 return true;
132}
133
134bool MdssRot::queueBuffer(int fd, uint32_t offset) {
135 if(enabled()) {
136 mRotData.data.memory_id = fd;
137 mRotData.data.offset = offset;
138
Saurabh Shah912c9482014-02-07 14:01:04 -0800139 if(false == remap(RotMem::ROT_NUM_BUFS)) {
140 ALOGE("%s Remap failed, not queuing", __FUNCTION__);
141 return false;
142 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700143
144 mRotData.dst_data.offset =
Saurabh Shah912c9482014-02-07 14:01:04 -0800145 mMem.mRotOffset[mMem.mCurrIndex];
146 mMem.mCurrIndex =
147 (mMem.mCurrIndex + 1) % mMem.mem.numBufs();
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700148
149 if(!overlay::mdp_wrapper::play(mFd.getFD(), mRotData)) {
150 ALOGE("MdssRot play failed!");
151 dump();
152 return false;
153 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700154 }
155 return true;
156}
157
158bool MdssRot::open_i(uint32_t numbufs, uint32_t bufsz)
159{
160 OvMem mem;
161 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700162 bool isSecure = mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700163
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700164 if(!mem.open(numbufs, bufsz, isSecure)){
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700165 ALOGE("%s: Failed to open", __func__);
166 mem.close();
167 return false;
168 }
169
170 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
171 OVASSERT(mem.getFD() != -1, "getFd is -1");
172
173 mRotData.dst_data.memory_id = mem.getFD();
174 mRotData.dst_data.offset = 0;
Saurabh Shah912c9482014-02-07 14:01:04 -0800175 mMem.mem = mem;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700176 return true;
177}
178
179bool MdssRot::remap(uint32_t numbufs) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800180 // Calculate the size based on rotator's dst format, w and h.
181 uint32_t opBufSize = calcOutputBufSize();
182 // If current size changed, remap
Saurabh Shah912c9482014-02-07 14:01:04 -0800183 if(opBufSize == mMem.size()) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800184 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, opBufSize);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700185 return true;
186 }
187
188 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700189
Saurabh Shah912c9482014-02-07 14:01:04 -0800190 if(!mMem.close()) {
191 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
192 return false;
193 }
194
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800195 if(!open_i(numbufs, opBufSize)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700196 ALOGE("%s Error could not open", __FUNCTION__);
197 return false;
198 }
Saurabh Shah912c9482014-02-07 14:01:04 -0800199
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700200 for (uint32_t i = 0; i < numbufs; ++i) {
Saurabh Shah912c9482014-02-07 14:01:04 -0800201 mMem.mRotOffset[i] = i * opBufSize;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700202 }
Saurabh Shah912c9482014-02-07 14:01:04 -0800203
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700204 return true;
205}
206
207bool MdssRot::close() {
208 bool success = true;
Ken Zhangba48b012012-12-11 20:33:59 -0500209 if(mFd.valid() && (getSessId() != (uint32_t) MSMFB_NEW_REQUEST)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700210 if(!mdp_wrapper::unsetOverlay(mFd.getFD(), getSessId())) {
211 ALOGE("MdssRot::close unsetOverlay failed, fd=%d sessId=%d",
212 mFd.getFD(), getSessId());
Ken Zhangba48b012012-12-11 20:33:59 -0500213 success = false;
214 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700215 }
216
217 if (!mFd.close()) {
218 ALOGE("Mdss Rot error closing fd");
219 success = false;
220 }
221 if (!mMem.close()) {
222 ALOGE("Mdss Rot error closing mem");
223 success = false;
224 }
225 reset();
226 return success;
227}
228
229void MdssRot::reset() {
230 ovutils::memset0(mRotInfo);
231 ovutils::memset0(mRotData);
232 mRotData.data.memory_id = -1;
233 mRotInfo.id = MSMFB_NEW_REQUEST;
Saurabh Shah912c9482014-02-07 14:01:04 -0800234 ovutils::memset0(mMem.mRotOffset);
235 mMem.mCurrIndex = 0;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700236 mOrientation = utils::OVERLAY_TRANSFORM_0;
237}
238
239void MdssRot::dump() const {
240 ALOGE("== Dump MdssRot start ==");
241 mFd.dump();
Saurabh Shah912c9482014-02-07 14:01:04 -0800242 mMem.mem.dump();
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700243 mdp_wrapper::dump("mRotInfo", mRotInfo);
244 mdp_wrapper::dump("mRotData", mRotData);
245 ALOGE("== Dump MdssRot end ==");
246}
247
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800248uint32_t MdssRot::calcOutputBufSize() {
249 uint32_t opBufSize = 0;
250 ovutils::Whf destWhf(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h,
251 mRotInfo.src.format); //mdss src and dst formats are same.
Sushil Chauhanb4d184f2013-02-11 16:13:10 -0800252
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800253 if (mRotInfo.flags & ovutils::OV_MDSS_MDP_BWC_EN) {
Sushil Chauhan466766f2013-07-31 11:21:07 -0700254 opBufSize = calcCompressedBufSize(destWhf);
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800255 } else {
256 opBufSize = Rotator::calcOutputBufSize(destWhf);
257 }
Sushil Chauhanb4d184f2013-02-11 16:13:10 -0800258
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800259 return opBufSize;
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800260}
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800261
262void MdssRot::getDump(char *buf, size_t len) const {
Saurabh Shahae61b2b2013-04-10 16:37:25 -0700263 ovutils::getDump(buf, len, "MdssRotCtrl", mRotInfo);
264 ovutils::getDump(buf, len, "MdssRotData", mRotData);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800265}
266
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800267// Calculate the compressed o/p buffer size for BWC
Sushil Chauhan466766f2013-07-31 11:21:07 -0700268uint32_t MdssRot::calcCompressedBufSize(const ovutils::Whf& destWhf) {
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800269 uint32_t bufSize = 0;
Saurabh Shah4b54c5b2014-05-01 18:36:51 -0700270 //Worst case alignments
Sushil Chauhan466766f2013-07-31 11:21:07 -0700271 int aWidth = ovutils::align(destWhf.w, 64);
272 int aHeight = ovutils::align(destWhf.h, 4);
Saurabh Shah4b54c5b2014-05-01 18:36:51 -0700273 /*
274 Format | RAU size (width x height)
275 ----------------------------------------------
276 ARGB | 32 pixel x 4 line
277 RGB888 | 32 pixel x 4 line
278 Y (Luma) | 64 pixel x 4 line
279 CRCB 420 | 32 pixel x 2 line
280 CRCB 422 H2V1 | 32 pixel x 4 line
281 CRCB 422 H1V2 | 64 pixel x 2 line
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800282
Saurabh Shah4b54c5b2014-05-01 18:36:51 -0700283 Metadata requirements:-
284 1 byte meta data for every 8 RAUs
285 2 byte meta data per RAU
286 */
287
288 //These blocks attempt to allocate for the worst case in each of the
289 //respective format classes, yuv/rgb. The table above is for reference
290 if(utils::isYuv(destWhf.format)) {
291 int yRauCount = aWidth / 64; //Y
292 int cRauCount = aWidth / 32; //C
293 int yStride = (64 * 4 * yRauCount) + alignup(yRauCount, 8) / 8;
294 int cStride = ((32 * 2 * cRauCount) + alignup(cRauCount, 8) / 8) * 2;
295 int yStrideOffset = (aHeight / 4);
296 int cStrideOffset = (aHeight / 2);
297 bufSize = (yStride * yStrideOffset + cStride * cStrideOffset) +
298 (yRauCount * yStrideOffset * 2) +
299 (cRauCount * cStrideOffset * 2) * 2;
300 ALOGD_IF(DEBUG_MDSS_ROT, "%s:YUV Y RAU Count = %d C RAU Count = %d",
301 __FUNCTION__, yRauCount, cRauCount);
302 } else {
303 int rauCount = aWidth / 32;
304 //Single plane
305 int stride = (32 * 4 * rauCount) + alignup(rauCount, 8) / 8;
306 int strideOffset = (aHeight / 4);
307 bufSize = (stride * strideOffset * 4 /*bpp*/) +
308 (rauCount * strideOffset * 2);
309 ALOGD_IF(DEBUG_MDSS_ROT, "%s:RGB RAU count = %d", __FUNCTION__,
310 rauCount);
311 }
312
313 ALOGD_IF(DEBUG_MDSS_ROT, "%s: aligned width = %d, aligned height = %d "
314 "Buf Size = %d", __FUNCTION__, aWidth, aHeight, bufSize);
315
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800316 return bufSize;
317}
318
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700319} // namespace overlay