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