blob: 089d3fbcd0a7b5981e273216189993cfc882bf78 [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{
822 return (format == HAL_PIXEL_FORMAT_YV12) ?
823 format : colorFormat(format);
824}
825
826// FB0
827template <int CHAN>
828inline Dim getPositionS3DImpl(const Whf& whf)
829{
830 switch (whf.format & OUTPUT_3D_MASK)
831 {
832 case HAL_3D_OUT_SBS_MASK:
833 // x, y, w, h
834 return Dim(0, 0, whf.w/2, whf.h);
835 case HAL_3D_OUT_TOP_BOT_MASK:
836 return Dim(0, 0, whf.w, whf.h/2);
837 case HAL_3D_OUT_MONOS_MASK:
838 return Dim();
839 case HAL_3D_OUT_INTERL_MASK:
840 // FIXME error?
841 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
842 return Dim();
843 default:
844 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
845 whf.format);
846 }
847 return Dim();
848}
849
850template <>
851inline Dim getPositionS3DImpl<utils::OV_PIPE1>(const Whf& whf)
852{
853 switch (whf.format & OUTPUT_3D_MASK)
854 {
855 case HAL_3D_OUT_SBS_MASK:
856 return Dim(whf.w/2, 0, whf.w/2, whf.h);
857 case HAL_3D_OUT_TOP_BOT_MASK:
858 return Dim(0, whf.h/2, whf.w, whf.h/2);
859 case HAL_3D_OUT_MONOS_MASK:
860 return Dim(0, 0, whf.w, whf.h);
861 case HAL_3D_OUT_INTERL_MASK:
862 // FIXME error?
863 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
864 return Dim();
865 default:
866 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
867 whf.format);
868 }
869 return Dim();
870}
871
872template <int CHAN>
873inline bool getPositionS3D(const Whf& whf, Dim& out) {
874 out = getPositionS3DImpl<CHAN>(whf);
875 return (out != Dim());
876}
877
878template <int CHAN>
879inline Dim getCropS3DImpl(const Dim& in, uint32_t fmt) {
880 switch (fmt & INPUT_3D_MASK)
881 {
882 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
883 return Dim(0, 0, in.w/2, in.h);
884 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
885 return Dim(in.w/2, 0, in.w/2, in.h);
886 case HAL_3D_IN_TOP_BOTTOM:
887 return Dim(0, 0, in.w, in.h/2);
888 case HAL_3D_IN_INTERLEAVE:
889 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
890 break;
891 default:
892 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
893 break;
894 }
895 return Dim();
896}
897
898template <>
899inline Dim getCropS3DImpl<utils::OV_PIPE1>(const Dim& in, uint32_t fmt) {
900 switch (fmt & INPUT_3D_MASK)
901 {
902 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
903 return Dim(in.w/2, 0, in.w/2, in.h);
904 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
905 return Dim(0, 0, in.w/2, in.h);
906 case HAL_3D_IN_TOP_BOTTOM:
907 return Dim(0, in.h/2, in.w, in.h/2);
908 case HAL_3D_IN_INTERLEAVE:
909 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
910 break;
911 default:
912 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
913 break;
914 }
915 return Dim();
916}
917
918template <int CHAN>
919inline bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt)
920{
921 out = getCropS3DImpl<CHAN>(in, fmt);
922 return (out != Dim());
923}
924
925template <class Type>
926void swapWidthHeight(Type& width, Type& height) {
927 Type tmp = width;
928 width = height;
929 height = tmp;
930}
931
932inline void ScreenInfo::dump(const char* const s) const {
933 ALOGE("== Dump %s ScreenInfo w=%d h=%d"
934 " bpp=%d stride=%d start/end ==",
935 s, mFBWidth, mFBHeight, mFBbpp, mFBystride);
936}
937
938inline void setSrcRectDim(const overlay::utils::Dim d,
939 mdp_overlay& ov) {
940 ov.src_rect.x = d.x;
941 ov.src_rect.y = d.y;
942 ov.src_rect.w = d.w;
943 ov.src_rect.h = d.h;
944}
945
946inline void setDstRectDim(const overlay::utils::Dim d,
947 mdp_overlay& ov) {
948 ov.dst_rect.x = d.x;
949 ov.dst_rect.y = d.y;
950 ov.dst_rect.w = d.w;
951 ov.dst_rect.h = d.h;
952}
953
954inline overlay::utils::Whf getSrcWhf(const mdp_overlay& ov) {
955 return overlay::utils::Whf(ov.src.width,
956 ov.src.height,
957 ov.src.format);
958}
959
960inline overlay::utils::Dim getSrcRectDim(const mdp_overlay& ov) {
961 return overlay::utils::Dim(ov.src_rect.x,
962 ov.src_rect.y,
963 ov.src_rect.w,
964 ov.src_rect.h);
965}
966
967inline overlay::utils::Dim getDstRectDim(const mdp_overlay& ov) {
968 return overlay::utils::Dim(ov.dst_rect.x,
969 ov.dst_rect.y,
970 ov.dst_rect.w,
971 ov.dst_rect.h);
972}
973
974
975} // namespace utils ends
976
977//--------------------Class Res stuff (namespace overlay only) -----------
978
979class Res {
980public:
981 // /dev/graphics/fb%u
982 static const char* const devTemplate;
983 // /dev/msm_rotator
984 static const char* const rotPath;
985 // /sys/class/graphics/fb1/format_3d
986 static const char* const format3DFile;
987 // /sys/class/graphics/fb1/3d_present
988 static const char* const edid3dInfoFile;
989 // /sys/devices/platform/mipi_novatek.0/enable_3d_barrier
990 static const char* const barrierFile;
991};
992
993
994//--------------------Class OvFD stuff (namespace overlay only) -----------
995
996class OvFD;
997
998/* helper function to open by using fbnum */
999bool open(OvFD& fd, uint32_t fbnum, const char* const dev,
1000 int flags = O_RDWR);
1001
1002/*
1003* Holds one FD
1004* Dtor will NOT close the underlying FD.
1005* That enables us to copy that object around
1006* */
1007class OvFD {
1008public:
1009 /* Ctor */
1010 explicit OvFD();
1011
1012 /* dtor will NOT close the underlying FD */
1013 ~OvFD();
1014
1015 /* Open fd using the path given by dev.
1016 * return false in failure */
1017 bool open(const char* const dev,
1018 int flags = O_RDWR);
1019
1020 /* populate path */
1021 void setPath(const char* const dev);
1022
1023 /* Close fd if we have a valid fd. */
1024 bool close();
1025
1026 /* returns underlying fd.*/
1027 int getFD() const;
1028
1029 /* returns true if fd is valid */
1030 bool valid() const;
1031
1032 /* like operator= */
1033 void copy(int fd);
1034
1035 /* dump the state of the instance */
1036 void dump() const;
1037private:
1038 /* helper enum for determine valid/invalid fd */
1039 enum { INVAL = -1 };
1040
1041 /* actual os fd */
1042 int mFD;
1043
1044 /* path, for debugging */
1045 char mPath[utils::MAX_PATH_LEN];
1046};
1047
1048//-------------------Inlines--------------------------
1049
1050inline bool open(OvFD& fd, uint32_t fbnum, const char* const dev, int flags)
1051{
1052 char dev_name[64] = {0};
1053 snprintf(dev_name, sizeof(dev_name), dev, fbnum);
1054 return fd.open(dev_name, flags);
1055}
1056
1057inline OvFD::OvFD() : mFD (INVAL) {
1058 mPath[0] = 0;
1059}
1060
1061inline OvFD::~OvFD() { /* no op in the meantime */ }
1062
1063inline bool OvFD::open(const char* const dev, int flags)
1064{
1065 mFD = ::open(dev, flags, 0);
1066 if (mFD < 0) {
1067 // FIXME errno, strerror in bionic?
1068 ALOGE("Cant open device %s err=%d", dev, errno);
1069 return false;
1070 }
1071 setPath(dev);
1072 return true;
1073}
1074
1075inline void OvFD::setPath(const char* const dev)
1076{
1077 ::strncpy(mPath, dev, utils::MAX_PATH_LEN);
1078}
1079
1080inline bool OvFD::close()
1081{
1082 int ret = 0;
1083 if(valid()) {
1084 ret = ::close(mFD);
1085 mFD = INVAL;
1086 }
1087 return (ret == 0);
1088}
1089
1090inline bool OvFD::valid() const
1091{
1092 return (mFD != INVAL);
1093}
1094
1095inline int OvFD::getFD() const { return mFD; }
1096
1097inline void OvFD::copy(int fd) {
1098 mFD = fd;
1099}
1100
1101inline void OvFD::dump() const
1102{
1103 ALOGE("== Dump OvFD fd=%d path=%s start/end ==",
1104 mFD, mPath);
1105}
1106
1107//--------------- class OvFD stuff ends ---------------------
1108
1109} // overlay
1110
1111
1112#endif // OVERLAY_UTILS_H