• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_register(buffer_handle_t handle)132 int gralloc_register(buffer_handle_t handle)
133 {
134     int err = 0;
135 
136     if (!mAllocMod) {
137         ALOGW("%s: gralloc module has not been initialized.", __func__);
138         if (gralloc_init()) {
139             ALOGE("%s: can't find the %s module", __func__,
140                     GRALLOC_HARDWARE_MODULE_ID);
141             return -1;
142         }
143     }
144 
145     err = mAllocMod->registerBuffer(mAllocMod, handle);
146     if (err) {
147         ALOGE("%s failed with %d (%s).\n", __func__, err, strerror(-err));
148         return -1;
149     } else {
150         ALOGV("registered buffer %p successfully\n", handle);
151     }
152 
153     return err;
154 }
155 
gralloc_unregister(buffer_handle_t handle)156 int gralloc_unregister(buffer_handle_t handle)
157 {
158     int err = 0;
159 
160     if (!mAllocMod) {
161         ALOGW("%s: gralloc module has not been initialized.", __func__);
162         if (gralloc_init()) {
163             ALOGE("%s: can't find the %s module", __func__,
164                     GRALLOC_HARDWARE_MODULE_ID);
165             return -1;
166         }
167     }
168 
169     err = mAllocMod->unregisterBuffer(mAllocMod, handle);
170     if (err) {
171         ALOGE("%s failed with %d (%s).\n", __func__, err, strerror(-err));
172         return -1;
173     } else {
174         ALOGV("unregistered buffer %p successfully\n", handle);
175     }
176 
177     return err;
178 }
179 
gralloc_init(void)180 int gralloc_init(void)
181 {
182     int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
183     if (err) {
184         ALOGE("FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
185         return -1;
186     } else
187         ALOGD("hw_get_module returned\n");
188     mAllocMod = (gralloc_module_t *)module;
189 
190     return 0;
191 }
192 
gralloc_getdisplaystatus(buffer_handle_t handle,int * status)193 int gralloc_getdisplaystatus(buffer_handle_t handle,  int* status)
194 {
195     int err;
196 #ifndef BAYTRAIL
197     uint32_t _status = 0U;
198     err = mAllocMod->perform(mAllocMod, GRALLOC_MODULE_GET_DISPLAY_STATUS_IMG, handle, &_status);
199     *status = (int)_status;
200 #else
201     err = 0;
202     *status = mAllocMod->perform(mAllocMod, INTEL_UFO_GRALLOC_MODULE_PERFORM_GET_BO_STATUS, handle);
203 #endif
204     if (err){
205         ALOGE("gralloc_getdisplaystatus(...) failed %d (%s).\n", err, strerror(-err));
206         return -1;
207     }
208 
209     return err;
210 }
211 
gralloc_getbuffd(buffer_handle_t handle)212 int gralloc_getbuffd(buffer_handle_t handle)
213 {
214     return ((IMG_native_handle_t*)handle)->fd[0];
215 }
216