blob: f308b95171d76a3373e84fa90c289fc2cc00b587 [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,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700405
406 /* Composition Bypass */
407 OV_BYPASS_1_LAYER,
408 OV_BYPASS_2_LAYER,
409 OV_BYPASS_3_LAYER,
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700410
411 /* External only for dual-disp */
412 OV_DUAL_DISP,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700413};
414
415inline void setMdpFlags(eMdpFlags& f, eMdpFlags v) {
416 f = static_cast<eMdpFlags>(setBit(f, v));
417}
418
419inline void clearMdpFlags(eMdpFlags& f, eMdpFlags v) {
420 f = static_cast<eMdpFlags>(clrBit(f, v));
421}
422
423// fb 0/1/2
424enum { FB0, FB1, FB2 };
425
426//Panels could be categorized as primary and external
427enum { PRIMARY, EXTERNAL };
428
429//External Panels could use HDMI or WFD
430enum {
431 HDMI = 1,
432 WFD = 2
433};
434
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700435//TODO Make this a part of some appropriate class
Naseer Ahmed29a26812012-06-14 00:56:20 -0700436static int sExtType = HDMI; //HDMI or WFD
Naseer Ahmed29a26812012-06-14 00:56:20 -0700437//Set by client as HDMI/WFD
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700438void setExtType(const int& type);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700439//Return External panel type set by client.
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700440int getExtType();
441
Naseer Ahmed29a26812012-06-14 00:56:20 -0700442
443//Gets the FB number for the external type.
444//As of now, HDMI always has fb1, WFD could use fb1 or fb2
445//Assumes Ext type set by setExtType() from client.
446static int getFBForPanel(int panel) { // PRIMARY OR EXTERNAL
447 switch(panel) {
448 case PRIMARY: return FB0;
449 break;
450 case EXTERNAL:
451 switch(getExtType()) {
452 case HDMI: return FB1;
453 break;
454 case WFD: return FB2;//Hardcoding fb2 for wfd. Will change.
455 break;
456 }
457 break;
458 default:
459 ALOGE("%s: Unrecognized PANEL category %d", __func__, panel);
460 break;
461 }
462 return -1;
463}
464
465// number of rgb pipes bufs (max)
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700466
Naseer Ahmed29a26812012-06-14 00:56:20 -0700467// 2 for rgb0/1 double bufs
468enum { RGB_PIPE_NUM_BUFS = 2 };
469
470struct ScreenInfo {
471 ScreenInfo() : mFBWidth(0),
472 mFBHeight(0),
473 mFBbpp(0),
474 mFBystride(0) {}
475 void dump(const char* const s) const;
476 uint32_t mFBWidth;
477 uint32_t mFBHeight;
478 uint32_t mFBbpp;
479 uint32_t mFBystride;
480};
481
482int getMdpFormat(int format);
483int getRotOutFmt(uint32_t format);
484/* flip is upside down and such. V, H flip
485 * rotation is 90, 180 etc
486 * It returns MDP related enum/define that match rot+flip*/
487int getMdpOrient(eTransform rotation);
Saurabh Shah9c876d92012-08-25 19:29:53 -0700488const char* getFormatString(int format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700489const char* getStateString(eOverlayState state);
490
Naseer Ahmed29a26812012-06-14 00:56:20 -0700491// Cannot use HW_OVERLAY_MAGNIFICATION_LIMIT, since at the time
492// of integration, HW_OVERLAY_MAGNIFICATION_LIMIT was a define
493enum { HW_OV_MAGNIFICATION_LIMIT = 20,
494 HW_OV_MINIFICATION_LIMIT = 8
495};
496
Naseer Ahmed29a26812012-06-14 00:56:20 -0700497template <class T>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700498inline void memset0(T& t) { ::memset(&t, 0, sizeof(T)); }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700499
500template <class T> inline void swap ( T& a, T& b )
501{
502 T c(a); a=b; b=c;
503}
504
505inline int alignup(int value, int a) {
506 //if align = 0, return the value. Else, do alignment.
507 return a ? ((((value - 1) / a) + 1) * a) : value;
508}
509
510// FIXME that align should replace the upper one.
511inline int align(int value, int a) {
512 //if align = 0, return the value. Else, do alignment.
513 return a ? ((value + (a-1)) & ~(a-1)) : value;
514}
515
Naseer Ahmed29a26812012-06-14 00:56:20 -0700516enum eRotOutFmt {
517 ROT_OUT_FMT_DEFAULT,
518 ROT_OUT_FMT_Y_CRCB_H2V2
519};
520
521template <int ROT_OUT_FMT> struct RotOutFmt;
522
523// FIXME, taken from gralloc_priv.h. Need to
524// put it back as soon as overlay takes place of the old one
525/* possible formats for 3D content*/
526enum {
527 HAL_NO_3D = 0x0000,
528 HAL_3D_IN_SIDE_BY_SIDE_L_R = 0x10000,
529 HAL_3D_IN_TOP_BOTTOM = 0x20000,
530 HAL_3D_IN_INTERLEAVE = 0x40000,
531 HAL_3D_IN_SIDE_BY_SIDE_R_L = 0x80000,
532 HAL_3D_OUT_SIDE_BY_SIDE = 0x1000,
533 HAL_3D_OUT_TOP_BOTTOM = 0x2000,
534 HAL_3D_OUT_INTERLEAVE = 0x4000,
535 HAL_3D_OUT_MONOSCOPIC = 0x8000
536};
537
538enum { HAL_3D_OUT_SBS_MASK =
539 HAL_3D_OUT_SIDE_BY_SIDE >> overlay::utils::SHIFT_OUT_3D,
540 HAL_3D_OUT_TOP_BOT_MASK =
541 HAL_3D_OUT_TOP_BOTTOM >> overlay::utils::SHIFT_OUT_3D,
542 HAL_3D_OUT_INTERL_MASK =
543 HAL_3D_OUT_INTERLEAVE >> overlay::utils::SHIFT_OUT_3D,
544 HAL_3D_OUT_MONOS_MASK =
545 HAL_3D_OUT_MONOSCOPIC >> overlay::utils::SHIFT_OUT_3D
546};
547
548
549inline bool isYuv(uint32_t format) {
550 switch(format){
551 case MDP_Y_CBCR_H2V1:
552 case MDP_Y_CBCR_H2V2:
553 case MDP_Y_CRCB_H2V2:
Saurabh Shahb121e142012-08-20 17:59:13 -0700554 case MDP_Y_CRCB_H1V1:
555 case MDP_Y_CRCB_H2V1:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700556 case MDP_Y_CRCB_H2V2_TILE:
557 case MDP_Y_CBCR_H2V2_TILE:
Saurabh Shahb121e142012-08-20 17:59:13 -0700558 case MDP_Y_CR_CB_H2V2:
Saurabh Shahae1044e2012-08-21 16:03:32 -0700559 case MDP_Y_CR_CB_GH2V2:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700560 return true;
561 default:
562 return false;
563 }
564 return false;
565}
566
567inline bool isRgb(uint32_t format) {
568 switch(format) {
569 case MDP_RGBA_8888:
570 case MDP_BGRA_8888:
571 case MDP_RGBX_8888:
572 case MDP_RGB_565:
573 return true;
574 default:
575 return false;
576 }
577 return false;
578}
579
580inline bool isValidDest(eDest dest)
581{
582 if ((OV_PIPE0 & dest) ||
583 (OV_PIPE1 & dest) ||
584 (OV_PIPE2 & dest)) {
585 return true;
586 }
587 return false;
588}
589
Saurabh Shah9c876d92012-08-25 19:29:53 -0700590inline const char* getFormatString(int format){
Naseer Ahmed29a26812012-06-14 00:56:20 -0700591 static const char* const formats[] = {
592 "MDP_RGB_565",
593 "MDP_XRGB_8888",
594 "MDP_Y_CBCR_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700595 "MDP_Y_CBCR_H2V2_ADRENO",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700596 "MDP_ARGB_8888",
597 "MDP_RGB_888",
598 "MDP_Y_CRCB_H2V2",
599 "MDP_YCRYCB_H2V1",
600 "MDP_Y_CRCB_H2V1",
601 "MDP_Y_CBCR_H2V1",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700602 "MDP_Y_CRCB_H1V2",
603 "MDP_Y_CBCR_H1V2",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700604 "MDP_RGBA_8888",
605 "MDP_BGRA_8888",
606 "MDP_RGBX_8888",
607 "MDP_Y_CRCB_H2V2_TILE",
608 "MDP_Y_CBCR_H2V2_TILE",
609 "MDP_Y_CR_CB_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700610 "MDP_Y_CR_CB_GH2V2",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700611 "MDP_Y_CB_CR_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700612 "MDP_Y_CRCB_H1V1",
613 "MDP_Y_CBCR_H1V1",
614 "MDP_YCRCB_H1V1",
615 "MDP_YCBCR_H1V1",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700616 "MDP_BGR_565",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700617 "MDP_IMGTYPE_LIMIT",
618 "MDP_RGB_BORDERFILL",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700619 "MDP_FB_FORMAT",
620 "MDP_IMGTYPE_LIMIT2"
621 };
Saurabh Shah9c876d92012-08-25 19:29:53 -0700622 if(format < 0 || format >= (int)(sizeof(formats) / sizeof(formats[0]))) {
623 ALOGE("%s wrong fmt %d", __FUNCTION__, format);
624 return "Unsupported format";
625 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700626 return formats[format];
627}
628
629inline const char* getStateString(eOverlayState state){
630 switch (state) {
631 case OV_CLOSED:
632 return "OV_CLOSED";
633 case OV_2D_VIDEO_ON_PANEL:
634 return "OV_2D_VIDEO_ON_PANEL";
635 case OV_2D_VIDEO_ON_PANEL_TV:
636 return "OV_2D_VIDEO_ON_PANEL_TV";
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700637 case OV_2D_VIDEO_ON_TV:
638 return "OV_2D_VIDEO_ON_TV";
Naseer Ahmed29a26812012-06-14 00:56:20 -0700639 case OV_3D_VIDEO_ON_2D_PANEL:
640 return "OV_3D_VIDEO_ON_2D_PANEL";
641 case OV_3D_VIDEO_ON_3D_PANEL:
642 return "OV_3D_VIDEO_ON_3D_PANEL";
643 case OV_3D_VIDEO_ON_3D_TV:
644 return "OV_3D_VIDEO_ON_3D_TV";
645 case OV_3D_VIDEO_ON_2D_PANEL_2D_TV:
646 return "OV_3D_VIDEO_ON_2D_PANEL_2D_TV";
647 case OV_UI_MIRROR:
648 return "OV_UI_MIRROR";
649 case OV_2D_TRUE_UI_MIRROR:
650 return "OV_2D_TRUE_UI_MIRROR";
651 case OV_BYPASS_1_LAYER:
652 return "OV_BYPASS_1_LAYER";
653 case OV_BYPASS_2_LAYER:
654 return "OV_BYPASS_2_LAYER";
655 case OV_BYPASS_3_LAYER:
656 return "OV_BYPASS_3_LAYER";
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700657 case OV_DUAL_DISP:
658 return "OV_DUAL_DISP";
Naseer Ahmed29a26812012-06-14 00:56:20 -0700659 default:
660 return "UNKNOWN_STATE";
661 }
662 return "BAD_STATE";
663}
664
Naseer Ahmed29a26812012-06-14 00:56:20 -0700665inline void Whf::dump() const {
666 ALOGE("== Dump WHF w=%d h=%d f=%d s=%d start/end ==",
667 w, h, format, size);
668}
669
670inline void Dim::dump() const {
671 ALOGE("== Dump Dim x=%d y=%d w=%d h=%d start/end ==", x, y, w, h);
672}
673
674inline int getMdpOrient(eTransform rotation) {
675 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, rotation);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700676 switch(rotation)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700677 {
678 case OVERLAY_TRANSFORM_0 : return 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700679 case OVERLAY_TRANSFORM_FLIP_V: return MDP_FLIP_UD;
680 case OVERLAY_TRANSFORM_FLIP_H: return MDP_FLIP_LR;
681 case OVERLAY_TRANSFORM_ROT_90: return MDP_ROT_90;
Saurabh Shaha73738d2012-08-09 18:15:18 -0700682 //getMdpOrient will switch the flips if the source is 90 rotated.
683 //Clients in Android dont factor in 90 rotation while deciding flip.
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700684 case OVERLAY_TRANSFORM_ROT_90_FLIP_V:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700685 return MDP_ROT_90 | MDP_FLIP_LR;
Saurabh Shaha73738d2012-08-09 18:15:18 -0700686 case OVERLAY_TRANSFORM_ROT_90_FLIP_H:
687 return MDP_ROT_90 | MDP_FLIP_UD;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700688 case OVERLAY_TRANSFORM_ROT_180: return MDP_ROT_180;
689 case OVERLAY_TRANSFORM_ROT_270: return MDP_ROT_270;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700690 default:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700691 ALOGE("%s: invalid rotation value (value = 0x%x",
692 __FUNCTION__, rotation);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700693 }
694 return -1;
695}
696
697inline int getRotOutFmt(uint32_t format) {
698 switch (format) {
699 case MDP_Y_CRCB_H2V2_TILE:
700 return MDP_Y_CRCB_H2V2;
701 case MDP_Y_CBCR_H2V2_TILE:
702 return MDP_Y_CBCR_H2V2;
703 case MDP_Y_CB_CR_H2V2:
704 return MDP_Y_CBCR_H2V2;
Saurabh Shahae1044e2012-08-21 16:03:32 -0700705 case MDP_Y_CR_CB_GH2V2:
706 return MDP_Y_CRCB_H2V2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700707 default:
708 return format;
709 }
710 // not reached
711 OVASSERT(false, "%s not reached", __FUNCTION__);
712 return -1;
713}
714
Naseer Ahmed29a26812012-06-14 00:56:20 -0700715
716inline uint32_t getColorFormat(uint32_t format)
717{
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700718 return (format == HAL_PIXEL_FORMAT_YV12) ?
719 format : colorFormat(format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700720}
721
722// FB0
723template <int CHAN>
724inline Dim getPositionS3DImpl(const Whf& whf)
725{
726 switch (whf.format & OUTPUT_3D_MASK)
727 {
728 case HAL_3D_OUT_SBS_MASK:
729 // x, y, w, h
730 return Dim(0, 0, whf.w/2, whf.h);
731 case HAL_3D_OUT_TOP_BOT_MASK:
732 return Dim(0, 0, whf.w, whf.h/2);
733 case HAL_3D_OUT_MONOS_MASK:
734 return Dim();
735 case HAL_3D_OUT_INTERL_MASK:
736 // FIXME error?
737 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
738 return Dim();
739 default:
740 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
741 whf.format);
742 }
743 return Dim();
744}
745
746template <>
747inline Dim getPositionS3DImpl<utils::OV_PIPE1>(const Whf& whf)
748{
749 switch (whf.format & OUTPUT_3D_MASK)
750 {
751 case HAL_3D_OUT_SBS_MASK:
752 return Dim(whf.w/2, 0, whf.w/2, whf.h);
753 case HAL_3D_OUT_TOP_BOT_MASK:
754 return Dim(0, whf.h/2, whf.w, whf.h/2);
755 case HAL_3D_OUT_MONOS_MASK:
756 return Dim(0, 0, whf.w, whf.h);
757 case HAL_3D_OUT_INTERL_MASK:
758 // FIXME error?
759 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
760 return Dim();
761 default:
762 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
763 whf.format);
764 }
765 return Dim();
766}
767
768template <int CHAN>
769inline bool getPositionS3D(const Whf& whf, Dim& out) {
770 out = getPositionS3DImpl<CHAN>(whf);
771 return (out != Dim());
772}
773
774template <int CHAN>
775inline Dim getCropS3DImpl(const Dim& in, uint32_t fmt) {
776 switch (fmt & INPUT_3D_MASK)
777 {
778 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
779 return Dim(0, 0, in.w/2, in.h);
780 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
781 return Dim(in.w/2, 0, in.w/2, in.h);
782 case HAL_3D_IN_TOP_BOTTOM:
783 return Dim(0, 0, in.w, in.h/2);
784 case HAL_3D_IN_INTERLEAVE:
785 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
786 break;
787 default:
788 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
789 break;
790 }
791 return Dim();
792}
793
794template <>
795inline Dim getCropS3DImpl<utils::OV_PIPE1>(const Dim& in, uint32_t fmt) {
796 switch (fmt & INPUT_3D_MASK)
797 {
798 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
799 return Dim(in.w/2, 0, in.w/2, in.h);
800 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
801 return Dim(0, 0, in.w/2, in.h);
802 case HAL_3D_IN_TOP_BOTTOM:
803 return Dim(0, in.h/2, in.w, in.h/2);
804 case HAL_3D_IN_INTERLEAVE:
805 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
806 break;
807 default:
808 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
809 break;
810 }
811 return Dim();
812}
813
814template <int CHAN>
815inline bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt)
816{
817 out = getCropS3DImpl<CHAN>(in, fmt);
818 return (out != Dim());
819}
820
821template <class Type>
822void swapWidthHeight(Type& width, Type& height) {
823 Type tmp = width;
824 width = height;
825 height = tmp;
826}
827
828inline void ScreenInfo::dump(const char* const s) const {
829 ALOGE("== Dump %s ScreenInfo w=%d h=%d"
830 " bpp=%d stride=%d start/end ==",
831 s, mFBWidth, mFBHeight, mFBbpp, mFBystride);
832}
833
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700834inline bool openDev(OvFD& fd, int fbnum,
835 const char* const devpath, int flags) {
836 return overlay::open(fd, fbnum, devpath, flags);
837}
838
Saurabh Shahb121e142012-08-20 17:59:13 -0700839template <class T>
840inline void even_ceil(T& value) {
841 if(value & 1)
842 value++;
843}
844
845template <class T>
846inline void even_floor(T& value) {
847 if(value & 1)
848 value--;
849}
850
Naseer Ahmed29a26812012-06-14 00:56:20 -0700851} // namespace utils ends
852
853//--------------------Class Res stuff (namespace overlay only) -----------
854
855class Res {
856public:
857 // /dev/graphics/fb%u
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700858 static const char* const fbPath;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700859 // /dev/msm_rotator
860 static const char* const rotPath;
861 // /sys/class/graphics/fb1/format_3d
862 static const char* const format3DFile;
863 // /sys/class/graphics/fb1/3d_present
864 static const char* const edid3dInfoFile;
865 // /sys/devices/platform/mipi_novatek.0/enable_3d_barrier
866 static const char* const barrierFile;
867};
868
869
870//--------------------Class OvFD stuff (namespace overlay only) -----------
871
Naseer Ahmed29a26812012-06-14 00:56:20 -0700872/*
873* Holds one FD
874* Dtor will NOT close the underlying FD.
875* That enables us to copy that object around
876* */
877class OvFD {
878public:
879 /* Ctor */
880 explicit OvFD();
881
882 /* dtor will NOT close the underlying FD */
883 ~OvFD();
884
885 /* Open fd using the path given by dev.
886 * return false in failure */
887 bool open(const char* const dev,
888 int flags = O_RDWR);
889
890 /* populate path */
891 void setPath(const char* const dev);
892
893 /* Close fd if we have a valid fd. */
894 bool close();
895
896 /* returns underlying fd.*/
897 int getFD() const;
898
899 /* returns true if fd is valid */
900 bool valid() const;
901
902 /* like operator= */
903 void copy(int fd);
904
905 /* dump the state of the instance */
906 void dump() const;
907private:
908 /* helper enum for determine valid/invalid fd */
909 enum { INVAL = -1 };
910
911 /* actual os fd */
912 int mFD;
913
914 /* path, for debugging */
915 char mPath[utils::MAX_PATH_LEN];
916};
917
918//-------------------Inlines--------------------------
919
920inline bool open(OvFD& fd, uint32_t fbnum, const char* const dev, int flags)
921{
922 char dev_name[64] = {0};
923 snprintf(dev_name, sizeof(dev_name), dev, fbnum);
924 return fd.open(dev_name, flags);
925}
926
927inline OvFD::OvFD() : mFD (INVAL) {
928 mPath[0] = 0;
929}
930
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700931inline OvFD::~OvFD() {
932 //no op since copy() can be used to share fd, in 3d cases.
933}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700934
935inline bool OvFD::open(const char* const dev, int flags)
936{
937 mFD = ::open(dev, flags, 0);
938 if (mFD < 0) {
939 // FIXME errno, strerror in bionic?
940 ALOGE("Cant open device %s err=%d", dev, errno);
941 return false;
942 }
943 setPath(dev);
944 return true;
945}
946
947inline void OvFD::setPath(const char* const dev)
948{
949 ::strncpy(mPath, dev, utils::MAX_PATH_LEN);
950}
951
952inline bool OvFD::close()
953{
954 int ret = 0;
955 if(valid()) {
956 ret = ::close(mFD);
957 mFD = INVAL;
958 }
959 return (ret == 0);
960}
961
962inline bool OvFD::valid() const
963{
964 return (mFD != INVAL);
965}
966
967inline int OvFD::getFD() const { return mFD; }
968
969inline void OvFD::copy(int fd) {
970 mFD = fd;
971}
972
973inline void OvFD::dump() const
974{
975 ALOGE("== Dump OvFD fd=%d path=%s start/end ==",
976 mFD, mPath);
977}
978
979//--------------- class OvFD stuff ends ---------------------
980
981} // overlay
982
983
984#endif // OVERLAY_UTILS_H