blob: 2db62740065065cb46fa6aa986c5a9313527c9eb [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 * Copyright (c) 2011-2012 Code Aurora Forum. 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 <genlock.h>
25
26#include "gr.h"
27#include "gpu.h"
28#include "memalloc.h"
29#include "alloc_controller.h"
30
31using namespace gralloc;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070032
33gpu_context_t::gpu_context_t(const private_module_t* module,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070034 IAllocController* alloc_ctrl ) :
Iliyan Malchev202a77d2012-06-11 14:41:12 -070035 mAllocCtrl(alloc_ctrl)
36{
37 // Zero out the alloc_device_t
38 memset(static_cast<alloc_device_t*>(this), 0, sizeof(alloc_device_t));
39
Iliyan Malchev202a77d2012-06-11 14:41:12 -070040 // Initialize the procs
41 common.tag = HARDWARE_DEVICE_TAG;
42 common.version = 0;
43 common.module = const_cast<hw_module_t*>(&module->base.common);
44 common.close = gralloc_close;
45 alloc = gralloc_alloc;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070046 free = gralloc_free;
47
48}
49
50int gpu_context_t::gralloc_alloc_framebuffer_locked(size_t size, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -070051 buffer_handle_t* pHandle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070052{
53 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
54
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070055 // we don't support framebuffer allocations with graphics heap flags
56 if (usage & GRALLOC_HEAP_MASK) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -070057 return -EINVAL;
58 }
59
60 if (m->framebuffer == NULL) {
61 ALOGE("%s: Invalid framebuffer", __FUNCTION__);
62 return -EINVAL;
63 }
64
65 const uint32_t bufferMask = m->bufferMask;
66 const uint32_t numBuffers = m->numBuffers;
67 size_t bufferSize = m->finfo.line_length * m->info.yres;
68
69 //adreno needs FB size to be page aligned
70 bufferSize = roundUpToPageSize(bufferSize);
71
72 if (numBuffers == 1) {
73 // If we have only one buffer, we never use page-flipping. Instead,
74 // we return a regular buffer which will be memcpy'ed to the main
75 // screen when post is called.
76 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
77 return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI,
78 m->fbFormat, m->info.xres, m->info.yres);
79 }
80
81 if (bufferMask >= ((1LU<<numBuffers)-1)) {
82 // We ran out of buffers.
83 return -ENOMEM;
84 }
85
86 // create a "fake" handles for it
87 // Set the PMEM flag as well, since adreno
88 // treats the FB memory as pmem
89 intptr_t vaddr = intptr_t(m->framebuffer->base);
90 private_handle_t* hnd = new private_handle_t(dup(m->framebuffer->fd), bufferSize,
91 private_handle_t::PRIV_FLAGS_USES_PMEM |
92 private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
93 BUFFER_TYPE_UI, m->fbFormat, m->info.xres,
94 m->info.yres);
95
96 // find a free slot
97 for (uint32_t i=0 ; i<numBuffers ; i++) {
98 if ((bufferMask & (1LU<<i)) == 0) {
99 m->bufferMask |= (1LU<<i);
100 break;
101 }
102 vaddr += bufferSize;
103 }
104
105 hnd->base = vaddr;
106 hnd->offset = vaddr - intptr_t(m->framebuffer->base);
107 *pHandle = hnd;
108 return 0;
109}
110
111
112int gpu_context_t::gralloc_alloc_framebuffer(size_t size, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700113 buffer_handle_t* pHandle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700114{
115 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
116 pthread_mutex_lock(&m->lock);
117 int err = gralloc_alloc_framebuffer_locked(size, usage, pHandle);
118 pthread_mutex_unlock(&m->lock);
119 return err;
120}
121
122int gpu_context_t::gralloc_alloc_buffer(size_t size, int usage,
123 buffer_handle_t* pHandle, int bufferType,
124 int format, int width, int height)
125{
126 int err = 0;
127 int flags = 0;
128 size = roundUpToPageSize(size);
129 alloc_data data;
130 data.offset = 0;
131 data.fd = -1;
132 data.base = 0;
133 data.size = size;
134 if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED)
135 data.align = 8192;
136 else
137 data.align = getpagesize();
138 data.pHandle = (unsigned int) pHandle;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700139 err = mAllocCtrl->allocate(data, usage);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700140
141 if (usage & GRALLOC_USAGE_PRIVATE_UNSYNCHRONIZED) {
142 flags |= private_handle_t::PRIV_FLAGS_UNSYNCHRONIZED;
143 }
144
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700145 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700146 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
147 //The EXTERNAL_BLOCK flag is always an add-on
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700148 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_BLOCK) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700149 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_BLOCK;
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700150 }if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_CC) {
151 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_CC;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700152 }
153 }
154
155 if (err == 0) {
156 flags |= data.allocType;
157 private_handle_t* hnd = new private_handle_t(data.fd, size, flags,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700158 bufferType, format, width,
159 height);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700160
161 hnd->offset = data.offset;
162 hnd->base = int(data.base) + data.offset;
163 *pHandle = hnd;
164 }
165
166 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
167 return err;
168}
169
170void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
171 int *colorFormat,
172 int *bufferType)
173{
174 *bufferType = BUFFER_TYPE_VIDEO;
175 *colorFormat = inputFormat;
176
Naseer Ahmed65f16562012-06-15 20:53:42 -0700177 if (inputFormat < 0x7) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700178 // RGB formats
179 *colorFormat = inputFormat;
180 *bufferType = BUFFER_TYPE_UI;
181 } else if ((inputFormat == HAL_PIXEL_FORMAT_R_8) ||
182 (inputFormat == HAL_PIXEL_FORMAT_RG_88)) {
183 *colorFormat = inputFormat;
184 *bufferType = BUFFER_TYPE_UI;
185 }
186}
187
188int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700189 buffer_handle_t* pHandle, int* pStride,
190 size_t bufferSize) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700191 if (!pHandle || !pStride)
192 return -EINVAL;
193
194 size_t size;
195 int alignedw, alignedh;
196 int colorFormat, bufferType;
197 getGrallocInformationFromFormat(format, &colorFormat, &bufferType);
198 size = getBufferSizeAndDimensions(w, h, colorFormat, alignedw, alignedh);
199
200 if ((ssize_t)size <= 0)
201 return -EINVAL;
202 size = (bufferSize >= size)? bufferSize : size;
203
204 // All buffers marked as protected or for external
205 // display need to go to overlay
206 if ((usage & GRALLOC_USAGE_EXTERNAL_DISP) ||
Naseer Ahmed29a26812012-06-14 00:56:20 -0700207 (usage & GRALLOC_USAGE_PROTECTED) ||
208 (usage & GRALLOC_USAGE_PRIVATE_CP_BUFFER)) {
209 bufferType = BUFFER_TYPE_VIDEO;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700210 }
211 int err;
212 if (usage & GRALLOC_USAGE_HW_FB) {
213 err = gralloc_alloc_framebuffer(size, usage, pHandle);
214 } else {
215 err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
216 format, alignedw, alignedh);
217 }
218
219 if (err < 0) {
220 return err;
221 }
222
223 // Create a genlock lock for this buffer handle.
224 err = genlock_create_lock((native_handle_t*)(*pHandle));
225 if (err) {
226 ALOGE("%s: genlock_create_lock failed", __FUNCTION__);
227 free_impl(reinterpret_cast<private_handle_t*>(pHandle));
228 return err;
229 }
230 *pStride = alignedw;
231 return 0;
232}
233
234int gpu_context_t::free_impl(private_handle_t const* hnd) {
235 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
236 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
237 // free this buffer
238 const size_t bufferSize = m->finfo.line_length * m->info.yres;
239 int index = (hnd->base - m->framebuffer->base) / bufferSize;
240 m->bufferMask &= ~(1<<index);
241 } else {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700242 terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700243 IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700244 int err = memalloc->free_buffer((void*)hnd->base, (size_t) hnd->size,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700245 hnd->offset, hnd->fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700246 if(err)
247 return err;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700248 }
249
250 // Release the genlock
251 int err = genlock_release_lock((native_handle_t*)hnd);
252 if (err) {
253 ALOGE("%s: genlock_release_lock failed", __FUNCTION__);
254 }
255
256 delete hnd;
257 return 0;
258}
259
260int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700261 int usage, buffer_handle_t* pHandle,
262 int* pStride)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700263{
264 if (!dev) {
265 return -EINVAL;
266 }
267 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
268 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0);
269}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700270int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h,
271 int format, int usage,
272 buffer_handle_t* pHandle, int* pStride,
273 int bufferSize)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700274{
275 if (!dev) {
276 return -EINVAL;
277 }
278 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
279 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize);
280}
281
282
283int gpu_context_t::gralloc_free(alloc_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700284 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700285{
286 if (private_handle_t::validate(handle) < 0)
287 return -EINVAL;
288
289 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
290 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
291 return gpu->free_impl(hnd);
292}
293
294/*****************************************************************************/
295
296int gpu_context_t::gralloc_close(struct hw_device_t *dev)
297{
298 gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev);
299 if (ctx) {
300 /* TODO: keep a list of all buffer_handle_t created, and free them
301 * all here.
302 */
303 delete ctx;
304 }
305 return 0;
306}
307