1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  * Copyright (c) 2011-2014 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 <linux/msm_ion.h>
30 
31 using namespace gralloc;
32 
gpu_context_t(const private_module_t * module,IAllocController * alloc_ctrl)33 gpu_context_t::gpu_context_t(const private_module_t* module,
34                              IAllocController* alloc_ctrl ) :
35     mAllocCtrl(alloc_ctrl)
36 {
37     // Zero out the alloc_device_t
38     memset(static_cast<alloc_device_t*>(this), 0, sizeof(alloc_device_t));
39 
40     // Initialize the procs
41     common.tag     = HARDWARE_DEVICE_TAG;
42     common.version = 0;
43     common.module  = const_cast<hw_module_t*>(&module->base.common);
44     common.close   = gralloc_close;
45     alloc          = gralloc_alloc;
46     free           = gralloc_free;
47 
48 }
49 
gralloc_alloc_buffer(unsigned int size,int usage,buffer_handle_t * pHandle,int bufferType,int format,int width,int height)50 int gpu_context_t::gralloc_alloc_buffer(unsigned int size, int usage,
51                                         buffer_handle_t* pHandle, int bufferType,
52                                         int format, int width, int height)
53 {
54     int err = 0;
55     int flags = 0;
56     size = roundUpToPageSize(size);
57     alloc_data data;
58     data.offset = 0;
59     data.fd = -1;
60     data.base = 0;
61     if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED)
62         data.align = 8192;
63     else
64         data.align = getpagesize();
65 
66     if (usage & GRALLOC_USAGE_PROTECTED) {
67             if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
68                 /* The alignment here reflects qsee mmu V7L/V8L requirement */
69                 data.align = SZ_2M;
70             } else {
71                 data.align = SECURE_ALIGN;
72             }
73         size = ALIGN(size, data.align);
74     }
75 
76     data.size = size;
77     data.pHandle = (uintptr_t) pHandle;
78     err = mAllocCtrl->allocate(data, usage);
79 
80     if (!err) {
81         /* allocate memory for enhancement data */
82         alloc_data eData;
83         eData.fd = -1;
84         eData.base = 0;
85         eData.offset = 0;
86         eData.size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
87         eData.pHandle = data.pHandle;
88         eData.align = getpagesize();
89         int eDataUsage = 0;
90         int eDataErr = mAllocCtrl->allocate(eData, eDataUsage);
91         ALOGE_IF(eDataErr, "gralloc failed for eDataErr=%s",
92                                           strerror(-eDataErr));
93 
94         if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY) {
95             flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
96         }
97 
98         if (usage & GRALLOC_USAGE_PRIVATE_INTERNAL_ONLY) {
99             flags |= private_handle_t::PRIV_FLAGS_INTERNAL_ONLY;
100         }
101 
102         if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) {
103             flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
104         }
105 
106         if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
107             flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
108         }
109 
110         if (usage & GRALLOC_USAGE_HW_CAMERA_READ) {
111             flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
112         }
113 
114         if (usage & GRALLOC_USAGE_HW_COMPOSER) {
115             flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER;
116         }
117 
118         if (usage & GRALLOC_USAGE_HW_TEXTURE) {
119             flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
120         }
121 
122         if(usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
123             flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
124         }
125 
126         if(isMacroTileEnabled(format, usage)) {
127             flags |= private_handle_t::PRIV_FLAGS_TILE_RENDERED;
128         }
129 
130         if (isUBwcEnabled(format, usage)) {
131             flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
132         }
133 
134         if(usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
135             flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
136         }
137 
138         ColorSpace_t colorSpace = ITU_R_601;
139         if (usage & GRALLOC_USAGE_HW_CAMERA_MASK) {
140             colorSpace = ITU_R_601_FR;
141         }
142 
143         if (usage & (GRALLOC_USAGE_HW_VIDEO_ENCODER |
144                 GRALLOC_USAGE_HW_CAMERA_WRITE |
145                 GRALLOC_USAGE_HW_RENDER |
146                 GRALLOC_USAGE_HW_FB)) {
147             flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
148         }
149 
150         if(usage & GRALLOC_USAGE_HW_COMPOSER) {
151             flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
152         }
153 
154         if(false == data.uncached) {
155             flags |= private_handle_t::PRIV_FLAGS_CACHED;
156         }
157 
158         flags |= data.allocType;
159         uint64_t eBaseAddr = (uint64_t)(eData.base) + eData.offset;
160         private_handle_t *hnd = new private_handle_t(data.fd, size, flags,
161                 bufferType, format, width, height, eData.fd, eData.offset,
162                 eBaseAddr);
163 
164         hnd->offset = data.offset;
165         hnd->base = (uint64_t)(data.base) + data.offset;
166         hnd->gpuaddr = 0;
167         setMetaData(hnd, UPDATE_COLOR_SPACE, (void*) &colorSpace);
168 
169         *pHandle = hnd;
170     }
171 
172     ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
173 
174     return err;
175 }
176 
getGrallocInformationFromFormat(int inputFormat,int * bufferType)177 void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
178                                                     int *bufferType)
179 {
180     *bufferType = BUFFER_TYPE_VIDEO;
181 
182     if (isUncompressedRgbFormat(inputFormat) == TRUE) {
183         // RGB formats
184         *bufferType = BUFFER_TYPE_UI;
185     }
186 }
187 
gralloc_alloc_framebuffer_locked(int usage,buffer_handle_t * pHandle)188 int gpu_context_t::gralloc_alloc_framebuffer_locked(int usage,
189                                                     buffer_handle_t* pHandle)
190 {
191     private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
192 
193     // This allocation will only happen when gralloc is in fb mode
194 
195     if (m->framebuffer == NULL) {
196         ALOGE("%s: Invalid framebuffer", __FUNCTION__);
197         return -EINVAL;
198     }
199 
200     const unsigned int bufferMask = m->bufferMask;
201     const uint32_t numBuffers = m->numBuffers;
202     unsigned int bufferSize = m->finfo.line_length * m->info.yres;
203 
204     //adreno needs FB size to be page aligned
205     bufferSize = roundUpToPageSize(bufferSize);
206 
207     if (numBuffers == 1) {
208         // If we have only one buffer, we never use page-flipping. Instead,
209         // we return a regular buffer which will be memcpy'ed to the main
210         // screen when post is called.
211         int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
212         return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI,
213                                     m->fbFormat, m->info.xres, m->info.yres);
214     }
215 
216     if (bufferMask >= ((1LU<<numBuffers)-1)) {
217         // We ran out of buffers.
218         return -ENOMEM;
219     }
220 
221     // create a "fake" handle for it
222     uint64_t vaddr = uint64_t(m->framebuffer->base);
223     // As GPU needs ION FD, the private handle is created
224     // using ION fd and ION flags are set
225     private_handle_t* hnd = new private_handle_t(
226         dup(m->framebuffer->fd), bufferSize,
227         private_handle_t::PRIV_FLAGS_USES_ION |
228         private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
229         BUFFER_TYPE_UI, m->fbFormat, m->info.xres,
230         m->info.yres);
231 
232     // find a free slot
233     for (uint32_t i=0 ; i<numBuffers ; i++) {
234         if ((bufferMask & (1LU<<i)) == 0) {
235             m->bufferMask |= (uint32_t)(1LU<<i);
236             break;
237         }
238         vaddr += bufferSize;
239     }
240     hnd->base = vaddr;
241     hnd->offset = (unsigned int)(vaddr - m->framebuffer->base);
242     *pHandle = hnd;
243     return 0;
244 }
245 
246 
gralloc_alloc_framebuffer(int usage,buffer_handle_t * pHandle)247 int gpu_context_t::gralloc_alloc_framebuffer(int usage,
248                                              buffer_handle_t* pHandle)
249 {
250     private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
251     pthread_mutex_lock(&m->lock);
252     int err = gralloc_alloc_framebuffer_locked(usage, pHandle);
253     pthread_mutex_unlock(&m->lock);
254     return err;
255 }
256 
alloc_impl(int w,int h,int format,int usage,buffer_handle_t * pHandle,int * pStride,unsigned int bufferSize)257 int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
258                               buffer_handle_t* pHandle, int* pStride,
259                               unsigned int bufferSize) {
260     if (!pHandle || !pStride)
261         return -EINVAL;
262 
263     unsigned int size;
264     int alignedw, alignedh;
265     int grallocFormat = format;
266     int bufferType;
267 
268     //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
269     //the usage bits, gralloc assigns a format.
270     if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
271        format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
272         if (usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC)
273             grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
274         else if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
275             grallocFormat = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; //NV12
276         else if((usage & GRALLOC_USAGE_HW_CAMERA_MASK)
277                 == GRALLOC_USAGE_HW_CAMERA_ZSL)
278             grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21 ZSL
279         else if(usage & GRALLOC_USAGE_HW_CAMERA_READ)
280             grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
281         else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
282            if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
283                grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21
284            } else {
285                grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; //NV12 preview
286            }
287         } else if(usage & GRALLOC_USAGE_HW_COMPOSER)
288             //XXX: If we still haven't set a format, default to RGBA8888
289             grallocFormat = HAL_PIXEL_FORMAT_RGBA_8888;
290         else if(format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
291             //If no other usage flags are detected, default the
292             //flexible YUV format to NV21_ZSL
293             grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL;
294         }
295     }
296 
297     bool useFbMem = false;
298     char property[PROPERTY_VALUE_MAX];
299     char isUBWC[PROPERTY_VALUE_MAX];
300     if (usage & GRALLOC_USAGE_HW_FB) {
301         if ((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
302             (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
303             (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
304             useFbMem = true;
305         } else {
306             usage &= ~GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
307             if (property_get("debug.gralloc.enable_fb_ubwc", isUBWC, NULL) > 0){
308                 if ((!strncmp(isUBWC, "1", PROPERTY_VALUE_MAX)) ||
309                     (!strncasecmp(isUBWC, "true", PROPERTY_VALUE_MAX))) {
310                     // Allocate UBWC aligned framebuffer
311                     usage |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
312                 }
313             }
314         }
315     }
316 
317     getGrallocInformationFromFormat(grallocFormat, &bufferType);
318     size = getBufferSizeAndDimensions(w, h, grallocFormat, usage, alignedw,
319                    alignedh);
320 
321     if ((unsigned int)size <= 0)
322         return -EINVAL;
323     size = (bufferSize >= size)? bufferSize : size;
324 
325     int err = 0;
326     if(useFbMem) {
327         err = gralloc_alloc_framebuffer(usage, pHandle);
328     } else {
329         err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
330                                    grallocFormat, alignedw, alignedh);
331     }
332 
333     if (err < 0) {
334         return err;
335     }
336 
337     *pStride = alignedw;
338     return 0;
339 }
340 
free_impl(private_handle_t const * hnd)341 int gpu_context_t::free_impl(private_handle_t const* hnd) {
342     private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
343     if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
344         const unsigned int bufferSize = m->finfo.line_length * m->info.yres;
345         unsigned int index = (unsigned int) ((hnd->base - m->framebuffer->base)
346                 / bufferSize);
347         m->bufferMask &= (uint32_t)~(1LU<<index);
348     } else {
349 
350         terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
351         IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
352         int err = memalloc->free_buffer((void*)hnd->base, hnd->size,
353                                         hnd->offset, hnd->fd);
354         if(err)
355             return err;
356         // free the metadata space
357         unsigned int size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
358         err = memalloc->free_buffer((void*)hnd->base_metadata,
359                                     size, hnd->offset_metadata,
360                                     hnd->fd_metadata);
361         if (err)
362             return err;
363     }
364     delete hnd;
365     return 0;
366 }
367 
gralloc_alloc(alloc_device_t * dev,int w,int h,int format,int usage,buffer_handle_t * pHandle,int * pStride)368 int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
369                                  int usage, buffer_handle_t* pHandle,
370                                  int* pStride)
371 {
372     if (!dev) {
373         return -EINVAL;
374     }
375     gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
376     return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0);
377 }
gralloc_alloc_size(alloc_device_t * dev,int w,int h,int format,int usage,buffer_handle_t * pHandle,int * pStride,int bufferSize)378 int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h,
379                                       int format, int usage,
380                                       buffer_handle_t* pHandle, int* pStride,
381                                       int bufferSize)
382 {
383     if (!dev) {
384         return -EINVAL;
385     }
386     gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
387     return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize);
388 }
389 
390 
gralloc_free(alloc_device_t * dev,buffer_handle_t handle)391 int gpu_context_t::gralloc_free(alloc_device_t* dev,
392                                 buffer_handle_t handle)
393 {
394     if (private_handle_t::validate(handle) < 0)
395         return -EINVAL;
396 
397     private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
398     gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
399     return gpu->free_impl(hnd);
400 }
401 
402 /*****************************************************************************/
403 
gralloc_close(struct hw_device_t * dev)404 int gpu_context_t::gralloc_close(struct hw_device_t *dev)
405 {
406     gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev);
407     if (ctx) {
408         /* TODO: keep a list of all buffer_handle_t created, and free them
409          * all here.
410          */
411         delete ctx;
412     }
413     return 0;
414 }
415 
416