blob: 2fa55cde47a0fedf60a4b0b29f1ba1b14015c399 [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08003 * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
4 *
5 * Not a Contribution, Apache license notifications and license are retained
6 * for attribution purposes only.
Naseer Ahmed29a26812012-06-14 00:56:20 -07007 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
Naseer Ahmed29a26812012-06-14 00:56:20 -070020#include <cutils/log.h>
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -080021#include <sys/resource.h>
22#include <sys/prctl.h>
Naseer Ahmed29a26812012-06-14 00:56:20 -070023
24#include <stdint.h>
25#include <string.h>
26#include <unistd.h>
27#include <errno.h>
28#include <fcntl.h>
29
30#include <sys/ioctl.h>
31#include <sys/types.h>
32#include <sys/mman.h>
33
34#include <linux/msm_kgsl.h>
35
36#include <EGL/eglplatform.h>
37#include <cutils/native_handle.h>
38#include <cutils/ashmem.h>
39#include <linux/ashmem.h>
40#include <gralloc_priv.h>
41
42#include <copybit.h>
43#include <alloc_controller.h>
44#include <memalloc.h>
45
46#include "c2d2.h"
47#include "software_converter.h"
48
49#include <dlfcn.h>
50
51using gralloc::IMemAlloc;
52using gralloc::IonController;
53using gralloc::alloc_data;
Naseer Ahmed29a26812012-06-14 00:56:20 -070054
55C2D_STATUS (*LINK_c2dCreateSurface)( uint32 *surface_id,
56 uint32 surface_bits,
57 C2D_SURFACE_TYPE surface_type,
58 void *surface_definition );
59
60C2D_STATUS (*LINK_c2dUpdateSurface)( uint32 surface_id,
61 uint32 surface_bits,
62 C2D_SURFACE_TYPE surface_type,
63 void *surface_definition );
64
65C2D_STATUS (*LINK_c2dReadSurface)( uint32 surface_id,
66 C2D_SURFACE_TYPE surface_type,
67 void *surface_definition,
68 int32 x, int32 y );
69
70C2D_STATUS (*LINK_c2dDraw)( uint32 target_id,
71 uint32 target_config, C2D_RECT *target_scissor,
72 uint32 target_mask_id, uint32 target_color_key,
73 C2D_OBJECT *objects_list, uint32 num_objects );
74
75C2D_STATUS (*LINK_c2dFinish)( uint32 target_id);
76
77C2D_STATUS (*LINK_c2dFlush)( uint32 target_id, c2d_ts_handle *timestamp);
78
79C2D_STATUS (*LINK_c2dWaitTimestamp)( c2d_ts_handle timestamp );
80
81C2D_STATUS (*LINK_c2dDestroySurface)( uint32 surface_id );
82
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -080083C2D_STATUS (*LINK_c2dMapAddr) ( int mem_fd, void * hostptr, uint32 len,
84 uint32 offset, uint32 flags, void ** gpuaddr);
Naseer Ahmed29a26812012-06-14 00:56:20 -070085
86C2D_STATUS (*LINK_c2dUnMapAddr) ( void * gpuaddr);
87
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -080088C2D_STATUS (*LINK_c2dGetDriverCapabilities) ( C2D_DRIVER_INFO * driver_info);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -080089
90/* create a fence fd for the timestamp */
91C2D_STATUS (*LINK_c2dCreateFenceFD) ( uint32 target_id, c2d_ts_handle timestamp,
92 int32 *fd);
Naseer Ahmed29a26812012-06-14 00:56:20 -070093/******************************************************************************/
94
95#if defined(COPYBIT_Z180)
96#define MAX_SCALE_FACTOR (4096)
97#define MAX_DIMENSION (4096)
98#else
99#error "Unsupported HW version"
100#endif
101
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800102// The following defines can be changed as required i.e. as we encounter
103// complex use cases.
104#define MAX_RGB_SURFACES 8 // Max. RGB layers currently supported per draw
105#define MAX_YUV_2_PLANE_SURFACES 4// Max. 2-plane YUV layers currently supported per draw
106#define MAX_YUV_3_PLANE_SURFACES 1// Max. 3-plane YUV layers currently supported per draw
107// +1 for the destination surface. We cannot have multiple destination surfaces.
108#define MAX_SURFACES (MAX_RGB_SURFACES + MAX_YUV_2_PLANE_SURFACES + MAX_YUV_3_PLANE_SURFACES + 1)
109#define NUM_SURFACE_TYPES 3 // RGB_SURFACE + YUV_SURFACE_2_PLANES + YUV_SURFACE_3_PLANES
110#define MAX_BLIT_OBJECT_COUNT 50 // Max. blit objects that can be passed per draw
Naseer Ahmed29a26812012-06-14 00:56:20 -0700111
112enum {
113 RGB_SURFACE,
114 YUV_SURFACE_2_PLANES,
115 YUV_SURFACE_3_PLANES
116};
117
118enum eConversionType {
119 CONVERT_TO_ANDROID_FORMAT,
120 CONVERT_TO_C2D_FORMAT
121};
122
123enum eC2DFlags {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800124 FLAGS_PREMULTIPLIED_ALPHA = 1<<0,
125 FLAGS_YUV_DESTINATION = 1<<1,
126 FLAGS_TEMP_SRC_DST = 1<<2
Naseer Ahmed29a26812012-06-14 00:56:20 -0700127};
128
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700129static gralloc::IAllocController* sAlloc = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700130/******************************************************************************/
131
132/** State information for each device instance */
133struct copybit_context_t {
134 struct copybit_device_t device;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800135 // Templates for the various source surfaces. These templates are created
136 // to avoid the expensive create/destroy C2D Surfaces
137 C2D_OBJECT_STR blit_rgb_object[MAX_RGB_SURFACES];
138 C2D_OBJECT_STR blit_yuv_2_plane_object[MAX_YUV_2_PLANE_SURFACES];
139 C2D_OBJECT_STR blit_yuv_3_plane_object[MAX_YUV_3_PLANE_SURFACES];
140 C2D_OBJECT_STR blit_list[MAX_BLIT_OBJECT_COUNT]; // Z-ordered list of blit objects
141 C2D_DRIVER_INFO c2d_driver_info;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700142 void *libc2d2;
143 alloc_data temp_src_buffer;
144 alloc_data temp_dst_buffer;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800145 unsigned int dst[NUM_SURFACE_TYPES]; // dst surfaces
146 unsigned int mapped_gpu_addr[MAX_SURFACES]; // GPU addresses mapped inside copybit
147 int blit_rgb_count; // Total RGB surfaces being blit
148 int blit_yuv_2_plane_count; // Total 2 plane YUV surfaces being
149 int blit_yuv_3_plane_count; // Total 3 plane YUV surfaces being blit
150 int blit_count; // Total blit objects.
151 unsigned int trg_transform; /* target transform */
Naseer Ahmed29a26812012-06-14 00:56:20 -0700152 int fb_width;
153 int fb_height;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800154 int src_global_alpha;
155 int config_mask;
156 int dst_surface_type;
157 bool is_premultiplied_alpha;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800158 void* time_stamp;
159
160 // used for signaling the wait thread
161 bool wait_timestamp;
162 pthread_t wait_thread_id;
163 bool stop_thread;
164 pthread_mutex_t wait_cleanup_lock;
165 pthread_cond_t wait_cleanup_cond;
166
Naseer Ahmed29a26812012-06-14 00:56:20 -0700167};
168
169struct bufferInfo {
170 int width;
171 int height;
172 int format;
173};
174
175struct yuvPlaneInfo {
176 int yStride; //luma stride
177 int plane1_stride;
178 int plane2_stride;
179 int plane1_offset;
180 int plane2_offset;
181};
182
183/**
184 * Common hardware methods
185 */
186
187static int open_copybit(const struct hw_module_t* module, const char* name,
188 struct hw_device_t** device);
189
190static struct hw_module_methods_t copybit_module_methods = {
191open: open_copybit
192};
193
194/*
195 * The COPYBIT Module
196 */
197struct copybit_module_t HAL_MODULE_INFO_SYM = {
198common: {
199tag: HARDWARE_MODULE_TAG,
200 version_major: 1,
201 version_minor: 0,
202 id: COPYBIT_HARDWARE_MODULE_ID,
203 name: "QCT COPYBIT C2D 2.0 Module",
204 author: "Qualcomm",
205 methods: &copybit_module_methods
206 }
207};
208
209
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800210/* thread function which waits on the timeStamp and cleans up the surfaces */
211static void* c2d_wait_loop(void* ptr) {
212 copybit_context_t* ctx = (copybit_context_t*)(ptr);
213 char thread_name[64] = "copybitWaitThr";
214 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
215 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
216
217 while(ctx->stop_thread == false) {
218 pthread_mutex_lock(&ctx->wait_cleanup_lock);
219 while(ctx->wait_timestamp == false && !ctx->stop_thread) {
220 pthread_cond_wait(&(ctx->wait_cleanup_cond),
221 &(ctx->wait_cleanup_lock));
222 }
223 if(ctx->wait_timestamp) {
224 if(LINK_c2dWaitTimestamp(ctx->time_stamp)) {
225 ALOGE("%s: LINK_c2dWaitTimeStamp ERROR!!", __FUNCTION__);
226 }
227 ctx->wait_timestamp = false;
228 // Unmap any mapped addresses.
229 for (int i = 0; i < MAX_SURFACES; i++) {
230 if (ctx->mapped_gpu_addr[i]) {
231 LINK_c2dUnMapAddr( (void*)ctx->mapped_gpu_addr[i]);
232 ctx->mapped_gpu_addr[i] = 0;
233 }
234 }
235 // Reset the counts after the draw.
236 ctx->blit_rgb_count = 0;
237 ctx->blit_yuv_2_plane_count = 0;
238 ctx->blit_yuv_3_plane_count = 0;
239 ctx->blit_count = 0;
240 }
241 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
242 if(ctx->stop_thread)
243 break;
244 }
245 pthread_exit(NULL);
246 return NULL;
247}
248
249
Naseer Ahmed29a26812012-06-14 00:56:20 -0700250/* convert COPYBIT_FORMAT to C2D format */
251static int get_format(int format) {
252 switch (format) {
253 case HAL_PIXEL_FORMAT_RGB_565: return C2D_COLOR_FORMAT_565_RGB;
254 case HAL_PIXEL_FORMAT_RGBX_8888: return C2D_COLOR_FORMAT_8888_ARGB |
255 C2D_FORMAT_SWAP_RB |
256 C2D_FORMAT_DISABLE_ALPHA;
257 case HAL_PIXEL_FORMAT_RGBA_8888: return C2D_COLOR_FORMAT_8888_ARGB |
258 C2D_FORMAT_SWAP_RB;
259 case HAL_PIXEL_FORMAT_BGRA_8888: return C2D_COLOR_FORMAT_8888_ARGB;
260 case HAL_PIXEL_FORMAT_RGBA_5551: return C2D_COLOR_FORMAT_5551_RGBA;
261 case HAL_PIXEL_FORMAT_RGBA_4444: return C2D_COLOR_FORMAT_4444_RGBA;
262 case HAL_PIXEL_FORMAT_YCbCr_420_SP: return C2D_COLOR_FORMAT_420_NV12;
263 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:return C2D_COLOR_FORMAT_420_NV12;
264 case HAL_PIXEL_FORMAT_YCrCb_420_SP: return C2D_COLOR_FORMAT_420_NV21;
265 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: return C2D_COLOR_FORMAT_420_NV12 |
266 C2D_FORMAT_MACROTILED;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800267 default: ALOGE("%s: invalid format (0x%x",
268 __FUNCTION__, format);
269 return -EINVAL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700270 }
271 return -EINVAL;
272}
273
274/* Get the C2D formats needed for conversion to YUV */
275static int get_c2d_format_for_yuv_destination(int halFormat) {
276 switch (halFormat) {
277 // We do not swap the RB when the target is YUV
278 case HAL_PIXEL_FORMAT_RGBX_8888: return C2D_COLOR_FORMAT_8888_ARGB |
279 C2D_FORMAT_DISABLE_ALPHA;
280 case HAL_PIXEL_FORMAT_RGBA_8888: return C2D_COLOR_FORMAT_8888_ARGB;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800281 // The U and V need to be interchanged when the target is YUV
Naseer Ahmed29a26812012-06-14 00:56:20 -0700282 case HAL_PIXEL_FORMAT_YCbCr_420_SP: return C2D_COLOR_FORMAT_420_NV21;
283 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:return C2D_COLOR_FORMAT_420_NV21;
284 case HAL_PIXEL_FORMAT_YCrCb_420_SP: return C2D_COLOR_FORMAT_420_NV12;
285 default: return get_format(halFormat);
286 }
287 return -EINVAL;
288}
289
290/* ------------------------------------------------------------------- *//*!
291 * \internal
292 * \brief Get the bpp for a particular color format
293 * \param color format
294 * \return bits per pixel
295 *//* ------------------------------------------------------------------- */
296int c2diGetBpp(int32 colorformat)
297{
298
299 int c2dBpp = 0;
300
301 switch(colorformat&0xFF)
302 {
303 case C2D_COLOR_FORMAT_4444_RGBA:
304 case C2D_COLOR_FORMAT_4444_ARGB:
305 case C2D_COLOR_FORMAT_1555_ARGB:
306 case C2D_COLOR_FORMAT_565_RGB:
307 case C2D_COLOR_FORMAT_5551_RGBA:
308 c2dBpp = 16;
309 break;
310 case C2D_COLOR_FORMAT_8888_RGBA:
311 case C2D_COLOR_FORMAT_8888_ARGB:
312 c2dBpp = 32;
313 break;
314 case C2D_COLOR_FORMAT_8_L:
315 case C2D_COLOR_FORMAT_8_A:
316 c2dBpp = 8;
317 break;
318 case C2D_COLOR_FORMAT_4_A:
319 c2dBpp = 4;
320 break;
321 case C2D_COLOR_FORMAT_1:
322 c2dBpp = 1;
323 break;
324 default:
325 ALOGE("%s ERROR", __func__);
326 break;
327 }
328 return c2dBpp;
329}
330
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800331static uint32 c2d_get_gpuaddr(copybit_context_t* ctx, struct private_handle_t *handle,
332 int &mapped_idx)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700333{
334 uint32 memtype, *gpuaddr;
335 C2D_STATUS rc;
336
337 if(!handle)
338 return 0;
339
340 if (handle->flags & (private_handle_t::PRIV_FLAGS_USES_PMEM |
341 private_handle_t::PRIV_FLAGS_USES_PMEM_ADSP))
342 memtype = KGSL_USER_MEM_TYPE_PMEM;
343 else if (handle->flags & private_handle_t::PRIV_FLAGS_USES_ASHMEM)
344 memtype = KGSL_USER_MEM_TYPE_ASHMEM;
345 else if (handle->flags & private_handle_t::PRIV_FLAGS_USES_ION)
346 memtype = KGSL_USER_MEM_TYPE_ION;
347 else {
348 ALOGE("Invalid handle flags: 0x%x", handle->flags);
349 return 0;
350 }
351
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800352 rc = LINK_c2dMapAddr(handle->fd, (void*)handle->base, handle->size,
353 handle->offset, memtype, (void**)&gpuaddr);
354
Naseer Ahmed29a26812012-06-14 00:56:20 -0700355 if (rc == C2D_STATUS_OK) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800356 // We have mapped the GPU address inside copybit. We need to unmap this
357 // address after the blit. Store this address
358 for (int i = 0; i < MAX_SURFACES; i++) {
359 if (ctx->mapped_gpu_addr[i] == 0) {
360 ctx->mapped_gpu_addr[i] = (uint32) gpuaddr;
361 mapped_idx = i;
362 break;
363 }
364 }
365
Naseer Ahmed29a26812012-06-14 00:56:20 -0700366 return (uint32) gpuaddr;
367 }
368 return 0;
369}
370
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800371static void unmap_gpuaddr(copybit_context_t* ctx, int mapped_idx)
372{
373 if (!ctx || (mapped_idx == -1))
374 return;
375
376 if (ctx->mapped_gpu_addr[mapped_idx]) {
377 LINK_c2dUnMapAddr( (void*)ctx->mapped_gpu_addr[mapped_idx]);
378 ctx->mapped_gpu_addr[mapped_idx] = 0;
379 }
380}
381
Naseer Ahmed29a26812012-06-14 00:56:20 -0700382static int is_supported_rgb_format(int format)
383{
384 switch(format) {
385 case HAL_PIXEL_FORMAT_RGBA_8888:
386 case HAL_PIXEL_FORMAT_RGBX_8888:
387 case HAL_PIXEL_FORMAT_RGB_565:
388 case HAL_PIXEL_FORMAT_BGRA_8888:
389 case HAL_PIXEL_FORMAT_RGBA_5551:
390 case HAL_PIXEL_FORMAT_RGBA_4444: {
391 return COPYBIT_SUCCESS;
392 }
393 default:
394 return COPYBIT_FAILURE;
395 }
396}
397
398static int get_num_planes(int format)
399{
400 switch(format) {
401 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
402 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
403 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
404 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: {
405 return 2;
406 }
407 case HAL_PIXEL_FORMAT_YV12: {
408 return 3;
409 }
410 default:
411 return COPYBIT_FAILURE;
412 }
413}
414
415static int is_supported_yuv_format(int format)
416{
417 switch(format) {
418 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
419 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
420 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
421 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: {
422 return COPYBIT_SUCCESS;
423 }
424 default:
425 return COPYBIT_FAILURE;
426 }
427}
428
429static int is_valid_destination_format(int format)
430{
431 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) {
432 // C2D does not support NV12Tile as a destination format.
433 return COPYBIT_FAILURE;
434 }
435 return COPYBIT_SUCCESS;
436}
437
438static int calculate_yuv_offset_and_stride(const bufferInfo& info,
439 yuvPlaneInfo& yuvInfo)
440{
441 int width = info.width;
442 int height = info.height;
443 int format = info.format;
444
445 int aligned_height = 0;
446 int aligned_width = 0, size = 0;
447
448 switch (format) {
449 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: {
450 /* NV12 Tile buffers have their luma height aligned to 32bytes and width
451 * aligned to 128 bytes. The chroma offset starts at an 8K boundary
452 */
453 aligned_height = ALIGN(height, 32);
454 aligned_width = ALIGN(width, 128);
455 size = aligned_width * aligned_height;
456 yuvInfo.plane1_offset = ALIGN(size,8192);
457 yuvInfo.yStride = aligned_width;
458 yuvInfo.plane1_stride = aligned_width;
459 break;
460 }
461 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
462 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
463 case HAL_PIXEL_FORMAT_YCrCb_420_SP: {
464 aligned_width = ALIGN(width, 32);
465 yuvInfo.yStride = aligned_width;
466 yuvInfo.plane1_stride = aligned_width;
467 if (HAL_PIXEL_FORMAT_NV12_ENCODEABLE == format) {
468 // The encoder requires a 2K aligned chroma offset
469 yuvInfo.plane1_offset = ALIGN(aligned_width * height, 2048);
470 } else
471 yuvInfo.plane1_offset = aligned_width * height;
472
473 break;
474 }
475 default: {
476 return COPYBIT_FAILURE;
477 }
478 }
479 return COPYBIT_SUCCESS;
480}
481
482/** create C2D surface from copybit image */
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800483static int set_image(copybit_context_t* ctx, uint32 surfaceId,
484 const struct copybit_image_t *rhs,
485 const eC2DFlags flags, int &mapped_idx)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700486{
487 struct private_handle_t* handle = (struct private_handle_t*)rhs->handle;
488 C2D_SURFACE_TYPE surfaceType;
489 int status = COPYBIT_SUCCESS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800490 uint32 gpuaddr = 0;
491 int c2d_format;
492 mapped_idx = -1;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700493
494 if (flags & FLAGS_YUV_DESTINATION) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800495 c2d_format = get_c2d_format_for_yuv_destination(rhs->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700496 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800497 c2d_format = get_format(rhs->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700498 }
499
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800500 if(c2d_format == -EINVAL) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700501 ALOGE("%s: invalid format", __FUNCTION__);
502 return -EINVAL;
503 }
504
505 if(handle == NULL) {
506 ALOGE("%s: invalid handle", __func__);
507 return -EINVAL;
508 }
509
510 if (handle->gpuaddr == 0) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800511 gpuaddr = c2d_get_gpuaddr(ctx, handle, mapped_idx);
512 if(!gpuaddr) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700513 ALOGE("%s: c2d_get_gpuaddr failed", __FUNCTION__);
514 return COPYBIT_FAILURE;
515 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800516 } else {
517 gpuaddr = handle->gpuaddr;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700518 }
519
520 /* create C2D surface */
521 if(is_supported_rgb_format(rhs->format) == COPYBIT_SUCCESS) {
522 /* RGB */
523 C2D_RGB_SURFACE_DEF surfaceDef;
524
525 surfaceType = (C2D_SURFACE_TYPE) (C2D_SURFACE_RGB_HOST | C2D_SURFACE_WITH_PHYS);
526
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800527 surfaceDef.phys = (void*) gpuaddr;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700528 surfaceDef.buffer = (void*) (handle->base);
529
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800530 surfaceDef.format = c2d_format |
Naseer Ahmed29a26812012-06-14 00:56:20 -0700531 ((flags & FLAGS_PREMULTIPLIED_ALPHA) ? C2D_FORMAT_PREMULTIPLIED : 0);
532 surfaceDef.width = rhs->w;
533 surfaceDef.height = rhs->h;
534 int aligned_width = ALIGN(surfaceDef.width,32);
535 surfaceDef.stride = (aligned_width * c2diGetBpp(surfaceDef.format))>>3;
536
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800537 if(LINK_c2dUpdateSurface( surfaceId,C2D_TARGET | C2D_SOURCE, surfaceType,
538 &surfaceDef)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700539 ALOGE("%s: RGB Surface c2dUpdateSurface ERROR", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800540 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700541 status = COPYBIT_FAILURE;
542 }
543 } else if (is_supported_yuv_format(rhs->format) == COPYBIT_SUCCESS) {
544 C2D_YUV_SURFACE_DEF surfaceDef;
545 memset(&surfaceDef, 0, sizeof(surfaceDef));
546 surfaceType = (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST | C2D_SURFACE_WITH_PHYS);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800547 surfaceDef.format = c2d_format;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700548
549 bufferInfo info;
550 info.width = rhs->w;
551 info.height = rhs->h;
552 info.format = rhs->format;
553
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700554 yuvPlaneInfo yuvInfo = {0};
Naseer Ahmed29a26812012-06-14 00:56:20 -0700555 status = calculate_yuv_offset_and_stride(info, yuvInfo);
556 if(status != COPYBIT_SUCCESS) {
557 ALOGE("%s: calculate_yuv_offset_and_stride error", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800558 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700559 }
560
561 surfaceDef.width = rhs->w;
562 surfaceDef.height = rhs->h;
563 surfaceDef.plane0 = (void*) (handle->base);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800564 surfaceDef.phys0 = (void*) (gpuaddr);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700565 surfaceDef.stride0 = yuvInfo.yStride;
566
567 surfaceDef.plane1 = (void*) (handle->base + yuvInfo.plane1_offset);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800568 surfaceDef.phys1 = (void*) (gpuaddr + yuvInfo.plane1_offset);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700569 surfaceDef.stride1 = yuvInfo.plane1_stride;
570 if (3 == get_num_planes(rhs->format)) {
571 surfaceDef.plane2 = (void*) (handle->base + yuvInfo.plane2_offset);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800572 surfaceDef.phys2 = (void*) (gpuaddr + yuvInfo.plane2_offset);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700573 surfaceDef.stride2 = yuvInfo.plane2_stride;
574 }
575
576 if(LINK_c2dUpdateSurface( surfaceId,C2D_TARGET | C2D_SOURCE, surfaceType,
577 &surfaceDef)) {
578 ALOGE("%s: YUV Surface c2dUpdateSurface ERROR", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800579 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700580 status = COPYBIT_FAILURE;
581 }
582 } else {
583 ALOGE("%s: invalid format 0x%x", __FUNCTION__, rhs->format);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800584 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700585 status = COPYBIT_FAILURE;
586 }
587
588 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700589}
590
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800591/** copy the bits */
592static int msm_copybit(struct copybit_context_t *ctx, unsigned int target)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700593{
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800594 if (ctx->blit_count == 0) {
595 return COPYBIT_SUCCESS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700596 }
597
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800598 for (int i = 0; i < ctx->blit_count; i++)
599 {
600 ctx->blit_list[i].next = &(ctx->blit_list[i+1]);
601 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800602 ctx->blit_list[ctx->blit_count-1].next = NULL;
603 if(LINK_c2dDraw(target,ctx->trg_transform, 0x0, 0, 0, ctx->blit_list,
604 ctx->blit_count)) {
605 ALOGE("%s: LINK_c2dDraw ERROR", __FUNCTION__);
606 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700607 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700608 return COPYBIT_SUCCESS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700609}
610
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800611
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800612
613static int flush_get_fence_copybit (struct copybit_device_t *dev, int* fd)
614{
615 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
616 int status = COPYBIT_FAILURE;
617 if (!ctx)
618 return COPYBIT_FAILURE;
619 pthread_mutex_lock(&ctx->wait_cleanup_lock);
620 status = msm_copybit(ctx, ctx->dst[ctx->dst_surface_type]);
621
622 if(LINK_c2dFlush(ctx->dst[ctx->dst_surface_type], &ctx->time_stamp)) {
623 ALOGE("%s: LINK_c2dFlush ERROR", __FUNCTION__);
624 // unlock the mutex and return failure
625 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
626 return COPYBIT_FAILURE;
627 }
628 if(LINK_c2dCreateFenceFD(ctx->dst[ctx->dst_surface_type], ctx->time_stamp,
629 fd)) {
630 ALOGE("%s: LINK_c2dCreateFenceFD ERROR", __FUNCTION__);
631 status = COPYBIT_FAILURE;
632 }
633 if(status == COPYBIT_SUCCESS) {
634 //signal the wait_thread
635 ctx->wait_timestamp = true;
636 pthread_cond_signal(&ctx->wait_cleanup_cond);
637 }
638 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
639 return status;
640}
641
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800642static int finish_copybit(struct copybit_device_t *dev)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700643{
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800644 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
645 if (!ctx)
646 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700647
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800648 int status = msm_copybit(ctx, ctx->dst[ctx->dst_surface_type]);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700649
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800650 if(LINK_c2dFinish(ctx->dst[ctx->dst_surface_type])) {
651 ALOGE("%s: LINK_c2dFinish ERROR", __FUNCTION__);
652 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700653 }
654
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800655 // Unmap any mapped addresses.
656 for (int i = 0; i < MAX_SURFACES; i++) {
657 if (ctx->mapped_gpu_addr[i]) {
658 LINK_c2dUnMapAddr( (void*)ctx->mapped_gpu_addr[i]);
659 ctx->mapped_gpu_addr[i] = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700660 }
661 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700662
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800663 // Reset the counts after the draw.
664 ctx->blit_rgb_count = 0;
665 ctx->blit_yuv_2_plane_count = 0;
666 ctx->blit_yuv_3_plane_count = 0;
667 ctx->blit_count = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700668 return status;
669}
670
671/** setup rectangles */
672static void set_rects(struct copybit_context_t *ctx,
673 C2D_OBJECT *c2dObject,
674 const struct copybit_rect_t *dst,
675 const struct copybit_rect_t *src,
676 const struct copybit_rect_t *scissor)
677{
678 // Set the target rect.
679 if((ctx->trg_transform & C2D_TARGET_ROTATE_90) &&
680 (ctx->trg_transform & C2D_TARGET_ROTATE_180)) {
681 /* target rotation is 270 */
682 c2dObject->target_rect.x = (dst->t)<<16;
683 c2dObject->target_rect.y = ctx->fb_width?(ALIGN(ctx->fb_width,32)- dst->r):dst->r;
684 c2dObject->target_rect.y = c2dObject->target_rect.y<<16;
685 c2dObject->target_rect.height = ((dst->r) - (dst->l))<<16;
686 c2dObject->target_rect.width = ((dst->b) - (dst->t))<<16;
687 } else if(ctx->trg_transform & C2D_TARGET_ROTATE_90) {
688 c2dObject->target_rect.x = ctx->fb_height?(ctx->fb_height - dst->b):dst->b;
689 c2dObject->target_rect.x = c2dObject->target_rect.x<<16;
690 c2dObject->target_rect.y = (dst->l)<<16;
691 c2dObject->target_rect.height = ((dst->r) - (dst->l))<<16;
692 c2dObject->target_rect.width = ((dst->b) - (dst->t))<<16;
693 } else if(ctx->trg_transform & C2D_TARGET_ROTATE_180) {
694 c2dObject->target_rect.y = ctx->fb_height?(ctx->fb_height - dst->b):dst->b;
695 c2dObject->target_rect.y = c2dObject->target_rect.y<<16;
696 c2dObject->target_rect.x = ctx->fb_width?(ALIGN(ctx->fb_width,32) - dst->r):dst->r;
697 c2dObject->target_rect.x = c2dObject->target_rect.x<<16;
698 c2dObject->target_rect.height = ((dst->b) - (dst->t))<<16;
699 c2dObject->target_rect.width = ((dst->r) - (dst->l))<<16;
700 } else {
701 c2dObject->target_rect.x = (dst->l)<<16;
702 c2dObject->target_rect.y = (dst->t)<<16;
703 c2dObject->target_rect.height = ((dst->b) - (dst->t))<<16;
704 c2dObject->target_rect.width = ((dst->r) - (dst->l))<<16;
705 }
706 c2dObject->config_mask |= C2D_TARGET_RECT_BIT;
707
708 // Set the source rect
709 c2dObject->source_rect.x = (src->l)<<16;
710 c2dObject->source_rect.y = (src->t)<<16;
711 c2dObject->source_rect.height = ((src->b) - (src->t))<<16;
712 c2dObject->source_rect.width = ((src->r) - (src->l))<<16;
713 c2dObject->config_mask |= C2D_SOURCE_RECT_BIT;
714
715 // Set the scissor rect
716 c2dObject->scissor_rect.x = scissor->l;
717 c2dObject->scissor_rect.y = scissor->t;
718 c2dObject->scissor_rect.height = (scissor->b) - (scissor->t);
719 c2dObject->scissor_rect.width = (scissor->r) - (scissor->l);
720 c2dObject->config_mask |= C2D_SCISSOR_RECT_BIT;
721}
722
Naseer Ahmed29a26812012-06-14 00:56:20 -0700723/*****************************************************************************/
724
725/** Set a parameter to value */
726static int set_parameter_copybit(
727 struct copybit_device_t *dev,
728 int name,
729 int value)
730{
731 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800732 int status = COPYBIT_SUCCESS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700733 if (!ctx) {
734 ALOGE("%s: null context", __FUNCTION__);
735 return -EINVAL;
736 }
737
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800738 pthread_mutex_lock(&ctx->wait_cleanup_lock);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700739 switch(name) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700740 case COPYBIT_PLANE_ALPHA:
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800741 {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700742 if (value < 0) value = 0;
743 if (value >= 256) value = 255;
744
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800745 ctx->src_global_alpha = value;
746 if (value < 255)
747 ctx->config_mask |= C2D_GLOBAL_ALPHA_BIT;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700748 else
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800749 ctx->config_mask &= ~C2D_GLOBAL_ALPHA_BIT;
750 }
751 break;
752 case COPYBIT_BLEND_MODE:
753 {
754 if (value == COPYBIT_BLENDING_NONE) {
755 ctx->config_mask |= C2D_ALPHA_BLEND_NONE;
756 ctx->is_premultiplied_alpha = true;
757 } else if (value == COPYBIT_BLENDING_PREMULT) {
758 ctx->is_premultiplied_alpha = true;
759 } else {
760 ctx->config_mask &= ~C2D_ALPHA_BLEND_NONE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700761 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800762 }
763 break;
764 case COPYBIT_TRANSFORM:
765 {
766 unsigned int transform = 0;
767 uint32 config_mask = 0;
768 config_mask |= C2D_OVERRIDE_GLOBAL_TARGET_ROTATE_CONFIG;
769 if((value & 0x7) == COPYBIT_TRANSFORM_ROT_180) {
770 transform = C2D_TARGET_ROTATE_180;
771 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_180;
772 } else if((value & 0x7) == COPYBIT_TRANSFORM_ROT_270) {
773 transform = C2D_TARGET_ROTATE_90;
774 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_90;
775 } else if(value == COPYBIT_TRANSFORM_ROT_90) {
776 transform = C2D_TARGET_ROTATE_270;
777 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_270;
778 } else {
779 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_0;
780 if(value & COPYBIT_TRANSFORM_FLIP_H) {
781 config_mask |= C2D_MIRROR_H_BIT;
782 } else if(value & COPYBIT_TRANSFORM_FLIP_V) {
783 config_mask |= C2D_MIRROR_V_BIT;
784 }
785 }
786
787 if (transform != ctx->trg_transform) {
788 if (ctx->c2d_driver_info.capabilities_mask &
789 C2D_DRIVER_SUPPORTS_OVERRIDE_TARGET_ROTATE_OP) {
790 ctx->config_mask |= config_mask;
791 } else {
792 // The transform for this surface does not match the current
793 // target transform. Draw all previous surfaces. This will be
794 // changed once we have a new mechanism to send different
795 // target rotations to c2d.
796 finish_copybit(dev);
797 }
798 }
799 ctx->trg_transform = transform;
800 }
801 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700802 case COPYBIT_FRAMEBUFFER_WIDTH:
803 ctx->fb_width = value;
804 break;
805 case COPYBIT_FRAMEBUFFER_HEIGHT:
806 ctx->fb_height = value;
807 break;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800808 case COPYBIT_ROTATION_DEG:
809 case COPYBIT_DITHER:
810 case COPYBIT_BLUR:
Naseer Ahmed45a99602012-07-31 19:15:24 -0700811 case COPYBIT_BLIT_TO_FRAMEBUFFER:
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800812 // Do nothing
Naseer Ahmed45a99602012-07-31 19:15:24 -0700813 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700814 default:
815 ALOGE("%s: default case param=0x%x", __FUNCTION__, name);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800816 status = -EINVAL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700817 break;
818 }
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800819 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
820 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700821}
822
823/** Get a static info value */
824static int get(struct copybit_device_t *dev, int name)
825{
826 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
827 int value;
828
829 if (!ctx) {
830 ALOGE("%s: null context error", __FUNCTION__);
831 return -EINVAL;
832 }
833
834 switch(name) {
835 case COPYBIT_MINIFICATION_LIMIT:
836 value = MAX_SCALE_FACTOR;
837 break;
838 case COPYBIT_MAGNIFICATION_LIMIT:
839 value = MAX_SCALE_FACTOR;
840 break;
841 case COPYBIT_SCALING_FRAC_BITS:
842 value = 32;
843 break;
844 case COPYBIT_ROTATION_STEP_DEG:
845 value = 1;
846 break;
847 default:
848 ALOGE("%s: default case param=0x%x", __FUNCTION__, name);
849 value = -EINVAL;
850 }
851 return value;
852}
853
854static int is_alpha(int cformat)
855{
856 int alpha = 0;
857 switch (cformat & 0xFF) {
858 case C2D_COLOR_FORMAT_8888_ARGB:
859 case C2D_COLOR_FORMAT_8888_RGBA:
860 case C2D_COLOR_FORMAT_5551_RGBA:
861 case C2D_COLOR_FORMAT_4444_ARGB:
862 alpha = 1;
863 break;
864 default:
865 alpha = 0;
866 break;
867 }
868
869 if(alpha && (cformat&C2D_FORMAT_DISABLE_ALPHA))
870 alpha = 0;
871
872 return alpha;
873}
874
875/* Function to check if we need a temporary buffer for the blit.
876 * This would happen if the requested destination stride and the
877 * C2D stride do not match. We ignore RGB buffers, since their
878 * stride is always aligned to 32.
879 */
880static bool need_temp_buffer(struct copybit_image_t const *img)
881{
882 if (COPYBIT_SUCCESS == is_supported_rgb_format(img->format))
883 return false;
884
885 struct private_handle_t* handle = (struct private_handle_t*)img->handle;
886
887 // The width parameter in the handle contains the aligned_w. We check if we
888 // need to convert based on this param. YUV formats have bpp=1, so checking
889 // if the requested stride is aligned should suffice.
890 if (0 == (handle->width)%32) {
891 return false;
892 }
893
894 return true;
895}
896
897/* Function to extract the information from the copybit image and set the corresponding
898 * values in the bufferInfo struct.
899 */
900static void populate_buffer_info(struct copybit_image_t const *img, bufferInfo& info)
901{
902 info.width = img->w;
903 info.height = img->h;
904 info.format = img->format;
905}
906
907/* Function to get the required size for a particular format, inorder for C2D to perform
908 * the blit operation.
909 */
910static size_t get_size(const bufferInfo& info)
911{
912 size_t size = 0;
913 int w = info.width;
914 int h = info.height;
915 int aligned_w = ALIGN(w, 32);
916 switch(info.format) {
917 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
918 {
919 // Chroma for this format is aligned to 2K.
920 size = ALIGN((aligned_w*h), 2048) +
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800921 ALIGN(aligned_w/2, 32) * (h/2) *2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700922 size = ALIGN(size, 4096);
923 } break;
924 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
925 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
926 {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800927 size = aligned_w * h +
928 ALIGN(aligned_w/2, 32) * (h/2) * 2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700929 size = ALIGN(size, 4096);
930 } break;
931 default: break;
932 }
933 return size;
934}
935
936/* Function to allocate memory for the temporary buffer. This memory is
937 * allocated from Ashmem. It is the caller's responsibility to free this
938 * memory.
939 */
940static int get_temp_buffer(const bufferInfo& info, alloc_data& data)
941{
942 ALOGD("%s E", __FUNCTION__);
943 // Alloc memory from system heap
944 data.base = 0;
945 data.fd = -1;
946 data.offset = 0;
947 data.size = get_size(info);
948 data.align = getpagesize();
949 data.uncached = true;
950 int allocFlags = GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP;
951
952 if (sAlloc == 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700953 sAlloc = gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700954 }
955
956 if (sAlloc == 0) {
957 ALOGE("%s: sAlloc is still NULL", __FUNCTION__);
958 return COPYBIT_FAILURE;
959 }
960
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700961 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700962 if (0 != err) {
963 ALOGE("%s: allocate failed", __FUNCTION__);
964 return COPYBIT_FAILURE;
965 }
966
967 ALOGD("%s X", __FUNCTION__);
968 return err;
969}
970
971/* Function to free the temporary allocated memory.*/
972static void free_temp_buffer(alloc_data &data)
973{
974 if (-1 != data.fd) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700975 IMemAlloc* memalloc = sAlloc->getAllocator(data.allocType);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700976 memalloc->free_buffer(data.base, data.size, 0, data.fd);
977 }
978}
979
980/* Function to perform the software color conversion. Convert the
981 * C2D compatible format to the Android compatible format
982 */
983static int copy_image(private_handle_t *src_handle,
984 struct copybit_image_t const *rhs,
985 eConversionType conversionType)
986{
987 if (src_handle->fd == -1) {
988 ALOGE("%s: src_handle fd is invalid", __FUNCTION__);
989 return COPYBIT_FAILURE;
990 }
991
992 // Copy the info.
993 int ret = COPYBIT_SUCCESS;
994 switch(rhs->format) {
995 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
996 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
997 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
998 {
999 if (CONVERT_TO_ANDROID_FORMAT == conversionType) {
1000 return convert_yuv_c2d_to_yuv_android(src_handle, rhs);
1001 } else {
1002 return convert_yuv_android_to_yuv_c2d(src_handle, rhs);
1003 }
1004
1005 } break;
1006 default: {
1007 ALOGE("%s: invalid format 0x%x", __FUNCTION__, rhs->format);
1008 ret = COPYBIT_FAILURE;
1009 } break;
1010 }
1011 return ret;
1012}
1013
1014static void delete_handle(private_handle_t *handle)
1015{
1016 if (handle) {
1017 delete handle;
1018 handle = 0;
1019 }
1020}
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001021
1022static bool need_to_execute_draw(struct copybit_context_t* ctx,
1023 eC2DFlags flags)
1024{
1025 if (flags & FLAGS_TEMP_SRC_DST) {
1026 return true;
1027 }
1028 if (flags & FLAGS_YUV_DESTINATION) {
1029 return true;
1030 }
1031 return false;
1032}
1033
Naseer Ahmed29a26812012-06-14 00:56:20 -07001034/** do a stretch blit type operation */
1035static int stretch_copybit_internal(
1036 struct copybit_device_t *dev,
1037 struct copybit_image_t const *dst,
1038 struct copybit_image_t const *src,
1039 struct copybit_rect_t const *dst_rect,
1040 struct copybit_rect_t const *src_rect,
1041 struct copybit_region_t const *region,
1042 bool enableBlend)
1043{
1044 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
1045 int status = COPYBIT_SUCCESS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001046 int flags = 0;
1047 int src_surface_type;
1048 int mapped_src_idx = -1, mapped_dst_idx = -1;
1049 C2D_OBJECT_STR src_surface;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001050
1051 if (!ctx) {
1052 ALOGE("%s: null context error", __FUNCTION__);
1053 return -EINVAL;
1054 }
1055
1056 if (src->w > MAX_DIMENSION || src->h > MAX_DIMENSION) {
1057 ALOGE("%s: src dimension error", __FUNCTION__);
1058 return -EINVAL;
1059 }
1060
1061 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001062 ALOGE("%s : dst dimension error dst w %d h %d", __FUNCTION__, dst->w,
1063 dst->h);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001064 return -EINVAL;
1065 }
1066
Naseer Ahmed29a26812012-06-14 00:56:20 -07001067 if (is_valid_destination_format(dst->format) == COPYBIT_FAILURE) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001068 ALOGE("%s: Invalid destination format format = 0x%x", __FUNCTION__,
1069 dst->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001070 return COPYBIT_FAILURE;
1071 }
1072
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001073 int dst_surface_type;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001074 if (is_supported_rgb_format(dst->format) == COPYBIT_SUCCESS) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001075 dst_surface_type = RGB_SURFACE;
1076 flags |= FLAGS_PREMULTIPLIED_ALPHA;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001077 } else if (is_supported_yuv_format(dst->format) == COPYBIT_SUCCESS) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001078 int num_planes = get_num_planes(dst->format);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001079 flags |= FLAGS_YUV_DESTINATION;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001080 if (num_planes == 2) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001081 dst_surface_type = YUV_SURFACE_2_PLANES;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001082 } else if (num_planes == 3) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001083 dst_surface_type = YUV_SURFACE_3_PLANES;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001084 } else {
1085 ALOGE("%s: dst number of YUV planes is invalid dst format = 0x%x",
1086 __FUNCTION__, dst->format);
1087 return COPYBIT_FAILURE;
1088 }
1089 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001090 ALOGE("%s: Invalid dst surface format 0x%x", __FUNCTION__,
1091 dst->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001092 return COPYBIT_FAILURE;
1093 }
1094
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001095 if (ctx->blit_rgb_count == MAX_RGB_SURFACES ||
1096 ctx->blit_yuv_2_plane_count == MAX_YUV_2_PLANE_SURFACES ||
1097 ctx->blit_yuv_3_plane_count == MAX_YUV_2_PLANE_SURFACES ||
1098 ctx->blit_count == MAX_BLIT_OBJECT_COUNT ||
1099 ctx->dst_surface_type != dst_surface_type) {
1100 // we have reached the max. limits of our internal structures or
1101 // changed the target.
1102 // Draw the remaining surfaces. We need to do the finish here since
1103 // we need to free up the surface templates.
1104 finish_copybit(dev);
1105 }
1106
1107 ctx->dst_surface_type = dst_surface_type;
1108
1109 // Update the destination
Naseer Ahmed29a26812012-06-14 00:56:20 -07001110 copybit_image_t dst_image;
1111 dst_image.w = dst->w;
1112 dst_image.h = dst->h;
1113 dst_image.format = dst->format;
1114 dst_image.handle = dst->handle;
1115 // Check if we need a temp. copy for the destination. We'd need this the destination
1116 // width is not aligned to 32. This case occurs for YUV formats. RGB formats are
1117 // aligned to 32.
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001118 bool need_temp_dst = need_temp_buffer(dst);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001119 bufferInfo dst_info;
1120 populate_buffer_info(dst, dst_info);
1121 private_handle_t* dst_hnd = new private_handle_t(-1, 0, 0, 0, dst_info.format,
1122 dst_info.width, dst_info.height);
1123 if (dst_hnd == NULL) {
1124 ALOGE("%s: dst_hnd is null", __FUNCTION__);
1125 return COPYBIT_FAILURE;
1126 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001127 if (need_temp_dst) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001128 if (get_size(dst_info) != ctx->temp_dst_buffer.size) {
1129 free_temp_buffer(ctx->temp_dst_buffer);
1130 // Create a temp buffer and set that as the destination.
1131 if (COPYBIT_FAILURE == get_temp_buffer(dst_info, ctx->temp_dst_buffer)) {
1132 ALOGE("%s: get_temp_buffer(dst) failed", __FUNCTION__);
1133 delete_handle(dst_hnd);
1134 return COPYBIT_FAILURE;
1135 }
1136 }
1137 dst_hnd->fd = ctx->temp_dst_buffer.fd;
1138 dst_hnd->size = ctx->temp_dst_buffer.size;
1139 dst_hnd->flags = ctx->temp_dst_buffer.allocType;
1140 dst_hnd->base = (int)(ctx->temp_dst_buffer.base);
1141 dst_hnd->offset = ctx->temp_dst_buffer.offset;
1142 dst_hnd->gpuaddr = 0;
1143 dst_image.handle = dst_hnd;
1144 }
1145
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001146 status = set_image(ctx, ctx->dst[ctx->dst_surface_type], &dst_image,
1147 (eC2DFlags)flags, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001148 if(status) {
1149 ALOGE("%s: dst: set_image error", __FUNCTION__);
1150 delete_handle(dst_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001151 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001152 return COPYBIT_FAILURE;
1153 }
1154
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001155 // Update the source
1156 flags = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001157 if(is_supported_rgb_format(src->format) == COPYBIT_SUCCESS) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001158 src_surface_type = RGB_SURFACE;
1159 src_surface = ctx->blit_rgb_object[ctx->blit_rgb_count];
Naseer Ahmed29a26812012-06-14 00:56:20 -07001160 } else if (is_supported_yuv_format(src->format) == COPYBIT_SUCCESS) {
1161 int num_planes = get_num_planes(src->format);
1162 if (num_planes == 2) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001163 src_surface_type = YUV_SURFACE_2_PLANES;
1164 src_surface = ctx->blit_yuv_2_plane_object[ctx->blit_yuv_2_plane_count];
Naseer Ahmed29a26812012-06-14 00:56:20 -07001165 } else if (num_planes == 3) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001166 src_surface_type = YUV_SURFACE_3_PLANES;
1167 src_surface = ctx->blit_yuv_3_plane_object[ctx->blit_yuv_2_plane_count];
Naseer Ahmed29a26812012-06-14 00:56:20 -07001168 } else {
1169 ALOGE("%s: src number of YUV planes is invalid src format = 0x%x",
1170 __FUNCTION__, src->format);
1171 delete_handle(dst_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001172 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001173 return -EINVAL;
1174 }
1175 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001176 ALOGE("%s: Invalid source surface format 0x%x", __FUNCTION__,
1177 src->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001178 delete_handle(dst_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001179 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001180 return -EINVAL;
1181 }
1182
1183 copybit_image_t src_image;
1184 src_image.w = src->w;
1185 src_image.h = src->h;
1186 src_image.format = src->format;
1187 src_image.handle = src->handle;
1188
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001189 bool need_temp_src = need_temp_buffer(src);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001190 bufferInfo src_info;
1191 populate_buffer_info(src, src_info);
1192 private_handle_t* src_hnd = new private_handle_t(-1, 0, 0, 0, src_info.format,
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001193 src_info.width, src_info.height);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001194 if (NULL == src_hnd) {
1195 ALOGE("%s: src_hnd is null", __FUNCTION__);
1196 delete_handle(dst_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001197 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001198 return COPYBIT_FAILURE;
1199 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001200 if (need_temp_src) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001201 if (get_size(src_info) != ctx->temp_src_buffer.size) {
1202 free_temp_buffer(ctx->temp_src_buffer);
1203 // Create a temp buffer and set that as the destination.
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001204 if (COPYBIT_SUCCESS != get_temp_buffer(src_info,
1205 ctx->temp_src_buffer)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001206 ALOGE("%s: get_temp_buffer(src) failed", __FUNCTION__);
1207 delete_handle(dst_hnd);
1208 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001209 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001210 return COPYBIT_FAILURE;
1211 }
1212 }
1213 src_hnd->fd = ctx->temp_src_buffer.fd;
1214 src_hnd->size = ctx->temp_src_buffer.size;
1215 src_hnd->flags = ctx->temp_src_buffer.allocType;
1216 src_hnd->base = (int)(ctx->temp_src_buffer.base);
1217 src_hnd->offset = ctx->temp_src_buffer.offset;
1218 src_hnd->gpuaddr = 0;
1219 src_image.handle = src_hnd;
1220
1221 // Copy the source.
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001222 status = copy_image((private_handle_t *)src->handle, &src_image,
1223 CONVERT_TO_C2D_FORMAT);
1224 if (status == COPYBIT_FAILURE) {
1225 ALOGE("%s:copy_image failed in temp source",__FUNCTION__);
1226 delete_handle(dst_hnd);
1227 delete_handle(src_hnd);
1228 unmap_gpuaddr(ctx, mapped_dst_idx);
1229 return status;
1230 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001231
1232 // Flush the cache
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001233 IMemAlloc* memalloc = sAlloc->getAllocator(src_hnd->flags);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001234 if (memalloc->clean_buffer((void *)(src_hnd->base), src_hnd->size,
1235 src_hnd->offset, src_hnd->fd)) {
1236 ALOGE("%s: clean_buffer failed", __FUNCTION__);
1237 delete_handle(dst_hnd);
1238 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001239 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001240 return COPYBIT_FAILURE;
1241 }
1242 }
1243
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001244 flags |= (ctx->is_premultiplied_alpha) ? FLAGS_PREMULTIPLIED_ALPHA : 0;
1245 flags |= (ctx->dst_surface_type != RGB_SURFACE) ? FLAGS_YUV_DESTINATION : 0;
1246 status = set_image(ctx, src_surface.surface_id, &src_image,
1247 (eC2DFlags)flags, mapped_src_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001248 if(status) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001249 ALOGE("%s: set_image (src) error", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001250 delete_handle(dst_hnd);
1251 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001252 unmap_gpuaddr(ctx, mapped_dst_idx);
1253 unmap_gpuaddr(ctx, mapped_src_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001254 return COPYBIT_FAILURE;
1255 }
1256
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001257 src_surface.config_mask = C2D_NO_BILINEAR_BIT | C2D_NO_ANTIALIASING_BIT |
1258 ctx->config_mask;
1259 src_surface.global_alpha = ctx->src_global_alpha;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001260 if (enableBlend) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001261 if(src_surface.config_mask & C2D_GLOBAL_ALPHA_BIT) {
1262 src_surface.config_mask &= ~C2D_ALPHA_BLEND_NONE;
1263 if(!(src_surface.global_alpha)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001264 // src alpha is zero
Naseer Ahmed29a26812012-06-14 00:56:20 -07001265 delete_handle(dst_hnd);
1266 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001267 unmap_gpuaddr(ctx, mapped_dst_idx);
1268 unmap_gpuaddr(ctx, mapped_src_idx);
1269 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001270 }
1271 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001272 int c2d_format = get_format(src->format);
1273 if(is_alpha(c2d_format))
1274 src_surface.config_mask &= ~C2D_ALPHA_BLEND_NONE;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001275 else
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001276 src_surface.config_mask |= C2D_ALPHA_BLEND_NONE;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001277 }
1278 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001279 src_surface.config_mask |= C2D_ALPHA_BLEND_NONE;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001280 }
1281
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001282 if (src_surface_type == RGB_SURFACE) {
1283 ctx->blit_rgb_object[ctx->blit_rgb_count] = src_surface;
1284 ctx->blit_rgb_count++;
1285 } else if (src_surface_type == YUV_SURFACE_2_PLANES) {
1286 ctx->blit_yuv_2_plane_object[ctx->blit_yuv_2_plane_count] = src_surface;
1287 ctx->blit_yuv_2_plane_count++;
1288 } else {
1289 ctx->blit_yuv_3_plane_object[ctx->blit_yuv_3_plane_count] = src_surface;
1290 ctx->blit_yuv_3_plane_count++;
1291 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001292
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001293 struct copybit_rect_t clip;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001294 while ((status == 0) && region->next(region, &clip)) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001295 set_rects(ctx, &(src_surface), dst_rect, src_rect, &clip);
1296 if (ctx->blit_count == MAX_BLIT_OBJECT_COUNT) {
1297 ALOGW("Reached end of blit count");
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001298 finish_copybit(dev);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001299 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001300 ctx->blit_list[ctx->blit_count] = src_surface;
1301 ctx->blit_count++;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001302 }
1303
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001304 // Check if we need to perform an early draw-finish.
1305 flags |= (need_temp_dst || need_temp_src) ? FLAGS_TEMP_SRC_DST : 0;
1306 if (need_to_execute_draw(ctx, (eC2DFlags)flags))
1307 {
1308 finish_copybit(dev);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001309 }
1310
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001311 if (need_temp_dst) {
1312 // copy the temp. destination without the alignment to the actual
1313 // destination.
1314 status = copy_image(dst_hnd, dst, CONVERT_TO_ANDROID_FORMAT);
1315 if (status == COPYBIT_FAILURE) {
1316 ALOGE("%s:copy_image failed in temp Dest",__FUNCTION__);
1317 delete_handle(dst_hnd);
1318 delete_handle(src_hnd);
1319 unmap_gpuaddr(ctx, mapped_dst_idx);
1320 unmap_gpuaddr(ctx, mapped_src_idx);
1321 return status;
1322 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001323 // Invalidate the cache.
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001324 IMemAlloc* memalloc = sAlloc->getAllocator(dst_hnd->flags);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001325 memalloc->clean_buffer((void *)(dst_hnd->base), dst_hnd->size,
1326 dst_hnd->offset, dst_hnd->fd);
1327 }
1328 delete_handle(dst_hnd);
1329 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001330
1331 ctx->is_premultiplied_alpha = false;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001332 ctx->fb_width = 0;
1333 ctx->fb_height = 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001334 ctx->config_mask = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001335 return status;
1336}
1337
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001338
Naseer Ahmed29a26812012-06-14 00:56:20 -07001339static int stretch_copybit(
1340 struct copybit_device_t *dev,
1341 struct copybit_image_t const *dst,
1342 struct copybit_image_t const *src,
1343 struct copybit_rect_t const *dst_rect,
1344 struct copybit_rect_t const *src_rect,
1345 struct copybit_region_t const *region)
1346{
1347 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001348 int status = COPYBIT_SUCCESS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001349 bool needsBlending = (ctx->src_global_alpha != 0);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001350 pthread_mutex_lock(&ctx->wait_cleanup_lock);
1351 status = stretch_copybit_internal(dev, dst, src, dst_rect, src_rect,
Naseer Ahmed29a26812012-06-14 00:56:20 -07001352 region, needsBlending);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001353 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
1354 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001355}
1356
1357/** Perform a blit type operation */
1358static int blit_copybit(
1359 struct copybit_device_t *dev,
1360 struct copybit_image_t const *dst,
1361 struct copybit_image_t const *src,
1362 struct copybit_region_t const *region)
1363{
1364 struct copybit_rect_t dr = { 0, 0, dst->w, dst->h };
1365 struct copybit_rect_t sr = { 0, 0, src->w, src->h };
1366 return stretch_copybit_internal(dev, dst, src, &dr, &sr, region, false);
1367}
1368
1369/*****************************************************************************/
1370
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001371static void clean_up(copybit_context_t* ctx)
1372{
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001373 void* ret;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001374 if (!ctx)
1375 return;
1376
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001377 // stop the wait_cleanup_thread
1378 pthread_mutex_lock(&ctx->wait_cleanup_lock);
1379 ctx->stop_thread = true;
1380 // Signal waiting thread
1381 pthread_cond_signal(&ctx->wait_cleanup_cond);
1382 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
1383 // waits for the cleanup thread to exit
1384 pthread_join(ctx->wait_thread_id, &ret);
1385 pthread_mutex_destroy(&ctx->wait_cleanup_lock);
1386 pthread_cond_destroy (&ctx->wait_cleanup_cond);
1387
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001388 for (int i = 0; i < NUM_SURFACE_TYPES; i++) {
1389 if (ctx->dst[i])
1390 LINK_c2dDestroySurface(ctx->dst[i]);
1391 }
1392
1393 for (int i = 0; i < MAX_RGB_SURFACES; i++) {
1394 if (ctx->blit_rgb_object[i].surface_id)
1395 LINK_c2dDestroySurface(ctx->blit_rgb_object[i].surface_id);
1396 }
1397
1398 for (int i = 0; i < MAX_YUV_2_PLANE_SURFACES; i++) {
1399 if (ctx->blit_yuv_2_plane_object[i].surface_id)
1400 LINK_c2dDestroySurface(ctx->blit_yuv_2_plane_object[i].surface_id);
1401 }
1402
1403 for (int i = 0; i < MAX_YUV_3_PLANE_SURFACES; i++) {
1404 if (ctx->blit_yuv_3_plane_object[i].surface_id)
1405 LINK_c2dDestroySurface(ctx->blit_yuv_3_plane_object[i].surface_id);
1406 }
1407
1408 if (ctx->libc2d2) {
1409 ::dlclose(ctx->libc2d2);
1410 ALOGV("dlclose(libc2d2)");
1411 }
1412
1413 free(ctx);
1414}
1415
Naseer Ahmed29a26812012-06-14 00:56:20 -07001416/** Close the copybit device */
1417static int close_copybit(struct hw_device_t *dev)
1418{
1419 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
1420 if (ctx) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001421 free_temp_buffer(ctx->temp_src_buffer);
1422 free_temp_buffer(ctx->temp_dst_buffer);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001423 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001424 clean_up(ctx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001425 return 0;
1426}
1427
1428/** Open a new instance of a copybit device using name */
1429static int open_copybit(const struct hw_module_t* module, const char* name,
1430 struct hw_device_t** device)
1431{
1432 int status = COPYBIT_SUCCESS;
1433 C2D_RGB_SURFACE_DEF surfDefinition = {0};
1434 C2D_YUV_SURFACE_DEF yuvSurfaceDef = {0} ;
1435 struct copybit_context_t *ctx;
1436 char fbName[64];
1437
1438 ctx = (struct copybit_context_t *)malloc(sizeof(struct copybit_context_t));
1439 if(!ctx) {
1440 ALOGE("%s: malloc failed", __FUNCTION__);
1441 return COPYBIT_FAILURE;
1442 }
1443
1444 /* initialize drawstate */
1445 memset(ctx, 0, sizeof(*ctx));
Naseer Ahmed29a26812012-06-14 00:56:20 -07001446 ctx->libc2d2 = ::dlopen("libC2D2.so", RTLD_NOW);
1447 if (!ctx->libc2d2) {
1448 ALOGE("FATAL ERROR: could not dlopen libc2d2.so: %s", dlerror());
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001449 clean_up(ctx);
1450 status = COPYBIT_FAILURE;
1451 *device = NULL;
1452 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001453 }
1454 *(void **)&LINK_c2dCreateSurface = ::dlsym(ctx->libc2d2,
1455 "c2dCreateSurface");
1456 *(void **)&LINK_c2dUpdateSurface = ::dlsym(ctx->libc2d2,
1457 "c2dUpdateSurface");
1458 *(void **)&LINK_c2dReadSurface = ::dlsym(ctx->libc2d2,
1459 "c2dReadSurface");
1460 *(void **)&LINK_c2dDraw = ::dlsym(ctx->libc2d2, "c2dDraw");
1461 *(void **)&LINK_c2dFlush = ::dlsym(ctx->libc2d2, "c2dFlush");
1462 *(void **)&LINK_c2dFinish = ::dlsym(ctx->libc2d2, "c2dFinish");
1463 *(void **)&LINK_c2dWaitTimestamp = ::dlsym(ctx->libc2d2,
1464 "c2dWaitTimestamp");
1465 *(void **)&LINK_c2dDestroySurface = ::dlsym(ctx->libc2d2,
1466 "c2dDestroySurface");
1467 *(void **)&LINK_c2dMapAddr = ::dlsym(ctx->libc2d2,
1468 "c2dMapAddr");
1469 *(void **)&LINK_c2dUnMapAddr = ::dlsym(ctx->libc2d2,
1470 "c2dUnMapAddr");
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001471 *(void **)&LINK_c2dGetDriverCapabilities = ::dlsym(ctx->libc2d2,
1472 "c2dGetDriverCapabilities");
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001473 *(void **)&LINK_c2dCreateFenceFD = ::dlsym(ctx->libc2d2,
1474 "c2dCreateFenceFD");
Naseer Ahmed29a26812012-06-14 00:56:20 -07001475
1476 if (!LINK_c2dCreateSurface || !LINK_c2dUpdateSurface || !LINK_c2dReadSurface
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001477 || !LINK_c2dDraw || !LINK_c2dFlush || !LINK_c2dWaitTimestamp ||
1478 !LINK_c2dFinish || !LINK_c2dDestroySurface ||
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001479 !LINK_c2dGetDriverCapabilities || !LINK_c2dCreateFenceFD) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001480 ALOGE("%s: dlsym ERROR", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001481 clean_up(ctx);
1482 status = COPYBIT_FAILURE;
1483 *device = NULL;
1484 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001485 }
1486
1487 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
1488 ctx->device.common.version = 1;
1489 ctx->device.common.module = (hw_module_t*)(module);
1490 ctx->device.common.close = close_copybit;
1491 ctx->device.set_parameter = set_parameter_copybit;
1492 ctx->device.get = get;
1493 ctx->device.blit = blit_copybit;
1494 ctx->device.stretch = stretch_copybit;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001495 ctx->device.finish = finish_copybit;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001496 ctx->device.flush_get_fence = flush_get_fence_copybit;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001497
1498 /* Create RGB Surface */
1499 surfDefinition.buffer = (void*)0xdddddddd;
1500 surfDefinition.phys = (void*)0xdddddddd;
1501 surfDefinition.stride = 1 * 4;
1502 surfDefinition.width = 1;
1503 surfDefinition.height = 1;
1504 surfDefinition.format = C2D_COLOR_FORMAT_8888_ARGB;
1505 if (LINK_c2dCreateSurface(&(ctx->dst[RGB_SURFACE]), C2D_TARGET | C2D_SOURCE,
1506 (C2D_SURFACE_TYPE)(C2D_SURFACE_RGB_HOST |
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001507 C2D_SURFACE_WITH_PHYS |
1508 C2D_SURFACE_WITH_PHYS_DUMMY ),
1509 &surfDefinition)) {
1510 ALOGE("%s: create ctx->dst_surface[RGB_SURFACE] failed", __FUNCTION__);
1511 ctx->dst[RGB_SURFACE] = 0;
1512 clean_up(ctx);
1513 status = COPYBIT_FAILURE;
1514 *device = NULL;
1515 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001516 }
1517
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001518 unsigned int surface_id = 0;
1519 for (int i = 0; i < MAX_RGB_SURFACES; i++)
1520 {
1521 if (LINK_c2dCreateSurface(&surface_id, C2D_TARGET | C2D_SOURCE,
Naseer Ahmed29a26812012-06-14 00:56:20 -07001522 (C2D_SURFACE_TYPE)(C2D_SURFACE_RGB_HOST |
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001523 C2D_SURFACE_WITH_PHYS |
1524 C2D_SURFACE_WITH_PHYS_DUMMY ),
1525 &surfDefinition)) {
1526 ALOGE("%s: create RGB source surface %d failed", __FUNCTION__, i);
1527 ctx->blit_rgb_object[i].surface_id = 0;
1528 status = COPYBIT_FAILURE;
1529 break;
1530 } else {
1531 ctx->blit_rgb_object[i].surface_id = surface_id;
1532 ALOGW("%s i = %d surface_id=%d", __FUNCTION__, i,
1533 ctx->blit_rgb_object[i].surface_id);
1534 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001535 }
1536
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001537 if (status == COPYBIT_FAILURE) {
1538 clean_up(ctx);
1539 status = COPYBIT_FAILURE;
1540 *device = NULL;
1541 return status;
1542 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001543
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001544 // Create 2 plane YUV surfaces
1545 yuvSurfaceDef.format = C2D_COLOR_FORMAT_420_NV12;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001546 yuvSurfaceDef.width = 4;
1547 yuvSurfaceDef.height = 4;
1548 yuvSurfaceDef.plane0 = (void*)0xaaaaaaaa;
1549 yuvSurfaceDef.phys0 = (void*) 0xaaaaaaaa;
1550 yuvSurfaceDef.stride0 = 4;
1551
1552 yuvSurfaceDef.plane1 = (void*)0xaaaaaaaa;
1553 yuvSurfaceDef.phys1 = (void*) 0xaaaaaaaa;
1554 yuvSurfaceDef.stride1 = 4;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001555 if (LINK_c2dCreateSurface(&(ctx->dst[YUV_SURFACE_2_PLANES]),
1556 C2D_TARGET | C2D_SOURCE,
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001557 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1558 C2D_SURFACE_WITH_PHYS |
1559 C2D_SURFACE_WITH_PHYS_DUMMY),
Naseer Ahmed29a26812012-06-14 00:56:20 -07001560 &yuvSurfaceDef)) {
1561 ALOGE("%s: create ctx->dst[YUV_SURFACE_2_PLANES] failed", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001562 ctx->dst[YUV_SURFACE_2_PLANES] = 0;
1563 clean_up(ctx);
1564 status = COPYBIT_FAILURE;
1565 *device = NULL;
1566 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001567 }
1568
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001569 for (int i=0; i < MAX_YUV_2_PLANE_SURFACES; i++)
1570 {
1571 if (LINK_c2dCreateSurface(&surface_id, C2D_TARGET | C2D_SOURCE,
1572 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1573 C2D_SURFACE_WITH_PHYS |
1574 C2D_SURFACE_WITH_PHYS_DUMMY ),
1575 &yuvSurfaceDef)) {
1576 ALOGE("%s: create YUV source %d failed", __FUNCTION__, i);
1577 ctx->blit_yuv_2_plane_object[i].surface_id = 0;
1578 status = COPYBIT_FAILURE;
1579 break;
1580 } else {
1581 ctx->blit_yuv_2_plane_object[i].surface_id = surface_id;
1582 ALOGW("%s: 2 Plane YUV i=%d surface_id=%d", __FUNCTION__, i,
1583 ctx->blit_yuv_2_plane_object[i].surface_id);
1584 }
1585 }
1586
1587 if (status == COPYBIT_FAILURE) {
1588 clean_up(ctx);
1589 status = COPYBIT_FAILURE;
1590 *device = NULL;
1591 return status;
1592 }
1593
1594 // Create YUV 3 plane surfaces
Naseer Ahmed29a26812012-06-14 00:56:20 -07001595 yuvSurfaceDef.format = C2D_COLOR_FORMAT_420_YV12;
1596 yuvSurfaceDef.plane2 = (void*)0xaaaaaaaa;
1597 yuvSurfaceDef.phys2 = (void*) 0xaaaaaaaa;
1598 yuvSurfaceDef.stride2 = 4;
1599
Naseer Ahmed29a26812012-06-14 00:56:20 -07001600 if (LINK_c2dCreateSurface(&(ctx->dst[YUV_SURFACE_3_PLANES]),
1601 C2D_TARGET | C2D_SOURCE,
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001602 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1603 C2D_SURFACE_WITH_PHYS |
1604 C2D_SURFACE_WITH_PHYS_DUMMY),
Naseer Ahmed29a26812012-06-14 00:56:20 -07001605 &yuvSurfaceDef)) {
1606 ALOGE("%s: create ctx->dst[YUV_SURFACE_3_PLANES] failed", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001607 ctx->dst[YUV_SURFACE_3_PLANES] = 0;
1608 clean_up(ctx);
1609 status = COPYBIT_FAILURE;
1610 *device = NULL;
1611 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001612 }
1613
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001614 for (int i=0; i < MAX_YUV_3_PLANE_SURFACES; i++)
1615 {
1616 if (LINK_c2dCreateSurface(&(surface_id),
1617 C2D_TARGET | C2D_SOURCE,
1618 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1619 C2D_SURFACE_WITH_PHYS |
1620 C2D_SURFACE_WITH_PHYS_DUMMY),
1621 &yuvSurfaceDef)) {
1622 ALOGE("%s: create 3 plane YUV surface %d failed", __FUNCTION__, i);
1623 ctx->blit_yuv_3_plane_object[i].surface_id = 0;
1624 status = COPYBIT_FAILURE;
1625 break;
1626 } else {
1627 ctx->blit_yuv_3_plane_object[i].surface_id = surface_id;
1628 ALOGW("%s: 3 Plane YUV i=%d surface_id=%d", __FUNCTION__, i,
1629 ctx->blit_yuv_3_plane_object[i].surface_id);
1630 }
1631 }
1632
1633 if (status == COPYBIT_FAILURE) {
1634 clean_up(ctx);
1635 status = COPYBIT_FAILURE;
1636 *device = NULL;
1637 return status;
1638 }
1639
1640 if (LINK_c2dGetDriverCapabilities(&(ctx->c2d_driver_info))) {
1641 ALOGE("%s: LINK_c2dGetDriverCapabilities failed", __FUNCTION__);
1642 clean_up(ctx);
1643 status = COPYBIT_FAILURE;
1644 *device = NULL;
1645 return status;
1646 }
1647 // Initialize context variables.
1648 ctx->trg_transform = C2D_TARGET_ROTATE_0;
1649
Naseer Ahmed29a26812012-06-14 00:56:20 -07001650 ctx->temp_src_buffer.fd = -1;
1651 ctx->temp_src_buffer.base = 0;
1652 ctx->temp_src_buffer.size = 0;
1653
1654 ctx->temp_dst_buffer.fd = -1;
1655 ctx->temp_dst_buffer.base = 0;
1656 ctx->temp_dst_buffer.size = 0;
1657
1658 ctx->fb_width = 0;
1659 ctx->fb_height = 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001660
1661 ctx->blit_rgb_count = 0;
1662 ctx->blit_yuv_2_plane_count = 0;
1663 ctx->blit_yuv_3_plane_count = 0;
1664 ctx->blit_count = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001665
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001666 ctx->wait_timestamp = false;
1667 ctx->stop_thread = false;
1668 pthread_mutex_init(&(ctx->wait_cleanup_lock), NULL);
1669 pthread_cond_init(&(ctx->wait_cleanup_cond), NULL);
1670 /* Start the wait thread */
1671 pthread_attr_t attr;
1672 pthread_attr_init(&attr);
1673 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1674
1675 pthread_create(&ctx->wait_thread_id, &attr, &c2d_wait_loop,
1676 (void *)ctx);
1677 pthread_attr_destroy(&attr);
1678
Naseer Ahmed29a26812012-06-14 00:56:20 -07001679 *device = &ctx->device.common;
1680 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001681}