blob: 67cdb43a65f6825a323b17b5b755472b7827391e [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;
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
148inline uint32_t format3D(uint32_t x) { return x & 0xFF000; }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700149inline uint32_t colorFormat(uint32_t fmt) {
150 /*TODO enable this block only if format has interlace / 3D info in top bits.
151 if(fmt & INTERLACE_MASK) {
152 fmt = fmt ^ HAL_PIXEL_FORMAT_INTERLACE;
153 }
154 fmt = fmt & 0xFFF;*/
155 return fmt;
156}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700157inline uint32_t format3DOutput(uint32_t x) {
158 return (x & 0xF000) >> SHIFT_OUT_3D; }
159inline uint32_t format3DInput(uint32_t x) { return x & 0xF0000; }
160uint32_t getColorFormat(uint32_t format);
161
162bool isHDMIConnected ();
163bool is3DTV();
164bool isPanel3D();
165bool usePanel3D();
166bool send3DInfoPacket (uint32_t fmt);
167bool enableBarrier (uint32_t orientation);
168uint32_t getS3DFormat(uint32_t fmt);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700169
Naseer Ahmed29a26812012-06-14 00:56:20 -0700170template <int CHAN>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700171bool getPositionS3D(const Whf& whf, Dim& out);
172
Naseer Ahmed29a26812012-06-14 00:56:20 -0700173template <int CHAN>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700174bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt);
175
Naseer Ahmed29a26812012-06-14 00:56:20 -0700176template <class Type>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700177void swapWidthHeight(Type& width, Type& height);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700178
179struct Dim {
180 Dim () : x(0), y(0),
181 w(0), h(0),
182 o(0) {}
183 Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h) :
184 x(_x), y(_y),
185 w(_w), h(_h) {}
186 Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h, uint32_t _o) :
187 x(_x), y(_y),
188 w(_w), h(_h),
189 o(_o) {}
190 bool check(uint32_t _w, uint32_t _h) const {
191 return (x+w <= _w && y+h <= _h);
192
193 }
194
195 bool operator==(const Dim& d) const {
196 return d.x == x && d.y == y &&
197 d.w == w && d.h == h &&
198 d.o == o;
199 }
200
201 bool operator!=(const Dim& d) const {
202 return !operator==(d);
203 }
204
Naseer Ahmed29a26812012-06-14 00:56:20 -0700205 void dump() const;
206 uint32_t x;
207 uint32_t y;
208 uint32_t w;
209 uint32_t h;
210 uint32_t o;
211};
212
213// TODO have Whfz
214
215struct Whf {
216 Whf() : w(0), h(0), format(0), size(0) {}
217 Whf(uint32_t wi, uint32_t he, uint32_t f) :
218 w(wi), h(he), format(f), size(0) {}
219 Whf(uint32_t wi, uint32_t he, uint32_t f, uint32_t s) :
220 w(wi), h(he), format(f), size(s) {}
221 // FIXME not comparing size at the moment
222 bool operator==(const Whf& whf) const {
223 return whf.w == w && whf.h == h &&
224 whf.format == format;
225 }
226 bool operator!=(const Whf& whf) const {
227 return !operator==(whf);
228 }
229 void dump() const;
230 uint32_t w;
231 uint32_t h;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700232 uint32_t format;
233 uint32_t size;
234};
235
236enum { MAX_PATH_LEN = 256 };
237
Naseer Ahmed29a26812012-06-14 00:56:20 -0700238/**
239 * Rotator flags: not to be confused with orientation flags.
240 * Ususally, you want to open the rotator to make sure it is
241 * ready for business.
242 * ROT_FLAG_DISABLED: Rotator would not kick in. (ioctl will emit errors).
243 * ROT_FLAG_ENABLED: and when rotation is needed.
244 * (prim video playback)
245 * (UI mirroring on HDMI w/ 0 degree rotator. - just memcpy)
246 * In HDMI UI mirroring, rotator is always used.
247 * Even when w/o orienation change on primary,
248 * we do 0 rotation on HDMI and using rotator buffers.
249 * That is because we might see tearing otherwise. so
250 * we use another buffer (rotator).
251 * When a simple video playback on HDMI, no rotator is being used.(null r).
252 * */
253enum eRotFlags {
254 ROT_FLAG_DISABLED = 0,
255 ROT_FLAG_ENABLED = 1 // needed in rot
256};
257
Naseer Ahmed29a26812012-06-14 00:56:20 -0700258/* The values for is_fg flag for control alpha and transp
259 * IS_FG_OFF means is_fg = 0
260 * IS_FG_SET means is_fg = 1
261 */
262enum eIsFg {
263 IS_FG_OFF = 0,
264 IS_FG_SET = 1
265};
266
267/*
268 * Various mdp flags like PIPE SHARE, DEINTERLACE etc...
269 * kernel/common/linux/msm_mdp.h
270 * INTERLACE_MASK: hardware/qcom/display/libgralloc/badger/fb_priv.h
271 * */
272enum eMdpFlags {
273 OV_MDP_FLAGS_NONE = 0,
274 OV_MDP_PIPE_SHARE = MDP_OV_PIPE_SHARE,
275 OV_MDP_DEINTERLACE = MDP_DEINTERLACE,
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700276 OV_MDP_SECURE_OVERLAY_SESSION = MDP_SECURE_OVERLAY_SESSION,
277 OV_MDP_SOURCE_ROTATED_90 = MDP_SOURCE_ROTATED_90,
278 OV_MDP_MEMORY_ID_TYPE_FB = MDP_MEMORY_ID_TYPE_FB,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700279};
280
Naseer Ahmed29a26812012-06-14 00:56:20 -0700281enum eZorder {
282 ZORDER_0,
283 ZORDER_1,
284 ZORDER_2,
285 Z_SYSTEM_ALLOC = 0xFFFF
286};
287
288enum eMdpPipeType {
289 OV_MDP_PIPE_RGB,
290 OV_MDP_PIPE_VG
291};
292
Naseer Ahmed29a26812012-06-14 00:56:20 -0700293// Max pipes via overlay (VG0, VG1, RGB1)
294enum { MAX_PIPES = 3 };
295
296/* Used to identify destination channels and
297 * also 3D channels e.g. when in 3D mode with 2
298 * pipes opened and it is used in get crop/pos 3D
Naseer Ahmed29a26812012-06-14 00:56:20 -0700299 * */
300enum eDest {
301 OV_PIPE0 = 1 << 0,
302 OV_PIPE1 = 1 << 1,
303 OV_PIPE2 = 1 << 2,
304 OV_PIPE_ALL = (OV_PIPE0 | OV_PIPE1 | OV_PIPE2)
305};
306
307/* values for copybit_set_parameter(OVERLAY_TRANSFORM) */
308enum eTransform {
309 /* No rot */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700310 OVERLAY_TRANSFORM_0 = 0x0,
311 /* flip source image horizontally 0x1 */
312 OVERLAY_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H,
313 /* flip source image vertically 0x2 */
314 OVERLAY_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700315 /* rotate source image 180 degrees
316 * It is basically bit-or-ed H | V == 0x3 */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700317 OVERLAY_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
318 /* rotate source image 90 degrees 0x4 */
319 OVERLAY_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
320 /* rotate source image 90 degrees and flip horizontally 0x5 */
321 OVERLAY_TRANSFORM_ROT_90_FLIP_H = HAL_TRANSFORM_ROT_90 |
322 HAL_TRANSFORM_FLIP_H,
323 /* rotate source image 90 degrees and flip vertically 0x6 */
324 OVERLAY_TRANSFORM_ROT_90_FLIP_V = HAL_TRANSFORM_ROT_90 |
325 HAL_TRANSFORM_FLIP_V,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700326 /* rotate source image 270 degrees
327 * Basically 180 | 90 == 0x7 */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700328 OVERLAY_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700329 /* rotate invalid like in Transform.h */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700330 OVERLAY_TRANSFORM_INV = 0x80
Naseer Ahmed29a26812012-06-14 00:56:20 -0700331};
332
333// Used to consolidate pipe params
334struct PipeArgs {
335 PipeArgs() : mdpFlags(OV_MDP_FLAGS_NONE),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700336 zorder(Z_SYSTEM_ALLOC),
337 isFg(IS_FG_OFF),
338 rotFlags(ROT_FLAG_DISABLED){
339 }
340
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700341 PipeArgs(eMdpFlags f, Whf _whf,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700342 eZorder z, eIsFg fg, eRotFlags r) :
343 mdpFlags(f),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700344 whf(_whf),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700345 zorder(z),
346 isFg(fg),
347 rotFlags(r) {
348 }
349
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700350 eMdpFlags mdpFlags; // for mdp_overlay flags
Naseer Ahmed29a26812012-06-14 00:56:20 -0700351 Whf whf;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700352 eZorder zorder; // stage number
353 eIsFg isFg; // control alpha & transp
354 eRotFlags rotFlags;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700355};
356
357enum eOverlayState{
358 /* No pipes from overlay open */
359 OV_CLOSED = 0,
360
361 /* 2D Video */
362 OV_2D_VIDEO_ON_PANEL,
363 OV_2D_VIDEO_ON_PANEL_TV,
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700364 OV_2D_VIDEO_ON_TV,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700365
366 /* 3D Video on one display (panel or TV) */
367 OV_3D_VIDEO_ON_2D_PANEL,
368 OV_3D_VIDEO_ON_3D_PANEL,
369 OV_3D_VIDEO_ON_3D_TV,
370
371 /* 3D Video on two displays (panel and TV) */
372 OV_3D_VIDEO_ON_2D_PANEL_2D_TV,
373
374 /* UI Mirroring */
375 OV_UI_MIRROR,
376 OV_2D_TRUE_UI_MIRROR,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700377
378 /* Composition Bypass */
379 OV_BYPASS_1_LAYER,
380 OV_BYPASS_2_LAYER,
381 OV_BYPASS_3_LAYER,
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700382
383 /* External only for dual-disp */
384 OV_DUAL_DISP,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700385};
386
387inline void setMdpFlags(eMdpFlags& f, eMdpFlags v) {
388 f = static_cast<eMdpFlags>(setBit(f, v));
389}
390
391inline void clearMdpFlags(eMdpFlags& f, eMdpFlags v) {
392 f = static_cast<eMdpFlags>(clrBit(f, v));
393}
394
395// fb 0/1/2
396enum { FB0, FB1, FB2 };
397
398//Panels could be categorized as primary and external
399enum { PRIMARY, EXTERNAL };
400
401//External Panels could use HDMI or WFD
402enum {
403 HDMI = 1,
404 WFD = 2
405};
406
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700407//TODO Make this a part of some appropriate class
Naseer Ahmed29a26812012-06-14 00:56:20 -0700408static int sExtType = HDMI; //HDMI or WFD
Naseer Ahmed29a26812012-06-14 00:56:20 -0700409//Set by client as HDMI/WFD
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700410void setExtType(const int& type);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700411//Return External panel type set by client.
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700412int getExtType();
413
Naseer Ahmed29a26812012-06-14 00:56:20 -0700414
415//Gets the FB number for the external type.
416//As of now, HDMI always has fb1, WFD could use fb1 or fb2
417//Assumes Ext type set by setExtType() from client.
418static int getFBForPanel(int panel) { // PRIMARY OR EXTERNAL
419 switch(panel) {
420 case PRIMARY: return FB0;
421 break;
422 case EXTERNAL:
423 switch(getExtType()) {
424 case HDMI: return FB1;
425 break;
426 case WFD: return FB2;//Hardcoding fb2 for wfd. Will change.
427 break;
428 }
429 break;
430 default:
431 ALOGE("%s: Unrecognized PANEL category %d", __func__, panel);
432 break;
433 }
434 return -1;
435}
436
437// number of rgb pipes bufs (max)
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700438
Naseer Ahmed29a26812012-06-14 00:56:20 -0700439// 2 for rgb0/1 double bufs
440enum { RGB_PIPE_NUM_BUFS = 2 };
441
442struct ScreenInfo {
443 ScreenInfo() : mFBWidth(0),
444 mFBHeight(0),
445 mFBbpp(0),
446 mFBystride(0) {}
447 void dump(const char* const s) const;
448 uint32_t mFBWidth;
449 uint32_t mFBHeight;
450 uint32_t mFBbpp;
451 uint32_t mFBystride;
452};
453
454int getMdpFormat(int format);
455int getRotOutFmt(uint32_t format);
456/* flip is upside down and such. V, H flip
457 * rotation is 90, 180 etc
458 * It returns MDP related enum/define that match rot+flip*/
459int getMdpOrient(eTransform rotation);
Saurabh Shah9c876d92012-08-25 19:29:53 -0700460const char* getFormatString(int format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700461const char* getStateString(eOverlayState state);
462
Naseer Ahmed29a26812012-06-14 00:56:20 -0700463// Cannot use HW_OVERLAY_MAGNIFICATION_LIMIT, since at the time
464// of integration, HW_OVERLAY_MAGNIFICATION_LIMIT was a define
465enum { HW_OV_MAGNIFICATION_LIMIT = 20,
466 HW_OV_MINIFICATION_LIMIT = 8
467};
468
Naseer Ahmed29a26812012-06-14 00:56:20 -0700469template <class T>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700470inline void memset0(T& t) { ::memset(&t, 0, sizeof(T)); }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700471
472template <class T> inline void swap ( T& a, T& b )
473{
474 T c(a); a=b; b=c;
475}
476
477inline int alignup(int value, int a) {
478 //if align = 0, return the value. Else, do alignment.
479 return a ? ((((value - 1) / a) + 1) * a) : value;
480}
481
482// FIXME that align should replace the upper one.
483inline int align(int value, int a) {
484 //if align = 0, return the value. Else, do alignment.
485 return a ? ((value + (a-1)) & ~(a-1)) : value;
486}
487
Naseer Ahmed29a26812012-06-14 00:56:20 -0700488enum eRotOutFmt {
489 ROT_OUT_FMT_DEFAULT,
490 ROT_OUT_FMT_Y_CRCB_H2V2
491};
492
493template <int ROT_OUT_FMT> struct RotOutFmt;
494
495// FIXME, taken from gralloc_priv.h. Need to
496// put it back as soon as overlay takes place of the old one
497/* possible formats for 3D content*/
498enum {
499 HAL_NO_3D = 0x0000,
500 HAL_3D_IN_SIDE_BY_SIDE_L_R = 0x10000,
501 HAL_3D_IN_TOP_BOTTOM = 0x20000,
502 HAL_3D_IN_INTERLEAVE = 0x40000,
503 HAL_3D_IN_SIDE_BY_SIDE_R_L = 0x80000,
504 HAL_3D_OUT_SIDE_BY_SIDE = 0x1000,
505 HAL_3D_OUT_TOP_BOTTOM = 0x2000,
506 HAL_3D_OUT_INTERLEAVE = 0x4000,
507 HAL_3D_OUT_MONOSCOPIC = 0x8000
508};
509
510enum { HAL_3D_OUT_SBS_MASK =
511 HAL_3D_OUT_SIDE_BY_SIDE >> overlay::utils::SHIFT_OUT_3D,
512 HAL_3D_OUT_TOP_BOT_MASK =
513 HAL_3D_OUT_TOP_BOTTOM >> overlay::utils::SHIFT_OUT_3D,
514 HAL_3D_OUT_INTERL_MASK =
515 HAL_3D_OUT_INTERLEAVE >> overlay::utils::SHIFT_OUT_3D,
516 HAL_3D_OUT_MONOS_MASK =
517 HAL_3D_OUT_MONOSCOPIC >> overlay::utils::SHIFT_OUT_3D
518};
519
520
521inline bool isYuv(uint32_t format) {
522 switch(format){
523 case MDP_Y_CBCR_H2V1:
524 case MDP_Y_CBCR_H2V2:
525 case MDP_Y_CRCB_H2V2:
Saurabh Shahb121e142012-08-20 17:59:13 -0700526 case MDP_Y_CRCB_H1V1:
527 case MDP_Y_CRCB_H2V1:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700528 case MDP_Y_CRCB_H2V2_TILE:
529 case MDP_Y_CBCR_H2V2_TILE:
Saurabh Shahb121e142012-08-20 17:59:13 -0700530 case MDP_Y_CR_CB_H2V2:
Saurabh Shahae1044e2012-08-21 16:03:32 -0700531 case MDP_Y_CR_CB_GH2V2:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700532 return true;
533 default:
534 return false;
535 }
536 return false;
537}
538
539inline bool isRgb(uint32_t format) {
540 switch(format) {
541 case MDP_RGBA_8888:
542 case MDP_BGRA_8888:
543 case MDP_RGBX_8888:
544 case MDP_RGB_565:
545 return true;
546 default:
547 return false;
548 }
549 return false;
550}
551
552inline bool isValidDest(eDest dest)
553{
554 if ((OV_PIPE0 & dest) ||
555 (OV_PIPE1 & dest) ||
556 (OV_PIPE2 & dest)) {
557 return true;
558 }
559 return false;
560}
561
Saurabh Shah9c876d92012-08-25 19:29:53 -0700562inline const char* getFormatString(int format){
Naseer Ahmed29a26812012-06-14 00:56:20 -0700563 static const char* const formats[] = {
564 "MDP_RGB_565",
565 "MDP_XRGB_8888",
566 "MDP_Y_CBCR_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700567 "MDP_Y_CBCR_H2V2_ADRENO",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700568 "MDP_ARGB_8888",
569 "MDP_RGB_888",
570 "MDP_Y_CRCB_H2V2",
571 "MDP_YCRYCB_H2V1",
572 "MDP_Y_CRCB_H2V1",
573 "MDP_Y_CBCR_H2V1",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700574 "MDP_Y_CRCB_H1V2",
575 "MDP_Y_CBCR_H1V2",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700576 "MDP_RGBA_8888",
577 "MDP_BGRA_8888",
578 "MDP_RGBX_8888",
579 "MDP_Y_CRCB_H2V2_TILE",
580 "MDP_Y_CBCR_H2V2_TILE",
581 "MDP_Y_CR_CB_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700582 "MDP_Y_CR_CB_GH2V2",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700583 "MDP_Y_CB_CR_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700584 "MDP_Y_CRCB_H1V1",
585 "MDP_Y_CBCR_H1V1",
586 "MDP_YCRCB_H1V1",
587 "MDP_YCBCR_H1V1",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700588 "MDP_BGR_565",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700589 "MDP_IMGTYPE_LIMIT",
590 "MDP_RGB_BORDERFILL",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700591 "MDP_FB_FORMAT",
592 "MDP_IMGTYPE_LIMIT2"
593 };
Saurabh Shah9c876d92012-08-25 19:29:53 -0700594 if(format < 0 || format >= (int)(sizeof(formats) / sizeof(formats[0]))) {
595 ALOGE("%s wrong fmt %d", __FUNCTION__, format);
596 return "Unsupported format";
597 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700598 return formats[format];
599}
600
601inline const char* getStateString(eOverlayState state){
602 switch (state) {
603 case OV_CLOSED:
604 return "OV_CLOSED";
605 case OV_2D_VIDEO_ON_PANEL:
606 return "OV_2D_VIDEO_ON_PANEL";
607 case OV_2D_VIDEO_ON_PANEL_TV:
608 return "OV_2D_VIDEO_ON_PANEL_TV";
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700609 case OV_2D_VIDEO_ON_TV:
610 return "OV_2D_VIDEO_ON_TV";
Naseer Ahmed29a26812012-06-14 00:56:20 -0700611 case OV_3D_VIDEO_ON_2D_PANEL:
612 return "OV_3D_VIDEO_ON_2D_PANEL";
613 case OV_3D_VIDEO_ON_3D_PANEL:
614 return "OV_3D_VIDEO_ON_3D_PANEL";
615 case OV_3D_VIDEO_ON_3D_TV:
616 return "OV_3D_VIDEO_ON_3D_TV";
617 case OV_3D_VIDEO_ON_2D_PANEL_2D_TV:
618 return "OV_3D_VIDEO_ON_2D_PANEL_2D_TV";
619 case OV_UI_MIRROR:
620 return "OV_UI_MIRROR";
621 case OV_2D_TRUE_UI_MIRROR:
622 return "OV_2D_TRUE_UI_MIRROR";
623 case OV_BYPASS_1_LAYER:
624 return "OV_BYPASS_1_LAYER";
625 case OV_BYPASS_2_LAYER:
626 return "OV_BYPASS_2_LAYER";
627 case OV_BYPASS_3_LAYER:
628 return "OV_BYPASS_3_LAYER";
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700629 case OV_DUAL_DISP:
630 return "OV_DUAL_DISP";
Naseer Ahmed29a26812012-06-14 00:56:20 -0700631 default:
632 return "UNKNOWN_STATE";
633 }
634 return "BAD_STATE";
635}
636
Naseer Ahmed29a26812012-06-14 00:56:20 -0700637inline void Whf::dump() const {
638 ALOGE("== Dump WHF w=%d h=%d f=%d s=%d start/end ==",
639 w, h, format, size);
640}
641
642inline void Dim::dump() const {
643 ALOGE("== Dump Dim x=%d y=%d w=%d h=%d start/end ==", x, y, w, h);
644}
645
646inline int getMdpOrient(eTransform rotation) {
647 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, rotation);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700648 switch(rotation)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700649 {
650 case OVERLAY_TRANSFORM_0 : return 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700651 case OVERLAY_TRANSFORM_FLIP_V: return MDP_FLIP_UD;
652 case OVERLAY_TRANSFORM_FLIP_H: return MDP_FLIP_LR;
653 case OVERLAY_TRANSFORM_ROT_90: return MDP_ROT_90;
Saurabh Shaha73738d2012-08-09 18:15:18 -0700654 //getMdpOrient will switch the flips if the source is 90 rotated.
655 //Clients in Android dont factor in 90 rotation while deciding flip.
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700656 case OVERLAY_TRANSFORM_ROT_90_FLIP_V:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700657 return MDP_ROT_90 | MDP_FLIP_LR;
Saurabh Shaha73738d2012-08-09 18:15:18 -0700658 case OVERLAY_TRANSFORM_ROT_90_FLIP_H:
659 return MDP_ROT_90 | MDP_FLIP_UD;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700660 case OVERLAY_TRANSFORM_ROT_180: return MDP_ROT_180;
661 case OVERLAY_TRANSFORM_ROT_270: return MDP_ROT_270;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700662 default:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700663 ALOGE("%s: invalid rotation value (value = 0x%x",
664 __FUNCTION__, rotation);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700665 }
666 return -1;
667}
668
669inline int getRotOutFmt(uint32_t format) {
670 switch (format) {
671 case MDP_Y_CRCB_H2V2_TILE:
672 return MDP_Y_CRCB_H2V2;
673 case MDP_Y_CBCR_H2V2_TILE:
674 return MDP_Y_CBCR_H2V2;
675 case MDP_Y_CB_CR_H2V2:
676 return MDP_Y_CBCR_H2V2;
Saurabh Shahae1044e2012-08-21 16:03:32 -0700677 case MDP_Y_CR_CB_GH2V2:
678 return MDP_Y_CRCB_H2V2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700679 default:
680 return format;
681 }
682 // not reached
683 OVASSERT(false, "%s not reached", __FUNCTION__);
684 return -1;
685}
686
Naseer Ahmed29a26812012-06-14 00:56:20 -0700687
688inline uint32_t getColorFormat(uint32_t format)
689{
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700690 return (format == HAL_PIXEL_FORMAT_YV12) ?
691 format : colorFormat(format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700692}
693
694// FB0
695template <int CHAN>
696inline Dim getPositionS3DImpl(const Whf& whf)
697{
698 switch (whf.format & OUTPUT_3D_MASK)
699 {
700 case HAL_3D_OUT_SBS_MASK:
701 // x, y, w, h
702 return Dim(0, 0, whf.w/2, whf.h);
703 case HAL_3D_OUT_TOP_BOT_MASK:
704 return Dim(0, 0, whf.w, whf.h/2);
705 case HAL_3D_OUT_MONOS_MASK:
706 return Dim();
707 case HAL_3D_OUT_INTERL_MASK:
708 // FIXME error?
709 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
710 return Dim();
711 default:
712 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
713 whf.format);
714 }
715 return Dim();
716}
717
718template <>
719inline Dim getPositionS3DImpl<utils::OV_PIPE1>(const Whf& whf)
720{
721 switch (whf.format & OUTPUT_3D_MASK)
722 {
723 case HAL_3D_OUT_SBS_MASK:
724 return Dim(whf.w/2, 0, whf.w/2, whf.h);
725 case HAL_3D_OUT_TOP_BOT_MASK:
726 return Dim(0, whf.h/2, whf.w, whf.h/2);
727 case HAL_3D_OUT_MONOS_MASK:
728 return Dim(0, 0, whf.w, whf.h);
729 case HAL_3D_OUT_INTERL_MASK:
730 // FIXME error?
731 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
732 return Dim();
733 default:
734 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
735 whf.format);
736 }
737 return Dim();
738}
739
740template <int CHAN>
741inline bool getPositionS3D(const Whf& whf, Dim& out) {
742 out = getPositionS3DImpl<CHAN>(whf);
743 return (out != Dim());
744}
745
746template <int CHAN>
747inline Dim getCropS3DImpl(const Dim& in, uint32_t fmt) {
748 switch (fmt & INPUT_3D_MASK)
749 {
750 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
751 return Dim(0, 0, in.w/2, in.h);
752 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
753 return Dim(in.w/2, 0, in.w/2, in.h);
754 case HAL_3D_IN_TOP_BOTTOM:
755 return Dim(0, 0, in.w, in.h/2);
756 case HAL_3D_IN_INTERLEAVE:
757 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
758 break;
759 default:
760 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
761 break;
762 }
763 return Dim();
764}
765
766template <>
767inline Dim getCropS3DImpl<utils::OV_PIPE1>(const Dim& in, uint32_t fmt) {
768 switch (fmt & INPUT_3D_MASK)
769 {
770 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
771 return Dim(in.w/2, 0, in.w/2, in.h);
772 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
773 return Dim(0, 0, in.w/2, in.h);
774 case HAL_3D_IN_TOP_BOTTOM:
775 return Dim(0, in.h/2, in.w, in.h/2);
776 case HAL_3D_IN_INTERLEAVE:
777 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
778 break;
779 default:
780 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
781 break;
782 }
783 return Dim();
784}
785
786template <int CHAN>
787inline bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt)
788{
789 out = getCropS3DImpl<CHAN>(in, fmt);
790 return (out != Dim());
791}
792
793template <class Type>
794void swapWidthHeight(Type& width, Type& height) {
795 Type tmp = width;
796 width = height;
797 height = tmp;
798}
799
800inline void ScreenInfo::dump(const char* const s) const {
801 ALOGE("== Dump %s ScreenInfo w=%d h=%d"
802 " bpp=%d stride=%d start/end ==",
803 s, mFBWidth, mFBHeight, mFBbpp, mFBystride);
804}
805
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700806inline bool openDev(OvFD& fd, int fbnum,
807 const char* const devpath, int flags) {
808 return overlay::open(fd, fbnum, devpath, flags);
809}
810
Saurabh Shahb121e142012-08-20 17:59:13 -0700811template <class T>
812inline void even_ceil(T& value) {
813 if(value & 1)
814 value++;
815}
816
817template <class T>
818inline void even_floor(T& value) {
819 if(value & 1)
820 value--;
821}
822
Naseer Ahmed29a26812012-06-14 00:56:20 -0700823} // namespace utils ends
824
825//--------------------Class Res stuff (namespace overlay only) -----------
826
827class Res {
828public:
829 // /dev/graphics/fb%u
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700830 static const char* const fbPath;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700831 // /dev/msm_rotator
832 static const char* const rotPath;
833 // /sys/class/graphics/fb1/format_3d
834 static const char* const format3DFile;
835 // /sys/class/graphics/fb1/3d_present
836 static const char* const edid3dInfoFile;
837 // /sys/devices/platform/mipi_novatek.0/enable_3d_barrier
838 static const char* const barrierFile;
839};
840
841
842//--------------------Class OvFD stuff (namespace overlay only) -----------
843
Naseer Ahmed29a26812012-06-14 00:56:20 -0700844/*
845* Holds one FD
846* Dtor will NOT close the underlying FD.
847* That enables us to copy that object around
848* */
849class OvFD {
850public:
851 /* Ctor */
852 explicit OvFD();
853
854 /* dtor will NOT close the underlying FD */
855 ~OvFD();
856
857 /* Open fd using the path given by dev.
858 * return false in failure */
859 bool open(const char* const dev,
860 int flags = O_RDWR);
861
862 /* populate path */
863 void setPath(const char* const dev);
864
865 /* Close fd if we have a valid fd. */
866 bool close();
867
868 /* returns underlying fd.*/
869 int getFD() const;
870
871 /* returns true if fd is valid */
872 bool valid() const;
873
874 /* like operator= */
875 void copy(int fd);
876
877 /* dump the state of the instance */
878 void dump() const;
879private:
880 /* helper enum for determine valid/invalid fd */
881 enum { INVAL = -1 };
882
883 /* actual os fd */
884 int mFD;
885
886 /* path, for debugging */
887 char mPath[utils::MAX_PATH_LEN];
888};
889
890//-------------------Inlines--------------------------
891
892inline bool open(OvFD& fd, uint32_t fbnum, const char* const dev, int flags)
893{
894 char dev_name[64] = {0};
895 snprintf(dev_name, sizeof(dev_name), dev, fbnum);
896 return fd.open(dev_name, flags);
897}
898
899inline OvFD::OvFD() : mFD (INVAL) {
900 mPath[0] = 0;
901}
902
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700903inline OvFD::~OvFD() {
904 //no op since copy() can be used to share fd, in 3d cases.
905}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700906
907inline bool OvFD::open(const char* const dev, int flags)
908{
909 mFD = ::open(dev, flags, 0);
910 if (mFD < 0) {
911 // FIXME errno, strerror in bionic?
912 ALOGE("Cant open device %s err=%d", dev, errno);
913 return false;
914 }
915 setPath(dev);
916 return true;
917}
918
919inline void OvFD::setPath(const char* const dev)
920{
921 ::strncpy(mPath, dev, utils::MAX_PATH_LEN);
922}
923
924inline bool OvFD::close()
925{
926 int ret = 0;
927 if(valid()) {
928 ret = ::close(mFD);
929 mFD = INVAL;
930 }
931 return (ret == 0);
932}
933
934inline bool OvFD::valid() const
935{
936 return (mFD != INVAL);
937}
938
939inline int OvFD::getFD() const { return mFD; }
940
941inline void OvFD::copy(int fd) {
942 mFD = fd;
943}
944
945inline void OvFD::dump() const
946{
947 ALOGE("== Dump OvFD fd=%d path=%s start/end ==",
948 mFD, mPath);
949}
950
951//--------------- class OvFD stuff ends ---------------------
952
953} // overlay
954
955
956#endif // OVERLAY_UTILS_H