blob: ea5dd2a0d129edf6d3e7b960d6b3f3882b38ffe9 [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * Copyright (c) 2010-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
Naseer Ahmed29a26812012-06-14 00:56:20 -070018#include <cutils/log.h>
19
20#include <stdint.h>
21#include <string.h>
22#include <unistd.h>
23#include <errno.h>
24#include <fcntl.h>
25
26#include <sys/ioctl.h>
27#include <sys/types.h>
28#include <sys/mman.h>
29
30#include <linux/msm_kgsl.h>
31
32#include <EGL/eglplatform.h>
33#include <cutils/native_handle.h>
34#include <cutils/ashmem.h>
35#include <linux/ashmem.h>
36#include <gralloc_priv.h>
37
38#include <copybit.h>
39#include <alloc_controller.h>
40#include <memalloc.h>
41
42#include "c2d2.h"
43#include "software_converter.h"
44
45#include <dlfcn.h>
46
47using gralloc::IMemAlloc;
48using gralloc::IonController;
49using gralloc::alloc_data;
Naseer Ahmed29a26812012-06-14 00:56:20 -070050
51C2D_STATUS (*LINK_c2dCreateSurface)( uint32 *surface_id,
52 uint32 surface_bits,
53 C2D_SURFACE_TYPE surface_type,
54 void *surface_definition );
55
56C2D_STATUS (*LINK_c2dUpdateSurface)( uint32 surface_id,
57 uint32 surface_bits,
58 C2D_SURFACE_TYPE surface_type,
59 void *surface_definition );
60
61C2D_STATUS (*LINK_c2dReadSurface)( uint32 surface_id,
62 C2D_SURFACE_TYPE surface_type,
63 void *surface_definition,
64 int32 x, int32 y );
65
66C2D_STATUS (*LINK_c2dDraw)( uint32 target_id,
67 uint32 target_config, C2D_RECT *target_scissor,
68 uint32 target_mask_id, uint32 target_color_key,
69 C2D_OBJECT *objects_list, uint32 num_objects );
70
71C2D_STATUS (*LINK_c2dFinish)( uint32 target_id);
72
73C2D_STATUS (*LINK_c2dFlush)( uint32 target_id, c2d_ts_handle *timestamp);
74
75C2D_STATUS (*LINK_c2dWaitTimestamp)( c2d_ts_handle timestamp );
76
77C2D_STATUS (*LINK_c2dDestroySurface)( uint32 surface_id );
78
79C2D_STATUS (*LINK_c2dMapAddr) ( int mem_fd, void * hostptr, uint32 len, uint32 offset, uint32 flags, void ** gpuaddr);
80
81C2D_STATUS (*LINK_c2dUnMapAddr) ( void * gpuaddr);
82
83/******************************************************************************/
84
85#if defined(COPYBIT_Z180)
86#define MAX_SCALE_FACTOR (4096)
87#define MAX_DIMENSION (4096)
88#else
89#error "Unsupported HW version"
90#endif
91
92#define NUM_SURFACES 3
93
94enum {
95 RGB_SURFACE,
96 YUV_SURFACE_2_PLANES,
97 YUV_SURFACE_3_PLANES
98};
99
100enum eConversionType {
101 CONVERT_TO_ANDROID_FORMAT,
102 CONVERT_TO_C2D_FORMAT
103};
104
105enum eC2DFlags {
106 FLAGS_PREMULTIPLIED_ALPHA = 1<<0,
107 FLAGS_YUV_DESTINATION = 1<<1
108};
109
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700110static gralloc::IAllocController* sAlloc = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700111/******************************************************************************/
112
113/** State information for each device instance */
114struct copybit_context_t {
115 struct copybit_device_t device;
116 unsigned int src[NUM_SURFACES]; /* src surfaces */
117 unsigned int dst[NUM_SURFACES]; /* dst surfaces */
118 unsigned int trg_transform; /* target transform */
119 C2D_OBJECT blitState;
120 void *libc2d2;
121 alloc_data temp_src_buffer;
122 alloc_data temp_dst_buffer;
123 int fb_width;
124 int fb_height;
125 bool isPremultipliedAlpha;
Naseer Ahmed45a99602012-07-31 19:15:24 -0700126 bool mBlitToFB;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700127};
128
129struct blitlist{
130 uint32_t count;
131 C2D_OBJECT blitObjects[12];
132};
133
134struct bufferInfo {
135 int width;
136 int height;
137 int format;
138};
139
140struct yuvPlaneInfo {
141 int yStride; //luma stride
142 int plane1_stride;
143 int plane2_stride;
144 int plane1_offset;
145 int plane2_offset;
146};
147
148/**
149 * Common hardware methods
150 */
151
152static int open_copybit(const struct hw_module_t* module, const char* name,
153 struct hw_device_t** device);
154
155static struct hw_module_methods_t copybit_module_methods = {
156open: open_copybit
157};
158
159/*
160 * The COPYBIT Module
161 */
162struct copybit_module_t HAL_MODULE_INFO_SYM = {
163common: {
164tag: HARDWARE_MODULE_TAG,
165 version_major: 1,
166 version_minor: 0,
167 id: COPYBIT_HARDWARE_MODULE_ID,
168 name: "QCT COPYBIT C2D 2.0 Module",
169 author: "Qualcomm",
170 methods: &copybit_module_methods
171 }
172};
173
174
175/* convert COPYBIT_FORMAT to C2D format */
176static int get_format(int format) {
177 switch (format) {
178 case HAL_PIXEL_FORMAT_RGB_565: return C2D_COLOR_FORMAT_565_RGB;
179 case HAL_PIXEL_FORMAT_RGBX_8888: return C2D_COLOR_FORMAT_8888_ARGB |
180 C2D_FORMAT_SWAP_RB |
181 C2D_FORMAT_DISABLE_ALPHA;
182 case HAL_PIXEL_FORMAT_RGBA_8888: return C2D_COLOR_FORMAT_8888_ARGB |
183 C2D_FORMAT_SWAP_RB;
184 case HAL_PIXEL_FORMAT_BGRA_8888: return C2D_COLOR_FORMAT_8888_ARGB;
185 case HAL_PIXEL_FORMAT_RGBA_5551: return C2D_COLOR_FORMAT_5551_RGBA;
186 case HAL_PIXEL_FORMAT_RGBA_4444: return C2D_COLOR_FORMAT_4444_RGBA;
187 case HAL_PIXEL_FORMAT_YCbCr_420_SP: return C2D_COLOR_FORMAT_420_NV12;
188 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:return C2D_COLOR_FORMAT_420_NV12;
189 case HAL_PIXEL_FORMAT_YCrCb_420_SP: return C2D_COLOR_FORMAT_420_NV21;
190 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: return C2D_COLOR_FORMAT_420_NV12 |
191 C2D_FORMAT_MACROTILED;
192 default: ALOGE("%s: invalid format (0x%x", __FUNCTION__, format); return -EINVAL;
193 }
194 return -EINVAL;
195}
196
197/* Get the C2D formats needed for conversion to YUV */
198static int get_c2d_format_for_yuv_destination(int halFormat) {
199 switch (halFormat) {
200 // We do not swap the RB when the target is YUV
201 case HAL_PIXEL_FORMAT_RGBX_8888: return C2D_COLOR_FORMAT_8888_ARGB |
202 C2D_FORMAT_DISABLE_ALPHA;
203 case HAL_PIXEL_FORMAT_RGBA_8888: return C2D_COLOR_FORMAT_8888_ARGB;
204 // The U and V need to be interchanged when the target is YUV
205 case HAL_PIXEL_FORMAT_YCbCr_420_SP: return C2D_COLOR_FORMAT_420_NV21;
206 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:return C2D_COLOR_FORMAT_420_NV21;
207 case HAL_PIXEL_FORMAT_YCrCb_420_SP: return C2D_COLOR_FORMAT_420_NV12;
208 default: return get_format(halFormat);
209 }
210 return -EINVAL;
211}
212
213/* ------------------------------------------------------------------- *//*!
214 * \internal
215 * \brief Get the bpp for a particular color format
216 * \param color format
217 * \return bits per pixel
218 *//* ------------------------------------------------------------------- */
219int c2diGetBpp(int32 colorformat)
220{
221
222 int c2dBpp = 0;
223
224 switch(colorformat&0xFF)
225 {
226 case C2D_COLOR_FORMAT_4444_RGBA:
227 case C2D_COLOR_FORMAT_4444_ARGB:
228 case C2D_COLOR_FORMAT_1555_ARGB:
229 case C2D_COLOR_FORMAT_565_RGB:
230 case C2D_COLOR_FORMAT_5551_RGBA:
231 c2dBpp = 16;
232 break;
233 case C2D_COLOR_FORMAT_8888_RGBA:
234 case C2D_COLOR_FORMAT_8888_ARGB:
235 c2dBpp = 32;
236 break;
237 case C2D_COLOR_FORMAT_8_L:
238 case C2D_COLOR_FORMAT_8_A:
239 c2dBpp = 8;
240 break;
241 case C2D_COLOR_FORMAT_4_A:
242 c2dBpp = 4;
243 break;
244 case C2D_COLOR_FORMAT_1:
245 c2dBpp = 1;
246 break;
247 default:
248 ALOGE("%s ERROR", __func__);
249 break;
250 }
251 return c2dBpp;
252}
253
254static uint32 c2d_get_gpuaddr( struct private_handle_t *handle)
255{
256 uint32 memtype, *gpuaddr;
257 C2D_STATUS rc;
258
259 if(!handle)
260 return 0;
261
262 if (handle->flags & (private_handle_t::PRIV_FLAGS_USES_PMEM |
263 private_handle_t::PRIV_FLAGS_USES_PMEM_ADSP))
264 memtype = KGSL_USER_MEM_TYPE_PMEM;
265 else if (handle->flags & private_handle_t::PRIV_FLAGS_USES_ASHMEM)
266 memtype = KGSL_USER_MEM_TYPE_ASHMEM;
267 else if (handle->flags & private_handle_t::PRIV_FLAGS_USES_ION)
268 memtype = KGSL_USER_MEM_TYPE_ION;
269 else {
270 ALOGE("Invalid handle flags: 0x%x", handle->flags);
271 return 0;
272 }
273
274 rc = LINK_c2dMapAddr(handle->fd, (void*)handle->base, handle->size, handle->offset, memtype, (void**)&gpuaddr);
275 if (rc == C2D_STATUS_OK) {
276 return (uint32) gpuaddr;
277 }
278 return 0;
279}
280
281static int is_supported_rgb_format(int format)
282{
283 switch(format) {
284 case HAL_PIXEL_FORMAT_RGBA_8888:
285 case HAL_PIXEL_FORMAT_RGBX_8888:
286 case HAL_PIXEL_FORMAT_RGB_565:
287 case HAL_PIXEL_FORMAT_BGRA_8888:
288 case HAL_PIXEL_FORMAT_RGBA_5551:
289 case HAL_PIXEL_FORMAT_RGBA_4444: {
290 return COPYBIT_SUCCESS;
291 }
292 default:
293 return COPYBIT_FAILURE;
294 }
295}
296
297static int get_num_planes(int format)
298{
299 switch(format) {
300 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
301 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
302 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
303 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: {
304 return 2;
305 }
306 case HAL_PIXEL_FORMAT_YV12: {
307 return 3;
308 }
309 default:
310 return COPYBIT_FAILURE;
311 }
312}
313
314static int is_supported_yuv_format(int format)
315{
316 switch(format) {
317 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
318 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
319 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
320 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: {
321 return COPYBIT_SUCCESS;
322 }
323 default:
324 return COPYBIT_FAILURE;
325 }
326}
327
328static int is_valid_destination_format(int format)
329{
330 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) {
331 // C2D does not support NV12Tile as a destination format.
332 return COPYBIT_FAILURE;
333 }
334 return COPYBIT_SUCCESS;
335}
336
337static int calculate_yuv_offset_and_stride(const bufferInfo& info,
338 yuvPlaneInfo& yuvInfo)
339{
340 int width = info.width;
341 int height = info.height;
342 int format = info.format;
343
344 int aligned_height = 0;
345 int aligned_width = 0, size = 0;
346
347 switch (format) {
348 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: {
349 /* NV12 Tile buffers have their luma height aligned to 32bytes and width
350 * aligned to 128 bytes. The chroma offset starts at an 8K boundary
351 */
352 aligned_height = ALIGN(height, 32);
353 aligned_width = ALIGN(width, 128);
354 size = aligned_width * aligned_height;
355 yuvInfo.plane1_offset = ALIGN(size,8192);
356 yuvInfo.yStride = aligned_width;
357 yuvInfo.plane1_stride = aligned_width;
358 break;
359 }
360 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
361 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
362 case HAL_PIXEL_FORMAT_YCrCb_420_SP: {
363 aligned_width = ALIGN(width, 32);
364 yuvInfo.yStride = aligned_width;
365 yuvInfo.plane1_stride = aligned_width;
366 if (HAL_PIXEL_FORMAT_NV12_ENCODEABLE == format) {
367 // The encoder requires a 2K aligned chroma offset
368 yuvInfo.plane1_offset = ALIGN(aligned_width * height, 2048);
369 } else
370 yuvInfo.plane1_offset = aligned_width * height;
371
372 break;
373 }
374 default: {
375 return COPYBIT_FAILURE;
376 }
377 }
378 return COPYBIT_SUCCESS;
379}
380
381/** create C2D surface from copybit image */
382static int set_image( uint32 surfaceId, const struct copybit_image_t *rhs,
383 int *cformat, uint32_t *mapped, const eC2DFlags flags)
384{
385 struct private_handle_t* handle = (struct private_handle_t*)rhs->handle;
386 C2D_SURFACE_TYPE surfaceType;
387 int status = COPYBIT_SUCCESS;
388
389 if (flags & FLAGS_YUV_DESTINATION) {
390 *cformat = get_c2d_format_for_yuv_destination(rhs->format);
391 } else {
392 *cformat = get_format(rhs->format);
393 }
394
395 if(*cformat == -EINVAL) {
396 ALOGE("%s: invalid format", __FUNCTION__);
397 return -EINVAL;
398 }
399
400 if(handle == NULL) {
401 ALOGE("%s: invalid handle", __func__);
402 return -EINVAL;
403 }
404
405 if (handle->gpuaddr == 0) {
406 handle->gpuaddr = c2d_get_gpuaddr(handle);
407 if(!handle->gpuaddr) {
408 ALOGE("%s: c2d_get_gpuaddr failed", __FUNCTION__);
409 return COPYBIT_FAILURE;
410 }
411 *mapped = 1;
412 }
413
414 /* create C2D surface */
415 if(is_supported_rgb_format(rhs->format) == COPYBIT_SUCCESS) {
416 /* RGB */
417 C2D_RGB_SURFACE_DEF surfaceDef;
418
419 surfaceType = (C2D_SURFACE_TYPE) (C2D_SURFACE_RGB_HOST | C2D_SURFACE_WITH_PHYS);
420
421 surfaceDef.phys = (void*) handle->gpuaddr;
422 surfaceDef.buffer = (void*) (handle->base);
423
424 surfaceDef.format = *cformat |
425 ((flags & FLAGS_PREMULTIPLIED_ALPHA) ? C2D_FORMAT_PREMULTIPLIED : 0);
426 surfaceDef.width = rhs->w;
427 surfaceDef.height = rhs->h;
428 int aligned_width = ALIGN(surfaceDef.width,32);
429 surfaceDef.stride = (aligned_width * c2diGetBpp(surfaceDef.format))>>3;
430
431 if(LINK_c2dUpdateSurface( surfaceId,C2D_TARGET | C2D_SOURCE, surfaceType, &surfaceDef)) {
432 ALOGE("%s: RGB Surface c2dUpdateSurface ERROR", __FUNCTION__);
433 goto error;
434 status = COPYBIT_FAILURE;
435 }
436 } else if (is_supported_yuv_format(rhs->format) == COPYBIT_SUCCESS) {
437 C2D_YUV_SURFACE_DEF surfaceDef;
438 memset(&surfaceDef, 0, sizeof(surfaceDef));
439 surfaceType = (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST | C2D_SURFACE_WITH_PHYS);
440 surfaceDef.format = *cformat;
441
442 bufferInfo info;
443 info.width = rhs->w;
444 info.height = rhs->h;
445 info.format = rhs->format;
446
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700447 yuvPlaneInfo yuvInfo = {0};
Naseer Ahmed29a26812012-06-14 00:56:20 -0700448 status = calculate_yuv_offset_and_stride(info, yuvInfo);
449 if(status != COPYBIT_SUCCESS) {
450 ALOGE("%s: calculate_yuv_offset_and_stride error", __FUNCTION__);
451 goto error;
452 }
453
454 surfaceDef.width = rhs->w;
455 surfaceDef.height = rhs->h;
456 surfaceDef.plane0 = (void*) (handle->base);
457 surfaceDef.phys0 = (void*) (handle->gpuaddr);
458 surfaceDef.stride0 = yuvInfo.yStride;
459
460 surfaceDef.plane1 = (void*) (handle->base + yuvInfo.plane1_offset);
461 surfaceDef.phys1 = (void*) (handle->gpuaddr + yuvInfo.plane1_offset);
462 surfaceDef.stride1 = yuvInfo.plane1_stride;
463 if (3 == get_num_planes(rhs->format)) {
464 surfaceDef.plane2 = (void*) (handle->base + yuvInfo.plane2_offset);
465 surfaceDef.phys2 = (void*) (handle->gpuaddr + yuvInfo.plane2_offset);
466 surfaceDef.stride2 = yuvInfo.plane2_stride;
467 }
468
469 if(LINK_c2dUpdateSurface( surfaceId,C2D_TARGET | C2D_SOURCE, surfaceType,
470 &surfaceDef)) {
471 ALOGE("%s: YUV Surface c2dUpdateSurface ERROR", __FUNCTION__);
472 goto error;
473 status = COPYBIT_FAILURE;
474 }
475 } else {
476 ALOGE("%s: invalid format 0x%x", __FUNCTION__, rhs->format);
477 goto error;
478 status = COPYBIT_FAILURE;
479 }
480
481 return status;
482
483error:
484 if(*mapped == 1) {
485 LINK_c2dUnMapAddr( (void*) handle->gpuaddr);
486 handle->gpuaddr = 0;
487 *mapped = 0;
488 }
489 return status;
490}
491
492static int set_src_image( uint32 *surfaceId, const struct copybit_image_t *rhs,
493 int *cformat, uint32 *mapped)
494{
495 struct private_handle_t* handle = (struct private_handle_t*)rhs->handle;
496 *cformat = get_format(rhs->format);
497 C2D_SURFACE_TYPE surfaceType;
498 uint32 gpuaddr = (uint32)handle->gpuaddr;
499 int status = COPYBIT_SUCCESS;
500
501 if (handle->gpuaddr == 0)
502 {
503 handle->gpuaddr = c2d_get_gpuaddr( handle);
504 if(!handle->gpuaddr)
505 return COPYBIT_FAILURE;
506
507 *mapped = 1;
508 }
509
510 /* create C2D surface */
511 if(is_supported_rgb_format(rhs->format) == COPYBIT_SUCCESS) {
512 /* RGB */
513 C2D_RGB_SURFACE_DEF surfaceDef;
514 surfaceType = (C2D_SURFACE_TYPE) (C2D_SURFACE_RGB_HOST | C2D_SURFACE_WITH_PHYS | C2D_SURFACE_WITH_PHYS_DUMMY);
515
516 surfaceDef.phys = (void*) handle->gpuaddr;
517 surfaceDef.buffer = (void*) (handle->base);
518 surfaceDef.buffer = (void*) (handle->base + handle->offset);
519
520 surfaceDef.format = get_format(rhs->format);
521 surfaceDef.width = rhs->w;
522 surfaceDef.height = rhs->h;
523 surfaceDef.stride = ALIGN(((surfaceDef.width * c2diGetBpp(surfaceDef.format))>>3), 32);
524
525 if(LINK_c2dCreateSurface( surfaceId, C2D_TARGET, surfaceType,(void*)&surfaceDef)) {
526 ALOGE("%s: LINK_c2dCreateSurface error", __FUNCTION__);
527 status = COPYBIT_FAILURE;
528 goto error;
529 }
530 } else if(is_supported_yuv_format(rhs->format) == COPYBIT_SUCCESS) {
531 /* YUV */
532 C2D_YUV_SURFACE_DEF surfaceDef;
533 int offset = 0;
534 int yStride = 0;
535 int uvStride = 0;
536 memset(&surfaceDef, 0, sizeof(surfaceDef));
537
538 surfaceType = (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST | C2D_SURFACE_WITH_PHYS | C2D_SURFACE_WITH_PHYS_DUMMY);
539 surfaceDef.format = get_format(rhs->format);
540 bufferInfo info;
541 info.width = rhs->w;
542 info.height = rhs->h;
543 info.format = rhs->format;
544
545 yuvPlaneInfo yuvInfo;
546 status = calculate_yuv_offset_and_stride(info, yuvInfo);
547 if(status != COPYBIT_SUCCESS) {
548 ALOGE("%s: calculate_yuv_offset_and_stride error", __FUNCTION__);
549 goto error;
550 }
551
552 surfaceDef.width = rhs->w;
553 surfaceDef.height = rhs->h;
554 surfaceDef.plane0 = (void*) (handle->base);
555 surfaceDef.phys0 = (void*) handle->gpuaddr;
556 surfaceDef.stride0 = yuvInfo.yStride;
557
558 surfaceDef.plane1 = (void*) (handle->base + yuvInfo.plane1_offset);
559 surfaceDef.phys1 = (void*) (handle->gpuaddr + yuvInfo.plane1_offset);
560 surfaceDef.stride1 = yuvInfo.plane1_stride;
561
562 if(LINK_c2dCreateSurface( surfaceId, C2D_TARGET | C2D_SOURCE, surfaceType,
563 (void*)&surfaceDef)) {
564 ALOGE("%s: YUV surface LINK_c2dCreateSurface error", __func__);
565 status = COPYBIT_FAILURE;
566 goto error;
567 }
568 } else {
569 ALOGE("%s: Invalid format 0x%x", __FUNCTION__, rhs->format);
570 status = COPYBIT_FAILURE;
571 }
572
573 return COPYBIT_SUCCESS;
574
575error:
576 if(*mapped == 1) {
577 LINK_c2dUnMapAddr( (void*) handle->gpuaddr);
578 handle->gpuaddr = 0;
579 *mapped = 0;
580 }
581 return status;
582}
583
584void unset_image( uint32 surfaceId, const struct copybit_image_t *rhs,
585 uint32 mmapped)
586{
587 struct private_handle_t* handle = (struct private_handle_t*)rhs->handle;
588
589 if (mmapped && handle->gpuaddr) {
590 // Unmap this gpuaddr
591 LINK_c2dUnMapAddr( (void*) handle->gpuaddr);
592 handle->gpuaddr = 0;
593 }
594}
595
596static int blit_to_target( uint32 surfaceId, const struct copybit_image_t *rhs)
597{
598 struct private_handle_t* handle = (struct private_handle_t*)rhs->handle;
599 uint32 cformat = get_format(rhs->format);
600 C2D_SURFACE_TYPE surfaceType;
601 uint32 memoryMapped = 0;
602 int status = COPYBIT_SUCCESS;
603
604 if (!handle->gpuaddr) {
605 handle->gpuaddr = c2d_get_gpuaddr(handle);
606 if(!handle->gpuaddr)
607 return COPYBIT_FAILURE;
608
609 memoryMapped = 1;
610 }
611
612 /* create C2D surface */
613
614 if(cformat) {
615 /* RGB */
616 C2D_RGB_SURFACE_DEF surfaceDef;
617 memset(&surfaceDef, 0, sizeof(surfaceDef));
618
619 surfaceDef.buffer = (void*) handle->base;
620 surfaceDef.phys = (void*) handle->gpuaddr;
621
622 surfaceType = C2D_SURFACE_RGB_HOST;
623 surfaceDef.format = get_format(rhs->format);
624 surfaceDef.width = rhs->w;
625 surfaceDef.height = rhs->h;
626 surfaceDef.stride = ALIGN(((surfaceDef.width * c2diGetBpp(surfaceDef.format))>>3), 32);
627
628 if(LINK_c2dReadSurface(surfaceId, surfaceType, (void*)&surfaceDef, 0, 0)) {
629 ALOGE("%s: LINK_c2dReadSurface ERROR", __func__);
630 status = COPYBIT_FAILURE;
631 goto done;
632 }
633 }
634 else {
635 /* YUV */
636 /* TODO */
637 }
638
639done:
640 if (memoryMapped) {
641 LINK_c2dUnMapAddr( (void*) handle->gpuaddr);
642 handle->gpuaddr = 0;
643 }
644 return status;
645}
646
647/** setup rectangles */
648static void set_rects(struct copybit_context_t *ctx,
649 C2D_OBJECT *c2dObject,
650 const struct copybit_rect_t *dst,
651 const struct copybit_rect_t *src,
652 const struct copybit_rect_t *scissor)
653{
654 // Set the target rect.
655 if((ctx->trg_transform & C2D_TARGET_ROTATE_90) &&
656 (ctx->trg_transform & C2D_TARGET_ROTATE_180)) {
657 /* target rotation is 270 */
658 c2dObject->target_rect.x = (dst->t)<<16;
659 c2dObject->target_rect.y = ctx->fb_width?(ALIGN(ctx->fb_width,32)- dst->r):dst->r;
660 c2dObject->target_rect.y = c2dObject->target_rect.y<<16;
661 c2dObject->target_rect.height = ((dst->r) - (dst->l))<<16;
662 c2dObject->target_rect.width = ((dst->b) - (dst->t))<<16;
663 } else if(ctx->trg_transform & C2D_TARGET_ROTATE_90) {
664 c2dObject->target_rect.x = ctx->fb_height?(ctx->fb_height - dst->b):dst->b;
665 c2dObject->target_rect.x = c2dObject->target_rect.x<<16;
666 c2dObject->target_rect.y = (dst->l)<<16;
667 c2dObject->target_rect.height = ((dst->r) - (dst->l))<<16;
668 c2dObject->target_rect.width = ((dst->b) - (dst->t))<<16;
669 } else if(ctx->trg_transform & C2D_TARGET_ROTATE_180) {
670 c2dObject->target_rect.y = ctx->fb_height?(ctx->fb_height - dst->b):dst->b;
671 c2dObject->target_rect.y = c2dObject->target_rect.y<<16;
672 c2dObject->target_rect.x = ctx->fb_width?(ALIGN(ctx->fb_width,32) - dst->r):dst->r;
673 c2dObject->target_rect.x = c2dObject->target_rect.x<<16;
674 c2dObject->target_rect.height = ((dst->b) - (dst->t))<<16;
675 c2dObject->target_rect.width = ((dst->r) - (dst->l))<<16;
676 } else {
677 c2dObject->target_rect.x = (dst->l)<<16;
678 c2dObject->target_rect.y = (dst->t)<<16;
679 c2dObject->target_rect.height = ((dst->b) - (dst->t))<<16;
680 c2dObject->target_rect.width = ((dst->r) - (dst->l))<<16;
681 }
682 c2dObject->config_mask |= C2D_TARGET_RECT_BIT;
683
684 // Set the source rect
685 c2dObject->source_rect.x = (src->l)<<16;
686 c2dObject->source_rect.y = (src->t)<<16;
687 c2dObject->source_rect.height = ((src->b) - (src->t))<<16;
688 c2dObject->source_rect.width = ((src->r) - (src->l))<<16;
689 c2dObject->config_mask |= C2D_SOURCE_RECT_BIT;
690
691 // Set the scissor rect
692 c2dObject->scissor_rect.x = scissor->l;
693 c2dObject->scissor_rect.y = scissor->t;
694 c2dObject->scissor_rect.height = (scissor->b) - (scissor->t);
695 c2dObject->scissor_rect.width = (scissor->r) - (scissor->l);
696 c2dObject->config_mask |= C2D_SCISSOR_RECT_BIT;
697}
698
699/** copy the bits */
700static int msm_copybit(struct copybit_context_t *dev, blitlist *list, uint32 target)
701{
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700702 unsigned int objects;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700703
704 for(objects = 0; objects < list->count; objects++) {
705 list->blitObjects[objects].next = &(list->blitObjects[objects+1]);
706 }
707
708 if(LINK_c2dDraw(target,dev->trg_transform, 0x0, 0, 0, list->blitObjects,
709 list->count)) {
710 ALOGE("%s: LINK_c2dDraw ERROR", __FUNCTION__);
711 return COPYBIT_FAILURE;
712 }
713
714 return COPYBIT_SUCCESS;
715}
716
717/*****************************************************************************/
718
719/** Set a parameter to value */
720static int set_parameter_copybit(
721 struct copybit_device_t *dev,
722 int name,
723 int value)
724{
725 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
726 if (!ctx) {
727 ALOGE("%s: null context", __FUNCTION__);
728 return -EINVAL;
729 }
730
731 switch(name) {
732 case COPYBIT_ROTATION_DEG:
733 ctx->blitState.rotation = value<<16;
734 /* SRC rotation */
735 if(!value)
736 ctx->blitState.config_mask &=~C2D_ROTATE_BIT;;
737 break;
738 case COPYBIT_PLANE_ALPHA:
739 if (value < 0) value = 0;
740 if (value >= 256) value = 255;
741
742 ctx->blitState.global_alpha = value;
743
744 if(ctx->blitState.global_alpha<255)
745 ctx->blitState.config_mask |= C2D_GLOBAL_ALPHA_BIT;
746 else
747 ctx->blitState.config_mask &=~C2D_GLOBAL_ALPHA_BIT;
748 break;
749 case COPYBIT_DITHER:
750 /* TODO */
751 break;
752 case COPYBIT_BLUR:
753 /* TODO */
754 break;
755 case COPYBIT_TRANSFORM:
756 ctx->blitState.config_mask &=~C2D_ROTATE_BIT;
757 ctx->blitState.config_mask &=~C2D_MIRROR_H_BIT;
758 ctx->blitState.config_mask &=~C2D_MIRROR_V_BIT;
759 ctx->trg_transform = C2D_TARGET_ROTATE_0;
760
761 if((value&0x7) == COPYBIT_TRANSFORM_ROT_180)
762 ctx->trg_transform = C2D_TARGET_ROTATE_180;
763 else if((value&0x7) == COPYBIT_TRANSFORM_ROT_270)
764 ctx->trg_transform = C2D_TARGET_ROTATE_90;
765 else {
766 if(value&COPYBIT_TRANSFORM_FLIP_H)
767 ctx->blitState.config_mask |= C2D_MIRROR_H_BIT;
768 if(value&COPYBIT_TRANSFORM_FLIP_V)
769 ctx->blitState.config_mask |= C2D_MIRROR_V_BIT;
770 if(value&COPYBIT_TRANSFORM_ROT_90)
771 ctx->trg_transform = C2D_TARGET_ROTATE_270;
772 }
773 break;
774 case COPYBIT_PREMULTIPLIED_ALPHA:
775 (value == COPYBIT_ENABLE) ? ctx->isPremultipliedAlpha = true :
776 ctx->isPremultipliedAlpha = false;
777 break;
778 case COPYBIT_FRAMEBUFFER_WIDTH:
779 ctx->fb_width = value;
780 break;
781 case COPYBIT_FRAMEBUFFER_HEIGHT:
782 ctx->fb_height = value;
783 break;
Naseer Ahmed45a99602012-07-31 19:15:24 -0700784 case COPYBIT_BLIT_TO_FRAMEBUFFER:
785 if (COPYBIT_ENABLE == value) {
786 ctx->mBlitToFB = value;
787 } else if (COPYBIT_DISABLE == value) {
788 ctx->mBlitToFB = value;
789 } else {
790 ALOGE ("%s:Invalid input for COPYBIT_BLIT_TO_FRAMEBUFFER : %d",
791 __FUNCTION__, value);
792 }
793 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700794 default:
795 ALOGE("%s: default case param=0x%x", __FUNCTION__, name);
796 return -EINVAL;
797 break;
798 }
799
800 return COPYBIT_SUCCESS;
801}
802
803/** Get a static info value */
804static int get(struct copybit_device_t *dev, int name)
805{
806 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
807 int value;
808
809 if (!ctx) {
810 ALOGE("%s: null context error", __FUNCTION__);
811 return -EINVAL;
812 }
813
814 switch(name) {
815 case COPYBIT_MINIFICATION_LIMIT:
816 value = MAX_SCALE_FACTOR;
817 break;
818 case COPYBIT_MAGNIFICATION_LIMIT:
819 value = MAX_SCALE_FACTOR;
820 break;
821 case COPYBIT_SCALING_FRAC_BITS:
822 value = 32;
823 break;
824 case COPYBIT_ROTATION_STEP_DEG:
825 value = 1;
826 break;
827 default:
828 ALOGE("%s: default case param=0x%x", __FUNCTION__, name);
829 value = -EINVAL;
830 }
831 return value;
832}
833
834static int is_alpha(int cformat)
835{
836 int alpha = 0;
837 switch (cformat & 0xFF) {
838 case C2D_COLOR_FORMAT_8888_ARGB:
839 case C2D_COLOR_FORMAT_8888_RGBA:
840 case C2D_COLOR_FORMAT_5551_RGBA:
841 case C2D_COLOR_FORMAT_4444_ARGB:
842 alpha = 1;
843 break;
844 default:
845 alpha = 0;
846 break;
847 }
848
849 if(alpha && (cformat&C2D_FORMAT_DISABLE_ALPHA))
850 alpha = 0;
851
852 return alpha;
853}
854
855/* Function to check if we need a temporary buffer for the blit.
856 * This would happen if the requested destination stride and the
857 * C2D stride do not match. We ignore RGB buffers, since their
858 * stride is always aligned to 32.
859 */
860static bool need_temp_buffer(struct copybit_image_t const *img)
861{
862 if (COPYBIT_SUCCESS == is_supported_rgb_format(img->format))
863 return false;
864
865 struct private_handle_t* handle = (struct private_handle_t*)img->handle;
866
867 // The width parameter in the handle contains the aligned_w. We check if we
868 // need to convert based on this param. YUV formats have bpp=1, so checking
869 // if the requested stride is aligned should suffice.
870 if (0 == (handle->width)%32) {
871 return false;
872 }
873
874 return true;
875}
876
877/* Function to extract the information from the copybit image and set the corresponding
878 * values in the bufferInfo struct.
879 */
880static void populate_buffer_info(struct copybit_image_t const *img, bufferInfo& info)
881{
882 info.width = img->w;
883 info.height = img->h;
884 info.format = img->format;
885}
886
887/* Function to get the required size for a particular format, inorder for C2D to perform
888 * the blit operation.
889 */
890static size_t get_size(const bufferInfo& info)
891{
892 size_t size = 0;
893 int w = info.width;
894 int h = info.height;
895 int aligned_w = ALIGN(w, 32);
896 switch(info.format) {
897 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
898 {
899 // Chroma for this format is aligned to 2K.
900 size = ALIGN((aligned_w*h), 2048) +
901 ALIGN(w/2, 32) * h/2 *2;
902 size = ALIGN(size, 4096);
903 } break;
904 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
905 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
906 {
907 size = aligned_w*h +
908 ALIGN(w/2, 32) * h/2 *2;
909 size = ALIGN(size, 4096);
910 } break;
911 default: break;
912 }
913 return size;
914}
915
916/* Function to allocate memory for the temporary buffer. This memory is
917 * allocated from Ashmem. It is the caller's responsibility to free this
918 * memory.
919 */
920static int get_temp_buffer(const bufferInfo& info, alloc_data& data)
921{
922 ALOGD("%s E", __FUNCTION__);
923 // Alloc memory from system heap
924 data.base = 0;
925 data.fd = -1;
926 data.offset = 0;
927 data.size = get_size(info);
928 data.align = getpagesize();
929 data.uncached = true;
930 int allocFlags = GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP;
931
932 if (sAlloc == 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700933 sAlloc = gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700934 }
935
936 if (sAlloc == 0) {
937 ALOGE("%s: sAlloc is still NULL", __FUNCTION__);
938 return COPYBIT_FAILURE;
939 }
940
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700941 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700942 if (0 != err) {
943 ALOGE("%s: allocate failed", __FUNCTION__);
944 return COPYBIT_FAILURE;
945 }
946
947 ALOGD("%s X", __FUNCTION__);
948 return err;
949}
950
951/* Function to free the temporary allocated memory.*/
952static void free_temp_buffer(alloc_data &data)
953{
954 if (-1 != data.fd) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700955 IMemAlloc* memalloc = sAlloc->getAllocator(data.allocType);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700956 memalloc->free_buffer(data.base, data.size, 0, data.fd);
957 }
958}
959
960/* Function to perform the software color conversion. Convert the
961 * C2D compatible format to the Android compatible format
962 */
963static int copy_image(private_handle_t *src_handle,
964 struct copybit_image_t const *rhs,
965 eConversionType conversionType)
966{
967 if (src_handle->fd == -1) {
968 ALOGE("%s: src_handle fd is invalid", __FUNCTION__);
969 return COPYBIT_FAILURE;
970 }
971
972 // Copy the info.
973 int ret = COPYBIT_SUCCESS;
974 switch(rhs->format) {
975 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
976 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
977 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
978 {
979 if (CONVERT_TO_ANDROID_FORMAT == conversionType) {
980 return convert_yuv_c2d_to_yuv_android(src_handle, rhs);
981 } else {
982 return convert_yuv_android_to_yuv_c2d(src_handle, rhs);
983 }
984
985 } break;
986 default: {
987 ALOGE("%s: invalid format 0x%x", __FUNCTION__, rhs->format);
988 ret = COPYBIT_FAILURE;
989 } break;
990 }
991 return ret;
992}
993
994static void delete_handle(private_handle_t *handle)
995{
996 if (handle) {
997 delete handle;
998 handle = 0;
999 }
1000}
1001/** do a stretch blit type operation */
1002static int stretch_copybit_internal(
1003 struct copybit_device_t *dev,
1004 struct copybit_image_t const *dst,
1005 struct copybit_image_t const *src,
1006 struct copybit_rect_t const *dst_rect,
1007 struct copybit_rect_t const *src_rect,
1008 struct copybit_region_t const *region,
1009 bool enableBlend)
1010{
1011 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
1012 int status = COPYBIT_SUCCESS;
1013 uint32 maxCount;
1014 uint32 src_mapped = 0, trg_mapped = 0;
1015 blitlist list;
1016 C2D_OBJECT *req;
1017 memset(&list, 0, sizeof(list));
1018 int cformat;
1019 c2d_ts_handle timestamp;
1020 uint32 src_surface_index = 0, dst_surface_index = 0;
1021
1022 if (!ctx) {
1023 ALOGE("%s: null context error", __FUNCTION__);
1024 return -EINVAL;
1025 }
1026
1027 if (src->w > MAX_DIMENSION || src->h > MAX_DIMENSION) {
1028 ALOGE("%s: src dimension error", __FUNCTION__);
1029 return -EINVAL;
1030 }
1031
1032 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
1033 ALOGE("%s : dst dimension error dst w %d h %d", __FUNCTION__, dst->w, dst->h);
1034 return -EINVAL;
1035 }
1036
1037 maxCount = sizeof(list.blitObjects)/sizeof(C2D_OBJECT);
1038
1039 struct copybit_rect_t clip;
1040 list.count = 0;
1041
1042 if (is_valid_destination_format(dst->format) == COPYBIT_FAILURE) {
1043 ALOGE("%s: Invalid destination format format = 0x%x", __FUNCTION__, dst->format);
1044 return COPYBIT_FAILURE;
1045 }
1046
1047 bool isYUVDestination = false;
1048 if (is_supported_rgb_format(dst->format) == COPYBIT_SUCCESS) {
1049 dst_surface_index = RGB_SURFACE;
1050 } else if (is_supported_yuv_format(dst->format) == COPYBIT_SUCCESS) {
1051 isYUVDestination = true;
1052 int num_planes = get_num_planes(dst->format);
1053 if (num_planes == 2) {
1054 dst_surface_index = YUV_SURFACE_2_PLANES;
1055 } else if (num_planes == 3) {
1056 dst_surface_index = YUV_SURFACE_3_PLANES;
1057 } else {
1058 ALOGE("%s: dst number of YUV planes is invalid dst format = 0x%x",
1059 __FUNCTION__, dst->format);
1060 return COPYBIT_FAILURE;
1061 }
1062 } else {
1063 ALOGE("%s: Invalid dst surface format 0x%x", __FUNCTION__, dst->format);
1064 return COPYBIT_FAILURE;
1065 }
1066
1067 copybit_image_t dst_image;
1068 dst_image.w = dst->w;
1069 dst_image.h = dst->h;
1070 dst_image.format = dst->format;
1071 dst_image.handle = dst->handle;
1072 // Check if we need a temp. copy for the destination. We'd need this the destination
1073 // width is not aligned to 32. This case occurs for YUV formats. RGB formats are
1074 // aligned to 32.
1075 bool needTempDestination = need_temp_buffer(dst);
1076 bufferInfo dst_info;
1077 populate_buffer_info(dst, dst_info);
1078 private_handle_t* dst_hnd = new private_handle_t(-1, 0, 0, 0, dst_info.format,
1079 dst_info.width, dst_info.height);
1080 if (dst_hnd == NULL) {
1081 ALOGE("%s: dst_hnd is null", __FUNCTION__);
1082 return COPYBIT_FAILURE;
1083 }
1084 if (needTempDestination) {
1085 if (get_size(dst_info) != ctx->temp_dst_buffer.size) {
1086 free_temp_buffer(ctx->temp_dst_buffer);
1087 // Create a temp buffer and set that as the destination.
1088 if (COPYBIT_FAILURE == get_temp_buffer(dst_info, ctx->temp_dst_buffer)) {
1089 ALOGE("%s: get_temp_buffer(dst) failed", __FUNCTION__);
1090 delete_handle(dst_hnd);
1091 return COPYBIT_FAILURE;
1092 }
1093 }
1094 dst_hnd->fd = ctx->temp_dst_buffer.fd;
1095 dst_hnd->size = ctx->temp_dst_buffer.size;
1096 dst_hnd->flags = ctx->temp_dst_buffer.allocType;
1097 dst_hnd->base = (int)(ctx->temp_dst_buffer.base);
1098 dst_hnd->offset = ctx->temp_dst_buffer.offset;
1099 dst_hnd->gpuaddr = 0;
1100 dst_image.handle = dst_hnd;
1101 }
1102
1103 int flags = 0;
1104 flags |= (ctx->isPremultipliedAlpha) ? FLAGS_PREMULTIPLIED_ALPHA : 0;
1105 flags |= (isYUVDestination) ? FLAGS_YUV_DESTINATION : 0;
1106
1107 status = set_image( ctx->dst[dst_surface_index], &dst_image,
1108 &cformat, &trg_mapped, (eC2DFlags)flags);
1109 if(status) {
1110 ALOGE("%s: dst: set_image error", __FUNCTION__);
1111 delete_handle(dst_hnd);
1112 return COPYBIT_FAILURE;
1113 }
1114
1115 if(is_supported_rgb_format(src->format) == COPYBIT_SUCCESS) {
1116 src_surface_index = RGB_SURFACE;
1117 } else if (is_supported_yuv_format(src->format) == COPYBIT_SUCCESS) {
1118 int num_planes = get_num_planes(src->format);
1119 if (num_planes == 2) {
1120 src_surface_index = YUV_SURFACE_2_PLANES;
1121 } else if (num_planes == 3) {
1122 src_surface_index = YUV_SURFACE_3_PLANES;
1123 } else {
1124 ALOGE("%s: src number of YUV planes is invalid src format = 0x%x",
1125 __FUNCTION__, src->format);
1126 delete_handle(dst_hnd);
1127 return -EINVAL;
1128 }
1129 } else {
1130 ALOGE("%s: Invalid source surface format 0x%x", __FUNCTION__, src->format);
1131 delete_handle(dst_hnd);
1132 return -EINVAL;
1133 }
1134
1135 copybit_image_t src_image;
1136 src_image.w = src->w;
1137 src_image.h = src->h;
1138 src_image.format = src->format;
1139 src_image.handle = src->handle;
1140
1141 bool needTempSource = need_temp_buffer(src);
1142 bufferInfo src_info;
1143 populate_buffer_info(src, src_info);
1144 private_handle_t* src_hnd = new private_handle_t(-1, 0, 0, 0, src_info.format,
1145 src_info.width, src_info.height);
1146 if (NULL == src_hnd) {
1147 ALOGE("%s: src_hnd is null", __FUNCTION__);
1148 delete_handle(dst_hnd);
1149 return COPYBIT_FAILURE;
1150 }
1151 if (needTempSource) {
1152 if (get_size(src_info) != ctx->temp_src_buffer.size) {
1153 free_temp_buffer(ctx->temp_src_buffer);
1154 // Create a temp buffer and set that as the destination.
1155 if (COPYBIT_SUCCESS != get_temp_buffer(src_info, ctx->temp_src_buffer)) {
1156 ALOGE("%s: get_temp_buffer(src) failed", __FUNCTION__);
1157 delete_handle(dst_hnd);
1158 delete_handle(src_hnd);
1159 return COPYBIT_FAILURE;
1160 }
1161 }
1162 src_hnd->fd = ctx->temp_src_buffer.fd;
1163 src_hnd->size = ctx->temp_src_buffer.size;
1164 src_hnd->flags = ctx->temp_src_buffer.allocType;
1165 src_hnd->base = (int)(ctx->temp_src_buffer.base);
1166 src_hnd->offset = ctx->temp_src_buffer.offset;
1167 src_hnd->gpuaddr = 0;
1168 src_image.handle = src_hnd;
1169
1170 // Copy the source.
1171 copy_image((private_handle_t *)src->handle, &src_image, CONVERT_TO_C2D_FORMAT);
1172
1173 // Flush the cache
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001174 IMemAlloc* memalloc = sAlloc->getAllocator(src_hnd->flags);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001175 if (memalloc->clean_buffer((void *)(src_hnd->base), src_hnd->size,
1176 src_hnd->offset, src_hnd->fd)) {
1177 ALOGE("%s: clean_buffer failed", __FUNCTION__);
1178 delete_handle(dst_hnd);
1179 delete_handle(src_hnd);
1180 return COPYBIT_FAILURE;
1181 }
1182 }
1183
1184 status = set_image( ctx->src[src_surface_index], &src_image,
1185 &cformat, &src_mapped, (eC2DFlags)flags);
1186 if(status) {
1187 ALOGE("%s: set_src_image error", __FUNCTION__);
1188 delete_handle(dst_hnd);
1189 delete_handle(src_hnd);
1190 return COPYBIT_FAILURE;
1191 }
1192
1193 if (enableBlend) {
1194 if(ctx->blitState.config_mask & C2D_GLOBAL_ALPHA_BIT) {
1195 ctx->blitState.config_mask &= ~C2D_ALPHA_BLEND_NONE;
1196 if(!(ctx->blitState.global_alpha)) {
1197 // src alpha is zero
1198 unset_image( ctx->src[src_surface_index],
1199 &src_image, src_mapped);
1200 unset_image( ctx->dst[dst_surface_index],
1201 &dst_image, trg_mapped);
1202 delete_handle(dst_hnd);
1203 delete_handle(src_hnd);
1204 return status;
1205 }
1206 } else {
1207 if(is_alpha(cformat))
1208 ctx->blitState.config_mask &= ~C2D_ALPHA_BLEND_NONE;
1209 else
1210 ctx->blitState.config_mask |= C2D_ALPHA_BLEND_NONE;
1211 }
1212 } else {
1213 ctx->blitState.config_mask |= C2D_ALPHA_BLEND_NONE;
1214 }
1215
1216 ctx->blitState.surface_id = ctx->src[src_surface_index];
1217
1218 while ((status == 0) && region->next(region, &clip)) {
1219 req = &(list.blitObjects[list.count]);
1220 memcpy(req,&ctx->blitState,sizeof(C2D_OBJECT));
1221
1222 set_rects(ctx, req, dst_rect, src_rect, &clip);
1223
1224 if (++list.count == maxCount) {
1225 status = msm_copybit(ctx, &list, ctx->dst[dst_surface_index]);
1226 list.count = 0;
1227 }
1228 }
1229 if ((status == 0) && list.count) {
1230 status = msm_copybit(ctx, &list, ctx->dst[dst_surface_index]);
1231 }
1232
1233 if(LINK_c2dFinish(ctx->dst[dst_surface_index])) {
1234 ALOGE("%s: LINK_c2dFinish ERROR", __FUNCTION__);
1235 }
1236
1237 unset_image( ctx->src[src_surface_index], &src_image,
1238 src_mapped);
1239 unset_image( ctx->dst[dst_surface_index], &dst_image,
1240 trg_mapped);
1241 if (needTempDestination) {
1242 // copy the temp. destination without the alignment to the actual destination.
1243 copy_image(dst_hnd, dst, CONVERT_TO_ANDROID_FORMAT);
1244 // Invalidate the cache.
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001245 IMemAlloc* memalloc = sAlloc->getAllocator(dst_hnd->flags);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001246 memalloc->clean_buffer((void *)(dst_hnd->base), dst_hnd->size,
1247 dst_hnd->offset, dst_hnd->fd);
1248 }
1249 delete_handle(dst_hnd);
1250 delete_handle(src_hnd);
1251 ctx->isPremultipliedAlpha = false;
1252 ctx->fb_width = 0;
1253 ctx->fb_height = 0;
1254 return status;
1255}
1256
1257static int stretch_copybit(
1258 struct copybit_device_t *dev,
1259 struct copybit_image_t const *dst,
1260 struct copybit_image_t const *src,
1261 struct copybit_rect_t const *dst_rect,
1262 struct copybit_rect_t const *src_rect,
1263 struct copybit_region_t const *region)
1264{
1265 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
1266 bool needsBlending = (ctx->blitState.global_alpha != 0);
1267 return stretch_copybit_internal(dev, dst, src, dst_rect, src_rect,
1268 region, needsBlending);
1269}
1270
1271/** Perform a blit type operation */
1272static int blit_copybit(
1273 struct copybit_device_t *dev,
1274 struct copybit_image_t const *dst,
1275 struct copybit_image_t const *src,
1276 struct copybit_region_t const *region)
1277{
1278 struct copybit_rect_t dr = { 0, 0, dst->w, dst->h };
1279 struct copybit_rect_t sr = { 0, 0, src->w, src->h };
1280 return stretch_copybit_internal(dev, dst, src, &dr, &sr, region, false);
1281}
1282
1283/*****************************************************************************/
1284
1285/** Close the copybit device */
1286static int close_copybit(struct hw_device_t *dev)
1287{
1288 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
1289 if (ctx) {
1290 for(int i = 0; i <NUM_SURFACES; i++) {
1291 LINK_c2dDestroySurface(ctx->dst[i]);
1292 LINK_c2dDestroySurface(ctx->src[i]);
1293 }
1294
1295 if (ctx->libc2d2) {
1296 ::dlclose(ctx->libc2d2);
1297 ALOGV("dlclose(libc2d2)");
1298 }
1299
1300 free_temp_buffer(ctx->temp_src_buffer);
1301 free_temp_buffer(ctx->temp_dst_buffer);
1302 free(ctx);
1303 }
1304
1305 return 0;
1306}
1307
1308/** Open a new instance of a copybit device using name */
1309static int open_copybit(const struct hw_module_t* module, const char* name,
1310 struct hw_device_t** device)
1311{
1312 int status = COPYBIT_SUCCESS;
1313 C2D_RGB_SURFACE_DEF surfDefinition = {0};
1314 C2D_YUV_SURFACE_DEF yuvSurfaceDef = {0} ;
1315 struct copybit_context_t *ctx;
1316 char fbName[64];
1317
1318 ctx = (struct copybit_context_t *)malloc(sizeof(struct copybit_context_t));
1319 if(!ctx) {
1320 ALOGE("%s: malloc failed", __FUNCTION__);
1321 return COPYBIT_FAILURE;
1322 }
1323
1324 /* initialize drawstate */
1325 memset(ctx, 0, sizeof(*ctx));
1326
1327 for (int i=0; i< NUM_SURFACES; i++) {
1328 ctx->dst[i] = -1;
1329 ctx->src[i] = -1;
1330 }
1331
1332 ctx->libc2d2 = ::dlopen("libC2D2.so", RTLD_NOW);
1333 if (!ctx->libc2d2) {
1334 ALOGE("FATAL ERROR: could not dlopen libc2d2.so: %s", dlerror());
1335 goto error;
1336 }
1337 *(void **)&LINK_c2dCreateSurface = ::dlsym(ctx->libc2d2,
1338 "c2dCreateSurface");
1339 *(void **)&LINK_c2dUpdateSurface = ::dlsym(ctx->libc2d2,
1340 "c2dUpdateSurface");
1341 *(void **)&LINK_c2dReadSurface = ::dlsym(ctx->libc2d2,
1342 "c2dReadSurface");
1343 *(void **)&LINK_c2dDraw = ::dlsym(ctx->libc2d2, "c2dDraw");
1344 *(void **)&LINK_c2dFlush = ::dlsym(ctx->libc2d2, "c2dFlush");
1345 *(void **)&LINK_c2dFinish = ::dlsym(ctx->libc2d2, "c2dFinish");
1346 *(void **)&LINK_c2dWaitTimestamp = ::dlsym(ctx->libc2d2,
1347 "c2dWaitTimestamp");
1348 *(void **)&LINK_c2dDestroySurface = ::dlsym(ctx->libc2d2,
1349 "c2dDestroySurface");
1350 *(void **)&LINK_c2dMapAddr = ::dlsym(ctx->libc2d2,
1351 "c2dMapAddr");
1352 *(void **)&LINK_c2dUnMapAddr = ::dlsym(ctx->libc2d2,
1353 "c2dUnMapAddr");
1354
1355 if (!LINK_c2dCreateSurface || !LINK_c2dUpdateSurface || !LINK_c2dReadSurface
1356 || !LINK_c2dDraw || !LINK_c2dFlush || !LINK_c2dWaitTimestamp || !LINK_c2dFinish
1357 || !LINK_c2dDestroySurface) {
1358 ALOGE("%s: dlsym ERROR", __FUNCTION__);
1359 goto error;
1360 }
1361
1362 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
1363 ctx->device.common.version = 1;
1364 ctx->device.common.module = (hw_module_t*)(module);
1365 ctx->device.common.close = close_copybit;
1366 ctx->device.set_parameter = set_parameter_copybit;
1367 ctx->device.get = get;
1368 ctx->device.blit = blit_copybit;
1369 ctx->device.stretch = stretch_copybit;
1370 ctx->blitState.config_mask = C2D_NO_BILINEAR_BIT | C2D_NO_ANTIALIASING_BIT;
1371 ctx->trg_transform = C2D_TARGET_ROTATE_0;
1372
1373 /* Create RGB Surface */
1374 surfDefinition.buffer = (void*)0xdddddddd;
1375 surfDefinition.phys = (void*)0xdddddddd;
1376 surfDefinition.stride = 1 * 4;
1377 surfDefinition.width = 1;
1378 surfDefinition.height = 1;
1379 surfDefinition.format = C2D_COLOR_FORMAT_8888_ARGB;
1380 if (LINK_c2dCreateSurface(&(ctx->dst[RGB_SURFACE]), C2D_TARGET | C2D_SOURCE,
1381 (C2D_SURFACE_TYPE)(C2D_SURFACE_RGB_HOST |
1382 C2D_SURFACE_WITH_PHYS | C2D_SURFACE_WITH_PHYS_DUMMY ), &surfDefinition)) {
1383 ALOGE("%s: create ctx->dst[RGB_SURFACE] failed", __FUNCTION__);
1384 ctx->dst[RGB_SURFACE] = -1;
1385 goto error;
1386 }
1387
1388
1389 if (LINK_c2dCreateSurface(&(ctx->src[RGB_SURFACE]), C2D_TARGET | C2D_SOURCE,
1390 (C2D_SURFACE_TYPE)(C2D_SURFACE_RGB_HOST |
1391 C2D_SURFACE_WITH_PHYS | C2D_SURFACE_WITH_PHYS_DUMMY), &surfDefinition)) {
1392 ALOGE("%s: create ctx->src[RGB_SURFACE] failed", __FUNCTION__);
1393 ctx->src[RGB_SURFACE] = -1;
1394 goto error;
1395 }
1396
1397 /* Create YUV source surface */
1398 yuvSurfaceDef.format = C2D_COLOR_FORMAT_420_NV12;
1399
1400 yuvSurfaceDef.width = 4;
1401 yuvSurfaceDef.height = 4;
1402 yuvSurfaceDef.plane0 = (void*)0xaaaaaaaa;
1403 yuvSurfaceDef.phys0 = (void*) 0xaaaaaaaa;
1404 yuvSurfaceDef.stride0 = 4;
1405
1406 yuvSurfaceDef.plane1 = (void*)0xaaaaaaaa;
1407 yuvSurfaceDef.phys1 = (void*) 0xaaaaaaaa;
1408 yuvSurfaceDef.stride1 = 4;
1409
1410 if (LINK_c2dCreateSurface(&(ctx->src[YUV_SURFACE_2_PLANES]),
1411 C2D_TARGET | C2D_SOURCE,
1412 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST|C2D_SURFACE_WITH_PHYS | C2D_SURFACE_WITH_PHYS_DUMMY),
1413 &yuvSurfaceDef)) {
1414 ALOGE("%s: create ctx->src[YUV_SURFACE_2_PLANES] failed", __FUNCTION__);
1415 ctx->src[YUV_SURFACE_2_PLANES] = -1;
1416 goto error;
1417 }
1418
1419 if (LINK_c2dCreateSurface(&(ctx->dst[YUV_SURFACE_2_PLANES]),
1420 C2D_TARGET | C2D_SOURCE,
1421 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST | C2D_SURFACE_WITH_PHYS | C2D_SURFACE_WITH_PHYS_DUMMY),
1422 &yuvSurfaceDef)) {
1423 ALOGE("%s: create ctx->dst[YUV_SURFACE_2_PLANES] failed", __FUNCTION__);
1424 ctx->dst[YUV_SURFACE_2_PLANES] = -1;
1425 goto error;
1426 }
1427
1428 yuvSurfaceDef.format = C2D_COLOR_FORMAT_420_YV12;
1429 yuvSurfaceDef.plane2 = (void*)0xaaaaaaaa;
1430 yuvSurfaceDef.phys2 = (void*) 0xaaaaaaaa;
1431 yuvSurfaceDef.stride2 = 4;
1432
1433 if (LINK_c2dCreateSurface(&(ctx->src[YUV_SURFACE_3_PLANES]),
1434 C2D_TARGET | C2D_SOURCE,
1435 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST | C2D_SURFACE_WITH_PHYS | C2D_SURFACE_WITH_PHYS_DUMMY),
1436 &yuvSurfaceDef)) {
1437 ALOGE("%s: create ctx->src[YUV_SURFACE_3_PLANES] failed", __FUNCTION__);
1438 ctx->src[YUV_SURFACE_3_PLANES] = -1;
1439 goto error;
1440 }
1441
1442 if (LINK_c2dCreateSurface(&(ctx->dst[YUV_SURFACE_3_PLANES]),
1443 C2D_TARGET | C2D_SOURCE,
1444 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST | C2D_SURFACE_WITH_PHYS | C2D_SURFACE_WITH_PHYS_DUMMY),
1445 &yuvSurfaceDef)) {
1446 ALOGE("%s: create ctx->dst[YUV_SURFACE_3_PLANES] failed", __FUNCTION__);
1447 ctx->dst[YUV_SURFACE_3_PLANES] = -1;
1448 goto error;
1449 }
1450
1451 ctx->temp_src_buffer.fd = -1;
1452 ctx->temp_src_buffer.base = 0;
1453 ctx->temp_src_buffer.size = 0;
1454
1455 ctx->temp_dst_buffer.fd = -1;
1456 ctx->temp_dst_buffer.base = 0;
1457 ctx->temp_dst_buffer.size = 0;
1458
1459 ctx->fb_width = 0;
1460 ctx->fb_height = 0;
1461 ctx->isPremultipliedAlpha = false;
1462
1463 *device = &ctx->device.common;
1464 return status;
1465
1466error:
1467 for (int i = 0; i<NUM_SURFACES; i++) {
1468 if (-1 != (ctx->src[i])) {
1469 LINK_c2dDestroySurface(ctx->src[i]);
1470 ctx->src[i] = -1;
1471 }
1472 if (-1 != (ctx->dst[i])) {
1473 LINK_c2dDestroySurface(ctx->dst[i]);
1474 ctx->dst[i] = -1;
1475 }
1476 }
1477 if (ctx->libc2d2)
1478 ::dlclose(ctx->libc2d2);
1479 if (ctx)
1480 free(ctx);
1481 status = COPYBIT_FAILURE;
1482 *device = NULL;
1483
1484 return status;
1485}