blob: 306e28f36aec7ace1b53564b87a0b45e6e45edce [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
2* Copyright (C) 2008 The Android Open Source Project
3* Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved.
4*
5* Licensed under the Apache License, Version 2.0 (the "License");
6* you may not use this file except in compliance with the License.
7* You may obtain a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS,
13* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14* See the License for the specific language governing permissions and
15* limitations under the License.
16*/
17
18#include "overlayRotator.h"
19#include "overlayUtils.h"
Naseer Ahmed29a26812012-06-14 00:56:20 -070020
21namespace ovutils = overlay::utils;
22
23namespace overlay {
24
Naseer Ahmedf48aef62012-07-20 09:05:53 -070025int IRotatorHw::getRotatorHwType() {
26 //TODO figure out based on ioctl
27 return TYPE_MDP;
Naseer Ahmed29a26812012-06-14 00:56:20 -070028}
29
Naseer Ahmedf48aef62012-07-20 09:05:53 -070030bool RotMem::close() {
31 bool ret = true;
32 for(uint32_t i=0; i < RotMem::MAX_ROT_MEM; ++i) {
33 // skip current, and if valid, close
34 if(m[i].valid()) {
35 if(m[i].close() == false) {
36 ALOGE("%s error in closing rot mem %d", __FUNCTION__, i);
37 ret = false;
38 }
39 }
Naseer Ahmed29a26812012-06-14 00:56:20 -070040 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -070041 return ret;
Naseer Ahmed29a26812012-06-14 00:56:20 -070042}
43
Naseer Ahmedf48aef62012-07-20 09:05:53 -070044bool MdpRot::init()
Naseer Ahmed29a26812012-06-14 00:56:20 -070045{
46 if(!mFd.open(Res::rotPath, O_RDWR)){
Naseer Ahmedf48aef62012-07-20 09:05:53 -070047 ALOGE("MdpRot failed to init %s", Res::rotPath);
Naseer Ahmed29a26812012-06-14 00:56:20 -070048 return false;
49 }
50 return true;
51}
52
Naseer Ahmedf48aef62012-07-20 09:05:53 -070053void MdpRot::setSource(const overlay::utils::Whf& awhf) {
54 utils::Whf whf(awhf);
55
56 mRotImgInfo.src.format = whf.format;
57 if(whf.format == MDP_Y_CRCB_H2V2_TILE ||
58 whf.format == MDP_Y_CBCR_H2V2_TILE) {
59 whf.w = utils::alignup(awhf.w, 64);
60 whf.h = utils::alignup(awhf.h, 32);
61 }
62
63 mRotImgInfo.src.width = whf.w;
64 mRotImgInfo.src.height = whf.h;
65
66 mRotImgInfo.src_rect.w = whf.w;
67 mRotImgInfo.src_rect.h = whf.h;
68
69 mRotImgInfo.dst.width = whf.w;
70 mRotImgInfo.dst.height = whf.h;
71
72 mBufSize = awhf.size;
73}
74
75void MdpRot::setFlags(const utils::eMdpFlags& flags) {
76 mRotImgInfo.secure = 0;
77 if(flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
78 mRotImgInfo.secure = 1;
79}
80
81void MdpRot::setTransform(const utils::eTransform& rot, const bool& rotUsed)
82{
83 mOrientation = rot;
84 int r = utils::getMdpOrient(rot);
85 ALOGE_IF(DEBUG_OVERLAY, "%s: r=%d", __FUNCTION__, r);
86 setRotations(r);
87 setDisable();
88 if(rotUsed) {
89 setEnable();
90 }
91}
92
93void MdpRot::doTransform() {
94 switch(mOrientation) {
95 case utils::OVERLAY_TRANSFORM_ROT_90:
96 case utils::OVERLAY_TRANSFORM_ROT_90_FLIP_H:
97 case utils::OVERLAY_TRANSFORM_ROT_90_FLIP_V:
98 case utils::OVERLAY_TRANSFORM_ROT_270:
99 utils::swap(mRotImgInfo.dst.width, mRotImgInfo.dst.height);
100 break;
101 default:
102 break;
103 }
104}
105
106bool MdpRot::commit() {
107 doTransform();
108 if(!overlay::mdp_wrapper::startRotator(mFd.getFD(), mRotImgInfo)) {
109 ALOGE("MdpRot commit failed");
110 dump();
111 return false;
112 }
113 mRotDataInfo.session_id = mRotImgInfo.session_id;
114 return true;
115}
116
Naseer Ahmed29a26812012-06-14 00:56:20 -0700117bool MdpRot::open_i(uint32_t numbufs, uint32_t bufsz)
118{
119 OvMem mem;
120
121 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
122
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700123 if(!mem.open(numbufs, bufsz, mRotImgInfo.secure)){
Naseer Ahmed29a26812012-06-14 00:56:20 -0700124 ALOGE("%s: Failed to open", __func__);
125 mem.close();
126 return false;
127 }
128
129 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
130 OVASSERT(mem.getFD() != -1, "getFd is -1");
131
Naseer Ahmed29a26812012-06-14 00:56:20 -0700132 mRotDataInfo.dst.memory_id = mem.getFD();
133 mRotDataInfo.dst.offset = 0;
134 mMem.curr().m = mem;
135 return true;
136}
137
Naseer Ahmed29a26812012-06-14 00:56:20 -0700138bool MdpRot::close() {
139 bool success = true;
140 if(mFd.valid() && (getSessId() > 0)) {
141 if(!mdp_wrapper::endRotator(mFd.getFD(), getSessId())) {
142 ALOGE("Mdp Rot error endRotator, fd=%d sessId=%d",
143 mFd.getFD(), getSessId());
144 success = false;
145 }
146 }
147 if (!mFd.close()) {
148 ALOGE("Mdp Rot error closing fd");
149 success = false;
150 }
151 if (!mMem.close()) {
152 ALOGE("Mdp Rot error closing mem");
153 success = false;
154 }
155 reset();
156 return success;
157}
158
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700159bool MdpRot::remap(uint32_t numbufs) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700160 // if current size changed, remap
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700161 if(mBufSize == mMem.curr().size()) {
162 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, mBufSize);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700163 return true;
164 }
165
166 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
167 OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
168
Naseer Ahmed29a26812012-06-14 00:56:20 -0700169 // ++mMem will make curr to be prev, and prev will be curr
170 ++mMem;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700171 if(!open_i(numbufs, mBufSize)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700172 ALOGE("%s Error could not open", __FUNCTION__);
173 return false;
174 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700175 for (uint32_t i = 0; i < numbufs; ++i) {
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700176 mMem.curr().mRotOffset[i] = i * mBufSize;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700177 }
178 return true;
179}
180
Naseer Ahmed29a26812012-06-14 00:56:20 -0700181void MdpRot::reset() {
182 ovutils::memset0(mRotImgInfo);
183 ovutils::memset0(mRotDataInfo);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700184 ovutils::memset0(mMem.curr().mRotOffset);
185 ovutils::memset0(mMem.prev().mRotOffset);
186 mMem.curr().mCurrOffset = 0;
187 mMem.prev().mCurrOffset = 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700188 mBufSize = 0;
189 mOrientation = utils::OVERLAY_TRANSFORM_0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700190}
191
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700192bool MdpRot::queueBuffer(int fd, uint32_t offset) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700193 if(enabled()) {
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700194 mRotDataInfo.src.memory_id = fd;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700195 mRotDataInfo.src.offset = offset;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700196
197 remap(RotMem::Mem::ROT_NUM_BUFS);
198 OVASSERT(mMem.curr().m.numBufs(),
199 "queueBuffer numbufs is 0");
Naseer Ahmed29a26812012-06-14 00:56:20 -0700200 mRotDataInfo.dst.offset =
201 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
202 mMem.curr().mCurrOffset =
203 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700204
Naseer Ahmed29a26812012-06-14 00:56:20 -0700205 if(!overlay::mdp_wrapper::rotate(mFd.getFD(), mRotDataInfo)) {
206 ALOGE("MdpRot failed rotate");
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700207 dump();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700208 return false;
209 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700210
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700211 // if the prev mem is valid, we need to close
212 if(mMem.prev().valid()) {
213 // FIXME if no wait for vsync the above
214 // play will return immediatly and might cause
215 // tearing when prev.close is called.
216 if(!mMem.prev().close()) {
217 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
218 return false;
219 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700220 }
221 }
222 return true;
223}
224
Naseer Ahmed29a26812012-06-14 00:56:20 -0700225void MdpRot::dump() const {
226 ALOGE("== Dump MdpRot start ==");
227 mFd.dump();
228 mMem.curr().m.dump();
229 mdp_wrapper::dump("mRotImgInfo", mRotImgInfo);
230 mdp_wrapper::dump("mRotDataInfo", mRotDataInfo);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700231 ALOGE("== Dump MdpRot end ==");
232}
233}