blob: 16b866af53d7f1856ab0cf5980e364e8f0128223 [file] [log] [blame]
Saurabh Shahe012f7a2012-08-18 15:11:57 -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 "overlayUtils.h"
19#include "overlayRotator.h"
20
21namespace ovutils = overlay::utils;
22
23namespace overlay {
24
25bool MdssRot::init() {
26 if(!utils::openDev(mFd, 0, Res::fbPath, O_RDWR)) {
27 ALOGE("MdssRot failed to init fb0");
28 return false;
29 }
30 return true;
31}
32
33void MdssRot::setSource(const overlay::utils::Whf& awhf) {
34 utils::Whf whf(awhf);
35
36 mRotInfo.src.format = whf.format;
37 if(whf.format == MDP_Y_CRCB_H2V2_TILE ||
38 whf.format == MDP_Y_CBCR_H2V2_TILE) {
39 whf.w = utils::alignup(awhf.w, 64);
40 whf.h = utils::alignup(awhf.h, 32);
41 }
42
43 mRotInfo.src.width = whf.w;
44 mRotInfo.src.height = whf.h;
45
46 mRotInfo.src_rect.w = whf.w;
47 mRotInfo.src_rect.h = whf.h;
48
49 mRotInfo.dst_rect.w = whf.w;
50 mRotInfo.dst_rect.h = whf.h;
51
52 mBufSize = awhf.size;
53}
54
55void MdssRot::setFlags(const utils::eMdpFlags& flags) {
56 // TODO
57}
58
59void MdssRot::setTransform(const utils::eTransform& rot, const bool& rotUsed)
60{
61 int flags = utils::getMdpOrient(rot);
62 if (flags != -1)
63 setRotations(flags);
64 //getMdpOrient will switch the flips if the source is 90 rotated.
65 //Clients in Android dont factor in 90 rotation while deciding the flip.
66 mOrientation = static_cast<utils::eTransform>(flags);
67 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, flags);
68
69 setDisable();
70 if(rotUsed) {
71 setEnable();
72 }
73}
74
75void MdssRot::doTransform() {
76 if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
77 utils::swap(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h);
78}
79
80bool MdssRot::commit() {
81 doTransform();
82 mRotInfo.flags |= MDSS_MDP_ROT_ONLY;
83 if(!overlay::mdp_wrapper::setOverlay(mFd.getFD(), mRotInfo)) {
84 ALOGE("MdssRot commit failed!");
85 dump();
86 return false;
87 }
88 mRotData.id = mRotInfo.id;
89 return true;
90}
91
92bool MdssRot::queueBuffer(int fd, uint32_t offset) {
93 if(enabled()) {
94 mRotData.data.memory_id = fd;
95 mRotData.data.offset = offset;
96
97 remap(RotMem::Mem::ROT_NUM_BUFS);
98 OVASSERT(mMem.curr().m.numBufs(), "queueBuffer numbufs is 0");
99
100 mRotData.dst_data.offset =
101 mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
102 mMem.curr().mCurrOffset =
103 (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
104
105 if(!overlay::mdp_wrapper::play(mFd.getFD(), mRotData)) {
106 ALOGE("MdssRot play failed!");
107 dump();
108 return false;
109 }
110
111 // if the prev mem is valid, we need to close
112 if(mMem.prev().valid()) {
113 // FIXME if no wait for vsync the above
114 // play will return immediatly and might cause
115 // tearing when prev.close is called.
116 if(!mMem.prev().close()) {
117 ALOGE("%s error in closing prev rot mem", __FUNCTION__);
118 return false;
119 }
120 }
121 }
122 return true;
123}
124
125bool MdssRot::open_i(uint32_t numbufs, uint32_t bufsz)
126{
127 OvMem mem;
128 OVASSERT(MAP_FAILED == mem.addr(), "MAP failed in open_i");
129
130 if(!mem.open(numbufs, bufsz, false)){ // TODO: secure for badger
131 ALOGE("%s: Failed to open", __func__);
132 mem.close();
133 return false;
134 }
135
136 OVASSERT(MAP_FAILED != mem.addr(), "MAP failed");
137 OVASSERT(mem.getFD() != -1, "getFd is -1");
138
139 mRotData.dst_data.memory_id = mem.getFD();
140 mRotData.dst_data.offset = 0;
141 mMem.curr().m = mem;
142 return true;
143}
144
145bool MdssRot::remap(uint32_t numbufs) {
146 // if current size changed, remap
147 if(mBufSize == mMem.curr().size()) {
148 ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, mBufSize);
149 return true;
150 }
151
152 ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
153 OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
154
155 // ++mMem will make curr to be prev, and prev will be curr
156 ++mMem;
157 if(!open_i(numbufs, mBufSize)) {
158 ALOGE("%s Error could not open", __FUNCTION__);
159 return false;
160 }
161 for (uint32_t i = 0; i < numbufs; ++i) {
162 mMem.curr().mRotOffset[i] = i * mBufSize;
163 }
164 return true;
165}
166
167bool MdssRot::close() {
168 bool success = true;
169 if(mFd.valid() && (getSessId() > 0)) {
170 if(!mdp_wrapper::unsetOverlay(mFd.getFD(), getSessId())) {
171 ALOGE("MdssRot::close unsetOverlay failed, fd=%d sessId=%d",
172 mFd.getFD(), getSessId());
173 success = false;
174 }
175 }
176
177 if (!mFd.close()) {
178 ALOGE("Mdss Rot error closing fd");
179 success = false;
180 }
181 if (!mMem.close()) {
182 ALOGE("Mdss Rot error closing mem");
183 success = false;
184 }
185 reset();
186 return success;
187}
188
189void MdssRot::reset() {
190 ovutils::memset0(mRotInfo);
191 ovutils::memset0(mRotData);
192 mRotData.data.memory_id = -1;
193 mRotInfo.id = MSMFB_NEW_REQUEST;
194 ovutils::memset0(mMem.curr().mRotOffset);
195 ovutils::memset0(mMem.prev().mRotOffset);
196 mMem.curr().mCurrOffset = 0;
197 mMem.prev().mCurrOffset = 0;
198 mBufSize = 0;
199 mOrientation = utils::OVERLAY_TRANSFORM_0;
200}
201
202void MdssRot::dump() const {
203 ALOGE("== Dump MdssRot start ==");
204 mFd.dump();
205 mMem.curr().m.dump();
206 mdp_wrapper::dump("mRotInfo", mRotInfo);
207 mdp_wrapper::dump("mRotData", mRotData);
208 ALOGE("== Dump MdssRot end ==");
209}
210
211} // namespace overlay