1/*
2 * Copyright (c) 2011 Intel Corporation. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 *    Fei Jiang  <fei.jiang@intel.com>
26 *    Austin Yuan <austin.yuan@intel.com>
27 *
28 */
29
30#include "android/psb_gralloc.h"
31#include <cutils/log.h>
32#include <utils/threads.h>
33#include <ui/PixelFormat.h>
34#include <hardware/gralloc.h>
35#include <system/graphics.h>
36#include <hardware/hardware.h>
37#ifdef BAYTRAIL
38#include <ufo/gralloc.h>
39#else
40#include <hal/hal_public.h>
41#include <sync/sync.h>
42#endif
43
44using namespace android;
45
46#ifdef  LOG_TAG
47#undef  LOG_TAG
48#endif
49
50#define LOG_TAG "pvr_drv_video"
51
52#ifdef BAYTRAIL
53static const gralloc_module_t *mGralloc;
54#else
55static const hw_device_t *mGralloc;
56#endif
57
58int gralloc_lock(buffer_handle_t handle,
59                 int usage, int left, int top, int width, int height,
60                 void** vaddr)
61{
62    int err, j;
63
64    if (!mGralloc) {
65        ALOGW("%s: gralloc module has not been initialized. Should initialize it first", __func__);
66        if (gralloc_init()) {
67            ALOGE("%s: can't find the %s module", __func__, GRALLOC_HARDWARE_MODULE_ID);
68            return -1;
69        }
70    }
71
72#ifdef BAYTRAIL
73    err = mGralloc->lock(mGralloc, handle, usage,
74                         left, top, width, height,
75                         vaddr);
76#else
77    const gralloc1_rect_t r = {
78        .left   = left,
79        .top    = top,
80        .width  = width,
81        .height = height
82    };
83    err = gralloc_lock_async_img(mGralloc, handle, usage, &r, vaddr, -1);
84#endif
85    ALOGV("gralloc_lock: handle is %p, usage is %x, vaddr is %p.\n", handle, usage, *vaddr);
86    if (err){
87        ALOGE("lock(...) failed %d (%s).\n", err, strerror(-err));
88        return -1;
89    } else {
90        ALOGV("lock returned with address %p\n", *vaddr);
91    }
92
93    return err;
94}
95
96int gralloc_unlock(buffer_handle_t handle)
97{
98    int err;
99
100    if (!mGralloc) {
101        ALOGW("%s: gralloc module has not been initialized. Should initialize it first", __func__);
102        if (gralloc_init()) {
103            ALOGE("%s: can't find the %s module", __func__, GRALLOC_HARDWARE_MODULE_ID);
104            return -1;
105        }
106    }
107
108#ifdef BAYTRAIL
109    err = mGralloc->unlock(mGralloc, handle);
110#else
111    int releaseFence = -1;
112    err = gralloc_unlock_async_img(mGralloc, handle, &releaseFence);
113    if (releaseFence >= 0) {
114        sync_wait(releaseFence, -1);
115        close(releaseFence);
116    }
117#endif
118    if (err) {
119        ALOGE("unlock(...) failed %d (%s)", err, strerror(-err));
120        return -1;
121    } else {
122        ALOGV("unlock returned\n");
123    }
124
125    return err;
126}
127
128int gralloc_register(buffer_handle_t handle)
129{
130    int err = 0;
131
132    if (!mGralloc) {
133        ALOGW("%s: gralloc module has not been initialized.", __func__);
134        if (gralloc_init()) {
135            ALOGE("%s: can't find the %s module", __func__,
136                    GRALLOC_HARDWARE_MODULE_ID);
137            return -1;
138        }
139    }
140
141    err = gralloc_register_img(mGralloc, handle);
142    if (err) {
143        ALOGE("%s failed with %d (%s).\n", __func__, err, strerror(-err));
144        return -1;
145    } else {
146        ALOGV("registered buffer %p successfully\n", handle);
147    }
148
149    return err;
150}
151
152int gralloc_unregister(buffer_handle_t handle)
153{
154    int err = 0;
155
156    if (!mGralloc) {
157        ALOGW("%s: gralloc module has not been initialized.", __func__);
158        if (gralloc_init()) {
159            ALOGE("%s: can't find the %s module", __func__,
160                    GRALLOC_HARDWARE_MODULE_ID);
161            return -1;
162        }
163    }
164
165    err = gralloc_unregister_img(mGralloc, handle);
166    if (err) {
167        ALOGE("%s failed with %d (%s).\n", __func__, err, strerror(-err));
168        return -1;
169    } else {
170        ALOGV("unregistered buffer %p successfully\n", handle);
171    }
172
173    return err;
174}
175
176int gralloc_init(void)
177{
178    int err;
179
180#ifdef BAYTRAIL
181    err = hw_get_module(GRALLOC_HW_MODULE_ID, (const hw_module_t **)&mGralloc);
182#else
183    err = gralloc_open_img(&mGralloc);
184#endif
185    if (err) {
186        ALOGE("FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
187        return -1;
188    } else
189        ALOGD("hw_get_module returned\n");
190
191    return 0;
192}
193
194int gralloc_getdisplaystatus(buffer_handle_t handle,  int* status)
195{
196    int err;
197
198#ifdef BAYTRAIL
199    *status = mGralloc->perform(mGralloc, INTEL_UFO_GRALLOC_MODULE_PERFORM_GET_BO_STATUS, handle);
200    err = 0;
201#else
202    uint32_t _status = 0U;
203    err = gralloc_get_display_status_img(mGralloc, handle, &_status);
204    *status = (int)_status;
205#endif
206    if (err){
207        ALOGE("gralloc_getdisplaystatus(...) failed %d (%s).\n", err, strerror(-err));
208        return -1;
209    }
210
211    return err;
212}
213
214int gralloc_getbuffd(buffer_handle_t handle)
215{
216    return ((IMG_native_handle_t*)handle)->fd[0];
217}
218