1f31d5416a60f83e184b0906a7ec77ba021840531hding/*
2f31d5416a60f83e184b0906a7ec77ba021840531hding * Copyright (c) 2011 Intel Corporation. All Rights Reserved.
3f31d5416a60f83e184b0906a7ec77ba021840531hding *
4f31d5416a60f83e184b0906a7ec77ba021840531hding * Permission is hereby granted, free of charge, to any person obtaining a
5f31d5416a60f83e184b0906a7ec77ba021840531hding * copy of this software and associated documentation files (the
6f31d5416a60f83e184b0906a7ec77ba021840531hding * "Software"), to deal in the Software without restriction, including
7f31d5416a60f83e184b0906a7ec77ba021840531hding * without limitation the rights to use, copy, modify, merge, publish,
8f31d5416a60f83e184b0906a7ec77ba021840531hding * distribute, sub license, and/or sell copies of the Software, and to
9f31d5416a60f83e184b0906a7ec77ba021840531hding * permit persons to whom the Software is furnished to do so, subject to
10f31d5416a60f83e184b0906a7ec77ba021840531hding * the following conditions:
11f31d5416a60f83e184b0906a7ec77ba021840531hding *
12f31d5416a60f83e184b0906a7ec77ba021840531hding * The above copyright notice and this permission notice (including the
13f31d5416a60f83e184b0906a7ec77ba021840531hding * next paragraph) shall be included in all copies or substantial portions
14f31d5416a60f83e184b0906a7ec77ba021840531hding * of the Software.
15f31d5416a60f83e184b0906a7ec77ba021840531hding *
16f31d5416a60f83e184b0906a7ec77ba021840531hding * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17f31d5416a60f83e184b0906a7ec77ba021840531hding * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18f31d5416a60f83e184b0906a7ec77ba021840531hding * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19f31d5416a60f83e184b0906a7ec77ba021840531hding * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20f31d5416a60f83e184b0906a7ec77ba021840531hding * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21f31d5416a60f83e184b0906a7ec77ba021840531hding * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22f31d5416a60f83e184b0906a7ec77ba021840531hding * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23f31d5416a60f83e184b0906a7ec77ba021840531hding *
24f31d5416a60f83e184b0906a7ec77ba021840531hding */
25f31d5416a60f83e184b0906a7ec77ba021840531hding
26f31d5416a60f83e184b0906a7ec77ba021840531hding#include <sys/mman.h>
27f31d5416a60f83e184b0906a7ec77ba021840531hding#include <va/va_tpi.h>
28f31d5416a60f83e184b0906a7ec77ba021840531hding#include "psb_drv_video.h"
29f31d5416a60f83e184b0906a7ec77ba021840531hding#include "psb_drv_debug.h"
30f31d5416a60f83e184b0906a7ec77ba021840531hding#include "psb_surface.h"
31cecb10be5449aa74cd1d9a2eb41c2a6a76d9ee79ywan#include "psb_surface_attrib.h"
32f31d5416a60f83e184b0906a7ec77ba021840531hding
33f31d5416a60f83e184b0906a7ec77ba021840531hding#include <gralloc.h>
34f31d5416a60f83e184b0906a7ec77ba021840531hding#include "android/psb_gralloc.h"
35f31d5416a60f83e184b0906a7ec77ba021840531hding#include "android/psb_android_glue.h"
36ca854bdaae4b5d5c28d9c492d0b4f3a563d57703Zhao Liang#ifndef BAYTRAIL
37ca854bdaae4b5d5c28d9c492d0b4f3a563d57703Zhao Liang#include <hal/hal_public.h>
38ca854bdaae4b5d5c28d9c492d0b4f3a563d57703Zhao Liang#endif
39cecb10be5449aa74cd1d9a2eb41c2a6a76d9ee79ywan#include <wsbm/wsbm_manager.h>
40f31d5416a60f83e184b0906a7ec77ba021840531hding
41f31d5416a60f83e184b0906a7ec77ba021840531hding#define INIT_DRIVER_DATA    psb_driver_data_p driver_data = (psb_driver_data_p) ctx->pDriverData;
42f31d5416a60f83e184b0906a7ec77ba021840531hding#define CONFIG(id)  ((object_config_p) object_heap_lookup( &driver_data->config_heap, id ))
43f31d5416a60f83e184b0906a7ec77ba021840531hding#define CONTEXT(id) ((object_context_p) object_heap_lookup( &driver_data->context_heap, id ))
44f31d5416a60f83e184b0906a7ec77ba021840531hding#define SURFACE(id)    ((object_surface_p) object_heap_lookup( &driver_data->surface_heap, id ))
45f31d5416a60f83e184b0906a7ec77ba021840531hding#define BUFFER(id)  ((object_buffer_p) object_heap_lookup( &driver_data->buffer_heap, id ))
46d640886ce44885e2a70f4d31187d17f4ce08d52dXigui Wang#define SHARE_INFO_INIT_VALUE   0x12345678
47f31d5416a60f83e184b0906a7ec77ba021840531hding
48d50cf45fbdb02245b8d45b14935fc5f2f4575653Thierry Strudelstatic pthread_mutex_t gralloc_mutex = PTHREAD_MUTEX_INITIALIZER;
49d50cf45fbdb02245b8d45b14935fc5f2f4575653Thierry Strudel
502db64e390f8b1a9ac4cfea98d89f45ec94d11d65Jason Hu/*FIXME: include hal_public.h instead of define it here*/
512db64e390f8b1a9ac4cfea98d89f45ec94d11d65Jason Huenum {
522db64e390f8b1a9ac4cfea98d89f45ec94d11d65Jason Hu    GRALLOC_SUB_BUFFER0 = 0,
532db64e390f8b1a9ac4cfea98d89f45ec94d11d65Jason Hu    GRALLOC_SUB_BUFFER1,
542db64e390f8b1a9ac4cfea98d89f45ec94d11d65Jason Hu    GRALLOC_SUB_BUFFER2,
552db64e390f8b1a9ac4cfea98d89f45ec94d11d65Jason Hu    GRALLOC_SUB_BUFFER_MAX,
562db64e390f8b1a9ac4cfea98d89f45ec94d11d65Jason Hu};
57f31d5416a60f83e184b0906a7ec77ba021840531hding
58f31d5416a60f83e184b0906a7ec77ba021840531hdingVAStatus psb_DestroySurfaceGralloc(object_surface_p obj_surface)
59f31d5416a60f83e184b0906a7ec77ba021840531hding{
602db64e390f8b1a9ac4cfea98d89f45ec94d11d65Jason Hu    void *vaddr[GRALLOC_SUB_BUFFER_MAX];
616028b94d5c9bc36237c553878e7fe66ee74fe908wfeng    int usage = GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_COMPOSER;
62f31d5416a60f83e184b0906a7ec77ba021840531hding    buffer_handle_t handle = obj_surface->psb_surface->buf.handle;
6306e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu    VAStatus vaStatus = VA_STATUS_SUCCESS;
64928d8fd5cd50f8f0d07545942e006deb14a489e6mahongpe
65928d8fd5cd50f8f0d07545942e006deb14a489e6mahongpe#ifdef PSBVIDEO_MRFL
66928d8fd5cd50f8f0d07545942e006deb14a489e6mahongpe    usage |= GRALLOC_USAGE_SW_WRITE_OFTEN;
67928d8fd5cd50f8f0d07545942e006deb14a489e6mahongpe#endif
68928d8fd5cd50f8f0d07545942e006deb14a489e6mahongpe
69d50cf45fbdb02245b8d45b14935fc5f2f4575653Thierry Strudel    pthread_mutex_lock(&gralloc_mutex);
70f31d5416a60f83e184b0906a7ec77ba021840531hding    if (!gralloc_lock(handle, usage, 0, 0,
712db64e390f8b1a9ac4cfea98d89f45ec94d11d65Jason Hu                      obj_surface->width, obj_surface->height, (void **)&vaddr[GRALLOC_SUB_BUFFER0])){
72595e050675b1d8ecd0d6c17d6729bb663fa5fdceZhao Liang        if (obj_surface->share_info && vaddr[GRALLOC_SUB_BUFFER1] == obj_surface->share_info) {
73b7f89c9efe4cecf957dee1f1d9f58dc1954746cfDan Liang            int metadata_rotate = obj_surface->share_info->metadata_rotate;
74b7f89c9efe4cecf957dee1f1d9f58dc1954746cfDan Liang            int surface_protected = obj_surface->share_info->surface_protected;
75b7f89c9efe4cecf957dee1f1d9f58dc1954746cfDan Liang            int force_output_method = obj_surface->share_info->force_output_method;
76b7f89c9efe4cecf957dee1f1d9f58dc1954746cfDan Liang            int bob_deinterlace = obj_surface->share_info->bob_deinterlace;
77b7f89c9efe4cecf957dee1f1d9f58dc1954746cfDan Liang
78f31d5416a60f83e184b0906a7ec77ba021840531hding            memset(obj_surface->share_info, 0, sizeof(struct psb_surface_share_info_s));
79b7f89c9efe4cecf957dee1f1d9f58dc1954746cfDan Liang            /* Still need to keep these info so that hwc can get them after suspend/resume cycle */
80b7f89c9efe4cecf957dee1f1d9f58dc1954746cfDan Liang            obj_surface->share_info->metadata_rotate = metadata_rotate;
81b7f89c9efe4cecf957dee1f1d9f58dc1954746cfDan Liang            obj_surface->share_info->surface_protected = surface_protected;
82b7f89c9efe4cecf957dee1f1d9f58dc1954746cfDan Liang            obj_surface->share_info->force_output_method = force_output_method;
83b7f89c9efe4cecf957dee1f1d9f58dc1954746cfDan Liang            obj_surface->share_info->bob_deinterlace = bob_deinterlace;
84f31d5416a60f83e184b0906a7ec77ba021840531hding        }
85f31d5416a60f83e184b0906a7ec77ba021840531hding        gralloc_unlock(handle);
8606e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu
8706e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu        if (gralloc_unregister(handle))
8806e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu            vaStatus = VA_STATUS_ERROR_UNKNOWN;
89f31d5416a60f83e184b0906a7ec77ba021840531hding    }
90d50cf45fbdb02245b8d45b14935fc5f2f4575653Thierry Strudel    pthread_mutex_unlock(&gralloc_mutex);
91f31d5416a60f83e184b0906a7ec77ba021840531hding
9206e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu    return vaStatus;
93f31d5416a60f83e184b0906a7ec77ba021840531hding}
94f31d5416a60f83e184b0906a7ec77ba021840531hding
95b7c527a64448cfeedb2fe114b673af667f92faf2hding#ifdef BAYTRAIL
96b7c527a64448cfeedb2fe114b673af667f92faf2hdingVAStatus psb_CreateSurfacesFromGralloc(
97b7c527a64448cfeedb2fe114b673af667f92faf2hding    VADriverContextP ctx,
98b7c527a64448cfeedb2fe114b673af667f92faf2hding    int width,
99b7c527a64448cfeedb2fe114b673af667f92faf2hding    int height,
100b7c527a64448cfeedb2fe114b673af667f92faf2hding    int format,
101b7c527a64448cfeedb2fe114b673af667f92faf2hding    int num_surfaces,
102b7c527a64448cfeedb2fe114b673af667f92faf2hding    VASurfaceID *surface_list,        /* out */
103cecb10be5449aa74cd1d9a2eb41c2a6a76d9ee79ywan    PsbSurfaceAttributeTPI *attribute_tpi
104b7c527a64448cfeedb2fe114b673af667f92faf2hding)
105b7c527a64448cfeedb2fe114b673af667f92faf2hding{
106b7c527a64448cfeedb2fe114b673af667f92faf2hding    INIT_DRIVER_DATA
107b7c527a64448cfeedb2fe114b673af667f92faf2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
108b7c527a64448cfeedb2fe114b673af667f92faf2hding    int i, height_origin, usage, buffer_stride = 0;
109b7c527a64448cfeedb2fe114b673af667f92faf2hding    int protected = (VA_RT_FORMAT_PROTECTED & format);
110b7c527a64448cfeedb2fe114b673af667f92faf2hding    unsigned long fourcc;
111b7c527a64448cfeedb2fe114b673af667f92faf2hding    VASurfaceAttributeTPI *external_buffers = NULL;
112030d8371cbd338afae0c5a7e753b6c890eaa34c3Fei Jiang    unsigned long handle;
113b7c527a64448cfeedb2fe114b673af667f92faf2hding    int size = num_surfaces * sizeof(unsigned int);
114b7c527a64448cfeedb2fe114b673af667f92faf2hding    void *vaddr;
115b7c527a64448cfeedb2fe114b673af667f92faf2hding
116b7c527a64448cfeedb2fe114b673af667f92faf2hding
117b7c527a64448cfeedb2fe114b673af667f92faf2hding    /* follow are gralloc-buffers */
118b7c527a64448cfeedb2fe114b673af667f92faf2hding    format = format & (~VA_RT_FORMAT_PROTECTED);
119b7c527a64448cfeedb2fe114b673af667f92faf2hding    driver_data->protected = protected;
120b7c527a64448cfeedb2fe114b673af667f92faf2hding
121b7c527a64448cfeedb2fe114b673af667f92faf2hding    CHECK_INVALID_PARAM(num_surfaces <= 0);
122b7c527a64448cfeedb2fe114b673af667f92faf2hding    CHECK_SURFACE(surface_list);
123b7c527a64448cfeedb2fe114b673af667f92faf2hding
124b7c527a64448cfeedb2fe114b673af667f92faf2hding    external_buffers = attribute_tpi;
125b7c527a64448cfeedb2fe114b673af667f92faf2hding
12646ec2e2bd4bfdfe667d89afad353cf3c351e705eywan    ALOGD("format is 0x%x, width is %d, height is %d, num_surfaces is %d.\n", format, width, height, num_surfaces);
127b7c527a64448cfeedb2fe114b673af667f92faf2hding    /* We only support one format */
128b7c527a64448cfeedb2fe114b673af667f92faf2hding    if ((VA_RT_FORMAT_YUV420 != format)
129b7c527a64448cfeedb2fe114b673af667f92faf2hding        && (VA_RT_FORMAT_YUV422 != format)) {
130b7c527a64448cfeedb2fe114b673af667f92faf2hding        vaStatus = VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
131b7c527a64448cfeedb2fe114b673af667f92faf2hding        DEBUG_FAILURE;
132b7c527a64448cfeedb2fe114b673af667f92faf2hding        return vaStatus;
133b7c527a64448cfeedb2fe114b673af667f92faf2hding    }
134b7c527a64448cfeedb2fe114b673af667f92faf2hding
135b7c527a64448cfeedb2fe114b673af667f92faf2hding    CHECK_INVALID_PARAM(external_buffers == NULL);
136a868322f86b75eddac71259cfc7eeaf2c466d86aTang, XiaoxiX
137b7c527a64448cfeedb2fe114b673af667f92faf2hding    /*
138b7c527a64448cfeedb2fe114b673af667f92faf2hding    vaStatus = psb__checkSurfaceDimensions(driver_data, width, height);
139b7c527a64448cfeedb2fe114b673af667f92faf2hding    CHECK_VASTATUS();
140b7c527a64448cfeedb2fe114b673af667f92faf2hding    */
141b7c527a64448cfeedb2fe114b673af667f92faf2hding    /* Adjust height to be a multiple of 32 (height of macroblock in interlaced mode) */
142b7c527a64448cfeedb2fe114b673af667f92faf2hding    height_origin = height;
143a373d8fe99a0ded9d4dba80480a4e3af8f5028feFei Jiang    height = (height + 0x1f) & ~0x1f;
14446ec2e2bd4bfdfe667d89afad353cf3c351e705eywan    ALOGD("external_buffers->pixel_format is 0x%x.\n", external_buffers->pixel_format);
145b7c527a64448cfeedb2fe114b673af667f92faf2hding    /* get native window from the reserved field */
146b7c527a64448cfeedb2fe114b673af667f92faf2hding    driver_data->native_window = (void *)external_buffers->reserved[0];
147b7c527a64448cfeedb2fe114b673af667f92faf2hding
148b7c527a64448cfeedb2fe114b673af667f92faf2hding    for (i = 0; i < num_surfaces; i++) {
149b7c527a64448cfeedb2fe114b673af667f92faf2hding        int surfaceID;
150b7c527a64448cfeedb2fe114b673af667f92faf2hding        object_surface_p obj_surface;
151b7c527a64448cfeedb2fe114b673af667f92faf2hding        psb_surface_p psb_surface;
152b7c527a64448cfeedb2fe114b673af667f92faf2hding
153b7c527a64448cfeedb2fe114b673af667f92faf2hding        surfaceID = object_heap_allocate(&driver_data->surface_heap);
154b7c527a64448cfeedb2fe114b673af667f92faf2hding        obj_surface = SURFACE(surfaceID);
155b7c527a64448cfeedb2fe114b673af667f92faf2hding        if (NULL == obj_surface) {
156b7c527a64448cfeedb2fe114b673af667f92faf2hding            vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
157b7c527a64448cfeedb2fe114b673af667f92faf2hding            DEBUG_FAILURE;
158b7c527a64448cfeedb2fe114b673af667f92faf2hding            break;
159b7c527a64448cfeedb2fe114b673af667f92faf2hding        }
160b7c527a64448cfeedb2fe114b673af667f92faf2hding        MEMSET_OBJECT(obj_surface, struct object_surface_s);
161b7c527a64448cfeedb2fe114b673af667f92faf2hding
162b7c527a64448cfeedb2fe114b673af667f92faf2hding        obj_surface->surface_id = surfaceID;
163b7c527a64448cfeedb2fe114b673af667f92faf2hding        surface_list[i] = surfaceID;
164b7c527a64448cfeedb2fe114b673af667f92faf2hding        obj_surface->context_id = -1;
165b7c527a64448cfeedb2fe114b673af667f92faf2hding        obj_surface->width = width;
166b7c527a64448cfeedb2fe114b673af667f92faf2hding        obj_surface->height = height;
167b7c527a64448cfeedb2fe114b673af667f92faf2hding        obj_surface->width_r = width;
168b7c527a64448cfeedb2fe114b673af667f92faf2hding        obj_surface->height_r = height;
169b7c527a64448cfeedb2fe114b673af667f92faf2hding        obj_surface->height_origin = height_origin;
170a868322f86b75eddac71259cfc7eeaf2c466d86aTang, XiaoxiX        obj_surface->is_ref_surface = 0;
171b7c527a64448cfeedb2fe114b673af667f92faf2hding
172b7c527a64448cfeedb2fe114b673af667f92faf2hding        psb_surface = (psb_surface_p) calloc(1, sizeof(struct psb_surface_s));
173b7c527a64448cfeedb2fe114b673af667f92faf2hding        if (NULL == psb_surface) {
174b7c527a64448cfeedb2fe114b673af667f92faf2hding            object_heap_free(&driver_data->surface_heap, (object_base_p) obj_surface);
175b7c527a64448cfeedb2fe114b673af667f92faf2hding            obj_surface->surface_id = VA_INVALID_SURFACE;
176b7c527a64448cfeedb2fe114b673af667f92faf2hding
177b7c527a64448cfeedb2fe114b673af667f92faf2hding            vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
178b7c527a64448cfeedb2fe114b673af667f92faf2hding
179b7c527a64448cfeedb2fe114b673af667f92faf2hding            DEBUG_FAILURE;
180b7c527a64448cfeedb2fe114b673af667f92faf2hding            break;
181b7c527a64448cfeedb2fe114b673af667f92faf2hding        }
182b7c527a64448cfeedb2fe114b673af667f92faf2hding
183b7c527a64448cfeedb2fe114b673af667f92faf2hding        switch (format) {
184b7c527a64448cfeedb2fe114b673af667f92faf2hding        case VA_RT_FORMAT_YUV422:
185b7c527a64448cfeedb2fe114b673af667f92faf2hding            fourcc = VA_FOURCC_YV16;
186b7c527a64448cfeedb2fe114b673af667f92faf2hding            break;
187b7c527a64448cfeedb2fe114b673af667f92faf2hding        case VA_RT_FORMAT_YUV420:
188b7c527a64448cfeedb2fe114b673af667f92faf2hding        default:
189b7c527a64448cfeedb2fe114b673af667f92faf2hding            fourcc = VA_FOURCC_NV12;
190b7c527a64448cfeedb2fe114b673af667f92faf2hding            break;
191b7c527a64448cfeedb2fe114b673af667f92faf2hding        }
192b7c527a64448cfeedb2fe114b673af667f92faf2hding
193b7c527a64448cfeedb2fe114b673af667f92faf2hding        /*hard code the gralloc buffer usage*/
194b7c527a64448cfeedb2fe114b673af667f92faf2hding        usage = GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_COMPOSER;
195b7c527a64448cfeedb2fe114b673af667f92faf2hding
196b7c527a64448cfeedb2fe114b673af667f92faf2hding        /* usage hack for byt */
197b7c527a64448cfeedb2fe114b673af667f92faf2hding        usage |= GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
198dbe20e0958ea95ce37178705e00b80fa6bff5dfeFei Jiang        /* usage hack to force pages alloc and CPU/GPU cache flush */
199dbe20e0958ea95ce37178705e00b80fa6bff5dfeFei Jiang        usage |= GRALLOC_USAGE_HW_VIDEO_ENCODER;
200b7c527a64448cfeedb2fe114b673af667f92faf2hding
201030d8371cbd338afae0c5a7e753b6c890eaa34c3Fei Jiang        handle = (unsigned long)external_buffers->buffers[i];
202d50cf45fbdb02245b8d45b14935fc5f2f4575653Thierry Strudel        pthread_mutex_lock(&gralloc_mutex);
203b7c527a64448cfeedb2fe114b673af667f92faf2hding        if (gralloc_lock(handle, usage, 0, 0, width, height, (void **)&vaddr)) {
204b7c527a64448cfeedb2fe114b673af667f92faf2hding            vaStatus = VA_STATUS_ERROR_UNKNOWN;
205b7c527a64448cfeedb2fe114b673af667f92faf2hding        } else {
206b7c527a64448cfeedb2fe114b673af667f92faf2hding            int cache_flag = PSB_USER_BUFFER_UNCACHED;
2072046ea17ddb468c845f542a88761a03b04898fd7Vinil Cheeramvelil            int buf_fd = gralloc_getbuffd(handle);
208b7c527a64448cfeedb2fe114b673af667f92faf2hding
209b7c527a64448cfeedb2fe114b673af667f92faf2hding            vaStatus = psb_surface_create_from_ub(driver_data, width, height, fourcc,
2102046ea17ddb468c845f542a88761a03b04898fd7Vinil Cheeramvelil                    external_buffers, psb_surface, vaddr, buf_fd,
211b7c527a64448cfeedb2fe114b673af667f92faf2hding                    cache_flag);
2128086c50ddd6fa200beba8d1878a4f0a4ae4de7eeFei Jiang
213b7c527a64448cfeedb2fe114b673af667f92faf2hding            psb_surface->buf.handle = handle;
214b7c527a64448cfeedb2fe114b673af667f92faf2hding            obj_surface->share_info = NULL;
215b7c527a64448cfeedb2fe114b673af667f92faf2hding            gralloc_unlock(handle);
216b7c527a64448cfeedb2fe114b673af667f92faf2hding        }
217d50cf45fbdb02245b8d45b14935fc5f2f4575653Thierry Strudel        pthread_mutex_unlock(&gralloc_mutex);
218b7c527a64448cfeedb2fe114b673af667f92faf2hding
219b7c527a64448cfeedb2fe114b673af667f92faf2hding        if (VA_STATUS_SUCCESS != vaStatus) {
220b7c527a64448cfeedb2fe114b673af667f92faf2hding            free(psb_surface);
221b7c527a64448cfeedb2fe114b673af667f92faf2hding            object_heap_free(&driver_data->surface_heap, (object_base_p) obj_surface);
222b7c527a64448cfeedb2fe114b673af667f92faf2hding            obj_surface->surface_id = VA_INVALID_SURFACE;
223b7c527a64448cfeedb2fe114b673af667f92faf2hding
224b7c527a64448cfeedb2fe114b673af667f92faf2hding            DEBUG_FAILURE;
225b7c527a64448cfeedb2fe114b673af667f92faf2hding            break;
226b7c527a64448cfeedb2fe114b673af667f92faf2hding        }
227b7c527a64448cfeedb2fe114b673af667f92faf2hding        buffer_stride = psb_surface->stride;
228a868322f86b75eddac71259cfc7eeaf2c466d86aTang, XiaoxiX#ifdef PSBVIDEO_MSVDX_DEC_TILING
229a868322f86b75eddac71259cfc7eeaf2c466d86aTang, XiaoxiX        psb_surface->extra_info[7] = external_buffers->tiling;
230a868322f86b75eddac71259cfc7eeaf2c466d86aTang, XiaoxiX#endif
231b7c527a64448cfeedb2fe114b673af667f92faf2hding        /* by default, surface fourcc is NV12 */
232b7c527a64448cfeedb2fe114b673af667f92faf2hding        psb_surface->extra_info[4] = fourcc;
233b7c527a64448cfeedb2fe114b673af667f92faf2hding        obj_surface->psb_surface = psb_surface;
234b7c527a64448cfeedb2fe114b673af667f92faf2hding    }
235b7c527a64448cfeedb2fe114b673af667f92faf2hding
236b7c527a64448cfeedb2fe114b673af667f92faf2hding    /* Error recovery */
237b7c527a64448cfeedb2fe114b673af667f92faf2hding    if (VA_STATUS_SUCCESS != vaStatus) {
238b7c527a64448cfeedb2fe114b673af667f92faf2hding        /* surface_list[i-1] was the last successful allocation */
239b7c527a64448cfeedb2fe114b673af667f92faf2hding        for (; i--;) {
240b7c527a64448cfeedb2fe114b673af667f92faf2hding            object_surface_p obj_surface = SURFACE(surface_list[i]);
241b7c527a64448cfeedb2fe114b673af667f92faf2hding            psb__destroy_surface(driver_data, obj_surface);
242b7c527a64448cfeedb2fe114b673af667f92faf2hding            surface_list[i] = VA_INVALID_SURFACE;
243b7c527a64448cfeedb2fe114b673af667f92faf2hding        }
244b7c527a64448cfeedb2fe114b673af667f92faf2hding        drv_debug_msg(VIDEO_DEBUG_ERROR, "CreateSurfaces failed\n");
245b7c527a64448cfeedb2fe114b673af667f92faf2hding
246b7c527a64448cfeedb2fe114b673af667f92faf2hding        return vaStatus;
247b7c527a64448cfeedb2fe114b673af667f92faf2hding    }
248b7c527a64448cfeedb2fe114b673af667f92faf2hding
249b7c527a64448cfeedb2fe114b673af667f92faf2hding    return vaStatus;
250b7c527a64448cfeedb2fe114b673af667f92faf2hding}
251b7c527a64448cfeedb2fe114b673af667f92faf2hding#else
252f31d5416a60f83e184b0906a7ec77ba021840531hdingVAStatus psb_CreateSurfacesFromGralloc(
253f31d5416a60f83e184b0906a7ec77ba021840531hding    VADriverContextP ctx,
254f31d5416a60f83e184b0906a7ec77ba021840531hding    int width,
255f31d5416a60f83e184b0906a7ec77ba021840531hding    int height,
256f31d5416a60f83e184b0906a7ec77ba021840531hding    int format,
257f31d5416a60f83e184b0906a7ec77ba021840531hding    int num_surfaces,
258f31d5416a60f83e184b0906a7ec77ba021840531hding    VASurfaceID *surface_list,        /* out */
259c81215b4bf0f69e83b393d14bd6ffba2ec3f0ee5Tianmi Chen    PsbSurfaceAttributeTPI *attribute_tpi
260f31d5416a60f83e184b0906a7ec77ba021840531hding)
261f31d5416a60f83e184b0906a7ec77ba021840531hding{
262f31d5416a60f83e184b0906a7ec77ba021840531hding    INIT_DRIVER_DATA
263f31d5416a60f83e184b0906a7ec77ba021840531hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
264f31d5416a60f83e184b0906a7ec77ba021840531hding    int i, height_origin, usage, buffer_stride = 0;
265f31d5416a60f83e184b0906a7ec77ba021840531hding    int protected = (VA_RT_FORMAT_PROTECTED & format);
266f31d5416a60f83e184b0906a7ec77ba021840531hding    unsigned long fourcc;
267c81215b4bf0f69e83b393d14bd6ffba2ec3f0ee5Tianmi Chen    PsbSurfaceAttributeTPI *external_buffers = NULL;
268030d8371cbd338afae0c5a7e753b6c890eaa34c3Fei Jiang    unsigned long handle;
269f31d5416a60f83e184b0906a7ec77ba021840531hding    int size = num_surfaces * sizeof(unsigned int);
2702db64e390f8b1a9ac4cfea98d89f45ec94d11d65Jason Hu    void *vaddr[GRALLOC_SUB_BUFFER_MAX];
271f31d5416a60f83e184b0906a7ec77ba021840531hding
272f31d5416a60f83e184b0906a7ec77ba021840531hding    /* follow are gralloc-buffers */
273f31d5416a60f83e184b0906a7ec77ba021840531hding    format = format & (~VA_RT_FORMAT_PROTECTED);
2745ed159b49cff6b05d923bcf00d09c67ee2ce1f80hding    driver_data->protected = protected;
2755ed159b49cff6b05d923bcf00d09c67ee2ce1f80hding
2766d37ae34fcae80f2e898b61e2506ed8e887bd16anguo    CHECK_INVALID_PARAM(num_surfaces <= 0);
2776d37ae34fcae80f2e898b61e2506ed8e887bd16anguo    CHECK_SURFACE(surface_list);
278f31d5416a60f83e184b0906a7ec77ba021840531hding
279f31d5416a60f83e184b0906a7ec77ba021840531hding    external_buffers = attribute_tpi;
280f31d5416a60f83e184b0906a7ec77ba021840531hding
281f31d5416a60f83e184b0906a7ec77ba021840531hding    /* We only support one format */
282f31d5416a60f83e184b0906a7ec77ba021840531hding    if ((VA_RT_FORMAT_YUV420 != format)
283e982f1e85b688d56a63c7e352281a182753f1e7bWang Kun        && (VA_RT_FORMAT_YUV422 != format)
284d50cf45fbdb02245b8d45b14935fc5f2f4575653Thierry Strudel        && (VA_RT_FORMAT_RGB32 != format)) {
285f31d5416a60f83e184b0906a7ec77ba021840531hding        vaStatus = VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
286f31d5416a60f83e184b0906a7ec77ba021840531hding        DEBUG_FAILURE;
287f31d5416a60f83e184b0906a7ec77ba021840531hding        return vaStatus;
288f31d5416a60f83e184b0906a7ec77ba021840531hding    }
289f31d5416a60f83e184b0906a7ec77ba021840531hding
290595e050675b1d8ecd0d6c17d6729bb663fa5fdceZhao Liang    CHECK_INVALID_PARAM(external_buffers == NULL);
291595e050675b1d8ecd0d6c17d6729bb663fa5fdceZhao Liang
292f31d5416a60f83e184b0906a7ec77ba021840531hding    /*
293f31d5416a60f83e184b0906a7ec77ba021840531hding    vaStatus = psb__checkSurfaceDimensions(driver_data, width, height);
2946d37ae34fcae80f2e898b61e2506ed8e887bd16anguo    CHECK_VASTATUS();
295f31d5416a60f83e184b0906a7ec77ba021840531hding    */
296f31d5416a60f83e184b0906a7ec77ba021840531hding    /* Adjust height to be a multiple of 32 (height of macroblock in interlaced mode) */
297f31d5416a60f83e184b0906a7ec77ba021840531hding    height_origin = height;
298ca854bdaae4b5d5c28d9c492d0b4f3a563d57703Zhao Liang
299ca854bdaae4b5d5c28d9c492d0b4f3a563d57703Zhao Liang    IMG_native_handle_t* h = (IMG_native_handle_t*)external_buffers->buffers[0];
300ca854bdaae4b5d5c28d9c492d0b4f3a563d57703Zhao Liang    int gfx_colorformat = h->iFormat;
301ca854bdaae4b5d5c28d9c492d0b4f3a563d57703Zhao Liang
302e982f1e85b688d56a63c7e352281a182753f1e7bWang Kun    if (gfx_colorformat != HAL_PIXEL_FORMAT_NV12 && format != VA_RT_FORMAT_RGB32)
303ca854bdaae4b5d5c28d9c492d0b4f3a563d57703Zhao Liang        height = (height + 0x1f) & ~0x1f;
3046d37ae34fcae80f2e898b61e2506ed8e887bd16anguo
305f31d5416a60f83e184b0906a7ec77ba021840531hding    /* get native window from the reserved field */
306f31d5416a60f83e184b0906a7ec77ba021840531hding    driver_data->native_window = (void *)external_buffers->reserved[0];
307d50cf45fbdb02245b8d45b14935fc5f2f4575653Thierry Strudel
308f31d5416a60f83e184b0906a7ec77ba021840531hding    for (i = 0; i < num_surfaces; i++) {
309f31d5416a60f83e184b0906a7ec77ba021840531hding        int surfaceID;
310f31d5416a60f83e184b0906a7ec77ba021840531hding        object_surface_p obj_surface;
311f31d5416a60f83e184b0906a7ec77ba021840531hding        psb_surface_p psb_surface;
312f31d5416a60f83e184b0906a7ec77ba021840531hding
313f31d5416a60f83e184b0906a7ec77ba021840531hding        surfaceID = object_heap_allocate(&driver_data->surface_heap);
314f31d5416a60f83e184b0906a7ec77ba021840531hding        obj_surface = SURFACE(surfaceID);
315f31d5416a60f83e184b0906a7ec77ba021840531hding        if (NULL == obj_surface) {
316f31d5416a60f83e184b0906a7ec77ba021840531hding            vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
317f31d5416a60f83e184b0906a7ec77ba021840531hding            DEBUG_FAILURE;
318f31d5416a60f83e184b0906a7ec77ba021840531hding            break;
319f31d5416a60f83e184b0906a7ec77ba021840531hding        }
320f31d5416a60f83e184b0906a7ec77ba021840531hding        MEMSET_OBJECT(obj_surface, struct object_surface_s);
321f31d5416a60f83e184b0906a7ec77ba021840531hding
322f31d5416a60f83e184b0906a7ec77ba021840531hding        obj_surface->surface_id = surfaceID;
323f31d5416a60f83e184b0906a7ec77ba021840531hding        surface_list[i] = surfaceID;
324f31d5416a60f83e184b0906a7ec77ba021840531hding        obj_surface->context_id = -1;
325f31d5416a60f83e184b0906a7ec77ba021840531hding        obj_surface->width = width;
326f31d5416a60f83e184b0906a7ec77ba021840531hding        obj_surface->height = height;
327f31d5416a60f83e184b0906a7ec77ba021840531hding        obj_surface->width_r = width;
328f31d5416a60f83e184b0906a7ec77ba021840531hding        obj_surface->height_r = height;
329f31d5416a60f83e184b0906a7ec77ba021840531hding        obj_surface->height_origin = height_origin;
330f31d5416a60f83e184b0906a7ec77ba021840531hding
331f31d5416a60f83e184b0906a7ec77ba021840531hding        psb_surface = (psb_surface_p) calloc(1, sizeof(struct psb_surface_s));
332f31d5416a60f83e184b0906a7ec77ba021840531hding        if (NULL == psb_surface) {
333f31d5416a60f83e184b0906a7ec77ba021840531hding            object_heap_free(&driver_data->surface_heap, (object_base_p) obj_surface);
334f31d5416a60f83e184b0906a7ec77ba021840531hding            obj_surface->surface_id = VA_INVALID_SURFACE;
335f31d5416a60f83e184b0906a7ec77ba021840531hding
336f31d5416a60f83e184b0906a7ec77ba021840531hding            vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
337f31d5416a60f83e184b0906a7ec77ba021840531hding
338f31d5416a60f83e184b0906a7ec77ba021840531hding            DEBUG_FAILURE;
339f31d5416a60f83e184b0906a7ec77ba021840531hding            break;
340f31d5416a60f83e184b0906a7ec77ba021840531hding        }
341f31d5416a60f83e184b0906a7ec77ba021840531hding
342f31d5416a60f83e184b0906a7ec77ba021840531hding        switch (format) {
343f31d5416a60f83e184b0906a7ec77ba021840531hding        case VA_RT_FORMAT_YUV422:
344f31d5416a60f83e184b0906a7ec77ba021840531hding            fourcc = VA_FOURCC_YV16;
345f31d5416a60f83e184b0906a7ec77ba021840531hding            break;
346d50cf45fbdb02245b8d45b14935fc5f2f4575653Thierry Strudel        case VA_RT_FORMAT_RGB32:
347e982f1e85b688d56a63c7e352281a182753f1e7bWang Kun            fourcc = VA_FOURCC_RGBA;
348e982f1e85b688d56a63c7e352281a182753f1e7bWang Kun            break;
349f31d5416a60f83e184b0906a7ec77ba021840531hding        case VA_RT_FORMAT_YUV420:
350f31d5416a60f83e184b0906a7ec77ba021840531hding        default:
351f31d5416a60f83e184b0906a7ec77ba021840531hding            fourcc = VA_FOURCC_NV12;
352f31d5416a60f83e184b0906a7ec77ba021840531hding            break;
353f31d5416a60f83e184b0906a7ec77ba021840531hding        }
3542db64e390f8b1a9ac4cfea98d89f45ec94d11d65Jason Hu
3551e3daae79c77ac457bad20d3758ab1523c79e7f3Li Zeng#ifndef PSBVIDEO_MSVDX_DEC_TILING
3561e3daae79c77ac457bad20d3758ab1523c79e7f3Li Zeng        external_buffers->tiling = 0;
357684f0e29e29ca32fad49439cfe0392417361d34dLi Zeng#endif
358f31d5416a60f83e184b0906a7ec77ba021840531hding        /*hard code the gralloc buffer usage*/
3596028b94d5c9bc36237c553878e7fe66ee74fe908wfeng        usage = GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_COMPOSER;
360595e050675b1d8ecd0d6c17d6729bb663fa5fdceZhao Liang
361ca854bdaae4b5d5c28d9c492d0b4f3a563d57703Zhao Liang        if (gfx_colorformat == HAL_PIXEL_FORMAT_NV12)
362ca854bdaae4b5d5c28d9c492d0b4f3a563d57703Zhao Liang            usage |= GRALLOC_USAGE_SW_READ_OFTEN;
363ca854bdaae4b5d5c28d9c492d0b4f3a563d57703Zhao Liang        else {
36429bf1fc0e43faa3b89b575c83a9db6f0376fa188Tianmi Chen            // video decoder allows app to read/write the buffer
36529bf1fc0e43faa3b89b575c83a9db6f0376fa188Tianmi Chen            usage |= GRALLOC_USAGE_SW_WRITE_RARELY | GRALLOC_USAGE_SW_READ_RARELY;
366ca854bdaae4b5d5c28d9c492d0b4f3a563d57703Zhao Liang        }
367595e050675b1d8ecd0d6c17d6729bb663fa5fdceZhao Liang
368030d8371cbd338afae0c5a7e753b6c890eaa34c3Fei Jiang        handle = (unsigned long)external_buffers->buffers[i];
369d50cf45fbdb02245b8d45b14935fc5f2f4575653Thierry Strudel        pthread_mutex_lock(&gralloc_mutex);
37029bf1fc0e43faa3b89b575c83a9db6f0376fa188Tianmi Chen
37106e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu        if (gralloc_register((buffer_handle_t)handle)) {
37206e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu            vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
373f31d5416a60f83e184b0906a7ec77ba021840531hding        } else {
37406e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu            if (gralloc_lock((buffer_handle_t)handle, usage, 0, 0, width, height, (void **)&vaddr[GRALLOC_SUB_BUFFER0])) {
37506e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                vaStatus = VA_STATUS_ERROR_UNKNOWN;
37606e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                gralloc_unregister((buffer_handle_t)handle);
37706e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu            } else {
37806e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                int cache_flag = PSB_USER_BUFFER_UNCACHED;
37906e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                int buf_fd = gralloc_getbuffd((buffer_handle_t)handle);
38062d3f290968580b7ed3a00e0ffb458c2f0249dacLi Zeng#ifdef PSBVIDEO_MRFL
38106e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                //cache_flag = 0;
38262d3f290968580b7ed3a00e0ffb458c2f0249dacLi Zeng#endif
38306e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                vaStatus = psb_surface_create_from_ub(driver_data, width, height, fourcc,
38406e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        (VASurfaceAttributeTPI *)external_buffers, psb_surface,
38506e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        vaddr[GRALLOC_SUB_BUFFER0], buf_fd, cache_flag);
38606e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                psb_surface->buf.handle = (void *)handle;
38706e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                obj_surface->share_info = NULL;
38806e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu
38906e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                if ((gfx_colorformat != HAL_PIXEL_FORMAT_NV12) &&
39006e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        (gfx_colorformat != HAL_PIXEL_FORMAT_YV12) &&
39106e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        (format != VA_RT_FORMAT_RGB32)) {
39206e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                    unsigned int decoder_share_info = (unsigned int)external_buffers->reserved[2];
39306e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                    drv_debug_msg(VIDEO_DEBUG_GENERAL, "%s : Create graphic buffer initialized share info %d", __FUNCTION__, decoder_share_info);
39406e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                    obj_surface->share_info = (psb_surface_share_info_t *)vaddr[GRALLOC_SUB_BUFFER1];
39506e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu
39606e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                    if (obj_surface->share_info->initialized != SHARE_INFO_INIT_VALUE) {
39706e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        memset(obj_surface->share_info, 0, sizeof(struct psb_surface_share_info_s));
39806e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        // Set clear video the default output method as OUTPUT_FORCE_OVERLAY_FOR_SW_DECODE
39906e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        // if the video can be decoded by HW, will reset the output method as 0 in psb_BeginPicture
400f31d5416a60f83e184b0906a7ec77ba021840531hding#ifdef PSBVIDEO_MSVDX_DEC_TILING
40106e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        obj_surface->share_info->tiling = external_buffers->tiling;
402f31d5416a60f83e184b0906a7ec77ba021840531hding#endif
40306e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        obj_surface->share_info->width = obj_surface->width;
40406e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        obj_surface->share_info->height = obj_surface->height_origin;
405d50cf45fbdb02245b8d45b14935fc5f2f4575653Thierry Strudel
40606e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        obj_surface->share_info->luma_stride = psb_surface->stride;
40706e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        obj_surface->share_info->chroma_u_stride = psb_surface->stride;
40806e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        obj_surface->share_info->chroma_v_stride = psb_surface->stride;
40906e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        obj_surface->share_info->format = VA_FOURCC_NV12;
410d50cf45fbdb02245b8d45b14935fc5f2f4575653Thierry Strudel
41106e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        obj_surface->share_info->khandle = (uint32_t)(wsbmKBufHandle(wsbmKBuf(psb_surface->buf.drm_buf)));
412d50cf45fbdb02245b8d45b14935fc5f2f4575653Thierry Strudel
41306e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        obj_surface->share_info->initialized = SHARE_INFO_INIT_VALUE;
41494ef7ebe0b0f58b76bf652ce74d09673c8918936Haitao Ding                    }
41594ef7ebe0b0f58b76bf652ce74d09673c8918936Haitao Ding
41606e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                    if (decoder_share_info) {
41706e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        obj_surface->share_info->force_output_method = protected ? OUTPUT_FORCE_OVERLAY : OUTPUT_FORCE_OVERLAY_FOR_SW_DECODE;
41806e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        obj_surface->share_info->native_window = (void *)external_buffers->reserved[0];
41906e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu
42006e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        attribute_tpi->reserved[1] = (unsigned long)obj_surface->share_info;
42106e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu
42206e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        if (vaddr[GRALLOC_SUB_BUFFER0] == NULL) {
42306e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                            drv_debug_msg(VIDEO_DEBUG_ERROR, "Failed to lock graphic buffer in psb_video");
42406e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        } else {
42506e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                            size = psb_surface->chroma_offset;
42606e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                            // the following memset was used to work-around Bug 19197299 on L.
42706e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                            // on DDK-1.5 we didn't observe the problem so comment it out.
42806e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                            // memset((char *)vaddr[GRALLOC_SUB_BUFFER0], 0, size);
42906e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                            // memset((char *)vaddr[GRALLOC_SUB_BUFFER0] + size, 0x80, psb_surface->size - size);
43006e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        }
43106e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        // overlay only support BT.601 and BT.709
43206e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        if (driver_data->load_csc_matrix == 1) {
43306e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                            obj_surface->share_info->csc_mode = (driver_data->is_BT601 == 1) ? 0 : 1;
43406e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        } else {
43506e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                            // if csc matrix is not set, use BT601 by default
43606e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                            obj_surface->share_info->csc_mode = 0;
43706e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        }
43806e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu
43906e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        if (driver_data->set_video_range == 1) {
44006e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                            obj_surface->share_info->video_range = driver_data->video_range;
44106e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        } else {
44206e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                            // if video range is not set, use limited range by default
44306e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                            obj_surface->share_info->video_range = 0;
44406e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        }
44506e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu
44606e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        obj_surface->share_info->surface_protected = driver_data->protected;
44706e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        if (driver_data->render_rect.width == 0 || driver_data->render_rect.height == 0) {
44806e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                            obj_surface->share_info->crop_width = obj_surface->share_info->width;
44906e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                            obj_surface->share_info->crop_height = obj_surface->share_info->height;
45006e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        } else {
45106e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                            obj_surface->share_info->crop_width = driver_data->render_rect.width;
45206e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                            obj_surface->share_info->crop_height = driver_data->render_rect.height;
45306e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        }
45406e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu
45506e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        if (obj_surface->share_info->coded_width == 0 || obj_surface->share_info->coded_height == 0) {
45606e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                            obj_surface->share_info->coded_width = (obj_surface->share_info->width + 0xf) & ~0xf;
45706e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                            obj_surface->share_info->coded_height = (obj_surface->share_info->height + 0xf) & ~0xf;
45806e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        }
45906e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu
46006e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                        drv_debug_msg(VIDEO_DEBUG_GENERAL, "%s : Create graphic buffer success"
46106e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                                "surface_id= 0x%x, vaddr[0] (0x%x), vaddr[1] (0x%x)\n",
46206e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                                __FUNCTION__, surfaceID, vaddr[GRALLOC_SUB_BUFFER0], vaddr[GRALLOC_SUB_BUFFER1]);
46394ef7ebe0b0f58b76bf652ce74d09673c8918936Haitao Ding                    }
46420f2ef257b798a174166858314731050410e31f4mahongpe                }
46506e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                gralloc_unlock((buffer_handle_t)handle);
46606e002d2cd1c3698b9ce4c96363a67e60e62707bAustin Hu                psb_surface->buf.user_ptr = NULL;
467ca854bdaae4b5d5c28d9c492d0b4f3a563d57703Zhao Liang            }
468f31d5416a60f83e184b0906a7ec77ba021840531hding        }
469d50cf45fbdb02245b8d45b14935fc5f2f4575653Thierry Strudel        pthread_mutex_unlock(&gralloc_mutex);
470d50cf45fbdb02245b8d45b14935fc5f2f4575653Thierry Strudel
471f31d5416a60f83e184b0906a7ec77ba021840531hding        if (VA_STATUS_SUCCESS != vaStatus) {
472f31d5416a60f83e184b0906a7ec77ba021840531hding            free(psb_surface);
473f31d5416a60f83e184b0906a7ec77ba021840531hding            object_heap_free(&driver_data->surface_heap, (object_base_p) obj_surface);
474f31d5416a60f83e184b0906a7ec77ba021840531hding            obj_surface->surface_id = VA_INVALID_SURFACE;
475f31d5416a60f83e184b0906a7ec77ba021840531hding
476f31d5416a60f83e184b0906a7ec77ba021840531hding            DEBUG_FAILURE;
477f31d5416a60f83e184b0906a7ec77ba021840531hding            break;
478f31d5416a60f83e184b0906a7ec77ba021840531hding        }
479f31d5416a60f83e184b0906a7ec77ba021840531hding        buffer_stride = psb_surface->stride;
480f31d5416a60f83e184b0906a7ec77ba021840531hding        /* by default, surface fourcc is NV12 */
481f31d5416a60f83e184b0906a7ec77ba021840531hding        psb_surface->extra_info[4] = fourcc;
48202f3955632048eb112d521f9c8e80ab2c911598aXigui Wang        /* save the pixel format set by application */
48302f3955632048eb112d521f9c8e80ab2c911598aXigui Wang        psb_surface->extra_info[8] = external_buffers->pixel_format;
484f31d5416a60f83e184b0906a7ec77ba021840531hding#ifdef PSBVIDEO_MSVDX_DEC_TILING
485f31d5416a60f83e184b0906a7ec77ba021840531hding        psb_surface->extra_info[7] = external_buffers->tiling;
486f31d5416a60f83e184b0906a7ec77ba021840531hding#endif
487f31d5416a60f83e184b0906a7ec77ba021840531hding        obj_surface->psb_surface = psb_surface;
488f31d5416a60f83e184b0906a7ec77ba021840531hding    }
489f31d5416a60f83e184b0906a7ec77ba021840531hding
490f31d5416a60f83e184b0906a7ec77ba021840531hding    /* Error recovery */
491f31d5416a60f83e184b0906a7ec77ba021840531hding    if (VA_STATUS_SUCCESS != vaStatus) {
492f31d5416a60f83e184b0906a7ec77ba021840531hding        /* surface_list[i-1] was the last successful allocation */
493f31d5416a60f83e184b0906a7ec77ba021840531hding        for (; i--;) {
494f31d5416a60f83e184b0906a7ec77ba021840531hding            object_surface_p obj_surface = SURFACE(surface_list[i]);
495f31d5416a60f83e184b0906a7ec77ba021840531hding            psb__destroy_surface(driver_data, obj_surface);
496f31d5416a60f83e184b0906a7ec77ba021840531hding            surface_list[i] = VA_INVALID_SURFACE;
497f31d5416a60f83e184b0906a7ec77ba021840531hding        }
498f31d5416a60f83e184b0906a7ec77ba021840531hding        drv_debug_msg(VIDEO_DEBUG_ERROR, "CreateSurfaces failed\n");
499d50cf45fbdb02245b8d45b14935fc5f2f4575653Thierry Strudel
500f31d5416a60f83e184b0906a7ec77ba021840531hding        return vaStatus;
501f31d5416a60f83e184b0906a7ec77ba021840531hding    }
502f31d5416a60f83e184b0906a7ec77ba021840531hding
503f31d5416a60f83e184b0906a7ec77ba021840531hding    return vaStatus;
504f31d5416a60f83e184b0906a7ec77ba021840531hding}
505b7c527a64448cfeedb2fe114b673af667f92faf2hding
506b7c527a64448cfeedb2fe114b673af667f92faf2hding#endif
507