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