blob: 5783dcb2ec151d3e8a418230a3f364fcf8d6872e [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 Shah8ec9b5e2014-06-30 14:37:17 -070066utils::Whf MdssRot::getDstWhf() const {
67 //For Mdss dst_rect itself represents buffer dimensions. We ignore actual
68 //aligned values during buffer allocation. Also the driver overwrites the
69 //src.format field if destination format is different.
70 //This implementation detail makes it possible to retrieve w,h even before
71 //buffer allocation, which happens in queueBuffer.
72 return utils::Whf(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h,
73 mRotInfo.src.format);
74}
75
76utils::Dim MdssRot::getDstDimensions() const {
77 return utils::Dim(mRotInfo.dst_rect.x, mRotInfo.dst_rect.y,
78 mRotInfo.dst_rect.w, mRotInfo.dst_rect.h);
79}
80
Saurabh Shahacf10202013-02-26 10:15:15 -080081uint32_t MdssRot::getSessId() const { return mRotInfo.id; }
Raj kamal23f69b22012-11-17 00:20:55 +053082
Saurabh Shahacf10202013-02-26 10:15:15 -080083bool MdssRot::init() {
Saurabh Shahe012f7a2012-08-18 15:11:57 -070084 if(!utils::openDev(mFd, 0, Res::fbPath, O_RDWR)) {
85 ALOGE("MdssRot failed to init fb0");
86 return false;
87 }
88 return true;
89}
90
91void MdssRot::setSource(const overlay::utils::Whf& awhf) {
92 utils::Whf whf(awhf);
93
94 mRotInfo.src.format = whf.format;
Saurabh Shahe012f7a2012-08-18 15:11:57 -070095 mRotInfo.src.width = whf.w;
96 mRotInfo.src.height = whf.h;
Sushil Chauhan80fc1f92013-04-23 17:30:05 -070097}
Saurabh Shahe012f7a2012-08-18 15:11:57 -070098
Sushil Chauhan80fc1f92013-04-23 17:30:05 -070099void MdssRot::setCrop(const utils::Dim& crop) {
Sushil Chauhan80fc1f92013-04-23 17:30:05 -0700100 mRotInfo.src_rect.x = crop.x;
101 mRotInfo.src_rect.y = crop.y;
102 mRotInfo.src_rect.w = crop.w;
103 mRotInfo.src_rect.h = crop.h;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700104}
105
Arun Kumar K.R7e5a1f82014-01-22 10:36:16 -0800106void MdssRot::setDownscale(int /*ds*/) {
107}
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800108
Saurabh Shahacf10202013-02-26 10:15:15 -0800109void MdssRot::setFlags(const utils::eMdpFlags& flags) {
Sushil Chauhan6dcffbf2013-05-13 19:11:11 -0700110 mRotInfo.flags = flags;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700111}
112
Saurabh Shahacf10202013-02-26 10:15:15 -0800113void MdssRot::setTransform(const utils::eTransform& rot)
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700114{
Saurabh Shah78ad4952013-04-05 09:38:01 -0700115 // reset rotation flags to avoid stale orientation values
116 mRotInfo.flags &= ~MDSS_ROT_MASK;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700117 int flags = utils::getMdpOrient(rot);
118 if (flags != -1)
119 setRotations(flags);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700120 mOrientation = static_cast<utils::eTransform>(flags);
121 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, flags);
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800122}
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700123
Saurabh Shahacf10202013-02-26 10:15:15 -0800124void MdssRot::doTransform() {
Sushil Chauhan6dcffbf2013-05-13 19:11:11 -0700125 mRotInfo.flags |= mOrientation;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700126 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
127 utils::swap(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h);
128}
129
130bool MdssRot::commit() {
Saurabh Shah8ec9b5e2014-06-30 14:37:17 -0700131 if (utils::isYuv(mRotInfo.src.format)) {
132 utils::normalizeCrop(mRotInfo.src_rect.x, mRotInfo.src_rect.w);
133 utils::normalizeCrop(mRotInfo.src_rect.y, mRotInfo.src_rect.h);
134 // For interlaced, crop.h should be 4-aligned
135 if ((mRotInfo.flags & utils::OV_MDP_DEINTERLACE) and
136 (mRotInfo.src_rect.h % 4))
137 mRotInfo.src_rect.h = utils::aligndown(mRotInfo.src_rect.h, 4);
138 }
139
140 mRotInfo.dst_rect.x = 0;
141 mRotInfo.dst_rect.y = 0;
142 mRotInfo.dst_rect.w = mRotInfo.src_rect.w;
143 mRotInfo.dst_rect.h = mRotInfo.src_rect.h;
144
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700145 doTransform();
Saurabh Shah8ec9b5e2014-06-30 14:37:17 -0700146
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700147 mRotInfo.flags |= MDSS_MDP_ROT_ONLY;
Saurabh Shahacf10202013-02-26 10:15:15 -0800148 mEnabled = true;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700149 if(!overlay::mdp_wrapper::setOverlay(mFd.getFD(), mRotInfo)) {
150 ALOGE("MdssRot commit failed!");
151 dump();
Saurabh Shahacf10202013-02-26 10:15:15 -0800152 return (mEnabled = false);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700153 }
154 mRotData.id = mRotInfo.id;
155 return true;
156}
157
158bool MdssRot::queueBuffer(int fd, uint32_t offset) {
159 if(enabled()) {
160 mRotData.data.memory_id = fd;
161 mRotData.data.offset = offset;
162
Saurabh Shah912c9482014-02-07 14:01:04 -0800163 if(false == remap(RotMem::ROT_NUM_BUFS)) {
164 ALOGE("%s Remap failed, not queuing", __FUNCTION__);
165 return false;
166 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700167
168 mRotData.dst_data.offset =
Saurabh Shah912c9482014-02-07 14:01:04 -0800169 mMem.mRotOffset[mMem.mCurrIndex];
170 mMem.mCurrIndex =
171 (mMem.mCurrIndex + 1) % mMem.mem.numBufs();
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700172
173 if(!overlay::mdp_wrapper::play(mFd.getFD(), mRotData)) {
174 ALOGE("MdssRot play failed!");
175 dump();
176 return false;
177 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700178 }
179 return true;
180}
181
182bool MdssRot::open_i(uint32_t numbufs, uint32_t bufsz)
183{
184 OvMem mem;
185 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700186 bool isSecure = mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700187
Sushil Chauhanfaae0422012-10-23 23:48:04 -0700188 if(!mem.open(numbufs, bufsz, isSecure)){
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700189 ALOGE("%s: Failed to open", __func__);
190 mem.close();
191 return false;
192 }
193
194 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
195 OVASSERT(mem.getFD() != -1, "getFd is -1");
196
197 mRotData.dst_data.memory_id = mem.getFD();
198 mRotData.dst_data.offset = 0;
Saurabh Shah912c9482014-02-07 14:01:04 -0800199 mMem.mem = mem;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700200 return true;
201}
202
203bool MdssRot::remap(uint32_t numbufs) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800204 // Calculate the size based on rotator's dst format, w and h.
205 uint32_t opBufSize = calcOutputBufSize();
206 // If current size changed, remap
Saurabh Shah912c9482014-02-07 14:01:04 -0800207 if(opBufSize == mMem.size()) {
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800208 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, opBufSize);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700209 return true;
210 }
211
212 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700213
Saurabh Shah912c9482014-02-07 14:01:04 -0800214 if(!mMem.close()) {
215 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
216 return false;
217 }
218
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800219 if(!open_i(numbufs, opBufSize)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700220 ALOGE("%s Error could not open", __FUNCTION__);
221 return false;
222 }
Saurabh Shah912c9482014-02-07 14:01:04 -0800223
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700224 for (uint32_t i = 0; i < numbufs; ++i) {
Saurabh Shah912c9482014-02-07 14:01:04 -0800225 mMem.mRotOffset[i] = i * opBufSize;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700226 }
Saurabh Shah912c9482014-02-07 14:01:04 -0800227
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700228 return true;
229}
230
231bool MdssRot::close() {
232 bool success = true;
Ken Zhangba48b012012-12-11 20:33:59 -0500233 if(mFd.valid() && (getSessId() != (uint32_t) MSMFB_NEW_REQUEST)) {
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700234 if(!mdp_wrapper::unsetOverlay(mFd.getFD(), getSessId())) {
235 ALOGE("MdssRot::close unsetOverlay failed, fd=%d sessId=%d",
236 mFd.getFD(), getSessId());
Ken Zhangba48b012012-12-11 20:33:59 -0500237 success = false;
238 }
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700239 }
240
241 if (!mFd.close()) {
242 ALOGE("Mdss Rot error closing fd");
243 success = false;
244 }
245 if (!mMem.close()) {
246 ALOGE("Mdss Rot error closing mem");
247 success = false;
248 }
249 reset();
250 return success;
251}
252
253void MdssRot::reset() {
254 ovutils::memset0(mRotInfo);
255 ovutils::memset0(mRotData);
256 mRotData.data.memory_id = -1;
257 mRotInfo.id = MSMFB_NEW_REQUEST;
Saurabh Shah912c9482014-02-07 14:01:04 -0800258 ovutils::memset0(mMem.mRotOffset);
259 mMem.mCurrIndex = 0;
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700260 mOrientation = utils::OVERLAY_TRANSFORM_0;
261}
262
263void MdssRot::dump() const {
264 ALOGE("== Dump MdssRot start ==");
265 mFd.dump();
Saurabh Shah912c9482014-02-07 14:01:04 -0800266 mMem.mem.dump();
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700267 mdp_wrapper::dump("mRotInfo", mRotInfo);
268 mdp_wrapper::dump("mRotData", mRotData);
269 ALOGE("== Dump MdssRot end ==");
270}
271
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800272uint32_t MdssRot::calcOutputBufSize() {
273 uint32_t opBufSize = 0;
274 ovutils::Whf destWhf(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h,
275 mRotInfo.src.format); //mdss src and dst formats are same.
Sushil Chauhanb4d184f2013-02-11 16:13:10 -0800276
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800277 if (mRotInfo.flags & ovutils::OV_MDSS_MDP_BWC_EN) {
Sushil Chauhan466766f2013-07-31 11:21:07 -0700278 opBufSize = calcCompressedBufSize(destWhf);
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800279 } else {
280 opBufSize = Rotator::calcOutputBufSize(destWhf);
281 }
Sushil Chauhanb4d184f2013-02-11 16:13:10 -0800282
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800283 return opBufSize;
Sushil Chauhanfbffad72012-11-06 13:05:42 -0800284}
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800285
286void MdssRot::getDump(char *buf, size_t len) const {
Saurabh Shahae61b2b2013-04-10 16:37:25 -0700287 ovutils::getDump(buf, len, "MdssRotCtrl", mRotInfo);
288 ovutils::getDump(buf, len, "MdssRotData", mRotData);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800289}
290
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800291// Calculate the compressed o/p buffer size for BWC
Sushil Chauhan466766f2013-07-31 11:21:07 -0700292uint32_t MdssRot::calcCompressedBufSize(const ovutils::Whf& destWhf) {
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800293 uint32_t bufSize = 0;
Saurabh Shah4b54c5b2014-05-01 18:36:51 -0700294 //Worst case alignments
Sushil Chauhan466766f2013-07-31 11:21:07 -0700295 int aWidth = ovutils::align(destWhf.w, 64);
296 int aHeight = ovutils::align(destWhf.h, 4);
Saurabh Shah4b54c5b2014-05-01 18:36:51 -0700297 /*
298 Format | RAU size (width x height)
299 ----------------------------------------------
300 ARGB | 32 pixel x 4 line
301 RGB888 | 32 pixel x 4 line
302 Y (Luma) | 64 pixel x 4 line
303 CRCB 420 | 32 pixel x 2 line
304 CRCB 422 H2V1 | 32 pixel x 4 line
305 CRCB 422 H1V2 | 64 pixel x 2 line
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800306
Saurabh Shah4b54c5b2014-05-01 18:36:51 -0700307 Metadata requirements:-
308 1 byte meta data for every 8 RAUs
309 2 byte meta data per RAU
310 */
311
312 //These blocks attempt to allocate for the worst case in each of the
313 //respective format classes, yuv/rgb. The table above is for reference
314 if(utils::isYuv(destWhf.format)) {
315 int yRauCount = aWidth / 64; //Y
316 int cRauCount = aWidth / 32; //C
317 int yStride = (64 * 4 * yRauCount) + alignup(yRauCount, 8) / 8;
318 int cStride = ((32 * 2 * cRauCount) + alignup(cRauCount, 8) / 8) * 2;
319 int yStrideOffset = (aHeight / 4);
320 int cStrideOffset = (aHeight / 2);
321 bufSize = (yStride * yStrideOffset + cStride * cStrideOffset) +
322 (yRauCount * yStrideOffset * 2) +
323 (cRauCount * cStrideOffset * 2) * 2;
324 ALOGD_IF(DEBUG_MDSS_ROT, "%s:YUV Y RAU Count = %d C RAU Count = %d",
325 __FUNCTION__, yRauCount, cRauCount);
326 } else {
327 int rauCount = aWidth / 32;
328 //Single plane
329 int stride = (32 * 4 * rauCount) + alignup(rauCount, 8) / 8;
330 int strideOffset = (aHeight / 4);
331 bufSize = (stride * strideOffset * 4 /*bpp*/) +
332 (rauCount * strideOffset * 2);
333 ALOGD_IF(DEBUG_MDSS_ROT, "%s:RGB RAU count = %d", __FUNCTION__,
334 rauCount);
335 }
336
337 ALOGD_IF(DEBUG_MDSS_ROT, "%s: aligned width = %d, aligned height = %d "
338 "Buf Size = %d", __FUNCTION__, aWidth, aHeight, bufSize);
339
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800340 return bufSize;
341}
342
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700343} // namespace overlay