xref: /OK3568_Linux_fs/external/linux-rga/core/GrallocOps.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2016 Rockchip Electronics Co., Ltd.
3  * Authors:
4  *  Zhiqin Wei <wzq@rock-chips.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #ifdef ANDROID
19 
20 #include "GrallocOps.h"
21 
22 #if USE_GRALLOC_4
23 
24 #include "platform_gralloc4.h"
25 
RkRgaGetHandleFd(buffer_handle_t handle,int * fd)26 int RkRgaGetHandleFd(buffer_handle_t handle, int *fd) {
27     int err = 0;
28 
29     err = gralloc4::get_share_fd(handle, fd);
30     if (err != android::OK)
31     {
32         ALOGE("Failed to get buffer share_fd, err : %d", err);
33         return -1;
34     }
35 
36     return err;
37 }
38 
RkRgaGetHandleAttributes(buffer_handle_t handle,std::vector<int> * attrs)39 int RkRgaGetHandleAttributes(buffer_handle_t handle,
40                              std::vector<int> *attrs) {
41     uint64_t w, h, size;
42     int pixel_stride, format;
43     int err = 0;
44 
45     err = gralloc4::get_width(handle, &w);
46     if (err != android::OK)
47     {
48         ALOGE("Failed to get buffer width, err : %d", err);
49         return -1;
50     }
51 
52     err = gralloc4::get_height(handle, &h);
53     if (err != android::OK)
54     {
55         ALOGE("Failed to get buffer height, err : %d", err);
56         return -1;
57     }
58 
59     err = gralloc4::get_pixel_stride(handle, &pixel_stride);
60     if (err != android::OK)
61     {
62         ALOGE("Failed to get buffer pixel_stride, err : %d", err);
63         return -1;
64     }
65 
66     err = gralloc4::get_format_requested(handle, &format);
67     if (err != android::OK)
68     {
69         ALOGE("Failed to get buffer format, err : %d", err);
70         return -1;
71     }
72 
73     err = gralloc4::get_allocation_size(handle, &size);
74     if (err != android::OK)
75     {
76         ALOGE("Failed to get buffer size, err : %d", err);
77         return -1;
78     }
79 
80     //add to attrs.
81     attrs->emplace_back(w);
82     attrs->emplace_back(h);
83     attrs->emplace_back(pixel_stride);
84     attrs->emplace_back(format);
85     attrs->emplace_back(size);
86 
87     return err;
88 }
89 
RkRgaGetHandleMapAddress(buffer_handle_t handle,void ** buf)90 int RkRgaGetHandleMapAddress(buffer_handle_t handle,
91                              void **buf) {
92     int err;
93     uint64_t w, h;
94 
95     err = gralloc4::get_width(handle, &w);
96     if (err != android::OK)
97     {
98         ALOGE("Failed to get buffer width, err : %d", err);
99         return -1;
100     }
101 
102     err = gralloc4::get_height(handle, &h);
103     if (err != android::OK)
104     {
105         ALOGE("Failed to get buffer height, err : %d", err);
106         return -1;
107     }
108 
109     err = gralloc4::lock(handle, GRALLOC_USAGE_SW_READ_MASK, 0, 0, w, h,
110                     buf);
111     if (err != android::OK)
112     {
113         ALOGE("Failed to lock buffer, err : %d", err);
114         return -1;
115     }
116 
117     gralloc4::unlock(handle);
118 
119     return err;
120 }
121 
122 /* ========================================================================== */
123 #else /* #if USE_GRALLOC_4, else is gralloc 0.3 */
124 
125 #ifndef RK3368
126 
127 #ifdef ANDROID_7_DRM
128 #include <hardware/gralloc.h>
129 #else
130 #include <gralloc_priv.h>
131 #endif
132 
133 #else
134 #include <hardware/gralloc.h>
135 #include <hardware/img_gralloc_public.h>
136 
137 #endif /* #ifndef RK3368 */
138 
139 /*
140  *   Only these macros in rockchip's private gralloc-0.3 header are
141  * needed in librga, and these are defined in different paths for
142  * different Android versions.
143  *   So when librga refers to gralloc0.3, it will judge whether
144  * there is a reference to the corresponding path, and if not,
145  * define these macros by itself.
146  */
147 #ifndef GRALLOC_MODULE_PERFORM_GET_HADNLE_PRIME_FD
148 #define GRALLOC_MODULE_PERFORM_GET_HADNLE_PRIME_FD 0x08100002
149 #endif
150 
151 #ifndef GRALLOC_MODULE_PERFORM_GET_HADNLE_ATTRIBUTES
152 #define GRALLOC_MODULE_PERFORM_GET_HADNLE_ATTRIBUTES 0x08100004
153 #endif
154 
155 #ifndef GRALLOC_MODULE_PERFORM_GET_INTERNAL_FORMAT
156 #define GRALLOC_MODULE_PERFORM_GET_INTERNAL_FORMAT 0x08100006
157 #endif
158 
159 #ifndef GRALLOC_MODULE_PERFORM_GET_USAGE
160 #define GRALLOC_MODULE_PERFORM_GET_USAGE 0x0feeff03
161 #endif
162 
163 gralloc_module_t const *mAllocMod = NULL;
164 
165 #ifdef RK3368
166 #define private_handle_t IMG_native_handle_t
167 #endif
168 
169 // ---------------------------------------------------------------------------
RkInitAllocModle()170 int RkInitAllocModle() {
171     const hw_module_t *allocMod = NULL;
172     int ret = 0;
173 
174     if (mAllocMod)
175         return 0;
176 
177     ret= hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &allocMod);
178     ALOGE_IF(ret, "FATAL:can't find the %s module",GRALLOC_HARDWARE_MODULE_ID);
179     if (ret == 0)
180         mAllocMod = reinterpret_cast<gralloc_module_t const *>(allocMod);
181 
182     return ret;
183 }
184 
185 #ifndef ANDROID_7_DRM
186 
gralloc_backend_get_fd(private_handle_t * hnd,int * fd)187 int gralloc_backend_get_fd(private_handle_t* hnd, int *fd) {
188     *fd = hnd->share_fd;
189     return 0;
190 }
191 
gralloc_backend_get_attrs(private_handle_t * hnd,void * attrs)192 int gralloc_backend_get_attrs(private_handle_t* hnd, void *attrs) {
193     std::vector<int> *attributes = (std::vector<int> *)attrs;
194     attributes->clear();
195     attributes->push_back(hnd->width);
196     attributes->push_back(hnd->height);
197     attributes->push_back(hnd->stride);
198     attributes->push_back(hnd->format);
199     attributes->push_back(hnd->size);
200     attributes->push_back(hnd->type);
201     return 0;
202 }
203 
204 #else
205 
206 #ifdef RK3368
207 
gralloc_backend_get_fd(private_handle_t * hnd,int * fd)208 int gralloc_backend_get_fd(private_handle_t* hnd, int *fd) {
209     *fd = hnd->fd[0];
210     return 0;
211 }
212 
gralloc_backend_get_attrs(private_handle_t * hnd,void * attrs)213 int gralloc_backend_get_attrs(private_handle_t* hnd, void *attrs) {
214     std::vector<int> *attributes = (std::vector<int> *)attrs;
215     attributes->clear();
216     attributes->push_back(hnd->width);
217     attributes->push_back(hnd->height);
218     attributes->push_back(hnd->stride);
219     attributes->push_back(hnd->format);
220     attributes->push_back(hnd->size);
221     attributes->push_back(hnd->type);
222     return 0;
223 }
224 
225 #endif      //RK3368
226 
227 #endif      //ANDROID_7_DRM
228 
RkRgaGetHandleFd(buffer_handle_t handle,int * fd)229 int RkRgaGetHandleFd(buffer_handle_t handle, int *fd) {
230     int ret = 0;
231 
232     if (!mAllocMod)
233         ret = RkInitAllocModle();
234 
235     if (ret)
236         return ret;
237 
238 #ifdef ANDROID_7_DRM
239 
240 #ifndef RK3368
241     if (mAllocMod->perform) {
242         mAllocMod->perform(mAllocMod, GRALLOC_MODULE_PERFORM_GET_HADNLE_PRIME_FD, handle, fd);
243 	}
244     else
245         return -ENODEV;
246 #else
247     private_handle_t* hnd = (private_handle_t*)handle;
248     ret = gralloc_backend_get_fd(hnd,fd);
249 #endif      //RK3368
250 
251 #else
252     private_handle_t* hnd = (private_handle_t*)handle;
253     ret = gralloc_backend_get_fd(hnd,fd);
254 #endif      //ANDROID_7_DRM
255 
256     if (ret)
257         ALOGE("GraphicBufferGetHandldFd fail %d for:%s",ret,strerror(ret));
258     else if (false) {
259         ALOGD("fd = %d",*fd);
260         fprintf(stderr,"fd = %d\n", *fd);
261     }
262 
263     return ret;
264 }
265 
RkRgaGetHandleAttributes(buffer_handle_t handle,std::vector<int> * attrs)266 int RkRgaGetHandleAttributes(buffer_handle_t handle,
267                              std::vector<int> *attrs) {
268     int ret = 0;
269 
270     if (!mAllocMod)
271         ret = RkInitAllocModle();
272 
273     if (ret)
274         return ret;
275 
276 #if RK3368_DRM
277     int w,h,pixel_stride,format,size;
278 
279     op = GRALLOC_MODULE_PERFORM_GET_HADNLE_WIDTH;
280     mAllocMod->perform(mAllocMod, op, handle, &w);
281     op = GRALLOC_MODULE_PERFORM_GET_HADNLE_HEIGHT;
282     mAllocMod->perform(mAllocMod, op, handle, &h);
283     op = GRALLOC_MODULE_PERFORM_GET_HADNLE_STRIDE;
284     mAllocMod->perform(mAllocMod, op, handle, &pixel_stride);
285     op = GRALLOC_MODULE_PERFORM_GET_HADNLE_FORMAT;
286     mAllocMod->perform(mAllocMod, op, handle, &format);
287     op = GRALLOC_MODULE_PERFORM_GET_HADNLE_SIZE;
288     mAllocMod->perform(mAllocMod, op, handle, &size);
289 
290     //add to attrs.
291     attrs->emplace_back(w);
292     attrs->emplace_back(h);
293     attrs->emplace_back(pixel_stride);
294     attrs->emplace_back(format);
295     attrs->emplace_back(size);
296 
297 #else
298 
299 #ifdef ANDROID_7_DRM
300 
301 #ifndef RK3368
302     int op = GRALLOC_MODULE_PERFORM_GET_HADNLE_ATTRIBUTES;
303     if(!mAllocMod->perform)
304         return -ENODEV;
305 
306     mAllocMod->perform(mAllocMod, op, handle, attrs);
307 #else
308     private_handle_t* hnd = (private_handle_t*)handle;
309     ret = gralloc_backend_get_attrs(hnd, (void*)attrs);
310 #endif      //RK3368
311 
312 #else
313     private_handle_t* hnd = (private_handle_t*)handle;
314     ret = gralloc_backend_get_attrs(hnd, (void*)attrs);
315 #endif      //ANDROID_7_DRM
316 
317 
318     if (ret)
319         ALOGE("GraphicBufferGetHandldAttributes fail %d for:%s",ret,strerror(ret));
320     else if (false) {
321         ALOGD("%d,%d,%d,%d,%d,%d",attrs->at(0),attrs->at(1),attrs->at(2),
322               attrs->at(3),attrs->at(4),attrs->at(5));
323         fprintf(stderr,"%d,%d,%d,%d,%d,%d\n",
324                 attrs->at(0),attrs->at(1),attrs->at(2),
325                 attrs->at(3),attrs->at(4),attrs->at(5));
326     }
327 #endif
328 
329     return ret;
330 }
331 
RkRgaGetHandleMapAddress(buffer_handle_t handle,void ** buf)332 int RkRgaGetHandleMapAddress(buffer_handle_t handle,
333                              void **buf) {
334     int usage = GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK;
335 #ifdef ANDROID_7_DRM
336     usage |= GRALLOC_USAGE_HW_FB;
337 #endif
338     int ret = 0;
339 
340     if (!mAllocMod)
341         ret = RkInitAllocModle();
342 
343     if (ret)
344         return ret;
345 
346     if (mAllocMod->lock)
347         ret = mAllocMod->lock(mAllocMod, handle, usage, 0, 0, 0, 0, buf);
348     else
349         return -ENODEV;
350 
351     if (ret)
352         ALOGE("GetHandleMapAddress fail %d for:%s",ret,strerror(ret));
353 
354     return ret;
355 }
356 
357 #endif //USE_GRALLOC_4
358 #endif /* ANDROID */
359