blob: 013640767a1185cae55757d4e40985c05d14a0eb [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
Ramkumar Radhakrishnanba713382013-08-30 18:41:07 -0700149 if(usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
150 flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
151 }
152
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700153 flags |= data.allocType;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800154 int eBaseAddr = int(eData.base) + eData.offset;
155 private_handle_t *hnd = new private_handle_t(data.fd, size, flags,
156 bufferType, format, width, height, eData.fd, eData.offset,
157 eBaseAddr);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700158
159 hnd->offset = data.offset;
160 hnd->base = int(data.base) + data.offset;
Naseer Ahmeda163b732013-02-12 14:53:33 -0500161 hnd->gpuaddr = 0;
162
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700163 *pHandle = hnd;
164 }
165
166 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800167
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700168 return err;
169}
170
171void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700172 int *bufferType)
173{
174 *bufferType = BUFFER_TYPE_VIDEO;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700175
Naseer Ahmed65f16562012-06-15 20:53:42 -0700176 if (inputFormat < 0x7) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700177 // RGB formats
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700178 *bufferType = BUFFER_TYPE_UI;
179 } else if ((inputFormat == HAL_PIXEL_FORMAT_R_8) ||
180 (inputFormat == HAL_PIXEL_FORMAT_RG_88)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700181 *bufferType = BUFFER_TYPE_UI;
182 }
183}
184
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400185int gpu_context_t::gralloc_alloc_framebuffer_locked(size_t size, int usage,
186 buffer_handle_t* pHandle)
187{
188 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
189
190 // we don't support framebuffer allocations with graphics heap flags
191 if (usage & GRALLOC_HEAP_MASK) {
192 return -EINVAL;
193 }
194
195 if (m->framebuffer == NULL) {
196 ALOGE("%s: Invalid framebuffer", __FUNCTION__);
197 return -EINVAL;
198 }
199
200 const uint32_t bufferMask = m->bufferMask;
201 const uint32_t numBuffers = m->numBuffers;
202 size_t 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 intptr_t vaddr = intptr_t(m->framebuffer->base);
223 private_handle_t* hnd = new private_handle_t(
224 dup(m->framebuffer->fd), bufferSize,
225 private_handle_t::PRIV_FLAGS_USES_PMEM |
226 private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
227 BUFFER_TYPE_UI, m->fbFormat, m->info.xres,
228 m->info.yres);
229
230 // find a free slot
231 for (uint32_t i=0 ; i<numBuffers ; i++) {
232 if ((bufferMask & (1LU<<i)) == 0) {
233 m->bufferMask |= (1LU<<i);
234 break;
235 }
236 vaddr += bufferSize;
237 }
238 hnd->base = vaddr;
239 hnd->offset = vaddr - intptr_t(m->framebuffer->base);
240 *pHandle = hnd;
241 return 0;
242}
243
244
245int gpu_context_t::gralloc_alloc_framebuffer(size_t size, int usage,
246 buffer_handle_t* pHandle)
247{
248 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
249 pthread_mutex_lock(&m->lock);
250 int err = gralloc_alloc_framebuffer_locked(size, usage, pHandle);
251 pthread_mutex_unlock(&m->lock);
252 return err;
253}
254
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700255int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700256 buffer_handle_t* pHandle, int* pStride,
257 size_t bufferSize) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700258 if (!pHandle || !pStride)
259 return -EINVAL;
260
261 size_t size;
262 int alignedw, alignedh;
Naseer Ahmed59802502012-08-21 17:03:27 -0400263 int grallocFormat = format;
264 int bufferType;
Naseer Ahmed59802502012-08-21 17:03:27 -0400265
Saurabh Shahaedf2362012-09-05 11:30:59 -0700266 //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
267 //the usage bits, gralloc assigns a format.
268 if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
269 if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
Shuzhen Wanga11b6ba2013-04-21 14:23:05 -0700270 grallocFormat = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; //NV12
Saurabh Shahaedf2362012-09-05 11:30:59 -0700271 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 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400276
Saurabh Shahaedf2362012-09-05 11:30:59 -0700277 getGrallocInformationFromFormat(grallocFormat, &bufferType);
Naseer Ahmed59802502012-08-21 17:03:27 -0400278 size = getBufferSizeAndDimensions(w, h, grallocFormat, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700279
280 if ((ssize_t)size <= 0)
281 return -EINVAL;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800282 size = (bufferSize >= size)? bufferSize : size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700283
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400284 bool useFbMem = false;
285 char property[PROPERTY_VALUE_MAX];
286 if((usage & GRALLOC_USAGE_HW_FB) &&
287 (property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
288 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
289 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
290 useFbMem = true;
291 }
292
293 int err = 0;
294 if(useFbMem) {
295 err = gralloc_alloc_framebuffer(size, usage, pHandle);
296 } else {
297 err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
298 grallocFormat, alignedw, alignedh);
299 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700300
301 if (err < 0) {
302 return err;
303 }
304
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700305 *pStride = alignedw;
306 return 0;
307}
308
309int gpu_context_t::free_impl(private_handle_t const* hnd) {
310 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400311 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
312 const size_t bufferSize = m->finfo.line_length * m->info.yres;
313 int index = (hnd->base - m->framebuffer->base) / bufferSize;
314 m->bufferMask &= ~(1<<index);
315 } else {
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800316
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400317 terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
318 IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
319 int err = memalloc->free_buffer((void*)hnd->base, (size_t) hnd->size,
320 hnd->offset, hnd->fd);
321 if(err)
322 return err;
323 // free the metadata space
324 unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
325 err = memalloc->free_buffer((void*)hnd->base_metadata,
326 (size_t) size, hnd->offset_metadata,
327 hnd->fd_metadata);
328 if (err)
329 return err;
330 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700331 delete hnd;
332 return 0;
333}
334
335int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700336 int usage, buffer_handle_t* pHandle,
337 int* pStride)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700338{
339 if (!dev) {
340 return -EINVAL;
341 }
342 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
343 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0);
344}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700345int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h,
346 int format, int usage,
347 buffer_handle_t* pHandle, int* pStride,
348 int bufferSize)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700349{
350 if (!dev) {
351 return -EINVAL;
352 }
353 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
354 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize);
355}
356
357
358int gpu_context_t::gralloc_free(alloc_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700359 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700360{
361 if (private_handle_t::validate(handle) < 0)
362 return -EINVAL;
363
364 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
365 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
366 return gpu->free_impl(hnd);
367}
368
369/*****************************************************************************/
370
371int gpu_context_t::gralloc_close(struct hw_device_t *dev)
372{
373 gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev);
374 if (ctx) {
375 /* TODO: keep a list of all buffer_handle_t created, and free them
376 * all here.
377 */
378 delete ctx;
379 }
380 return 0;
381}
382