blob: 4f00e4ab20e4ee1fa6ddc7fe7bfb74b464834816 [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;
32using android::sp;
33
34gpu_context_t::gpu_context_t(const private_module_t* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -070035 sp<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;
47#if 0
48 allocSize = gralloc_alloc_size;
49#endif
50 free = gralloc_free;
51
52}
53
54int gpu_context_t::gralloc_alloc_framebuffer_locked(size_t size, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -070055 buffer_handle_t* pHandle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070056{
57 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
58
59 // we don't support allocations with both the FB and PMEM_ADSP flags
60 if (usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP) {
61 return -EINVAL;
62 }
63
64 if (m->framebuffer == NULL) {
65 ALOGE("%s: Invalid framebuffer", __FUNCTION__);
66 return -EINVAL;
67 }
68
69 const uint32_t bufferMask = m->bufferMask;
70 const uint32_t numBuffers = m->numBuffers;
71 size_t bufferSize = m->finfo.line_length * m->info.yres;
72
73 //adreno needs FB size to be page aligned
74 bufferSize = roundUpToPageSize(bufferSize);
75
76 if (numBuffers == 1) {
77 // If we have only one buffer, we never use page-flipping. Instead,
78 // we return a regular buffer which will be memcpy'ed to the main
79 // screen when post is called.
80 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
81 return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI,
82 m->fbFormat, m->info.xres, m->info.yres);
83 }
84
85 if (bufferMask >= ((1LU<<numBuffers)-1)) {
86 // We ran out of buffers.
87 return -ENOMEM;
88 }
89
90 // create a "fake" handles for it
91 // Set the PMEM flag as well, since adreno
92 // treats the FB memory as pmem
93 intptr_t vaddr = intptr_t(m->framebuffer->base);
94 private_handle_t* hnd = new private_handle_t(dup(m->framebuffer->fd), bufferSize,
95 private_handle_t::PRIV_FLAGS_USES_PMEM |
96 private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
97 BUFFER_TYPE_UI, m->fbFormat, m->info.xres,
98 m->info.yres);
99
100 // find a free slot
101 for (uint32_t i=0 ; i<numBuffers ; i++) {
102 if ((bufferMask & (1LU<<i)) == 0) {
103 m->bufferMask |= (1LU<<i);
104 break;
105 }
106 vaddr += bufferSize;
107 }
108
109 hnd->base = vaddr;
110 hnd->offset = vaddr - intptr_t(m->framebuffer->base);
111 *pHandle = hnd;
112 return 0;
113}
114
115
116int gpu_context_t::gralloc_alloc_framebuffer(size_t size, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700117 buffer_handle_t* pHandle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700118{
119 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
120 pthread_mutex_lock(&m->lock);
121 int err = gralloc_alloc_framebuffer_locked(size, usage, pHandle);
122 pthread_mutex_unlock(&m->lock);
123 return err;
124}
125
126int gpu_context_t::gralloc_alloc_buffer(size_t size, int usage,
127 buffer_handle_t* pHandle, int bufferType,
128 int format, int width, int height)
129{
130 int err = 0;
131 int flags = 0;
132 size = roundUpToPageSize(size);
133 alloc_data data;
134 data.offset = 0;
135 data.fd = -1;
136 data.base = 0;
137 data.size = size;
138 if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED)
139 data.align = 8192;
140 else
141 data.align = getpagesize();
142 data.pHandle = (unsigned int) pHandle;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700143 err = mAllocCtrl->allocate(data, usage, 0);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700144
145 if (usage & GRALLOC_USAGE_PRIVATE_UNSYNCHRONIZED) {
146 flags |= private_handle_t::PRIV_FLAGS_UNSYNCHRONIZED;
147 }
148
149 if (usage & GRALLOC_USAGE_EXTERNAL_ONLY) {
150 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
151 //The EXTERNAL_BLOCK flag is always an add-on
152 if (usage & GRALLOC_USAGE_EXTERNAL_BLOCK) {
153 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_BLOCK;
154 }
155 }
156
157 if (err == 0) {
158 flags |= data.allocType;
159 private_handle_t* hnd = new private_handle_t(data.fd, size, flags,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700160 bufferType, format, width,
161 height);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700162
163 hnd->offset = data.offset;
164 hnd->base = int(data.base) + data.offset;
165 *pHandle = hnd;
166 }
167
168 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
169 return err;
170}
171
172void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
173 int *colorFormat,
174 int *bufferType)
175{
176 *bufferType = BUFFER_TYPE_VIDEO;
177 *colorFormat = inputFormat;
178
Naseer Ahmed65f16562012-06-15 20:53:42 -0700179 if (inputFormat < 0x7) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700180 // RGB formats
181 *colorFormat = inputFormat;
182 *bufferType = BUFFER_TYPE_UI;
183 } else if ((inputFormat == HAL_PIXEL_FORMAT_R_8) ||
184 (inputFormat == HAL_PIXEL_FORMAT_RG_88)) {
185 *colorFormat = inputFormat;
186 *bufferType = BUFFER_TYPE_UI;
187 }
188}
189
190int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700191 buffer_handle_t* pHandle, int* pStride,
192 size_t bufferSize) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700193 if (!pHandle || !pStride)
194 return -EINVAL;
195
196 size_t size;
197 int alignedw, alignedh;
198 int colorFormat, bufferType;
199 getGrallocInformationFromFormat(format, &colorFormat, &bufferType);
200 size = getBufferSizeAndDimensions(w, h, colorFormat, alignedw, alignedh);
201
202 if ((ssize_t)size <= 0)
203 return -EINVAL;
204 size = (bufferSize >= size)? bufferSize : size;
205
206 // All buffers marked as protected or for external
207 // display need to go to overlay
208 if ((usage & GRALLOC_USAGE_EXTERNAL_DISP) ||
Naseer Ahmed29a26812012-06-14 00:56:20 -0700209 (usage & GRALLOC_USAGE_PROTECTED) ||
210 (usage & GRALLOC_USAGE_PRIVATE_CP_BUFFER)) {
211 bufferType = BUFFER_TYPE_VIDEO;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700212 }
213 int err;
214 if (usage & GRALLOC_USAGE_HW_FB) {
215 err = gralloc_alloc_framebuffer(size, usage, pHandle);
216 } else {
217 err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
218 format, alignedw, alignedh);
219 }
220
221 if (err < 0) {
222 return err;
223 }
224
225 // Create a genlock lock for this buffer handle.
226 err = genlock_create_lock((native_handle_t*)(*pHandle));
227 if (err) {
228 ALOGE("%s: genlock_create_lock failed", __FUNCTION__);
229 free_impl(reinterpret_cast<private_handle_t*>(pHandle));
230 return err;
231 }
232 *pStride = alignedw;
233 return 0;
234}
235
236int gpu_context_t::free_impl(private_handle_t const* hnd) {
237 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
238 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
239 // free this buffer
240 const size_t bufferSize = m->finfo.line_length * m->info.yres;
241 int index = (hnd->base - m->framebuffer->base) / bufferSize;
242 m->bufferMask &= ~(1<<index);
243 } else {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700244 terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700245 sp<IMemAlloc> memalloc = mAllocCtrl->getAllocator(hnd->flags);
246 int err = memalloc->free_buffer((void*)hnd->base, (size_t) hnd->size,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700247 hnd->offset, hnd->fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700248 if(err)
249 return err;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700250 }
251
252 // Release the genlock
253 int err = genlock_release_lock((native_handle_t*)hnd);
254 if (err) {
255 ALOGE("%s: genlock_release_lock failed", __FUNCTION__);
256 }
257
258 delete hnd;
259 return 0;
260}
261
262int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700263 int usage, buffer_handle_t* pHandle,
264 int* pStride)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700265{
266 if (!dev) {
267 return -EINVAL;
268 }
269 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
270 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0);
271}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700272int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h,
273 int format, int usage,
274 buffer_handle_t* pHandle, int* pStride,
275 int bufferSize)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700276{
277 if (!dev) {
278 return -EINVAL;
279 }
280 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
281 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize);
282}
283
284
285int gpu_context_t::gralloc_free(alloc_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700286 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700287{
288 if (private_handle_t::validate(handle) < 0)
289 return -EINVAL;
290
291 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
292 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
293 return gpu->free_impl(hnd);
294}
295
296/*****************************************************************************/
297
298int gpu_context_t::gralloc_close(struct hw_device_t *dev)
299{
300 gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev);
301 if (ctx) {
302 /* TODO: keep a list of all buffer_handle_t created, and free them
303 * all here.
304 */
305 delete ctx;
306 }
307 return 0;
308}
309