psb_drv_video.h revision e17e82feae9a81cafd79e27e093741e4af9b1e46
1/*
2 * Copyright (c) 2011 Intel Corporation. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 *    Waldo Bastian <waldo.bastian@intel.com>
26 *
27 */
28
29#ifndef _PSB_DRV_VIDEO_H_
30#define _PSB_DRV_VIDEO_H_
31
32#include <pthread.h> /* POSIX threads headers */
33
34#include <va/va_backend.h>
35#include <va/va.h>
36#include "object_heap.h"
37#include "psb_def.h"
38#include "xf86drm.h"
39#include "psb_drm.h"
40#include "psb_overlay.h"
41#include "psb_texture.h"
42#include <stdint.h>
43#ifndef ANDROID
44#include <X11/Xlibint.h>
45#include <X11/X.h>
46#include <X11/extensions/Xv.h>
47#include <X11/extensions/Xvlib.h>
48#include <X11/Xlib.h>
49#else
50#define XID unsigned int
51#define INT16 unsigned int
52#include <cutils/log.h>
53#include <system/window.h>
54#undef  LOG_TAG
55#define LOG_TAG "pvr_drv_video"
56#endif
57#include "hwdefs/dxva_fw_flags.h"
58#include <wsbm/wsbm_pool.h>
59
60#ifndef min
61#define min(a, b) ((a) < (b)) ? (a) : (b)
62#endif
63
64#ifndef max
65#define max(a, b) ((a) > (b)) ? (a) : (b)
66#endif
67
68/*
69 * WORKAROUND_DMA_OFF_BY_ONE: LLDMA requests may access one additional byte which can cause
70 * a MMU fault if the next byte after the buffer end is on a different page that isn't mapped.
71 */
72#define WORKAROUND_DMA_OFF_BY_ONE
73#define FOURCC_XVVA     (('A' << 24) + ('V' << 16) + ('V' << 8) + 'X')
74
75#define PSB_MAX_PROFILES                                14
76#define PSB_MAX_ENTRYPOINTS                             8
77#define PSB_MAX_CONFIG_ATTRIBUTES               10
78#define PSB_MAX_BUFFERTYPES                     32
79
80/* Max # of command submission buffers */
81#define PSB_MAX_CMDBUFS                         10
82#define LNC_MAX_CMDBUFS_ENCODE                  4
83#define PNW_MAX_CMDBUFS_ENCODE                  4
84
85#define PSB_SURFACE_DISPLAYING_F (0x1U<<0)
86#define PSB_SURFACE_IS_FLAG_SET(flags, mask) (((flags)& PSB_SURFACE_DISPLAYING_F) != 0)
87
88/*xrandr dirty flag*/
89#define PSB_NEW_ROTATION        1
90#define PSB_NEW_EXTVIDEO        2
91
92#define PSB_NEW_VA_ROTATION     1 << 0
93#define PSB_NEW_WM_ROTATION     1 << 1
94
95#define MAX_SLICES_PER_PICTURE 72
96#define MAX_MB_ERRORS 72
97
98typedef struct object_config_s *object_config_p;
99typedef struct object_context_s *object_context_p;
100typedef struct object_surface_s *object_surface_p;
101typedef struct object_buffer_s *object_buffer_p;
102typedef struct object_image_s *object_image_p;
103typedef struct object_subpic_s *object_subpic_p;
104typedef struct format_vtable_s *format_vtable_p;
105typedef struct psb_driver_data_s *psb_driver_data_p;
106
107typedef struct psb_surface_share_info_s psb_surface_share_info_t, *psb_surface_share_info_p;
108/* post-processing data structure */
109enum psb_output_method_t {
110    PSB_PUTSURFACE_NONE = 0,
111    PSB_PUTSURFACE_X11,/* use x11 method */
112    PSB_PUTSURFACE_TEXTURE,/* texture xvideo */
113    PSB_PUTSURFACE_OVERLAY,/* overlay xvideo */
114    PSB_PUTSURFACE_COVERLAY,/* client overlay */
115    PSB_PUTSURFACE_CTEXTURE,/* client textureblit */
116    PSB_PUTSURFACE_TEXSTREAMING,/* texsteaming */
117    PSB_PUTSURFACE_FORCE_TEXTURE,/* force texture xvideo */
118    PSB_PUTSURFACE_FORCE_OVERLAY,/* force overlay xvideo */
119    PSB_PUTSURFACE_FORCE_CTEXTURE,/* force client textureblit */
120    PSB_PUTSURFACE_FORCE_COVERLAY,/* force client overlay */
121    PSB_PUTSURFACE_FORCE_TEXSTREAMING,/* force texstreaming */
122};
123
124typedef struct psb_decode_info {
125    uint32_t num_surface;
126    uint32_t surface_id;
127} psb_decode_info_t;
128typedef struct msvdx_decode_info *psb_decode_info_p;
129
130#define CSC_MATRIX_X  (3)
131#define CSC_MATRIX_Y  (3)
132
133struct psb_driver_data_s {
134    struct object_heap_s        config_heap;
135    struct object_heap_s        context_heap;
136    struct object_heap_s        surface_heap;
137    struct object_heap_s        buffer_heap;
138    struct object_heap_s        image_heap;
139    struct object_heap_s        subpic_heap;
140    char *                      bus_id;
141    uint32_t                    dev_id;
142    int                         drm_fd;
143    int                         dup_drm_fd;
144
145    /*  PM_QoS */
146    int                         pm_qos_fd;
147    int                         dri2;
148    int                         dri_dummy;
149    XID                         context_id;
150    drm_context_t               drm_context;
151    drmLock                     *drm_lock;
152    int                         contended_lock;
153    pthread_mutex_t             drm_mutex;
154    format_vtable_p             profile2Format[PSB_MAX_PROFILES][PSB_MAX_ENTRYPOINTS];
155    uint32_t                    msvdx_context_base;
156    int                         video_sd_disabled;
157    int                         video_hd_disabled;
158    unsigned char *                      camera_bo;
159    uint32_t                    camera_phyaddr;
160    uint32_t                    camera_size;
161    unsigned char *                      rar_bo;
162    uint32_t                    rar_phyaddr;
163    uint32_t                    rar_size;
164
165    int encode_supported;
166    int decode_supported;
167    int hd_encode_supported;
168    int hd_decode_supported;
169
170    int execIoctlOffset;
171    int getParamIoctlOffset;
172
173    struct _WsbmBufferPool *main_pool;
174    struct _WsbmFenceMgr *fence_mgr;
175
176    enum psb_output_method_t output_method;
177
178    /* whether the post-processing use client overlay or not */
179    int coverlay;
180    int coverlay_init;
181    PsbPortPrivRec coverlay_priv;
182
183
184    /* whether the post-processing use client textureblit or not */
185    int ctexture;
186    struct psb_texture_s ctexture_priv;
187
188    /*
189    //whether the post-processing use texstreaing or not
190    int ctexstreaing;
191    struct psb_texstreaing ctexstreaing_priv;
192    */
193
194    unsigned char *ws_priv; /* window system related data structure */
195
196
197    VASurfaceID cur_displaying_surface;
198    VASurfaceID last_displaying_surface;
199
200    VADisplayAttribute ble_black_mode;
201    VADisplayAttribute ble_white_mode;
202
203    VADisplayAttribute blueStretch_gain;
204    VADisplayAttribute skinColorCorrection_gain;
205
206    VADisplayAttribute brightness;
207    VADisplayAttribute hue;
208    VADisplayAttribute contrast;
209    VADisplayAttribute saturation;
210    /*Save RenderMode and RenderRect attribute
211     * for medfield android extend video mode.*/
212    uint32_t render_device;
213    uint32_t render_mode;
214    VARectangle  render_rect;
215
216    unsigned int clear_color;
217
218    int  is_oold;
219
220    unsigned int load_csc_matrix;
221    signed int   csc_matrix[CSC_MATRIX_X][CSC_MATRIX_Y];
222
223    /* subpic number current buffers support */
224    unsigned int max_subpic;
225
226    /* for multi-thread safe */
227    int use_xrandr_thread;
228    pthread_mutex_t output_mutex;
229    pthread_t xrandr_thread_id;
230    int extend_fullscreen;
231
232    int drawable_info;
233    int dummy_putsurface;
234    int fixed_fps;
235    unsigned int frame_count;
236
237    uint32_t blend_mode;
238    uint32_t blend_color;
239    uint32_t overlay_auto_paint_color_key;
240    uint32_t color_key;
241
242    /*output rotation info*/
243    int disable_msvdx_rotate;
244    int msvdx_rotate_want; /* msvdx rotate info programed to msvdx */
245    int va_rotate; /* VA rotate passed from APP */
246    int mipi0_rotation; /* window manager rotation */
247    int mipi1_rotation; /* window manager rotation */
248    int hdmi_rotation; /* window manager rotation */
249    int local_rotation; /* final device rotate: VA rotate+wm rotate */
250    int extend_rotation; /* final device rotate: VA rotate+wm rotate */
251    int rotation_dirty;  /*flag for recaculate final rotation*/
252
253    unsigned int outputmethod_checkinterval;
254
255    uint32_t bcd_id;
256    uint32_t bcd_ioctrl_num;
257    uint32_t bcd_registered;
258    uint32_t bcd_buffer_num;
259    int bcd_buffer_width;
260    int bcd_buffer_height;
261    int bcd_buffer_stride;
262    VASurfaceID *bcd_buffer_surfaces;
263    uint32_t ts_source_created;
264
265    uint32_t xrandr_dirty;
266    uint32_t xrandr_update;
267    /*only VAProfileH264ConstrainedBaseline profile enable error concealment*/
268    uint32_t ec_enabled;
269
270    uint32_t pre_surfaceid;
271    psb_decode_info_t decode_info;
272    drm_psb_msvdx_decode_status_t *msvdx_decode_status;
273    VASurfaceDecodeMBErrors *surface_mb_error;
274
275    unsigned char *hPVR2DContext;
276
277    VAGenericID wrapped_surface_id[VIDEO_BUFFER_NUM];
278    VAGenericID wrapped_subpic_id[VIDEO_BUFFER_NUM];
279    PVR2DMEMINFO *videoBuf[VIDEO_BUFFER_NUM];
280    PVR2DMEMINFO *subpicBuf[VIDEO_BUFFER_NUM];
281    void *native_window;
282    int is_android;
283};
284
285#define IS_MRST(driver_data) ((driver_data->dev_id & 0xFFFC) == 0x4100)
286#define IS_MFLD(driver_data) (((driver_data->dev_id & 0xFFFC) == 0x0130) || ((driver_data->dev_id & 0xFFFF) == 0x08C0) || ((driver_data->dev_id & 0xFFFF) == 0x08C7) || ((driver_data->dev_id & 0xFFFF) == 0x08C8))
287
288struct object_config_s {
289    struct object_base_s base;
290    VAProfile profile;
291    VAEntrypoint entrypoint;
292    VAConfigAttrib attrib_list[PSB_MAX_CONFIG_ATTRIBUTES];
293    int attrib_count;
294    format_vtable_p format_vtable;
295};
296
297struct object_context_s {
298    struct object_base_s base;
299    VAContextID context_id;
300    VAConfigID config_id;
301    VAProfile profile;
302    VAEntrypoint entry_point;
303    int picture_width;
304    int picture_height;
305    int num_render_targets;
306    VASurfaceID *render_targets;
307    int va_flags;
308
309    object_surface_p current_render_target;
310    VASurfaceID current_render_surface_id;
311    psb_driver_data_p driver_data;
312    format_vtable_p format_vtable;
313    unsigned char *format_data;
314    struct psb_cmdbuf_s *cmdbuf_list[PSB_MAX_CMDBUFS];
315    struct lnc_cmdbuf_s *lnc_cmdbuf_list[LNC_MAX_CMDBUFS_ENCODE];
316    struct pnw_cmdbuf_s *pnw_cmdbuf_list[PNW_MAX_CMDBUFS_ENCODE];
317
318    struct psb_cmdbuf_s *cmdbuf; /* Current cmd buffer */
319    struct lnc_cmdbuf_s *lnc_cmdbuf;
320    struct pnw_cmdbuf_s *pnw_cmdbuf;
321
322    int cmdbuf_current;
323
324    /* Buffers */
325    object_buffer_p buffers_unused[PSB_MAX_BUFFERTYPES]; /* Linked lists (HEAD) of unused buffers for each buffer type */
326    int buffers_unused_count[PSB_MAX_BUFFERTYPES]; /* Linked lists (HEAD) of unused buffers for each buffer type */
327    object_buffer_p buffers_unused_tail[PSB_MAX_BUFFERTYPES]; /* Linked lists (TAIL) of unused buffers for each buffer type */
328    object_buffer_p buffers_active[PSB_MAX_BUFFERTYPES]; /* Linked lists of active buffers for each buffer type */
329
330    object_buffer_p *buffer_list; /* for vaRenderPicture */
331    int num_buffers;
332
333    enum {
334        psb_video_none = 0,
335        psb_video_mc,
336        psb_video_vld,
337        psb_video_deblock
338    } video_op;
339
340    uint32_t operating_mode;
341    uint32_t flags; /* See render flags below */
342    uint32_t first_mb;
343    uint32_t last_mb;
344
345    int is_oold;
346    int msvdx_rotate;
347    int interlaced_stream;
348
349    uint32_t msvdx_context;
350
351    /* Debug */
352    uint32_t frame_count;
353    uint32_t slice_count;
354};
355
356#define ROTATE_VA2MSVDX(va_rotate)  (va_rotate)
357#define CONTEXT_ROTATE(obj_context) (obj_context->msvdx_rotate != ROTATE_VA2MSVDX(VA_ROTATION_NONE))
358
359#define MAX_SHARE_INFO_KHANDLES 32
360struct psb_surface_share_info_s {
361    //int rotation_sf;                    /*rotaion degree from surface flinger.*/
362    int surface_rotate;                 /*rotation degree of current rotation surface*/
363    int metadata_rotate;                /*rotation degree of meta data*/
364    int width_r;
365    int height_r;
366    int surface_protected;              /*whether this surface need be protected*/
367    /*Force render path.
368    0 : no fore.
369    1 : force gpu render;
370    2 : force overlay render.*/
371    int force_output_method;
372    unsigned int rotate_khandle;
373    unsigned int renderStatus;
374    int bob_deinterlace;
375    unsigned int width;
376    unsigned int height;
377    unsigned int luma_stride;
378    unsigned int chroma_u_stride;
379    unsigned int chroma_v_stride;
380    unsigned int format;
381    unsigned int khandle;
382    unsigned int khandles_count;   /* the number of khandle */
383    unsigned int khandles[MAX_SHARE_INFO_KHANDLES];      /* save all khandles */
384};
385
386struct object_surface_s {
387    struct object_base_s base;
388    VASurfaceID surface_id;
389    VAContextID context_id;
390    int width;
391    int height;
392    int height_origin;
393    int width_r;
394    int height_r;
395    struct psb_surface_s *psb_surface;
396    struct psb_surface_s *psb_surface_rotate; /* Alternative output surface for rotation */
397    void *subpictures;/* if not NULL, have subpicture information */
398    unsigned int subpic_count; /* to ensure output have enough space for PDS & RAST */
399    unsigned int derived_imgcnt; /* is the surface derived by a VAImage? */
400    unsigned long display_timestamp; /* record the time point of put surface*/
401    void *rotate_vaddr;
402    struct psb_surface_share_info_s *share_info;
403};
404
405#define PSB_CODEDBUF_SLICE_NUM_MASK (0xff)
406#define PSB_CODEDBUF_SLICE_NUM_SHIFT (0)
407
408#define PSB_CODEDBUF_NONE_VCL_NUM_MASK (0xff)
409#define PSB_CODEDBUF_NONE_VCL_NUM_SHIFT (8)
410
411#define SET_CODEDBUF_INFO(flag, aux_info, slice_num) \
412    do {\
413	(aux_info) &= ~(PSB_CODEDBUF_##flag##_MASK<<PSB_CODEDBUF_##flag##_SHIFT);\
414	(aux_info) |= ((slice_num) & PSB_CODEDBUF_##flag##_MASK)\
415	<<PSB_CODEDBUF_##flag##_SHIFT;\
416    } while (0)
417
418#define CLEAR_CODEDBUF_INFO(flag, aux_info) \
419    do {\
420	(aux_info) &= ~(PSB_CODEDBUF_##flag##_MASK<<PSB_CODEDBUF_##flag##_SHIFT);\
421    } while (0)
422
423#define GET_CODEDBUF_INFO(flag, aux_info) \
424	(((aux_info)>>PSB_CODEDBUF_##flag##_SHIFT) & PSB_CODEDBUF_##flag##_MASK)
425
426
427#define PSB_CODEDBUF_SEGMENT_MAX  (8)
428
429struct object_buffer_s {
430    struct object_base_s base;
431    object_buffer_p ptr_next; /* Generic ptr for linked list */
432    object_buffer_p *pptr_prev_next; /* Generic ptr for linked list */
433    struct psb_buffer_s *psb_buffer;
434    unsigned char *buffer_data;
435    unsigned int size;
436    unsigned int alloc_size;
437    unsigned int max_num_elements;
438    unsigned int num_elements;
439    object_context_p context;
440    VABufferType type;
441    uint32_t last_used;
442
443    /* for VAEncCodedBufferType */
444    VACodedBufferSegment codedbuf_mapinfo[PSB_CODEDBUF_SEGMENT_MAX];
445    uint32_t codedbuf_aux_info;
446};
447
448struct object_image_s {
449    struct object_base_s base;
450    VAImage image;
451    unsigned int palette[16];
452    int subpic_ref;
453    VASurfaceID derived_surface;
454};
455
456struct object_subpic_s {
457    struct object_base_s base;
458    VASubpictureID subpic_id;
459
460    VAImageID image_id;
461
462    /* chromakey range */
463    unsigned int chromakey_min;
464    unsigned int chromakey_max;
465    unsigned int chromakey_mask;
466
467    /* global alpha */
468    unsigned int global_alpha;
469
470    /* flags */
471    unsigned int flags; /* see below */
472
473    unsigned char *surfaces; /* surfaces, associated with this subpicture */
474};
475
476#define MEMSET_OBJECT(ptr, data_struct) \
477        memset((unsigned char *)ptr + sizeof(struct object_base_s),\
478                0,                          \
479               sizeof(data_struct) - sizeof(struct object_base_s))
480
481struct format_vtable_s {
482    void (*queryConfigAttributes)(
483        VAProfile profile,
484        VAEntrypoint entrypoint,
485        VAConfigAttrib *attrib_list,
486        int num_attribs
487    );
488    VAStatus(*validateConfig)(
489        object_config_p obj_config
490    );
491    VAStatus(*createContext)(
492        object_context_p obj_context,
493        object_config_p obj_config
494    );
495    void (*destroyContext)(
496        object_context_p obj_context
497    );
498    VAStatus(*beginPicture)(
499        object_context_p obj_context
500    );
501    VAStatus(*renderPicture)(
502        object_context_p obj_context,
503        object_buffer_p *buffers,
504        int num_buffers
505    );
506    VAStatus(*endPicture)(
507        object_context_p obj_context
508    );
509};
510
511typedef struct IMG_native_handle
512{
513    native_handle_t base;
514    int fd;
515    unsigned long long ui64Stamp;
516    int usage;
517    int width;
518    int height;
519    unsigned int bpp;
520    int format;
521}IMG_native_handle_t;
522
523#define psb__bounds_check(x, max)                                       \
524    do { ASSERT(x < max); if (x >= max) x = max - 1; } while(0);
525
526static inline unsigned long GetTickCount()
527{
528    struct timeval tv;
529    if (gettimeofday(&tv, NULL))
530        return 0;
531    return tv.tv_usec / 1000 + tv.tv_sec * 1000;
532}
533
534inline static char * buffer_type_to_string(int type)
535{
536    switch (type) {
537    case VAPictureParameterBufferType:
538        return "VAPictureParameterBufferType";
539    case VAIQMatrixBufferType:
540        return "VAIQMatrixBufferType";
541    case VABitPlaneBufferType:
542        return "VABitPlaneBufferType";
543    case VASliceGroupMapBufferType:
544        return "VASliceGroupMapBufferType";
545    case VASliceParameterBufferType:
546        return "VASliceParameterBufferType";
547    case VASliceDataBufferType:
548        return "VASliceDataBufferType";
549    case VAProtectedSliceDataBufferType:
550        return "VAProtectedSliceDataBufferType";
551    case VAMacroblockParameterBufferType:
552        return "VAMacroblockParameterBufferType";
553    case VAResidualDataBufferType:
554        return "VAResidualDataBufferType";
555    case VADeblockingParameterBufferType:
556        return "VADeblockingParameterBufferType";
557    case VAImageBufferType:
558        return "VAImageBufferType";
559    case VAEncCodedBufferType:
560        return "VAEncCodedBufferType";
561    case VAEncSequenceParameterBufferType:
562        return "VAEncSequenceParameterBufferType";
563    case VAEncPictureParameterBufferType:
564        return "VAEncPictureParameterBufferType";
565    case VAEncSliceParameterBufferType:
566        return "VAEncSliceParameterBufferType";
567    case VAEncH264VUIBufferType:
568        return "VAEncH264VUIBufferType";
569    case VAEncH264SEIBufferType:
570        return "VAEncH264SEIBufferType";
571    case VAEncMiscParameterBufferType:
572        return "VAEncMiscParameterBufferType";
573    default:
574        return "UnknowBuffer";
575    }
576}
577
578inline static int Angle2Rotation(int angle)
579{
580    angle %= 360;
581    switch (angle) {
582    case 0:
583        return VA_ROTATION_NONE;
584    case 90:
585        return VA_ROTATION_90;
586    case 180:
587        return VA_ROTATION_180;
588    case 270:
589        return VA_ROTATION_270;
590    default:
591        return -1;
592    }
593}
594
595inline static int Rotation2Angle(int rotation)
596{
597    switch (rotation) {
598    case VA_ROTATION_NONE:
599        return 0;
600    case VA_ROTATION_90:
601        return 90;
602    case VA_ROTATION_180:
603        return 180;
604    case VA_ROTATION_270:
605        return 270;
606    default:
607        return -1;
608    }
609}
610
611int psb_parse_config(char *env, char *env_value);
612
613int LOCK_HARDWARE(psb_driver_data_p driver_data);
614int UNLOCK_HARDWARE(psb_driver_data_p driver_data);
615
616#endif /* _PSB_DRV_VIDEO_H_ */
617