blob: eebf5accd5663a61207e9fd36a2527bd3efb58fc [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);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700460const char* getFormatString(uint32_t format);
461const 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:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700531 return true;
532 default:
533 return false;
534 }
535 return false;
536}
537
538inline bool isRgb(uint32_t format) {
539 switch(format) {
540 case MDP_RGBA_8888:
541 case MDP_BGRA_8888:
542 case MDP_RGBX_8888:
543 case MDP_RGB_565:
544 return true;
545 default:
546 return false;
547 }
548 return false;
549}
550
551inline bool isValidDest(eDest dest)
552{
553 if ((OV_PIPE0 & dest) ||
554 (OV_PIPE1 & dest) ||
555 (OV_PIPE2 & dest)) {
556 return true;
557 }
558 return false;
559}
560
561inline const char* getFormatString(uint32_t format){
562 static const char* const formats[] = {
563 "MDP_RGB_565",
564 "MDP_XRGB_8888",
565 "MDP_Y_CBCR_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700566 "MDP_Y_CBCR_H2V2_ADRENO",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700567 "MDP_ARGB_8888",
568 "MDP_RGB_888",
569 "MDP_Y_CRCB_H2V2",
570 "MDP_YCRYCB_H2V1",
571 "MDP_Y_CRCB_H2V1",
572 "MDP_Y_CBCR_H2V1",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700573 "MDP_Y_CRCB_H1V2",
574 "MDP_Y_CBCR_H1V2",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700575 "MDP_RGBA_8888",
576 "MDP_BGRA_8888",
577 "MDP_RGBX_8888",
578 "MDP_Y_CRCB_H2V2_TILE",
579 "MDP_Y_CBCR_H2V2_TILE",
580 "MDP_Y_CR_CB_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700581 "MDP_Y_CR_CB_GH2V2",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700582 "MDP_Y_CB_CR_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700583 "MDP_Y_CRCB_H1V1",
584 "MDP_Y_CBCR_H1V1",
585 "MDP_YCRCB_H1V1",
586 "MDP_YCBCR_H1V1",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700587 "MDP_BGR_565",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700588 "MDP_IMGTYPE_LIMIT",
589 "MDP_RGB_BORDERFILL",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700590 "MDP_FB_FORMAT",
591 "MDP_IMGTYPE_LIMIT2"
592 };
593 OVASSERT(format < sizeof(formats) / sizeof(formats[0]),
594 "getFormatString wrong fmt %d", format);
595 return formats[format];
596}
597
598inline const char* getStateString(eOverlayState state){
599 switch (state) {
600 case OV_CLOSED:
601 return "OV_CLOSED";
602 case OV_2D_VIDEO_ON_PANEL:
603 return "OV_2D_VIDEO_ON_PANEL";
604 case OV_2D_VIDEO_ON_PANEL_TV:
605 return "OV_2D_VIDEO_ON_PANEL_TV";
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700606 case OV_2D_VIDEO_ON_TV:
607 return "OV_2D_VIDEO_ON_TV";
Naseer Ahmed29a26812012-06-14 00:56:20 -0700608 case OV_3D_VIDEO_ON_2D_PANEL:
609 return "OV_3D_VIDEO_ON_2D_PANEL";
610 case OV_3D_VIDEO_ON_3D_PANEL:
611 return "OV_3D_VIDEO_ON_3D_PANEL";
612 case OV_3D_VIDEO_ON_3D_TV:
613 return "OV_3D_VIDEO_ON_3D_TV";
614 case OV_3D_VIDEO_ON_2D_PANEL_2D_TV:
615 return "OV_3D_VIDEO_ON_2D_PANEL_2D_TV";
616 case OV_UI_MIRROR:
617 return "OV_UI_MIRROR";
618 case OV_2D_TRUE_UI_MIRROR:
619 return "OV_2D_TRUE_UI_MIRROR";
620 case OV_BYPASS_1_LAYER:
621 return "OV_BYPASS_1_LAYER";
622 case OV_BYPASS_2_LAYER:
623 return "OV_BYPASS_2_LAYER";
624 case OV_BYPASS_3_LAYER:
625 return "OV_BYPASS_3_LAYER";
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700626 case OV_DUAL_DISP:
627 return "OV_DUAL_DISP";
Naseer Ahmed29a26812012-06-14 00:56:20 -0700628 default:
629 return "UNKNOWN_STATE";
630 }
631 return "BAD_STATE";
632}
633
Naseer Ahmed29a26812012-06-14 00:56:20 -0700634inline void Whf::dump() const {
635 ALOGE("== Dump WHF w=%d h=%d f=%d s=%d start/end ==",
636 w, h, format, size);
637}
638
639inline void Dim::dump() const {
640 ALOGE("== Dump Dim x=%d y=%d w=%d h=%d start/end ==", x, y, w, h);
641}
642
643inline int getMdpOrient(eTransform rotation) {
644 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, rotation);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700645 switch(rotation)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700646 {
647 case OVERLAY_TRANSFORM_0 : return 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700648 case OVERLAY_TRANSFORM_FLIP_V: return MDP_FLIP_UD;
649 case OVERLAY_TRANSFORM_FLIP_H: return MDP_FLIP_LR;
650 case OVERLAY_TRANSFORM_ROT_90: return MDP_ROT_90;
Saurabh Shaha73738d2012-08-09 18:15:18 -0700651 //getMdpOrient will switch the flips if the source is 90 rotated.
652 //Clients in Android dont factor in 90 rotation while deciding flip.
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700653 case OVERLAY_TRANSFORM_ROT_90_FLIP_V:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700654 return MDP_ROT_90 | MDP_FLIP_LR;
Saurabh Shaha73738d2012-08-09 18:15:18 -0700655 case OVERLAY_TRANSFORM_ROT_90_FLIP_H:
656 return MDP_ROT_90 | MDP_FLIP_UD;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700657 case OVERLAY_TRANSFORM_ROT_180: return MDP_ROT_180;
658 case OVERLAY_TRANSFORM_ROT_270: return MDP_ROT_270;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700659 default:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700660 ALOGE("%s: invalid rotation value (value = 0x%x",
661 __FUNCTION__, rotation);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700662 }
663 return -1;
664}
665
666inline int getRotOutFmt(uint32_t format) {
667 switch (format) {
668 case MDP_Y_CRCB_H2V2_TILE:
669 return MDP_Y_CRCB_H2V2;
670 case MDP_Y_CBCR_H2V2_TILE:
671 return MDP_Y_CBCR_H2V2;
672 case MDP_Y_CB_CR_H2V2:
673 return MDP_Y_CBCR_H2V2;
674 default:
675 return format;
676 }
677 // not reached
678 OVASSERT(false, "%s not reached", __FUNCTION__);
679 return -1;
680}
681
Naseer Ahmed29a26812012-06-14 00:56:20 -0700682
683inline uint32_t getColorFormat(uint32_t format)
684{
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700685 return (format == HAL_PIXEL_FORMAT_YV12) ?
686 format : colorFormat(format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700687}
688
689// FB0
690template <int CHAN>
691inline Dim getPositionS3DImpl(const Whf& whf)
692{
693 switch (whf.format & OUTPUT_3D_MASK)
694 {
695 case HAL_3D_OUT_SBS_MASK:
696 // x, y, w, h
697 return Dim(0, 0, whf.w/2, whf.h);
698 case HAL_3D_OUT_TOP_BOT_MASK:
699 return Dim(0, 0, whf.w, whf.h/2);
700 case HAL_3D_OUT_MONOS_MASK:
701 return Dim();
702 case HAL_3D_OUT_INTERL_MASK:
703 // FIXME error?
704 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
705 return Dim();
706 default:
707 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
708 whf.format);
709 }
710 return Dim();
711}
712
713template <>
714inline Dim getPositionS3DImpl<utils::OV_PIPE1>(const Whf& whf)
715{
716 switch (whf.format & OUTPUT_3D_MASK)
717 {
718 case HAL_3D_OUT_SBS_MASK:
719 return Dim(whf.w/2, 0, whf.w/2, whf.h);
720 case HAL_3D_OUT_TOP_BOT_MASK:
721 return Dim(0, whf.h/2, whf.w, whf.h/2);
722 case HAL_3D_OUT_MONOS_MASK:
723 return Dim(0, 0, whf.w, whf.h);
724 case HAL_3D_OUT_INTERL_MASK:
725 // FIXME error?
726 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
727 return Dim();
728 default:
729 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
730 whf.format);
731 }
732 return Dim();
733}
734
735template <int CHAN>
736inline bool getPositionS3D(const Whf& whf, Dim& out) {
737 out = getPositionS3DImpl<CHAN>(whf);
738 return (out != Dim());
739}
740
741template <int CHAN>
742inline Dim getCropS3DImpl(const Dim& in, uint32_t fmt) {
743 switch (fmt & INPUT_3D_MASK)
744 {
745 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
746 return Dim(0, 0, in.w/2, in.h);
747 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
748 return Dim(in.w/2, 0, in.w/2, in.h);
749 case HAL_3D_IN_TOP_BOTTOM:
750 return Dim(0, 0, in.w, in.h/2);
751 case HAL_3D_IN_INTERLEAVE:
752 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
753 break;
754 default:
755 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
756 break;
757 }
758 return Dim();
759}
760
761template <>
762inline Dim getCropS3DImpl<utils::OV_PIPE1>(const Dim& in, uint32_t fmt) {
763 switch (fmt & INPUT_3D_MASK)
764 {
765 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
766 return Dim(in.w/2, 0, in.w/2, in.h);
767 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
768 return Dim(0, 0, in.w/2, in.h);
769 case HAL_3D_IN_TOP_BOTTOM:
770 return Dim(0, in.h/2, in.w, in.h/2);
771 case HAL_3D_IN_INTERLEAVE:
772 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
773 break;
774 default:
775 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
776 break;
777 }
778 return Dim();
779}
780
781template <int CHAN>
782inline bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt)
783{
784 out = getCropS3DImpl<CHAN>(in, fmt);
785 return (out != Dim());
786}
787
788template <class Type>
789void swapWidthHeight(Type& width, Type& height) {
790 Type tmp = width;
791 width = height;
792 height = tmp;
793}
794
795inline void ScreenInfo::dump(const char* const s) const {
796 ALOGE("== Dump %s ScreenInfo w=%d h=%d"
797 " bpp=%d stride=%d start/end ==",
798 s, mFBWidth, mFBHeight, mFBbpp, mFBystride);
799}
800
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700801inline bool openDev(OvFD& fd, int fbnum,
802 const char* const devpath, int flags) {
803 return overlay::open(fd, fbnum, devpath, flags);
804}
805
Saurabh Shahb121e142012-08-20 17:59:13 -0700806template <class T>
807inline void even_ceil(T& value) {
808 if(value & 1)
809 value++;
810}
811
812template <class T>
813inline void even_floor(T& value) {
814 if(value & 1)
815 value--;
816}
817
Naseer Ahmed29a26812012-06-14 00:56:20 -0700818} // namespace utils ends
819
820//--------------------Class Res stuff (namespace overlay only) -----------
821
822class Res {
823public:
824 // /dev/graphics/fb%u
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700825 static const char* const fbPath;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700826 // /dev/msm_rotator
827 static const char* const rotPath;
828 // /sys/class/graphics/fb1/format_3d
829 static const char* const format3DFile;
830 // /sys/class/graphics/fb1/3d_present
831 static const char* const edid3dInfoFile;
832 // /sys/devices/platform/mipi_novatek.0/enable_3d_barrier
833 static const char* const barrierFile;
834};
835
836
837//--------------------Class OvFD stuff (namespace overlay only) -----------
838
Naseer Ahmed29a26812012-06-14 00:56:20 -0700839/*
840* Holds one FD
841* Dtor will NOT close the underlying FD.
842* That enables us to copy that object around
843* */
844class OvFD {
845public:
846 /* Ctor */
847 explicit OvFD();
848
849 /* dtor will NOT close the underlying FD */
850 ~OvFD();
851
852 /* Open fd using the path given by dev.
853 * return false in failure */
854 bool open(const char* const dev,
855 int flags = O_RDWR);
856
857 /* populate path */
858 void setPath(const char* const dev);
859
860 /* Close fd if we have a valid fd. */
861 bool close();
862
863 /* returns underlying fd.*/
864 int getFD() const;
865
866 /* returns true if fd is valid */
867 bool valid() const;
868
869 /* like operator= */
870 void copy(int fd);
871
872 /* dump the state of the instance */
873 void dump() const;
874private:
875 /* helper enum for determine valid/invalid fd */
876 enum { INVAL = -1 };
877
878 /* actual os fd */
879 int mFD;
880
881 /* path, for debugging */
882 char mPath[utils::MAX_PATH_LEN];
883};
884
885//-------------------Inlines--------------------------
886
887inline bool open(OvFD& fd, uint32_t fbnum, const char* const dev, int flags)
888{
889 char dev_name[64] = {0};
890 snprintf(dev_name, sizeof(dev_name), dev, fbnum);
891 return fd.open(dev_name, flags);
892}
893
894inline OvFD::OvFD() : mFD (INVAL) {
895 mPath[0] = 0;
896}
897
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700898inline OvFD::~OvFD() {
899 //no op since copy() can be used to share fd, in 3d cases.
900}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700901
902inline bool OvFD::open(const char* const dev, int flags)
903{
904 mFD = ::open(dev, flags, 0);
905 if (mFD < 0) {
906 // FIXME errno, strerror in bionic?
907 ALOGE("Cant open device %s err=%d", dev, errno);
908 return false;
909 }
910 setPath(dev);
911 return true;
912}
913
914inline void OvFD::setPath(const char* const dev)
915{
916 ::strncpy(mPath, dev, utils::MAX_PATH_LEN);
917}
918
919inline bool OvFD::close()
920{
921 int ret = 0;
922 if(valid()) {
923 ret = ::close(mFD);
924 mFD = INVAL;
925 }
926 return (ret == 0);
927}
928
929inline bool OvFD::valid() const
930{
931 return (mFD != INVAL);
932}
933
934inline int OvFD::getFD() const { return mFD; }
935
936inline void OvFD::copy(int fd) {
937 mFD = fd;
938}
939
940inline void OvFD::dump() const
941{
942 ALOGE("== Dump OvFD fd=%d path=%s start/end ==",
943 mFD, mPath);
944}
945
946//--------------- class OvFD stuff ends ---------------------
947
948} // overlay
949
950
951#endif // OVERLAY_UTILS_H