blob: 0648c8577caaacb1489887acfc9a5eb4895fbbb6 [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
148inline uint32_t format3D(uint32_t x) { return x & 0xFF000; }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700149inline uint32_t colorFormat(uint32_t fmt) {
150 /*TODO enable this block only if format has interlace / 3D info in top bits.
151 if(fmt & INTERLACE_MASK) {
152 fmt = fmt ^ HAL_PIXEL_FORMAT_INTERLACE;
153 }
154 fmt = fmt & 0xFFF;*/
155 return fmt;
156}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700157inline uint32_t format3DOutput(uint32_t x) {
158 return (x & 0xF000) >> SHIFT_OUT_3D; }
159inline uint32_t format3DInput(uint32_t x) { return x & 0xF0000; }
160uint32_t getColorFormat(uint32_t format);
161
162bool isHDMIConnected ();
163bool is3DTV();
164bool isPanel3D();
165bool usePanel3D();
166bool send3DInfoPacket (uint32_t fmt);
167bool enableBarrier (uint32_t orientation);
168uint32_t getS3DFormat(uint32_t fmt);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700169
Naseer Ahmed29a26812012-06-14 00:56:20 -0700170template <int CHAN>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700171bool getPositionS3D(const Whf& whf, Dim& out);
172
Naseer Ahmed29a26812012-06-14 00:56:20 -0700173template <int CHAN>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700174bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt);
175
Naseer Ahmed29a26812012-06-14 00:56:20 -0700176template <class Type>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700177void swapWidthHeight(Type& width, Type& height);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700178
179struct Dim {
180 Dim () : x(0), y(0),
181 w(0), h(0),
182 o(0) {}
183 Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h) :
184 x(_x), y(_y),
185 w(_w), h(_h) {}
186 Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h, uint32_t _o) :
187 x(_x), y(_y),
188 w(_w), h(_h),
189 o(_o) {}
190 bool check(uint32_t _w, uint32_t _h) const {
191 return (x+w <= _w && y+h <= _h);
192
193 }
194
195 bool operator==(const Dim& d) const {
196 return d.x == x && d.y == y &&
197 d.w == w && d.h == h &&
198 d.o == o;
199 }
200
201 bool operator!=(const Dim& d) const {
202 return !operator==(d);
203 }
204
Naseer Ahmed29a26812012-06-14 00:56:20 -0700205 void dump() const;
206 uint32_t x;
207 uint32_t y;
208 uint32_t w;
209 uint32_t h;
210 uint32_t o;
211};
212
213// TODO have Whfz
214
215struct Whf {
216 Whf() : w(0), h(0), format(0), size(0) {}
217 Whf(uint32_t wi, uint32_t he, uint32_t f) :
218 w(wi), h(he), format(f), size(0) {}
219 Whf(uint32_t wi, uint32_t he, uint32_t f, uint32_t s) :
220 w(wi), h(he), format(f), size(s) {}
221 // FIXME not comparing size at the moment
222 bool operator==(const Whf& whf) const {
223 return whf.w == w && whf.h == h &&
224 whf.format == format;
225 }
226 bool operator!=(const Whf& whf) const {
227 return !operator==(whf);
228 }
229 void dump() const;
230 uint32_t w;
231 uint32_t h;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700232 uint32_t format;
233 uint32_t size;
234};
235
236enum { MAX_PATH_LEN = 256 };
237
Naseer Ahmed29a26812012-06-14 00:56:20 -0700238/**
239 * Rotator flags: not to be confused with orientation flags.
240 * Ususally, you want to open the rotator to make sure it is
241 * ready for business.
242 * ROT_FLAG_DISABLED: Rotator would not kick in. (ioctl will emit errors).
243 * ROT_FLAG_ENABLED: and when rotation is needed.
244 * (prim video playback)
245 * (UI mirroring on HDMI w/ 0 degree rotator. - just memcpy)
246 * In HDMI UI mirroring, rotator is always used.
247 * Even when w/o orienation change on primary,
248 * we do 0 rotation on HDMI and using rotator buffers.
249 * That is because we might see tearing otherwise. so
250 * we use another buffer (rotator).
251 * When a simple video playback on HDMI, no rotator is being used.(null r).
252 * */
253enum eRotFlags {
254 ROT_FLAG_DISABLED = 0,
255 ROT_FLAG_ENABLED = 1 // needed in rot
256};
257
Naseer Ahmed29a26812012-06-14 00:56:20 -0700258/* The values for is_fg flag for control alpha and transp
259 * IS_FG_OFF means is_fg = 0
260 * IS_FG_SET means is_fg = 1
261 */
262enum eIsFg {
263 IS_FG_OFF = 0,
264 IS_FG_SET = 1
265};
266
267/*
268 * Various mdp flags like PIPE SHARE, DEINTERLACE etc...
269 * kernel/common/linux/msm_mdp.h
270 * INTERLACE_MASK: hardware/qcom/display/libgralloc/badger/fb_priv.h
271 * */
272enum eMdpFlags {
273 OV_MDP_FLAGS_NONE = 0,
274 OV_MDP_PIPE_SHARE = MDP_OV_PIPE_SHARE,
275 OV_MDP_DEINTERLACE = MDP_DEINTERLACE,
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700276 OV_MDP_SECURE_OVERLAY_SESSION = MDP_SECURE_OVERLAY_SESSION,
277 OV_MDP_SOURCE_ROTATED_90 = MDP_SOURCE_ROTATED_90,
278 OV_MDP_MEMORY_ID_TYPE_FB = MDP_MEMORY_ID_TYPE_FB,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700279};
280
Naseer Ahmed29a26812012-06-14 00:56:20 -0700281enum eZorder {
282 ZORDER_0,
283 ZORDER_1,
284 ZORDER_2,
285 Z_SYSTEM_ALLOC = 0xFFFF
286};
287
288enum eMdpPipeType {
289 OV_MDP_PIPE_RGB,
290 OV_MDP_PIPE_VG
291};
292
Naseer Ahmed29a26812012-06-14 00:56:20 -0700293// Max pipes via overlay (VG0, VG1, RGB1)
294enum { MAX_PIPES = 3 };
295
296/* Used to identify destination channels and
297 * also 3D channels e.g. when in 3D mode with 2
298 * pipes opened and it is used in get crop/pos 3D
Naseer Ahmed29a26812012-06-14 00:56:20 -0700299 * */
300enum eDest {
301 OV_PIPE0 = 1 << 0,
302 OV_PIPE1 = 1 << 1,
303 OV_PIPE2 = 1 << 2,
304 OV_PIPE_ALL = (OV_PIPE0 | OV_PIPE1 | OV_PIPE2)
305};
306
307/* values for copybit_set_parameter(OVERLAY_TRANSFORM) */
308enum eTransform {
309 /* No rot */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700310 OVERLAY_TRANSFORM_0 = 0x0,
311 /* flip source image horizontally 0x1 */
312 OVERLAY_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H,
313 /* flip source image vertically 0x2 */
314 OVERLAY_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700315 /* rotate source image 180 degrees
316 * It is basically bit-or-ed H | V == 0x3 */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700317 OVERLAY_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
318 /* rotate source image 90 degrees 0x4 */
319 OVERLAY_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
320 /* rotate source image 90 degrees and flip horizontally 0x5 */
321 OVERLAY_TRANSFORM_ROT_90_FLIP_H = HAL_TRANSFORM_ROT_90 |
322 HAL_TRANSFORM_FLIP_H,
323 /* rotate source image 90 degrees and flip vertically 0x6 */
324 OVERLAY_TRANSFORM_ROT_90_FLIP_V = HAL_TRANSFORM_ROT_90 |
325 HAL_TRANSFORM_FLIP_V,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700326 /* rotate source image 270 degrees
327 * Basically 180 | 90 == 0x7 */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700328 OVERLAY_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700329 /* rotate invalid like in Transform.h */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700330 OVERLAY_TRANSFORM_INV = 0x80
Naseer Ahmed29a26812012-06-14 00:56:20 -0700331};
332
333// Used to consolidate pipe params
334struct PipeArgs {
335 PipeArgs() : mdpFlags(OV_MDP_FLAGS_NONE),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700336 zorder(Z_SYSTEM_ALLOC),
337 isFg(IS_FG_OFF),
338 rotFlags(ROT_FLAG_DISABLED){
339 }
340
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700341 PipeArgs(eMdpFlags f, Whf _whf,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700342 eZorder z, eIsFg fg, eRotFlags r) :
343 mdpFlags(f),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700344 whf(_whf),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700345 zorder(z),
346 isFg(fg),
347 rotFlags(r) {
348 }
349
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700350 eMdpFlags mdpFlags; // for mdp_overlay flags
Naseer Ahmed29a26812012-06-14 00:56:20 -0700351 Whf whf;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700352 eZorder zorder; // stage number
353 eIsFg isFg; // control alpha & transp
354 eRotFlags rotFlags;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700355};
356
357enum eOverlayState{
358 /* No pipes from overlay open */
359 OV_CLOSED = 0,
360
361 /* 2D Video */
362 OV_2D_VIDEO_ON_PANEL,
363 OV_2D_VIDEO_ON_PANEL_TV,
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700364 OV_2D_VIDEO_ON_TV,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700365
366 /* 3D Video on one display (panel or TV) */
367 OV_3D_VIDEO_ON_2D_PANEL,
368 OV_3D_VIDEO_ON_3D_PANEL,
369 OV_3D_VIDEO_ON_3D_TV,
370
371 /* 3D Video on two displays (panel and TV) */
372 OV_3D_VIDEO_ON_2D_PANEL_2D_TV,
373
374 /* UI Mirroring */
375 OV_UI_MIRROR,
376 OV_2D_TRUE_UI_MIRROR,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700377
378 /* Composition Bypass */
379 OV_BYPASS_1_LAYER,
380 OV_BYPASS_2_LAYER,
381 OV_BYPASS_3_LAYER,
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700382
383 /* External only for dual-disp */
384 OV_DUAL_DISP,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700385};
386
387inline void setMdpFlags(eMdpFlags& f, eMdpFlags v) {
388 f = static_cast<eMdpFlags>(setBit(f, v));
389}
390
391inline void clearMdpFlags(eMdpFlags& f, eMdpFlags v) {
392 f = static_cast<eMdpFlags>(clrBit(f, v));
393}
394
395// fb 0/1/2
396enum { FB0, FB1, FB2 };
397
398//Panels could be categorized as primary and external
399enum { PRIMARY, EXTERNAL };
400
401//External Panels could use HDMI or WFD
402enum {
403 HDMI = 1,
404 WFD = 2
405};
406
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700407//TODO Make this a part of some appropriate class
Naseer Ahmed29a26812012-06-14 00:56:20 -0700408static int sExtType = HDMI; //HDMI or WFD
Naseer Ahmed29a26812012-06-14 00:56:20 -0700409//Set by client as HDMI/WFD
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700410void setExtType(const int& type);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700411//Return External panel type set by client.
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700412int getExtType();
413
Naseer Ahmed29a26812012-06-14 00:56:20 -0700414
415//Gets the FB number for the external type.
416//As of now, HDMI always has fb1, WFD could use fb1 or fb2
417//Assumes Ext type set by setExtType() from client.
418static int getFBForPanel(int panel) { // PRIMARY OR EXTERNAL
419 switch(panel) {
420 case PRIMARY: return FB0;
421 break;
422 case EXTERNAL:
423 switch(getExtType()) {
424 case HDMI: return FB1;
425 break;
426 case WFD: return FB2;//Hardcoding fb2 for wfd. Will change.
427 break;
428 }
429 break;
430 default:
431 ALOGE("%s: Unrecognized PANEL category %d", __func__, panel);
432 break;
433 }
434 return -1;
435}
436
437// number of rgb pipes bufs (max)
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700438
Naseer Ahmed29a26812012-06-14 00:56:20 -0700439// 2 for rgb0/1 double bufs
440enum { RGB_PIPE_NUM_BUFS = 2 };
441
442struct ScreenInfo {
443 ScreenInfo() : mFBWidth(0),
444 mFBHeight(0),
445 mFBbpp(0),
446 mFBystride(0) {}
447 void dump(const char* const s) const;
448 uint32_t mFBWidth;
449 uint32_t mFBHeight;
450 uint32_t mFBbpp;
451 uint32_t mFBystride;
452};
453
454int getMdpFormat(int format);
455int getRotOutFmt(uint32_t format);
456/* flip is upside down and such. V, H flip
457 * rotation is 90, 180 etc
458 * It returns MDP related enum/define that match rot+flip*/
459int getMdpOrient(eTransform rotation);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700460const char* getFormatString(uint32_t format);
461const char* getStateString(eOverlayState state);
462
Naseer Ahmed29a26812012-06-14 00:56:20 -0700463// Cannot use HW_OVERLAY_MAGNIFICATION_LIMIT, since at the time
464// of integration, HW_OVERLAY_MAGNIFICATION_LIMIT was a define
465enum { HW_OV_MAGNIFICATION_LIMIT = 20,
466 HW_OV_MINIFICATION_LIMIT = 8
467};
468
Naseer Ahmed29a26812012-06-14 00:56:20 -0700469template <class T>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700470inline void memset0(T& t) { ::memset(&t, 0, sizeof(T)); }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700471
472template <class T> inline void swap ( T& a, T& b )
473{
474 T c(a); a=b; b=c;
475}
476
477inline int alignup(int value, int a) {
478 //if align = 0, return the value. Else, do alignment.
479 return a ? ((((value - 1) / a) + 1) * a) : value;
480}
481
482// FIXME that align should replace the upper one.
483inline int align(int value, int a) {
484 //if align = 0, return the value. Else, do alignment.
485 return a ? ((value + (a-1)) & ~(a-1)) : value;
486}
487
Naseer Ahmed29a26812012-06-14 00:56:20 -0700488enum eRotOutFmt {
489 ROT_OUT_FMT_DEFAULT,
490 ROT_OUT_FMT_Y_CRCB_H2V2
491};
492
493template <int ROT_OUT_FMT> struct RotOutFmt;
494
495// FIXME, taken from gralloc_priv.h. Need to
496// put it back as soon as overlay takes place of the old one
497/* possible formats for 3D content*/
498enum {
499 HAL_NO_3D = 0x0000,
500 HAL_3D_IN_SIDE_BY_SIDE_L_R = 0x10000,
501 HAL_3D_IN_TOP_BOTTOM = 0x20000,
502 HAL_3D_IN_INTERLEAVE = 0x40000,
503 HAL_3D_IN_SIDE_BY_SIDE_R_L = 0x80000,
504 HAL_3D_OUT_SIDE_BY_SIDE = 0x1000,
505 HAL_3D_OUT_TOP_BOTTOM = 0x2000,
506 HAL_3D_OUT_INTERLEAVE = 0x4000,
507 HAL_3D_OUT_MONOSCOPIC = 0x8000
508};
509
510enum { HAL_3D_OUT_SBS_MASK =
511 HAL_3D_OUT_SIDE_BY_SIDE >> overlay::utils::SHIFT_OUT_3D,
512 HAL_3D_OUT_TOP_BOT_MASK =
513 HAL_3D_OUT_TOP_BOTTOM >> overlay::utils::SHIFT_OUT_3D,
514 HAL_3D_OUT_INTERL_MASK =
515 HAL_3D_OUT_INTERLEAVE >> overlay::utils::SHIFT_OUT_3D,
516 HAL_3D_OUT_MONOS_MASK =
517 HAL_3D_OUT_MONOSCOPIC >> overlay::utils::SHIFT_OUT_3D
518};
519
520
521inline bool isYuv(uint32_t format) {
522 switch(format){
523 case MDP_Y_CBCR_H2V1:
524 case MDP_Y_CBCR_H2V2:
525 case MDP_Y_CRCB_H2V2:
526 case MDP_Y_CRCB_H2V2_TILE:
527 case MDP_Y_CBCR_H2V2_TILE:
528 return true;
529 default:
530 return false;
531 }
532 return false;
533}
534
535inline bool isRgb(uint32_t format) {
536 switch(format) {
537 case MDP_RGBA_8888:
538 case MDP_BGRA_8888:
539 case MDP_RGBX_8888:
540 case MDP_RGB_565:
541 return true;
542 default:
543 return false;
544 }
545 return false;
546}
547
548inline bool isValidDest(eDest dest)
549{
550 if ((OV_PIPE0 & dest) ||
551 (OV_PIPE1 & dest) ||
552 (OV_PIPE2 & dest)) {
553 return true;
554 }
555 return false;
556}
557
558inline const char* getFormatString(uint32_t format){
559 static const char* const formats[] = {
560 "MDP_RGB_565",
561 "MDP_XRGB_8888",
562 "MDP_Y_CBCR_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700563 "MDP_Y_CBCR_H2V2_ADRENO",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700564 "MDP_ARGB_8888",
565 "MDP_RGB_888",
566 "MDP_Y_CRCB_H2V2",
567 "MDP_YCRYCB_H2V1",
568 "MDP_Y_CRCB_H2V1",
569 "MDP_Y_CBCR_H2V1",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700570 "MDP_Y_CRCB_H1V2",
571 "MDP_Y_CBCR_H1V2",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700572 "MDP_RGBA_8888",
573 "MDP_BGRA_8888",
574 "MDP_RGBX_8888",
575 "MDP_Y_CRCB_H2V2_TILE",
576 "MDP_Y_CBCR_H2V2_TILE",
577 "MDP_Y_CR_CB_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700578 "MDP_Y_CR_CB_GH2V2",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700579 "MDP_Y_CB_CR_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700580 "MDP_Y_CRCB_H1V1",
581 "MDP_Y_CBCR_H1V1",
582 "MDP_YCRCB_H1V1",
583 "MDP_YCBCR_H1V1",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700584 "MDP_BGR_565",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700585 "MDP_IMGTYPE_LIMIT",
586 "MDP_RGB_BORDERFILL",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700587 "MDP_FB_FORMAT",
588 "MDP_IMGTYPE_LIMIT2"
589 };
590 OVASSERT(format < sizeof(formats) / sizeof(formats[0]),
591 "getFormatString wrong fmt %d", format);
592 return formats[format];
593}
594
595inline const char* getStateString(eOverlayState state){
596 switch (state) {
597 case OV_CLOSED:
598 return "OV_CLOSED";
599 case OV_2D_VIDEO_ON_PANEL:
600 return "OV_2D_VIDEO_ON_PANEL";
601 case OV_2D_VIDEO_ON_PANEL_TV:
602 return "OV_2D_VIDEO_ON_PANEL_TV";
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700603 case OV_2D_VIDEO_ON_TV:
604 return "OV_2D_VIDEO_ON_TV";
Naseer Ahmed29a26812012-06-14 00:56:20 -0700605 case OV_3D_VIDEO_ON_2D_PANEL:
606 return "OV_3D_VIDEO_ON_2D_PANEL";
607 case OV_3D_VIDEO_ON_3D_PANEL:
608 return "OV_3D_VIDEO_ON_3D_PANEL";
609 case OV_3D_VIDEO_ON_3D_TV:
610 return "OV_3D_VIDEO_ON_3D_TV";
611 case OV_3D_VIDEO_ON_2D_PANEL_2D_TV:
612 return "OV_3D_VIDEO_ON_2D_PANEL_2D_TV";
613 case OV_UI_MIRROR:
614 return "OV_UI_MIRROR";
615 case OV_2D_TRUE_UI_MIRROR:
616 return "OV_2D_TRUE_UI_MIRROR";
617 case OV_BYPASS_1_LAYER:
618 return "OV_BYPASS_1_LAYER";
619 case OV_BYPASS_2_LAYER:
620 return "OV_BYPASS_2_LAYER";
621 case OV_BYPASS_3_LAYER:
622 return "OV_BYPASS_3_LAYER";
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700623 case OV_DUAL_DISP:
624 return "OV_DUAL_DISP";
Naseer Ahmed29a26812012-06-14 00:56:20 -0700625 default:
626 return "UNKNOWN_STATE";
627 }
628 return "BAD_STATE";
629}
630
Naseer Ahmed29a26812012-06-14 00:56:20 -0700631inline void Whf::dump() const {
632 ALOGE("== Dump WHF w=%d h=%d f=%d s=%d start/end ==",
633 w, h, format, size);
634}
635
636inline void Dim::dump() const {
637 ALOGE("== Dump Dim x=%d y=%d w=%d h=%d start/end ==", x, y, w, h);
638}
639
640inline int getMdpOrient(eTransform rotation) {
641 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, rotation);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700642 switch(rotation)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700643 {
644 case OVERLAY_TRANSFORM_0 : return 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700645 case OVERLAY_TRANSFORM_FLIP_V: return MDP_FLIP_UD;
646 case OVERLAY_TRANSFORM_FLIP_H: return MDP_FLIP_LR;
647 case OVERLAY_TRANSFORM_ROT_90: return MDP_ROT_90;
Saurabh Shaha73738d2012-08-09 18:15:18 -0700648 //getMdpOrient will switch the flips if the source is 90 rotated.
649 //Clients in Android dont factor in 90 rotation while deciding flip.
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700650 case OVERLAY_TRANSFORM_ROT_90_FLIP_V:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700651 return MDP_ROT_90 | MDP_FLIP_LR;
Saurabh Shaha73738d2012-08-09 18:15:18 -0700652 case OVERLAY_TRANSFORM_ROT_90_FLIP_H:
653 return MDP_ROT_90 | MDP_FLIP_UD;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700654 case OVERLAY_TRANSFORM_ROT_180: return MDP_ROT_180;
655 case OVERLAY_TRANSFORM_ROT_270: return MDP_ROT_270;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700656 default:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700657 ALOGE("%s: invalid rotation value (value = 0x%x",
658 __FUNCTION__, rotation);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700659 }
660 return -1;
661}
662
663inline int getRotOutFmt(uint32_t format) {
664 switch (format) {
665 case MDP_Y_CRCB_H2V2_TILE:
666 return MDP_Y_CRCB_H2V2;
667 case MDP_Y_CBCR_H2V2_TILE:
668 return MDP_Y_CBCR_H2V2;
669 case MDP_Y_CB_CR_H2V2:
670 return MDP_Y_CBCR_H2V2;
671 default:
672 return format;
673 }
674 // not reached
675 OVASSERT(false, "%s not reached", __FUNCTION__);
676 return -1;
677}
678
Naseer Ahmed29a26812012-06-14 00:56:20 -0700679
680inline uint32_t getColorFormat(uint32_t format)
681{
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700682 return (format == HAL_PIXEL_FORMAT_YV12) ?
683 format : colorFormat(format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700684}
685
686// FB0
687template <int CHAN>
688inline Dim getPositionS3DImpl(const Whf& whf)
689{
690 switch (whf.format & OUTPUT_3D_MASK)
691 {
692 case HAL_3D_OUT_SBS_MASK:
693 // x, y, w, h
694 return Dim(0, 0, whf.w/2, whf.h);
695 case HAL_3D_OUT_TOP_BOT_MASK:
696 return Dim(0, 0, whf.w, whf.h/2);
697 case HAL_3D_OUT_MONOS_MASK:
698 return Dim();
699 case HAL_3D_OUT_INTERL_MASK:
700 // FIXME error?
701 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
702 return Dim();
703 default:
704 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
705 whf.format);
706 }
707 return Dim();
708}
709
710template <>
711inline Dim getPositionS3DImpl<utils::OV_PIPE1>(const Whf& whf)
712{
713 switch (whf.format & OUTPUT_3D_MASK)
714 {
715 case HAL_3D_OUT_SBS_MASK:
716 return Dim(whf.w/2, 0, whf.w/2, whf.h);
717 case HAL_3D_OUT_TOP_BOT_MASK:
718 return Dim(0, whf.h/2, whf.w, whf.h/2);
719 case HAL_3D_OUT_MONOS_MASK:
720 return Dim(0, 0, whf.w, whf.h);
721 case HAL_3D_OUT_INTERL_MASK:
722 // FIXME error?
723 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
724 return Dim();
725 default:
726 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
727 whf.format);
728 }
729 return Dim();
730}
731
732template <int CHAN>
733inline bool getPositionS3D(const Whf& whf, Dim& out) {
734 out = getPositionS3DImpl<CHAN>(whf);
735 return (out != Dim());
736}
737
738template <int CHAN>
739inline Dim getCropS3DImpl(const Dim& in, uint32_t fmt) {
740 switch (fmt & INPUT_3D_MASK)
741 {
742 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
743 return Dim(0, 0, in.w/2, in.h);
744 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
745 return Dim(in.w/2, 0, in.w/2, in.h);
746 case HAL_3D_IN_TOP_BOTTOM:
747 return Dim(0, 0, in.w, in.h/2);
748 case HAL_3D_IN_INTERLEAVE:
749 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
750 break;
751 default:
752 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
753 break;
754 }
755 return Dim();
756}
757
758template <>
759inline Dim getCropS3DImpl<utils::OV_PIPE1>(const Dim& in, uint32_t fmt) {
760 switch (fmt & INPUT_3D_MASK)
761 {
762 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
763 return Dim(in.w/2, 0, in.w/2, in.h);
764 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
765 return Dim(0, 0, in.w/2, in.h);
766 case HAL_3D_IN_TOP_BOTTOM:
767 return Dim(0, in.h/2, in.w, in.h/2);
768 case HAL_3D_IN_INTERLEAVE:
769 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
770 break;
771 default:
772 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
773 break;
774 }
775 return Dim();
776}
777
778template <int CHAN>
779inline bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt)
780{
781 out = getCropS3DImpl<CHAN>(in, fmt);
782 return (out != Dim());
783}
784
785template <class Type>
786void swapWidthHeight(Type& width, Type& height) {
787 Type tmp = width;
788 width = height;
789 height = tmp;
790}
791
792inline void ScreenInfo::dump(const char* const s) const {
793 ALOGE("== Dump %s ScreenInfo w=%d h=%d"
794 " bpp=%d stride=%d start/end ==",
795 s, mFBWidth, mFBHeight, mFBbpp, mFBystride);
796}
797
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700798inline bool openDev(OvFD& fd, int fbnum,
799 const char* const devpath, int flags) {
800 return overlay::open(fd, fbnum, devpath, flags);
801}
802
Naseer Ahmed29a26812012-06-14 00:56:20 -0700803} // namespace utils ends
804
805//--------------------Class Res stuff (namespace overlay only) -----------
806
807class Res {
808public:
809 // /dev/graphics/fb%u
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700810 static const char* const fbPath;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700811 // /dev/msm_rotator
812 static const char* const rotPath;
813 // /sys/class/graphics/fb1/format_3d
814 static const char* const format3DFile;
815 // /sys/class/graphics/fb1/3d_present
816 static const char* const edid3dInfoFile;
817 // /sys/devices/platform/mipi_novatek.0/enable_3d_barrier
818 static const char* const barrierFile;
819};
820
821
822//--------------------Class OvFD stuff (namespace overlay only) -----------
823
Naseer Ahmed29a26812012-06-14 00:56:20 -0700824/*
825* Holds one FD
826* Dtor will NOT close the underlying FD.
827* That enables us to copy that object around
828* */
829class OvFD {
830public:
831 /* Ctor */
832 explicit OvFD();
833
834 /* dtor will NOT close the underlying FD */
835 ~OvFD();
836
837 /* Open fd using the path given by dev.
838 * return false in failure */
839 bool open(const char* const dev,
840 int flags = O_RDWR);
841
842 /* populate path */
843 void setPath(const char* const dev);
844
845 /* Close fd if we have a valid fd. */
846 bool close();
847
848 /* returns underlying fd.*/
849 int getFD() const;
850
851 /* returns true if fd is valid */
852 bool valid() const;
853
854 /* like operator= */
855 void copy(int fd);
856
857 /* dump the state of the instance */
858 void dump() const;
859private:
860 /* helper enum for determine valid/invalid fd */
861 enum { INVAL = -1 };
862
863 /* actual os fd */
864 int mFD;
865
866 /* path, for debugging */
867 char mPath[utils::MAX_PATH_LEN];
868};
869
870//-------------------Inlines--------------------------
871
872inline bool open(OvFD& fd, uint32_t fbnum, const char* const dev, int flags)
873{
874 char dev_name[64] = {0};
875 snprintf(dev_name, sizeof(dev_name), dev, fbnum);
876 return fd.open(dev_name, flags);
877}
878
879inline OvFD::OvFD() : mFD (INVAL) {
880 mPath[0] = 0;
881}
882
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700883inline OvFD::~OvFD() {
884 //no op since copy() can be used to share fd, in 3d cases.
885}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700886
887inline bool OvFD::open(const char* const dev, int flags)
888{
889 mFD = ::open(dev, flags, 0);
890 if (mFD < 0) {
891 // FIXME errno, strerror in bionic?
892 ALOGE("Cant open device %s err=%d", dev, errno);
893 return false;
894 }
895 setPath(dev);
896 return true;
897}
898
899inline void OvFD::setPath(const char* const dev)
900{
901 ::strncpy(mPath, dev, utils::MAX_PATH_LEN);
902}
903
904inline bool OvFD::close()
905{
906 int ret = 0;
907 if(valid()) {
908 ret = ::close(mFD);
909 mFD = INVAL;
910 }
911 return (ret == 0);
912}
913
914inline bool OvFD::valid() const
915{
916 return (mFD != INVAL);
917}
918
919inline int OvFD::getFD() const { return mFD; }
920
921inline void OvFD::copy(int fd) {
922 mFD = fd;
923}
924
925inline void OvFD::dump() const
926{
927 ALOGE("== Dump OvFD fd=%d path=%s start/end ==",
928 mFD, mPath);
929}
930
931//--------------- class OvFD stuff ends ---------------------
932
933} // overlay
934
935
936#endif // OVERLAY_UTILS_H