blob: dfbbf76c3d0d252a3721fb803aac6011080909c4 [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 Ahmed183939d2013-03-14 20:44:17 -040093
94C2D_STATUS (*LINK_c2dFillSurface) ( uint32 surface_id, uint32 fill_color,
95 C2D_RECT * fill_rect);
96
Naseer Ahmed29a26812012-06-14 00:56:20 -070097/******************************************************************************/
98
99#if defined(COPYBIT_Z180)
100#define MAX_SCALE_FACTOR (4096)
101#define MAX_DIMENSION (4096)
102#else
103#error "Unsupported HW version"
104#endif
105
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800106// The following defines can be changed as required i.e. as we encounter
107// complex use cases.
108#define MAX_RGB_SURFACES 8 // Max. RGB layers currently supported per draw
109#define MAX_YUV_2_PLANE_SURFACES 4// Max. 2-plane YUV layers currently supported per draw
110#define MAX_YUV_3_PLANE_SURFACES 1// Max. 3-plane YUV layers currently supported per draw
111// +1 for the destination surface. We cannot have multiple destination surfaces.
112#define MAX_SURFACES (MAX_RGB_SURFACES + MAX_YUV_2_PLANE_SURFACES + MAX_YUV_3_PLANE_SURFACES + 1)
113#define NUM_SURFACE_TYPES 3 // RGB_SURFACE + YUV_SURFACE_2_PLANES + YUV_SURFACE_3_PLANES
114#define MAX_BLIT_OBJECT_COUNT 50 // Max. blit objects that can be passed per draw
Naseer Ahmed29a26812012-06-14 00:56:20 -0700115
116enum {
117 RGB_SURFACE,
118 YUV_SURFACE_2_PLANES,
119 YUV_SURFACE_3_PLANES
120};
121
122enum eConversionType {
123 CONVERT_TO_ANDROID_FORMAT,
124 CONVERT_TO_C2D_FORMAT
125};
126
127enum eC2DFlags {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800128 FLAGS_PREMULTIPLIED_ALPHA = 1<<0,
129 FLAGS_YUV_DESTINATION = 1<<1,
130 FLAGS_TEMP_SRC_DST = 1<<2
Naseer Ahmed29a26812012-06-14 00:56:20 -0700131};
132
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700133static gralloc::IAllocController* sAlloc = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700134/******************************************************************************/
135
136/** State information for each device instance */
137struct copybit_context_t {
138 struct copybit_device_t device;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800139 // Templates for the various source surfaces. These templates are created
140 // to avoid the expensive create/destroy C2D Surfaces
141 C2D_OBJECT_STR blit_rgb_object[MAX_RGB_SURFACES];
142 C2D_OBJECT_STR blit_yuv_2_plane_object[MAX_YUV_2_PLANE_SURFACES];
143 C2D_OBJECT_STR blit_yuv_3_plane_object[MAX_YUV_3_PLANE_SURFACES];
144 C2D_OBJECT_STR blit_list[MAX_BLIT_OBJECT_COUNT]; // Z-ordered list of blit objects
145 C2D_DRIVER_INFO c2d_driver_info;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700146 void *libc2d2;
147 alloc_data temp_src_buffer;
148 alloc_data temp_dst_buffer;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800149 unsigned int dst[NUM_SURFACE_TYPES]; // dst surfaces
150 unsigned int mapped_gpu_addr[MAX_SURFACES]; // GPU addresses mapped inside copybit
151 int blit_rgb_count; // Total RGB surfaces being blit
152 int blit_yuv_2_plane_count; // Total 2 plane YUV surfaces being
153 int blit_yuv_3_plane_count; // Total 3 plane YUV surfaces being blit
154 int blit_count; // Total blit objects.
155 unsigned int trg_transform; /* target transform */
Naseer Ahmed29a26812012-06-14 00:56:20 -0700156 int fb_width;
157 int fb_height;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800158 int src_global_alpha;
159 int config_mask;
160 int dst_surface_type;
161 bool is_premultiplied_alpha;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800162 void* time_stamp;
163
164 // used for signaling the wait thread
165 bool wait_timestamp;
166 pthread_t wait_thread_id;
167 bool stop_thread;
168 pthread_mutex_t wait_cleanup_lock;
169 pthread_cond_t wait_cleanup_cond;
170
Naseer Ahmed29a26812012-06-14 00:56:20 -0700171};
172
173struct bufferInfo {
174 int width;
175 int height;
176 int format;
177};
178
179struct yuvPlaneInfo {
180 int yStride; //luma stride
181 int plane1_stride;
182 int plane2_stride;
183 int plane1_offset;
184 int plane2_offset;
185};
186
187/**
188 * Common hardware methods
189 */
190
191static int open_copybit(const struct hw_module_t* module, const char* name,
192 struct hw_device_t** device);
193
194static struct hw_module_methods_t copybit_module_methods = {
195open: open_copybit
196};
197
198/*
199 * The COPYBIT Module
200 */
201struct copybit_module_t HAL_MODULE_INFO_SYM = {
202common: {
203tag: HARDWARE_MODULE_TAG,
204 version_major: 1,
205 version_minor: 0,
206 id: COPYBIT_HARDWARE_MODULE_ID,
207 name: "QCT COPYBIT C2D 2.0 Module",
208 author: "Qualcomm",
209 methods: &copybit_module_methods
210 }
211};
212
213
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800214/* thread function which waits on the timeStamp and cleans up the surfaces */
215static void* c2d_wait_loop(void* ptr) {
216 copybit_context_t* ctx = (copybit_context_t*)(ptr);
217 char thread_name[64] = "copybitWaitThr";
218 prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
219 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
220
221 while(ctx->stop_thread == false) {
222 pthread_mutex_lock(&ctx->wait_cleanup_lock);
223 while(ctx->wait_timestamp == false && !ctx->stop_thread) {
224 pthread_cond_wait(&(ctx->wait_cleanup_cond),
225 &(ctx->wait_cleanup_lock));
226 }
227 if(ctx->wait_timestamp) {
228 if(LINK_c2dWaitTimestamp(ctx->time_stamp)) {
229 ALOGE("%s: LINK_c2dWaitTimeStamp ERROR!!", __FUNCTION__);
230 }
231 ctx->wait_timestamp = false;
232 // Unmap any mapped addresses.
233 for (int i = 0; i < MAX_SURFACES; i++) {
234 if (ctx->mapped_gpu_addr[i]) {
235 LINK_c2dUnMapAddr( (void*)ctx->mapped_gpu_addr[i]);
236 ctx->mapped_gpu_addr[i] = 0;
237 }
238 }
239 // Reset the counts after the draw.
240 ctx->blit_rgb_count = 0;
241 ctx->blit_yuv_2_plane_count = 0;
242 ctx->blit_yuv_3_plane_count = 0;
243 ctx->blit_count = 0;
244 }
245 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
246 if(ctx->stop_thread)
247 break;
248 }
249 pthread_exit(NULL);
250 return NULL;
251}
252
253
Naseer Ahmed29a26812012-06-14 00:56:20 -0700254/* convert COPYBIT_FORMAT to C2D format */
255static int get_format(int format) {
256 switch (format) {
257 case HAL_PIXEL_FORMAT_RGB_565: return C2D_COLOR_FORMAT_565_RGB;
258 case HAL_PIXEL_FORMAT_RGBX_8888: return C2D_COLOR_FORMAT_8888_ARGB |
259 C2D_FORMAT_SWAP_RB |
260 C2D_FORMAT_DISABLE_ALPHA;
261 case HAL_PIXEL_FORMAT_RGBA_8888: return C2D_COLOR_FORMAT_8888_ARGB |
262 C2D_FORMAT_SWAP_RB;
263 case HAL_PIXEL_FORMAT_BGRA_8888: return C2D_COLOR_FORMAT_8888_ARGB;
264 case HAL_PIXEL_FORMAT_RGBA_5551: return C2D_COLOR_FORMAT_5551_RGBA;
265 case HAL_PIXEL_FORMAT_RGBA_4444: return C2D_COLOR_FORMAT_4444_RGBA;
266 case HAL_PIXEL_FORMAT_YCbCr_420_SP: return C2D_COLOR_FORMAT_420_NV12;
267 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:return C2D_COLOR_FORMAT_420_NV12;
268 case HAL_PIXEL_FORMAT_YCrCb_420_SP: return C2D_COLOR_FORMAT_420_NV21;
269 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: return C2D_COLOR_FORMAT_420_NV12 |
270 C2D_FORMAT_MACROTILED;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800271 default: ALOGE("%s: invalid format (0x%x",
272 __FUNCTION__, format);
273 return -EINVAL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700274 }
275 return -EINVAL;
276}
277
278/* Get the C2D formats needed for conversion to YUV */
279static int get_c2d_format_for_yuv_destination(int halFormat) {
280 switch (halFormat) {
281 // We do not swap the RB when the target is YUV
282 case HAL_PIXEL_FORMAT_RGBX_8888: return C2D_COLOR_FORMAT_8888_ARGB |
283 C2D_FORMAT_DISABLE_ALPHA;
284 case HAL_PIXEL_FORMAT_RGBA_8888: return C2D_COLOR_FORMAT_8888_ARGB;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800285 // The U and V need to be interchanged when the target is YUV
Naseer Ahmed29a26812012-06-14 00:56:20 -0700286 case HAL_PIXEL_FORMAT_YCbCr_420_SP: return C2D_COLOR_FORMAT_420_NV21;
287 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:return C2D_COLOR_FORMAT_420_NV21;
288 case HAL_PIXEL_FORMAT_YCrCb_420_SP: return C2D_COLOR_FORMAT_420_NV12;
289 default: return get_format(halFormat);
290 }
291 return -EINVAL;
292}
293
294/* ------------------------------------------------------------------- *//*!
295 * \internal
296 * \brief Get the bpp for a particular color format
297 * \param color format
298 * \return bits per pixel
299 *//* ------------------------------------------------------------------- */
300int c2diGetBpp(int32 colorformat)
301{
302
303 int c2dBpp = 0;
304
305 switch(colorformat&0xFF)
306 {
307 case C2D_COLOR_FORMAT_4444_RGBA:
308 case C2D_COLOR_FORMAT_4444_ARGB:
309 case C2D_COLOR_FORMAT_1555_ARGB:
310 case C2D_COLOR_FORMAT_565_RGB:
311 case C2D_COLOR_FORMAT_5551_RGBA:
312 c2dBpp = 16;
313 break;
314 case C2D_COLOR_FORMAT_8888_RGBA:
315 case C2D_COLOR_FORMAT_8888_ARGB:
316 c2dBpp = 32;
317 break;
318 case C2D_COLOR_FORMAT_8_L:
319 case C2D_COLOR_FORMAT_8_A:
320 c2dBpp = 8;
321 break;
322 case C2D_COLOR_FORMAT_4_A:
323 c2dBpp = 4;
324 break;
325 case C2D_COLOR_FORMAT_1:
326 c2dBpp = 1;
327 break;
328 default:
329 ALOGE("%s ERROR", __func__);
330 break;
331 }
332 return c2dBpp;
333}
334
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800335static uint32 c2d_get_gpuaddr(copybit_context_t* ctx, struct private_handle_t *handle,
336 int &mapped_idx)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700337{
338 uint32 memtype, *gpuaddr;
339 C2D_STATUS rc;
340
341 if(!handle)
342 return 0;
343
344 if (handle->flags & (private_handle_t::PRIV_FLAGS_USES_PMEM |
345 private_handle_t::PRIV_FLAGS_USES_PMEM_ADSP))
346 memtype = KGSL_USER_MEM_TYPE_PMEM;
347 else if (handle->flags & private_handle_t::PRIV_FLAGS_USES_ASHMEM)
348 memtype = KGSL_USER_MEM_TYPE_ASHMEM;
349 else if (handle->flags & private_handle_t::PRIV_FLAGS_USES_ION)
350 memtype = KGSL_USER_MEM_TYPE_ION;
351 else {
352 ALOGE("Invalid handle flags: 0x%x", handle->flags);
353 return 0;
354 }
355
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800356 rc = LINK_c2dMapAddr(handle->fd, (void*)handle->base, handle->size,
357 handle->offset, memtype, (void**)&gpuaddr);
358
Naseer Ahmed29a26812012-06-14 00:56:20 -0700359 if (rc == C2D_STATUS_OK) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800360 // We have mapped the GPU address inside copybit. We need to unmap this
361 // address after the blit. Store this address
362 for (int i = 0; i < MAX_SURFACES; i++) {
363 if (ctx->mapped_gpu_addr[i] == 0) {
364 ctx->mapped_gpu_addr[i] = (uint32) gpuaddr;
365 mapped_idx = i;
366 break;
367 }
368 }
369
Naseer Ahmed29a26812012-06-14 00:56:20 -0700370 return (uint32) gpuaddr;
371 }
372 return 0;
373}
374
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800375static void unmap_gpuaddr(copybit_context_t* ctx, int mapped_idx)
376{
377 if (!ctx || (mapped_idx == -1))
378 return;
379
380 if (ctx->mapped_gpu_addr[mapped_idx]) {
381 LINK_c2dUnMapAddr( (void*)ctx->mapped_gpu_addr[mapped_idx]);
382 ctx->mapped_gpu_addr[mapped_idx] = 0;
383 }
384}
385
Naseer Ahmed29a26812012-06-14 00:56:20 -0700386static int is_supported_rgb_format(int format)
387{
388 switch(format) {
389 case HAL_PIXEL_FORMAT_RGBA_8888:
390 case HAL_PIXEL_FORMAT_RGBX_8888:
391 case HAL_PIXEL_FORMAT_RGB_565:
392 case HAL_PIXEL_FORMAT_BGRA_8888:
393 case HAL_PIXEL_FORMAT_RGBA_5551:
394 case HAL_PIXEL_FORMAT_RGBA_4444: {
395 return COPYBIT_SUCCESS;
396 }
397 default:
398 return COPYBIT_FAILURE;
399 }
400}
401
402static int get_num_planes(int format)
403{
404 switch(format) {
405 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
406 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
407 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
408 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: {
409 return 2;
410 }
411 case HAL_PIXEL_FORMAT_YV12: {
412 return 3;
413 }
414 default:
415 return COPYBIT_FAILURE;
416 }
417}
418
419static int is_supported_yuv_format(int format)
420{
421 switch(format) {
422 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
423 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
424 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
425 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: {
426 return COPYBIT_SUCCESS;
427 }
428 default:
429 return COPYBIT_FAILURE;
430 }
431}
432
433static int is_valid_destination_format(int format)
434{
435 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) {
436 // C2D does not support NV12Tile as a destination format.
437 return COPYBIT_FAILURE;
438 }
439 return COPYBIT_SUCCESS;
440}
441
442static int calculate_yuv_offset_and_stride(const bufferInfo& info,
443 yuvPlaneInfo& yuvInfo)
444{
445 int width = info.width;
446 int height = info.height;
447 int format = info.format;
448
449 int aligned_height = 0;
450 int aligned_width = 0, size = 0;
451
452 switch (format) {
453 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: {
454 /* NV12 Tile buffers have their luma height aligned to 32bytes and width
455 * aligned to 128 bytes. The chroma offset starts at an 8K boundary
456 */
457 aligned_height = ALIGN(height, 32);
458 aligned_width = ALIGN(width, 128);
459 size = aligned_width * aligned_height;
460 yuvInfo.plane1_offset = ALIGN(size,8192);
461 yuvInfo.yStride = aligned_width;
462 yuvInfo.plane1_stride = aligned_width;
463 break;
464 }
465 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
466 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
467 case HAL_PIXEL_FORMAT_YCrCb_420_SP: {
468 aligned_width = ALIGN(width, 32);
469 yuvInfo.yStride = aligned_width;
470 yuvInfo.plane1_stride = aligned_width;
471 if (HAL_PIXEL_FORMAT_NV12_ENCODEABLE == format) {
472 // The encoder requires a 2K aligned chroma offset
473 yuvInfo.plane1_offset = ALIGN(aligned_width * height, 2048);
474 } else
475 yuvInfo.plane1_offset = aligned_width * height;
476
477 break;
478 }
479 default: {
480 return COPYBIT_FAILURE;
481 }
482 }
483 return COPYBIT_SUCCESS;
484}
485
486/** create C2D surface from copybit image */
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800487static int set_image(copybit_context_t* ctx, uint32 surfaceId,
488 const struct copybit_image_t *rhs,
489 const eC2DFlags flags, int &mapped_idx)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700490{
491 struct private_handle_t* handle = (struct private_handle_t*)rhs->handle;
492 C2D_SURFACE_TYPE surfaceType;
493 int status = COPYBIT_SUCCESS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800494 uint32 gpuaddr = 0;
495 int c2d_format;
496 mapped_idx = -1;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700497
498 if (flags & FLAGS_YUV_DESTINATION) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800499 c2d_format = get_c2d_format_for_yuv_destination(rhs->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700500 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800501 c2d_format = get_format(rhs->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700502 }
503
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800504 if(c2d_format == -EINVAL) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700505 ALOGE("%s: invalid format", __FUNCTION__);
506 return -EINVAL;
507 }
508
509 if(handle == NULL) {
510 ALOGE("%s: invalid handle", __func__);
511 return -EINVAL;
512 }
513
514 if (handle->gpuaddr == 0) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800515 gpuaddr = c2d_get_gpuaddr(ctx, handle, mapped_idx);
516 if(!gpuaddr) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700517 ALOGE("%s: c2d_get_gpuaddr failed", __FUNCTION__);
518 return COPYBIT_FAILURE;
519 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800520 } else {
521 gpuaddr = handle->gpuaddr;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700522 }
523
524 /* create C2D surface */
525 if(is_supported_rgb_format(rhs->format) == COPYBIT_SUCCESS) {
526 /* RGB */
527 C2D_RGB_SURFACE_DEF surfaceDef;
528
529 surfaceType = (C2D_SURFACE_TYPE) (C2D_SURFACE_RGB_HOST | C2D_SURFACE_WITH_PHYS);
530
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800531 surfaceDef.phys = (void*) gpuaddr;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700532 surfaceDef.buffer = (void*) (handle->base);
533
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800534 surfaceDef.format = c2d_format |
Naseer Ahmed29a26812012-06-14 00:56:20 -0700535 ((flags & FLAGS_PREMULTIPLIED_ALPHA) ? C2D_FORMAT_PREMULTIPLIED : 0);
536 surfaceDef.width = rhs->w;
537 surfaceDef.height = rhs->h;
538 int aligned_width = ALIGN(surfaceDef.width,32);
539 surfaceDef.stride = (aligned_width * c2diGetBpp(surfaceDef.format))>>3;
540
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800541 if(LINK_c2dUpdateSurface( surfaceId,C2D_TARGET | C2D_SOURCE, surfaceType,
542 &surfaceDef)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700543 ALOGE("%s: RGB Surface c2dUpdateSurface ERROR", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800544 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700545 status = COPYBIT_FAILURE;
546 }
547 } else if (is_supported_yuv_format(rhs->format) == COPYBIT_SUCCESS) {
548 C2D_YUV_SURFACE_DEF surfaceDef;
549 memset(&surfaceDef, 0, sizeof(surfaceDef));
550 surfaceType = (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST | C2D_SURFACE_WITH_PHYS);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800551 surfaceDef.format = c2d_format;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700552
553 bufferInfo info;
554 info.width = rhs->w;
555 info.height = rhs->h;
556 info.format = rhs->format;
557
Naseer Ahmedb16edac2012-07-15 23:56:21 -0700558 yuvPlaneInfo yuvInfo = {0};
Naseer Ahmed29a26812012-06-14 00:56:20 -0700559 status = calculate_yuv_offset_and_stride(info, yuvInfo);
560 if(status != COPYBIT_SUCCESS) {
561 ALOGE("%s: calculate_yuv_offset_and_stride error", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800562 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700563 }
564
565 surfaceDef.width = rhs->w;
566 surfaceDef.height = rhs->h;
567 surfaceDef.plane0 = (void*) (handle->base);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800568 surfaceDef.phys0 = (void*) (gpuaddr);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700569 surfaceDef.stride0 = yuvInfo.yStride;
570
571 surfaceDef.plane1 = (void*) (handle->base + yuvInfo.plane1_offset);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800572 surfaceDef.phys1 = (void*) (gpuaddr + yuvInfo.plane1_offset);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700573 surfaceDef.stride1 = yuvInfo.plane1_stride;
574 if (3 == get_num_planes(rhs->format)) {
575 surfaceDef.plane2 = (void*) (handle->base + yuvInfo.plane2_offset);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800576 surfaceDef.phys2 = (void*) (gpuaddr + yuvInfo.plane2_offset);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700577 surfaceDef.stride2 = yuvInfo.plane2_stride;
578 }
579
580 if(LINK_c2dUpdateSurface( surfaceId,C2D_TARGET | C2D_SOURCE, surfaceType,
581 &surfaceDef)) {
582 ALOGE("%s: YUV Surface c2dUpdateSurface ERROR", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800583 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700584 status = COPYBIT_FAILURE;
585 }
586 } else {
587 ALOGE("%s: invalid format 0x%x", __FUNCTION__, rhs->format);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800588 unmap_gpuaddr(ctx, mapped_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700589 status = COPYBIT_FAILURE;
590 }
591
592 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700593}
594
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800595/** copy the bits */
596static int msm_copybit(struct copybit_context_t *ctx, unsigned int target)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700597{
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800598 if (ctx->blit_count == 0) {
599 return COPYBIT_SUCCESS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700600 }
601
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800602 for (int i = 0; i < ctx->blit_count; i++)
603 {
604 ctx->blit_list[i].next = &(ctx->blit_list[i+1]);
605 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800606 ctx->blit_list[ctx->blit_count-1].next = NULL;
Arun Kumar K.Rb2fc9562013-02-25 16:28:51 -0800607 uint32_t target_transform = ctx->trg_transform;
608 if (ctx->c2d_driver_info.capabilities_mask &
609 C2D_DRIVER_SUPPORTS_OVERRIDE_TARGET_ROTATE_OP) {
610 // For A3xx - set 0x0 as the transform is set in the config_mask
611 target_transform = 0x0;
612 }
613 if(LINK_c2dDraw(target, target_transform, 0x0, 0, 0, ctx->blit_list,
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800614 ctx->blit_count)) {
615 ALOGE("%s: LINK_c2dDraw ERROR", __FUNCTION__);
616 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700617 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700618 return COPYBIT_SUCCESS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700619}
620
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800621
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800622
623static int flush_get_fence_copybit (struct copybit_device_t *dev, int* fd)
624{
625 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
626 int status = COPYBIT_FAILURE;
627 if (!ctx)
628 return COPYBIT_FAILURE;
629 pthread_mutex_lock(&ctx->wait_cleanup_lock);
630 status = msm_copybit(ctx, ctx->dst[ctx->dst_surface_type]);
631
632 if(LINK_c2dFlush(ctx->dst[ctx->dst_surface_type], &ctx->time_stamp)) {
633 ALOGE("%s: LINK_c2dFlush ERROR", __FUNCTION__);
634 // unlock the mutex and return failure
635 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
636 return COPYBIT_FAILURE;
637 }
638 if(LINK_c2dCreateFenceFD(ctx->dst[ctx->dst_surface_type], ctx->time_stamp,
639 fd)) {
640 ALOGE("%s: LINK_c2dCreateFenceFD ERROR", __FUNCTION__);
641 status = COPYBIT_FAILURE;
642 }
643 if(status == COPYBIT_SUCCESS) {
644 //signal the wait_thread
645 ctx->wait_timestamp = true;
646 pthread_cond_signal(&ctx->wait_cleanup_cond);
647 }
648 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
649 return status;
650}
651
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800652static int finish_copybit(struct copybit_device_t *dev)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700653{
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800654 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
655 if (!ctx)
656 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700657
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800658 int status = msm_copybit(ctx, ctx->dst[ctx->dst_surface_type]);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700659
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800660 if(LINK_c2dFinish(ctx->dst[ctx->dst_surface_type])) {
661 ALOGE("%s: LINK_c2dFinish ERROR", __FUNCTION__);
662 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700663 }
664
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800665 // Unmap any mapped addresses.
666 for (int i = 0; i < MAX_SURFACES; i++) {
667 if (ctx->mapped_gpu_addr[i]) {
668 LINK_c2dUnMapAddr( (void*)ctx->mapped_gpu_addr[i]);
669 ctx->mapped_gpu_addr[i] = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700670 }
671 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700672
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800673 // Reset the counts after the draw.
674 ctx->blit_rgb_count = 0;
675 ctx->blit_yuv_2_plane_count = 0;
676 ctx->blit_yuv_3_plane_count = 0;
677 ctx->blit_count = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700678 return status;
679}
680
Naseer Ahmed183939d2013-03-14 20:44:17 -0400681static int clear_copybit(struct copybit_device_t *dev,
682 struct copybit_image_t const *buf,
683 struct copybit_rect_t *rect)
684{
685 int ret = 0;
686 int flags = FLAGS_PREMULTIPLIED_ALPHA;
687 int mapped_dst_idx = -1;
688 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
689 C2D_RECT c2drect = {rect->l, rect->t, rect->r - rect->l, rect->b - rect->t};
690 ret = set_image(ctx, ctx->dst[RGB_SURFACE], buf,
691 (eC2DFlags)flags, mapped_dst_idx);
692 if(ret) {
693 ALOGE("%s: set_image error", __FUNCTION__);
694 unmap_gpuaddr(ctx, mapped_dst_idx);
695 return COPYBIT_FAILURE;
696 }
697
698 ret = LINK_c2dFillSurface(ctx->dst[RGB_SURFACE], 0x0, &c2drect);
699 return ret;
700}
701
702
Naseer Ahmed29a26812012-06-14 00:56:20 -0700703/** setup rectangles */
704static void set_rects(struct copybit_context_t *ctx,
705 C2D_OBJECT *c2dObject,
706 const struct copybit_rect_t *dst,
707 const struct copybit_rect_t *src,
708 const struct copybit_rect_t *scissor)
709{
710 // Set the target rect.
711 if((ctx->trg_transform & C2D_TARGET_ROTATE_90) &&
712 (ctx->trg_transform & C2D_TARGET_ROTATE_180)) {
713 /* target rotation is 270 */
714 c2dObject->target_rect.x = (dst->t)<<16;
715 c2dObject->target_rect.y = ctx->fb_width?(ALIGN(ctx->fb_width,32)- dst->r):dst->r;
716 c2dObject->target_rect.y = c2dObject->target_rect.y<<16;
717 c2dObject->target_rect.height = ((dst->r) - (dst->l))<<16;
718 c2dObject->target_rect.width = ((dst->b) - (dst->t))<<16;
719 } else if(ctx->trg_transform & C2D_TARGET_ROTATE_90) {
720 c2dObject->target_rect.x = ctx->fb_height?(ctx->fb_height - dst->b):dst->b;
721 c2dObject->target_rect.x = c2dObject->target_rect.x<<16;
722 c2dObject->target_rect.y = (dst->l)<<16;
723 c2dObject->target_rect.height = ((dst->r) - (dst->l))<<16;
724 c2dObject->target_rect.width = ((dst->b) - (dst->t))<<16;
725 } else if(ctx->trg_transform & C2D_TARGET_ROTATE_180) {
726 c2dObject->target_rect.y = ctx->fb_height?(ctx->fb_height - dst->b):dst->b;
727 c2dObject->target_rect.y = c2dObject->target_rect.y<<16;
728 c2dObject->target_rect.x = ctx->fb_width?(ALIGN(ctx->fb_width,32) - dst->r):dst->r;
729 c2dObject->target_rect.x = c2dObject->target_rect.x<<16;
730 c2dObject->target_rect.height = ((dst->b) - (dst->t))<<16;
731 c2dObject->target_rect.width = ((dst->r) - (dst->l))<<16;
732 } else {
733 c2dObject->target_rect.x = (dst->l)<<16;
734 c2dObject->target_rect.y = (dst->t)<<16;
735 c2dObject->target_rect.height = ((dst->b) - (dst->t))<<16;
736 c2dObject->target_rect.width = ((dst->r) - (dst->l))<<16;
737 }
738 c2dObject->config_mask |= C2D_TARGET_RECT_BIT;
739
740 // Set the source rect
741 c2dObject->source_rect.x = (src->l)<<16;
742 c2dObject->source_rect.y = (src->t)<<16;
743 c2dObject->source_rect.height = ((src->b) - (src->t))<<16;
744 c2dObject->source_rect.width = ((src->r) - (src->l))<<16;
745 c2dObject->config_mask |= C2D_SOURCE_RECT_BIT;
746
747 // Set the scissor rect
748 c2dObject->scissor_rect.x = scissor->l;
749 c2dObject->scissor_rect.y = scissor->t;
750 c2dObject->scissor_rect.height = (scissor->b) - (scissor->t);
751 c2dObject->scissor_rect.width = (scissor->r) - (scissor->l);
752 c2dObject->config_mask |= C2D_SCISSOR_RECT_BIT;
753}
754
Naseer Ahmed29a26812012-06-14 00:56:20 -0700755/*****************************************************************************/
756
757/** Set a parameter to value */
758static int set_parameter_copybit(
759 struct copybit_device_t *dev,
760 int name,
761 int value)
762{
763 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800764 int status = COPYBIT_SUCCESS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700765 if (!ctx) {
766 ALOGE("%s: null context", __FUNCTION__);
767 return -EINVAL;
768 }
769
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800770 pthread_mutex_lock(&ctx->wait_cleanup_lock);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700771 switch(name) {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700772 case COPYBIT_PLANE_ALPHA:
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800773 {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700774 if (value < 0) value = 0;
775 if (value >= 256) value = 255;
776
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800777 ctx->src_global_alpha = value;
778 if (value < 255)
779 ctx->config_mask |= C2D_GLOBAL_ALPHA_BIT;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700780 else
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800781 ctx->config_mask &= ~C2D_GLOBAL_ALPHA_BIT;
782 }
783 break;
784 case COPYBIT_BLEND_MODE:
785 {
786 if (value == COPYBIT_BLENDING_NONE) {
787 ctx->config_mask |= C2D_ALPHA_BLEND_NONE;
788 ctx->is_premultiplied_alpha = true;
789 } else if (value == COPYBIT_BLENDING_PREMULT) {
790 ctx->is_premultiplied_alpha = true;
791 } else {
792 ctx->config_mask &= ~C2D_ALPHA_BLEND_NONE;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700793 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800794 }
795 break;
796 case COPYBIT_TRANSFORM:
797 {
798 unsigned int transform = 0;
799 uint32 config_mask = 0;
800 config_mask |= C2D_OVERRIDE_GLOBAL_TARGET_ROTATE_CONFIG;
801 if((value & 0x7) == COPYBIT_TRANSFORM_ROT_180) {
802 transform = C2D_TARGET_ROTATE_180;
803 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_180;
804 } else if((value & 0x7) == COPYBIT_TRANSFORM_ROT_270) {
805 transform = C2D_TARGET_ROTATE_90;
806 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_90;
807 } else if(value == COPYBIT_TRANSFORM_ROT_90) {
808 transform = C2D_TARGET_ROTATE_270;
809 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_270;
810 } else {
811 config_mask |= C2D_OVERRIDE_TARGET_ROTATE_0;
812 if(value & COPYBIT_TRANSFORM_FLIP_H) {
813 config_mask |= C2D_MIRROR_H_BIT;
814 } else if(value & COPYBIT_TRANSFORM_FLIP_V) {
815 config_mask |= C2D_MIRROR_V_BIT;
816 }
817 }
818
Arun Kumar K.Rb2fc9562013-02-25 16:28:51 -0800819 if (ctx->c2d_driver_info.capabilities_mask &
820 C2D_DRIVER_SUPPORTS_OVERRIDE_TARGET_ROTATE_OP) {
821 ctx->config_mask |= config_mask;
822 } else {
823 // The transform for this surface does not match the current
824 // target transform. Draw all previous surfaces. This will be
825 // changed once we have a new mechanism to send different
826 // target rotations to c2d.
827 finish_copybit(dev);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800828 }
829 ctx->trg_transform = transform;
830 }
831 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700832 case COPYBIT_FRAMEBUFFER_WIDTH:
833 ctx->fb_width = value;
834 break;
835 case COPYBIT_FRAMEBUFFER_HEIGHT:
836 ctx->fb_height = value;
837 break;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800838 case COPYBIT_ROTATION_DEG:
839 case COPYBIT_DITHER:
840 case COPYBIT_BLUR:
Naseer Ahmed45a99602012-07-31 19:15:24 -0700841 case COPYBIT_BLIT_TO_FRAMEBUFFER:
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800842 // Do nothing
Naseer Ahmed45a99602012-07-31 19:15:24 -0700843 break;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700844 default:
845 ALOGE("%s: default case param=0x%x", __FUNCTION__, name);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800846 status = -EINVAL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700847 break;
848 }
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -0800849 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
850 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700851}
852
853/** Get a static info value */
854static int get(struct copybit_device_t *dev, int name)
855{
856 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
857 int value;
858
859 if (!ctx) {
860 ALOGE("%s: null context error", __FUNCTION__);
861 return -EINVAL;
862 }
863
864 switch(name) {
865 case COPYBIT_MINIFICATION_LIMIT:
866 value = MAX_SCALE_FACTOR;
867 break;
868 case COPYBIT_MAGNIFICATION_LIMIT:
869 value = MAX_SCALE_FACTOR;
870 break;
871 case COPYBIT_SCALING_FRAC_BITS:
872 value = 32;
873 break;
874 case COPYBIT_ROTATION_STEP_DEG:
875 value = 1;
876 break;
877 default:
878 ALOGE("%s: default case param=0x%x", __FUNCTION__, name);
879 value = -EINVAL;
880 }
881 return value;
882}
883
884static int is_alpha(int cformat)
885{
886 int alpha = 0;
887 switch (cformat & 0xFF) {
888 case C2D_COLOR_FORMAT_8888_ARGB:
889 case C2D_COLOR_FORMAT_8888_RGBA:
890 case C2D_COLOR_FORMAT_5551_RGBA:
891 case C2D_COLOR_FORMAT_4444_ARGB:
892 alpha = 1;
893 break;
894 default:
895 alpha = 0;
896 break;
897 }
898
899 if(alpha && (cformat&C2D_FORMAT_DISABLE_ALPHA))
900 alpha = 0;
901
902 return alpha;
903}
904
905/* Function to check if we need a temporary buffer for the blit.
906 * This would happen if the requested destination stride and the
907 * C2D stride do not match. We ignore RGB buffers, since their
908 * stride is always aligned to 32.
909 */
910static bool need_temp_buffer(struct copybit_image_t const *img)
911{
912 if (COPYBIT_SUCCESS == is_supported_rgb_format(img->format))
913 return false;
914
915 struct private_handle_t* handle = (struct private_handle_t*)img->handle;
916
917 // The width parameter in the handle contains the aligned_w. We check if we
918 // need to convert based on this param. YUV formats have bpp=1, so checking
919 // if the requested stride is aligned should suffice.
920 if (0 == (handle->width)%32) {
921 return false;
922 }
923
924 return true;
925}
926
927/* Function to extract the information from the copybit image and set the corresponding
928 * values in the bufferInfo struct.
929 */
930static void populate_buffer_info(struct copybit_image_t const *img, bufferInfo& info)
931{
932 info.width = img->w;
933 info.height = img->h;
934 info.format = img->format;
935}
936
937/* Function to get the required size for a particular format, inorder for C2D to perform
938 * the blit operation.
939 */
940static size_t get_size(const bufferInfo& info)
941{
942 size_t size = 0;
943 int w = info.width;
944 int h = info.height;
945 int aligned_w = ALIGN(w, 32);
946 switch(info.format) {
947 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
948 {
949 // Chroma for this format is aligned to 2K.
950 size = ALIGN((aligned_w*h), 2048) +
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800951 ALIGN(aligned_w/2, 32) * (h/2) *2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700952 size = ALIGN(size, 4096);
953 } break;
954 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
955 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
956 {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -0800957 size = aligned_w * h +
958 ALIGN(aligned_w/2, 32) * (h/2) * 2;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700959 size = ALIGN(size, 4096);
960 } break;
961 default: break;
962 }
963 return size;
964}
965
966/* Function to allocate memory for the temporary buffer. This memory is
967 * allocated from Ashmem. It is the caller's responsibility to free this
968 * memory.
969 */
970static int get_temp_buffer(const bufferInfo& info, alloc_data& data)
971{
972 ALOGD("%s E", __FUNCTION__);
973 // Alloc memory from system heap
974 data.base = 0;
975 data.fd = -1;
976 data.offset = 0;
977 data.size = get_size(info);
978 data.align = getpagesize();
979 data.uncached = true;
980 int allocFlags = GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP;
981
982 if (sAlloc == 0) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700983 sAlloc = gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700984 }
985
986 if (sAlloc == 0) {
987 ALOGE("%s: sAlloc is still NULL", __FUNCTION__);
988 return COPYBIT_FAILURE;
989 }
990
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700991 int err = sAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700992 if (0 != err) {
993 ALOGE("%s: allocate failed", __FUNCTION__);
994 return COPYBIT_FAILURE;
995 }
996
997 ALOGD("%s X", __FUNCTION__);
998 return err;
999}
1000
1001/* Function to free the temporary allocated memory.*/
1002static void free_temp_buffer(alloc_data &data)
1003{
1004 if (-1 != data.fd) {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001005 IMemAlloc* memalloc = sAlloc->getAllocator(data.allocType);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001006 memalloc->free_buffer(data.base, data.size, 0, data.fd);
1007 }
1008}
1009
1010/* Function to perform the software color conversion. Convert the
1011 * C2D compatible format to the Android compatible format
1012 */
1013static int copy_image(private_handle_t *src_handle,
1014 struct copybit_image_t const *rhs,
1015 eConversionType conversionType)
1016{
1017 if (src_handle->fd == -1) {
1018 ALOGE("%s: src_handle fd is invalid", __FUNCTION__);
1019 return COPYBIT_FAILURE;
1020 }
1021
1022 // Copy the info.
1023 int ret = COPYBIT_SUCCESS;
1024 switch(rhs->format) {
1025 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
1026 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
1027 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1028 {
1029 if (CONVERT_TO_ANDROID_FORMAT == conversionType) {
1030 return convert_yuv_c2d_to_yuv_android(src_handle, rhs);
1031 } else {
1032 return convert_yuv_android_to_yuv_c2d(src_handle, rhs);
1033 }
1034
1035 } break;
1036 default: {
1037 ALOGE("%s: invalid format 0x%x", __FUNCTION__, rhs->format);
1038 ret = COPYBIT_FAILURE;
1039 } break;
1040 }
1041 return ret;
1042}
1043
1044static void delete_handle(private_handle_t *handle)
1045{
1046 if (handle) {
1047 delete handle;
1048 handle = 0;
1049 }
1050}
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001051
1052static bool need_to_execute_draw(struct copybit_context_t* ctx,
1053 eC2DFlags flags)
1054{
1055 if (flags & FLAGS_TEMP_SRC_DST) {
1056 return true;
1057 }
1058 if (flags & FLAGS_YUV_DESTINATION) {
1059 return true;
1060 }
1061 return false;
1062}
1063
Naseer Ahmed29a26812012-06-14 00:56:20 -07001064/** do a stretch blit type operation */
1065static int stretch_copybit_internal(
1066 struct copybit_device_t *dev,
1067 struct copybit_image_t const *dst,
1068 struct copybit_image_t const *src,
1069 struct copybit_rect_t const *dst_rect,
1070 struct copybit_rect_t const *src_rect,
1071 struct copybit_region_t const *region,
1072 bool enableBlend)
1073{
1074 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
1075 int status = COPYBIT_SUCCESS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001076 int flags = 0;
1077 int src_surface_type;
1078 int mapped_src_idx = -1, mapped_dst_idx = -1;
1079 C2D_OBJECT_STR src_surface;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001080
1081 if (!ctx) {
1082 ALOGE("%s: null context error", __FUNCTION__);
1083 return -EINVAL;
1084 }
1085
1086 if (src->w > MAX_DIMENSION || src->h > MAX_DIMENSION) {
1087 ALOGE("%s: src dimension error", __FUNCTION__);
1088 return -EINVAL;
1089 }
1090
1091 if (dst->w > MAX_DIMENSION || dst->h > MAX_DIMENSION) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001092 ALOGE("%s : dst dimension error dst w %d h %d", __FUNCTION__, dst->w,
1093 dst->h);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001094 return -EINVAL;
1095 }
1096
Naseer Ahmed29a26812012-06-14 00:56:20 -07001097 if (is_valid_destination_format(dst->format) == COPYBIT_FAILURE) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001098 ALOGE("%s: Invalid destination format format = 0x%x", __FUNCTION__,
1099 dst->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001100 return COPYBIT_FAILURE;
1101 }
1102
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001103 int dst_surface_type;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001104 if (is_supported_rgb_format(dst->format) == COPYBIT_SUCCESS) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001105 dst_surface_type = RGB_SURFACE;
1106 flags |= FLAGS_PREMULTIPLIED_ALPHA;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001107 } else if (is_supported_yuv_format(dst->format) == COPYBIT_SUCCESS) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001108 int num_planes = get_num_planes(dst->format);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001109 flags |= FLAGS_YUV_DESTINATION;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001110 if (num_planes == 2) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001111 dst_surface_type = YUV_SURFACE_2_PLANES;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001112 } else if (num_planes == 3) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001113 dst_surface_type = YUV_SURFACE_3_PLANES;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001114 } else {
1115 ALOGE("%s: dst number of YUV planes is invalid dst format = 0x%x",
1116 __FUNCTION__, dst->format);
1117 return COPYBIT_FAILURE;
1118 }
1119 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001120 ALOGE("%s: Invalid dst surface format 0x%x", __FUNCTION__,
1121 dst->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001122 return COPYBIT_FAILURE;
1123 }
1124
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001125 if (ctx->blit_rgb_count == MAX_RGB_SURFACES ||
1126 ctx->blit_yuv_2_plane_count == MAX_YUV_2_PLANE_SURFACES ||
1127 ctx->blit_yuv_3_plane_count == MAX_YUV_2_PLANE_SURFACES ||
1128 ctx->blit_count == MAX_BLIT_OBJECT_COUNT ||
1129 ctx->dst_surface_type != dst_surface_type) {
1130 // we have reached the max. limits of our internal structures or
1131 // changed the target.
1132 // Draw the remaining surfaces. We need to do the finish here since
1133 // we need to free up the surface templates.
1134 finish_copybit(dev);
1135 }
1136
1137 ctx->dst_surface_type = dst_surface_type;
1138
1139 // Update the destination
Naseer Ahmed29a26812012-06-14 00:56:20 -07001140 copybit_image_t dst_image;
1141 dst_image.w = dst->w;
1142 dst_image.h = dst->h;
1143 dst_image.format = dst->format;
1144 dst_image.handle = dst->handle;
1145 // Check if we need a temp. copy for the destination. We'd need this the destination
1146 // width is not aligned to 32. This case occurs for YUV formats. RGB formats are
1147 // aligned to 32.
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001148 bool need_temp_dst = need_temp_buffer(dst);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001149 bufferInfo dst_info;
1150 populate_buffer_info(dst, dst_info);
1151 private_handle_t* dst_hnd = new private_handle_t(-1, 0, 0, 0, dst_info.format,
1152 dst_info.width, dst_info.height);
1153 if (dst_hnd == NULL) {
1154 ALOGE("%s: dst_hnd is null", __FUNCTION__);
1155 return COPYBIT_FAILURE;
1156 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001157 if (need_temp_dst) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001158 if (get_size(dst_info) != ctx->temp_dst_buffer.size) {
1159 free_temp_buffer(ctx->temp_dst_buffer);
1160 // Create a temp buffer and set that as the destination.
1161 if (COPYBIT_FAILURE == get_temp_buffer(dst_info, ctx->temp_dst_buffer)) {
1162 ALOGE("%s: get_temp_buffer(dst) failed", __FUNCTION__);
1163 delete_handle(dst_hnd);
1164 return COPYBIT_FAILURE;
1165 }
1166 }
1167 dst_hnd->fd = ctx->temp_dst_buffer.fd;
1168 dst_hnd->size = ctx->temp_dst_buffer.size;
1169 dst_hnd->flags = ctx->temp_dst_buffer.allocType;
1170 dst_hnd->base = (int)(ctx->temp_dst_buffer.base);
1171 dst_hnd->offset = ctx->temp_dst_buffer.offset;
1172 dst_hnd->gpuaddr = 0;
1173 dst_image.handle = dst_hnd;
1174 }
1175
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001176 status = set_image(ctx, ctx->dst[ctx->dst_surface_type], &dst_image,
1177 (eC2DFlags)flags, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001178 if(status) {
1179 ALOGE("%s: dst: set_image error", __FUNCTION__);
1180 delete_handle(dst_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001181 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001182 return COPYBIT_FAILURE;
1183 }
1184
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001185 // Update the source
1186 flags = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001187 if(is_supported_rgb_format(src->format) == COPYBIT_SUCCESS) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001188 src_surface_type = RGB_SURFACE;
1189 src_surface = ctx->blit_rgb_object[ctx->blit_rgb_count];
Naseer Ahmed29a26812012-06-14 00:56:20 -07001190 } else if (is_supported_yuv_format(src->format) == COPYBIT_SUCCESS) {
1191 int num_planes = get_num_planes(src->format);
1192 if (num_planes == 2) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001193 src_surface_type = YUV_SURFACE_2_PLANES;
1194 src_surface = ctx->blit_yuv_2_plane_object[ctx->blit_yuv_2_plane_count];
Naseer Ahmed29a26812012-06-14 00:56:20 -07001195 } else if (num_planes == 3) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001196 src_surface_type = YUV_SURFACE_3_PLANES;
1197 src_surface = ctx->blit_yuv_3_plane_object[ctx->blit_yuv_2_plane_count];
Naseer Ahmed29a26812012-06-14 00:56:20 -07001198 } else {
1199 ALOGE("%s: src number of YUV planes is invalid src format = 0x%x",
1200 __FUNCTION__, src->format);
1201 delete_handle(dst_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001202 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001203 return -EINVAL;
1204 }
1205 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001206 ALOGE("%s: Invalid source surface format 0x%x", __FUNCTION__,
1207 src->format);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001208 delete_handle(dst_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 -EINVAL;
1211 }
1212
1213 copybit_image_t src_image;
1214 src_image.w = src->w;
1215 src_image.h = src->h;
1216 src_image.format = src->format;
1217 src_image.handle = src->handle;
1218
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001219 bool need_temp_src = need_temp_buffer(src);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001220 bufferInfo src_info;
1221 populate_buffer_info(src, src_info);
1222 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 -08001223 src_info.width, src_info.height);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001224 if (NULL == src_hnd) {
1225 ALOGE("%s: src_hnd is null", __FUNCTION__);
1226 delete_handle(dst_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001227 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001228 return COPYBIT_FAILURE;
1229 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001230 if (need_temp_src) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001231 if (get_size(src_info) != ctx->temp_src_buffer.size) {
1232 free_temp_buffer(ctx->temp_src_buffer);
1233 // Create a temp buffer and set that as the destination.
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001234 if (COPYBIT_SUCCESS != get_temp_buffer(src_info,
1235 ctx->temp_src_buffer)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001236 ALOGE("%s: get_temp_buffer(src) 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 src_hnd->fd = ctx->temp_src_buffer.fd;
1244 src_hnd->size = ctx->temp_src_buffer.size;
1245 src_hnd->flags = ctx->temp_src_buffer.allocType;
1246 src_hnd->base = (int)(ctx->temp_src_buffer.base);
1247 src_hnd->offset = ctx->temp_src_buffer.offset;
1248 src_hnd->gpuaddr = 0;
1249 src_image.handle = src_hnd;
1250
1251 // Copy the source.
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001252 status = copy_image((private_handle_t *)src->handle, &src_image,
1253 CONVERT_TO_C2D_FORMAT);
1254 if (status == COPYBIT_FAILURE) {
1255 ALOGE("%s:copy_image failed in temp source",__FUNCTION__);
1256 delete_handle(dst_hnd);
1257 delete_handle(src_hnd);
1258 unmap_gpuaddr(ctx, mapped_dst_idx);
1259 return status;
1260 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001261
1262 // Flush the cache
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001263 IMemAlloc* memalloc = sAlloc->getAllocator(src_hnd->flags);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001264 if (memalloc->clean_buffer((void *)(src_hnd->base), src_hnd->size,
1265 src_hnd->offset, src_hnd->fd)) {
1266 ALOGE("%s: clean_buffer failed", __FUNCTION__);
1267 delete_handle(dst_hnd);
1268 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001269 unmap_gpuaddr(ctx, mapped_dst_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001270 return COPYBIT_FAILURE;
1271 }
1272 }
1273
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001274 flags |= (ctx->is_premultiplied_alpha) ? FLAGS_PREMULTIPLIED_ALPHA : 0;
1275 flags |= (ctx->dst_surface_type != RGB_SURFACE) ? FLAGS_YUV_DESTINATION : 0;
1276 status = set_image(ctx, src_surface.surface_id, &src_image,
1277 (eC2DFlags)flags, mapped_src_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001278 if(status) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001279 ALOGE("%s: set_image (src) error", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001280 delete_handle(dst_hnd);
1281 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001282 unmap_gpuaddr(ctx, mapped_dst_idx);
1283 unmap_gpuaddr(ctx, mapped_src_idx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001284 return COPYBIT_FAILURE;
1285 }
1286
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001287 src_surface.config_mask = C2D_NO_BILINEAR_BIT | C2D_NO_ANTIALIASING_BIT |
1288 ctx->config_mask;
1289 src_surface.global_alpha = ctx->src_global_alpha;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001290 if (enableBlend) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001291 if(src_surface.config_mask & C2D_GLOBAL_ALPHA_BIT) {
1292 src_surface.config_mask &= ~C2D_ALPHA_BLEND_NONE;
1293 if(!(src_surface.global_alpha)) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001294 // src alpha is zero
Naseer Ahmed29a26812012-06-14 00:56:20 -07001295 delete_handle(dst_hnd);
1296 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001297 unmap_gpuaddr(ctx, mapped_dst_idx);
1298 unmap_gpuaddr(ctx, mapped_src_idx);
1299 return COPYBIT_FAILURE;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001300 }
1301 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001302 int c2d_format = get_format(src->format);
1303 if(is_alpha(c2d_format))
1304 src_surface.config_mask &= ~C2D_ALPHA_BLEND_NONE;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001305 else
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001306 src_surface.config_mask |= C2D_ALPHA_BLEND_NONE;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001307 }
1308 } else {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001309 src_surface.config_mask |= C2D_ALPHA_BLEND_NONE;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001310 }
1311
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001312 if (src_surface_type == RGB_SURFACE) {
1313 ctx->blit_rgb_object[ctx->blit_rgb_count] = src_surface;
1314 ctx->blit_rgb_count++;
1315 } else if (src_surface_type == YUV_SURFACE_2_PLANES) {
1316 ctx->blit_yuv_2_plane_object[ctx->blit_yuv_2_plane_count] = src_surface;
1317 ctx->blit_yuv_2_plane_count++;
1318 } else {
1319 ctx->blit_yuv_3_plane_object[ctx->blit_yuv_3_plane_count] = src_surface;
1320 ctx->blit_yuv_3_plane_count++;
1321 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001322
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001323 struct copybit_rect_t clip;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001324 while ((status == 0) && region->next(region, &clip)) {
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001325 set_rects(ctx, &(src_surface), dst_rect, src_rect, &clip);
1326 if (ctx->blit_count == MAX_BLIT_OBJECT_COUNT) {
1327 ALOGW("Reached end of blit count");
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001328 finish_copybit(dev);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001329 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001330 ctx->blit_list[ctx->blit_count] = src_surface;
1331 ctx->blit_count++;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001332 }
1333
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001334 // Check if we need to perform an early draw-finish.
1335 flags |= (need_temp_dst || need_temp_src) ? FLAGS_TEMP_SRC_DST : 0;
1336 if (need_to_execute_draw(ctx, (eC2DFlags)flags))
1337 {
1338 finish_copybit(dev);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001339 }
1340
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001341 if (need_temp_dst) {
1342 // copy the temp. destination without the alignment to the actual
1343 // destination.
1344 status = copy_image(dst_hnd, dst, CONVERT_TO_ANDROID_FORMAT);
1345 if (status == COPYBIT_FAILURE) {
1346 ALOGE("%s:copy_image failed in temp Dest",__FUNCTION__);
1347 delete_handle(dst_hnd);
1348 delete_handle(src_hnd);
1349 unmap_gpuaddr(ctx, mapped_dst_idx);
1350 unmap_gpuaddr(ctx, mapped_src_idx);
1351 return status;
1352 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001353 // Invalidate the cache.
Naseer Ahmed01d3fd32012-07-14 21:08:13 -07001354 IMemAlloc* memalloc = sAlloc->getAllocator(dst_hnd->flags);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001355 memalloc->clean_buffer((void *)(dst_hnd->base), dst_hnd->size,
1356 dst_hnd->offset, dst_hnd->fd);
1357 }
1358 delete_handle(dst_hnd);
1359 delete_handle(src_hnd);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001360
1361 ctx->is_premultiplied_alpha = false;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001362 ctx->fb_width = 0;
1363 ctx->fb_height = 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001364 ctx->config_mask = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001365 return status;
1366}
1367
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001368
Naseer Ahmed29a26812012-06-14 00:56:20 -07001369static int stretch_copybit(
1370 struct copybit_device_t *dev,
1371 struct copybit_image_t const *dst,
1372 struct copybit_image_t const *src,
1373 struct copybit_rect_t const *dst_rect,
1374 struct copybit_rect_t const *src_rect,
1375 struct copybit_region_t const *region)
1376{
1377 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001378 int status = COPYBIT_SUCCESS;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001379 bool needsBlending = (ctx->src_global_alpha != 0);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001380 pthread_mutex_lock(&ctx->wait_cleanup_lock);
1381 status = stretch_copybit_internal(dev, dst, src, dst_rect, src_rect,
Naseer Ahmed29a26812012-06-14 00:56:20 -07001382 region, needsBlending);
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001383 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
1384 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001385}
1386
1387/** Perform a blit type operation */
1388static int blit_copybit(
1389 struct copybit_device_t *dev,
1390 struct copybit_image_t const *dst,
1391 struct copybit_image_t const *src,
1392 struct copybit_region_t const *region)
1393{
1394 struct copybit_rect_t dr = { 0, 0, dst->w, dst->h };
1395 struct copybit_rect_t sr = { 0, 0, src->w, src->h };
1396 return stretch_copybit_internal(dev, dst, src, &dr, &sr, region, false);
1397}
1398
1399/*****************************************************************************/
1400
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001401static void clean_up(copybit_context_t* ctx)
1402{
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001403 void* ret;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001404 if (!ctx)
1405 return;
1406
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001407 // stop the wait_cleanup_thread
1408 pthread_mutex_lock(&ctx->wait_cleanup_lock);
1409 ctx->stop_thread = true;
1410 // Signal waiting thread
1411 pthread_cond_signal(&ctx->wait_cleanup_cond);
1412 pthread_mutex_unlock(&ctx->wait_cleanup_lock);
1413 // waits for the cleanup thread to exit
1414 pthread_join(ctx->wait_thread_id, &ret);
1415 pthread_mutex_destroy(&ctx->wait_cleanup_lock);
1416 pthread_cond_destroy (&ctx->wait_cleanup_cond);
1417
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001418 for (int i = 0; i < NUM_SURFACE_TYPES; i++) {
1419 if (ctx->dst[i])
1420 LINK_c2dDestroySurface(ctx->dst[i]);
1421 }
1422
1423 for (int i = 0; i < MAX_RGB_SURFACES; i++) {
1424 if (ctx->blit_rgb_object[i].surface_id)
1425 LINK_c2dDestroySurface(ctx->blit_rgb_object[i].surface_id);
1426 }
1427
1428 for (int i = 0; i < MAX_YUV_2_PLANE_SURFACES; i++) {
1429 if (ctx->blit_yuv_2_plane_object[i].surface_id)
1430 LINK_c2dDestroySurface(ctx->blit_yuv_2_plane_object[i].surface_id);
1431 }
1432
1433 for (int i = 0; i < MAX_YUV_3_PLANE_SURFACES; i++) {
1434 if (ctx->blit_yuv_3_plane_object[i].surface_id)
1435 LINK_c2dDestroySurface(ctx->blit_yuv_3_plane_object[i].surface_id);
1436 }
1437
1438 if (ctx->libc2d2) {
1439 ::dlclose(ctx->libc2d2);
1440 ALOGV("dlclose(libc2d2)");
1441 }
1442
1443 free(ctx);
1444}
1445
Naseer Ahmed29a26812012-06-14 00:56:20 -07001446/** Close the copybit device */
1447static int close_copybit(struct hw_device_t *dev)
1448{
1449 struct copybit_context_t* ctx = (struct copybit_context_t*)dev;
1450 if (ctx) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001451 free_temp_buffer(ctx->temp_src_buffer);
1452 free_temp_buffer(ctx->temp_dst_buffer);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001453 }
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001454 clean_up(ctx);
Naseer Ahmed29a26812012-06-14 00:56:20 -07001455 return 0;
1456}
1457
1458/** Open a new instance of a copybit device using name */
1459static int open_copybit(const struct hw_module_t* module, const char* name,
1460 struct hw_device_t** device)
1461{
1462 int status = COPYBIT_SUCCESS;
1463 C2D_RGB_SURFACE_DEF surfDefinition = {0};
1464 C2D_YUV_SURFACE_DEF yuvSurfaceDef = {0} ;
1465 struct copybit_context_t *ctx;
1466 char fbName[64];
1467
1468 ctx = (struct copybit_context_t *)malloc(sizeof(struct copybit_context_t));
1469 if(!ctx) {
1470 ALOGE("%s: malloc failed", __FUNCTION__);
1471 return COPYBIT_FAILURE;
1472 }
1473
1474 /* initialize drawstate */
1475 memset(ctx, 0, sizeof(*ctx));
Naseer Ahmed29a26812012-06-14 00:56:20 -07001476 ctx->libc2d2 = ::dlopen("libC2D2.so", RTLD_NOW);
1477 if (!ctx->libc2d2) {
1478 ALOGE("FATAL ERROR: could not dlopen libc2d2.so: %s", dlerror());
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001479 clean_up(ctx);
1480 status = COPYBIT_FAILURE;
1481 *device = NULL;
1482 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001483 }
1484 *(void **)&LINK_c2dCreateSurface = ::dlsym(ctx->libc2d2,
1485 "c2dCreateSurface");
1486 *(void **)&LINK_c2dUpdateSurface = ::dlsym(ctx->libc2d2,
1487 "c2dUpdateSurface");
1488 *(void **)&LINK_c2dReadSurface = ::dlsym(ctx->libc2d2,
1489 "c2dReadSurface");
1490 *(void **)&LINK_c2dDraw = ::dlsym(ctx->libc2d2, "c2dDraw");
1491 *(void **)&LINK_c2dFlush = ::dlsym(ctx->libc2d2, "c2dFlush");
1492 *(void **)&LINK_c2dFinish = ::dlsym(ctx->libc2d2, "c2dFinish");
1493 *(void **)&LINK_c2dWaitTimestamp = ::dlsym(ctx->libc2d2,
1494 "c2dWaitTimestamp");
1495 *(void **)&LINK_c2dDestroySurface = ::dlsym(ctx->libc2d2,
1496 "c2dDestroySurface");
1497 *(void **)&LINK_c2dMapAddr = ::dlsym(ctx->libc2d2,
1498 "c2dMapAddr");
1499 *(void **)&LINK_c2dUnMapAddr = ::dlsym(ctx->libc2d2,
1500 "c2dUnMapAddr");
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001501 *(void **)&LINK_c2dGetDriverCapabilities = ::dlsym(ctx->libc2d2,
1502 "c2dGetDriverCapabilities");
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001503 *(void **)&LINK_c2dCreateFenceFD = ::dlsym(ctx->libc2d2,
1504 "c2dCreateFenceFD");
Naseer Ahmed183939d2013-03-14 20:44:17 -04001505 *(void **)&LINK_c2dFillSurface = ::dlsym(ctx->libc2d2,
1506 "c2dFillSurface");
Naseer Ahmed29a26812012-06-14 00:56:20 -07001507
1508 if (!LINK_c2dCreateSurface || !LINK_c2dUpdateSurface || !LINK_c2dReadSurface
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001509 || !LINK_c2dDraw || !LINK_c2dFlush || !LINK_c2dWaitTimestamp ||
1510 !LINK_c2dFinish || !LINK_c2dDestroySurface ||
Naseer Ahmed183939d2013-03-14 20:44:17 -04001511 !LINK_c2dGetDriverCapabilities || !LINK_c2dCreateFenceFD ||
1512 !LINK_c2dFillSurface) {
Naseer Ahmed29a26812012-06-14 00:56:20 -07001513 ALOGE("%s: dlsym ERROR", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001514 clean_up(ctx);
1515 status = COPYBIT_FAILURE;
1516 *device = NULL;
1517 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001518 }
1519
1520 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
1521 ctx->device.common.version = 1;
1522 ctx->device.common.module = (hw_module_t*)(module);
1523 ctx->device.common.close = close_copybit;
1524 ctx->device.set_parameter = set_parameter_copybit;
1525 ctx->device.get = get;
1526 ctx->device.blit = blit_copybit;
1527 ctx->device.stretch = stretch_copybit;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001528 ctx->device.finish = finish_copybit;
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001529 ctx->device.flush_get_fence = flush_get_fence_copybit;
Naseer Ahmed183939d2013-03-14 20:44:17 -04001530 ctx->device.clear = clear_copybit;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001531
1532 /* Create RGB Surface */
1533 surfDefinition.buffer = (void*)0xdddddddd;
1534 surfDefinition.phys = (void*)0xdddddddd;
1535 surfDefinition.stride = 1 * 4;
1536 surfDefinition.width = 1;
1537 surfDefinition.height = 1;
1538 surfDefinition.format = C2D_COLOR_FORMAT_8888_ARGB;
1539 if (LINK_c2dCreateSurface(&(ctx->dst[RGB_SURFACE]), C2D_TARGET | C2D_SOURCE,
1540 (C2D_SURFACE_TYPE)(C2D_SURFACE_RGB_HOST |
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001541 C2D_SURFACE_WITH_PHYS |
1542 C2D_SURFACE_WITH_PHYS_DUMMY ),
1543 &surfDefinition)) {
1544 ALOGE("%s: create ctx->dst_surface[RGB_SURFACE] failed", __FUNCTION__);
1545 ctx->dst[RGB_SURFACE] = 0;
1546 clean_up(ctx);
1547 status = COPYBIT_FAILURE;
1548 *device = NULL;
1549 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001550 }
1551
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001552 unsigned int surface_id = 0;
1553 for (int i = 0; i < MAX_RGB_SURFACES; i++)
1554 {
1555 if (LINK_c2dCreateSurface(&surface_id, C2D_TARGET | C2D_SOURCE,
Naseer Ahmed29a26812012-06-14 00:56:20 -07001556 (C2D_SURFACE_TYPE)(C2D_SURFACE_RGB_HOST |
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001557 C2D_SURFACE_WITH_PHYS |
1558 C2D_SURFACE_WITH_PHYS_DUMMY ),
1559 &surfDefinition)) {
1560 ALOGE("%s: create RGB source surface %d failed", __FUNCTION__, i);
1561 ctx->blit_rgb_object[i].surface_id = 0;
1562 status = COPYBIT_FAILURE;
1563 break;
1564 } else {
1565 ctx->blit_rgb_object[i].surface_id = surface_id;
1566 ALOGW("%s i = %d surface_id=%d", __FUNCTION__, i,
1567 ctx->blit_rgb_object[i].surface_id);
1568 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001569 }
1570
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001571 if (status == COPYBIT_FAILURE) {
1572 clean_up(ctx);
1573 status = COPYBIT_FAILURE;
1574 *device = NULL;
1575 return status;
1576 }
Naseer Ahmed29a26812012-06-14 00:56:20 -07001577
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001578 // Create 2 plane YUV surfaces
1579 yuvSurfaceDef.format = C2D_COLOR_FORMAT_420_NV12;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001580 yuvSurfaceDef.width = 4;
1581 yuvSurfaceDef.height = 4;
1582 yuvSurfaceDef.plane0 = (void*)0xaaaaaaaa;
1583 yuvSurfaceDef.phys0 = (void*) 0xaaaaaaaa;
1584 yuvSurfaceDef.stride0 = 4;
1585
1586 yuvSurfaceDef.plane1 = (void*)0xaaaaaaaa;
1587 yuvSurfaceDef.phys1 = (void*) 0xaaaaaaaa;
1588 yuvSurfaceDef.stride1 = 4;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001589 if (LINK_c2dCreateSurface(&(ctx->dst[YUV_SURFACE_2_PLANES]),
1590 C2D_TARGET | C2D_SOURCE,
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001591 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1592 C2D_SURFACE_WITH_PHYS |
1593 C2D_SURFACE_WITH_PHYS_DUMMY),
Naseer Ahmed29a26812012-06-14 00:56:20 -07001594 &yuvSurfaceDef)) {
1595 ALOGE("%s: create ctx->dst[YUV_SURFACE_2_PLANES] failed", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001596 ctx->dst[YUV_SURFACE_2_PLANES] = 0;
1597 clean_up(ctx);
1598 status = COPYBIT_FAILURE;
1599 *device = NULL;
1600 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001601 }
1602
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001603 for (int i=0; i < MAX_YUV_2_PLANE_SURFACES; i++)
1604 {
1605 if (LINK_c2dCreateSurface(&surface_id, C2D_TARGET | C2D_SOURCE,
1606 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1607 C2D_SURFACE_WITH_PHYS |
1608 C2D_SURFACE_WITH_PHYS_DUMMY ),
1609 &yuvSurfaceDef)) {
1610 ALOGE("%s: create YUV source %d failed", __FUNCTION__, i);
1611 ctx->blit_yuv_2_plane_object[i].surface_id = 0;
1612 status = COPYBIT_FAILURE;
1613 break;
1614 } else {
1615 ctx->blit_yuv_2_plane_object[i].surface_id = surface_id;
1616 ALOGW("%s: 2 Plane YUV i=%d surface_id=%d", __FUNCTION__, i,
1617 ctx->blit_yuv_2_plane_object[i].surface_id);
1618 }
1619 }
1620
1621 if (status == COPYBIT_FAILURE) {
1622 clean_up(ctx);
1623 status = COPYBIT_FAILURE;
1624 *device = NULL;
1625 return status;
1626 }
1627
1628 // Create YUV 3 plane surfaces
Naseer Ahmed29a26812012-06-14 00:56:20 -07001629 yuvSurfaceDef.format = C2D_COLOR_FORMAT_420_YV12;
1630 yuvSurfaceDef.plane2 = (void*)0xaaaaaaaa;
1631 yuvSurfaceDef.phys2 = (void*) 0xaaaaaaaa;
1632 yuvSurfaceDef.stride2 = 4;
1633
Naseer Ahmed29a26812012-06-14 00:56:20 -07001634 if (LINK_c2dCreateSurface(&(ctx->dst[YUV_SURFACE_3_PLANES]),
1635 C2D_TARGET | C2D_SOURCE,
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001636 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1637 C2D_SURFACE_WITH_PHYS |
1638 C2D_SURFACE_WITH_PHYS_DUMMY),
Naseer Ahmed29a26812012-06-14 00:56:20 -07001639 &yuvSurfaceDef)) {
1640 ALOGE("%s: create ctx->dst[YUV_SURFACE_3_PLANES] failed", __FUNCTION__);
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001641 ctx->dst[YUV_SURFACE_3_PLANES] = 0;
1642 clean_up(ctx);
1643 status = COPYBIT_FAILURE;
1644 *device = NULL;
1645 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001646 }
1647
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001648 for (int i=0; i < MAX_YUV_3_PLANE_SURFACES; i++)
1649 {
1650 if (LINK_c2dCreateSurface(&(surface_id),
1651 C2D_TARGET | C2D_SOURCE,
1652 (C2D_SURFACE_TYPE)(C2D_SURFACE_YUV_HOST |
1653 C2D_SURFACE_WITH_PHYS |
1654 C2D_SURFACE_WITH_PHYS_DUMMY),
1655 &yuvSurfaceDef)) {
1656 ALOGE("%s: create 3 plane YUV surface %d failed", __FUNCTION__, i);
1657 ctx->blit_yuv_3_plane_object[i].surface_id = 0;
1658 status = COPYBIT_FAILURE;
1659 break;
1660 } else {
1661 ctx->blit_yuv_3_plane_object[i].surface_id = surface_id;
1662 ALOGW("%s: 3 Plane YUV i=%d surface_id=%d", __FUNCTION__, i,
1663 ctx->blit_yuv_3_plane_object[i].surface_id);
1664 }
1665 }
1666
1667 if (status == COPYBIT_FAILURE) {
1668 clean_up(ctx);
1669 status = COPYBIT_FAILURE;
1670 *device = NULL;
1671 return status;
1672 }
1673
1674 if (LINK_c2dGetDriverCapabilities(&(ctx->c2d_driver_info))) {
1675 ALOGE("%s: LINK_c2dGetDriverCapabilities failed", __FUNCTION__);
1676 clean_up(ctx);
1677 status = COPYBIT_FAILURE;
1678 *device = NULL;
1679 return status;
1680 }
1681 // Initialize context variables.
1682 ctx->trg_transform = C2D_TARGET_ROTATE_0;
1683
Naseer Ahmed29a26812012-06-14 00:56:20 -07001684 ctx->temp_src_buffer.fd = -1;
1685 ctx->temp_src_buffer.base = 0;
1686 ctx->temp_src_buffer.size = 0;
1687
1688 ctx->temp_dst_buffer.fd = -1;
1689 ctx->temp_dst_buffer.base = 0;
1690 ctx->temp_dst_buffer.size = 0;
1691
1692 ctx->fb_width = 0;
1693 ctx->fb_height = 0;
Arun Kumar K.R6b353bd2012-11-27 17:18:45 -08001694
1695 ctx->blit_rgb_count = 0;
1696 ctx->blit_yuv_2_plane_count = 0;
1697 ctx->blit_yuv_3_plane_count = 0;
1698 ctx->blit_count = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001699
Arun Kumar K.Reb128aa2012-12-18 12:38:28 -08001700 ctx->wait_timestamp = false;
1701 ctx->stop_thread = false;
1702 pthread_mutex_init(&(ctx->wait_cleanup_lock), NULL);
1703 pthread_cond_init(&(ctx->wait_cleanup_cond), NULL);
1704 /* Start the wait thread */
1705 pthread_attr_t attr;
1706 pthread_attr_init(&attr);
1707 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1708
1709 pthread_create(&ctx->wait_thread_id, &attr, &c2d_wait_loop,
1710 (void *)ctx);
1711 pthread_attr_destroy(&attr);
1712
Naseer Ahmed29a26812012-06-14 00:56:20 -07001713 *device = &ctx->device.common;
1714 return status;
Naseer Ahmed29a26812012-06-14 00:56:20 -07001715}