• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  * Copyright (c) 2011-2013 The Linux Foundation. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <limits.h>
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include <cutils/properties.h>
22 #include <sys/mman.h>
23 
24 #include "gr.h"
25 #include "gpu.h"
26 #include "memalloc.h"
27 #include "alloc_controller.h"
28 #include <qdMetaData.h>
29 
30 using namespace gralloc;
31 
32 #define SZ_1M 0x100000
33 
gpu_context_t(const private_module_t * module,IAllocController * alloc_ctrl)34 gpu_context_t::gpu_context_t(const private_module_t* module,
35                              IAllocController* alloc_ctrl ) :
36     mAllocCtrl(alloc_ctrl)
37 {
38     // Zero out the alloc_device_t
39     memset(static_cast<alloc_device_t*>(this), 0, sizeof(alloc_device_t));
40 
41     // Initialize the procs
42     common.tag     = HARDWARE_DEVICE_TAG;
43     common.version = 0;
44     common.module  = const_cast<hw_module_t*>(&module->base.common);
45     common.close   = gralloc_close;
46     alloc          = gralloc_alloc;
47 #ifdef QCOM_BSP
48     allocSize      = gralloc_alloc_size;
49 #endif
50     free           = gralloc_free;
51 
52 }
53 
gralloc_alloc_buffer(size_t size,int usage,buffer_handle_t * pHandle,int bufferType,int format,int width,int height)54 int gpu_context_t::gralloc_alloc_buffer(size_t size, int usage,
55                                         buffer_handle_t* pHandle, int bufferType,
56                                         int format, int width, int height)
57 {
58     int err = 0;
59     int flags = 0;
60     size = roundUpToPageSize(size);
61     alloc_data data;
62     data.offset = 0;
63     data.fd = -1;
64     data.base = 0;
65     if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED)
66         data.align = 8192;
67     else
68         data.align = getpagesize();
69 
70     /* force 1MB alignment selectively for secure buffers, MDP5 onwards */
71 #ifdef MDSS_TARGET
72     if (usage & GRALLOC_USAGE_PROTECTED) {
73         data.align = ALIGN(data.align, SZ_1M);
74         size = ALIGN(size, data.align);
75     }
76 #endif
77 
78     data.size = size;
79     data.pHandle = (unsigned int) pHandle;
80     err = mAllocCtrl->allocate(data, usage);
81 
82     if (!err) {
83         /* allocate memory for enhancement data */
84         alloc_data eData;
85         eData.fd = -1;
86         eData.base = 0;
87         eData.offset = 0;
88         eData.size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
89         eData.pHandle = data.pHandle;
90         eData.align = getpagesize();
91         int eDataUsage = GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP;
92         int eDataErr = mAllocCtrl->allocate(eData, eDataUsage);
93         ALOGE_IF(eDataErr, "gralloc failed for eDataErr=%s",
94                                           strerror(-eDataErr));
95 
96         if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY) {
97             flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
98             //The EXTERNAL_BLOCK flag is always an add-on
99             if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_BLOCK) {
100                 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_BLOCK;
101             }
102             if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_CC) {
103                 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_CC;
104             }
105         }
106 
107         if (bufferType == BUFFER_TYPE_VIDEO) {
108             if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
109 #ifndef MDSS_TARGET
110                 flags |= private_handle_t::PRIV_FLAGS_ITU_R_601_FR;
111 #else
112                 // Per the camera spec ITU 709 format should be set only for
113                 // video encoding.
114                 // It should be set to ITU 601 full range format for any other
115                 // camera buffer
116                 //
117                 if (usage & GRALLOC_USAGE_HW_CAMERA_MASK) {
118                     if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
119                         flags |= private_handle_t::PRIV_FLAGS_ITU_R_709;
120                     else
121                         flags |= private_handle_t::PRIV_FLAGS_ITU_R_601_FR;
122                 }
123 #endif
124             } else {
125                 flags |= private_handle_t::PRIV_FLAGS_ITU_R_601;
126             }
127         }
128 
129         if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) {
130             flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
131         }
132 
133         if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
134             flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
135         }
136 
137         if (usage & GRALLOC_USAGE_HW_CAMERA_READ) {
138             flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
139         }
140 
141         if (usage & GRALLOC_USAGE_HW_COMPOSER) {
142             flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER;
143         }
144 
145         if (usage & GRALLOC_USAGE_HW_TEXTURE) {
146             flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
147         }
148 
149         flags |= data.allocType;
150         int eBaseAddr = int(eData.base) + eData.offset;
151         private_handle_t *hnd = new private_handle_t(data.fd, size, flags,
152                 bufferType, format, width, height, eData.fd, eData.offset,
153                 eBaseAddr);
154 
155         hnd->offset = data.offset;
156         hnd->base = int(data.base) + data.offset;
157         hnd->gpuaddr = 0;
158 
159         *pHandle = hnd;
160     }
161 
162     ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
163 
164     return err;
165 }
166 
getGrallocInformationFromFormat(int inputFormat,int * bufferType)167 void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
168                                                     int *bufferType)
169 {
170     *bufferType = BUFFER_TYPE_VIDEO;
171 
172     if (inputFormat <= HAL_PIXEL_FORMAT_sRGB_X_8888) {
173         // RGB formats
174         *bufferType = BUFFER_TYPE_UI;
175     } else if ((inputFormat == HAL_PIXEL_FORMAT_R_8) ||
176                (inputFormat == HAL_PIXEL_FORMAT_RG_88)) {
177         *bufferType = BUFFER_TYPE_UI;
178     }
179 }
180 
gralloc_alloc_framebuffer_locked(size_t size,int usage,buffer_handle_t * pHandle)181 int gpu_context_t::gralloc_alloc_framebuffer_locked(size_t size, int usage,
182                                                     buffer_handle_t* pHandle)
183 {
184     private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
185 
186     // we don't support framebuffer allocations with graphics heap flags
187     if (usage & GRALLOC_HEAP_MASK) {
188         return -EINVAL;
189     }
190 
191     if (m->framebuffer == NULL) {
192         ALOGE("%s: Invalid framebuffer", __FUNCTION__);
193         return -EINVAL;
194     }
195 
196     const uint32_t bufferMask = m->bufferMask;
197     const uint32_t numBuffers = m->numBuffers;
198     size_t bufferSize = m->finfo.line_length * m->info.yres;
199 
200     //adreno needs FB size to be page aligned
201     bufferSize = roundUpToPageSize(bufferSize);
202 
203     if (numBuffers == 1) {
204         // If we have only one buffer, we never use page-flipping. Instead,
205         // we return a regular buffer which will be memcpy'ed to the main
206         // screen when post is called.
207         int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
208         return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI,
209                                     m->fbFormat, m->info.xres, m->info.yres);
210     }
211 
212     if (bufferMask >= ((1LU<<numBuffers)-1)) {
213         // We ran out of buffers.
214         return -ENOMEM;
215     }
216 
217     // create a "fake" handle for it
218     intptr_t vaddr = intptr_t(m->framebuffer->base);
219     private_handle_t* hnd = new private_handle_t(
220         dup(m->framebuffer->fd), bufferSize,
221         private_handle_t::PRIV_FLAGS_USES_PMEM |
222         private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
223         BUFFER_TYPE_UI, m->fbFormat, m->info.xres,
224         m->info.yres);
225 
226     // find a free slot
227     for (uint32_t i=0 ; i<numBuffers ; i++) {
228         if ((bufferMask & (1LU<<i)) == 0) {
229             m->bufferMask |= (1LU<<i);
230             break;
231         }
232         vaddr += bufferSize;
233     }
234     hnd->base = vaddr;
235     hnd->offset = vaddr - intptr_t(m->framebuffer->base);
236     *pHandle = hnd;
237     return 0;
238 }
239 
240 
gralloc_alloc_framebuffer(size_t size,int usage,buffer_handle_t * pHandle)241 int gpu_context_t::gralloc_alloc_framebuffer(size_t size, int usage,
242                                              buffer_handle_t* pHandle)
243 {
244     private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
245     pthread_mutex_lock(&m->lock);
246     int err = gralloc_alloc_framebuffer_locked(size, usage, pHandle);
247     pthread_mutex_unlock(&m->lock);
248     return err;
249 }
250 
alloc_impl(int w,int h,int format,int usage,buffer_handle_t * pHandle,int * pStride,size_t bufferSize)251 int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
252                               buffer_handle_t* pHandle, int* pStride,
253                               size_t bufferSize) {
254     if (!pHandle || !pStride)
255         return -EINVAL;
256 
257     size_t size;
258     int alignedw, alignedh;
259     int grallocFormat = format;
260     int bufferType;
261 
262     //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
263     //the usage bits, gralloc assigns a format.
264     if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
265        format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
266         if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
267             grallocFormat = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; //NV12
268         else if((usage & GRALLOC_USAGE_HW_CAMERA_MASK)
269                 == GRALLOC_USAGE_HW_CAMERA_ZSL)
270             grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21 ZSL
271         else if(usage & GRALLOC_USAGE_HW_CAMERA_READ)
272             grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
273         else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
274             grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
275     }
276 
277     if (grallocFormat == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
278             (usage & GRALLOC_USAGE_HW_COMPOSER )) {
279         //XXX: If we still haven't set a format, default to RGBA8888
280         grallocFormat = HAL_PIXEL_FORMAT_RGBA_8888;
281     }
282 
283     getGrallocInformationFromFormat(grallocFormat, &bufferType);
284     size = getBufferSizeAndDimensions(w, h, grallocFormat, alignedw, alignedh);
285 
286     if ((ssize_t)size <= 0)
287         return -EINVAL;
288     size = (bufferSize >= size)? bufferSize : size;
289 
290     // All buffers marked as protected or for external
291     // display need to go to overlay
292     if ((usage & GRALLOC_USAGE_EXTERNAL_DISP) ||
293         (usage & GRALLOC_USAGE_PROTECTED)) {
294         bufferType = BUFFER_TYPE_VIDEO;
295     }
296 
297     bool useFbMem = false;
298     char property[PROPERTY_VALUE_MAX];
299     if((usage & GRALLOC_USAGE_HW_FB) &&
300        (property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
301        (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
302         (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
303         useFbMem = true;
304     }
305 
306     int err = 0;
307     if(useFbMem) {
308         err = gralloc_alloc_framebuffer(size, usage, pHandle);
309     } else {
310         err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
311                                    grallocFormat, alignedw, alignedh);
312     }
313 
314     if (err < 0) {
315         return err;
316     }
317 
318     *pStride = alignedw;
319     return 0;
320 }
321 
free_impl(private_handle_t const * hnd)322 int gpu_context_t::free_impl(private_handle_t const* hnd) {
323     private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
324     if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
325         const size_t bufferSize = m->finfo.line_length * m->info.yres;
326         int index = (hnd->base - m->framebuffer->base) / bufferSize;
327         m->bufferMask &= ~(1<<index);
328     } else {
329 
330         terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
331         IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
332         int err = memalloc->free_buffer((void*)hnd->base, (size_t) hnd->size,
333                                         hnd->offset, hnd->fd);
334         if(err)
335             return err;
336         // free the metadata space
337         unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
338         err = memalloc->free_buffer((void*)hnd->base_metadata,
339                                     (size_t) size, hnd->offset_metadata,
340                                     hnd->fd_metadata);
341         if (err)
342             return err;
343     }
344     delete hnd;
345     return 0;
346 }
347 
gralloc_alloc(alloc_device_t * dev,int w,int h,int format,int usage,buffer_handle_t * pHandle,int * pStride)348 int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
349                                  int usage, buffer_handle_t* pHandle,
350                                  int* pStride)
351 {
352     if (!dev) {
353         return -EINVAL;
354     }
355     gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
356     return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0);
357 }
gralloc_alloc_size(alloc_device_t * dev,int w,int h,int format,int usage,buffer_handle_t * pHandle,int * pStride,int bufferSize)358 int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h,
359                                       int format, int usage,
360                                       buffer_handle_t* pHandle, int* pStride,
361                                       int bufferSize)
362 {
363     if (!dev) {
364         return -EINVAL;
365     }
366     gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
367     return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize);
368 }
369 
370 
gralloc_free(alloc_device_t * dev,buffer_handle_t handle)371 int gpu_context_t::gralloc_free(alloc_device_t* dev,
372                                 buffer_handle_t handle)
373 {
374     if (private_handle_t::validate(handle) < 0)
375         return -EINVAL;
376 
377     private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
378     gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
379     return gpu->free_impl(hnd);
380 }
381 
382 /*****************************************************************************/
383 
gralloc_close(struct hw_device_t * dev)384 int gpu_context_t::gralloc_close(struct hw_device_t *dev)
385 {
386     gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev);
387     if (ctx) {
388         /* TODO: keep a list of all buffer_handle_t created, and free them
389          * all here.
390          */
391         delete ctx;
392     }
393     return 0;
394 }
395 
396