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