blob: 8db37c51aca5acc7a234ce8b1f60190d66e1ae57 [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
Saurabh Shah56f610d2012-08-07 15:27:06 -07002* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
Naseer Ahmed29a26812012-06-14 00:56:20 -07003*
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
Saurabh Shah56f610d2012-08-07 15:27:06 -0700241class ActionSafe {
242private:
243 ActionSafe() : mWidth(0.0f), mHeight(0.0f) { };
244 float mWidth;
245 float mHeight;
246 static ActionSafe *sActionSafe;
247public:
248 ~ActionSafe() { };
249 static ActionSafe* getInstance() {
250 if(!sActionSafe) {
251 sActionSafe = new ActionSafe();
252 }
253 return sActionSafe;
254 }
255 void setDimension(int w, int h) {
256 mWidth = (float)w;
257 mHeight = (float)h;
258 }
259 float getWidth() { return mWidth; }
260 float getHeight() { return mHeight; }
261};
262
Naseer Ahmed29a26812012-06-14 00:56:20 -0700263enum { MAX_PATH_LEN = 256 };
264
Naseer Ahmed29a26812012-06-14 00:56:20 -0700265/**
266 * Rotator flags: not to be confused with orientation flags.
267 * Ususally, you want to open the rotator to make sure it is
268 * ready for business.
269 * ROT_FLAG_DISABLED: Rotator would not kick in. (ioctl will emit errors).
270 * ROT_FLAG_ENABLED: and when rotation is needed.
271 * (prim video playback)
272 * (UI mirroring on HDMI w/ 0 degree rotator. - just memcpy)
273 * In HDMI UI mirroring, rotator is always used.
274 * Even when w/o orienation change on primary,
275 * we do 0 rotation on HDMI and using rotator buffers.
276 * That is because we might see tearing otherwise. so
277 * we use another buffer (rotator).
278 * When a simple video playback on HDMI, no rotator is being used.(null r).
279 * */
280enum eRotFlags {
281 ROT_FLAG_DISABLED = 0,
282 ROT_FLAG_ENABLED = 1 // needed in rot
283};
284
Naseer Ahmed29a26812012-06-14 00:56:20 -0700285/* The values for is_fg flag for control alpha and transp
286 * IS_FG_OFF means is_fg = 0
287 * IS_FG_SET means is_fg = 1
288 */
289enum eIsFg {
290 IS_FG_OFF = 0,
291 IS_FG_SET = 1
292};
293
294/*
295 * Various mdp flags like PIPE SHARE, DEINTERLACE etc...
296 * kernel/common/linux/msm_mdp.h
297 * INTERLACE_MASK: hardware/qcom/display/libgralloc/badger/fb_priv.h
298 * */
299enum eMdpFlags {
300 OV_MDP_FLAGS_NONE = 0,
301 OV_MDP_PIPE_SHARE = MDP_OV_PIPE_SHARE,
302 OV_MDP_DEINTERLACE = MDP_DEINTERLACE,
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700303 OV_MDP_SECURE_OVERLAY_SESSION = MDP_SECURE_OVERLAY_SESSION,
304 OV_MDP_SOURCE_ROTATED_90 = MDP_SOURCE_ROTATED_90,
305 OV_MDP_MEMORY_ID_TYPE_FB = MDP_MEMORY_ID_TYPE_FB,
Saurabh Shah799a3972012-09-01 12:16:12 -0700306 OV_MDP_BACKEND_COMPOSITION = MDP_BACKEND_COMPOSITION,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700307};
308
Naseer Ahmed29a26812012-06-14 00:56:20 -0700309enum eZorder {
310 ZORDER_0,
311 ZORDER_1,
312 ZORDER_2,
313 Z_SYSTEM_ALLOC = 0xFFFF
314};
315
316enum eMdpPipeType {
317 OV_MDP_PIPE_RGB,
318 OV_MDP_PIPE_VG
319};
320
Naseer Ahmed29a26812012-06-14 00:56:20 -0700321// Max pipes via overlay (VG0, VG1, RGB1)
322enum { MAX_PIPES = 3 };
323
324/* Used to identify destination channels and
325 * also 3D channels e.g. when in 3D mode with 2
326 * pipes opened and it is used in get crop/pos 3D
Naseer Ahmed29a26812012-06-14 00:56:20 -0700327 * */
328enum eDest {
329 OV_PIPE0 = 1 << 0,
330 OV_PIPE1 = 1 << 1,
331 OV_PIPE2 = 1 << 2,
332 OV_PIPE_ALL = (OV_PIPE0 | OV_PIPE1 | OV_PIPE2)
333};
334
335/* values for copybit_set_parameter(OVERLAY_TRANSFORM) */
336enum eTransform {
337 /* No rot */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700338 OVERLAY_TRANSFORM_0 = 0x0,
339 /* flip source image horizontally 0x1 */
340 OVERLAY_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H,
341 /* flip source image vertically 0x2 */
342 OVERLAY_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700343 /* rotate source image 180 degrees
344 * It is basically bit-or-ed H | V == 0x3 */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700345 OVERLAY_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
346 /* rotate source image 90 degrees 0x4 */
347 OVERLAY_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
348 /* rotate source image 90 degrees and flip horizontally 0x5 */
349 OVERLAY_TRANSFORM_ROT_90_FLIP_H = HAL_TRANSFORM_ROT_90 |
350 HAL_TRANSFORM_FLIP_H,
351 /* rotate source image 90 degrees and flip vertically 0x6 */
352 OVERLAY_TRANSFORM_ROT_90_FLIP_V = HAL_TRANSFORM_ROT_90 |
353 HAL_TRANSFORM_FLIP_V,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700354 /* rotate source image 270 degrees
355 * Basically 180 | 90 == 0x7 */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700356 OVERLAY_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700357 /* rotate invalid like in Transform.h */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700358 OVERLAY_TRANSFORM_INV = 0x80
Naseer Ahmed29a26812012-06-14 00:56:20 -0700359};
360
361// Used to consolidate pipe params
362struct PipeArgs {
363 PipeArgs() : mdpFlags(OV_MDP_FLAGS_NONE),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700364 zorder(Z_SYSTEM_ALLOC),
365 isFg(IS_FG_OFF),
366 rotFlags(ROT_FLAG_DISABLED){
367 }
368
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700369 PipeArgs(eMdpFlags f, Whf _whf,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700370 eZorder z, eIsFg fg, eRotFlags r) :
371 mdpFlags(f),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700372 whf(_whf),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700373 zorder(z),
374 isFg(fg),
375 rotFlags(r) {
376 }
377
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700378 eMdpFlags mdpFlags; // for mdp_overlay flags
Naseer Ahmed29a26812012-06-14 00:56:20 -0700379 Whf whf;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700380 eZorder zorder; // stage number
381 eIsFg isFg; // control alpha & transp
382 eRotFlags rotFlags;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700383};
384
385enum eOverlayState{
386 /* No pipes from overlay open */
387 OV_CLOSED = 0,
388
389 /* 2D Video */
390 OV_2D_VIDEO_ON_PANEL,
391 OV_2D_VIDEO_ON_PANEL_TV,
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700392 OV_2D_VIDEO_ON_TV,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700393
394 /* 3D Video on one display (panel or TV) */
395 OV_3D_VIDEO_ON_2D_PANEL,
396 OV_3D_VIDEO_ON_3D_PANEL,
397 OV_3D_VIDEO_ON_3D_TV,
398
399 /* 3D Video on two displays (panel and TV) */
400 OV_3D_VIDEO_ON_2D_PANEL_2D_TV,
401
402 /* UI Mirroring */
403 OV_UI_MIRROR,
404 OV_2D_TRUE_UI_MIRROR,
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700405 /* Dual display with video */
406 OV_UI_VIDEO_TV,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700407
408 /* Composition Bypass */
409 OV_BYPASS_1_LAYER,
410 OV_BYPASS_2_LAYER,
411 OV_BYPASS_3_LAYER,
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700412
413 /* External only for dual-disp */
414 OV_DUAL_DISP,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700415};
416
417inline void setMdpFlags(eMdpFlags& f, eMdpFlags v) {
418 f = static_cast<eMdpFlags>(setBit(f, v));
419}
420
421inline void clearMdpFlags(eMdpFlags& f, eMdpFlags v) {
422 f = static_cast<eMdpFlags>(clrBit(f, v));
423}
424
425// fb 0/1/2
426enum { FB0, FB1, FB2 };
427
428//Panels could be categorized as primary and external
429enum { PRIMARY, EXTERNAL };
430
431//External Panels could use HDMI or WFD
432enum {
433 HDMI = 1,
434 WFD = 2
435};
436
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700437//TODO Make this a part of some appropriate class
Naseer Ahmed29a26812012-06-14 00:56:20 -0700438static int sExtType = HDMI; //HDMI or WFD
Naseer Ahmed29a26812012-06-14 00:56:20 -0700439//Set by client as HDMI/WFD
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700440void setExtType(const int& type);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700441//Return External panel type set by client.
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700442int getExtType();
443
Naseer Ahmed29a26812012-06-14 00:56:20 -0700444
445//Gets the FB number for the external type.
446//As of now, HDMI always has fb1, WFD could use fb1 or fb2
447//Assumes Ext type set by setExtType() from client.
448static int getFBForPanel(int panel) { // PRIMARY OR EXTERNAL
449 switch(panel) {
450 case PRIMARY: return FB0;
451 break;
452 case EXTERNAL:
453 switch(getExtType()) {
454 case HDMI: return FB1;
455 break;
456 case WFD: return FB2;//Hardcoding fb2 for wfd. Will change.
457 break;
458 }
459 break;
460 default:
461 ALOGE("%s: Unrecognized PANEL category %d", __func__, panel);
462 break;
463 }
464 return -1;
465}
466
467// number of rgb pipes bufs (max)
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700468
Naseer Ahmed29a26812012-06-14 00:56:20 -0700469// 2 for rgb0/1 double bufs
470enum { RGB_PIPE_NUM_BUFS = 2 };
471
472struct ScreenInfo {
473 ScreenInfo() : mFBWidth(0),
474 mFBHeight(0),
475 mFBbpp(0),
476 mFBystride(0) {}
477 void dump(const char* const s) const;
478 uint32_t mFBWidth;
479 uint32_t mFBHeight;
480 uint32_t mFBbpp;
481 uint32_t mFBystride;
482};
483
484int getMdpFormat(int format);
485int getRotOutFmt(uint32_t format);
486/* flip is upside down and such. V, H flip
487 * rotation is 90, 180 etc
488 * It returns MDP related enum/define that match rot+flip*/
489int getMdpOrient(eTransform rotation);
Saurabh Shah9c876d92012-08-25 19:29:53 -0700490const char* getFormatString(int format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700491const char* getStateString(eOverlayState state);
492
Naseer Ahmed29a26812012-06-14 00:56:20 -0700493// Cannot use HW_OVERLAY_MAGNIFICATION_LIMIT, since at the time
494// of integration, HW_OVERLAY_MAGNIFICATION_LIMIT was a define
495enum { HW_OV_MAGNIFICATION_LIMIT = 20,
496 HW_OV_MINIFICATION_LIMIT = 8
497};
498
Naseer Ahmed29a26812012-06-14 00:56:20 -0700499template <class T>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700500inline void memset0(T& t) { ::memset(&t, 0, sizeof(T)); }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700501
502template <class T> inline void swap ( T& a, T& b )
503{
504 T c(a); a=b; b=c;
505}
506
507inline int alignup(int value, int a) {
508 //if align = 0, return the value. Else, do alignment.
509 return a ? ((((value - 1) / a) + 1) * a) : value;
510}
511
512// FIXME that align should replace the upper one.
513inline int align(int value, int a) {
514 //if align = 0, return the value. Else, do alignment.
515 return a ? ((value + (a-1)) & ~(a-1)) : value;
516}
517
Naseer Ahmed29a26812012-06-14 00:56:20 -0700518enum eRotOutFmt {
519 ROT_OUT_FMT_DEFAULT,
520 ROT_OUT_FMT_Y_CRCB_H2V2
521};
522
523template <int ROT_OUT_FMT> struct RotOutFmt;
524
525// FIXME, taken from gralloc_priv.h. Need to
526// put it back as soon as overlay takes place of the old one
527/* possible formats for 3D content*/
528enum {
529 HAL_NO_3D = 0x0000,
530 HAL_3D_IN_SIDE_BY_SIDE_L_R = 0x10000,
531 HAL_3D_IN_TOP_BOTTOM = 0x20000,
532 HAL_3D_IN_INTERLEAVE = 0x40000,
533 HAL_3D_IN_SIDE_BY_SIDE_R_L = 0x80000,
534 HAL_3D_OUT_SIDE_BY_SIDE = 0x1000,
535 HAL_3D_OUT_TOP_BOTTOM = 0x2000,
536 HAL_3D_OUT_INTERLEAVE = 0x4000,
537 HAL_3D_OUT_MONOSCOPIC = 0x8000
538};
539
540enum { HAL_3D_OUT_SBS_MASK =
541 HAL_3D_OUT_SIDE_BY_SIDE >> overlay::utils::SHIFT_OUT_3D,
542 HAL_3D_OUT_TOP_BOT_MASK =
543 HAL_3D_OUT_TOP_BOTTOM >> overlay::utils::SHIFT_OUT_3D,
544 HAL_3D_OUT_INTERL_MASK =
545 HAL_3D_OUT_INTERLEAVE >> overlay::utils::SHIFT_OUT_3D,
546 HAL_3D_OUT_MONOS_MASK =
547 HAL_3D_OUT_MONOSCOPIC >> overlay::utils::SHIFT_OUT_3D
548};
549
550
551inline bool isYuv(uint32_t format) {
552 switch(format){
553 case MDP_Y_CBCR_H2V1:
554 case MDP_Y_CBCR_H2V2:
555 case MDP_Y_CRCB_H2V2:
Saurabh Shahb121e142012-08-20 17:59:13 -0700556 case MDP_Y_CRCB_H1V1:
557 case MDP_Y_CRCB_H2V1:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700558 case MDP_Y_CRCB_H2V2_TILE:
559 case MDP_Y_CBCR_H2V2_TILE:
Saurabh Shahb121e142012-08-20 17:59:13 -0700560 case MDP_Y_CR_CB_H2V2:
Saurabh Shahae1044e2012-08-21 16:03:32 -0700561 case MDP_Y_CR_CB_GH2V2:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700562 return true;
563 default:
564 return false;
565 }
566 return false;
567}
568
569inline bool isRgb(uint32_t format) {
570 switch(format) {
571 case MDP_RGBA_8888:
572 case MDP_BGRA_8888:
573 case MDP_RGBX_8888:
574 case MDP_RGB_565:
575 return true;
576 default:
577 return false;
578 }
579 return false;
580}
581
582inline bool isValidDest(eDest dest)
583{
584 if ((OV_PIPE0 & dest) ||
585 (OV_PIPE1 & dest) ||
586 (OV_PIPE2 & dest)) {
587 return true;
588 }
589 return false;
590}
591
Saurabh Shah9c876d92012-08-25 19:29:53 -0700592inline const char* getFormatString(int format){
Naseer Ahmed29a26812012-06-14 00:56:20 -0700593 static const char* const formats[] = {
594 "MDP_RGB_565",
595 "MDP_XRGB_8888",
596 "MDP_Y_CBCR_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700597 "MDP_Y_CBCR_H2V2_ADRENO",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700598 "MDP_ARGB_8888",
599 "MDP_RGB_888",
600 "MDP_Y_CRCB_H2V2",
601 "MDP_YCRYCB_H2V1",
602 "MDP_Y_CRCB_H2V1",
603 "MDP_Y_CBCR_H2V1",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700604 "MDP_Y_CRCB_H1V2",
605 "MDP_Y_CBCR_H1V2",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700606 "MDP_RGBA_8888",
607 "MDP_BGRA_8888",
608 "MDP_RGBX_8888",
609 "MDP_Y_CRCB_H2V2_TILE",
610 "MDP_Y_CBCR_H2V2_TILE",
611 "MDP_Y_CR_CB_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700612 "MDP_Y_CR_CB_GH2V2",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700613 "MDP_Y_CB_CR_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700614 "MDP_Y_CRCB_H1V1",
615 "MDP_Y_CBCR_H1V1",
616 "MDP_YCRCB_H1V1",
617 "MDP_YCBCR_H1V1",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700618 "MDP_BGR_565",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700619 "MDP_IMGTYPE_LIMIT",
620 "MDP_RGB_BORDERFILL",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700621 "MDP_FB_FORMAT",
622 "MDP_IMGTYPE_LIMIT2"
623 };
Saurabh Shah9c876d92012-08-25 19:29:53 -0700624 if(format < 0 || format >= (int)(sizeof(formats) / sizeof(formats[0]))) {
625 ALOGE("%s wrong fmt %d", __FUNCTION__, format);
626 return "Unsupported format";
627 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700628 return formats[format];
629}
630
631inline const char* getStateString(eOverlayState state){
632 switch (state) {
633 case OV_CLOSED:
634 return "OV_CLOSED";
635 case OV_2D_VIDEO_ON_PANEL:
636 return "OV_2D_VIDEO_ON_PANEL";
637 case OV_2D_VIDEO_ON_PANEL_TV:
638 return "OV_2D_VIDEO_ON_PANEL_TV";
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700639 case OV_2D_VIDEO_ON_TV:
640 return "OV_2D_VIDEO_ON_TV";
Naseer Ahmed29a26812012-06-14 00:56:20 -0700641 case OV_3D_VIDEO_ON_2D_PANEL:
642 return "OV_3D_VIDEO_ON_2D_PANEL";
643 case OV_3D_VIDEO_ON_3D_PANEL:
644 return "OV_3D_VIDEO_ON_3D_PANEL";
645 case OV_3D_VIDEO_ON_3D_TV:
646 return "OV_3D_VIDEO_ON_3D_TV";
647 case OV_3D_VIDEO_ON_2D_PANEL_2D_TV:
648 return "OV_3D_VIDEO_ON_2D_PANEL_2D_TV";
649 case OV_UI_MIRROR:
650 return "OV_UI_MIRROR";
651 case OV_2D_TRUE_UI_MIRROR:
652 return "OV_2D_TRUE_UI_MIRROR";
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700653 case OV_UI_VIDEO_TV:
654 return "OV_UI_VIDEO_TV";
Naseer Ahmed29a26812012-06-14 00:56:20 -0700655 case OV_BYPASS_1_LAYER:
656 return "OV_BYPASS_1_LAYER";
657 case OV_BYPASS_2_LAYER:
658 return "OV_BYPASS_2_LAYER";
659 case OV_BYPASS_3_LAYER:
660 return "OV_BYPASS_3_LAYER";
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700661 case OV_DUAL_DISP:
662 return "OV_DUAL_DISP";
Naseer Ahmed29a26812012-06-14 00:56:20 -0700663 default:
664 return "UNKNOWN_STATE";
665 }
666 return "BAD_STATE";
667}
668
Naseer Ahmed29a26812012-06-14 00:56:20 -0700669inline void Whf::dump() const {
670 ALOGE("== Dump WHF w=%d h=%d f=%d s=%d start/end ==",
671 w, h, format, size);
672}
673
674inline void Dim::dump() const {
675 ALOGE("== Dump Dim x=%d y=%d w=%d h=%d start/end ==", x, y, w, h);
676}
677
678inline int getMdpOrient(eTransform rotation) {
679 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, rotation);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700680 switch(rotation)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700681 {
682 case OVERLAY_TRANSFORM_0 : return 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700683 case OVERLAY_TRANSFORM_FLIP_V: return MDP_FLIP_UD;
684 case OVERLAY_TRANSFORM_FLIP_H: return MDP_FLIP_LR;
685 case OVERLAY_TRANSFORM_ROT_90: return MDP_ROT_90;
Saurabh Shaha73738d2012-08-09 18:15:18 -0700686 //getMdpOrient will switch the flips if the source is 90 rotated.
687 //Clients in Android dont factor in 90 rotation while deciding flip.
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700688 case OVERLAY_TRANSFORM_ROT_90_FLIP_V:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700689 return MDP_ROT_90 | MDP_FLIP_LR;
Saurabh Shaha73738d2012-08-09 18:15:18 -0700690 case OVERLAY_TRANSFORM_ROT_90_FLIP_H:
691 return MDP_ROT_90 | MDP_FLIP_UD;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700692 case OVERLAY_TRANSFORM_ROT_180: return MDP_ROT_180;
693 case OVERLAY_TRANSFORM_ROT_270: return MDP_ROT_270;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700694 default:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700695 ALOGE("%s: invalid rotation value (value = 0x%x",
696 __FUNCTION__, rotation);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700697 }
698 return -1;
699}
700
701inline int getRotOutFmt(uint32_t format) {
702 switch (format) {
703 case MDP_Y_CRCB_H2V2_TILE:
704 return MDP_Y_CRCB_H2V2;
705 case MDP_Y_CBCR_H2V2_TILE:
706 return MDP_Y_CBCR_H2V2;
707 case MDP_Y_CB_CR_H2V2:
708 return MDP_Y_CBCR_H2V2;
Saurabh Shahae1044e2012-08-21 16:03:32 -0700709 case MDP_Y_CR_CB_GH2V2:
710 return MDP_Y_CRCB_H2V2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700711 default:
712 return format;
713 }
714 // not reached
715 OVASSERT(false, "%s not reached", __FUNCTION__);
716 return -1;
717}
718
Naseer Ahmed29a26812012-06-14 00:56:20 -0700719
720inline uint32_t getColorFormat(uint32_t format)
721{
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700722 return (format == HAL_PIXEL_FORMAT_YV12) ?
723 format : colorFormat(format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700724}
725
726// FB0
727template <int CHAN>
728inline Dim getPositionS3DImpl(const Whf& whf)
729{
730 switch (whf.format & OUTPUT_3D_MASK)
731 {
732 case HAL_3D_OUT_SBS_MASK:
733 // x, y, w, h
734 return Dim(0, 0, whf.w/2, whf.h);
735 case HAL_3D_OUT_TOP_BOT_MASK:
736 return Dim(0, 0, whf.w, whf.h/2);
737 case HAL_3D_OUT_MONOS_MASK:
738 return Dim();
739 case HAL_3D_OUT_INTERL_MASK:
740 // FIXME error?
741 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
742 return Dim();
743 default:
744 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
745 whf.format);
746 }
747 return Dim();
748}
749
750template <>
751inline Dim getPositionS3DImpl<utils::OV_PIPE1>(const Whf& whf)
752{
753 switch (whf.format & OUTPUT_3D_MASK)
754 {
755 case HAL_3D_OUT_SBS_MASK:
756 return Dim(whf.w/2, 0, whf.w/2, whf.h);
757 case HAL_3D_OUT_TOP_BOT_MASK:
758 return Dim(0, whf.h/2, whf.w, whf.h/2);
759 case HAL_3D_OUT_MONOS_MASK:
760 return Dim(0, 0, whf.w, whf.h);
761 case HAL_3D_OUT_INTERL_MASK:
762 // FIXME error?
763 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
764 return Dim();
765 default:
766 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
767 whf.format);
768 }
769 return Dim();
770}
771
772template <int CHAN>
773inline bool getPositionS3D(const Whf& whf, Dim& out) {
774 out = getPositionS3DImpl<CHAN>(whf);
775 return (out != Dim());
776}
777
778template <int CHAN>
779inline Dim getCropS3DImpl(const Dim& in, uint32_t fmt) {
780 switch (fmt & INPUT_3D_MASK)
781 {
782 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
783 return Dim(0, 0, in.w/2, in.h);
784 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
785 return Dim(in.w/2, 0, in.w/2, in.h);
786 case HAL_3D_IN_TOP_BOTTOM:
787 return Dim(0, 0, in.w, in.h/2);
788 case HAL_3D_IN_INTERLEAVE:
789 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
790 break;
791 default:
792 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
793 break;
794 }
795 return Dim();
796}
797
798template <>
799inline Dim getCropS3DImpl<utils::OV_PIPE1>(const Dim& in, uint32_t fmt) {
800 switch (fmt & INPUT_3D_MASK)
801 {
802 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
803 return Dim(in.w/2, 0, in.w/2, in.h);
804 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
805 return Dim(0, 0, in.w/2, in.h);
806 case HAL_3D_IN_TOP_BOTTOM:
807 return Dim(0, in.h/2, in.w, in.h/2);
808 case HAL_3D_IN_INTERLEAVE:
809 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
810 break;
811 default:
812 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
813 break;
814 }
815 return Dim();
816}
817
818template <int CHAN>
819inline bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt)
820{
821 out = getCropS3DImpl<CHAN>(in, fmt);
822 return (out != Dim());
823}
824
825template <class Type>
826void swapWidthHeight(Type& width, Type& height) {
827 Type tmp = width;
828 width = height;
829 height = tmp;
830}
831
832inline void ScreenInfo::dump(const char* const s) const {
833 ALOGE("== Dump %s ScreenInfo w=%d h=%d"
834 " bpp=%d stride=%d start/end ==",
835 s, mFBWidth, mFBHeight, mFBbpp, mFBystride);
836}
837
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700838inline bool openDev(OvFD& fd, int fbnum,
839 const char* const devpath, int flags) {
840 return overlay::open(fd, fbnum, devpath, flags);
841}
842
Saurabh Shahb121e142012-08-20 17:59:13 -0700843template <class T>
844inline void even_ceil(T& value) {
845 if(value & 1)
846 value++;
847}
848
849template <class T>
850inline void even_floor(T& value) {
851 if(value & 1)
852 value--;
853}
854
Naseer Ahmed29a26812012-06-14 00:56:20 -0700855} // namespace utils ends
856
857//--------------------Class Res stuff (namespace overlay only) -----------
858
859class Res {
860public:
861 // /dev/graphics/fb%u
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700862 static const char* const fbPath;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700863 // /dev/msm_rotator
864 static const char* const rotPath;
865 // /sys/class/graphics/fb1/format_3d
866 static const char* const format3DFile;
867 // /sys/class/graphics/fb1/3d_present
868 static const char* const edid3dInfoFile;
869 // /sys/devices/platform/mipi_novatek.0/enable_3d_barrier
870 static const char* const barrierFile;
871};
872
873
874//--------------------Class OvFD stuff (namespace overlay only) -----------
875
Naseer Ahmed29a26812012-06-14 00:56:20 -0700876/*
877* Holds one FD
878* Dtor will NOT close the underlying FD.
879* That enables us to copy that object around
880* */
881class OvFD {
882public:
883 /* Ctor */
884 explicit OvFD();
885
886 /* dtor will NOT close the underlying FD */
887 ~OvFD();
888
889 /* Open fd using the path given by dev.
890 * return false in failure */
891 bool open(const char* const dev,
892 int flags = O_RDWR);
893
894 /* populate path */
895 void setPath(const char* const dev);
896
897 /* Close fd if we have a valid fd. */
898 bool close();
899
900 /* returns underlying fd.*/
901 int getFD() const;
902
903 /* returns true if fd is valid */
904 bool valid() const;
905
906 /* like operator= */
907 void copy(int fd);
908
909 /* dump the state of the instance */
910 void dump() const;
911private:
912 /* helper enum for determine valid/invalid fd */
913 enum { INVAL = -1 };
914
915 /* actual os fd */
916 int mFD;
917
918 /* path, for debugging */
919 char mPath[utils::MAX_PATH_LEN];
920};
921
922//-------------------Inlines--------------------------
923
924inline bool open(OvFD& fd, uint32_t fbnum, const char* const dev, int flags)
925{
926 char dev_name[64] = {0};
927 snprintf(dev_name, sizeof(dev_name), dev, fbnum);
928 return fd.open(dev_name, flags);
929}
930
931inline OvFD::OvFD() : mFD (INVAL) {
932 mPath[0] = 0;
933}
934
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700935inline OvFD::~OvFD() {
936 //no op since copy() can be used to share fd, in 3d cases.
937}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700938
939inline bool OvFD::open(const char* const dev, int flags)
940{
941 mFD = ::open(dev, flags, 0);
942 if (mFD < 0) {
943 // FIXME errno, strerror in bionic?
944 ALOGE("Cant open device %s err=%d", dev, errno);
945 return false;
946 }
947 setPath(dev);
948 return true;
949}
950
951inline void OvFD::setPath(const char* const dev)
952{
953 ::strncpy(mPath, dev, utils::MAX_PATH_LEN);
954}
955
956inline bool OvFD::close()
957{
958 int ret = 0;
959 if(valid()) {
960 ret = ::close(mFD);
961 mFD = INVAL;
962 }
963 return (ret == 0);
964}
965
966inline bool OvFD::valid() const
967{
968 return (mFD != INVAL);
969}
970
971inline int OvFD::getFD() const { return mFD; }
972
973inline void OvFD::copy(int fd) {
974 mFD = fd;
975}
976
977inline void OvFD::dump() const
978{
979 ALOGE("== Dump OvFD fd=%d path=%s start/end ==",
980 mFD, mPath);
981}
982
983//--------------- class OvFD stuff ends ---------------------
984
985} // overlay
986
987
988#endif // OVERLAY_UTILS_H