blob: 7432ff89f3507de5d038b6058422673239405028 [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,
Saurabh Shah91a6a992012-08-20 15:25:28 -0700307 OV_MDP_BLEND_FG_PREMULT = MDP_BLEND_FG_PREMULT,
Saurabh Shah09549f62012-10-04 13:25:44 -0700308 OV_MDP_FLIP_H = MDP_FLIP_LR,
309 OV_MDP_FLIP_V = MDP_FLIP_UD,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700310};
311
Naseer Ahmed29a26812012-06-14 00:56:20 -0700312enum eZorder {
313 ZORDER_0,
314 ZORDER_1,
315 ZORDER_2,
316 Z_SYSTEM_ALLOC = 0xFFFF
317};
318
319enum eMdpPipeType {
320 OV_MDP_PIPE_RGB,
321 OV_MDP_PIPE_VG
322};
323
Naseer Ahmed29a26812012-06-14 00:56:20 -0700324// Max pipes via overlay (VG0, VG1, RGB1)
325enum { MAX_PIPES = 3 };
326
327/* Used to identify destination channels and
328 * also 3D channels e.g. when in 3D mode with 2
329 * pipes opened and it is used in get crop/pos 3D
Naseer Ahmed29a26812012-06-14 00:56:20 -0700330 * */
331enum eDest {
332 OV_PIPE0 = 1 << 0,
333 OV_PIPE1 = 1 << 1,
334 OV_PIPE2 = 1 << 2,
335 OV_PIPE_ALL = (OV_PIPE0 | OV_PIPE1 | OV_PIPE2)
336};
337
338/* values for copybit_set_parameter(OVERLAY_TRANSFORM) */
339enum eTransform {
340 /* No rot */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700341 OVERLAY_TRANSFORM_0 = 0x0,
342 /* flip source image horizontally 0x1 */
343 OVERLAY_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H,
344 /* flip source image vertically 0x2 */
345 OVERLAY_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700346 /* rotate source image 180 degrees
347 * It is basically bit-or-ed H | V == 0x3 */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700348 OVERLAY_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
349 /* rotate source image 90 degrees 0x4 */
350 OVERLAY_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
351 /* rotate source image 90 degrees and flip horizontally 0x5 */
352 OVERLAY_TRANSFORM_ROT_90_FLIP_H = HAL_TRANSFORM_ROT_90 |
353 HAL_TRANSFORM_FLIP_H,
354 /* rotate source image 90 degrees and flip vertically 0x6 */
355 OVERLAY_TRANSFORM_ROT_90_FLIP_V = HAL_TRANSFORM_ROT_90 |
356 HAL_TRANSFORM_FLIP_V,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700357 /* rotate source image 270 degrees
358 * Basically 180 | 90 == 0x7 */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700359 OVERLAY_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700360 /* rotate invalid like in Transform.h */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700361 OVERLAY_TRANSFORM_INV = 0x80
Naseer Ahmed29a26812012-06-14 00:56:20 -0700362};
363
364// Used to consolidate pipe params
365struct PipeArgs {
366 PipeArgs() : mdpFlags(OV_MDP_FLAGS_NONE),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700367 zorder(Z_SYSTEM_ALLOC),
368 isFg(IS_FG_OFF),
369 rotFlags(ROT_FLAG_DISABLED){
370 }
371
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700372 PipeArgs(eMdpFlags f, Whf _whf,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700373 eZorder z, eIsFg fg, eRotFlags r) :
374 mdpFlags(f),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700375 whf(_whf),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700376 zorder(z),
377 isFg(fg),
378 rotFlags(r) {
379 }
380
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700381 eMdpFlags mdpFlags; // for mdp_overlay flags
Naseer Ahmed29a26812012-06-14 00:56:20 -0700382 Whf whf;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700383 eZorder zorder; // stage number
384 eIsFg isFg; // control alpha & transp
385 eRotFlags rotFlags;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700386};
387
388enum eOverlayState{
389 /* No pipes from overlay open */
390 OV_CLOSED = 0,
391
392 /* 2D Video */
393 OV_2D_VIDEO_ON_PANEL,
394 OV_2D_VIDEO_ON_PANEL_TV,
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700395 OV_2D_VIDEO_ON_TV,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700396
397 /* 3D Video on one display (panel or TV) */
398 OV_3D_VIDEO_ON_2D_PANEL,
399 OV_3D_VIDEO_ON_3D_PANEL,
400 OV_3D_VIDEO_ON_3D_TV,
401
402 /* 3D Video on two displays (panel and TV) */
403 OV_3D_VIDEO_ON_2D_PANEL_2D_TV,
404
405 /* UI Mirroring */
406 OV_UI_MIRROR,
407 OV_2D_TRUE_UI_MIRROR,
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700408 /* Dual display with video */
409 OV_UI_VIDEO_TV,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700410
411 /* Composition Bypass */
412 OV_BYPASS_1_LAYER,
413 OV_BYPASS_2_LAYER,
414 OV_BYPASS_3_LAYER,
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700415
416 /* External only for dual-disp */
417 OV_DUAL_DISP,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700418};
419
420inline void setMdpFlags(eMdpFlags& f, eMdpFlags v) {
421 f = static_cast<eMdpFlags>(setBit(f, v));
422}
423
424inline void clearMdpFlags(eMdpFlags& f, eMdpFlags v) {
425 f = static_cast<eMdpFlags>(clrBit(f, v));
426}
427
428// fb 0/1/2
429enum { FB0, FB1, FB2 };
430
431//Panels could be categorized as primary and external
432enum { PRIMARY, EXTERNAL };
433
434//External Panels could use HDMI or WFD
435enum {
436 HDMI = 1,
437 WFD = 2
438};
439
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700440//TODO Make this a part of some appropriate class
Naseer Ahmed29a26812012-06-14 00:56:20 -0700441static int sExtType = HDMI; //HDMI or WFD
Naseer Ahmed29a26812012-06-14 00:56:20 -0700442//Set by client as HDMI/WFD
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700443void setExtType(const int& type);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700444//Return External panel type set by client.
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700445int getExtType();
446
Naseer Ahmed29a26812012-06-14 00:56:20 -0700447
448//Gets the FB number for the external type.
449//As of now, HDMI always has fb1, WFD could use fb1 or fb2
450//Assumes Ext type set by setExtType() from client.
451static int getFBForPanel(int panel) { // PRIMARY OR EXTERNAL
452 switch(panel) {
453 case PRIMARY: return FB0;
454 break;
455 case EXTERNAL:
456 switch(getExtType()) {
457 case HDMI: return FB1;
458 break;
459 case WFD: return FB2;//Hardcoding fb2 for wfd. Will change.
460 break;
461 }
462 break;
463 default:
464 ALOGE("%s: Unrecognized PANEL category %d", __func__, panel);
465 break;
466 }
467 return -1;
468}
469
470// number of rgb pipes bufs (max)
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700471
Naseer Ahmed29a26812012-06-14 00:56:20 -0700472// 2 for rgb0/1 double bufs
473enum { RGB_PIPE_NUM_BUFS = 2 };
474
475struct ScreenInfo {
476 ScreenInfo() : mFBWidth(0),
477 mFBHeight(0),
478 mFBbpp(0),
479 mFBystride(0) {}
480 void dump(const char* const s) const;
481 uint32_t mFBWidth;
482 uint32_t mFBHeight;
483 uint32_t mFBbpp;
484 uint32_t mFBystride;
485};
486
487int getMdpFormat(int format);
488int getRotOutFmt(uint32_t format);
489/* flip is upside down and such. V, H flip
490 * rotation is 90, 180 etc
491 * It returns MDP related enum/define that match rot+flip*/
492int getMdpOrient(eTransform rotation);
Saurabh Shah9c876d92012-08-25 19:29:53 -0700493const char* getFormatString(int format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700494const char* getStateString(eOverlayState state);
495
Naseer Ahmed29a26812012-06-14 00:56:20 -0700496// Cannot use HW_OVERLAY_MAGNIFICATION_LIMIT, since at the time
497// of integration, HW_OVERLAY_MAGNIFICATION_LIMIT was a define
498enum { HW_OV_MAGNIFICATION_LIMIT = 20,
499 HW_OV_MINIFICATION_LIMIT = 8
500};
501
Naseer Ahmed29a26812012-06-14 00:56:20 -0700502template <class T>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700503inline void memset0(T& t) { ::memset(&t, 0, sizeof(T)); }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700504
505template <class T> inline void swap ( T& a, T& b )
506{
507 T c(a); a=b; b=c;
508}
509
510inline int alignup(int value, int a) {
511 //if align = 0, return the value. Else, do alignment.
512 return a ? ((((value - 1) / a) + 1) * a) : value;
513}
514
515// FIXME that align should replace the upper one.
516inline int align(int value, int a) {
517 //if align = 0, return the value. Else, do alignment.
518 return a ? ((value + (a-1)) & ~(a-1)) : value;
519}
520
Naseer Ahmed29a26812012-06-14 00:56:20 -0700521enum eRotOutFmt {
522 ROT_OUT_FMT_DEFAULT,
523 ROT_OUT_FMT_Y_CRCB_H2V2
524};
525
526template <int ROT_OUT_FMT> struct RotOutFmt;
527
528// FIXME, taken from gralloc_priv.h. Need to
529// put it back as soon as overlay takes place of the old one
530/* possible formats for 3D content*/
531enum {
532 HAL_NO_3D = 0x0000,
533 HAL_3D_IN_SIDE_BY_SIDE_L_R = 0x10000,
534 HAL_3D_IN_TOP_BOTTOM = 0x20000,
535 HAL_3D_IN_INTERLEAVE = 0x40000,
536 HAL_3D_IN_SIDE_BY_SIDE_R_L = 0x80000,
537 HAL_3D_OUT_SIDE_BY_SIDE = 0x1000,
538 HAL_3D_OUT_TOP_BOTTOM = 0x2000,
539 HAL_3D_OUT_INTERLEAVE = 0x4000,
540 HAL_3D_OUT_MONOSCOPIC = 0x8000
541};
542
543enum { HAL_3D_OUT_SBS_MASK =
544 HAL_3D_OUT_SIDE_BY_SIDE >> overlay::utils::SHIFT_OUT_3D,
545 HAL_3D_OUT_TOP_BOT_MASK =
546 HAL_3D_OUT_TOP_BOTTOM >> overlay::utils::SHIFT_OUT_3D,
547 HAL_3D_OUT_INTERL_MASK =
548 HAL_3D_OUT_INTERLEAVE >> overlay::utils::SHIFT_OUT_3D,
549 HAL_3D_OUT_MONOS_MASK =
550 HAL_3D_OUT_MONOSCOPIC >> overlay::utils::SHIFT_OUT_3D
551};
552
553
554inline bool isYuv(uint32_t format) {
555 switch(format){
556 case MDP_Y_CBCR_H2V1:
557 case MDP_Y_CBCR_H2V2:
558 case MDP_Y_CRCB_H2V2:
Saurabh Shahb121e142012-08-20 17:59:13 -0700559 case MDP_Y_CRCB_H1V1:
560 case MDP_Y_CRCB_H2V1:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700561 case MDP_Y_CRCB_H2V2_TILE:
562 case MDP_Y_CBCR_H2V2_TILE:
Saurabh Shahb121e142012-08-20 17:59:13 -0700563 case MDP_Y_CR_CB_H2V2:
Saurabh Shahae1044e2012-08-21 16:03:32 -0700564 case MDP_Y_CR_CB_GH2V2:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700565 return true;
566 default:
567 return false;
568 }
569 return false;
570}
571
572inline bool isRgb(uint32_t format) {
573 switch(format) {
574 case MDP_RGBA_8888:
575 case MDP_BGRA_8888:
576 case MDP_RGBX_8888:
577 case MDP_RGB_565:
578 return true;
579 default:
580 return false;
581 }
582 return false;
583}
584
585inline bool isValidDest(eDest dest)
586{
587 if ((OV_PIPE0 & dest) ||
588 (OV_PIPE1 & dest) ||
589 (OV_PIPE2 & dest)) {
590 return true;
591 }
592 return false;
593}
594
Saurabh Shah9c876d92012-08-25 19:29:53 -0700595inline const char* getFormatString(int format){
Naseer Ahmed29a26812012-06-14 00:56:20 -0700596 static const char* const formats[] = {
597 "MDP_RGB_565",
598 "MDP_XRGB_8888",
599 "MDP_Y_CBCR_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700600 "MDP_Y_CBCR_H2V2_ADRENO",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700601 "MDP_ARGB_8888",
602 "MDP_RGB_888",
603 "MDP_Y_CRCB_H2V2",
604 "MDP_YCRYCB_H2V1",
605 "MDP_Y_CRCB_H2V1",
606 "MDP_Y_CBCR_H2V1",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700607 "MDP_Y_CRCB_H1V2",
608 "MDP_Y_CBCR_H1V2",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700609 "MDP_RGBA_8888",
610 "MDP_BGRA_8888",
611 "MDP_RGBX_8888",
612 "MDP_Y_CRCB_H2V2_TILE",
613 "MDP_Y_CBCR_H2V2_TILE",
614 "MDP_Y_CR_CB_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700615 "MDP_Y_CR_CB_GH2V2",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700616 "MDP_Y_CB_CR_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700617 "MDP_Y_CRCB_H1V1",
618 "MDP_Y_CBCR_H1V1",
619 "MDP_YCRCB_H1V1",
620 "MDP_YCBCR_H1V1",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700621 "MDP_BGR_565",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700622 "MDP_IMGTYPE_LIMIT",
623 "MDP_RGB_BORDERFILL",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700624 "MDP_FB_FORMAT",
625 "MDP_IMGTYPE_LIMIT2"
626 };
Saurabh Shah9c876d92012-08-25 19:29:53 -0700627 if(format < 0 || format >= (int)(sizeof(formats) / sizeof(formats[0]))) {
628 ALOGE("%s wrong fmt %d", __FUNCTION__, format);
629 return "Unsupported format";
630 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700631 return formats[format];
632}
633
634inline const char* getStateString(eOverlayState state){
635 switch (state) {
636 case OV_CLOSED:
637 return "OV_CLOSED";
638 case OV_2D_VIDEO_ON_PANEL:
639 return "OV_2D_VIDEO_ON_PANEL";
640 case OV_2D_VIDEO_ON_PANEL_TV:
641 return "OV_2D_VIDEO_ON_PANEL_TV";
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700642 case OV_2D_VIDEO_ON_TV:
643 return "OV_2D_VIDEO_ON_TV";
Naseer Ahmed29a26812012-06-14 00:56:20 -0700644 case OV_3D_VIDEO_ON_2D_PANEL:
645 return "OV_3D_VIDEO_ON_2D_PANEL";
646 case OV_3D_VIDEO_ON_3D_PANEL:
647 return "OV_3D_VIDEO_ON_3D_PANEL";
648 case OV_3D_VIDEO_ON_3D_TV:
649 return "OV_3D_VIDEO_ON_3D_TV";
650 case OV_3D_VIDEO_ON_2D_PANEL_2D_TV:
651 return "OV_3D_VIDEO_ON_2D_PANEL_2D_TV";
652 case OV_UI_MIRROR:
653 return "OV_UI_MIRROR";
654 case OV_2D_TRUE_UI_MIRROR:
655 return "OV_2D_TRUE_UI_MIRROR";
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700656 case OV_UI_VIDEO_TV:
657 return "OV_UI_VIDEO_TV";
Naseer Ahmed29a26812012-06-14 00:56:20 -0700658 case OV_BYPASS_1_LAYER:
659 return "OV_BYPASS_1_LAYER";
660 case OV_BYPASS_2_LAYER:
661 return "OV_BYPASS_2_LAYER";
662 case OV_BYPASS_3_LAYER:
663 return "OV_BYPASS_3_LAYER";
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700664 case OV_DUAL_DISP:
665 return "OV_DUAL_DISP";
Naseer Ahmed29a26812012-06-14 00:56:20 -0700666 default:
667 return "UNKNOWN_STATE";
668 }
669 return "BAD_STATE";
670}
671
Naseer Ahmed29a26812012-06-14 00:56:20 -0700672inline void Whf::dump() const {
673 ALOGE("== Dump WHF w=%d h=%d f=%d s=%d start/end ==",
674 w, h, format, size);
675}
676
677inline void Dim::dump() const {
678 ALOGE("== Dump Dim x=%d y=%d w=%d h=%d start/end ==", x, y, w, h);
679}
680
681inline int getMdpOrient(eTransform rotation) {
682 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, rotation);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700683 switch(rotation)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700684 {
685 case OVERLAY_TRANSFORM_0 : return 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700686 case OVERLAY_TRANSFORM_FLIP_V: return MDP_FLIP_UD;
687 case OVERLAY_TRANSFORM_FLIP_H: return MDP_FLIP_LR;
688 case OVERLAY_TRANSFORM_ROT_90: return MDP_ROT_90;
Saurabh Shaha73738d2012-08-09 18:15:18 -0700689 //getMdpOrient will switch the flips if the source is 90 rotated.
690 //Clients in Android dont factor in 90 rotation while deciding flip.
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700691 case OVERLAY_TRANSFORM_ROT_90_FLIP_V:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700692 return MDP_ROT_90 | MDP_FLIP_LR;
Saurabh Shaha73738d2012-08-09 18:15:18 -0700693 case OVERLAY_TRANSFORM_ROT_90_FLIP_H:
694 return MDP_ROT_90 | MDP_FLIP_UD;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700695 case OVERLAY_TRANSFORM_ROT_180: return MDP_ROT_180;
696 case OVERLAY_TRANSFORM_ROT_270: return MDP_ROT_270;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700697 default:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700698 ALOGE("%s: invalid rotation value (value = 0x%x",
699 __FUNCTION__, rotation);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700700 }
701 return -1;
702}
703
704inline int getRotOutFmt(uint32_t format) {
705 switch (format) {
706 case MDP_Y_CRCB_H2V2_TILE:
707 return MDP_Y_CRCB_H2V2;
708 case MDP_Y_CBCR_H2V2_TILE:
709 return MDP_Y_CBCR_H2V2;
710 case MDP_Y_CB_CR_H2V2:
711 return MDP_Y_CBCR_H2V2;
Saurabh Shahae1044e2012-08-21 16:03:32 -0700712 case MDP_Y_CR_CB_GH2V2:
713 return MDP_Y_CRCB_H2V2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700714 default:
715 return format;
716 }
717 // not reached
718 OVASSERT(false, "%s not reached", __FUNCTION__);
719 return -1;
720}
721
Naseer Ahmed29a26812012-06-14 00:56:20 -0700722
723inline uint32_t getColorFormat(uint32_t format)
724{
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700725 return (format == HAL_PIXEL_FORMAT_YV12) ?
726 format : colorFormat(format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700727}
728
729// FB0
730template <int CHAN>
731inline Dim getPositionS3DImpl(const Whf& whf)
732{
733 switch (whf.format & OUTPUT_3D_MASK)
734 {
735 case HAL_3D_OUT_SBS_MASK:
736 // x, y, w, h
737 return Dim(0, 0, whf.w/2, whf.h);
738 case HAL_3D_OUT_TOP_BOT_MASK:
739 return Dim(0, 0, whf.w, whf.h/2);
740 case HAL_3D_OUT_MONOS_MASK:
741 return Dim();
742 case HAL_3D_OUT_INTERL_MASK:
743 // FIXME error?
744 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
745 return Dim();
746 default:
747 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
748 whf.format);
749 }
750 return Dim();
751}
752
753template <>
754inline Dim getPositionS3DImpl<utils::OV_PIPE1>(const Whf& whf)
755{
756 switch (whf.format & OUTPUT_3D_MASK)
757 {
758 case HAL_3D_OUT_SBS_MASK:
759 return Dim(whf.w/2, 0, whf.w/2, whf.h);
760 case HAL_3D_OUT_TOP_BOT_MASK:
761 return Dim(0, whf.h/2, whf.w, whf.h/2);
762 case HAL_3D_OUT_MONOS_MASK:
763 return Dim(0, 0, whf.w, whf.h);
764 case HAL_3D_OUT_INTERL_MASK:
765 // FIXME error?
766 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
767 return Dim();
768 default:
769 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
770 whf.format);
771 }
772 return Dim();
773}
774
775template <int CHAN>
776inline bool getPositionS3D(const Whf& whf, Dim& out) {
777 out = getPositionS3DImpl<CHAN>(whf);
778 return (out != Dim());
779}
780
781template <int CHAN>
782inline Dim getCropS3DImpl(const Dim& in, uint32_t fmt) {
783 switch (fmt & INPUT_3D_MASK)
784 {
785 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
786 return Dim(0, 0, in.w/2, in.h);
787 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
788 return Dim(in.w/2, 0, in.w/2, in.h);
789 case HAL_3D_IN_TOP_BOTTOM:
790 return Dim(0, 0, in.w, in.h/2);
791 case HAL_3D_IN_INTERLEAVE:
792 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
793 break;
794 default:
795 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
796 break;
797 }
798 return Dim();
799}
800
801template <>
802inline Dim getCropS3DImpl<utils::OV_PIPE1>(const Dim& in, uint32_t fmt) {
803 switch (fmt & INPUT_3D_MASK)
804 {
805 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
806 return Dim(in.w/2, 0, in.w/2, in.h);
807 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
808 return Dim(0, 0, in.w/2, in.h);
809 case HAL_3D_IN_TOP_BOTTOM:
810 return Dim(0, in.h/2, in.w, in.h/2);
811 case HAL_3D_IN_INTERLEAVE:
812 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
813 break;
814 default:
815 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
816 break;
817 }
818 return Dim();
819}
820
821template <int CHAN>
822inline bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt)
823{
824 out = getCropS3DImpl<CHAN>(in, fmt);
825 return (out != Dim());
826}
827
828template <class Type>
829void swapWidthHeight(Type& width, Type& height) {
830 Type tmp = width;
831 width = height;
832 height = tmp;
833}
834
835inline void ScreenInfo::dump(const char* const s) const {
836 ALOGE("== Dump %s ScreenInfo w=%d h=%d"
837 " bpp=%d stride=%d start/end ==",
838 s, mFBWidth, mFBHeight, mFBbpp, mFBystride);
839}
840
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700841inline bool openDev(OvFD& fd, int fbnum,
842 const char* const devpath, int flags) {
843 return overlay::open(fd, fbnum, devpath, flags);
844}
845
Saurabh Shahb121e142012-08-20 17:59:13 -0700846template <class T>
847inline void even_ceil(T& value) {
848 if(value & 1)
849 value++;
850}
851
852template <class T>
853inline void even_floor(T& value) {
854 if(value & 1)
855 value--;
856}
857
Naseer Ahmed29a26812012-06-14 00:56:20 -0700858} // namespace utils ends
859
860//--------------------Class Res stuff (namespace overlay only) -----------
861
862class Res {
863public:
864 // /dev/graphics/fb%u
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700865 static const char* const fbPath;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700866 // /dev/msm_rotator
867 static const char* const rotPath;
868 // /sys/class/graphics/fb1/format_3d
869 static const char* const format3DFile;
870 // /sys/class/graphics/fb1/3d_present
871 static const char* const edid3dInfoFile;
872 // /sys/devices/platform/mipi_novatek.0/enable_3d_barrier
873 static const char* const barrierFile;
874};
875
876
877//--------------------Class OvFD stuff (namespace overlay only) -----------
878
Naseer Ahmed29a26812012-06-14 00:56:20 -0700879/*
880* Holds one FD
881* Dtor will NOT close the underlying FD.
882* That enables us to copy that object around
883* */
884class OvFD {
885public:
886 /* Ctor */
887 explicit OvFD();
888
889 /* dtor will NOT close the underlying FD */
890 ~OvFD();
891
892 /* Open fd using the path given by dev.
893 * return false in failure */
894 bool open(const char* const dev,
895 int flags = O_RDWR);
896
897 /* populate path */
898 void setPath(const char* const dev);
899
900 /* Close fd if we have a valid fd. */
901 bool close();
902
903 /* returns underlying fd.*/
904 int getFD() const;
905
906 /* returns true if fd is valid */
907 bool valid() const;
908
909 /* like operator= */
910 void copy(int fd);
911
912 /* dump the state of the instance */
913 void dump() const;
914private:
915 /* helper enum for determine valid/invalid fd */
916 enum { INVAL = -1 };
917
918 /* actual os fd */
919 int mFD;
920
921 /* path, for debugging */
922 char mPath[utils::MAX_PATH_LEN];
923};
924
925//-------------------Inlines--------------------------
926
927inline bool open(OvFD& fd, uint32_t fbnum, const char* const dev, int flags)
928{
929 char dev_name[64] = {0};
930 snprintf(dev_name, sizeof(dev_name), dev, fbnum);
931 return fd.open(dev_name, flags);
932}
933
934inline OvFD::OvFD() : mFD (INVAL) {
935 mPath[0] = 0;
936}
937
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700938inline OvFD::~OvFD() {
939 //no op since copy() can be used to share fd, in 3d cases.
940}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700941
942inline bool OvFD::open(const char* const dev, int flags)
943{
944 mFD = ::open(dev, flags, 0);
945 if (mFD < 0) {
946 // FIXME errno, strerror in bionic?
947 ALOGE("Cant open device %s err=%d", dev, errno);
948 return false;
949 }
950 setPath(dev);
951 return true;
952}
953
954inline void OvFD::setPath(const char* const dev)
955{
956 ::strncpy(mPath, dev, utils::MAX_PATH_LEN);
957}
958
959inline bool OvFD::close()
960{
961 int ret = 0;
962 if(valid()) {
963 ret = ::close(mFD);
964 mFD = INVAL;
965 }
966 return (ret == 0);
967}
968
969inline bool OvFD::valid() const
970{
971 return (mFD != INVAL);
972}
973
974inline int OvFD::getFD() const { return mFD; }
975
976inline void OvFD::copy(int fd) {
977 mFD = fd;
978}
979
980inline void OvFD::dump() const
981{
982 ALOGE("== Dump OvFD fd=%d path=%s start/end ==",
983 mFD, mPath);
984}
985
986//--------------- class OvFD stuff ends ---------------------
987
988} // overlay
989
990
991#endif // OVERLAY_UTILS_H