blob: 5bf7dbc814ae04b30e325a74e4027895edca79ef [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{
Naseer Ahmedf48aef62012-07-20 09:05:53 -070083 int r = utils::getMdpOrient(rot);
Naseer Ahmedf48aef62012-07-20 09:05:53 -070084 setRotations(r);
Saurabh Shaha73738d2012-08-09 18:15:18 -070085 //getMdpOrient will switch the flips if the source is 90 rotated.
86 //Clients in Android dont factor in 90 rotation while deciding the flip.
87 mOrientation = static_cast<utils::eTransform>(r);
88 ALOGE_IF(DEBUG_OVERLAY, "%s: r=%d", __FUNCTION__, r);
89
Naseer Ahmedf48aef62012-07-20 09:05:53 -070090 setDisable();
91 if(rotUsed) {
92 setEnable();
93 }
94}
95
96void MdpRot::doTransform() {
97 switch(mOrientation) {
98 case utils::OVERLAY_TRANSFORM_ROT_90:
99 case utils::OVERLAY_TRANSFORM_ROT_90_FLIP_H:
100 case utils::OVERLAY_TRANSFORM_ROT_90_FLIP_V:
101 case utils::OVERLAY_TRANSFORM_ROT_270:
102 utils::swap(mRotImgInfo.dst.width, mRotImgInfo.dst.height);
103 break;
104 default:
105 break;
106 }
107}
108
109bool MdpRot::commit() {
110 doTransform();
111 if(!overlay::mdp_wrapper::startRotator(mFd.getFD(), mRotImgInfo)) {
112 ALOGE("MdpRot commit failed");
113 dump();
114 return false;
115 }
116 mRotDataInfo.session_id = mRotImgInfo.session_id;
117 return true;
118}
119
Naseer Ahmed29a26812012-06-14 00:56:20 -0700120bool MdpRot::open_i(uint32_t numbufs, uint32_t bufsz)
121{
122 OvMem mem;
123
124 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
125
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700126 if(!mem.open(numbufs, bufsz, mRotImgInfo.secure)){
Naseer Ahmed29a26812012-06-14 00:56:20 -0700127 ALOGE("%s: Failed to open", __func__);
128 mem.close();
129 return false;
130 }
131
132 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
133 OVASSERT(mem.getFD() != -1, "getFd is -1");
134
Naseer Ahmed29a26812012-06-14 00:56:20 -0700135 mRotDataInfo.dst.memory_id = mem.getFD();
136 mRotDataInfo.dst.offset = 0;
137 mMem.curr().m = mem;
138 return true;
139}
140
Naseer Ahmed29a26812012-06-14 00:56:20 -0700141bool MdpRot::close() {
142 bool success = true;
143 if(mFd.valid() && (getSessId() > 0)) {
144 if(!mdp_wrapper::endRotator(mFd.getFD(), getSessId())) {
145 ALOGE("Mdp Rot error endRotator, fd=%d sessId=%d",
146 mFd.getFD(), getSessId());
147 success = false;
148 }
149 }
150 if (!mFd.close()) {
151 ALOGE("Mdp Rot error closing fd");
152 success = false;
153 }
154 if (!mMem.close()) {
155 ALOGE("Mdp Rot error closing mem");
156 success = false;
157 }
158 reset();
159 return success;
160}
161
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700162bool MdpRot::remap(uint32_t numbufs) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700163 // if current size changed, remap
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700164 if(mBufSize == mMem.curr().size()) {
165 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, mBufSize);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700166 return true;
167 }
168
169 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
170 OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
171
Naseer Ahmed29a26812012-06-14 00:56:20 -0700172 // ++mMem will make curr to be prev, and prev will be curr
173 ++mMem;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700174 if(!open_i(numbufs, mBufSize)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700175 ALOGE("%s Error could not open", __FUNCTION__);
176 return false;
177 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700178 for (uint32_t i = 0; i < numbufs; ++i) {
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700179 mMem.curr().mRotOffset[i] = i * mBufSize;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700180 }
181 return true;
182}
183
Naseer Ahmed29a26812012-06-14 00:56:20 -0700184void MdpRot::reset() {
185 ovutils::memset0(mRotImgInfo);
186 ovutils::memset0(mRotDataInfo);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700187 ovutils::memset0(mMem.curr().mRotOffset);
188 ovutils::memset0(mMem.prev().mRotOffset);
189 mMem.curr().mCurrOffset = 0;
190 mMem.prev().mCurrOffset = 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700191 mBufSize = 0;
192 mOrientation = utils::OVERLAY_TRANSFORM_0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700193}
194
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700195bool MdpRot::queueBuffer(int fd, uint32_t offset) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700196 if(enabled()) {
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700197 mRotDataInfo.src.memory_id = fd;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700198 mRotDataInfo.src.offset = offset;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700199
200 remap(RotMem::Mem::ROT_NUM_BUFS);
201 OVASSERT(mMem.curr().m.numBufs(),
202 "queueBuffer numbufs is 0");
Naseer Ahmed29a26812012-06-14 00:56:20 -0700203 mRotDataInfo.dst.offset =
204 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
205 mMem.curr().mCurrOffset =
206 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700207
Naseer Ahmed29a26812012-06-14 00:56:20 -0700208 if(!overlay::mdp_wrapper::rotate(mFd.getFD(), mRotDataInfo)) {
209 ALOGE("MdpRot failed rotate");
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700210 dump();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700211 return false;
212 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700213
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700214 // if the prev mem is valid, we need to close
215 if(mMem.prev().valid()) {
216 // FIXME if no wait for vsync the above
217 // play will return immediatly and might cause
218 // tearing when prev.close is called.
219 if(!mMem.prev().close()) {
220 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
221 return false;
222 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700223 }
224 }
225 return true;
226}
227
Naseer Ahmed29a26812012-06-14 00:56:20 -0700228void MdpRot::dump() const {
229 ALOGE("== Dump MdpRot start ==");
230 mFd.dump();
231 mMem.curr().m.dump();
232 mdp_wrapper::dump("mRotImgInfo", mRotImgInfo);
233 mdp_wrapper::dump("mRotDataInfo", mRotDataInfo);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700234 ALOGE("== Dump MdpRot end ==");
235}
236}