aboutsummaryrefslogtreecommitdiff
path: root/src/android/psb_gralloc.cpp
blob: a920fd51eb88e81ec2dfc22899b0ff67f89daabb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
 * Copyright (c) 2011 Intel Corporation. All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Fei Jiang  <fei.jiang@intel.com>
 *    Austin Yuan <austin.yuan@intel.com>
 *
 */

#include "android/psb_gralloc.h"
#include <cutils/log.h>
#include <utils/threads.h>
#include <ui/PixelFormat.h>
#include <hardware/gralloc.h>
#include <system/graphics.h>
#include <hardware/hardware.h>
#ifdef BAYTRAIL
#include <ufo/gralloc.h>
#endif
#ifndef BAYTRAIL
#include <hal/hal_public.h>
#endif

using namespace android;

#ifdef  LOG_TAG
#undef  LOG_TAG
#endif

#define LOG_TAG "pvr_drv_video"

static hw_module_t const *module;
static gralloc_module_t *mAllocMod; /* get by force hw_module_t */

int gralloc_lock(buffer_handle_t handle,
                int usage, int left, int top, int width, int height,
                void** vaddr)
{
    int err, j;

    if (!mAllocMod) {
        ALOGW("%s: gralloc module has not been initialized. Should initialize it first", __func__);
        if (gralloc_init()) {
            ALOGE("%s: can't find the %s module", __func__, GRALLOC_HARDWARE_MODULE_ID);
            return -1;
        }
    }

    err = mAllocMod->lock(mAllocMod, handle, usage,
                          left, top, width, height,
                          vaddr);
    ALOGV("gralloc_lock: handle is %p, usage is %x, vaddr is %p.\n", handle, usage, *vaddr);

//#ifdef BAYTRAIL
#if 0
    unsigned char *tmp_buffer = (unsigned char *)(*vaddr);
    int dst_stride;
    if (width <= 512)
        dst_stride = 512;
    else if (width <= 1024)
        dst_stride = 1024;
    else if (width <= 1280)
        dst_stride = 1280;
    else if (width <= 2048)
        dst_stride = 2048;

    int align_h = 32;
    int dsth = (height + align_h - 1) & ~(align_h - 1);
    LOGD("width is %d, dst_stride is %d, dsth is %d.\n",
         width, dst_stride, dsth);

    for (j = 0; j < dst_stride * dsth * 3 / 2; j = j + 4096) {
        *(tmp_buffer + j) = 0xa5;
        if (*(tmp_buffer + j) !=  0xa5)
            LOGE("access page failed, width is %d, dst_stride is %d, dsth is %d.\n",
                 width, dst_stride, dsth);
    }
#endif
    if (err){
        ALOGE("lock(...) failed %d (%s).\n", err, strerror(-err));
        return -1;
    } else {
        ALOGV("lock returned with address %p\n", *vaddr);
    }

    return err;
}

int gralloc_unlock(buffer_handle_t handle)
{
    int err;

    if (!mAllocMod) {
        ALOGW("%s: gralloc module has not been initialized. Should initialize it first", __func__);
        if (gralloc_init()) {
            ALOGE("%s: can't find the %s module", __func__, GRALLOC_HARDWARE_MODULE_ID);
            return -1;
        }
    }

    err = mAllocMod->unlock(mAllocMod, handle);
    if (err) {
        ALOGE("unlock(...) failed %d (%s)", err, strerror(-err));
        return -1;
    } else {
        ALOGV("unlock returned\n");
    }

    return err;
}

int gralloc_register(buffer_handle_t handle)
{
    int err = 0;

    if (!mAllocMod) {
        ALOGW("%s: gralloc module has not been initialized.", __func__);
        if (gralloc_init()) {
            ALOGE("%s: can't find the %s module", __func__,
                    GRALLOC_HARDWARE_MODULE_ID);
            return -1;
        }
    }

    err = mAllocMod->registerBuffer(mAllocMod, handle);
    if (err) {
        ALOGE("%s failed with %d (%s).\n", __func__, err, strerror(-err));
        return -1;
    } else {
        ALOGV("registered buffer %p successfully\n", handle);
    }

    return err;
}

int gralloc_unregister(buffer_handle_t handle)
{
    int err = 0;

    if (!mAllocMod) {
        ALOGW("%s: gralloc module has not been initialized.", __func__);
        if (gralloc_init()) {
            ALOGE("%s: can't find the %s module", __func__,
                    GRALLOC_HARDWARE_MODULE_ID);
            return -1;
        }
    }

    err = mAllocMod->unregisterBuffer(mAllocMod, handle);
    if (err) {
        ALOGE("%s failed with %d (%s).\n", __func__, err, strerror(-err));
        return -1;
    } else {
        ALOGV("unregistered buffer %p successfully\n", handle);
    }

    return err;
}

int gralloc_init(void)
{
    int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
    if (err) {
        ALOGE("FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
        return -1;
    } else
        ALOGD("hw_get_module returned\n");
    mAllocMod = (gralloc_module_t *)module;

    return 0;
}

int gralloc_getdisplaystatus(buffer_handle_t handle,  int* status)
{
    int err;
#ifndef BAYTRAIL
    uint32_t _status = 0U;
    err = mAllocMod->perform(mAllocMod, GRALLOC_MODULE_GET_DISPLAY_STATUS_IMG, handle, &_status);
    *status = (int)_status;
#else
    err = 0;
    *status = mAllocMod->perform(mAllocMod, INTEL_UFO_GRALLOC_MODULE_PERFORM_GET_BO_STATUS, handle);
#endif
    if (err){
        ALOGE("gralloc_getdisplaystatus(...) failed %d (%s).\n", err, strerror(-err));
        return -1;
    }

    return err;
}

int gralloc_getbuffd(buffer_handle_t handle)
{
    return ((IMG_native_handle_t*)handle)->fd[0];
}