blob: 55f17675e6b6b21732b74e293729d95d85115827 [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
Sushil Chauhan81c4e7d2012-12-27 22:52:30 -08002* Copyright (c) 2011-2013, 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.
Duy Truong73d36df2013-02-09 20:33:23 -080013* * Neither the name of The Linux Foundation nor the names of its
Naseer Ahmed29a26812012-06-14 00:56:20 -070014* 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
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -080039#include <linux/msm_rotator.h>
Naseer Ahmed29a26812012-06-14 00:56:20 -070040#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <sys/stat.h>
44#include <sys/types.h>
45#include <utils/Log.h>
Naseer Ahmedf48aef62012-07-20 09:05:53 -070046#include "gralloc_priv.h" //for interlace
Naseer Ahmed758bfc52012-11-28 17:02:08 -050047
Sushil Chauhan4437bcb2013-03-11 10:36:15 -070048// Older platforms do not support Venus
49#ifndef VENUS_COLOR_FORMAT
Saurabh Shah5380a232013-04-10 17:13:11 -070050#define MDP_Y_CBCR_H2V2_VENUS MDP_IMGTYPE_LIMIT
Sushil Chauhan4437bcb2013-03-11 10:36:15 -070051#endif
52
Naseer Ahmed29a26812012-06-14 00:56:20 -070053/*
54*
55* Collection of utilities functions/structs/enums etc...
56*
57* */
58
59// comment that out if you want to remove asserts
60// or put it as -D in Android.mk. your choice.
61#define OVERLAY_HAS_ASSERT
62
63#ifdef OVERLAY_HAS_ASSERT
64# define OVASSERT(x, ...) if(!(x)) { ALOGE(__VA_ARGS__); abort(); }
65#else
66# define OVASSERT(x, ...) ALOGE_IF(!(x), __VA_ARGS__)
67#endif // OVERLAY_HAS_ASSERT
68
69#define DEBUG_OVERLAY 0
70#define PROFILE_OVERLAY 0
71
Saurabh Shahcf053c62012-12-13 12:32:55 -080072#ifndef MDSS_MDP_RIGHT_MIXER
73#define MDSS_MDP_RIGHT_MIXER 0x100
74#endif
75
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -080076#ifndef MDP_OV_PIPE_FORCE_DMA
77#define MDP_OV_PIPE_FORCE_DMA 0x4000
78#endif
79
Saurabh Shahd9e426d2013-08-01 13:35:31 -070080#ifndef MDSS_MDP_DUAL_PIPE
81#define MDSS_MDP_DUAL_PIPE 0x200
82#endif
83
Sushil Chauhan07a2c762013-03-06 15:36:49 -080084#define FB_DEVICE_TEMPLATE "/dev/graphics/fb%u"
Sushil Chauhan07a2c762013-03-06 15:36:49 -080085
Naseer Ahmed29a26812012-06-14 00:56:20 -070086namespace overlay {
87
88// fwd
89class Overlay;
Saurabh Shahe012f7a2012-08-18 15:11:57 -070090class OvFD;
91
92/* helper function to open by using fbnum */
93bool open(OvFD& fd, uint32_t fbnum, const char* const dev,
94 int flags = O_RDWR);
Naseer Ahmed29a26812012-06-14 00:56:20 -070095
96namespace utils {
97struct Whf;
98struct Dim;
Naseer Ahmed29a26812012-06-14 00:56:20 -070099
100inline uint32_t setBit(uint32_t x, uint32_t mask) {
101 return (x | mask);
102}
103
104inline uint32_t clrBit(uint32_t x, uint32_t mask) {
105 return (x & ~mask);
106}
107
108/* Utility class to help avoid copying instances by making the copy ctor
109* and assignment operator private
110*
111* Usage:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700112* class SomeClass : utils::NoCopy {...};
Naseer Ahmed29a26812012-06-14 00:56:20 -0700113*/
114class NoCopy {
115protected:
116 NoCopy(){}
117 ~NoCopy() {}
118private:
119 NoCopy(const NoCopy&);
120 const NoCopy& operator=(const NoCopy&);
121};
122
Naseer Ahmed29a26812012-06-14 00:56:20 -0700123
124/* 3D related utils, defines etc...
125 * The compound format passed to the overlay is
126 * ABCCC where A is the input 3D format
127 * B is the output 3D format
128 * CCC is the color format e.g YCbCr420SP YCrCb420SP etc */
129enum { SHIFT_OUT_3D = 12,
130 SHIFT_TOT_3D = 16 };
131enum { INPUT_3D_MASK = 0xFFFF0000,
132 OUTPUT_3D_MASK = 0x0000FFFF };
133enum { BARRIER_LAND = 1,
134 BARRIER_PORT = 2 };
135
136inline uint32_t format3D(uint32_t x) { return x & 0xFF000; }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700137inline uint32_t format3DOutput(uint32_t x) {
138 return (x & 0xF000) >> SHIFT_OUT_3D; }
139inline uint32_t format3DInput(uint32_t x) { return x & 0xF0000; }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700140
141bool isHDMIConnected ();
142bool is3DTV();
143bool isPanel3D();
144bool usePanel3D();
145bool send3DInfoPacket (uint32_t fmt);
146bool enableBarrier (uint32_t orientation);
147uint32_t getS3DFormat(uint32_t fmt);
Sushil Chauhanb4d184f2013-02-11 16:13:10 -0800148bool isMdssRotator();
Sushil Chauhan1cac8152013-05-08 15:53:51 -0700149void normalizeCrop(uint32_t& xy, uint32_t& wh);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700150
Naseer Ahmed29a26812012-06-14 00:56:20 -0700151template <int CHAN>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700152bool getPositionS3D(const Whf& whf, Dim& out);
153
Naseer Ahmed29a26812012-06-14 00:56:20 -0700154template <int CHAN>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700155bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt);
156
Naseer Ahmed29a26812012-06-14 00:56:20 -0700157template <class Type>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700158void swapWidthHeight(Type& width, Type& height);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700159
160struct Dim {
161 Dim () : x(0), y(0),
162 w(0), h(0),
163 o(0) {}
164 Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h) :
165 x(_x), y(_y),
166 w(_w), h(_h) {}
167 Dim(uint32_t _x, uint32_t _y, uint32_t _w, uint32_t _h, uint32_t _o) :
168 x(_x), y(_y),
169 w(_w), h(_h),
170 o(_o) {}
171 bool check(uint32_t _w, uint32_t _h) const {
172 return (x+w <= _w && y+h <= _h);
173
174 }
175
176 bool operator==(const Dim& d) const {
177 return d.x == x && d.y == y &&
178 d.w == w && d.h == h &&
179 d.o == o;
180 }
181
182 bool operator!=(const Dim& d) const {
183 return !operator==(d);
184 }
185
Naseer Ahmed29a26812012-06-14 00:56:20 -0700186 void dump() const;
187 uint32_t x;
188 uint32_t y;
189 uint32_t w;
190 uint32_t h;
191 uint32_t o;
192};
193
194// TODO have Whfz
195
196struct Whf {
197 Whf() : w(0), h(0), format(0), size(0) {}
198 Whf(uint32_t wi, uint32_t he, uint32_t f) :
199 w(wi), h(he), format(f), size(0) {}
200 Whf(uint32_t wi, uint32_t he, uint32_t f, uint32_t s) :
201 w(wi), h(he), format(f), size(s) {}
202 // FIXME not comparing size at the moment
203 bool operator==(const Whf& whf) const {
204 return whf.w == w && whf.h == h &&
205 whf.format == format;
206 }
207 bool operator!=(const Whf& whf) const {
208 return !operator==(whf);
209 }
210 void dump() const;
211 uint32_t w;
212 uint32_t h;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700213 uint32_t format;
214 uint32_t size;
215};
216
217enum { MAX_PATH_LEN = 256 };
218
Naseer Ahmed522ce662013-03-18 20:14:05 -0400219enum { DEFAULT_PLANE_ALPHA = 0xFF };
220
Naseer Ahmed29a26812012-06-14 00:56:20 -0700221/**
222 * Rotator flags: not to be confused with orientation flags.
Saurabh Shahacf10202013-02-26 10:15:15 -0800223 * Usually, you want to open the rotator to make sure it is
Naseer Ahmed29a26812012-06-14 00:56:20 -0700224 * ready for business.
Naseer Ahmed29a26812012-06-14 00:56:20 -0700225 * */
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800226 enum eRotFlags {
227 ROT_FLAGS_NONE = 0,
228 //Use rotator for 0 rotation. It is used anyway for others.
229 ROT_0_ENABLED = 1 << 0,
230 //Enable rotator downscale optimization for hardware bugs not handled in
231 //driver. If downscale optimizatation is required,
232 //then rotator will be used even if its 0 rotation case.
233 ROT_DOWNSCALE_ENABLED = 1 << 1,
Saurabh Shahacf10202013-02-26 10:15:15 -0800234 ROT_PREROTATED = 1 << 2,
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800235};
236
237enum eRotDownscale {
238 ROT_DS_NONE = 0,
239 ROT_DS_HALF = 1,
240 ROT_DS_FOURTH = 2,
241 ROT_DS_EIGHTH = 3,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700242};
243
Naseer Ahmed29a26812012-06-14 00:56:20 -0700244/* The values for is_fg flag for control alpha and transp
245 * IS_FG_OFF means is_fg = 0
246 * IS_FG_SET means is_fg = 1
247 */
248enum eIsFg {
249 IS_FG_OFF = 0,
250 IS_FG_SET = 1
251};
252
253/*
254 * Various mdp flags like PIPE SHARE, DEINTERLACE etc...
255 * kernel/common/linux/msm_mdp.h
256 * INTERLACE_MASK: hardware/qcom/display/libgralloc/badger/fb_priv.h
257 * */
258enum eMdpFlags {
259 OV_MDP_FLAGS_NONE = 0,
260 OV_MDP_PIPE_SHARE = MDP_OV_PIPE_SHARE,
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800261 OV_MDP_PIPE_FORCE_DMA = MDP_OV_PIPE_FORCE_DMA,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700262 OV_MDP_DEINTERLACE = MDP_DEINTERLACE,
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700263 OV_MDP_SECURE_OVERLAY_SESSION = MDP_SECURE_OVERLAY_SESSION,
Ramkumar Radhakrishnanba713382013-08-30 18:41:07 -0700264 OV_MDP_SECURE_DISPLAY_OVERLAY_SESSION = MDP_SECURE_DISPLAY_OVERLAY_SESSION,
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700265 OV_MDP_SOURCE_ROTATED_90 = MDP_SOURCE_ROTATED_90,
Saurabh Shah799a3972012-09-01 12:16:12 -0700266 OV_MDP_BACKEND_COMPOSITION = MDP_BACKEND_COMPOSITION,
Saurabh Shah91a6a992012-08-20 15:25:28 -0700267 OV_MDP_BLEND_FG_PREMULT = MDP_BLEND_FG_PREMULT,
Saurabh Shah09549f62012-10-04 13:25:44 -0700268 OV_MDP_FLIP_H = MDP_FLIP_LR,
269 OV_MDP_FLIP_V = MDP_FLIP_UD,
Saurabh Shahcf053c62012-12-13 12:32:55 -0800270 OV_MDSS_MDP_RIGHT_MIXER = MDSS_MDP_RIGHT_MIXER,
Saurabh Shah5daeee52013-01-23 16:52:26 +0800271 OV_MDP_PP_EN = MDP_OVERLAY_PP_CFG_EN,
Sushil Chauhanbab187a2013-01-30 17:44:15 -0800272 OV_MDSS_MDP_BWC_EN = MDP_BWC_EN,
Saurabh Shahd9e426d2013-08-01 13:35:31 -0700273 OV_MDSS_MDP_DUAL_PIPE = MDSS_MDP_DUAL_PIPE,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700274};
275
Naseer Ahmed29a26812012-06-14 00:56:20 -0700276enum eZorder {
Saurabh Shahacf10202013-02-26 10:15:15 -0800277 ZORDER_0 = 0,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700278 ZORDER_1,
279 ZORDER_2,
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500280 ZORDER_3,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700281 Z_SYSTEM_ALLOC = 0xFFFF
282};
283
284enum eMdpPipeType {
Sushil Chauhan07a2c762013-03-06 15:36:49 -0800285 OV_MDP_PIPE_RGB = 0,
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500286 OV_MDP_PIPE_VG,
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800287 OV_MDP_PIPE_DMA,
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500288 OV_MDP_PIPE_ANY, //Any
Naseer Ahmed29a26812012-06-14 00:56:20 -0700289};
290
Sushil Chauhan07a2c762013-03-06 15:36:49 -0800291// Identify destination pipes
292// TODO Names useless, replace with int and change all interfaces
Naseer Ahmed29a26812012-06-14 00:56:20 -0700293enum eDest {
Sushil Chauhan07a2c762013-03-06 15:36:49 -0800294 OV_P0 = 0,
295 OV_P1,
296 OV_P2,
297 OV_P3,
298 OV_P4,
299 OV_P5,
300 OV_P6,
301 OV_P7,
302 OV_P8,
303 OV_P9,
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500304 OV_INVALID,
Sushil Chauhan07a2c762013-03-06 15:36:49 -0800305 OV_MAX = OV_INVALID,
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500306};
307
308/* Used when a buffer is split over 2 pipes and sent to display */
309enum {
310 OV_LEFT_SPLIT = 0,
311 OV_RIGHT_SPLIT,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700312};
313
314/* values for copybit_set_parameter(OVERLAY_TRANSFORM) */
315enum eTransform {
316 /* No rot */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700317 OVERLAY_TRANSFORM_0 = 0x0,
318 /* flip source image horizontally 0x1 */
319 OVERLAY_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H,
320 /* flip source image vertically 0x2 */
321 OVERLAY_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700322 /* rotate source image 180 degrees
323 * It is basically bit-or-ed H | V == 0x3 */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700324 OVERLAY_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
325 /* rotate source image 90 degrees 0x4 */
326 OVERLAY_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
327 /* rotate source image 90 degrees and flip horizontally 0x5 */
328 OVERLAY_TRANSFORM_ROT_90_FLIP_H = HAL_TRANSFORM_ROT_90 |
329 HAL_TRANSFORM_FLIP_H,
330 /* rotate source image 90 degrees and flip vertically 0x6 */
331 OVERLAY_TRANSFORM_ROT_90_FLIP_V = HAL_TRANSFORM_ROT_90 |
332 HAL_TRANSFORM_FLIP_V,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700333 /* rotate source image 270 degrees
334 * Basically 180 | 90 == 0x7 */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700335 OVERLAY_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700336 /* rotate invalid like in Transform.h */
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700337 OVERLAY_TRANSFORM_INV = 0x80
Naseer Ahmed29a26812012-06-14 00:56:20 -0700338};
339
Naseer Ahmed522ce662013-03-18 20:14:05 -0400340enum eBlending {
341 OVERLAY_BLENDING_UNDEFINED = 0x0,
342 /* No blending */
343 OVERLAY_BLENDING_OPAQUE,
344 /* src.rgb + dst.rgb*(1-src_alpha) */
345 OVERLAY_BLENDING_PREMULT,
346 /* src.rgb * src_alpha + dst.rgb (1 - src_alpha) */
347 OVERLAY_BLENDING_COVERAGE,
348};
349
Naseer Ahmed29a26812012-06-14 00:56:20 -0700350// Used to consolidate pipe params
351struct PipeArgs {
352 PipeArgs() : mdpFlags(OV_MDP_FLAGS_NONE),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700353 zorder(Z_SYSTEM_ALLOC),
354 isFg(IS_FG_OFF),
Naseer Ahmed522ce662013-03-18 20:14:05 -0400355 rotFlags(ROT_FLAGS_NONE),
356 planeAlpha(DEFAULT_PLANE_ALPHA),
357 blending(OVERLAY_BLENDING_COVERAGE){
Naseer Ahmed29a26812012-06-14 00:56:20 -0700358 }
359
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700360 PipeArgs(eMdpFlags f, Whf _whf,
Naseer Ahmed522ce662013-03-18 20:14:05 -0400361 eZorder z, eIsFg fg, eRotFlags r,
362 int pA = DEFAULT_PLANE_ALPHA, eBlending b = OVERLAY_BLENDING_COVERAGE) :
Naseer Ahmed29a26812012-06-14 00:56:20 -0700363 mdpFlags(f),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700364 whf(_whf),
Naseer Ahmed29a26812012-06-14 00:56:20 -0700365 zorder(z),
366 isFg(fg),
Naseer Ahmed522ce662013-03-18 20:14:05 -0400367 rotFlags(r),
368 planeAlpha(pA),
369 blending(b){
Naseer Ahmed29a26812012-06-14 00:56:20 -0700370 }
371
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700372 eMdpFlags mdpFlags; // for mdp_overlay flags
Naseer Ahmed29a26812012-06-14 00:56:20 -0700373 Whf whf;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700374 eZorder zorder; // stage number
375 eIsFg isFg; // control alpha & transp
376 eRotFlags rotFlags;
Naseer Ahmed522ce662013-03-18 20:14:05 -0400377 int planeAlpha;
378 eBlending blending;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700379};
380
Sushil Chauhan07a2c762013-03-06 15:36:49 -0800381// Cannot use HW_OVERLAY_MAGNIFICATION_LIMIT, since at the time
382// of integration, HW_OVERLAY_MAGNIFICATION_LIMIT was a define
383enum { HW_OV_MAGNIFICATION_LIMIT = 20,
384 HW_OV_MINIFICATION_LIMIT = 8
385};
386
Naseer Ahmed29a26812012-06-14 00:56:20 -0700387inline void setMdpFlags(eMdpFlags& f, eMdpFlags v) {
388 f = static_cast<eMdpFlags>(setBit(f, v));
389}
390
391inline void clearMdpFlags(eMdpFlags& f, eMdpFlags v) {
392 f = static_cast<eMdpFlags>(clrBit(f, v));
393}
394
Naseer Ahmed29a26812012-06-14 00:56:20 -0700395enum { FB0, FB1, FB2 };
396
Naseer Ahmed29a26812012-06-14 00:56:20 -0700397struct ScreenInfo {
398 ScreenInfo() : mFBWidth(0),
399 mFBHeight(0),
400 mFBbpp(0),
401 mFBystride(0) {}
402 void dump(const char* const s) const;
403 uint32_t mFBWidth;
404 uint32_t mFBHeight;
405 uint32_t mFBbpp;
406 uint32_t mFBystride;
407};
408
409int getMdpFormat(int format);
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800410int getHALFormat(int mdpFormat);
Saurabh Shahacf10202013-02-26 10:15:15 -0800411int getDownscaleFactor(const int& src_w, const int& src_h,
412 const int& dst_w, const int& dst_h);
Saurabh Shah1a03d482013-05-29 13:44:20 -0700413void getDecimationFactor(const int& src_w, const int& src_h,
414 const int& dst_w, const int& dst_h, float& horDscale,
415 float& verDscale);
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800416
Naseer Ahmed29a26812012-06-14 00:56:20 -0700417/* flip is upside down and such. V, H flip
418 * rotation is 90, 180 etc
419 * It returns MDP related enum/define that match rot+flip*/
420int getMdpOrient(eTransform rotation);
Saurabh Shah9c876d92012-08-25 19:29:53 -0700421const char* getFormatString(int format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700422
Naseer Ahmed29a26812012-06-14 00:56:20 -0700423template <class T>
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700424inline void memset0(T& t) { ::memset(&t, 0, sizeof(T)); }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700425
426template <class T> inline void swap ( T& a, T& b )
427{
428 T c(a); a=b; b=c;
429}
430
431inline int alignup(int value, int a) {
432 //if align = 0, return the value. Else, do alignment.
433 return a ? ((((value - 1) / a) + 1) * a) : value;
434}
435
Ramkumar Radhakrishnan288f8c72013-01-15 11:37:54 -0800436inline int aligndown(int value, int a) {
437 //if align = 0, return the value. Else, do alignment.
438 return a ? ((value) & ~(a-1)) : value;
439}
440
Naseer Ahmed29a26812012-06-14 00:56:20 -0700441// FIXME that align should replace the upper one.
442inline int align(int value, int a) {
443 //if align = 0, return the value. Else, do alignment.
444 return a ? ((value + (a-1)) & ~(a-1)) : value;
445}
446
Naseer Ahmed29a26812012-06-14 00:56:20 -0700447enum eRotOutFmt {
448 ROT_OUT_FMT_DEFAULT,
449 ROT_OUT_FMT_Y_CRCB_H2V2
450};
451
452template <int ROT_OUT_FMT> struct RotOutFmt;
453
454// FIXME, taken from gralloc_priv.h. Need to
455// put it back as soon as overlay takes place of the old one
456/* possible formats for 3D content*/
457enum {
458 HAL_NO_3D = 0x0000,
459 HAL_3D_IN_SIDE_BY_SIDE_L_R = 0x10000,
460 HAL_3D_IN_TOP_BOTTOM = 0x20000,
461 HAL_3D_IN_INTERLEAVE = 0x40000,
462 HAL_3D_IN_SIDE_BY_SIDE_R_L = 0x80000,
463 HAL_3D_OUT_SIDE_BY_SIDE = 0x1000,
464 HAL_3D_OUT_TOP_BOTTOM = 0x2000,
465 HAL_3D_OUT_INTERLEAVE = 0x4000,
466 HAL_3D_OUT_MONOSCOPIC = 0x8000
467};
468
469enum { HAL_3D_OUT_SBS_MASK =
470 HAL_3D_OUT_SIDE_BY_SIDE >> overlay::utils::SHIFT_OUT_3D,
471 HAL_3D_OUT_TOP_BOT_MASK =
472 HAL_3D_OUT_TOP_BOTTOM >> overlay::utils::SHIFT_OUT_3D,
473 HAL_3D_OUT_INTERL_MASK =
474 HAL_3D_OUT_INTERLEAVE >> overlay::utils::SHIFT_OUT_3D,
475 HAL_3D_OUT_MONOS_MASK =
476 HAL_3D_OUT_MONOSCOPIC >> overlay::utils::SHIFT_OUT_3D
477};
478
479
480inline bool isYuv(uint32_t format) {
481 switch(format){
482 case MDP_Y_CBCR_H2V1:
483 case MDP_Y_CBCR_H2V2:
484 case MDP_Y_CRCB_H2V2:
Saurabh Shahb121e142012-08-20 17:59:13 -0700485 case MDP_Y_CRCB_H1V1:
486 case MDP_Y_CRCB_H2V1:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700487 case MDP_Y_CRCB_H2V2_TILE:
488 case MDP_Y_CBCR_H2V2_TILE:
Saurabh Shahb121e142012-08-20 17:59:13 -0700489 case MDP_Y_CR_CB_H2V2:
Saurabh Shahae1044e2012-08-21 16:03:32 -0700490 case MDP_Y_CR_CB_GH2V2:
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700491 case MDP_Y_CBCR_H2V2_VENUS:
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700492 case MDP_YCBYCR_H2V1:
493 case MDP_YCRYCB_H2V1:
Naseer Ahmed29a26812012-06-14 00:56:20 -0700494 return true;
495 default:
496 return false;
497 }
498 return false;
499}
500
501inline bool isRgb(uint32_t format) {
502 switch(format) {
503 case MDP_RGBA_8888:
504 case MDP_BGRA_8888:
505 case MDP_RGBX_8888:
506 case MDP_RGB_565:
507 return true;
508 default:
509 return false;
510 }
511 return false;
512}
513
Saurabh Shah9c876d92012-08-25 19:29:53 -0700514inline const char* getFormatString(int format){
Saurabh Shah5380a232013-04-10 17:13:11 -0700515 #define STR(f) #f;
516 static const char* formats[MDP_IMGTYPE_LIMIT + 1] = {0};
517 formats[MDP_RGB_565] = STR(MDP_RGB_565);
518 formats[MDP_XRGB_8888] = STR(MDP_XRGB_8888);
519 formats[MDP_Y_CBCR_H2V2] = STR(MDP_Y_CBCR_H2V2);
520 formats[MDP_Y_CBCR_H2V2_ADRENO] = STR(MDP_Y_CBCR_H2V2_ADRENO);
521 formats[MDP_ARGB_8888] = STR(MDP_ARGB_8888);
522 formats[MDP_RGB_888] = STR(MDP_RGB_888);
523 formats[MDP_Y_CRCB_H2V2] = STR(MDP_Y_CRCB_H2V2);
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700524 formats[MDP_YCBYCR_H2V1] = STR(MDP_YCBYCR_H2V1);
Saurabh Shah5380a232013-04-10 17:13:11 -0700525 formats[MDP_YCRYCB_H2V1] = STR(MDP_YCRYCB_H2V1);
526 formats[MDP_CBYCRY_H2V1] = STR(MDP_CBYCRY_H2V1);
527 formats[MDP_Y_CRCB_H2V1] = STR(MDP_Y_CRCB_H2V1);
528 formats[MDP_Y_CBCR_H2V1] = STR(MDP_Y_CBCR_H2V1);
529 formats[MDP_Y_CRCB_H1V2] = STR(MDP_Y_CRCB_H1V2);
530 formats[MDP_Y_CBCR_H1V2] = STR(MDP_Y_CBCR_H1V2);
531 formats[MDP_RGBA_8888] = STR(MDP_RGBA_8888);
532 formats[MDP_BGRA_8888] = STR(MDP_BGRA_8888);
533 formats[MDP_RGBX_8888] = STR(MDP_RGBX_8888);
534 formats[MDP_Y_CRCB_H2V2_TILE] = STR(MDP_Y_CRCB_H2V2_TILE);
535 formats[MDP_Y_CBCR_H2V2_TILE] = STR(MDP_Y_CBCR_H2V2_TILE);
536 formats[MDP_Y_CR_CB_H2V2] = STR(MDP_Y_CR_CB_H2V2);
537 formats[MDP_Y_CR_CB_GH2V2] = STR(MDP_Y_CR_CB_GH2V2);
538 formats[MDP_Y_CB_CR_H2V2] = STR(MDP_Y_CB_CR_H2V2);
539 formats[MDP_Y_CRCB_H1V1] = STR(MDP_Y_CRCB_H1V1);
540 formats[MDP_Y_CBCR_H1V1] = STR(MDP_Y_CBCR_H1V1);
541 formats[MDP_YCRCB_H1V1] = STR(MDP_YCRCB_H1V1);
542 formats[MDP_YCBCR_H1V1] = STR(MDP_YCBCR_H1V1);
543 formats[MDP_BGR_565] = STR(MDP_BGR_565);
544 formats[MDP_BGR_888] = STR(MDP_BGR_888);
545 formats[MDP_Y_CBCR_H2V2_VENUS] = STR(MDP_Y_CBCR_H2V2_VENUS);
546 formats[MDP_BGRX_8888] = STR(MDP_BGRX_8888);
547 formats[MDP_IMGTYPE_LIMIT] = STR(MDP_IMGTYPE_LIMIT);
548
549 if(format < 0 || format >= MDP_IMGTYPE_LIMIT) {
Saurabh Shah9c876d92012-08-25 19:29:53 -0700550 ALOGE("%s wrong fmt %d", __FUNCTION__, format);
551 return "Unsupported format";
552 }
Saurabh Shah5380a232013-04-10 17:13:11 -0700553 if(formats[format] == 0) {
554 ALOGE("%s: table missing format %d from header", __FUNCTION__, format);
555 return "";
556 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700557 return formats[format];
558}
559
Naseer Ahmed29a26812012-06-14 00:56:20 -0700560inline void Whf::dump() const {
561 ALOGE("== Dump WHF w=%d h=%d f=%d s=%d start/end ==",
562 w, h, format, size);
563}
564
565inline void Dim::dump() const {
566 ALOGE("== Dump Dim x=%d y=%d w=%d h=%d start/end ==", x, y, w, h);
567}
568
Naseer Ahmed29a26812012-06-14 00:56:20 -0700569// FB0
570template <int CHAN>
571inline Dim getPositionS3DImpl(const Whf& whf)
572{
573 switch (whf.format & OUTPUT_3D_MASK)
574 {
575 case HAL_3D_OUT_SBS_MASK:
576 // x, y, w, h
577 return Dim(0, 0, whf.w/2, whf.h);
578 case HAL_3D_OUT_TOP_BOT_MASK:
579 return Dim(0, 0, whf.w, whf.h/2);
580 case HAL_3D_OUT_MONOS_MASK:
581 return Dim();
582 case HAL_3D_OUT_INTERL_MASK:
583 // FIXME error?
584 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
585 return Dim();
586 default:
587 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
588 whf.format);
589 }
590 return Dim();
591}
592
593template <>
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500594inline Dim getPositionS3DImpl<utils::OV_RIGHT_SPLIT>(const Whf& whf)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700595{
596 switch (whf.format & OUTPUT_3D_MASK)
597 {
598 case HAL_3D_OUT_SBS_MASK:
599 return Dim(whf.w/2, 0, whf.w/2, whf.h);
600 case HAL_3D_OUT_TOP_BOT_MASK:
601 return Dim(0, whf.h/2, whf.w, whf.h/2);
602 case HAL_3D_OUT_MONOS_MASK:
603 return Dim(0, 0, whf.w, whf.h);
604 case HAL_3D_OUT_INTERL_MASK:
605 // FIXME error?
606 ALOGE("%s HAL_3D_OUT_INTERLEAVE_MASK", __FUNCTION__);
607 return Dim();
608 default:
609 ALOGE("%s Unsupported 3D output format %d", __FUNCTION__,
610 whf.format);
611 }
612 return Dim();
613}
614
615template <int CHAN>
616inline bool getPositionS3D(const Whf& whf, Dim& out) {
617 out = getPositionS3DImpl<CHAN>(whf);
618 return (out != Dim());
619}
620
621template <int CHAN>
622inline Dim getCropS3DImpl(const Dim& in, uint32_t fmt) {
623 switch (fmt & INPUT_3D_MASK)
624 {
625 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
626 return Dim(0, 0, in.w/2, in.h);
627 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
628 return Dim(in.w/2, 0, in.w/2, in.h);
629 case HAL_3D_IN_TOP_BOTTOM:
630 return Dim(0, 0, in.w, in.h/2);
631 case HAL_3D_IN_INTERLEAVE:
632 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
633 break;
634 default:
635 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
636 break;
637 }
638 return Dim();
639}
640
641template <>
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500642inline Dim getCropS3DImpl<utils::OV_RIGHT_SPLIT>(const Dim& in, uint32_t fmt) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700643 switch (fmt & INPUT_3D_MASK)
644 {
645 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
646 return Dim(in.w/2, 0, in.w/2, in.h);
647 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
648 return Dim(0, 0, in.w/2, in.h);
649 case HAL_3D_IN_TOP_BOTTOM:
650 return Dim(0, in.h/2, in.w, in.h/2);
651 case HAL_3D_IN_INTERLEAVE:
652 ALOGE("%s HAL_3D_IN_INTERLEAVE", __FUNCTION__);
653 break;
654 default:
655 ALOGE("%s Unsupported 3D format %d", __FUNCTION__, fmt);
656 break;
657 }
658 return Dim();
659}
660
661template <int CHAN>
662inline bool getCropS3D(const Dim& in, Dim& out, uint32_t fmt)
663{
664 out = getCropS3DImpl<CHAN>(in, fmt);
665 return (out != Dim());
666}
667
668template <class Type>
669void swapWidthHeight(Type& width, Type& height) {
670 Type tmp = width;
671 width = height;
672 height = tmp;
673}
674
675inline void ScreenInfo::dump(const char* const s) const {
676 ALOGE("== Dump %s ScreenInfo w=%d h=%d"
677 " bpp=%d stride=%d start/end ==",
678 s, mFBWidth, mFBHeight, mFBbpp, mFBystride);
679}
680
Saurabh Shahe012f7a2012-08-18 15:11:57 -0700681inline bool openDev(OvFD& fd, int fbnum,
682 const char* const devpath, int flags) {
683 return overlay::open(fd, fbnum, devpath, flags);
684}
685
Saurabh Shahb121e142012-08-20 17:59:13 -0700686template <class T>
687inline void even_ceil(T& value) {
688 if(value & 1)
689 value++;
690}
691
692template <class T>
693inline void even_floor(T& value) {
694 if(value & 1)
695 value--;
696}
697
Saurabh Shah76fd6552013-03-14 14:45:38 -0700698void preRotateSource(const eTransform& tr, Whf& whf, Dim& srcCrop);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800699void getDump(char *buf, size_t len, const char *prefix, const mdp_overlay& ov);
700void getDump(char *buf, size_t len, const char *prefix, const msmfb_img& ov);
701void getDump(char *buf, size_t len, const char *prefix, const mdp_rect& ov);
702void getDump(char *buf, size_t len, const char *prefix,
703 const msmfb_overlay_data& ov);
704void getDump(char *buf, size_t len, const char *prefix, const msmfb_data& ov);
705void getDump(char *buf, size_t len, const char *prefix,
706 const msm_rotator_img_info& ov);
707void getDump(char *buf, size_t len, const char *prefix,
708 const msm_rotator_data_info& ov);
709
Naseer Ahmed29a26812012-06-14 00:56:20 -0700710} // namespace utils ends
711
712//--------------------Class Res stuff (namespace overlay only) -----------
713
714class Res {
715public:
716 // /dev/graphics/fb%u
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700717 static const char* const fbPath;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700718 // /dev/msm_rotator
719 static const char* const rotPath;
720 // /sys/class/graphics/fb1/format_3d
721 static const char* const format3DFile;
722 // /sys/class/graphics/fb1/3d_present
723 static const char* const edid3dInfoFile;
724 // /sys/devices/platform/mipi_novatek.0/enable_3d_barrier
725 static const char* const barrierFile;
726};
727
728
729//--------------------Class OvFD stuff (namespace overlay only) -----------
730
Naseer Ahmed29a26812012-06-14 00:56:20 -0700731/*
732* Holds one FD
733* Dtor will NOT close the underlying FD.
734* That enables us to copy that object around
735* */
736class OvFD {
737public:
738 /* Ctor */
739 explicit OvFD();
740
741 /* dtor will NOT close the underlying FD */
742 ~OvFD();
743
744 /* Open fd using the path given by dev.
745 * return false in failure */
746 bool open(const char* const dev,
747 int flags = O_RDWR);
748
749 /* populate path */
750 void setPath(const char* const dev);
751
752 /* Close fd if we have a valid fd. */
753 bool close();
754
755 /* returns underlying fd.*/
756 int getFD() const;
757
758 /* returns true if fd is valid */
759 bool valid() const;
760
761 /* like operator= */
762 void copy(int fd);
763
764 /* dump the state of the instance */
765 void dump() const;
766private:
767 /* helper enum for determine valid/invalid fd */
768 enum { INVAL = -1 };
769
770 /* actual os fd */
771 int mFD;
772
773 /* path, for debugging */
774 char mPath[utils::MAX_PATH_LEN];
775};
776
777//-------------------Inlines--------------------------
778
779inline bool open(OvFD& fd, uint32_t fbnum, const char* const dev, int flags)
780{
781 char dev_name[64] = {0};
782 snprintf(dev_name, sizeof(dev_name), dev, fbnum);
783 return fd.open(dev_name, flags);
784}
785
786inline OvFD::OvFD() : mFD (INVAL) {
787 mPath[0] = 0;
788}
789
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700790inline OvFD::~OvFD() {
791 //no op since copy() can be used to share fd, in 3d cases.
792}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700793
794inline bool OvFD::open(const char* const dev, int flags)
795{
796 mFD = ::open(dev, flags, 0);
797 if (mFD < 0) {
798 // FIXME errno, strerror in bionic?
799 ALOGE("Cant open device %s err=%d", dev, errno);
800 return false;
801 }
802 setPath(dev);
803 return true;
804}
805
806inline void OvFD::setPath(const char* const dev)
807{
808 ::strncpy(mPath, dev, utils::MAX_PATH_LEN);
809}
810
811inline bool OvFD::close()
812{
813 int ret = 0;
814 if(valid()) {
815 ret = ::close(mFD);
816 mFD = INVAL;
817 }
818 return (ret == 0);
819}
820
821inline bool OvFD::valid() const
822{
823 return (mFD != INVAL);
824}
825
826inline int OvFD::getFD() const { return mFD; }
827
828inline void OvFD::copy(int fd) {
829 mFD = fd;
830}
831
832inline void OvFD::dump() const
833{
834 ALOGE("== Dump OvFD fd=%d path=%s start/end ==",
835 mFD, mPath);
836}
837
838//--------------- class OvFD stuff ends ---------------------
839
840} // overlay
841
842
843#endif // OVERLAY_UTILS_H