blob: 7d964a99056fc558f1cef52cd9adc6c568ff8144 [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
38#include <linux/msm_mdp.h> // MDP_OV_PLAY_NOWAIT etc ...
39#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>
45
46/*
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;
73template <class T>
74 inline void even_out(T& x) { if (x & 0x0001) --x; }
75
76inline uint32_t getBit(uint32_t x, uint32_t mask) {
77 return (x & mask);
78}
79
80inline uint32_t setBit(uint32_t x, uint32_t mask) {
81 return (x | mask);
82}
83
84inline uint32_t clrBit(uint32_t x, uint32_t mask) {
85 return (x & ~mask);
86}
87
88/* Utility class to help avoid copying instances by making the copy ctor
89* and assignment operator private
90*
91* Usage:
92* * class SomeClass : utils::NoCopy {...};
93*/
94class NoCopy {
95protected:
96 NoCopy(){}
97 ~NoCopy() {}
98private:
99 NoCopy(const NoCopy&);
100 const NoCopy& operator=(const NoCopy&);
101};
102
103/*
104* Utility class to query the framebuffer info for primary display
105*
106* Usage:
107* Outside of functions:
108* utils::FrameBufferInfo* utils::FrameBufferInfo::sFBInfoInstance = 0;
109* Inside functions:
110* utils::FrameBufferInfo::getInstance()->supportTrueMirroring()
111*/
112class FrameBufferInfo {
113
114public:
115 /* ctor init */
116 explicit FrameBufferInfo();
117
118 /* Gets an instance if one does not already exist */
119 static FrameBufferInfo* getInstance();
120
121 /* Gets width of primary framebuffer */
122 int getWidth() const;
123
124 /* Gets height of primary framebuffer */
125 int getHeight() const;
126
127 /* Indicates whether true mirroring is supported */
128 bool supportTrueMirroring() const;
129
130private:
131 int mFBWidth;
132 int mFBHeight;
133 bool mBorderFillSupported;
134 static FrameBufferInfo *sFBInfoInstance;
135};
136
137/* 3D related utils, defines etc...
138 * The compound format passed to the overlay is
139 * ABCCC where A is the input 3D format
140 * B is the output 3D format
141 * CCC is the color format e.g YCbCr420SP YCrCb420SP etc */
142enum { SHIFT_OUT_3D = 12,
143 SHIFT_TOT_3D = 16 };
144enum { INPUT_3D_MASK = 0xFFFF0000,
145 OUTPUT_3D_MASK = 0x0000FFFF };
146enum { BARRIER_LAND = 1,
147 BARRIER_PORT = 2 };
148
149inline uint32_t format3D(uint32_t x) { return x & 0xFF000; }
150inline uint32_t colorFormat(uint32_t x) { return x & 0xFFF; }
151inline uint32_t format3DOutput(uint32_t x) {
152 return (x & 0xF000) >> SHIFT_OUT_3D; }
153inline uint32_t format3DInput(uint32_t x) { return x & 0xF0000; }
154uint32_t getColorFormat(uint32_t format);
155
156bool isHDMIConnected ();
157bool is3DTV();
158bool isPanel3D();
159bool usePanel3D();
160bool send3DInfoPacket (uint32_t fmt);
161bool enableBarrier (uint32_t orientation);
162uint32_t getS3DFormat(uint32_t fmt);
163template <int CHAN>
164 bool getPositionS3D(const Whf& whf, Dim& out);
165template <int CHAN>
166 bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt);
167template <class Type>
168 void swapWidthHeight(Type& width, Type& height);
169
170struct Dim {
171 Dim () : x(0), y(0),
172 w(0), h(0),
173 o(0) {}
174 Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h) :
175 x(_x), y(_y),
176 w(_w), h(_h) {}
177 Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h, uint32_t _o) :
178 x(_x), y(_y),
179 w(_w), h(_h),
180 o(_o) {}
181 bool check(uint32_t _w, uint32_t _h) const {
182 return (x+w <= _w && y+h <= _h);
183
184 }
185
186 bool operator==(const Dim& d) const {
187 return d.x == x && d.y == y &&
188 d.w == w && d.h == h &&
189 d.o == o;
190 }
191
192 bool operator!=(const Dim& d) const {
193 return !operator==(d);
194 }
195
196 void even_out() {
197 utils::even_out(x);
198 utils::even_out(y);
199 utils::even_out(w);
200 utils::even_out(h);
201 }
202
203 void dump() const;
204 uint32_t x;
205 uint32_t y;
206 uint32_t w;
207 uint32_t h;
208 uint32_t o;
209};
210
211// TODO have Whfz
212
213struct Whf {
214 Whf() : w(0), h(0), format(0), size(0) {}
215 Whf(uint32_t wi, uint32_t he, uint32_t f) :
216 w(wi), h(he), format(f), size(0) {}
217 Whf(uint32_t wi, uint32_t he, uint32_t f, uint32_t s) :
218 w(wi), h(he), format(f), size(s) {}
219 // FIXME not comparing size at the moment
220 bool operator==(const Whf& whf) const {
221 return whf.w == w && whf.h == h &&
222 whf.format == format;
223 }
224 bool operator!=(const Whf& whf) const {
225 return !operator==(whf);
226 }
227 void dump() const;
228 uint32_t w;
229 uint32_t h;
230 // FIXME need to be int32_t ?
231 uint32_t format;
232 uint32_t size;
233};
234
235enum { MAX_PATH_LEN = 256 };
236
237enum eParams {
238 OVERLAY_DITHER,
239 OVERLAY_TRANSFORM,
240 OVERLAY_TRANSFORM_UI
241};
242
243struct Params{
244 Params(eParams p, int v) : param(p), value(v) {}
245 eParams param;
246 int value;
247};
248
249
250/**
251 * Rotator flags: not to be confused with orientation flags.
252 * Ususally, you want to open the rotator to make sure it is
253 * ready for business.
254 * ROT_FLAG_DISABLED: Rotator would not kick in. (ioctl will emit errors).
255 * ROT_FLAG_ENABLED: and when rotation is needed.
256 * (prim video playback)
257 * (UI mirroring on HDMI w/ 0 degree rotator. - just memcpy)
258 * In HDMI UI mirroring, rotator is always used.
259 * Even when w/o orienation change on primary,
260 * we do 0 rotation on HDMI and using rotator buffers.
261 * That is because we might see tearing otherwise. so
262 * we use another buffer (rotator).
263 * When a simple video playback on HDMI, no rotator is being used.(null r).
264 * */
265enum eRotFlags {
266 ROT_FLAG_DISABLED = 0,
267 ROT_FLAG_ENABLED = 1 // needed in rot
268};
269
270/* Used for rotator open.
271 * FIXME that is default, might be configs */
272enum { ROT_NUM_BUFS = 2 };
273
274/* Wait/No wait for waiting for vsync
275 * WAIT - wait for vsync, ignore fb (no need to compose w/ fb)
276 * NO_WAIT - do not wait for vsync and return immediatly since
277 * we need to run composition code */
278enum eWait {
279 WAIT,
280 NO_WAIT
281};
282
283/* The values for is_fg flag for control alpha and transp
284 * IS_FG_OFF means is_fg = 0
285 * IS_FG_SET means is_fg = 1
286 */
287enum eIsFg {
288 IS_FG_OFF = 0,
289 IS_FG_SET = 1
290};
291
292/*
293 * Various mdp flags like PIPE SHARE, DEINTERLACE etc...
294 * kernel/common/linux/msm_mdp.h
295 * INTERLACE_MASK: hardware/qcom/display/libgralloc/badger/fb_priv.h
296 * */
297enum eMdpFlags {
298 OV_MDP_FLAGS_NONE = 0,
299 OV_MDP_PIPE_SHARE = MDP_OV_PIPE_SHARE,
300 OV_MDP_DEINTERLACE = MDP_DEINTERLACE,
301 OV_MDP_PLAY_NOWAIT = MDP_OV_PLAY_NOWAIT,
302 OV_MDP_SECURE_OVERLAY_SESSION = MDP_SECURE_OVERLAY_SESSION
303};
304
305enum eOverlayPipeType {
306 OV_PIPE_TYPE_NULL,
307 OV_PIPE_TYPE_BYPASS,
308 OV_PIPE_TYPE_GENERIC,
309 OV_PIPE_TYPE_HDMI,
310 OV_PIPE_TYPE_M3D_EXTERNAL,
311 OV_PIPE_TYPE_M3D_PRIMARY,
312 OV_PIPE_TYPE_RGB,
313 OV_PIPE_TYPE_S3D_EXTERNAL,
314 OV_PIPE_TYPE_S3D_PRIMARY,
315 OV_PIPE_TYPE_UI_MIRROR
316};
317
318enum eZorder {
319 ZORDER_0,
320 ZORDER_1,
321 ZORDER_2,
322 Z_SYSTEM_ALLOC = 0xFFFF
323};
324
325enum eMdpPipeType {
326 OV_MDP_PIPE_RGB,
327 OV_MDP_PIPE_VG
328};
329
330/* Corresponds to pipes in eDest */
331enum eChannel {
332 CHANNEL_0,
333 CHANNEL_1,
334 CHANNEL_2
335};
336
337// Max pipes via overlay (VG0, VG1, RGB1)
338enum { MAX_PIPES = 3 };
339
340/* Used to identify destination channels and
341 * also 3D channels e.g. when in 3D mode with 2
342 * pipes opened and it is used in get crop/pos 3D
343 *
344 * PLEASE NOTE : DO NOT USE eDest FOR ARRAYS
345 * i.e. args[OV_PIPE1] since it is a BIT MASK
346 * use CHANNELS enum instead. Each OV_PIPEX is
347 * not specific to a display (primary/external).
348 * */
349enum eDest {
350 OV_PIPE0 = 1 << 0,
351 OV_PIPE1 = 1 << 1,
352 OV_PIPE2 = 1 << 2,
353 OV_PIPE_ALL = (OV_PIPE0 | OV_PIPE1 | OV_PIPE2)
354};
355
356/* values for copybit_set_parameter(OVERLAY_TRANSFORM) */
357enum eTransform {
358 /* No rot */
359 OVERLAY_TRANSFORM_0 = 0x0,
360 /* flip source image horizontally */
361 OVERLAY_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H,
362 /* flip source image vertically */
363 OVERLAY_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
364 /* rotate source image 90 degrees */
365 OVERLAY_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
366 /* rotate source image 180 degrees
367 * It is basically bit-or-ed H | V == 0x3 */
368 OVERLAY_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
369 /* rotate source image 270 degrees
370 * Basically 180 | 90 == 0x7 */
371 OVERLAY_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
372 /* rotate invalid like in Transform.h */
373 OVERLAY_TRANSFORM_INV = 0x80
374};
375
376/* offset and fd are play info */
377struct PlayInfo {
378 PlayInfo() : fd(-1), offset(0) {}
379 PlayInfo(int _fd, uint32_t _offset) :
380 fd(_fd), offset(_offset) {}
381 bool operator==(const PlayInfo& p) {
382 return (fd == p.fd && offset == p.offset);
383 }
384 int fd;
385 uint32_t offset;
386};
387
388// Used to consolidate pipe params
389struct PipeArgs {
390 PipeArgs() : mdpFlags(OV_MDP_FLAGS_NONE),
391 orientation(OVERLAY_TRANSFORM_0),
392 wait(NO_WAIT),
393 zorder(Z_SYSTEM_ALLOC),
394 isFg(IS_FG_OFF),
395 rotFlags(ROT_FLAG_DISABLED){
396 }
397
398 PipeArgs(eMdpFlags f, eTransform o,
399 Whf _whf, eWait w,
400 eZorder z, eIsFg fg, eRotFlags r) :
401 mdpFlags(f),
402 orientation(o),
403 whf(_whf),
404 wait(w),
405 zorder(z),
406 isFg(fg),
407 rotFlags(r) {
408 }
409
410 eMdpFlags mdpFlags; // for mdp_overlay flags PIPE_SHARE, NO_WAIT, etc
411 eTransform orientation; // FIXME docs
412 Whf whf;
413 eWait wait; // flags WAIT/NO_WAIT
414 eZorder zorder; // stage number
415 eIsFg isFg; // control alpha & transp
416 eRotFlags rotFlags;
417 PlayInfo play;
418};
419
420enum eOverlayState{
421 /* No pipes from overlay open */
422 OV_CLOSED = 0,
423
424 /* 2D Video */
425 OV_2D_VIDEO_ON_PANEL,
426 OV_2D_VIDEO_ON_PANEL_TV,
427
428 /* 3D Video on one display (panel or TV) */
429 OV_3D_VIDEO_ON_2D_PANEL,
430 OV_3D_VIDEO_ON_3D_PANEL,
431 OV_3D_VIDEO_ON_3D_TV,
432
433 /* 3D Video on two displays (panel and TV) */
434 OV_3D_VIDEO_ON_2D_PANEL_2D_TV,
435
436 /* UI Mirroring */
437 OV_UI_MIRROR,
438 OV_2D_TRUE_UI_MIRROR,
439 OV_M3D_TRUE_UI_MIRROR, // Not yet supported
440
441 /* Composition Bypass */
442 OV_BYPASS_1_LAYER,
443 OV_BYPASS_2_LAYER,
444 OV_BYPASS_3_LAYER,
445};
446
447inline void setMdpFlags(eMdpFlags& f, eMdpFlags v) {
448 f = static_cast<eMdpFlags>(setBit(f, v));
449}
450
451inline void clearMdpFlags(eMdpFlags& f, eMdpFlags v) {
452 f = static_cast<eMdpFlags>(clrBit(f, v));
453}
454
455// fb 0/1/2
456enum { FB0, FB1, FB2 };
457
458//Panels could be categorized as primary and external
459enum { PRIMARY, EXTERNAL };
460
461//External Panels could use HDMI or WFD
462enum {
463 HDMI = 1,
464 WFD = 2
465};
466
467static int sExtType = HDMI; //HDMI or WFD
468
469//Set by client as HDMI/WFD
470static inline void setExtType(const int& type) {
471 if(type != HDMI || type != WFD) {
472 ALOGE("%s: Unrecognized type %d", __func__, type);
473 return;
474 }
475 sExtType = type;
476}
477
478//Return External panel type set by client.
479static inline int getExtType() {
480 return sExtType;
481}
482
483//Gets the FB number for the external type.
484//As of now, HDMI always has fb1, WFD could use fb1 or fb2
485//Assumes Ext type set by setExtType() from client.
486static int getFBForPanel(int panel) { // PRIMARY OR EXTERNAL
487 switch(panel) {
488 case PRIMARY: return FB0;
489 break;
490 case EXTERNAL:
491 switch(getExtType()) {
492 case HDMI: return FB1;
493 break;
494 case WFD: return FB2;//Hardcoding fb2 for wfd. Will change.
495 break;
496 }
497 break;
498 default:
499 ALOGE("%s: Unrecognized PANEL category %d", __func__, panel);
500 break;
501 }
502 return -1;
503}
504
505// number of rgb pipes bufs (max)
506// 2 for rgb0/1 double bufs
507enum { RGB_PIPE_NUM_BUFS = 2 };
508
509struct ScreenInfo {
510 ScreenInfo() : mFBWidth(0),
511 mFBHeight(0),
512 mFBbpp(0),
513 mFBystride(0) {}
514 void dump(const char* const s) const;
515 uint32_t mFBWidth;
516 uint32_t mFBHeight;
517 uint32_t mFBbpp;
518 uint32_t mFBystride;
519};
520
521int getMdpFormat(int format);
522int getRotOutFmt(uint32_t format);
523/* flip is upside down and such. V, H flip
524 * rotation is 90, 180 etc
525 * It returns MDP related enum/define that match rot+flip*/
526int getMdpOrient(eTransform rotation);
527uint32_t getSize(const Whf& whf);
528uint32_t getSizeByMdp(const Whf& whf);
529const char* getFormatString(uint32_t format);
530const char* getStateString(eOverlayState state);
531
532inline int setWait(eWait wait, int flags) {
533 return (wait == WAIT) ?
534 flags &= ~MDP_OV_PLAY_NOWAIT :
535 flags |= MDP_OV_PLAY_NOWAIT;
536}
537/* possible overlay formats libhardware/include/hardware/hardware.h */
538enum eFormat {
539 OVERLAY_FORMAT_RGBA_8888 = HAL_PIXEL_FORMAT_RGBA_8888,
540 OVERLAY_FORMAT_RGB_565 = HAL_PIXEL_FORMAT_RGB_565,
541 OVERLAY_FORMAT_BGRA_8888 = HAL_PIXEL_FORMAT_BGRA_8888,
542 OVERLAY_FORMAT_YCbYCr_422_I = 0x14,
543 OVERLAY_FORMAT_CbYCrY_422_I = 0x16,
544 OVERLAY_FORMAT_DEFAULT = 99 // The actual color format is
545 // determined by the overlay
546};
547
548// Cannot use HW_OVERLAY_MAGNIFICATION_LIMIT, since at the time
549// of integration, HW_OVERLAY_MAGNIFICATION_LIMIT was a define
550enum { HW_OV_MAGNIFICATION_LIMIT = 20,
551 HW_OV_MINIFICATION_LIMIT = 8
552};
553
554inline bool rotated(int orie) {
555 return (orie == OVERLAY_TRANSFORM_ROT_90 ||
556 orie == OVERLAY_TRANSFORM_ROT_270);
557}
558
559/* used by crop funcs in order to
560 * normalizes the crop values to be all even */
561void normalizeCrop(uint32_t& xy, uint32_t& wh);
562
563template <class T>
564 inline void memset0(T& t) { ::memset(&t, 0, sizeof(T)); }
565
566template <class ROT, class MDP>
567 inline void swapOVRotWidthHeight(ROT& rot, MDP& mdp)
568 {
569 mdp.swapSrcWH();
570 mdp.swapSrcRectWH();
571 rot.swapDstWH();
572 }
573
574template <class T> inline void swap ( T& a, T& b )
575{
576 T c(a); a=b; b=c;
577}
578
579inline int alignup(int value, int a) {
580 //if align = 0, return the value. Else, do alignment.
581 return a ? ((((value - 1) / a) + 1) * a) : value;
582}
583
584// FIXME that align should replace the upper one.
585inline int align(int value, int a) {
586 //if align = 0, return the value. Else, do alignment.
587 return a ? ((value + (a-1)) & ~(a-1)) : value;
588}
589
590
591template <class MDP>
592inline utils::Dim getSrcRectDim(const MDP& ov) {
593 return utils::Dim(ov.src_rect.x,
594 ov.src_rect.y,
595 ov.src_rect.w,
596 ov.src_rect.h);
597}
598
599template <class MDP>
600inline utils::Whf getSrcWhf(const MDP& ov) {
601 return utils::Whf(ov.src.width,
602 ov.src.height,
603 ov.src.format);
604}
605template <class MDP>
606inline void setSrcRectDim(MDP& ov, const utils::Dim& d) {
607 ov.src_rect.x = d.x;
608 ov.src_rect.y = d.y;
609 ov.src_rect.w = d.w;
610 ov.src_rect.h = d.h;
611}
612template <class MDP>
613inline void setSrcWhf(MDP& ov, const utils::Whf& whf) {
614 ov.src.width = whf.w;
615 ov.src.height = whf.h;
616 ov.src.format = whf.format;
617}
618
619enum eRotOutFmt {
620 ROT_OUT_FMT_DEFAULT,
621 ROT_OUT_FMT_Y_CRCB_H2V2
622};
623
624template <int ROT_OUT_FMT> struct RotOutFmt;
625
626// FIXME, taken from gralloc_priv.h. Need to
627// put it back as soon as overlay takes place of the old one
628/* possible formats for 3D content*/
629enum {
630 HAL_NO_3D = 0x0000,
631 HAL_3D_IN_SIDE_BY_SIDE_L_R = 0x10000,
632 HAL_3D_IN_TOP_BOTTOM = 0x20000,
633 HAL_3D_IN_INTERLEAVE = 0x40000,
634 HAL_3D_IN_SIDE_BY_SIDE_R_L = 0x80000,
635 HAL_3D_OUT_SIDE_BY_SIDE = 0x1000,
636 HAL_3D_OUT_TOP_BOTTOM = 0x2000,
637 HAL_3D_OUT_INTERLEAVE = 0x4000,
638 HAL_3D_OUT_MONOSCOPIC = 0x8000
639};
640
641enum { HAL_3D_OUT_SBS_MASK =
642 HAL_3D_OUT_SIDE_BY_SIDE >> overlay::utils::SHIFT_OUT_3D,
643 HAL_3D_OUT_TOP_BOT_MASK =
644 HAL_3D_OUT_TOP_BOTTOM >> overlay::utils::SHIFT_OUT_3D,
645 HAL_3D_OUT_INTERL_MASK =
646 HAL_3D_OUT_INTERLEAVE >> overlay::utils::SHIFT_OUT_3D,
647 HAL_3D_OUT_MONOS_MASK =
648 HAL_3D_OUT_MONOSCOPIC >> overlay::utils::SHIFT_OUT_3D
649};
650
651
652inline bool isYuv(uint32_t format) {
653 switch(format){
654 case MDP_Y_CBCR_H2V1:
655 case MDP_Y_CBCR_H2V2:
656 case MDP_Y_CRCB_H2V2:
657 case MDP_Y_CRCB_H2V2_TILE:
658 case MDP_Y_CBCR_H2V2_TILE:
659 return true;
660 default:
661 return false;
662 }
663 return false;
664}
665
666inline bool isRgb(uint32_t format) {
667 switch(format) {
668 case MDP_RGBA_8888:
669 case MDP_BGRA_8888:
670 case MDP_RGBX_8888:
671 case MDP_RGB_565:
672 return true;
673 default:
674 return false;
675 }
676 return false;
677}
678
679inline bool isValidDest(eDest dest)
680{
681 if ((OV_PIPE0 & dest) ||
682 (OV_PIPE1 & dest) ||
683 (OV_PIPE2 & dest)) {
684 return true;
685 }
686 return false;
687}
688
689inline const char* getFormatString(uint32_t format){
690 static const char* const formats[] = {
691 "MDP_RGB_565",
692 "MDP_XRGB_8888",
693 "MDP_Y_CBCR_H2V2",
694 "MDP_ARGB_8888",
695 "MDP_RGB_888",
696 "MDP_Y_CRCB_H2V2",
697 "MDP_YCRYCB_H2V1",
698 "MDP_Y_CRCB_H2V1",
699 "MDP_Y_CBCR_H2V1",
700 "MDP_RGBA_8888",
701 "MDP_BGRA_8888",
702 "MDP_RGBX_8888",
703 "MDP_Y_CRCB_H2V2_TILE",
704 "MDP_Y_CBCR_H2V2_TILE",
705 "MDP_Y_CR_CB_H2V2",
706 "MDP_Y_CB_CR_H2V2",
707 "MDP_IMGTYPE_LIMIT",
708 "MDP_BGR_565",
709 "MDP_FB_FORMAT",
710 "MDP_IMGTYPE_LIMIT2"
711 };
712 OVASSERT(format < sizeof(formats) / sizeof(formats[0]),
713 "getFormatString wrong fmt %d", format);
714 return formats[format];
715}
716
717inline const char* getStateString(eOverlayState state){
718 switch (state) {
719 case OV_CLOSED:
720 return "OV_CLOSED";
721 case OV_2D_VIDEO_ON_PANEL:
722 return "OV_2D_VIDEO_ON_PANEL";
723 case OV_2D_VIDEO_ON_PANEL_TV:
724 return "OV_2D_VIDEO_ON_PANEL_TV";
725 case OV_3D_VIDEO_ON_2D_PANEL:
726 return "OV_3D_VIDEO_ON_2D_PANEL";
727 case OV_3D_VIDEO_ON_3D_PANEL:
728 return "OV_3D_VIDEO_ON_3D_PANEL";
729 case OV_3D_VIDEO_ON_3D_TV:
730 return "OV_3D_VIDEO_ON_3D_TV";
731 case OV_3D_VIDEO_ON_2D_PANEL_2D_TV:
732 return "OV_3D_VIDEO_ON_2D_PANEL_2D_TV";
733 case OV_UI_MIRROR:
734 return "OV_UI_MIRROR";
735 case OV_2D_TRUE_UI_MIRROR:
736 return "OV_2D_TRUE_UI_MIRROR";
737 case OV_BYPASS_1_LAYER:
738 return "OV_BYPASS_1_LAYER";
739 case OV_BYPASS_2_LAYER:
740 return "OV_BYPASS_2_LAYER";
741 case OV_BYPASS_3_LAYER:
742 return "OV_BYPASS_3_LAYER";
743 default:
744 return "UNKNOWN_STATE";
745 }
746 return "BAD_STATE";
747}
748
749inline uint32_t getSizeByMdp(const Whf& whf) {
750 Whf _whf(whf);
751 int fmt = getMdpFormat(whf.format);
752 OVASSERT(-1 != fmt, "getSizeByMdp error in format %d",
753 whf.format);
754 _whf.format = fmt;
755 return getSize(_whf);
756}
757
758inline void Whf::dump() const {
759 ALOGE("== Dump WHF w=%d h=%d f=%d s=%d start/end ==",
760 w, h, format, size);
761}
762
763inline void Dim::dump() const {
764 ALOGE("== Dump Dim x=%d y=%d w=%d h=%d start/end ==", x, y, w, h);
765}
766
767inline int getMdpOrient(eTransform rotation) {
768 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, rotation);
769 switch(int(rotation))
770 {
771 case OVERLAY_TRANSFORM_0 : return 0;
772 case HAL_TRANSFORM_FLIP_V: return MDP_FLIP_UD;
773 case HAL_TRANSFORM_FLIP_H: return MDP_FLIP_LR;
774 case HAL_TRANSFORM_ROT_90: return MDP_ROT_90;
775 case HAL_TRANSFORM_ROT_90|HAL_TRANSFORM_FLIP_V:
776 return MDP_ROT_90|MDP_FLIP_LR;
777 case HAL_TRANSFORM_ROT_90|HAL_TRANSFORM_FLIP_H:
778 return MDP_ROT_90|MDP_FLIP_UD;
779 case HAL_TRANSFORM_ROT_180: return MDP_ROT_180;
780 case HAL_TRANSFORM_ROT_270: return MDP_ROT_270;
781 default:
782 ALOGE("%s: invalid rotation value (value = 0x%x",
783 __FUNCTION__, rotation);
784 }
785 return -1;
786}
787
788inline int getRotOutFmt(uint32_t format) {
789 switch (format) {
790 case MDP_Y_CRCB_H2V2_TILE:
791 return MDP_Y_CRCB_H2V2;
792 case MDP_Y_CBCR_H2V2_TILE:
793 return MDP_Y_CBCR_H2V2;
794 case MDP_Y_CB_CR_H2V2:
795 return MDP_Y_CBCR_H2V2;
796 default:
797 return format;
798 }
799 // not reached
800 OVASSERT(false, "%s not reached", __FUNCTION__);
801 return -1;
802}
803
804template<>
805struct RotOutFmt<ROT_OUT_FMT_DEFAULT>
806{
807 static inline int fmt(uint32_t format) {
808 return getRotOutFmt(format);
809 }
810};
811
812template<>
813struct RotOutFmt<ROT_OUT_FMT_Y_CRCB_H2V2>
814{
815 static inline int fmt(uint32_t) {
816 return MDP_Y_CRCB_H2V2;
817 }
818};
819
820inline uint32_t getColorFormat(uint32_t format)
821{
Naseer Ahmed3b4cee62012-06-21 20:52:18 -0700822 //XXX: Earlier this used to mask the format
823 //to check for interlaced or 3D. Just return
824 //the format now
825 return format;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700826}
827
828// FB0
829template <int CHAN>
830inline Dim getPositionS3DImpl(const Whf& whf)
831{
832 switch (whf.format & OUTPUT_3D_MASK)
833 {
834 case HAL_3D_OUT_SBS_MASK:
835 // x, y, w, h
836 return Dim(0, 0, whf.w/2, whf.h);
837 case HAL_3D_OUT_TOP_BOT_MASK:
838 return Dim(0, 0, whf.w, whf.h/2);
839 case HAL_3D_OUT_MONOS_MASK:
840 return Dim();
841 case HAL_3D_OUT_INTERL_MASK:
842 // FIXME error?
843 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
844 return Dim();
845 default:
846 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
847 whf.format);
848 }
849 return Dim();
850}
851
852template <>
853inline Dim getPositionS3DImpl<utils::OV_PIPE1>(const Whf& whf)
854{
855 switch (whf.format & OUTPUT_3D_MASK)
856 {
857 case HAL_3D_OUT_SBS_MASK:
858 return Dim(whf.w/2, 0, whf.w/2, whf.h);
859 case HAL_3D_OUT_TOP_BOT_MASK:
860 return Dim(0, whf.h/2, whf.w, whf.h/2);
861 case HAL_3D_OUT_MONOS_MASK:
862 return Dim(0, 0, whf.w, whf.h);
863 case HAL_3D_OUT_INTERL_MASK:
864 // FIXME error?
865 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
866 return Dim();
867 default:
868 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
869 whf.format);
870 }
871 return Dim();
872}
873
874template <int CHAN>
875inline bool getPositionS3D(const Whf& whf, Dim& out) {
876 out = getPositionS3DImpl<CHAN>(whf);
877 return (out != Dim());
878}
879
880template <int CHAN>
881inline Dim getCropS3DImpl(const Dim& in, uint32_t fmt) {
882 switch (fmt & INPUT_3D_MASK)
883 {
884 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
885 return Dim(0, 0, in.w/2, in.h);
886 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
887 return Dim(in.w/2, 0, in.w/2, in.h);
888 case HAL_3D_IN_TOP_BOTTOM:
889 return Dim(0, 0, in.w, in.h/2);
890 case HAL_3D_IN_INTERLEAVE:
891 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
892 break;
893 default:
894 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
895 break;
896 }
897 return Dim();
898}
899
900template <>
901inline Dim getCropS3DImpl<utils::OV_PIPE1>(const Dim& in, uint32_t fmt) {
902 switch (fmt & INPUT_3D_MASK)
903 {
904 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
905 return Dim(in.w/2, 0, in.w/2, in.h);
906 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
907 return Dim(0, 0, in.w/2, in.h);
908 case HAL_3D_IN_TOP_BOTTOM:
909 return Dim(0, in.h/2, in.w, in.h/2);
910 case HAL_3D_IN_INTERLEAVE:
911 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
912 break;
913 default:
914 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
915 break;
916 }
917 return Dim();
918}
919
920template <int CHAN>
921inline bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt)
922{
923 out = getCropS3DImpl<CHAN>(in, fmt);
924 return (out != Dim());
925}
926
927template <class Type>
928void swapWidthHeight(Type& width, Type& height) {
929 Type tmp = width;
930 width = height;
931 height = tmp;
932}
933
934inline void ScreenInfo::dump(const char* const s) const {
935 ALOGE("== Dump %s ScreenInfo w=%d h=%d"
936 " bpp=%d stride=%d start/end ==",
937 s, mFBWidth, mFBHeight, mFBbpp, mFBystride);
938}
939
940inline void setSrcRectDim(const overlay::utils::Dim d,
941 mdp_overlay& ov) {
942 ov.src_rect.x = d.x;
943 ov.src_rect.y = d.y;
944 ov.src_rect.w = d.w;
945 ov.src_rect.h = d.h;
946}
947
948inline void setDstRectDim(const overlay::utils::Dim d,
949 mdp_overlay& ov) {
950 ov.dst_rect.x = d.x;
951 ov.dst_rect.y = d.y;
952 ov.dst_rect.w = d.w;
953 ov.dst_rect.h = d.h;
954}
955
956inline overlay::utils::Whf getSrcWhf(const mdp_overlay& ov) {
957 return overlay::utils::Whf(ov.src.width,
958 ov.src.height,
959 ov.src.format);
960}
961
962inline overlay::utils::Dim getSrcRectDim(const mdp_overlay& ov) {
963 return overlay::utils::Dim(ov.src_rect.x,
964 ov.src_rect.y,
965 ov.src_rect.w,
966 ov.src_rect.h);
967}
968
969inline overlay::utils::Dim getDstRectDim(const mdp_overlay& ov) {
970 return overlay::utils::Dim(ov.dst_rect.x,
971 ov.dst_rect.y,
972 ov.dst_rect.w,
973 ov.dst_rect.h);
974}
975
976
977} // namespace utils ends
978
979//--------------------Class Res stuff (namespace overlay only) -----------
980
981class Res {
982public:
983 // /dev/graphics/fb%u
984 static const char* const devTemplate;
985 // /dev/msm_rotator
986 static const char* const rotPath;
987 // /sys/class/graphics/fb1/format_3d
988 static const char* const format3DFile;
989 // /sys/class/graphics/fb1/3d_present
990 static const char* const edid3dInfoFile;
991 // /sys/devices/platform/mipi_novatek.0/enable_3d_barrier
992 static const char* const barrierFile;
993};
994
995
996//--------------------Class OvFD stuff (namespace overlay only) -----------
997
998class OvFD;
999
1000/* helper function to open by using fbnum */
1001bool open(OvFD& fd, uint32_t fbnum, const char* const dev,
1002 int flags = O_RDWR);
1003
1004/*
1005* Holds one FD
1006* Dtor will NOT close the underlying FD.
1007* That enables us to copy that object around
1008* */
1009class OvFD {
1010public:
1011 /* Ctor */
1012 explicit OvFD();
1013
1014 /* dtor will NOT close the underlying FD */
1015 ~OvFD();
1016
1017 /* Open fd using the path given by dev.
1018 * return false in failure */
1019 bool open(const char* const dev,
1020 int flags = O_RDWR);
1021
1022 /* populate path */
1023 void setPath(const char* const dev);
1024
1025 /* Close fd if we have a valid fd. */
1026 bool close();
1027
1028 /* returns underlying fd.*/
1029 int getFD() const;
1030
1031 /* returns true if fd is valid */
1032 bool valid() const;
1033
1034 /* like operator= */
1035 void copy(int fd);
1036
1037 /* dump the state of the instance */
1038 void dump() const;
1039private:
1040 /* helper enum for determine valid/invalid fd */
1041 enum { INVAL = -1 };
1042
1043 /* actual os fd */
1044 int mFD;
1045
1046 /* path, for debugging */
1047 char mPath[utils::MAX_PATH_LEN];
1048};
1049
1050//-------------------Inlines--------------------------
1051
1052inline bool open(OvFD& fd, uint32_t fbnum, const char* const dev, int flags)
1053{
1054 char dev_name[64] = {0};
1055 snprintf(dev_name, sizeof(dev_name), dev, fbnum);
1056 return fd.open(dev_name, flags);
1057}
1058
1059inline OvFD::OvFD() : mFD (INVAL) {
1060 mPath[0] = 0;
1061}
1062
1063inline OvFD::~OvFD() { /* no op in the meantime */ }
1064
1065inline bool OvFD::open(const char* const dev, int flags)
1066{
1067 mFD = ::open(dev, flags, 0);
1068 if (mFD < 0) {
1069 // FIXME errno, strerror in bionic?
1070 ALOGE("Cant open device %s err=%d", dev, errno);
1071 return false;
1072 }
1073 setPath(dev);
1074 return true;
1075}
1076
1077inline void OvFD::setPath(const char* const dev)
1078{
1079 ::strncpy(mPath, dev, utils::MAX_PATH_LEN);
1080}
1081
1082inline bool OvFD::close()
1083{
1084 int ret = 0;
1085 if(valid()) {
1086 ret = ::close(mFD);
1087 mFD = INVAL;
1088 }
1089 return (ret == 0);
1090}
1091
1092inline bool OvFD::valid() const
1093{
1094 return (mFD != INVAL);
1095}
1096
1097inline int OvFD::getFD() const { return mFD; }
1098
1099inline void OvFD::copy(int fd) {
1100 mFD = fd;
1101}
1102
1103inline void OvFD::dump() const
1104{
1105 ALOGE("== Dump OvFD fd=%d path=%s start/end ==",
1106 mFD, mPath);
1107}
1108
1109//--------------- class OvFD stuff ends ---------------------
1110
1111} // overlay
1112
1113
1114#endif // OVERLAY_UTILS_H