OSDN Git Service

Support VBR for HEVC 10bit
[android-x86/hardware-intel-common-vaapi.git] / src / i965_drv_video.c
1 /*
2  * Copyright ?2009 Intel Corporation
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  *    Xiang Haihao <haihao.xiang@intel.com>
26  *    Zou Nan hai <nanhai.zou@intel.com>
27  *
28  */
29
30 #include "sysdeps.h"
31 #include <unistd.h>
32 #include <dlfcn.h>
33 #include <drm_fourcc.h>
34
35 #ifdef HAVE_VA_X11
36 # include "i965_output_dri.h"
37 #endif
38
39 #ifdef HAVE_VA_WAYLAND
40 # include "i965_output_wayland.h"
41 #endif
42
43 #include "intel_version.h"
44 #include "intel_driver.h"
45 #include "intel_memman.h"
46 #include "intel_batchbuffer.h"
47 #include "i965_defines.h"
48 #include "i965_drv_video.h"
49 #include "i965_decoder.h"
50 #include "i965_encoder.h"
51
52 #include "i965_post_processing.h"
53
54 #include "gen9_vp9_encapi.h"
55
56 #define CONFIG_ID_OFFSET                0x01000000
57 #define CONTEXT_ID_OFFSET               0x02000000
58 #define SURFACE_ID_OFFSET               0x04000000
59 #define BUFFER_ID_OFFSET                0x08000000
60 #define IMAGE_ID_OFFSET                 0x0a000000
61 #define SUBPIC_ID_OFFSET                0x10000000
62
63 static int get_sampling_from_fourcc(unsigned int fourcc);
64
65 /* Check whether we are rendering to X11 (VA/X11 or VA/GLX API) */
66 #define IS_VA_X11(ctx) \
67     (((ctx)->display_type & VA_DISPLAY_MAJOR_MASK) == VA_DISPLAY_X11)
68
69 /* Check whether we are rendering to Wayland */
70 #define IS_VA_WAYLAND(ctx) \
71     (((ctx)->display_type & VA_DISPLAY_MAJOR_MASK) == VA_DISPLAY_WAYLAND)
72
73 #define I965_BIT        1
74 #define I965_2BITS      (I965_BIT << 1)
75 #define I965_4BITS      (I965_BIT << 2)
76 #define I965_8BITS      (I965_BIT << 3)
77 #define I965_16BITS     (I965_BIT << 4)
78 #define I965_32BITS     (I965_BIT << 5)
79
80 #define PLANE_0         0
81 #define PLANE_1         1
82 #define PLANE_2         2
83
84 #define OFFSET_0        0
85 #define OFFSET_4        4
86 #define OFFSET_8        8
87 #define OFFSET_16       16
88 #define OFFSET_24       24
89
90 /* hfactor, vfactor, num_planes, bpp[], num_components, components[] */
91 #define I_NV12  2, 2, 2, {I965_8BITS, I965_4BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_1, OFFSET_8} }
92 #define I_I420  2, 2, 3, {I965_8BITS, I965_2BITS, I965_2BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_2, OFFSET_0} }
93 #define I_IYUV  I_I420
94 #define I_IMC3  I_I420
95 #define I_YV12  2, 2, 3, {I965_8BITS, I965_2BITS, I965_2BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_2, OFFSET_0}, {PLANE_1, OFFSET_0} }
96 #define I_IMC1  I_YV12
97
98 #define I_P010  2, 2, 2, {I965_16BITS, I965_8BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_1, OFFSET_16} }
99
100 #define I_I010  2, 2, 3, {I965_16BITS, I965_4BITS, I965_4BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_2, OFFSET_0} }
101
102 #define I_422H  2, 1, 3, {I965_8BITS, I965_4BITS, I965_4BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_2, OFFSET_0} }
103 #define I_422V  1, 2, 3, {I965_8BITS, I965_4BITS, I965_4BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_2, OFFSET_0} }
104 #define I_YV16  2, 1, 3, {I965_8BITS, I965_4BITS, I965_4BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_2, OFFSET_0}, {PLANE_1, OFFSET_0} }
105 #define I_YUY2  2, 1, 1, {I965_16BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_24} }
106 #define I_UYVY  2, 1, 1, {I965_16BITS}, 3, { {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_16} }
107
108 #define I_444P  1, 1, 3, {I965_8BITS, I965_8BITS, I965_8BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_2, OFFSET_0} }
109
110 #define I_411P  4, 1, 3, {I965_8BITS, I965_2BITS, I965_2BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_1, OFFSET_0}, {PLANE_2, OFFSET_0} }
111
112 #define I_Y800  1, 1, 1, {I965_8BITS}, 1, { {PLANE_0, OFFSET_0} }
113
114 #define I_RGBA  1, 1, 1, {I965_32BITS}, 4, { {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_16}, {PLANE_0, OFFSET_24} }
115 #define I_RGBX  1, 1, 1, {I965_32BITS}, 3, { {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_16} }
116 #define I_BGRA  1, 1, 1, {I965_32BITS}, 4, { {PLANE_0, OFFSET_16}, {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_24} }
117 #define I_BGRX  1, 1, 1, {I965_32BITS}, 3, { {PLANE_0, OFFSET_16}, {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_0} }
118
119 #define I_ARGB  1, 1, 1, {I965_32BITS}, 4, { {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_16}, {PLANE_0, OFFSET_24}, {PLANE_0, OFFSET_0} }
120 #define I_ABGR  1, 1, 1, {I965_32BITS}, 4, { {PLANE_0, OFFSET_24}, {PLANE_0, OFFSET_16}, {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_0} }
121
122 #define I_IA88  1, 1, 1, {I965_16BITS}, 2, { {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_8} }
123 #define I_AI88  1, 1, 1, {I965_16BITS}, 2, { {PLANE_0, OFFSET_8}, {PLANE_0, OFFSET_0} }
124
125 #define I_IA44  1, 1, 1, {I965_8BITS}, 2, { {PLANE_0, OFFSET_0}, {PLANE_0, OFFSET_4} }
126 #define I_AI44  1, 1, 1, {I965_8BITS}, 2, { {PLANE_0, OFFSET_4}, {PLANE_0, OFFSET_0} }
127
128 /* flag */
129 #define I_S             1
130 #define I_I             2
131 #define I_SI            (I_S | I_I)
132
133 #define DEF_FOUCC_INFO(FOURCC, FORMAT, SUB, FLAG)       { VA_FOURCC_##FOURCC, I965_COLOR_##FORMAT, SUBSAMPLE_##SUB, FLAG, I_##FOURCC }
134 #define DEF_YUV(FOURCC, SUB, FLAG)                      DEF_FOUCC_INFO(FOURCC, YUV, SUB, FLAG)
135 #define DEF_RGB(FOURCC, SUB, FLAG)                      DEF_FOUCC_INFO(FOURCC, RGB, SUB, FLAG)
136 #define DEF_INDEX(FOURCC, SUB, FLAG)                    DEF_FOUCC_INFO(FOURCC, INDEX, SUB, FLAG)
137
138 static const i965_fourcc_info i965_fourcc_infos[] = {
139     DEF_YUV(NV12, YUV420, I_SI),
140     DEF_YUV(I420, YUV420, I_SI),
141     DEF_YUV(IYUV, YUV420, I_S),
142     DEF_YUV(IMC3, YUV420, I_S),
143     DEF_YUV(YV12, YUV420, I_SI),
144     DEF_YUV(IMC1, YUV420, I_S),
145
146     DEF_YUV(P010, YUV420, I_SI),
147     DEF_YUV(I010, YUV420, I_S),
148
149     DEF_YUV(422H, YUV422H, I_SI),
150     DEF_YUV(422V, YUV422V, I_S),
151     DEF_YUV(YV16, YUV422H, I_S),
152     DEF_YUV(YUY2, YUV422H, I_SI),
153     DEF_YUV(UYVY, YUV422H, I_SI),
154
155     DEF_YUV(444P, YUV444, I_S),
156
157     DEF_YUV(411P, YUV411, I_S),
158
159     DEF_YUV(Y800, YUV400, I_S),
160
161     DEF_RGB(RGBA, RGBX, I_SI),
162     DEF_RGB(RGBX, RGBX, I_SI),
163     DEF_RGB(BGRA, RGBX, I_SI),
164     DEF_RGB(BGRX, RGBX, I_SI),
165
166     DEF_RGB(ARGB, RGBX, I_I),
167     DEF_RGB(ABGR, RGBX, I_I),
168
169     DEF_INDEX(IA88, RGBX, I_I),
170     DEF_INDEX(AI88, RGBX, I_I),
171
172     DEF_INDEX(IA44, RGBX, I_I),
173     DEF_INDEX(AI44, RGBX, I_I)
174 };
175
176 const i965_fourcc_info *
177 get_fourcc_info(unsigned int fourcc)
178 {
179     unsigned int i;
180
181     for (i = 0; i < ARRAY_ELEMS(i965_fourcc_infos); i++) {
182         const i965_fourcc_info * const info = &i965_fourcc_infos[i];
183
184         if (info->fourcc == fourcc)
185             return info;
186     }
187
188     return NULL;
189 }
190
191 static int
192 get_bpp_from_fourcc(unsigned int fourcc)
193 {
194     const i965_fourcc_info *info = get_fourcc_info(fourcc);
195     unsigned int i = 0;
196     unsigned int bpp = 0;
197
198     if (!info)
199         return 0;
200
201     for (i = 0; i < info->num_planes; i++)
202         bpp += info->bpp[i];
203
204     return bpp;
205 }
206
207 enum {
208     I965_SURFACETYPE_RGBA = 1,
209     I965_SURFACETYPE_YUV,
210     I965_SURFACETYPE_INDEXED
211 };
212
213 /* List of supported display attributes */
214 static const VADisplayAttribute i965_display_attributes[] = {
215     {
216         VADisplayAttribBrightness,
217         -100, 100, DEFAULT_BRIGHTNESS,
218         VA_DISPLAY_ATTRIB_GETTABLE | VA_DISPLAY_ATTRIB_SETTABLE
219     },
220
221     {
222         VADisplayAttribContrast,
223         0, 100, DEFAULT_CONTRAST,
224         VA_DISPLAY_ATTRIB_GETTABLE | VA_DISPLAY_ATTRIB_SETTABLE
225     },
226
227     {
228         VADisplayAttribHue,
229         -180, 180, DEFAULT_HUE,
230         VA_DISPLAY_ATTRIB_GETTABLE | VA_DISPLAY_ATTRIB_SETTABLE
231     },
232
233     {
234         VADisplayAttribSaturation,
235         0, 100, DEFAULT_SATURATION,
236         VA_DISPLAY_ATTRIB_GETTABLE | VA_DISPLAY_ATTRIB_SETTABLE
237     },
238
239     {
240         VADisplayAttribRotation,
241         0, 3, VA_ROTATION_NONE,
242         VA_DISPLAY_ATTRIB_GETTABLE | VA_DISPLAY_ATTRIB_SETTABLE
243     },
244 };
245
246 /* List of supported image formats */
247 typedef struct {
248     unsigned int        type;
249     VAImageFormat       va_format;
250 } i965_image_format_map_t;
251
252 static const i965_image_format_map_t
253 i965_image_formats_map[I965_MAX_IMAGE_FORMATS + 1] = {
254     {
255         I965_SURFACETYPE_YUV,
256         { VA_FOURCC_YV12, VA_LSB_FIRST, 12, }
257     },
258     {
259         I965_SURFACETYPE_YUV,
260         { VA_FOURCC_I420, VA_LSB_FIRST, 12, }
261     },
262     {
263         I965_SURFACETYPE_YUV,
264         { VA_FOURCC_NV12, VA_LSB_FIRST, 12, }
265     },
266     {
267         I965_SURFACETYPE_YUV,
268         { VA_FOURCC_YUY2, VA_LSB_FIRST, 16, }
269     },
270     {
271         I965_SURFACETYPE_YUV,
272         { VA_FOURCC_UYVY, VA_LSB_FIRST, 16, }
273     },
274     {
275         I965_SURFACETYPE_YUV,
276         { VA_FOURCC_422H, VA_LSB_FIRST, 16, }
277     },
278     {
279         I965_SURFACETYPE_RGBA,
280         { VA_FOURCC_RGBX, VA_LSB_FIRST, 32, 24, 0x000000ff, 0x0000ff00, 0x00ff0000 }
281     },
282     {
283         I965_SURFACETYPE_RGBA,
284         { VA_FOURCC_BGRX, VA_LSB_FIRST, 32, 24, 0x00ff0000, 0x0000ff00, 0x000000ff }
285     },
286     {
287         I965_SURFACETYPE_YUV,
288         { VA_FOURCC_P010, VA_LSB_FIRST, 24, }
289     },
290 };
291
292 /* List of supported subpicture formats */
293 typedef struct {
294     unsigned int        type;
295     unsigned int        format;
296     VAImageFormat       va_format;
297     unsigned int        va_flags;
298 } i965_subpic_format_map_t;
299
300 #define COMMON_SUBPICTURE_FLAGS                 \
301     (VA_SUBPICTURE_DESTINATION_IS_SCREEN_COORD| \
302      VA_SUBPICTURE_GLOBAL_ALPHA)
303
304 static const i965_subpic_format_map_t
305 i965_subpic_formats_map[I965_MAX_SUBPIC_FORMATS + 1] = {
306     {
307         I965_SURFACETYPE_INDEXED, I965_SURFACEFORMAT_P4A4_UNORM,
308         { VA_FOURCC_IA44, VA_MSB_FIRST, 8, },
309         COMMON_SUBPICTURE_FLAGS
310     },
311     {
312         I965_SURFACETYPE_INDEXED, I965_SURFACEFORMAT_A4P4_UNORM,
313         { VA_FOURCC_AI44, VA_MSB_FIRST, 8, },
314         COMMON_SUBPICTURE_FLAGS
315     },
316     {
317         I965_SURFACETYPE_INDEXED, I965_SURFACEFORMAT_P8A8_UNORM,
318         { VA_FOURCC_IA88, VA_MSB_FIRST, 16, },
319         COMMON_SUBPICTURE_FLAGS
320     },
321     {
322         I965_SURFACETYPE_INDEXED, I965_SURFACEFORMAT_A8P8_UNORM,
323         { VA_FOURCC_AI88, VA_MSB_FIRST, 16, },
324         COMMON_SUBPICTURE_FLAGS
325     },
326     {
327         I965_SURFACETYPE_RGBA, I965_SURFACEFORMAT_B8G8R8A8_UNORM,
328         {
329             VA_FOURCC_BGRA, VA_LSB_FIRST, 32,
330             32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000
331         },
332         COMMON_SUBPICTURE_FLAGS
333     },
334     {
335         I965_SURFACETYPE_RGBA, I965_SURFACEFORMAT_R8G8B8A8_UNORM,
336         {
337             VA_FOURCC_RGBA, VA_LSB_FIRST, 32,
338             32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
339         },
340         COMMON_SUBPICTURE_FLAGS
341     },
342 };
343
344 static const i965_subpic_format_map_t *
345 get_subpic_format(const VAImageFormat *va_format)
346 {
347     unsigned int i;
348     for (i = 0; i965_subpic_formats_map[i].type != 0; i++) {
349         const i965_subpic_format_map_t * const m = &i965_subpic_formats_map[i];
350         if (m->va_format.fourcc == va_format->fourcc &&
351             (m->type == I965_SURFACETYPE_RGBA ?
352              (m->va_format.byte_order == va_format->byte_order &&
353               m->va_format.red_mask   == va_format->red_mask   &&
354               m->va_format.green_mask == va_format->green_mask &&
355               m->va_format.blue_mask  == va_format->blue_mask  &&
356               m->va_format.alpha_mask == va_format->alpha_mask) : 1))
357             return m;
358     }
359     return NULL;
360 }
361
362 /* Checks whether the surface is in busy state */
363 static bool
364 is_surface_busy(struct i965_driver_data *i965,
365                 struct object_surface *obj_surface)
366 {
367     assert(obj_surface != NULL);
368
369     if (obj_surface->locked_image_id != VA_INVALID_ID)
370         return true;
371     if (obj_surface->derived_image_id != VA_INVALID_ID)
372         return true;
373     return false;
374 }
375
376 /* Checks whether the image is in busy state */
377 static bool
378 is_image_busy(struct i965_driver_data *i965, struct object_image *obj_image, VASurfaceID surface)
379 {
380     struct object_buffer *obj_buffer;
381
382     assert(obj_image != NULL);
383
384     if (obj_image->derived_surface != VA_INVALID_ID &&
385         obj_image->derived_surface == surface)
386         return true;
387
388     obj_buffer = BUFFER(obj_image->image.buf);
389     if (obj_buffer && obj_buffer->export_refcount > 0)
390         return true;
391     return false;
392 }
393
394 #define I965_PACKED_HEADER_BASE         0
395 #define I965_SEQ_PACKED_HEADER_BASE     0
396 #define I965_SEQ_PACKED_HEADER_END      2
397 #define I965_PIC_PACKED_HEADER_BASE     2
398 #define I965_PACKED_MISC_HEADER_BASE    4
399
400 int
401 va_enc_packed_type_to_idx(int packed_type)
402 {
403     int idx = 0;
404
405     if (packed_type & VAEncPackedHeaderMiscMask) {
406         idx = I965_PACKED_MISC_HEADER_BASE;
407         packed_type = (~VAEncPackedHeaderMiscMask & packed_type);
408         ASSERT_RET(packed_type > 0, 0);
409         idx += (packed_type - 1);
410     } else {
411         idx = I965_PACKED_HEADER_BASE;
412
413         switch (packed_type) {
414         case VAEncPackedHeaderSequence:
415             idx = I965_SEQ_PACKED_HEADER_BASE + 0;
416             break;
417
418         case VAEncPackedHeaderPicture:
419             idx = I965_PIC_PACKED_HEADER_BASE + 0;
420             break;
421
422         case VAEncPackedHeaderSlice:
423             idx = I965_PIC_PACKED_HEADER_BASE + 1;
424             break;
425
426         default:
427             /* Should not get here */
428             ASSERT_RET(0, 0);
429             break;
430         }
431     }
432
433     ASSERT_RET(idx < 5, 0);
434     return idx;
435 }
436
437 #define CALL_VTABLE(vawr, status, param) status = (vawr->vtable->param)
438
439 static VAStatus
440 i965_surface_wrapper(VADriverContextP ctx, VASurfaceID surface)
441 {
442     struct i965_driver_data *i965 = i965_driver_data(ctx);
443     struct object_surface *obj_surface = SURFACE(surface);
444     VAStatus va_status = VA_STATUS_SUCCESS;
445
446     if (!obj_surface) {
447         return VA_STATUS_ERROR_INVALID_SURFACE;
448     }
449
450     if (obj_surface->wrapper_surface != VA_INVALID_ID) {
451         /* the wrapped surface already exists. just return it */
452         return va_status;
453     }
454
455     if (obj_surface->fourcc == 0)
456         i965_check_alloc_surface_bo(ctx, obj_surface,
457                                     1, VA_FOURCC_NV12, SUBSAMPLE_YUV420);
458
459     /*
460      * TBD: Support more surface formats.
461      * Currently only NV12 is support as NV12 is used by decoding.
462      */
463     if (obj_surface->fourcc != VA_FOURCC_NV12)
464         return VA_STATUS_ERROR_INVALID_PARAMETER;
465
466     if ((i965->wrapper_pdrvctx == NULL) ||
467         (obj_surface->bo == NULL))
468         return VA_STATUS_ERROR_INVALID_PARAMETER;
469
470     {
471         int fd_handle;
472         VASurfaceAttrib attrib_list[2];
473         VASurfaceAttribExternalBuffers buffer_descriptor;
474         VAGenericID wrapper_surface;
475
476         if (drm_intel_bo_gem_export_to_prime(obj_surface->bo, &fd_handle) != 0)
477             return VA_STATUS_ERROR_OPERATION_FAILED;
478
479         obj_surface->exported_primefd = fd_handle;
480
481         memset(&attrib_list, 0, sizeof(attrib_list));
482         memset(&buffer_descriptor, 0, sizeof(buffer_descriptor));
483
484         attrib_list[0].type = VASurfaceAttribExternalBufferDescriptor;
485         attrib_list[0].flags = VA_SURFACE_ATTRIB_SETTABLE;
486         attrib_list[0].value.value.p = &buffer_descriptor;
487         attrib_list[0].value.type = VAGenericValueTypePointer;
488
489         attrib_list[1].type = VASurfaceAttribMemoryType;
490         attrib_list[1].flags = VA_SURFACE_ATTRIB_SETTABLE;
491         attrib_list[1].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
492         attrib_list[1].value.type = VAGenericValueTypeInteger;
493
494         buffer_descriptor.num_buffers = 1;
495         buffer_descriptor.num_planes = 2;
496         buffer_descriptor.width = obj_surface->orig_width;
497         buffer_descriptor.height = obj_surface->orig_height;
498         buffer_descriptor.pixel_format = obj_surface->fourcc;
499         buffer_descriptor.data_size = obj_surface->size;
500         buffer_descriptor.pitches[0] = obj_surface->width;
501         buffer_descriptor.pitches[1] = obj_surface->cb_cr_pitch;
502         buffer_descriptor.offsets[0] = 0;
503         buffer_descriptor.offsets[1] = obj_surface->width * obj_surface->height;
504         buffer_descriptor.buffers = (void *)&fd_handle;
505
506         CALL_VTABLE(i965->wrapper_pdrvctx, va_status,
507                     vaCreateSurfaces2(i965->wrapper_pdrvctx,
508                                       VA_RT_FORMAT_YUV420,
509                                       obj_surface->orig_width,
510                                       obj_surface->orig_height,
511                                       &wrapper_surface, 1,
512                                       attrib_list, 2));
513
514         if (va_status == VA_STATUS_SUCCESS) {
515             obj_surface->wrapper_surface = wrapper_surface;
516         } else {
517             /* This needs to be checked */
518             va_status = VA_STATUS_ERROR_OPERATION_FAILED;
519         }
520         return va_status;
521     }
522
523 }
524
525 VAStatus
526 i965_QueryConfigProfiles(VADriverContextP ctx,
527                          VAProfile *profile_list,       /* out */
528                          int *num_profiles)             /* out */
529 {
530     struct i965_driver_data * const i965 = i965_driver_data(ctx);
531     int i = 0;
532
533     if (HAS_MPEG2_DECODING(i965) ||
534         HAS_MPEG2_ENCODING(i965)) {
535         profile_list[i++] = VAProfileMPEG2Simple;
536         profile_list[i++] = VAProfileMPEG2Main;
537     }
538
539     if (HAS_H264_DECODING(i965) ||
540         HAS_H264_ENCODING(i965) ||
541         HAS_LP_H264_ENCODING(i965) ||
542         HAS_FEI_H264_ENCODING(i965) ||
543         HAS_H264_PREENC(i965)) {
544         profile_list[i++] = VAProfileH264ConstrainedBaseline;
545         profile_list[i++] = VAProfileH264Main;
546         profile_list[i++] = VAProfileH264High;
547     }
548     if (HAS_H264_MVC_DECODING_PROFILE(i965, VAProfileH264MultiviewHigh) ||
549         HAS_H264_MVC_ENCODING(i965))
550         profile_list[i++] = VAProfileH264MultiviewHigh;
551     if (HAS_H264_MVC_DECODING_PROFILE(i965, VAProfileH264StereoHigh) ||
552         HAS_H264_MVC_ENCODING(i965))
553         profile_list[i++] = VAProfileH264StereoHigh;
554
555     if (HAS_VC1_DECODING(i965)) {
556         profile_list[i++] = VAProfileVC1Simple;
557         profile_list[i++] = VAProfileVC1Main;
558         profile_list[i++] = VAProfileVC1Advanced;
559     }
560
561     if (HAS_VPP(i965)) {
562         profile_list[i++] = VAProfileNone;
563     }
564
565     if (HAS_JPEG_DECODING(i965) ||
566         HAS_JPEG_ENCODING(i965)) {
567         profile_list[i++] = VAProfileJPEGBaseline;
568     }
569
570     if (HAS_VP8_DECODING(i965) ||
571         HAS_VP8_ENCODING(i965)) {
572         profile_list[i++] = VAProfileVP8Version0_3;
573     }
574
575     if (HAS_HEVC_DECODING(i965) ||
576         HAS_HEVC_ENCODING(i965)) {
577         profile_list[i++] = VAProfileHEVCMain;
578     }
579
580     if (HAS_HEVC10_DECODING(i965) ||
581         HAS_HEVC10_ENCODING(i965)) {
582         profile_list[i++] = VAProfileHEVCMain10;
583     }
584
585     if (HAS_VP9_DECODING_PROFILE(i965, VAProfileVP9Profile0) ||
586         HAS_VP9_ENCODING(i965) ||
587         HAS_LP_VP9_ENCODING(i965)) {
588         profile_list[i++] = VAProfileVP9Profile0;
589     }
590
591     if (HAS_VP9_DECODING_PROFILE(i965, VAProfileVP9Profile2)) {
592         profile_list[i++] = VAProfileVP9Profile2;
593     }
594
595     if (i965->wrapper_pdrvctx) {
596         VAProfile wrapper_list[4];
597         int wrapper_num;
598         VADriverContextP pdrvctx;
599         VAStatus va_status;
600
601         pdrvctx = i965->wrapper_pdrvctx;
602         CALL_VTABLE(pdrvctx, va_status,
603                     vaQueryConfigProfiles(pdrvctx,
604                                           wrapper_list, &wrapper_num));
605
606         if (va_status == VA_STATUS_SUCCESS) {
607             int j;
608             for (j = 0; j < wrapper_num; j++)
609                 if (wrapper_list[j] != VAProfileNone)
610                     profile_list[i++] = wrapper_list[j];
611         }
612     }
613
614     /* If the assert fails then I965_MAX_PROFILES needs to be bigger */
615     ASSERT_RET(i <= I965_MAX_PROFILES, VA_STATUS_ERROR_OPERATION_FAILED);
616     *num_profiles = i;
617
618     return VA_STATUS_SUCCESS;
619 }
620
621 VAStatus
622 i965_QueryConfigEntrypoints(VADriverContextP ctx,
623                             VAProfile profile,
624                             VAEntrypoint *entrypoint_list,      /* out */
625                             int *num_entrypoints)               /* out */
626 {
627     struct i965_driver_data * const i965 = i965_driver_data(ctx);
628     int n = 0;
629
630     switch (profile) {
631     case VAProfileMPEG2Simple:
632     case VAProfileMPEG2Main:
633         if (HAS_MPEG2_DECODING(i965))
634             entrypoint_list[n++] = VAEntrypointVLD;
635
636         if (HAS_MPEG2_ENCODING(i965))
637             entrypoint_list[n++] = VAEntrypointEncSlice;
638
639         break;
640
641     case VAProfileH264ConstrainedBaseline:
642     case VAProfileH264Main:
643     case VAProfileH264High:
644         if (HAS_H264_DECODING(i965))
645             entrypoint_list[n++] = VAEntrypointVLD;
646
647         if (HAS_H264_ENCODING(i965))
648             entrypoint_list[n++] = VAEntrypointEncSlice;
649
650         if (HAS_LP_H264_ENCODING(i965))
651             entrypoint_list[n++] = VAEntrypointEncSliceLP;
652
653         if (HAS_FEI_H264_ENCODING(i965))
654             entrypoint_list[n++] = VAEntrypointFEI;
655
656         if (HAS_H264_PREENC(i965))
657             entrypoint_list[n++] = VAEntrypointStats;
658         break;
659
660     case VAProfileH264MultiviewHigh:
661     case VAProfileH264StereoHigh:
662         if (HAS_H264_MVC_DECODING_PROFILE(i965, profile))
663             entrypoint_list[n++] = VAEntrypointVLD;
664
665         if (HAS_H264_MVC_ENCODING(i965))
666             entrypoint_list[n++] = VAEntrypointEncSlice;
667         break;
668
669     case VAProfileVC1Simple:
670     case VAProfileVC1Main:
671     case VAProfileVC1Advanced:
672         if (HAS_VC1_DECODING(i965))
673             entrypoint_list[n++] = VAEntrypointVLD;
674         break;
675
676     case VAProfileNone:
677         if (HAS_VPP(i965))
678             entrypoint_list[n++] = VAEntrypointVideoProc;
679         break;
680
681     case VAProfileJPEGBaseline:
682         if (HAS_JPEG_DECODING(i965))
683             entrypoint_list[n++] = VAEntrypointVLD;
684
685         if (HAS_JPEG_ENCODING(i965))
686             entrypoint_list[n++] = VAEntrypointEncPicture;
687         break;
688
689     case VAProfileVP8Version0_3:
690         if (HAS_VP8_DECODING(i965))
691             entrypoint_list[n++] = VAEntrypointVLD;
692
693         if (HAS_VP8_ENCODING(i965))
694             entrypoint_list[n++] = VAEntrypointEncSlice;
695
696         break;
697
698     case VAProfileHEVCMain:
699         if (HAS_HEVC_DECODING(i965))
700             entrypoint_list[n++] = VAEntrypointVLD;
701
702         if (HAS_HEVC_ENCODING(i965))
703             entrypoint_list[n++] = VAEntrypointEncSlice;
704
705         break;
706
707     case VAProfileHEVCMain10:
708         if (HAS_HEVC10_DECODING(i965))
709             entrypoint_list[n++] = VAEntrypointVLD;
710
711         if (HAS_HEVC10_ENCODING(i965))
712             entrypoint_list[n++] = VAEntrypointEncSlice;
713
714         break;
715
716     case VAProfileVP9Profile0:
717     case VAProfileVP9Profile2:
718         if (HAS_VP9_DECODING_PROFILE(i965, profile))
719             entrypoint_list[n++] = VAEntrypointVLD;
720
721         if (HAS_VP9_ENCODING(i965) && (profile == VAProfileVP9Profile0))
722             entrypoint_list[n++] = VAEntrypointEncSlice;
723
724         if (HAS_LP_VP9_ENCODING(i965) && (profile == VAProfileVP9Profile0))
725             entrypoint_list[n++] = VAEntrypointEncSliceLP;
726
727         if (profile == VAProfileVP9Profile0) {
728             if (i965->wrapper_pdrvctx) {
729                 VAStatus va_status = VA_STATUS_SUCCESS;
730                 VADriverContextP pdrvctx = i965->wrapper_pdrvctx;
731
732                 CALL_VTABLE(pdrvctx, va_status,
733                             vaQueryConfigEntrypoints(pdrvctx, profile,
734                                                      entrypoint_list,
735                                                      num_entrypoints));
736                 return va_status;
737             }
738         }
739
740         break;
741
742     default:
743         break;
744     }
745
746     /* If the assert fails then I965_MAX_ENTRYPOINTS needs to be bigger */
747     ASSERT_RET(n <= I965_MAX_ENTRYPOINTS, VA_STATUS_ERROR_OPERATION_FAILED);
748     *num_entrypoints = n;
749     return n > 0 ? VA_STATUS_SUCCESS : VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
750 }
751
752 static VAStatus
753 i965_validate_config(VADriverContextP ctx, VAProfile profile,
754                      VAEntrypoint entrypoint)
755 {
756     struct i965_driver_data * const i965 = i965_driver_data(ctx);
757     VAStatus va_status;
758
759     /* Validate profile & entrypoint */
760     switch (profile) {
761     case VAProfileMPEG2Simple:
762     case VAProfileMPEG2Main:
763         if ((HAS_MPEG2_DECODING(i965) && entrypoint == VAEntrypointVLD) ||
764             (HAS_MPEG2_ENCODING(i965) && entrypoint == VAEntrypointEncSlice)) {
765             va_status = VA_STATUS_SUCCESS;
766         } else if (!HAS_MPEG2_DECODING(i965) && !HAS_MPEG2_ENCODING(i965)) {
767             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
768         } else {
769             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
770         }
771         break;
772
773     case VAProfileH264ConstrainedBaseline:
774     case VAProfileH264Main:
775     case VAProfileH264High:
776         if ((HAS_H264_DECODING(i965) && entrypoint == VAEntrypointVLD) ||
777             (HAS_H264_ENCODING(i965) && entrypoint == VAEntrypointEncSlice) ||
778             (HAS_LP_H264_ENCODING(i965) && entrypoint == VAEntrypointEncSliceLP) ||
779             (HAS_FEI_H264_ENCODING(i965) && entrypoint == VAEntrypointFEI) ||
780             (HAS_H264_PREENC(i965) && entrypoint == VAEntrypointStats)) {
781             va_status = VA_STATUS_SUCCESS;
782         } else if (!HAS_H264_DECODING(i965) && !HAS_H264_ENCODING(i965) &&
783                    !HAS_LP_H264_ENCODING(i965) && !HAS_FEI_H264_ENCODING(i965) &&
784                    !HAS_H264_PREENC(i965)) {
785             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
786         } else {
787             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
788         }
789         break;
790
791     case VAProfileVC1Simple:
792     case VAProfileVC1Main:
793     case VAProfileVC1Advanced:
794         if (HAS_VC1_DECODING(i965) && entrypoint == VAEntrypointVLD) {
795             va_status = VA_STATUS_SUCCESS;
796         } else if (!HAS_VC1_DECODING(i965)) {
797             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
798         } else {
799             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
800         }
801         break;
802
803     case VAProfileNone:
804         if (HAS_VPP(i965) && VAEntrypointVideoProc == entrypoint) {
805             va_status = VA_STATUS_SUCCESS;
806         } else if (!HAS_VPP(i965)) {
807             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
808         } else {
809             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
810         }
811         break;
812
813     case VAProfileJPEGBaseline:
814         if ((HAS_JPEG_DECODING(i965) && entrypoint == VAEntrypointVLD) ||
815             (HAS_JPEG_ENCODING(i965) && entrypoint == VAEntrypointEncPicture)) {
816             va_status = VA_STATUS_SUCCESS;
817         } else if (!HAS_JPEG_DECODING(i965) && !HAS_JPEG_ENCODING(i965)) {
818             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
819         } else {
820             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
821         }
822         break;
823
824     case VAProfileVP8Version0_3:
825         if ((HAS_VP8_DECODING(i965) && entrypoint == VAEntrypointVLD) ||
826             (HAS_VP8_ENCODING(i965) && entrypoint == VAEntrypointEncSlice)) {
827             va_status = VA_STATUS_SUCCESS;
828         } else if (!HAS_VP8_DECODING(i965) && !HAS_VP8_ENCODING(i965)) {
829             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
830         } else {
831             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
832         }
833         break;
834
835     case VAProfileH264MultiviewHigh:
836     case VAProfileH264StereoHigh:
837         if ((HAS_H264_MVC_DECODING_PROFILE(i965, profile) &&
838              entrypoint == VAEntrypointVLD) ||
839             (HAS_H264_MVC_ENCODING(i965) &&
840              entrypoint == VAEntrypointEncSlice)) {
841             va_status = VA_STATUS_SUCCESS;
842         } else if (!HAS_H264_MVC_DECODING_PROFILE(i965, profile) &&
843                    !HAS_H264_MVC_ENCODING(i965)) {
844             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
845         } else {
846             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
847         }
848
849         break;
850
851     case VAProfileHEVCMain:
852         if ((HAS_HEVC_DECODING(i965) && (entrypoint == VAEntrypointVLD)) ||
853             (HAS_HEVC_ENCODING(i965) && (entrypoint == VAEntrypointEncSlice))) {
854             va_status = VA_STATUS_SUCCESS;
855         } else if (!HAS_HEVC_DECODING(i965) && !HAS_HEVC_ENCODING(i965)) {
856             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
857         } else {
858             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
859         }
860
861         break;
862
863     case VAProfileHEVCMain10:
864         if ((HAS_HEVC10_DECODING(i965) && (entrypoint == VAEntrypointVLD)) ||
865             (HAS_HEVC10_ENCODING(i965) &&
866              (entrypoint == VAEntrypointEncSlice))) {
867             va_status = VA_STATUS_SUCCESS;
868         } else if (!HAS_HEVC10_DECODING(i965) && !HAS_HEVC10_ENCODING(i965)) {
869             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
870         } else {
871             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
872         }
873
874         break;
875
876     case VAProfileVP9Profile0:
877     case VAProfileVP9Profile2:
878         if ((HAS_VP9_DECODING_PROFILE(i965, profile)) &&
879             (entrypoint == VAEntrypointVLD)) {
880             va_status = VA_STATUS_SUCCESS;
881         } else if ((HAS_VP9_ENCODING_PROFILE(i965, profile)) &&
882                    (entrypoint == VAEntrypointEncSlice)) {
883             va_status = VA_STATUS_SUCCESS;
884         } else if ((HAS_LP_VP9_ENCODING(i965) &&
885                     profile == VAProfileVP9Profile0 &&
886                     entrypoint == VAEntrypointEncSliceLP)) {
887             va_status = VA_STATUS_SUCCESS;
888         } else if (profile == VAProfileVP9Profile0 &&
889                    entrypoint == VAEntrypointVLD &&
890                    i965->wrapper_pdrvctx) {
891             va_status = VA_STATUS_SUCCESS;
892         } else if (!HAS_VP9_DECODING_PROFILE(i965, profile) &&
893                    !HAS_VP9_ENCODING(i965) &&
894                    !HAS_LP_VP9_ENCODING(i965) &&
895                    !i965->wrapper_pdrvctx) {
896             va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
897         } else {
898             va_status = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
899         }
900
901         break;
902
903     default:
904         va_status = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
905         break;
906     }
907     return va_status;
908 }
909
910 static uint32_t
911 i965_get_default_chroma_formats(VADriverContextP ctx, VAProfile profile,
912                                 VAEntrypoint entrypoint)
913 {
914     struct i965_driver_data * const i965 = i965_driver_data(ctx);
915     uint32_t chroma_formats = VA_RT_FORMAT_YUV420;
916
917     switch (profile) {
918     case VAProfileH264ConstrainedBaseline:
919     case VAProfileH264Main:
920     case VAProfileH264High:
921         if (HAS_H264_DECODING(i965) && entrypoint == VAEntrypointVLD)
922             chroma_formats |= i965->codec_info->h264_dec_chroma_formats;
923         break;
924
925     case VAProfileH264MultiviewHigh:
926     case VAProfileH264StereoHigh:
927         if (HAS_H264_MVC_DECODING(i965) && entrypoint == VAEntrypointVLD)
928             chroma_formats |= i965->codec_info->h264_dec_chroma_formats;
929         break;
930
931     case VAProfileJPEGBaseline:
932         if (HAS_JPEG_DECODING(i965) && entrypoint == VAEntrypointVLD)
933             chroma_formats |= i965->codec_info->jpeg_dec_chroma_formats;
934         if (HAS_JPEG_ENCODING(i965) && entrypoint == VAEntrypointEncPicture)
935             chroma_formats |= i965->codec_info->jpeg_enc_chroma_formats;
936         break;
937
938     case VAProfileHEVCMain10:
939         if (HAS_HEVC10_ENCODING(i965) && entrypoint == VAEntrypointEncSlice)
940             chroma_formats = VA_RT_FORMAT_YUV420_10BPP;
941         if (HAS_HEVC10_DECODING(i965) && entrypoint == VAEntrypointVLD)
942             chroma_formats |= i965->codec_info->hevc_dec_chroma_formats;
943         break;
944
945     case VAProfileNone:
946         if (HAS_VPP_P010(i965))
947             chroma_formats |= VA_RT_FORMAT_YUV420_10BPP;
948
949         if (HAS_VPP(i965))
950             chroma_formats |= VA_RT_FORMAT_YUV422 | VA_RT_FORMAT_RGB32;
951         break;
952
953     case VAProfileVP9Profile0:
954     case VAProfileVP9Profile2:
955         if (HAS_VP9_DECODING_PROFILE(i965, profile) && entrypoint == VAEntrypointVLD)
956             chroma_formats |= i965->codec_info->vp9_dec_chroma_formats;
957         break;
958
959     default:
960         break;
961     }
962     return chroma_formats;
963 }
964
965 static uint32_t
966 i965_get_rc_attributes(VADriverContextP ctx, VAProfile profile, VAEntrypoint entrypoint)
967 {
968     struct i965_driver_data * const i965 = i965_driver_data(ctx);
969
970     uint32_t rc_attribs = VA_ATTRIB_NOT_SUPPORTED;
971
972     if (entrypoint == VAEntrypointEncSlice) {
973
974         rc_attribs = VA_RC_CQP;
975
976         if (profile != VAProfileMPEG2Main &&
977             profile != VAProfileMPEG2Simple)
978             rc_attribs |= VA_RC_CBR;
979
980         if (profile == VAProfileVP8Version0_3 ||
981             profile == VAProfileHEVCMain ||
982             profile == VAProfileHEVCMain10)
983             rc_attribs |= VA_RC_VBR;
984
985         if (profile == VAProfileVP9Profile0)
986             rc_attribs = i965->codec_info->vp9_brc_mode;
987
988         if (profile == VAProfileH264ConstrainedBaseline ||
989             profile == VAProfileH264Main ||
990             profile == VAProfileH264High ||
991             profile == VAProfileH264MultiviewHigh ||
992             profile == VAProfileH264StereoHigh)
993             rc_attribs = i965->codec_info->h264_brc_mode;
994
995     } else if (entrypoint == VAEntrypointEncSliceLP) {
996
997         if (profile == VAProfileH264ConstrainedBaseline ||
998             profile == VAProfileH264Main ||
999             profile == VAProfileH264High ||
1000             profile == VAProfileH264MultiviewHigh ||
1001             profile == VAProfileH264StereoHigh)
1002             rc_attribs = i965->codec_info->lp_h264_brc_mode;
1003
1004         else if (profile == VAProfileVP9Profile0)
1005             rc_attribs = i965->codec_info->lp_vp9_brc_mode;
1006
1007     } else if (entrypoint == VAEntrypointFEI) {
1008
1009         if (profile == VAProfileH264ConstrainedBaseline ||
1010             profile == VAProfileH264Main ||
1011             profile == VAProfileH264High)
1012             rc_attribs = VA_RC_CQP;
1013
1014     } else if (entrypoint == VAEntrypointEncPicture) {
1015         if (profile == VAProfileJPEGBaseline)
1016             rc_attribs = VA_RC_CQP;
1017     }
1018
1019     return rc_attribs;
1020 }
1021
1022 static uint32_t
1023 i965_get_enc_packed_attributes(VADriverContextP ctx, VAProfile profile, VAEntrypoint entrypoint)
1024 {
1025     uint32_t enc_packed_attribs = VA_ATTRIB_NOT_SUPPORTED;
1026
1027     if (entrypoint == VAEntrypointEncSlice ||
1028         entrypoint == VAEntrypointEncSliceLP ||
1029         entrypoint == VAEntrypointFEI) {
1030
1031         switch (profile) {
1032         case VAProfileMPEG2Simple:
1033         case VAProfileMPEG2Main:
1034             enc_packed_attribs = VA_ENC_PACKED_HEADER_SEQUENCE | VA_ENC_PACKED_HEADER_PICTURE | VA_ENC_PACKED_HEADER_MISC;
1035             break;
1036
1037         case VAProfileH264ConstrainedBaseline:
1038         case VAProfileH264Main:
1039         case VAProfileH264High:
1040         case VAProfileH264StereoHigh:
1041         case VAProfileH264MultiviewHigh:
1042         case VAProfileHEVCMain:
1043         case VAProfileHEVCMain10:
1044             enc_packed_attribs = VA_ENC_PACKED_HEADER_SEQUENCE | VA_ENC_PACKED_HEADER_PICTURE | VA_ENC_PACKED_HEADER_MISC;
1045             enc_packed_attribs |= (VA_ENC_PACKED_HEADER_RAW_DATA | VA_ENC_PACKED_HEADER_SLICE);
1046             break;
1047
1048         case VAProfileVP9Profile0:
1049             enc_packed_attribs = VA_ENC_PACKED_HEADER_RAW_DATA;
1050             break;
1051
1052         default:
1053             break;
1054         }
1055
1056     } else if (entrypoint == VAEntrypointEncPicture) {
1057         if (profile == VAProfileJPEGBaseline)
1058             enc_packed_attribs = VA_ENC_PACKED_HEADER_RAW_DATA;
1059     }
1060
1061     return enc_packed_attribs;
1062 }
1063
1064 VAStatus
1065 i965_GetConfigAttributes(VADriverContextP ctx,
1066                          VAProfile profile,
1067                          VAEntrypoint entrypoint,
1068                          VAConfigAttrib *attrib_list,  /* in/out */
1069                          int num_attribs)
1070 {
1071     struct i965_driver_data * const i965 = i965_driver_data(ctx);
1072     VAStatus va_status;
1073     int i;
1074
1075     va_status = i965_validate_config(ctx, profile, entrypoint);
1076     if (va_status != VA_STATUS_SUCCESS)
1077         return va_status;
1078
1079     /* Other attributes don't seem to be defined */
1080     /* What to do if we don't know the attribute? */
1081     for (i = 0; i < num_attribs; i++) {
1082         attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1083         switch (attrib_list[i].type) {
1084         case VAConfigAttribRTFormat:
1085             attrib_list[i].value = i965_get_default_chroma_formats(ctx,
1086                                                                    profile, entrypoint);
1087             break;
1088
1089         case VAConfigAttribRateControl:
1090             attrib_list[i].value = i965_get_rc_attributes(ctx, profile, entrypoint);
1091
1092             break;
1093
1094         case VAConfigAttribEncPackedHeaders:
1095             attrib_list[i].value = i965_get_enc_packed_attributes(ctx, profile, entrypoint);
1096
1097             break;
1098
1099         case VAConfigAttribEncMaxRefFrames:
1100             if (entrypoint == VAEntrypointEncSlice || entrypoint == VAEntrypointFEI) {
1101                 attrib_list[i].value = (1 << 16) | (1 << 0);
1102                 if (profile == VAProfileH264ConstrainedBaseline ||
1103                     profile == VAProfileH264Main ||
1104                     profile == VAProfileH264High ||
1105                     profile == VAProfileH264StereoHigh ||
1106                     profile == VAProfileH264MultiviewHigh) {
1107                     if (IS_GEN8(i965->intel.device_info) ||
1108                         IS_GEN9(i965->intel.device_info))
1109                         attrib_list[i].value = (1 << 16) | (4 << 0);
1110                 } else if (profile == VAProfileHEVCMain ||
1111                            profile == VAProfileHEVCMain10) {
1112                     if (IS_GEN9(i965->intel.device_info))
1113                         attrib_list[i].value = (1 << 16) | (3 << 0);
1114                     else if (IS_GEN10(i965->intel.device_info))
1115                         attrib_list[i].value = (1 << 16) | (3 << 0);
1116                 }
1117             } else if (entrypoint == VAEntrypointEncSliceLP) {
1118                 /* Don't support B frame for low power mode */
1119                 if (profile == VAProfileH264ConstrainedBaseline ||
1120                     profile == VAProfileH264Main ||
1121                     profile == VAProfileH264High)
1122                     attrib_list[i].value = (1 << 0);
1123                 else
1124                     attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1125             } else if (entrypoint == VAEntrypointStats)
1126                 attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1127
1128             break;
1129
1130         case VAConfigAttribEncQualityRange:
1131             if (entrypoint == VAEntrypointEncSlice ||
1132                 entrypoint == VAEntrypointEncSliceLP ||
1133                 entrypoint == VAEntrypointFEI) {
1134                 attrib_list[i].value = 1;
1135                 if (profile == VAProfileH264ConstrainedBaseline ||
1136                     profile == VAProfileH264Main ||
1137                     profile == VAProfileH264High) {
1138                     attrib_list[i].value = ENCODER_QUALITY_RANGE;
1139                     if (entrypoint == VAEntrypointEncSlice) {
1140                         if (IS_GEN9(i965->intel.device_info) ||
1141                             IS_GEN10(i965->intel.device_info))
1142                             attrib_list[i].value = ENCODER_QUALITY_RANGE_AVC;
1143                     }
1144                 } else if (profile == VAProfileHEVCMain ||
1145                            profile == VAProfileHEVCMain10)
1146                     attrib_list[i].value = ENCODER_QUALITY_RANGE_HEVC;
1147                 else if (profile == VAProfileVP9Profile0 &&
1148                          entrypoint == VAEntrypointEncSliceLP) {
1149                     attrib_list[i].value = ENCODER_QUALITY_RANGE_VP9;
1150                 }
1151
1152                 break;
1153             } else if (entrypoint == VAEntrypointStats)
1154                 attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1155             break;
1156
1157         case VAConfigAttribEncJPEG:
1158             if (entrypoint == VAEntrypointEncPicture) {
1159                 VAConfigAttribValEncJPEG *configVal = (VAConfigAttribValEncJPEG*) & (attrib_list[i].value);
1160                 (configVal->bits).arithmatic_coding_mode = 0; // Huffman coding is used
1161                 (configVal->bits).progressive_dct_mode = 0;   // Only Sequential DCT is supported
1162                 (configVal->bits).non_interleaved_mode = 1;   // Support both interleaved and non-interleaved
1163                 (configVal->bits).differential_mode = 0;      // Baseline DCT is non-differential
1164                 (configVal->bits).max_num_components = 3;     // Only 3 components supported
1165                 (configVal->bits).max_num_scans = 1;          // Only 1 scan per frame
1166                 (configVal->bits).max_num_huffman_tables = 3; // Max 3 huffman tables
1167                 (configVal->bits).max_num_quantization_tables = 3; // Max 3 quantization tables
1168             }
1169             break;
1170
1171         case VAConfigAttribDecSliceMode:
1172             if (entrypoint == VAEntrypointVLD)
1173                 attrib_list[i].value = VA_DEC_SLICE_MODE_NORMAL;
1174             break;
1175
1176         case VAConfigAttribEncROI:
1177             if (entrypoint == VAEntrypointEncSlice ||
1178                 entrypoint == VAEntrypointEncSliceLP ||
1179                 entrypoint == VAEntrypointFEI) {
1180                 VAConfigAttribValEncROI *roi_config =
1181                     (VAConfigAttribValEncROI *) & (attrib_list[i].value);
1182
1183                 if (profile == VAProfileH264ConstrainedBaseline ||
1184                     profile == VAProfileH264Main ||
1185                     profile == VAProfileH264High) {
1186
1187                     if (IS_GEN9(i965->intel.device_info) &&
1188                         (entrypoint == VAEntrypointEncSlice ||
1189                          entrypoint == VAEntrypointFEI ||
1190                          entrypoint == VAEntrypointStats))
1191                         attrib_list[i].value = 0;
1192                     else if (IS_GEN10(i965->intel.device_info) &&
1193                              (entrypoint == VAEntrypointEncSlice || entrypoint == VAEntrypointFEI))
1194                         attrib_list[i].value = 0;
1195                     else {
1196                         if (entrypoint == VAEntrypointEncSliceLP) {
1197                             roi_config->bits.num_roi_regions = 3;
1198                             roi_config->bits.roi_rc_priority_support = 0;
1199                             roi_config->bits.roi_rc_qp_delta_support = 0;
1200                         } else {
1201                             roi_config->bits.num_roi_regions =
1202                                 I965_MAX_NUM_ROI_REGIONS;
1203                             roi_config->bits.roi_rc_priority_support = 0;
1204                             roi_config->bits.roi_rc_qp_delta_support = 1;
1205                         }
1206                     }
1207                 } else if (profile == VAProfileHEVCMain ||
1208                            profile == VAProfileHEVCMain10) {
1209                     roi_config->bits.num_roi_regions =
1210                         I965_MAX_NUM_ROI_REGIONS;
1211                     roi_config->bits.roi_rc_priority_support = 1;
1212                     roi_config->bits.roi_rc_qp_delta_support = 1;
1213                 } else {
1214                     attrib_list[i].value = 0;
1215                 }
1216             }
1217
1218             break;
1219
1220         case VAConfigAttribEncRateControlExt:
1221             if ((profile == VAProfileH264ConstrainedBaseline ||
1222                  profile == VAProfileH264Main ||
1223                  profile == VAProfileH264High) &&
1224                 entrypoint == VAEntrypointEncSlice) {
1225                 if (IS_GEN9(i965->intel.device_info))
1226                     attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1227                 else if (IS_GEN10(i965->intel.device_info))
1228                     attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1229                 else {
1230                     VAConfigAttribValEncRateControlExt *val_config = (VAConfigAttribValEncRateControlExt *) & (attrib_list[i].value);
1231
1232                     val_config->bits.max_num_temporal_layers_minus1 = MAX_TEMPORAL_LAYERS - 1;
1233                     val_config->bits.temporal_layer_bitrate_control_flag = 1;
1234                 }
1235             } else {
1236                 attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1237             }
1238
1239             break;
1240
1241         case VAConfigAttribEncMaxSlices:
1242             attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1243             if (entrypoint == VAEntrypointEncSlice) {
1244                 if ((profile == VAProfileH264ConstrainedBaseline ||
1245                      profile == VAProfileH264Main ||
1246                      profile == VAProfileH264High) ||
1247                     profile == VAProfileH264StereoHigh ||
1248                     profile == VAProfileH264MultiviewHigh) {
1249                     attrib_list[i].value = I965_MAX_NUM_SLICE;
1250                 } else if (profile == VAProfileHEVCMain ||
1251                            profile == VAProfileHEVCMain10) {
1252                     attrib_list[i].value = I965_MAX_NUM_SLICE;
1253                 }
1254             } else if (entrypoint == VAEntrypointEncSliceLP || entrypoint == VAEntrypointFEI) {
1255                 if ((profile == VAProfileH264ConstrainedBaseline ||
1256                      profile == VAProfileH264Main ||
1257                      profile == VAProfileH264High) ||
1258                     profile == VAProfileH264StereoHigh ||
1259                     profile == VAProfileH264MultiviewHigh)
1260                     attrib_list[i].value = I965_MAX_NUM_SLICE;
1261             }
1262
1263             break;
1264
1265         case VAConfigAttribEncSliceStructure:
1266             attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1267             if (entrypoint == VAEntrypointEncSlice) {
1268                 if ((profile == VAProfileH264ConstrainedBaseline ||
1269                      profile == VAProfileH264Main ||
1270                      profile == VAProfileH264High) ||
1271                     profile == VAProfileH264StereoHigh ||
1272                     profile == VAProfileH264MultiviewHigh ||
1273                     profile == VAProfileHEVCMain ||
1274                     profile == VAProfileHEVCMain10) {
1275                     attrib_list[i].value = VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS;
1276                 }
1277             }
1278
1279             break;
1280
1281         case VAConfigAttribFEIFunctionType:
1282             /* Supporing all possible modes of FEI */
1283             attrib_list[i].value = VA_FEI_FUNCTION_ENC |
1284                                    VA_FEI_FUNCTION_PAK | VA_FEI_FUNCTION_ENC_PAK;
1285             break;
1286
1287         case VAConfigAttribFEIMVPredictors:
1288             attrib_list[i].value = 4;
1289             break;
1290
1291         case VAConfigAttribStats:
1292             if (entrypoint == VAEntrypointStats) {
1293                 VAConfigAttribValStats *configVal =
1294                     (VAConfigAttribValStats*) & (attrib_list[i].value);
1295                 (configVal->bits).max_num_past_references = STATS_MAX_NUM_PAST_REFS;
1296                 (configVal->bits).max_num_future_references = STATS_MAX_NUM_FUTURE_REFS;
1297                 (configVal->bits).num_outputs = STATS_MAX_NUM_OUTPUTS;
1298                 (configVal->bits).interlaced = STATS_INTERLACED_SUPPORT;
1299             }
1300             break;
1301
1302         default:
1303             /* Do nothing */
1304             attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1305             break;
1306         }
1307     }
1308
1309     return VA_STATUS_SUCCESS;
1310 }
1311
1312 static void
1313 i965_destroy_config(struct object_heap *heap, struct object_base *obj)
1314 {
1315     object_heap_free(heap, obj);
1316 }
1317
1318 static VAConfigAttrib *
1319 i965_lookup_config_attribute(struct object_config *obj_config,
1320                              VAConfigAttribType type)
1321 {
1322     int i;
1323
1324     for (i = 0; i < obj_config->num_attribs; i++) {
1325         VAConfigAttrib * const attrib = &obj_config->attrib_list[i];
1326         if (attrib->type == type)
1327             return attrib;
1328     }
1329     return NULL;
1330 }
1331
1332 static VAStatus
1333 i965_append_config_attribute(struct object_config *obj_config,
1334                              const VAConfigAttrib *new_attrib)
1335 {
1336     VAConfigAttrib *attrib;
1337
1338     if (obj_config->num_attribs >= I965_MAX_CONFIG_ATTRIBUTES)
1339         return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
1340
1341     attrib = &obj_config->attrib_list[obj_config->num_attribs++];
1342     attrib->type = new_attrib->type;
1343     attrib->value = new_attrib->value;
1344     return VA_STATUS_SUCCESS;
1345 }
1346
1347 static VAStatus
1348 i965_ensure_config_attribute(struct object_config *obj_config,
1349                              const VAConfigAttrib *new_attrib)
1350 {
1351     VAConfigAttrib *attrib;
1352
1353     /* Check for existing attributes */
1354     attrib = i965_lookup_config_attribute(obj_config, new_attrib->type);
1355     if (attrib) {
1356         /* Update existing attribute */
1357         attrib->value = new_attrib->value;
1358         return VA_STATUS_SUCCESS;
1359     }
1360     return i965_append_config_attribute(obj_config, new_attrib);
1361 }
1362
1363 VAStatus
1364 i965_CreateConfig(VADriverContextP ctx,
1365                   VAProfile profile,
1366                   VAEntrypoint entrypoint,
1367                   VAConfigAttrib *attrib_list,
1368                   int num_attribs,
1369                   VAConfigID *config_id)        /* out */
1370 {
1371     struct i965_driver_data * const i965 = i965_driver_data(ctx);
1372     struct object_config *obj_config;
1373     int configID;
1374     int i;
1375     VAStatus vaStatus;
1376
1377     vaStatus = i965_validate_config(ctx, profile, entrypoint);
1378
1379     if (VA_STATUS_SUCCESS != vaStatus) {
1380         return vaStatus;
1381     }
1382
1383     configID = NEW_CONFIG_ID();
1384     obj_config = CONFIG(configID);
1385
1386     if (NULL == obj_config) {
1387         vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
1388         return vaStatus;
1389     }
1390
1391     obj_config->profile = profile;
1392     obj_config->entrypoint = entrypoint;
1393     obj_config->num_attribs = 0;
1394     obj_config->wrapper_config = VA_INVALID_ID;
1395
1396     for (i = 0; i < num_attribs; i++) {
1397         // add it later and ignore the user input for VAConfigAttribEncMaxSlices
1398         if (attrib_list[i].type == VAConfigAttribEncMaxSlices ||
1399             attrib_list[i].type == VAConfigAttribEncSliceStructure)
1400             continue;
1401         vaStatus = i965_ensure_config_attribute(obj_config, &attrib_list[i]);
1402         if (vaStatus != VA_STATUS_SUCCESS)
1403             break;
1404     }
1405
1406     if (vaStatus == VA_STATUS_SUCCESS) {
1407         VAConfigAttrib attrib, *attrib_found;
1408         attrib.type = VAConfigAttribRTFormat;
1409         attrib.value = i965_get_default_chroma_formats(ctx, profile, entrypoint);
1410         attrib_found = i965_lookup_config_attribute(obj_config, attrib.type);
1411         if (!attrib_found || !attrib_found->value)
1412             vaStatus = i965_append_config_attribute(obj_config, &attrib);
1413         else if (!(attrib_found->value & attrib.value))
1414             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
1415     }
1416
1417
1418     if (vaStatus == VA_STATUS_SUCCESS) {
1419         VAConfigAttrib *attrib_found = i965_lookup_config_attribute(obj_config, VAConfigAttribRateControl);
1420
1421         if (attrib_found) {
1422             uint32_t rc_attribs = i965_get_rc_attributes(ctx, profile, entrypoint);
1423
1424             if (!(attrib_found->value & rc_attribs))
1425                 vaStatus = VA_STATUS_ERROR_INVALID_VALUE;
1426         }
1427     }
1428
1429     if (vaStatus == VA_STATUS_SUCCESS) {
1430         VAConfigAttrib *attrib_found = i965_lookup_config_attribute(obj_config, VAConfigAttribEncPackedHeaders);
1431
1432         if (attrib_found) {
1433             uint32_t enc_packed_attribs = i965_get_enc_packed_attributes(ctx, profile, entrypoint);
1434
1435             if (!(attrib_found->value & enc_packed_attribs))
1436                 vaStatus = VA_STATUS_ERROR_INVALID_VALUE;
1437         }
1438     }
1439
1440     if (vaStatus == VA_STATUS_SUCCESS) {
1441         VAConfigAttrib attrib;
1442         attrib.type = VAConfigAttribEncMaxSlices;
1443         attrib.value = VA_ATTRIB_NOT_SUPPORTED;
1444         if (entrypoint == VAEntrypointEncSlice) {
1445             if ((profile == VAProfileH264ConstrainedBaseline ||
1446                  profile == VAProfileH264Main ||
1447                  profile == VAProfileH264High) ||
1448                 profile == VAProfileH264StereoHigh ||
1449                 profile == VAProfileH264MultiviewHigh) {
1450                 attrib.value = I965_MAX_NUM_SLICE;
1451             } else if (profile == VAProfileHEVCMain ||
1452                        profile == VAProfileHEVCMain10)
1453                 attrib.value = I965_MAX_NUM_SLICE;
1454         } else if (entrypoint == VAEntrypointEncSliceLP || entrypoint == VAEntrypointFEI) {
1455             if ((profile == VAProfileH264ConstrainedBaseline ||
1456                  profile == VAProfileH264Main ||
1457                  profile == VAProfileH264High) ||
1458                 profile == VAProfileH264StereoHigh ||
1459                 profile == VAProfileH264MultiviewHigh)
1460                 attrib.value = I965_MAX_NUM_SLICE;
1461         }
1462
1463         if (attrib.value != VA_ATTRIB_NOT_SUPPORTED)
1464             vaStatus = i965_append_config_attribute(obj_config, &attrib);
1465     }
1466
1467     if (vaStatus == VA_STATUS_SUCCESS) {
1468         VAConfigAttrib attrib;
1469         attrib.type = VAConfigAttribEncSliceStructure;
1470         attrib.value = VA_ATTRIB_NOT_SUPPORTED;
1471         if (entrypoint == VAEntrypointEncSlice) {
1472             if ((profile == VAProfileH264ConstrainedBaseline ||
1473                  profile == VAProfileH264Main ||
1474                  profile == VAProfileH264High) ||
1475                 profile == VAProfileH264StereoHigh ||
1476                 profile == VAProfileH264MultiviewHigh) {
1477                 attrib.value = VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS;
1478             }
1479         }
1480
1481         if (attrib.value != VA_ATTRIB_NOT_SUPPORTED)
1482             vaStatus = i965_append_config_attribute(obj_config, &attrib);
1483     }
1484
1485     if (vaStatus == VA_STATUS_SUCCESS) {
1486         VAConfigAttrib *attrib_found;
1487
1488         attrib_found = i965_lookup_config_attribute(obj_config, VAConfigAttribDecSliceMode);
1489
1490         if (attrib_found &&
1491             (entrypoint != VAEntrypointVLD ||
1492              attrib_found->value != VA_DEC_SLICE_MODE_NORMAL))
1493             vaStatus = VA_STATUS_ERROR_INVALID_VALUE;
1494     }
1495
1496     if ((vaStatus == VA_STATUS_SUCCESS) &&
1497         (profile == VAProfileVP9Profile0) &&
1498         (entrypoint == VAEntrypointVLD) &&
1499         !HAS_VP9_DECODING(i965)) {
1500
1501         if (i965->wrapper_pdrvctx) {
1502             VAGenericID wrapper_config;
1503
1504             CALL_VTABLE(i965->wrapper_pdrvctx, vaStatus,
1505                         vaCreateConfig(i965->wrapper_pdrvctx, profile,
1506                                        entrypoint, attrib_list,
1507                                        num_attribs, &wrapper_config));
1508
1509             if (vaStatus == VA_STATUS_SUCCESS)
1510                 obj_config->wrapper_config = wrapper_config;
1511         }
1512     }
1513
1514     /* Error recovery */
1515     if (VA_STATUS_SUCCESS != vaStatus) {
1516         i965_destroy_config(&i965->config_heap, (struct object_base *)obj_config);
1517     } else {
1518         *config_id = configID;
1519     }
1520
1521     return vaStatus;
1522 }
1523
1524 VAStatus
1525 i965_DestroyConfig(VADriverContextP ctx, VAConfigID config_id)
1526 {
1527     struct i965_driver_data *i965 = i965_driver_data(ctx);
1528     struct object_config *obj_config = CONFIG(config_id);
1529     VAStatus vaStatus;
1530
1531     if (NULL == obj_config) {
1532         vaStatus = VA_STATUS_ERROR_INVALID_CONFIG;
1533         return vaStatus;
1534     }
1535
1536     if ((obj_config->wrapper_config != VA_INVALID_ID) &&
1537         i965->wrapper_pdrvctx) {
1538         CALL_VTABLE(i965->wrapper_pdrvctx, vaStatus,
1539                     vaDestroyConfig(i965->wrapper_pdrvctx,
1540                                     obj_config->wrapper_config));
1541         obj_config->wrapper_config = VA_INVALID_ID;
1542     }
1543
1544     i965_destroy_config(&i965->config_heap, (struct object_base *)obj_config);
1545     return VA_STATUS_SUCCESS;
1546 }
1547
1548 VAStatus i965_QueryConfigAttributes(VADriverContextP ctx,
1549                                     VAConfigID config_id,
1550                                     VAProfile *profile,                 /* out */
1551                                     VAEntrypoint *entrypoint,           /* out */
1552                                     VAConfigAttrib *attrib_list,        /* out */
1553                                     int *num_attribs)                   /* out */
1554 {
1555     struct i965_driver_data *i965 = i965_driver_data(ctx);
1556     struct object_config *obj_config = CONFIG(config_id);
1557     VAStatus vaStatus = VA_STATUS_SUCCESS;
1558     int i;
1559
1560     ASSERT_RET(obj_config, VA_STATUS_ERROR_INVALID_CONFIG);
1561     *profile = obj_config->profile;
1562     *entrypoint = obj_config->entrypoint;
1563     *num_attribs = obj_config->num_attribs;
1564
1565     for (i = 0; i < obj_config->num_attribs; i++) {
1566         attrib_list[i] = obj_config->attrib_list[i];
1567     }
1568
1569     return vaStatus;
1570 }
1571
1572 void
1573 i965_destroy_surface_storage(struct object_surface *obj_surface)
1574 {
1575     if (!obj_surface)
1576         return;
1577
1578     dri_bo_unreference(obj_surface->bo);
1579     obj_surface->bo = NULL;
1580
1581     if (obj_surface->free_private_data != NULL) {
1582         obj_surface->free_private_data(&obj_surface->private_data);
1583         obj_surface->private_data = NULL;
1584     }
1585 }
1586
1587 static void
1588 i965_destroy_surface(struct object_heap *heap, struct object_base *obj)
1589 {
1590     struct object_surface *obj_surface = (struct object_surface *)obj;
1591
1592     i965_destroy_surface_storage(obj_surface);
1593     object_heap_free(heap, obj);
1594 }
1595
1596 /* byte-per-pixel of the first plane */
1597 static int
1598 bpp_1stplane_by_fourcc(unsigned int fourcc)
1599 {
1600     const i965_fourcc_info *info = get_fourcc_info(fourcc);
1601
1602     if (info && (info->flag & I_S))
1603         return info->bpp[0] / 8;
1604     else
1605         return 0;
1606 }
1607
1608 static VAStatus
1609 i965_surface_native_memory(VADriverContextP ctx,
1610                            struct object_surface *obj_surface,
1611                            int format,
1612                            int expected_fourcc)
1613 {
1614     struct i965_driver_data *i965 = i965_driver_data(ctx);
1615     int tiling = HAS_TILED_SURFACE(i965);
1616
1617     if (!expected_fourcc)
1618         return VA_STATUS_SUCCESS;
1619
1620     // todo, should we disable tiling for 422 format?
1621     if (expected_fourcc == VA_FOURCC_I420 ||
1622         expected_fourcc == VA_FOURCC_IYUV ||
1623         expected_fourcc == VA_FOURCC_I010 ||
1624         expected_fourcc == VA_FOURCC_YV12 ||
1625         expected_fourcc == VA_FOURCC_YV16)
1626         tiling = 0;
1627
1628     return i965_check_alloc_surface_bo(ctx, obj_surface, tiling, expected_fourcc, get_sampling_from_fourcc(expected_fourcc));
1629 }
1630
1631 static VAStatus
1632 i965_suface_external_memory(VADriverContextP ctx,
1633                             struct object_surface *obj_surface,
1634                             int external_memory_type,
1635                             VASurfaceAttribExternalBuffers *memory_attibute,
1636                             int index)
1637 {
1638     struct i965_driver_data *i965 = i965_driver_data(ctx);
1639     unsigned int tiling, swizzle;
1640
1641     if (!memory_attibute ||
1642         !memory_attibute->buffers ||
1643         index >= memory_attibute->num_buffers)
1644         return VA_STATUS_ERROR_INVALID_PARAMETER;
1645
1646     obj_surface->size = memory_attibute->data_size;
1647     if (external_memory_type == I965_SURFACE_MEM_GEM_FLINK)
1648         obj_surface->bo = drm_intel_bo_gem_create_from_name(i965->intel.bufmgr,
1649                                                             "gem flinked vaapi surface",
1650                                                             memory_attibute->buffers[index]);
1651     else if (external_memory_type == I965_SURFACE_MEM_DRM_PRIME)
1652         obj_surface->bo = drm_intel_bo_gem_create_from_prime(i965->intel.bufmgr,
1653                                                              memory_attibute->buffers[index],
1654                                                              obj_surface->size);
1655
1656     if (!obj_surface->bo)
1657         return VA_STATUS_ERROR_INVALID_PARAMETER;
1658
1659     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
1660
1661     ASSERT_RET(obj_surface->orig_width == memory_attibute->width, VA_STATUS_ERROR_INVALID_PARAMETER);
1662     ASSERT_RET(obj_surface->orig_height == memory_attibute->height, VA_STATUS_ERROR_INVALID_PARAMETER);
1663     ASSERT_RET(memory_attibute->num_planes >= 1, VA_STATUS_ERROR_INVALID_PARAMETER);
1664
1665     obj_surface->fourcc = memory_attibute->pixel_format;
1666     obj_surface->width = memory_attibute->pitches[0];
1667     int bpp_1stplane = bpp_1stplane_by_fourcc(obj_surface->fourcc);
1668     ASSERT_RET(IS_ALIGNED(obj_surface->width, 16), VA_STATUS_ERROR_INVALID_PARAMETER);
1669     ASSERT_RET(obj_surface->width >= obj_surface->orig_width * bpp_1stplane, VA_STATUS_ERROR_INVALID_PARAMETER);
1670
1671     if (memory_attibute->num_planes == 1)
1672         obj_surface->height = memory_attibute->data_size / obj_surface->width;
1673     else
1674         obj_surface->height = memory_attibute->offsets[1] / obj_surface->width;
1675
1676     if (memory_attibute->num_planes > 1) {
1677         ASSERT_RET(obj_surface->height >= obj_surface->orig_height, VA_STATUS_ERROR_INVALID_PARAMETER);
1678     }
1679
1680     if (tiling) {
1681         ASSERT_RET(IS_ALIGNED(obj_surface->width, 128), VA_STATUS_ERROR_INVALID_PARAMETER);
1682
1683         if (memory_attibute->num_planes > 1)
1684             ASSERT_RET(IS_ALIGNED(obj_surface->height, 32), VA_STATUS_ERROR_INVALID_PARAMETER);
1685     } else {
1686         ASSERT_RET(IS_ALIGNED(obj_surface->width, i965->codec_info->min_linear_wpitch), VA_STATUS_ERROR_INVALID_PARAMETER);
1687
1688         if (memory_attibute->num_planes > 1)
1689             ASSERT_RET(IS_ALIGNED(obj_surface->height, i965->codec_info->min_linear_hpitch), VA_STATUS_ERROR_INVALID_PARAMETER);
1690     }
1691
1692     obj_surface->x_cb_offset = 0; /* X offset is always 0 */
1693     obj_surface->x_cr_offset = 0;
1694     if ((obj_surface->fourcc == VA_FOURCC_I420 ||
1695          obj_surface->fourcc == VA_FOURCC_IYUV ||
1696          obj_surface->fourcc == VA_FOURCC_I010 ||
1697          obj_surface->fourcc == VA_FOURCC_YV12 ||
1698          obj_surface->fourcc == VA_FOURCC_YV16) && tiling)
1699         return VA_STATUS_ERROR_INVALID_PARAMETER;
1700
1701     switch (obj_surface->fourcc) {
1702     case VA_FOURCC_NV12:
1703     case VA_FOURCC_P010:
1704         ASSERT_RET(memory_attibute->num_planes == 2, VA_STATUS_ERROR_INVALID_PARAMETER);
1705         ASSERT_RET(memory_attibute->pitches[0] == memory_attibute->pitches[1], VA_STATUS_ERROR_INVALID_PARAMETER);
1706
1707         obj_surface->subsampling = SUBSAMPLE_YUV420;
1708         obj_surface->y_cb_offset = obj_surface->height;
1709         obj_surface->y_cr_offset = obj_surface->height;
1710         obj_surface->cb_cr_width = obj_surface->orig_width / 2;
1711         obj_surface->cb_cr_height = obj_surface->orig_height / 2;
1712         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1713         if (tiling)
1714             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, 128), VA_STATUS_ERROR_INVALID_PARAMETER);
1715         else
1716             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, i965->codec_info->min_linear_wpitch), VA_STATUS_ERROR_INVALID_PARAMETER);
1717
1718         break;
1719
1720     case VA_FOURCC_YV12:
1721     case VA_FOURCC_IMC1:
1722         ASSERT_RET(memory_attibute->num_planes == 3, VA_STATUS_ERROR_INVALID_PARAMETER);
1723         ASSERT_RET(memory_attibute->pitches[1] == memory_attibute->pitches[2], VA_STATUS_ERROR_INVALID_PARAMETER);
1724
1725         obj_surface->subsampling = SUBSAMPLE_YUV420;
1726         obj_surface->y_cr_offset = obj_surface->height;
1727         obj_surface->y_cb_offset = memory_attibute->offsets[2] / obj_surface->width;
1728         obj_surface->cb_cr_width = obj_surface->orig_width / 2;
1729         obj_surface->cb_cr_height = obj_surface->orig_height / 2;
1730         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1731
1732         if (tiling)
1733             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, 128), VA_STATUS_ERROR_INVALID_PARAMETER);
1734         else
1735             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, i965->codec_info->min_linear_wpitch), VA_STATUS_ERROR_INVALID_PARAMETER);
1736
1737         break;
1738
1739     case VA_FOURCC_I420:
1740     case VA_FOURCC_IYUV:
1741     case VA_FOURCC_IMC3:
1742     case VA_FOURCC_I010:
1743         ASSERT_RET(memory_attibute->num_planes == 3, VA_STATUS_ERROR_INVALID_PARAMETER);
1744         ASSERT_RET(memory_attibute->pitches[1] == memory_attibute->pitches[2], VA_STATUS_ERROR_INVALID_PARAMETER);
1745
1746         obj_surface->subsampling = SUBSAMPLE_YUV420;
1747         obj_surface->y_cb_offset = obj_surface->height;
1748         obj_surface->y_cr_offset = memory_attibute->offsets[2] / obj_surface->width;
1749         obj_surface->cb_cr_width = obj_surface->orig_width / 2;
1750         obj_surface->cb_cr_height = obj_surface->orig_height / 2;
1751         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1752         if (tiling)
1753             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, 128), VA_STATUS_ERROR_INVALID_PARAMETER);
1754         else
1755             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, i965->codec_info->min_linear_wpitch), VA_STATUS_ERROR_INVALID_PARAMETER);
1756
1757         break;
1758
1759     case VA_FOURCC_YUY2:
1760     case VA_FOURCC_UYVY:
1761         ASSERT_RET(memory_attibute->num_planes == 1, VA_STATUS_ERROR_INVALID_PARAMETER);
1762
1763         obj_surface->subsampling = SUBSAMPLE_YUV422H;
1764         obj_surface->y_cb_offset = 0;
1765         obj_surface->y_cr_offset = 0;
1766         obj_surface->cb_cr_width = obj_surface->orig_width / 2;
1767         obj_surface->cb_cr_height = obj_surface->orig_height;
1768         obj_surface->cb_cr_pitch = memory_attibute->pitches[0];
1769
1770         break;
1771
1772     case VA_FOURCC_RGBA:
1773     case VA_FOURCC_RGBX:
1774     case VA_FOURCC_BGRA:
1775     case VA_FOURCC_BGRX:
1776         ASSERT_RET(memory_attibute->num_planes == 1, VA_STATUS_ERROR_INVALID_PARAMETER);
1777
1778         obj_surface->subsampling = SUBSAMPLE_RGBX;
1779         obj_surface->y_cb_offset = 0;
1780         obj_surface->y_cr_offset = 0;
1781         obj_surface->cb_cr_width = 0;
1782         obj_surface->cb_cr_height = 0;
1783         obj_surface->cb_cr_pitch = 0;
1784
1785         break;
1786
1787     case VA_FOURCC_Y800: /* monochrome surface */
1788         ASSERT_RET(memory_attibute->num_planes == 1, VA_STATUS_ERROR_INVALID_PARAMETER);
1789
1790         obj_surface->subsampling = SUBSAMPLE_YUV400;
1791         obj_surface->y_cb_offset = 0;
1792         obj_surface->y_cr_offset = 0;
1793         obj_surface->cb_cr_width = 0;
1794         obj_surface->cb_cr_height = 0;
1795         obj_surface->cb_cr_pitch = 0;
1796
1797         break;
1798
1799     case VA_FOURCC_411P:
1800         ASSERT_RET(memory_attibute->num_planes == 3, VA_STATUS_ERROR_INVALID_PARAMETER);
1801         ASSERT_RET(memory_attibute->pitches[1] == memory_attibute->pitches[2], VA_STATUS_ERROR_INVALID_PARAMETER);
1802
1803         obj_surface->subsampling = SUBSAMPLE_YUV411;
1804         obj_surface->y_cb_offset = obj_surface->height;
1805         obj_surface->y_cr_offset = memory_attibute->offsets[2] / obj_surface->width;
1806         obj_surface->cb_cr_width = obj_surface->orig_width / 4;
1807         obj_surface->cb_cr_height = obj_surface->orig_height;
1808         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1809         if (tiling)
1810             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, 128), VA_STATUS_ERROR_INVALID_PARAMETER);
1811         else
1812             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, i965->codec_info->min_linear_wpitch), VA_STATUS_ERROR_INVALID_PARAMETER);
1813         break;
1814
1815     case VA_FOURCC_422H:
1816         ASSERT_RET(memory_attibute->num_planes == 3, VA_STATUS_ERROR_INVALID_PARAMETER);
1817         ASSERT_RET(memory_attibute->pitches[1] == memory_attibute->pitches[2], VA_STATUS_ERROR_INVALID_PARAMETER);
1818
1819         obj_surface->subsampling = SUBSAMPLE_YUV422H;
1820         obj_surface->y_cb_offset = obj_surface->height;
1821         obj_surface->y_cr_offset = memory_attibute->offsets[2] / obj_surface->width;
1822         obj_surface->cb_cr_width = obj_surface->orig_width / 2;
1823         obj_surface->cb_cr_height = obj_surface->orig_height;
1824         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1825         if (tiling)
1826             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, 128), VA_STATUS_ERROR_INVALID_PARAMETER);
1827         else
1828             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, i965->codec_info->min_linear_wpitch), VA_STATUS_ERROR_INVALID_PARAMETER);
1829
1830         break;
1831
1832     case VA_FOURCC_YV16:
1833         ASSERT_RET(memory_attibute->num_planes == 3, VA_STATUS_ERROR_INVALID_PARAMETER);
1834         ASSERT_RET(memory_attibute->pitches[1] == memory_attibute->pitches[2], VA_STATUS_ERROR_INVALID_PARAMETER);
1835
1836         obj_surface->subsampling = SUBSAMPLE_YUV422H;
1837         obj_surface->y_cr_offset = memory_attibute->offsets[1] / obj_surface->width;
1838         obj_surface->y_cb_offset = memory_attibute->offsets[2] / obj_surface->width;
1839         obj_surface->cb_cr_width = obj_surface->orig_width / 2;
1840         obj_surface->cb_cr_height = obj_surface->orig_height;
1841         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1842         ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, i965->codec_info->min_linear_wpitch), VA_STATUS_ERROR_INVALID_PARAMETER);
1843
1844         break;
1845
1846     case VA_FOURCC_422V:
1847         ASSERT_RET(memory_attibute->num_planes == 3, VA_STATUS_ERROR_INVALID_PARAMETER);
1848         ASSERT_RET(memory_attibute->pitches[1] == memory_attibute->pitches[2], VA_STATUS_ERROR_INVALID_PARAMETER);
1849
1850         obj_surface->subsampling = SUBSAMPLE_YUV422H;
1851         obj_surface->y_cb_offset = obj_surface->height;
1852         obj_surface->y_cr_offset = memory_attibute->offsets[2] / obj_surface->width;
1853         obj_surface->cb_cr_width = obj_surface->orig_width;
1854         obj_surface->cb_cr_height = obj_surface->orig_height / 2;
1855         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1856         if (tiling)
1857             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, 128), VA_STATUS_ERROR_INVALID_PARAMETER);
1858         else
1859             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, i965->codec_info->min_linear_wpitch), VA_STATUS_ERROR_INVALID_PARAMETER);
1860
1861         break;
1862
1863     case VA_FOURCC_444P:
1864         ASSERT_RET(memory_attibute->num_planes == 3, VA_STATUS_ERROR_INVALID_PARAMETER);
1865         ASSERT_RET(memory_attibute->pitches[1] == memory_attibute->pitches[2], VA_STATUS_ERROR_INVALID_PARAMETER);
1866
1867         obj_surface->subsampling = SUBSAMPLE_YUV444;
1868         obj_surface->y_cb_offset = obj_surface->height;
1869         obj_surface->y_cr_offset = memory_attibute->offsets[2] / obj_surface->width;
1870         obj_surface->cb_cr_width = obj_surface->orig_width;
1871         obj_surface->cb_cr_height = obj_surface->orig_height;
1872         obj_surface->cb_cr_pitch = memory_attibute->pitches[1];
1873         if (tiling)
1874             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, 128), VA_STATUS_ERROR_INVALID_PARAMETER);
1875         else
1876             ASSERT_RET(IS_ALIGNED(obj_surface->cb_cr_pitch, i965->codec_info->min_linear_wpitch), VA_STATUS_ERROR_INVALID_PARAMETER);
1877
1878         break;
1879
1880     default:
1881         return VA_STATUS_ERROR_INVALID_PARAMETER;
1882     }
1883
1884     return VA_STATUS_SUCCESS;
1885 }
1886
1887 static VAStatus
1888 i965_CreateSurfaces2(
1889     VADriverContextP    ctx,
1890     unsigned int        format,
1891     unsigned int        width,
1892     unsigned int        height,
1893     VASurfaceID        *surfaces,
1894     unsigned int        num_surfaces,
1895     VASurfaceAttrib    *attrib_list,
1896     unsigned int        num_attribs
1897 )
1898 {
1899     struct i965_driver_data *i965 = i965_driver_data(ctx);
1900     int i, j;
1901     VAStatus vaStatus = VA_STATUS_SUCCESS;
1902     int expected_fourcc = 0;
1903     int memory_type = I965_SURFACE_MEM_NATIVE; /* native */
1904     VASurfaceAttribExternalBuffers *memory_attibute = NULL;
1905
1906     for (i = 0; i < num_attribs && attrib_list; i++) {
1907         if ((attrib_list[i].type == VASurfaceAttribPixelFormat) &&
1908             (attrib_list[i].flags & VA_SURFACE_ATTRIB_SETTABLE)) {
1909             ASSERT_RET(attrib_list[i].value.type == VAGenericValueTypeInteger, VA_STATUS_ERROR_INVALID_PARAMETER);
1910             expected_fourcc = attrib_list[i].value.value.i;
1911         }
1912
1913         if ((attrib_list[i].type == VASurfaceAttribMemoryType) &&
1914             (attrib_list[i].flags & VA_SURFACE_ATTRIB_SETTABLE)) {
1915
1916             ASSERT_RET(attrib_list[i].value.type == VAGenericValueTypeInteger, VA_STATUS_ERROR_INVALID_PARAMETER);
1917
1918             if (attrib_list[i].value.value.i == VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM)
1919                 memory_type = I965_SURFACE_MEM_GEM_FLINK; /* flinked GEM handle */
1920             else if (attrib_list[i].value.value.i == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME)
1921                 memory_type = I965_SURFACE_MEM_DRM_PRIME; /* drm prime fd */
1922             else if (attrib_list[i].value.value.i == VA_SURFACE_ATTRIB_MEM_TYPE_VA)
1923                 memory_type = I965_SURFACE_MEM_NATIVE; /* va native memory, to be allocated */
1924         }
1925
1926         if ((attrib_list[i].type == VASurfaceAttribExternalBufferDescriptor) &&
1927             (attrib_list[i].flags == VA_SURFACE_ATTRIB_SETTABLE)) {
1928             ASSERT_RET(attrib_list[i].value.type == VAGenericValueTypePointer, VA_STATUS_ERROR_INVALID_PARAMETER);
1929             memory_attibute = (VASurfaceAttribExternalBuffers *)attrib_list[i].value.value.p;
1930         }
1931     }
1932
1933     /* support 420 & 422 & RGB32 format, 422 and RGB32 are only used
1934      * for post-processing (including color conversion) */
1935     if (VA_RT_FORMAT_YUV420 != format &&
1936         VA_RT_FORMAT_YUV420_10BPP != format &&
1937         VA_RT_FORMAT_YUV422 != format &&
1938         VA_RT_FORMAT_YUV444 != format &&
1939         VA_RT_FORMAT_YUV411 != format &&
1940         VA_RT_FORMAT_YUV400 != format &&
1941         VA_RT_FORMAT_RGB32  != format) {
1942         return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
1943     }
1944
1945     for (i = 0; i < num_surfaces; i++) {
1946         int surfaceID = NEW_SURFACE_ID();
1947         struct object_surface *obj_surface = SURFACE(surfaceID);
1948
1949         if (NULL == obj_surface) {
1950             vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
1951             break;
1952         }
1953
1954         surfaces[i] = surfaceID;
1955         obj_surface->status = VASurfaceReady;
1956         obj_surface->orig_width = width;
1957         obj_surface->orig_height = height;
1958         obj_surface->user_disable_tiling = false;
1959         obj_surface->user_h_stride_set = false;
1960         obj_surface->user_v_stride_set = false;
1961         obj_surface->border_cleared = false;
1962
1963         obj_surface->subpic_render_idx = 0;
1964         for (j = 0; j < I965_MAX_SUBPIC_SUM; j++) {
1965             obj_surface->subpic[j] = VA_INVALID_ID;
1966             obj_surface->obj_subpic[j] = NULL;
1967         }
1968
1969         assert(i965->codec_info->min_linear_wpitch);
1970         assert(i965->codec_info->min_linear_hpitch);
1971         obj_surface->width = ALIGN(width, i965->codec_info->min_linear_wpitch);
1972         obj_surface->height = ALIGN(height, i965->codec_info->min_linear_hpitch);
1973         obj_surface->flags = SURFACE_REFERENCED;
1974         obj_surface->fourcc = 0;
1975         obj_surface->expected_format = format;
1976         obj_surface->bo = NULL;
1977         obj_surface->locked_image_id = VA_INVALID_ID;
1978         obj_surface->derived_image_id = VA_INVALID_ID;
1979         obj_surface->private_data = NULL;
1980         obj_surface->free_private_data = NULL;
1981         obj_surface->subsampling = SUBSAMPLE_YUV420;
1982
1983         obj_surface->wrapper_surface = VA_INVALID_ID;
1984         obj_surface->exported_primefd = -1;
1985
1986         switch (memory_type) {
1987         case I965_SURFACE_MEM_NATIVE:
1988             if (memory_attibute) {
1989                 if (!(memory_attibute->flags & VA_SURFACE_EXTBUF_DESC_ENABLE_TILING))
1990                     obj_surface->user_disable_tiling = true;
1991
1992                 if (memory_attibute->pixel_format) {
1993                     if (expected_fourcc)
1994                         ASSERT_RET(memory_attibute->pixel_format == expected_fourcc, VA_STATUS_ERROR_INVALID_PARAMETER);
1995                     else
1996                         expected_fourcc = memory_attibute->pixel_format;
1997                 }
1998                 ASSERT_RET(expected_fourcc, VA_STATUS_ERROR_INVALID_PARAMETER);
1999                 if (memory_attibute->pitches[0]) {
2000                     int bpp_1stplane = bpp_1stplane_by_fourcc(expected_fourcc);
2001                     ASSERT_RET(bpp_1stplane, VA_STATUS_ERROR_INVALID_PARAMETER);
2002                     obj_surface->width = memory_attibute->pitches[0];
2003                     obj_surface->user_h_stride_set = true;
2004                     ASSERT_RET(IS_ALIGNED(obj_surface->width, 16), VA_STATUS_ERROR_INVALID_PARAMETER);
2005                     ASSERT_RET(obj_surface->width >= width * bpp_1stplane, VA_STATUS_ERROR_INVALID_PARAMETER);
2006
2007                     if (memory_attibute->offsets[1]) {
2008                         ASSERT_RET(!memory_attibute->offsets[0], VA_STATUS_ERROR_INVALID_PARAMETER);
2009                         obj_surface->height = memory_attibute->offsets[1] / memory_attibute->pitches[0];
2010                         obj_surface->user_v_stride_set = true;
2011                         ASSERT_RET(IS_ALIGNED(obj_surface->height, 16), VA_STATUS_ERROR_INVALID_PARAMETER);
2012                         ASSERT_RET(obj_surface->height >= height, VA_STATUS_ERROR_INVALID_PARAMETER);
2013                     }
2014                 }
2015             }
2016             vaStatus = i965_surface_native_memory(ctx,
2017                                                   obj_surface,
2018                                                   format,
2019                                                   expected_fourcc);
2020             break;
2021
2022         case I965_SURFACE_MEM_GEM_FLINK:
2023         case I965_SURFACE_MEM_DRM_PRIME:
2024             vaStatus = i965_suface_external_memory(ctx,
2025                                                    obj_surface,
2026                                                    memory_type,
2027                                                    memory_attibute,
2028                                                    i);
2029             break;
2030         }
2031         if (VA_STATUS_SUCCESS != vaStatus) {
2032             i965_destroy_surface(&i965->surface_heap, (struct object_base *)obj_surface);
2033             break;
2034         }
2035     }
2036
2037     /* Error recovery */
2038     if (VA_STATUS_SUCCESS != vaStatus) {
2039         /* surfaces[i-1] was the last successful allocation */
2040         for (; i--;) {
2041             struct object_surface *obj_surface = SURFACE(surfaces[i]);
2042
2043             surfaces[i] = VA_INVALID_SURFACE;
2044             assert(obj_surface);
2045             i965_destroy_surface(&i965->surface_heap, (struct object_base *)obj_surface);
2046         }
2047     }
2048
2049     return vaStatus;
2050 }
2051
2052 VAStatus
2053 i965_CreateSurfaces(VADriverContextP ctx,
2054                     int width,
2055                     int height,
2056                     int format,
2057                     int num_surfaces,
2058                     VASurfaceID *surfaces)      /* out */
2059 {
2060     return i965_CreateSurfaces2(ctx,
2061                                 format,
2062                                 width,
2063                                 height,
2064                                 surfaces,
2065                                 num_surfaces,
2066                                 NULL,
2067                                 0);
2068 }
2069
2070 VAStatus
2071 i965_DestroySurfaces(VADriverContextP ctx,
2072                      VASurfaceID *surface_list,
2073                      int num_surfaces)
2074 {
2075     struct i965_driver_data *i965 = i965_driver_data(ctx);
2076     int i;
2077     VAStatus va_status = VA_STATUS_SUCCESS;
2078
2079     for (i = num_surfaces; i--;) {
2080         struct object_surface *obj_surface = SURFACE(surface_list[i]);
2081
2082         ASSERT_RET(obj_surface, VA_STATUS_ERROR_INVALID_SURFACE);
2083
2084         if ((obj_surface->wrapper_surface != VA_INVALID_ID) &&
2085             i965->wrapper_pdrvctx) {
2086             CALL_VTABLE(i965->wrapper_pdrvctx, va_status,
2087                         vaDestroySurfaces(i965->wrapper_pdrvctx,
2088                                           &(obj_surface->wrapper_surface),
2089                                           1));
2090             obj_surface->wrapper_surface = VA_INVALID_ID;
2091         }
2092         if (obj_surface->exported_primefd >= 0) {
2093             close(obj_surface->exported_primefd);
2094             obj_surface->exported_primefd = -1;
2095         }
2096
2097         i965_destroy_surface(&i965->surface_heap, (struct object_base *)obj_surface);
2098     }
2099
2100     return va_status;
2101 }
2102
2103 VAStatus
2104 i965_QueryImageFormats(VADriverContextP ctx,
2105                        VAImageFormat *format_list,      /* out */
2106                        int *num_formats)                /* out */
2107 {
2108     int n;
2109
2110     for (n = 0; i965_image_formats_map[n].va_format.fourcc != 0; n++) {
2111         const i965_image_format_map_t * const m = &i965_image_formats_map[n];
2112         if (format_list)
2113             format_list[n] = m->va_format;
2114     }
2115
2116     if (num_formats)
2117         *num_formats = n;
2118
2119     return VA_STATUS_SUCCESS;
2120 }
2121
2122 /*
2123  * Guess the format when the usage of a VA surface is unknown
2124  * 1. Without a valid context: YV12
2125  * 2. The current context is valid:
2126  *    a) always NV12 on GEN6 and later
2127  *    b) I420 for MPEG-2 and NV12 for other codec on GEN4 & GEN5
2128  */
2129 static void
2130 i965_guess_surface_format(VADriverContextP ctx,
2131                           VASurfaceID surface,
2132                           unsigned int *fourcc,
2133                           unsigned int *is_tiled)
2134 {
2135     struct i965_driver_data *i965 = i965_driver_data(ctx);
2136     struct object_context *obj_context = NULL;
2137     struct object_config *obj_config = NULL;
2138
2139     *fourcc = VA_FOURCC_YV12;
2140     *is_tiled = 0;
2141
2142     if (i965->current_context_id == VA_INVALID_ID)
2143         return;
2144
2145     obj_context = CONTEXT(i965->current_context_id);
2146
2147     if (!obj_context)
2148         return;
2149
2150     obj_config = obj_context->obj_config;
2151     assert(obj_config);
2152
2153     if (!obj_config)
2154         return;
2155
2156     if (IS_GEN6(i965->intel.device_info) ||
2157         IS_GEN7(i965->intel.device_info) ||
2158         IS_GEN8(i965->intel.device_info) ||
2159         IS_GEN10(i965->intel.device_info) ||
2160         IS_GEN9(i965->intel.device_info)) {
2161         *fourcc = VA_FOURCC_NV12;
2162         *is_tiled = 1;
2163         return;
2164     }
2165
2166     switch (obj_config->profile) {
2167     case VAProfileMPEG2Simple:
2168     case VAProfileMPEG2Main:
2169         *fourcc = VA_FOURCC_I420;
2170         *is_tiled = 0;
2171         break;
2172
2173     default:
2174         *fourcc = VA_FOURCC_NV12;
2175         *is_tiled = 0;
2176         break;
2177     }
2178 }
2179
2180 VAStatus
2181 i965_QuerySubpictureFormats(VADriverContextP ctx,
2182                             VAImageFormat *format_list,         /* out */
2183                             unsigned int *flags,                /* out */
2184                             unsigned int *num_formats)          /* out */
2185 {
2186     int n;
2187
2188     for (n = 0; i965_subpic_formats_map[n].va_format.fourcc != 0; n++) {
2189         const i965_subpic_format_map_t * const m = &i965_subpic_formats_map[n];
2190         if (format_list)
2191             format_list[n] = m->va_format;
2192         if (flags)
2193             flags[n] = m->va_flags;
2194     }
2195
2196     if (num_formats)
2197         *num_formats = n;
2198
2199     return VA_STATUS_SUCCESS;
2200 }
2201
2202 static void
2203 i965_destroy_subpic(struct object_heap *heap, struct object_base *obj)
2204 {
2205     //    struct object_subpic *obj_subpic = (struct object_subpic *)obj;
2206
2207     object_heap_free(heap, obj);
2208 }
2209
2210 VAStatus
2211 i965_CreateSubpicture(VADriverContextP ctx,
2212                       VAImageID image,
2213                       VASubpictureID *subpicture)         /* out */
2214 {
2215     struct i965_driver_data *i965 = i965_driver_data(ctx);
2216     VASubpictureID subpicID = NEW_SUBPIC_ID()
2217                               struct object_subpic * obj_subpic = SUBPIC(subpicID);
2218
2219     if (!obj_subpic)
2220         return VA_STATUS_ERROR_ALLOCATION_FAILED;
2221
2222     struct object_image *obj_image = IMAGE(image);
2223     if (!obj_image)
2224         return VA_STATUS_ERROR_INVALID_IMAGE;
2225
2226     const i965_subpic_format_map_t * const m = get_subpic_format(&obj_image->image.format);
2227     if (!m)
2228         return VA_STATUS_ERROR_UNKNOWN; /* XXX: VA_STATUS_ERROR_UNSUPPORTED_FORMAT? */
2229
2230     *subpicture = subpicID;
2231     obj_subpic->image  = image;
2232     obj_subpic->obj_image = obj_image;
2233     obj_subpic->format = m->format;
2234     obj_subpic->width  = obj_image->image.width;
2235     obj_subpic->height = obj_image->image.height;
2236     obj_subpic->pitch  = obj_image->image.pitches[0];
2237     obj_subpic->bo     = obj_image->bo;
2238     obj_subpic->global_alpha = 1.0;
2239
2240     return VA_STATUS_SUCCESS;
2241 }
2242
2243 VAStatus
2244 i965_DestroySubpicture(VADriverContextP ctx,
2245                        VASubpictureID subpicture)
2246 {
2247     struct i965_driver_data *i965 = i965_driver_data(ctx);
2248     struct object_subpic *obj_subpic = SUBPIC(subpicture);
2249
2250     if (!obj_subpic)
2251         return VA_STATUS_ERROR_INVALID_SUBPICTURE;
2252
2253     ASSERT_RET(obj_subpic->obj_image, VA_STATUS_ERROR_INVALID_SUBPICTURE);
2254     i965_destroy_subpic(&i965->subpic_heap, (struct object_base *)obj_subpic);
2255     return VA_STATUS_SUCCESS;
2256 }
2257
2258 VAStatus
2259 i965_SetSubpictureImage(VADriverContextP ctx,
2260                         VASubpictureID subpicture,
2261                         VAImageID image)
2262 {
2263     /* TODO */
2264     return VA_STATUS_ERROR_UNIMPLEMENTED;
2265 }
2266
2267 VAStatus
2268 i965_SetSubpictureChromakey(VADriverContextP ctx,
2269                             VASubpictureID subpicture,
2270                             unsigned int chromakey_min,
2271                             unsigned int chromakey_max,
2272                             unsigned int chromakey_mask)
2273 {
2274     /* TODO */
2275     return VA_STATUS_ERROR_UNIMPLEMENTED;
2276 }
2277
2278 VAStatus
2279 i965_SetSubpictureGlobalAlpha(VADriverContextP ctx,
2280                               VASubpictureID subpicture,
2281                               float global_alpha)
2282 {
2283     struct i965_driver_data *i965 = i965_driver_data(ctx);
2284     struct object_subpic *obj_subpic = SUBPIC(subpicture);
2285
2286     if (global_alpha > 1.0 || global_alpha < 0.0) {
2287         return VA_STATUS_ERROR_INVALID_PARAMETER;
2288     }
2289
2290     if (!obj_subpic)
2291         return VA_STATUS_ERROR_INVALID_SUBPICTURE;
2292
2293     obj_subpic->global_alpha  = global_alpha;
2294
2295     return VA_STATUS_SUCCESS;
2296 }
2297
2298 VAStatus
2299 i965_AssociateSubpicture(VADriverContextP ctx,
2300                          VASubpictureID subpicture,
2301                          VASurfaceID *target_surfaces,
2302                          int num_surfaces,
2303                          short src_x, /* upper left offset in subpicture */
2304                          short src_y,
2305                          unsigned short src_width,
2306                          unsigned short src_height,
2307                          short dest_x, /* upper left offset in surface */
2308                          short dest_y,
2309                          unsigned short dest_width,
2310                          unsigned short dest_height,
2311                          /*
2312                           * whether to enable chroma-keying or global-alpha
2313                           * see VA_SUBPICTURE_XXX values
2314                           */
2315                          unsigned int flags)
2316 {
2317     struct i965_driver_data *i965 = i965_driver_data(ctx);
2318     struct object_subpic *obj_subpic = SUBPIC(subpicture);
2319     int i, j;
2320
2321     if (!obj_subpic)
2322         return VA_STATUS_ERROR_INVALID_SUBPICTURE;
2323
2324     ASSERT_RET(obj_subpic->obj_image, VA_STATUS_ERROR_INVALID_SUBPICTURE);
2325
2326     obj_subpic->src_rect.x      = src_x;
2327     obj_subpic->src_rect.y      = src_y;
2328     obj_subpic->src_rect.width  = src_width;
2329     obj_subpic->src_rect.height = src_height;
2330     obj_subpic->dst_rect.x      = dest_x;
2331     obj_subpic->dst_rect.y      = dest_y;
2332     obj_subpic->dst_rect.width  = dest_width;
2333     obj_subpic->dst_rect.height = dest_height;
2334     obj_subpic->flags           = flags;
2335
2336     for (i = 0; i < num_surfaces; i++) {
2337         struct object_surface *obj_surface = SURFACE(target_surfaces[i]);
2338         if (!obj_surface)
2339             return VA_STATUS_ERROR_INVALID_SURFACE;
2340
2341         for (j = 0; j < I965_MAX_SUBPIC_SUM; j ++) {
2342             if (obj_surface->subpic[j] == VA_INVALID_ID) {
2343                 assert(obj_surface->obj_subpic[j] == NULL);
2344                 obj_surface->subpic[j] = subpicture;
2345                 obj_surface->obj_subpic[j] = obj_subpic;
2346                 break;
2347             }
2348         }
2349
2350         if (j == I965_MAX_SUBPIC_SUM) {
2351             return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
2352         }
2353
2354     }
2355     return VA_STATUS_SUCCESS;
2356 }
2357
2358
2359 VAStatus
2360 i965_DeassociateSubpicture(VADriverContextP ctx,
2361                            VASubpictureID subpicture,
2362                            VASurfaceID *target_surfaces,
2363                            int num_surfaces)
2364 {
2365     struct i965_driver_data *i965 = i965_driver_data(ctx);
2366     struct object_subpic *obj_subpic = SUBPIC(subpicture);
2367     int i, j;
2368
2369     if (!obj_subpic)
2370         return VA_STATUS_ERROR_INVALID_SUBPICTURE;
2371
2372     for (i = 0; i < num_surfaces; i++) {
2373         struct object_surface *obj_surface = SURFACE(target_surfaces[i]);
2374         if (!obj_surface)
2375             return VA_STATUS_ERROR_INVALID_SURFACE;
2376
2377         for (j = 0; j < I965_MAX_SUBPIC_SUM; j ++) {
2378             if (obj_surface->subpic[j] == subpicture) {
2379                 assert(obj_surface->obj_subpic[j] == obj_subpic);
2380                 obj_surface->subpic[j] = VA_INVALID_ID;
2381                 obj_surface->obj_subpic[j] = NULL;
2382                 break;
2383             }
2384         }
2385
2386         if (j == I965_MAX_SUBPIC_SUM) {
2387             return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
2388         }
2389     }
2390     return VA_STATUS_SUCCESS;
2391 }
2392
2393 void
2394 i965_reference_buffer_store(struct buffer_store **ptr,
2395                             struct buffer_store *buffer_store)
2396 {
2397     assert(*ptr == NULL);
2398
2399     if (buffer_store) {
2400         buffer_store->ref_count++;
2401         *ptr = buffer_store;
2402     }
2403 }
2404
2405 void
2406 i965_release_buffer_store(struct buffer_store **ptr)
2407 {
2408     struct buffer_store *buffer_store = *ptr;
2409
2410     if (buffer_store == NULL)
2411         return;
2412
2413     assert(buffer_store->bo || buffer_store->buffer);
2414     assert(!(buffer_store->bo && buffer_store->buffer));
2415     buffer_store->ref_count--;
2416
2417     if (buffer_store->ref_count == 0) {
2418         dri_bo_unreference(buffer_store->bo);
2419         free(buffer_store->buffer);
2420         buffer_store->bo = NULL;
2421         buffer_store->buffer = NULL;
2422         free(buffer_store);
2423     }
2424
2425     *ptr = NULL;
2426 }
2427
2428 static void
2429 i965_destroy_context(struct object_heap *heap, struct object_base *obj)
2430 {
2431     struct object_context *obj_context = (struct object_context *)obj;
2432     int i, j;
2433
2434     if (obj_context->hw_context) {
2435         obj_context->hw_context->destroy(obj_context->hw_context);
2436         obj_context->hw_context = NULL;
2437     }
2438
2439     if (obj_context->codec_type == CODEC_PROC) {
2440         i965_release_buffer_store(&obj_context->codec_state.proc.pipeline_param);
2441
2442     } else if (obj_context->codec_type == CODEC_ENC) {
2443         i965_release_buffer_store(&obj_context->codec_state.encode.q_matrix);
2444         i965_release_buffer_store(&obj_context->codec_state.encode.huffman_table);
2445
2446         assert(obj_context->codec_state.encode.num_slice_params_ext <= obj_context->codec_state.encode.max_slice_params_ext);
2447         i965_release_buffer_store(&obj_context->codec_state.encode.pic_param_ext);
2448         i965_release_buffer_store(&obj_context->codec_state.encode.seq_param_ext);
2449
2450         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.packed_header_param); i++)
2451             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_param[i]);
2452
2453         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.packed_header_data); i++)
2454             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_data[i]);
2455
2456         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.misc_param); i++)
2457             for (j = 0; j < ARRAY_ELEMS(obj_context->codec_state.encode.misc_param[0]); j++)
2458                 i965_release_buffer_store(&obj_context->codec_state.encode.misc_param[i][j]);
2459
2460         for (i = 0; i < obj_context->codec_state.encode.num_slice_params_ext; i++)
2461             i965_release_buffer_store(&obj_context->codec_state.encode.slice_params_ext[i]);
2462
2463         free(obj_context->codec_state.encode.slice_params_ext);
2464         if (obj_context->codec_state.encode.slice_rawdata_index) {
2465             free(obj_context->codec_state.encode.slice_rawdata_index);
2466             obj_context->codec_state.encode.slice_rawdata_index = NULL;
2467         }
2468         if (obj_context->codec_state.encode.slice_rawdata_count) {
2469             free(obj_context->codec_state.encode.slice_rawdata_count);
2470             obj_context->codec_state.encode.slice_rawdata_count = NULL;
2471         }
2472
2473         if (obj_context->codec_state.encode.slice_header_index) {
2474             free(obj_context->codec_state.encode.slice_header_index);
2475             obj_context->codec_state.encode.slice_header_index = NULL;
2476         }
2477
2478         for (i = 0; i < obj_context->codec_state.encode.num_packed_header_params_ext; i++)
2479             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_params_ext[i]);
2480         free(obj_context->codec_state.encode.packed_header_params_ext);
2481
2482         for (i = 0; i < obj_context->codec_state.encode.num_packed_header_data_ext; i++)
2483             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_data_ext[i]);
2484         free(obj_context->codec_state.encode.packed_header_data_ext);
2485
2486         i965_release_buffer_store(&obj_context->codec_state.encode.encmb_map);
2487     } else if (obj_context->codec_type == CODEC_PREENC) {
2488         /* using the same encode codec_state for preenc too,
2489          * since both sharig all most same code except in few places */
2490         i965_release_buffer_store(&obj_context->codec_state.encode.stat_param_ext);
2491     } else {
2492         assert(obj_context->codec_state.decode.num_slice_params <= obj_context->codec_state.decode.max_slice_params);
2493         assert(obj_context->codec_state.decode.num_slice_datas <= obj_context->codec_state.decode.max_slice_datas);
2494
2495         i965_release_buffer_store(&obj_context->codec_state.decode.pic_param);
2496         i965_release_buffer_store(&obj_context->codec_state.decode.iq_matrix);
2497         i965_release_buffer_store(&obj_context->codec_state.decode.huffman_table);
2498         i965_release_buffer_store(&obj_context->codec_state.decode.bit_plane);
2499
2500         for (i = 0; i < obj_context->codec_state.decode.num_slice_params; i++)
2501             i965_release_buffer_store(&obj_context->codec_state.decode.slice_params[i]);
2502
2503         for (i = 0; i < obj_context->codec_state.decode.num_slice_datas; i++)
2504             i965_release_buffer_store(&obj_context->codec_state.decode.slice_datas[i]);
2505
2506         free(obj_context->codec_state.decode.slice_params);
2507         free(obj_context->codec_state.decode.slice_datas);
2508     }
2509
2510     free(obj_context->render_targets);
2511     object_heap_free(heap, obj);
2512 }
2513
2514 static inline void
2515 max_resolution(struct i965_driver_data *i965,
2516                struct object_config *obj_config,
2517                int *w,                                  /* out */
2518                int *h)                                  /* out */
2519 {
2520     if (i965->codec_info->max_resolution) {
2521         i965->codec_info->max_resolution(i965, obj_config, w, h);
2522     } else {
2523         *w = i965->codec_info->max_width;
2524         *h = i965->codec_info->max_height;
2525     }
2526 }
2527
2528 VAStatus
2529 i965_CreateContext(VADriverContextP ctx,
2530                    VAConfigID config_id,
2531                    int picture_width,
2532                    int picture_height,
2533                    int flag,
2534                    VASurfaceID *render_targets,
2535                    int num_render_targets,
2536                    VAContextID *context)                /* out */
2537 {
2538     struct i965_driver_data *i965 = i965_driver_data(ctx);
2539     struct object_config *obj_config = CONFIG(config_id);
2540     struct object_context *obj_context = NULL;
2541     VAConfigAttrib *attrib;
2542     VAStatus vaStatus = VA_STATUS_SUCCESS;
2543     int contextID;
2544     int i;
2545     int max_width;
2546     int max_height;
2547     int min_width_height = I965_MIN_CODEC_ENC_RESOLUTION_WIDTH_HEIGHT;
2548
2549     if (NULL == obj_config) {
2550         vaStatus = VA_STATUS_ERROR_INVALID_CONFIG;
2551         return vaStatus;
2552     }
2553
2554     if (obj_config->profile == VAProfileJPEGBaseline)
2555         min_width_height = 1;
2556
2557     max_resolution(i965, obj_config, &max_width, &max_height);
2558
2559     if (picture_width > max_width ||
2560         picture_height > max_height) {
2561         vaStatus = VA_STATUS_ERROR_RESOLUTION_NOT_SUPPORTED;
2562         return vaStatus;
2563     }
2564
2565     if ((VAEntrypointEncSlice == obj_config->entrypoint) ||
2566         (VAEntrypointEncPicture == obj_config->entrypoint) ||
2567         (VAEntrypointEncSliceLP == obj_config->entrypoint) ||
2568         (VAEntrypointFEI == obj_config->entrypoint)) {
2569         if (picture_width < min_width_height ||
2570             picture_height < min_width_height) {
2571             vaStatus = VA_STATUS_ERROR_RESOLUTION_NOT_SUPPORTED;
2572             return vaStatus;
2573         }
2574     }
2575
2576     /* Validate flag */
2577     /* Validate picture dimensions */
2578     contextID = NEW_CONTEXT_ID();
2579     obj_context = CONTEXT(contextID);
2580
2581     if (NULL == obj_context) {
2582         vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
2583         return vaStatus;
2584     }
2585
2586     *context = contextID;
2587     obj_context->flags = flag;
2588     obj_context->context_id = contextID;
2589     obj_context->obj_config = obj_config;
2590     obj_context->picture_width = picture_width;
2591     obj_context->picture_height = picture_height;
2592     obj_context->num_render_targets = num_render_targets;
2593     obj_context->render_targets =
2594         (VASurfaceID *)calloc(num_render_targets, sizeof(VASurfaceID));
2595     obj_context->hw_context = NULL;
2596     obj_context->wrapper_context = VA_INVALID_ID;
2597
2598     if (!obj_context->render_targets)
2599         return VA_STATUS_ERROR_ALLOCATION_FAILED;
2600
2601     for (i = 0; i < num_render_targets; i++) {
2602         if (NULL == SURFACE(render_targets[i])) {
2603             vaStatus = VA_STATUS_ERROR_INVALID_SURFACE;
2604             break;
2605         }
2606
2607         obj_context->render_targets[i] = render_targets[i];
2608     }
2609
2610     if (VA_STATUS_SUCCESS == vaStatus) {
2611         if (VAEntrypointVideoProc == obj_config->entrypoint) {
2612             obj_context->codec_type = CODEC_PROC;
2613             memset(&obj_context->codec_state.proc, 0, sizeof(obj_context->codec_state.proc));
2614             obj_context->codec_state.proc.current_render_target = VA_INVALID_ID;
2615             assert(i965->codec_info->proc_hw_context_init);
2616             obj_context->hw_context = i965->codec_info->proc_hw_context_init(ctx, obj_config);
2617         } else if ((VAEntrypointEncSlice == obj_config->entrypoint) ||
2618                    (VAEntrypointEncPicture == obj_config->entrypoint) ||
2619                    (VAEntrypointEncSliceLP == obj_config->entrypoint) ||
2620                    (VAEntrypointFEI == obj_config->entrypoint)) {
2621             VAConfigAttrib *packed_attrib;
2622             obj_context->codec_type = CODEC_ENC;
2623             memset(&obj_context->codec_state.encode, 0, sizeof(obj_context->codec_state.encode));
2624             obj_context->codec_state.encode.current_render_target = VA_INVALID_ID;
2625             obj_context->codec_state.encode.max_packed_header_params_ext = NUM_SLICES;
2626             obj_context->codec_state.encode.packed_header_params_ext =
2627                 calloc(obj_context->codec_state.encode.max_packed_header_params_ext,
2628                        sizeof(struct buffer_store *));
2629
2630             obj_context->codec_state.encode.max_packed_header_data_ext = NUM_SLICES;
2631             obj_context->codec_state.encode.packed_header_data_ext =
2632                 calloc(obj_context->codec_state.encode.max_packed_header_data_ext,
2633                        sizeof(struct buffer_store *));
2634
2635             obj_context->codec_state.encode.max_slice_num = NUM_SLICES;
2636             obj_context->codec_state.encode.slice_rawdata_index =
2637                 calloc(obj_context->codec_state.encode.max_slice_num, sizeof(int));
2638             obj_context->codec_state.encode.slice_rawdata_count =
2639                 calloc(obj_context->codec_state.encode.max_slice_num, sizeof(int));
2640
2641             obj_context->codec_state.encode.slice_header_index =
2642                 calloc(obj_context->codec_state.encode.max_slice_num, sizeof(int));
2643
2644             obj_context->codec_state.encode.vps_sps_seq_index = 0;
2645
2646             obj_context->codec_state.encode.slice_index = 0;
2647             packed_attrib = i965_lookup_config_attribute(obj_config, VAConfigAttribEncPackedHeaders);
2648             if (packed_attrib) {
2649                 obj_context->codec_state.encode.packed_header_flag = packed_attrib->value;
2650                 if (obj_config->profile == VAProfileVP9Profile0)
2651                     obj_context->codec_state.encode.packed_header_flag =
2652                         packed_attrib->value & VA_ENC_PACKED_HEADER_RAW_DATA;
2653             } else {
2654                 /* use the default value. SPS/PPS/RAWDATA is passed from user
2655                  * while Slice_header data is generated by driver.
2656                  */
2657                 obj_context->codec_state.encode.packed_header_flag =
2658                     VA_ENC_PACKED_HEADER_SEQUENCE |
2659                     VA_ENC_PACKED_HEADER_PICTURE |
2660                     VA_ENC_PACKED_HEADER_RAW_DATA;
2661
2662                 /* it is not used for VP9 */
2663                 if (obj_config->profile == VAProfileVP9Profile0)
2664                     obj_context->codec_state.encode.packed_header_flag = 0;
2665             }
2666             assert(i965->codec_info->enc_hw_context_init);
2667             obj_context->hw_context = i965->codec_info->enc_hw_context_init(ctx, obj_config);
2668         } else if (VAEntrypointStats == obj_config->entrypoint) {
2669             /* PreEnc sharing the ENC structures */
2670             obj_context->codec_type = CODEC_PREENC;
2671             memset(&obj_context->codec_state.encode, 0, sizeof(obj_context->codec_state.encode));
2672             obj_context->codec_state.encode.current_render_target = VA_INVALID_ID;
2673             assert(i965->codec_info->enc_hw_context_init);
2674             obj_context->hw_context = i965->codec_info->enc_hw_context_init(ctx, obj_config);
2675         } else {
2676             obj_context->codec_type = CODEC_DEC;
2677             memset(&obj_context->codec_state.decode, 0, sizeof(obj_context->codec_state.decode));
2678             obj_context->codec_state.decode.current_render_target = -1;
2679             obj_context->codec_state.decode.max_slice_params = NUM_SLICES;
2680             obj_context->codec_state.decode.max_slice_datas = NUM_SLICES;
2681             obj_context->codec_state.decode.slice_params = calloc(obj_context->codec_state.decode.max_slice_params,
2682                                                                   sizeof(*obj_context->codec_state.decode.slice_params));
2683             obj_context->codec_state.decode.slice_datas = calloc(obj_context->codec_state.decode.max_slice_datas,
2684                                                                  sizeof(*obj_context->codec_state.decode.slice_datas));
2685
2686             assert(i965->codec_info->dec_hw_context_init);
2687             obj_context->hw_context = i965->codec_info->dec_hw_context_init(ctx, obj_config);
2688         }
2689     }
2690
2691     attrib = i965_lookup_config_attribute(obj_config, VAConfigAttribRTFormat);
2692     if (!attrib)
2693         return VA_STATUS_ERROR_INVALID_CONFIG;
2694     obj_context->codec_state.base.chroma_formats = attrib->value;
2695
2696     if (obj_config->wrapper_config != VA_INVALID_ID) {
2697         /* The wrapper_pdrvctx should exist when wrapper_config is valid.
2698          * So it won't check i965->wrapper_pdrvctx again.
2699          * Fixme if it is incorrect.
2700          */
2701         VAGenericID wrapper_context;
2702
2703         /*
2704          * The render_surface is not passed when calling
2705          * vaCreateContext.
2706          * If it is needed, we must get the wrapped surface
2707          * for the corresponding Surface_list.
2708          * So the wrapped surface conversion is deferred.
2709          */
2710         CALL_VTABLE(i965->wrapper_pdrvctx, vaStatus,
2711                     vaCreateContext(i965->wrapper_pdrvctx,
2712                                     obj_config->wrapper_config,
2713                                     picture_width, picture_height,
2714                                     flag, NULL, 0,
2715                                     &wrapper_context));
2716
2717         if (vaStatus == VA_STATUS_SUCCESS)
2718             obj_context->wrapper_context = wrapper_context;
2719     }
2720
2721     /* Error recovery */
2722     if (VA_STATUS_SUCCESS != vaStatus) {
2723         i965_destroy_context(&i965->context_heap, (struct object_base *)obj_context);
2724     }
2725
2726     i965->current_context_id = contextID;
2727
2728     return vaStatus;
2729 }
2730
2731 VAStatus
2732 i965_DestroyContext(VADriverContextP ctx, VAContextID context)
2733 {
2734     struct i965_driver_data *i965 = i965_driver_data(ctx);
2735     struct object_context *obj_context = CONTEXT(context);
2736     VAStatus va_status = VA_STATUS_SUCCESS;
2737
2738     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
2739
2740     if (i965->current_context_id == context)
2741         i965->current_context_id = VA_INVALID_ID;
2742
2743     if ((obj_context->wrapper_context != VA_INVALID_ID) &&
2744         i965->wrapper_pdrvctx) {
2745         CALL_VTABLE(i965->wrapper_pdrvctx, va_status,
2746                     vaDestroyContext(i965->wrapper_pdrvctx,
2747                                      obj_context->wrapper_context));
2748
2749         obj_context->wrapper_context = VA_INVALID_ID;
2750     }
2751
2752     i965_destroy_context(&i965->context_heap, (struct object_base *)obj_context);
2753
2754     return va_status;
2755 }
2756
2757 static void
2758 i965_destroy_buffer(struct object_heap *heap, struct object_base *obj)
2759 {
2760     struct object_buffer *obj_buffer = (struct object_buffer *)obj;
2761
2762     assert(obj_buffer->buffer_store);
2763     i965_release_buffer_store(&obj_buffer->buffer_store);
2764     object_heap_free(heap, obj);
2765 }
2766
2767 static VAStatus
2768 i965_create_buffer_internal(VADriverContextP ctx,
2769                             VAContextID context,
2770                             VABufferType type,
2771                             unsigned int size,
2772                             unsigned int num_elements,
2773                             void *data,
2774                             dri_bo *store_bo,
2775                             VABufferID *buf_id)
2776 {
2777     struct i965_driver_data *i965 = i965_driver_data(ctx);
2778     struct object_buffer *obj_buffer = NULL;
2779     struct buffer_store *buffer_store = NULL;
2780     int bufferID;
2781     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
2782     struct object_context *obj_context = CONTEXT(context);
2783     int wrapper_flag = 0;
2784
2785     /* Validate type */
2786     switch (type) {
2787     case VAPictureParameterBufferType:
2788     case VAIQMatrixBufferType:
2789     case VAQMatrixBufferType:
2790     case VABitPlaneBufferType:
2791     case VASliceGroupMapBufferType:
2792     case VASliceParameterBufferType:
2793     case VASliceDataBufferType:
2794     case VAMacroblockParameterBufferType:
2795     case VAResidualDataBufferType:
2796     case VADeblockingParameterBufferType:
2797     case VAImageBufferType:
2798     case VAEncCodedBufferType:
2799     case VAEncSequenceParameterBufferType:
2800     case VAEncPictureParameterBufferType:
2801     case VAEncSliceParameterBufferType:
2802     case VAEncPackedHeaderParameterBufferType:
2803     case VAEncPackedHeaderDataBufferType:
2804     case VAEncMiscParameterBufferType:
2805     case VAProcPipelineParameterBufferType:
2806     case VAProcFilterParameterBufferType:
2807     case VAHuffmanTableBufferType:
2808     case VAProbabilityBufferType:
2809     case VAEncMacroblockMapBufferType:
2810     case VAEncQPBufferType:
2811     case VAEncFEIMVBufferType:
2812     case VAEncFEIMBCodeBufferType:
2813     case VAEncFEIDistortionBufferType:
2814     case VAEncFEIMBControlBufferType:
2815     case VAEncFEIMVPredictorBufferType:
2816     case VAStatsStatisticsParameterBufferType:
2817     case VAStatsStatisticsBufferType:
2818     case VAStatsStatisticsBottomFieldBufferType:
2819     case VAStatsMVBufferType:
2820     case VAStatsMVPredictorBufferType:
2821
2822         /* Ok */
2823         break;
2824
2825     default:
2826         return VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
2827     }
2828
2829     bufferID = NEW_BUFFER_ID();
2830     obj_buffer = BUFFER(bufferID);
2831
2832     if (NULL == obj_buffer) {
2833         return VA_STATUS_ERROR_ALLOCATION_FAILED;
2834     }
2835
2836     if (type == VAEncCodedBufferType) {
2837         size += I965_CODEDBUFFER_HEADER_SIZE;
2838         size += 0x1000; /* for upper bound check */
2839     }
2840
2841     obj_buffer->max_num_elements = num_elements;
2842     obj_buffer->num_elements = num_elements;
2843     obj_buffer->size_element = size;
2844     obj_buffer->type = type;
2845     obj_buffer->export_refcount = 0;
2846     obj_buffer->buffer_store = NULL;
2847     obj_buffer->wrapper_buffer = VA_INVALID_ID;
2848     obj_buffer->context_id = context;
2849
2850     buffer_store = calloc(1, sizeof(struct buffer_store));
2851     assert(buffer_store);
2852     buffer_store->ref_count = 1;
2853
2854     if (obj_context &&
2855         (obj_context->wrapper_context != VA_INVALID_ID) &&
2856         i965->wrapper_pdrvctx) {
2857         VAGenericID wrapper_buffer;
2858         VADriverContextP pdrvctx = i965->wrapper_pdrvctx;
2859
2860         CALL_VTABLE(pdrvctx, vaStatus,
2861                     vaCreateBuffer(pdrvctx, obj_context->wrapper_context, type, size, num_elements,
2862                                    data, &wrapper_buffer));
2863         if (vaStatus == VA_STATUS_SUCCESS) {
2864             obj_buffer->wrapper_buffer = wrapper_buffer;
2865         } else {
2866             free(buffer_store);
2867             return vaStatus;
2868         }
2869         wrapper_flag = 1;
2870     }
2871
2872     if (store_bo != NULL) {
2873         buffer_store->bo = store_bo;
2874         dri_bo_reference(buffer_store->bo);
2875
2876         /* If the buffer is wrapped, the buffer_store is bogus. Unnecessary to copy it */
2877         if (data && !wrapper_flag)
2878             dri_bo_subdata(buffer_store->bo, 0, size * num_elements, data);
2879     } else if (type == VASliceDataBufferType ||
2880                type == VAImageBufferType ||
2881                type == VAEncCodedBufferType ||
2882                type == VAEncMacroblockMapBufferType ||
2883                type == VAProbabilityBufferType ||
2884                type == VAEncQPBufferType ||
2885                type == VAEncFEIMVBufferType ||
2886                type == VAEncFEIMBCodeBufferType ||
2887                type == VAEncFEIDistortionBufferType ||
2888                type == VAEncFEIMBControlBufferType ||
2889                type == VAEncFEIMVPredictorBufferType ||
2890                type == VAEncFEIMVPredictorBufferType ||
2891                type == VAStatsStatisticsBufferType ||
2892                type == VAStatsStatisticsBottomFieldBufferType ||
2893                type == VAStatsMVBufferType ||
2894                type == VAStatsMVPredictorBufferType) {
2895
2896         /* If the buffer is wrapped, the bo/buffer of buffer_store is bogus.
2897          * So it is enough to allocate one 64 byte bo
2898          */
2899         if (wrapper_flag)
2900             buffer_store->bo = dri_bo_alloc(i965->intel.bufmgr, "Bogus buffer",
2901                                             64, 64);
2902         else
2903             buffer_store->bo = dri_bo_alloc(i965->intel.bufmgr,
2904                                             "Buffer",
2905                                             size * num_elements, 64);
2906         assert(buffer_store->bo);
2907
2908         /* If the buffer is wrapped, the bo/buffer of buffer_store is bogus.
2909          * In fact it can be skipped. But it is still allocated and it is
2910          * only to follow the normal flowchart of buffer_allocation/release.
2911          */
2912         if (!wrapper_flag) {
2913             if (type == VAEncCodedBufferType) {
2914                 struct i965_coded_buffer_segment *coded_buffer_segment;
2915
2916                 dri_bo_map(buffer_store->bo, 1);
2917                 coded_buffer_segment = (struct i965_coded_buffer_segment *)buffer_store->bo->virtual;
2918                 coded_buffer_segment->base.size = size - I965_CODEDBUFFER_HEADER_SIZE;
2919                 coded_buffer_segment->base.bit_offset = 0;
2920                 coded_buffer_segment->base.status = 0;
2921                 coded_buffer_segment->base.buf = NULL;
2922                 coded_buffer_segment->base.next = NULL;
2923                 coded_buffer_segment->mapped = 0;
2924                 coded_buffer_segment->codec = 0;
2925                 coded_buffer_segment->status_support = 0;
2926                 dri_bo_unmap(buffer_store->bo);
2927             } else if (data) {
2928                 dri_bo_subdata(buffer_store->bo, 0, size * num_elements, data);
2929             }
2930         }
2931
2932     } else {
2933         int msize = size;
2934
2935         if (type == VAEncPackedHeaderDataBufferType) {
2936             msize = ALIGN(size, 4);
2937         }
2938
2939         /* If the buffer is wrapped, it is enough to allocate 4 bytes */
2940         if (wrapper_flag)
2941             buffer_store->buffer = malloc(4);
2942         else
2943             buffer_store->buffer = malloc(msize * num_elements);
2944         assert(buffer_store->buffer);
2945
2946         if (!wrapper_flag) {
2947             if (data)
2948                 memcpy(buffer_store->buffer, data, size * num_elements);
2949             else
2950                 memset(buffer_store->buffer, 0, size * num_elements);
2951         }
2952     }
2953
2954     buffer_store->num_elements = obj_buffer->num_elements;
2955     i965_reference_buffer_store(&obj_buffer->buffer_store, buffer_store);
2956     i965_release_buffer_store(&buffer_store);
2957     *buf_id = bufferID;
2958
2959     return VA_STATUS_SUCCESS;
2960 }
2961
2962 VAStatus
2963 i965_CreateBuffer(VADriverContextP ctx,
2964                   VAContextID context,          /* in */
2965                   VABufferType type,            /* in */
2966                   unsigned int size,            /* in */
2967                   unsigned int num_elements,    /* in */
2968                   void *data,                   /* in */
2969                   VABufferID *buf_id)           /* out */
2970 {
2971     return i965_create_buffer_internal(ctx, context, type, size, num_elements, data, NULL, buf_id);
2972 }
2973
2974
2975 VAStatus
2976 i965_BufferSetNumElements(VADriverContextP ctx,
2977                           VABufferID buf_id,           /* in */
2978                           unsigned int num_elements)   /* in */
2979 {
2980     struct i965_driver_data *i965 = i965_driver_data(ctx);
2981     struct object_buffer *obj_buffer = BUFFER(buf_id);
2982     VAStatus vaStatus = VA_STATUS_SUCCESS;
2983
2984     ASSERT_RET(obj_buffer, VA_STATUS_ERROR_INVALID_BUFFER);
2985
2986     /* When the wrapper_buffer exists, it will wrapper to the
2987      * buffer allocated from backend driver.
2988      */
2989     if ((obj_buffer->wrapper_buffer != VA_INVALID_ID) &&
2990         i965->wrapper_pdrvctx) {
2991         VADriverContextP pdrvctx = i965->wrapper_pdrvctx;
2992
2993         CALL_VTABLE(pdrvctx, vaStatus,
2994                     vaBufferSetNumElements(pdrvctx, obj_buffer->wrapper_buffer,
2995                                            num_elements));
2996         return vaStatus;
2997     }
2998
2999     if (num_elements > obj_buffer->max_num_elements) {
3000         vaStatus = VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
3001     } else {
3002         obj_buffer->num_elements = num_elements;
3003         if (obj_buffer->buffer_store != NULL) {
3004             obj_buffer->buffer_store->num_elements = num_elements;
3005         }
3006     }
3007
3008     return vaStatus;
3009 }
3010
3011 VAStatus
3012 i965_MapBuffer(VADriverContextP ctx,
3013                VABufferID buf_id,       /* in */
3014                void **pbuf)             /* out */
3015 {
3016     struct i965_driver_data *i965 = i965_driver_data(ctx);
3017     struct object_buffer *obj_buffer = BUFFER(buf_id);
3018     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
3019     struct object_context *obj_context;
3020
3021     ASSERT_RET(obj_buffer && obj_buffer->buffer_store, VA_STATUS_ERROR_INVALID_BUFFER);
3022
3023     obj_context = CONTEXT(obj_buffer->context_id);
3024
3025     /* When the wrapper_buffer exists, it will wrapper to the
3026      * buffer allocated from backend driver.
3027      */
3028     if ((obj_buffer->wrapper_buffer != VA_INVALID_ID) &&
3029         i965->wrapper_pdrvctx) {
3030         VADriverContextP pdrvctx = i965->wrapper_pdrvctx;
3031
3032         CALL_VTABLE(pdrvctx, vaStatus,
3033                     vaMapBuffer(pdrvctx, obj_buffer->wrapper_buffer, pbuf));
3034         return vaStatus;
3035     }
3036
3037     ASSERT_RET(obj_buffer->buffer_store->bo || obj_buffer->buffer_store->buffer, VA_STATUS_ERROR_INVALID_BUFFER);
3038     ASSERT_RET(!(obj_buffer->buffer_store->bo && obj_buffer->buffer_store->buffer), VA_STATUS_ERROR_INVALID_BUFFER);
3039
3040     if (obj_buffer->export_refcount > 0)
3041         return VA_STATUS_ERROR_INVALID_BUFFER;
3042
3043     if (NULL != obj_buffer->buffer_store->bo) {
3044         unsigned int tiling, swizzle;
3045
3046         dri_bo_get_tiling(obj_buffer->buffer_store->bo, &tiling, &swizzle);
3047
3048         if (tiling != I915_TILING_NONE)
3049             drm_intel_gem_bo_map_gtt(obj_buffer->buffer_store->bo);
3050         else
3051             dri_bo_map(obj_buffer->buffer_store->bo, 1);
3052
3053         ASSERT_RET(obj_buffer->buffer_store->bo->virtual, VA_STATUS_ERROR_OPERATION_FAILED);
3054         *pbuf = obj_buffer->buffer_store->bo->virtual;
3055         vaStatus = VA_STATUS_SUCCESS;
3056
3057         if (obj_buffer->type == VAEncCodedBufferType) {
3058             int i;
3059             unsigned char *buffer = NULL;
3060             unsigned int  header_offset = I965_CODEDBUFFER_HEADER_SIZE;
3061             struct i965_coded_buffer_segment *coded_buffer_segment = (struct i965_coded_buffer_segment *)(obj_buffer->buffer_store->bo->virtual);
3062
3063             if (!coded_buffer_segment->mapped) {
3064                 unsigned char delimiter0, delimiter1, delimiter2, delimiter3, delimiter4;
3065
3066                 coded_buffer_segment->base.buf = buffer = (unsigned char *)(obj_buffer->buffer_store->bo->virtual) + I965_CODEDBUFFER_HEADER_SIZE;
3067
3068                 if (obj_context &&
3069                     obj_context->hw_context &&
3070                     obj_context->hw_context->get_status &&
3071                     coded_buffer_segment->status_support) {
3072                     vaStatus = obj_context->hw_context->get_status(ctx, obj_context->hw_context, coded_buffer_segment);
3073                 } else {
3074                     if (coded_buffer_segment->codec == CODEC_H264 ||
3075                         coded_buffer_segment->codec == CODEC_H264_MVC) {
3076                         delimiter0 = H264_DELIMITER0;
3077                         delimiter1 = H264_DELIMITER1;
3078                         delimiter2 = H264_DELIMITER2;
3079                         delimiter3 = H264_DELIMITER3;
3080                         delimiter4 = H264_DELIMITER4;
3081                     } else if (coded_buffer_segment->codec == CODEC_MPEG2) {
3082                         delimiter0 = MPEG2_DELIMITER0;
3083                         delimiter1 = MPEG2_DELIMITER1;
3084                         delimiter2 = MPEG2_DELIMITER2;
3085                         delimiter3 = MPEG2_DELIMITER3;
3086                         delimiter4 = MPEG2_DELIMITER4;
3087                     } else if (coded_buffer_segment->codec == CODEC_JPEG) {
3088                         //In JPEG End of Image (EOI = 0xDDF9) marker can be used for delimiter.
3089                         delimiter0 = 0xFF;
3090                         delimiter1 = 0xD9;
3091                     } else if (coded_buffer_segment->codec == CODEC_HEVC) {
3092                         delimiter0 = HEVC_DELIMITER0;
3093                         delimiter1 = HEVC_DELIMITER1;
3094                         delimiter2 = HEVC_DELIMITER2;
3095                         delimiter3 = HEVC_DELIMITER3;
3096                         delimiter4 = HEVC_DELIMITER4;
3097                     } else if (coded_buffer_segment->codec != CODEC_VP8) {
3098                         ASSERT_RET(0, VA_STATUS_ERROR_UNSUPPORTED_PROFILE);
3099                     }
3100
3101                     if (coded_buffer_segment->codec == CODEC_JPEG) {
3102                         for (i = 0; i <  obj_buffer->size_element - header_offset - 1 - 0x1000; i++) {
3103                             if ((buffer[i] == 0xFF) && (buffer[i + 1] == 0xD9)) {
3104                                 break;
3105                             }
3106                         }
3107                         coded_buffer_segment->base.size = i + 2;
3108                     } else if (coded_buffer_segment->codec != CODEC_VP8) {
3109                         /* vp8 coded buffer size can be told by vp8 internal statistics buffer,
3110                            so it don't need to traversal the coded buffer */
3111                         for (i = 0; i < obj_buffer->size_element - header_offset - 3 - 0x1000; i++) {
3112                             if ((buffer[i] == delimiter0) &&
3113                                 (buffer[i + 1] == delimiter1) &&
3114                                 (buffer[i + 2] == delimiter2) &&
3115                                 (buffer[i + 3] == delimiter3) &&
3116                                 (buffer[i + 4] == delimiter4))
3117                                 break;
3118                         }
3119
3120                         if (i == obj_buffer->size_element - header_offset - 3 - 0x1000) {
3121                             coded_buffer_segment->base.status |= VA_CODED_BUF_STATUS_SLICE_OVERFLOW_MASK;
3122                         }
3123                         coded_buffer_segment->base.size = i;
3124                     }
3125
3126                     if (coded_buffer_segment->base.size >= obj_buffer->size_element - header_offset - 0x1000) {
3127                         coded_buffer_segment->base.status |= VA_CODED_BUF_STATUS_SLICE_OVERFLOW_MASK;
3128                     }
3129
3130                     vaStatus = VA_STATUS_SUCCESS;
3131                 }
3132
3133                 coded_buffer_segment->mapped = 1;
3134             } else {
3135                 assert(coded_buffer_segment->base.buf);
3136                 vaStatus = VA_STATUS_SUCCESS;
3137             }
3138         }
3139     } else if (NULL != obj_buffer->buffer_store->buffer) {
3140         *pbuf = obj_buffer->buffer_store->buffer;
3141         vaStatus = VA_STATUS_SUCCESS;
3142     }
3143
3144     return vaStatus;
3145 }
3146
3147 VAStatus
3148 i965_UnmapBuffer(VADriverContextP ctx, VABufferID buf_id)
3149 {
3150     struct i965_driver_data *i965 = i965_driver_data(ctx);
3151     struct object_buffer *obj_buffer = BUFFER(buf_id);
3152     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
3153
3154     if ((buf_id & OBJECT_HEAP_OFFSET_MASK) != BUFFER_ID_OFFSET)
3155         return VA_STATUS_ERROR_INVALID_BUFFER;
3156
3157     ASSERT_RET(obj_buffer && obj_buffer->buffer_store, VA_STATUS_ERROR_INVALID_BUFFER);
3158     /* When the wrapper_buffer exists, it will wrapper to the
3159      * buffer allocated from backend driver.
3160      */
3161     if ((obj_buffer->wrapper_buffer != VA_INVALID_ID) &&
3162         i965->wrapper_pdrvctx) {
3163         VADriverContextP pdrvctx = i965->wrapper_pdrvctx;
3164
3165         CALL_VTABLE(pdrvctx, vaStatus,
3166                     vaUnmapBuffer(pdrvctx, obj_buffer->wrapper_buffer));
3167         return vaStatus;
3168     }
3169
3170     ASSERT_RET(obj_buffer->buffer_store->bo || obj_buffer->buffer_store->buffer, VA_STATUS_ERROR_OPERATION_FAILED);
3171     ASSERT_RET(!(obj_buffer->buffer_store->bo && obj_buffer->buffer_store->buffer), VA_STATUS_ERROR_OPERATION_FAILED);
3172
3173     if (NULL != obj_buffer->buffer_store->bo) {
3174         unsigned int tiling, swizzle;
3175
3176         dri_bo_get_tiling(obj_buffer->buffer_store->bo, &tiling, &swizzle);
3177
3178         if (tiling != I915_TILING_NONE)
3179             drm_intel_gem_bo_unmap_gtt(obj_buffer->buffer_store->bo);
3180         else
3181             dri_bo_unmap(obj_buffer->buffer_store->bo);
3182
3183         vaStatus = VA_STATUS_SUCCESS;
3184     } else if (NULL != obj_buffer->buffer_store->buffer) {
3185         /* Do nothing */
3186         vaStatus = VA_STATUS_SUCCESS;
3187     }
3188
3189     return vaStatus;
3190 }
3191
3192 VAStatus
3193 i965_DestroyBuffer(VADriverContextP ctx, VABufferID buffer_id)
3194 {
3195     struct i965_driver_data *i965 = i965_driver_data(ctx);
3196     struct object_buffer *obj_buffer = BUFFER(buffer_id);
3197     VAStatus va_status = VA_STATUS_SUCCESS;
3198
3199     ASSERT_RET(obj_buffer, VA_STATUS_ERROR_INVALID_BUFFER);
3200
3201     if ((obj_buffer->wrapper_buffer != VA_INVALID_ID) &&
3202         i965->wrapper_pdrvctx) {
3203         CALL_VTABLE(i965->wrapper_pdrvctx, va_status,
3204                     vaDestroyBuffer(i965->wrapper_pdrvctx,
3205                                     obj_buffer->wrapper_buffer));
3206         obj_buffer->wrapper_buffer = VA_INVALID_ID;
3207     }
3208
3209     i965_destroy_buffer(&i965->buffer_heap, (struct object_base *)obj_buffer);
3210
3211     return va_status;
3212 }
3213
3214 VAStatus
3215 i965_BeginPicture(VADriverContextP ctx,
3216                   VAContextID context,
3217                   VASurfaceID render_target)
3218 {
3219     struct i965_driver_data *i965 = i965_driver_data(ctx);
3220     struct object_context *obj_context = CONTEXT(context);
3221     struct object_surface *obj_surface = SURFACE(render_target);
3222     struct object_config *obj_config;
3223     VAStatus vaStatus = VA_STATUS_SUCCESS;
3224     int i, j;
3225
3226     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
3227     ASSERT_RET(obj_surface, VA_STATUS_ERROR_INVALID_SURFACE);
3228     obj_config = obj_context->obj_config;
3229     ASSERT_RET(obj_config, VA_STATUS_ERROR_INVALID_CONFIG);
3230
3231     if (is_surface_busy(i965, obj_surface))
3232         return VA_STATUS_ERROR_SURFACE_BUSY;
3233
3234     if (obj_context->codec_type == CODEC_PROC) {
3235         obj_context->codec_state.proc.current_render_target = render_target;
3236     } else if (obj_context->codec_type == CODEC_ENC) {
3237         /* ext */
3238         i965_release_buffer_store(&obj_context->codec_state.encode.pic_param_ext);
3239
3240         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.packed_header_param); i++)
3241             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_param[i]);
3242
3243         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.packed_header_data); i++)
3244             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_data[i]);
3245
3246         for (i = 0; i < obj_context->codec_state.encode.num_slice_params_ext; i++)
3247             i965_release_buffer_store(&obj_context->codec_state.encode.slice_params_ext[i]);
3248
3249         obj_context->codec_state.encode.num_slice_params_ext = 0;
3250         obj_context->codec_state.encode.current_render_target = render_target;     /*This is input new frame*/
3251         obj_context->codec_state.encode.last_packed_header_type = 0;
3252         memset(obj_context->codec_state.encode.slice_rawdata_index, 0,
3253                sizeof(int) * obj_context->codec_state.encode.max_slice_num);
3254         memset(obj_context->codec_state.encode.slice_rawdata_count, 0,
3255                sizeof(int) * obj_context->codec_state.encode.max_slice_num);
3256         memset(obj_context->codec_state.encode.slice_header_index, 0,
3257                sizeof(int) * obj_context->codec_state.encode.max_slice_num);
3258
3259         for (i = 0; i < obj_context->codec_state.encode.num_packed_header_params_ext; i++)
3260             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_params_ext[i]);
3261         for (i = 0; i < obj_context->codec_state.encode.num_packed_header_data_ext; i++)
3262             i965_release_buffer_store(&obj_context->codec_state.encode.packed_header_data_ext[i]);
3263         obj_context->codec_state.encode.num_packed_header_params_ext = 0;
3264         obj_context->codec_state.encode.num_packed_header_data_ext = 0;
3265         obj_context->codec_state.encode.slice_index = 0;
3266         obj_context->codec_state.encode.vps_sps_seq_index = 0;
3267
3268         for (i = 0; i < ARRAY_ELEMS(obj_context->codec_state.encode.misc_param); i++)
3269             for (j = 0; j < ARRAY_ELEMS(obj_context->codec_state.encode.misc_param[0]); j++)
3270                 i965_release_buffer_store(&obj_context->codec_state.encode.misc_param[i][j]);
3271
3272         i965_release_buffer_store(&obj_context->codec_state.encode.encmb_map);
3273     } else if (obj_context->codec_type == CODEC_PREENC) {
3274         /* PreEnc sharing the ENC structures */
3275         i965_release_buffer_store(&obj_context->codec_state.encode.stat_param_ext);
3276         obj_context->codec_state.encode.current_render_target = render_target;
3277     } else {
3278         obj_context->codec_state.decode.current_render_target = render_target;
3279         i965_release_buffer_store(&obj_context->codec_state.decode.pic_param);
3280         i965_release_buffer_store(&obj_context->codec_state.decode.iq_matrix);
3281         i965_release_buffer_store(&obj_context->codec_state.decode.bit_plane);
3282         i965_release_buffer_store(&obj_context->codec_state.decode.huffman_table);
3283
3284         for (i = 0; i < obj_context->codec_state.decode.num_slice_params; i++) {
3285             i965_release_buffer_store(&obj_context->codec_state.decode.slice_params[i]);
3286             i965_release_buffer_store(&obj_context->codec_state.decode.slice_datas[i]);
3287         }
3288
3289         obj_context->codec_state.decode.num_slice_params = 0;
3290         obj_context->codec_state.decode.num_slice_datas = 0;
3291
3292         if ((obj_context->wrapper_context != VA_INVALID_ID) &&
3293             i965->wrapper_pdrvctx) {
3294             if (obj_surface->wrapper_surface == VA_INVALID_ID)
3295                 vaStatus = i965_surface_wrapper(ctx, render_target);
3296
3297             if (vaStatus != VA_STATUS_SUCCESS)
3298                 return vaStatus;
3299
3300             CALL_VTABLE(i965->wrapper_pdrvctx, vaStatus,
3301                         vaBeginPicture(i965->wrapper_pdrvctx,
3302                                        obj_context->wrapper_context,
3303                                        obj_surface->wrapper_surface));
3304         }
3305     }
3306
3307     return vaStatus;
3308 }
3309
3310 #define I965_RENDER_BUFFER(category, name) i965_render_##category##_##name##_buffer(ctx, obj_context, obj_buffer)
3311
3312 #define DEF_RENDER_SINGLE_BUFFER_FUNC(category, name, member)           \
3313     static VAStatus                                                     \
3314     i965_render_##category##_##name##_buffer(VADriverContextP ctx,      \
3315                                              struct object_context *obj_context, \
3316                                              struct object_buffer *obj_buffer) \
3317     {                                                                   \
3318         struct category##_state *category = &obj_context->codec_state.category; \
3319         i965_release_buffer_store(&category->member);                   \
3320         i965_reference_buffer_store(&category->member, obj_buffer->buffer_store); \
3321         return VA_STATUS_SUCCESS;                                       \
3322     }
3323
3324 #define DEF_RENDER_MULTI_BUFFER_FUNC(category, name, member)            \
3325     static VAStatus                                                     \
3326     i965_render_##category##_##name##_buffer(VADriverContextP ctx,      \
3327                                              struct object_context *obj_context, \
3328                                              struct object_buffer *obj_buffer) \
3329     {                                                                   \
3330         struct category##_state *category = &obj_context->codec_state.category; \
3331         if (category->num_##member == category->max_##member) {         \
3332             category->member = realloc(category->member, (category->max_##member + NUM_SLICES) * sizeof(*category->member)); \
3333             memset(category->member + category->max_##member, 0, NUM_SLICES * sizeof(*category->member)); \
3334             category->max_##member += NUM_SLICES;                       \
3335         }                                                               \
3336         i965_release_buffer_store(&category->member[category->num_##member]); \
3337         i965_reference_buffer_store(&category->member[category->num_##member], obj_buffer->buffer_store); \
3338         category->num_##member++;                                       \
3339         return VA_STATUS_SUCCESS;                                       \
3340     }
3341
3342 #define I965_RENDER_DECODE_BUFFER(name) I965_RENDER_BUFFER(decode, name)
3343
3344 #define DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(name, member) DEF_RENDER_SINGLE_BUFFER_FUNC(decode, name, member)
3345 DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(picture_parameter, pic_param)
3346 DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(iq_matrix, iq_matrix)
3347 DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(bit_plane, bit_plane)
3348 DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(huffman_table, huffman_table)
3349 DEF_RENDER_DECODE_SINGLE_BUFFER_FUNC(probability_data, probability_data)
3350
3351 #define DEF_RENDER_DECODE_MULTI_BUFFER_FUNC(name, member) DEF_RENDER_MULTI_BUFFER_FUNC(decode, name, member)
3352 DEF_RENDER_DECODE_MULTI_BUFFER_FUNC(slice_parameter, slice_params)
3353 DEF_RENDER_DECODE_MULTI_BUFFER_FUNC(slice_data, slice_datas)
3354
3355
3356 static VAStatus
3357 i965_decoder_vp9_wrapper_picture(VADriverContextP ctx,
3358                                  VABufferID *buffers,
3359                                  int num_buffers)
3360 {
3361     struct i965_driver_data *i965 = i965_driver_data(ctx);
3362     VAStatus vaStatus = VA_STATUS_SUCCESS;
3363     int i;
3364     VADecPictureParameterBufferVP9 *pVp9PicParams;
3365     VADriverContextP pdrvctx;
3366     struct object_buffer *obj_buffer;
3367
3368     pdrvctx = i965->wrapper_pdrvctx;
3369     /* do the conversion of VADecPictureParameterBufferVP9 */
3370     for (i = 0; i < num_buffers; i++) {
3371         obj_buffer = BUFFER(buffers[i]);
3372
3373         if (!obj_buffer)
3374             continue;
3375
3376         if (obj_buffer->wrapper_buffer == VA_INVALID_ID)
3377             continue;
3378
3379         if (obj_buffer->type == VAPictureParameterBufferType) {
3380             int j;
3381             VASurfaceID surface_id;
3382             struct object_surface *obj_surface;
3383
3384             pdrvctx = i965->wrapper_pdrvctx;
3385
3386             CALL_VTABLE(pdrvctx, vaStatus,
3387                         vaMapBuffer(pdrvctx, obj_buffer->wrapper_buffer,
3388                                     (void **)(&pVp9PicParams)));
3389
3390             if (vaStatus != VA_STATUS_SUCCESS)
3391                 return vaStatus;
3392
3393             for (j = 0; j < 8; j++) {
3394                 surface_id = pVp9PicParams->reference_frames[j];
3395                 obj_surface = SURFACE(surface_id);
3396
3397                 if (!obj_surface)
3398                     continue;
3399
3400                 if (obj_surface->wrapper_surface == VA_INVALID_ID) {
3401                     vaStatus = i965_surface_wrapper(ctx, surface_id);
3402                     if (vaStatus != VA_STATUS_SUCCESS) {
3403                         pdrvctx->vtable->vaUnmapBuffer(pdrvctx,
3404                                                        obj_buffer->wrapper_buffer);
3405                         goto fail_out;
3406                     }
3407                 }
3408
3409                 pVp9PicParams->reference_frames[j] = obj_surface->wrapper_surface;
3410             }
3411             CALL_VTABLE(pdrvctx, vaStatus,
3412                         vaUnmapBuffer(pdrvctx, obj_buffer->wrapper_buffer));
3413             break;
3414         }
3415     }
3416
3417     return VA_STATUS_SUCCESS;
3418
3419 fail_out:
3420     return vaStatus;
3421 }
3422
3423 static VAStatus
3424 i965_decoder_wrapper_picture(VADriverContextP ctx,
3425                              VAContextID context,
3426                              VABufferID *buffers,
3427                              int num_buffers)
3428 {
3429     struct i965_driver_data *i965 = i965_driver_data(ctx);
3430     struct object_context *obj_context = CONTEXT(context);
3431     VAStatus vaStatus = VA_STATUS_SUCCESS;
3432     int i;
3433     VADriverContextP pdrvctx;
3434     struct object_buffer *obj_buffer;
3435
3436     if (obj_context == NULL)
3437         return VA_STATUS_ERROR_INVALID_CONTEXT;
3438
3439     /* When it is not wrapped context, continue the normal flowchart */
3440     if (obj_context->wrapper_context == VA_INVALID_ID)
3441         return vaStatus;
3442
3443     if (obj_context->obj_config &&
3444         (obj_context->obj_config->profile == VAProfileVP9Profile0)) {
3445         vaStatus = i965_decoder_vp9_wrapper_picture(ctx, buffers, num_buffers);
3446     } else
3447         return VA_STATUS_ERROR_INVALID_PARAMETER;
3448
3449     pdrvctx = i965->wrapper_pdrvctx;
3450
3451     for (i = 0; i < num_buffers && vaStatus == VA_STATUS_SUCCESS; i++) {
3452         obj_buffer = BUFFER(buffers[i]);
3453
3454         if (!obj_buffer)
3455             continue;
3456
3457         if (obj_buffer->wrapper_buffer == VA_INVALID_ID) {
3458             vaStatus = VA_STATUS_ERROR_INVALID_BUFFER;
3459             break;
3460         }
3461
3462         CALL_VTABLE(pdrvctx, vaStatus,
3463                     vaRenderPicture(pdrvctx, obj_context->wrapper_context,
3464                                     &(obj_buffer->wrapper_buffer), 1));
3465     }
3466     return vaStatus;
3467 }
3468
3469 static VAStatus
3470 i965_decoder_render_picture(VADriverContextP ctx,
3471                             VAContextID context,
3472                             VABufferID *buffers,
3473                             int num_buffers)
3474 {
3475     struct i965_driver_data *i965 = i965_driver_data(ctx);
3476     struct object_context *obj_context = CONTEXT(context);
3477     VAStatus vaStatus = VA_STATUS_SUCCESS;
3478     int i;
3479
3480     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
3481
3482     for (i = 0; i < num_buffers && vaStatus == VA_STATUS_SUCCESS; i++) {
3483         struct object_buffer *obj_buffer = BUFFER(buffers[i]);
3484
3485         if (!obj_buffer)
3486             return VA_STATUS_ERROR_INVALID_BUFFER;
3487
3488         switch (obj_buffer->type) {
3489         case VAPictureParameterBufferType:
3490             vaStatus = I965_RENDER_DECODE_BUFFER(picture_parameter);
3491             break;
3492
3493         case VAIQMatrixBufferType:
3494             vaStatus = I965_RENDER_DECODE_BUFFER(iq_matrix);
3495             break;
3496
3497         case VABitPlaneBufferType:
3498             vaStatus = I965_RENDER_DECODE_BUFFER(bit_plane);
3499             break;
3500
3501         case VASliceParameterBufferType:
3502             vaStatus = I965_RENDER_DECODE_BUFFER(slice_parameter);
3503             break;
3504
3505         case VASliceDataBufferType:
3506             vaStatus = I965_RENDER_DECODE_BUFFER(slice_data);
3507             break;
3508
3509         case VAHuffmanTableBufferType:
3510             vaStatus = I965_RENDER_DECODE_BUFFER(huffman_table);
3511             break;
3512
3513         case VAProbabilityBufferType:
3514             vaStatus = I965_RENDER_DECODE_BUFFER(probability_data);
3515             break;
3516
3517         default:
3518             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
3519             break;
3520         }
3521     }
3522
3523     if ((vaStatus == VA_STATUS_SUCCESS) &&
3524         (obj_context->wrapper_context != VA_INVALID_ID))
3525         vaStatus = i965_decoder_wrapper_picture(ctx, context, buffers, num_buffers);
3526
3527     return vaStatus;
3528 }
3529
3530 #define I965_RENDER_ENCODE_BUFFER(name) I965_RENDER_BUFFER(encode, name)
3531
3532 #define DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(name, member) DEF_RENDER_SINGLE_BUFFER_FUNC(encode, name, member)
3533 // DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(sequence_parameter, seq_param)
3534 // DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(picture_parameter, pic_param)
3535 // DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(picture_control, pic_control)
3536 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(qmatrix, q_matrix)
3537 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(iqmatrix, iq_matrix)
3538 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(huffman_table, huffman_table)
3539 /* extended buffer */
3540 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(sequence_parameter_ext, seq_param_ext)
3541 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(picture_parameter_ext, pic_param_ext)
3542 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(encmb_map, encmb_map)
3543 DEF_RENDER_ENCODE_SINGLE_BUFFER_FUNC(statistics_parameter_ext, stat_param_ext)
3544
3545 #define DEF_RENDER_ENCODE_MULTI_BUFFER_FUNC(name, member) DEF_RENDER_MULTI_BUFFER_FUNC(encode, name, member)
3546 // DEF_RENDER_ENCODE_MULTI_BUFFER_FUNC(slice_parameter, slice_params)
3547 DEF_RENDER_ENCODE_MULTI_BUFFER_FUNC(slice_parameter_ext, slice_params_ext)
3548
3549 DEF_RENDER_ENCODE_MULTI_BUFFER_FUNC(packed_header_params_ext, packed_header_params_ext)
3550 DEF_RENDER_ENCODE_MULTI_BUFFER_FUNC(packed_header_data_ext, packed_header_data_ext)
3551
3552 static VAStatus
3553 i965_encoder_render_packed_header_parameter_buffer(VADriverContextP ctx,
3554                                                    struct object_context *obj_context,
3555                                                    struct object_buffer *obj_buffer,
3556                                                    int type_index)
3557 {
3558     struct encode_state *encode = &obj_context->codec_state.encode;
3559
3560     ASSERT_RET(obj_buffer->buffer_store->bo == NULL, VA_STATUS_ERROR_INVALID_BUFFER);
3561     ASSERT_RET(obj_buffer->buffer_store->buffer, VA_STATUS_ERROR_INVALID_BUFFER);
3562     i965_release_buffer_store(&encode->packed_header_param[type_index]);
3563     i965_reference_buffer_store(&encode->packed_header_param[type_index], obj_buffer->buffer_store);
3564
3565     return VA_STATUS_SUCCESS;
3566 }
3567
3568 static VAStatus
3569 i965_encoder_render_packed_header_data_buffer(VADriverContextP ctx,
3570                                               struct object_context *obj_context,
3571                                               struct object_buffer *obj_buffer,
3572                                               int type_index)
3573 {
3574     struct encode_state *encode = &obj_context->codec_state.encode;
3575
3576     ASSERT_RET(obj_buffer->buffer_store->bo == NULL, VA_STATUS_ERROR_INVALID_BUFFER);
3577     ASSERT_RET(obj_buffer->buffer_store->buffer, VA_STATUS_ERROR_INVALID_BUFFER);
3578     i965_release_buffer_store(&encode->packed_header_data[type_index]);
3579     i965_reference_buffer_store(&encode->packed_header_data[type_index], obj_buffer->buffer_store);
3580
3581     return VA_STATUS_SUCCESS;
3582 }
3583
3584 static int
3585 i965_encoder_get_misc_paramerter_buffer_index(VADriverContextP ctx,
3586                                               struct encode_state *encode,
3587                                               VAEncMiscParameterBuffer *misc_param)
3588 {
3589     int index = 0;
3590
3591     if (!encode->has_layers)
3592         return 0;
3593
3594     if (misc_param->type == VAEncMiscParameterTypeRateControl) {
3595         VAEncMiscParameterRateControl *misc_rate_control = (VAEncMiscParameterRateControl *)misc_param->data;
3596
3597         index = misc_rate_control->rc_flags.bits.temporal_id;
3598     } else if (misc_param->type == VAEncMiscParameterTypeFrameRate) {
3599         VAEncMiscParameterFrameRate *misc_frame_rate = (VAEncMiscParameterFrameRate *)misc_param->data;
3600
3601         index = misc_frame_rate->framerate_flags.bits.temporal_id;
3602     }
3603
3604     return index;
3605 }
3606
3607 static VAStatus
3608 i965_encoder_render_misc_parameter_buffer(VADriverContextP ctx,
3609                                           struct object_context *obj_context,
3610                                           struct object_buffer *obj_buffer)
3611 {
3612     struct encode_state *encode = &obj_context->codec_state.encode;
3613     VAEncMiscParameterBuffer *param = NULL;
3614     int index;
3615
3616     ASSERT_RET(obj_buffer->buffer_store->bo == NULL, VA_STATUS_ERROR_INVALID_BUFFER);
3617     ASSERT_RET(obj_buffer->buffer_store->buffer, VA_STATUS_ERROR_INVALID_BUFFER);
3618
3619     param = (VAEncMiscParameterBuffer *)obj_buffer->buffer_store->buffer;
3620
3621     if (param->type >= ARRAY_ELEMS(encode->misc_param))
3622         return VA_STATUS_ERROR_INVALID_PARAMETER;
3623
3624     if (param->type == VAEncMiscParameterTypeTemporalLayerStructure)
3625         encode->has_layers = 1;
3626
3627     index = i965_encoder_get_misc_paramerter_buffer_index(ctx, encode, param);
3628
3629     if (index >= ARRAY_ELEMS(encode->misc_param[0]))
3630         return VA_STATUS_ERROR_INVALID_PARAMETER;
3631
3632     i965_release_buffer_store(&encode->misc_param[param->type][index]);
3633     i965_reference_buffer_store(&encode->misc_param[param->type][index], obj_buffer->buffer_store);
3634
3635     return VA_STATUS_SUCCESS;
3636 }
3637
3638 static VAStatus
3639 i965_encoder_render_picture(VADriverContextP ctx,
3640                             VAContextID context,
3641                             VABufferID *buffers,
3642                             int num_buffers)
3643 {
3644     struct i965_driver_data *i965 = i965_driver_data(ctx);
3645     struct object_context *obj_context = CONTEXT(context);
3646     struct object_config *obj_config;
3647     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
3648     struct encode_state *encode;
3649     int i;
3650
3651     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
3652     obj_config = obj_context->obj_config;
3653     ASSERT_RET(obj_config, VA_STATUS_ERROR_INVALID_CONFIG);
3654
3655     encode = &obj_context->codec_state.encode;
3656     for (i = 0; i < num_buffers; i++) {
3657         struct object_buffer *obj_buffer = BUFFER(buffers[i]);
3658
3659         if (!obj_buffer)
3660             return VA_STATUS_ERROR_INVALID_BUFFER;
3661
3662         switch (obj_buffer->type) {
3663         case VAQMatrixBufferType:
3664             vaStatus = I965_RENDER_ENCODE_BUFFER(qmatrix);
3665             break;
3666
3667         case VAIQMatrixBufferType:
3668             vaStatus = I965_RENDER_ENCODE_BUFFER(iqmatrix);
3669             break;
3670
3671         case VAEncSequenceParameterBufferType:
3672             vaStatus = I965_RENDER_ENCODE_BUFFER(sequence_parameter_ext);
3673             break;
3674
3675         case VAEncPictureParameterBufferType:
3676             vaStatus = I965_RENDER_ENCODE_BUFFER(picture_parameter_ext);
3677             break;
3678
3679         case VAHuffmanTableBufferType:
3680             vaStatus = I965_RENDER_ENCODE_BUFFER(huffman_table);
3681             break;
3682
3683         case VAEncSliceParameterBufferType:
3684             vaStatus = I965_RENDER_ENCODE_BUFFER(slice_parameter_ext);
3685             if (vaStatus == VA_STATUS_SUCCESS) {
3686                 /* When the max number of slices is updated, it also needs
3687                  * to reallocate the arrays that is used to store
3688                  * the packed data index/count for the slice
3689                  */
3690                 if (!(encode->packed_header_flag & VA_ENC_PACKED_HEADER_SLICE)) {
3691                     encode->slice_index++;
3692                 }
3693                 if (encode->slice_index == encode->max_slice_num) {
3694                     int slice_num = encode->max_slice_num;
3695                     encode->slice_rawdata_index = realloc(encode->slice_rawdata_index,
3696                                                           (slice_num + NUM_SLICES) * sizeof(int));
3697                     encode->slice_rawdata_count = realloc(encode->slice_rawdata_count,
3698                                                           (slice_num + NUM_SLICES) * sizeof(int));
3699                     encode->slice_header_index = realloc(encode->slice_header_index,
3700                                                          (slice_num + NUM_SLICES) * sizeof(int));
3701                     memset(encode->slice_rawdata_index + slice_num, 0,
3702                            sizeof(int) * NUM_SLICES);
3703                     memset(encode->slice_rawdata_count + slice_num, 0,
3704                            sizeof(int) * NUM_SLICES);
3705                     memset(encode->slice_header_index + slice_num, 0,
3706                            sizeof(int) * NUM_SLICES);
3707
3708                     encode->max_slice_num += NUM_SLICES;
3709                     if ((encode->slice_rawdata_index == NULL) ||
3710                         (encode->slice_header_index == NULL)  ||
3711                         (encode->slice_rawdata_count == NULL)) {
3712                         vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
3713                         return vaStatus;
3714                     }
3715                 }
3716             }
3717             break;
3718
3719         case VAEncPackedHeaderParameterBufferType: {
3720             VAEncPackedHeaderParameterBuffer *param = (VAEncPackedHeaderParameterBuffer *)obj_buffer->buffer_store->buffer;
3721             encode->last_packed_header_type = param->type;
3722
3723             if ((param->type == VAEncPackedHeaderRawData) ||
3724                 (param->type == VAEncPackedHeaderSlice)) {
3725                 vaStatus = I965_RENDER_ENCODE_BUFFER(packed_header_params_ext);
3726             } else if ((obj_config->profile == VAProfileHEVCMain ||
3727                         obj_config->profile == VAProfileHEVCMain10) &&
3728                        (encode->last_packed_header_type == VAEncPackedHeaderSequence)) {
3729                 vaStatus = i965_encoder_render_packed_header_parameter_buffer(ctx,
3730                                                                               obj_context,
3731                                                                               obj_buffer,
3732                                                                               va_enc_packed_type_to_idx(encode->last_packed_header_type) + encode->vps_sps_seq_index);
3733             } else {
3734                 vaStatus = i965_encoder_render_packed_header_parameter_buffer(ctx,
3735                                                                               obj_context,
3736                                                                               obj_buffer,
3737                                                                               va_enc_packed_type_to_idx(encode->last_packed_header_type));
3738             }
3739             break;
3740         }
3741
3742         case VAEncPackedHeaderDataBufferType: {
3743             if (encode->last_packed_header_type == 0) {
3744                 WARN_ONCE("the packed header data is passed without type!\n");
3745                 vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
3746                 return vaStatus;
3747             }
3748
3749             if (encode->last_packed_header_type == VAEncPackedHeaderRawData ||
3750                 encode->last_packed_header_type == VAEncPackedHeaderSlice) {
3751                 vaStatus = I965_RENDER_ENCODE_BUFFER(packed_header_data_ext);
3752
3753                 if (obj_config->profile == VAProfileVP9Profile0)
3754                     break;
3755
3756                 /* When the PACKED_SLICE_HEADER flag is passed, it will use
3757                  * the packed_slice_header as the delimeter to decide how
3758                  * the packed rawdata is inserted for the given slice.
3759                  * Otherwise it will use the VAEncSequenceParameterBuffer
3760                  * as the delimeter
3761                  */
3762                 if (encode->packed_header_flag & VA_ENC_PACKED_HEADER_SLICE) {
3763                     /* store the first index of the packed header data for current slice */
3764                     if (encode->slice_rawdata_index[encode->slice_index] == 0) {
3765                         encode->slice_rawdata_index[encode->slice_index] =
3766                             SLICE_PACKED_DATA_INDEX_TYPE | (encode->num_packed_header_data_ext - 1);
3767                     }
3768                     encode->slice_rawdata_count[encode->slice_index]++;
3769                     if (encode->last_packed_header_type == VAEncPackedHeaderSlice) {
3770                         /* find one packed slice_header delimeter. And the following
3771                          * packed data is for the next slice
3772                          */
3773                         encode->slice_header_index[encode->slice_index] =
3774                             SLICE_PACKED_DATA_INDEX_TYPE | (encode->num_packed_header_data_ext - 1);
3775                         encode->slice_index++;
3776                         /* Reallocate the buffer to record the index/count of
3777                          * packed_data for one slice.
3778                          */
3779                         if (encode->slice_index == encode->max_slice_num) {
3780                             int slice_num = encode->max_slice_num;
3781
3782                             encode->slice_rawdata_index = realloc(encode->slice_rawdata_index,
3783                                                                   (slice_num + NUM_SLICES) * sizeof(int));
3784                             encode->slice_rawdata_count = realloc(encode->slice_rawdata_count,
3785                                                                   (slice_num + NUM_SLICES) * sizeof(int));
3786                             encode->slice_header_index = realloc(encode->slice_header_index,
3787                                                                  (slice_num + NUM_SLICES) * sizeof(int));
3788                             memset(encode->slice_rawdata_index + slice_num, 0,
3789                                    sizeof(int) * NUM_SLICES);
3790                             memset(encode->slice_rawdata_count + slice_num, 0,
3791                                    sizeof(int) * NUM_SLICES);
3792                             memset(encode->slice_header_index + slice_num, 0,
3793                                    sizeof(int) * NUM_SLICES);
3794                             encode->max_slice_num += NUM_SLICES;
3795                         }
3796                     }
3797                 } else {
3798                     if (vaStatus == VA_STATUS_SUCCESS) {
3799                         /* store the first index of the packed header data for current slice */
3800                         if (encode->slice_rawdata_index[encode->slice_index] == 0) {
3801                             encode->slice_rawdata_index[encode->slice_index] =
3802                                 SLICE_PACKED_DATA_INDEX_TYPE | (encode->num_packed_header_data_ext - 1);
3803                         }
3804                         encode->slice_rawdata_count[encode->slice_index]++;
3805                         if (encode->last_packed_header_type == VAEncPackedHeaderSlice) {
3806                             if (encode->slice_header_index[encode->slice_index] == 0) {
3807                                 encode->slice_header_index[encode->slice_index] =
3808                                     SLICE_PACKED_DATA_INDEX_TYPE | (encode->num_packed_header_data_ext - 1);
3809                             } else {
3810                                 WARN_ONCE("Multi slice header data is passed for"
3811                                           " slice %d!\n", encode->slice_index);
3812                             }
3813                         }
3814                     }
3815                 }
3816             } else {
3817                 ASSERT_RET(encode->last_packed_header_type == VAEncPackedHeaderSequence ||
3818                            encode->last_packed_header_type == VAEncPackedHeaderPicture ||
3819                            encode->last_packed_header_type == VAEncPackedHeaderSlice ||
3820                            (((encode->last_packed_header_type & VAEncPackedHeaderMiscMask) == VAEncPackedHeaderMiscMask) &&
3821                             ((encode->last_packed_header_type & (~VAEncPackedHeaderMiscMask)) != 0)),
3822                            VA_STATUS_ERROR_ENCODING_ERROR);
3823
3824                 if ((obj_config->profile == VAProfileHEVCMain ||
3825                      obj_config->profile == VAProfileHEVCMain10) &&
3826                     (encode->last_packed_header_type == VAEncPackedHeaderSequence)) {
3827
3828                     vaStatus = i965_encoder_render_packed_header_data_buffer(ctx,
3829                                                                              obj_context,
3830                                                                              obj_buffer,
3831                                                                              va_enc_packed_type_to_idx(encode->last_packed_header_type) + encode->vps_sps_seq_index);
3832                     encode->vps_sps_seq_index = (encode->vps_sps_seq_index + 1) % I965_SEQ_PACKED_HEADER_END;
3833                 } else {
3834                     vaStatus = i965_encoder_render_packed_header_data_buffer(ctx,
3835                                                                              obj_context,
3836                                                                              obj_buffer,
3837                                                                              va_enc_packed_type_to_idx(encode->last_packed_header_type));
3838
3839                 }
3840             }
3841             encode->last_packed_header_type = 0;
3842             break;
3843         }
3844
3845         case VAEncMiscParameterBufferType:
3846             vaStatus = i965_encoder_render_misc_parameter_buffer(ctx,
3847                                                                  obj_context,
3848                                                                  obj_buffer);
3849             break;
3850
3851         case VAEncMacroblockMapBufferType:
3852             vaStatus = I965_RENDER_ENCODE_BUFFER(encmb_map);
3853             break;
3854
3855         default:
3856             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
3857             break;
3858         }
3859     }
3860
3861     return vaStatus;
3862 }
3863
3864 #define I965_RENDER_PROC_BUFFER(name) I965_RENDER_BUFFER(proc, name)
3865
3866 #define DEF_RENDER_PROC_SINGLE_BUFFER_FUNC(name, member) DEF_RENDER_SINGLE_BUFFER_FUNC(proc, name, member)
3867 DEF_RENDER_PROC_SINGLE_BUFFER_FUNC(pipeline_parameter, pipeline_param)
3868
3869 static VAStatus
3870 i965_proc_render_picture(VADriverContextP ctx,
3871                          VAContextID context,
3872                          VABufferID *buffers,
3873                          int num_buffers)
3874 {
3875     struct i965_driver_data *i965 = i965_driver_data(ctx);
3876     struct object_context *obj_context = CONTEXT(context);
3877     VAStatus vaStatus = VA_STATUS_SUCCESS;
3878     int i;
3879
3880     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
3881
3882     for (i = 0; i < num_buffers && vaStatus == VA_STATUS_SUCCESS; i++) {
3883         struct object_buffer *obj_buffer = BUFFER(buffers[i]);
3884
3885         if (!obj_buffer)
3886             return VA_STATUS_ERROR_INVALID_BUFFER;
3887
3888         switch (obj_buffer->type) {
3889         case VAProcPipelineParameterBufferType:
3890             vaStatus = I965_RENDER_PROC_BUFFER(pipeline_parameter);
3891             break;
3892
3893         default:
3894             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
3895             break;
3896         }
3897     }
3898
3899     return vaStatus;
3900 }
3901
3902 static VAStatus
3903 i965_pre_encoder_render_picture(VADriverContextP ctx,
3904                                 VAContextID context,
3905                                 VABufferID *buffers,
3906                                 int num_buffers)
3907 {
3908     struct i965_driver_data *i965 = i965_driver_data(ctx);
3909     struct object_context *obj_context = CONTEXT(context);
3910     struct object_config *obj_config;
3911     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
3912     int i;
3913
3914     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
3915     obj_config = obj_context->obj_config;
3916     ASSERT_RET(obj_config, VA_STATUS_ERROR_INVALID_CONFIG);
3917
3918     for (i = 0; i < num_buffers; i++) {
3919         struct object_buffer *obj_buffer = BUFFER(buffers[i]);
3920
3921         if (!obj_buffer)
3922             return VA_STATUS_ERROR_INVALID_BUFFER;
3923
3924         switch (obj_buffer->type) {
3925         case VAStatsStatisticsParameterBufferType:
3926             vaStatus = I965_RENDER_ENCODE_BUFFER(statistics_parameter_ext);
3927             break;
3928
3929         default:
3930             vaStatus = VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
3931             break;
3932         }
3933     }
3934
3935     return vaStatus;
3936 }
3937
3938 VAStatus
3939 i965_RenderPicture(VADriverContextP ctx,
3940                    VAContextID context,
3941                    VABufferID *buffers,
3942                    int num_buffers)
3943 {
3944     struct i965_driver_data *i965 = i965_driver_data(ctx);
3945     struct object_context *obj_context;
3946     struct object_config *obj_config;
3947     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
3948
3949     obj_context = CONTEXT(context);
3950     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
3951
3952     if (num_buffers <= 0)
3953         return VA_STATUS_ERROR_INVALID_PARAMETER;
3954
3955     obj_config = obj_context->obj_config;
3956     ASSERT_RET(obj_config, VA_STATUS_ERROR_INVALID_CONFIG);
3957
3958     if (VAEntrypointVideoProc == obj_config->entrypoint) {
3959         vaStatus = i965_proc_render_picture(ctx, context, buffers, num_buffers);
3960     } else if ((VAEntrypointEncSlice == obj_config->entrypoint) ||
3961                (VAEntrypointEncPicture == obj_config->entrypoint) ||
3962                (VAEntrypointEncSliceLP == obj_config->entrypoint) ||
3963                (VAEntrypointFEI == obj_config->entrypoint)) {
3964         vaStatus = i965_encoder_render_picture(ctx, context, buffers, num_buffers);
3965     } else if (VAEntrypointStats == obj_config->entrypoint) {
3966         vaStatus = i965_pre_encoder_render_picture(ctx, context, buffers, num_buffers);
3967     } else {
3968         vaStatus = i965_decoder_render_picture(ctx, context, buffers, num_buffers);
3969     }
3970
3971     return vaStatus;
3972 }
3973
3974 VAStatus
3975 i965_EndPicture(VADriverContextP ctx, VAContextID context)
3976 {
3977     struct i965_driver_data *i965 = i965_driver_data(ctx);
3978     struct object_context *obj_context = CONTEXT(context);
3979     struct object_config *obj_config;
3980
3981     ASSERT_RET(obj_context, VA_STATUS_ERROR_INVALID_CONTEXT);
3982     obj_config = obj_context->obj_config;
3983     ASSERT_RET(obj_config, VA_STATUS_ERROR_INVALID_CONFIG);
3984
3985     if (obj_context->codec_type == CODEC_PROC) {
3986         ASSERT_RET(VAEntrypointVideoProc == obj_config->entrypoint, VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT);
3987     } else if (obj_context->codec_type == CODEC_ENC) {
3988         ASSERT_RET(((VAEntrypointEncSlice == obj_config->entrypoint) ||
3989                     (VAEntrypointEncPicture == obj_config->entrypoint) ||
3990                     (VAEntrypointEncSliceLP == obj_config->entrypoint) ||
3991                     (VAEntrypointFEI == obj_config->entrypoint)),
3992                    VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT);
3993
3994         if (obj_context->codec_state.encode.num_packed_header_params_ext !=
3995             obj_context->codec_state.encode.num_packed_header_data_ext) {
3996             WARN_ONCE("the packed header/data is not paired for encoding!\n");
3997             return VA_STATUS_ERROR_INVALID_PARAMETER;
3998         }
3999         if (!obj_context->codec_state.encode.pic_param_ext) {
4000             return VA_STATUS_ERROR_INVALID_PARAMETER;
4001         }
4002         if (!obj_context->codec_state.encode.seq_param_ext &&
4003             (VAEntrypointEncPicture != obj_config->entrypoint)) {
4004             /* The seq_param is not mandatory for VP9 encoding */
4005             if (obj_config->profile != VAProfileVP9Profile0)
4006                 return VA_STATUS_ERROR_INVALID_PARAMETER;
4007         }
4008         if ((obj_context->codec_state.encode.num_slice_params_ext <= 0) &&
4009             ((obj_config->profile != VAProfileVP8Version0_3) &&
4010              (obj_config->profile != VAProfileVP9Profile0))) {
4011             return VA_STATUS_ERROR_INVALID_PARAMETER;
4012         }
4013
4014         if ((obj_context->codec_state.encode.packed_header_flag & VA_ENC_PACKED_HEADER_SLICE) &&
4015             (obj_context->codec_state.encode.num_slice_params_ext !=
4016              obj_context->codec_state.encode.slice_index)) {
4017             WARN_ONCE("packed slice_header data is missing for some slice"
4018                       " under packed SLICE_HEADER mode\n");
4019             return VA_STATUS_ERROR_INVALID_PARAMETER;
4020         }
4021     } else if (obj_context->codec_type == CODEC_PREENC) {
4022         ASSERT_RET((VAEntrypointStats == obj_config->entrypoint),
4023                    VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT);
4024
4025         if (!obj_context->codec_state.encode.stat_param_ext) {
4026             return VA_STATUS_ERROR_INVALID_PARAMETER;
4027         }
4028     } else {
4029         if (obj_context->codec_state.decode.pic_param == NULL) {
4030             return VA_STATUS_ERROR_INVALID_PARAMETER;
4031         }
4032         if (obj_context->codec_state.decode.num_slice_params <= 0) {
4033             return VA_STATUS_ERROR_INVALID_PARAMETER;
4034         }
4035         if (obj_context->codec_state.decode.num_slice_datas <= 0) {
4036             return VA_STATUS_ERROR_INVALID_PARAMETER;
4037         }
4038
4039         if (obj_context->codec_state.decode.num_slice_params !=
4040             obj_context->codec_state.decode.num_slice_datas) {
4041             return VA_STATUS_ERROR_INVALID_PARAMETER;
4042         }
4043
4044         if (obj_context->wrapper_context != VA_INVALID_ID) {
4045             /* call the vaEndPicture of wrapped driver */
4046             VADriverContextP pdrvctx;
4047             VAStatus va_status;
4048
4049             pdrvctx = i965->wrapper_pdrvctx;
4050             CALL_VTABLE(pdrvctx, va_status,
4051                         vaEndPicture(pdrvctx, obj_context->wrapper_context));
4052
4053             return va_status;
4054         }
4055     }
4056
4057     ASSERT_RET(obj_context->hw_context->run, VA_STATUS_ERROR_OPERATION_FAILED);
4058     return obj_context->hw_context->run(ctx, obj_config->profile, &obj_context->codec_state, obj_context->hw_context);
4059 }
4060
4061 VAStatus
4062 i965_SyncSurface(VADriverContextP ctx,
4063                  VASurfaceID render_target)
4064 {
4065     struct i965_driver_data *i965 = i965_driver_data(ctx);
4066     struct object_surface *obj_surface = SURFACE(render_target);
4067
4068     ASSERT_RET(obj_surface, VA_STATUS_ERROR_INVALID_SURFACE);
4069
4070     if (obj_surface->bo)
4071         drm_intel_bo_wait_rendering(obj_surface->bo);
4072
4073     return VA_STATUS_SUCCESS;
4074 }
4075
4076 VAStatus
4077 i965_QuerySurfaceStatus(VADriverContextP ctx,
4078                         VASurfaceID render_target,
4079                         VASurfaceStatus *status)        /* out */
4080 {
4081     struct i965_driver_data *i965 = i965_driver_data(ctx);
4082     struct object_surface *obj_surface = SURFACE(render_target);
4083
4084     ASSERT_RET(obj_surface, VA_STATUS_ERROR_INVALID_SURFACE);
4085
4086     if (obj_surface->bo) {
4087         if (drm_intel_bo_busy(obj_surface->bo)) {
4088             *status = VASurfaceRendering;
4089         } else {
4090             *status = VASurfaceReady;
4091         }
4092     } else {
4093         *status = VASurfaceReady;
4094     }
4095
4096     return VA_STATUS_SUCCESS;
4097 }
4098
4099 static VADisplayAttribute *
4100 get_display_attribute(VADriverContextP ctx, VADisplayAttribType type)
4101 {
4102     struct i965_driver_data * const i965 = i965_driver_data(ctx);
4103     unsigned int i;
4104
4105     if (!i965->display_attributes)
4106         return NULL;
4107
4108     for (i = 0; i < i965->num_display_attributes; i++) {
4109         if (i965->display_attributes[i].type == type)
4110             return &i965->display_attributes[i];
4111     }
4112     return NULL;
4113 }
4114
4115 static void
4116 i965_display_attributes_terminate(VADriverContextP ctx)
4117 {
4118     struct i965_driver_data * const i965 = i965_driver_data(ctx);
4119
4120     if (i965->display_attributes) {
4121         free(i965->display_attributes);
4122         i965->display_attributes = NULL;
4123         i965->num_display_attributes = 0;
4124     }
4125 }
4126
4127 static bool
4128 i965_display_attributes_init(VADriverContextP ctx)
4129 {
4130     struct i965_driver_data * const i965 = i965_driver_data(ctx);
4131
4132     i965->num_display_attributes = ARRAY_ELEMS(i965_display_attributes);
4133     i965->display_attributes = malloc(
4134                                    i965->num_display_attributes * sizeof(i965->display_attributes[0]));
4135     if (!i965->display_attributes)
4136         goto error;
4137
4138     memcpy(
4139         i965->display_attributes,
4140         i965_display_attributes,
4141         sizeof(i965_display_attributes)
4142     );
4143
4144     i965->rotation_attrib = get_display_attribute(ctx, VADisplayAttribRotation);
4145     i965->brightness_attrib = get_display_attribute(ctx, VADisplayAttribBrightness);
4146     i965->contrast_attrib = get_display_attribute(ctx, VADisplayAttribContrast);
4147     i965->hue_attrib = get_display_attribute(ctx, VADisplayAttribHue);
4148     i965->saturation_attrib = get_display_attribute(ctx, VADisplayAttribSaturation);
4149
4150     if (!i965->rotation_attrib ||
4151         !i965->brightness_attrib ||
4152         !i965->contrast_attrib ||
4153         !i965->hue_attrib ||
4154         !i965->saturation_attrib) {
4155         goto error;
4156     }
4157     return true;
4158
4159 error:
4160     i965_display_attributes_terminate(ctx);
4161     return false;
4162 }
4163
4164 /*
4165  * Query display attributes
4166  * The caller must provide a "attr_list" array that can hold at
4167  * least vaMaxNumDisplayAttributes() entries. The actual number of attributes
4168  * returned in "attr_list" is returned in "num_attributes".
4169  */
4170 VAStatus
4171 i965_QueryDisplayAttributes(
4172     VADriverContextP    ctx,
4173     VADisplayAttribute *attribs,        /* out */
4174     int                *num_attribs_ptr /* out */
4175 )
4176 {
4177     const int num_attribs = ARRAY_ELEMS(i965_display_attributes);
4178
4179     if (attribs && num_attribs > 0)
4180         memcpy(attribs, i965_display_attributes, sizeof(i965_display_attributes));
4181
4182     if (num_attribs_ptr)
4183         *num_attribs_ptr = num_attribs;
4184
4185     return VA_STATUS_SUCCESS;
4186 }
4187
4188 /*
4189  * Get display attributes
4190  * This function returns the current attribute values in "attr_list".
4191  * Only attributes returned with VA_DISPLAY_ATTRIB_GETTABLE set in the "flags" field
4192  * from vaQueryDisplayAttributes() can have their values retrieved.
4193  */
4194 VAStatus
4195 i965_GetDisplayAttributes(
4196     VADriverContextP    ctx,
4197     VADisplayAttribute *attribs,        /* inout */
4198     int                 num_attribs     /* in */
4199 )
4200 {
4201     int i;
4202
4203     for (i = 0; i < num_attribs; i++) {
4204         VADisplayAttribute *src_attrib, * const dst_attrib = &attribs[i];
4205
4206         src_attrib = get_display_attribute(ctx, dst_attrib->type);
4207
4208         if (src_attrib && (src_attrib->flags & VA_DISPLAY_ATTRIB_GETTABLE)) {
4209             dst_attrib->min_value = src_attrib->min_value;
4210             dst_attrib->max_value = src_attrib->max_value;
4211             dst_attrib->value     = src_attrib->value;
4212             dst_attrib->flags     = src_attrib->flags;
4213         } else if (src_attrib &&
4214                    (src_attrib->flags & VA_DISPLAY_ATTRIB_SETTABLE)) {
4215             dst_attrib->flags     = src_attrib->flags;
4216         } else
4217             dst_attrib->flags = VA_DISPLAY_ATTRIB_NOT_SUPPORTED;
4218     }
4219     return VA_STATUS_SUCCESS;
4220 }
4221
4222 /*
4223  * Set display attributes
4224  * Only attributes returned with VA_DISPLAY_ATTRIB_SETTABLE set in the "flags" field
4225  * from vaQueryDisplayAttributes() can be set.  If the attribute is not settable or
4226  * the value is out of range, the function returns VA_STATUS_ERROR_ATTR_NOT_SUPPORTED
4227  */
4228 VAStatus
4229 i965_SetDisplayAttributes(
4230     VADriverContextP    ctx,
4231     VADisplayAttribute *attribs,        /* in */
4232     int                 num_attribs     /* in */
4233 )
4234 {
4235     int i;
4236
4237     for (i = 0; i < num_attribs; i++) {
4238         VADisplayAttribute *dst_attrib, * const src_attrib = &attribs[i];
4239
4240         dst_attrib = get_display_attribute(ctx, src_attrib->type);
4241         if (!dst_attrib)
4242             return VA_STATUS_ERROR_ATTR_NOT_SUPPORTED;
4243
4244         if (!(dst_attrib->flags & VA_DISPLAY_ATTRIB_SETTABLE))
4245             continue;
4246
4247         if (src_attrib->value < dst_attrib->min_value ||
4248             src_attrib->value > dst_attrib->max_value)
4249             return VA_STATUS_ERROR_INVALID_PARAMETER;
4250
4251         dst_attrib->value = src_attrib->value;
4252         /* XXX: track modified attributes through timestamps */
4253     }
4254     return VA_STATUS_SUCCESS;
4255 }
4256
4257 VAStatus
4258 i965_DbgCopySurfaceToBuffer(VADriverContextP ctx,
4259                             VASurfaceID surface,
4260                             void **buffer,              /* out */
4261                             unsigned int *stride)       /* out */
4262 {
4263     /* TODO */
4264     return VA_STATUS_ERROR_UNIMPLEMENTED;
4265 }
4266
4267 static void
4268 i965_destroy_heap(struct object_heap *heap,
4269                   void (*func)(struct object_heap *heap, struct object_base *object))
4270 {
4271     struct object_base *object;
4272     object_heap_iterator iter;
4273
4274     object = object_heap_first(heap, &iter);
4275
4276     while (object) {
4277         if (func)
4278             func(heap, object);
4279
4280         object = object_heap_next(heap, &iter);
4281     }
4282
4283     object_heap_destroy(heap);
4284 }
4285
4286
4287 VAStatus
4288 i965_DestroyImage(VADriverContextP ctx, VAImageID image);
4289
4290 VAStatus
4291 i965_CreateImage(VADriverContextP ctx,
4292                  VAImageFormat *format,
4293                  int width,
4294                  int height,
4295                  VAImage *out_image)        /* out */
4296 {
4297     struct i965_driver_data *i965 = i965_driver_data(ctx);
4298     struct object_image *obj_image;
4299     VAStatus va_status = VA_STATUS_ERROR_OPERATION_FAILED;
4300     VAImageID image_id;
4301     unsigned int size2, size, awidth, aheight;
4302
4303     out_image->image_id = VA_INVALID_ID;
4304     out_image->buf      = VA_INVALID_ID;
4305
4306     image_id = NEW_IMAGE_ID();
4307     if (image_id == VA_INVALID_ID)
4308         return VA_STATUS_ERROR_ALLOCATION_FAILED;
4309
4310     obj_image = IMAGE(image_id);
4311     if (!obj_image)
4312         return VA_STATUS_ERROR_ALLOCATION_FAILED;
4313     obj_image->bo         = NULL;
4314     obj_image->palette    = NULL;
4315     obj_image->derived_surface = VA_INVALID_ID;
4316
4317     VAImage * const image = &obj_image->image;
4318     image->image_id       = image_id;
4319     image->buf            = VA_INVALID_ID;
4320
4321     awidth = ALIGN(width, i965->codec_info->min_linear_wpitch);
4322
4323     if ((format->fourcc == VA_FOURCC_YV12) ||
4324         (format->fourcc == VA_FOURCC_I420)) {
4325         if (awidth % 128 != 0) {
4326             awidth = ALIGN(width, 128);
4327         }
4328     }
4329
4330     aheight = ALIGN(height, i965->codec_info->min_linear_hpitch);
4331     size    = awidth * aheight;
4332     size2    = (awidth / 2) * (aheight / 2);
4333
4334     image->num_palette_entries = 0;
4335     image->entry_bytes         = 0;
4336     memset(image->component_order, 0, sizeof(image->component_order));
4337
4338     switch (format->fourcc) {
4339     case VA_FOURCC_IA44:
4340     case VA_FOURCC_AI44:
4341         image->num_planes = 1;
4342         image->pitches[0] = awidth;
4343         image->offsets[0] = 0;
4344         image->data_size  = image->offsets[0] + image->pitches[0] * aheight;
4345         image->num_palette_entries = 16;
4346         image->entry_bytes         = 3;
4347         image->component_order[0]  = 'R';
4348         image->component_order[1]  = 'G';
4349         image->component_order[2]  = 'B';
4350         break;
4351     case VA_FOURCC_IA88:
4352     case VA_FOURCC_AI88:
4353         image->num_planes = 1;
4354         image->pitches[0] = awidth * 2;
4355         image->offsets[0] = 0;
4356         image->data_size  = image->offsets[0] + image->pitches[0] * aheight;
4357         image->num_palette_entries = 256;
4358         image->entry_bytes         = 3;
4359         image->component_order[0]  = 'R';
4360         image->component_order[1]  = 'G';
4361         image->component_order[2]  = 'B';
4362         break;
4363     case VA_FOURCC_ARGB:
4364     case VA_FOURCC_ABGR:
4365     case VA_FOURCC_BGRA:
4366     case VA_FOURCC_RGBA:
4367     case VA_FOURCC_BGRX:
4368     case VA_FOURCC_RGBX:
4369         image->num_planes = 1;
4370         image->pitches[0] = awidth * 4;
4371         image->offsets[0] = 0;
4372         image->data_size  = image->offsets[0] + image->pitches[0] * aheight;
4373         break;
4374     case VA_FOURCC_YV12:
4375         image->num_planes = 3;
4376         image->pitches[0] = awidth;
4377         image->offsets[0] = 0;
4378         image->pitches[1] = awidth / 2;
4379         image->offsets[1] = size;
4380         image->pitches[2] = awidth / 2;
4381         image->offsets[2] = size + size2;
4382         image->data_size  = size + 2 * size2;
4383         break;
4384     case VA_FOURCC_I420:
4385         image->num_planes = 3;
4386         image->pitches[0] = awidth;
4387         image->offsets[0] = 0;
4388         image->pitches[1] = awidth / 2;
4389         image->offsets[1] = size;
4390         image->pitches[2] = awidth / 2;
4391         image->offsets[2] = size + size2;
4392         image->data_size  = size + 2 * size2;
4393         break;
4394     case VA_FOURCC_422H:
4395         image->num_planes = 3;
4396         image->pitches[0] = awidth;
4397         image->offsets[0] = 0;
4398         image->pitches[1] = awidth / 2;
4399         image->offsets[1] = size;
4400         image->pitches[2] = awidth / 2;
4401         image->offsets[2] = size + (awidth / 2) * aheight;
4402         image->data_size  = size + 2 * ((awidth / 2) * aheight);
4403         break;
4404     case VA_FOURCC_NV12:
4405         image->num_planes = 2;
4406         image->pitches[0] = awidth;
4407         image->offsets[0] = 0;
4408         image->pitches[1] = awidth;
4409         image->offsets[1] = size;
4410         image->data_size  = size + 2 * size2;
4411         break;
4412     case VA_FOURCC_YUY2:
4413     case VA_FOURCC_UYVY:
4414         image->num_planes = 1;
4415         image->pitches[0] = awidth * 2;
4416         image->offsets[0] = 0;
4417         image->data_size  = size * 2;
4418         break;
4419     case VA_FOURCC_P010:
4420         image->num_planes = 2;
4421         image->pitches[0] = awidth * 2;
4422         image->offsets[0] = 0;
4423         image->pitches[1] = awidth * 2;
4424         image->offsets[1] = size * 2;
4425         image->data_size  = size * 2 + 2 * size2 * 2;
4426         break;
4427     default:
4428         goto error;
4429     }
4430
4431     va_status = i965_CreateBuffer(ctx, 0, VAImageBufferType,
4432                                   image->data_size, 1, NULL, &image->buf);
4433     if (va_status != VA_STATUS_SUCCESS)
4434         goto error;
4435
4436     struct object_buffer *obj_buffer = BUFFER(image->buf);
4437
4438     if (!obj_buffer ||
4439         !obj_buffer->buffer_store ||
4440         !obj_buffer->buffer_store->bo)
4441         return VA_STATUS_ERROR_ALLOCATION_FAILED;
4442
4443     obj_image->bo = obj_buffer->buffer_store->bo;
4444     dri_bo_reference(obj_image->bo);
4445
4446     if (image->num_palette_entries > 0 && image->entry_bytes > 0) {
4447         obj_image->palette = malloc(image->num_palette_entries * sizeof(*obj_image->palette));
4448         if (!obj_image->palette)
4449             goto error;
4450     }
4451
4452     image->image_id             = image_id;
4453     image->format               = *format;
4454     image->width                = width;
4455     image->height               = height;
4456
4457     *out_image                  = *image;
4458     return VA_STATUS_SUCCESS;
4459
4460 error:
4461     i965_DestroyImage(ctx, image_id);
4462     return va_status;
4463 }
4464
4465 VAStatus
4466 i965_check_alloc_surface_bo(VADriverContextP ctx,
4467                             struct object_surface *obj_surface,
4468                             int tiled,
4469                             unsigned int fourcc,
4470                             unsigned int subsampling)
4471 {
4472     struct i965_driver_data *i965 = i965_driver_data(ctx);
4473     int region_width, region_height;
4474
4475     if (obj_surface->bo) {
4476         ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
4477         ASSERT_RET(obj_surface->fourcc == fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
4478         ASSERT_RET(obj_surface->subsampling == subsampling, VA_STATUS_ERROR_INVALID_SURFACE);
4479         return VA_STATUS_SUCCESS;
4480     }
4481
4482     obj_surface->x_cb_offset = 0; /* X offset is always 0 */
4483     obj_surface->x_cr_offset = 0;
4484
4485     int bpp_1stplane = bpp_1stplane_by_fourcc(fourcc);
4486
4487     if ((tiled && !obj_surface->user_disable_tiling)) {
4488         ASSERT_RET(fourcc != VA_FOURCC_I420 &&
4489                    fourcc != VA_FOURCC_IYUV &&
4490                    fourcc != VA_FOURCC_YV12,
4491                    VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT);
4492
4493         if (obj_surface->user_h_stride_set) {
4494             ASSERT_RET(IS_ALIGNED(obj_surface->width, 128), VA_STATUS_ERROR_INVALID_PARAMETER);
4495         } else
4496             obj_surface->width = ALIGN(obj_surface->orig_width * bpp_1stplane, 128);
4497
4498         if (obj_surface->user_v_stride_set) {
4499             ASSERT_RET(IS_ALIGNED(obj_surface->height, 32), VA_STATUS_ERROR_INVALID_PARAMETER);
4500         } else
4501             obj_surface->height = ALIGN(obj_surface->orig_height, 32);
4502
4503         region_height = obj_surface->height;
4504
4505         switch (fourcc) {
4506         case VA_FOURCC_NV12:
4507         case VA_FOURCC_P010:
4508             assert(subsampling == SUBSAMPLE_YUV420);
4509             obj_surface->cb_cr_pitch = obj_surface->width;
4510             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4511             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4512             obj_surface->y_cb_offset = obj_surface->height;
4513             obj_surface->y_cr_offset = obj_surface->height;
4514             region_width = obj_surface->width;
4515             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32);
4516
4517             break;
4518
4519         case VA_FOURCC_IMC1:
4520             assert(subsampling == SUBSAMPLE_YUV420);
4521             obj_surface->cb_cr_pitch = obj_surface->width;
4522             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4523             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4524             obj_surface->y_cr_offset = obj_surface->height;
4525             obj_surface->y_cb_offset = obj_surface->y_cr_offset + ALIGN(obj_surface->cb_cr_height, 32);
4526             region_width = obj_surface->width;
4527             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32) * 2;
4528
4529             break;
4530
4531         case VA_FOURCC_IMC3:
4532             assert(subsampling == SUBSAMPLE_YUV420);
4533             obj_surface->cb_cr_pitch = obj_surface->width;
4534             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4535             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4536             obj_surface->y_cb_offset = obj_surface->height;
4537             obj_surface->y_cr_offset = obj_surface->y_cb_offset + ALIGN(obj_surface->cb_cr_height, 32);
4538             region_width = obj_surface->width;
4539             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32) * 2;
4540
4541             break;
4542
4543         case VA_FOURCC_422H:
4544             assert(subsampling == SUBSAMPLE_YUV422H);
4545             obj_surface->cb_cr_pitch = obj_surface->width;
4546             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4547             obj_surface->cb_cr_height = obj_surface->orig_height;
4548             obj_surface->y_cb_offset = obj_surface->height;
4549             obj_surface->y_cr_offset = obj_surface->y_cb_offset + ALIGN(obj_surface->cb_cr_height, 32);
4550             region_width = obj_surface->width;
4551             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32) * 2;
4552
4553             break;
4554
4555         case VA_FOURCC_422V:
4556             assert(subsampling == SUBSAMPLE_YUV422V);
4557             obj_surface->cb_cr_pitch = obj_surface->width;
4558             obj_surface->cb_cr_width = obj_surface->orig_width;
4559             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4560             obj_surface->y_cb_offset = obj_surface->height;
4561             obj_surface->y_cr_offset = obj_surface->y_cb_offset + ALIGN(obj_surface->cb_cr_height, 32);
4562             region_width = obj_surface->width;
4563             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32) * 2;
4564
4565             break;
4566
4567         case VA_FOURCC_411P:
4568             assert(subsampling == SUBSAMPLE_YUV411);
4569             obj_surface->cb_cr_pitch = obj_surface->width;
4570             obj_surface->cb_cr_width = obj_surface->orig_width / 4;
4571             obj_surface->cb_cr_height = obj_surface->orig_height;
4572             obj_surface->y_cb_offset = obj_surface->height;
4573             obj_surface->y_cr_offset = obj_surface->y_cb_offset + ALIGN(obj_surface->cb_cr_height, 32);
4574             region_width = obj_surface->width;
4575             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32) * 2;
4576
4577             break;
4578
4579         case VA_FOURCC_444P:
4580             assert(subsampling == SUBSAMPLE_YUV444);
4581             obj_surface->cb_cr_pitch = obj_surface->width;
4582             obj_surface->cb_cr_width = obj_surface->orig_width;
4583             obj_surface->cb_cr_height = obj_surface->orig_height;
4584             obj_surface->y_cb_offset = obj_surface->height;
4585             obj_surface->y_cr_offset = obj_surface->y_cb_offset + ALIGN(obj_surface->cb_cr_height, 32);
4586             region_width = obj_surface->width;
4587             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32) * 2;
4588
4589             break;
4590
4591         case VA_FOURCC_Y800:
4592             assert(subsampling == SUBSAMPLE_YUV400);
4593             obj_surface->cb_cr_pitch = 0;
4594             obj_surface->cb_cr_width = 0;
4595             obj_surface->cb_cr_height = 0;
4596             obj_surface->y_cb_offset = 0;
4597             obj_surface->y_cr_offset = 0;
4598             region_width = obj_surface->width;
4599             region_height = obj_surface->height;
4600
4601             break;
4602
4603         case VA_FOURCC_YUY2:
4604         case VA_FOURCC_UYVY:
4605             assert(subsampling == SUBSAMPLE_YUV422H);
4606             obj_surface->width = ALIGN(obj_surface->orig_width * 2, 128);
4607             obj_surface->cb_cr_pitch = obj_surface->width;
4608             obj_surface->y_cb_offset = 0;
4609             obj_surface->y_cr_offset = 0;
4610             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4611             obj_surface->cb_cr_height = obj_surface->orig_height;
4612             region_width = obj_surface->width;
4613             region_height = obj_surface->height;
4614
4615             break;
4616
4617         case VA_FOURCC_RGBA:
4618         case VA_FOURCC_RGBX:
4619         case VA_FOURCC_BGRA:
4620         case VA_FOURCC_BGRX:
4621             assert(subsampling == SUBSAMPLE_RGBX);
4622
4623             obj_surface->width = ALIGN(obj_surface->orig_width * 4, 128);
4624             region_width = obj_surface->width;
4625             region_height = obj_surface->height;
4626             break;
4627
4628         default:
4629             /* Never get here */
4630             ASSERT_RET(0, VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT);
4631             break;
4632         }
4633     } else {
4634         assert(subsampling == SUBSAMPLE_YUV420 ||
4635                subsampling == SUBSAMPLE_YUV422H ||
4636                subsampling == SUBSAMPLE_YUV422V ||
4637                subsampling == SUBSAMPLE_RGBX);
4638
4639         region_width = obj_surface->width;
4640         region_height = obj_surface->height;
4641
4642         switch (fourcc) {
4643         case VA_FOURCC_NV12:
4644         case VA_FOURCC_P010:
4645             obj_surface->y_cb_offset = obj_surface->height;
4646             obj_surface->y_cr_offset = obj_surface->height;
4647             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4648             obj_surface->width = ALIGN(obj_surface->cb_cr_width * 2, i965->codec_info->min_linear_wpitch) *
4649                                  bpp_1stplane;
4650             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4651             obj_surface->cb_cr_pitch = obj_surface->width;
4652             region_width = obj_surface->width;
4653             region_height = obj_surface->height + obj_surface->height / 2;
4654             break;
4655
4656         case VA_FOURCC_YV16:
4657             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4658             obj_surface->width = ALIGN(obj_surface->cb_cr_width, i965->codec_info->min_linear_wpitch) * 2;
4659             obj_surface->cb_cr_height = obj_surface->orig_height;
4660             obj_surface->y_cr_offset = obj_surface->height;
4661             obj_surface->y_cb_offset = obj_surface->y_cr_offset + ALIGN(obj_surface->cb_cr_height, 32) / 2;
4662             obj_surface->cb_cr_pitch = obj_surface->width / 2;
4663             region_width = obj_surface->width;
4664             region_height = obj_surface->height + ALIGN(obj_surface->cb_cr_height, 32);
4665             break;
4666
4667         case VA_FOURCC_YV12:
4668         case VA_FOURCC_I420:
4669         case VA_FOURCC_IYUV:
4670             if (fourcc == VA_FOURCC_YV12) {
4671                 obj_surface->y_cr_offset = obj_surface->height;
4672                 obj_surface->y_cb_offset = obj_surface->height + obj_surface->height / 4;
4673             } else {
4674                 obj_surface->y_cb_offset = obj_surface->height;
4675                 obj_surface->y_cr_offset = obj_surface->height + obj_surface->height / 4;
4676             }
4677
4678             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4679             obj_surface->width = ALIGN(obj_surface->cb_cr_width, i965->codec_info->min_linear_wpitch) * 2;
4680             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4681             obj_surface->cb_cr_pitch = obj_surface->width / 2;
4682             region_width = obj_surface->width;
4683             region_height = obj_surface->height + obj_surface->height / 2;
4684             break;
4685
4686         case VA_FOURCC_I010:
4687             obj_surface->y_cb_offset = obj_surface->height;
4688             obj_surface->y_cr_offset = obj_surface->height + obj_surface->height / 4;
4689             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4690             obj_surface->width = ALIGN(obj_surface->cb_cr_width * 2, i965->codec_info->min_linear_wpitch) * 2;
4691             obj_surface->cb_cr_height = obj_surface->orig_height / 2;
4692             obj_surface->cb_cr_pitch = obj_surface->width / 2;
4693             region_width = obj_surface->width;
4694             region_height = obj_surface->height + obj_surface->height / 2;
4695
4696             break;
4697         case VA_FOURCC_YUY2:
4698         case VA_FOURCC_UYVY:
4699             obj_surface->width = ALIGN(obj_surface->orig_width * 2, i965->codec_info->min_linear_wpitch);
4700             obj_surface->y_cb_offset = 0;
4701             obj_surface->y_cr_offset = 0;
4702             obj_surface->cb_cr_width = obj_surface->orig_width / 2;
4703             obj_surface->cb_cr_height = obj_surface->orig_height;
4704             obj_surface->cb_cr_pitch = obj_surface->width;
4705             region_width = obj_surface->width;
4706             region_height = obj_surface->height;
4707             break;
4708         case VA_FOURCC_RGBA:
4709         case VA_FOURCC_RGBX:
4710         case VA_FOURCC_BGRA:
4711         case VA_FOURCC_BGRX:
4712             obj_surface->width = ALIGN(obj_surface->orig_width * 4, i965->codec_info->min_linear_wpitch);
4713             region_width = obj_surface->width;
4714             region_height = obj_surface->height;
4715             break;
4716
4717         default:
4718             /* Never get here */
4719             ASSERT_RET(0, VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT);
4720             break;
4721         }
4722     }
4723
4724     obj_surface->size = ALIGN(region_width * region_height, 0x1000);
4725
4726     if ((tiled && !obj_surface->user_disable_tiling)) {
4727         uint32_t tiling_mode = I915_TILING_Y; /* always uses Y-tiled format */
4728         unsigned long pitch;
4729
4730         obj_surface->bo = drm_intel_bo_alloc_tiled(i965->intel.bufmgr,
4731                                                    "vaapi surface",
4732                                                    region_width,
4733                                                    region_height,
4734                                                    1,
4735                                                    &tiling_mode,
4736                                                    &pitch,
4737                                                    0);
4738         assert(tiling_mode == I915_TILING_Y);
4739         assert(pitch == obj_surface->width);
4740     } else {
4741         obj_surface->bo = dri_bo_alloc(i965->intel.bufmgr,
4742                                        "vaapi surface",
4743                                        obj_surface->size,
4744                                        0x1000);
4745     }
4746
4747     obj_surface->fourcc = fourcc;
4748     obj_surface->subsampling = subsampling;
4749     assert(obj_surface->bo);
4750     return VA_STATUS_SUCCESS;
4751 }
4752
4753 VAStatus i965_DeriveImage(VADriverContextP ctx,
4754                           VASurfaceID surface,
4755                           VAImage *out_image)        /* out */
4756 {
4757     struct i965_driver_data *i965 = i965_driver_data(ctx);
4758     struct object_image *obj_image;
4759     struct object_surface *obj_surface;
4760     VAImageID image_id;
4761     unsigned int w_pitch;
4762     VAStatus va_status = VA_STATUS_ERROR_OPERATION_FAILED;
4763
4764     out_image->image_id = VA_INVALID_ID;
4765     obj_surface = SURFACE(surface);
4766
4767     if (!obj_surface)
4768         return VA_STATUS_ERROR_INVALID_SURFACE;
4769
4770     if (!obj_surface->bo) {
4771         unsigned int is_tiled = 0;
4772         unsigned int fourcc = VA_FOURCC_YV12;
4773         i965_guess_surface_format(ctx, surface, &fourcc, &is_tiled);
4774         int sampling = get_sampling_from_fourcc(fourcc);
4775         va_status = i965_check_alloc_surface_bo(ctx, obj_surface, is_tiled, fourcc, sampling);
4776         if (va_status != VA_STATUS_SUCCESS)
4777             return va_status;
4778     }
4779
4780     ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
4781
4782     w_pitch = obj_surface->width;
4783
4784     image_id = NEW_IMAGE_ID();
4785
4786     if (image_id == VA_INVALID_ID)
4787         return VA_STATUS_ERROR_ALLOCATION_FAILED;
4788
4789     obj_image = IMAGE(image_id);
4790
4791     if (!obj_image)
4792         return VA_STATUS_ERROR_ALLOCATION_FAILED;
4793
4794     obj_image->bo = NULL;
4795     obj_image->palette = NULL;
4796     obj_image->derived_surface = VA_INVALID_ID;
4797
4798     VAImage * const image = &obj_image->image;
4799
4800     memset(image, 0, sizeof(*image));
4801     image->image_id = image_id;
4802     image->buf = VA_INVALID_ID;
4803     image->num_palette_entries = 0;
4804     image->entry_bytes = 0;
4805     image->width = obj_surface->orig_width;
4806     image->height = obj_surface->orig_height;
4807     image->data_size = obj_surface->size;
4808
4809     image->format.fourcc = obj_surface->fourcc;
4810     image->format.byte_order = VA_LSB_FIRST;
4811     image->format.bits_per_pixel = get_bpp_from_fourcc(obj_surface->fourcc);
4812
4813     if (!image->format.bits_per_pixel)
4814         goto error;
4815
4816     switch (image->format.fourcc) {
4817     case VA_FOURCC_YV12:
4818         image->num_planes = 3;
4819         image->pitches[0] = w_pitch; /* Y */
4820         image->offsets[0] = 0;
4821         image->pitches[1] = obj_surface->cb_cr_pitch; /* V */
4822         image->offsets[1] = w_pitch * obj_surface->y_cr_offset;
4823         image->pitches[2] = obj_surface->cb_cr_pitch; /* U */
4824         image->offsets[2] = w_pitch * obj_surface->y_cb_offset;
4825         break;
4826
4827     case VA_FOURCC_YV16:
4828         image->num_planes = 3;
4829         image->pitches[0] = w_pitch; /* Y */
4830         image->offsets[0] = 0;
4831         image->pitches[1] = obj_surface->cb_cr_pitch; /* V */
4832         image->offsets[1] = w_pitch * obj_surface->y_cr_offset;
4833         image->pitches[2] = obj_surface->cb_cr_pitch; /* U */
4834         image->offsets[2] = w_pitch * obj_surface->y_cb_offset;
4835         break;
4836
4837     case VA_FOURCC_NV12:
4838     case VA_FOURCC_P010:
4839         image->num_planes = 2;
4840         image->pitches[0] = w_pitch; /* Y */
4841         image->offsets[0] = 0;
4842         image->pitches[1] = obj_surface->cb_cr_pitch; /* UV */
4843         image->offsets[1] = w_pitch * obj_surface->y_cb_offset;
4844         break;
4845
4846     case VA_FOURCC_I420:
4847     case VA_FOURCC_I010:
4848     case VA_FOURCC_422H:
4849     case VA_FOURCC_IMC3:
4850     case VA_FOURCC_444P:
4851     case VA_FOURCC_422V:
4852     case VA_FOURCC_411P:
4853         image->num_planes = 3;
4854         image->pitches[0] = w_pitch; /* Y */
4855         image->offsets[0] = 0;
4856         image->pitches[1] = obj_surface->cb_cr_pitch; /* U */
4857         image->offsets[1] = w_pitch * obj_surface->y_cb_offset;
4858         image->pitches[2] = obj_surface->cb_cr_pitch; /* V */
4859         image->offsets[2] = w_pitch * obj_surface->y_cr_offset;
4860         break;
4861
4862     case VA_FOURCC_YUY2:
4863     case VA_FOURCC_UYVY:
4864     case VA_FOURCC_Y800:
4865         image->num_planes = 1;
4866         image->pitches[0] = obj_surface->width; /* Y, width is aligned already */
4867         image->offsets[0] = 0;
4868         break;
4869     case VA_FOURCC_RGBA:
4870     case VA_FOURCC_RGBX:
4871     case VA_FOURCC_BGRA:
4872     case VA_FOURCC_BGRX:
4873         image->num_planes = 1;
4874         image->pitches[0] = obj_surface->width;
4875
4876         switch (image->format.fourcc) {
4877         case VA_FOURCC_RGBA:
4878         case VA_FOURCC_RGBX:
4879             image->format.red_mask = 0x000000ff;
4880             image->format.green_mask = 0x0000ff00;
4881             image->format.blue_mask = 0x00ff0000;
4882             break;
4883         case VA_FOURCC_BGRA:
4884         case VA_FOURCC_BGRX:
4885             image->format.red_mask = 0x00ff0000;
4886             image->format.green_mask = 0x0000ff00;
4887             image->format.blue_mask = 0x000000ff;
4888             break;
4889         default:
4890             goto error;
4891         }
4892
4893         switch (image->format.fourcc) {
4894         case VA_FOURCC_RGBA:
4895         case VA_FOURCC_BGRA:
4896             image->format.alpha_mask = 0xff000000;
4897             image->format.depth = 32;
4898             break;
4899         case VA_FOURCC_RGBX:
4900         case VA_FOURCC_BGRX:
4901             image->format.alpha_mask = 0x00000000;
4902             image->format.depth = 24;
4903             break;
4904         default:
4905             goto error;
4906         }
4907
4908         break;
4909     default:
4910         goto error;
4911     }
4912
4913     va_status = i965_create_buffer_internal(ctx, 0, VAImageBufferType,
4914                                             obj_surface->size, 1, NULL, obj_surface->bo, &image->buf);
4915     if (va_status != VA_STATUS_SUCCESS)
4916         goto error;
4917
4918     struct object_buffer *obj_buffer = BUFFER(image->buf);
4919
4920     if (!obj_buffer ||
4921         !obj_buffer->buffer_store ||
4922         !obj_buffer->buffer_store->bo)
4923         return VA_STATUS_ERROR_ALLOCATION_FAILED;
4924
4925     obj_image->bo = obj_buffer->buffer_store->bo;
4926     dri_bo_reference(obj_image->bo);
4927
4928     if (image->num_palette_entries > 0 && image->entry_bytes > 0) {
4929         obj_image->palette = malloc(image->num_palette_entries * sizeof(*obj_image->palette));
4930         if (!obj_image->palette) {
4931             va_status = VA_STATUS_ERROR_ALLOCATION_FAILED;
4932             goto error;
4933         }
4934     }
4935
4936     *out_image = *image;
4937     obj_surface->flags |= SURFACE_DERIVED;
4938     obj_surface->derived_image_id = image_id;
4939     obj_image->derived_surface = surface;
4940
4941     return VA_STATUS_SUCCESS;
4942
4943 error:
4944     i965_DestroyImage(ctx, image_id);
4945     return va_status;
4946 }
4947
4948 static void
4949 i965_destroy_image(struct object_heap *heap, struct object_base *obj)
4950 {
4951     object_heap_free(heap, obj);
4952 }
4953
4954
4955 VAStatus
4956 i965_DestroyImage(VADriverContextP ctx, VAImageID image)
4957 {
4958     struct i965_driver_data *i965 = i965_driver_data(ctx);
4959     struct object_image *obj_image = IMAGE(image);
4960     struct object_surface *obj_surface;
4961
4962     if (!obj_image)
4963         return VA_STATUS_SUCCESS;
4964
4965     dri_bo_unreference(obj_image->bo);
4966     obj_image->bo = NULL;
4967
4968     if (obj_image->image.buf != VA_INVALID_ID) {
4969         i965_DestroyBuffer(ctx, obj_image->image.buf);
4970         obj_image->image.buf = VA_INVALID_ID;
4971     }
4972
4973     if (obj_image->palette) {
4974         free(obj_image->palette);
4975         obj_image->palette = NULL;
4976     }
4977
4978     obj_surface = SURFACE(obj_image->derived_surface);
4979
4980     if (obj_surface) {
4981         obj_surface->flags &= ~SURFACE_DERIVED;
4982         obj_surface->derived_image_id = VA_INVALID_ID;
4983     }
4984
4985     i965_destroy_image(&i965->image_heap, (struct object_base *)obj_image);
4986
4987     return VA_STATUS_SUCCESS;
4988 }
4989
4990 /*
4991  * pointer to an array holding the palette data.  The size of the array is
4992  * num_palette_entries * entry_bytes in size.  The order of the components
4993  * in the palette is described by the component_order in VASubpicture struct
4994  */
4995 VAStatus
4996 i965_SetImagePalette(VADriverContextP ctx,
4997                      VAImageID image,
4998                      unsigned char *palette)
4999 {
5000     struct i965_driver_data *i965 = i965_driver_data(ctx);
5001     unsigned int i;
5002
5003     struct object_image *obj_image = IMAGE(image);
5004     if (!obj_image)
5005         return VA_STATUS_ERROR_INVALID_IMAGE;
5006
5007     if (!obj_image->palette)
5008         return VA_STATUS_ERROR_ALLOCATION_FAILED; /* XXX: unpaletted/error */
5009
5010     for (i = 0; i < obj_image->image.num_palette_entries; i++)
5011         obj_image->palette[i] = (((unsigned int)palette[3 * i + 0] << 16) |
5012                                  ((unsigned int)palette[3 * i + 1] <<  8) |
5013                                  (unsigned int)palette[3 * i + 2]);
5014     return VA_STATUS_SUCCESS;
5015 }
5016
5017 static int
5018 get_sampling_from_fourcc(unsigned int fourcc)
5019 {
5020     const i965_fourcc_info *info = get_fourcc_info(fourcc);
5021
5022     if (info && (info->flag & I_S))
5023         return info->subsampling;
5024     else
5025         return -1;
5026 }
5027
5028 static inline void
5029 memcpy_pic(uint8_t *dst, unsigned int dst_stride,
5030            const uint8_t *src, unsigned int src_stride,
5031            unsigned int len, unsigned int height)
5032 {
5033     unsigned int i;
5034
5035     for (i = 0; i < height; i++) {
5036         memcpy(dst, src, len);
5037         dst += dst_stride;
5038         src += src_stride;
5039     }
5040 }
5041
5042 static VAStatus
5043 get_image_i420(struct object_image *obj_image, uint8_t *image_data,
5044                struct object_surface *obj_surface,
5045                const VARectangle *rect)
5046 {
5047     uint8_t *dst[3], *src[3];
5048     const int Y = 0;
5049     const int U = obj_image->image.format.fourcc == obj_surface->fourcc ? 1 : 2;
5050     const int V = obj_image->image.format.fourcc == obj_surface->fourcc ? 2 : 1;
5051     unsigned int tiling, swizzle;
5052     VAStatus va_status = VA_STATUS_SUCCESS;
5053
5054     if (!obj_surface->bo)
5055         return VA_STATUS_ERROR_INVALID_SURFACE;
5056
5057     ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
5058     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
5059
5060     if (tiling != I915_TILING_NONE)
5061         drm_intel_gem_bo_map_gtt(obj_surface->bo);
5062     else
5063         dri_bo_map(obj_surface->bo, 0);
5064
5065     if (!obj_surface->bo->virtual)
5066         return VA_STATUS_ERROR_INVALID_SURFACE;
5067
5068     /* Dest VA image has either I420 or YV12 format.
5069        Source VA surface alway has I420 format */
5070     dst[Y] = image_data + obj_image->image.offsets[Y];
5071     src[0] = (uint8_t *)obj_surface->bo->virtual;
5072     dst[U] = image_data + obj_image->image.offsets[U];
5073     src[1] = src[0] + obj_surface->width * obj_surface->height;
5074     dst[V] = image_data + obj_image->image.offsets[V];
5075     src[2] = src[1] + (obj_surface->width / 2) * (obj_surface->height / 2);
5076
5077     /* Y plane */
5078     dst[Y] += rect->y * obj_image->image.pitches[Y] + rect->x;
5079     src[0] += rect->y * obj_surface->width + rect->x;
5080     memcpy_pic(dst[Y], obj_image->image.pitches[Y],
5081                src[0], obj_surface->width,
5082                rect->width, rect->height);
5083
5084     /* U plane */
5085     dst[U] += (rect->y / 2) * obj_image->image.pitches[U] + rect->x / 2;
5086     src[1] += (rect->y / 2) * obj_surface->width / 2 + rect->x / 2;
5087     memcpy_pic(dst[U], obj_image->image.pitches[U],
5088                src[1], obj_surface->width / 2,
5089                rect->width / 2, rect->height / 2);
5090
5091     /* V plane */
5092     dst[V] += (rect->y / 2) * obj_image->image.pitches[V] + rect->x / 2;
5093     src[2] += (rect->y / 2) * obj_surface->width / 2 + rect->x / 2;
5094     memcpy_pic(dst[V], obj_image->image.pitches[V],
5095                src[2], obj_surface->width / 2,
5096                rect->width / 2, rect->height / 2);
5097
5098     if (tiling != I915_TILING_NONE)
5099         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
5100     else
5101         dri_bo_unmap(obj_surface->bo);
5102
5103     return va_status;
5104 }
5105
5106 static VAStatus
5107 get_image_nv12(struct object_image *obj_image, uint8_t *image_data,
5108                struct object_surface *obj_surface,
5109                const VARectangle *rect)
5110 {
5111     uint8_t *dst[2], *src[2];
5112     unsigned int tiling, swizzle;
5113     VAStatus va_status = VA_STATUS_SUCCESS;
5114
5115     if (!obj_surface->bo)
5116         return VA_STATUS_ERROR_INVALID_SURFACE;
5117
5118     assert(obj_surface->fourcc);
5119     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
5120
5121     if (tiling != I915_TILING_NONE)
5122         drm_intel_gem_bo_map_gtt(obj_surface->bo);
5123     else
5124         dri_bo_map(obj_surface->bo, 0);
5125
5126     if (!obj_surface->bo->virtual)
5127         return VA_STATUS_ERROR_INVALID_SURFACE;
5128
5129     /* Both dest VA image and source surface have NV12 format */
5130     dst[0] = image_data + obj_image->image.offsets[0];
5131     src[0] = (uint8_t *)obj_surface->bo->virtual;
5132     dst[1] = image_data + obj_image->image.offsets[1];
5133     src[1] = src[0] + obj_surface->width * obj_surface->height;
5134
5135     /* Y plane */
5136     dst[0] += rect->y * obj_image->image.pitches[0] + rect->x;
5137     src[0] += rect->y * obj_surface->width + rect->x;
5138     memcpy_pic(dst[0], obj_image->image.pitches[0],
5139                src[0], obj_surface->width,
5140                rect->width, rect->height);
5141
5142     /* UV plane */
5143     dst[1] += (rect->y / 2) * obj_image->image.pitches[1] + (rect->x & -2);
5144     src[1] += (rect->y / 2) * obj_surface->width + (rect->x & -2);
5145     memcpy_pic(dst[1], obj_image->image.pitches[1],
5146                src[1], obj_surface->width,
5147                rect->width, rect->height / 2);
5148
5149     if (tiling != I915_TILING_NONE)
5150         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
5151     else
5152         dri_bo_unmap(obj_surface->bo);
5153
5154     return va_status;
5155 }
5156
5157 static VAStatus
5158 get_image_yuy2(struct object_image *obj_image, uint8_t *image_data,
5159                struct object_surface *obj_surface,
5160                const VARectangle *rect)
5161 {
5162     uint8_t *dst, *src;
5163     unsigned int tiling, swizzle;
5164     VAStatus va_status = VA_STATUS_SUCCESS;
5165
5166     if (!obj_surface->bo)
5167         return VA_STATUS_ERROR_INVALID_SURFACE;
5168
5169     assert(obj_surface->fourcc);
5170     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
5171
5172     if (tiling != I915_TILING_NONE)
5173         drm_intel_gem_bo_map_gtt(obj_surface->bo);
5174     else
5175         dri_bo_map(obj_surface->bo, 0);
5176
5177     if (!obj_surface->bo->virtual)
5178         return VA_STATUS_ERROR_INVALID_SURFACE;
5179
5180     /* Both dest VA image and source surface have YUYV format */
5181     dst = image_data + obj_image->image.offsets[0];
5182     src = (uint8_t *)obj_surface->bo->virtual;
5183
5184     /* Y plane */
5185     dst += rect->y * obj_image->image.pitches[0] + rect->x * 2;
5186     src += rect->y * obj_surface->width + rect->x * 2;
5187     memcpy_pic(dst, obj_image->image.pitches[0],
5188                src, obj_surface->width * 2,
5189                rect->width * 2, rect->height);
5190
5191     if (tiling != I915_TILING_NONE)
5192         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
5193     else
5194         dri_bo_unmap(obj_surface->bo);
5195
5196     return va_status;
5197 }
5198
5199 static VAStatus
5200 i965_sw_getimage(VADriverContextP ctx,
5201                  struct object_surface *obj_surface, struct object_image *obj_image,
5202                  const VARectangle *rect)
5203 {
5204     void *image_data = NULL;
5205     VAStatus va_status;
5206
5207     if (obj_surface->fourcc != obj_image->image.format.fourcc)
5208         return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
5209
5210     va_status = i965_MapBuffer(ctx, obj_image->image.buf, &image_data);
5211     if (va_status != VA_STATUS_SUCCESS)
5212         return va_status;
5213
5214     switch (obj_image->image.format.fourcc) {
5215     case VA_FOURCC_YV12:
5216     case VA_FOURCC_I420:
5217         get_image_i420(obj_image, image_data, obj_surface, rect);
5218         break;
5219     case VA_FOURCC_NV12:
5220         get_image_nv12(obj_image, image_data, obj_surface, rect);
5221         break;
5222     case VA_FOURCC_YUY2:
5223         /* YUY2 is the format supported by overlay plane */
5224         get_image_yuy2(obj_image, image_data, obj_surface, rect);
5225         break;
5226     default:
5227         va_status = VA_STATUS_ERROR_OPERATION_FAILED;
5228         break;
5229     }
5230     if (va_status != VA_STATUS_SUCCESS)
5231         return va_status;
5232
5233     va_status = i965_UnmapBuffer(ctx, obj_image->image.buf);
5234     return va_status;
5235 }
5236
5237 static VAStatus
5238 i965_hw_getimage(VADriverContextP ctx,
5239                  struct object_surface *obj_surface, struct object_image *obj_image,
5240                  const VARectangle *rect)
5241 {
5242     struct i965_surface src_surface;
5243     struct i965_surface dst_surface;
5244
5245     src_surface.base = (struct object_base *)obj_surface;
5246     src_surface.type = I965_SURFACE_TYPE_SURFACE;
5247     src_surface.flags = I965_SURFACE_FLAG_FRAME;
5248
5249     dst_surface.base = (struct object_base *)obj_image;
5250     dst_surface.type = I965_SURFACE_TYPE_IMAGE;
5251     dst_surface.flags = I965_SURFACE_FLAG_FRAME;
5252
5253     return i965_image_processing(ctx, &src_surface, rect, &dst_surface, rect);
5254 }
5255
5256 VAStatus
5257 i965_GetImage(VADriverContextP ctx,
5258               VASurfaceID surface,
5259               int x,   /* coordinates of the upper left source pixel */
5260               int y,
5261               unsigned int width,      /* width and height of the region */
5262               unsigned int height,
5263               VAImageID image)
5264 {
5265     struct i965_driver_data * const i965 = i965_driver_data(ctx);
5266     struct object_surface * const obj_surface = SURFACE(surface);
5267     struct object_image * const obj_image = IMAGE(image);
5268     VARectangle rect;
5269     VAStatus va_status;
5270
5271     if (!obj_surface)
5272         return VA_STATUS_ERROR_INVALID_SURFACE;
5273     if (!obj_surface->bo) /* don't get anything, keep previous data */
5274         return VA_STATUS_SUCCESS;
5275     if (is_surface_busy(i965, obj_surface))
5276         return VA_STATUS_ERROR_SURFACE_BUSY;
5277
5278     if (!obj_image || !obj_image->bo)
5279         return VA_STATUS_ERROR_INVALID_IMAGE;
5280     if (is_image_busy(i965, obj_image, surface))
5281         return VA_STATUS_ERROR_SURFACE_BUSY;
5282
5283     if (x < 0 || y < 0)
5284         return VA_STATUS_ERROR_INVALID_PARAMETER;
5285     if (x + width > obj_surface->orig_width ||
5286         y + height > obj_surface->orig_height)
5287         return VA_STATUS_ERROR_INVALID_PARAMETER;
5288     if (x + width > obj_image->image.width ||
5289         y + height > obj_image->image.height)
5290         return VA_STATUS_ERROR_INVALID_PARAMETER;
5291
5292     rect.x = x;
5293     rect.y = y;
5294     rect.width = width;
5295     rect.height = height;
5296
5297     if (HAS_ACCELERATED_GETIMAGE(i965))
5298         va_status = i965_hw_getimage(ctx, obj_surface, obj_image, &rect);
5299     else
5300         va_status = i965_sw_getimage(ctx, obj_surface, obj_image, &rect);
5301
5302     return va_status;
5303 }
5304
5305 static VAStatus
5306 put_image_i420(struct object_surface *obj_surface,
5307                const VARectangle *dst_rect,
5308                struct object_image *obj_image, uint8_t *image_data,
5309                const VARectangle *src_rect)
5310 {
5311     uint8_t *dst[3], *src[3];
5312     const int Y = 0;
5313     const int U = obj_image->image.format.fourcc == obj_surface->fourcc ? 1 : 2;
5314     const int V = obj_image->image.format.fourcc == obj_surface->fourcc ? 2 : 1;
5315     unsigned int tiling, swizzle;
5316     VAStatus va_status = VA_STATUS_SUCCESS;
5317
5318     ASSERT_RET(obj_surface->bo, VA_STATUS_ERROR_INVALID_SURFACE);
5319
5320     ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
5321     ASSERT_RET(dst_rect->width == src_rect->width, VA_STATUS_ERROR_UNIMPLEMENTED);
5322     ASSERT_RET(dst_rect->height == src_rect->height, VA_STATUS_ERROR_UNIMPLEMENTED);
5323     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
5324
5325     if (tiling != I915_TILING_NONE)
5326         drm_intel_gem_bo_map_gtt(obj_surface->bo);
5327     else
5328         dri_bo_map(obj_surface->bo, 0);
5329
5330     if (!obj_surface->bo->virtual)
5331         return VA_STATUS_ERROR_INVALID_SURFACE;
5332
5333     /* Dest VA image has either I420 or YV12 format.
5334        Source VA surface alway has I420 format */
5335     dst[0] = (uint8_t *)obj_surface->bo->virtual;
5336     src[Y] = image_data + obj_image->image.offsets[Y];
5337     dst[1] = dst[0] + obj_surface->width * obj_surface->height;
5338     src[U] = image_data + obj_image->image.offsets[U];
5339     dst[2] = dst[1] + (obj_surface->width / 2) * (obj_surface->height / 2);
5340     src[V] = image_data + obj_image->image.offsets[V];
5341
5342     /* Y plane */
5343     dst[0] += dst_rect->y * obj_surface->width + dst_rect->x;
5344     src[Y] += src_rect->y * obj_image->image.pitches[Y] + src_rect->x;
5345     memcpy_pic(dst[0], obj_surface->width,
5346                src[Y], obj_image->image.pitches[Y],
5347                src_rect->width, src_rect->height);
5348
5349     /* U plane */
5350     dst[1] += (dst_rect->y / 2) * obj_surface->width / 2 + dst_rect->x / 2;
5351     src[U] += (src_rect->y / 2) * obj_image->image.pitches[U] + src_rect->x / 2;
5352     memcpy_pic(dst[1], obj_surface->width / 2,
5353                src[U], obj_image->image.pitches[U],
5354                src_rect->width / 2, src_rect->height / 2);
5355
5356     /* V plane */
5357     dst[2] += (dst_rect->y / 2) * obj_surface->width / 2 + dst_rect->x / 2;
5358     src[V] += (src_rect->y / 2) * obj_image->image.pitches[V] + src_rect->x / 2;
5359     memcpy_pic(dst[2], obj_surface->width / 2,
5360                src[V], obj_image->image.pitches[V],
5361                src_rect->width / 2, src_rect->height / 2);
5362
5363     if (tiling != I915_TILING_NONE)
5364         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
5365     else
5366         dri_bo_unmap(obj_surface->bo);
5367
5368     return va_status;
5369 }
5370
5371 static VAStatus
5372 put_image_nv12(struct object_surface *obj_surface,
5373                const VARectangle *dst_rect,
5374                struct object_image *obj_image, uint8_t *image_data,
5375                const VARectangle *src_rect)
5376 {
5377     uint8_t *dst[2], *src[2];
5378     unsigned int tiling, swizzle;
5379     VAStatus va_status = VA_STATUS_SUCCESS;
5380
5381     if (!obj_surface->bo)
5382         return VA_STATUS_ERROR_INVALID_SURFACE;
5383
5384     ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
5385     ASSERT_RET(dst_rect->width == src_rect->width, VA_STATUS_ERROR_UNIMPLEMENTED);
5386     ASSERT_RET(dst_rect->height == src_rect->height, VA_STATUS_ERROR_UNIMPLEMENTED);
5387     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
5388
5389     if (tiling != I915_TILING_NONE)
5390         drm_intel_gem_bo_map_gtt(obj_surface->bo);
5391     else
5392         dri_bo_map(obj_surface->bo, 0);
5393
5394     if (!obj_surface->bo->virtual)
5395         return VA_STATUS_ERROR_INVALID_SURFACE;
5396
5397     /* Both dest VA image and source surface have NV12 format */
5398     dst[0] = (uint8_t *)obj_surface->bo->virtual;
5399     src[0] = image_data + obj_image->image.offsets[0];
5400     dst[1] = dst[0] + obj_surface->width * obj_surface->height;
5401     src[1] = image_data + obj_image->image.offsets[1];
5402
5403     /* Y plane */
5404     dst[0] += dst_rect->y * obj_surface->width + dst_rect->x;
5405     src[0] += src_rect->y * obj_image->image.pitches[0] + src_rect->x;
5406     memcpy_pic(dst[0], obj_surface->width,
5407                src[0], obj_image->image.pitches[0],
5408                src_rect->width, src_rect->height);
5409
5410     /* UV plane */
5411     dst[1] += (dst_rect->y / 2) * obj_surface->width + (dst_rect->x & -2);
5412     src[1] += (src_rect->y / 2) * obj_image->image.pitches[1] + (src_rect->x & -2);
5413     memcpy_pic(dst[1], obj_surface->width,
5414                src[1], obj_image->image.pitches[1],
5415                src_rect->width, src_rect->height / 2);
5416
5417     if (tiling != I915_TILING_NONE)
5418         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
5419     else
5420         dri_bo_unmap(obj_surface->bo);
5421
5422     return va_status;
5423 }
5424
5425 static VAStatus
5426 put_image_yuy2(struct object_surface *obj_surface,
5427                const VARectangle *dst_rect,
5428                struct object_image *obj_image, uint8_t *image_data,
5429                const VARectangle *src_rect)
5430 {
5431     uint8_t *dst, *src;
5432     unsigned int tiling, swizzle;
5433     VAStatus va_status = VA_STATUS_SUCCESS;
5434
5435     ASSERT_RET(obj_surface->bo, VA_STATUS_ERROR_INVALID_SURFACE);
5436     ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
5437     ASSERT_RET(dst_rect->width == src_rect->width, VA_STATUS_ERROR_UNIMPLEMENTED);
5438     ASSERT_RET(dst_rect->height == src_rect->height, VA_STATUS_ERROR_UNIMPLEMENTED);
5439     dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
5440
5441     if (tiling != I915_TILING_NONE)
5442         drm_intel_gem_bo_map_gtt(obj_surface->bo);
5443     else
5444         dri_bo_map(obj_surface->bo, 0);
5445
5446     if (!obj_surface->bo->virtual)
5447         return VA_STATUS_ERROR_INVALID_SURFACE;
5448
5449     /* Both dest VA image and source surface have YUY2 format */
5450     dst = (uint8_t *)obj_surface->bo->virtual;
5451     src = image_data + obj_image->image.offsets[0];
5452
5453     /* YUYV packed plane */
5454     dst += dst_rect->y * obj_surface->width + dst_rect->x * 2;
5455     src += src_rect->y * obj_image->image.pitches[0] + src_rect->x * 2;
5456     memcpy_pic(dst, obj_surface->width * 2,
5457                src, obj_image->image.pitches[0],
5458                src_rect->width * 2, src_rect->height);
5459
5460     if (tiling != I915_TILING_NONE)
5461         drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
5462     else
5463         dri_bo_unmap(obj_surface->bo);
5464
5465     return va_status;
5466 }
5467
5468 static VAStatus
5469 i965_sw_putimage(VADriverContextP ctx,
5470                  struct object_surface *obj_surface, struct object_image *obj_image,
5471                  const VARectangle *src_rect, const VARectangle *dst_rect)
5472 {
5473     VAStatus va_status = VA_STATUS_SUCCESS;
5474     void *image_data = NULL;
5475
5476     /* XXX: don't allow scaling */
5477     if (src_rect->width != dst_rect->width ||
5478         src_rect->height != dst_rect->height)
5479         return VA_STATUS_ERROR_INVALID_PARAMETER;
5480
5481     if (obj_surface->fourcc) {
5482         /* Don't allow format mismatch */
5483         if (obj_surface->fourcc != obj_image->image.format.fourcc)
5484             return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
5485     }
5486
5487     else {
5488         /* VA is surface not used for decoding, use same VA image format */
5489         va_status = i965_check_alloc_surface_bo(
5490                         ctx,
5491                         obj_surface,
5492                         0, /* XXX: don't use tiled surface */
5493                         obj_image->image.format.fourcc,
5494                         get_sampling_from_fourcc(obj_image->image.format.fourcc));
5495     }
5496
5497     if (va_status != VA_STATUS_SUCCESS)
5498         return va_status;
5499
5500     va_status = i965_MapBuffer(ctx, obj_image->image.buf, &image_data);
5501     if (va_status != VA_STATUS_SUCCESS)
5502         return va_status;
5503
5504     switch (obj_image->image.format.fourcc) {
5505     case VA_FOURCC_YV12:
5506     case VA_FOURCC_I420:
5507         va_status = put_image_i420(obj_surface, dst_rect, obj_image, image_data, src_rect);
5508         break;
5509     case VA_FOURCC_NV12:
5510         va_status = put_image_nv12(obj_surface, dst_rect, obj_image, image_data, src_rect);
5511         break;
5512     case VA_FOURCC_YUY2:
5513         va_status = put_image_yuy2(obj_surface, dst_rect, obj_image, image_data, src_rect);
5514         break;
5515     default:
5516         va_status = VA_STATUS_ERROR_OPERATION_FAILED;
5517         break;
5518     }
5519     if (va_status != VA_STATUS_SUCCESS)
5520         return va_status;
5521
5522     va_status = i965_UnmapBuffer(ctx, obj_image->image.buf);
5523     return va_status;
5524 }
5525
5526 static VAStatus
5527 i965_hw_putimage(VADriverContextP ctx,
5528                  struct object_surface *obj_surface, struct object_image *obj_image,
5529                  const VARectangle *src_rect, const VARectangle *dst_rect)
5530 {
5531     struct i965_surface src_surface, dst_surface;
5532     VAStatus va_status = VA_STATUS_SUCCESS;
5533
5534     if (!obj_surface->bo) {
5535         unsigned int tiling, swizzle;
5536         int surface_sampling = get_sampling_from_fourcc(obj_image->image.format.fourcc);;
5537         dri_bo_get_tiling(obj_image->bo, &tiling, &swizzle);
5538
5539         i965_check_alloc_surface_bo(ctx,
5540                                     obj_surface,
5541                                     !!tiling,
5542                                     obj_image->image.format.fourcc,
5543                                     surface_sampling);
5544     }
5545
5546     ASSERT_RET(obj_surface->fourcc, VA_STATUS_ERROR_INVALID_SURFACE);
5547
5548     src_surface.base = (struct object_base *)obj_image;
5549     src_surface.type = I965_SURFACE_TYPE_IMAGE;
5550     src_surface.flags = I965_SURFACE_FLAG_FRAME;
5551
5552     dst_surface.base = (struct object_base *)obj_surface;
5553     dst_surface.type = I965_SURFACE_TYPE_SURFACE;
5554     dst_surface.flags = I965_SURFACE_FLAG_FRAME;
5555
5556     va_status = i965_image_processing(ctx,
5557                                       &src_surface,
5558                                       src_rect,
5559                                       &dst_surface,
5560                                       dst_rect);
5561
5562     return  va_status;
5563 }
5564
5565 static VAStatus
5566 i965_PutImage(VADriverContextP ctx,
5567               VASurfaceID surface,
5568               VAImageID image,
5569               int src_x,
5570               int src_y,
5571               unsigned int src_width,
5572               unsigned int src_height,
5573               int dest_x,
5574               int dest_y,
5575               unsigned int dest_width,
5576               unsigned int dest_height)
5577 {
5578     struct i965_driver_data * const i965 = i965_driver_data(ctx);
5579     struct object_surface * const obj_surface = SURFACE(surface);
5580     struct object_image * const obj_image = IMAGE(image);
5581     VARectangle src_rect, dst_rect;
5582     VAStatus va_status;
5583
5584     if (!obj_surface)
5585         return VA_STATUS_ERROR_INVALID_SURFACE;
5586     if (is_surface_busy(i965, obj_surface))
5587         return VA_STATUS_ERROR_SURFACE_BUSY;
5588
5589     if (!obj_image || !obj_image->bo)
5590         return VA_STATUS_ERROR_INVALID_IMAGE;
5591     if (is_image_busy(i965, obj_image, surface))
5592         return VA_STATUS_ERROR_SURFACE_BUSY;
5593
5594     if (src_x < 0 ||
5595         src_y < 0 ||
5596         src_x + src_width > obj_image->image.width ||
5597         src_y + src_height > obj_image->image.height)
5598         return VA_STATUS_ERROR_INVALID_PARAMETER;
5599
5600     src_rect.x       = src_x;
5601     src_rect.y       = src_y;
5602     src_rect.width   = src_width;
5603     src_rect.height  = src_height;
5604
5605     if (dest_x < 0 ||
5606         dest_y < 0 ||
5607         dest_x + dest_width > obj_surface->orig_width ||
5608         dest_y + dest_height > obj_surface->orig_height)
5609         return VA_STATUS_ERROR_INVALID_PARAMETER;
5610
5611     dst_rect.x      = dest_x;
5612     dst_rect.y      = dest_y;
5613     dst_rect.width  = dest_width;
5614     dst_rect.height = dest_height;
5615
5616     if (HAS_ACCELERATED_PUTIMAGE(i965))
5617         va_status = i965_hw_putimage(ctx, obj_surface, obj_image,
5618                                      &src_rect, &dst_rect);
5619     else
5620         va_status = i965_sw_putimage(ctx, obj_surface, obj_image,
5621                                      &src_rect, &dst_rect);
5622
5623     return va_status;
5624 }
5625
5626 VAStatus
5627 i965_PutSurface(VADriverContextP ctx,
5628                 VASurfaceID surface,
5629                 void *draw, /* X Drawable */
5630                 short srcx,
5631                 short srcy,
5632                 unsigned short srcw,
5633                 unsigned short srch,
5634                 short destx,
5635                 short desty,
5636                 unsigned short destw,
5637                 unsigned short desth,
5638                 VARectangle *cliprects, /* client supplied clip list */
5639                 unsigned int number_cliprects, /* number of clip rects in the clip list */
5640                 unsigned int flags) /* de-interlacing flags */
5641 {
5642 #ifdef HAVE_VA_X11
5643     if (IS_VA_X11(ctx)) {
5644         VARectangle src_rect, dst_rect;
5645
5646         src_rect.x      = srcx;
5647         src_rect.y      = srcy;
5648         src_rect.width  = srcw;
5649         src_rect.height = srch;
5650
5651         dst_rect.x      = destx;
5652         dst_rect.y      = desty;
5653         dst_rect.width  = destw;
5654         dst_rect.height = desth;
5655
5656         return i965_put_surface_dri(ctx, surface, draw, &src_rect, &dst_rect,
5657                                     cliprects, number_cliprects, flags);
5658     }
5659 #endif
5660     return VA_STATUS_ERROR_UNIMPLEMENTED;
5661 }
5662
5663 static VAStatus
5664 i965_BufferInfo(
5665     VADriverContextP ctx,       /* in */
5666     VABufferID buf_id,          /* in */
5667     VABufferType *type,         /* out */
5668     unsigned int *size,         /* out */
5669     unsigned int *num_elements  /* out */
5670 )
5671 {
5672     struct i965_driver_data *i965 = NULL;
5673     struct object_buffer *obj_buffer = NULL;
5674
5675     i965 = i965_driver_data(ctx);
5676     obj_buffer = BUFFER(buf_id);
5677
5678     ASSERT_RET(obj_buffer, VA_STATUS_ERROR_INVALID_BUFFER);
5679
5680     *type = obj_buffer->type;
5681     *size = obj_buffer->size_element;
5682     *num_elements = obj_buffer->num_elements;
5683
5684     return VA_STATUS_SUCCESS;
5685 }
5686
5687 static VAStatus
5688 i965_LockSurface(
5689     VADriverContextP ctx,           /* in */
5690     VASurfaceID surface,            /* in */
5691     unsigned int *fourcc,           /* out */
5692     unsigned int *luma_stride,      /* out */
5693     unsigned int *chroma_u_stride,  /* out */
5694     unsigned int *chroma_v_stride,  /* out */
5695     unsigned int *luma_offset,      /* out */
5696     unsigned int *chroma_u_offset,  /* out */
5697     unsigned int *chroma_v_offset,  /* out */
5698     unsigned int *buffer_name,      /* out */
5699     void **buffer                   /* out */
5700 )
5701 {
5702     VAStatus vaStatus = VA_STATUS_SUCCESS;
5703     struct i965_driver_data *i965 = i965_driver_data(ctx);
5704     struct object_surface *obj_surface = NULL;
5705     VAImage tmpImage;
5706
5707     ASSERT_RET(fourcc, VA_STATUS_ERROR_INVALID_PARAMETER);
5708     ASSERT_RET(luma_stride, VA_STATUS_ERROR_INVALID_PARAMETER);
5709     ASSERT_RET(chroma_u_stride, VA_STATUS_ERROR_INVALID_PARAMETER);
5710     ASSERT_RET(chroma_v_stride, VA_STATUS_ERROR_INVALID_PARAMETER);
5711     ASSERT_RET(luma_offset, VA_STATUS_ERROR_INVALID_PARAMETER);
5712     ASSERT_RET(chroma_u_offset, VA_STATUS_ERROR_INVALID_PARAMETER);
5713     ASSERT_RET(chroma_v_offset, VA_STATUS_ERROR_INVALID_PARAMETER);
5714     ASSERT_RET(buffer_name, VA_STATUS_ERROR_INVALID_PARAMETER);
5715     ASSERT_RET(buffer, VA_STATUS_ERROR_INVALID_PARAMETER);
5716
5717     tmpImage.image_id = VA_INVALID_ID;
5718
5719     obj_surface = SURFACE(surface);
5720     if (obj_surface == NULL) {
5721         // Surface is absent.
5722         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
5723         goto error;
5724     }
5725
5726     // Lock functionality is absent now.
5727     if (obj_surface->locked_image_id != VA_INVALID_ID) {
5728         // Surface is locked already.
5729         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
5730         goto error;
5731     }
5732
5733     vaStatus = i965_DeriveImage(
5734                    ctx,
5735                    surface,
5736                    &tmpImage);
5737     if (vaStatus != VA_STATUS_SUCCESS) {
5738         goto error;
5739     }
5740
5741     obj_surface->locked_image_id = tmpImage.image_id;
5742
5743     vaStatus = i965_MapBuffer(
5744                    ctx,
5745                    tmpImage.buf,
5746                    buffer);
5747     if (vaStatus != VA_STATUS_SUCCESS) {
5748         goto error;
5749     }
5750
5751     *fourcc = tmpImage.format.fourcc;
5752     *luma_offset = tmpImage.offsets[0];
5753     *luma_stride = tmpImage.pitches[0];
5754     *chroma_u_offset = tmpImage.offsets[1];
5755     *chroma_u_stride = tmpImage.pitches[1];
5756     *chroma_v_offset = tmpImage.offsets[2];
5757     *chroma_v_stride = tmpImage.pitches[2];
5758     *buffer_name = tmpImage.buf;
5759
5760 error:
5761     if (vaStatus != VA_STATUS_SUCCESS) {
5762         buffer = NULL;
5763     }
5764
5765     return vaStatus;
5766 }
5767
5768 static VAStatus
5769 i965_UnlockSurface(
5770     VADriverContextP ctx,   /* in */
5771     VASurfaceID surface     /* in */
5772 )
5773 {
5774     VAStatus vaStatus = VA_STATUS_SUCCESS;
5775     struct i965_driver_data *i965 = i965_driver_data(ctx);
5776     struct object_image *locked_img = NULL;
5777     struct object_surface *obj_surface = NULL;
5778
5779     obj_surface = SURFACE(surface);
5780
5781     if (obj_surface == NULL) {
5782         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;   // Surface is absent
5783         return vaStatus;
5784     }
5785     if (obj_surface->locked_image_id == VA_INVALID_ID) {
5786         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;   // Surface is not locked
5787         return vaStatus;
5788     }
5789
5790     locked_img = IMAGE(obj_surface->locked_image_id);
5791     if (locked_img == NULL || (locked_img->image.image_id == VA_INVALID_ID)) {
5792         // Work image was deallocated before i965_UnlockSurface()
5793         vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
5794         goto error;
5795     }
5796
5797     vaStatus = i965_UnmapBuffer(
5798                    ctx,
5799                    locked_img->image.buf);
5800     if (vaStatus != VA_STATUS_SUCCESS) {
5801         goto error;
5802     }
5803
5804     vaStatus = i965_DestroyImage(
5805                    ctx,
5806                    locked_img->image.image_id);
5807     if (vaStatus != VA_STATUS_SUCCESS) {
5808         goto error;
5809     }
5810
5811     locked_img->image.image_id = VA_INVALID_ID;
5812
5813 error:
5814     obj_surface->locked_image_id = VA_INVALID_ID;
5815
5816     return vaStatus;
5817 }
5818
5819 static VAStatus
5820 i965_GetSurfaceAttributes(
5821     VADriverContextP ctx,
5822     VAConfigID config,
5823     VASurfaceAttrib *attrib_list,
5824     unsigned int num_attribs
5825 )
5826 {
5827     VAStatus vaStatus = VA_STATUS_SUCCESS;
5828     struct i965_driver_data *i965 = i965_driver_data(ctx);
5829     struct object_config *obj_config;
5830     int i;
5831
5832     if (config == VA_INVALID_ID)
5833         return VA_STATUS_ERROR_INVALID_CONFIG;
5834
5835     obj_config = CONFIG(config);
5836
5837     if (obj_config == NULL)
5838         return VA_STATUS_ERROR_INVALID_CONFIG;
5839
5840     if (attrib_list == NULL || num_attribs == 0)
5841         return VA_STATUS_ERROR_INVALID_PARAMETER;
5842
5843     for (i = 0; i < num_attribs; i++) {
5844         switch (attrib_list[i].type) {
5845         case VASurfaceAttribPixelFormat:
5846             attrib_list[i].value.type = VAGenericValueTypeInteger;
5847             attrib_list[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
5848
5849             if (attrib_list[i].value.value.i == 0) {
5850                 if (IS_G4X(i965->intel.device_info)) {
5851                     if (obj_config->profile == VAProfileMPEG2Simple ||
5852                         obj_config->profile == VAProfileMPEG2Main) {
5853                         attrib_list[i].value.value.i = VA_FOURCC_I420;
5854                     } else {
5855                         assert(0);
5856                         attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5857                     }
5858                 } else if (IS_IRONLAKE(i965->intel.device_info)) {
5859                     if (obj_config->profile == VAProfileMPEG2Simple ||
5860                         obj_config->profile == VAProfileMPEG2Main) {
5861                         attrib_list[i].value.value.i = VA_FOURCC_I420;
5862                     } else if (obj_config->profile == VAProfileH264ConstrainedBaseline ||
5863                                obj_config->profile == VAProfileH264Main ||
5864                                obj_config->profile == VAProfileH264High) {
5865                         attrib_list[i].value.value.i = VA_FOURCC_NV12;
5866                     } else if (obj_config->profile == VAProfileNone) {
5867                         attrib_list[i].value.value.i = VA_FOURCC_NV12;
5868                     } else {
5869                         assert(0);
5870                         attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5871                     }
5872                 } else if (IS_GEN6(i965->intel.device_info)) {
5873                     attrib_list[i].value.value.i = VA_FOURCC_NV12;
5874                 } else if (IS_GEN7(i965->intel.device_info) ||
5875                            IS_GEN8(i965->intel.device_info) ||
5876                            IS_GEN9(i965->intel.device_info) ||
5877                            IS_GEN10(i965->intel.device_info)) {
5878                     if (obj_config->profile == VAProfileJPEGBaseline)
5879                         attrib_list[i].value.value.i = 0; /* internal format */
5880                     else
5881                         attrib_list[i].value.value.i = VA_FOURCC_NV12;
5882                 }
5883             } else {
5884                 if (IS_G4X(i965->intel.device_info)) {
5885                     if (obj_config->profile == VAProfileMPEG2Simple ||
5886                         obj_config->profile == VAProfileMPEG2Main) {
5887                         if (attrib_list[i].value.value.i != VA_FOURCC_I420) {
5888                             attrib_list[i].value.value.i = 0;
5889                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5890                         }
5891                     } else {
5892                         assert(0);
5893                         attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5894                     }
5895                 } else if (IS_IRONLAKE(i965->intel.device_info)) {
5896                     if (obj_config->profile == VAProfileMPEG2Simple ||
5897                         obj_config->profile == VAProfileMPEG2Main) {
5898                         if (attrib_list[i].value.value.i != VA_FOURCC_I420) {
5899                             attrib_list[i].value.value.i = 0;
5900                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5901                         }
5902                     } else if (obj_config->profile == VAProfileH264ConstrainedBaseline ||
5903                                obj_config->profile == VAProfileH264Main ||
5904                                obj_config->profile == VAProfileH264High) {
5905                         if (attrib_list[i].value.value.i != VA_FOURCC_NV12) {
5906                             attrib_list[i].value.value.i = 0;
5907                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5908                         }
5909                     } else if (obj_config->profile == VAProfileNone) {
5910                         switch (attrib_list[i].value.value.i) {
5911                         case VA_FOURCC_NV12:
5912                         case VA_FOURCC_I420:
5913                         case VA_FOURCC_YV12:
5914                         case VA_FOURCC_YUY2:
5915                         case VA_FOURCC_BGRA:
5916                         case VA_FOURCC_BGRX:
5917                         case VA_FOURCC_RGBX:
5918                         case VA_FOURCC_RGBA:
5919                             break;
5920                         default:
5921                             attrib_list[i].value.value.i = 0;
5922                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5923                             break;
5924                         }
5925                     } else {
5926                         assert(0);
5927                         attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5928                     }
5929                 } else if (IS_GEN6(i965->intel.device_info)) {
5930                     if (obj_config->entrypoint == VAEntrypointEncSlice ||
5931                         obj_config->entrypoint == VAEntrypointVideoProc) {
5932                         switch (attrib_list[i].value.value.i) {
5933                         case VA_FOURCC_NV12:
5934                         case VA_FOURCC_I420:
5935                         case VA_FOURCC_YV12:
5936                         case VA_FOURCC_YUY2:
5937                         case VA_FOURCC_BGRA:
5938                         case VA_FOURCC_BGRX:
5939                         case VA_FOURCC_RGBX:
5940                         case VA_FOURCC_RGBA:
5941                             break;
5942                         default:
5943                             attrib_list[i].value.value.i = 0;
5944                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5945                             break;
5946                         }
5947                     } else {
5948                         if (attrib_list[i].value.value.i != VA_FOURCC_NV12) {
5949                             attrib_list[i].value.value.i = 0;
5950                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5951                         }
5952                     }
5953                 } else if (IS_GEN7(i965->intel.device_info) ||
5954                            IS_GEN8(i965->intel.device_info) ||
5955                            IS_GEN9(i965->intel.device_info) ||
5956                            IS_GEN10(i965->intel.device_info)) {
5957                     if (obj_config->entrypoint == VAEntrypointEncSlice ||
5958                         obj_config->entrypoint == VAEntrypointVideoProc ||
5959                         obj_config->entrypoint == VAEntrypointEncSliceLP ||
5960                         obj_config->entrypoint == VAEntrypointFEI) {
5961                         switch (attrib_list[i].value.value.i) {
5962                         case VA_FOURCC_NV12:
5963                         case VA_FOURCC_I420:
5964                         case VA_FOURCC_YV12:
5965                             break;
5966                         default:
5967                             attrib_list[i].value.value.i = 0;
5968                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5969                             break;
5970                         }
5971                     } else if (obj_config->entrypoint == VAEntrypointStats) {
5972                         if (attrib_list[i].value.value.i != VA_FOURCC_NV12) {
5973                             attrib_list[i].value.value.i = 0;
5974                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5975                         }
5976                     } else {
5977                         if (obj_config->profile == VAProfileJPEGBaseline) {
5978                             attrib_list[i].value.value.i = 0;   /* JPEG decoding always uses an internal format */
5979                             attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5980                         } else {
5981                             if (attrib_list[i].value.value.i != VA_FOURCC_NV12) {
5982                                 attrib_list[i].value.value.i = 0;
5983                                 attrib_list[i].flags &= ~VA_SURFACE_ATTRIB_SETTABLE;
5984                             }
5985                         }
5986                     }
5987                 }
5988             }
5989
5990             break;
5991         case VASurfaceAttribMinWidth:
5992             /* FIXME: add support for it later */
5993             attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5994             break;
5995         case VASurfaceAttribMaxWidth:
5996             attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
5997             break;
5998         case VASurfaceAttribMinHeight:
5999             attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
6000             break;
6001         case VASurfaceAttribMaxHeight:
6002             attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
6003             break;
6004         default:
6005             attrib_list[i].flags = VA_SURFACE_ATTRIB_NOT_SUPPORTED;
6006             break;
6007         }
6008     }
6009
6010     return vaStatus;
6011 }
6012
6013 static VAStatus
6014 i965_QuerySurfaceAttributes(VADriverContextP ctx,
6015                             VAConfigID config,
6016                             VASurfaceAttrib *attrib_list,
6017                             unsigned int *num_attribs)
6018 {
6019     VAStatus vaStatus = VA_STATUS_SUCCESS;
6020     struct i965_driver_data *i965 = i965_driver_data(ctx);
6021     struct object_config *obj_config;
6022     int i = 0;
6023     VASurfaceAttrib *attribs = NULL;
6024     int max_width;
6025     int max_height;
6026
6027     if (config == VA_INVALID_ID)
6028         return VA_STATUS_ERROR_INVALID_CONFIG;
6029
6030     obj_config = CONFIG(config);
6031
6032     if (obj_config == NULL)
6033         return VA_STATUS_ERROR_INVALID_CONFIG;
6034
6035     if (!attrib_list && !num_attribs)
6036         return VA_STATUS_ERROR_INVALID_PARAMETER;
6037
6038     if (attrib_list == NULL) {
6039         *num_attribs = I965_MAX_SURFACE_ATTRIBUTES;
6040         return VA_STATUS_SUCCESS;
6041     }
6042
6043     attribs = malloc(I965_MAX_SURFACE_ATTRIBUTES * sizeof(*attribs));
6044
6045     if (attribs == NULL)
6046         return VA_STATUS_ERROR_ALLOCATION_FAILED;
6047
6048     if (IS_G4X(i965->intel.device_info)) {
6049         if (obj_config->profile == VAProfileMPEG2Simple ||
6050             obj_config->profile == VAProfileMPEG2Main) {
6051             attribs[i].type = VASurfaceAttribPixelFormat;
6052             attribs[i].value.type = VAGenericValueTypeInteger;
6053             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6054             attribs[i].value.value.i = VA_FOURCC_I420;
6055             i++;
6056         }
6057     } else if (IS_IRONLAKE(i965->intel.device_info)) {
6058         switch (obj_config->profile) {
6059         case VAProfileMPEG2Simple:
6060         case VAProfileMPEG2Main:
6061             attribs[i].type = VASurfaceAttribPixelFormat;
6062             attribs[i].value.type = VAGenericValueTypeInteger;
6063             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6064             attribs[i].value.value.i = VA_FOURCC_I420;
6065             i++;
6066
6067             break;
6068
6069         case VAProfileH264ConstrainedBaseline:
6070         case VAProfileH264Main:
6071         case VAProfileH264High:
6072             attribs[i].type = VASurfaceAttribPixelFormat;
6073             attribs[i].value.type = VAGenericValueTypeInteger;
6074             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6075             attribs[i].value.value.i = VA_FOURCC_NV12;
6076             i++;
6077
6078         case VAProfileNone:
6079             attribs[i].type = VASurfaceAttribPixelFormat;
6080             attribs[i].value.type = VAGenericValueTypeInteger;
6081             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6082             attribs[i].value.value.i = VA_FOURCC_NV12;
6083             i++;
6084
6085             attribs[i].type = VASurfaceAttribPixelFormat;
6086             attribs[i].value.type = VAGenericValueTypeInteger;
6087             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6088             attribs[i].value.value.i = VA_FOURCC_I420;
6089             i++;
6090
6091             break;
6092
6093         default:
6094             break;
6095         }
6096     } else if (IS_GEN6(i965->intel.device_info)) {
6097         if (obj_config->entrypoint == VAEntrypointVLD) { /* decode */
6098             attribs[i].type = VASurfaceAttribPixelFormat;
6099             attribs[i].value.type = VAGenericValueTypeInteger;
6100             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6101             attribs[i].value.value.i = VA_FOURCC_NV12;
6102             i++;
6103         } else if (obj_config->entrypoint == VAEntrypointEncSlice ||  /* encode */
6104                    obj_config->entrypoint == VAEntrypointVideoProc) { /* vpp */
6105             attribs[i].type = VASurfaceAttribPixelFormat;
6106             attribs[i].value.type = VAGenericValueTypeInteger;
6107             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6108             attribs[i].value.value.i = VA_FOURCC_NV12;
6109             i++;
6110
6111             attribs[i].type = VASurfaceAttribPixelFormat;
6112             attribs[i].value.type = VAGenericValueTypeInteger;
6113             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6114             attribs[i].value.value.i = VA_FOURCC_I420;
6115             i++;
6116
6117             attribs[i].type = VASurfaceAttribPixelFormat;
6118             attribs[i].value.type = VAGenericValueTypeInteger;
6119             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6120             attribs[i].value.value.i = VA_FOURCC_YV12;
6121             i++;
6122
6123             if (obj_config->entrypoint == VAEntrypointVideoProc) {
6124                 attribs[i].type = VASurfaceAttribPixelFormat;
6125                 attribs[i].value.type = VAGenericValueTypeInteger;
6126                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6127                 attribs[i].value.value.i = VA_FOURCC_YUY2;
6128                 i++;
6129
6130                 attribs[i].type = VASurfaceAttribPixelFormat;
6131                 attribs[i].value.type = VAGenericValueTypeInteger;
6132                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6133                 attribs[i].value.value.i = VA_FOURCC_RGBA;
6134                 i++;
6135
6136                 attribs[i].type = VASurfaceAttribPixelFormat;
6137                 attribs[i].value.type = VAGenericValueTypeInteger;
6138                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6139                 attribs[i].value.value.i = VA_FOURCC_RGBX;
6140                 i++;
6141             }
6142         }
6143     } else if (IS_GEN7(i965->intel.device_info)) {
6144         if (obj_config->entrypoint == VAEntrypointVLD) { /* decode */
6145             if (obj_config->profile == VAProfileJPEGBaseline) {
6146                 attribs[i].type = VASurfaceAttribPixelFormat;
6147                 attribs[i].value.type = VAGenericValueTypeInteger;
6148                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6149                 attribs[i].value.value.i = VA_FOURCC_IMC3;
6150                 i++;
6151
6152                 attribs[i].type = VASurfaceAttribPixelFormat;
6153                 attribs[i].value.type = VAGenericValueTypeInteger;
6154                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6155                 attribs[i].value.value.i = VA_FOURCC_IMC1;
6156                 i++;
6157
6158                 attribs[i].type = VASurfaceAttribPixelFormat;
6159                 attribs[i].value.type = VAGenericValueTypeInteger;
6160                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6161                 attribs[i].value.value.i = VA_FOURCC_Y800;
6162                 i++;
6163
6164                 attribs[i].type = VASurfaceAttribPixelFormat;
6165                 attribs[i].value.type = VAGenericValueTypeInteger;
6166                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6167                 attribs[i].value.value.i = VA_FOURCC_411P;
6168                 i++;
6169
6170                 attribs[i].type = VASurfaceAttribPixelFormat;
6171                 attribs[i].value.type = VAGenericValueTypeInteger;
6172                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6173                 attribs[i].value.value.i = VA_FOURCC_422H;
6174                 i++;
6175
6176                 attribs[i].type = VASurfaceAttribPixelFormat;
6177                 attribs[i].value.type = VAGenericValueTypeInteger;
6178                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6179                 attribs[i].value.value.i = VA_FOURCC_422V;
6180                 i++;
6181
6182                 attribs[i].type = VASurfaceAttribPixelFormat;
6183                 attribs[i].value.type = VAGenericValueTypeInteger;
6184                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6185                 attribs[i].value.value.i = VA_FOURCC_444P;
6186                 i++;
6187             } else if (obj_config->profile == VAProfileHEVCMain10) {
6188                 attribs[i].type = VASurfaceAttribPixelFormat;
6189                 attribs[i].value.type = VAGenericValueTypeInteger;
6190                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6191                 attribs[i].value.value.i = VA_FOURCC_P010;
6192                 i++;
6193             } else {
6194                 attribs[i].type = VASurfaceAttribPixelFormat;
6195                 attribs[i].value.type = VAGenericValueTypeInteger;
6196                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6197                 attribs[i].value.value.i = VA_FOURCC_NV12;
6198                 i++;
6199             }
6200         } else if (obj_config->entrypoint == VAEntrypointEncSlice ||  /* encode */
6201                    obj_config->entrypoint == VAEntrypointVideoProc) { /* vpp */
6202             attribs[i].type = VASurfaceAttribPixelFormat;
6203             attribs[i].value.type = VAGenericValueTypeInteger;
6204             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6205             attribs[i].value.value.i = VA_FOURCC_NV12;
6206             i++;
6207
6208             attribs[i].type = VASurfaceAttribPixelFormat;
6209             attribs[i].value.type = VAGenericValueTypeInteger;
6210             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6211             attribs[i].value.value.i = VA_FOURCC_I420;
6212             i++;
6213
6214             attribs[i].type = VASurfaceAttribPixelFormat;
6215             attribs[i].value.type = VAGenericValueTypeInteger;
6216             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6217             attribs[i].value.value.i = VA_FOURCC_YV12;
6218             i++;
6219
6220             attribs[i].type = VASurfaceAttribPixelFormat;
6221             attribs[i].value.type = VAGenericValueTypeInteger;
6222             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6223             attribs[i].value.value.i = VA_FOURCC_IMC3;
6224             i++;
6225
6226             if (obj_config->entrypoint == VAEntrypointVideoProc) {
6227                 attribs[i].type = VASurfaceAttribPixelFormat;
6228                 attribs[i].value.type = VAGenericValueTypeInteger;
6229                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6230                 attribs[i].value.value.i = VA_FOURCC_YUY2;
6231                 i++;
6232
6233                 attribs[i].type = VASurfaceAttribPixelFormat;
6234                 attribs[i].value.type = VAGenericValueTypeInteger;
6235                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6236                 attribs[i].value.value.i = VA_FOURCC_UYVY;
6237                 i++;
6238
6239                 attribs[i].type = VASurfaceAttribPixelFormat;
6240                 attribs[i].value.type = VAGenericValueTypeInteger;
6241                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6242                 attribs[i].value.value.i = VA_FOURCC_RGBA;
6243                 i++;
6244
6245                 attribs[i].type = VASurfaceAttribPixelFormat;
6246                 attribs[i].value.type = VAGenericValueTypeInteger;
6247                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6248                 attribs[i].value.value.i = VA_FOURCC_RGBX;
6249                 i++;
6250
6251                 attribs[i].type = VASurfaceAttribPixelFormat;
6252                 attribs[i].value.type = VAGenericValueTypeInteger;
6253                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6254                 attribs[i].value.value.i = VA_FOURCC_BGRA;
6255                 i++;
6256
6257                 attribs[i].type = VASurfaceAttribPixelFormat;
6258                 attribs[i].value.type = VAGenericValueTypeInteger;
6259                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6260                 attribs[i].value.value.i = VA_FOURCC_BGRX;
6261                 i++;
6262
6263                 attribs[i].type = VASurfaceAttribPixelFormat;
6264                 attribs[i].value.type = VAGenericValueTypeInteger;
6265                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6266                 attribs[i].value.value.i = VA_FOURCC_YV16;
6267                 i++;
6268             }
6269         }
6270     } else if (IS_GEN8(i965->intel.device_info) ||
6271                IS_GEN9(i965->intel.device_info) ||
6272                IS_GEN10(i965->intel.device_info)) {
6273         if (obj_config->entrypoint == VAEntrypointVLD) { /* decode */
6274             if (obj_config->profile == VAProfileJPEGBaseline) {
6275                 attribs[i].type = VASurfaceAttribPixelFormat;
6276                 attribs[i].value.type = VAGenericValueTypeInteger;
6277                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6278                 attribs[i].value.value.i = VA_FOURCC_IMC3;
6279                 i++;
6280
6281                 attribs[i].type = VASurfaceAttribPixelFormat;
6282                 attribs[i].value.type = VAGenericValueTypeInteger;
6283                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6284                 attribs[i].value.value.i = VA_FOURCC_IMC1;
6285                 i++;
6286
6287                 attribs[i].type = VASurfaceAttribPixelFormat;
6288                 attribs[i].value.type = VAGenericValueTypeInteger;
6289                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6290                 attribs[i].value.value.i = VA_FOURCC_Y800;
6291                 i++;
6292
6293                 attribs[i].type = VASurfaceAttribPixelFormat;
6294                 attribs[i].value.type = VAGenericValueTypeInteger;
6295                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6296                 attribs[i].value.value.i = VA_FOURCC_411P;
6297                 i++;
6298
6299                 attribs[i].type = VASurfaceAttribPixelFormat;
6300                 attribs[i].value.type = VAGenericValueTypeInteger;
6301                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6302                 attribs[i].value.value.i = VA_FOURCC_422H;
6303                 i++;
6304
6305                 attribs[i].type = VASurfaceAttribPixelFormat;
6306                 attribs[i].value.type = VAGenericValueTypeInteger;
6307                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6308                 attribs[i].value.value.i = VA_FOURCC_422V;
6309                 i++;
6310
6311                 attribs[i].type = VASurfaceAttribPixelFormat;
6312                 attribs[i].value.type = VAGenericValueTypeInteger;
6313                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6314                 attribs[i].value.value.i = VA_FOURCC_444P;
6315                 i++;
6316             } else {
6317                 attribs[i].type = VASurfaceAttribPixelFormat;
6318                 attribs[i].value.type = VAGenericValueTypeInteger;
6319                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6320                 attribs[i].value.value.i = VA_FOURCC_NV12;
6321                 i++;
6322
6323                 if ((obj_config->profile == VAProfileHEVCMain10) ||
6324                     (obj_config->profile == VAProfileVP9Profile2)) {
6325                     attribs[i].type = VASurfaceAttribPixelFormat;
6326                     attribs[i].value.type = VAGenericValueTypeInteger;
6327                     attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6328                     attribs[i].value.value.i = VA_FOURCC_P010;
6329                     i++;
6330                 }
6331             }
6332         } else if (obj_config->entrypoint == VAEntrypointEncSlice ||  /* encode */
6333                    obj_config->entrypoint == VAEntrypointVideoProc ||
6334                    obj_config->entrypoint == VAEntrypointEncSliceLP ||
6335                    obj_config->entrypoint == VAEntrypointEncPicture ||
6336                    obj_config->entrypoint == VAEntrypointFEI) {
6337
6338             if (obj_config->profile == VAProfileHEVCMain10) {
6339                 attribs[i].type = VASurfaceAttribPixelFormat;
6340                 attribs[i].value.type = VAGenericValueTypeInteger;
6341                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6342                 attribs[i].value.value.i = VA_FOURCC_P010;
6343                 i++;
6344             } else {
6345                 attribs[i].type = VASurfaceAttribPixelFormat;
6346                 attribs[i].value.type = VAGenericValueTypeInteger;
6347                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6348                 attribs[i].value.value.i = VA_FOURCC_NV12;
6349                 i++;
6350
6351                 attribs[i].type = VASurfaceAttribPixelFormat;
6352                 attribs[i].value.type = VAGenericValueTypeInteger;
6353                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6354                 attribs[i].value.value.i = VA_FOURCC_I420;
6355                 i++;
6356
6357                 attribs[i].type = VASurfaceAttribPixelFormat;
6358                 attribs[i].value.type = VAGenericValueTypeInteger;
6359                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6360                 attribs[i].value.value.i = VA_FOURCC_YV12;
6361                 i++;
6362
6363                 attribs[i].type = VASurfaceAttribPixelFormat;
6364                 attribs[i].value.type = VAGenericValueTypeInteger;
6365                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6366                 attribs[i].value.value.i = VA_FOURCC_IMC3;
6367                 i++;
6368             }
6369
6370             if (obj_config->entrypoint == VAEntrypointVideoProc) {
6371                 attribs[i].type = VASurfaceAttribPixelFormat;
6372                 attribs[i].value.type = VAGenericValueTypeInteger;
6373                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6374                 attribs[i].value.value.i = VA_FOURCC_YUY2;
6375                 i++;
6376
6377                 attribs[i].type = VASurfaceAttribPixelFormat;
6378                 attribs[i].value.type = VAGenericValueTypeInteger;
6379                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6380                 attribs[i].value.value.i = VA_FOURCC_UYVY;
6381                 i++;
6382
6383                 attribs[i].type = VASurfaceAttribPixelFormat;
6384                 attribs[i].value.type = VAGenericValueTypeInteger;
6385                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6386                 attribs[i].value.value.i = VA_FOURCC_RGBA;
6387                 i++;
6388
6389                 attribs[i].type = VASurfaceAttribPixelFormat;
6390                 attribs[i].value.type = VAGenericValueTypeInteger;
6391                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6392                 attribs[i].value.value.i = VA_FOURCC_RGBX;
6393                 i++;
6394
6395                 attribs[i].type = VASurfaceAttribPixelFormat;
6396                 attribs[i].value.type = VAGenericValueTypeInteger;
6397                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6398                 attribs[i].value.value.i = VA_FOURCC_BGRA;
6399                 i++;
6400
6401                 attribs[i].type = VASurfaceAttribPixelFormat;
6402                 attribs[i].value.type = VAGenericValueTypeInteger;
6403                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6404                 attribs[i].value.value.i = VA_FOURCC_BGRX;
6405                 i++;
6406
6407                 attribs[i].type = VASurfaceAttribPixelFormat;
6408                 attribs[i].value.type = VAGenericValueTypeInteger;
6409                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6410                 attribs[i].value.value.i = VA_FOURCC_YV16;
6411                 i++;
6412
6413                 if (HAS_VPP_P010(i965)) {
6414                     attribs[i].type = VASurfaceAttribPixelFormat;
6415                     attribs[i].value.type = VAGenericValueTypeInteger;
6416                     attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6417                     attribs[i].value.value.i = VA_FOURCC_P010;
6418                     i++;
6419
6420                     attribs[i].type = VASurfaceAttribPixelFormat;
6421                     attribs[i].value.type = VAGenericValueTypeInteger;
6422                     attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6423                     attribs[i].value.value.i = VA_FOURCC_I010;
6424                     i++;
6425                 }
6426             }
6427
6428             /* Additional support for jpeg encoder */
6429             if (obj_config->profile == VAProfileJPEGBaseline
6430                 && obj_config->entrypoint == VAEntrypointEncPicture) {
6431                 attribs[i].type = VASurfaceAttribPixelFormat;
6432                 attribs[i].value.type = VAGenericValueTypeInteger;
6433                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6434                 attribs[i].value.value.i = VA_FOURCC_YUY2;
6435                 i++;
6436
6437                 attribs[i].type = VASurfaceAttribPixelFormat;
6438                 attribs[i].value.type = VAGenericValueTypeInteger;
6439                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6440                 attribs[i].value.value.i = VA_FOURCC_UYVY;
6441                 i++;
6442
6443                 attribs[i].type = VASurfaceAttribPixelFormat;
6444                 attribs[i].value.type = VAGenericValueTypeInteger;
6445                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6446                 attribs[i].value.value.i = VA_FOURCC_YV16;
6447                 i++;
6448
6449                 attribs[i].type = VASurfaceAttribPixelFormat;
6450                 attribs[i].value.type = VAGenericValueTypeInteger;
6451                 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6452                 attribs[i].value.value.i = VA_FOURCC_Y800;
6453                 i++;
6454             }
6455         } else if (obj_config->entrypoint == VAEntrypointStats) {
6456             attribs[i].type = VASurfaceAttribPixelFormat;
6457             attribs[i].value.type = VAGenericValueTypeInteger;
6458             attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6459             attribs[i].value.value.i = VA_FOURCC_NV12;
6460             i++;
6461         }
6462     }
6463
6464     attribs[i].type = VASurfaceAttribMemoryType;
6465     attribs[i].value.type = VAGenericValueTypeInteger;
6466     attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
6467     attribs[i].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_VA |
6468                                VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM |
6469                                VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
6470     i++;
6471
6472     attribs[i].type = VASurfaceAttribExternalBufferDescriptor;
6473     attribs[i].value.type = VAGenericValueTypePointer;
6474     attribs[i].flags = VA_SURFACE_ATTRIB_SETTABLE;
6475     attribs[i].value.value.p = NULL; /* ignore */
6476     i++;
6477
6478     max_resolution(i965, obj_config, &max_width, &max_height);
6479
6480     attribs[i].type = VASurfaceAttribMaxWidth;
6481     attribs[i].value.type = VAGenericValueTypeInteger;
6482     attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
6483     attribs[i].value.value.i = max_width;
6484     i++;
6485
6486     attribs[i].type = VASurfaceAttribMaxHeight;
6487     attribs[i].value.type = VAGenericValueTypeInteger;
6488     attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
6489     attribs[i].value.value.i = max_height;
6490     i++;
6491
6492     if (obj_config->entrypoint == VAEntrypointEncSlice ||
6493         obj_config->entrypoint == VAEntrypointEncSliceLP ||
6494         obj_config->entrypoint == VAEntrypointEncPicture ||
6495         obj_config->entrypoint == VAEntrypointFEI) {
6496         attribs[i].type = VASurfaceAttribMinWidth;
6497         attribs[i].value.type = VAGenericValueTypeInteger;
6498         attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
6499         attribs[i].value.value.i = I965_MIN_CODEC_ENC_RESOLUTION_WIDTH_HEIGHT;
6500         if (obj_config->profile == VAProfileJPEGBaseline)
6501             attribs[i].value.value.i = 1;
6502         i++;
6503
6504         attribs[i].type = VASurfaceAttribMinHeight;
6505         attribs[i].value.type = VAGenericValueTypeInteger;
6506         attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
6507         attribs[i].value.value.i = I965_MIN_CODEC_ENC_RESOLUTION_WIDTH_HEIGHT;
6508         if (obj_config->profile == VAProfileJPEGBaseline)
6509             attribs[i].value.value.i = 1;
6510         i++;
6511     }
6512
6513     if (i > *num_attribs) {
6514         *num_attribs = i;
6515         free(attribs);
6516         return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
6517     }
6518
6519     *num_attribs = i;
6520     memcpy(attrib_list, attribs, i * sizeof(*attribs));
6521     free(attribs);
6522
6523     return vaStatus;
6524 }
6525
6526 /* Acquires buffer handle for external API usage (internal implementation) */
6527 static VAStatus
6528 i965_acquire_buffer_handle(struct object_buffer *obj_buffer,
6529                            uint32_t mem_type, VABufferInfo *out_buf_info)
6530 {
6531     struct buffer_store *buffer_store;
6532
6533     buffer_store = obj_buffer->buffer_store;
6534     if (!buffer_store || !buffer_store->bo)
6535         return VA_STATUS_ERROR_INVALID_BUFFER;
6536
6537     /* Synchronization point */
6538     drm_intel_bo_wait_rendering(buffer_store->bo);
6539
6540     if (obj_buffer->export_refcount > 0) {
6541         if (obj_buffer->export_state.mem_type != mem_type)
6542             return VA_STATUS_ERROR_INVALID_PARAMETER;
6543     } else {
6544         VABufferInfo * const buf_info = &obj_buffer->export_state;
6545
6546         switch (mem_type) {
6547         case VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM: {
6548             uint32_t name;
6549             if (drm_intel_bo_flink(buffer_store->bo, &name) != 0)
6550                 return VA_STATUS_ERROR_INVALID_BUFFER;
6551             buf_info->handle = name;
6552             break;
6553         }
6554         case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME: {
6555             int fd;
6556             if (drm_intel_bo_gem_export_to_prime(buffer_store->bo, &fd) != 0)
6557                 return VA_STATUS_ERROR_INVALID_BUFFER;
6558             buf_info->handle = (intptr_t)fd;
6559             break;
6560         }
6561         }
6562
6563         buf_info->type = obj_buffer->type;
6564         buf_info->mem_type = mem_type;
6565         buf_info->mem_size =
6566             obj_buffer->num_elements * obj_buffer->size_element;
6567     }
6568
6569     obj_buffer->export_refcount++;
6570     *out_buf_info = obj_buffer->export_state;
6571     return VA_STATUS_SUCCESS;
6572 }
6573
6574 /* Releases buffer handle after usage (internal implementation) */
6575 static VAStatus
6576 i965_release_buffer_handle(struct object_buffer *obj_buffer)
6577 {
6578     if (obj_buffer->export_refcount == 0)
6579         return VA_STATUS_ERROR_INVALID_BUFFER;
6580
6581     if (--obj_buffer->export_refcount == 0) {
6582         VABufferInfo * const buf_info = &obj_buffer->export_state;
6583
6584         switch (buf_info->mem_type) {
6585         case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME: {
6586             close((intptr_t)buf_info->handle);
6587             break;
6588         }
6589         }
6590         buf_info->mem_type = 0;
6591     }
6592     return VA_STATUS_SUCCESS;
6593 }
6594
6595 /** Acquires buffer handle for external API usage */
6596 static VAStatus
6597 i965_AcquireBufferHandle(VADriverContextP ctx, VABufferID buf_id,
6598                          VABufferInfo *buf_info)
6599 {
6600     struct i965_driver_data * const i965 = i965_driver_data(ctx);
6601     struct object_buffer * const obj_buffer = BUFFER(buf_id);
6602     uint32_t i, mem_type;
6603
6604     /* List of supported memory types, in preferred order */
6605     static const uint32_t mem_types[] = {
6606         VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME,
6607         VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM,
6608         0
6609     };
6610
6611     if (!obj_buffer)
6612         return VA_STATUS_ERROR_INVALID_BUFFER;
6613     /* XXX: only VA surface|image like buffers are supported for now */
6614     if (obj_buffer->type != VAImageBufferType)
6615         return VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
6616
6617     /*
6618      * As the allocated buffer by calling vaCreateBuffer is related with
6619      * the specific context, it is unnecessary to export it.
6620      * So it is not supported when the buffer is allocated from wrapped
6621      * backend dirver.
6622      */
6623     if (obj_buffer->wrapper_buffer != VA_INVALID_ID) {
6624         return VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
6625     }
6626
6627     if (!buf_info)
6628         return VA_STATUS_ERROR_INVALID_PARAMETER;
6629
6630     if (!buf_info->mem_type)
6631         mem_type = mem_types[0];
6632     else {
6633         mem_type = 0;
6634         for (i = 0; mem_types[i] != 0; i++) {
6635             if (buf_info->mem_type & mem_types[i]) {
6636                 mem_type = buf_info->mem_type;
6637                 break;
6638             }
6639         }
6640         if (!mem_type)
6641             return VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE;
6642     }
6643     return i965_acquire_buffer_handle(obj_buffer, mem_type, buf_info);
6644 }
6645
6646 /** Releases buffer handle after usage from external API */
6647 static VAStatus
6648 i965_ReleaseBufferHandle(VADriverContextP ctx, VABufferID buf_id)
6649 {
6650     struct i965_driver_data * const i965 = i965_driver_data(ctx);
6651     struct object_buffer * const obj_buffer = BUFFER(buf_id);
6652
6653     if (!obj_buffer)
6654         return VA_STATUS_ERROR_INVALID_BUFFER;
6655
6656     if (obj_buffer->wrapper_buffer != VA_INVALID_ID) {
6657         return VA_STATUS_ERROR_INVALID_BUFFER;
6658     }
6659
6660     return i965_release_buffer_handle(obj_buffer);
6661 }
6662
6663 // Locally define DRM_FORMAT values not available in older but still
6664 // supported versions of libdrm.
6665 #ifndef DRM_FORMAT_R8
6666 #define DRM_FORMAT_R8        fourcc_code('R', '8', ' ', ' ')
6667 #endif
6668 #ifndef DRM_FORMAT_R16
6669 #define DRM_FORMAT_R16       fourcc_code('R', '1', '6', ' ')
6670 #endif
6671 #ifndef DRM_FORMAT_GR88
6672 #define DRM_FORMAT_GR88      fourcc_code('G', 'R', '8', '8')
6673 #endif
6674 #ifndef DRM_FORMAT_GR1616
6675 #define DRM_FORMAT_GR1616    fourcc_code('G', 'R', '3', '2')
6676 #endif
6677
6678 static uint32_t drm_format_of_separate_plane(uint32_t fourcc, int plane)
6679 {
6680     if (plane == 0) {
6681         switch (fourcc) {
6682         case VA_FOURCC_NV12:
6683         case VA_FOURCC_I420:
6684         case VA_FOURCC_YV12:
6685         case VA_FOURCC_YV16:
6686         case VA_FOURCC_Y800:
6687             return DRM_FORMAT_R8;
6688         case VA_FOURCC_P010:
6689         case VA_FOURCC_I010:
6690             return DRM_FORMAT_R16;
6691
6692         case VA_FOURCC_YUY2:
6693         case VA_FOURCC_UYVY:
6694             // These are not representable as separate planes.
6695             return 0;
6696
6697         case VA_FOURCC_RGBA:
6698             return DRM_FORMAT_ABGR8888;
6699         case VA_FOURCC_RGBX:
6700             return DRM_FORMAT_XBGR8888;
6701         case VA_FOURCC_BGRA:
6702             return DRM_FORMAT_ARGB8888;
6703         case VA_FOURCC_BGRX:
6704             return DRM_FORMAT_XRGB8888;
6705         case VA_FOURCC_ARGB:
6706             return DRM_FORMAT_BGRA8888;
6707         case VA_FOURCC_ABGR:
6708             return DRM_FORMAT_RGBA8888;
6709         }
6710     } else {
6711         switch (fourcc) {
6712         case VA_FOURCC_NV12:
6713             return DRM_FORMAT_GR88;
6714         case VA_FOURCC_I420:
6715         case VA_FOURCC_YV12:
6716         case VA_FOURCC_YV16:
6717             return DRM_FORMAT_R8;
6718         case VA_FOURCC_P010:
6719             return DRM_FORMAT_GR1616;
6720         case VA_FOURCC_I010:
6721             return DRM_FORMAT_R16;
6722         }
6723     }
6724     return 0;
6725 }
6726
6727 static uint32_t drm_format_of_composite_object(uint32_t fourcc)
6728 {
6729     switch (fourcc) {
6730     case VA_FOURCC_NV12:
6731         return DRM_FORMAT_NV12;
6732     case VA_FOURCC_I420:
6733         return DRM_FORMAT_YUV420;
6734     case VA_FOURCC_YV12:
6735         return DRM_FORMAT_YVU420;
6736     case VA_FOURCC_YV16:
6737         return DRM_FORMAT_YVU422;
6738     case VA_FOURCC_YUY2:
6739         return DRM_FORMAT_YUYV;
6740     case VA_FOURCC_UYVY:
6741         return DRM_FORMAT_UYVY;
6742     case VA_FOURCC_Y800:
6743         return DRM_FORMAT_R8;
6744
6745     case VA_FOURCC_P010:
6746     case VA_FOURCC_I010:
6747         // These currently have no composite DRM format - they are usable
6748         // only as separate planes.
6749         return 0;
6750
6751     case VA_FOURCC_RGBA:
6752         return DRM_FORMAT_ABGR8888;
6753     case VA_FOURCC_RGBX:
6754         return DRM_FORMAT_XBGR8888;
6755     case VA_FOURCC_BGRA:
6756         return DRM_FORMAT_ARGB8888;
6757     case VA_FOURCC_BGRX:
6758         return DRM_FORMAT_XRGB8888;
6759     case VA_FOURCC_ARGB:
6760         return DRM_FORMAT_BGRA8888;
6761     case VA_FOURCC_ABGR:
6762         return DRM_FORMAT_RGBA8888;
6763     }
6764     return 0;
6765 }
6766
6767 static VAStatus
6768 i965_ExportSurfaceHandle(VADriverContextP ctx, VASurfaceID surface_id,
6769                          uint32_t mem_type, uint32_t flags,
6770                          void *descriptor)
6771 {
6772     struct i965_driver_data *const i965 = i965_driver_data(ctx);
6773     struct object_surface *obj_surface = SURFACE(surface_id);
6774     const i965_fourcc_info *info;
6775     VADRMPRIMESurfaceDescriptor *desc;
6776     unsigned int tiling, swizzle;
6777     uint32_t formats[4], pitch, height, offset;
6778     int fd, p;
6779     int composite_object =
6780         flags & VA_EXPORT_SURFACE_COMPOSED_LAYERS;
6781
6782     if (!obj_surface || !obj_surface->bo)
6783         return VA_STATUS_ERROR_INVALID_SURFACE;
6784
6785     if (mem_type != VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2) {
6786         i965_log_info(ctx, "vaExportSurfaceHandle: memory type %08x "
6787                       "is not supported.\n", mem_type);
6788         return VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE;
6789     }
6790
6791     info = get_fourcc_info(obj_surface->fourcc);
6792     if (!info)
6793         return VA_STATUS_ERROR_INVALID_SURFACE;
6794     if (composite_object) {
6795         formats[0] =
6796             drm_format_of_composite_object(obj_surface->fourcc);
6797         if (!formats[0]) {
6798             i965_log_info(ctx, "vaExportSurfaceHandle: fourcc %08x "
6799                           "is not supported for export as a composite "
6800                           "object.\n", obj_surface->fourcc);
6801             return VA_STATUS_ERROR_INVALID_SURFACE;
6802         }
6803     } else {
6804         for (p = 0; p < info->num_planes; p++) {
6805             formats[p] =
6806                 drm_format_of_separate_plane(obj_surface->fourcc, p);
6807             if (!formats[p]) {
6808                 i965_log_info(ctx, "vaExportSurfaceHandle: fourcc %08x "
6809                               "is not supported for export as separate "
6810                               "planes.\n", obj_surface->fourcc);
6811                 return VA_STATUS_ERROR_INVALID_SURFACE;
6812             }
6813         }
6814     }
6815
6816     if (drm_intel_bo_gem_export_to_prime(obj_surface->bo, &fd))
6817         return VA_STATUS_ERROR_INVALID_SURFACE;
6818
6819     if (drm_intel_bo_get_tiling(obj_surface->bo, &tiling, &swizzle))
6820         tiling = I915_TILING_NONE;
6821
6822     desc = descriptor;
6823
6824     desc->fourcc = obj_surface->fourcc;
6825     desc->width  = obj_surface->orig_width;
6826     desc->height = obj_surface->orig_height;
6827
6828     desc->num_objects     = 1;
6829     desc->objects[0].fd   = fd;
6830     desc->objects[0].size = obj_surface->size;
6831     switch (tiling) {
6832     case I915_TILING_X:
6833         desc->objects[0].drm_format_modifier = I915_FORMAT_MOD_X_TILED;
6834         break;
6835     case I915_TILING_Y:
6836         desc->objects[0].drm_format_modifier = I915_FORMAT_MOD_Y_TILED;
6837         break;
6838     case I915_TILING_NONE:
6839     default:
6840         desc->objects[0].drm_format_modifier = DRM_FORMAT_MOD_NONE;
6841     }
6842
6843     if (composite_object) {
6844         desc->num_layers = 1;
6845
6846         desc->layers[0].drm_format = formats[0];
6847         desc->layers[0].num_planes = info->num_planes;
6848
6849         offset = 0;
6850         for (p = 0; p < info->num_planes; p++) {
6851             desc->layers[0].object_index[p] = 0;
6852
6853             if (p == 0) {
6854                 pitch  = obj_surface->width;
6855                 height = obj_surface->height;
6856             } else {
6857                 pitch  = obj_surface->cb_cr_pitch;
6858                 height = obj_surface->cb_cr_height;
6859             }
6860
6861             desc->layers[0].offset[p] = offset;
6862             desc->layers[0].pitch[p]  = pitch;
6863
6864             offset += pitch * height;
6865         }
6866     } else {
6867         desc->num_layers = info->num_planes;
6868
6869         offset = 0;
6870         for (p = 0; p < info->num_planes; p++) {
6871             desc->layers[p].drm_format = formats[p];
6872             desc->layers[p].num_planes = 1;
6873
6874             desc->layers[p].object_index[0] = 0;
6875
6876             if (p == 0) {
6877                 pitch  = obj_surface->width;
6878                 height = obj_surface->height;
6879             } else {
6880                 pitch  = obj_surface->cb_cr_pitch;
6881                 height = obj_surface->cb_cr_height;
6882             }
6883
6884             desc->layers[p].offset[0] = offset;
6885             desc->layers[p].pitch[0]  = pitch;
6886
6887             offset += pitch * height;
6888         }
6889     }
6890
6891     return VA_STATUS_SUCCESS;
6892 }
6893
6894 static int
6895 i965_os_has_ring_support(VADriverContextP ctx,
6896                          int ring)
6897 {
6898     struct i965_driver_data *const i965 = i965_driver_data(ctx);
6899
6900     switch (ring) {
6901     case I965_RING_BSD:
6902         return i965->intel.has_bsd;
6903
6904     case I965_RING_BLT:
6905         return i965->intel.has_blt;
6906
6907     case I965_RING_VEBOX:
6908         return i965->intel.has_vebox;
6909
6910     case I965_RING_NULL:
6911         return 1; /* Always support */
6912
6913     default:
6914         /* should never get here */
6915         assert(0);
6916         break;
6917     }
6918
6919     return 0;
6920 }
6921
6922 /*
6923  * Query video processing pipeline
6924  */
6925 VAStatus i965_QueryVideoProcFilters(
6926     VADriverContextP    ctx,
6927     VAContextID         context,
6928     VAProcFilterType   *filters,
6929     unsigned int       *num_filters
6930 )
6931 {
6932     struct i965_driver_data *const i965 = i965_driver_data(ctx);
6933     unsigned int i = 0, num = 0;
6934
6935     if (!num_filters  || !filters)
6936         return VA_STATUS_ERROR_INVALID_PARAMETER;
6937
6938     for (i = 0; i < i965->codec_info->num_filters; i++) {
6939         if (i965_os_has_ring_support(ctx, i965->codec_info->filters[i].ring)) {
6940             if (num == *num_filters) {
6941                 *num_filters = i965->codec_info->num_filters;
6942
6943                 return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
6944             }
6945
6946             filters[num++] = i965->codec_info->filters[i].type;
6947         }
6948     }
6949
6950     *num_filters = num;
6951
6952     return VA_STATUS_SUCCESS;
6953 }
6954
6955 VAStatus i965_QueryVideoProcFilterCaps(
6956     VADriverContextP    ctx,
6957     VAContextID         context,
6958     VAProcFilterType    type,
6959     void               *filter_caps,
6960     unsigned int       *num_filter_caps
6961 )
6962 {
6963     unsigned int i = 0;
6964     struct i965_driver_data *const i965 = i965_driver_data(ctx);
6965
6966     if (!filter_caps || !num_filter_caps)
6967         return VA_STATUS_ERROR_INVALID_PARAMETER;
6968
6969     for (i = 0; i < i965->codec_info->num_filters; i++) {
6970         if (type == i965->codec_info->filters[i].type &&
6971             i965_os_has_ring_support(ctx, i965->codec_info->filters[i].ring))
6972             break;
6973     }
6974
6975     if (i == i965->codec_info->num_filters)
6976         return VA_STATUS_ERROR_UNSUPPORTED_FILTER;
6977
6978     i = 0;
6979
6980     switch (type) {
6981     case VAProcFilterNoiseReduction:
6982     case VAProcFilterSharpening: {
6983         VAProcFilterCap *cap = filter_caps;
6984
6985         if (*num_filter_caps < 1) {
6986             *num_filter_caps = 1;
6987             return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
6988         }
6989
6990         cap->range.min_value = 0.0;
6991         cap->range.max_value = 1.0;
6992         cap->range.default_value = 0.5;
6993         cap->range.step = 0.03125; /* 1.0 / 32 */
6994         i++;
6995     }
6996
6997     break;
6998
6999     case VAProcFilterDeinterlacing: {
7000         VAProcFilterCapDeinterlacing *cap = filter_caps;
7001
7002         if (*num_filter_caps < VAProcDeinterlacingCount) {
7003             *num_filter_caps = VAProcDeinterlacingCount;
7004             return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
7005         }
7006
7007         cap->type = VAProcDeinterlacingBob;
7008         i++;
7009         cap++;
7010
7011
7012         if (i965->codec_info->has_di_motion_adptive) {
7013             cap->type = VAProcDeinterlacingMotionAdaptive;
7014             i++;
7015             cap++;
7016         }
7017
7018         if (i965->codec_info->has_di_motion_compensated) {
7019             cap->type = VAProcDeinterlacingMotionCompensated;
7020             i++;
7021             cap++;
7022         }
7023     }
7024
7025     break;
7026
7027     case VAProcFilterColorBalance: {
7028         VAProcFilterCapColorBalance *cap = filter_caps;
7029
7030         if (*num_filter_caps < VAProcColorBalanceCount) {
7031             *num_filter_caps = VAProcColorBalanceCount;
7032             return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
7033         }
7034
7035         cap->type = VAProcColorBalanceHue;
7036         cap->range.min_value = -180.0;
7037         cap->range.max_value = 180.0;
7038         cap->range.default_value = 0.0;
7039         cap->range.step = 1.0;
7040         i++;
7041         cap++;
7042
7043         cap->type = VAProcColorBalanceSaturation;
7044         cap->range.min_value = 0.0;
7045         cap->range.max_value = 10.0;
7046         cap->range.default_value = 1.0;
7047         cap->range.step = 0.1;
7048         i++;
7049         cap++;
7050
7051         cap->type = VAProcColorBalanceBrightness;
7052         cap->range.min_value = -100.0;
7053         cap->range.max_value = 100.0;
7054         cap->range.default_value = 0.0;
7055         cap->range.step = 1.0;
7056         i++;
7057         cap++;
7058
7059         cap->type = VAProcColorBalanceContrast;
7060         cap->range.min_value = 0.0;
7061         cap->range.max_value = 10.0;
7062         cap->range.default_value = 1.0;
7063         cap->range.step = 0.1;
7064         i++;
7065         cap++;
7066     }
7067
7068     break;
7069
7070     default:
7071
7072         break;
7073     }
7074
7075     *num_filter_caps = i;
7076
7077     return VA_STATUS_SUCCESS;
7078 }
7079
7080 static VAProcColorStandardType vpp_input_color_standards[VAProcColorStandardCount] = {
7081     VAProcColorStandardBT601,
7082 };
7083
7084 static VAProcColorStandardType vpp_output_color_standards[VAProcColorStandardCount] = {
7085     VAProcColorStandardBT601,
7086 };
7087
7088 VAStatus i965_QueryVideoProcPipelineCaps(
7089     VADriverContextP ctx,
7090     VAContextID context,
7091     VABufferID *filters,
7092     unsigned int num_filters,
7093     VAProcPipelineCaps *pipeline_cap     /* out */
7094 )
7095 {
7096     struct i965_driver_data * const i965 = i965_driver_data(ctx);
7097     unsigned int i = 0;
7098
7099     pipeline_cap->pipeline_flags = 0;
7100     pipeline_cap->filter_flags = 0;
7101     pipeline_cap->num_forward_references = 0;
7102     pipeline_cap->num_backward_references = 0;
7103     pipeline_cap->num_input_color_standards = 1;
7104     pipeline_cap->input_color_standards = vpp_input_color_standards;
7105     pipeline_cap->num_output_color_standards = 1;
7106     pipeline_cap->output_color_standards = vpp_output_color_standards;
7107
7108     for (i = 0; i < num_filters; i++) {
7109         struct object_buffer *obj_buffer = BUFFER(filters[i]);
7110
7111         if (!obj_buffer ||
7112             !obj_buffer->buffer_store ||
7113             !obj_buffer->buffer_store->buffer)
7114             return VA_STATUS_ERROR_INVALID_BUFFER;
7115
7116         VAProcFilterParameterBufferBase *base = (VAProcFilterParameterBufferBase *)obj_buffer->buffer_store->buffer;
7117
7118         if (base->type == VAProcFilterNoiseReduction) {
7119             VAProcFilterParameterBuffer *denoise = (VAProcFilterParameterBuffer *)base;
7120             (void)denoise;
7121         } else if (base->type == VAProcFilterDeinterlacing) {
7122             VAProcFilterParameterBufferDeinterlacing *deint = (VAProcFilterParameterBufferDeinterlacing *)base;
7123
7124             ASSERT_RET(deint->algorithm == VAProcDeinterlacingBob ||
7125                        deint->algorithm == VAProcDeinterlacingMotionAdaptive ||
7126                        deint->algorithm == VAProcDeinterlacingMotionCompensated,
7127                        VA_STATUS_ERROR_INVALID_PARAMETER);
7128
7129             if (deint->algorithm == VAProcDeinterlacingMotionAdaptive ||
7130                 deint->algorithm == VAProcDeinterlacingMotionCompensated)
7131                 pipeline_cap->num_forward_references++;
7132         } else if (base->type == VAProcFilterSkinToneEnhancement) {
7133             VAProcFilterParameterBuffer *stde = (VAProcFilterParameterBuffer *)base;
7134             (void)stde;
7135         }
7136     }
7137
7138     return VA_STATUS_SUCCESS;
7139 }
7140
7141 void i965_log_error(VADriverContextP ctx, const char *format, ...)
7142 {
7143     va_list vl;
7144
7145     va_start(vl, format);
7146
7147     if (!ctx->error_callback) {
7148         // No error callback: this is a error message which should be
7149         // user-visible, so print it to stderr instead.
7150         vfprintf(stderr, format, vl);
7151     } else {
7152         // Put the message on the stack.  If it overruns the size of the
7153         // then it will just be truncated - callers shouldn't be sending
7154         // messages which are too long.
7155         char tmp[1024];
7156         int ret;
7157         ret = vsnprintf(tmp, sizeof(tmp), format, vl);
7158         if (ret > 0)
7159             ctx->error_callback(ctx, tmp);
7160     }
7161
7162     va_end(vl);
7163 }
7164
7165 void i965_log_info(VADriverContextP ctx, const char *format, ...)
7166 {
7167     va_list vl;
7168
7169     va_start(vl, format);
7170
7171     if (!ctx->info_callback) {
7172         // No info callback: this message is only useful for developers,
7173         // so just discard it.
7174     } else {
7175         // Put the message on the stack.  If it overruns the size of the
7176         // then it will just be truncated - callers shouldn't be sending
7177         // messages which are too long.
7178         char tmp[1024];
7179         int ret;
7180         ret = vsnprintf(tmp, sizeof(tmp), format, vl);
7181         if (ret > 0)
7182             ctx->info_callback(ctx, tmp);
7183     }
7184
7185     va_end(vl);
7186 }
7187
7188 extern struct hw_codec_info *i965_get_codec_info(int devid);
7189
7190 static bool
7191 i965_driver_data_init(VADriverContextP ctx)
7192 {
7193     struct i965_driver_data *i965 = i965_driver_data(ctx);
7194
7195     i965->codec_info = i965_get_codec_info(i965->intel.device_id);
7196
7197     if (!i965->codec_info)
7198         return false;
7199
7200     if (object_heap_init(&i965->config_heap,
7201                          sizeof(struct object_config),
7202                          CONFIG_ID_OFFSET))
7203         goto err_config_heap;
7204     if (object_heap_init(&i965->context_heap,
7205                          sizeof(struct object_context),
7206                          CONTEXT_ID_OFFSET))
7207         goto err_context_heap;
7208
7209     if (object_heap_init(&i965->surface_heap,
7210                          sizeof(struct object_surface),
7211                          SURFACE_ID_OFFSET))
7212         goto err_surface_heap;
7213     if (object_heap_init(&i965->buffer_heap,
7214                          sizeof(struct object_buffer),
7215                          BUFFER_ID_OFFSET))
7216         goto err_buffer_heap;
7217     if (object_heap_init(&i965->image_heap,
7218                          sizeof(struct object_image),
7219                          IMAGE_ID_OFFSET))
7220         goto err_image_heap;
7221     if (object_heap_init(&i965->subpic_heap,
7222                          sizeof(struct object_subpic),
7223                          SUBPIC_ID_OFFSET))
7224         goto err_subpic_heap;
7225
7226     i965->batch = intel_batchbuffer_new(&i965->intel, I915_EXEC_RENDER, 0);
7227     i965->pp_batch = intel_batchbuffer_new(&i965->intel, I915_EXEC_RENDER, 0);
7228     _i965InitMutex(&i965->render_mutex);
7229     _i965InitMutex(&i965->pp_mutex);
7230
7231     return true;
7232
7233 err_subpic_heap:
7234     object_heap_destroy(&i965->image_heap);
7235 err_image_heap:
7236     object_heap_destroy(&i965->buffer_heap);
7237 err_buffer_heap:
7238     object_heap_destroy(&i965->surface_heap);
7239 err_surface_heap:
7240     object_heap_destroy(&i965->context_heap);
7241 err_context_heap:
7242     object_heap_destroy(&i965->config_heap);
7243 err_config_heap:
7244
7245     return false;
7246 }
7247
7248 static void
7249 i965_driver_data_terminate(VADriverContextP ctx)
7250 {
7251     struct i965_driver_data *i965 = i965_driver_data(ctx);
7252
7253     _i965DestroyMutex(&i965->pp_mutex);
7254     _i965DestroyMutex(&i965->render_mutex);
7255
7256     if (i965->batch)
7257         intel_batchbuffer_free(i965->batch);
7258
7259     if (i965->pp_batch)
7260         intel_batchbuffer_free(i965->pp_batch);
7261
7262     i965_destroy_heap(&i965->subpic_heap, i965_destroy_subpic);
7263     i965_destroy_heap(&i965->image_heap, i965_destroy_image);
7264     i965_destroy_heap(&i965->buffer_heap, i965_destroy_buffer);
7265     i965_destroy_heap(&i965->surface_heap, i965_destroy_surface);
7266     i965_destroy_heap(&i965->context_heap, i965_destroy_context);
7267     i965_destroy_heap(&i965->config_heap, i965_destroy_config);
7268 }
7269
7270 struct {
7271     bool (*init)(VADriverContextP ctx);
7272     void (*terminate)(VADriverContextP ctx);
7273     int display_type;
7274 } i965_sub_ops[] =  {
7275     {
7276         intel_driver_init,
7277         intel_driver_terminate,
7278         0,
7279     },
7280
7281     {
7282         i965_driver_data_init,
7283         i965_driver_data_terminate,
7284         0,
7285     },
7286
7287     {
7288         i965_display_attributes_init,
7289         i965_display_attributes_terminate,
7290         0,
7291     },
7292
7293     {
7294         i965_post_processing_init,
7295         i965_post_processing_terminate,
7296         0,
7297     },
7298
7299     {
7300         i965_render_init,
7301         i965_render_terminate,
7302         0,
7303     },
7304
7305 #ifdef HAVE_VA_WAYLAND
7306     {
7307         i965_output_wayland_init,
7308         i965_output_wayland_terminate,
7309         VA_DISPLAY_WAYLAND,
7310     },
7311 #endif
7312
7313 #ifdef HAVE_VA_X11
7314     {
7315         i965_output_dri_init,
7316         i965_output_dri_terminate,
7317         VA_DISPLAY_X11,
7318     },
7319 #endif
7320
7321     {
7322         i965_gpe_table_init,
7323         i965_gpe_table_terminate,
7324         0,
7325     },
7326 };
7327
7328 static bool
7329 ensure_vendor_string(struct i965_driver_data *i965, const char *chipset)
7330 {
7331     int ret, len;
7332
7333     if (i965->va_vendor[0] != '\0')
7334         return true;
7335
7336     len = 0;
7337     ret = snprintf(i965->va_vendor, sizeof(i965->va_vendor),
7338                    "%s %s driver for %s - %d.%d.%d",
7339                    INTEL_STR_DRIVER_VENDOR, INTEL_STR_DRIVER_NAME, chipset,
7340                    INTEL_DRIVER_MAJOR_VERSION, INTEL_DRIVER_MINOR_VERSION,
7341                    INTEL_DRIVER_MICRO_VERSION);
7342     if (ret < 0 || ret >= sizeof(i965->va_vendor))
7343         goto error;
7344     len = ret;
7345
7346     if (INTEL_DRIVER_PRE_VERSION > 0) {
7347         ret = snprintf(&i965->va_vendor[len], sizeof(i965->va_vendor) - len,
7348                        ".pre%d", INTEL_DRIVER_PRE_VERSION);
7349         if (ret < 0 || ret >= sizeof(i965->va_vendor))
7350             goto error;
7351         len += ret;
7352
7353         ret = snprintf(&i965->va_vendor[len], sizeof(i965->va_vendor) - len,
7354                        " (%s)", INTEL_DRIVER_GIT_VERSION);
7355         if (ret < 0 || ret >= sizeof(i965->va_vendor))
7356             goto error;
7357         len += ret;
7358     }
7359     return true;
7360
7361 error:
7362     i965->va_vendor[0] = '\0';
7363     ASSERT_RET(ret > 0 && len < sizeof(i965->va_vendor), false);
7364     return false;
7365 }
7366
7367 /* Only when the option of "enable-wrapper" is passed, it is possible
7368  * to initialize/load the wrapper context of backend driver.
7369  * Otherwise it is not loaded.
7370  */
7371 #if HAVE_HYBRID_CODEC
7372
7373 static VAStatus
7374 i965_initialize_wrapper(VADriverContextP ctx, const char *driver_name)
7375 {
7376 #define DRIVER_EXTENSION    "_drv_video.so"
7377
7378     struct i965_driver_data *i965 = i965_driver_data(ctx);
7379
7380     VADriverContextP wrapper_pdrvctx;
7381     struct VADriverVTable *vtable;
7382     char *search_path, *driver_dir;
7383     char *saveptr;
7384     char driver_path[256];
7385     void *handle = NULL;
7386     VAStatus va_status = VA_STATUS_SUCCESS;
7387     bool driver_loaded = false;
7388
7389     if (HAS_VP9_DECODING(i965)) {
7390         i965->wrapper_pdrvctx = NULL;
7391         return va_status;
7392     }
7393
7394     wrapper_pdrvctx = calloc(1, sizeof(*wrapper_pdrvctx));
7395     vtable = calloc(1, sizeof(*vtable));
7396
7397     if (!wrapper_pdrvctx || !vtable) {
7398         fprintf(stderr, "Failed to allocate memory for wrapper \n");
7399         free(wrapper_pdrvctx);
7400         free(vtable);
7401         return VA_STATUS_ERROR_ALLOCATION_FAILED;
7402     }
7403
7404     /* use the same drm_state with CTX */
7405     wrapper_pdrvctx->drm_state = ctx->drm_state;
7406     wrapper_pdrvctx->display_type = ctx->display_type;
7407     wrapper_pdrvctx->vtable = vtable;
7408
7409     search_path = VA_DRIVERS_PATH;
7410     search_path = strdup((const char *)search_path);
7411
7412     driver_dir = strtok_r(search_path, ":", &saveptr);
7413     while (driver_dir && !driver_loaded) {
7414         memset(driver_path, 0, sizeof(driver_path));
7415         sprintf(driver_path, "%s/%s%s", driver_dir, driver_name, DRIVER_EXTENSION);
7416
7417         handle = dlopen(driver_path, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
7418         if (!handle) {
7419             fprintf(stderr, "failed to open %s\n", driver_path);
7420             driver_dir = strtok_r(NULL, ":", &saveptr);
7421             continue;
7422         }
7423         {
7424             VADriverInit init_func = NULL;
7425             char init_func_s[256];
7426             int i;
7427
7428             static const struct {
7429                 int major;
7430                 int minor;
7431             } compatible_versions[] = {
7432                 { VA_MAJOR_VERSION, VA_MINOR_VERSION },
7433                 { 0, 37 },
7434                 { 0, 36 },
7435                 { 0, 35 },
7436                 { 0, 34 },
7437                 { 0, 33 },
7438                 { 0, 32 },
7439                 { -1, }
7440             };
7441             for (i = 0; compatible_versions[i].major >= 0; i++) {
7442                 snprintf(init_func_s, sizeof(init_func_s),
7443                          "__vaDriverInit_%d_%d",
7444                          compatible_versions[i].major,
7445                          compatible_versions[i].minor);
7446                 init_func = (VADriverInit)dlsym(handle, init_func_s);
7447                 if (init_func) {
7448                     break;
7449                 }
7450             }
7451             if (compatible_versions[i].major < 0) {
7452                 dlclose(handle);
7453                 fprintf(stderr, "%s has no function %s\n",
7454                         driver_path, init_func_s);
7455                 driver_dir = strtok_r(NULL, ":", &saveptr);
7456                 continue;
7457             }
7458
7459             if (init_func)
7460                 va_status = (*init_func)(wrapper_pdrvctx);
7461
7462             if (va_status != VA_STATUS_SUCCESS) {
7463                 dlclose(handle);
7464                 fprintf(stderr, "%s init failed\n", driver_path);
7465                 driver_dir = strtok_r(NULL, ":", &saveptr);
7466                 continue;
7467             }
7468
7469             wrapper_pdrvctx->handle = handle;
7470             driver_loaded = true;
7471         }
7472     }
7473
7474     free(search_path);
7475
7476     if (driver_loaded) {
7477         i965->wrapper_pdrvctx = wrapper_pdrvctx;
7478         return VA_STATUS_SUCCESS;
7479     } else {
7480         fprintf(stderr, "Failed to wrapper %s%s\n", driver_name, DRIVER_EXTENSION);
7481         free(vtable);
7482         free(wrapper_pdrvctx);
7483         return VA_STATUS_ERROR_OPERATION_FAILED;
7484     }
7485 }
7486 #endif
7487
7488 static VAStatus
7489 i965_Init(VADriverContextP ctx)
7490 {
7491     struct i965_driver_data *i965 = i965_driver_data(ctx);
7492     int i;
7493     const char *chipset;
7494
7495     for (i = 0; i < ARRAY_ELEMS(i965_sub_ops); i++) {
7496         if ((i965_sub_ops[i].display_type == 0 ||
7497              i965_sub_ops[i].display_type == (ctx->display_type & VA_DISPLAY_MAJOR_MASK)) &&
7498             !i965_sub_ops[i].init(ctx))
7499             break;
7500     }
7501
7502     if (i == ARRAY_ELEMS(i965_sub_ops)) {
7503         switch (i965->intel.device_id) {
7504 #undef CHIPSET
7505 #define CHIPSET(id, family, dev, str) case id: chipset = str; break;
7506 #include "i965_pciids.h"
7507         default:
7508             chipset = "Unknown Intel Chipset";
7509             break;
7510         }
7511
7512         if (!ensure_vendor_string(i965, chipset))
7513             return VA_STATUS_ERROR_ALLOCATION_FAILED;
7514
7515         i965->current_context_id = VA_INVALID_ID;
7516
7517         if (i965->codec_info && i965->codec_info->preinit_hw_codec)
7518             i965->codec_info->preinit_hw_codec(ctx, i965->codec_info);
7519
7520 #if HAVE_HYBRID_CODEC
7521         i965_initialize_wrapper(ctx, "hybrid");
7522 #endif
7523
7524         return VA_STATUS_SUCCESS;
7525     } else {
7526         i--;
7527
7528         for (; i >= 0; i--) {
7529             if (i965_sub_ops[i].display_type == 0 ||
7530                 i965_sub_ops[i].display_type == (ctx->display_type & VA_DISPLAY_MAJOR_MASK)) {
7531                 i965_sub_ops[i].terminate(ctx);
7532             }
7533         }
7534
7535         return VA_STATUS_ERROR_UNKNOWN;
7536     }
7537 }
7538
7539 VAStatus
7540 i965_Terminate(VADriverContextP ctx)
7541 {
7542     struct i965_driver_data *i965 = i965_driver_data(ctx);
7543     int i;
7544
7545     if (i965) {
7546 #if HAVE_HYBRID_CODEC
7547         if (i965->wrapper_pdrvctx) {
7548             VADriverContextP pdrvctx;
7549             pdrvctx = i965->wrapper_pdrvctx;
7550             if (pdrvctx->handle) {
7551                 pdrvctx->vtable->vaTerminate(pdrvctx);
7552                 dlclose(pdrvctx->handle);
7553                 pdrvctx->handle = NULL;
7554             }
7555             free(pdrvctx->vtable);
7556             free(pdrvctx);
7557             i965->wrapper_pdrvctx = NULL;
7558         }
7559 #endif
7560
7561         for (i = ARRAY_ELEMS(i965_sub_ops); i > 0; i--)
7562             if (i965_sub_ops[i - 1].display_type == 0 ||
7563                 i965_sub_ops[i - 1].display_type == (ctx->display_type & VA_DISPLAY_MAJOR_MASK)) {
7564                 i965_sub_ops[i - 1].terminate(ctx);
7565             }
7566
7567         free(i965);
7568         ctx->pDriverData = NULL;
7569     }
7570
7571     return VA_STATUS_SUCCESS;
7572 }
7573
7574 VAStatus DLL_EXPORT
7575 VA_DRIVER_INIT_FUNC(VADriverContextP ctx);
7576
7577 VAStatus
7578 VA_DRIVER_INIT_FUNC(VADriverContextP ctx)
7579 {
7580     struct VADriverVTable * const vtable = ctx->vtable;
7581     struct VADriverVTableVPP * const vtable_vpp = ctx->vtable_vpp;
7582
7583     struct i965_driver_data *i965;
7584     VAStatus ret = VA_STATUS_ERROR_UNKNOWN;
7585
7586     ctx->version_major = VA_MAJOR_VERSION;
7587     ctx->version_minor = VA_MINOR_VERSION;
7588     ctx->max_profiles = I965_MAX_PROFILES;
7589     ctx->max_entrypoints = I965_MAX_ENTRYPOINTS;
7590     ctx->max_attributes = I965_MAX_CONFIG_ATTRIBUTES;
7591     ctx->max_image_formats = I965_MAX_IMAGE_FORMATS;
7592     ctx->max_subpic_formats = I965_MAX_SUBPIC_FORMATS;
7593     ctx->max_display_attributes = 1 + ARRAY_ELEMS(i965_display_attributes);
7594
7595     vtable->vaTerminate = i965_Terminate;
7596     vtable->vaQueryConfigEntrypoints = i965_QueryConfigEntrypoints;
7597     vtable->vaQueryConfigProfiles = i965_QueryConfigProfiles;
7598     vtable->vaQueryConfigAttributes = i965_QueryConfigAttributes;
7599     vtable->vaCreateConfig = i965_CreateConfig;
7600     vtable->vaDestroyConfig = i965_DestroyConfig;
7601     vtable->vaGetConfigAttributes = i965_GetConfigAttributes;
7602     vtable->vaCreateSurfaces = i965_CreateSurfaces;
7603     vtable->vaDestroySurfaces = i965_DestroySurfaces;
7604     vtable->vaCreateContext = i965_CreateContext;
7605     vtable->vaDestroyContext = i965_DestroyContext;
7606     vtable->vaCreateBuffer = i965_CreateBuffer;
7607     vtable->vaBufferSetNumElements = i965_BufferSetNumElements;
7608     vtable->vaMapBuffer = i965_MapBuffer;
7609     vtable->vaUnmapBuffer = i965_UnmapBuffer;
7610     vtable->vaDestroyBuffer = i965_DestroyBuffer;
7611     vtable->vaBeginPicture = i965_BeginPicture;
7612     vtable->vaRenderPicture = i965_RenderPicture;
7613     vtable->vaEndPicture = i965_EndPicture;
7614     vtable->vaSyncSurface = i965_SyncSurface;
7615     vtable->vaQuerySurfaceStatus = i965_QuerySurfaceStatus;
7616     vtable->vaPutSurface = i965_PutSurface;
7617     vtable->vaQueryImageFormats = i965_QueryImageFormats;
7618     vtable->vaCreateImage = i965_CreateImage;
7619     vtable->vaDeriveImage = i965_DeriveImage;
7620     vtable->vaDestroyImage = i965_DestroyImage;
7621     vtable->vaSetImagePalette = i965_SetImagePalette;
7622     vtable->vaGetImage = i965_GetImage;
7623     vtable->vaPutImage = i965_PutImage;
7624     vtable->vaQuerySubpictureFormats = i965_QuerySubpictureFormats;
7625     vtable->vaCreateSubpicture = i965_CreateSubpicture;
7626     vtable->vaDestroySubpicture = i965_DestroySubpicture;
7627     vtable->vaSetSubpictureImage = i965_SetSubpictureImage;
7628     vtable->vaSetSubpictureChromakey = i965_SetSubpictureChromakey;
7629     vtable->vaSetSubpictureGlobalAlpha = i965_SetSubpictureGlobalAlpha;
7630     vtable->vaAssociateSubpicture = i965_AssociateSubpicture;
7631     vtable->vaDeassociateSubpicture = i965_DeassociateSubpicture;
7632     vtable->vaQueryDisplayAttributes = i965_QueryDisplayAttributes;
7633     vtable->vaGetDisplayAttributes = i965_GetDisplayAttributes;
7634     vtable->vaSetDisplayAttributes = i965_SetDisplayAttributes;
7635     vtable->vaBufferInfo = i965_BufferInfo;
7636     vtable->vaLockSurface = i965_LockSurface;
7637     vtable->vaUnlockSurface = i965_UnlockSurface;
7638     vtable->vaGetSurfaceAttributes = i965_GetSurfaceAttributes;
7639     vtable->vaQuerySurfaceAttributes = i965_QuerySurfaceAttributes;
7640     vtable->vaCreateSurfaces2 = i965_CreateSurfaces2;
7641
7642     /* 0.36.0 */
7643     vtable->vaAcquireBufferHandle = i965_AcquireBufferHandle;
7644     vtable->vaReleaseBufferHandle = i965_ReleaseBufferHandle;
7645
7646     /* 1.0.0 */
7647     vtable->vaExportSurfaceHandle = i965_ExportSurfaceHandle;
7648
7649     vtable_vpp->vaQueryVideoProcFilters = i965_QueryVideoProcFilters;
7650     vtable_vpp->vaQueryVideoProcFilterCaps = i965_QueryVideoProcFilterCaps;
7651     vtable_vpp->vaQueryVideoProcPipelineCaps = i965_QueryVideoProcPipelineCaps;
7652
7653     i965 = (struct i965_driver_data *)calloc(1, sizeof(*i965));
7654
7655     if (i965 == NULL) {
7656         ctx->pDriverData = NULL;
7657
7658         return VA_STATUS_ERROR_ALLOCATION_FAILED;
7659     }
7660
7661     i965->wrapper_pdrvctx = NULL;
7662     ctx->pDriverData = (void *)i965;
7663     ret = i965_Init(ctx);
7664
7665     if (ret == VA_STATUS_SUCCESS) {
7666         ctx->str_vendor = i965->va_vendor;
7667     } else {
7668         free(i965);
7669         ctx->pDriverData = NULL;
7670     }
7671
7672     return ret;
7673 }