blob: f1af045826733d99332d38af9066067a87e90d8b [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Duy Truong73d36df2013-02-09 20:33:23 -08003 * Copyright (c) 2011-2012 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
24#include <genlock.h>
25
26#include "gr.h"
27#include "gpu.h"
28#include "memalloc.h"
29#include "alloc_controller.h"
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080030#include <qdMetaData.h>
Praveen Chavan4e931c12012-11-28 19:43:17 -080031#include "mdp_version.h"
Iliyan Malchev202a77d2012-06-11 14:41:12 -070032
33using namespace gralloc;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070034
Praveen Chavan4e931c12012-11-28 19:43:17 -080035#define SZ_1M 0x100000
36
Iliyan Malchev202a77d2012-06-11 14:41:12 -070037gpu_context_t::gpu_context_t(const private_module_t* module,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070038 IAllocController* alloc_ctrl ) :
Iliyan Malchev202a77d2012-06-11 14:41:12 -070039 mAllocCtrl(alloc_ctrl)
40{
41 // Zero out the alloc_device_t
42 memset(static_cast<alloc_device_t*>(this), 0, sizeof(alloc_device_t));
43
Iliyan Malchev202a77d2012-06-11 14:41:12 -070044 // Initialize the procs
45 common.tag = HARDWARE_DEVICE_TAG;
46 common.version = 0;
47 common.module = const_cast<hw_module_t*>(&module->base.common);
48 common.close = gralloc_close;
49 alloc = gralloc_alloc;
Naseer Ahmed74214722013-02-09 08:11:36 -050050#ifdef QCOM_BSP
Ramkumar Radhakrishnan3d4c0ac2012-12-10 15:42:54 -080051 allocSize = gralloc_alloc_size;
Naseer Ahmed74214722013-02-09 08:11:36 -050052#endif
Iliyan Malchev202a77d2012-06-11 14:41:12 -070053 free = gralloc_free;
54
55}
56
57int gpu_context_t::gralloc_alloc_framebuffer_locked(size_t size, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -070058 buffer_handle_t* pHandle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070059{
60 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
61
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070062 // we don't support framebuffer allocations with graphics heap flags
63 if (usage & GRALLOC_HEAP_MASK) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -070064 return -EINVAL;
65 }
66
67 if (m->framebuffer == NULL) {
68 ALOGE("%s: Invalid framebuffer", __FUNCTION__);
69 return -EINVAL;
70 }
71
72 const uint32_t bufferMask = m->bufferMask;
73 const uint32_t numBuffers = m->numBuffers;
74 size_t bufferSize = m->finfo.line_length * m->info.yres;
75
76 //adreno needs FB size to be page aligned
77 bufferSize = roundUpToPageSize(bufferSize);
78
79 if (numBuffers == 1) {
80 // If we have only one buffer, we never use page-flipping. Instead,
81 // we return a regular buffer which will be memcpy'ed to the main
82 // screen when post is called.
83 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
84 return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI,
85 m->fbFormat, m->info.xres, m->info.yres);
86 }
87
88 if (bufferMask >= ((1LU<<numBuffers)-1)) {
89 // We ran out of buffers.
90 return -ENOMEM;
91 }
92
Naseer Ahmedd7f84272012-12-13 13:09:53 -050093 // create a "fake" handle for it
Iliyan Malchev202a77d2012-06-11 14:41:12 -070094 intptr_t vaddr = intptr_t(m->framebuffer->base);
Naseer Ahmedd7f84272012-12-13 13:09:53 -050095 private_handle_t* hnd = new private_handle_t(
96 dup(m->framebuffer->fd), bufferSize,
97 private_handle_t::PRIV_FLAGS_USES_ION |
98 private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
99 BUFFER_TYPE_UI, m->fbFormat, m->info.xres,
100 m->info.yres);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700101
102 // find a free slot
103 for (uint32_t i=0 ; i<numBuffers ; i++) {
104 if ((bufferMask & (1LU<<i)) == 0) {
105 m->bufferMask |= (1LU<<i);
106 break;
107 }
108 vaddr += bufferSize;
109 }
110
111 hnd->base = vaddr;
112 hnd->offset = vaddr - intptr_t(m->framebuffer->base);
113 *pHandle = hnd;
114 return 0;
115}
116
117
118int gpu_context_t::gralloc_alloc_framebuffer(size_t size, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700119 buffer_handle_t* pHandle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700120{
121 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
122 pthread_mutex_lock(&m->lock);
123 int err = gralloc_alloc_framebuffer_locked(size, usage, pHandle);
124 pthread_mutex_unlock(&m->lock);
125 return err;
126}
127
128int gpu_context_t::gralloc_alloc_buffer(size_t size, int usage,
129 buffer_handle_t* pHandle, int bufferType,
130 int format, int width, int height)
131{
132 int err = 0;
133 int flags = 0;
134 size = roundUpToPageSize(size);
135 alloc_data data;
136 data.offset = 0;
137 data.fd = -1;
138 data.base = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700139 if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED)
140 data.align = 8192;
141 else
142 data.align = getpagesize();
Praveen Chavan4e931c12012-11-28 19:43:17 -0800143
144 /* force 1MB alignment selectively for secure buffers, MDP5 onwards */
145 if ((qdutils::MDPVersion::getInstance().getMDPVersion() >= \
146 qdutils::MDSS_V5) && (usage & GRALLOC_USAGE_PROTECTED)) {
147 data.align = ALIGN(data.align, SZ_1M);
148 size = ALIGN(size, data.align);
149 }
150 data.size = size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700151 data.pHandle = (unsigned int) pHandle;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700152 err = mAllocCtrl->allocate(data, usage);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700153
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800154 if (!err) {
155 /* allocate memory for enhancement data */
156 alloc_data eData;
157 eData.fd = -1;
158 eData.base = 0;
159 eData.offset = 0;
160 eData.size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
161 eData.pHandle = data.pHandle;
162 eData.align = getpagesize();
163 int eDataUsage = GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP;
164 int eDataErr = mAllocCtrl->allocate(eData, eDataUsage);
165 ALOGE_IF(eDataErr, "gralloc failed for eDataErr=%s",
166 strerror(-eDataErr));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700167
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800168 if (usage & GRALLOC_USAGE_PRIVATE_UNSYNCHRONIZED) {
169 flags |= private_handle_t::PRIV_FLAGS_UNSYNCHRONIZED;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700170 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700171
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800172 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY) {
173 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
174 //The EXTERNAL_BLOCK flag is always an add-on
175 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_BLOCK) {
176 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_BLOCK;
177 }
178 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_CC) {
179 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_CC;
180 }
181 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400182
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800183 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) {
184 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
185 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400186
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800187 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
188 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
189 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400190
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800191 if (usage & GRALLOC_USAGE_HW_CAMERA_READ) {
192 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
193 }
194
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700195 flags |= data.allocType;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800196 int eBaseAddr = int(eData.base) + eData.offset;
197 private_handle_t *hnd = new private_handle_t(data.fd, size, flags,
198 bufferType, format, width, height, eData.fd, eData.offset,
199 eBaseAddr);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700200
201 hnd->offset = data.offset;
202 hnd->base = int(data.base) + data.offset;
203 *pHandle = hnd;
204 }
205
206 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800207
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700208 return err;
209}
210
211void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700212 int *bufferType)
213{
214 *bufferType = BUFFER_TYPE_VIDEO;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700215
Naseer Ahmed65f16562012-06-15 20:53:42 -0700216 if (inputFormat < 0x7) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700217 // RGB formats
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700218 *bufferType = BUFFER_TYPE_UI;
219 } else if ((inputFormat == HAL_PIXEL_FORMAT_R_8) ||
220 (inputFormat == HAL_PIXEL_FORMAT_RG_88)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700221 *bufferType = BUFFER_TYPE_UI;
222 }
223}
224
225int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700226 buffer_handle_t* pHandle, int* pStride,
227 size_t bufferSize) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700228 if (!pHandle || !pStride)
229 return -EINVAL;
230
231 size_t size;
232 int alignedw, alignedh;
Naseer Ahmed59802502012-08-21 17:03:27 -0400233 int grallocFormat = format;
234 int bufferType;
Naseer Ahmed59802502012-08-21 17:03:27 -0400235
Saurabh Shahaedf2362012-09-05 11:30:59 -0700236 //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
237 //the usage bits, gralloc assigns a format.
238 if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
239 if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
240 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP; //NV12
241 else if(usage & GRALLOC_USAGE_HW_CAMERA_READ)
242 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
243 else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
244 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
245 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400246
Saurabh Shahaedf2362012-09-05 11:30:59 -0700247 getGrallocInformationFromFormat(grallocFormat, &bufferType);
Naseer Ahmed59802502012-08-21 17:03:27 -0400248 size = getBufferSizeAndDimensions(w, h, grallocFormat, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700249
250 if ((ssize_t)size <= 0)
251 return -EINVAL;
Ramkumar Radhakrishnan3d4c0ac2012-12-10 15:42:54 -0800252 size = (bufferSize != 0)? bufferSize : size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700253
254 // All buffers marked as protected or for external
255 // display need to go to overlay
256 if ((usage & GRALLOC_USAGE_EXTERNAL_DISP) ||
Sushil Chauhan7651a802013-01-08 16:08:09 -0800257 (usage & GRALLOC_USAGE_PROTECTED)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700258 bufferType = BUFFER_TYPE_VIDEO;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700259 }
Naseer Ahmed768619e2012-11-28 17:02:52 -0500260
261 int err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
262 grallocFormat, alignedw, alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700263
264 if (err < 0) {
265 return err;
266 }
267
268 // Create a genlock lock for this buffer handle.
269 err = genlock_create_lock((native_handle_t*)(*pHandle));
270 if (err) {
271 ALOGE("%s: genlock_create_lock failed", __FUNCTION__);
272 free_impl(reinterpret_cast<private_handle_t*>(pHandle));
273 return err;
274 }
275 *pStride = alignedw;
276 return 0;
277}
278
279int gpu_context_t::free_impl(private_handle_t const* hnd) {
280 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
281 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
282 // free this buffer
283 const size_t bufferSize = m->finfo.line_length * m->info.yres;
284 int index = (hnd->base - m->framebuffer->base) / bufferSize;
285 m->bufferMask &= ~(1<<index);
286 } else {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700287 terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700288 IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700289 int err = memalloc->free_buffer((void*)hnd->base, (size_t) hnd->size,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700290 hnd->offset, hnd->fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700291 if(err)
292 return err;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800293 // free the metadata space
294 unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
295 err = memalloc->free_buffer((void*)hnd->base_metadata,
296 (size_t) size, hnd->offset_metadata,
297 hnd->fd_metadata);
298 if (err)
299 return err;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700300 }
301
302 // Release the genlock
303 int err = genlock_release_lock((native_handle_t*)hnd);
304 if (err) {
305 ALOGE("%s: genlock_release_lock failed", __FUNCTION__);
306 }
307
308 delete hnd;
309 return 0;
310}
311
312int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700313 int usage, buffer_handle_t* pHandle,
314 int* pStride)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700315{
316 if (!dev) {
317 return -EINVAL;
318 }
319 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
320 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0);
321}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700322int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h,
323 int format, int usage,
324 buffer_handle_t* pHandle, int* pStride,
325 int bufferSize)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700326{
327 if (!dev) {
328 return -EINVAL;
329 }
330 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
331 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize);
332}
333
334
335int gpu_context_t::gralloc_free(alloc_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700336 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700337{
338 if (private_handle_t::validate(handle) < 0)
339 return -EINVAL;
340
341 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
342 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
343 return gpu->free_impl(hnd);
344}
345
346/*****************************************************************************/
347
348int gpu_context_t::gralloc_close(struct hw_device_t *dev)
349{
350 gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev);
351 if (ctx) {
352 /* TODO: keep a list of all buffer_handle_t created, and free them
353 * all here.
354 */
355 delete ctx;
356 }
357 return 0;
358}
359