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