blob: f9fcca82a05fe28b60da4cb81a6d1eda7ca63c09 [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
66namespace overlay {
67
68// fwd
69class Overlay;
Saurabh Shahe012f7a2012-08-18 15:11:57 -070070class OvFD;
71
72/* helper function to open by using fbnum */
73bool open(OvFD& fd, uint32_t fbnum, const char* const dev,
74 int flags = O_RDWR);
Naseer Ahmed29a26812012-06-14 00:56:20 -070075
76namespace utils {
77struct Whf;
78struct Dim;
Naseer Ahmed29a26812012-06-14 00:56:20 -070079
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:
Naseer Ahmedf48aef62012-07-20 09:05:53 -070092* class SomeClass : utils::NoCopy {...};
Naseer Ahmed29a26812012-06-14 00:56:20 -070093*/
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
Naseer Ahmed29a26812012-06-14 00:56:20 -0700127private:
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 */
139enum { SHIFT_OUT_3D = 12,
140 SHIFT_TOT_3D = 16 };
141enum { INPUT_3D_MASK = 0xFFFF0000,
142 OUTPUT_3D_MASK = 0x0000FFFF };
143enum { BARRIER_LAND = 1,
144 BARRIER_PORT = 2 };
145
Ajay Dudanicb3da0a2012-09-07 13:37:49 -0700146/* if SurfaceFlinger process gets killed in bypass mode, In initOverlay()
147 * close all the pipes if it is opened after reboot.
148 */
149int initOverlay(void);
150
Naseer Ahmed29a26812012-06-14 00:56:20 -0700151inline uint32_t format3D(uint32_t x) { return x & 0xFF000; }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700152inline 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}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700160inline uint32_t format3DOutput(uint32_t x) {
161 return (x & 0xF000) >> SHIFT_OUT_3D; }
162inline uint32_t format3DInput(uint32_t x) { return x & 0xF0000; }
163uint32_t getColorFormat(uint32_t format);
164
165bool isHDMIConnected ();
166bool is3DTV();
167bool isPanel3D();
168bool usePanel3D();
169bool send3DInfoPacket (uint32_t fmt);
170bool enableBarrier (uint32_t orientation);
171uint32_t getS3DFormat(uint32_t fmt);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700172
Naseer Ahmed29a26812012-06-14 00:56:20 -0700173template <int CHAN>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700174bool getPositionS3D(const Whf& whf, Dim& out);
175
Naseer Ahmed29a26812012-06-14 00:56:20 -0700176template <int CHAN>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700177bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt);
178
Naseer Ahmed29a26812012-06-14 00:56:20 -0700179template <class Type>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700180void swapWidthHeight(Type& width, Type& height);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700181
182struct Dim {
183 Dim () : x(0), y(0),
184 w(0), h(0),
185 o(0) {}
186 Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h) :
187 x(_x), y(_y),
188 w(_w), h(_h) {}
189 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) {}
193 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
Naseer Ahmed29a26812012-06-14 00:56:20 -0700208 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
218struct Whf {
219 Whf() : w(0), h(0), format(0), size(0) {}
220 Whf(uint32_t wi, uint32_t he, uint32_t f) :
221 w(wi), h(he), format(f), size(0) {}
222 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;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700235 uint32_t format;
236 uint32_t size;
237};
238
Saurabh Shah56f610d2012-08-07 15:27:06 -0700239class ActionSafe {
240private:
241 ActionSafe() : mWidth(0.0f), mHeight(0.0f) { };
242 float mWidth;
243 float mHeight;
244 static ActionSafe *sActionSafe;
245public:
246 ~ActionSafe() { };
247 static ActionSafe* getInstance() {
248 if(!sActionSafe) {
249 sActionSafe = new ActionSafe();
250 }
251 return sActionSafe;
252 }
253 void setDimension(int w, int h) {
254 mWidth = (float)w;
255 mHeight = (float)h;
256 }
257 float getWidth() { return mWidth; }
258 float getHeight() { return mHeight; }
259};
260
Naseer Ahmed29a26812012-06-14 00:56:20 -0700261enum { MAX_PATH_LEN = 256 };
262
Naseer Ahmed29a26812012-06-14 00:56:20 -0700263/**
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.
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500267 * ROT_FLAG_DISABLED: Rotator not used unless required.
268 * ROT_FLAG_ENABLED: Rotator used even if not required.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700269 * */
270enum eRotFlags {
271 ROT_FLAG_DISABLED = 0,
272 ROT_FLAG_ENABLED = 1 // needed in rot
273};
274
Naseer Ahmed29a26812012-06-14 00:56:20 -0700275/* 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 */
279enum 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 * */
289enum eMdpFlags {
290 OV_MDP_FLAGS_NONE = 0,
291 OV_MDP_PIPE_SHARE = MDP_OV_PIPE_SHARE,
292 OV_MDP_DEINTERLACE = MDP_DEINTERLACE,
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700293 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,
Saurabh Shah799a3972012-09-01 12:16:12 -0700296 OV_MDP_BACKEND_COMPOSITION = MDP_BACKEND_COMPOSITION,
Saurabh Shah91a6a992012-08-20 15:25:28 -0700297 OV_MDP_BLEND_FG_PREMULT = MDP_BLEND_FG_PREMULT,
Saurabh Shah09549f62012-10-04 13:25:44 -0700298 OV_MDP_FLIP_H = MDP_FLIP_LR,
299 OV_MDP_FLIP_V = MDP_FLIP_UD,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700300};
301
Naseer Ahmed29a26812012-06-14 00:56:20 -0700302enum eZorder {
303 ZORDER_0,
304 ZORDER_1,
305 ZORDER_2,
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500306 ZORDER_3,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700307 Z_SYSTEM_ALLOC = 0xFFFF
308};
309
310enum eMdpPipeType {
311 OV_MDP_PIPE_RGB,
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500312 OV_MDP_PIPE_VG,
313 OV_MDP_PIPE_ANY, //Any
Naseer Ahmed29a26812012-06-14 00:56:20 -0700314};
315
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500316/* Used to identify destination pipes
317 */
Naseer Ahmed29a26812012-06-14 00:56:20 -0700318enum eDest {
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500319 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 */
329enum {
330 OV_LEFT_SPLIT = 0,
331 OV_RIGHT_SPLIT,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700332};
333
334/* values for copybit_set_parameter(OVERLAY_TRANSFORM) */
335enum eTransform {
336 /* No rot */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700337 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,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700342 /* rotate source image 180 degrees
343 * It is basically bit-or-ed H | V == 0x3 */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700344 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,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700353 /* rotate source image 270 degrees
354 * Basically 180 | 90 == 0x7 */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700355 OVERLAY_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700356 /* rotate invalid like in Transform.h */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700357 OVERLAY_TRANSFORM_INV = 0x80
Naseer Ahmed29a26812012-06-14 00:56:20 -0700358};
359
360// Used to consolidate pipe params
361struct PipeArgs {
362 PipeArgs() : mdpFlags(OV_MDP_FLAGS_NONE),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700363 zorder(Z_SYSTEM_ALLOC),
364 isFg(IS_FG_OFF),
365 rotFlags(ROT_FLAG_DISABLED){
366 }
367
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700368 PipeArgs(eMdpFlags f, Whf _whf,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700369 eZorder z, eIsFg fg, eRotFlags r) :
370 mdpFlags(f),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700371 whf(_whf),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700372 zorder(z),
373 isFg(fg),
374 rotFlags(r) {
375 }
376
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700377 eMdpFlags mdpFlags; // for mdp_overlay flags
Naseer Ahmed29a26812012-06-14 00:56:20 -0700378 Whf whf;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700379 eZorder zorder; // stage number
380 eIsFg isFg; // control alpha & transp
381 eRotFlags rotFlags;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700382};
383
Naseer Ahmed29a26812012-06-14 00:56:20 -0700384inline void setMdpFlags(eMdpFlags& f, eMdpFlags v) {
385 f = static_cast<eMdpFlags>(setBit(f, v));
386}
387
388inline void clearMdpFlags(eMdpFlags& f, eMdpFlags v) {
389 f = static_cast<eMdpFlags>(clrBit(f, v));
390}
391
392// fb 0/1/2
393enum { FB0, FB1, FB2 };
394
395//Panels could be categorized as primary and external
396enum { PRIMARY, EXTERNAL };
397
Naseer Ahmed29a26812012-06-14 00:56:20 -0700398// 2 for rgb0/1 double bufs
399enum { RGB_PIPE_NUM_BUFS = 2 };
400
401struct ScreenInfo {
402 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
413int getMdpFormat(int format);
414int 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*/
418int getMdpOrient(eTransform rotation);
Saurabh Shah9c876d92012-08-25 19:29:53 -0700419const char* getFormatString(int format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700420
Naseer Ahmed29a26812012-06-14 00:56:20 -0700421// Cannot use HW_OVERLAY_MAGNIFICATION_LIMIT, since at the time
422// of integration, HW_OVERLAY_MAGNIFICATION_LIMIT was a define
423enum { HW_OV_MAGNIFICATION_LIMIT = 20,
424 HW_OV_MINIFICATION_LIMIT = 8
425};
426
Naseer Ahmed29a26812012-06-14 00:56:20 -0700427template <class T>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700428inline void memset0(T& t) { ::memset(&t, 0, sizeof(T)); }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700429
430template <class T> inline void swap ( T& a, T& b )
431{
432 T c(a); a=b; b=c;
433}
434
435inline 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.
441inline 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
Naseer Ahmed29a26812012-06-14 00:56:20 -0700446enum eRotOutFmt {
447 ROT_OUT_FMT_DEFAULT,
448 ROT_OUT_FMT_Y_CRCB_H2V2
449};
450
451template <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*/
456enum {
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
468enum { 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
479inline 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:
Saurabh Shahb121e142012-08-20 17:59:13 -0700484 case MDP_Y_CRCB_H1V1:
485 case MDP_Y_CRCB_H2V1:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700486 case MDP_Y_CRCB_H2V2_TILE:
487 case MDP_Y_CBCR_H2V2_TILE:
Saurabh Shahb121e142012-08-20 17:59:13 -0700488 case MDP_Y_CR_CB_H2V2:
Saurabh Shahae1044e2012-08-21 16:03:32 -0700489 case MDP_Y_CR_CB_GH2V2:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700490 return true;
491 default:
492 return false;
493 }
494 return false;
495}
496
497inline 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
Saurabh Shah9c876d92012-08-25 19:29:53 -0700510inline const char* getFormatString(int format){
Naseer Ahmed29a26812012-06-14 00:56:20 -0700511 static const char* const formats[] = {
512 "MDP_RGB_565",
513 "MDP_XRGB_8888",
514 "MDP_Y_CBCR_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700515 "MDP_Y_CBCR_H2V2_ADRENO",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700516 "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",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700522 "MDP_Y_CRCB_H1V2",
523 "MDP_Y_CBCR_H1V2",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700524 "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",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700530 "MDP_Y_CR_CB_GH2V2",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700531 "MDP_Y_CB_CR_H2V2",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700532 "MDP_Y_CRCB_H1V1",
533 "MDP_Y_CBCR_H1V1",
534 "MDP_YCRCB_H1V1",
535 "MDP_YCBCR_H1V1",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700536 "MDP_BGR_565",
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700537 "MDP_IMGTYPE_LIMIT",
538 "MDP_RGB_BORDERFILL",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700539 "MDP_FB_FORMAT",
540 "MDP_IMGTYPE_LIMIT2"
541 };
Saurabh Shah9c876d92012-08-25 19:29:53 -0700542 if(format < 0 || format >= (int)(sizeof(formats) / sizeof(formats[0]))) {
543 ALOGE("%s wrong fmt %d", __FUNCTION__, format);
544 return "Unsupported format";
545 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700546 return formats[format];
547}
548
Naseer Ahmed29a26812012-06-14 00:56:20 -0700549inline 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
554inline void Dim::dump() const {
555 ALOGE("== Dump Dim x=%d y=%d w=%d h=%d start/end ==", x, y, w, h);
556}
557
558inline int getMdpOrient(eTransform rotation) {
559 ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, rotation);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700560 switch(rotation)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700561 {
562 case OVERLAY_TRANSFORM_0 : return 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700563 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;
Saurabh Shaha73738d2012-08-09 18:15:18 -0700566 //getMdpOrient will switch the flips if the source is 90 rotated.
567 //Clients in Android dont factor in 90 rotation while deciding flip.
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700568 case OVERLAY_TRANSFORM_ROT_90_FLIP_V:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700569 return MDP_ROT_90 | MDP_FLIP_LR;
Saurabh Shaha73738d2012-08-09 18:15:18 -0700570 case OVERLAY_TRANSFORM_ROT_90_FLIP_H:
571 return MDP_ROT_90 | MDP_FLIP_UD;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700572 case OVERLAY_TRANSFORM_ROT_180: return MDP_ROT_180;
573 case OVERLAY_TRANSFORM_ROT_270: return MDP_ROT_270;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700574 default:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700575 ALOGE("%s: invalid rotation value (value = 0x%x",
576 __FUNCTION__, rotation);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700577 }
578 return -1;
579}
580
581inline 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;
Saurabh Shahae1044e2012-08-21 16:03:32 -0700589 case MDP_Y_CR_CB_GH2V2:
590 return MDP_Y_CRCB_H2V2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700591 default:
592 return format;
593 }
594 // not reached
595 OVASSERT(false, "%s not reached", __FUNCTION__);
596 return -1;
597}
598
Naseer Ahmed29a26812012-06-14 00:56:20 -0700599
600inline uint32_t getColorFormat(uint32_t format)
601{
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700602 return (format == HAL_PIXEL_FORMAT_YV12) ?
603 format : colorFormat(format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700604}
605
606// FB0
607template <int CHAN>
608inline 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
630template <>
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500631inline Dim getPositionS3DImpl<utils::OV_RIGHT_SPLIT>(const Whf& whf)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700632{
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
652template <int CHAN>
653inline bool getPositionS3D(const Whf& whf, Dim& out) {
654 out = getPositionS3DImpl<CHAN>(whf);
655 return (out != Dim());
656}
657
658template <int CHAN>
659inline 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
678template <>
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500679inline Dim getCropS3DImpl<utils::OV_RIGHT_SPLIT>(const Dim& in, uint32_t fmt) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700680 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
698template <int CHAN>
699inline bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt)
700{
701 out = getCropS3DImpl<CHAN>(in, fmt);
702 return (out != Dim());
703}
704
705template <class Type>
706void swapWidthHeight(Type& width, Type& height) {
707 Type tmp = width;
708 width = height;
709 height = tmp;
710}
711
712inline 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
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700718inline bool openDev(OvFD& fd, int fbnum,
719 const char* const devpath, int flags) {
720 return overlay::open(fd, fbnum, devpath, flags);
721}
722
Saurabh Shahb121e142012-08-20 17:59:13 -0700723template <class T>
724inline void even_ceil(T& value) {
725 if(value & 1)
726 value++;
727}
728
729template <class T>
730inline void even_floor(T& value) {
731 if(value & 1)
732 value--;
733}
734
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500735inline 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
748inline 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
Naseer Ahmed29a26812012-06-14 00:56:20 -0700764} // namespace utils ends
765
766//--------------------Class Res stuff (namespace overlay only) -----------
767
768class Res {
769public:
770 // /dev/graphics/fb%u
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700771 static const char* const fbPath;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700772 // /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
Naseer Ahmed29a26812012-06-14 00:56:20 -0700785/*
786* Holds one FD
787* Dtor will NOT close the underlying FD.
788* That enables us to copy that object around
789* */
790class OvFD {
791public:
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;
820private:
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
833inline 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
840inline OvFD::OvFD() : mFD (INVAL) {
841 mPath[0] = 0;
842}
843
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700844inline OvFD::~OvFD() {
845 //no op since copy() can be used to share fd, in 3d cases.
846}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700847
848inline 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
860inline void OvFD::setPath(const char* const dev)
861{
862 ::strncpy(mPath, dev, utils::MAX_PATH_LEN);
863}
864
865inline 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
875inline bool OvFD::valid() const
876{
877 return (mFD != INVAL);
878}
879
880inline int OvFD::getFD() const { return mFD; }
881
882inline void OvFD::copy(int fd) {
883 mFD = fd;
884}
885
886inline 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