blob: c2d774ba51311b32bc6e193253b62d364520e3ef [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
2* Copyright (c) 2011-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_GENERIC_PIPE_H
31#define OVERLAY_GENERIC_PIPE_H
32
33#include "overlayUtils.h"
34#include "overlayRotator.h"
35#include "overlayCtrlData.h"
36
Naseer Ahmed29a26812012-06-14 00:56:20 -070037namespace overlay {
38
39template <int PANEL>
40class GenericPipe : utils::NoCopy {
41public:
Naseer Ahmedf48aef62012-07-20 09:05:53 -070042 /* ctor */
Naseer Ahmed29a26812012-06-14 00:56:20 -070043 explicit GenericPipe();
Naseer Ahmedf48aef62012-07-20 09:05:53 -070044 /* dtor */
Naseer Ahmed29a26812012-06-14 00:56:20 -070045 ~GenericPipe();
Naseer Ahmedf48aef62012-07-20 09:05:53 -070046 /* CTRL/DATA init. Not owning rotator, will not init it */
47 bool init(RotatorBase* rot);
Naseer Ahmed29a26812012-06-14 00:56:20 -070048 /* CTRL/DATA close. Not owning rotator, will not close it */
49 bool close();
50
Naseer Ahmedf48aef62012-07-20 09:05:53 -070051 /* Control APIs */
52 /* set source using whf, orient and wait flag */
53 bool setSource(const utils::PipeArgs& args);
54 /* set crop a.k.a the region of interest */
55 bool setCrop(const utils::Dim& d);
56 /* set orientation*/
57 bool setTransform(const utils::eTransform& param);
58 /* set mdp posision using dim */
59 bool setPosition(const utils::Dim& dim);
Naseer Ahmed29a26812012-06-14 00:56:20 -070060 /* commit changes to the overlay "set"*/
61 bool commit();
62
Naseer Ahmedf48aef62012-07-20 09:05:53 -070063 /* Data APIs */
Naseer Ahmed29a26812012-06-14 00:56:20 -070064 /* queue buffer to the overlay */
Naseer Ahmedf48aef62012-07-20 09:05:53 -070065 bool queueBuffer(int fd, uint32_t offset);
Naseer Ahmed29a26812012-06-14 00:56:20 -070066 /* wait for vsync to be done */
67 bool waitForVsync();
68
Naseer Ahmed29a26812012-06-14 00:56:20 -070069 /* return cached startup args */
70 const utils::PipeArgs& getArgs() const;
71
72 /* retrieve screen info */
73 utils::ScreenInfo getScreenInfo() const;
74
75 /* retrieve cached crop data */
76 utils::Dim getCrop() const;
77
78 /* return aspect ratio from ctrl data */
79 utils::Dim getAspectRatio(const utils::Whf& whf) const;
80
81 /* return aspect ratio from ctrl data for true UI mirroring */
82 utils::Dim getAspectRatio(const utils::Dim& dim) const;
83
84 /* is closed */
85 bool isClosed() const;
86
87 /* is open */
88 bool isOpen() const;
89
90 /* return Ctrl fd. Used for S3D */
91 int getCtrlFd() const;
92
93 /* Get the overlay pipe type */
94 utils::eOverlayPipeType getOvPipeType() const;
95
96 /* dump the state of the object */
97 void dump() const;
98private:
Naseer Ahmedf48aef62012-07-20 09:05:53 -070099 /* set Closed pipe */
Naseer Ahmed29a26812012-06-14 00:56:20 -0700100 bool setClosed();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700101
102 /* Ctrl/Data aggregator */
103 CtrlData mCtrlData;
104
Naseer Ahmed29a26812012-06-14 00:56:20 -0700105 /* rotator mdp base
106 * Can point to NullRotator or to Rotator*/
107 RotatorBase* mRot;
108
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700109 //Whether rotator is used for 0-rot or otherwise
110 bool mRotUsed;
111
112 /* Pipe open or closed */
113 enum ePipeState {
114 CLOSED,
115 OPEN
116 };
117 ePipeState pipeState;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700118};
119
120//------------------------Inlines and Templates ----------------------
121
122template <int PANEL>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700123GenericPipe<PANEL>::GenericPipe() : mRot(0), mRotUsed(false),
124 pipeState(CLOSED) {
125}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700126
127template <int PANEL>
128GenericPipe<PANEL>::~GenericPipe() {
129 close();
130}
131
132template <int PANEL>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700133bool GenericPipe<PANEL>::init(RotatorBase* rot)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700134{
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700135 ALOGE_IF(DEBUG_OVERLAY, "GenericPipe init");
Naseer Ahmed29a26812012-06-14 00:56:20 -0700136 OVASSERT(rot, "rot is null");
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700137
138 // init ctrl and data
Naseer Ahmed29a26812012-06-14 00:56:20 -0700139 uint32_t fbnum = utils::getFBForPanel(PANEL);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700140
141 if(!mCtrlData.ctrl.init(fbnum)) {
142 ALOGE("GenericPipe failed to init ctrl");
Naseer Ahmed29a26812012-06-14 00:56:20 -0700143 return false;
144 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700145
146 if(!mCtrlData.data.init(fbnum)) {
147 ALOGE("GenericPipe failed to init data");
Naseer Ahmed29a26812012-06-14 00:56:20 -0700148 return false;
149 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700150
151 //Cache the rot ref. Ownership is with OverlayImpl.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700152 mRot = rot;
153
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700154 mRotUsed = false;
155
156 // NOTE:init() on the rot is called by OverlayImpl
157 // Pipes only have to worry about using rot, and not init or close.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700158
159 return true;
160}
161
162template <int PANEL>
163bool GenericPipe<PANEL>::close() {
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700164 if(isClosed())
165 return true;
166
Naseer Ahmed29a26812012-06-14 00:56:20 -0700167 bool ret = true;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700168
Naseer Ahmed29a26812012-06-14 00:56:20 -0700169 if(!mCtrlData.ctrl.close()) {
170 ALOGE("GenericPipe failed to close ctrl");
171 ret = false;
172 }
173 if (!mCtrlData.data.close()) {
174 ALOGE("GenericPipe failed to close data");
175 ret = false;
176 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700177
178 // NOTE:close() on the rot is called by OverlayImpl
179 // Pipes only have to worry about using rot, and not init or close.
180
Naseer Ahmed29a26812012-06-14 00:56:20 -0700181 setClosed();
182 return ret;
183}
184
185template <int PANEL>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700186inline bool GenericPipe<PANEL>::setSource(
187 const utils::PipeArgs& args)
188{
189 utils::PipeArgs newargs(args);
190 //Interlace video handling.
191 if(newargs.whf.format & INTERLACE_MASK) {
192 setMdpFlags(newargs.mdpFlags, utils::OV_MDP_DEINTERLACE);
193 }
194 utils::Whf whf(newargs.whf);
195 //Extract HAL format from lower bytes. Deinterlace if interlaced.
196 whf.format = utils::getColorFormat(whf.format);
197 //Get MDP equivalent of HAL format.
198 whf.format = utils::getMdpFormat(whf.format);
199 newargs.whf = whf;
200
201 //Cache if user wants 0-rotation
202 mRotUsed = newargs.rotFlags & utils::ROT_FLAG_ENABLED;
203 mRot->setSource(newargs.whf);
204 mRot->setFlags(newargs.mdpFlags);
205 return mCtrlData.ctrl.setSource(newargs);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700206}
207
208template <int PANEL>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700209inline bool GenericPipe<PANEL>::setCrop(
210 const overlay::utils::Dim& d) {
211 return mCtrlData.ctrl.setCrop(d);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700212}
213
214template <int PANEL>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700215inline bool GenericPipe<PANEL>::setTransform(
216 const utils::eTransform& orient)
217{
218 //Rotation could be enabled by user for zero-rot or the layer could have
219 //some transform. Mark rotation enabled in either case.
220 mRotUsed |= (orient != utils::OVERLAY_TRANSFORM_0);
221 mRot->setTransform(orient, mRotUsed);
222
223 return mCtrlData.ctrl.setTransform(orient, mRotUsed);
224}
225
226template <int PANEL>
227inline bool GenericPipe<PANEL>::setPosition(const utils::Dim& d)
228{
229 return mCtrlData.ctrl.setPosition(d);
230}
231
232template <int PANEL>
233inline bool GenericPipe<PANEL>::commit() {
234 bool ret = false;
235 //If wanting to use rotator, start it.
236 if(mRotUsed) {
237 if(!mRot->commit()) {
238 ALOGE("GenPipe Rotator commit failed");
239 return false;
240 }
241 }
242 ret = mCtrlData.ctrl.commit();
243 pipeState = ret ? OPEN : CLOSED;
244 return ret;
245}
246
247template <int PANEL>
248inline bool GenericPipe<PANEL>::queueBuffer(int fd, uint32_t offset) {
249 //TODO Move pipe-id transfer to CtrlData class. Make ctrl and data private.
250 OVASSERT(isOpen(), "State is closed, cannot queueBuffer");
251 int pipeId = mCtrlData.ctrl.getPipeId();
252 OVASSERT(-1 != pipeId, "Ctrl ID should not be -1");
253 // set pipe id from ctrl to data
254 mCtrlData.data.setPipeId(pipeId);
255
256 int finalFd = fd;
257 uint32_t finalOffset = offset;
258 //If rotator is to be used, queue to it, so it can ROTATE.
259 if(mRotUsed) {
260 if(!mRot->queueBuffer(fd, offset)) {
261 ALOGE("GenPipe Rotator play failed");
262 return false;
263 }
264 //Configure MDP's source buffer as the current output buffer of rotator
265 if(mRot->getDstMemId() != -1) {
266 finalFd = mRot->getDstMemId();
267 finalOffset = mRot->getDstOffset();
268 } else {
269 //Could be -1 for NullRotator, if queue above succeeds.
270 //Need an actual rotator. Modify overlay State Traits.
271 //Not fatal, keep queuing to MDP without rotation.
272 ALOGE("Null rotator in use, where an actual is required");
273 }
274 }
275 return mCtrlData.data.queueBuffer(finalFd, finalOffset);
276}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700277
278template <int PANEL>
279inline int GenericPipe<PANEL>::getCtrlFd() const {
280 return mCtrlData.ctrl.getFd();
281}
282
283template <int PANEL>
Naseer Ahmed29a26812012-06-14 00:56:20 -0700284inline bool GenericPipe<PANEL>::waitForVsync() {
285 OVASSERT(isOpen(), "State is closed, cannot waitForVsync");
Naseer Ahmed29a26812012-06-14 00:56:20 -0700286 return mCtrlData.data.waitForVsync();
287}
288
289template <int PANEL>
Naseer Ahmed29a26812012-06-14 00:56:20 -0700290inline utils::Dim GenericPipe<PANEL>::getAspectRatio(
291 const utils::Whf& whf) const
292{
293 return mCtrlData.ctrl.getAspectRatio(whf);
294}
295
296template <int PANEL>
297inline utils::Dim GenericPipe<PANEL>::getAspectRatio(
298 const utils::Dim& dim) const
299{
300 return mCtrlData.ctrl.getAspectRatio(dim);
301}
302
303template <int PANEL>
304inline utils::ScreenInfo GenericPipe<PANEL>::getScreenInfo() const
305{
306 return mCtrlData.ctrl.getScreenInfo();
307}
308
309template <int PANEL>
310inline utils::Dim GenericPipe<PANEL>::getCrop() const
311{
312 return mCtrlData.ctrl.getCrop();
313}
314
315template <int PANEL>
316inline utils::eOverlayPipeType GenericPipe<PANEL>::getOvPipeType() const {
317 return utils::OV_PIPE_TYPE_GENERIC;
318}
319
320template <int PANEL>
321void GenericPipe<PANEL>::dump() const
322{
323 ALOGE("== Dump Generic pipe start ==");
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700324 ALOGE("pipe state = %d", (int)pipeState);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700325 OVASSERT(mRot, "GenericPipe should have a valid Rot");
326 mCtrlData.ctrl.dump();
327 mCtrlData.data.dump();
328 mRot->dump();
329 ALOGE("== Dump Generic pipe end ==");
330}
331
332template <int PANEL>
333inline bool GenericPipe<PANEL>::isClosed() const {
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700334 return (pipeState == CLOSED);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700335}
336
337template <int PANEL>
338inline bool GenericPipe<PANEL>::isOpen() const {
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700339 return (pipeState == OPEN);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700340}
341
342template <int PANEL>
343inline bool GenericPipe<PANEL>::setClosed() {
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700344 pipeState = CLOSED;
345 return true;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700346}
347
Naseer Ahmed29a26812012-06-14 00:56:20 -0700348} //namespace overlay
349
350#endif // OVERLAY_GENERIC_PIPE_H