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#endif
40using namespace android;
41
42#ifdef  LOG_TAG
43#undef  LOG_TAG
44#endif
45
46#define LOG_TAG "pvr_drv_video"
47
48static hw_module_t const *module;
49static gralloc_module_t *mAllocMod; /* get by force hw_module_t */
50
51int gralloc_lock(buffer_handle_t handle,
52                int usage, int left, int top, int width, int height,
53                void** vaddr)
54{
55    int err, j;
56
57    if (!mAllocMod) {
58        ALOGW("%s: gralloc module has not been initialized. Should initialize it first", __func__);
59        if (gralloc_init()) {
60            ALOGE("%s: can't find the %s module", __func__, GRALLOC_HARDWARE_MODULE_ID);
61            return -1;
62        }
63    }
64
65    err = mAllocMod->lock(mAllocMod, handle, usage,
66                          left, top, width, height,
67                          vaddr);
68    ALOGV("gralloc_lock: handle is %p, usage is %x, vaddr is %p.\n", handle, usage, *vaddr);
69
70//#ifdef BAYTRAIL
71#if 0
72    unsigned char *tmp_buffer = (unsigned char *)(*vaddr);
73    int dst_stride;
74    if (width <= 512)
75        dst_stride = 512;
76    else if (width <= 1024)
77        dst_stride = 1024;
78    else if (width <= 1280)
79        dst_stride = 1280;
80    else if (width <= 2048)
81        dst_stride = 2048;
82
83    int align_h = 32;
84    int dsth = (height + align_h - 1) & ~(align_h - 1);
85    LOGD("width is %d, dst_stride is %d, dsth is %d.\n",
86         width, dst_stride, dsth);
87
88    for (j = 0; j < dst_stride * dsth * 3 / 2; j = j + 4096) {
89        *(tmp_buffer + j) = 0xa5;
90        if (*(tmp_buffer + j) !=  0xa5)
91            LOGE("access page failed, width is %d, dst_stride is %d, dsth is %d.\n",
92                 width, dst_stride, dsth);
93    }
94#endif
95    if (err){
96        ALOGE("lock(...) failed %d (%s).\n", err, strerror(-err));
97        return -1;
98    } else {
99        ALOGV("lock returned with address %p\n", *vaddr);
100    }
101
102    return err;
103}
104
105int gralloc_unlock(buffer_handle_t handle)
106{
107    int err;
108
109    if (!mAllocMod) {
110        ALOGW("%s: gralloc module has not been initialized. Should initialize it first", __func__);
111        if (gralloc_init()) {
112            ALOGE("%s: can't find the %s module", __func__, GRALLOC_HARDWARE_MODULE_ID);
113            return -1;
114        }
115    }
116
117    err = mAllocMod->unlock(mAllocMod, handle);
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_init(void)
129{
130    int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
131    if (err) {
132        ALOGE("FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
133        return -1;
134    } else
135        ALOGD("hw_get_module returned\n");
136    mAllocMod = (gralloc_module_t *)module;
137
138    return 0;
139}
140
141int gralloc_getdisplaystatus(buffer_handle_t handle,  int* status)
142{
143    int err;
144#ifndef BAYTRAIL
145    int (*get_display_status)(gralloc_module_t*, buffer_handle_t, int*);
146
147    get_display_status = (int (*)(gralloc_module_t*, buffer_handle_t, int*))(mAllocMod->reserved_proc[0]);
148    if (get_display_status == NULL) {
149        ALOGE("can't get gralloc_getdisplaystatus(...) \n");
150        return -1;
151    }
152    err = (*get_display_status)(mAllocMod, handle, status);
153#else
154    err = 0;
155    *status = mAllocMod->perform(mAllocMod, INTEL_UFO_GRALLOC_MODULE_PERFORM_GET_BO_STATUS, handle);
156#endif
157    if (err){
158        ALOGE("gralloc_getdisplaystatus(...) failed %d (%s).\n", err, strerror(-err));
159        return -1;
160    }
161
162    return err;
163}
164