blob: 1d58eceb8e206ca33329e6b87f2b8851438a1c0e [file] [log] [blame]
Saurabh Shahc8118ac2013-06-27 10:03:19 -07001/*
2* Copyright (c) 2013 The Linux Foundation. All rights reserved.
3*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are
6* met:
7* * Redistributions of source code must retain the above copyright
8* notice, this list of conditions and the following disclaimer.
9* * Redistributions in binary form must reproduce the above
10* copyright notice, this list of conditions and the following
11* disclaimer in the documentation and/or other materials provided
12* with the distribution.
13* * Neither the name of The Linux Foundation. nor the names of its
14* contributors may be used to endorse or promote products derived
15* from this software without specific prior written permission.
16*
17* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
29
30#include "overlay.h"
31#include "overlayWriteback.h"
32#include "mdpWrapper.h"
33
Saurabh Shah885e1442013-09-05 13:27:04 -070034#define SIZE_1M 0x00100000
35
Saurabh Shahc8118ac2013-06-27 10:03:19 -070036namespace overlay {
37
38//=========== class WritebackMem ==============================================
39bool WritebackMem::manageMem(uint32_t size, bool isSecure) {
Saurabh Shah885e1442013-09-05 13:27:04 -070040 if(isSecure) {
41 size = utils::align(size, SIZE_1M);
42 }
Saurabh Shahc8118ac2013-06-27 10:03:19 -070043 if(mBuf.bufSz() == size) {
44 return true;
45 }
46 if(mBuf.valid()) {
47 if(!mBuf.close()) {
48 ALOGE("%s error closing mem", __func__);
49 return false;
50 }
51 }
52 return alloc(size, isSecure);
53}
54
55bool WritebackMem::alloc(uint32_t size, bool isSecure) {
56 if(!mBuf.open(NUM_BUFS, size, isSecure)){
57 ALOGE("%s: Failed to open", __func__);
58 mBuf.close();
59 return false;
60 }
61
62 OVASSERT(MAP_FAILED != mBuf.addr(), "MAP failed");
63 OVASSERT(mBuf.getFD() != -1, "getFd is -1");
64
65 mCurrOffsetIndex = 0;
66 for (uint32_t i = 0; i < NUM_BUFS; i++) {
67 mOffsets[i] = i * size;
68 }
69 return true;
70}
71
72bool WritebackMem::dealloc() {
73 bool ret = true;
74 if(mBuf.valid()) {
75 ret = mBuf.close();
76 }
77 return ret;
78}
79
80//=========== class Writeback =================================================
Saurabh Shaha9da08f2013-07-03 13:27:53 -070081Writeback::Writeback() : mXres(0), mYres(0), mOpFmt(-1) {
Saurabh Shahc8118ac2013-06-27 10:03:19 -070082 int fbNum = Overlay::getFbForDpy(Overlay::DPY_WRITEBACK);
83 if(!utils::openDev(mFd, fbNum, Res::fbPath, O_RDWR)) {
84 ALOGE("%s failed to init %s", __func__, Res::fbPath);
85 return;
86 }
Saurabh Shaha9da08f2013-07-03 13:27:53 -070087 queryOutputFormat();
Saurabh Shahc8118ac2013-06-27 10:03:19 -070088 startSession();
89}
90
91Writeback::~Writeback() {
92 stopSession();
93 if (!mFd.close()) {
94 ALOGE("%s error closing fd", __func__);
95 }
96}
97
98bool Writeback::startSession() {
99 if(!mdp_wrapper::wbInitStart(mFd.getFD())) {
100 ALOGE("%s failed", __func__);
101 return false;
102 }
103 return true;
104}
105
106bool Writeback::stopSession() {
107 if(mFd.valid()) {
108 if(!mdp_wrapper::wbStopTerminate(mFd.getFD())) {
109 ALOGE("%s failed", __func__);
110 return false;
111 }
112 } else {
113 ALOGE("%s Invalid fd", __func__);
114 return false;
115 }
116 return true;
117}
118
119bool Writeback::configureDpyInfo(int xres, int yres) {
120 if(mXres != xres || mYres != yres) {
121 fb_var_screeninfo vinfo;
122 memset(&vinfo, 0, sizeof(fb_var_screeninfo));
123 if(!mdp_wrapper::getVScreenInfo(mFd.getFD(), vinfo)) {
124 ALOGE("%s failed", __func__);
125 return false;
126 }
127 vinfo.xres = xres;
128 vinfo.yres = yres;
129 vinfo.xres_virtual = xres;
130 vinfo.yres_virtual = yres;
131 vinfo.xoffset = 0;
132 vinfo.yoffset = 0;
133 if(!mdp_wrapper::setVScreenInfo(mFd.getFD(), vinfo)) {
134 ALOGE("%s failed", __func__);
135 return false;
136 }
137 mXres = xres;
138 mYres = yres;
139 }
140 return true;
141}
142
143bool Writeback::configureMemory(uint32_t size, bool isSecure) {
144 if(!mWbMem.manageMem(size, isSecure)) {
145 ALOGE("%s failed, memory failure", __func__);
146 return false;
147 }
148 return true;
149}
150
151bool Writeback::queueBuffer(int opFd, uint32_t opOffset) {
152 memset(&mFbData, 0, sizeof(struct msmfb_data));
153 //Queue
154 mFbData.offset = opOffset;
155 mFbData.memory_id = opFd;
156 mFbData.id = 0;
157 mFbData.flags = 0;
158 if(!mdp_wrapper::wbQueueBuffer(mFd.getFD(), mFbData)) {
159 ALOGE("%s: queuebuffer failed", __func__);
160 return false;
161 }
162 return true;
163}
164
165bool Writeback::dequeueBuffer() {
166 //Dequeue
167 mFbData.flags = MSMFB_WRITEBACK_DEQUEUE_BLOCKING;
168 if(!mdp_wrapper::wbDequeueBuffer(mFd.getFD(), mFbData)) {
169 ALOGE("%s: dequeuebuffer failed", __func__);
170 return false;
171 }
172 return true;
173}
174
175bool Writeback::writeSync(int opFd, uint32_t opOffset) {
176 if(!queueBuffer(opFd, opOffset)) {
177 return false;
178 }
179 if(!Overlay::displayCommit(mFd.getFD())) {
180 return false;
181 }
182 if(!dequeueBuffer()) {
183 return false;
184 }
185 return true;
186}
187
188bool Writeback::writeSync() {
189 mWbMem.useNextBuffer();
190 return writeSync(mWbMem.getDstFd(), mWbMem.getOffset());
191}
192
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700193void Writeback::queryOutputFormat() {
194 struct msmfb_metadata metadata;
195 memset(&metadata, 0 , sizeof(metadata));
196 metadata.op = metadata_op_wb_format;
197 if (ioctl(mFd.getFD(), MSMFB_METADATA_GET, &metadata) < 0) {
198 ALOGE("Error retrieving MDP Writeback format");
199 return;
200 }
201 mOpFmt = metadata.data.mixer_cfg.writeback_format;
202}
203
Saurabh Shahc8118ac2013-06-27 10:03:19 -0700204//static
205
206Writeback *Writeback::getInstance() {
207 if(sWb == NULL) {
208 sWb = new Writeback();
209 }
210 sUsed = true;
211 return sWb;
212}
213
214void Writeback::configDone() {
215 if(sUsed == false && sWb) {
216 delete sWb;
217 sWb = NULL;
218 }
219}
220
221void Writeback::clear() {
222 sUsed = false;
223 if(sWb) {
224 delete sWb;
225 sWb = NULL;
226 }
227}
228
229Writeback *Writeback::sWb = 0;
230bool Writeback::sUsed = false;
231
232} //namespace overlay