blob: ce153048a55e1fa67e24e7b35ea0cd2b527feb28 [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Naseer Ahmeda163b732013-02-12 14:53:33 -05003 * Copyright (c) 2011-2013 The Linux Foundation. All rights reserved.
Iliyan Malchev202a77d2012-06-11 14:41:12 -07004 *
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
Iliyan Malchev202a77d2012-06-11 14:41:12 -070024#include "gr.h"
25#include "gpu.h"
26#include "memalloc.h"
27#include "alloc_controller.h"
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080028#include <qdMetaData.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070029
30using namespace gralloc;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070031
Praveen Chavan4e931c12012-11-28 19:43:17 -080032#define SZ_1M 0x100000
33
Iliyan Malchev202a77d2012-06-11 14:41:12 -070034gpu_context_t::gpu_context_t(const private_module_t* module,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070035 IAllocController* alloc_ctrl ) :
Iliyan Malchev202a77d2012-06-11 14:41:12 -070036 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
Iliyan Malchev202a77d2012-06-11 14:41:12 -070041 // 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;
Naseer Ahmed74214722013-02-09 08:11:36 -050047#ifdef QCOM_BSP
Ramkumar Radhakrishnan3d4c0ac2012-12-10 15:42:54 -080048 allocSize = gralloc_alloc_size;
Naseer Ahmed74214722013-02-09 08:11:36 -050049#endif
Iliyan Malchev202a77d2012-06-11 14:41:12 -070050 free = gralloc_free;
51
52}
53
Iliyan Malchev202a77d2012-06-11 14:41:12 -070054int 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;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070065 if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED)
66 data.align = 8192;
67 else
68 data.align = getpagesize();
Praveen Chavan4e931c12012-11-28 19:43:17 -080069
70 /* force 1MB alignment selectively for secure buffers, MDP5 onwards */
Naseer Ahmed84786722013-06-14 16:29:59 -040071#ifdef MDSS_TARGET
72 if (usage & GRALLOC_USAGE_PROTECTED) {
Praveen Chavan4e931c12012-11-28 19:43:17 -080073 data.align = ALIGN(data.align, SZ_1M);
74 size = ALIGN(size, data.align);
75 }
Naseer Ahmed84786722013-06-14 16:29:59 -040076#endif
77
Praveen Chavan4e931c12012-11-28 19:43:17 -080078 data.size = size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070079 data.pHandle = (unsigned int) pHandle;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070080 err = mAllocCtrl->allocate(data, usage);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070081
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080082 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));
Iliyan Malchev202a77d2012-06-11 14:41:12 -070095
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080096 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 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400106
Naseer Ahmedfc940ef2013-04-29 15:47:47 -0400107 if (bufferType == BUFFER_TYPE_VIDEO) {
108 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
Naseer Ahmed84786722013-06-14 16:29:59 -0400109#ifndef MDSS_TARGET
110 flags |= private_handle_t::PRIV_FLAGS_ITU_R_601_FR;
111#else
Naseer Ahmed22fa2d32013-08-14 12:59:24 -0400112 // 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)
Naseer Ahmedfc940ef2013-04-29 15:47:47 -0400119 flags |= private_handle_t::PRIV_FLAGS_ITU_R_709;
Naseer Ahmed22fa2d32013-08-14 12:59:24 -0400120 else
Naseer Ahmedfc940ef2013-04-29 15:47:47 -0400121 flags |= private_handle_t::PRIV_FLAGS_ITU_R_601_FR;
Naseer Ahmed22fa2d32013-08-14 12:59:24 -0400122 }
Naseer Ahmed84786722013-06-14 16:29:59 -0400123#endif
Naseer Ahmedfc940ef2013-04-29 15:47:47 -0400124 } else {
125 flags |= private_handle_t::PRIV_FLAGS_ITU_R_601;
126 }
127 }
128
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800129 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) {
130 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
131 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400132
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800133 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
134 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
135 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400136
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800137 if (usage & GRALLOC_USAGE_HW_CAMERA_READ) {
138 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
139 }
140
Naseer Ahmede41ad9c2013-04-05 17:59:28 -0400141 if (usage & GRALLOC_USAGE_HW_COMPOSER) {
142 flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER;
143 }
144
Shuzhen Wang46ca2262013-04-10 23:02:22 -0700145 if (usage & GRALLOC_USAGE_HW_TEXTURE) {
146 flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
147 }
148
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700149 flags |= data.allocType;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800150 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);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700154
155 hnd->offset = data.offset;
156 hnd->base = int(data.base) + data.offset;
Naseer Ahmeda163b732013-02-12 14:53:33 -0500157 hnd->gpuaddr = 0;
158
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700159 *pHandle = hnd;
160 }
161
162 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800163
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700164 return err;
165}
166
167void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700168 int *bufferType)
169{
170 *bufferType = BUFFER_TYPE_VIDEO;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700171
Naseer Ahmed65f16562012-06-15 20:53:42 -0700172 if (inputFormat < 0x7) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700173 // RGB formats
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700174 *bufferType = BUFFER_TYPE_UI;
175 } else if ((inputFormat == HAL_PIXEL_FORMAT_R_8) ||
176 (inputFormat == HAL_PIXEL_FORMAT_RG_88)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700177 *bufferType = BUFFER_TYPE_UI;
178 }
179}
180
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400181int 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
241int 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
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700251int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700252 buffer_handle_t* pHandle, int* pStride,
253 size_t bufferSize) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700254 if (!pHandle || !pStride)
255 return -EINVAL;
256
257 size_t size;
258 int alignedw, alignedh;
Naseer Ahmed59802502012-08-21 17:03:27 -0400259 int grallocFormat = format;
260 int bufferType;
Naseer Ahmed59802502012-08-21 17:03:27 -0400261
Saurabh Shahaedf2362012-09-05 11:30:59 -0700262 //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 if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
Shuzhen Wanga11b6ba2013-04-21 14:23:05 -0700266 grallocFormat = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; //NV12
Saurabh Shahaedf2362012-09-05 11:30:59 -0700267 else if(usage & GRALLOC_USAGE_HW_CAMERA_READ)
268 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
269 else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
270 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
271 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400272
Saurabh Shahaedf2362012-09-05 11:30:59 -0700273 getGrallocInformationFromFormat(grallocFormat, &bufferType);
Naseer Ahmed59802502012-08-21 17:03:27 -0400274 size = getBufferSizeAndDimensions(w, h, grallocFormat, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700275
276 if ((ssize_t)size <= 0)
277 return -EINVAL;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800278 size = (bufferSize >= size)? bufferSize : size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700279
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400280 bool useFbMem = false;
281 char property[PROPERTY_VALUE_MAX];
282 if((usage & GRALLOC_USAGE_HW_FB) &&
283 (property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
284 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
285 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
286 useFbMem = true;
287 }
288
289 int err = 0;
290 if(useFbMem) {
291 err = gralloc_alloc_framebuffer(size, usage, pHandle);
292 } else {
293 err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
294 grallocFormat, alignedw, alignedh);
295 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700296
297 if (err < 0) {
298 return err;
299 }
300
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700301 *pStride = alignedw;
302 return 0;
303}
304
305int gpu_context_t::free_impl(private_handle_t const* hnd) {
306 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400307 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
308 const size_t bufferSize = m->finfo.line_length * m->info.yres;
309 int index = (hnd->base - m->framebuffer->base) / bufferSize;
310 m->bufferMask &= ~(1<<index);
311 } else {
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800312
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400313 terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
314 IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
315 int err = memalloc->free_buffer((void*)hnd->base, (size_t) hnd->size,
316 hnd->offset, hnd->fd);
317 if(err)
318 return err;
319 // free the metadata space
320 unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
321 err = memalloc->free_buffer((void*)hnd->base_metadata,
322 (size_t) size, hnd->offset_metadata,
323 hnd->fd_metadata);
324 if (err)
325 return err;
326 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700327 delete hnd;
328 return 0;
329}
330
331int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700332 int usage, buffer_handle_t* pHandle,
333 int* pStride)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700334{
335 if (!dev) {
336 return -EINVAL;
337 }
338 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
339 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0);
340}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700341int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h,
342 int format, int usage,
343 buffer_handle_t* pHandle, int* pStride,
344 int bufferSize)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700345{
346 if (!dev) {
347 return -EINVAL;
348 }
349 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
350 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize);
351}
352
353
354int gpu_context_t::gralloc_free(alloc_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700355 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700356{
357 if (private_handle_t::validate(handle) < 0)
358 return -EINVAL;
359
360 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
361 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
362 return gpu->free_impl(hnd);
363}
364
365/*****************************************************************************/
366
367int gpu_context_t::gralloc_close(struct hw_device_t *dev)
368{
369 gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev);
370 if (ctx) {
371 /* TODO: keep a list of all buffer_handle_t created, and free them
372 * all here.
373 */
374 delete ctx;
375 }
376 return 0;
377}
378