blob: a09010c02bb6e25f2b313ea03b2043a3c2df8a5e [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
2* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
3*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are
6* met:
7* * Redistributions of source code must retain the above copyright
8* notice, this list of conditions and the following disclaimer.
9* * Redistributions in binary form must reproduce the above
10* copyright notice, this list of conditions and the following
11* disclaimer in the documentation and/or other materials provided
12* with the distribution.
13* * Neither the name of Code Aurora Forum, Inc. nor the names of its
14* contributors may be used to endorse or promote products derived
15* from this software without specific prior written permission.
16*
17* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
29
30#ifndef OVERLAY_UTILS_H
31#define OVERLAY_UTILS_H
32
33#include <cutils/log.h> // ALOGE, etc
34#include <errno.h>
35#include <fcntl.h> // open, O_RDWR, etc
36#include <hardware/hardware.h>
37#include <hardware/gralloc.h> // buffer_handle_t
Naseer Ahmedf48aef62012-07-20 09:05:53 -070038#include <linux/msm_mdp.h> // flags
Naseer Ahmed29a26812012-06-14 00:56:20 -070039#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <sys/stat.h>
43#include <sys/types.h>
44#include <utils/Log.h>
Naseer Ahmedf48aef62012-07-20 09:05:53 -070045#include "gralloc_priv.h" //for interlace
Naseer Ahmed29a26812012-06-14 00:56:20 -070046/*
47*
48* Collection of utilities functions/structs/enums etc...
49*
50* */
51
52// comment that out if you want to remove asserts
53// or put it as -D in Android.mk. your choice.
54#define OVERLAY_HAS_ASSERT
55
56#ifdef OVERLAY_HAS_ASSERT
57# define OVASSERT(x, ...) if(!(x)) { ALOGE(__VA_ARGS__); abort(); }
58#else
59# define OVASSERT(x, ...) ALOGE_IF(!(x), __VA_ARGS__)
60#endif // OVERLAY_HAS_ASSERT
61
62#define DEBUG_OVERLAY 0
63#define PROFILE_OVERLAY 0
64
65namespace overlay {
66
67// fwd
68class Overlay;
69
70namespace utils {
71struct Whf;
72struct Dim;
Naseer Ahmed29a26812012-06-14 00:56:20 -070073
74inline uint32_t setBit(uint32_t x, uint32_t mask) {
75 return (x | mask);
76}
77
78inline uint32_t clrBit(uint32_t x, uint32_t mask) {
79 return (x & ~mask);
80}
81
82/* Utility class to help avoid copying instances by making the copy ctor
83* and assignment operator private
84*
85* Usage:
Naseer Ahmedf48aef62012-07-20 09:05:53 -070086* class SomeClass : utils::NoCopy {...};
Naseer Ahmed29a26812012-06-14 00:56:20 -070087*/
88class NoCopy {
89protected:
90 NoCopy(){}
91 ~NoCopy() {}
92private:
93 NoCopy(const NoCopy&);
94 const NoCopy& operator=(const NoCopy&);
95};
96
97/*
98* Utility class to query the framebuffer info for primary display
99*
100* Usage:
101* Outside of functions:
102* utils::FrameBufferInfo* utils::FrameBufferInfo::sFBInfoInstance = 0;
103* Inside functions:
104* utils::FrameBufferInfo::getInstance()->supportTrueMirroring()
105*/
106class FrameBufferInfo {
107
108public:
109 /* ctor init */
110 explicit FrameBufferInfo();
111
112 /* Gets an instance if one does not already exist */
113 static FrameBufferInfo* getInstance();
114
115 /* Gets width of primary framebuffer */
116 int getWidth() const;
117
118 /* Gets height of primary framebuffer */
119 int getHeight() const;
120
121 /* Indicates whether true mirroring is supported */
122 bool supportTrueMirroring() const;
123
124private:
125 int mFBWidth;
126 int mFBHeight;
127 bool mBorderFillSupported;
128 static FrameBufferInfo *sFBInfoInstance;
129};
130
131/* 3D related utils, defines etc...
132 * The compound format passed to the overlay is
133 * ABCCC where A is the input 3D format
134 * B is the output 3D format
135 * CCC is the color format e.g YCbCr420SP YCrCb420SP etc */
136enum { SHIFT_OUT_3D = 12,
137 SHIFT_TOT_3D = 16 };
138enum { INPUT_3D_MASK = 0xFFFF0000,
139 OUTPUT_3D_MASK = 0x0000FFFF };
140enum { BARRIER_LAND = 1,
141 BARRIER_PORT = 2 };
142
143inline uint32_t format3D(uint32_t x) { return x & 0xFF000; }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700144inline uint32_t colorFormat(uint32_t fmt) {
145 /*TODO enable this block only if format has interlace / 3D info in top bits.
146 if(fmt & INTERLACE_MASK) {
147 fmt = fmt ^ HAL_PIXEL_FORMAT_INTERLACE;
148 }
149 fmt = fmt & 0xFFF;*/
150 return fmt;
151}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700152inline uint32_t format3DOutput(uint32_t x) {
153 return (x & 0xF000) >> SHIFT_OUT_3D; }
154inline uint32_t format3DInput(uint32_t x) { return x & 0xF0000; }
155uint32_t getColorFormat(uint32_t format);
156
157bool isHDMIConnected ();
158bool is3DTV();
159bool isPanel3D();
160bool usePanel3D();
161bool send3DInfoPacket (uint32_t fmt);
162bool enableBarrier (uint32_t orientation);
163uint32_t getS3DFormat(uint32_t fmt);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700164
Naseer Ahmed29a26812012-06-14 00:56:20 -0700165template <int CHAN>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700166bool getPositionS3D(const Whf& whf, Dim& out);
167
Naseer Ahmed29a26812012-06-14 00:56:20 -0700168template <int CHAN>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700169bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt);
170
Naseer Ahmed29a26812012-06-14 00:56:20 -0700171template <class Type>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700172void swapWidthHeight(Type& width, Type& height);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700173
174struct Dim {
175 Dim () : x(0), y(0),
176 w(0), h(0),
177 o(0) {}
178 Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h) :
179 x(_x), y(_y),
180 w(_w), h(_h) {}
181 Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h, uint32_t _o) :
182 x(_x), y(_y),
183 w(_w), h(_h),
184 o(_o) {}
185 bool check(uint32_t _w, uint32_t _h) const {
186 return (x+w <= _w && y+h <= _h);
187
188 }
189
190 bool operator==(const Dim& d) const {
191 return d.x == x && d.y == y &&
192 d.w == w && d.h == h &&
193 d.o == o;
194 }
195
196 bool operator!=(const Dim& d) const {
197 return !operator==(d);
198 }
199
Naseer Ahmed29a26812012-06-14 00:56:20 -0700200 void dump() const;
201 uint32_t x;
202 uint32_t y;
203 uint32_t w;
204 uint32_t h;
205 uint32_t o;
206};
207
208// TODO have Whfz
209
210struct Whf {
211 Whf() : w(0), h(0), format(0), size(0) {}
212 Whf(uint32_t wi, uint32_t he, uint32_t f) :
213 w(wi), h(he), format(f), size(0) {}
214 Whf(uint32_t wi, uint32_t he, uint32_t f, uint32_t s) :
215 w(wi), h(he), format(f), size(s) {}
216 // FIXME not comparing size at the moment
217 bool operator==(const Whf& whf) const {
218 return whf.w == w && whf.h == h &&
219 whf.format == format;
220 }
221 bool operator!=(const Whf& whf) const {
222 return !operator==(whf);
223 }
224 void dump() const;
225 uint32_t w;
226 uint32_t h;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700227 uint32_t format;
228 uint32_t size;
229};
230
231enum { MAX_PATH_LEN = 256 };
232
Naseer Ahmed29a26812012-06-14 00:56:20 -0700233/**
234 * Rotator flags: not to be confused with orientation flags.
235 * Ususally, you want to open the rotator to make sure it is
236 * ready for business.
237 * ROT_FLAG_DISABLED: Rotator would not kick in. (ioctl will emit errors).
238 * ROT_FLAG_ENABLED: and when rotation is needed.
239 * (prim video playback)
240 * (UI mirroring on HDMI w/ 0 degree rotator. - just memcpy)
241 * In HDMI UI mirroring, rotator is always used.
242 * Even when w/o orienation change on primary,
243 * we do 0 rotation on HDMI and using rotator buffers.
244 * That is because we might see tearing otherwise. so
245 * we use another buffer (rotator).
246 * When a simple video playback on HDMI, no rotator is being used.(null r).
247 * */
248enum eRotFlags {
249 ROT_FLAG_DISABLED = 0,
250 ROT_FLAG_ENABLED = 1 // needed in rot
251};
252
Naseer Ahmed29a26812012-06-14 00:56:20 -0700253/* The values for is_fg flag for control alpha and transp
254 * IS_FG_OFF means is_fg = 0
255 * IS_FG_SET means is_fg = 1
256 */
257enum eIsFg {
258 IS_FG_OFF = 0,
259 IS_FG_SET = 1
260};
261
262/*
263 * Various mdp flags like PIPE SHARE, DEINTERLACE etc...
264 * kernel/common/linux/msm_mdp.h
265 * INTERLACE_MASK: hardware/qcom/display/libgralloc/badger/fb_priv.h
266 * */
267enum eMdpFlags {
268 OV_MDP_FLAGS_NONE = 0,
269 OV_MDP_PIPE_SHARE = MDP_OV_PIPE_SHARE,
270 OV_MDP_DEINTERLACE = MDP_DEINTERLACE,
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700271 OV_MDP_SECURE_OVERLAY_SESSION = MDP_SECURE_OVERLAY_SESSION,
272 OV_MDP_SOURCE_ROTATED_90 = MDP_SOURCE_ROTATED_90,
273 OV_MDP_MEMORY_ID_TYPE_FB = MDP_MEMORY_ID_TYPE_FB,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700274};
275
Naseer Ahmed29a26812012-06-14 00:56:20 -0700276enum eZorder {
277 ZORDER_0,
278 ZORDER_1,
279 ZORDER_2,
280 Z_SYSTEM_ALLOC = 0xFFFF
281};
282
283enum eMdpPipeType {
284 OV_MDP_PIPE_RGB,
285 OV_MDP_PIPE_VG
286};
287
Naseer Ahmed29a26812012-06-14 00:56:20 -0700288// Max pipes via overlay (VG0, VG1, RGB1)
289enum { MAX_PIPES = 3 };
290
291/* Used to identify destination channels and
292 * also 3D channels e.g. when in 3D mode with 2
293 * pipes opened and it is used in get crop/pos 3D
Naseer Ahmed29a26812012-06-14 00:56:20 -0700294 * */
295enum eDest {
296 OV_PIPE0 = 1 << 0,
297 OV_PIPE1 = 1 << 1,
298 OV_PIPE2 = 1 << 2,
299 OV_PIPE_ALL = (OV_PIPE0 | OV_PIPE1 | OV_PIPE2)
300};
301
302/* values for copybit_set_parameter(OVERLAY_TRANSFORM) */
303enum eTransform {
304 /* No rot */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700305 OVERLAY_TRANSFORM_0 = 0x0,
306 /* flip source image horizontally 0x1 */
307 OVERLAY_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H,
308 /* flip source image vertically 0x2 */
309 OVERLAY_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700310 /* rotate source image 180 degrees
311 * It is basically bit-or-ed H | V == 0x3 */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700312 OVERLAY_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
313 /* rotate source image 90 degrees 0x4 */
314 OVERLAY_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
315 /* rotate source image 90 degrees and flip horizontally 0x5 */
316 OVERLAY_TRANSFORM_ROT_90_FLIP_H = HAL_TRANSFORM_ROT_90 |
317 HAL_TRANSFORM_FLIP_H,
318 /* rotate source image 90 degrees and flip vertically 0x6 */
319 OVERLAY_TRANSFORM_ROT_90_FLIP_V = HAL_TRANSFORM_ROT_90 |
320 HAL_TRANSFORM_FLIP_V,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700321 /* rotate source image 270 degrees
322 * Basically 180 | 90 == 0x7 */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700323 OVERLAY_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700324 /* rotate invalid like in Transform.h */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700325 OVERLAY_TRANSFORM_INV = 0x80
Naseer Ahmed29a26812012-06-14 00:56:20 -0700326};
327
328// Used to consolidate pipe params
329struct PipeArgs {
330 PipeArgs() : mdpFlags(OV_MDP_FLAGS_NONE),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700331 zorder(Z_SYSTEM_ALLOC),
332 isFg(IS_FG_OFF),
333 rotFlags(ROT_FLAG_DISABLED){
334 }
335
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700336 PipeArgs(eMdpFlags f, Whf _whf,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700337 eZorder z, eIsFg fg, eRotFlags r) :
338 mdpFlags(f),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700339 whf(_whf),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700340 zorder(z),
341 isFg(fg),
342 rotFlags(r) {
343 }
344
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700345 eMdpFlags mdpFlags; // for mdp_overlay flags
Naseer Ahmed29a26812012-06-14 00:56:20 -0700346 Whf whf;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700347 eZorder zorder; // stage number
348 eIsFg isFg; // control alpha & transp
349 eRotFlags rotFlags;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700350};
351
352enum eOverlayState{
353 /* No pipes from overlay open */
354 OV_CLOSED = 0,
355
356 /* 2D Video */
357 OV_2D_VIDEO_ON_PANEL,
358 OV_2D_VIDEO_ON_PANEL_TV,
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700359 OV_2D_VIDEO_ON_TV,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700360
361 /* 3D Video on one display (panel or TV) */
362 OV_3D_VIDEO_ON_2D_PANEL,
363 OV_3D_VIDEO_ON_3D_PANEL,
364 OV_3D_VIDEO_ON_3D_TV,
365
366 /* 3D Video on two displays (panel and TV) */
367 OV_3D_VIDEO_ON_2D_PANEL_2D_TV,
368
369 /* UI Mirroring */
370 OV_UI_MIRROR,
371 OV_2D_TRUE_UI_MIRROR,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700372
373 /* Composition Bypass */
374 OV_BYPASS_1_LAYER,
375 OV_BYPASS_2_LAYER,
376 OV_BYPASS_3_LAYER,
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700377
378 /* External only for dual-disp */
379 OV_DUAL_DISP,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700380};
381
382inline void setMdpFlags(eMdpFlags& f, eMdpFlags v) {
383 f = static_cast<eMdpFlags>(setBit(f, v));
384}
385
386inline void clearMdpFlags(eMdpFlags& f, eMdpFlags v) {
387 f = static_cast<eMdpFlags>(clrBit(f, v));
388}
389
390// fb 0/1/2
391enum { FB0, FB1, FB2 };
392
393//Panels could be categorized as primary and external
394enum { PRIMARY, EXTERNAL };
395
396//External Panels could use HDMI or WFD
397enum {
398 HDMI = 1,
399 WFD = 2
400};
401
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700402//TODO Make this a part of some appropriate class
Naseer Ahmed29a26812012-06-14 00:56:20 -0700403static int sExtType = HDMI; //HDMI or WFD
Naseer Ahmed29a26812012-06-14 00:56:20 -0700404//Set by client as HDMI/WFD
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700405void setExtType(const int& type);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700406//Return External panel type set by client.
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700407int getExtType();
408
Naseer Ahmed29a26812012-06-14 00:56:20 -0700409
410//Gets the FB number for the external type.
411//As of now, HDMI always has fb1, WFD could use fb1 or fb2
412//Assumes Ext type set by setExtType() from client.
413static int getFBForPanel(int panel) { // PRIMARY OR EXTERNAL
414 switch(panel) {
415 case PRIMARY: return FB0;
416 break;
417 case EXTERNAL:
418 switch(getExtType()) {
419 case HDMI: return FB1;
420 break;
421 case WFD: return FB2;//Hardcoding fb2 for wfd. Will change.
422 break;
423 }
424 break;
425 default:
426 ALOGE("%s: Unrecognized PANEL category %d", __func__, panel);
427 break;
428 }
429 return -1;
430}
431
432// number of rgb pipes bufs (max)
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700433
Naseer Ahmed29a26812012-06-14 00:56:20 -0700434// 2 for rgb0/1 double bufs
435enum { RGB_PIPE_NUM_BUFS = 2 };
436
437struct ScreenInfo {
438 ScreenInfo() : mFBWidth(0),
439 mFBHeight(0),
440 mFBbpp(0),
441 mFBystride(0) {}
442 void dump(const char* const s) const;
443 uint32_t mFBWidth;
444 uint32_t mFBHeight;
445 uint32_t mFBbpp;
446 uint32_t mFBystride;
447};
448
449int getMdpFormat(int format);
450int getRotOutFmt(uint32_t format);
451/* flip is upside down and such. V, H flip
452 * rotation is 90, 180 etc
453 * It returns MDP related enum/define that match rot+flip*/
454int getMdpOrient(eTransform rotation);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700455const char* getFormatString(uint32_t format);
456const char* getStateString(eOverlayState state);
457
Naseer Ahmed29a26812012-06-14 00:56:20 -0700458// Cannot use HW_OVERLAY_MAGNIFICATION_LIMIT, since at the time
459// of integration, HW_OVERLAY_MAGNIFICATION_LIMIT was a define
460enum { HW_OV_MAGNIFICATION_LIMIT = 20,
461 HW_OV_MINIFICATION_LIMIT = 8
462};
463
Naseer Ahmed29a26812012-06-14 00:56:20 -0700464template <class T>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700465inline void memset0(T& t) { ::memset(&t, 0, sizeof(T)); }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700466
467template <class T> inline void swap ( T& a, T& b )
468{
469 T c(a); a=b; b=c;
470}
471
472inline int alignup(int value, int a) {
473 //if align = 0, return the value. Else, do alignment.
474 return a ? ((((value - 1) / a) + 1) * a) : value;
475}
476
477// FIXME that align should replace the upper one.
478inline int align(int value, int a) {
479 //if align = 0, return the value. Else, do alignment.
480 return a ? ((value + (a-1)) & ~(a-1)) : value;
481}
482
Naseer Ahmed29a26812012-06-14 00:56:20 -0700483enum eRotOutFmt {
484 ROT_OUT_FMT_DEFAULT,
485 ROT_OUT_FMT_Y_CRCB_H2V2
486};
487
488template <int ROT_OUT_FMT> struct RotOutFmt;
489
490// FIXME, taken from gralloc_priv.h. Need to
491// put it back as soon as overlay takes place of the old one
492/* possible formats for 3D content*/
493enum {
494 HAL_NO_3D = 0x0000,
495 HAL_3D_IN_SIDE_BY_SIDE_L_R = 0x10000,
496 HAL_3D_IN_TOP_BOTTOM = 0x20000,
497 HAL_3D_IN_INTERLEAVE = 0x40000,
498 HAL_3D_IN_SIDE_BY_SIDE_R_L = 0x80000,
499 HAL_3D_OUT_SIDE_BY_SIDE = 0x1000,
500 HAL_3D_OUT_TOP_BOTTOM = 0x2000,
501 HAL_3D_OUT_INTERLEAVE = 0x4000,
502 HAL_3D_OUT_MONOSCOPIC = 0x8000
503};
504
505enum { HAL_3D_OUT_SBS_MASK =
506 HAL_3D_OUT_SIDE_BY_SIDE >> overlay::utils::SHIFT_OUT_3D,
507 HAL_3D_OUT_TOP_BOT_MASK =
508 HAL_3D_OUT_TOP_BOTTOM >> overlay::utils::SHIFT_OUT_3D,
509 HAL_3D_OUT_INTERL_MASK =
510 HAL_3D_OUT_INTERLEAVE >> overlay::utils::SHIFT_OUT_3D,
511 HAL_3D_OUT_MONOS_MASK =
512 HAL_3D_OUT_MONOSCOPIC >> overlay::utils::SHIFT_OUT_3D
513};
514
515
516inline bool isYuv(uint32_t format) {
517 switch(format){
518 case MDP_Y_CBCR_H2V1:
519 case MDP_Y_CBCR_H2V2:
520 case MDP_Y_CRCB_H2V2:
521 case MDP_Y_CRCB_H2V2_TILE:
522 case MDP_Y_CBCR_H2V2_TILE:
523 return true;
524 default:
525 return false;
526 }
527 return false;
528}
529
530inline bool isRgb(uint32_t format) {
531 switch(format) {
532 case MDP_RGBA_8888:
533 case MDP_BGRA_8888:
534 case MDP_RGBX_8888:
535 case MDP_RGB_565:
536 return true;
537 default:
538 return false;
539 }
540 return false;
541}
542
543inline bool isValidDest(eDest dest)
544{
545 if ((OV_PIPE0 & dest) ||
546 (OV_PIPE1 & dest) ||
547 (OV_PIPE2 & dest)) {
548 return true;
549 }
550 return false;
551}
552
553inline const char* getFormatString(uint32_t format){
554 static const char* const formats[] = {
555 "MDP_RGB_565",
556 "MDP_XRGB_8888",
557 "MDP_Y_CBCR_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700558 "MDP_Y_CBCR_H2V2_ADRENO",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700559 "MDP_ARGB_8888",
560 "MDP_RGB_888",
561 "MDP_Y_CRCB_H2V2",
562 "MDP_YCRYCB_H2V1",
563 "MDP_Y_CRCB_H2V1",
564 "MDP_Y_CBCR_H2V1",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700565 "MDP_Y_CRCB_H1V2",
566 "MDP_Y_CBCR_H1V2",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700567 "MDP_RGBA_8888",
568 "MDP_BGRA_8888",
569 "MDP_RGBX_8888",
570 "MDP_Y_CRCB_H2V2_TILE",
571 "MDP_Y_CBCR_H2V2_TILE",
572 "MDP_Y_CR_CB_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700573 "MDP_Y_CR_CB_GH2V2",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700574 "MDP_Y_CB_CR_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700575 "MDP_Y_CRCB_H1V1",
576 "MDP_Y_CBCR_H1V1",
577 "MDP_YCRCB_H1V1",
578 "MDP_YCBCR_H1V1",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700579 "MDP_BGR_565",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700580 "MDP_IMGTYPE_LIMIT",
581 "MDP_RGB_BORDERFILL",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700582 "MDP_FB_FORMAT",
583 "MDP_IMGTYPE_LIMIT2"
584 };
585 OVASSERT(format < sizeof(formats) / sizeof(formats[0]),
586 "getFormatString wrong fmt %d", format);
587 return formats[format];
588}
589
590inline const char* getStateString(eOverlayState state){
591 switch (state) {
592 case OV_CLOSED:
593 return "OV_CLOSED";
594 case OV_2D_VIDEO_ON_PANEL:
595 return "OV_2D_VIDEO_ON_PANEL";
596 case OV_2D_VIDEO_ON_PANEL_TV:
597 return "OV_2D_VIDEO_ON_PANEL_TV";
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700598 case OV_2D_VIDEO_ON_TV:
599 return "OV_2D_VIDEO_ON_TV";
Naseer Ahmed29a26812012-06-14 00:56:20 -0700600 case OV_3D_VIDEO_ON_2D_PANEL:
601 return "OV_3D_VIDEO_ON_2D_PANEL";
602 case OV_3D_VIDEO_ON_3D_PANEL:
603 return "OV_3D_VIDEO_ON_3D_PANEL";
604 case OV_3D_VIDEO_ON_3D_TV:
605 return "OV_3D_VIDEO_ON_3D_TV";
606 case OV_3D_VIDEO_ON_2D_PANEL_2D_TV:
607 return "OV_3D_VIDEO_ON_2D_PANEL_2D_TV";
608 case OV_UI_MIRROR:
609 return "OV_UI_MIRROR";
610 case OV_2D_TRUE_UI_MIRROR:
611 return "OV_2D_TRUE_UI_MIRROR";
612 case OV_BYPASS_1_LAYER:
613 return "OV_BYPASS_1_LAYER";
614 case OV_BYPASS_2_LAYER:
615 return "OV_BYPASS_2_LAYER";
616 case OV_BYPASS_3_LAYER:
617 return "OV_BYPASS_3_LAYER";
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700618 case OV_DUAL_DISP:
619 return "OV_DUAL_DISP";
Naseer Ahmed29a26812012-06-14 00:56:20 -0700620 default:
621 return "UNKNOWN_STATE";
622 }
623 return "BAD_STATE";
624}
625
Naseer Ahmed29a26812012-06-14 00:56:20 -0700626inline void Whf::dump() const {
627 ALOGE("== Dump WHF w=%d h=%d f=%d s=%d start/end ==",
628 w, h, format, size);
629}
630
631inline void Dim::dump() const {
632 ALOGE("== Dump Dim x=%d y=%d w=%d h=%d start/end ==", x, y, w, h);
633}
634
635inline int getMdpOrient(eTransform rotation) {
636 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, rotation);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700637 switch(rotation)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700638 {
639 case OVERLAY_TRANSFORM_0 : return 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700640 case OVERLAY_TRANSFORM_FLIP_V: return MDP_FLIP_UD;
641 case OVERLAY_TRANSFORM_FLIP_H: return MDP_FLIP_LR;
642 case OVERLAY_TRANSFORM_ROT_90: return MDP_ROT_90;
643 case OVERLAY_TRANSFORM_ROT_90_FLIP_V:
644 return MDP_ROT_90 | MDP_FLIP_UD;
645 case OVERLAY_TRANSFORM_ROT_90_FLIP_H:
646 return MDP_ROT_90 | MDP_FLIP_LR;
647 case OVERLAY_TRANSFORM_ROT_180: return MDP_ROT_180;
648 case OVERLAY_TRANSFORM_ROT_270: return MDP_ROT_270;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700649 default:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700650 ALOGE("%s: invalid rotation value (value = 0x%x",
651 __FUNCTION__, rotation);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700652 }
653 return -1;
654}
655
656inline int getRotOutFmt(uint32_t format) {
657 switch (format) {
658 case MDP_Y_CRCB_H2V2_TILE:
659 return MDP_Y_CRCB_H2V2;
660 case MDP_Y_CBCR_H2V2_TILE:
661 return MDP_Y_CBCR_H2V2;
662 case MDP_Y_CB_CR_H2V2:
663 return MDP_Y_CBCR_H2V2;
664 default:
665 return format;
666 }
667 // not reached
668 OVASSERT(false, "%s not reached", __FUNCTION__);
669 return -1;
670}
671
Naseer Ahmed29a26812012-06-14 00:56:20 -0700672
673inline uint32_t getColorFormat(uint32_t format)
674{
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700675 return (format == HAL_PIXEL_FORMAT_YV12) ?
676 format : colorFormat(format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700677}
678
679// FB0
680template <int CHAN>
681inline Dim getPositionS3DImpl(const Whf& whf)
682{
683 switch (whf.format & OUTPUT_3D_MASK)
684 {
685 case HAL_3D_OUT_SBS_MASK:
686 // x, y, w, h
687 return Dim(0, 0, whf.w/2, whf.h);
688 case HAL_3D_OUT_TOP_BOT_MASK:
689 return Dim(0, 0, whf.w, whf.h/2);
690 case HAL_3D_OUT_MONOS_MASK:
691 return Dim();
692 case HAL_3D_OUT_INTERL_MASK:
693 // FIXME error?
694 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
695 return Dim();
696 default:
697 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
698 whf.format);
699 }
700 return Dim();
701}
702
703template <>
704inline Dim getPositionS3DImpl<utils::OV_PIPE1>(const Whf& whf)
705{
706 switch (whf.format & OUTPUT_3D_MASK)
707 {
708 case HAL_3D_OUT_SBS_MASK:
709 return Dim(whf.w/2, 0, whf.w/2, whf.h);
710 case HAL_3D_OUT_TOP_BOT_MASK:
711 return Dim(0, whf.h/2, whf.w, whf.h/2);
712 case HAL_3D_OUT_MONOS_MASK:
713 return Dim(0, 0, whf.w, whf.h);
714 case HAL_3D_OUT_INTERL_MASK:
715 // FIXME error?
716 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
717 return Dim();
718 default:
719 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
720 whf.format);
721 }
722 return Dim();
723}
724
725template <int CHAN>
726inline bool getPositionS3D(const Whf& whf, Dim& out) {
727 out = getPositionS3DImpl<CHAN>(whf);
728 return (out != Dim());
729}
730
731template <int CHAN>
732inline Dim getCropS3DImpl(const Dim& in, uint32_t fmt) {
733 switch (fmt & INPUT_3D_MASK)
734 {
735 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
736 return Dim(0, 0, in.w/2, in.h);
737 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
738 return Dim(in.w/2, 0, in.w/2, in.h);
739 case HAL_3D_IN_TOP_BOTTOM:
740 return Dim(0, 0, in.w, in.h/2);
741 case HAL_3D_IN_INTERLEAVE:
742 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
743 break;
744 default:
745 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
746 break;
747 }
748 return Dim();
749}
750
751template <>
752inline Dim getCropS3DImpl<utils::OV_PIPE1>(const Dim& in, uint32_t fmt) {
753 switch (fmt & INPUT_3D_MASK)
754 {
755 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
756 return Dim(in.w/2, 0, in.w/2, in.h);
757 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
758 return Dim(0, 0, in.w/2, in.h);
759 case HAL_3D_IN_TOP_BOTTOM:
760 return Dim(0, in.h/2, in.w, in.h/2);
761 case HAL_3D_IN_INTERLEAVE:
762 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
763 break;
764 default:
765 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
766 break;
767 }
768 return Dim();
769}
770
771template <int CHAN>
772inline bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt)
773{
774 out = getCropS3DImpl<CHAN>(in, fmt);
775 return (out != Dim());
776}
777
778template <class Type>
779void swapWidthHeight(Type& width, Type& height) {
780 Type tmp = width;
781 width = height;
782 height = tmp;
783}
784
785inline void ScreenInfo::dump(const char* const s) const {
786 ALOGE("== Dump %s ScreenInfo w=%d h=%d"
787 " bpp=%d stride=%d start/end ==",
788 s, mFBWidth, mFBHeight, mFBbpp, mFBystride);
789}
790
Naseer Ahmed29a26812012-06-14 00:56:20 -0700791} // namespace utils ends
792
793//--------------------Class Res stuff (namespace overlay only) -----------
794
795class Res {
796public:
797 // /dev/graphics/fb%u
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700798 static const char* const fbPath;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700799 // /dev/msm_rotator
800 static const char* const rotPath;
801 // /sys/class/graphics/fb1/format_3d
802 static const char* const format3DFile;
803 // /sys/class/graphics/fb1/3d_present
804 static const char* const edid3dInfoFile;
805 // /sys/devices/platform/mipi_novatek.0/enable_3d_barrier
806 static const char* const barrierFile;
807};
808
809
810//--------------------Class OvFD stuff (namespace overlay only) -----------
811
812class OvFD;
813
814/* helper function to open by using fbnum */
815bool open(OvFD& fd, uint32_t fbnum, const char* const dev,
816 int flags = O_RDWR);
817
818/*
819* Holds one FD
820* Dtor will NOT close the underlying FD.
821* That enables us to copy that object around
822* */
823class OvFD {
824public:
825 /* Ctor */
826 explicit OvFD();
827
828 /* dtor will NOT close the underlying FD */
829 ~OvFD();
830
831 /* Open fd using the path given by dev.
832 * return false in failure */
833 bool open(const char* const dev,
834 int flags = O_RDWR);
835
836 /* populate path */
837 void setPath(const char* const dev);
838
839 /* Close fd if we have a valid fd. */
840 bool close();
841
842 /* returns underlying fd.*/
843 int getFD() const;
844
845 /* returns true if fd is valid */
846 bool valid() const;
847
848 /* like operator= */
849 void copy(int fd);
850
851 /* dump the state of the instance */
852 void dump() const;
853private:
854 /* helper enum for determine valid/invalid fd */
855 enum { INVAL = -1 };
856
857 /* actual os fd */
858 int mFD;
859
860 /* path, for debugging */
861 char mPath[utils::MAX_PATH_LEN];
862};
863
864//-------------------Inlines--------------------------
865
866inline bool open(OvFD& fd, uint32_t fbnum, const char* const dev, int flags)
867{
868 char dev_name[64] = {0};
869 snprintf(dev_name, sizeof(dev_name), dev, fbnum);
870 return fd.open(dev_name, flags);
871}
872
873inline OvFD::OvFD() : mFD (INVAL) {
874 mPath[0] = 0;
875}
876
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700877inline OvFD::~OvFD() {
878 //no op since copy() can be used to share fd, in 3d cases.
879}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700880
881inline bool OvFD::open(const char* const dev, int flags)
882{
883 mFD = ::open(dev, flags, 0);
884 if (mFD < 0) {
885 // FIXME errno, strerror in bionic?
886 ALOGE("Cant open device %s err=%d", dev, errno);
887 return false;
888 }
889 setPath(dev);
890 return true;
891}
892
893inline void OvFD::setPath(const char* const dev)
894{
895 ::strncpy(mPath, dev, utils::MAX_PATH_LEN);
896}
897
898inline bool OvFD::close()
899{
900 int ret = 0;
901 if(valid()) {
902 ret = ::close(mFD);
903 mFD = INVAL;
904 }
905 return (ret == 0);
906}
907
908inline bool OvFD::valid() const
909{
910 return (mFD != INVAL);
911}
912
913inline int OvFD::getFD() const { return mFD; }
914
915inline void OvFD::copy(int fd) {
916 mFD = fd;
917}
918
919inline void OvFD::dump() const
920{
921 ALOGE("== Dump OvFD fd=%d path=%s start/end ==",
922 mFD, mPath);
923}
924
925//--------------- class OvFD stuff ends ---------------------
926
927} // overlay
928
929
930#endif // OVERLAY_UTILS_H