1 /*
2 * Copyright (C) 2010 The Android Open Source Project
3 * Copyright (c) 2011-2014 The Linux Foundation. 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 "gr.h"
25 #include "gpu.h"
26 #include "memalloc.h"
27 #include "alloc_controller.h"
28 #include <qdMetaData.h>
29
30 using namespace gralloc;
31
32 #define SZ_1M 0x100000
33 #define SZ_2M 0x200000
34
gpu_context_t(const private_module_t * module,IAllocController * alloc_ctrl)35 gpu_context_t::gpu_context_t(const private_module_t* module,
36 IAllocController* alloc_ctrl ) :
37 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
42 // 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;
48 #ifdef QCOM_BSP
49 allocSize = gralloc_alloc_size;
50 #endif
51 free = gralloc_free;
52
53 }
54
gralloc_alloc_buffer(size_t size,int usage,buffer_handle_t * pHandle,int bufferType,int format,int width,int height)55 int 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;
66 if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED)
67 data.align = 8192;
68 else
69 data.align = getpagesize();
70
71 /* force 1MB alignment selectively for secure buffers, MDP5 onwards */
72 #ifdef MDSS_TARGET
73 if ((usage & GRALLOC_USAGE_PROTECTED) &&
74 (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP)) {
75 data.align = ALIGN((int) data.align, SZ_2M);
76 size = ALIGN(size, data.align);
77 }
78 #endif
79
80 data.size = size;
81 data.pHandle = (uintptr_t) pHandle;
82 err = mAllocCtrl->allocate(data, usage);
83
84 if (!err) {
85 /* allocate memory for enhancement data */
86 alloc_data eData;
87 eData.fd = -1;
88 eData.base = 0;
89 eData.offset = 0;
90 eData.size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
91 eData.pHandle = data.pHandle;
92 eData.align = getpagesize();
93 int eDataUsage = GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP;
94 int eDataErr = mAllocCtrl->allocate(eData, eDataUsage);
95 ALOGE_IF(eDataErr, "gralloc failed for eDataErr=%s",
96 strerror(-eDataErr));
97
98 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY) {
99 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
100 }
101
102 ColorSpace_t colorSpace = ITU_R_601;
103 flags |= private_handle_t::PRIV_FLAGS_ITU_R_601;
104 if (bufferType == BUFFER_TYPE_VIDEO) {
105 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
106 #ifndef MDSS_TARGET
107 colorSpace = ITU_R_601_FR;
108 flags |= private_handle_t::PRIV_FLAGS_ITU_R_601_FR;
109 #else
110 // Per the camera spec ITU 709 format should be set only for
111 // video encoding.
112 // It should be set to ITU 601 full range format for any other
113 // camera buffer
114 //
115 if (usage & GRALLOC_USAGE_HW_CAMERA_MASK) {
116 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
117 flags |= private_handle_t::PRIV_FLAGS_ITU_R_709;
118 colorSpace = ITU_R_709;
119 } else {
120 flags |= private_handle_t::PRIV_FLAGS_ITU_R_601_FR;
121 colorSpace = ITU_R_601_FR;
122 }
123 }
124 #endif
125 }
126 }
127
128 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) {
129 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
130 }
131
132 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
133 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
134 }
135
136 if (usage & GRALLOC_USAGE_HW_CAMERA_READ) {
137 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
138 }
139
140 if (usage & GRALLOC_USAGE_HW_COMPOSER) {
141 flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER;
142 }
143
144 if (usage & GRALLOC_USAGE_HW_TEXTURE) {
145 flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
146 }
147
148 if(usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
149 flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
150 }
151
152 if(isMacroTileEnabled(format, usage)) {
153 flags |= private_handle_t::PRIV_FLAGS_TILE_RENDERED;
154 }
155
156 flags |= data.allocType;
157 uintptr_t eBaseAddr = (uintptr_t)(eData.base) + eData.offset;
158 private_handle_t *hnd = new private_handle_t(data.fd, size, flags,
159 bufferType, format, width, height, eData.fd, eData.offset,
160 eBaseAddr);
161
162 hnd->offset = data.offset;
163 hnd->base = (uintptr_t)(data.base) + data.offset;
164 hnd->gpuaddr = 0;
165 setMetaData(hnd, UPDATE_COLOR_SPACE, (void*) &colorSpace);
166
167 *pHandle = hnd;
168 }
169
170 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
171
172 return err;
173 }
174
getGrallocInformationFromFormat(int inputFormat,int * bufferType)175 void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
176 int *bufferType)
177 {
178 *bufferType = BUFFER_TYPE_VIDEO;
179
180 if (inputFormat <= HAL_PIXEL_FORMAT_sRGB_X_8888) {
181 // RGB formats
182 *bufferType = BUFFER_TYPE_UI;
183 } else if ((inputFormat == HAL_PIXEL_FORMAT_R_8) ||
184 (inputFormat == HAL_PIXEL_FORMAT_RG_88)) {
185 *bufferType = BUFFER_TYPE_UI;
186 }
187 }
188
gralloc_alloc_framebuffer_locked(int usage,buffer_handle_t * pHandle)189 int gpu_context_t::gralloc_alloc_framebuffer_locked(int usage,
190 buffer_handle_t* pHandle)
191 {
192 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
193
194 // we don't support framebuffer allocations with graphics heap flags
195 if (usage & GRALLOC_HEAP_MASK) {
196 return -EINVAL;
197 }
198
199 if (m->framebuffer == NULL) {
200 ALOGE("%s: Invalid framebuffer", __FUNCTION__);
201 return -EINVAL;
202 }
203
204 const size_t bufferMask = m->bufferMask;
205 const uint32_t numBuffers = m->numBuffers;
206 size_t bufferSize = m->finfo.line_length * m->info.yres;
207
208 //adreno needs FB size to be page aligned
209 bufferSize = roundUpToPageSize(bufferSize);
210
211 if (numBuffers == 1) {
212 // If we have only one buffer, we never use page-flipping. Instead,
213 // we return a regular buffer which will be memcpy'ed to the main
214 // screen when post is called.
215 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
216 return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI,
217 m->fbFormat, m->info.xres, m->info.yres);
218 }
219
220 if (bufferMask >= ((1LU<<numBuffers)-1)) {
221 // We ran out of buffers.
222 return -ENOMEM;
223 }
224
225 // create a "fake" handle for it
226 uintptr_t vaddr = uintptr_t(m->framebuffer->base);
227 private_handle_t* hnd = new private_handle_t(
228 dup(m->framebuffer->fd), bufferSize,
229 private_handle_t::PRIV_FLAGS_USES_PMEM |
230 private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
231 BUFFER_TYPE_UI, m->fbFormat, m->info.xres,
232 m->info.yres);
233
234 // find a free slot
235 for (uint32_t i=0 ; i<numBuffers ; i++) {
236 if ((bufferMask & (1LU<<i)) == 0) {
237 m->bufferMask |= (1LU<<i);
238 break;
239 }
240 vaddr += bufferSize;
241 }
242 hnd->base = vaddr;
243 hnd->offset = vaddr - uintptr_t(m->framebuffer->base);
244 *pHandle = hnd;
245 return 0;
246 }
247
248
gralloc_alloc_framebuffer(int usage,buffer_handle_t * pHandle)249 int gpu_context_t::gralloc_alloc_framebuffer(int usage,
250 buffer_handle_t* pHandle)
251 {
252 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
253 pthread_mutex_lock(&m->lock);
254 int err = gralloc_alloc_framebuffer_locked(usage, pHandle);
255 pthread_mutex_unlock(&m->lock);
256 return err;
257 }
258
alloc_impl(int w,int h,int format,int usage,buffer_handle_t * pHandle,int * pStride,size_t bufferSize)259 int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
260 buffer_handle_t* pHandle, int* pStride,
261 size_t bufferSize) {
262 if (!pHandle || !pStride)
263 return -EINVAL;
264
265 size_t size;
266 int alignedw, alignedh;
267 int grallocFormat = format;
268 int bufferType;
269
270 //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
271 //the usage bits, gralloc assigns a format.
272 if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
273 format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
274 if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
275 grallocFormat = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; //NV12
276 else if((usage & GRALLOC_USAGE_HW_CAMERA_MASK)
277 == GRALLOC_USAGE_HW_CAMERA_ZSL)
278 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21 ZSL
279 else if(usage & GRALLOC_USAGE_HW_CAMERA_READ)
280 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
281 else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
282 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
283 else if(usage & GRALLOC_USAGE_HW_COMPOSER)
284 //XXX: If we still haven't set a format, default to RGBA8888
285 grallocFormat = HAL_PIXEL_FORMAT_RGBA_8888;
286 }
287
288 getGrallocInformationFromFormat(grallocFormat, &bufferType);
289 size = getBufferSizeAndDimensions(w, h, grallocFormat, usage, alignedw,
290 alignedh);
291
292 if ((ssize_t)size <= 0)
293 return -EINVAL;
294 size = (bufferSize >= size)? bufferSize : size;
295
296 bool useFbMem = false;
297 char property[PROPERTY_VALUE_MAX];
298 if((usage & GRALLOC_USAGE_HW_FB) &&
299 (property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
300 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
301 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
302 useFbMem = true;
303 }
304
305 int err = 0;
306 if(useFbMem) {
307 err = gralloc_alloc_framebuffer(usage, pHandle);
308 } else {
309 err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
310 grallocFormat, alignedw, alignedh);
311 }
312
313 if (err < 0) {
314 return err;
315 }
316
317 *pStride = alignedw;
318 return 0;
319 }
320
free_impl(private_handle_t const * hnd)321 int gpu_context_t::free_impl(private_handle_t const* hnd) {
322 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
323 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
324 const size_t bufferSize = m->finfo.line_length * m->info.yres;
325 size_t index = (hnd->base - m->framebuffer->base) / bufferSize;
326 m->bufferMask &= ~(1LU<<index);
327 } else {
328
329 terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
330 IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
331 int err = memalloc->free_buffer((void*)hnd->base, (size_t) hnd->size,
332 hnd->offset, hnd->fd);
333 if(err)
334 return err;
335 // free the metadata space
336 size_t size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
337 err = memalloc->free_buffer((void*)hnd->base_metadata,
338 size, hnd->offset_metadata,
339 hnd->fd_metadata);
340 if (err)
341 return err;
342 }
343 delete hnd;
344 return 0;
345 }
346
gralloc_alloc(alloc_device_t * dev,int w,int h,int format,int usage,buffer_handle_t * pHandle,int * pStride)347 int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
348 int usage, buffer_handle_t* pHandle,
349 int* pStride)
350 {
351 if (!dev) {
352 return -EINVAL;
353 }
354 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
355 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0);
356 }
gralloc_alloc_size(alloc_device_t * dev,int w,int h,int format,int usage,buffer_handle_t * pHandle,int * pStride,int bufferSize)357 int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h,
358 int format, int usage,
359 buffer_handle_t* pHandle, int* pStride,
360 int bufferSize)
361 {
362 if (!dev) {
363 return -EINVAL;
364 }
365 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
366 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize);
367 }
368
369
gralloc_free(alloc_device_t * dev,buffer_handle_t handle)370 int gpu_context_t::gralloc_free(alloc_device_t* dev,
371 buffer_handle_t handle)
372 {
373 if (private_handle_t::validate(handle) < 0)
374 return -EINVAL;
375
376 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
377 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
378 return gpu->free_impl(hnd);
379 }
380
381 /*****************************************************************************/
382
gralloc_close(struct hw_device_t * dev)383 int gpu_context_t::gralloc_close(struct hw_device_t *dev)
384 {
385 gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev);
386 if (ctx) {
387 /* TODO: keep a list of all buffer_handle_t created, and free them
388 * all here.
389 */
390 delete ctx;
391 }
392 return 0;
393 }
394
395