Saurabh Shah | c8118ac | 2013-06-27 10:03:19 -0700 | [diff] [blame] | 1 | /* |
| 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 Shah | 885e144 | 2013-09-05 13:27:04 -0700 | [diff] [blame^] | 34 | #define SIZE_1M 0x00100000 |
| 35 | |
Saurabh Shah | c8118ac | 2013-06-27 10:03:19 -0700 | [diff] [blame] | 36 | namespace overlay { |
| 37 | |
| 38 | //=========== class WritebackMem ============================================== |
| 39 | bool WritebackMem::manageMem(uint32_t size, bool isSecure) { |
Saurabh Shah | 885e144 | 2013-09-05 13:27:04 -0700 | [diff] [blame^] | 40 | if(isSecure) { |
| 41 | size = utils::align(size, SIZE_1M); |
| 42 | } |
Saurabh Shah | c8118ac | 2013-06-27 10:03:19 -0700 | [diff] [blame] | 43 | 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 | |
| 55 | bool 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 | |
| 72 | bool WritebackMem::dealloc() { |
| 73 | bool ret = true; |
| 74 | if(mBuf.valid()) { |
| 75 | ret = mBuf.close(); |
| 76 | } |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | //=========== class Writeback ================================================= |
Saurabh Shah | a9da08f | 2013-07-03 13:27:53 -0700 | [diff] [blame] | 81 | Writeback::Writeback() : mXres(0), mYres(0), mOpFmt(-1) { |
Saurabh Shah | c8118ac | 2013-06-27 10:03:19 -0700 | [diff] [blame] | 82 | 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 Shah | a9da08f | 2013-07-03 13:27:53 -0700 | [diff] [blame] | 87 | queryOutputFormat(); |
Saurabh Shah | c8118ac | 2013-06-27 10:03:19 -0700 | [diff] [blame] | 88 | startSession(); |
| 89 | } |
| 90 | |
| 91 | Writeback::~Writeback() { |
| 92 | stopSession(); |
| 93 | if (!mFd.close()) { |
| 94 | ALOGE("%s error closing fd", __func__); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | bool Writeback::startSession() { |
| 99 | if(!mdp_wrapper::wbInitStart(mFd.getFD())) { |
| 100 | ALOGE("%s failed", __func__); |
| 101 | return false; |
| 102 | } |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | bool 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 | |
| 119 | bool 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 | |
| 143 | bool 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 | |
| 151 | bool 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 | |
| 165 | bool 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 | |
| 175 | bool 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 | |
| 188 | bool Writeback::writeSync() { |
| 189 | mWbMem.useNextBuffer(); |
| 190 | return writeSync(mWbMem.getDstFd(), mWbMem.getOffset()); |
| 191 | } |
| 192 | |
Saurabh Shah | a9da08f | 2013-07-03 13:27:53 -0700 | [diff] [blame] | 193 | void 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 Shah | c8118ac | 2013-06-27 10:03:19 -0700 | [diff] [blame] | 204 | //static |
| 205 | |
| 206 | Writeback *Writeback::getInstance() { |
| 207 | if(sWb == NULL) { |
| 208 | sWb = new Writeback(); |
| 209 | } |
| 210 | sUsed = true; |
| 211 | return sWb; |
| 212 | } |
| 213 | |
| 214 | void Writeback::configDone() { |
| 215 | if(sUsed == false && sWb) { |
| 216 | delete sWb; |
| 217 | sWb = NULL; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void Writeback::clear() { |
| 222 | sUsed = false; |
| 223 | if(sWb) { |
| 224 | delete sWb; |
| 225 | sWb = NULL; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | Writeback *Writeback::sWb = 0; |
| 230 | bool Writeback::sUsed = false; |
| 231 | |
| 232 | } //namespace overlay |