blob: cbcb05afe390364c516fbab21dbf0f76b37794d8 [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
Naseer Ahmed29a26812012-06-14 00:56:20 -070067 /* return cached startup args */
68 const utils::PipeArgs& getArgs() const;
69
70 /* retrieve screen info */
71 utils::ScreenInfo getScreenInfo() const;
72
73 /* retrieve cached crop data */
74 utils::Dim getCrop() const;
75
76 /* return aspect ratio from ctrl data */
77 utils::Dim getAspectRatio(const utils::Whf& whf) const;
78
79 /* return aspect ratio from ctrl data for true UI mirroring */
80 utils::Dim getAspectRatio(const utils::Dim& dim) const;
81
82 /* is closed */
83 bool isClosed() const;
84
85 /* is open */
86 bool isOpen() const;
87
88 /* return Ctrl fd. Used for S3D */
89 int getCtrlFd() const;
90
Naseer Ahmed29a26812012-06-14 00:56:20 -070091 /* dump the state of the object */
92 void dump() const;
93private:
Naseer Ahmedf48aef62012-07-20 09:05:53 -070094 /* set Closed pipe */
Naseer Ahmed29a26812012-06-14 00:56:20 -070095 bool setClosed();
Naseer Ahmed29a26812012-06-14 00:56:20 -070096
97 /* Ctrl/Data aggregator */
98 CtrlData mCtrlData;
99
Naseer Ahmed29a26812012-06-14 00:56:20 -0700100 /* rotator mdp base
101 * Can point to NullRotator or to Rotator*/
102 RotatorBase* mRot;
103
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700104 //Whether rotator is used for 0-rot or otherwise
105 bool mRotUsed;
106
107 /* Pipe open or closed */
108 enum ePipeState {
109 CLOSED,
110 OPEN
111 };
112 ePipeState pipeState;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700113};
114
115//------------------------Inlines and Templates ----------------------
116
117template <int PANEL>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700118GenericPipe<PANEL>::GenericPipe() : mRot(0), mRotUsed(false),
119 pipeState(CLOSED) {
120}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700121
122template <int PANEL>
123GenericPipe<PANEL>::~GenericPipe() {
124 close();
125}
126
127template <int PANEL>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700128bool GenericPipe<PANEL>::init(RotatorBase* rot)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700129{
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700130 ALOGE_IF(DEBUG_OVERLAY, "GenericPipe init");
Naseer Ahmed29a26812012-06-14 00:56:20 -0700131 OVASSERT(rot, "rot is null");
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700132
133 // init ctrl and data
Naseer Ahmed29a26812012-06-14 00:56:20 -0700134 uint32_t fbnum = utils::getFBForPanel(PANEL);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700135
136 if(!mCtrlData.ctrl.init(fbnum)) {
137 ALOGE("GenericPipe failed to init ctrl");
Naseer Ahmed29a26812012-06-14 00:56:20 -0700138 return false;
139 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700140
141 if(!mCtrlData.data.init(fbnum)) {
142 ALOGE("GenericPipe failed to init data");
Naseer Ahmed29a26812012-06-14 00:56:20 -0700143 return false;
144 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700145
146 //Cache the rot ref. Ownership is with OverlayImpl.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700147 mRot = rot;
148
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700149 mRotUsed = false;
150
151 // NOTE:init() on the rot is called by OverlayImpl
152 // Pipes only have to worry about using rot, and not init or close.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700153
154 return true;
155}
156
157template <int PANEL>
158bool GenericPipe<PANEL>::close() {
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700159 if(isClosed())
160 return true;
161
Naseer Ahmed29a26812012-06-14 00:56:20 -0700162 bool ret = true;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700163
Naseer Ahmed29a26812012-06-14 00:56:20 -0700164 if(!mCtrlData.ctrl.close()) {
165 ALOGE("GenericPipe failed to close ctrl");
166 ret = false;
167 }
168 if (!mCtrlData.data.close()) {
169 ALOGE("GenericPipe failed to close data");
170 ret = false;
171 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700172
173 // NOTE:close() on the rot is called by OverlayImpl
174 // Pipes only have to worry about using rot, and not init or close.
175
Naseer Ahmed29a26812012-06-14 00:56:20 -0700176 setClosed();
177 return ret;
178}
179
180template <int PANEL>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700181inline bool GenericPipe<PANEL>::setSource(
182 const utils::PipeArgs& args)
183{
184 utils::PipeArgs newargs(args);
185 //Interlace video handling.
186 if(newargs.whf.format & INTERLACE_MASK) {
187 setMdpFlags(newargs.mdpFlags, utils::OV_MDP_DEINTERLACE);
188 }
189 utils::Whf whf(newargs.whf);
190 //Extract HAL format from lower bytes. Deinterlace if interlaced.
191 whf.format = utils::getColorFormat(whf.format);
192 //Get MDP equivalent of HAL format.
193 whf.format = utils::getMdpFormat(whf.format);
194 newargs.whf = whf;
195
196 //Cache if user wants 0-rotation
197 mRotUsed = newargs.rotFlags & utils::ROT_FLAG_ENABLED;
198 mRot->setSource(newargs.whf);
199 mRot->setFlags(newargs.mdpFlags);
200 return mCtrlData.ctrl.setSource(newargs);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700201}
202
203template <int PANEL>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700204inline bool GenericPipe<PANEL>::setCrop(
205 const overlay::utils::Dim& d) {
206 return mCtrlData.ctrl.setCrop(d);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700207}
208
209template <int PANEL>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700210inline bool GenericPipe<PANEL>::setTransform(
211 const utils::eTransform& orient)
212{
213 //Rotation could be enabled by user for zero-rot or the layer could have
214 //some transform. Mark rotation enabled in either case.
215 mRotUsed |= (orient != utils::OVERLAY_TRANSFORM_0);
216 mRot->setTransform(orient, mRotUsed);
217
218 return mCtrlData.ctrl.setTransform(orient, mRotUsed);
219}
220
221template <int PANEL>
222inline bool GenericPipe<PANEL>::setPosition(const utils::Dim& d)
223{
224 return mCtrlData.ctrl.setPosition(d);
225}
226
227template <int PANEL>
228inline bool GenericPipe<PANEL>::commit() {
229 bool ret = false;
230 //If wanting to use rotator, start it.
231 if(mRotUsed) {
232 if(!mRot->commit()) {
233 ALOGE("GenPipe Rotator commit failed");
234 return false;
235 }
236 }
237 ret = mCtrlData.ctrl.commit();
238 pipeState = ret ? OPEN : CLOSED;
239 return ret;
240}
241
242template <int PANEL>
243inline bool GenericPipe<PANEL>::queueBuffer(int fd, uint32_t offset) {
244 //TODO Move pipe-id transfer to CtrlData class. Make ctrl and data private.
245 OVASSERT(isOpen(), "State is closed, cannot queueBuffer");
246 int pipeId = mCtrlData.ctrl.getPipeId();
247 OVASSERT(-1 != pipeId, "Ctrl ID should not be -1");
248 // set pipe id from ctrl to data
249 mCtrlData.data.setPipeId(pipeId);
250
251 int finalFd = fd;
252 uint32_t finalOffset = offset;
253 //If rotator is to be used, queue to it, so it can ROTATE.
254 if(mRotUsed) {
255 if(!mRot->queueBuffer(fd, offset)) {
256 ALOGE("GenPipe Rotator play failed");
257 return false;
258 }
259 //Configure MDP's source buffer as the current output buffer of rotator
260 if(mRot->getDstMemId() != -1) {
261 finalFd = mRot->getDstMemId();
262 finalOffset = mRot->getDstOffset();
263 } else {
264 //Could be -1 for NullRotator, if queue above succeeds.
265 //Need an actual rotator. Modify overlay State Traits.
266 //Not fatal, keep queuing to MDP without rotation.
267 ALOGE("Null rotator in use, where an actual is required");
268 }
269 }
270 return mCtrlData.data.queueBuffer(finalFd, finalOffset);
271}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700272
273template <int PANEL>
274inline int GenericPipe<PANEL>::getCtrlFd() const {
275 return mCtrlData.ctrl.getFd();
276}
277
278template <int PANEL>
Naseer Ahmed29a26812012-06-14 00:56:20 -0700279inline utils::Dim GenericPipe<PANEL>::getAspectRatio(
280 const utils::Whf& whf) const
281{
282 return mCtrlData.ctrl.getAspectRatio(whf);
283}
284
285template <int PANEL>
286inline utils::Dim GenericPipe<PANEL>::getAspectRatio(
287 const utils::Dim& dim) const
288{
289 return mCtrlData.ctrl.getAspectRatio(dim);
290}
291
292template <int PANEL>
293inline utils::ScreenInfo GenericPipe<PANEL>::getScreenInfo() const
294{
295 return mCtrlData.ctrl.getScreenInfo();
296}
297
298template <int PANEL>
299inline utils::Dim GenericPipe<PANEL>::getCrop() const
300{
301 return mCtrlData.ctrl.getCrop();
302}
303
304template <int PANEL>
Naseer Ahmed29a26812012-06-14 00:56:20 -0700305void GenericPipe<PANEL>::dump() const
306{
307 ALOGE("== Dump Generic pipe start ==");
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700308 ALOGE("pipe state = %d", (int)pipeState);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700309 OVASSERT(mRot, "GenericPipe should have a valid Rot");
310 mCtrlData.ctrl.dump();
311 mCtrlData.data.dump();
312 mRot->dump();
313 ALOGE("== Dump Generic pipe end ==");
314}
315
316template <int PANEL>
317inline bool GenericPipe<PANEL>::isClosed() const {
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700318 return (pipeState == CLOSED);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700319}
320
321template <int PANEL>
322inline bool GenericPipe<PANEL>::isOpen() const {
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700323 return (pipeState == OPEN);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700324}
325
326template <int PANEL>
327inline bool GenericPipe<PANEL>::setClosed() {
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700328 pipeState = CLOSED;
329 return true;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700330}
331
Naseer Ahmed29a26812012-06-14 00:56:20 -0700332} //namespace overlay
333
334#endif // OVERLAY_GENERIC_PIPE_H