blob: 3b2f6d7c9715a746c7cd9b1bc9b717f1a0e8307c [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
Saurabh Shah56f610d2012-08-07 15:27:06 -07002* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
Naseer Ahmed29a26812012-06-14 00:56:20 -07003*
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 Ahmed758bfc52012-11-28 17:02:08 -050046
Naseer Ahmed29a26812012-06-14 00:56:20 -070047/*
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
Saurabh Shahcf053c62012-12-13 12:32:55 -080066#ifndef MDSS_MDP_RIGHT_MIXER
67#define MDSS_MDP_RIGHT_MIXER 0x100
68#endif
69
Naseer Ahmed29a26812012-06-14 00:56:20 -070070namespace overlay {
71
72// fwd
73class Overlay;
Saurabh Shahe012f7a2012-08-18 15:11:57 -070074class OvFD;
75
76/* helper function to open by using fbnum */
77bool open(OvFD& fd, uint32_t fbnum, const char* const dev,
78 int flags = O_RDWR);
Naseer Ahmed29a26812012-06-14 00:56:20 -070079
80namespace utils {
81struct Whf;
82struct Dim;
Naseer Ahmed29a26812012-06-14 00:56:20 -070083
84inline uint32_t setBit(uint32_t x, uint32_t mask) {
85 return (x | mask);
86}
87
88inline uint32_t clrBit(uint32_t x, uint32_t mask) {
89 return (x & ~mask);
90}
91
92/* Utility class to help avoid copying instances by making the copy ctor
93* and assignment operator private
94*
95* Usage:
Naseer Ahmedf48aef62012-07-20 09:05:53 -070096* class SomeClass : utils::NoCopy {...};
Naseer Ahmed29a26812012-06-14 00:56:20 -070097*/
98class NoCopy {
99protected:
100 NoCopy(){}
101 ~NoCopy() {}
102private:
103 NoCopy(const NoCopy&);
104 const NoCopy& operator=(const NoCopy&);
105};
106
107/*
108* Utility class to query the framebuffer info for primary display
109*
110* Usage:
111* Outside of functions:
112* utils::FrameBufferInfo* utils::FrameBufferInfo::sFBInfoInstance = 0;
113* Inside functions:
114* utils::FrameBufferInfo::getInstance()->supportTrueMirroring()
115*/
116class FrameBufferInfo {
117
118public:
119 /* ctor init */
120 explicit FrameBufferInfo();
121
122 /* Gets an instance if one does not already exist */
123 static FrameBufferInfo* getInstance();
124
125 /* Gets width of primary framebuffer */
126 int getWidth() const;
127
128 /* Gets height of primary framebuffer */
129 int getHeight() const;
130
Naseer Ahmed29a26812012-06-14 00:56:20 -0700131private:
132 int mFBWidth;
133 int mFBHeight;
134 bool mBorderFillSupported;
135 static FrameBufferInfo *sFBInfoInstance;
136};
137
138/* 3D related utils, defines etc...
139 * The compound format passed to the overlay is
140 * ABCCC where A is the input 3D format
141 * B is the output 3D format
142 * CCC is the color format e.g YCbCr420SP YCrCb420SP etc */
143enum { SHIFT_OUT_3D = 12,
144 SHIFT_TOT_3D = 16 };
145enum { INPUT_3D_MASK = 0xFFFF0000,
146 OUTPUT_3D_MASK = 0x0000FFFF };
147enum { BARRIER_LAND = 1,
148 BARRIER_PORT = 2 };
149
Ajay Dudanicb3da0a2012-09-07 13:37:49 -0700150/* if SurfaceFlinger process gets killed in bypass mode, In initOverlay()
151 * close all the pipes if it is opened after reboot.
152 */
153int initOverlay(void);
154
Naseer Ahmed29a26812012-06-14 00:56:20 -0700155inline uint32_t format3D(uint32_t x) { return x & 0xFF000; }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700156inline uint32_t colorFormat(uint32_t fmt) {
157 /*TODO enable this block only if format has interlace / 3D info in top bits.
158 if(fmt & INTERLACE_MASK) {
159 fmt = fmt ^ HAL_PIXEL_FORMAT_INTERLACE;
160 }
161 fmt = fmt & 0xFFF;*/
162 return fmt;
163}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700164inline uint32_t format3DOutput(uint32_t x) {
165 return (x & 0xF000) >> SHIFT_OUT_3D; }
166inline uint32_t format3DInput(uint32_t x) { return x & 0xF0000; }
167uint32_t getColorFormat(uint32_t format);
168
169bool isHDMIConnected ();
170bool is3DTV();
171bool isPanel3D();
172bool usePanel3D();
173bool send3DInfoPacket (uint32_t fmt);
174bool enableBarrier (uint32_t orientation);
175uint32_t getS3DFormat(uint32_t fmt);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700176
Naseer Ahmed29a26812012-06-14 00:56:20 -0700177template <int CHAN>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700178bool getPositionS3D(const Whf& whf, Dim& out);
179
Naseer Ahmed29a26812012-06-14 00:56:20 -0700180template <int CHAN>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700181bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt);
182
Naseer Ahmed29a26812012-06-14 00:56:20 -0700183template <class Type>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700184void swapWidthHeight(Type& width, Type& height);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700185
186struct Dim {
187 Dim () : x(0), y(0),
188 w(0), h(0),
189 o(0) {}
190 Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h) :
191 x(_x), y(_y),
192 w(_w), h(_h) {}
193 Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h, uint32_t _o) :
194 x(_x), y(_y),
195 w(_w), h(_h),
196 o(_o) {}
197 bool check(uint32_t _w, uint32_t _h) const {
198 return (x+w <= _w && y+h <= _h);
199
200 }
201
202 bool operator==(const Dim& d) const {
203 return d.x == x && d.y == y &&
204 d.w == w && d.h == h &&
205 d.o == o;
206 }
207
208 bool operator!=(const Dim& d) const {
209 return !operator==(d);
210 }
211
Naseer Ahmed29a26812012-06-14 00:56:20 -0700212 void dump() const;
213 uint32_t x;
214 uint32_t y;
215 uint32_t w;
216 uint32_t h;
217 uint32_t o;
218};
219
220// TODO have Whfz
221
222struct Whf {
223 Whf() : w(0), h(0), format(0), size(0) {}
224 Whf(uint32_t wi, uint32_t he, uint32_t f) :
225 w(wi), h(he), format(f), size(0) {}
226 Whf(uint32_t wi, uint32_t he, uint32_t f, uint32_t s) :
227 w(wi), h(he), format(f), size(s) {}
228 // FIXME not comparing size at the moment
229 bool operator==(const Whf& whf) const {
230 return whf.w == w && whf.h == h &&
231 whf.format == format;
232 }
233 bool operator!=(const Whf& whf) const {
234 return !operator==(whf);
235 }
236 void dump() const;
237 uint32_t w;
238 uint32_t h;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700239 uint32_t format;
240 uint32_t size;
241};
242
Saurabh Shah56f610d2012-08-07 15:27:06 -0700243class ActionSafe {
244private:
245 ActionSafe() : mWidth(0.0f), mHeight(0.0f) { };
246 float mWidth;
247 float mHeight;
248 static ActionSafe *sActionSafe;
249public:
250 ~ActionSafe() { };
251 static ActionSafe* getInstance() {
252 if(!sActionSafe) {
253 sActionSafe = new ActionSafe();
254 }
255 return sActionSafe;
256 }
257 void setDimension(int w, int h) {
258 mWidth = (float)w;
259 mHeight = (float)h;
260 }
261 float getWidth() { return mWidth; }
262 float getHeight() { return mHeight; }
263};
264
Naseer Ahmed29a26812012-06-14 00:56:20 -0700265enum { MAX_PATH_LEN = 256 };
266
Naseer Ahmed29a26812012-06-14 00:56:20 -0700267/**
268 * Rotator flags: not to be confused with orientation flags.
269 * Ususally, you want to open the rotator to make sure it is
270 * ready for business.
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500271 * ROT_FLAG_DISABLED: Rotator not used unless required.
272 * ROT_FLAG_ENABLED: Rotator used even if not required.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700273 * */
274enum eRotFlags {
275 ROT_FLAG_DISABLED = 0,
276 ROT_FLAG_ENABLED = 1 // needed in rot
277};
278
Naseer Ahmed29a26812012-06-14 00:56:20 -0700279/* The values for is_fg flag for control alpha and transp
280 * IS_FG_OFF means is_fg = 0
281 * IS_FG_SET means is_fg = 1
282 */
283enum eIsFg {
284 IS_FG_OFF = 0,
285 IS_FG_SET = 1
286};
287
288/*
289 * Various mdp flags like PIPE SHARE, DEINTERLACE etc...
290 * kernel/common/linux/msm_mdp.h
291 * INTERLACE_MASK: hardware/qcom/display/libgralloc/badger/fb_priv.h
292 * */
293enum eMdpFlags {
294 OV_MDP_FLAGS_NONE = 0,
295 OV_MDP_PIPE_SHARE = MDP_OV_PIPE_SHARE,
296 OV_MDP_DEINTERLACE = MDP_DEINTERLACE,
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700297 OV_MDP_SECURE_OVERLAY_SESSION = MDP_SECURE_OVERLAY_SESSION,
298 OV_MDP_SOURCE_ROTATED_90 = MDP_SOURCE_ROTATED_90,
299 OV_MDP_MEMORY_ID_TYPE_FB = MDP_MEMORY_ID_TYPE_FB,
Saurabh Shah799a3972012-09-01 12:16:12 -0700300 OV_MDP_BACKEND_COMPOSITION = MDP_BACKEND_COMPOSITION,
Saurabh Shah91a6a992012-08-20 15:25:28 -0700301 OV_MDP_BLEND_FG_PREMULT = MDP_BLEND_FG_PREMULT,
Saurabh Shah09549f62012-10-04 13:25:44 -0700302 OV_MDP_FLIP_H = MDP_FLIP_LR,
303 OV_MDP_FLIP_V = MDP_FLIP_UD,
Saurabh Shahcf053c62012-12-13 12:32:55 -0800304 OV_MDSS_MDP_RIGHT_MIXER = MDSS_MDP_RIGHT_MIXER,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700305};
306
Naseer Ahmed29a26812012-06-14 00:56:20 -0700307enum eZorder {
308 ZORDER_0,
309 ZORDER_1,
310 ZORDER_2,
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500311 ZORDER_3,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700312 Z_SYSTEM_ALLOC = 0xFFFF
313};
314
315enum eMdpPipeType {
316 OV_MDP_PIPE_RGB,
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500317 OV_MDP_PIPE_VG,
318 OV_MDP_PIPE_ANY, //Any
Naseer Ahmed29a26812012-06-14 00:56:20 -0700319};
320
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500321/* Used to identify destination pipes
322 */
Naseer Ahmed29a26812012-06-14 00:56:20 -0700323enum eDest {
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500324 OV_VG0 = 0,
325 OV_RGB0,
326 OV_VG1,
327 OV_RGB1,
328 OV_VG2,
329 OV_RGB2,
330 OV_INVALID,
331};
332
333/* Used when a buffer is split over 2 pipes and sent to display */
334enum {
335 OV_LEFT_SPLIT = 0,
336 OV_RIGHT_SPLIT,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700337};
338
339/* values for copybit_set_parameter(OVERLAY_TRANSFORM) */
340enum eTransform {
341 /* No rot */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700342 OVERLAY_TRANSFORM_0 = 0x0,
343 /* flip source image horizontally 0x1 */
344 OVERLAY_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H,
345 /* flip source image vertically 0x2 */
346 OVERLAY_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700347 /* rotate source image 180 degrees
348 * It is basically bit-or-ed H | V == 0x3 */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700349 OVERLAY_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
350 /* rotate source image 90 degrees 0x4 */
351 OVERLAY_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
352 /* rotate source image 90 degrees and flip horizontally 0x5 */
353 OVERLAY_TRANSFORM_ROT_90_FLIP_H = HAL_TRANSFORM_ROT_90 |
354 HAL_TRANSFORM_FLIP_H,
355 /* rotate source image 90 degrees and flip vertically 0x6 */
356 OVERLAY_TRANSFORM_ROT_90_FLIP_V = HAL_TRANSFORM_ROT_90 |
357 HAL_TRANSFORM_FLIP_V,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700358 /* rotate source image 270 degrees
359 * Basically 180 | 90 == 0x7 */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700360 OVERLAY_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700361 /* rotate invalid like in Transform.h */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700362 OVERLAY_TRANSFORM_INV = 0x80
Naseer Ahmed29a26812012-06-14 00:56:20 -0700363};
364
365// Used to consolidate pipe params
366struct PipeArgs {
367 PipeArgs() : mdpFlags(OV_MDP_FLAGS_NONE),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700368 zorder(Z_SYSTEM_ALLOC),
369 isFg(IS_FG_OFF),
370 rotFlags(ROT_FLAG_DISABLED){
371 }
372
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700373 PipeArgs(eMdpFlags f, Whf _whf,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700374 eZorder z, eIsFg fg, eRotFlags r) :
375 mdpFlags(f),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700376 whf(_whf),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700377 zorder(z),
378 isFg(fg),
379 rotFlags(r) {
380 }
381
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700382 eMdpFlags mdpFlags; // for mdp_overlay flags
Naseer Ahmed29a26812012-06-14 00:56:20 -0700383 Whf whf;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700384 eZorder zorder; // stage number
385 eIsFg isFg; // control alpha & transp
386 eRotFlags rotFlags;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700387};
388
Naseer Ahmed29a26812012-06-14 00:56:20 -0700389inline void setMdpFlags(eMdpFlags& f, eMdpFlags v) {
390 f = static_cast<eMdpFlags>(setBit(f, v));
391}
392
393inline void clearMdpFlags(eMdpFlags& f, eMdpFlags v) {
394 f = static_cast<eMdpFlags>(clrBit(f, v));
395}
396
397// fb 0/1/2
398enum { FB0, FB1, FB2 };
399
400//Panels could be categorized as primary and external
401enum { PRIMARY, EXTERNAL };
402
Naseer Ahmed29a26812012-06-14 00:56:20 -0700403// 2 for rgb0/1 double bufs
404enum { RGB_PIPE_NUM_BUFS = 2 };
405
406struct ScreenInfo {
407 ScreenInfo() : mFBWidth(0),
408 mFBHeight(0),
409 mFBbpp(0),
410 mFBystride(0) {}
411 void dump(const char* const s) const;
412 uint32_t mFBWidth;
413 uint32_t mFBHeight;
414 uint32_t mFBbpp;
415 uint32_t mFBystride;
416};
417
418int getMdpFormat(int format);
419int getRotOutFmt(uint32_t format);
420/* flip is upside down and such. V, H flip
421 * rotation is 90, 180 etc
422 * It returns MDP related enum/define that match rot+flip*/
423int getMdpOrient(eTransform rotation);
Saurabh Shah9c876d92012-08-25 19:29:53 -0700424const char* getFormatString(int format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700425
Naseer Ahmed29a26812012-06-14 00:56:20 -0700426// Cannot use HW_OVERLAY_MAGNIFICATION_LIMIT, since at the time
427// of integration, HW_OVERLAY_MAGNIFICATION_LIMIT was a define
428enum { HW_OV_MAGNIFICATION_LIMIT = 20,
429 HW_OV_MINIFICATION_LIMIT = 8
430};
431
Naseer Ahmed29a26812012-06-14 00:56:20 -0700432template <class T>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700433inline void memset0(T& t) { ::memset(&t, 0, sizeof(T)); }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700434
435template <class T> inline void swap ( T& a, T& b )
436{
437 T c(a); a=b; b=c;
438}
439
440inline int alignup(int value, int a) {
441 //if align = 0, return the value. Else, do alignment.
442 return a ? ((((value - 1) / a) + 1) * a) : value;
443}
444
445// FIXME that align should replace the upper one.
446inline int align(int value, int a) {
447 //if align = 0, return the value. Else, do alignment.
448 return a ? ((value + (a-1)) & ~(a-1)) : value;
449}
450
Naseer Ahmed29a26812012-06-14 00:56:20 -0700451enum eRotOutFmt {
452 ROT_OUT_FMT_DEFAULT,
453 ROT_OUT_FMT_Y_CRCB_H2V2
454};
455
456template <int ROT_OUT_FMT> struct RotOutFmt;
457
458// FIXME, taken from gralloc_priv.h. Need to
459// put it back as soon as overlay takes place of the old one
460/* possible formats for 3D content*/
461enum {
462 HAL_NO_3D = 0x0000,
463 HAL_3D_IN_SIDE_BY_SIDE_L_R = 0x10000,
464 HAL_3D_IN_TOP_BOTTOM = 0x20000,
465 HAL_3D_IN_INTERLEAVE = 0x40000,
466 HAL_3D_IN_SIDE_BY_SIDE_R_L = 0x80000,
467 HAL_3D_OUT_SIDE_BY_SIDE = 0x1000,
468 HAL_3D_OUT_TOP_BOTTOM = 0x2000,
469 HAL_3D_OUT_INTERLEAVE = 0x4000,
470 HAL_3D_OUT_MONOSCOPIC = 0x8000
471};
472
473enum { HAL_3D_OUT_SBS_MASK =
474 HAL_3D_OUT_SIDE_BY_SIDE >> overlay::utils::SHIFT_OUT_3D,
475 HAL_3D_OUT_TOP_BOT_MASK =
476 HAL_3D_OUT_TOP_BOTTOM >> overlay::utils::SHIFT_OUT_3D,
477 HAL_3D_OUT_INTERL_MASK =
478 HAL_3D_OUT_INTERLEAVE >> overlay::utils::SHIFT_OUT_3D,
479 HAL_3D_OUT_MONOS_MASK =
480 HAL_3D_OUT_MONOSCOPIC >> overlay::utils::SHIFT_OUT_3D
481};
482
483
484inline bool isYuv(uint32_t format) {
485 switch(format){
486 case MDP_Y_CBCR_H2V1:
487 case MDP_Y_CBCR_H2V2:
488 case MDP_Y_CRCB_H2V2:
Saurabh Shahb121e142012-08-20 17:59:13 -0700489 case MDP_Y_CRCB_H1V1:
490 case MDP_Y_CRCB_H2V1:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700491 case MDP_Y_CRCB_H2V2_TILE:
492 case MDP_Y_CBCR_H2V2_TILE:
Saurabh Shahb121e142012-08-20 17:59:13 -0700493 case MDP_Y_CR_CB_H2V2:
Saurabh Shahae1044e2012-08-21 16:03:32 -0700494 case MDP_Y_CR_CB_GH2V2:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700495 return true;
496 default:
497 return false;
498 }
499 return false;
500}
501
502inline bool isRgb(uint32_t format) {
503 switch(format) {
504 case MDP_RGBA_8888:
505 case MDP_BGRA_8888:
506 case MDP_RGBX_8888:
507 case MDP_RGB_565:
508 return true;
509 default:
510 return false;
511 }
512 return false;
513}
514
Saurabh Shah9c876d92012-08-25 19:29:53 -0700515inline const char* getFormatString(int format){
Naseer Ahmed29a26812012-06-14 00:56:20 -0700516 static const char* const formats[] = {
517 "MDP_RGB_565",
518 "MDP_XRGB_8888",
519 "MDP_Y_CBCR_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700520 "MDP_Y_CBCR_H2V2_ADRENO",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700521 "MDP_ARGB_8888",
522 "MDP_RGB_888",
523 "MDP_Y_CRCB_H2V2",
524 "MDP_YCRYCB_H2V1",
525 "MDP_Y_CRCB_H2V1",
526 "MDP_Y_CBCR_H2V1",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700527 "MDP_Y_CRCB_H1V2",
528 "MDP_Y_CBCR_H1V2",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700529 "MDP_RGBA_8888",
530 "MDP_BGRA_8888",
531 "MDP_RGBX_8888",
532 "MDP_Y_CRCB_H2V2_TILE",
533 "MDP_Y_CBCR_H2V2_TILE",
534 "MDP_Y_CR_CB_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700535 "MDP_Y_CR_CB_GH2V2",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700536 "MDP_Y_CB_CR_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700537 "MDP_Y_CRCB_H1V1",
538 "MDP_Y_CBCR_H1V1",
539 "MDP_YCRCB_H1V1",
540 "MDP_YCBCR_H1V1",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700541 "MDP_BGR_565",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700542 "MDP_IMGTYPE_LIMIT",
543 "MDP_RGB_BORDERFILL",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700544 "MDP_FB_FORMAT",
545 "MDP_IMGTYPE_LIMIT2"
546 };
Saurabh Shah9c876d92012-08-25 19:29:53 -0700547 if(format < 0 || format >= (int)(sizeof(formats) / sizeof(formats[0]))) {
548 ALOGE("%s wrong fmt %d", __FUNCTION__, format);
549 return "Unsupported format";
550 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700551 return formats[format];
552}
553
Naseer Ahmed29a26812012-06-14 00:56:20 -0700554inline void Whf::dump() const {
555 ALOGE("== Dump WHF w=%d h=%d f=%d s=%d start/end ==",
556 w, h, format, size);
557}
558
559inline void Dim::dump() const {
560 ALOGE("== Dump Dim x=%d y=%d w=%d h=%d start/end ==", x, y, w, h);
561}
562
563inline int getMdpOrient(eTransform rotation) {
564 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, rotation);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700565 switch(rotation)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700566 {
567 case OVERLAY_TRANSFORM_0 : return 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700568 case OVERLAY_TRANSFORM_FLIP_V: return MDP_FLIP_UD;
569 case OVERLAY_TRANSFORM_FLIP_H: return MDP_FLIP_LR;
570 case OVERLAY_TRANSFORM_ROT_90: return MDP_ROT_90;
Saurabh Shaha73738d2012-08-09 18:15:18 -0700571 //getMdpOrient will switch the flips if the source is 90 rotated.
572 //Clients in Android dont factor in 90 rotation while deciding flip.
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700573 case OVERLAY_TRANSFORM_ROT_90_FLIP_V:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700574 return MDP_ROT_90 | MDP_FLIP_LR;
Saurabh Shaha73738d2012-08-09 18:15:18 -0700575 case OVERLAY_TRANSFORM_ROT_90_FLIP_H:
576 return MDP_ROT_90 | MDP_FLIP_UD;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700577 case OVERLAY_TRANSFORM_ROT_180: return MDP_ROT_180;
578 case OVERLAY_TRANSFORM_ROT_270: return MDP_ROT_270;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700579 default:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700580 ALOGE("%s: invalid rotation value (value = 0x%x",
581 __FUNCTION__, rotation);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700582 }
583 return -1;
584}
585
586inline int getRotOutFmt(uint32_t format) {
587 switch (format) {
588 case MDP_Y_CRCB_H2V2_TILE:
589 return MDP_Y_CRCB_H2V2;
590 case MDP_Y_CBCR_H2V2_TILE:
591 return MDP_Y_CBCR_H2V2;
592 case MDP_Y_CB_CR_H2V2:
593 return MDP_Y_CBCR_H2V2;
Saurabh Shahae1044e2012-08-21 16:03:32 -0700594 case MDP_Y_CR_CB_GH2V2:
595 return MDP_Y_CRCB_H2V2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700596 default:
597 return format;
598 }
599 // not reached
600 OVASSERT(false, "%s not reached", __FUNCTION__);
601 return -1;
602}
603
Naseer Ahmed29a26812012-06-14 00:56:20 -0700604
605inline uint32_t getColorFormat(uint32_t format)
606{
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700607 return (format == HAL_PIXEL_FORMAT_YV12) ?
608 format : colorFormat(format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700609}
610
611// FB0
612template <int CHAN>
613inline Dim getPositionS3DImpl(const Whf& whf)
614{
615 switch (whf.format & OUTPUT_3D_MASK)
616 {
617 case HAL_3D_OUT_SBS_MASK:
618 // x, y, w, h
619 return Dim(0, 0, whf.w/2, whf.h);
620 case HAL_3D_OUT_TOP_BOT_MASK:
621 return Dim(0, 0, whf.w, whf.h/2);
622 case HAL_3D_OUT_MONOS_MASK:
623 return Dim();
624 case HAL_3D_OUT_INTERL_MASK:
625 // FIXME error?
626 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
627 return Dim();
628 default:
629 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
630 whf.format);
631 }
632 return Dim();
633}
634
635template <>
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500636inline Dim getPositionS3DImpl<utils::OV_RIGHT_SPLIT>(const Whf& whf)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700637{
638 switch (whf.format & OUTPUT_3D_MASK)
639 {
640 case HAL_3D_OUT_SBS_MASK:
641 return Dim(whf.w/2, 0, whf.w/2, whf.h);
642 case HAL_3D_OUT_TOP_BOT_MASK:
643 return Dim(0, whf.h/2, whf.w, whf.h/2);
644 case HAL_3D_OUT_MONOS_MASK:
645 return Dim(0, 0, whf.w, whf.h);
646 case HAL_3D_OUT_INTERL_MASK:
647 // FIXME error?
648 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
649 return Dim();
650 default:
651 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
652 whf.format);
653 }
654 return Dim();
655}
656
657template <int CHAN>
658inline bool getPositionS3D(const Whf& whf, Dim& out) {
659 out = getPositionS3DImpl<CHAN>(whf);
660 return (out != Dim());
661}
662
663template <int CHAN>
664inline Dim getCropS3DImpl(const Dim& in, uint32_t fmt) {
665 switch (fmt & INPUT_3D_MASK)
666 {
667 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
668 return Dim(0, 0, in.w/2, in.h);
669 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
670 return Dim(in.w/2, 0, in.w/2, in.h);
671 case HAL_3D_IN_TOP_BOTTOM:
672 return Dim(0, 0, in.w, in.h/2);
673 case HAL_3D_IN_INTERLEAVE:
674 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
675 break;
676 default:
677 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
678 break;
679 }
680 return Dim();
681}
682
683template <>
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500684inline Dim getCropS3DImpl<utils::OV_RIGHT_SPLIT>(const Dim& in, uint32_t fmt) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700685 switch (fmt & INPUT_3D_MASK)
686 {
687 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
688 return Dim(in.w/2, 0, in.w/2, in.h);
689 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
690 return Dim(0, 0, in.w/2, in.h);
691 case HAL_3D_IN_TOP_BOTTOM:
692 return Dim(0, in.h/2, in.w, in.h/2);
693 case HAL_3D_IN_INTERLEAVE:
694 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
695 break;
696 default:
697 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
698 break;
699 }
700 return Dim();
701}
702
703template <int CHAN>
704inline bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt)
705{
706 out = getCropS3DImpl<CHAN>(in, fmt);
707 return (out != Dim());
708}
709
710template <class Type>
711void swapWidthHeight(Type& width, Type& height) {
712 Type tmp = width;
713 width = height;
714 height = tmp;
715}
716
717inline void ScreenInfo::dump(const char* const s) const {
718 ALOGE("== Dump %s ScreenInfo w=%d h=%d"
719 " bpp=%d stride=%d start/end ==",
720 s, mFBWidth, mFBHeight, mFBbpp, mFBystride);
721}
722
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700723inline bool openDev(OvFD& fd, int fbnum,
724 const char* const devpath, int flags) {
725 return overlay::open(fd, fbnum, devpath, flags);
726}
727
Saurabh Shahb121e142012-08-20 17:59:13 -0700728template <class T>
729inline void even_ceil(T& value) {
730 if(value & 1)
731 value++;
732}
733
734template <class T>
735inline void even_floor(T& value) {
736 if(value & 1)
737 value--;
738}
739
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500740inline const char* getDestStr(eDest dest) {
741 switch(dest) {
742 case OV_VG0: return "VG0";
743 case OV_RGB0: return "RGB0";
744 case OV_VG1: return "VG1";
745 case OV_RGB1: return "RGB1";
746 case OV_VG2: return "VG2";
747 case OV_RGB2: return "RGB2";
748 default: return "Invalid";
749 }
750 return "Invalid";
751}
752
753inline eMdpPipeType getPipeType(eDest dest) {
754 switch(dest) {
755 case OV_VG0:
756 case OV_VG1:
757 case OV_VG2:
758 return OV_MDP_PIPE_VG;
759 case OV_RGB0:
760 case OV_RGB1:
761 case OV_RGB2:
762 return OV_MDP_PIPE_RGB;
763 default:
764 return OV_MDP_PIPE_ANY;
765 }
766 return OV_MDP_PIPE_ANY;
767}
768
Naseer Ahmed29a26812012-06-14 00:56:20 -0700769} // namespace utils ends
770
771//--------------------Class Res stuff (namespace overlay only) -----------
772
773class Res {
774public:
775 // /dev/graphics/fb%u
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700776 static const char* const fbPath;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700777 // /dev/msm_rotator
778 static const char* const rotPath;
779 // /sys/class/graphics/fb1/format_3d
780 static const char* const format3DFile;
781 // /sys/class/graphics/fb1/3d_present
782 static const char* const edid3dInfoFile;
783 // /sys/devices/platform/mipi_novatek.0/enable_3d_barrier
784 static const char* const barrierFile;
785};
786
787
788//--------------------Class OvFD stuff (namespace overlay only) -----------
789
Naseer Ahmed29a26812012-06-14 00:56:20 -0700790/*
791* Holds one FD
792* Dtor will NOT close the underlying FD.
793* That enables us to copy that object around
794* */
795class OvFD {
796public:
797 /* Ctor */
798 explicit OvFD();
799
800 /* dtor will NOT close the underlying FD */
801 ~OvFD();
802
803 /* Open fd using the path given by dev.
804 * return false in failure */
805 bool open(const char* const dev,
806 int flags = O_RDWR);
807
808 /* populate path */
809 void setPath(const char* const dev);
810
811 /* Close fd if we have a valid fd. */
812 bool close();
813
814 /* returns underlying fd.*/
815 int getFD() const;
816
817 /* returns true if fd is valid */
818 bool valid() const;
819
820 /* like operator= */
821 void copy(int fd);
822
823 /* dump the state of the instance */
824 void dump() const;
825private:
826 /* helper enum for determine valid/invalid fd */
827 enum { INVAL = -1 };
828
829 /* actual os fd */
830 int mFD;
831
832 /* path, for debugging */
833 char mPath[utils::MAX_PATH_LEN];
834};
835
836//-------------------Inlines--------------------------
837
838inline bool open(OvFD& fd, uint32_t fbnum, const char* const dev, int flags)
839{
840 char dev_name[64] = {0};
841 snprintf(dev_name, sizeof(dev_name), dev, fbnum);
842 return fd.open(dev_name, flags);
843}
844
845inline OvFD::OvFD() : mFD (INVAL) {
846 mPath[0] = 0;
847}
848
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700849inline OvFD::~OvFD() {
850 //no op since copy() can be used to share fd, in 3d cases.
851}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700852
853inline bool OvFD::open(const char* const dev, int flags)
854{
855 mFD = ::open(dev, flags, 0);
856 if (mFD < 0) {
857 // FIXME errno, strerror in bionic?
858 ALOGE("Cant open device %s err=%d", dev, errno);
859 return false;
860 }
861 setPath(dev);
862 return true;
863}
864
865inline void OvFD::setPath(const char* const dev)
866{
867 ::strncpy(mPath, dev, utils::MAX_PATH_LEN);
868}
869
870inline bool OvFD::close()
871{
872 int ret = 0;
873 if(valid()) {
874 ret = ::close(mFD);
875 mFD = INVAL;
876 }
877 return (ret == 0);
878}
879
880inline bool OvFD::valid() const
881{
882 return (mFD != INVAL);
883}
884
885inline int OvFD::getFD() const { return mFD; }
886
887inline void OvFD::copy(int fd) {
888 mFD = fd;
889}
890
891inline void OvFD::dump() const
892{
893 ALOGE("== Dump OvFD fd=%d path=%s start/end ==",
894 mFD, mPath);
895}
896
897//--------------- class OvFD stuff ends ---------------------
898
899} // overlay
900
901
902#endif // OVERLAY_UTILS_H