Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012, Code Aurora Forum. 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 Code Aurora Forum, Inc. 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 | #ifndef OVERLAY_CTRLDATA_H |
| 31 | #define OVERLAY_CTRLDATA_H |
| 32 | |
| 33 | #include "overlayUtils.h" |
| 34 | #include "overlayMdp.h" |
| 35 | #include "gralloc_priv.h" // INTERLACE_MASK |
| 36 | |
| 37 | namespace ovutils = overlay::utils; |
| 38 | |
| 39 | namespace overlay { |
| 40 | |
| 41 | // FIXME make int to be uint32 whenever possible |
| 42 | |
| 43 | class RotatorBase; |
| 44 | |
| 45 | /* |
| 46 | * FIXME do we want rot to be template parameter? |
| 47 | * It's already using inheritance... |
| 48 | * |
| 49 | * Sequence to use: |
| 50 | * open |
| 51 | * start |
| 52 | * setXXX |
| 53 | * close |
| 54 | * |
| 55 | * Can call setRot anytime to replace rotator on-the-fly |
| 56 | * */ |
| 57 | class Ctrl : utils::NoCopy { |
| 58 | public: |
| 59 | |
| 60 | /* ctor */ |
| 61 | explicit Ctrl(); |
| 62 | |
| 63 | /* dtor close */ |
| 64 | ~Ctrl(); |
| 65 | |
| 66 | /* should open devices? or start()? */ |
| 67 | bool open(uint32_t fbnum, RotatorBase* rot); |
| 68 | |
| 69 | /* close underlying mdp */ |
| 70 | bool close(); |
| 71 | |
| 72 | /* Invoke methods for opening underlying devices |
| 73 | * flags - PIPE SHARED |
| 74 | * wait - WAIT, NO_WAIT */ |
| 75 | bool start(const utils::PipeArgs& args); |
| 76 | |
| 77 | /* Dynamically set rotator*/ |
| 78 | void setRot(RotatorBase* rot); |
| 79 | |
| 80 | /* set mdp posision using dim */ |
| 81 | bool setPosition(const utils::Dim& dim); |
| 82 | |
| 83 | /* set param using Params (param,value pair) */ |
| 84 | bool setParameter(const utils::Params& p); |
| 85 | |
| 86 | /* set source using whf, orient and wait flag */ |
| 87 | bool setSource(const utils::PipeArgs& args); |
| 88 | |
| 89 | /* set crop info and pass it down to mdp */ |
| 90 | bool setCrop(const utils::Dim& d); |
| 91 | |
| 92 | /* mdp set overlay/commit changes */ |
| 93 | bool commit(); |
| 94 | |
| 95 | /* ctrl id */ |
| 96 | int getId() const; |
| 97 | /* ctrl fd */ |
| 98 | int getFd() const; |
| 99 | bool getRotSessId(int& id) const; |
| 100 | utils::Dim getAspectRatio(const utils::Whf& whf) const; |
| 101 | utils::Dim getAspectRatio(const utils::Dim& dim) const; |
| 102 | |
| 103 | /* access for screen info */ |
| 104 | utils::ScreenInfo getScreenInfo() const; |
| 105 | |
| 106 | /* retrieve cached crop data */ |
| 107 | utils::Dim getCrop() const; |
| 108 | |
| 109 | /* dump the state of the object */ |
| 110 | void dump() const; |
| 111 | |
| 112 | private: |
| 113 | /* Retrieve screen info from underlying mdp */ |
| 114 | bool getScreenInfo(utils::ScreenInfo& info); |
| 115 | |
| 116 | /* calls underlying mdp set info */ |
| 117 | bool setInfo(const utils::PipeArgs& args); |
| 118 | |
| 119 | /* given whf, update src */ |
| 120 | void updateSource(RotatorBase* r, |
| 121 | const utils::PipeArgs& args, |
| 122 | utils::ScreenInfo& info); |
| 123 | |
| 124 | // mdp ctrl struct(info e.g.) |
| 125 | MdpCtrl mMdp; |
| 126 | |
| 127 | // Rotator |
| 128 | RotatorBase* mRot; |
| 129 | |
| 130 | /* Cache cropped value */ |
| 131 | utils::Dim mCrop; |
| 132 | |
| 133 | /* Screen info */ |
| 134 | utils::ScreenInfo mInfo; |
| 135 | |
| 136 | /* orientation cache FIXME */ |
| 137 | utils::eTransform mOrient; |
| 138 | |
| 139 | /* Cache last known whfz. |
| 140 | * That would help us compare to a previous |
| 141 | * source that was submitted */ |
| 142 | utils::Whf mOvBufInfo; |
| 143 | }; |
| 144 | |
| 145 | |
| 146 | /* |
| 147 | * MDP = DataMdp, ROT = CtrlMdp usually since Rotator<> |
| 148 | * is instansiated with Ctrl data structure. |
| 149 | * */ |
| 150 | class Data : utils::NoCopy { |
| 151 | public: |
| 152 | /* init, reset */ |
| 153 | explicit Data(); |
| 154 | |
| 155 | /* calls close */ |
| 156 | ~Data(); |
| 157 | |
| 158 | /* should open devices? or start()? */ |
| 159 | bool open(uint32_t fbnum, RotatorBase* rot); |
| 160 | |
| 161 | /* calls underlying mdp close */ |
| 162 | bool close(); |
| 163 | |
| 164 | /* set the rotator */ |
| 165 | void setRot(RotatorBase* rot); |
| 166 | |
| 167 | /* set memory id in the mdp struct */ |
| 168 | void setMemoryId(int id); |
| 169 | |
| 170 | /* set overlay id in the mdp struct */ |
| 171 | void setId(int id); |
| 172 | |
| 173 | /* get overlay id in the mdp struct */ |
| 174 | int getId() const; |
| 175 | |
| 176 | /* queue buffer to the overlay */ |
| 177 | bool queueBuffer(uint32_t offset); |
| 178 | |
| 179 | /* wait for vsync to be done */ |
| 180 | bool waitForVsync(); |
| 181 | |
| 182 | /* sump the state of the obj */ |
| 183 | void dump() const; |
| 184 | private: |
| 185 | /* play wrapper */ |
| 186 | bool play(); |
| 187 | |
| 188 | /* playWait wrapper */ |
| 189 | bool playWait(); |
| 190 | |
| 191 | // mdp data struct |
| 192 | MdpData mMdp; |
| 193 | |
| 194 | // Rotator |
| 195 | RotatorBase* mRot; |
| 196 | }; |
| 197 | |
| 198 | /* This class just creates a Ctrl Data pair to be used by a pipe. |
| 199 | * Although this was legacy design, this separation still makes sense, since we |
| 200 | * need to use the Ctrl channel in hwc_prepare (i.e config stage) and Data |
| 201 | * channel in hwc_set (i.e draw stage) |
| 202 | */ |
| 203 | struct CtrlData { |
| 204 | Ctrl ctrl; |
| 205 | Data data; |
| 206 | }; |
| 207 | |
| 208 | //-------------Inlines------------------------------- |
| 209 | |
| 210 | inline Ctrl::Ctrl() : mRot(0), mOrient(utils::OVERLAY_TRANSFORM_0) { |
| 211 | mMdp.reset(); |
| 212 | } |
| 213 | |
| 214 | inline Ctrl::~Ctrl() { |
| 215 | close(); |
| 216 | } |
| 217 | |
| 218 | inline bool Ctrl::close() { |
| 219 | // do not close the rotator |
| 220 | if(!mMdp.close()) |
| 221 | return false; |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | inline bool Ctrl::commit() { |
| 226 | if(!mMdp.set()) { |
| 227 | ALOGE("Ctrl commit failed set overlay"); |
| 228 | return false; |
| 229 | } |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | inline bool Ctrl::getScreenInfo(utils::ScreenInfo& info) { |
| 234 | if(!mMdp.getScreenInfo(info)){ |
| 235 | ALOGE("Ctrl failed to get screen info"); |
| 236 | return false; |
| 237 | } |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | inline bool Ctrl::setInfo(const utils::PipeArgs& args) |
| 242 | { |
| 243 | // FIXME set flags, zorder and wait separtly |
| 244 | if(!mMdp.setInfo(mRot, args, mInfo)){ |
| 245 | ALOGE("Ctrl failed to setInfo wait=%d mdpflags=%d " |
| 246 | "zorder=%d", args.wait, args.mdpFlags, args.zorder); |
| 247 | return false; |
| 248 | } |
| 249 | return true; |
| 250 | } |
| 251 | |
| 252 | inline int Ctrl::getId() const { |
| 253 | // FIXME check channel up? |
| 254 | return mMdp.getId(); |
| 255 | } |
| 256 | |
| 257 | inline int Ctrl::getFd() const { |
| 258 | // FIXME check channel up? |
| 259 | return mMdp.getFd(); |
| 260 | } |
| 261 | |
| 262 | inline bool Ctrl::getRotSessId(int& id) const { |
| 263 | // FIXME check channel up? |
| 264 | // should be -1 in case of no rot session active |
| 265 | id = mRot->getSessId(); |
| 266 | return true; |
| 267 | } |
| 268 | |
| 269 | inline utils::ScreenInfo Ctrl::getScreenInfo() const { |
| 270 | return mInfo; |
| 271 | } |
| 272 | |
| 273 | inline utils::Dim Ctrl::getCrop() const { |
| 274 | return mCrop; |
| 275 | } |
| 276 | |
| 277 | |
| 278 | |
| 279 | inline Data::Data() : mRot(0) { |
| 280 | mMdp.reset(); |
| 281 | } |
| 282 | |
| 283 | inline Data::~Data() { close(); } |
| 284 | |
| 285 | inline void Data::setRot(RotatorBase* rot) { mRot = rot; } |
| 286 | |
| 287 | inline void Data::setMemoryId(int id) { mMdp.setMemoryId(id); } |
| 288 | |
| 289 | // really a reqid |
| 290 | inline void Data::setId(int id) { mMdp.setId(id); } |
| 291 | |
| 292 | inline int Data::getId() const { return mMdp.getId(); } |
| 293 | |
| 294 | inline bool Data::open(uint32_t fbnum, |
| 295 | RotatorBase* rot) { |
| 296 | if(!mMdp.open(fbnum)) { |
| 297 | ALOGE("Data cannot open mdp"); |
| 298 | return false; |
| 299 | } |
| 300 | |
| 301 | OVASSERT(rot, "rot is null"); |
| 302 | mRot = rot; |
| 303 | |
| 304 | // rotator should be already opened here |
| 305 | return true; |
| 306 | } |
| 307 | |
| 308 | inline bool Data::close() { |
| 309 | if(!mMdp.close()) { |
| 310 | ALOGE("Data close failed"); |
| 311 | return false; |
| 312 | } |
| 313 | return true; |
| 314 | } |
| 315 | |
| 316 | inline bool Data::queueBuffer(uint32_t offset) { |
| 317 | // FIXME asserts on state validity |
| 318 | |
| 319 | mMdp.setOffset(offset); |
| 320 | mRot->setRotDataSrcMemId(mMdp.getMemoryId()); |
| 321 | // will play if succeeded |
| 322 | if(!mRot->prepareQueueBuf(offset)) { |
| 323 | ALOGE("Data failed to prepareQueueBuf"); |
| 324 | return false; |
| 325 | } |
| 326 | // Play can go either from mdp or rot |
| 327 | if(!this->play()){ |
| 328 | ALOGE("Data error in MDP/ROT play"); |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | return true; |
| 333 | } |
| 334 | |
| 335 | inline bool Data::waitForVsync() { |
| 336 | |
| 337 | // Call mdp playWait |
| 338 | if(!this->playWait()){ |
| 339 | ALOGE("Error in MDP playWait"); |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | return true; |
| 344 | } |
| 345 | |
| 346 | inline bool Data::play() { |
| 347 | int fd = mMdp.getFd(); |
| 348 | return mRot->enabled() ? mRot->play(fd) : mMdp.play(); |
| 349 | } |
| 350 | |
| 351 | inline bool Data::playWait() { |
| 352 | return mMdp.playWait(); |
| 353 | } |
| 354 | |
| 355 | inline void Data::dump() const { |
| 356 | ALOGE("== Dump Data MDP start =="); |
| 357 | mMdp.dump(); |
| 358 | mRot->dump(); |
| 359 | ALOGE("== Dump Data MDP end =="); |
| 360 | } |
| 361 | |
| 362 | |
| 363 | } // overlay |
| 364 | |
| 365 | #endif |