blob: e5f711916502e4e5bef060a38dd30f0aff964864 [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_UTILS_H
31#define OVERLAY_UTILS_H
32
33#include <cutils/log.h> // ALOGE, etc
34#include <errno.h>
35#include <fcntl.h> // open, O_RDWR, etc
36#include <hardware/hardware.h>
37#include <hardware/gralloc.h> // buffer_handle_t
Naseer Ahmedf48aef62012-07-20 09:05:53 -070038#include <linux/msm_mdp.h> // flags
Naseer Ahmed29a26812012-06-14 00:56:20 -070039#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <sys/stat.h>
43#include <sys/types.h>
44#include <utils/Log.h>
Naseer Ahmedf48aef62012-07-20 09:05:53 -070045#include "gralloc_priv.h" //for interlace
Naseer Ahmed29a26812012-06-14 00:56:20 -070046/*
47*
48* Collection of utilities functions/structs/enums etc...
49*
50* */
51
52// comment that out if you want to remove asserts
53// or put it as -D in Android.mk. your choice.
54#define OVERLAY_HAS_ASSERT
55
56#ifdef OVERLAY_HAS_ASSERT
57# define OVASSERT(x, ...) if(!(x)) { ALOGE(__VA_ARGS__); abort(); }
58#else
59# define OVASSERT(x, ...) ALOGE_IF(!(x), __VA_ARGS__)
60#endif // OVERLAY_HAS_ASSERT
61
62#define DEBUG_OVERLAY 0
63#define PROFILE_OVERLAY 0
64
65namespace overlay {
66
67// fwd
68class Overlay;
Saurabh Shahe012f7a2012-08-18 15:11:57 -070069class OvFD;
70
71/* helper function to open by using fbnum */
72bool open(OvFD& fd, uint32_t fbnum, const char* const dev,
73 int flags = O_RDWR);
Naseer Ahmed29a26812012-06-14 00:56:20 -070074
75namespace utils {
76struct Whf;
77struct Dim;
Naseer Ahmed29a26812012-06-14 00:56:20 -070078
79inline uint32_t setBit(uint32_t x, uint32_t mask) {
80 return (x | mask);
81}
82
83inline uint32_t clrBit(uint32_t x, uint32_t mask) {
84 return (x & ~mask);
85}
86
87/* Utility class to help avoid copying instances by making the copy ctor
88* and assignment operator private
89*
90* Usage:
Naseer Ahmedf48aef62012-07-20 09:05:53 -070091* class SomeClass : utils::NoCopy {...};
Naseer Ahmed29a26812012-06-14 00:56:20 -070092*/
93class NoCopy {
94protected:
95 NoCopy(){}
96 ~NoCopy() {}
97private:
98 NoCopy(const NoCopy&);
99 const NoCopy& operator=(const NoCopy&);
100};
101
102/*
103* Utility class to query the framebuffer info for primary display
104*
105* Usage:
106* Outside of functions:
107* utils::FrameBufferInfo* utils::FrameBufferInfo::sFBInfoInstance = 0;
108* Inside functions:
109* utils::FrameBufferInfo::getInstance()->supportTrueMirroring()
110*/
111class FrameBufferInfo {
112
113public:
114 /* ctor init */
115 explicit FrameBufferInfo();
116
117 /* Gets an instance if one does not already exist */
118 static FrameBufferInfo* getInstance();
119
120 /* Gets width of primary framebuffer */
121 int getWidth() const;
122
123 /* Gets height of primary framebuffer */
124 int getHeight() const;
125
126 /* Indicates whether true mirroring is supported */
127 bool supportTrueMirroring() const;
128
129private:
130 int mFBWidth;
131 int mFBHeight;
132 bool mBorderFillSupported;
133 static FrameBufferInfo *sFBInfoInstance;
134};
135
136/* 3D related utils, defines etc...
137 * The compound format passed to the overlay is
138 * ABCCC where A is the input 3D format
139 * B is the output 3D format
140 * CCC is the color format e.g YCbCr420SP YCrCb420SP etc */
141enum { SHIFT_OUT_3D = 12,
142 SHIFT_TOT_3D = 16 };
143enum { INPUT_3D_MASK = 0xFFFF0000,
144 OUTPUT_3D_MASK = 0x0000FFFF };
145enum { BARRIER_LAND = 1,
146 BARRIER_PORT = 2 };
147
Ajay Dudanicb3da0a2012-09-07 13:37:49 -0700148/* if SurfaceFlinger process gets killed in bypass mode, In initOverlay()
149 * close all the pipes if it is opened after reboot.
150 */
151int initOverlay(void);
152
Naseer Ahmed29a26812012-06-14 00:56:20 -0700153inline uint32_t format3D(uint32_t x) { return x & 0xFF000; }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700154inline uint32_t colorFormat(uint32_t fmt) {
155 /*TODO enable this block only if format has interlace / 3D info in top bits.
156 if(fmt & INTERLACE_MASK) {
157 fmt = fmt ^ HAL_PIXEL_FORMAT_INTERLACE;
158 }
159 fmt = fmt & 0xFFF;*/
160 return fmt;
161}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700162inline uint32_t format3DOutput(uint32_t x) {
163 return (x & 0xF000) >> SHIFT_OUT_3D; }
164inline uint32_t format3DInput(uint32_t x) { return x & 0xF0000; }
165uint32_t getColorFormat(uint32_t format);
166
167bool isHDMIConnected ();
168bool is3DTV();
169bool isPanel3D();
170bool usePanel3D();
171bool send3DInfoPacket (uint32_t fmt);
172bool enableBarrier (uint32_t orientation);
173uint32_t getS3DFormat(uint32_t fmt);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700174
Naseer Ahmed29a26812012-06-14 00:56:20 -0700175template <int CHAN>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700176bool getPositionS3D(const Whf& whf, Dim& out);
177
Naseer Ahmed29a26812012-06-14 00:56:20 -0700178template <int CHAN>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700179bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt);
180
Naseer Ahmed29a26812012-06-14 00:56:20 -0700181template <class Type>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700182void swapWidthHeight(Type& width, Type& height);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700183
184struct Dim {
185 Dim () : x(0), y(0),
186 w(0), h(0),
187 o(0) {}
188 Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h) :
189 x(_x), y(_y),
190 w(_w), h(_h) {}
191 Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h, uint32_t _o) :
192 x(_x), y(_y),
193 w(_w), h(_h),
194 o(_o) {}
195 bool check(uint32_t _w, uint32_t _h) const {
196 return (x+w <= _w && y+h <= _h);
197
198 }
199
200 bool operator==(const Dim& d) const {
201 return d.x == x && d.y == y &&
202 d.w == w && d.h == h &&
203 d.o == o;
204 }
205
206 bool operator!=(const Dim& d) const {
207 return !operator==(d);
208 }
209
Naseer Ahmed29a26812012-06-14 00:56:20 -0700210 void dump() const;
211 uint32_t x;
212 uint32_t y;
213 uint32_t w;
214 uint32_t h;
215 uint32_t o;
216};
217
218// TODO have Whfz
219
220struct Whf {
221 Whf() : w(0), h(0), format(0), size(0) {}
222 Whf(uint32_t wi, uint32_t he, uint32_t f) :
223 w(wi), h(he), format(f), size(0) {}
224 Whf(uint32_t wi, uint32_t he, uint32_t f, uint32_t s) :
225 w(wi), h(he), format(f), size(s) {}
226 // FIXME not comparing size at the moment
227 bool operator==(const Whf& whf) const {
228 return whf.w == w && whf.h == h &&
229 whf.format == format;
230 }
231 bool operator!=(const Whf& whf) const {
232 return !operator==(whf);
233 }
234 void dump() const;
235 uint32_t w;
236 uint32_t h;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700237 uint32_t format;
238 uint32_t size;
239};
240
241enum { MAX_PATH_LEN = 256 };
242
Naseer Ahmed29a26812012-06-14 00:56:20 -0700243/**
244 * Rotator flags: not to be confused with orientation flags.
245 * Ususally, you want to open the rotator to make sure it is
246 * ready for business.
247 * ROT_FLAG_DISABLED: Rotator would not kick in. (ioctl will emit errors).
248 * ROT_FLAG_ENABLED: and when rotation is needed.
249 * (prim video playback)
250 * (UI mirroring on HDMI w/ 0 degree rotator. - just memcpy)
251 * In HDMI UI mirroring, rotator is always used.
252 * Even when w/o orienation change on primary,
253 * we do 0 rotation on HDMI and using rotator buffers.
254 * That is because we might see tearing otherwise. so
255 * we use another buffer (rotator).
256 * When a simple video playback on HDMI, no rotator is being used.(null r).
257 * */
258enum eRotFlags {
259 ROT_FLAG_DISABLED = 0,
260 ROT_FLAG_ENABLED = 1 // needed in rot
261};
262
Naseer Ahmed29a26812012-06-14 00:56:20 -0700263/* The values for is_fg flag for control alpha and transp
264 * IS_FG_OFF means is_fg = 0
265 * IS_FG_SET means is_fg = 1
266 */
267enum eIsFg {
268 IS_FG_OFF = 0,
269 IS_FG_SET = 1
270};
271
272/*
273 * Various mdp flags like PIPE SHARE, DEINTERLACE etc...
274 * kernel/common/linux/msm_mdp.h
275 * INTERLACE_MASK: hardware/qcom/display/libgralloc/badger/fb_priv.h
276 * */
277enum eMdpFlags {
278 OV_MDP_FLAGS_NONE = 0,
279 OV_MDP_PIPE_SHARE = MDP_OV_PIPE_SHARE,
280 OV_MDP_DEINTERLACE = MDP_DEINTERLACE,
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700281 OV_MDP_SECURE_OVERLAY_SESSION = MDP_SECURE_OVERLAY_SESSION,
282 OV_MDP_SOURCE_ROTATED_90 = MDP_SOURCE_ROTATED_90,
283 OV_MDP_MEMORY_ID_TYPE_FB = MDP_MEMORY_ID_TYPE_FB,
Saurabh Shah799a3972012-09-01 12:16:12 -0700284 OV_MDP_BACKEND_COMPOSITION = MDP_BACKEND_COMPOSITION,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700285};
286
Naseer Ahmed29a26812012-06-14 00:56:20 -0700287enum eZorder {
288 ZORDER_0,
289 ZORDER_1,
290 ZORDER_2,
291 Z_SYSTEM_ALLOC = 0xFFFF
292};
293
294enum eMdpPipeType {
295 OV_MDP_PIPE_RGB,
296 OV_MDP_PIPE_VG
297};
298
Naseer Ahmed29a26812012-06-14 00:56:20 -0700299// Max pipes via overlay (VG0, VG1, RGB1)
300enum { MAX_PIPES = 3 };
301
302/* Used to identify destination channels and
303 * also 3D channels e.g. when in 3D mode with 2
304 * pipes opened and it is used in get crop/pos 3D
Naseer Ahmed29a26812012-06-14 00:56:20 -0700305 * */
306enum eDest {
307 OV_PIPE0 = 1 << 0,
308 OV_PIPE1 = 1 << 1,
309 OV_PIPE2 = 1 << 2,
310 OV_PIPE_ALL = (OV_PIPE0 | OV_PIPE1 | OV_PIPE2)
311};
312
313/* values for copybit_set_parameter(OVERLAY_TRANSFORM) */
314enum eTransform {
315 /* No rot */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700316 OVERLAY_TRANSFORM_0 = 0x0,
317 /* flip source image horizontally 0x1 */
318 OVERLAY_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H,
319 /* flip source image vertically 0x2 */
320 OVERLAY_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700321 /* rotate source image 180 degrees
322 * It is basically bit-or-ed H | V == 0x3 */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700323 OVERLAY_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
324 /* rotate source image 90 degrees 0x4 */
325 OVERLAY_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
326 /* rotate source image 90 degrees and flip horizontally 0x5 */
327 OVERLAY_TRANSFORM_ROT_90_FLIP_H = HAL_TRANSFORM_ROT_90 |
328 HAL_TRANSFORM_FLIP_H,
329 /* rotate source image 90 degrees and flip vertically 0x6 */
330 OVERLAY_TRANSFORM_ROT_90_FLIP_V = HAL_TRANSFORM_ROT_90 |
331 HAL_TRANSFORM_FLIP_V,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700332 /* rotate source image 270 degrees
333 * Basically 180 | 90 == 0x7 */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700334 OVERLAY_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700335 /* rotate invalid like in Transform.h */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700336 OVERLAY_TRANSFORM_INV = 0x80
Naseer Ahmed29a26812012-06-14 00:56:20 -0700337};
338
339// Used to consolidate pipe params
340struct PipeArgs {
341 PipeArgs() : mdpFlags(OV_MDP_FLAGS_NONE),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700342 zorder(Z_SYSTEM_ALLOC),
343 isFg(IS_FG_OFF),
344 rotFlags(ROT_FLAG_DISABLED){
345 }
346
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700347 PipeArgs(eMdpFlags f, Whf _whf,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700348 eZorder z, eIsFg fg, eRotFlags r) :
349 mdpFlags(f),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700350 whf(_whf),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700351 zorder(z),
352 isFg(fg),
353 rotFlags(r) {
354 }
355
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700356 eMdpFlags mdpFlags; // for mdp_overlay flags
Naseer Ahmed29a26812012-06-14 00:56:20 -0700357 Whf whf;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700358 eZorder zorder; // stage number
359 eIsFg isFg; // control alpha & transp
360 eRotFlags rotFlags;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700361};
362
363enum eOverlayState{
364 /* No pipes from overlay open */
365 OV_CLOSED = 0,
366
367 /* 2D Video */
368 OV_2D_VIDEO_ON_PANEL,
369 OV_2D_VIDEO_ON_PANEL_TV,
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700370 OV_2D_VIDEO_ON_TV,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700371
372 /* 3D Video on one display (panel or TV) */
373 OV_3D_VIDEO_ON_2D_PANEL,
374 OV_3D_VIDEO_ON_3D_PANEL,
375 OV_3D_VIDEO_ON_3D_TV,
376
377 /* 3D Video on two displays (panel and TV) */
378 OV_3D_VIDEO_ON_2D_PANEL_2D_TV,
379
380 /* UI Mirroring */
381 OV_UI_MIRROR,
382 OV_2D_TRUE_UI_MIRROR,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700383
384 /* Composition Bypass */
385 OV_BYPASS_1_LAYER,
386 OV_BYPASS_2_LAYER,
387 OV_BYPASS_3_LAYER,
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700388
389 /* External only for dual-disp */
390 OV_DUAL_DISP,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700391};
392
393inline void setMdpFlags(eMdpFlags& f, eMdpFlags v) {
394 f = static_cast<eMdpFlags>(setBit(f, v));
395}
396
397inline void clearMdpFlags(eMdpFlags& f, eMdpFlags v) {
398 f = static_cast<eMdpFlags>(clrBit(f, v));
399}
400
401// fb 0/1/2
402enum { FB0, FB1, FB2 };
403
404//Panels could be categorized as primary and external
405enum { PRIMARY, EXTERNAL };
406
407//External Panels could use HDMI or WFD
408enum {
409 HDMI = 1,
410 WFD = 2
411};
412
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700413//TODO Make this a part of some appropriate class
Naseer Ahmed29a26812012-06-14 00:56:20 -0700414static int sExtType = HDMI; //HDMI or WFD
Naseer Ahmed29a26812012-06-14 00:56:20 -0700415//Set by client as HDMI/WFD
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700416void setExtType(const int& type);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700417//Return External panel type set by client.
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700418int getExtType();
419
Naseer Ahmed29a26812012-06-14 00:56:20 -0700420
421//Gets the FB number for the external type.
422//As of now, HDMI always has fb1, WFD could use fb1 or fb2
423//Assumes Ext type set by setExtType() from client.
424static int getFBForPanel(int panel) { // PRIMARY OR EXTERNAL
425 switch(panel) {
426 case PRIMARY: return FB0;
427 break;
428 case EXTERNAL:
429 switch(getExtType()) {
430 case HDMI: return FB1;
431 break;
432 case WFD: return FB2;//Hardcoding fb2 for wfd. Will change.
433 break;
434 }
435 break;
436 default:
437 ALOGE("%s: Unrecognized PANEL category %d", __func__, panel);
438 break;
439 }
440 return -1;
441}
442
443// number of rgb pipes bufs (max)
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700444
Naseer Ahmed29a26812012-06-14 00:56:20 -0700445// 2 for rgb0/1 double bufs
446enum { RGB_PIPE_NUM_BUFS = 2 };
447
448struct ScreenInfo {
449 ScreenInfo() : mFBWidth(0),
450 mFBHeight(0),
451 mFBbpp(0),
452 mFBystride(0) {}
453 void dump(const char* const s) const;
454 uint32_t mFBWidth;
455 uint32_t mFBHeight;
456 uint32_t mFBbpp;
457 uint32_t mFBystride;
458};
459
460int getMdpFormat(int format);
461int getRotOutFmt(uint32_t format);
462/* flip is upside down and such. V, H flip
463 * rotation is 90, 180 etc
464 * It returns MDP related enum/define that match rot+flip*/
465int getMdpOrient(eTransform rotation);
Saurabh Shah9c876d92012-08-25 19:29:53 -0700466const char* getFormatString(int format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700467const char* getStateString(eOverlayState state);
468
Naseer Ahmed29a26812012-06-14 00:56:20 -0700469// Cannot use HW_OVERLAY_MAGNIFICATION_LIMIT, since at the time
470// of integration, HW_OVERLAY_MAGNIFICATION_LIMIT was a define
471enum { HW_OV_MAGNIFICATION_LIMIT = 20,
472 HW_OV_MINIFICATION_LIMIT = 8
473};
474
Naseer Ahmed29a26812012-06-14 00:56:20 -0700475template <class T>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700476inline void memset0(T& t) { ::memset(&t, 0, sizeof(T)); }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700477
478template <class T> inline void swap ( T& a, T& b )
479{
480 T c(a); a=b; b=c;
481}
482
483inline int alignup(int value, int a) {
484 //if align = 0, return the value. Else, do alignment.
485 return a ? ((((value - 1) / a) + 1) * a) : value;
486}
487
488// FIXME that align should replace the upper one.
489inline int align(int value, int a) {
490 //if align = 0, return the value. Else, do alignment.
491 return a ? ((value + (a-1)) & ~(a-1)) : value;
492}
493
Naseer Ahmed29a26812012-06-14 00:56:20 -0700494enum eRotOutFmt {
495 ROT_OUT_FMT_DEFAULT,
496 ROT_OUT_FMT_Y_CRCB_H2V2
497};
498
499template <int ROT_OUT_FMT> struct RotOutFmt;
500
501// FIXME, taken from gralloc_priv.h. Need to
502// put it back as soon as overlay takes place of the old one
503/* possible formats for 3D content*/
504enum {
505 HAL_NO_3D = 0x0000,
506 HAL_3D_IN_SIDE_BY_SIDE_L_R = 0x10000,
507 HAL_3D_IN_TOP_BOTTOM = 0x20000,
508 HAL_3D_IN_INTERLEAVE = 0x40000,
509 HAL_3D_IN_SIDE_BY_SIDE_R_L = 0x80000,
510 HAL_3D_OUT_SIDE_BY_SIDE = 0x1000,
511 HAL_3D_OUT_TOP_BOTTOM = 0x2000,
512 HAL_3D_OUT_INTERLEAVE = 0x4000,
513 HAL_3D_OUT_MONOSCOPIC = 0x8000
514};
515
516enum { HAL_3D_OUT_SBS_MASK =
517 HAL_3D_OUT_SIDE_BY_SIDE >> overlay::utils::SHIFT_OUT_3D,
518 HAL_3D_OUT_TOP_BOT_MASK =
519 HAL_3D_OUT_TOP_BOTTOM >> overlay::utils::SHIFT_OUT_3D,
520 HAL_3D_OUT_INTERL_MASK =
521 HAL_3D_OUT_INTERLEAVE >> overlay::utils::SHIFT_OUT_3D,
522 HAL_3D_OUT_MONOS_MASK =
523 HAL_3D_OUT_MONOSCOPIC >> overlay::utils::SHIFT_OUT_3D
524};
525
526
527inline bool isYuv(uint32_t format) {
528 switch(format){
529 case MDP_Y_CBCR_H2V1:
530 case MDP_Y_CBCR_H2V2:
531 case MDP_Y_CRCB_H2V2:
Saurabh Shahb121e142012-08-20 17:59:13 -0700532 case MDP_Y_CRCB_H1V1:
533 case MDP_Y_CRCB_H2V1:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700534 case MDP_Y_CRCB_H2V2_TILE:
535 case MDP_Y_CBCR_H2V2_TILE:
Saurabh Shahb121e142012-08-20 17:59:13 -0700536 case MDP_Y_CR_CB_H2V2:
Saurabh Shahae1044e2012-08-21 16:03:32 -0700537 case MDP_Y_CR_CB_GH2V2:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700538 return true;
539 default:
540 return false;
541 }
542 return false;
543}
544
545inline bool isRgb(uint32_t format) {
546 switch(format) {
547 case MDP_RGBA_8888:
548 case MDP_BGRA_8888:
549 case MDP_RGBX_8888:
550 case MDP_RGB_565:
551 return true;
552 default:
553 return false;
554 }
555 return false;
556}
557
558inline bool isValidDest(eDest dest)
559{
560 if ((OV_PIPE0 & dest) ||
561 (OV_PIPE1 & dest) ||
562 (OV_PIPE2 & dest)) {
563 return true;
564 }
565 return false;
566}
567
Saurabh Shah9c876d92012-08-25 19:29:53 -0700568inline const char* getFormatString(int format){
Naseer Ahmed29a26812012-06-14 00:56:20 -0700569 static const char* const formats[] = {
570 "MDP_RGB_565",
571 "MDP_XRGB_8888",
572 "MDP_Y_CBCR_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700573 "MDP_Y_CBCR_H2V2_ADRENO",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700574 "MDP_ARGB_8888",
575 "MDP_RGB_888",
576 "MDP_Y_CRCB_H2V2",
577 "MDP_YCRYCB_H2V1",
578 "MDP_Y_CRCB_H2V1",
579 "MDP_Y_CBCR_H2V1",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700580 "MDP_Y_CRCB_H1V2",
581 "MDP_Y_CBCR_H1V2",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700582 "MDP_RGBA_8888",
583 "MDP_BGRA_8888",
584 "MDP_RGBX_8888",
585 "MDP_Y_CRCB_H2V2_TILE",
586 "MDP_Y_CBCR_H2V2_TILE",
587 "MDP_Y_CR_CB_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700588 "MDP_Y_CR_CB_GH2V2",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700589 "MDP_Y_CB_CR_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700590 "MDP_Y_CRCB_H1V1",
591 "MDP_Y_CBCR_H1V1",
592 "MDP_YCRCB_H1V1",
593 "MDP_YCBCR_H1V1",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700594 "MDP_BGR_565",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700595 "MDP_IMGTYPE_LIMIT",
596 "MDP_RGB_BORDERFILL",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700597 "MDP_FB_FORMAT",
598 "MDP_IMGTYPE_LIMIT2"
599 };
Saurabh Shah9c876d92012-08-25 19:29:53 -0700600 if(format < 0 || format >= (int)(sizeof(formats) / sizeof(formats[0]))) {
601 ALOGE("%s wrong fmt %d", __FUNCTION__, format);
602 return "Unsupported format";
603 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700604 return formats[format];
605}
606
607inline const char* getStateString(eOverlayState state){
608 switch (state) {
609 case OV_CLOSED:
610 return "OV_CLOSED";
611 case OV_2D_VIDEO_ON_PANEL:
612 return "OV_2D_VIDEO_ON_PANEL";
613 case OV_2D_VIDEO_ON_PANEL_TV:
614 return "OV_2D_VIDEO_ON_PANEL_TV";
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700615 case OV_2D_VIDEO_ON_TV:
616 return "OV_2D_VIDEO_ON_TV";
Naseer Ahmed29a26812012-06-14 00:56:20 -0700617 case OV_3D_VIDEO_ON_2D_PANEL:
618 return "OV_3D_VIDEO_ON_2D_PANEL";
619 case OV_3D_VIDEO_ON_3D_PANEL:
620 return "OV_3D_VIDEO_ON_3D_PANEL";
621 case OV_3D_VIDEO_ON_3D_TV:
622 return "OV_3D_VIDEO_ON_3D_TV";
623 case OV_3D_VIDEO_ON_2D_PANEL_2D_TV:
624 return "OV_3D_VIDEO_ON_2D_PANEL_2D_TV";
625 case OV_UI_MIRROR:
626 return "OV_UI_MIRROR";
627 case OV_2D_TRUE_UI_MIRROR:
628 return "OV_2D_TRUE_UI_MIRROR";
629 case OV_BYPASS_1_LAYER:
630 return "OV_BYPASS_1_LAYER";
631 case OV_BYPASS_2_LAYER:
632 return "OV_BYPASS_2_LAYER";
633 case OV_BYPASS_3_LAYER:
634 return "OV_BYPASS_3_LAYER";
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700635 case OV_DUAL_DISP:
636 return "OV_DUAL_DISP";
Naseer Ahmed29a26812012-06-14 00:56:20 -0700637 default:
638 return "UNKNOWN_STATE";
639 }
640 return "BAD_STATE";
641}
642
Naseer Ahmed29a26812012-06-14 00:56:20 -0700643inline void Whf::dump() const {
644 ALOGE("== Dump WHF w=%d h=%d f=%d s=%d start/end ==",
645 w, h, format, size);
646}
647
648inline void Dim::dump() const {
649 ALOGE("== Dump Dim x=%d y=%d w=%d h=%d start/end ==", x, y, w, h);
650}
651
652inline int getMdpOrient(eTransform rotation) {
653 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, rotation);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700654 switch(rotation)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700655 {
656 case OVERLAY_TRANSFORM_0 : return 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700657 case OVERLAY_TRANSFORM_FLIP_V: return MDP_FLIP_UD;
658 case OVERLAY_TRANSFORM_FLIP_H: return MDP_FLIP_LR;
659 case OVERLAY_TRANSFORM_ROT_90: return MDP_ROT_90;
Saurabh Shaha73738d2012-08-09 18:15:18 -0700660 //getMdpOrient will switch the flips if the source is 90 rotated.
661 //Clients in Android dont factor in 90 rotation while deciding flip.
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700662 case OVERLAY_TRANSFORM_ROT_90_FLIP_V:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700663 return MDP_ROT_90 | MDP_FLIP_LR;
Saurabh Shaha73738d2012-08-09 18:15:18 -0700664 case OVERLAY_TRANSFORM_ROT_90_FLIP_H:
665 return MDP_ROT_90 | MDP_FLIP_UD;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700666 case OVERLAY_TRANSFORM_ROT_180: return MDP_ROT_180;
667 case OVERLAY_TRANSFORM_ROT_270: return MDP_ROT_270;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700668 default:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700669 ALOGE("%s: invalid rotation value (value = 0x%x",
670 __FUNCTION__, rotation);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700671 }
672 return -1;
673}
674
675inline int getRotOutFmt(uint32_t format) {
676 switch (format) {
677 case MDP_Y_CRCB_H2V2_TILE:
678 return MDP_Y_CRCB_H2V2;
679 case MDP_Y_CBCR_H2V2_TILE:
680 return MDP_Y_CBCR_H2V2;
681 case MDP_Y_CB_CR_H2V2:
682 return MDP_Y_CBCR_H2V2;
Saurabh Shahae1044e2012-08-21 16:03:32 -0700683 case MDP_Y_CR_CB_GH2V2:
684 return MDP_Y_CRCB_H2V2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700685 default:
686 return format;
687 }
688 // not reached
689 OVASSERT(false, "%s not reached", __FUNCTION__);
690 return -1;
691}
692
Naseer Ahmed29a26812012-06-14 00:56:20 -0700693
694inline uint32_t getColorFormat(uint32_t format)
695{
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700696 return (format == HAL_PIXEL_FORMAT_YV12) ?
697 format : colorFormat(format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700698}
699
700// FB0
701template <int CHAN>
702inline Dim getPositionS3DImpl(const Whf& whf)
703{
704 switch (whf.format & OUTPUT_3D_MASK)
705 {
706 case HAL_3D_OUT_SBS_MASK:
707 // x, y, w, h
708 return Dim(0, 0, whf.w/2, whf.h);
709 case HAL_3D_OUT_TOP_BOT_MASK:
710 return Dim(0, 0, whf.w, whf.h/2);
711 case HAL_3D_OUT_MONOS_MASK:
712 return Dim();
713 case HAL_3D_OUT_INTERL_MASK:
714 // FIXME error?
715 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
716 return Dim();
717 default:
718 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
719 whf.format);
720 }
721 return Dim();
722}
723
724template <>
725inline Dim getPositionS3DImpl<utils::OV_PIPE1>(const Whf& whf)
726{
727 switch (whf.format & OUTPUT_3D_MASK)
728 {
729 case HAL_3D_OUT_SBS_MASK:
730 return Dim(whf.w/2, 0, whf.w/2, whf.h);
731 case HAL_3D_OUT_TOP_BOT_MASK:
732 return Dim(0, whf.h/2, whf.w, whf.h/2);
733 case HAL_3D_OUT_MONOS_MASK:
734 return Dim(0, 0, whf.w, whf.h);
735 case HAL_3D_OUT_INTERL_MASK:
736 // FIXME error?
737 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
738 return Dim();
739 default:
740 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
741 whf.format);
742 }
743 return Dim();
744}
745
746template <int CHAN>
747inline bool getPositionS3D(const Whf& whf, Dim& out) {
748 out = getPositionS3DImpl<CHAN>(whf);
749 return (out != Dim());
750}
751
752template <int CHAN>
753inline Dim getCropS3DImpl(const Dim& in, uint32_t fmt) {
754 switch (fmt & INPUT_3D_MASK)
755 {
756 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
757 return Dim(0, 0, in.w/2, in.h);
758 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
759 return Dim(in.w/2, 0, in.w/2, in.h);
760 case HAL_3D_IN_TOP_BOTTOM:
761 return Dim(0, 0, in.w, in.h/2);
762 case HAL_3D_IN_INTERLEAVE:
763 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
764 break;
765 default:
766 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
767 break;
768 }
769 return Dim();
770}
771
772template <>
773inline Dim getCropS3DImpl<utils::OV_PIPE1>(const Dim& in, uint32_t fmt) {
774 switch (fmt & INPUT_3D_MASK)
775 {
776 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
777 return Dim(in.w/2, 0, in.w/2, in.h);
778 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
779 return Dim(0, 0, in.w/2, in.h);
780 case HAL_3D_IN_TOP_BOTTOM:
781 return Dim(0, in.h/2, in.w, in.h/2);
782 case HAL_3D_IN_INTERLEAVE:
783 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
784 break;
785 default:
786 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
787 break;
788 }
789 return Dim();
790}
791
792template <int CHAN>
793inline bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt)
794{
795 out = getCropS3DImpl<CHAN>(in, fmt);
796 return (out != Dim());
797}
798
799template <class Type>
800void swapWidthHeight(Type& width, Type& height) {
801 Type tmp = width;
802 width = height;
803 height = tmp;
804}
805
806inline void ScreenInfo::dump(const char* const s) const {
807 ALOGE("== Dump %s ScreenInfo w=%d h=%d"
808 " bpp=%d stride=%d start/end ==",
809 s, mFBWidth, mFBHeight, mFBbpp, mFBystride);
810}
811
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700812inline bool openDev(OvFD& fd, int fbnum,
813 const char* const devpath, int flags) {
814 return overlay::open(fd, fbnum, devpath, flags);
815}
816
Saurabh Shahb121e142012-08-20 17:59:13 -0700817template <class T>
818inline void even_ceil(T& value) {
819 if(value & 1)
820 value++;
821}
822
823template <class T>
824inline void even_floor(T& value) {
825 if(value & 1)
826 value--;
827}
828
Naseer Ahmed29a26812012-06-14 00:56:20 -0700829} // namespace utils ends
830
831//--------------------Class Res stuff (namespace overlay only) -----------
832
833class Res {
834public:
835 // /dev/graphics/fb%u
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700836 static const char* const fbPath;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700837 // /dev/msm_rotator
838 static const char* const rotPath;
839 // /sys/class/graphics/fb1/format_3d
840 static const char* const format3DFile;
841 // /sys/class/graphics/fb1/3d_present
842 static const char* const edid3dInfoFile;
843 // /sys/devices/platform/mipi_novatek.0/enable_3d_barrier
844 static const char* const barrierFile;
845};
846
847
848//--------------------Class OvFD stuff (namespace overlay only) -----------
849
Naseer Ahmed29a26812012-06-14 00:56:20 -0700850/*
851* Holds one FD
852* Dtor will NOT close the underlying FD.
853* That enables us to copy that object around
854* */
855class OvFD {
856public:
857 /* Ctor */
858 explicit OvFD();
859
860 /* dtor will NOT close the underlying FD */
861 ~OvFD();
862
863 /* Open fd using the path given by dev.
864 * return false in failure */
865 bool open(const char* const dev,
866 int flags = O_RDWR);
867
868 /* populate path */
869 void setPath(const char* const dev);
870
871 /* Close fd if we have a valid fd. */
872 bool close();
873
874 /* returns underlying fd.*/
875 int getFD() const;
876
877 /* returns true if fd is valid */
878 bool valid() const;
879
880 /* like operator= */
881 void copy(int fd);
882
883 /* dump the state of the instance */
884 void dump() const;
885private:
886 /* helper enum for determine valid/invalid fd */
887 enum { INVAL = -1 };
888
889 /* actual os fd */
890 int mFD;
891
892 /* path, for debugging */
893 char mPath[utils::MAX_PATH_LEN];
894};
895
896//-------------------Inlines--------------------------
897
898inline bool open(OvFD& fd, uint32_t fbnum, const char* const dev, int flags)
899{
900 char dev_name[64] = {0};
901 snprintf(dev_name, sizeof(dev_name), dev, fbnum);
902 return fd.open(dev_name, flags);
903}
904
905inline OvFD::OvFD() : mFD (INVAL) {
906 mPath[0] = 0;
907}
908
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700909inline OvFD::~OvFD() {
910 //no op since copy() can be used to share fd, in 3d cases.
911}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700912
913inline bool OvFD::open(const char* const dev, int flags)
914{
915 mFD = ::open(dev, flags, 0);
916 if (mFD < 0) {
917 // FIXME errno, strerror in bionic?
918 ALOGE("Cant open device %s err=%d", dev, errno);
919 return false;
920 }
921 setPath(dev);
922 return true;
923}
924
925inline void OvFD::setPath(const char* const dev)
926{
927 ::strncpy(mPath, dev, utils::MAX_PATH_LEN);
928}
929
930inline bool OvFD::close()
931{
932 int ret = 0;
933 if(valid()) {
934 ret = ::close(mFD);
935 mFD = INVAL;
936 }
937 return (ret == 0);
938}
939
940inline bool OvFD::valid() const
941{
942 return (mFD != INVAL);
943}
944
945inline int OvFD::getFD() const { return mFD; }
946
947inline void OvFD::copy(int fd) {
948 mFD = fd;
949}
950
951inline void OvFD::dump() const
952{
953 ALOGE("== Dump OvFD fd=%d path=%s start/end ==",
954 mFD, mPath);
955}
956
957//--------------- class OvFD stuff ends ---------------------
958
959} // overlay
960
961
962#endif // OVERLAY_UTILS_H