1 /*
2 * Copyright (c) 2011-2012, The Linux Foundation. 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> // flags
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 #include "gralloc_priv.h" //for interlace
46
47 /*
48 *
49 * Collection of utilities functions/structs/enums etc...
50 *
51 * */
52
53 // comment that out if you want to remove asserts
54 // or put it as -D in Android.mk. your choice.
55 #define OVERLAY_HAS_ASSERT
56
57 #ifdef OVERLAY_HAS_ASSERT
58 # define OVASSERT(x, ...) if(!(x)) { ALOGE(__VA_ARGS__); abort(); }
59 #else
60 # define OVASSERT(x, ...) ALOGE_IF(!(x), __VA_ARGS__)
61 #endif // OVERLAY_HAS_ASSERT
62
63 #define DEBUG_OVERLAY 0
64 #define PROFILE_OVERLAY 0
65
66 namespace overlay {
67
68 // fwd
69 class Overlay;
70 class OvFD;
71
72 /* helper function to open by using fbnum */
73 bool open(OvFD& fd, uint32_t fbnum, const char* const dev,
74 int flags = O_RDWR);
75
76 namespace utils {
77 struct Whf;
78 struct Dim;
79
setBit(uint32_t x,uint32_t mask)80 inline uint32_t setBit(uint32_t x, uint32_t mask) {
81 return (x | mask);
82 }
83
clrBit(uint32_t x,uint32_t mask)84 inline 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 */
94 class NoCopy {
95 protected:
NoCopy()96 NoCopy(){}
~NoCopy()97 ~NoCopy() {}
98 private:
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 */
112 class FrameBufferInfo {
113
114 public:
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 private:
128 int mFBWidth;
129 int mFBHeight;
130 bool mBorderFillSupported;
131 static FrameBufferInfo *sFBInfoInstance;
132 };
133
134 /* 3D related utils, defines etc...
135 * The compound format passed to the overlay is
136 * ABCCC where A is the input 3D format
137 * B is the output 3D format
138 * CCC is the color format e.g YCbCr420SP YCrCb420SP etc */
139 enum { SHIFT_OUT_3D = 12,
140 SHIFT_TOT_3D = 16 };
141 enum { INPUT_3D_MASK = 0xFFFF0000,
142 OUTPUT_3D_MASK = 0x0000FFFF };
143 enum { BARRIER_LAND = 1,
144 BARRIER_PORT = 2 };
145
146 /* if SurfaceFlinger process gets killed in bypass mode, In initOverlay()
147 * close all the pipes if it is opened after reboot.
148 */
149 int initOverlay(void);
150
format3D(uint32_t x)151 inline uint32_t format3D(uint32_t x) { return x & 0xFF000; }
colorFormat(uint32_t fmt)152 inline uint32_t colorFormat(uint32_t fmt) {
153 /*TODO enable this block only if format has interlace / 3D info in top bits.
154 if(fmt & INTERLACE_MASK) {
155 fmt = fmt ^ HAL_PIXEL_FORMAT_INTERLACE;
156 }
157 fmt = fmt & 0xFFF;*/
158 return fmt;
159 }
format3DOutput(uint32_t x)160 inline uint32_t format3DOutput(uint32_t x) {
161 return (x & 0xF000) >> SHIFT_OUT_3D; }
format3DInput(uint32_t x)162 inline uint32_t format3DInput(uint32_t x) { return x & 0xF0000; }
163 uint32_t getColorFormat(uint32_t format);
164
165 bool isHDMIConnected ();
166 bool is3DTV();
167 bool isPanel3D();
168 bool usePanel3D();
169 bool send3DInfoPacket (uint32_t fmt);
170 bool enableBarrier (uint32_t orientation);
171 uint32_t getS3DFormat(uint32_t fmt);
172
173 template <int CHAN>
174 bool getPositionS3D(const Whf& whf, Dim& out);
175
176 template <int CHAN>
177 bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt);
178
179 template <class Type>
180 void swapWidthHeight(Type& width, Type& height);
181
182 struct Dim {
DimDim183 Dim () : x(0), y(0),
184 w(0), h(0),
185 o(0) {}
DimDim186 Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h) :
187 x(_x), y(_y),
188 w(_w), h(_h) {}
DimDim189 Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h, uint32_t _o) :
190 x(_x), y(_y),
191 w(_w), h(_h),
192 o(_o) {}
checkDim193 bool check(uint32_t _w, uint32_t _h) const {
194 return (x+w <= _w && y+h <= _h);
195
196 }
197
198 bool operator==(const Dim& d) const {
199 return d.x == x && d.y == y &&
200 d.w == w && d.h == h &&
201 d.o == o;
202 }
203
204 bool operator!=(const Dim& d) const {
205 return !operator==(d);
206 }
207
208 void dump() const;
209 uint32_t x;
210 uint32_t y;
211 uint32_t w;
212 uint32_t h;
213 uint32_t o;
214 };
215
216 // TODO have Whfz
217
218 struct Whf {
WhfWhf219 Whf() : w(0), h(0), format(0), size(0) {}
WhfWhf220 Whf(uint32_t wi, uint32_t he, uint32_t f) :
221 w(wi), h(he), format(f), size(0) {}
WhfWhf222 Whf(uint32_t wi, uint32_t he, uint32_t f, uint32_t s) :
223 w(wi), h(he), format(f), size(s) {}
224 // FIXME not comparing size at the moment
225 bool operator==(const Whf& whf) const {
226 return whf.w == w && whf.h == h &&
227 whf.format == format;
228 }
229 bool operator!=(const Whf& whf) const {
230 return !operator==(whf);
231 }
232 void dump() const;
233 uint32_t w;
234 uint32_t h;
235 uint32_t format;
236 uint32_t size;
237 };
238
239 class ActionSafe {
240 private:
ActionSafe()241 ActionSafe() : mWidth(0.0f), mHeight(0.0f) { };
242 float mWidth;
243 float mHeight;
244 static ActionSafe *sActionSafe;
245 public:
~ActionSafe()246 ~ActionSafe() { };
getInstance()247 static ActionSafe* getInstance() {
248 if(!sActionSafe) {
249 sActionSafe = new ActionSafe();
250 }
251 return sActionSafe;
252 }
setDimension(int w,int h)253 void setDimension(int w, int h) {
254 mWidth = (float)w;
255 mHeight = (float)h;
256 }
getWidth()257 float getWidth() { return mWidth; }
getHeight()258 float getHeight() { return mHeight; }
259 };
260
261 enum { MAX_PATH_LEN = 256 };
262
263 /**
264 * Rotator flags: not to be confused with orientation flags.
265 * Ususally, you want to open the rotator to make sure it is
266 * ready for business.
267 * ROT_FLAG_DISABLED: Rotator not used unless required.
268 * ROT_FLAG_ENABLED: Rotator used even if not required.
269 * */
270 enum eRotFlags {
271 ROT_FLAG_DISABLED = 0,
272 ROT_FLAG_ENABLED = 1 // needed in rot
273 };
274
275 /* The values for is_fg flag for control alpha and transp
276 * IS_FG_OFF means is_fg = 0
277 * IS_FG_SET means is_fg = 1
278 */
279 enum eIsFg {
280 IS_FG_OFF = 0,
281 IS_FG_SET = 1
282 };
283
284 /*
285 * Various mdp flags like PIPE SHARE, DEINTERLACE etc...
286 * kernel/common/linux/msm_mdp.h
287 * INTERLACE_MASK: hardware/qcom/display/libgralloc/badger/fb_priv.h
288 * */
289 enum eMdpFlags {
290 OV_MDP_FLAGS_NONE = 0,
291 OV_MDP_PIPE_SHARE = MDP_OV_PIPE_SHARE,
292 OV_MDP_DEINTERLACE = MDP_DEINTERLACE,
293 OV_MDP_SECURE_OVERLAY_SESSION = MDP_SECURE_OVERLAY_SESSION,
294 OV_MDP_SOURCE_ROTATED_90 = MDP_SOURCE_ROTATED_90,
295 OV_MDP_MEMORY_ID_TYPE_FB = MDP_MEMORY_ID_TYPE_FB,
296 OV_MDP_BACKEND_COMPOSITION = MDP_BACKEND_COMPOSITION,
297 OV_MDP_BLEND_FG_PREMULT = MDP_BLEND_FG_PREMULT,
298 OV_MDP_FLIP_H = MDP_FLIP_LR,
299 OV_MDP_FLIP_V = MDP_FLIP_UD,
300 };
301
302 enum eZorder {
303 ZORDER_0,
304 ZORDER_1,
305 ZORDER_2,
306 ZORDER_3,
307 Z_SYSTEM_ALLOC = 0xFFFF
308 };
309
310 enum eMdpPipeType {
311 OV_MDP_PIPE_RGB,
312 OV_MDP_PIPE_VG,
313 OV_MDP_PIPE_ANY, //Any
314 };
315
316 /* Used to identify destination pipes
317 */
318 enum eDest {
319 OV_VG0 = 0,
320 OV_RGB0,
321 OV_VG1,
322 OV_RGB1,
323 OV_VG2,
324 OV_RGB2,
325 OV_INVALID,
326 };
327
328 /* Used when a buffer is split over 2 pipes and sent to display */
329 enum {
330 OV_LEFT_SPLIT = 0,
331 OV_RIGHT_SPLIT,
332 };
333
334 /* values for copybit_set_parameter(OVERLAY_TRANSFORM) */
335 enum eTransform {
336 /* No rot */
337 OVERLAY_TRANSFORM_0 = 0x0,
338 /* flip source image horizontally 0x1 */
339 OVERLAY_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H,
340 /* flip source image vertically 0x2 */
341 OVERLAY_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
342 /* rotate source image 180 degrees
343 * It is basically bit-or-ed H | V == 0x3 */
344 OVERLAY_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
345 /* rotate source image 90 degrees 0x4 */
346 OVERLAY_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
347 /* rotate source image 90 degrees and flip horizontally 0x5 */
348 OVERLAY_TRANSFORM_ROT_90_FLIP_H = HAL_TRANSFORM_ROT_90 |
349 HAL_TRANSFORM_FLIP_H,
350 /* rotate source image 90 degrees and flip vertically 0x6 */
351 OVERLAY_TRANSFORM_ROT_90_FLIP_V = HAL_TRANSFORM_ROT_90 |
352 HAL_TRANSFORM_FLIP_V,
353 /* rotate source image 270 degrees
354 * Basically 180 | 90 == 0x7 */
355 OVERLAY_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
356 /* rotate invalid like in Transform.h */
357 OVERLAY_TRANSFORM_INV = 0x80
358 };
359
360 // Used to consolidate pipe params
361 struct PipeArgs {
PipeArgsPipeArgs362 PipeArgs() : mdpFlags(OV_MDP_FLAGS_NONE),
363 zorder(Z_SYSTEM_ALLOC),
364 isFg(IS_FG_OFF),
365 rotFlags(ROT_FLAG_DISABLED){
366 }
367
PipeArgsPipeArgs368 PipeArgs(eMdpFlags f, Whf _whf,
369 eZorder z, eIsFg fg, eRotFlags r) :
370 mdpFlags(f),
371 whf(_whf),
372 zorder(z),
373 isFg(fg),
374 rotFlags(r) {
375 }
376
377 eMdpFlags mdpFlags; // for mdp_overlay flags
378 Whf whf;
379 eZorder zorder; // stage number
380 eIsFg isFg; // control alpha & transp
381 eRotFlags rotFlags;
382 };
383
setMdpFlags(eMdpFlags & f,eMdpFlags v)384 inline void setMdpFlags(eMdpFlags& f, eMdpFlags v) {
385 f = static_cast<eMdpFlags>(setBit(f, v));
386 }
387
clearMdpFlags(eMdpFlags & f,eMdpFlags v)388 inline void clearMdpFlags(eMdpFlags& f, eMdpFlags v) {
389 f = static_cast<eMdpFlags>(clrBit(f, v));
390 }
391
392 // fb 0/1/2
393 enum { FB0, FB1, FB2 };
394
395 //Panels could be categorized as primary and external
396 enum { PRIMARY, EXTERNAL };
397
398 // 2 for rgb0/1 double bufs
399 enum { RGB_PIPE_NUM_BUFS = 2 };
400
401 struct ScreenInfo {
ScreenInfoScreenInfo402 ScreenInfo() : mFBWidth(0),
403 mFBHeight(0),
404 mFBbpp(0),
405 mFBystride(0) {}
406 void dump(const char* const s) const;
407 uint32_t mFBWidth;
408 uint32_t mFBHeight;
409 uint32_t mFBbpp;
410 uint32_t mFBystride;
411 };
412
413 int getMdpFormat(int format);
414 int getRotOutFmt(uint32_t format);
415 /* flip is upside down and such. V, H flip
416 * rotation is 90, 180 etc
417 * It returns MDP related enum/define that match rot+flip*/
418 int getMdpOrient(eTransform rotation);
419 const char* getFormatString(int format);
420
421 // Cannot use HW_OVERLAY_MAGNIFICATION_LIMIT, since at the time
422 // of integration, HW_OVERLAY_MAGNIFICATION_LIMIT was a define
423 enum { HW_OV_MAGNIFICATION_LIMIT = 20,
424 HW_OV_MINIFICATION_LIMIT = 8
425 };
426
427 template <class T>
memset0(T & t)428 inline void memset0(T& t) { ::memset(&t, 0, sizeof(T)); }
429
swap(T & a,T & b)430 template <class T> inline void swap ( T& a, T& b )
431 {
432 T c(a); a=b; b=c;
433 }
434
alignup(int value,int a)435 inline int alignup(int value, int a) {
436 //if align = 0, return the value. Else, do alignment.
437 return a ? ((((value - 1) / a) + 1) * a) : value;
438 }
439
440 // FIXME that align should replace the upper one.
align(int value,int a)441 inline int align(int value, int a) {
442 //if align = 0, return the value. Else, do alignment.
443 return a ? ((value + (a-1)) & ~(a-1)) : value;
444 }
445
446 enum eRotOutFmt {
447 ROT_OUT_FMT_DEFAULT,
448 ROT_OUT_FMT_Y_CRCB_H2V2
449 };
450
451 template <int ROT_OUT_FMT> struct RotOutFmt;
452
453 // FIXME, taken from gralloc_priv.h. Need to
454 // put it back as soon as overlay takes place of the old one
455 /* possible formats for 3D content*/
456 enum {
457 HAL_NO_3D = 0x0000,
458 HAL_3D_IN_SIDE_BY_SIDE_L_R = 0x10000,
459 HAL_3D_IN_TOP_BOTTOM = 0x20000,
460 HAL_3D_IN_INTERLEAVE = 0x40000,
461 HAL_3D_IN_SIDE_BY_SIDE_R_L = 0x80000,
462 HAL_3D_OUT_SIDE_BY_SIDE = 0x1000,
463 HAL_3D_OUT_TOP_BOTTOM = 0x2000,
464 HAL_3D_OUT_INTERLEAVE = 0x4000,
465 HAL_3D_OUT_MONOSCOPIC = 0x8000
466 };
467
468 enum { HAL_3D_OUT_SBS_MASK =
469 HAL_3D_OUT_SIDE_BY_SIDE >> overlay::utils::SHIFT_OUT_3D,
470 HAL_3D_OUT_TOP_BOT_MASK =
471 HAL_3D_OUT_TOP_BOTTOM >> overlay::utils::SHIFT_OUT_3D,
472 HAL_3D_OUT_INTERL_MASK =
473 HAL_3D_OUT_INTERLEAVE >> overlay::utils::SHIFT_OUT_3D,
474 HAL_3D_OUT_MONOS_MASK =
475 HAL_3D_OUT_MONOSCOPIC >> overlay::utils::SHIFT_OUT_3D
476 };
477
478
isYuv(uint32_t format)479 inline bool isYuv(uint32_t format) {
480 switch(format){
481 case MDP_Y_CBCR_H2V1:
482 case MDP_Y_CBCR_H2V2:
483 case MDP_Y_CRCB_H2V2:
484 case MDP_Y_CRCB_H1V1:
485 case MDP_Y_CRCB_H2V1:
486 case MDP_Y_CRCB_H2V2_TILE:
487 case MDP_Y_CBCR_H2V2_TILE:
488 case MDP_Y_CR_CB_H2V2:
489 case MDP_Y_CR_CB_GH2V2:
490 return true;
491 default:
492 return false;
493 }
494 return false;
495 }
496
isRgb(uint32_t format)497 inline bool isRgb(uint32_t format) {
498 switch(format) {
499 case MDP_RGBA_8888:
500 case MDP_BGRA_8888:
501 case MDP_RGBX_8888:
502 case MDP_RGB_565:
503 return true;
504 default:
505 return false;
506 }
507 return false;
508 }
509
getFormatString(int format)510 inline const char* getFormatString(int format){
511 static const char* const formats[] = {
512 "MDP_RGB_565",
513 "MDP_XRGB_8888",
514 "MDP_Y_CBCR_H2V2",
515 "MDP_Y_CBCR_H2V2_ADRENO",
516 "MDP_ARGB_8888",
517 "MDP_RGB_888",
518 "MDP_Y_CRCB_H2V2",
519 "MDP_YCRYCB_H2V1",
520 "MDP_Y_CRCB_H2V1",
521 "MDP_Y_CBCR_H2V1",
522 "MDP_Y_CRCB_H1V2",
523 "MDP_Y_CBCR_H1V2",
524 "MDP_RGBA_8888",
525 "MDP_BGRA_8888",
526 "MDP_RGBX_8888",
527 "MDP_Y_CRCB_H2V2_TILE",
528 "MDP_Y_CBCR_H2V2_TILE",
529 "MDP_Y_CR_CB_H2V2",
530 "MDP_Y_CR_CB_GH2V2",
531 "MDP_Y_CB_CR_H2V2",
532 "MDP_Y_CRCB_H1V1",
533 "MDP_Y_CBCR_H1V1",
534 "MDP_YCRCB_H1V1",
535 "MDP_YCBCR_H1V1",
536 "MDP_BGR_565",
537 "MDP_IMGTYPE_LIMIT",
538 "MDP_RGB_BORDERFILL",
539 "MDP_FB_FORMAT",
540 "MDP_IMGTYPE_LIMIT2"
541 };
542 if(format < 0 || format >= (int)(sizeof(formats) / sizeof(formats[0]))) {
543 ALOGE("%s wrong fmt %d", __FUNCTION__, format);
544 return "Unsupported format";
545 }
546 return formats[format];
547 }
548
dump()549 inline void Whf::dump() const {
550 ALOGE("== Dump WHF w=%d h=%d f=%d s=%d start/end ==",
551 w, h, format, size);
552 }
553
dump()554 inline void Dim::dump() const {
555 ALOGE("== Dump Dim x=%d y=%d w=%d h=%d start/end ==", x, y, w, h);
556 }
557
getMdpOrient(eTransform rotation)558 inline int getMdpOrient(eTransform rotation) {
559 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, rotation);
560 switch(rotation)
561 {
562 case OVERLAY_TRANSFORM_0 : return 0;
563 case OVERLAY_TRANSFORM_FLIP_V: return MDP_FLIP_UD;
564 case OVERLAY_TRANSFORM_FLIP_H: return MDP_FLIP_LR;
565 case OVERLAY_TRANSFORM_ROT_90: return MDP_ROT_90;
566 //getMdpOrient will switch the flips if the source is 90 rotated.
567 //Clients in Android dont factor in 90 rotation while deciding flip.
568 case OVERLAY_TRANSFORM_ROT_90_FLIP_V:
569 return MDP_ROT_90 | MDP_FLIP_LR;
570 case OVERLAY_TRANSFORM_ROT_90_FLIP_H:
571 return MDP_ROT_90 | MDP_FLIP_UD;
572 case OVERLAY_TRANSFORM_ROT_180: return MDP_ROT_180;
573 case OVERLAY_TRANSFORM_ROT_270: return MDP_ROT_270;
574 default:
575 ALOGE("%s: invalid rotation value (value = 0x%x",
576 __FUNCTION__, rotation);
577 }
578 return -1;
579 }
580
getRotOutFmt(uint32_t format)581 inline int getRotOutFmt(uint32_t format) {
582 switch (format) {
583 case MDP_Y_CRCB_H2V2_TILE:
584 return MDP_Y_CRCB_H2V2;
585 case MDP_Y_CBCR_H2V2_TILE:
586 return MDP_Y_CBCR_H2V2;
587 case MDP_Y_CB_CR_H2V2:
588 return MDP_Y_CBCR_H2V2;
589 case MDP_Y_CR_CB_GH2V2:
590 return MDP_Y_CRCB_H2V2;
591 default:
592 return format;
593 }
594 // not reached
595 OVASSERT(false, "%s not reached", __FUNCTION__);
596 return -1;
597 }
598
599
getColorFormat(uint32_t format)600 inline uint32_t getColorFormat(uint32_t format)
601 {
602 return (format == HAL_PIXEL_FORMAT_YV12) ?
603 format : colorFormat(format);
604 }
605
606 // FB0
607 template <int CHAN>
getPositionS3DImpl(const Whf & whf)608 inline Dim getPositionS3DImpl(const Whf& whf)
609 {
610 switch (whf.format & OUTPUT_3D_MASK)
611 {
612 case HAL_3D_OUT_SBS_MASK:
613 // x, y, w, h
614 return Dim(0, 0, whf.w/2, whf.h);
615 case HAL_3D_OUT_TOP_BOT_MASK:
616 return Dim(0, 0, whf.w, whf.h/2);
617 case HAL_3D_OUT_MONOS_MASK:
618 return Dim();
619 case HAL_3D_OUT_INTERL_MASK:
620 // FIXME error?
621 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
622 return Dim();
623 default:
624 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
625 whf.format);
626 }
627 return Dim();
628 }
629
630 template <>
631 inline Dim getPositionS3DImpl<utils::OV_RIGHT_SPLIT>(const Whf& whf)
632 {
633 switch (whf.format & OUTPUT_3D_MASK)
634 {
635 case HAL_3D_OUT_SBS_MASK:
636 return Dim(whf.w/2, 0, whf.w/2, whf.h);
637 case HAL_3D_OUT_TOP_BOT_MASK:
638 return Dim(0, whf.h/2, whf.w, whf.h/2);
639 case HAL_3D_OUT_MONOS_MASK:
640 return Dim(0, 0, whf.w, whf.h);
641 case HAL_3D_OUT_INTERL_MASK:
642 // FIXME error?
643 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
644 return Dim();
645 default:
646 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
647 whf.format);
648 }
649 return Dim();
650 }
651
652 template <int CHAN>
getPositionS3D(const Whf & whf,Dim & out)653 inline bool getPositionS3D(const Whf& whf, Dim& out) {
654 out = getPositionS3DImpl<CHAN>(whf);
655 return (out != Dim());
656 }
657
658 template <int CHAN>
getCropS3DImpl(const Dim & in,uint32_t fmt)659 inline Dim getCropS3DImpl(const Dim& in, uint32_t fmt) {
660 switch (fmt & INPUT_3D_MASK)
661 {
662 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
663 return Dim(0, 0, in.w/2, in.h);
664 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
665 return Dim(in.w/2, 0, in.w/2, in.h);
666 case HAL_3D_IN_TOP_BOTTOM:
667 return Dim(0, 0, in.w, in.h/2);
668 case HAL_3D_IN_INTERLEAVE:
669 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
670 break;
671 default:
672 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
673 break;
674 }
675 return Dim();
676 }
677
678 template <>
679 inline Dim getCropS3DImpl<utils::OV_RIGHT_SPLIT>(const Dim& in, uint32_t fmt) {
680 switch (fmt & INPUT_3D_MASK)
681 {
682 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
683 return Dim(in.w/2, 0, in.w/2, in.h);
684 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
685 return Dim(0, 0, in.w/2, in.h);
686 case HAL_3D_IN_TOP_BOTTOM:
687 return Dim(0, in.h/2, in.w, in.h/2);
688 case HAL_3D_IN_INTERLEAVE:
689 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
690 break;
691 default:
692 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
693 break;
694 }
695 return Dim();
696 }
697
698 template <int CHAN>
getCropS3D(const Dim & in,Dim & out,uint32_t fmt)699 inline bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt)
700 {
701 out = getCropS3DImpl<CHAN>(in, fmt);
702 return (out != Dim());
703 }
704
705 template <class Type>
swapWidthHeight(Type & width,Type & height)706 void swapWidthHeight(Type& width, Type& height) {
707 Type tmp = width;
708 width = height;
709 height = tmp;
710 }
711
dump(const char * const s)712 inline void ScreenInfo::dump(const char* const s) const {
713 ALOGE("== Dump %s ScreenInfo w=%d h=%d"
714 " bpp=%d stride=%d start/end ==",
715 s, mFBWidth, mFBHeight, mFBbpp, mFBystride);
716 }
717
openDev(OvFD & fd,int fbnum,const char * const devpath,int flags)718 inline bool openDev(OvFD& fd, int fbnum,
719 const char* const devpath, int flags) {
720 return overlay::open(fd, fbnum, devpath, flags);
721 }
722
723 template <class T>
even_ceil(T & value)724 inline void even_ceil(T& value) {
725 if(value & 1)
726 value++;
727 }
728
729 template <class T>
even_floor(T & value)730 inline void even_floor(T& value) {
731 if(value & 1)
732 value--;
733 }
734
getDestStr(eDest dest)735 inline const char* getDestStr(eDest dest) {
736 switch(dest) {
737 case OV_VG0: return "VG0";
738 case OV_RGB0: return "RGB0";
739 case OV_VG1: return "VG1";
740 case OV_RGB1: return "RGB1";
741 case OV_VG2: return "VG2";
742 case OV_RGB2: return "RGB2";
743 default: return "Invalid";
744 }
745 return "Invalid";
746 }
747
getPipeType(eDest dest)748 inline eMdpPipeType getPipeType(eDest dest) {
749 switch(dest) {
750 case OV_VG0:
751 case OV_VG1:
752 case OV_VG2:
753 return OV_MDP_PIPE_VG;
754 case OV_RGB0:
755 case OV_RGB1:
756 case OV_RGB2:
757 return OV_MDP_PIPE_RGB;
758 default:
759 return OV_MDP_PIPE_ANY;
760 }
761 return OV_MDP_PIPE_ANY;
762 }
763
764 } // namespace utils ends
765
766 //--------------------Class Res stuff (namespace overlay only) -----------
767
768 class Res {
769 public:
770 // /dev/graphics/fb%u
771 static const char* const fbPath;
772 // /dev/msm_rotator
773 static const char* const rotPath;
774 // /sys/class/graphics/fb1/format_3d
775 static const char* const format3DFile;
776 // /sys/class/graphics/fb1/3d_present
777 static const char* const edid3dInfoFile;
778 // /sys/devices/platform/mipi_novatek.0/enable_3d_barrier
779 static const char* const barrierFile;
780 };
781
782
783 //--------------------Class OvFD stuff (namespace overlay only) -----------
784
785 /*
786 * Holds one FD
787 * Dtor will NOT close the underlying FD.
788 * That enables us to copy that object around
789 * */
790 class OvFD {
791 public:
792 /* Ctor */
793 explicit OvFD();
794
795 /* dtor will NOT close the underlying FD */
796 ~OvFD();
797
798 /* Open fd using the path given by dev.
799 * return false in failure */
800 bool open(const char* const dev,
801 int flags = O_RDWR);
802
803 /* populate path */
804 void setPath(const char* const dev);
805
806 /* Close fd if we have a valid fd. */
807 bool close();
808
809 /* returns underlying fd.*/
810 int getFD() const;
811
812 /* returns true if fd is valid */
813 bool valid() const;
814
815 /* like operator= */
816 void copy(int fd);
817
818 /* dump the state of the instance */
819 void dump() const;
820 private:
821 /* helper enum for determine valid/invalid fd */
822 enum { INVAL = -1 };
823
824 /* actual os fd */
825 int mFD;
826
827 /* path, for debugging */
828 char mPath[utils::MAX_PATH_LEN];
829 };
830
831 //-------------------Inlines--------------------------
832
open(OvFD & fd,uint32_t fbnum,const char * const dev,int flags)833 inline bool open(OvFD& fd, uint32_t fbnum, const char* const dev, int flags)
834 {
835 char dev_name[64] = {0};
836 snprintf(dev_name, sizeof(dev_name), dev, fbnum);
837 return fd.open(dev_name, flags);
838 }
839
OvFD()840 inline OvFD::OvFD() : mFD (INVAL) {
841 mPath[0] = 0;
842 }
843
~OvFD()844 inline OvFD::~OvFD() {
845 //no op since copy() can be used to share fd, in 3d cases.
846 }
847
open(const char * const dev,int flags)848 inline bool OvFD::open(const char* const dev, int flags)
849 {
850 mFD = ::open(dev, flags, 0);
851 if (mFD < 0) {
852 // FIXME errno, strerror in bionic?
853 ALOGE("Cant open device %s err=%d", dev, errno);
854 return false;
855 }
856 setPath(dev);
857 return true;
858 }
859
setPath(const char * const dev)860 inline void OvFD::setPath(const char* const dev)
861 {
862 ::strncpy(mPath, dev, utils::MAX_PATH_LEN);
863 }
864
close()865 inline bool OvFD::close()
866 {
867 int ret = 0;
868 if(valid()) {
869 ret = ::close(mFD);
870 mFD = INVAL;
871 }
872 return (ret == 0);
873 }
874
valid()875 inline bool OvFD::valid() const
876 {
877 return (mFD != INVAL);
878 }
879
getFD()880 inline int OvFD::getFD() const { return mFD; }
881
copy(int fd)882 inline void OvFD::copy(int fd) {
883 mFD = fd;
884 }
885
dump()886 inline void OvFD::dump() const
887 {
888 ALOGE("== Dump OvFD fd=%d path=%s start/end ==",
889 mFD, mPath);
890 }
891
892 //--------------- class OvFD stuff ends ---------------------
893
894 } // overlay
895
896
897 #endif // OVERLAY_UTILS_H
898