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