blob: e5dfcc19d699905e86afd1f3da0aaeca87d0a7d8 [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>
Praveen Chavan4e931c12012-11-28 19:43:17 -080029#include "mdp_version.h"
Iliyan Malchev202a77d2012-06-11 14:41:12 -070030
31using namespace gralloc;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070032
Praveen Chavan4e931c12012-11-28 19:43:17 -080033#define SZ_1M 0x100000
34
Iliyan Malchev202a77d2012-06-11 14:41:12 -070035gpu_context_t::gpu_context_t(const private_module_t* module,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070036 IAllocController* alloc_ctrl ) :
Iliyan Malchev202a77d2012-06-11 14:41:12 -070037 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
Iliyan Malchev202a77d2012-06-11 14:41:12 -070042 // 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;
Naseer Ahmed74214722013-02-09 08:11:36 -050048#ifdef QCOM_BSP
Ramkumar Radhakrishnan3d4c0ac2012-12-10 15:42:54 -080049 allocSize = gralloc_alloc_size;
Naseer Ahmed74214722013-02-09 08:11:36 -050050#endif
Iliyan Malchev202a77d2012-06-11 14:41:12 -070051 free = gralloc_free;
52
53}
54
Iliyan Malchev202a77d2012-06-11 14:41:12 -070055int 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;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070066 if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED)
67 data.align = 8192;
68 else
69 data.align = getpagesize();
Praveen Chavan4e931c12012-11-28 19:43:17 -080070
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;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070078 data.pHandle = (unsigned int) pHandle;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070079 err = mAllocCtrl->allocate(data, usage);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070080
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080081 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));
Iliyan Malchev202a77d2012-06-11 14:41:12 -070094
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080095 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 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400105
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800106 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) {
107 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
108 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400109
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800110 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
111 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
112 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400113
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800114 if (usage & GRALLOC_USAGE_HW_CAMERA_READ) {
115 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
116 }
117
Naseer Ahmede41ad9c2013-04-05 17:59:28 -0400118 if (usage & GRALLOC_USAGE_HW_COMPOSER) {
119 flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER;
120 }
121
Shuzhen Wang46ca2262013-04-10 23:02:22 -0700122 if (usage & GRALLOC_USAGE_HW_TEXTURE) {
123 flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
124 }
125
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700126 flags |= data.allocType;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800127 int eBaseAddr = int(eData.base) + eData.offset;
128 private_handle_t *hnd = new private_handle_t(data.fd, size, flags,
129 bufferType, format, width, height, eData.fd, eData.offset,
130 eBaseAddr);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700131
132 hnd->offset = data.offset;
133 hnd->base = int(data.base) + data.offset;
Naseer Ahmeda163b732013-02-12 14:53:33 -0500134 hnd->gpuaddr = 0;
135
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700136 *pHandle = hnd;
137 }
138
139 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800140
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700141 return err;
142}
143
144void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700145 int *bufferType)
146{
147 *bufferType = BUFFER_TYPE_VIDEO;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700148
Naseer Ahmed65f16562012-06-15 20:53:42 -0700149 if (inputFormat < 0x7) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700150 // RGB formats
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700151 *bufferType = BUFFER_TYPE_UI;
152 } else if ((inputFormat == HAL_PIXEL_FORMAT_R_8) ||
153 (inputFormat == HAL_PIXEL_FORMAT_RG_88)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700154 *bufferType = BUFFER_TYPE_UI;
155 }
156}
157
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400158int gpu_context_t::gralloc_alloc_framebuffer_locked(size_t size, int usage,
159 buffer_handle_t* pHandle)
160{
161 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
162
163 // we don't support framebuffer allocations with graphics heap flags
164 if (usage & GRALLOC_HEAP_MASK) {
165 return -EINVAL;
166 }
167
168 if (m->framebuffer == NULL) {
169 ALOGE("%s: Invalid framebuffer", __FUNCTION__);
170 return -EINVAL;
171 }
172
173 const uint32_t bufferMask = m->bufferMask;
174 const uint32_t numBuffers = m->numBuffers;
175 size_t bufferSize = m->finfo.line_length * m->info.yres;
176
177 //adreno needs FB size to be page aligned
178 bufferSize = roundUpToPageSize(bufferSize);
179
180 if (numBuffers == 1) {
181 // If we have only one buffer, we never use page-flipping. Instead,
182 // we return a regular buffer which will be memcpy'ed to the main
183 // screen when post is called.
184 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
185 return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI,
186 m->fbFormat, m->info.xres, m->info.yres);
187 }
188
189 if (bufferMask >= ((1LU<<numBuffers)-1)) {
190 // We ran out of buffers.
191 return -ENOMEM;
192 }
193
194 // create a "fake" handle for it
195 intptr_t vaddr = intptr_t(m->framebuffer->base);
196 private_handle_t* hnd = new private_handle_t(
197 dup(m->framebuffer->fd), bufferSize,
198 private_handle_t::PRIV_FLAGS_USES_PMEM |
199 private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
200 BUFFER_TYPE_UI, m->fbFormat, m->info.xres,
201 m->info.yres);
202
203 // find a free slot
204 for (uint32_t i=0 ; i<numBuffers ; i++) {
205 if ((bufferMask & (1LU<<i)) == 0) {
206 m->bufferMask |= (1LU<<i);
207 break;
208 }
209 vaddr += bufferSize;
210 }
211 hnd->base = vaddr;
212 hnd->offset = vaddr - intptr_t(m->framebuffer->base);
213 *pHandle = hnd;
214 return 0;
215}
216
217
218int gpu_context_t::gralloc_alloc_framebuffer(size_t size, int usage,
219 buffer_handle_t* pHandle)
220{
221 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
222 pthread_mutex_lock(&m->lock);
223 int err = gralloc_alloc_framebuffer_locked(size, usage, pHandle);
224 pthread_mutex_unlock(&m->lock);
225 return err;
226}
227
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700228int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700229 buffer_handle_t* pHandle, int* pStride,
230 size_t bufferSize) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700231 if (!pHandle || !pStride)
232 return -EINVAL;
233
234 size_t size;
235 int alignedw, alignedh;
Naseer Ahmed59802502012-08-21 17:03:27 -0400236 int grallocFormat = format;
237 int bufferType;
Naseer Ahmed59802502012-08-21 17:03:27 -0400238
Saurabh Shahaedf2362012-09-05 11:30:59 -0700239 //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
240 //the usage bits, gralloc assigns a format.
241 if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
242 if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
243 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP; //NV12
244 else if(usage & GRALLOC_USAGE_HW_CAMERA_READ)
245 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
246 else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
247 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
248 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400249
Saurabh Shahaedf2362012-09-05 11:30:59 -0700250 getGrallocInformationFromFormat(grallocFormat, &bufferType);
Naseer Ahmed59802502012-08-21 17:03:27 -0400251 size = getBufferSizeAndDimensions(w, h, grallocFormat, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700252
253 if ((ssize_t)size <= 0)
254 return -EINVAL;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800255 size = (bufferSize >= size)? bufferSize : size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700256
257 // All buffers marked as protected or for external
258 // display need to go to overlay
259 if ((usage & GRALLOC_USAGE_EXTERNAL_DISP) ||
Sushil Chauhan7651a802013-01-08 16:08:09 -0800260 (usage & GRALLOC_USAGE_PROTECTED)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700261 bufferType = BUFFER_TYPE_VIDEO;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700262 }
Naseer Ahmed768619e2012-11-28 17:02:52 -0500263
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400264 bool useFbMem = false;
265 char property[PROPERTY_VALUE_MAX];
266 if((usage & GRALLOC_USAGE_HW_FB) &&
267 (property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
268 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
269 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
270 useFbMem = true;
271 }
272
273 int err = 0;
274 if(useFbMem) {
275 err = gralloc_alloc_framebuffer(size, usage, pHandle);
276 } else {
277 err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
278 grallocFormat, alignedw, alignedh);
279 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700280
281 if (err < 0) {
282 return err;
283 }
284
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700285 *pStride = alignedw;
286 return 0;
287}
288
289int gpu_context_t::free_impl(private_handle_t const* hnd) {
290 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400291 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
292 const size_t bufferSize = m->finfo.line_length * m->info.yres;
293 int index = (hnd->base - m->framebuffer->base) / bufferSize;
294 m->bufferMask &= ~(1<<index);
295 } else {
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800296
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400297 terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
298 IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
299 int err = memalloc->free_buffer((void*)hnd->base, (size_t) hnd->size,
300 hnd->offset, hnd->fd);
301 if(err)
302 return err;
303 // free the metadata space
304 unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
305 err = memalloc->free_buffer((void*)hnd->base_metadata,
306 (size_t) size, hnd->offset_metadata,
307 hnd->fd_metadata);
308 if (err)
309 return err;
310 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700311 delete hnd;
312 return 0;
313}
314
315int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700316 int usage, buffer_handle_t* pHandle,
317 int* pStride)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700318{
319 if (!dev) {
320 return -EINVAL;
321 }
322 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
323 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0);
324}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700325int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h,
326 int format, int usage,
327 buffer_handle_t* pHandle, int* pStride,
328 int bufferSize)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700329{
330 if (!dev) {
331 return -EINVAL;
332 }
333 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
334 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize);
335}
336
337
338int gpu_context_t::gralloc_free(alloc_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700339 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700340{
341 if (private_handle_t::validate(handle) < 0)
342 return -EINVAL;
343
344 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
345 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
346 return gpu->free_impl(hnd);
347}
348
349/*****************************************************************************/
350
351int gpu_context_t::gralloc_close(struct hw_device_t *dev)
352{
353 gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev);
354 if (ctx) {
355 /* TODO: keep a list of all buffer_handle_t created, and free them
356 * all here.
357 */
358 delete ctx;
359 }
360 return 0;
361}
362