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